lan8671_driver.c
Go to the documentation of this file.
1 /**
2  * @file lan8671_driver.c
3  * @brief LAN8671 10Base-T1S Ethernet PHY driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
37 #include "debug.h"
38 
39 
40 /**
41  * @brief LAN8671 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief LAN8671 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing LAN8671...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = LAN8671_PHY_ADDR;
70  }
71 
72  //Initialize serial management interface
73  if(interface->smiDriver != NULL)
74  {
75  interface->smiDriver->init();
76  }
77 
78  //Initialize external interrupt line driver
79  if(interface->extIntDriver != NULL)
80  {
81  interface->extIntDriver->init();
82  }
83 
84  //Dump PHY registers for debugging purpose
85  lan8671DumpPhyReg(interface);
86 
87  //Configure PHY transceiver
88  lan8671ModifyMmdReg(interface, 0x1F, 0x00D0, 0x0E03, 0x0002);
89  lan8671ModifyMmdReg(interface, 0x1F, 0x00D1, 0x0300, 0x0000);
90  lan8671ModifyMmdReg(interface, 0x1F, 0x0084, 0xFFC0, 0x3380);
91  lan8671ModifyMmdReg(interface, 0x1F, 0x0085, 0x000F, 0x0006);
92  lan8671ModifyMmdReg(interface, 0x1F, 0x008A, 0xF800, 0xC000);
93  lan8671ModifyMmdReg(interface, 0x1F, 0x0087, 0x801C, 0x801C);
94  lan8671ModifyMmdReg(interface, 0x1F, 0x0088, 0x1FFF, 0x033F);
95  lan8671ModifyMmdReg(interface, 0x1F, 0x008B, 0xFFFF, 0x0404);
96  lan8671ModifyMmdReg(interface, 0x1F, 0x0080, 0x0600, 0x0600);
97  lan8671ModifyMmdReg(interface, 0x1F, 0x00F1, 0x7F00, 0x2400);
98  lan8671ModifyMmdReg(interface, 0x1F, 0x0096, 0x2000, 0x2000);
99  lan8671ModifyMmdReg(interface, 0x1F, 0x0099, 0xFFFF, 0x7F80);
100 
101 #if (LAN8671_PLCA_SUPPORT == ENABLED)
102  //Set PLCA burst
105 
106  //Set PLCA node count and local ID
110 
111  //Enable PLCA
113 #else
114  //Disable PLCA
115  lan8671WriteMmdReg(interface, LAN8671_PLCA_CTRL0, 0);
116 #endif
117 
118  //Perform custom configuration
119  lan8671InitHook(interface);
120 
121  //Force the TCP/IP stack to poll the link state at startup
122  interface->phyEvent = TRUE;
123  //Notify the TCP/IP stack of the event
125 
126  //Successful initialization
127  return NO_ERROR;
128 }
129 
130 
131 /**
132  * @brief LAN8671 custom configuration
133  * @param[in] interface Underlying network interface
134  **/
135 
136 __weak_func void lan8671InitHook(NetInterface *interface)
137 {
138 }
139 
140 
141 /**
142  * @brief LAN8671 timer handler
143  * @param[in] interface Underlying network interface
144  **/
145 
146 void lan8671Tick(NetInterface *interface)
147 {
148  uint16_t value;
149  bool_t linkState;
150 
151  //No external interrupt line driver?
152  if(interface->extIntDriver == NULL)
153  {
154 #if (LAN8671_PLCA_SUPPORT == ENABLED)
155  //Read PLCA status register
157 
158  //The PST field indicates that the PLCA reconciliation sublayer is active
159  //and a BEACON is being regularly transmitted or received
160  linkState = (value & LAN8671_PLCA_STS_PST) ? TRUE : FALSE;
161 #else
162  //Link status indication is not supported
163  linkState = TRUE;
164 #endif
165 
166  //Link up event?
167  if(linkState && !interface->linkState)
168  {
169  //Set event flag
170  interface->phyEvent = TRUE;
171  //Notify the TCP/IP stack of the event
173  }
174  //Link down event?
175  else if(!linkState && interface->linkState)
176  {
177  //Set event flag
178  interface->phyEvent = TRUE;
179  //Notify the TCP/IP stack of the event
181  }
182  }
183 }
184 
185 
186 /**
187  * @brief Enable interrupts
188  * @param[in] interface Underlying network interface
189  **/
190 
192 {
193  //Enable PHY transceiver interrupts
194  if(interface->extIntDriver != NULL)
195  {
196  interface->extIntDriver->enableIrq();
197  }
198 }
199 
200 
201 /**
202  * @brief Disable interrupts
203  * @param[in] interface Underlying network interface
204  **/
205 
207 {
208  //Disable PHY transceiver interrupts
209  if(interface->extIntDriver != NULL)
210  {
211  interface->extIntDriver->disableIrq();
212  }
213 }
214 
215 
216 /**
217  * @brief LAN8671 event handler
218  * @param[in] interface Underlying network interface
219  **/
220 
222 {
223  uint16_t value;
224  bool_t linkState;
225 
226 #if (LAN8671_PLCA_SUPPORT == ENABLED)
227  //Read PLCA status register
229 
230  //The PST field indicates that the PLCA reconciliation sublayer is active
231  //and a BEACON is being regularly transmitted or received
232  linkState = (value & LAN8671_PLCA_STS_PST) ? TRUE : FALSE;
233 #else
234  //Link status indication is not supported
235  linkState = TRUE;
236 #endif
237 
238  //Link is up?
239  if(linkState)
240  {
241  //The PHY is only able to operate in 10 Mbps mode
242  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
243  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
244 
245  //Adjust MAC configuration parameters for proper operation
246  interface->nicDriver->updateMacConfig(interface);
247 
248  //Update link state
249  interface->linkState = TRUE;
250  }
251  else
252  {
253  //Update link state
254  interface->linkState = FALSE;
255  }
256 
257  //Process link state change event
258  nicNotifyLinkChange(interface);
259 }
260 
261 
262 /**
263  * @brief Write PHY register
264  * @param[in] interface Underlying network interface
265  * @param[in] address PHY register address
266  * @param[in] data Register value
267  **/
268 
269 void lan8671WritePhyReg(NetInterface *interface, uint8_t address,
270  uint16_t data)
271 {
272  //Write the specified PHY register
273  if(interface->smiDriver != NULL)
274  {
275  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
276  interface->phyAddr, address, data);
277  }
278  else
279  {
280  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
281  interface->phyAddr, address, data);
282  }
283 }
284 
285 
286 /**
287  * @brief Read PHY register
288  * @param[in] interface Underlying network interface
289  * @param[in] address PHY register address
290  * @return Register value
291  **/
292 
293 uint16_t lan8671ReadPhyReg(NetInterface *interface, uint8_t address)
294 {
295  uint16_t data;
296 
297  //Read the specified PHY register
298  if(interface->smiDriver != NULL)
299  {
300  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
301  interface->phyAddr, address);
302  }
303  else
304  {
305  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
306  interface->phyAddr, address);
307  }
308 
309  //Return the value of the PHY register
310  return data;
311 }
312 
313 
314 /**
315  * @brief Dump PHY registers for debugging purpose
316  * @param[in] interface Underlying network interface
317  **/
318 
320 {
321  uint8_t i;
322 
323  //Loop through PHY registers
324  for(i = 0; i < 32; i++)
325  {
326  //Display current PHY register
327  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
328  lan8671ReadPhyReg(interface, i));
329  }
330 
331  //Terminate with a line feed
332  TRACE_DEBUG("\r\n");
333 }
334 
335 
336 /**
337  * @brief Write MMD register
338  * @param[in] interface Underlying network interface
339  * @param[in] devAddr Device address
340  * @param[in] regAddr Register address
341  * @param[in] data MMD register value
342  **/
343 
344 void lan8671WriteMmdReg(NetInterface *interface, uint8_t devAddr,
345  uint16_t regAddr, uint16_t data)
346 {
347  //Select register operation
350 
351  //Write MMD register address
353 
354  //Select data operation
357 
358  //Write the content of the MMD register
360 }
361 
362 
363 /**
364  * @brief Read MMD register
365  * @param[in] interface Underlying network interface
366  * @param[in] devAddr Device address
367  * @param[in] regAddr Register address
368  * @return MMD register value
369  **/
370 
371 uint16_t lan8671ReadMmdReg(NetInterface *interface, uint8_t devAddr,
372  uint16_t regAddr)
373 {
374  //Select register operation
377 
378  //Write MMD register address
380 
381  //Select data operation
384 
385  //Read the content of the MMD register
386  return lan8671ReadPhyReg(interface, LAN8671_MMDAD);
387 }
388 
389 
390 /**
391  * @brief Modify MMD register
392  * @param[in] interface Underlying network interface
393  * @param[in] devAddr Device address
394  * @param[in] regAddr Register address
395  * @param[in] mask 16-bit mask
396  * @param[in] data 16-bit value
397  **/
398 
399 void lan8671ModifyMmdReg(NetInterface *interface, uint8_t devAddr,
400  uint16_t regAddr, uint16_t mask, uint16_t data)
401 {
402  uint16_t value;
403 
404  //Read the current value of the MMD register
405  value = lan8671ReadMmdReg(interface, devAddr, regAddr);
406  //Modify register value
407  value = (value & ~mask) | data;
408  //Write the modified value back to the MMD register
409  lan8671WriteMmdReg(interface, devAddr, regAddr, value);
410 }
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t data[]
Definition: ethernet.h:222
Ipv6Addr address[]
Definition: ipv6.h:316
void lan8671WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
void lan8671DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void lan8671Tick(NetInterface *interface)
LAN8671 timer handler.
void lan8671ModifyMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t mask, uint16_t data)
Modify MMD register.
uint16_t lan8671ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void lan8671EventHandler(NetInterface *interface)
LAN8671 event handler.
error_t lan8671Init(NetInterface *interface)
LAN8671 PHY transceiver initialization.
const PhyDriver lan8671PhyDriver
LAN8671 Ethernet PHY driver.
void lan8671DisableIrq(NetInterface *interface)
Disable interrupts.
void lan8671EnableIrq(NetInterface *interface)
Enable interrupts.
void lan8671WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
uint16_t lan8671ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
__weak_func void lan8671InitHook(NetInterface *interface)
LAN8671 custom configuration.
LAN8671 10Base-T1S Ethernet PHY driver.
#define LAN8671_PLCA_CTRL0
#define LAN8671_PLCA_CTRL0_EN
#define LAN8671_PLCA_BURST_BTMR_DEFAULT
#define LAN8671_PLCA_STS
#define LAN8671_PLCA_CTRL1_ID
#define LAN8671_NODE_COUNT
#define LAN8671_MMDCTRL_FNCTN_DATA_NO_POST_INC
#define LAN8671_PLCA_BURST_MAXBC_DISABLED
#define LAN8671_LOCAL_ID
#define LAN8671_PHY_ADDR
#define LAN8671_MMDAD
#define LAN8671_PLCA_STS_PST
#define LAN8671_MMDCTRL_DEVAD
#define LAN8671_PLCA_CTRL1_NCNT
#define LAN8671_PLCA_CTRL1
#define LAN8671_MMDCTRL_FNCTN_ADDR
#define LAN8671_MMDCTRL
#define LAN8671_PLCA_BURST
uint16_t regAddr
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netEvent
Definition: net_legacy.h:196
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:548
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define SMI_OPCODE_READ
Definition: nic.h:67
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
Ethernet PHY driver.
Definition: nic.h:308
uint8_t value[]
Definition: tcp.h:369
uint8_t mask
Definition: web_socket.h:319