dp83640_driver.c
Go to the documentation of this file.
1 /**
2  * @file dp83640_driver.c
3  * @brief DP83640 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 DP83640 Ethernet PHY driver
40  **/
41 
43 {
49 };
50 
51 
52 /**
53  * @brief DP83640 PHY transceiver initialization
54  * @param[in] interface Underlying network interface
55  * @return Error code
56  **/
57 
59 {
60  uint16_t value;
61 
62  //Debug message
63  TRACE_INFO("Initializing DP83640...\r\n");
64 
65  //Initialize external interrupt line driver
66  if(interface->extIntDriver != NULL)
67  interface->extIntDriver->init();
68 
69  //A software reset is accomplished by setting the RESET bit of the BMCR register
71  //Wait for the reset to complete
73 
74  //Dump PHY registers for debugging purpose
75  dp83640DumpPhyReg(interface);
76 
77  //Configure PWR_DOWN/INT pin as an interrupt output
79  //The PHY will generate interrupts when link status changes are detected
81 
82  //Select page 6
84  //Read PTP_COC register
86 
87  //If the CLK_OUT pin is to be used as a 50 MHz RMII clock, the default PTP
88  //clock output function must be disabled
90 
91  //Write PTP_COC register
93  //Select page 0
95 
96  //Force the TCP/IP stack to poll the link state at startup
97  interface->phyEvent = TRUE;
98  //Notify the TCP/IP stack of the event
100 
101  //Successful initialization
102  return NO_ERROR;
103 }
104 
105 
106 /**
107  * @brief DP83640 timer handler
108  * @param[in] interface Underlying network interface
109  **/
110 
111 void dp83640Tick(NetInterface *interface)
112 {
113  uint16_t value;
114  bool_t linkState;
115 
116  //No external interrupt line driver?
117  if(interface->extIntDriver == NULL)
118  {
119  //Read basic status register
121  //Retrieve current link state
122  linkState = (value & BMSR_LINK_STATUS) ? TRUE : FALSE;
123 
124  //Link up event?
125  if(linkState && !interface->linkState)
126  {
127  //Set event flag
128  interface->phyEvent = TRUE;
129  //Notify the TCP/IP stack of the event
131  }
132  //Link down event?
133  else if(!linkState && interface->linkState)
134  {
135  //Set event flag
136  interface->phyEvent = TRUE;
137  //Notify the TCP/IP stack of the event
139  }
140  }
141 }
142 
143 
144 /**
145  * @brief Enable interrupts
146  * @param[in] interface Underlying network interface
147  **/
148 
150 {
151  //Enable PHY transceiver interrupts
152  if(interface->extIntDriver != NULL)
153  interface->extIntDriver->enableIrq();
154 }
155 
156 
157 /**
158  * @brief Disable interrupts
159  * @param[in] interface Underlying network interface
160  **/
161 
163 {
164  //Disable PHY transceiver interrupts
165  if(interface->extIntDriver != NULL)
166  interface->extIntDriver->disableIrq();
167 }
168 
169 
170 /**
171  * @brief DP83640 event handler
172  * @param[in] interface Underlying network interface
173  **/
174 
176 {
177  uint16_t status;
178 
179  //Read status register to acknowledge the interrupt
180  status = dp83640ReadPhyReg(interface, DP83640_PHY_REG_MISR);
181 
182  //Link status change?
183  if(status & MISR_LINK_INT)
184  {
185  //Read PHY status register
186  status = dp83640ReadPhyReg(interface, DP83640_PHY_REG_PHYSTS);
187 
188  //Link is up?
189  if(status & PHYSTS_LINK_STATUS)
190  {
191  //Check current speed
192  if(status & PHYSTS_SPEED_STATUS)
193  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
194  else
195  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
196 
197  //Check duplex mode
198  if(status & PHYSTS_DUPLEX_STATUS)
199  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
200  else
201  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
202 
203  //Update link state
204  interface->linkState = TRUE;
205 
206  //Adjust MAC configuration parameters for proper operation
207  interface->nicDriver->updateMacConfig(interface);
208  }
209  else
210  {
211  //Update link state
212  interface->linkState = FALSE;
213  }
214 
215  //Process link state change event
216  nicNotifyLinkChange(interface);
217  }
218 }
219 
220 
221 /**
222  * @brief Write PHY register
223  * @param[in] interface Underlying network interface
224  * @param[in] address PHY register address
225  * @param[in] data Register value
226  **/
227 
228 void dp83640WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
229 {
230  uint8_t phyAddr;
231 
232  //Get the address of the PHY transceiver
233  if(interface->phyAddr < 32)
234  phyAddr = interface->phyAddr;
235  else
236  phyAddr = DP83640_PHY_ADDR;
237 
238  //Write the specified PHY register
239  interface->nicDriver->writePhyReg(phyAddr, address, data);
240 }
241 
242 
243 /**
244  * @brief Read PHY register
245  * @param[in] interface Underlying network interface
246  * @param[in] address PHY register address
247  * @return Register value
248  **/
249 
250 uint16_t dp83640ReadPhyReg(NetInterface *interface, uint8_t address)
251 {
252  uint8_t phyAddr;
253 
254  //Get the address of the PHY transceiver
255  if(interface->phyAddr < 32)
256  phyAddr = interface->phyAddr;
257  else
258  phyAddr = DP83640_PHY_ADDR;
259 
260  //Read the specified PHY register
261  return interface->nicDriver->readPhyReg(phyAddr, address);
262 }
263 
264 
265 /**
266  * @brief Dump PHY registers for debugging purpose
267  * @param[in] interface Underlying network interface
268  **/
269 
271 {
272  uint8_t i;
273 
274  //Loop through PHY registers
275  for(i = 0; i < 32; i++)
276  {
277  //Display current PHY register
278  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, dp83640ReadPhyReg(interface, i));
279  }
280 
281  //Terminate with a line feed
282  TRACE_DEBUG("\r\n");
283 }
error_t dp83640Init(NetInterface *interface)
DP83640 PHY transceiver initialization.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
#define PHYSTS_LINK_STATUS
TCP/IP stack core.
Debugging facilities.
#define MICR_INTEN
#define DP83640_PHY_REG_MICR
#define BMSR_LINK_STATUS
Definition: ar8031_driver.h:95
void dp83640EnableIrq(NetInterface *interface)
Enable interrupts.
void dp83640EventHandler(NetInterface *interface)
DP83640 event handler.
void dp83640DisableIrq(NetInterface *interface)
Disable interrupts.
#define TRUE
Definition: os_port.h:48
#define PHYSTS_SPEED_STATUS
uint16_t dp83640ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define DP83640_PHY_REG_PHYSTS
const PhyDriver dp83640PhyDriver
DP83640 Ethernet PHY driver.
#define DP83640_PHY_REG_BMCR
PHY driver.
Definition: nic.h:196
void dp83640Tick(NetInterface *interface)
DP83640 timer handler.
#define PHYSTS_DUPLEX_STATUS
#define DP83640_PHY_REG_MISR
#define DP83640_PHY_REG_PAGESEL
#define BMCR_RESET
Definition: ar8031_driver.h:71
#define DP83640_PHY_REG_PTP_COC
#define PTP_COC_PTP_CLKOUT_EN
#define DP83640_PHY_ADDR
DP83640 Ethernet PHY transceiver.
void dp83640WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define TRACE_INFO(...)
Definition: debug.h:86
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
uint8_t data[]
Definition: dtls_misc.h:167
#define NetInterface
Definition: net.h:34
uint8_t value[]
Definition: dtls_misc.h:141
#define MICR_INT_OE
#define DP83640_PHY_REG_BMSR
#define MISR_LINK_INT_EN
#define FALSE
Definition: os_port.h:44
void dp83640DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define MISR_LINK_INT
int bool_t
Definition: compiler_port.h:47
#define TRACE_DEBUG(...)
Definition: debug.h:98