Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file rtl8211.c
Sergunb 0:8918a71cdbe9 3 * @brief RTL8211 Ethernet PHY transceiver
Sergunb 0:8918a71cdbe9 4 *
Sergunb 0:8918a71cdbe9 5 * @section License
Sergunb 0:8918a71cdbe9 6 *
Sergunb 0:8918a71cdbe9 7 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
Sergunb 0:8918a71cdbe9 8 *
Sergunb 0:8918a71cdbe9 9 * This file is part of CycloneTCP Open.
Sergunb 0:8918a71cdbe9 10 *
Sergunb 0:8918a71cdbe9 11 * This program is free software; you can redistribute it and/or
Sergunb 0:8918a71cdbe9 12 * modify it under the terms of the GNU General Public License
Sergunb 0:8918a71cdbe9 13 * as published by the Free Software Foundation; either version 2
Sergunb 0:8918a71cdbe9 14 * of the License, or (at your option) any later version.
Sergunb 0:8918a71cdbe9 15 *
Sergunb 0:8918a71cdbe9 16 * This program is distributed in the hope that it will be useful,
Sergunb 0:8918a71cdbe9 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Sergunb 0:8918a71cdbe9 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Sergunb 0:8918a71cdbe9 19 * GNU General Public License for more details.
Sergunb 0:8918a71cdbe9 20 *
Sergunb 0:8918a71cdbe9 21 * You should have received a copy of the GNU General Public License
Sergunb 0:8918a71cdbe9 22 * along with this program; if not, write to the Free Software Foundation,
Sergunb 0:8918a71cdbe9 23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Sergunb 0:8918a71cdbe9 24 *
Sergunb 0:8918a71cdbe9 25 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 26 * @version 1.7.6
Sergunb 0:8918a71cdbe9 27 **/
Sergunb 0:8918a71cdbe9 28
Sergunb 0:8918a71cdbe9 29 //Switch to the appropriate trace level
Sergunb 0:8918a71cdbe9 30 #define TRACE_LEVEL NIC_TRACE_LEVEL
Sergunb 0:8918a71cdbe9 31
Sergunb 0:8918a71cdbe9 32 //Dependencies
Sergunb 0:8918a71cdbe9 33 #include "core/net.h"
Sergunb 0:8918a71cdbe9 34 #include "drivers/rtl8211.h"
Sergunb 0:8918a71cdbe9 35 #include "debug.h"
Sergunb 0:8918a71cdbe9 36
Sergunb 0:8918a71cdbe9 37
Sergunb 0:8918a71cdbe9 38 /**
Sergunb 0:8918a71cdbe9 39 * @brief RTL8211 Ethernet PHY driver
Sergunb 0:8918a71cdbe9 40 **/
Sergunb 0:8918a71cdbe9 41
Sergunb 0:8918a71cdbe9 42 const PhyDriver rtl8211PhyDriver =
Sergunb 0:8918a71cdbe9 43 {
Sergunb 0:8918a71cdbe9 44 rtl8211Init,
Sergunb 0:8918a71cdbe9 45 rtl8211Tick,
Sergunb 0:8918a71cdbe9 46 rtl8211EnableIrq,
Sergunb 0:8918a71cdbe9 47 rtl8211DisableIrq,
Sergunb 0:8918a71cdbe9 48 rtl8211EventHandler,
Sergunb 0:8918a71cdbe9 49 };
Sergunb 0:8918a71cdbe9 50
Sergunb 0:8918a71cdbe9 51
Sergunb 0:8918a71cdbe9 52 /**
Sergunb 0:8918a71cdbe9 53 * @brief RTL8211 PHY transceiver initialization
Sergunb 0:8918a71cdbe9 54 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 55 * @return Error code
Sergunb 0:8918a71cdbe9 56 **/
Sergunb 0:8918a71cdbe9 57
Sergunb 0:8918a71cdbe9 58 error_t rtl8211Init(NetInterface *interface)
Sergunb 0:8918a71cdbe9 59 {
Sergunb 0:8918a71cdbe9 60 //Debug message
Sergunb 0:8918a71cdbe9 61 TRACE_INFO("Initializing RTL8211...\r\n");
Sergunb 0:8918a71cdbe9 62
Sergunb 0:8918a71cdbe9 63 //Initialize external interrupt line driver
Sergunb 0:8918a71cdbe9 64 if(interface->extIntDriver != NULL)
Sergunb 0:8918a71cdbe9 65 interface->extIntDriver->init();
Sergunb 0:8918a71cdbe9 66
Sergunb 0:8918a71cdbe9 67 //Reset PHY transceiver
Sergunb 0:8918a71cdbe9 68 rtl8211WritePhyReg(interface, RTL8211_PHY_REG_BMCR, BMCR_RESET);
Sergunb 0:8918a71cdbe9 69 //Wait for the reset to complete
Sergunb 0:8918a71cdbe9 70 while(rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_BMCR) & BMCR_RESET);
Sergunb 0:8918a71cdbe9 71
Sergunb 0:8918a71cdbe9 72 //Dump PHY registers for debugging purpose
Sergunb 0:8918a71cdbe9 73 rtl8211DumpPhyReg(interface);
Sergunb 0:8918a71cdbe9 74
Sergunb 0:8918a71cdbe9 75 //The PHY will generate interrupts when link status changes are detected
Sergunb 0:8918a71cdbe9 76 rtl8211WritePhyReg(interface, RTL8211_PHY_REG_INER, INER_AN_COMPLETE | INER_LINK_STATUS);
Sergunb 0:8918a71cdbe9 77
Sergunb 0:8918a71cdbe9 78 //Force the TCP/IP stack to poll the link state at startup
Sergunb 0:8918a71cdbe9 79 interface->phyEvent = TRUE;
Sergunb 0:8918a71cdbe9 80 //Notify the TCP/IP stack of the event
Sergunb 0:8918a71cdbe9 81 osSetEvent(&netEvent);
Sergunb 0:8918a71cdbe9 82
Sergunb 0:8918a71cdbe9 83 //Successful initialization
Sergunb 0:8918a71cdbe9 84 return NO_ERROR;
Sergunb 0:8918a71cdbe9 85 }
Sergunb 0:8918a71cdbe9 86
Sergunb 0:8918a71cdbe9 87
Sergunb 0:8918a71cdbe9 88 /**
Sergunb 0:8918a71cdbe9 89 * @brief RTL8211 timer handler
Sergunb 0:8918a71cdbe9 90 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 91 **/
Sergunb 0:8918a71cdbe9 92
Sergunb 0:8918a71cdbe9 93 void rtl8211Tick(NetInterface *interface)
Sergunb 0:8918a71cdbe9 94 {
Sergunb 0:8918a71cdbe9 95 uint16_t value;
Sergunb 0:8918a71cdbe9 96 bool_t linkState;
Sergunb 0:8918a71cdbe9 97
Sergunb 0:8918a71cdbe9 98 //No external interrupt line driver?
Sergunb 0:8918a71cdbe9 99 if(interface->extIntDriver == NULL)
Sergunb 0:8918a71cdbe9 100 {
Sergunb 0:8918a71cdbe9 101 //Read basic status register
Sergunb 0:8918a71cdbe9 102 value = rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_BMSR);
Sergunb 0:8918a71cdbe9 103 //Retrieve current link state
Sergunb 0:8918a71cdbe9 104 linkState = (value & BMSR_LINK_STATUS) ? TRUE : FALSE;
Sergunb 0:8918a71cdbe9 105
Sergunb 0:8918a71cdbe9 106 //Link up event?
Sergunb 0:8918a71cdbe9 107 if(linkState && !interface->linkState)
Sergunb 0:8918a71cdbe9 108 {
Sergunb 0:8918a71cdbe9 109 //Set event flag
Sergunb 0:8918a71cdbe9 110 interface->phyEvent = TRUE;
Sergunb 0:8918a71cdbe9 111 //Notify the TCP/IP stack of the event
Sergunb 0:8918a71cdbe9 112 osSetEvent(&netEvent);
Sergunb 0:8918a71cdbe9 113 }
Sergunb 0:8918a71cdbe9 114 //Link down event?
Sergunb 0:8918a71cdbe9 115 else if(!linkState && interface->linkState)
Sergunb 0:8918a71cdbe9 116 {
Sergunb 0:8918a71cdbe9 117 //Set event flag
Sergunb 0:8918a71cdbe9 118 interface->phyEvent = TRUE;
Sergunb 0:8918a71cdbe9 119 //Notify the TCP/IP stack of the event
Sergunb 0:8918a71cdbe9 120 osSetEvent(&netEvent);
Sergunb 0:8918a71cdbe9 121 }
Sergunb 0:8918a71cdbe9 122 }
Sergunb 0:8918a71cdbe9 123 }
Sergunb 0:8918a71cdbe9 124
Sergunb 0:8918a71cdbe9 125
Sergunb 0:8918a71cdbe9 126 /**
Sergunb 0:8918a71cdbe9 127 * @brief Enable interrupts
Sergunb 0:8918a71cdbe9 128 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 129 **/
Sergunb 0:8918a71cdbe9 130
Sergunb 0:8918a71cdbe9 131 void rtl8211EnableIrq(NetInterface *interface)
Sergunb 0:8918a71cdbe9 132 {
Sergunb 0:8918a71cdbe9 133 //Enable PHY transceiver interrupts
Sergunb 0:8918a71cdbe9 134 if(interface->extIntDriver != NULL)
Sergunb 0:8918a71cdbe9 135 interface->extIntDriver->enableIrq();
Sergunb 0:8918a71cdbe9 136 }
Sergunb 0:8918a71cdbe9 137
Sergunb 0:8918a71cdbe9 138
Sergunb 0:8918a71cdbe9 139 /**
Sergunb 0:8918a71cdbe9 140 * @brief Disable interrupts
Sergunb 0:8918a71cdbe9 141 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 142 **/
Sergunb 0:8918a71cdbe9 143
Sergunb 0:8918a71cdbe9 144 void rtl8211DisableIrq(NetInterface *interface)
Sergunb 0:8918a71cdbe9 145 {
Sergunb 0:8918a71cdbe9 146 //Disable PHY transceiver interrupts
Sergunb 0:8918a71cdbe9 147 if(interface->extIntDriver != NULL)
Sergunb 0:8918a71cdbe9 148 interface->extIntDriver->disableIrq();
Sergunb 0:8918a71cdbe9 149 }
Sergunb 0:8918a71cdbe9 150
Sergunb 0:8918a71cdbe9 151
Sergunb 0:8918a71cdbe9 152 /**
Sergunb 0:8918a71cdbe9 153 * @brief RTL8211 event handler
Sergunb 0:8918a71cdbe9 154 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 155 **/
Sergunb 0:8918a71cdbe9 156
Sergunb 0:8918a71cdbe9 157 void rtl8211EventHandler(NetInterface *interface)
Sergunb 0:8918a71cdbe9 158 {
Sergunb 0:8918a71cdbe9 159 uint16_t status;
Sergunb 0:8918a71cdbe9 160
Sergunb 0:8918a71cdbe9 161 //Read status register to acknowledge the interrupt
Sergunb 0:8918a71cdbe9 162 status = rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_INSR);
Sergunb 0:8918a71cdbe9 163
Sergunb 0:8918a71cdbe9 164 //Link status change?
Sergunb 0:8918a71cdbe9 165 if(status & (INSR_AN_COMPLETE | INSR_LINK_STATUS))
Sergunb 0:8918a71cdbe9 166 {
Sergunb 0:8918a71cdbe9 167 //Any link failure condition is latched in the BMSR register... Reading
Sergunb 0:8918a71cdbe9 168 //the register twice will always return the actual link status
Sergunb 0:8918a71cdbe9 169 status = rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_BMSR);
Sergunb 0:8918a71cdbe9 170 status = rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_BMSR);
Sergunb 0:8918a71cdbe9 171
Sergunb 0:8918a71cdbe9 172 //Link is up?
Sergunb 0:8918a71cdbe9 173 if(status & BMSR_LINK_STATUS)
Sergunb 0:8918a71cdbe9 174 {
Sergunb 0:8918a71cdbe9 175 //Read PHY status register
Sergunb 0:8918a71cdbe9 176 status = rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_PHYSR);
Sergunb 0:8918a71cdbe9 177
Sergunb 0:8918a71cdbe9 178 //Check current speed
Sergunb 0:8918a71cdbe9 179 switch(status & PHYSR_SPEED_MASK)
Sergunb 0:8918a71cdbe9 180 {
Sergunb 0:8918a71cdbe9 181 //10BASE-T
Sergunb 0:8918a71cdbe9 182 case PHYSR_SPEED_10:
Sergunb 0:8918a71cdbe9 183 interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
Sergunb 0:8918a71cdbe9 184 break;
Sergunb 0:8918a71cdbe9 185 //100BASE-TX
Sergunb 0:8918a71cdbe9 186 case PHYSR_SPEED_100:
Sergunb 0:8918a71cdbe9 187 interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
Sergunb 0:8918a71cdbe9 188 break;
Sergunb 0:8918a71cdbe9 189 //1000BASE-T
Sergunb 0:8918a71cdbe9 190 case PHYSR_SPEED_1000:
Sergunb 0:8918a71cdbe9 191 interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
Sergunb 0:8918a71cdbe9 192 break;
Sergunb 0:8918a71cdbe9 193 //Unknown speed
Sergunb 0:8918a71cdbe9 194 default:
Sergunb 0:8918a71cdbe9 195 //Debug message
Sergunb 0:8918a71cdbe9 196 TRACE_WARNING("Invalid speed\r\n");
Sergunb 0:8918a71cdbe9 197 break;
Sergunb 0:8918a71cdbe9 198 }
Sergunb 0:8918a71cdbe9 199
Sergunb 0:8918a71cdbe9 200 //Check current duplex mode
Sergunb 0:8918a71cdbe9 201 if(status & PHYSR_DUPLEX)
Sergunb 0:8918a71cdbe9 202 interface->duplexMode = NIC_FULL_DUPLEX_MODE;
Sergunb 0:8918a71cdbe9 203 else
Sergunb 0:8918a71cdbe9 204 interface->duplexMode = NIC_HALF_DUPLEX_MODE;
Sergunb 0:8918a71cdbe9 205
Sergunb 0:8918a71cdbe9 206 //Update link state
Sergunb 0:8918a71cdbe9 207 interface->linkState = TRUE;
Sergunb 0:8918a71cdbe9 208
Sergunb 0:8918a71cdbe9 209 //Adjust MAC configuration parameters for proper operation
Sergunb 0:8918a71cdbe9 210 interface->nicDriver->updateMacConfig(interface);
Sergunb 0:8918a71cdbe9 211 }
Sergunb 0:8918a71cdbe9 212 else
Sergunb 0:8918a71cdbe9 213 {
Sergunb 0:8918a71cdbe9 214 //Update link state
Sergunb 0:8918a71cdbe9 215 interface->linkState = FALSE;
Sergunb 0:8918a71cdbe9 216 }
Sergunb 0:8918a71cdbe9 217
Sergunb 0:8918a71cdbe9 218 //Process link state change event
Sergunb 0:8918a71cdbe9 219 nicNotifyLinkChange(interface);
Sergunb 0:8918a71cdbe9 220 }
Sergunb 0:8918a71cdbe9 221 }
Sergunb 0:8918a71cdbe9 222
Sergunb 0:8918a71cdbe9 223
Sergunb 0:8918a71cdbe9 224 /**
Sergunb 0:8918a71cdbe9 225 * @brief Write PHY register
Sergunb 0:8918a71cdbe9 226 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 227 * @param[in] address PHY register address
Sergunb 0:8918a71cdbe9 228 * @param[in] data Register value
Sergunb 0:8918a71cdbe9 229 **/
Sergunb 0:8918a71cdbe9 230
Sergunb 0:8918a71cdbe9 231 void rtl8211WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Sergunb 0:8918a71cdbe9 232 {
Sergunb 0:8918a71cdbe9 233 uint8_t phyAddr;
Sergunb 0:8918a71cdbe9 234
Sergunb 0:8918a71cdbe9 235 //Get the address of the PHY transceiver
Sergunb 0:8918a71cdbe9 236 if(interface->phyAddr < 32)
Sergunb 0:8918a71cdbe9 237 phyAddr = interface->phyAddr;
Sergunb 0:8918a71cdbe9 238 else
Sergunb 0:8918a71cdbe9 239 phyAddr = RTL8211_PHY_ADDR;
Sergunb 0:8918a71cdbe9 240
Sergunb 0:8918a71cdbe9 241 //Write the specified PHY register
Sergunb 0:8918a71cdbe9 242 interface->nicDriver->writePhyReg(phyAddr, address, data);
Sergunb 0:8918a71cdbe9 243 }
Sergunb 0:8918a71cdbe9 244
Sergunb 0:8918a71cdbe9 245
Sergunb 0:8918a71cdbe9 246 /**
Sergunb 0:8918a71cdbe9 247 * @brief Read PHY register
Sergunb 0:8918a71cdbe9 248 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 249 * @param[in] address PHY register address
Sergunb 0:8918a71cdbe9 250 * @return Register value
Sergunb 0:8918a71cdbe9 251 **/
Sergunb 0:8918a71cdbe9 252
Sergunb 0:8918a71cdbe9 253 uint16_t rtl8211ReadPhyReg(NetInterface *interface, uint8_t address)
Sergunb 0:8918a71cdbe9 254 {
Sergunb 0:8918a71cdbe9 255 uint8_t phyAddr;
Sergunb 0:8918a71cdbe9 256
Sergunb 0:8918a71cdbe9 257 //Get the address of the PHY transceiver
Sergunb 0:8918a71cdbe9 258 if(interface->phyAddr < 32)
Sergunb 0:8918a71cdbe9 259 phyAddr = interface->phyAddr;
Sergunb 0:8918a71cdbe9 260 else
Sergunb 0:8918a71cdbe9 261 phyAddr = RTL8211_PHY_ADDR;
Sergunb 0:8918a71cdbe9 262
Sergunb 0:8918a71cdbe9 263 //Read the specified PHY register
Sergunb 0:8918a71cdbe9 264 return interface->nicDriver->readPhyReg(phyAddr, address);
Sergunb 0:8918a71cdbe9 265 }
Sergunb 0:8918a71cdbe9 266
Sergunb 0:8918a71cdbe9 267
Sergunb 0:8918a71cdbe9 268 /**
Sergunb 0:8918a71cdbe9 269 * @brief Dump PHY registers for debugging purpose
Sergunb 0:8918a71cdbe9 270 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 271 **/
Sergunb 0:8918a71cdbe9 272
Sergunb 0:8918a71cdbe9 273 void rtl8211DumpPhyReg(NetInterface *interface)
Sergunb 0:8918a71cdbe9 274 {
Sergunb 0:8918a71cdbe9 275 uint8_t i;
Sergunb 0:8918a71cdbe9 276
Sergunb 0:8918a71cdbe9 277 //Loop through PHY registers
Sergunb 0:8918a71cdbe9 278 for(i = 0; i < 32; i++)
Sergunb 0:8918a71cdbe9 279 {
Sergunb 0:8918a71cdbe9 280 //Display current PHY register
Sergunb 0:8918a71cdbe9 281 TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, rtl8211ReadPhyReg(interface, i));
Sergunb 0:8918a71cdbe9 282 }
Sergunb 0:8918a71cdbe9 283
Sergunb 0:8918a71cdbe9 284 //Terminate with a line feed
Sergunb 0:8918a71cdbe9 285 TRACE_DEBUG("\r\n");
Sergunb 0:8918a71cdbe9 286 }
Sergunb 0:8918a71cdbe9 287