tja1101_driver.c
Go to the documentation of this file.
1 /**
2  * @file tja1101_driver.c
3  * @brief TJA1101 100Base-T1 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 TJA1101 Ethernet PHY driver
42  **/
43 
45 {
51  NULL,
52  NULL
53 };
54 
55 
56 /**
57  * @brief TJA1101 PHY transceiver initialization
58  * @param[in] interface Underlying network interface
59  * @return Error code
60  **/
61 
63 {
64  uint16_t value;
65 
66  //Debug message
67  TRACE_INFO("Initializing TJA1101...\r\n");
68 
69  //Undefined PHY address?
70  if(interface->phyAddr >= 32)
71  {
72  //Use the default address
73  interface->phyAddr = TJA1101_PHY_ADDR;
74  }
75 
76  //Initialize external interrupt line driver
77  if(interface->extIntDriver != NULL)
78  {
79  interface->extIntDriver->init();
80  }
81 
82  //Reset PHY transceiver
85 
86  //Wait for the reset to complete
87  while(tja1101ReadPhyReg(interface, TJA1101_BASIC_CTRL) &
89  {
90  }
91 
92  //Dump PHY registers for debugging purpose
93  tja1101DumpPhyReg(interface);
94 
95  //Enable configuration register access
99 
100  //Select RMII mode (25MHz XTAL)
105 
106  //The PHY is configured for autonomous operation
110 
111  //Force the TCP/IP stack to poll the link state at startup
112  interface->phyEvent = TRUE;
113  //Notify the TCP/IP stack of the event
115 
116  //Successful initialization
117  return NO_ERROR;
118 }
119 
120 
121 /**
122  * @brief TJA1101 timer handler
123  * @param[in] interface Underlying network interface
124  **/
125 
126 void tja1101Tick(NetInterface *interface)
127 {
128  uint16_t value;
129  bool_t linkState;
130 
131  //No external interrupt line driver?
132  if(interface->extIntDriver == NULL)
133  {
134  //Read status register
136  //Retrieve current link state
137  linkState = (value & TJA1101_BASIC_STAT_LINK_STATUS) ? TRUE : FALSE;
138 
139  //Link up event?
140  if(linkState && !interface->linkState)
141  {
142  //Set event flag
143  interface->phyEvent = TRUE;
144  //Notify the TCP/IP stack of the event
146  }
147  //Link down event?
148  else if(!linkState && interface->linkState)
149  {
150  //Set event flag
151  interface->phyEvent = TRUE;
152  //Notify the TCP/IP stack of the event
154  }
155  }
156 }
157 
158 
159 /**
160  * @brief Enable interrupts
161  * @param[in] interface Underlying network interface
162  **/
163 
165 {
166  //Enable PHY transceiver interrupts
167  if(interface->extIntDriver != NULL)
168  {
169  interface->extIntDriver->enableIrq();
170  }
171 }
172 
173 
174 /**
175  * @brief Disable interrupts
176  * @param[in] interface Underlying network interface
177  **/
178 
180 {
181  //Disable PHY transceiver interrupts
182  if(interface->extIntDriver != NULL)
183  {
184  interface->extIntDriver->disableIrq();
185  }
186 }
187 
188 
189 /**
190  * @brief TJA1101 event handler
191  * @param[in] interface Underlying network interface
192  **/
193 
195 {
196  uint16_t value;
197 
198  //Read status register
200 
201  //Link is up?
203  {
204  //Adjust MAC configuration parameters for proper operation
205  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
206  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
207  interface->nicDriver->updateMacConfig(interface);
208 
209  //Update link state
210  interface->linkState = TRUE;
211  }
212  else
213  {
214  //Update link state
215  interface->linkState = FALSE;
216  }
217 
218  //Process link state change event
219  nicNotifyLinkChange(interface);
220 }
221 
222 
223 /**
224  * @brief Write PHY register
225  * @param[in] interface Underlying network interface
226  * @param[in] address PHY register address
227  * @param[in] data Register value
228  **/
229 
230 void tja1101WritePhyReg(NetInterface *interface, uint8_t address,
231  uint16_t data)
232 {
233  //Write the specified PHY register
234  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
235  interface->phyAddr, address, data);
236 }
237 
238 
239 /**
240  * @brief Read PHY register
241  * @param[in] interface Underlying network interface
242  * @param[in] address PHY register address
243  * @return Register value
244  **/
245 
246 uint16_t tja1101ReadPhyReg(NetInterface *interface, uint8_t address)
247 {
248  //Read the specified PHY register
249  return interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
250  interface->phyAddr, address);
251 }
252 
253 
254 /**
255  * @brief Dump PHY registers for debugging purpose
256  * @param[in] interface Underlying network interface
257  **/
258 
260 {
261  uint8_t i;
262 
263  //Loop through PHY registers
264  for(i = 0; i < 32; i++)
265  {
266  //Display current PHY register
267  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
268  tja1101ReadPhyReg(interface, i));
269  }
270 
271  //Terminate with a line feed
272  TRACE_DEBUG("\r\n");
273 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
#define TJA1101_BASIC_STAT
int bool_t
Definition: compiler_port.h:49
#define TJA1101_PHY_ADDR
const PhyDriver tja1101PhyDriver
TJA1101 Ethernet PHY driver.
#define TRUE
Definition: os_port.h:50
PHY driver.
Definition: nic.h:214
#define TJA1101_COMM_CTRL
#define TJA1101_CONFIG1_MII_MODE_RMII_25MHZ
void tja1101DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define SMI_OPCODE_WRITE
Definition: nic.h:62
#define TJA1101_BASIC_STAT_LINK_STATUS
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
#define TJA1101_BASIC_CTRL
#define NetInterface
Definition: net.h:36
void tja1101Tick(NetInterface *interface)
TJA1101 timer handler.
#define TJA1101_BASIC_CTRL_RESET
OsEvent netEvent
Definition: net.c:77
void tja1101DisableIrq(NetInterface *interface)
Disable interrupts.
#define SMI_OPCODE_READ
Definition: nic.h:63
#define TRACE_INFO(...)
Definition: debug.h:94
error_t tja1101Init(NetInterface *interface)
TJA1101 PHY transceiver initialization.
uint16_t tja1101ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define TJA1101_CONFIG1
#define TJA1101_EXTENDED_CTRL_CONFIG_EN
#define TJA1101_EXTENDED_CTRL
#define TRACE_DEBUG(...)
Definition: debug.h:106
#define TJA1101_CONFIG1_MII_MODE
void tja1101WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define TJA1101_COMM_CTRL_AUTO_OP
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void tja1101EventHandler(NetInterface *interface)
TJA1101 event handler.
uint8_t value[]
Definition: dtls_misc.h:150
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
TJA1101 100Base-T1 Ethernet PHY transceiver.
Success.
Definition: error.h:44
Debugging facilities.
void tja1101EnableIrq(NetInterface *interface)
Enable interrupts.