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