ra6_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file ra6_eth_driver.c
3  * @brief Renesas RA6M2 / RA6M3 / RA6M4 Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 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 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "bsp_api.h"
36 #include "core/net.h"
38 #include "debug.h"
39 
40 //Underlying network interface
41 static NetInterface *nicDriverInterface;
42 
43 //IAR EWARM compiler?
44 #if defined(__ICCARM__)
45 
46 //Transmit buffer
47 #pragma data_alignment = 32
48 #pragma location = RA6_ETH_RAM_SECTION
50 //Receive buffer
51 #pragma data_alignment = 32
52 #pragma location = RA6_ETH_RAM_SECTION
54 //Transmit DMA descriptors
55 #pragma data_alignment = 16
56 #pragma location = RA6_ETH_RAM_SECTION
58 //Receive DMA descriptors
59 #pragma data_alignment = 16
60 #pragma location = RA6_ETH_RAM_SECTION
62 
63 //ARM or GCC compiler?
64 #else
65 
66 //Transmit buffer
68  __attribute__((aligned(32), __section__(RA6_ETH_RAM_SECTION)));
69 //Receive buffer
71  __attribute__((aligned(32), __section__(RA6_ETH_RAM_SECTION)));
72 //Transmit DMA descriptors
74  __attribute__((aligned(16), __section__(RA6_ETH_RAM_SECTION)));
75 //Receive DMA descriptors
77  __attribute__((aligned(16), __section__(RA6_ETH_RAM_SECTION)));
78 
79 #endif
80 
81 //Current transmit descriptor
82 static uint_t txIndex;
83 //Current receive descriptor
84 static uint_t rxIndex;
85 
86 
87 /**
88  * @brief RA6 Ethernet MAC driver
89  **/
90 
92 {
94  ETH_MTU,
95  ra6EthInit,
96  ra6EthTick,
105  TRUE,
106  TRUE,
107  TRUE,
108  TRUE
109 };
110 
111 
112 /**
113  * @brief RA6 Ethernet MAC initialization
114  * @param[in] interface Underlying network interface
115  * @return Error code
116  **/
117 
119 {
120  error_t error;
121 
122  //Debug message
123  TRACE_INFO("Initializing RA6 Ethernet MAC...\r\n");
124 
125  //Save underlying network interface
126  nicDriverInterface = interface;
127 
128  //Disable protection
129  R_SYSTEM->PRCR = 0xA50B;
130  //Cancel EDMAC0 module stop state
131  R_MSTP->MSTPCRB &= ~R_MSTP_MSTPCRB_MSTPB15_Msk;
132  //Enable protection
133  R_SYSTEM->PRCR = 0xA500;
134 
135  //GPIO configuration
136  ra6EthInitGpio(interface);
137 
138  //Reset EDMAC0 module
139  R_ETHERC_EDMAC->EDMR |= R_ETHERC_EDMAC_EDMR_SWR_Msk;
140  //Wait for the reset to complete
141  sleep(10);
142 
143  //Valid Ethernet PHY or switch driver?
144  if(interface->phyDriver != NULL)
145  {
146  //Ethernet PHY initialization
147  error = interface->phyDriver->init(interface);
148  }
149  else if(interface->switchDriver != NULL)
150  {
151  //Ethernet switch initialization
152  error = interface->switchDriver->init(interface);
153  }
154  else
155  {
156  //The interface is not properly configured
157  error = ERROR_FAILURE;
158  }
159 
160  //Any error to report?
161  if(error)
162  {
163  return error;
164  }
165 
166  //Initialize DMA descriptor lists
167  ra6EthInitDmaDesc(interface);
168 
169  //Maximum frame length that can be accepted
170  R_ETHERC0->RFLR = RA6_ETH_RX_BUFFER_SIZE;
171  //Set default inter packet gap (96-bit time)
172  R_ETHERC0->IPGR = 0x14;
173 
174  //Set the upper 32 bits of the MAC address
175  R_ETHERC0->MAHR = (interface->macAddr.b[0] << 24) | (interface->macAddr.b[1] << 16) |
176  (interface->macAddr.b[2] << 8) | interface->macAddr.b[3];
177 
178  //Set the lower 16 bits of the MAC address
179  R_ETHERC0->MALR = (interface->macAddr.b[4] << 8) | interface->macAddr.b[5];
180 
181  //Select little endian mode and set descriptor length (16 bytes)
182  R_ETHERC_EDMAC->EDMR = R_ETHERC_EDMAC_EDMR_DE_Msk |
183  (0 << R_ETHERC_EDMAC_EDMR_DL_Pos);
184 
185  //Use store and forward mode
186  R_ETHERC_EDMAC->TFTR = 0;
187 
188  //Set transmit FIFO size (2048 bytes) and receive FIFO size (4096 bytes)
189  R_ETHERC_EDMAC->FDR = (7 << R_ETHERC_EDMAC_FDR_TFD_Pos) |
190  (15 << R_ETHERC_EDMAC_FDR_RFD_Pos);
191 
192  //Enable continuous reception of multiple frames
193  R_ETHERC_EDMAC->RMCR = R_ETHERC_EDMAC_RMCR_RNR_Msk;
194 
195  //Select write-back complete interrupt mode and enable transmit interrupts
196  R_ETHERC_EDMAC->TRIMD = R_ETHERC_EDMAC_TRIMD_TIM_Msk |
197  R_ETHERC_EDMAC_TRIMD_TIS_Msk;
198 
199  //Disable all ETHERC interrupts
200  R_ETHERC0->ECSIPR = 0;
201 
202  //Enable the desired EDMAC interrupts
203  R_ETHERC_EDMAC->EESIPR = R_ETHERC_EDMAC_EESIPR_TWBIP_Msk |
204  R_ETHERC_EDMAC_EESIPR_FRIP_Msk;
205 
206  //Set priority grouping (4 bits for pre-emption priority, no bits for subpriority)
207  NVIC_SetPriorityGrouping(RA6_ETH_IRQ_PRIORITY_GROUPING);
208 
209  //Configure EDMAC interrupt priority
210  NVIC_SetPriority(EDMAC0_EINT_IRQn, NVIC_EncodePriority(RA6_ETH_IRQ_PRIORITY_GROUPING,
212 
213  //Enable transmission and reception
214  R_ETHERC0->ECMR |= R_ETHERC0_ECMR_TE_Msk | R_ETHERC0_ECMR_RE_Msk;
215 
216  //Instruct the DMA to poll the receive descriptor list
217  R_ETHERC_EDMAC->EDRRR = R_ETHERC_EDMAC_EDRRR_RR_Msk;
218 
219  //Accept any packets from the upper layer
220  osSetEvent(&interface->nicTxEvent);
221 
222  //Successful initialization
223  return NO_ERROR;
224 }
225 
226 
227 /**
228  * @brief GPIO configuration
229  * @param[in] interface Underlying network interface
230  **/
231 
232 __weak_func void ra6EthInitGpio(NetInterface *interface)
233 {
234 //EK-RA6M3, EK-RA6M4 or EK-RA6M5 evaluation board?
235 #if defined(USE_EK_RA6M3) || defined(USE_EK_RA6M4) || defined(USE_EK_RA6M5)
236  //Disable protection
237  R_SYSTEM->PRCR = 0xA50B;
238  //Disable VBATT channel 0 input (P4_2)
239  R_SYSTEM->VBTICTLR &= ~R_SYSTEM_VBTICTLR_VCH0INEN_Msk;
240  //Enable protection
241  R_SYSTEM->PRCR = 0xA500;
242 
243  //Unlock PFS registers
244  R_PMISC->PWPR &= ~R_PMISC_PWPR_B0WI_Msk;
245  R_PMISC->PWPR |= R_PMISC_PWPR_PFSWE_Msk;
246 
247  //Select RMII interface mode
248  R_PMISC->PFENET &= ~R_PMISC_PFENET_PHYMODE0_Msk;
249 
250  //Configure ET0_MDC (P4_1)
251  R_PFS->PORT[4].PIN[1].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
252  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (1 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
253 
254  //Configure ET0_MDIO (P4_2)
255  R_PFS->PORT[4].PIN[2].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
256  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (1 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
257 
258  //Configure RMII0_TXD_EN_B (P4_5)
259  R_PFS->PORT[4].PIN[5].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
260  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
261 
262  //Configure RMII0_TXD1_B (P4_6)
263  R_PFS->PORT[4].PIN[6].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
264  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
265 
266  //Configure RMII0_TXD0_B (P7_0)
267  R_PFS->PORT[7].PIN[0].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
268  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
269 
270  //Configure REF50CK0_B (P7_1)
271  R_PFS->PORT[7].PIN[1].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
272  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
273 
274  //Configure RMII0_RXD0_B (P7_2)
275  R_PFS->PORT[7].PIN[2].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
276  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
277 
278  //Configure RMII0_RXD1_B (P7_3)
279  R_PFS->PORT[7].PIN[3].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
280  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
281 
282  //Configure RMII0_RX_ER_B (P7_4)
283  R_PFS->PORT[7].PIN[4].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
284  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
285 
286  //Configure RMII0_CRS_DV_B (P7_5)
287  R_PFS->PORT[7].PIN[5].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
288  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
289 
290  //Lock PFS registers
291  R_PMISC->PWPR &= ~R_PMISC_PWPR_PFSWE_Msk;
292  R_PMISC->PWPR |= R_PMISC_PWPR_B0WI_Msk;
293 
294 //M13-RA6M2-EK, M13-RA6M4-EK or M13-RA6M5-EK evaluation board?
295 #elif defined(USE_M13_RA6M2_EK) || defined(USE_M13_RA6M4_EK) || \
296  defined(USE_M13_RA6M5_EK)
297  //Unlock PFS registers
298  R_PMISC->PWPR &= ~R_PMISC_PWPR_B0WI_Msk;
299  R_PMISC->PWPR |= R_PMISC_PWPR_PFSWE_Msk;
300 
301  //Select RMII interface mode
302  R_PMISC->PFENET &= ~R_PMISC_PFENET_PHYMODE0_Msk;
303 
304  //Configure RMII0_TXD_EN_B (P4_5)
305  R_PFS->PORT[4].PIN[5].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
306  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
307 
308  //Configure RMII0_TXD1_B (P4_6)
309  R_PFS->PORT[4].PIN[6].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
310  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
311 
312  //Configure RMII0_TXD0_B (P7_0)
313  R_PFS->PORT[7].PIN[0].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
314  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
315 
316  //Configure REF50CK0_B (P7_1)
317  R_PFS->PORT[7].PIN[1].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
318  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
319 
320  //Configure RMII0_RXD0_B (P7_2)
321  R_PFS->PORT[7].PIN[2].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
322  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
323 
324  //Configure RMII0_RXD1_B (P7_3)
325  R_PFS->PORT[7].PIN[3].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
326  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
327 
328  //Configure RMII0_RX_ER_B (P7_4)
329  R_PFS->PORT[7].PIN[4].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
330  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
331 
332  //Configure RMII0_CRS_DV_B (P7_5)
333  R_PFS->PORT[7].PIN[5].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
334  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
335 
336  //Configure PHY reset pin (P6_5)
337  R_PFS->PORT[6].PIN[5].PmnPFS = R_PFS_PORT_PIN_PmnPFS_PDR_Msk;
338 
339  //Lock PFS registers
340  R_PMISC->PWPR &= ~R_PMISC_PWPR_PFSWE_Msk;
341  R_PMISC->PWPR |= R_PMISC_PWPR_B0WI_Msk;
342 
343  //Reset PHY transceiver
344  R_PORT6->PCNTR3 = (1 << 5) << R_PORT0_PCNTR3_PORR_Pos;
345  sleep(10);
346  R_PORT6->PCNTR3 = (1 << 5) << R_PORT0_PCNTR3_POSR_Pos;
347  sleep(10);
348 
349 //M13-RA6M3-EK evaluation board?
350 #elif defined(USE_M13_RA6M3_EK)
351  //Disable protection
352  R_SYSTEM->PRCR = 0xA50B;
353  //Disable VBATT channel 0 input (P4_2)
354  R_SYSTEM->VBTICTLR &= ~R_SYSTEM_VBTICTLR_VCH0INEN_Msk;
355  //Enable protection
356  R_SYSTEM->PRCR = 0xA500;
357 
358  //Unlock PFS registers
359  R_PMISC->PWPR &= ~R_PMISC_PWPR_B0WI_Msk;
360  R_PMISC->PWPR |= R_PMISC_PWPR_PFSWE_Msk;
361 
362  //Select RMII interface mode
363  R_PMISC->PFENET &= ~R_PMISC_PFENET_PHYMODE0_Msk;
364 
365  //Configure ET0_MDC (P4_1)
366  R_PFS->PORT[4].PIN[1].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
367  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (1 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
368 
369  //Configure ET0_MDIO (P4_2)
370  R_PFS->PORT[4].PIN[2].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
371  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (1 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
372 
373  //Configure RMII0_CRS_DV_A (P4_8)
374  R_PFS->PORT[4].PIN[8].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
375  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
376 
377  //Configure RMII0_RXD1_A (P4_10)
378  R_PFS->PORT[4].PIN[10].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
379  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
380 
381  //Configure RMII0_RXD0_A (P4_11)
382  R_PFS->PORT[4].PIN[11].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
383  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
384 
385  //Configure REF50CK0_A (P4_12)
386  R_PFS->PORT[4].PIN[12].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
387  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
388 
389  //Configure RMII0_TXD0_A (P4_13)
390  R_PFS->PORT[4].PIN[13].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
391  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
392 
393  //Configure RMII0_TXD1_A (P4_14)
394  R_PFS->PORT[4].PIN[14].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
395  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
396 
397  //Configure RMII0_TXD_EN_A (P4_15)
398  R_PFS->PORT[4].PIN[15].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
399  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
400 
401  //Lock PFS registers
402  R_PMISC->PWPR &= ~R_PMISC_PWPR_PFSWE_Msk;
403  R_PMISC->PWPR |= R_PMISC_PWPR_B0WI_Msk;
404 #endif
405 }
406 
407 
408 /**
409  * @brief Initialize DMA descriptor lists
410  * @param[in] interface Underlying network interface
411  **/
412 
414 {
415  uint_t i;
416 
417  //Initialize TX descriptors
418  for(i = 0; i < RA6_ETH_TX_BUFFER_COUNT; i++)
419  {
420  //The descriptor is initially owned by the application
421  txDmaDesc[i].td0 = 0;
422  //Transmit buffer length
423  txDmaDesc[i].td1 = 0;
424  //Transmit buffer address
425  txDmaDesc[i].td2 = (uint32_t) txBuffer[i];
426  //Clear padding field
427  txDmaDesc[i].padding = 0;
428  }
429 
430  //Mark the last descriptor entry with the TDLE flag
431  txDmaDesc[i - 1].td0 |= EDMAC_TD0_TDLE;
432  //Initialize TX descriptor index
433  txIndex = 0;
434 
435  //Initialize RX descriptors
436  for(i = 0; i < RA6_ETH_RX_BUFFER_COUNT; i++)
437  {
438  //The descriptor is initially owned by the DMA
439  rxDmaDesc[i].rd0 = EDMAC_RD0_RACT;
440  //Receive buffer length
442  //Receive buffer address
443  rxDmaDesc[i].rd2 = (uint32_t) rxBuffer[i];
444  //Clear padding field
445  rxDmaDesc[i].padding = 0;
446  }
447 
448  //Mark the last descriptor entry with the RDLE flag
449  rxDmaDesc[i - 1].rd0 |= EDMAC_RD0_RDLE;
450  //Initialize RX descriptor index
451  rxIndex = 0;
452 
453  //Start address of the TX descriptor list
454  R_ETHERC_EDMAC->TDLAR = (uint32_t) txDmaDesc;
455  //Start address of the RX descriptor list
456  R_ETHERC_EDMAC->RDLAR = (uint32_t) rxDmaDesc;
457 }
458 
459 
460 /**
461  * @brief RA6 Ethernet MAC timer handler
462  *
463  * This routine is periodically called by the TCP/IP stack to handle periodic
464  * operations such as polling the link state
465  *
466  * @param[in] interface Underlying network interface
467  **/
468 
469 void ra6EthTick(NetInterface *interface)
470 {
471  //Valid Ethernet PHY or switch driver?
472  if(interface->phyDriver != NULL)
473  {
474  //Handle periodic operations
475  interface->phyDriver->tick(interface);
476  }
477  else if(interface->switchDriver != NULL)
478  {
479  //Handle periodic operations
480  interface->switchDriver->tick(interface);
481  }
482  else
483  {
484  //Just for sanity
485  }
486 }
487 
488 
489 /**
490  * @brief Enable interrupts
491  * @param[in] interface Underlying network interface
492  **/
493 
495 {
496  //Enable Ethernet MAC interrupts
497  NVIC_EnableIRQ(EDMAC0_EINT_IRQn);
498 
499  //Valid Ethernet PHY or switch driver?
500  if(interface->phyDriver != NULL)
501  {
502  //Enable Ethernet PHY interrupts
503  interface->phyDriver->enableIrq(interface);
504  }
505  else if(interface->switchDriver != NULL)
506  {
507  //Enable Ethernet switch interrupts
508  interface->switchDriver->enableIrq(interface);
509  }
510  else
511  {
512  //Just for sanity
513  }
514 }
515 
516 
517 /**
518  * @brief Disable interrupts
519  * @param[in] interface Underlying network interface
520  **/
521 
523 {
524  //Disable Ethernet MAC interrupts
525  NVIC_DisableIRQ(EDMAC0_EINT_IRQn);
526 
527  //Valid Ethernet PHY or switch driver?
528  if(interface->phyDriver != NULL)
529  {
530  //Disable Ethernet PHY interrupts
531  interface->phyDriver->disableIrq(interface);
532  }
533  else if(interface->switchDriver != NULL)
534  {
535  //Disable Ethernet switch interrupts
536  interface->switchDriver->disableIrq(interface);
537  }
538  else
539  {
540  //Just for sanity
541  }
542 }
543 
544 
545 /**
546  * @brief RA6 Ethernet MAC interrupt service routine
547  **/
548 
550 {
551  bool_t flag;
552  uint32_t status;
553 
554  //Interrupt service routine prologue
555  osEnterIsr();
556 
557  //This flag will be set if a higher priority task must be woken
558  flag = FALSE;
559 
560  //Read interrupt status register
561  status = R_ETHERC_EDMAC->EESR;
562 
563  //Packet transmitted?
564  if((status & R_ETHERC_EDMAC_EESR_TWB_Msk) != 0)
565  {
566  //Clear TWB interrupt flag
567  R_ETHERC_EDMAC->EESR = R_ETHERC_EDMAC_EESR_TWB_Msk;
568 
569  //Check whether the TX buffer is available for writing
570  if((txDmaDesc[txIndex].td0 & EDMAC_TD0_TACT) == 0)
571  {
572  //Notify the TCP/IP stack that the transmitter is ready to send
573  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
574  }
575  }
576 
577  //Packet received?
578  if((status & R_ETHERC_EDMAC_EESR_FR_Msk) != 0)
579  {
580  //Clear FR interrupt flag
581  R_ETHERC_EDMAC->EESR = R_ETHERC_EDMAC_EESR_FR_Msk;
582 
583  //Set event flag
584  nicDriverInterface->nicEvent = TRUE;
585  //Notify the TCP/IP stack of the event
586  flag |= osSetEventFromIsr(&netEvent);
587  }
588 
589  //Clear IR flag
590  R_ICU->IELSR[EDMAC0_EINT_IRQn] &= ~R_ICU_IELSR_IR_Msk;
591 
592  //Interrupt service routine epilogue
593  osExitIsr(flag);
594 }
595 
596 
597 /**
598  * @brief RA6 Ethernet MAC event handler
599  * @param[in] interface Underlying network interface
600  **/
601 
603 {
604  error_t error;
605 
606  //Process all pending packets
607  do
608  {
609  //Read incoming packet
610  error = ra6EthReceivePacket(interface);
611 
612  //No more data in the receive buffer?
613  } while(error != ERROR_BUFFER_EMPTY);
614 }
615 
616 
617 /**
618  * @brief Send a packet
619  * @param[in] interface Underlying network interface
620  * @param[in] buffer Multi-part buffer containing the data to send
621  * @param[in] offset Offset to the first data byte
622  * @param[in] ancillary Additional options passed to the stack along with
623  * the packet
624  * @return Error code
625  **/
626 
628  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
629 {
630  //Retrieve the length of the packet
631  size_t length = netBufferGetLength(buffer) - offset;
632 
633  //Check the frame length
635  {
636  //The transmitter can accept another packet
637  osSetEvent(&interface->nicTxEvent);
638  //Report an error
639  return ERROR_INVALID_LENGTH;
640  }
641 
642  //Make sure the current buffer is available for writing
643  if((txDmaDesc[txIndex].td0 & EDMAC_TD0_TACT) != 0)
644  {
645  return ERROR_FAILURE;
646  }
647 
648  //Copy user data to the transmit buffer
649  netBufferRead(txBuffer[txIndex], buffer, offset, length);
650 
651  //Write the number of bytes to send
652  txDmaDesc[txIndex].td1 = (length << 16) & EDMAC_TD1_TBL;
653 
654  //Check current index
655  if(txIndex < (RA6_ETH_TX_BUFFER_COUNT - 1))
656  {
657  //Give the ownership of the descriptor to the DMA engine
658  txDmaDesc[txIndex].td0 = EDMAC_TD0_TACT | EDMAC_TD0_TFP_SOF |
660 
661  //Point to the next descriptor
662  txIndex++;
663  }
664  else
665  {
666  //Give the ownership of the descriptor to the DMA engine
667  txDmaDesc[txIndex].td0 = EDMAC_TD0_TACT | EDMAC_TD0_TDLE |
669 
670  //Wrap around
671  txIndex = 0;
672  }
673 
674  //Instruct the DMA to poll the transmit descriptor list
675  R_ETHERC_EDMAC->EDTRR = R_ETHERC_EDMAC_EDTRR_TR_Msk;
676 
677  //Check whether the next buffer is available for writing
678  if((txDmaDesc[txIndex].td0 & EDMAC_TD0_TACT) == 0)
679  {
680  //The transmitter can accept another packet
681  osSetEvent(&interface->nicTxEvent);
682  }
683 
684  //Successful write operation
685  return NO_ERROR;
686 }
687 
688 
689 /**
690  * @brief Receive a packet
691  * @param[in] interface Underlying network interface
692  * @return Error code
693  **/
694 
696 {
697  error_t error;
698  size_t n;
699  NetRxAncillary ancillary;
700 
701  //Current buffer available for reading?
702  if((rxDmaDesc[rxIndex].rd0 & EDMAC_RD0_RACT) == 0)
703  {
704  //SOF and EOF flags should be set
705  if((rxDmaDesc[rxIndex].rd0 & EDMAC_RD0_RFP_SOF) != 0 &&
706  (rxDmaDesc[rxIndex].rd0 & EDMAC_RD0_RFP_EOF) != 0)
707  {
708  //Make sure no error occurred
709  if((rxDmaDesc[rxIndex].rd0 & (EDMAC_RD0_RFS_MASK & ~EDMAC_RD0_RFS_RMAF)) == 0)
710  {
711  //Retrieve the length of the frame
712  n = rxDmaDesc[rxIndex].rd1 & EDMAC_RD1_RFL;
713  //Limit the number of data to read
715 
716  //Additional options can be passed to the stack along with the packet
717  ancillary = NET_DEFAULT_RX_ANCILLARY;
718 
719  //Pass the packet to the upper layer
720  nicProcessPacket(interface, rxBuffer[rxIndex], n, &ancillary);
721 
722  //Valid packet received
723  error = NO_ERROR;
724  }
725  else
726  {
727  //The received packet contains an error
728  error = ERROR_INVALID_PACKET;
729  }
730  }
731  else
732  {
733  //The packet is not valid
734  error = ERROR_INVALID_PACKET;
735  }
736 
737  //Check current index
738  if(rxIndex < (RA6_ETH_RX_BUFFER_COUNT - 1))
739  {
740  //Give the ownership of the descriptor back to the DMA
741  rxDmaDesc[rxIndex].rd0 = EDMAC_RD0_RACT;
742  //Point to the next descriptor
743  rxIndex++;
744  }
745  else
746  {
747  //Give the ownership of the descriptor back to the DMA
748  rxDmaDesc[rxIndex].rd0 = EDMAC_RD0_RACT | EDMAC_RD0_RDLE;
749  //Wrap around
750  rxIndex = 0;
751  }
752 
753  //Instruct the DMA to poll the receive descriptor list
754  R_ETHERC_EDMAC->EDRRR = R_ETHERC_EDMAC_EDRRR_RR_Msk;
755  }
756  else
757  {
758  //No more data in the receive buffer
759  error = ERROR_BUFFER_EMPTY;
760  }
761 
762  //Return status code
763  return error;
764 }
765 
766 
767 /**
768  * @brief Configure MAC address filtering
769  * @param[in] interface Underlying network interface
770  * @return Error code
771  **/
772 
774 {
775  uint_t i;
776  bool_t acceptMulticast;
777 
778  //Debug message
779  TRACE_DEBUG("Updating MAC filter...\r\n");
780 
781  //Promiscuous mode?
782  if(interface->promiscuous)
783  {
784  //Accept all frames regardless of their destination address
785  R_ETHERC0->ECMR |= R_ETHERC0_ECMR_PRM_Msk;
786  }
787  else
788  {
789  //Disable promiscuous mode
790  R_ETHERC0->ECMR &= ~R_ETHERC0_ECMR_PRM_Msk;
791 
792  //Set the upper 32 bits of the MAC address
793  R_ETHERC0->MAHR = (interface->macAddr.b[0] << 24) | (interface->macAddr.b[1] << 16) |
794  (interface->macAddr.b[2] << 8) | interface->macAddr.b[3];
795 
796  //Set the lower 16 bits of the MAC address
797  R_ETHERC0->MALR = (interface->macAddr.b[4] << 8) | interface->macAddr.b[5];
798 
799  //This flag will be set if multicast addresses should be accepted
800  acceptMulticast = FALSE;
801 
802  //The MAC address filter contains the list of MAC addresses to accept
803  //when receiving an Ethernet frame
804  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
805  {
806  //Valid entry?
807  if(interface->macAddrFilter[i].refCount > 0)
808  {
809  //Accept multicast addresses
810  acceptMulticast = TRUE;
811  //We are done
812  break;
813  }
814  }
815 
816  //Enable or disable the reception of multicast frames
817  if(acceptMulticast || interface->acceptAllMulticast)
818  {
819  R_ETHERC_EDMAC->EESR |= R_ETHERC_EDMAC_EESR_RMAF_Msk;
820  }
821  else
822  {
823  R_ETHERC_EDMAC->EESR &= ~R_ETHERC_EDMAC_EESR_RMAF_Msk;
824  }
825  }
826 
827  //Successful processing
828  return NO_ERROR;
829 }
830 
831 
832 /**
833  * @brief Adjust MAC configuration parameters for proper operation
834  * @param[in] interface Underlying network interface
835  * @return Error code
836  **/
837 
839 {
840  uint32_t mode;
841 
842  //Read ETHERC mode register
843  mode = R_ETHERC0->ECMR;
844 
845  //10BASE-T or 100BASE-TX operation mode?
846  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
847  {
848  mode |= R_ETHERC0_ECMR_RTM_Msk;
849  }
850  else
851  {
852  mode &= ~R_ETHERC0_ECMR_RTM_Msk;
853  }
854 
855  //Half-duplex or full-duplex mode?
856  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
857  {
858  mode |= R_ETHERC0_ECMR_DM_Msk;
859  }
860  else
861  {
862  mode &= ~R_ETHERC0_ECMR_DM_Msk;
863  }
864 
865  //Update ETHERC mode register
866  R_ETHERC0->ECMR = mode;
867 
868  //Successful processing
869  return NO_ERROR;
870 }
871 
872 
873 /**
874  * @brief Write PHY register
875  * @param[in] opcode Access type (2 bits)
876  * @param[in] phyAddr PHY address (5 bits)
877  * @param[in] regAddr Register address (5 bits)
878  * @param[in] data Register value
879  **/
880 
881 void ra6EthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
882  uint8_t regAddr, uint16_t data)
883 {
884  //Synchronization pattern
886  //Start of frame
888  //Set up a write operation
890  //Write PHY address
891  ra6EthWriteSmi(phyAddr, 5);
892  //Write register address
894  //Turnaround
896  //Write register value
897  ra6EthWriteSmi(data, 16);
898  //Release MDIO
899  ra6EthReadSmi(1);
900 }
901 
902 
903 /**
904  * @brief Read PHY register
905  * @param[in] opcode Access type (2 bits)
906  * @param[in] phyAddr PHY address (5 bits)
907  * @param[in] regAddr Register address (5 bits)
908  * @return Register value
909  **/
910 
911 uint16_t ra6EthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
912  uint8_t regAddr)
913 {
914  uint16_t data;
915 
916  //Synchronization pattern
918  //Start of frame
920  //Set up a read operation
922  //Write PHY address
923  ra6EthWriteSmi(phyAddr, 5);
924  //Write register address
926  //Turnaround to avoid contention
927  ra6EthReadSmi(1);
928  //Read register value
929  data = ra6EthReadSmi(16);
930  //Force the PHY to release the MDIO pin
931  ra6EthReadSmi(1);
932 
933  //Return PHY register contents
934  return data;
935 }
936 
937 
938 /**
939  * @brief SMI write operation
940  * @param[in] data Raw data to be written
941  * @param[in] length Number of bits to be written
942  **/
943 
945 {
946  //Skip the most significant bits since they are meaningless
947  data <<= 32 - length;
948 
949  //Configure MDIO as an output
950  R_ETHERC0->PIR |= R_ETHERC0_PIR_MMD_Msk;
951 
952  //Write the specified number of bits
953  while(length--)
954  {
955  //Write MDIO
956  if((data & 0x80000000) != 0)
957  {
958  R_ETHERC0->PIR |= R_ETHERC0_PIR_MDO_Msk;
959  }
960  else
961  {
962  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MDO_Msk;
963  }
964 
965  //Assert MDC
966  usleep(1);
967  R_ETHERC0->PIR |= R_ETHERC0_PIR_MDC_Msk;
968  //Deassert MDC
969  usleep(1);
970  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MDC_Msk;
971 
972  //Rotate data
973  data <<= 1;
974  }
975 }
976 
977 
978 /**
979  * @brief SMI read operation
980  * @param[in] length Number of bits to be read
981  * @return Data resulting from the MDIO read operation
982  **/
983 
985 {
986  uint32_t data = 0;
987 
988  //Configure MDIO as an input
989  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MMD_Msk;
990 
991  //Read the specified number of bits
992  while(length--)
993  {
994  //Rotate data
995  data <<= 1;
996 
997  //Assert MDC
998  R_ETHERC0->PIR |= R_ETHERC0_PIR_MDC_Msk;
999  usleep(1);
1000  //Deassert MDC
1001  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MDC_Msk;
1002  usleep(1);
1003 
1004  //Check MDIO state
1005  if((R_ETHERC0->PIR & R_ETHERC0_PIR_MDI_Msk) != 0)
1006  {
1007  data |= 0x01;
1008  }
1009  }
1010 
1011  //Return the received data
1012  return data;
1013 }
#define txDmaDesc
#define rxBuffer
#define txBuffer
#define rxDmaDesc
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t n
uint8_t opcode
Definition: dns_common.h:188
error_t
Error codes.
Definition: error.h:43
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PACKET
Definition: error.h:140
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define ETH_MTU
Definition: ethernet.h:116
uint8_t data[]
Definition: ethernet.h:222
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
uint16_t regAddr
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netEvent
Definition: net_legacy.h:196
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:674
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:101
#define NetRxAncillary
Definition: net_misc.h:40
#define NetTxAncillary
Definition: net_misc.h:36
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:391
#define SMI_SYNC
Definition: nic.h:63
#define SMI_START
Definition: nic.h:64
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83
#define SMI_TA
Definition: nic.h:68
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define MIN(a, b)
Definition: os_port.h:63
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
#define usleep(delay)
Definition: os_port.h:297
#define sleep(delay)
Definition: os_port.h:301
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define osEnterIsr()
#define osExitIsr(flag)
void ra6EthTick(NetInterface *interface)
RA6 Ethernet MAC timer handler.
uint16_t ra6EthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
void ra6EthEnableIrq(NetInterface *interface)
Enable interrupts.
error_t ra6EthReceivePacket(NetInterface *interface)
Receive a packet.
uint32_t ra6EthReadSmi(uint_t length)
SMI read operation.
void ra6EthDisableIrq(NetInterface *interface)
Disable interrupts.
void ra6EthWriteSmi(uint32_t data, uint_t length)
SMI write operation.
void ra6EthEventHandler(NetInterface *interface)
RA6 Ethernet MAC event handler.
error_t ra6EthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
void EDMAC0_EINT_IRQHandler(void)
RA6 Ethernet MAC interrupt service routine.
void ra6EthInitDmaDesc(NetInterface *interface)
Initialize DMA descriptor lists.
void ra6EthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
error_t ra6EthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
const NicDriver ra6EthDriver
RA6 Ethernet MAC driver.
__weak_func void ra6EthInitGpio(NetInterface *interface)
GPIO configuration.
error_t ra6EthInit(NetInterface *interface)
RA6 Ethernet MAC initialization.
error_t ra6EthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
Renesas RA6M2 / RA6M3 / RA6M4 Ethernet MAC driver.
#define RA6_ETH_TX_BUFFER_SIZE
#define EDMAC_TD1_TBL
#define EDMAC_RD0_RFS_MASK
#define EDMAC_RD0_RDLE
#define EDMAC_RD0_RFS_RMAF
#define EDMAC_RD1_RFL
#define EDMAC_TD0_TWBI
#define RA6_ETH_IRQ_GROUP_PRIORITY
#define RA6_ETH_RX_BUFFER_COUNT
#define RA6_ETH_TX_BUFFER_COUNT
#define EDMAC_TD0_TACT
#define RA6_ETH_RX_BUFFER_SIZE
#define RA6_ETH_IRQ_SUB_PRIORITY
#define EDMAC_RD1_RBL
#define R_ETHERC_EDMAC
#define RA6_ETH_RAM_SECTION
#define R_MSTP_MSTPCRB_MSTPB15_Msk
#define EDMAC_TD0_TDLE
#define EDMAC_TD0_TFP_EOF
#define RA6_ETH_IRQ_PRIORITY_GROUPING
#define EDMAC_RD0_RACT
#define EDMAC_RD0_RFP_EOF
#define EDMAC_RD0_RFP_SOF
#define EDMAC_TD0_TFP_SOF
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
NIC driver.
Definition: nic.h:283
Receive DMA descriptor.
Transmit DMA descriptor.
uint8_t length
Definition: tcp.h:368