ra8_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file ra8_eth_driver.c
3  * @brief Renesas RA8D1 / RA8M1 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 = RA8_ETH_RAM_SECTION
50 //Receive buffer
51 #pragma data_alignment = 32
52 #pragma location = RA8_ETH_RAM_SECTION
54 //Transmit DMA descriptors
55 #pragma data_alignment = 16
56 #pragma location = RA8_ETH_RAM_SECTION
58 //Receive DMA descriptors
59 #pragma data_alignment = 16
60 #pragma location = RA8_ETH_RAM_SECTION
62 
63 //ARM or GCC compiler?
64 #else
65 
66 //Transmit buffer
68  __attribute__((aligned(32), __section__(RA8_ETH_RAM_SECTION)));
69 //Receive buffer
71  __attribute__((aligned(32), __section__(RA8_ETH_RAM_SECTION)));
72 //Transmit DMA descriptors
74  __attribute__((aligned(16), __section__(RA8_ETH_RAM_SECTION)));
75 //Receive DMA descriptors
77  __attribute__((aligned(16), __section__(RA8_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 RA8 Ethernet MAC driver
89  **/
90 
92 {
94  ETH_MTU,
95  ra8EthInit,
96  ra8EthTick,
105  TRUE,
106  TRUE,
107  TRUE,
108  TRUE
109 };
110 
111 
112 /**
113  * @brief RA8 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 RA8 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  ra8EthInitGpio(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  ra8EthInitDmaDesc(interface);
168 
169  //Maximum frame length that can be accepted
170  R_ETHERC0->RFLR = RA8_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(RA8_ETH_IRQ_PRIORITY_GROUPING);
208 
209  //Configure EDMAC interrupt priority
210  NVIC_SetPriority(EDMAC0_EINT_IRQn, NVIC_EncodePriority(RA8_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 ra8EthInitGpio(NetInterface *interface)
233 {
234 //EK-RA8D1 evaluation board?
235 #if defined(USE_EK_RA8D1)
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->PWPRS &= ~R_PMISC_PWPR_B0WI_Msk;
245  R_PMISC->PWPRS |= 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  //Configure PHY reset pin (P7_6)
291  R_PFS->PORT[7].PIN[6].PmnPFS = R_PFS_PORT_PIN_PmnPFS_PDR_Msk;
292 
293  //Lock PFS registers
294  R_PMISC->PWPRS &= ~R_PMISC_PWPR_PFSWE_Msk;
295  R_PMISC->PWPRS |= R_PMISC_PWPR_B0WI_Msk;
296 
297  //Reset PHY transceiver
298  R_PORT7->PCNTR3 = (1 << 6) << R_PORT0_PCNTR3_PORR_Pos;
299  sleep(10);
300  R_PORT7->PCNTR3 = (1 << 6) << R_PORT0_PCNTR3_POSR_Pos;
301  sleep(10);
302 
303 //EK-RA8M1 evaluation board?
304 #elif defined(USE_EK_RA8M1)
305  //Disable protection
306  R_SYSTEM->PRCR = 0xA50B;
307  //Disable VBATT channel 0 input (P4_2)
308  R_SYSTEM->VBTICTLR &= ~R_SYSTEM_VBTICTLR_VCH0INEN_Msk;
309  //Enable protection
310  R_SYSTEM->PRCR = 0xA500;
311 
312  //Unlock PFS registers
313  R_PMISC->PWPRS &= ~R_PMISC_PWPR_B0WI_Msk;
314  R_PMISC->PWPRS |= R_PMISC_PWPR_PFSWE_Msk;
315 
316  //Select RMII interface mode
317  R_PMISC->PFENET &= ~R_PMISC_PFENET_PHYMODE0_Msk;
318 
319  //Configure ET0_MDC (P4_1)
320  R_PFS->PORT[4].PIN[1].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
321  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (1 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
322 
323  //Configure ET0_MDIO (P4_2)
324  R_PFS->PORT[4].PIN[2].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
325  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (1 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
326 
327  //Configure RMII0_TXD_EN_B (P4_5)
328  R_PFS->PORT[4].PIN[5].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
329  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
330 
331  //Configure RMII0_TXD1_B (P4_6)
332  R_PFS->PORT[4].PIN[6].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
333  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
334 
335  //Configure RMII0_TXD0_B (P7_0)
336  R_PFS->PORT[7].PIN[0].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
337  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
338 
339  //Configure REF50CK0_B (P7_1)
340  R_PFS->PORT[7].PIN[1].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
341  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
342 
343  //Configure RMII0_RXD0_B (P7_2)
344  R_PFS->PORT[7].PIN[2].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
345  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
346 
347  //Configure RMII0_RXD1_B (P7_3)
348  R_PFS->PORT[7].PIN[3].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
349  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
350 
351  //Configure RMII0_RX_ER_B (P7_4)
352  R_PFS->PORT[7].PIN[4].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
353  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
354 
355  //Configure RMII0_CRS_DV_B (P7_5)
356  R_PFS->PORT[7].PIN[5].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
357  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
358 
359  //Configure PHY reset pin (P4_4)
360  R_PFS->PORT[4].PIN[4].PmnPFS = R_PFS_PORT_PIN_PmnPFS_PDR_Msk;
361 
362  //Lock PFS registers
363  R_PMISC->PWPRS &= ~R_PMISC_PWPR_PFSWE_Msk;
364  R_PMISC->PWPRS |= R_PMISC_PWPR_B0WI_Msk;
365 
366  //Reset PHY transceiver
367  R_PORT4->PCNTR3 = (1 << 4) << R_PORT0_PCNTR3_PORR_Pos;
368  sleep(10);
369  R_PORT4->PCNTR3 = (1 << 4) << R_PORT0_PCNTR3_POSR_Pos;
370  sleep(10);
371 #endif
372 }
373 
374 
375 /**
376  * @brief Initialize DMA descriptor lists
377  * @param[in] interface Underlying network interface
378  **/
379 
381 {
382  uint_t i;
383 
384  //Initialize TX descriptors
385  for(i = 0; i < RA8_ETH_TX_BUFFER_COUNT; i++)
386  {
387  //The descriptor is initially owned by the application
388  txDmaDesc[i].td0 = 0;
389  //Transmit buffer length
390  txDmaDesc[i].td1 = 0;
391  //Transmit buffer address
392  txDmaDesc[i].td2 = (uint32_t) txBuffer[i];
393  //Clear padding field
394  txDmaDesc[i].padding = 0;
395  }
396 
397  //Mark the last descriptor entry with the TDLE flag
398  txDmaDesc[i - 1].td0 |= EDMAC_TD0_TDLE;
399  //Initialize TX descriptor index
400  txIndex = 0;
401 
402  //Initialize RX descriptors
403  for(i = 0; i < RA8_ETH_RX_BUFFER_COUNT; i++)
404  {
405  //The descriptor is initially owned by the DMA
406  rxDmaDesc[i].rd0 = EDMAC_RD0_RACT;
407  //Receive buffer length
409  //Receive buffer address
410  rxDmaDesc[i].rd2 = (uint32_t) rxBuffer[i];
411  //Clear padding field
412  rxDmaDesc[i].padding = 0;
413  }
414 
415  //Mark the last descriptor entry with the RDLE flag
416  rxDmaDesc[i - 1].rd0 |= EDMAC_RD0_RDLE;
417  //Initialize RX descriptor index
418  rxIndex = 0;
419 
420  //Start address of the TX descriptor list
421  R_ETHERC_EDMAC->TDLAR = (uint32_t) txDmaDesc;
422  //Start address of the RX descriptor list
423  R_ETHERC_EDMAC->RDLAR = (uint32_t) rxDmaDesc;
424 }
425 
426 
427 /**
428  * @brief RA8 Ethernet MAC timer handler
429  *
430  * This routine is periodically called by the TCP/IP stack to handle periodic
431  * operations such as polling the link state
432  *
433  * @param[in] interface Underlying network interface
434  **/
435 
436 void ra8EthTick(NetInterface *interface)
437 {
438  //Valid Ethernet PHY or switch driver?
439  if(interface->phyDriver != NULL)
440  {
441  //Handle periodic operations
442  interface->phyDriver->tick(interface);
443  }
444  else if(interface->switchDriver != NULL)
445  {
446  //Handle periodic operations
447  interface->switchDriver->tick(interface);
448  }
449  else
450  {
451  //Just for sanity
452  }
453 }
454 
455 
456 /**
457  * @brief Enable interrupts
458  * @param[in] interface Underlying network interface
459  **/
460 
462 {
463  //Enable Ethernet MAC interrupts
464  NVIC_EnableIRQ(EDMAC0_EINT_IRQn);
465 
466  //Valid Ethernet PHY or switch driver?
467  if(interface->phyDriver != NULL)
468  {
469  //Enable Ethernet PHY interrupts
470  interface->phyDriver->enableIrq(interface);
471  }
472  else if(interface->switchDriver != NULL)
473  {
474  //Enable Ethernet switch interrupts
475  interface->switchDriver->enableIrq(interface);
476  }
477  else
478  {
479  //Just for sanity
480  }
481 }
482 
483 
484 /**
485  * @brief Disable interrupts
486  * @param[in] interface Underlying network interface
487  **/
488 
490 {
491  //Disable Ethernet MAC interrupts
492  NVIC_DisableIRQ(EDMAC0_EINT_IRQn);
493 
494  //Valid Ethernet PHY or switch driver?
495  if(interface->phyDriver != NULL)
496  {
497  //Disable Ethernet PHY interrupts
498  interface->phyDriver->disableIrq(interface);
499  }
500  else if(interface->switchDriver != NULL)
501  {
502  //Disable Ethernet switch interrupts
503  interface->switchDriver->disableIrq(interface);
504  }
505  else
506  {
507  //Just for sanity
508  }
509 }
510 
511 
512 /**
513  * @brief RA8 Ethernet MAC interrupt service routine
514  **/
515 
517 {
518  bool_t flag;
519  uint32_t status;
520 
521  //Interrupt service routine prologue
522  osEnterIsr();
523 
524  //This flag will be set if a higher priority task must be woken
525  flag = FALSE;
526 
527  //Read interrupt status register
528  status = R_ETHERC_EDMAC->EESR;
529 
530  //Packet transmitted?
531  if((status & R_ETHERC_EDMAC_EESR_TWB_Msk) != 0)
532  {
533  //Clear TWB interrupt flag
534  R_ETHERC_EDMAC->EESR = R_ETHERC_EDMAC_EESR_TWB_Msk;
535 
536  //Check whether the TX buffer is available for writing
537  if((txDmaDesc[txIndex].td0 & EDMAC_TD0_TACT) == 0)
538  {
539  //Notify the TCP/IP stack that the transmitter is ready to send
540  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
541  }
542  }
543 
544  //Packet received?
545  if((status & R_ETHERC_EDMAC_EESR_FR_Msk) != 0)
546  {
547  //Clear FR interrupt flag
548  R_ETHERC_EDMAC->EESR = R_ETHERC_EDMAC_EESR_FR_Msk;
549 
550  //Set event flag
551  nicDriverInterface->nicEvent = TRUE;
552  //Notify the TCP/IP stack of the event
553  flag |= osSetEventFromIsr(&netEvent);
554  }
555 
556  //Clear IR flag
557  R_ICU->IELSR[EDMAC0_EINT_IRQn] &= ~R_ICU_IELSR_IR_Msk;
558 
559  //Interrupt service routine epilogue
560  osExitIsr(flag);
561 }
562 
563 
564 /**
565  * @brief RA8 Ethernet MAC event handler
566  * @param[in] interface Underlying network interface
567  **/
568 
570 {
571  error_t error;
572 
573  //Process all pending packets
574  do
575  {
576  //Read incoming packet
577  error = ra8EthReceivePacket(interface);
578 
579  //No more data in the receive buffer?
580  } while(error != ERROR_BUFFER_EMPTY);
581 }
582 
583 
584 /**
585  * @brief Send a packet
586  * @param[in] interface Underlying network interface
587  * @param[in] buffer Multi-part buffer containing the data to send
588  * @param[in] offset Offset to the first data byte
589  * @param[in] ancillary Additional options passed to the stack along with
590  * the packet
591  * @return Error code
592  **/
593 
595  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
596 {
597  //Retrieve the length of the packet
598  size_t length = netBufferGetLength(buffer) - offset;
599 
600  //Check the frame length
602  {
603  //The transmitter can accept another packet
604  osSetEvent(&interface->nicTxEvent);
605  //Report an error
606  return ERROR_INVALID_LENGTH;
607  }
608 
609  //Make sure the current buffer is available for writing
610  if((txDmaDesc[txIndex].td0 & EDMAC_TD0_TACT) != 0)
611  {
612  return ERROR_FAILURE;
613  }
614 
615  //Copy user data to the transmit buffer
616  netBufferRead(txBuffer[txIndex], buffer, offset, length);
617 
618  //Write the number of bytes to send
619  txDmaDesc[txIndex].td1 = (length << 16) & EDMAC_TD1_TBL;
620 
621  //Check current index
622  if(txIndex < (RA8_ETH_TX_BUFFER_COUNT - 1))
623  {
624  //Give the ownership of the descriptor to the DMA engine
625  txDmaDesc[txIndex].td0 = EDMAC_TD0_TACT | EDMAC_TD0_TFP_SOF |
627 
628  //Point to the next descriptor
629  txIndex++;
630  }
631  else
632  {
633  //Give the ownership of the descriptor to the DMA engine
634  txDmaDesc[txIndex].td0 = EDMAC_TD0_TACT | EDMAC_TD0_TDLE |
636 
637  //Wrap around
638  txIndex = 0;
639  }
640 
641  //Instruct the DMA to poll the transmit descriptor list
642  R_ETHERC_EDMAC->EDTRR = R_ETHERC_EDMAC_EDTRR_TR_Msk;
643 
644  //Check whether the next buffer is available for writing
645  if((txDmaDesc[txIndex].td0 & EDMAC_TD0_TACT) == 0)
646  {
647  //The transmitter can accept another packet
648  osSetEvent(&interface->nicTxEvent);
649  }
650 
651  //Successful write operation
652  return NO_ERROR;
653 }
654 
655 
656 /**
657  * @brief Receive a packet
658  * @param[in] interface Underlying network interface
659  * @return Error code
660  **/
661 
663 {
664  error_t error;
665  size_t n;
666  NetRxAncillary ancillary;
667 
668  //Current buffer available for reading?
669  if((rxDmaDesc[rxIndex].rd0 & EDMAC_RD0_RACT) == 0)
670  {
671  //SOF and EOF flags should be set
672  if((rxDmaDesc[rxIndex].rd0 & EDMAC_RD0_RFP_SOF) != 0 &&
673  (rxDmaDesc[rxIndex].rd0 & EDMAC_RD0_RFP_EOF) != 0)
674  {
675  //Make sure no error occurred
676  if((rxDmaDesc[rxIndex].rd0 & (EDMAC_RD0_RFS_MASK & ~EDMAC_RD0_RFS_RMAF)) == 0)
677  {
678  //Retrieve the length of the frame
679  n = rxDmaDesc[rxIndex].rd1 & EDMAC_RD1_RFL;
680  //Limit the number of data to read
682 
683  //Additional options can be passed to the stack along with the packet
684  ancillary = NET_DEFAULT_RX_ANCILLARY;
685 
686  //Pass the packet to the upper layer
687  nicProcessPacket(interface, rxBuffer[rxIndex], n, &ancillary);
688 
689  //Valid packet received
690  error = NO_ERROR;
691  }
692  else
693  {
694  //The received packet contains an error
695  error = ERROR_INVALID_PACKET;
696  }
697  }
698  else
699  {
700  //The packet is not valid
701  error = ERROR_INVALID_PACKET;
702  }
703 
704  //Check current index
705  if(rxIndex < (RA8_ETH_RX_BUFFER_COUNT - 1))
706  {
707  //Give the ownership of the descriptor back to the DMA
708  rxDmaDesc[rxIndex].rd0 = EDMAC_RD0_RACT;
709  //Point to the next descriptor
710  rxIndex++;
711  }
712  else
713  {
714  //Give the ownership of the descriptor back to the DMA
715  rxDmaDesc[rxIndex].rd0 = EDMAC_RD0_RACT | EDMAC_RD0_RDLE;
716  //Wrap around
717  rxIndex = 0;
718  }
719 
720  //Instruct the DMA to poll the receive descriptor list
721  R_ETHERC_EDMAC->EDRRR = R_ETHERC_EDMAC_EDRRR_RR_Msk;
722  }
723  else
724  {
725  //No more data in the receive buffer
726  error = ERROR_BUFFER_EMPTY;
727  }
728 
729  //Return status code
730  return error;
731 }
732 
733 
734 /**
735  * @brief Configure MAC address filtering
736  * @param[in] interface Underlying network interface
737  * @return Error code
738  **/
739 
741 {
742  uint_t i;
743  bool_t acceptMulticast;
744 
745  //Debug message
746  TRACE_DEBUG("Updating MAC filter...\r\n");
747 
748  //Promiscuous mode?
749  if(interface->promiscuous)
750  {
751  //Accept all frames regardless of their destination address
752  R_ETHERC0->ECMR |= R_ETHERC0_ECMR_PRM_Msk;
753  }
754  else
755  {
756  //Disable promiscuous mode
757  R_ETHERC0->ECMR &= ~R_ETHERC0_ECMR_PRM_Msk;
758 
759  //Set the upper 32 bits of the MAC address
760  R_ETHERC0->MAHR = (interface->macAddr.b[0] << 24) | (interface->macAddr.b[1] << 16) |
761  (interface->macAddr.b[2] << 8) | interface->macAddr.b[3];
762 
763  //Set the lower 16 bits of the MAC address
764  R_ETHERC0->MALR = (interface->macAddr.b[4] << 8) | interface->macAddr.b[5];
765 
766  //This flag will be set if multicast addresses should be accepted
767  acceptMulticast = FALSE;
768 
769  //The MAC address filter contains the list of MAC addresses to accept
770  //when receiving an Ethernet frame
771  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
772  {
773  //Valid entry?
774  if(interface->macAddrFilter[i].refCount > 0)
775  {
776  //Accept multicast addresses
777  acceptMulticast = TRUE;
778  //We are done
779  break;
780  }
781  }
782 
783  //Enable or disable the reception of multicast frames
784  if(acceptMulticast || interface->acceptAllMulticast)
785  {
786  R_ETHERC_EDMAC->EESR |= R_ETHERC_EDMAC_EESR_RMAF_Msk;
787  }
788  else
789  {
790  R_ETHERC_EDMAC->EESR &= ~R_ETHERC_EDMAC_EESR_RMAF_Msk;
791  }
792  }
793 
794  //Successful processing
795  return NO_ERROR;
796 }
797 
798 
799 /**
800  * @brief Adjust MAC configuration parameters for proper operation
801  * @param[in] interface Underlying network interface
802  * @return Error code
803  **/
804 
806 {
807  uint32_t mode;
808 
809  //Read ETHERC mode register
810  mode = R_ETHERC0->ECMR;
811 
812  //10BASE-T or 100BASE-TX operation mode?
813  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
814  {
815  mode |= R_ETHERC0_ECMR_RTM_Msk;
816  }
817  else
818  {
819  mode &= ~R_ETHERC0_ECMR_RTM_Msk;
820  }
821 
822  //Half-duplex or full-duplex mode?
823  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
824  {
825  mode |= R_ETHERC0_ECMR_DM_Msk;
826  }
827  else
828  {
829  mode &= ~R_ETHERC0_ECMR_DM_Msk;
830  }
831 
832  //Update ETHERC mode register
833  R_ETHERC0->ECMR = mode;
834 
835  //Successful processing
836  return NO_ERROR;
837 }
838 
839 
840 /**
841  * @brief Write PHY register
842  * @param[in] opcode Access type (2 bits)
843  * @param[in] phyAddr PHY address (5 bits)
844  * @param[in] regAddr Register address (5 bits)
845  * @param[in] data Register value
846  **/
847 
848 void ra8EthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
849  uint8_t regAddr, uint16_t data)
850 {
851  //Synchronization pattern
853  //Start of frame
855  //Set up a write operation
857  //Write PHY address
858  ra8EthWriteSmi(phyAddr, 5);
859  //Write register address
861  //Turnaround
863  //Write register value
864  ra8EthWriteSmi(data, 16);
865  //Release MDIO
866  ra8EthReadSmi(1);
867 }
868 
869 
870 /**
871  * @brief Read PHY register
872  * @param[in] opcode Access type (2 bits)
873  * @param[in] phyAddr PHY address (5 bits)
874  * @param[in] regAddr Register address (5 bits)
875  * @return Register value
876  **/
877 
878 uint16_t ra8EthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
879  uint8_t regAddr)
880 {
881  uint16_t data;
882 
883  //Synchronization pattern
885  //Start of frame
887  //Set up a read operation
889  //Write PHY address
890  ra8EthWriteSmi(phyAddr, 5);
891  //Write register address
893  //Turnaround to avoid contention
894  ra8EthReadSmi(1);
895  //Read register value
896  data = ra8EthReadSmi(16);
897  //Force the PHY to release the MDIO pin
898  ra8EthReadSmi(1);
899 
900  //Return PHY register contents
901  return data;
902 }
903 
904 
905 /**
906  * @brief SMI write operation
907  * @param[in] data Raw data to be written
908  * @param[in] length Number of bits to be written
909  **/
910 
912 {
913  //Skip the most significant bits since they are meaningless
914  data <<= 32 - length;
915 
916  //Configure MDIO as an output
917  R_ETHERC0->PIR |= R_ETHERC0_PIR_MMD_Msk;
918 
919  //Write the specified number of bits
920  while(length--)
921  {
922  //Write MDIO
923  if((data & 0x80000000) != 0)
924  {
925  R_ETHERC0->PIR |= R_ETHERC0_PIR_MDO_Msk;
926  }
927  else
928  {
929  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MDO_Msk;
930  }
931 
932  //Assert MDC
933  usleep(1);
934  R_ETHERC0->PIR |= R_ETHERC0_PIR_MDC_Msk;
935  //Deassert MDC
936  usleep(1);
937  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MDC_Msk;
938 
939  //Rotate data
940  data <<= 1;
941  }
942 }
943 
944 
945 /**
946  * @brief SMI read operation
947  * @param[in] length Number of bits to be read
948  * @return Data resulting from the MDIO read operation
949  **/
950 
952 {
953  uint32_t data = 0;
954 
955  //Configure MDIO as an input
956  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MMD_Msk;
957 
958  //Read the specified number of bits
959  while(length--)
960  {
961  //Rotate data
962  data <<= 1;
963 
964  //Assert MDC
965  R_ETHERC0->PIR |= R_ETHERC0_PIR_MDC_Msk;
966  usleep(1);
967  //Deassert MDC
968  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MDC_Msk;
969  usleep(1);
970 
971  //Check MDIO state
972  if((R_ETHERC0->PIR & R_ETHERC0_PIR_MDI_Msk) != 0)
973  {
974  data |= 0x01;
975  }
976  }
977 
978  //Return the received data
979  return data;
980 }
#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)
#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 EDMAC_TD0_TACT
#define EDMAC_RD1_RBL
#define R_ETHERC_EDMAC
#define R_MSTP_MSTPCRB_MSTPB15_Msk
#define EDMAC_TD0_TDLE
#define EDMAC_TD0_TFP_EOF
#define EDMAC_RD0_RACT
#define EDMAC_RD0_RFP_EOF
#define EDMAC_RD0_RFP_SOF
#define EDMAC_TD0_TFP_SOF
void ra8EthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
error_t ra8EthInit(NetInterface *interface)
RA8 Ethernet MAC initialization.
void ra8EthEnableIrq(NetInterface *interface)
Enable interrupts.
uint16_t ra8EthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
error_t ra8EthReceivePacket(NetInterface *interface)
Receive a packet.
void ra8EthTick(NetInterface *interface)
RA8 Ethernet MAC timer handler.
const NicDriver ra8EthDriver
RA8 Ethernet MAC driver.
error_t ra8EthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
void EDMAC0_EINT_IRQHandler(void)
RA8 Ethernet MAC interrupt service routine.
void ra8EthInitDmaDesc(NetInterface *interface)
Initialize DMA descriptor lists.
void ra8EthWriteSmi(uint32_t data, uint_t length)
SMI write operation.
void ra8EthDisableIrq(NetInterface *interface)
Disable interrupts.
error_t ra8EthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
uint32_t ra8EthReadSmi(uint_t length)
SMI read operation.
void ra8EthEventHandler(NetInterface *interface)
RA8 Ethernet MAC event handler.
error_t ra8EthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
__weak_func void ra8EthInitGpio(NetInterface *interface)
GPIO configuration.
Renesas RA8D1 / RA8M1 Ethernet MAC driver.
#define RA8_ETH_TX_BUFFER_COUNT
#define RA8_ETH_IRQ_GROUP_PRIORITY
#define RA8_ETH_RX_BUFFER_COUNT
#define RA8_ETH_IRQ_PRIORITY_GROUPING
#define RA8_ETH_RAM_SECTION
#define RA8_ETH_IRQ_SUB_PRIORITY
#define RA8_ETH_TX_BUFFER_SIZE
#define RA8_ETH_RX_BUFFER_SIZE
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