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 ksz8061.c
Sergunb 0:8918a71cdbe9 3 * @brief KSZ8061 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/ksz8061.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 KSZ8061 Ethernet PHY driver
Sergunb 0:8918a71cdbe9 40 **/
Sergunb 0:8918a71cdbe9 41
Sergunb 0:8918a71cdbe9 42 const PhyDriver ksz8061PhyDriver =
Sergunb 0:8918a71cdbe9 43 {
Sergunb 0:8918a71cdbe9 44 ksz8061Init,
Sergunb 0:8918a71cdbe9 45 ksz8061Tick,
Sergunb 0:8918a71cdbe9 46 ksz8061EnableIrq,
Sergunb 0:8918a71cdbe9 47 ksz8061DisableIrq,
Sergunb 0:8918a71cdbe9 48 ksz8061EventHandler,
Sergunb 0:8918a71cdbe9 49 };
Sergunb 0:8918a71cdbe9 50
Sergunb 0:8918a71cdbe9 51
Sergunb 0:8918a71cdbe9 52 /**
Sergunb 0:8918a71cdbe9 53 * @brief KSZ8061 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 ksz8061Init(NetInterface *interface)
Sergunb 0:8918a71cdbe9 59 {
Sergunb 0:8918a71cdbe9 60 //Debug message
Sergunb 0:8918a71cdbe9 61 TRACE_INFO("Initializing KSZ8061...\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 ksz8061WritePhyReg(interface, KSZ8061_PHY_REG_BMCR, BMCR_RESET);
Sergunb 0:8918a71cdbe9 69 //Wait for the reset to complete
Sergunb 0:8918a71cdbe9 70 while(ksz8061ReadPhyReg(interface, KSZ8061_PHY_REG_BMCR) & BMCR_RESET);
Sergunb 0:8918a71cdbe9 71
Sergunb 0:8918a71cdbe9 72 //Dump PHY registers for debugging purpose
Sergunb 0:8918a71cdbe9 73 ksz8061DumpPhyReg(interface);
Sergunb 0:8918a71cdbe9 74
Sergunb 0:8918a71cdbe9 75 //The PHY will generate interrupts when link status changes are detected
Sergunb 0:8918a71cdbe9 76 ksz8061WritePhyReg(interface, KSZ8061_PHY_REG_ICSR, ICSR_LINK_DOWN_IE | ICSR_LINK_UP_IE);
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 KSZ8061 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 ksz8061Tick(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 = ksz8061ReadPhyReg(interface, KSZ8061_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 ksz8061EnableIrq(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 ksz8061DisableIrq(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 KSZ8061 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 ksz8061EventHandler(NetInterface *interface)
Sergunb 0:8918a71cdbe9 158 {
Sergunb 0:8918a71cdbe9 159 uint16_t value;
Sergunb 0:8918a71cdbe9 160
Sergunb 0:8918a71cdbe9 161 //Read status register to acknowledge the interrupt
Sergunb 0:8918a71cdbe9 162 value = ksz8061ReadPhyReg(interface, KSZ8061_PHY_REG_ICSR);
Sergunb 0:8918a71cdbe9 163
Sergunb 0:8918a71cdbe9 164 //Link status change?
Sergunb 0:8918a71cdbe9 165 if(value & (ICSR_LINK_DOWN_IF | ICSR_LINK_UP_IF))
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 value = ksz8061ReadPhyReg(interface, KSZ8061_PHY_REG_BMSR);
Sergunb 0:8918a71cdbe9 170 value = ksz8061ReadPhyReg(interface, KSZ8061_PHY_REG_BMSR);
Sergunb 0:8918a71cdbe9 171
Sergunb 0:8918a71cdbe9 172 //Link is up?
Sergunb 0:8918a71cdbe9 173 if(value & BMSR_LINK_STATUS)
Sergunb 0:8918a71cdbe9 174 {
Sergunb 0:8918a71cdbe9 175 //Read PHY control register
Sergunb 0:8918a71cdbe9 176 value = ksz8061ReadPhyReg(interface, KSZ8061_PHY_REG_PHYCON1);
Sergunb 0:8918a71cdbe9 177
Sergunb 0:8918a71cdbe9 178 //Check current operation mode
Sergunb 0:8918a71cdbe9 179 switch(value & PHYCON1_OP_MODE_MASK)
Sergunb 0:8918a71cdbe9 180 {
Sergunb 0:8918a71cdbe9 181 //10BASE-T
Sergunb 0:8918a71cdbe9 182 case PHYCON1_OP_MODE_10BT:
Sergunb 0:8918a71cdbe9 183 interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
Sergunb 0:8918a71cdbe9 184 interface->duplexMode = NIC_HALF_DUPLEX_MODE;
Sergunb 0:8918a71cdbe9 185 break;
Sergunb 0:8918a71cdbe9 186 //10BASE-T full-duplex
Sergunb 0:8918a71cdbe9 187 case PHYCON1_OP_MODE_10BT_FD:
Sergunb 0:8918a71cdbe9 188 interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
Sergunb 0:8918a71cdbe9 189 interface->duplexMode = NIC_FULL_DUPLEX_MODE;
Sergunb 0:8918a71cdbe9 190 break;
Sergunb 0:8918a71cdbe9 191 //100BASE-TX
Sergunb 0:8918a71cdbe9 192 case PHYCON1_OP_MODE_100BTX:
Sergunb 0:8918a71cdbe9 193 interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
Sergunb 0:8918a71cdbe9 194 interface->duplexMode = NIC_HALF_DUPLEX_MODE;
Sergunb 0:8918a71cdbe9 195 break;
Sergunb 0:8918a71cdbe9 196 //100BASE-TX full-duplex
Sergunb 0:8918a71cdbe9 197 case PHYCON1_OP_MODE_100BTX_FD:
Sergunb 0:8918a71cdbe9 198 interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
Sergunb 0:8918a71cdbe9 199 interface->duplexMode = NIC_FULL_DUPLEX_MODE;
Sergunb 0:8918a71cdbe9 200 break;
Sergunb 0:8918a71cdbe9 201 //Unknown operation mode
Sergunb 0:8918a71cdbe9 202 default:
Sergunb 0:8918a71cdbe9 203 //Debug message
Sergunb 0:8918a71cdbe9 204 TRACE_WARNING("Invalid Duplex mode\r\n");
Sergunb 0:8918a71cdbe9 205 break;
Sergunb 0:8918a71cdbe9 206 }
Sergunb 0:8918a71cdbe9 207
Sergunb 0:8918a71cdbe9 208 //Update link state
Sergunb 0:8918a71cdbe9 209 interface->linkState = TRUE;
Sergunb 0:8918a71cdbe9 210
Sergunb 0:8918a71cdbe9 211 //Adjust MAC configuration parameters for proper operation
Sergunb 0:8918a71cdbe9 212 interface->nicDriver->updateMacConfig(interface);
Sergunb 0:8918a71cdbe9 213 }
Sergunb 0:8918a71cdbe9 214 else
Sergunb 0:8918a71cdbe9 215 {
Sergunb 0:8918a71cdbe9 216 //Update link state
Sergunb 0:8918a71cdbe9 217 interface->linkState = FALSE;
Sergunb 0:8918a71cdbe9 218 }
Sergunb 0:8918a71cdbe9 219
Sergunb 0:8918a71cdbe9 220 //Process link state change event
Sergunb 0:8918a71cdbe9 221 nicNotifyLinkChange(interface);
Sergunb 0:8918a71cdbe9 222 }
Sergunb 0:8918a71cdbe9 223 }
Sergunb 0:8918a71cdbe9 224
Sergunb 0:8918a71cdbe9 225
Sergunb 0:8918a71cdbe9 226 /**
Sergunb 0:8918a71cdbe9 227 * @brief Write PHY register
Sergunb 0:8918a71cdbe9 228 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 229 * @param[in] address PHY register address
Sergunb 0:8918a71cdbe9 230 * @param[in] data Register value
Sergunb 0:8918a71cdbe9 231 **/
Sergunb 0:8918a71cdbe9 232
Sergunb 0:8918a71cdbe9 233 void ksz8061WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Sergunb 0:8918a71cdbe9 234 {
Sergunb 0:8918a71cdbe9 235 uint8_t phyAddr;
Sergunb 0:8918a71cdbe9 236
Sergunb 0:8918a71cdbe9 237 //Get the address of the PHY transceiver
Sergunb 0:8918a71cdbe9 238 if(interface->phyAddr < 32)
Sergunb 0:8918a71cdbe9 239 phyAddr = interface->phyAddr;
Sergunb 0:8918a71cdbe9 240 else
Sergunb 0:8918a71cdbe9 241 phyAddr = KSZ8061_PHY_ADDR;
Sergunb 0:8918a71cdbe9 242
Sergunb 0:8918a71cdbe9 243 //Write the specified PHY register
Sergunb 0:8918a71cdbe9 244 interface->nicDriver->writePhyReg(phyAddr, address, data);
Sergunb 0:8918a71cdbe9 245 }
Sergunb 0:8918a71cdbe9 246
Sergunb 0:8918a71cdbe9 247
Sergunb 0:8918a71cdbe9 248 /**
Sergunb 0:8918a71cdbe9 249 * @brief Read PHY register
Sergunb 0:8918a71cdbe9 250 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 251 * @param[in] address PHY register address
Sergunb 0:8918a71cdbe9 252 * @return Register value
Sergunb 0:8918a71cdbe9 253 **/
Sergunb 0:8918a71cdbe9 254
Sergunb 0:8918a71cdbe9 255 uint16_t ksz8061ReadPhyReg(NetInterface *interface, uint8_t address)
Sergunb 0:8918a71cdbe9 256 {
Sergunb 0:8918a71cdbe9 257 uint8_t phyAddr;
Sergunb 0:8918a71cdbe9 258
Sergunb 0:8918a71cdbe9 259 //Get the address of the PHY transceiver
Sergunb 0:8918a71cdbe9 260 if(interface->phyAddr < 32)
Sergunb 0:8918a71cdbe9 261 phyAddr = interface->phyAddr;
Sergunb 0:8918a71cdbe9 262 else
Sergunb 0:8918a71cdbe9 263 phyAddr = KSZ8061_PHY_ADDR;
Sergunb 0:8918a71cdbe9 264
Sergunb 0:8918a71cdbe9 265 //Read the specified PHY register
Sergunb 0:8918a71cdbe9 266 return interface->nicDriver->readPhyReg(phyAddr, address);
Sergunb 0:8918a71cdbe9 267 }
Sergunb 0:8918a71cdbe9 268
Sergunb 0:8918a71cdbe9 269
Sergunb 0:8918a71cdbe9 270 /**
Sergunb 0:8918a71cdbe9 271 * @brief Dump PHY registers for debugging purpose
Sergunb 0:8918a71cdbe9 272 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 273 **/
Sergunb 0:8918a71cdbe9 274
Sergunb 0:8918a71cdbe9 275 void ksz8061DumpPhyReg(NetInterface *interface)
Sergunb 0:8918a71cdbe9 276 {
Sergunb 0:8918a71cdbe9 277 uint8_t i;
Sergunb 0:8918a71cdbe9 278
Sergunb 0:8918a71cdbe9 279 //Loop through PHY registers
Sergunb 0:8918a71cdbe9 280 for(i = 0; i < 32; i++)
Sergunb 0:8918a71cdbe9 281 {
Sergunb 0:8918a71cdbe9 282 //Display current PHY register
Sergunb 0:8918a71cdbe9 283 TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, ksz8061ReadPhyReg(interface, i));
Sergunb 0:8918a71cdbe9 284 }
Sergunb 0:8918a71cdbe9 285
Sergunb 0:8918a71cdbe9 286 //Terminate with a line feed
Sergunb 0:8918a71cdbe9 287 TRACE_DEBUG("\r\n");
Sergunb 0:8918a71cdbe9 288 }
Sergunb 0:8918a71cdbe9 289