Webserver+3d print
cyclone_tcp/drivers/lan8742.c
- Committer:
- Sergunb
- Date:
- 2017-02-04
- Revision:
- 0:8918a71cdbe9
File content as of revision 0:8918a71cdbe9:
/** * @file lan8742.c * @brief LAN8742 Ethernet PHY transceiver * * @section License * * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. * * This file is part of CycloneTCP Open. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @author Oryx Embedded SARL (www.oryx-embedded.com) * @version 1.7.6 **/ //Switch to the appropriate trace level #define TRACE_LEVEL NIC_TRACE_LEVEL //Dependencies #include "core/net.h" #include "drivers/lan8742.h" #include "debug.h" /** * @brief LAN8742 Ethernet PHY driver **/ const PhyDriver lan8742PhyDriver = { lan8742Init, lan8742Tick, lan8742EnableIrq, lan8742DisableIrq, lan8742EventHandler, }; /** * @brief LAN8742 PHY transceiver initialization * @param[in] interface Underlying network interface * @return Error code **/ error_t lan8742Init(NetInterface *interface) { //Debug message TRACE_INFO("Initializing LAN8742...\r\n"); //Initialize external interrupt line driver if(interface->extIntDriver != NULL) interface->extIntDriver->init(); //Reset PHY transceiver (soft reset) lan8742WritePhyReg(interface, LAN8742_PHY_REG_BMCR, BMCR_RESET); //Wait for the reset to complete while(lan8742ReadPhyReg(interface, LAN8742_PHY_REG_BMCR) & BMCR_RESET); //Restore default auto-negotiation advertisement parameters lan8742WritePhyReg(interface, LAN8742_PHY_REG_ANAR, ANAR_100BTX_FD | ANAR_100BTX | ANAR_10BT_FD | ANAR_10BT | ANAR_SELECTOR0); //Enable auto-negotiation lan8742WritePhyReg(interface, LAN8742_PHY_REG_BMCR, BMCR_AN_EN); //Dump PHY registers for debugging purpose lan8742DumpPhyReg(interface); //The PHY will generate interrupts when link status changes are detected lan8742WritePhyReg(interface, LAN8742_PHY_REG_IMR, IMR_AN_COMPLETE | IMR_LINK_DOWN); //Force the TCP/IP stack to poll the link state at startup interface->phyEvent = TRUE; //Notify the TCP/IP stack of the event osSetEvent(&netEvent); //Successful initialization return NO_ERROR; } /** * @brief LAN8742 timer handler * @param[in] interface Underlying network interface **/ void lan8742Tick(NetInterface *interface) { uint16_t value; bool_t linkState; //No external interrupt line driver? if(interface->extIntDriver == NULL) { //Read basic status register value = lan8742ReadPhyReg(interface, LAN8742_PHY_REG_BMSR); //Retrieve current link state linkState = (value & BMSR_LINK_STATUS) ? TRUE : FALSE; //Link up event? if(linkState && !interface->linkState) { //Set event flag interface->phyEvent = TRUE; //Notify the TCP/IP stack of the event osSetEvent(&netEvent); } //Link down event? else if(!linkState && interface->linkState) { //Set event flag interface->phyEvent = TRUE; //Notify the TCP/IP stack of the event osSetEvent(&netEvent); } } } /** * @brief Enable interrupts * @param[in] interface Underlying network interface **/ void lan8742EnableIrq(NetInterface *interface) { //Enable PHY transceiver interrupts if(interface->extIntDriver != NULL) interface->extIntDriver->enableIrq(); } /** * @brief Disable interrupts * @param[in] interface Underlying network interface **/ void lan8742DisableIrq(NetInterface *interface) { //Disable PHY transceiver interrupts if(interface->extIntDriver != NULL) interface->extIntDriver->disableIrq(); } /** * @brief LAN8742 event handler * @param[in] interface Underlying network interface **/ void lan8742EventHandler(NetInterface *interface) { uint16_t value; //Read status register to acknowledge the interrupt value = lan8742ReadPhyReg(interface, LAN8742_PHY_REG_ISR); //Link status change? if(value & (IMR_AN_COMPLETE | IMR_LINK_DOWN)) { //Any link failure condition is latched in the BMSR register... Reading //the register twice will always return the actual link status value = lan8742ReadPhyReg(interface, LAN8742_PHY_REG_BMSR); value = lan8742ReadPhyReg(interface, LAN8742_PHY_REG_BMSR); //Link is up? if(value & BMSR_LINK_STATUS) { //Read PHY special control/status register value = lan8742ReadPhyReg(interface, LAN8742_PHY_REG_PSCSR); //Check current operation mode switch(value & PSCSR_HCDSPEED_MASK) { //10BASE-T case PSCSR_HCDSPEED_10BT: interface->linkSpeed = NIC_LINK_SPEED_10MBPS; interface->duplexMode = NIC_HALF_DUPLEX_MODE; break; //10BASE-T full-duplex case PSCSR_HCDSPEED_10BT_FD: interface->linkSpeed = NIC_LINK_SPEED_10MBPS; interface->duplexMode = NIC_FULL_DUPLEX_MODE; break; //100BASE-TX case PSCSR_HCDSPEED_100BTX: interface->linkSpeed = NIC_LINK_SPEED_100MBPS; interface->duplexMode = NIC_HALF_DUPLEX_MODE; break; //100BASE-TX full-duplex case PSCSR_HCDSPEED_100BTX_FD: interface->linkSpeed = NIC_LINK_SPEED_100MBPS; interface->duplexMode = NIC_FULL_DUPLEX_MODE; break; //Unknown operation mode default: //Debug message TRACE_WARNING("Invalid Duplex mode\r\n"); break; } //Update link state interface->linkState = TRUE; //Adjust MAC configuration parameters for proper operation interface->nicDriver->updateMacConfig(interface); } else { //Update link state interface->linkState = FALSE; } //Process link state change event nicNotifyLinkChange(interface); } } /** * @brief Write PHY register * @param[in] interface Underlying network interface * @param[in] address PHY register address * @param[in] data Register value **/ void lan8742WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data) { uint8_t phyAddr; //Get the address of the PHY transceiver if(interface->phyAddr < 32) phyAddr = interface->phyAddr; else phyAddr = LAN8742_PHY_ADDR; //Write the specified PHY register interface->nicDriver->writePhyReg(phyAddr, address, data); } /** * @brief Read PHY register * @param[in] interface Underlying network interface * @param[in] address PHY register address * @return Register value **/ uint16_t lan8742ReadPhyReg(NetInterface *interface, uint8_t address) { uint8_t phyAddr; //Get the address of the PHY transceiver if(interface->phyAddr < 32) phyAddr = interface->phyAddr; else phyAddr = LAN8742_PHY_ADDR; //Read the specified PHY register return interface->nicDriver->readPhyReg(phyAddr, address); } /** * @brief Dump PHY registers for debugging purpose * @param[in] interface Underlying network interface **/ void lan8742DumpPhyReg(NetInterface *interface) { uint8_t i; //Loop through PHY registers for(i = 0; i < 32; i++) { //Display current PHY register TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, lan8742ReadPhyReg(interface, i)); } //Terminate with a line feed TRACE_DEBUG("\r\n"); }