ip101_driver.c
Go to the documentation of this file.
1 /**
2  * @file ip101_driver.c
3  * @brief IC+ IP101 Ethernet PHY transceiver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 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 1.9.6
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 IP101 Ethernet PHY driver
42  **/
43 
45 {
46  ip101Init,
47  ip101Tick,
51  NULL,
52  NULL
53 };
54 
55 
56 /**
57  * @brief IP101 PHY transceiver initialization
58  * @param[in] interface Underlying network interface
59  * @return Error code
60  **/
61 
63 {
64  //Debug message
65  TRACE_INFO("Initializing IP101...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = IP101_PHY_ADDR;
72  }
73 
74  //Reset PHY transceiver
76 
77  //Wait for the reset to complete
78  while(ip101ReadPhyReg(interface, IP101_BMCR) & IP101_BMCR_RESET)
79  {
80  }
81 
82  //Dump PHY registers for debugging purpose
83  ip101DumpPhyReg(interface);
84 
85  //Force the TCP/IP stack to poll the link state at startup
86  interface->phyEvent = TRUE;
87  //Notify the TCP/IP stack of the event
89 
90  //Successful initialization
91  return NO_ERROR;
92 }
93 
94 
95 /**
96  * @brief IP101 timer handler
97  * @param[in] interface Underlying network interface
98  **/
99 
100 void ip101Tick(NetInterface *interface)
101 {
102  uint16_t value;
103  bool_t linkState;
104 
105  //Read basic status register
106  value = ip101ReadPhyReg(interface, IP101_PHYMCSSR);
107  //Retrieve current link state
108  linkState = (value & IP101_PHYMCSSR_LINK_UP) ? TRUE : FALSE;
109 
110  //Link up event?
111  if(linkState && !interface->linkState)
112  {
113  //Set event flag
114  interface->phyEvent = TRUE;
115  //Notify the TCP/IP stack of the event
117  }
118  //Link down event?
119  else if(!linkState && interface->linkState)
120  {
121  //Set event flag
122  interface->phyEvent = TRUE;
123  //Notify the TCP/IP stack of the event
125  }
126 }
127 
128 
129 /**
130  * @brief Enable interrupts
131  * @param[in] interface Underlying network interface
132  **/
133 
134 void ip101EnableIrq(NetInterface *interface)
135 {
136 }
137 
138 
139 /**
140  * @brief Disable interrupts
141  * @param[in] interface Underlying network interface
142  **/
143 
145 {
146 }
147 
148 
149 /**
150  * @brief IP101 event handler
151  * @param[in] interface Underlying network interface
152  **/
153 
155 {
156  uint16_t value;
157 
158  //Read PHY status register
159  value = ip101ReadPhyReg(interface, IP101_PHYMCSSR);
160 
161  //Link is up?
163  {
164  //Check current operation mode
165  switch(value & IP101_PHYMCSSR_OP_MODE)
166  {
167  //10BASE-T half-duplex
169  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
170  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
171  break;
172  //10BASE-T full-duplex
174  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
175  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
176  break;
177  //100BASE-TX half-duplex
179  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
180  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
181  break;
182  //100BASE-TX full-duplex
184  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
185  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
186  break;
187  //Unknown operation mode
188  default:
189  //Debug message
190  TRACE_WARNING("Invalid operation mode!\r\n");
191  break;
192  }
193 
194  //Update link state
195  interface->linkState = TRUE;
196 
197  //Adjust MAC configuration parameters for proper operation
198  interface->nicDriver->updateMacConfig(interface);
199  }
200  else
201  {
202  //Update link state
203  interface->linkState = FALSE;
204  }
205 
206  //Process link state change event
207  nicNotifyLinkChange(interface);
208 }
209 
210 
211 /**
212  * @brief Write PHY register
213  * @param[in] interface Underlying network interface
214  * @param[in] address PHY register address
215  * @param[in] data Register value
216  **/
217 
218 void ip101WritePhyReg(NetInterface *interface, uint8_t address,
219  uint16_t data)
220 {
221  //Write the specified PHY register
222  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
223  interface->phyAddr, address, data);
224 }
225 
226 
227 /**
228  * @brief Read PHY register
229  * @param[in] interface Underlying network interface
230  * @param[in] address PHY register address
231  * @return Register value
232  **/
233 
234 uint16_t ip101ReadPhyReg(NetInterface *interface, uint8_t address)
235 {
236  //Read the specified PHY register
237  return interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
238  interface->phyAddr, address);
239 }
240 
241 
242 /**
243  * @brief Dump PHY registers for debugging purpose
244  * @param[in] interface Underlying network interface
245  **/
246 
248 {
249  uint8_t i;
250 
251  //Loop through PHY registers
252  for(i = 0; i < 32; i++)
253  {
254  //Display current PHY register
255  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
256  ip101ReadPhyReg(interface, i));
257  }
258 
259  //Terminate with a line feed
260  TRACE_DEBUG("\r\n");
261 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
int bool_t
Definition: compiler_port.h:49
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:119
#define IP101_PHYMCSSR_OP_MODE
Definition: ip101_driver.h:191
#define TRUE
Definition: os_port.h:50
PHY driver.
Definition: nic.h:214
#define IP101_PHYMCSSR_OP_MODE_10M_HD
Definition: ip101_driver.h:193
#define SMI_OPCODE_WRITE
Definition: nic.h:62
#define IP101_PHYMCSSR
Definition: ip101_driver.h:60
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:105
const PhyDriver ip101PhyDriver
IP101 Ethernet PHY driver.
Definition: ip101_driver.c:44
void ip101DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
Definition: ip101_driver.c:247
OsEvent netEvent
Definition: net.c:77
#define SMI_OPCODE_READ
Definition: nic.h:63
#define TRACE_INFO(...)
Definition: debug.h:94
void ip101EventHandler(NetInterface *interface)
IP101 event handler.
Definition: ip101_driver.c:154
uint16_t ip101ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
Definition: ip101_driver.c:234
#define IP101_PHY_ADDR
Definition: ip101_driver.h:39
#define IP101_PHYMCSSR_LINK_UP
Definition: ip101_driver.h:189
#define IP101_PHYMCSSR_OP_MODE_10M_FD
Definition: ip101_driver.h:195
void ip101EnableIrq(NetInterface *interface)
Enable interrupts.
Definition: ip101_driver.c:134
void ip101Tick(NetInterface *interface)
IP101 timer handler.
Definition: ip101_driver.c:100
#define IP101_PHYMCSSR_OP_MODE_100M_FD
Definition: ip101_driver.h:196
#define TRACE_WARNING(...)
Definition: debug.h:84
#define TRACE_DEBUG(...)
Definition: debug.h:106
#define IP101_PHYMCSSR_OP_MODE_100M_HD
Definition: ip101_driver.h:194
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:118
error_t ip101Init(NetInterface *interface)
IP101 PHY transceiver initialization.
Definition: ip101_driver.c:62
void ip101DisableIrq(NetInterface *interface)
Disable interrupts.
Definition: ip101_driver.c:144
IC+ IP101 Ethernet PHY transceiver.
#define IP101_BMCR
Definition: ip101_driver.h:45
void ip101WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
Definition: ip101_driver.c:218
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:106
uint8_t value[]
Definition: dtls_misc.h:150
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define IP101_BMCR_RESET
Definition: ip101_driver.h:63