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