dp83td510_driver.c
Go to the documentation of this file.
1 /**
2  * @file dp83td510_driver.c
3  * @brief DP83TD510 10Base-T1L 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 DP83TD510 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief DP83TD510 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing DP83TD510...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = DP83TD510_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  //Reset PHY transceiver
87 
88  //Wait for the reset to complete
89  while(dp83td510ReadPhyReg(interface, DP83TD510_MII_REG_0) &
91  {
92  }
93 
94  //Dump PHY registers for debugging purpose
95  dp83td510DumpPhyReg(interface);
96 
97  //Perform custom configuration
98  dp83td510InitHook(interface);
99 
100  //Force the TCP/IP stack to poll the link state at startup
101  interface->phyEvent = TRUE;
102  //Notify the TCP/IP stack of the event
104 
105  //Successful initialization
106  return NO_ERROR;
107 }
108 
109 
110 /**
111  * @brief DP83TD510 custom configuration
112  * @param[in] interface Underlying network interface
113  **/
114 
115 __weak_func void dp83td510InitHook(NetInterface *interface)
116 {
117 }
118 
119 
120 /**
121  * @brief DP83TD510 timer handler
122  * @param[in] interface Underlying network interface
123  **/
124 
125 void dp83td510Tick(NetInterface *interface)
126 {
127  uint16_t value;
128  bool_t linkState;
129 
130  //No external interrupt line driver?
131  if(interface->extIntDriver == NULL)
132  {
133  //Read PHY status register
135  //Retrieve current link state
136  linkState = (value & DP83TD510_PHY_STS_LINK_STATUS) ? TRUE : FALSE;
137 
138  //Link up event?
139  if(linkState && !interface->linkState)
140  {
141  //Set event flag
142  interface->phyEvent = TRUE;
143  //Notify the TCP/IP stack of the event
145  }
146  //Link down event?
147  else if(!linkState && interface->linkState)
148  {
149  //Set event flag
150  interface->phyEvent = TRUE;
151  //Notify the TCP/IP stack of the event
153  }
154  }
155 }
156 
157 
158 /**
159  * @brief Enable interrupts
160  * @param[in] interface Underlying network interface
161  **/
162 
164 {
165  //Enable PHY transceiver interrupts
166  if(interface->extIntDriver != NULL)
167  {
168  interface->extIntDriver->enableIrq();
169  }
170 }
171 
172 
173 /**
174  * @brief Disable interrupts
175  * @param[in] interface Underlying network interface
176  **/
177 
179 {
180  //Disable PHY transceiver interrupts
181  if(interface->extIntDriver != NULL)
182  {
183  interface->extIntDriver->disableIrq();
184  }
185 }
186 
187 
188 /**
189  * @brief DP83TD510 event handler
190  * @param[in] interface Underlying network interface
191  **/
192 
194 {
195  uint16_t value;
196 
197  //Read PHY status register
199 
200  //Link is up?
202  {
203  //The PHY is only able to operate in 10 Mbps mode
204  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
205  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
206 
207  //Adjust MAC configuration parameters for proper operation
208  interface->nicDriver->updateMacConfig(interface);
209 
210  //Update link state
211  interface->linkState = TRUE;
212  }
213  else
214  {
215  //Update link state
216  interface->linkState = FALSE;
217  }
218 
219  //Process link state change event
220  nicNotifyLinkChange(interface);
221 }
222 
223 
224 /**
225  * @brief Write PHY register
226  * @param[in] interface Underlying network interface
227  * @param[in] address PHY register address
228  * @param[in] data Register value
229  **/
230 
231 void dp83td510WritePhyReg(NetInterface *interface, uint8_t address,
232  uint16_t data)
233 {
234  //Write the specified PHY register
235  if(interface->smiDriver != NULL)
236  {
237  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
238  interface->phyAddr, address, data);
239  }
240  else
241  {
242  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
243  interface->phyAddr, address, data);
244  }
245 }
246 
247 
248 /**
249  * @brief Read PHY register
250  * @param[in] interface Underlying network interface
251  * @param[in] address PHY register address
252  * @return Register value
253  **/
254 
255 uint16_t dp83td510ReadPhyReg(NetInterface *interface, uint8_t address)
256 {
257  uint16_t data;
258 
259  //Read the specified PHY register
260  if(interface->smiDriver != NULL)
261  {
262  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
263  interface->phyAddr, address);
264  }
265  else
266  {
267  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
268  interface->phyAddr, address);
269  }
270 
271  //Return the value of the PHY register
272  return data;
273 }
274 
275 
276 /**
277  * @brief Dump PHY registers for debugging purpose
278  * @param[in] interface Underlying network interface
279  **/
280 
282 {
283  uint8_t i;
284 
285  //Loop through PHY registers
286  for(i = 0; i < 32; i++)
287  {
288  //Display current PHY register
289  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
290  dp83td510ReadPhyReg(interface, i));
291  }
292 
293  //Terminate with a line feed
294  TRACE_DEBUG("\r\n");
295 }
296 
297 
298 /**
299  * @brief Write MMD register
300  * @param[in] interface Underlying network interface
301  * @param[in] devAddr Device address
302  * @param[in] regAddr Register address
303  * @param[in] data MMD register value
304  **/
305 
306 void dp83td510WriteMmdReg(NetInterface *interface, uint8_t devAddr,
307  uint16_t regAddr, uint16_t data)
308 {
309  //Select register operation
312 
313  //Write MMD register address
315 
316  //Select data operation
319 
320  //Write the content of the MMD register
322 }
323 
324 
325 /**
326  * @brief Read MMD register
327  * @param[in] interface Underlying network interface
328  * @param[in] devAddr Device address
329  * @param[in] regAddr Register address
330  * @return MMD register value
331  **/
332 
333 uint16_t dp83td510ReadMmdReg(NetInterface *interface, uint8_t devAddr,
334  uint16_t regAddr)
335 {
336  //Select register operation
339 
340  //Write MMD register address
342 
343  //Select data operation
346 
347  //Read the content of the MMD register
348  return dp83td510ReadPhyReg(interface, DP83TD510_ADDAR);
349 }
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
__weak_func void dp83td510InitHook(NetInterface *interface)
DP83TD510 custom configuration.
void dp83td510EnableIrq(NetInterface *interface)
Enable interrupts.
error_t dp83td510Init(NetInterface *interface)
DP83TD510 PHY transceiver initialization.
void dp83td510DisableIrq(NetInterface *interface)
Disable interrupts.
void dp83td510EventHandler(NetInterface *interface)
DP83TD510 event handler.
void dp83td510WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
void dp83td510WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
uint16_t dp83td510ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
void dp83td510DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void dp83td510Tick(NetInterface *interface)
DP83TD510 timer handler.
uint16_t dp83td510ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
const PhyDriver dp83td510PhyDriver
DP83TD510 Ethernet PHY driver.
DP83TD510 10Base-T1L Ethernet PHY driver.
#define DP83TD510_ADDAR
#define DP83TD510_REGCR_CMD_DATA_NO_POST_INC
#define DP83TD510_REGCR_DEVAD
#define DP83TD510_MII_REG_0
#define DP83TD510_PHY_ADDR
#define DP83TD510_MII_REG_0_MII_RESET
#define DP83TD510_PHY_STS
#define DP83TD510_REGCR
#define DP83TD510_PHY_STS_LINK_STATUS
#define DP83TD510_REGCR_CMD_ADDR
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
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_FULL_DUPLEX_MODE
Definition: nic.h:125
@ 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