ra8_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file ra8_eth_driver.c
3  * @brief Renesas RA8M1 / RA8D1 / RA8T1 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.4
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-RA8M1 evaluation board?
235 #if defined(USE_EK_RA8M1)
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 (P4_4)
291  R_PFS->PORT[4].PIN[4].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_PORT4->PCNTR3 = (1 << 4) << R_PORT0_PCNTR3_PORR_Pos;
299  sleep(10);
300  R_PORT4->PCNTR3 = (1 << 4) << R_PORT0_PCNTR3_POSR_Pos;
301  sleep(10);
302 
303 //EK-RA8D1 evaluation board?
304 #elif defined(USE_EK_RA8D1)
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 (P7_6)
360  R_PFS->PORT[7].PIN[6].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_PORT7->PCNTR3 = (1 << 6) << R_PORT0_PCNTR3_PORR_Pos;
368  sleep(10);
369  R_PORT7->PCNTR3 = (1 << 6) << R_PORT0_PCNTR3_POSR_Pos;
370  sleep(10);
371 
372 //MCK-RA8T1 evaluation board?
373 #elif defined(USE_MCK_RA8T1)
374  //Disable protection
375  R_SYSTEM->PRCR = 0xA50B;
376  //Disable VBATT channel 0 input (P4_2)
377  R_SYSTEM->VBTICTLR &= ~R_SYSTEM_VBTICTLR_VCH0INEN_Msk;
378  //Enable protection
379  R_SYSTEM->PRCR = 0xA500;
380 
381  //Unlock PFS registers
382  R_PMISC->PWPRS &= ~R_PMISC_PWPR_B0WI_Msk;
383  R_PMISC->PWPRS |= R_PMISC_PWPR_PFSWE_Msk;
384 
385  //Select RMII interface mode
386  R_PMISC->PFENET &= ~R_PMISC_PFENET_PHYMODE0_Msk;
387 
388  //Configure ET0_MDC (P4_1)
389  R_PFS->PORT[4].PIN[1].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
390  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (1 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
391 
392  //Configure ET0_MDIO (P4_2)
393  R_PFS->PORT[4].PIN[2].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
394  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (1 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
395 
396  //Configure RMII0_TXD_EN_B (P4_5)
397  R_PFS->PORT[4].PIN[5].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
398  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
399 
400  //Configure RMII0_TXD1_B (P4_6)
401  R_PFS->PORT[4].PIN[6].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
402  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
403 
404  //Configure RMII0_TXD0_B (P7_0)
405  R_PFS->PORT[7].PIN[0].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
406  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
407 
408  //Configure REF50CK0_B (P7_1)
409  R_PFS->PORT[7].PIN[1].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
410  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
411 
412  //Configure RMII0_RXD0_B (P7_2)
413  R_PFS->PORT[7].PIN[2].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
414  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
415 
416  //Configure RMII0_RXD1_B (P7_3)
417  R_PFS->PORT[7].PIN[3].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
418  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
419 
420  //Configure RMII0_RX_ER_B (P7_4)
421  R_PFS->PORT[7].PIN[4].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
422  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
423 
424  //Configure RMII0_CRS_DV_B (P7_5)
425  R_PFS->PORT[7].PIN[5].PmnPFS = (23 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos) |
426  R_PFS_PORT_PIN_PmnPFS_PMR_Msk | (3 << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos);
427 
428  //Configure PHY reset pin (PB_1)
429  R_PFS->PORT[11].PIN[1].PmnPFS = R_PFS_PORT_PIN_PmnPFS_PDR_Msk;
430 
431  //Lock PFS registers
432  R_PMISC->PWPRS &= ~R_PMISC_PWPR_PFSWE_Msk;
433  R_PMISC->PWPRS |= R_PMISC_PWPR_B0WI_Msk;
434 
435  //Reset PHY transceiver
436  R_PORTB->PCNTR3 = (1 << 1) << R_PORTB_PCNTR3_PORR_Pos;
437  sleep(10);
438  R_PORTB->PCNTR3 = (1 << 1) << R_PORTB_PCNTR3_POSR_Pos;
439  sleep(10);
440 #endif
441 }
442 
443 
444 /**
445  * @brief Initialize DMA descriptor lists
446  * @param[in] interface Underlying network interface
447  **/
448 
450 {
451  uint_t i;
452 
453  //Initialize TX descriptors
454  for(i = 0; i < RA8_ETH_TX_BUFFER_COUNT; i++)
455  {
456  //The descriptor is initially owned by the application
457  txDmaDesc[i].td0 = 0;
458  //Transmit buffer length
459  txDmaDesc[i].td1 = 0;
460  //Transmit buffer address
461  txDmaDesc[i].td2 = (uint32_t) txBuffer[i];
462  //Clear padding field
463  txDmaDesc[i].padding = 0;
464  }
465 
466  //Mark the last descriptor entry with the TDLE flag
467  txDmaDesc[i - 1].td0 |= EDMAC_TD0_TDLE;
468  //Initialize TX descriptor index
469  txIndex = 0;
470 
471  //Initialize RX descriptors
472  for(i = 0; i < RA8_ETH_RX_BUFFER_COUNT; i++)
473  {
474  //The descriptor is initially owned by the DMA
475  rxDmaDesc[i].rd0 = EDMAC_RD0_RACT;
476  //Receive buffer length
478  //Receive buffer address
479  rxDmaDesc[i].rd2 = (uint32_t) rxBuffer[i];
480  //Clear padding field
481  rxDmaDesc[i].padding = 0;
482  }
483 
484  //Mark the last descriptor entry with the RDLE flag
485  rxDmaDesc[i - 1].rd0 |= EDMAC_RD0_RDLE;
486  //Initialize RX descriptor index
487  rxIndex = 0;
488 
489  //Start address of the TX descriptor list
490  R_ETHERC_EDMAC->TDLAR = (uint32_t) txDmaDesc;
491  //Start address of the RX descriptor list
492  R_ETHERC_EDMAC->RDLAR = (uint32_t) rxDmaDesc;
493 }
494 
495 
496 /**
497  * @brief RA8 Ethernet MAC timer handler
498  *
499  * This routine is periodically called by the TCP/IP stack to handle periodic
500  * operations such as polling the link state
501  *
502  * @param[in] interface Underlying network interface
503  **/
504 
505 void ra8EthTick(NetInterface *interface)
506 {
507  //Valid Ethernet PHY or switch driver?
508  if(interface->phyDriver != NULL)
509  {
510  //Handle periodic operations
511  interface->phyDriver->tick(interface);
512  }
513  else if(interface->switchDriver != NULL)
514  {
515  //Handle periodic operations
516  interface->switchDriver->tick(interface);
517  }
518  else
519  {
520  //Just for sanity
521  }
522 }
523 
524 
525 /**
526  * @brief Enable interrupts
527  * @param[in] interface Underlying network interface
528  **/
529 
531 {
532  //Enable Ethernet MAC interrupts
533  NVIC_EnableIRQ(EDMAC0_EINT_IRQn);
534 
535  //Valid Ethernet PHY or switch driver?
536  if(interface->phyDriver != NULL)
537  {
538  //Enable Ethernet PHY interrupts
539  interface->phyDriver->enableIrq(interface);
540  }
541  else if(interface->switchDriver != NULL)
542  {
543  //Enable Ethernet switch interrupts
544  interface->switchDriver->enableIrq(interface);
545  }
546  else
547  {
548  //Just for sanity
549  }
550 }
551 
552 
553 /**
554  * @brief Disable interrupts
555  * @param[in] interface Underlying network interface
556  **/
557 
559 {
560  //Disable Ethernet MAC interrupts
561  NVIC_DisableIRQ(EDMAC0_EINT_IRQn);
562 
563  //Valid Ethernet PHY or switch driver?
564  if(interface->phyDriver != NULL)
565  {
566  //Disable Ethernet PHY interrupts
567  interface->phyDriver->disableIrq(interface);
568  }
569  else if(interface->switchDriver != NULL)
570  {
571  //Disable Ethernet switch interrupts
572  interface->switchDriver->disableIrq(interface);
573  }
574  else
575  {
576  //Just for sanity
577  }
578 }
579 
580 
581 /**
582  * @brief RA8 Ethernet MAC interrupt service routine
583  **/
584 
586 {
587  bool_t flag;
588  uint32_t status;
589 
590  //Interrupt service routine prologue
591  osEnterIsr();
592 
593  //This flag will be set if a higher priority task must be woken
594  flag = FALSE;
595 
596  //Read interrupt status register
597  status = R_ETHERC_EDMAC->EESR;
598 
599  //Packet transmitted?
600  if((status & R_ETHERC_EDMAC_EESR_TWB_Msk) != 0)
601  {
602  //Clear TWB interrupt flag
603  R_ETHERC_EDMAC->EESR = R_ETHERC_EDMAC_EESR_TWB_Msk;
604 
605  //Check whether the TX buffer is available for writing
606  if((txDmaDesc[txIndex].td0 & EDMAC_TD0_TACT) == 0)
607  {
608  //Notify the TCP/IP stack that the transmitter is ready to send
609  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
610  }
611  }
612 
613  //Packet received?
614  if((status & R_ETHERC_EDMAC_EESR_FR_Msk) != 0)
615  {
616  //Clear FR interrupt flag
617  R_ETHERC_EDMAC->EESR = R_ETHERC_EDMAC_EESR_FR_Msk;
618 
619  //Set event flag
620  nicDriverInterface->nicEvent = TRUE;
621  //Notify the TCP/IP stack of the event
622  flag |= osSetEventFromIsr(&netEvent);
623  }
624 
625  //Clear IR flag
626  R_ICU->IELSR[EDMAC0_EINT_IRQn] &= ~R_ICU_IELSR_IR_Msk;
627 
628  //Interrupt service routine epilogue
629  osExitIsr(flag);
630 }
631 
632 
633 /**
634  * @brief RA8 Ethernet MAC event handler
635  * @param[in] interface Underlying network interface
636  **/
637 
639 {
640  error_t error;
641 
642  //Process all pending packets
643  do
644  {
645  //Read incoming packet
646  error = ra8EthReceivePacket(interface);
647 
648  //No more data in the receive buffer?
649  } while(error != ERROR_BUFFER_EMPTY);
650 }
651 
652 
653 /**
654  * @brief Send a packet
655  * @param[in] interface Underlying network interface
656  * @param[in] buffer Multi-part buffer containing the data to send
657  * @param[in] offset Offset to the first data byte
658  * @param[in] ancillary Additional options passed to the stack along with
659  * the packet
660  * @return Error code
661  **/
662 
664  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
665 {
666  //Retrieve the length of the packet
667  size_t length = netBufferGetLength(buffer) - offset;
668 
669  //Check the frame length
671  {
672  //The transmitter can accept another packet
673  osSetEvent(&interface->nicTxEvent);
674  //Report an error
675  return ERROR_INVALID_LENGTH;
676  }
677 
678  //Make sure the current buffer is available for writing
679  if((txDmaDesc[txIndex].td0 & EDMAC_TD0_TACT) != 0)
680  {
681  return ERROR_FAILURE;
682  }
683 
684  //Copy user data to the transmit buffer
685  netBufferRead(txBuffer[txIndex], buffer, offset, length);
686 
687  //Write the number of bytes to send
688  txDmaDesc[txIndex].td1 = (length << 16) & EDMAC_TD1_TBL;
689 
690  //Check current index
691  if(txIndex < (RA8_ETH_TX_BUFFER_COUNT - 1))
692  {
693  //Give the ownership of the descriptor to the DMA engine
694  txDmaDesc[txIndex].td0 = EDMAC_TD0_TACT | EDMAC_TD0_TFP_SOF |
696 
697  //Point to the next descriptor
698  txIndex++;
699  }
700  else
701  {
702  //Give the ownership of the descriptor to the DMA engine
703  txDmaDesc[txIndex].td0 = EDMAC_TD0_TACT | EDMAC_TD0_TDLE |
705 
706  //Wrap around
707  txIndex = 0;
708  }
709 
710  //Instruct the DMA to poll the transmit descriptor list
711  R_ETHERC_EDMAC->EDTRR = R_ETHERC_EDMAC_EDTRR_TR_Msk;
712 
713  //Check whether the next buffer is available for writing
714  if((txDmaDesc[txIndex].td0 & EDMAC_TD0_TACT) == 0)
715  {
716  //The transmitter can accept another packet
717  osSetEvent(&interface->nicTxEvent);
718  }
719 
720  //Successful write operation
721  return NO_ERROR;
722 }
723 
724 
725 /**
726  * @brief Receive a packet
727  * @param[in] interface Underlying network interface
728  * @return Error code
729  **/
730 
732 {
733  error_t error;
734  size_t n;
735  NetRxAncillary ancillary;
736 
737  //Current buffer available for reading?
738  if((rxDmaDesc[rxIndex].rd0 & EDMAC_RD0_RACT) == 0)
739  {
740  //SOF and EOF flags should be set
741  if((rxDmaDesc[rxIndex].rd0 & EDMAC_RD0_RFP_SOF) != 0 &&
742  (rxDmaDesc[rxIndex].rd0 & EDMAC_RD0_RFP_EOF) != 0)
743  {
744  //Make sure no error occurred
745  if((rxDmaDesc[rxIndex].rd0 & (EDMAC_RD0_RFS_MASK & ~EDMAC_RD0_RFS_RMAF)) == 0)
746  {
747  //Retrieve the length of the frame
748  n = rxDmaDesc[rxIndex].rd1 & EDMAC_RD1_RFL;
749  //Limit the number of data to read
751 
752  //Additional options can be passed to the stack along with the packet
753  ancillary = NET_DEFAULT_RX_ANCILLARY;
754 
755  //Pass the packet to the upper layer
756  nicProcessPacket(interface, rxBuffer[rxIndex], n, &ancillary);
757 
758  //Valid packet received
759  error = NO_ERROR;
760  }
761  else
762  {
763  //The received packet contains an error
764  error = ERROR_INVALID_PACKET;
765  }
766  }
767  else
768  {
769  //The packet is not valid
770  error = ERROR_INVALID_PACKET;
771  }
772 
773  //Check current index
774  if(rxIndex < (RA8_ETH_RX_BUFFER_COUNT - 1))
775  {
776  //Give the ownership of the descriptor back to the DMA
777  rxDmaDesc[rxIndex].rd0 = EDMAC_RD0_RACT;
778  //Point to the next descriptor
779  rxIndex++;
780  }
781  else
782  {
783  //Give the ownership of the descriptor back to the DMA
784  rxDmaDesc[rxIndex].rd0 = EDMAC_RD0_RACT | EDMAC_RD0_RDLE;
785  //Wrap around
786  rxIndex = 0;
787  }
788 
789  //Instruct the DMA to poll the receive descriptor list
790  R_ETHERC_EDMAC->EDRRR = R_ETHERC_EDMAC_EDRRR_RR_Msk;
791  }
792  else
793  {
794  //No more data in the receive buffer
795  error = ERROR_BUFFER_EMPTY;
796  }
797 
798  //Return status code
799  return error;
800 }
801 
802 
803 /**
804  * @brief Configure MAC address filtering
805  * @param[in] interface Underlying network interface
806  * @return Error code
807  **/
808 
810 {
811  uint_t i;
812  bool_t acceptMulticast;
813 
814  //Debug message
815  TRACE_DEBUG("Updating MAC filter...\r\n");
816 
817  //Promiscuous mode?
818  if(interface->promiscuous)
819  {
820  //Accept all frames regardless of their destination address
821  R_ETHERC0->ECMR |= R_ETHERC0_ECMR_PRM_Msk;
822  }
823  else
824  {
825  //Disable promiscuous mode
826  R_ETHERC0->ECMR &= ~R_ETHERC0_ECMR_PRM_Msk;
827 
828  //Set the upper 32 bits of the MAC address
829  R_ETHERC0->MAHR = (interface->macAddr.b[0] << 24) | (interface->macAddr.b[1] << 16) |
830  (interface->macAddr.b[2] << 8) | interface->macAddr.b[3];
831 
832  //Set the lower 16 bits of the MAC address
833  R_ETHERC0->MALR = (interface->macAddr.b[4] << 8) | interface->macAddr.b[5];
834 
835  //This flag will be set if multicast addresses should be accepted
836  acceptMulticast = FALSE;
837 
838  //The MAC address filter contains the list of MAC addresses to accept
839  //when receiving an Ethernet frame
840  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
841  {
842  //Valid entry?
843  if(interface->macAddrFilter[i].refCount > 0)
844  {
845  //Accept multicast addresses
846  acceptMulticast = TRUE;
847  //We are done
848  break;
849  }
850  }
851 
852  //Enable or disable the reception of multicast frames
853  if(acceptMulticast || interface->acceptAllMulticast)
854  {
855  R_ETHERC_EDMAC->EESR |= R_ETHERC_EDMAC_EESR_RMAF_Msk;
856  }
857  else
858  {
859  R_ETHERC_EDMAC->EESR &= ~R_ETHERC_EDMAC_EESR_RMAF_Msk;
860  }
861  }
862 
863  //Successful processing
864  return NO_ERROR;
865 }
866 
867 
868 /**
869  * @brief Adjust MAC configuration parameters for proper operation
870  * @param[in] interface Underlying network interface
871  * @return Error code
872  **/
873 
875 {
876  uint32_t mode;
877 
878  //Read ETHERC mode register
879  mode = R_ETHERC0->ECMR;
880 
881  //10BASE-T or 100BASE-TX operation mode?
882  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
883  {
884  mode |= R_ETHERC0_ECMR_RTM_Msk;
885  }
886  else
887  {
888  mode &= ~R_ETHERC0_ECMR_RTM_Msk;
889  }
890 
891  //Half-duplex or full-duplex mode?
892  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
893  {
894  mode |= R_ETHERC0_ECMR_DM_Msk;
895  }
896  else
897  {
898  mode &= ~R_ETHERC0_ECMR_DM_Msk;
899  }
900 
901  //Update ETHERC mode register
902  R_ETHERC0->ECMR = mode;
903 
904  //Successful processing
905  return NO_ERROR;
906 }
907 
908 
909 /**
910  * @brief Write PHY register
911  * @param[in] opcode Access type (2 bits)
912  * @param[in] phyAddr PHY address (5 bits)
913  * @param[in] regAddr Register address (5 bits)
914  * @param[in] data Register value
915  **/
916 
917 void ra8EthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
918  uint8_t regAddr, uint16_t data)
919 {
920  //Synchronization pattern
922  //Start of frame
924  //Set up a write operation
926  //Write PHY address
927  ra8EthWriteSmi(phyAddr, 5);
928  //Write register address
930  //Turnaround
932  //Write register value
933  ra8EthWriteSmi(data, 16);
934  //Release MDIO
935  ra8EthReadSmi(1);
936 }
937 
938 
939 /**
940  * @brief Read PHY register
941  * @param[in] opcode Access type (2 bits)
942  * @param[in] phyAddr PHY address (5 bits)
943  * @param[in] regAddr Register address (5 bits)
944  * @return Register value
945  **/
946 
947 uint16_t ra8EthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
948  uint8_t regAddr)
949 {
950  uint16_t data;
951 
952  //Synchronization pattern
954  //Start of frame
956  //Set up a read operation
958  //Write PHY address
959  ra8EthWriteSmi(phyAddr, 5);
960  //Write register address
962  //Turnaround to avoid contention
963  ra8EthReadSmi(1);
964  //Read register value
965  data = ra8EthReadSmi(16);
966  //Force the PHY to release the MDIO pin
967  ra8EthReadSmi(1);
968 
969  //Return PHY register contents
970  return data;
971 }
972 
973 
974 /**
975  * @brief SMI write operation
976  * @param[in] data Raw data to be written
977  * @param[in] length Number of bits to be written
978  **/
979 
981 {
982  //Skip the most significant bits since they are meaningless
983  data <<= 32 - length;
984 
985  //Configure MDIO as an output
986  R_ETHERC0->PIR |= R_ETHERC0_PIR_MMD_Msk;
987 
988  //Write the specified number of bits
989  while(length--)
990  {
991  //Write MDIO
992  if((data & 0x80000000) != 0)
993  {
994  R_ETHERC0->PIR |= R_ETHERC0_PIR_MDO_Msk;
995  }
996  else
997  {
998  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MDO_Msk;
999  }
1000 
1001  //Assert MDC
1002  usleep(1);
1003  R_ETHERC0->PIR |= R_ETHERC0_PIR_MDC_Msk;
1004  //Deassert MDC
1005  usleep(1);
1006  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MDC_Msk;
1007 
1008  //Rotate data
1009  data <<= 1;
1010  }
1011 }
1012 
1013 
1014 /**
1015  * @brief SMI read operation
1016  * @param[in] length Number of bits to be read
1017  * @return Data resulting from the MDIO read operation
1018  **/
1019 
1021 {
1022  uint32_t data = 0;
1023 
1024  //Configure MDIO as an input
1025  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MMD_Msk;
1026 
1027  //Read the specified number of bits
1028  while(length--)
1029  {
1030  //Rotate data
1031  data <<= 1;
1032 
1033  //Assert MDC
1034  R_ETHERC0->PIR |= R_ETHERC0_PIR_MDC_Msk;
1035  usleep(1);
1036  //Deassert MDC
1037  R_ETHERC0->PIR &= ~R_ETHERC0_PIR_MDC_Msk;
1038  usleep(1);
1039 
1040  //Check MDIO state
1041  if((R_ETHERC0->PIR & R_ETHERC0_PIR_MDI_Msk) != 0)
1042  {
1043  data |= 0x01;
1044  }
1045  }
1046 
1047  //Return the received data
1048  return data;
1049 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define EDMAC_RD0_RFP_EOF
#define usleep(delay)
Definition: os_port.h:303
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
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:690
#define RA8_ETH_IRQ_GROUP_PRIORITY
void ra8EthEnableIrq(NetInterface *interface)
Enable interrupts.
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
#define sleep(delay)
Definition: os_port.h:307
void ra8EthWriteSmi(uint32_t data, uint_t length)
SMI write operation.
#define EDMAC_RD0_RACT
#define RA8_ETH_TX_BUFFER_SIZE
#define R_MSTP_MSTPCRB_MSTPB15_Msk
uint16_t ra8EthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define SMI_TA
Definition: nic.h:68
Renesas RA8M1 / RA8D1 / RA8T1 Ethernet MAC driver.
#define SMI_START
Definition: nic.h:64
error_t ra8EthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:392
void ra8EthInitDmaDesc(NetInterface *interface)
Initialize DMA descriptor lists.
void EDMAC0_EINT_IRQHandler(void)
RA8 Ethernet MAC interrupt service routine.
#define osExitIsr(flag)
#define EDMAC_TD0_TWBI
error_t ra8EthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define EDMAC_RD1_RFL
#define EDMAC_RD0_RDLE
#define FALSE
Definition: os_port.h:46
#define EDMAC_RD0_RFP_SOF
void ra8EthDisableIrq(NetInterface *interface)
Disable interrupts.
error_t
Error codes.
Definition: error.h:43
__weak_func void ra8EthInitGpio(NetInterface *interface)
GPIO configuration.
#define EDMAC_RD1_RBL
#define R_ETHERC_EDMAC
#define RA8_ETH_RX_BUFFER_SIZE
#define RA8_ETH_IRQ_SUB_PRIORITY
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
#define EDMAC_RD0_RFS_RMAF
@ ERROR_INVALID_PACKET
Definition: error.h:140
#define NetInterface
Definition: net.h:36
@ ERROR_INVALID_LENGTH
Definition: error.h:111
#define EDMAC_RD0_RFS_MASK
const NicDriver ra8EthDriver
RA8 Ethernet MAC driver.
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
#define NetTxAncillary
Definition: net_misc.h:36
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t length
Definition: tcp.h:368
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
#define MIN(a, b)
Definition: os_port.h:63
#define RA8_ETH_RAM_SECTION
#define RA8_ETH_RX_BUFFER_COUNT
#define rxBuffer
uint32_t ra8EthReadSmi(uint_t length)
SMI read operation.
error_t ra8EthReceivePacket(NetInterface *interface)
Receive a packet.
void ra8EthEventHandler(NetInterface *interface)
RA8 Ethernet MAC event handler.
#define EDMAC_TD1_TBL
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define EDMAC_TD0_TFP_SOF
#define EDMAC_TD0_TDLE
error_t ra8EthInit(NetInterface *interface)
RA8 Ethernet MAC initialization.
uint16_t regAddr
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
#define osEnterIsr()
error_t ra8EthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
Transmit DMA descriptor.
void ra8EthTick(NetInterface *interface)
RA8 Ethernet MAC timer handler.
#define rxDmaDesc
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define txDmaDesc
#define RA8_ETH_IRQ_PRIORITY_GROUPING
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define EDMAC_TD0_TACT
#define SMI_SYNC
Definition: nic.h:63
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
#define RA8_ETH_TX_BUFFER_COUNT
NIC driver.
Definition: nic.h:286
void ra8EthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
Receive DMA descriptor.
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
#define EDMAC_TD0_TFP_EOF
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83