ncn26000_driver.c
Go to the documentation of this file.
1 /**
2  * @file ncn26000_driver.c
3  * @brief NCN26000 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.4
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 NCN26000 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief NCN26000 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing NCN26000...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = NCN26000_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  ncn26000DumpPhyReg(interface);
86 
87  //Perform custom configuration
88  ncn26000InitHook(interface);
89 
90  //Force the TCP/IP stack to poll the link state at startup
91  interface->phyEvent = TRUE;
92  //Notify the TCP/IP stack of the event
94 
95  //Successful initialization
96  return NO_ERROR;
97 }
98 
99 
100 /**
101  * @brief NCN26000 custom configuration
102  * @param[in] interface Underlying network interface
103  **/
104 
105 __weak_func void ncn26000InitHook(NetInterface *interface)
106 {
107 #if (NCN26000_PLCA_SUPPORT == ENABLED)
108  //Set PLCA burst
112 
113  //Set PLCA node count and local ID
117 
118  //Enable PLCA
120 #else
121  //Disable PLCA
123 #endif
124 }
125 
126 
127 /**
128  * @brief NCN26000 timer handler
129  * @param[in] interface Underlying network interface
130  **/
131 
132 void ncn26000Tick(NetInterface *interface)
133 {
134  uint16_t value;
135  bool_t linkState;
136 
137  //No external interrupt line driver?
138  if(interface->extIntDriver == NULL)
139  {
140 #if (NCN26000_PLCA_SUPPORT == ENABLED)
141  //Read PLCA status register
143 
144  //The PST field indicates that the PLCA reconciliation sublayer is active
145  //and a BEACON is being regularly transmitted or received
146  linkState = (value & NCN26000_PLCA_STATUS_PST) ? TRUE : FALSE;
147 #else
148  //Link status indication is not supported
149  linkState = TRUE;
150 #endif
151 
152  //Link up event?
153  if(linkState && !interface->linkState)
154  {
155  //Set event flag
156  interface->phyEvent = TRUE;
157  //Notify the TCP/IP stack of the event
159  }
160  //Link down event?
161  else if(!linkState && interface->linkState)
162  {
163  //Set event flag
164  interface->phyEvent = TRUE;
165  //Notify the TCP/IP stack of the event
167  }
168  }
169 }
170 
171 
172 /**
173  * @brief Enable interrupts
174  * @param[in] interface Underlying network interface
175  **/
176 
178 {
179  //Enable PHY transceiver interrupts
180  if(interface->extIntDriver != NULL)
181  {
182  interface->extIntDriver->enableIrq();
183  }
184 }
185 
186 
187 /**
188  * @brief Disable interrupts
189  * @param[in] interface Underlying network interface
190  **/
191 
193 {
194  //Disable PHY transceiver interrupts
195  if(interface->extIntDriver != NULL)
196  {
197  interface->extIntDriver->disableIrq();
198  }
199 }
200 
201 
202 /**
203  * @brief NCN26000 event handler
204  * @param[in] interface Underlying network interface
205  **/
206 
208 {
209  uint16_t value;
210  bool_t linkState;
211 
212 #if (NCN26000_PLCA_SUPPORT == ENABLED)
213  //Read PLCA status register
215 
216  //The PST field indicates that the PLCA reconciliation sublayer is active
217  //and a BEACON is being regularly transmitted or received
218  linkState = (value & NCN26000_PLCA_STATUS_PST) ? TRUE : FALSE;
219 #else
220  //Link status indication is not supported
221  linkState = TRUE;
222 #endif
223 
224  //Link is up?
225  if(linkState)
226  {
227  //The PHY is only able to operate in 10 Mbps mode
228  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
229  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
230 
231  //Adjust MAC configuration parameters for proper operation
232  interface->nicDriver->updateMacConfig(interface);
233 
234  //Update link state
235  interface->linkState = TRUE;
236  }
237  else
238  {
239  //Update link state
240  interface->linkState = FALSE;
241  }
242 
243  //Process link state change event
244  nicNotifyLinkChange(interface);
245 }
246 
247 
248 /**
249  * @brief Write PHY register
250  * @param[in] interface Underlying network interface
251  * @param[in] address PHY register address
252  * @param[in] data Register value
253  **/
254 
255 void ncn26000WritePhyReg(NetInterface *interface, uint8_t address,
256  uint16_t data)
257 {
258  //Write the specified PHY register
259  if(interface->smiDriver != NULL)
260  {
261  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
262  interface->phyAddr, address, data);
263  }
264  else
265  {
266  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
267  interface->phyAddr, address, data);
268  }
269 }
270 
271 
272 /**
273  * @brief Read PHY register
274  * @param[in] interface Underlying network interface
275  * @param[in] address PHY register address
276  * @return Register value
277  **/
278 
279 uint16_t ncn26000ReadPhyReg(NetInterface *interface, uint8_t address)
280 {
281  uint16_t data;
282 
283  //Read the specified PHY register
284  if(interface->smiDriver != NULL)
285  {
286  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
287  interface->phyAddr, address);
288  }
289  else
290  {
291  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
292  interface->phyAddr, address);
293  }
294 
295  //Return the value of the PHY register
296  return data;
297 }
298 
299 
300 /**
301  * @brief Dump PHY registers for debugging purpose
302  * @param[in] interface Underlying network interface
303  **/
304 
306 {
307  uint8_t i;
308 
309  //Loop through PHY registers
310  for(i = 0; i < 32; i++)
311  {
312  //Display current PHY register
313  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
314  ncn26000ReadPhyReg(interface, i));
315  }
316 
317  //Terminate with a line feed
318  TRACE_DEBUG("\r\n");
319 }
320 
321 
322 /**
323  * @brief Write MMD register
324  * @param[in] interface Underlying network interface
325  * @param[in] devAddr Device address
326  * @param[in] regAddr Register address
327  * @param[in] data MMD register value
328  **/
329 
330 void ncn26000WriteMmdReg(NetInterface *interface, uint8_t devAddr,
331  uint16_t regAddr, uint16_t data)
332 {
333  //Select register operation
336 
337  //Write MMD register address
339 
340  //Select data operation
343 
344  //Write the content of the MMD register
346 }
347 
348 
349 /**
350  * @brief Read MMD register
351  * @param[in] interface Underlying network interface
352  * @param[in] devAddr Device address
353  * @param[in] regAddr Register address
354  * @return MMD register value
355  **/
356 
357 uint16_t ncn26000ReadMmdReg(NetInterface *interface, uint8_t devAddr,
358  uint16_t regAddr)
359 {
360  //Select register operation
363 
364  //Write MMD register address
366 
367  //Select data operation
370 
371  //Read the content of the MMD register
372  return ncn26000ReadPhyReg(interface, NCN26000_MAADR);
373 }
374 
375 
376 /**
377  * @brief Modify MMD register
378  * @param[in] interface Underlying network interface
379  * @param[in] devAddr Device address
380  * @param[in] regAddr Register address
381  * @param[in] mask 16-bit mask
382  * @param[in] data 16-bit value
383  **/
384 
385 void ncn26000ModifyMmdReg(NetInterface *interface, uint8_t devAddr,
386  uint16_t regAddr, uint16_t mask, uint16_t data)
387 {
388  uint16_t value;
389 
390  //Read the current value of the MMD register
391  value = ncn26000ReadMmdReg(interface, devAddr, regAddr);
392  //Modify register value
393  value = (value & ~mask) | data;
394  //Write the modified value back to the MMD register
395  ncn26000WriteMmdReg(interface, devAddr, regAddr, value);
396 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
#define NCN26000_PLCA_BURST_MODE_IFG_COMP_TMR_DEFAULT
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
#define NCN26000_PLCA_CTRL1_ID
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
void ncn26000Tick(NetInterface *interface)
NCN26000 timer handler.
#define NCN26000_PLCA_BURST_MODE_MAX_BURST_COUNT_DEFAULT
#define NCN26000_MAADR
#define NCN26000_MACR
NCN26000 10Base-T1S Ethernet PHY driver.
#define SMI_OPCODE_WRITE
Definition: nic.h:66
void ncn26000EnableIrq(NetInterface *interface)
Enable interrupts.
#define FALSE
Definition: os_port.h:46
#define NCN26000_MACR_DEVADD
void ncn26000DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define NCN26000_PLCA_STATUS
error_t
Error codes.
Definition: error.h:43
__weak_func void ncn26000InitHook(NetInterface *interface)
NCN26000 custom configuration.
#define NCN26000_PLCA_CTRL1_NCNT
uint16_t ncn26000ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
void ncn26000WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define NCN26000_MACR_FUNC_DATA_NO_POST_INC
uint8_t mask
Definition: web_socket.h:319
#define SMI_OPCODE_READ
Definition: nic.h:67
void ncn26000WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
#define TRACE_INFO(...)
Definition: debug.h:95
const PhyDriver ncn26000PhyDriver
NCN26000 Ethernet PHY driver.
#define NCN26000_LOCAL_ID
#define TRACE_DEBUG(...)
Definition: debug.h:107
void ncn26000EventHandler(NetInterface *interface)
NCN26000 event handler.
uint16_t ncn26000ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
uint16_t regAddr
#define NCN26000_PLCA_BURST_MODE
Ipv6Addr address[]
Definition: ipv6.h:325
#define NCN26000_PLCA_CTRL1
#define NCN26000_NODE_COUNT
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
uint8_t value[]
Definition: tcp.h:369
void ncn26000ModifyMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t mask, uint16_t data)
Modify MMD register.
error_t ncn26000Init(NetInterface *interface)
NCN26000 PHY transceiver initialization.
#define NCN26000_PHY_ADDR
#define NCN26000_PLCA_STATUS_PST
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define NCN26000_MACR_FUNC_ADDR
#define NCN26000_PLCA_CTRL0
TCP/IP stack core.
void ncn26000DisableIrq(NetInterface *interface)
Disable interrupts.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define NCN26000_PLCA_CTRL0_EN