/* ================================================================== >>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<< ------------------------------------------------------------------ Copyright (c) 2019-2023 by Lattice Semiconductor Corporation ALL RIGHTS RESERVED ------------------------------------------------------------------ IMPORTANT: THIS FILE IS USED BY OR GENERATED BY the LATTICE PROPELâ„¢ DEVELOPMENT SUITE, WHICH INCLUDES PROPEL BUILDER AND PROPEL SDK. Lattice grants permission to use this code pursuant to the terms of the Lattice Propel License Agreement. DISCLAIMER: LATTICE MAKES NO WARRANTIES ON THIS FILE OR ITS CONTENTS, WHETHER EXPRESSED, IMPLIED, STATUTORY, OR IN ANY PROVISION OF THE LATTICE PROPEL LICENSE AGREEMENT OR COMMUNICATION WITH LICENSEE, AND LATTICE SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. LATTICE DOES NOT WARRANT THAT THE FUNCTIONS CONTAINED HEREIN WILL MEET LICENSEE 'S REQUIREMENTS, OR THAT LICENSEE' S OPERATION OF ANY DEVICE, SOFTWARE OR SYSTEM USING THIS FILE OR ITS CONTENTS WILL BE UNINTERRUPTED OR ERROR FREE, OR THAT DEFECTS HEREIN WILL BE CORRECTED. LICENSEE ASSUMES RESPONSIBILITY FOR SELECTION OF MATERIALS TO ACHIEVE ITS INTENDED RESULTS, AND FOR THE PROPER INSTALLATION, USE, AND RESULTS OBTAINED THEREFROM. LICENSEE ASSUMES THE ENTIRE RISK OF THE FILE AND ITS CONTENTS PROVING DEFECTIVE OR FAILING TO PERFORM PROPERLY AND IN SUCH EVENT, LICENSEE SHALL ASSUME THE ENTIRE COST AND RISK OF ANY REPAIR, SERVICE, CORRECTION, OR ANY OTHER LIABILITIES OR DAMAGES CAUSED BY OR ASSOCIATED WITH THE SOFTWARE.IN NO EVENT SHALL LATTICE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS FILE OR ITS CONTENTS, EVEN IF LATTICE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. LATTICE 'S SOLE LIABILITY, AND LICENSEE' S SOLE REMEDY, IS SET FORTH ABOVE. LATTICE DOES NOT WARRANT OR REPRESENT THAT THIS FILE, ITS CONTENTS OR USE THEREOF DOES NOT INFRINGE ON THIRD PARTIES' INTELLECTUAL PROPERTY RIGHTS, INCLUDING ANY PATENT. IT IS THE USER' S RESPONSIBILITY TO VERIFY THE USER SOFTWARE DESIGN FOR CONSISTENCY AND FUNCTIONALITY THROUGH THE USE OF FORMAL SOFTWARE VALIDATION METHODS. ------------------------------------------------------------------ ================================================================== */ #include "ethernet.h" /** * @brief This function is used to configure to receive all address frame and enabling * the tsemac at speed of 10/100/1000 Mbps. * @param : handle : handle for the tsemac. * @return : status for initialization */ unsigned char ethernet_init(tsemac_handle_t *handle) { unsigned char status; if(handle->adr != ZERO) { tsemac_reg_type_t *tsemac = (tsemac_reg_type_t *) (handle->adr); tsemac->max_packet_size = MAX_PACKET_SIZE; //configure maximum packet size register tsemac->ipg = IPG_TIME; tsemac->tx_rx_ctrl |= handle->tx_rx_ctrl_var; //configure Rx MAC to receive multicast and broadcast frame receive ethernet_set_speed(handle); status = SUCCESS; } else { status = FAILURE; } return status; } /** * @brief This function is used to transmit the frame by copying src_packet buffer into dest_packet * and same packet is transmit to tsemac. * @param : handle : handle for the tsemac. * src_packet : buffer containing the packet to transmit to tsemac. * dest_packet :src_packet to copy in dest_packet for user. * @return : void */ void ethernet_packet_handle(tsemac_handle_t *handle,unsigned int *src_packet,unsigned int *dest_packet) { int count; for(count=ZERO;countframe_length;count++) { dest_packet[count] = src_packet[count]; } } /** * @brief This function is used to set the mac address for tsemac. * @param: handle : handle for the tsemac. * @return: return the status of setting the MAC address. */ unsigned char ethernet_set_mac_address(tsemac_handle_t *handle) { unsigned char status; if((handle->adr != ZERO) && (handle->mac_upper != ZERO) && (handle->mac_lower != ZERO)) { tsemac_reg_type_t *tsemac = (tsemac_reg_type_t *) (handle->adr); tsemac->mac_addr0 = handle->mac_upper; //storing the MAC address for first 4 byte in register MAC address0 tsemac->mac_addr1 = handle->mac_lower; //storing the MAC address for last 2 byte in register MAC address1 status = SUCCESS; } else { status = FAILURE; } return status; } /** * @brief This function is used to read the mac address for tsemac. * @param: handle : handle for the tsemac. * @retrun: return the status of MAC address read. */ unsigned char ethernet_get_mac_address(tsemac_handle_t *handle) { unsigned char status; if(handle->adr != ZERO) { tsemac_reg_type_t *tsemac = (tsemac_reg_type_t *) (handle->adr); handle->mac_upper = tsemac->mac_addr0; //read first 4 byte of MAC address from address word 0 register handle->mac_lower = tsemac->mac_addr1; //read last 2 byte of MAC address from address word 1 register status = SUCCESS; } else { status = FAILURE; } return status; } /** * @brief This function is used to set for multicast address for tsemac. * @param: handle : handle for the tsemac. * @retrun: return the status of setting the multicast address. */ unsigned char ethernet_set_multicast_address(tsemac_handle_t *handle) { unsigned char status; if((handle->adr != ZERO) && (handle->multicast_upper != ZERO) && (handle->multicast_lower != ZERO)) { tsemac_reg_type_t *tsemac = (tsemac_reg_type_t *) (handle->adr); tsemac->multi_cast0 = handle->multicast_upper; //storing the first 4 byte of the 64 bit hash in mutlicast table word 0 register tsemac->multi_cast1 = handle->multicast_lower; //storing the last 4 byte of the 64 bit hash in mutlicast table word 1 register status = SUCCESS; } else { status = FAILURE; } return status; } /** * @brief This function is used to get for multicast address. * @param: handle : handle for the tsemac. * @retrun: return the status of multicast address read. */ unsigned char ethernet_get_multicast_address(tsemac_handle_t *handle) { unsigned char status; if(handle->adr != ZERO) { tsemac_reg_type_t *tsemac = (tsemac_reg_type_t *) (handle->adr); handle->multicast_upper = tsemac->multi_cast0; //read first 4 byte of the 64 bit hash from multicast table word 0 register handle->multicast_lower = tsemac->multi_cast1; //read last 4 byte of the 64 bit hash from multicast table word 1 register status = SUCCESS; } else { status = FAILURE; } return status; } /** * @brief This function is used to set the speed for tsemac at 10/100/1000 Mbps. * @param : handle : handle for the tsemac. * @return : return the success or failure status based on condition. */ unsigned char ethernet_set_speed(tsemac_handle_t *handle) { unsigned char status; if(handle->adr != ZERO) { tsemac_reg_type_t *tsemac = (tsemac_reg_type_t *) (handle->adr); if(handle->speed_mode == one_g_mode) { tsemac->tx_rx_ctrl &= (~(ONE << SET_FULL_DUPLEX_MODE)); //set full duplex mode tsemac->mode_reg |= (ONE << SPEED_1G); //set into 1G mode } else if(handle->speed_mode == fast_half_duplex_mode) { tsemac->tx_rx_ctrl |= (ONE << SET_HALF_DUPLEX_MODE); //set half duplex mode tsemac->mode_reg &= (~(ONE << SPEED_10_OR_100_MBPS)); //set into 10 or 100 mbps } else { tsemac->tx_rx_ctrl &= (~(ONE << SET_FULL_DUPLEX_MODE)); //set full duplex mode tsemac->mode_reg &= (~(ONE << SPEED_10_OR_100_MBPS)); //set into 10 or 100 mbps } tsemac->mode_reg |= handle->enable_tx_mac | handle->enable_rx_mac; //enable transmit and receive mode status = SUCCESS; } else { status = FAILURE; } return status; } /** * @brief This function is used to read the statistics counter register. * @param: handle : handle for the tsemac. * stat_counter_reg_enum : enum for statistics counter register. * @return: total_count : return the total count after reading the statistics counter register. * Note : This API could not be tested due to IP limitation for Release 2023.2 */ unsigned int ethernet_statistics_counter_register_read(tsemac_handle_t *handle,unsigned char stat_counter_reg_enum) { unsigned int total_count; volatile unsigned int *counter_reg0 = (unsigned int *)(handle->adr + (stat_counter_reg_enum*FOUR)); volatile unsigned int *counter_reg1 = (unsigned int *)(handle->adr + (stat_counter_reg_enum+ONE)*FOUR); total_count = (*counter_reg1 << SIXTEEN_BIT) | *counter_reg0; return total_count; } /** * @brief This function is used to read the Transmit and receive status register. * @param: handle : handle for the tsemac. * @return : return status register value after reading. */ unsigned int ethernet_tx_rx_status_reg_read(tsemac_handle_t *handle) { tsemac_reg_type_t *tsemac = (tsemac_reg_type_t *) (handle->adr); return tsemac->tx_rx_status; } /** * @brief This function is used to read the mode register. * @param: handle : handle for the tsemac. * @return : return mode register value after reading. */ unsigned int ethernet_mode_reg_read(tsemac_handle_t *handle) { tsemac_reg_type_t *tsemac = (tsemac_reg_type_t *) (handle->adr); return tsemac->mode_reg; } /** * @brief This function is used to set the particular bit in Transmit and Receive control register. * @param: handle : handle for the tsemac. * @return : return the success or failure status based on condition. */ unsigned char ethernet_tx_rx_control_reg_set(tsemac_handle_t *handle,unsigned char bit_pos) { unsigned char status; if(handle->adr != ZERO) { tsemac_reg_type_t *tsemac = (tsemac_reg_type_t *) (handle->adr); tsemac->tx_rx_ctrl |= ONE << bit_pos; status = SUCCESS; } else { status = FAILURE; } return status; }