st802rt1a_driver.c
Go to the documentation of this file.
1 /**
2  * @file st802rt1a_driver.c
3  * @brief ST802RT1A 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 ST802RT1A Ethernet PHY driver
40  **/
41 
43 {
49 };
50 
51 
52 /**
53  * @brief ST802RT1A PHY transceiver initialization
54  * @param[in] interface Underlying network interface
55  * @return Error code
56  **/
57 
59 {
60  //Debug message
61  TRACE_INFO("Initializing ST802RT1A...\r\n");
62 
63  //Reset PHY transceiver
65  //Wait for the reset to complete
67 
68  //Dump PHY registers for debugging purpose
69  st802rt1aDumpPhyReg(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 ST802RT1A timer handler
83  * @param[in] interface Underlying network interface
84  **/
85 
86 void st802rt1aTick(NetInterface *interface)
87 {
88  uint16_t value;
89  bool_t linkState;
90 
91  //Read status register
93  //Retrieve current link state
94  linkState = (value & RN01_LINK_STATUS) ? 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 
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 ST802RT1A event handler
137  * @param[in] interface Underlying network interface
138  **/
139 
141 {
142  uint16_t value;
143  bool_t linkState;
144 
145  //Read status register
147  //Retrieve current link state
148  linkState = (value & RN01_LINK_STATUS) ? TRUE : FALSE;
149 
150  //Link is up?
151  if(linkState && !interface->linkState)
152  {
153  //Read RN13 register
155 
156  //Check current operation mode
157  switch(value & RN13_CMODE_MASK)
158  {
159  //10BASE-T
160  case RN13_CMODE_10BT:
161  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
162  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
163  break;
164  //10BASE-T full-duplex
165  case RN13_CMODE_10BT_FD:
166  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
167  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
168  break;
169  //100BASE-TX
170  case RN13_CMODE_100BTX:
171  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
172  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
173  break;
174  //100BASE-TX full-duplex
176  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
177  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
178  break;
179  //Unknown operation mode
180  default:
181  //Debug message
182  TRACE_WARNING("Invalid Duplex mode\r\n");
183  break;
184  }
185 
186  //Update link state
187  interface->linkState = TRUE;
188 
189  //Adjust MAC configuration parameters for proper operation
190  interface->nicDriver->updateMacConfig(interface);
191 
192  //Process link state change event
193  nicNotifyLinkChange(interface);
194  }
195  //Link is down?
196  else if(!linkState && interface->linkState)
197  {
198  //Update link state
199  interface->linkState = FALSE;
200 
201  //Process link state change event
202  nicNotifyLinkChange(interface);
203  }
204 }
205 
206 
207 /**
208  * @brief Write PHY register
209  * @param[in] interface Underlying network interface
210  * @param[in] address PHY register Register address
211  * @param[in] data Register value
212  **/
213 
214 void st802rt1aWritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
215 {
216  uint8_t phyAddr;
217 
218  //Get the address of the PHY transceiver
219  if(interface->phyAddr < 32)
220  phyAddr = interface->phyAddr;
221  else
222  phyAddr = ST802RT1A_PHY_ADDR;
223 
224  //Write the specified PHY register
225  interface->nicDriver->writePhyReg(phyAddr, address, data);
226 }
227 
228 
229 /**
230  * @brief Read PHY register
231  * @param[in] interface Underlying network interface
232  * @param[in] address PHY register address
233  * @return Register value
234  **/
235 
236 uint16_t st802rt1aReadPhyReg(NetInterface *interface, uint8_t address)
237 {
238  uint8_t phyAddr;
239 
240  //Get the address of the PHY transceiver
241  if(interface->phyAddr < 32)
242  phyAddr = interface->phyAddr;
243  else
244  phyAddr = ST802RT1A_PHY_ADDR;
245 
246  //Read the specified PHY register
247  return interface->nicDriver->readPhyReg(phyAddr, address);
248 }
249 
250 
251 /**
252  * @brief Dump PHY registers for debugging purpose
253  * @param[in] interface Underlying network interface
254  **/
255 
257 {
258  uint8_t i;
259 
260  //Loop through PHY registers
261  for(i = 0; i < 32; i++)
262  {
263  //Display current PHY register
264  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, st802rt1aReadPhyReg(interface, i));
265  }
266 
267  //Terminate with a line feed
268  TRACE_DEBUG("\r\n");
269 }
void st802rt1aDumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
TCP/IP stack core.
void st802rt1aEnableIrq(NetInterface *interface)
Enable interrupts.
Debugging facilities.
#define RN01_LINK_STATUS
#define ST802RT1A_PHY_REG_RN01
void st802rt1aTick(NetInterface *interface)
ST802RT1A timer handler.
#define ST802RT1A_PHY_ADDR
#define TRUE
Definition: os_port.h:48
PHY driver.
Definition: nic.h:196
#define RN13_CMODE_10BT_FD
#define RN13_CMODE_100BTX
const PhyDriver st802rt1aPhyDriver
ST802RT1A Ethernet PHY driver.
#define TRACE_INFO(...)
Definition: debug.h:86
error_t st802rt1aInit(NetInterface *interface)
ST802RT1A PHY transceiver initialization.
Success.
Definition: error.h:42
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
void st802rt1aWritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define RN00_SOFT_RESET
uint8_t data[]
Definition: dtls_misc.h:167
#define NetInterface
Definition: net.h:34
uint8_t value[]
Definition: dtls_misc.h:141
uint16_t st802rt1aReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define RN13_CMODE_100BTX_FD
void st802rt1aEventHandler(NetInterface *interface)
ST802RT1A event handler.
#define RN13_CMODE_MASK
#define ST802RT1A_PHY_REG_RN00
#define RN13_CMODE_10BT
ST802RT1A Ethernet PHY transceiver.
#define FALSE
Definition: os_port.h:44
int bool_t
Definition: compiler_port.h:47
#define ST802RT1A_PHY_REG_RN13
void st802rt1aDisableIrq(NetInterface *interface)
Disable interrupts.
#define TRACE_DEBUG(...)
Definition: debug.h:98