Webserver+3d print
cyclone_tcp/drivers/st802rt1a.c@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file st802rt1a.c |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief ST802RT1A 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/st802rt1a.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 ST802RT1A Ethernet PHY driver |
Sergunb | 0:8918a71cdbe9 | 40 | **/ |
Sergunb | 0:8918a71cdbe9 | 41 | |
Sergunb | 0:8918a71cdbe9 | 42 | const PhyDriver st802rt1aPhyDriver = |
Sergunb | 0:8918a71cdbe9 | 43 | { |
Sergunb | 0:8918a71cdbe9 | 44 | st802rt1aInit, |
Sergunb | 0:8918a71cdbe9 | 45 | st802rt1aTick, |
Sergunb | 0:8918a71cdbe9 | 46 | st802rt1aEnableIrq, |
Sergunb | 0:8918a71cdbe9 | 47 | st802rt1aDisableIrq, |
Sergunb | 0:8918a71cdbe9 | 48 | st802rt1aEventHandler, |
Sergunb | 0:8918a71cdbe9 | 49 | }; |
Sergunb | 0:8918a71cdbe9 | 50 | |
Sergunb | 0:8918a71cdbe9 | 51 | |
Sergunb | 0:8918a71cdbe9 | 52 | /** |
Sergunb | 0:8918a71cdbe9 | 53 | * @brief ST802RT1A 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 st802rt1aInit(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 59 | { |
Sergunb | 0:8918a71cdbe9 | 60 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 61 | TRACE_INFO("Initializing ST802RT1A...\r\n"); |
Sergunb | 0:8918a71cdbe9 | 62 | |
Sergunb | 0:8918a71cdbe9 | 63 | //Reset PHY transceiver |
Sergunb | 0:8918a71cdbe9 | 64 | st802rt1aWritePhyReg(interface, ST802RT1A_PHY_REG_RN00, RN00_SOFT_RESET); |
Sergunb | 0:8918a71cdbe9 | 65 | //Wait for the reset to complete |
Sergunb | 0:8918a71cdbe9 | 66 | while(st802rt1aReadPhyReg(interface, ST802RT1A_PHY_REG_RN00) & RN00_SOFT_RESET); |
Sergunb | 0:8918a71cdbe9 | 67 | |
Sergunb | 0:8918a71cdbe9 | 68 | //Dump PHY registers for debugging purpose |
Sergunb | 0:8918a71cdbe9 | 69 | st802rt1aDumpPhyReg(interface); |
Sergunb | 0:8918a71cdbe9 | 70 | |
Sergunb | 0:8918a71cdbe9 | 71 | //Force the TCP/IP stack to poll the link state at startup |
Sergunb | 0:8918a71cdbe9 | 72 | interface->phyEvent = TRUE; |
Sergunb | 0:8918a71cdbe9 | 73 | //Notify the TCP/IP stack of the event |
Sergunb | 0:8918a71cdbe9 | 74 | osSetEvent(&netEvent); |
Sergunb | 0:8918a71cdbe9 | 75 | |
Sergunb | 0:8918a71cdbe9 | 76 | //Successful initialization |
Sergunb | 0:8918a71cdbe9 | 77 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 78 | } |
Sergunb | 0:8918a71cdbe9 | 79 | |
Sergunb | 0:8918a71cdbe9 | 80 | |
Sergunb | 0:8918a71cdbe9 | 81 | /** |
Sergunb | 0:8918a71cdbe9 | 82 | * @brief ST802RT1A timer handler |
Sergunb | 0:8918a71cdbe9 | 83 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 84 | **/ |
Sergunb | 0:8918a71cdbe9 | 85 | |
Sergunb | 0:8918a71cdbe9 | 86 | void st802rt1aTick(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 87 | { |
Sergunb | 0:8918a71cdbe9 | 88 | uint16_t value; |
Sergunb | 0:8918a71cdbe9 | 89 | bool_t linkState; |
Sergunb | 0:8918a71cdbe9 | 90 | |
Sergunb | 0:8918a71cdbe9 | 91 | //Read status register |
Sergunb | 0:8918a71cdbe9 | 92 | value = st802rt1aReadPhyReg(interface, ST802RT1A_PHY_REG_RN01); |
Sergunb | 0:8918a71cdbe9 | 93 | //Retrieve current link state |
Sergunb | 0:8918a71cdbe9 | 94 | linkState = (value & RN01_LINK_STATUS) ? TRUE : FALSE; |
Sergunb | 0:8918a71cdbe9 | 95 | |
Sergunb | 0:8918a71cdbe9 | 96 | //Link up event? |
Sergunb | 0:8918a71cdbe9 | 97 | if(linkState && !interface->linkState) |
Sergunb | 0:8918a71cdbe9 | 98 | { |
Sergunb | 0:8918a71cdbe9 | 99 | //Set event flag |
Sergunb | 0:8918a71cdbe9 | 100 | interface->phyEvent = TRUE; |
Sergunb | 0:8918a71cdbe9 | 101 | //Notify the TCP/IP stack of the event |
Sergunb | 0:8918a71cdbe9 | 102 | osSetEvent(&netEvent); |
Sergunb | 0:8918a71cdbe9 | 103 | } |
Sergunb | 0:8918a71cdbe9 | 104 | //Link down event? |
Sergunb | 0:8918a71cdbe9 | 105 | else if(!linkState && interface->linkState) |
Sergunb | 0:8918a71cdbe9 | 106 | { |
Sergunb | 0:8918a71cdbe9 | 107 | //Set event flag |
Sergunb | 0:8918a71cdbe9 | 108 | interface->phyEvent = TRUE; |
Sergunb | 0:8918a71cdbe9 | 109 | //Notify the TCP/IP stack of the event |
Sergunb | 0:8918a71cdbe9 | 110 | osSetEvent(&netEvent); |
Sergunb | 0:8918a71cdbe9 | 111 | } |
Sergunb | 0:8918a71cdbe9 | 112 | } |
Sergunb | 0:8918a71cdbe9 | 113 | |
Sergunb | 0:8918a71cdbe9 | 114 | |
Sergunb | 0:8918a71cdbe9 | 115 | /** |
Sergunb | 0:8918a71cdbe9 | 116 | * @brief Enable interrupts |
Sergunb | 0:8918a71cdbe9 | 117 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 118 | **/ |
Sergunb | 0:8918a71cdbe9 | 119 | |
Sergunb | 0:8918a71cdbe9 | 120 | void st802rt1aEnableIrq(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 121 | { |
Sergunb | 0:8918a71cdbe9 | 122 | } |
Sergunb | 0:8918a71cdbe9 | 123 | |
Sergunb | 0:8918a71cdbe9 | 124 | |
Sergunb | 0:8918a71cdbe9 | 125 | /** |
Sergunb | 0:8918a71cdbe9 | 126 | * @brief Disable interrupts |
Sergunb | 0:8918a71cdbe9 | 127 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 128 | **/ |
Sergunb | 0:8918a71cdbe9 | 129 | |
Sergunb | 0:8918a71cdbe9 | 130 | void st802rt1aDisableIrq(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 131 | { |
Sergunb | 0:8918a71cdbe9 | 132 | } |
Sergunb | 0:8918a71cdbe9 | 133 | |
Sergunb | 0:8918a71cdbe9 | 134 | |
Sergunb | 0:8918a71cdbe9 | 135 | /** |
Sergunb | 0:8918a71cdbe9 | 136 | * @brief ST802RT1A event handler |
Sergunb | 0:8918a71cdbe9 | 137 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 138 | **/ |
Sergunb | 0:8918a71cdbe9 | 139 | |
Sergunb | 0:8918a71cdbe9 | 140 | void st802rt1aEventHandler(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 141 | { |
Sergunb | 0:8918a71cdbe9 | 142 | uint16_t value; |
Sergunb | 0:8918a71cdbe9 | 143 | bool_t linkState; |
Sergunb | 0:8918a71cdbe9 | 144 | |
Sergunb | 0:8918a71cdbe9 | 145 | //Read status register |
Sergunb | 0:8918a71cdbe9 | 146 | value = st802rt1aReadPhyReg(interface, ST802RT1A_PHY_REG_RN01); |
Sergunb | 0:8918a71cdbe9 | 147 | //Retrieve current link state |
Sergunb | 0:8918a71cdbe9 | 148 | linkState = (value & RN01_LINK_STATUS) ? TRUE : FALSE; |
Sergunb | 0:8918a71cdbe9 | 149 | |
Sergunb | 0:8918a71cdbe9 | 150 | //Link is up? |
Sergunb | 0:8918a71cdbe9 | 151 | if(linkState && !interface->linkState) |
Sergunb | 0:8918a71cdbe9 | 152 | { |
Sergunb | 0:8918a71cdbe9 | 153 | //Read RN13 register |
Sergunb | 0:8918a71cdbe9 | 154 | value = st802rt1aReadPhyReg(interface, ST802RT1A_PHY_REG_RN13); |
Sergunb | 0:8918a71cdbe9 | 155 | |
Sergunb | 0:8918a71cdbe9 | 156 | //Check current operation mode |
Sergunb | 0:8918a71cdbe9 | 157 | switch(value & RN13_CMODE_MASK) |
Sergunb | 0:8918a71cdbe9 | 158 | { |
Sergunb | 0:8918a71cdbe9 | 159 | //10BASE-T |
Sergunb | 0:8918a71cdbe9 | 160 | case RN13_CMODE_10BT: |
Sergunb | 0:8918a71cdbe9 | 161 | interface->linkSpeed = NIC_LINK_SPEED_10MBPS; |
Sergunb | 0:8918a71cdbe9 | 162 | interface->duplexMode = NIC_HALF_DUPLEX_MODE; |
Sergunb | 0:8918a71cdbe9 | 163 | break; |
Sergunb | 0:8918a71cdbe9 | 164 | //10BASE-T full-duplex |
Sergunb | 0:8918a71cdbe9 | 165 | case RN13_CMODE_10BT_FD: |
Sergunb | 0:8918a71cdbe9 | 166 | interface->linkSpeed = NIC_LINK_SPEED_10MBPS; |
Sergunb | 0:8918a71cdbe9 | 167 | interface->duplexMode = NIC_FULL_DUPLEX_MODE; |
Sergunb | 0:8918a71cdbe9 | 168 | break; |
Sergunb | 0:8918a71cdbe9 | 169 | //100BASE-TX |
Sergunb | 0:8918a71cdbe9 | 170 | case RN13_CMODE_100BTX: |
Sergunb | 0:8918a71cdbe9 | 171 | interface->linkSpeed = NIC_LINK_SPEED_100MBPS; |
Sergunb | 0:8918a71cdbe9 | 172 | interface->duplexMode = NIC_HALF_DUPLEX_MODE; |
Sergunb | 0:8918a71cdbe9 | 173 | break; |
Sergunb | 0:8918a71cdbe9 | 174 | //100BASE-TX full-duplex |
Sergunb | 0:8918a71cdbe9 | 175 | case RN13_CMODE_100BTX_FD: |
Sergunb | 0:8918a71cdbe9 | 176 | interface->linkSpeed = NIC_LINK_SPEED_100MBPS; |
Sergunb | 0:8918a71cdbe9 | 177 | interface->duplexMode = NIC_FULL_DUPLEX_MODE; |
Sergunb | 0:8918a71cdbe9 | 178 | break; |
Sergunb | 0:8918a71cdbe9 | 179 | //Unknown operation mode |
Sergunb | 0:8918a71cdbe9 | 180 | default: |
Sergunb | 0:8918a71cdbe9 | 181 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 182 | TRACE_WARNING("Invalid Duplex mode\r\n"); |
Sergunb | 0:8918a71cdbe9 | 183 | break; |
Sergunb | 0:8918a71cdbe9 | 184 | } |
Sergunb | 0:8918a71cdbe9 | 185 | |
Sergunb | 0:8918a71cdbe9 | 186 | //Update link state |
Sergunb | 0:8918a71cdbe9 | 187 | interface->linkState = TRUE; |
Sergunb | 0:8918a71cdbe9 | 188 | |
Sergunb | 0:8918a71cdbe9 | 189 | //Adjust MAC configuration parameters for proper operation |
Sergunb | 0:8918a71cdbe9 | 190 | interface->nicDriver->updateMacConfig(interface); |
Sergunb | 0:8918a71cdbe9 | 191 | |
Sergunb | 0:8918a71cdbe9 | 192 | //Process link state change event |
Sergunb | 0:8918a71cdbe9 | 193 | nicNotifyLinkChange(interface); |
Sergunb | 0:8918a71cdbe9 | 194 | } |
Sergunb | 0:8918a71cdbe9 | 195 | //Link is down? |
Sergunb | 0:8918a71cdbe9 | 196 | else if(!linkState && interface->linkState) |
Sergunb | 0:8918a71cdbe9 | 197 | { |
Sergunb | 0:8918a71cdbe9 | 198 | //Update link state |
Sergunb | 0:8918a71cdbe9 | 199 | interface->linkState = FALSE; |
Sergunb | 0:8918a71cdbe9 | 200 | |
Sergunb | 0:8918a71cdbe9 | 201 | //Process link state change event |
Sergunb | 0:8918a71cdbe9 | 202 | nicNotifyLinkChange(interface); |
Sergunb | 0:8918a71cdbe9 | 203 | } |
Sergunb | 0:8918a71cdbe9 | 204 | } |
Sergunb | 0:8918a71cdbe9 | 205 | |
Sergunb | 0:8918a71cdbe9 | 206 | |
Sergunb | 0:8918a71cdbe9 | 207 | /** |
Sergunb | 0:8918a71cdbe9 | 208 | * @brief Write PHY register |
Sergunb | 0:8918a71cdbe9 | 209 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 210 | * @param[in] address PHY register Register address |
Sergunb | 0:8918a71cdbe9 | 211 | * @param[in] data Register value |
Sergunb | 0:8918a71cdbe9 | 212 | **/ |
Sergunb | 0:8918a71cdbe9 | 213 | |
Sergunb | 0:8918a71cdbe9 | 214 | void st802rt1aWritePhyReg(NetInterface *interface, uint8_t address, uint16_t data) |
Sergunb | 0:8918a71cdbe9 | 215 | { |
Sergunb | 0:8918a71cdbe9 | 216 | uint8_t phyAddr; |
Sergunb | 0:8918a71cdbe9 | 217 | |
Sergunb | 0:8918a71cdbe9 | 218 | //Get the address of the PHY transceiver |
Sergunb | 0:8918a71cdbe9 | 219 | if(interface->phyAddr < 32) |
Sergunb | 0:8918a71cdbe9 | 220 | phyAddr = interface->phyAddr; |
Sergunb | 0:8918a71cdbe9 | 221 | else |
Sergunb | 0:8918a71cdbe9 | 222 | phyAddr = ST802RT1A_PHY_ADDR; |
Sergunb | 0:8918a71cdbe9 | 223 | |
Sergunb | 0:8918a71cdbe9 | 224 | //Write the specified PHY register |
Sergunb | 0:8918a71cdbe9 | 225 | interface->nicDriver->writePhyReg(phyAddr, address, data); |
Sergunb | 0:8918a71cdbe9 | 226 | } |
Sergunb | 0:8918a71cdbe9 | 227 | |
Sergunb | 0:8918a71cdbe9 | 228 | |
Sergunb | 0:8918a71cdbe9 | 229 | /** |
Sergunb | 0:8918a71cdbe9 | 230 | * @brief Read PHY register |
Sergunb | 0:8918a71cdbe9 | 231 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 232 | * @param[in] address PHY register address |
Sergunb | 0:8918a71cdbe9 | 233 | * @return Register value |
Sergunb | 0:8918a71cdbe9 | 234 | **/ |
Sergunb | 0:8918a71cdbe9 | 235 | |
Sergunb | 0:8918a71cdbe9 | 236 | uint16_t st802rt1aReadPhyReg(NetInterface *interface, uint8_t address) |
Sergunb | 0:8918a71cdbe9 | 237 | { |
Sergunb | 0:8918a71cdbe9 | 238 | uint8_t phyAddr; |
Sergunb | 0:8918a71cdbe9 | 239 | |
Sergunb | 0:8918a71cdbe9 | 240 | //Get the address of the PHY transceiver |
Sergunb | 0:8918a71cdbe9 | 241 | if(interface->phyAddr < 32) |
Sergunb | 0:8918a71cdbe9 | 242 | phyAddr = interface->phyAddr; |
Sergunb | 0:8918a71cdbe9 | 243 | else |
Sergunb | 0:8918a71cdbe9 | 244 | phyAddr = ST802RT1A_PHY_ADDR; |
Sergunb | 0:8918a71cdbe9 | 245 | |
Sergunb | 0:8918a71cdbe9 | 246 | //Read the specified PHY register |
Sergunb | 0:8918a71cdbe9 | 247 | return interface->nicDriver->readPhyReg(phyAddr, address); |
Sergunb | 0:8918a71cdbe9 | 248 | } |
Sergunb | 0:8918a71cdbe9 | 249 | |
Sergunb | 0:8918a71cdbe9 | 250 | |
Sergunb | 0:8918a71cdbe9 | 251 | /** |
Sergunb | 0:8918a71cdbe9 | 252 | * @brief Dump PHY registers for debugging purpose |
Sergunb | 0:8918a71cdbe9 | 253 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 254 | **/ |
Sergunb | 0:8918a71cdbe9 | 255 | |
Sergunb | 0:8918a71cdbe9 | 256 | void st802rt1aDumpPhyReg(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 257 | { |
Sergunb | 0:8918a71cdbe9 | 258 | uint8_t i; |
Sergunb | 0:8918a71cdbe9 | 259 | |
Sergunb | 0:8918a71cdbe9 | 260 | //Loop through PHY registers |
Sergunb | 0:8918a71cdbe9 | 261 | for(i = 0; i < 32; i++) |
Sergunb | 0:8918a71cdbe9 | 262 | { |
Sergunb | 0:8918a71cdbe9 | 263 | //Display current PHY register |
Sergunb | 0:8918a71cdbe9 | 264 | TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, st802rt1aReadPhyReg(interface, i)); |
Sergunb | 0:8918a71cdbe9 | 265 | } |
Sergunb | 0:8918a71cdbe9 | 266 | |
Sergunb | 0:8918a71cdbe9 | 267 | //Terminate with a line feed |
Sergunb | 0:8918a71cdbe9 | 268 | TRACE_DEBUG("\r\n"); |
Sergunb | 0:8918a71cdbe9 | 269 | } |
Sergunb | 0:8918a71cdbe9 | 270 |