stm32n6xx_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file stm32n6xx_eth_driver.c
3  * @brief STM32N6 Gigabit Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "stm32n6xx.h"
36 #include "stm32n6xx_hal.h"
37 #include "core/net.h"
39 #include "debug.h"
40 
41 //Underlying network interface
42 static NetInterface *nicDriverInterface;
43 
44 //IAR EWARM compiler?
45 #if defined(__ICCARM__)
46 
47 //Transmit buffer
48 #pragma data_alignment = 4
49 #pragma location = STM32N6XX_ETH_RAM_SECTION
51 //Receive buffer
52 #pragma data_alignment = 4
53 #pragma location = STM32N6XX_ETH_RAM_SECTION
55 //Transmit DMA descriptors
56 #pragma data_alignment = 8
57 #pragma location = STM32N6XX_ETH_RAM_SECTION
59 //Receive DMA descriptors
60 #pragma data_alignment = 8
61 #pragma location = STM32N6XX_ETH_RAM_SECTION
63 
64 //Keil MDK-ARM or GCC compiler?
65 #else
66 
67 //Transmit buffer
69  __attribute__((aligned(4), __section__(STM32N6XX_ETH_RAM_SECTION)));
70 //Receive buffer
72  __attribute__((aligned(4), __section__(STM32N6XX_ETH_RAM_SECTION)));
73 //Transmit DMA descriptors
75  __attribute__((aligned(8), __section__(STM32N6XX_ETH_RAM_SECTION)));
76 //Receive DMA descriptors
78  __attribute__((aligned(8), __section__(STM32N6XX_ETH_RAM_SECTION)));
79 
80 #endif
81 
82 //Current transmit descriptor
83 static uint_t txIndex;
84 //Current receive descriptor
85 static uint_t rxIndex;
86 
87 
88 /**
89  * @brief STM32N6 Ethernet MAC driver
90  **/
91 
93 {
95  ETH_MTU,
106  TRUE,
107  TRUE,
108  TRUE,
109  FALSE
110 };
111 
112 
113 /**
114  * @brief STM32N6 Ethernet MAC initialization
115  * @param[in] interface Underlying network interface
116  * @return Error code
117  **/
118 
120 {
121  error_t error;
122  uint32_t temp;
123 
124  //Debug message
125  TRACE_INFO("Initializing STM32N6 Ethernet MAC...\r\n");
126 
127  //Save underlying network interface
128  nicDriverInterface = interface;
129 
130  //GPIO configuration
131  stm32n6xxEthInitGpio(interface);
132 
133  //Enable Ethernet MAC clock
134  __HAL_RCC_ETH1_CLK_ENABLE();
135  __HAL_RCC_ETH1MAC_CLK_ENABLE();
136  __HAL_RCC_ETH1TX_CLK_ENABLE();
137  __HAL_RCC_ETH1RX_CLK_ENABLE();
138 
139  //Reset Ethernet MAC peripheral
140  __HAL_RCC_ETH1_FORCE_RESET();
141  __HAL_RCC_ETH1_RELEASE_RESET();
142 
143  //Perform a software reset
144  ETH1->DMAMR |= ETH_DMAMR_SWR;
145  //Wait for the reset to complete
146  while((ETH1->DMAMR & ETH_DMAMR_SWR) != 0)
147  {
148  }
149 
150  //Adjust MDC clock range depending on HCLK frequency
151  ETH1->MACMDIOAR = ETH_MACMDIOAR_CR_Val(5);
152 
153  //Valid Ethernet PHY or switch driver?
154  if(interface->phyDriver != NULL)
155  {
156  //Ethernet PHY initialization
157  error = interface->phyDriver->init(interface);
158  }
159  else if(interface->switchDriver != NULL)
160  {
161  //Ethernet switch initialization
162  error = interface->switchDriver->init(interface);
163  }
164  else
165  {
166  //The interface is not properly configured
167  error = ERROR_FAILURE;
168  }
169 
170  //Any error to report?
171  if(error)
172  {
173  return error;
174  }
175 
176  //Use default MAC configuration
177  ETH1->MACCR = ETH_MACCR_GPSLCE | ETH_MACCR_DO;
178 
179  //Set the maximum packet size that can be accepted
180  temp = ETH1->MACECR & ~ETH_MACECR_GPSL;
181  ETH1->MACECR = temp | STM32N6XX_ETH_RX_BUFFER_SIZE;
182 
183  //Configure MAC address filtering
185 
186  //Disable flow control
187  ETH1->MACQ0TXFCR = 0;
188  ETH1->MACRXFCR = 0;
189 
190  //Enable the first RX queue
191  ETH1->MACRXQC0R = ETH_MACRXQC0R_RXQ0EN_Val(2);
192 
193  //Configure DMA operating mode
194  ETH1->DMAMR = ETH_DMAMR_INTM_Val(0) | ETH_DMAMR_TXPR_Val(0);
195  //Configure system bus mode
196  ETH1->DMASBMR |= ETH_DMASBMR_AAL;
197 
198  //The DMA takes the descriptor table as contiguous
199  ETH1->DMA_CH[0].DMACCR = ETH_DMACxCR_DSL_Val(0);
200  //Configure TX features
201  ETH1->DMA_CH[0].DMACTXCR = ETH_DMACxTXCR_TXPBL_Val(32);
202 
203  //Configure RX features
204  ETH1->DMA_CH[0].DMACRXCR = ETH_DMACxRXCR_RXPBL_Val(32) |
206 
207  //Enable store and forward mode for transmission
208  ETH1->MTL_QUEUE[0].MTLTXQOMR = ETH_MTLTXQxOMR_TQS_Val(7) | ETH_MTLTXQxOMR_TXQEN_Val(2) |
209  ETH_MTLTXQxOMR_TSF;
210 
211  //Enable store and forward mode for reception
212  ETH1->MTL_QUEUE[0].MTLRXQOMR = ETH_MTLRXQxOMR_RQS_Val(7) | ETH_MTLRXQxOMR_RSF;
213 
214  //Initialize DMA descriptor lists
215  stm32n6xxEthInitDmaDesc(interface);
216 
217  //Prevent interrupts from being generated when the transmit statistic
218  //counters reach half their maximum value
219  ETH1->MMCTIMR = ETH_MMCTIMR_TXLPITRCIM | ETH_MMCTIMR_TXLPIUSCIM |
220  ETH_MMCTIMR_TXGPKTIM | ETH_MMCTIMR_TXMCOLGPIM | ETH_MMCTIMR_TXSCOLGPIM;
221 
222  //Prevent interrupts from being generated when the receive statistic
223  //counters reach half their maximum value
224  ETH1->MMCRIMR = ETH_MMCRIMR_RXLPITRCIM | ETH_MMCRIMR_RXLPIUSCIM |
225  ETH_MMCRIMR_RXUCGPIM | ETH_MMCRIMR_RXALGNERPIM | ETH_MMCRIMR_RXCRCERPIM;
226 
227  //Disable MAC interrupts
228  ETH1->MACIER = 0;
229  //Enable the desired DMA interrupts
230  ETH1->DMA_CH[0].DMACIER = ETH_DMACxIER_NIE | ETH_DMACxIER_RIE | ETH_DMACxIER_TIE;
231 
232  //Set priority grouping (4 bits for pre-emption priority, no bits for subpriority)
233  NVIC_SetPriorityGrouping(STM32N6XX_ETH_IRQ_PRIORITY_GROUPING);
234 
235  //Configure Ethernet interrupt priority
236  NVIC_SetPriority(ETH1_IRQn, NVIC_EncodePriority(STM32N6XX_ETH_IRQ_PRIORITY_GROUPING,
238 
239  //Enable MAC transmission and reception
240  ETH1->MACCR |= ETH_MACCR_TE | ETH_MACCR_RE;
241 
242  //Enable DMA transmission and reception
243  ETH1->DMA_CH[0].DMACTXCR |= ETH_DMACxTXCR_ST;
244  ETH1->DMA_CH[0].DMACRXCR |= ETH_DMACxRXCR_SR;
245 
246  //Accept any packets from the upper layer
247  osSetEvent(&interface->nicTxEvent);
248 
249  //Successful initialization
250  return NO_ERROR;
251 }
252 
253 
254 /**
255  * @brief GPIO configuration
256  * @param[in] interface Underlying network interface
257  **/
258 
259 __weak_func void stm32n6xxEthInitGpio(NetInterface *interface)
260 {
261 //Nucleo-N657X0-Q evaluation board?
262 #if defined(USE_STM32N6xx_NUCLEO)
263  GPIO_InitTypeDef GPIO_InitStructure;
264 
265  //Enable GPIO clocks
266  __HAL_RCC_GPIOF_CLK_ENABLE();
267  __HAL_RCC_GPIOG_CLK_ENABLE();
268 
269  //Select RMII interface mode
270  SET_BIT(RCC->CCIPR2, RCC_ETH1PHYIF_RMII);
271 
272  //Configure RMII pins
273  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
274  GPIO_InitStructure.Pull = GPIO_NOPULL;
275  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
276  GPIO_InitStructure.Alternate = GPIO_AF11_ETH1;
277 
278  //Configure ETH1_MDIO (PF4), ETH1_RMII_REF_CLK (PF7),
279  //ETH1_RMII_CRS_DV (PF10), ETH1_RMII_TX_EN (PF11), ETH1_RMII_TXD0 (PF12),
280  //ETH1_RMII_TXD1 (PF13), ETH1_RMII_RXD0 (PF14) and ETH1_RMII_RXD1 (PF15)
281  GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_7 | GPIO_PIN_10 |
282  GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
283  HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
284 
285  //Configure ETH1_MDC (PG11),
286  GPIO_InitStructure.Pin = GPIO_PIN_11;
287  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
288 
289 //STM32N6570-DK evaluation board?
290 #elif defined(USE_STM32N6570_DK)
291  GPIO_InitTypeDef GPIO_InitStructure;
292 
293  //Enable GPIO clocks
294  __HAL_RCC_GPIOD_CLK_ENABLE();
295  __HAL_RCC_GPIOF_CLK_ENABLE();
296  __HAL_RCC_GPIOG_CLK_ENABLE();
297 
298  //Select RGMII interface mode
299  SET_BIT(RCC->CCIPR2, RCC_ETH1PHYIF_RGMII);
300 
301  //Configure RGMII pins
302  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
303  GPIO_InitStructure.Pull = GPIO_NOPULL;
304  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
305 
306  //Configure ETH1_MDC (PD1) and ETH1_MDIO (PD12)
307  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_12;
308  GPIO_InitStructure.Alternate = GPIO_AF11_ETH1;
309  HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
310 
311  //Configure ETH1_RGMII_GTX_CLK (PF0)
312  GPIO_InitStructure.Pin = GPIO_PIN_0;
313  GPIO_InitStructure.Alternate = GPIO_AF12_ETH1;
314  HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
315 
316  //Configure ETH1_RGMII_CLK125 (PF2), ETH1_RGMII_RX_CLK (PF7),
317  //ETH1_RGMII_RXD2 (PF8), ETH1_RGMII_RXD3 (PF9), ETH1_RGMII_RX_CTL (PF10),
318  //ETH1_RGMII_TX_CTL (PF11), ETH1_RGMII_TXD0 (PF12), ETH1_RMII_TXD1 (PF13),
319  //ETH1_RGMII_RXD0 (PF14) and ETH1_RGMII_RXD1 (PF15)
320  GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 |
321  GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 |
322  GPIO_PIN_15;
323  GPIO_InitStructure.Alternate = GPIO_AF11_ETH1;
324  HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
325 
326  //Configure ETH1_RGMII_TXD2 (PG3) and ETH1_RGMII_TXD3 (PG4)
327  GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_4;
328  GPIO_InitStructure.Alternate = GPIO_AF11_ETH1;
329  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
330 #endif
331 }
332 
333 
334 /**
335  * @brief Initialize DMA descriptor lists
336  * @param[in] interface Underlying network interface
337  **/
338 
340 {
341  uint_t i;
342 
343  //Initialize TX DMA descriptor list
344  for(i = 0; i < STM32N6XX_ETH_TX_BUFFER_COUNT; i++)
345  {
346  //The descriptor is initially owned by the application
347  txDmaDesc[i].tdes0 = 0;
348  txDmaDesc[i].tdes1 = 0;
349  txDmaDesc[i].tdes2 = 0;
350  txDmaDesc[i].tdes3 = 0;
351  }
352 
353  //Initialize TX descriptor index
354  txIndex = 0;
355 
356  //Initialize RX DMA descriptor list
357  for(i = 0; i < STM32N6XX_ETH_RX_BUFFER_COUNT; i++)
358  {
359  //The descriptor is initially owned by the DMA
360  rxDmaDesc[i].rdes0 = (uint32_t) rxBuffer[i];
361  rxDmaDesc[i].rdes1 = 0;
362  rxDmaDesc[i].rdes2 = 0;
364  }
365 
366  //Initialize RX descriptor index
367  rxIndex = 0;
368 
369  //Start location of the TX descriptor list
370  ETH1->DMA_CH[0].DMACTXDLAR = (uint32_t) &txDmaDesc[0];
371  //Length of the transmit descriptor ring
372  ETH1->DMA_CH[0].DMACTXRLR = STM32N6XX_ETH_TX_BUFFER_COUNT - 1;
373 
374  //Start location of the RX descriptor list
375  ETH1->DMA_CH[0].DMACRXDLAR = (uint32_t) &rxDmaDesc[0];
376  //Length of the receive descriptor ring
377  ETH1->DMA_CH[0].DMACRXRLR = STM32N6XX_ETH_RX_BUFFER_COUNT - 1;
378 }
379 
380 
381 /**
382  * @brief STM32N6 Ethernet MAC timer handler
383  *
384  * This routine is periodically called by the TCP/IP stack to handle periodic
385  * operations such as polling the link state
386  *
387  * @param[in] interface Underlying network interface
388  **/
389 
391 {
392  //Valid Ethernet PHY or switch driver?
393  if(interface->phyDriver != NULL)
394  {
395  //Handle periodic operations
396  interface->phyDriver->tick(interface);
397  }
398  else if(interface->switchDriver != NULL)
399  {
400  //Handle periodic operations
401  interface->switchDriver->tick(interface);
402  }
403  else
404  {
405  //Just for sanity
406  }
407 }
408 
409 
410 /**
411  * @brief Enable interrupts
412  * @param[in] interface Underlying network interface
413  **/
414 
416 {
417  //Enable Ethernet MAC interrupts
418  NVIC_EnableIRQ(ETH1_IRQn);
419 
420  //Valid Ethernet PHY or switch driver?
421  if(interface->phyDriver != NULL)
422  {
423  //Enable Ethernet PHY interrupts
424  interface->phyDriver->enableIrq(interface);
425  }
426  else if(interface->switchDriver != NULL)
427  {
428  //Enable Ethernet switch interrupts
429  interface->switchDriver->enableIrq(interface);
430  }
431  else
432  {
433  //Just for sanity
434  }
435 }
436 
437 
438 /**
439  * @brief Disable interrupts
440  * @param[in] interface Underlying network interface
441  **/
442 
444 {
445  //Disable Ethernet MAC interrupts
446  NVIC_DisableIRQ(ETH1_IRQn);
447 
448  //Valid Ethernet PHY or switch driver?
449  if(interface->phyDriver != NULL)
450  {
451  //Disable Ethernet PHY interrupts
452  interface->phyDriver->disableIrq(interface);
453  }
454  else if(interface->switchDriver != NULL)
455  {
456  //Disable Ethernet switch interrupts
457  interface->switchDriver->disableIrq(interface);
458  }
459  else
460  {
461  //Just for sanity
462  }
463 }
464 
465 
466 /**
467  * @brief STM32N6 Ethernet MAC interrupt service routine
468  **/
469 
470 void ETH1_IRQHandler(void)
471 {
472  bool_t flag;
473  uint32_t status;
474 
475  //Interrupt service routine prologue
476  osEnterIsr();
477 
478  //This flag will be set if a higher priority task must be woken
479  flag = FALSE;
480 
481  //Read DMA status register
482  status = ETH1->DMA_CH[0].DMACSR;
483 
484  //Packet transmitted?
485  if((status & ETH_DMACxSR_TI) != 0)
486  {
487  //Clear TI interrupt flag
488  ETH1->DMA_CH[0].DMACSR = ETH_DMACxSR_TI;
489 
490  //Check whether the TX buffer is available for writing
491  if((txDmaDesc[txIndex].tdes3 & ETH_TDES3_OWN) == 0)
492  {
493  //Notify the TCP/IP stack that the transmitter is ready to send
494  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
495  }
496  }
497 
498  //Packet received?
499  if((status & ETH_DMACxSR_RI) != 0)
500  {
501  //Clear RI interrupt flag
502  ETH1->DMA_CH[0].DMACSR = ETH_DMACxSR_RI;
503 
504  //Set event flag
505  nicDriverInterface->nicEvent = TRUE;
506  //Notify the TCP/IP stack of the event
507  flag |= osSetEventFromIsr(&netEvent);
508  }
509 
510  //Clear NIS interrupt flag
511  ETH1->DMA_CH[0].DMACSR = ETH_DMACxSR_NIS;
512 
513  //Interrupt service routine epilogue
514  osExitIsr(flag);
515 }
516 
517 
518 /**
519  * @brief STM32N6 Ethernet MAC event handler
520  * @param[in] interface Underlying network interface
521  **/
522 
524 {
525  error_t error;
526 
527  //Process all pending packets
528  do
529  {
530  //Read incoming packet
531  error = stm32n6xxEthReceivePacket(interface);
532 
533  //No more data in the receive buffer?
534  } while(error != ERROR_BUFFER_EMPTY);
535 }
536 
537 
538 /**
539  * @brief Send a packet
540  * @param[in] interface Underlying network interface
541  * @param[in] buffer Multi-part buffer containing the data to send
542  * @param[in] offset Offset to the first data byte
543  * @param[in] ancillary Additional options passed to the stack along with
544  * the packet
545  * @return Error code
546  **/
547 
549  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
550 {
551  size_t length;
552 
553  //Retrieve the length of the packet
554  length = netBufferGetLength(buffer) - offset;
555 
556  //Check the frame length
558  {
559  //The transmitter can accept another packet
560  osSetEvent(&interface->nicTxEvent);
561  //Report an error
562  return ERROR_INVALID_LENGTH;
563  }
564 
565  //Make sure the current buffer is available for writing
566  if((txDmaDesc[txIndex].tdes3 & ETH_TDES3_OWN) != 0)
567  {
568  return ERROR_FAILURE;
569  }
570 
571  //Copy user data to the transmit buffer
572  netBufferRead(txBuffer[txIndex], buffer, offset, length);
573 
574  //Set the start address of the buffer
575  txDmaDesc[txIndex].tdes0 = (uint32_t) txBuffer[txIndex];
576  //Write the number of bytes to send
577  txDmaDesc[txIndex].tdes2 = ETH_TDES2_IOC | (length & ETH_TDES2_B1L);
578  //Give the ownership of the descriptor to the DMA
579  txDmaDesc[txIndex].tdes3 = ETH_TDES3_OWN | ETH_TDES3_FD | ETH_TDES3_LD;
580 
581  //Data synchronization barrier
582  __DSB();
583 
584  //Clear TBU flag to resume processing
585  ETH1->DMA_CH[0].DMACSR = ETH_DMACxSR_TBU;
586  //Instruct the DMA to poll the transmit descriptor list
587  ETH1->DMA_CH[0].DMACTXDTPR = 0;
588 
589  //Increment index and wrap around if necessary
590  if(++txIndex >= STM32N6XX_ETH_TX_BUFFER_COUNT)
591  {
592  txIndex = 0;
593  }
594 
595  //Check whether the next buffer is available for writing
596  if((txDmaDesc[txIndex].tdes3 & ETH_TDES3_OWN) == 0)
597  {
598  //The transmitter can accept another packet
599  osSetEvent(&interface->nicTxEvent);
600  }
601 
602  //Data successfully written
603  return NO_ERROR;
604 }
605 
606 
607 /**
608  * @brief Receive a packet
609  * @param[in] interface Underlying network interface
610  * @return Error code
611  **/
612 
614 {
615  error_t error;
616  size_t n;
617  NetRxAncillary ancillary;
618 
619  //Current buffer available for reading?
620  if((rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_OWN) == 0)
621  {
622  //FD and LD flags should be set
623  if((rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_FD) != 0 &&
624  (rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_LD) != 0)
625  {
626  //Make sure no error occurred
627  if((rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_ES) == 0)
628  {
629  //Retrieve the length of the frame
630  n = rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_PL;
631  //Limit the number of data to read
633 
634  //Additional options can be passed to the stack along with the packet
635  ancillary = NET_DEFAULT_RX_ANCILLARY;
636 
637  //Pass the packet to the upper layer
638  nicProcessPacket(interface, rxBuffer[rxIndex], n, &ancillary);
639 
640  //Valid packet received
641  error = NO_ERROR;
642  }
643  else
644  {
645  //The received packet contains an error
646  error = ERROR_INVALID_PACKET;
647  }
648  }
649  else
650  {
651  //The packet is not valid
652  error = ERROR_INVALID_PACKET;
653  }
654 
655  //Set the start address of the buffer
656  rxDmaDesc[rxIndex].rdes0 = (uint32_t) rxBuffer[rxIndex];
657  //Give the ownership of the descriptor back to the DMA
659 
660  //Increment index and wrap around if necessary
661  if(++rxIndex >= STM32N6XX_ETH_RX_BUFFER_COUNT)
662  {
663  rxIndex = 0;
664  }
665  }
666  else
667  {
668  //No more data in the receive buffer
669  error = ERROR_BUFFER_EMPTY;
670  }
671 
672  //Clear RBU flag to resume processing
673  ETH1->DMA_CH[0].DMACSR = ETH_DMACxSR_RBU;
674  //Instruct the DMA to poll the receive descriptor list
675  ETH1->DMA_CH[0].DMACRXDTPR = 0;
676 
677  //Return status code
678  return error;
679 }
680 
681 
682 /**
683  * @brief Configure MAC address filtering
684  * @param[in] interface Underlying network interface
685  * @return Error code
686  **/
687 
689 {
690  uint_t i;
691  uint_t j;
692  uint_t k;
693  uint32_t crc;
694  uint32_t hashTable[2];
695  MacAddr unicastMacAddr[3];
696  MacFilterEntry *entry;
697 
698  //Debug message
699  TRACE_DEBUG("Updating MAC filter...\r\n");
700 
701  //Promiscuous mode?
702  if(interface->promiscuous)
703  {
704  //Pass all incoming frames regardless of their destination address
705  ETH1->MACPFR = ETH_MACPFR_PR;
706  }
707  else
708  {
709  //Set the MAC address of the station
710  ETH1->MACA0LR = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
711  ETH1->MACA0HR = interface->macAddr.w[2];
712 
713  //The MAC supports 3 additional addresses for unicast perfect filtering
714  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
715  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
716  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
717 
718  //The hash table is used for multicast address filtering
719  hashTable[0] = 0;
720  hashTable[1] = 0;
721 
722  //The MAC address filter contains the list of MAC addresses to accept
723  //when receiving an Ethernet frame
724  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
725  {
726  //Point to the current entry
727  entry = &interface->macAddrFilter[i];
728 
729  //Valid entry?
730  if(entry->refCount > 0)
731  {
732  //Multicast address?
733  if(macIsMulticastAddr(&entry->addr))
734  {
735  //Compute CRC over the current MAC address
736  crc = stm32n6xxEthCalcCrc(&entry->addr, sizeof(MacAddr));
737 
738  //The upper 6 bits in the CRC register are used to index the
739  //contents of the hash table
740  k = (crc >> 26) & 0x3F;
741 
742  //Update hash table contents
743  hashTable[k / 32] |= (1 << (k % 32));
744  }
745  else
746  {
747  //Up to 3 additional MAC addresses can be specified
748  if(j < 3)
749  {
750  //Save the unicast address
751  unicastMacAddr[j++] = entry->addr;
752  }
753  }
754  }
755  }
756 
757  //Configure the first unicast address filter
758  if(j >= 1)
759  {
760  //When the AE bit is set, the entry is used for perfect filtering
761  ETH1->MACA1LR = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
762  ETH1->MACA1HR = unicastMacAddr[0].w[2] | ETH_MACAxHR_AE;
763  }
764  else
765  {
766  //When the AE bit is cleared, the entry is ignored
767  ETH1->MACA1LR = 0;
768  ETH1->MACA1HR = 0;
769  }
770 
771  //Configure the second unicast address filter
772  if(j >= 2)
773  {
774  //When the AE bit is set, the entry is used for perfect filtering
775  ETH1->MACA2LR = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
776  ETH1->MACA2HR = unicastMacAddr[1].w[2] | ETH_MACAxHR_AE;
777  }
778  else
779  {
780  //When the AE bit is cleared, the entry is ignored
781  ETH1->MACA2LR = 0;
782  ETH1->MACA2HR = 0;
783  }
784 
785  //Configure the third unicast address filter
786  if(j >= 3)
787  {
788  //When the AE bit is set, the entry is used for perfect filtering
789  ETH1->MACA3LR = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
790  ETH1->MACA3HR = unicastMacAddr[2].w[2] | ETH_MACAxHR_AE;
791  }
792  else
793  {
794  //When the AE bit is cleared, the entry is ignored
795  ETH1->MACA3LR = 0;
796  ETH1->MACA3HR = 0;
797  }
798 
799  //Check whether frames with a multicast destination address should be
800  //accepted
801  if(interface->acceptAllMulticast)
802  {
803  //Configure the receive filter
804  ETH1->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_PM;
805  }
806  else
807  {
808  //Configure the receive filter
809  ETH1->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_HMC;
810 
811  //Configure the multicast hash table
812  ETH1->MACHT0R = hashTable[0];
813  ETH1->MACHT1R = hashTable[1];
814 
815  //Debug message
816  TRACE_DEBUG(" MACHT0R = %08" PRIX32 "\r\n", ETH1->MACHT0R);
817  TRACE_DEBUG(" MACHT1R = %08" PRIX32 "\r\n", ETH1->MACHT1R);
818  }
819  }
820 
821  //Successful processing
822  return NO_ERROR;
823 }
824 
825 
826 /**
827  * @brief Adjust MAC configuration parameters for proper operation
828  * @param[in] interface Underlying network interface
829  * @return Error code
830  **/
831 
833 {
834  uint32_t config;
835 
836  //Read current MAC configuration
837  config = ETH1->MACCR;
838 
839  //1000BASE-T operation mode?
840  if(interface->linkSpeed == NIC_LINK_SPEED_1GBPS)
841  {
842  config &= ~ETH_MACCR_PS;
843  config &= ~ETH_MACCR_FES;
844  }
845  //100BASE-TX operation mode?
846  else if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
847  {
848  config |= ETH_MACCR_PS;
849  config |= ETH_MACCR_FES;
850  }
851  //10BASE-T operation mode?
852  else
853  {
854  config |= ETH_MACCR_PS;
855  config &= ~ETH_MACCR_FES;
856  }
857 
858  //Half-duplex or full-duplex mode?
859  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
860  {
861  config |= ETH_MACCR_DM;
862  }
863  else
864  {
865  config &= ~ETH_MACCR_DM;
866  }
867 
868  //Update MAC configuration register
869  ETH1->MACCR = config;
870 
871  //Successful processing
872  return NO_ERROR;
873 }
874 
875 
876 /**
877  * @brief Write PHY register
878  * @param[in] opcode Access type (2 bits)
879  * @param[in] phyAddr PHY address (5 bits)
880  * @param[in] regAddr Register address (5 bits)
881  * @param[in] data Register value
882  **/
883 
884 void stm32n6xxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
885  uint8_t regAddr, uint16_t data)
886 {
887  uint32_t temp;
888 
889  //Valid opcode?
890  if(opcode == SMI_OPCODE_WRITE)
891  {
892  //Take care not to alter MDC clock configuration
893  temp = ETH1->MACMDIOAR & ETH_MACMDIOAR_CR;
894  //Set up a write operation
895  temp |= ETH_MACMDIOAR_GOC_Val(1) | ETH_MACMDIOAR_GB;
896  //PHY address
897  temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
898  //Register address
899  temp |= (regAddr << 16) & ETH_MACMDIOAR_RDA;
900 
901  //Data to be written in the PHY register
902  ETH1->MACMDIODR = data & ETH_MACMDIODR_GD;
903 
904  //Start a write operation
905  ETH1->MACMDIOAR = temp;
906  //Wait for the write to complete
907  while((ETH1->MACMDIOAR & ETH_MACMDIOAR_GB) != 0)
908  {
909  }
910  }
911  else
912  {
913  //The MAC peripheral only supports standard Clause 22 opcodes
914  }
915 }
916 
917 
918 /**
919  * @brief Read PHY register
920  * @param[in] opcode Access type (2 bits)
921  * @param[in] phyAddr PHY address (5 bits)
922  * @param[in] regAddr Register address (5 bits)
923  * @return Register value
924  **/
925 
926 uint16_t stm32n6xxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
927  uint8_t regAddr)
928 {
929  uint16_t data;
930  uint32_t temp;
931 
932  //Valid opcode?
933  if(opcode == SMI_OPCODE_READ)
934  {
935  //Take care not to alter MDC clock configuration
936  temp = ETH1->MACMDIOAR & ETH_MACMDIOAR_CR;
937  //Set up a read operation
938  temp |= ETH_MACMDIOAR_GOC_Val(3) | ETH_MACMDIOAR_GB;
939  //PHY address
940  temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
941  //Register address
942  temp |= (regAddr << 16) & ETH_MACMDIOAR_RDA;
943 
944  //Start a read operation
945  ETH1->MACMDIOAR = temp;
946  //Wait for the read to complete
947  while((ETH1->MACMDIOAR & ETH_MACMDIOAR_GB) != 0)
948  {
949  }
950 
951  //Get register value
952  data = ETH1->MACMDIODR & ETH_MACMDIODR_GD;
953  }
954  else
955  {
956  //The MAC peripheral only supports standard Clause 22 opcodes
957  data = 0;
958  }
959 
960  //Return the value of the PHY register
961  return data;
962 }
963 
964 
965 /**
966  * @brief CRC calculation
967  * @param[in] data Pointer to the data over which to calculate the CRC
968  * @param[in] length Number of bytes to process
969  * @return Resulting CRC value
970  **/
971 
972 uint32_t stm32n6xxEthCalcCrc(const void *data, size_t length)
973 {
974  uint_t i;
975  uint_t j;
976  uint32_t crc;
977  const uint8_t *p;
978 
979  //Point to the data over which to calculate the CRC
980  p = (uint8_t *) data;
981  //CRC preset value
982  crc = 0xFFFFFFFF;
983 
984  //Loop through data
985  for(i = 0; i < length; i++)
986  {
987  //The message is processed bit by bit
988  for(j = 0; j < 8; j++)
989  {
990  //Update CRC value
991  if((((crc >> 31) ^ (p[i] >> j)) & 0x01) != 0)
992  {
993  crc = (crc << 1) ^ 0x04C11DB7;
994  }
995  else
996  {
997  crc = crc << 1;
998  }
999  }
1000  }
1001 
1002  //Return CRC value
1003  return ~crc;
1004 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
#define STM32N6XX_ETH_IRQ_GROUP_PRIORITY
#define STM32N6XX_ETH_RX_BUFFER_SIZE
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:61
error_t stm32n6xxEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define netEvent
Definition: net_legacy.h:196
Transmit descriptor.
#define ETH_MTLTXQxOMR_TXQEN_Val(n)
@ 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 ETH_MTLTXQxOMR_TQS_Val(n)
#define ETH_RDES3_LD
#define ETH_MACMDIOAR_CR_Val(n)
#define ETH_DMACxRXCR_RBSZ_Val(n)
void stm32n6xxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
uint8_t p
Definition: ndp.h:300
#define ETH_RDES3_ES
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define ETH_MTLRXQxOMR_RQS_Val(n)
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
#define ETH_TDES3_LD
void stm32n6xxEthTick(NetInterface *interface)
STM32N6 Ethernet MAC timer handler.
#define STM32N6XX_ETH_RX_BUFFER_COUNT
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
#define STM32N6XX_ETH_TX_BUFFER_SIZE
uint32_t stm32n6xxEthCalcCrc(const void *data, size_t length)
CRC calculation.
Receive descriptor.
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:392
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define osExitIsr(flag)
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define ETH_DMACxCR_DSL_Val(n)
#define FALSE
Definition: os_port.h:46
#define STM32N6XX_ETH_IRQ_PRIORITY_GROUPING
error_t
Error codes.
Definition: error.h:43
error_t stm32n6xxEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
#define ETH_TDES2_B1L
#define ETH_DMAMR_TXPR_Val(n)
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:105
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
void stm32n6xxEthEnableIrq(NetInterface *interface)
Enable interrupts.
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
@ ERROR_INVALID_PACKET
Definition: error.h:141
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:263
uint16_t stm32n6xxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
#define NetTxAncillary
Definition: net_misc.h:36
#define ETH_MACRXQC0R_RXQ0EN_Val(n)
#define ETH_RDES3_BUF1V
#define SMI_OPCODE_READ
Definition: nic.h:67
#define ETH_TDES2_IOC
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
const NicDriver stm32n6xxEthDriver
STM32N6 Ethernet MAC driver.
void stm32n6xxEthDisableIrq(NetInterface *interface)
Disable interrupts.
#define MIN(a, b)
Definition: os_port.h:63
#define ETH_RDES3_OWN
#define ETH_TDES3_OWN
#define rxBuffer
MacAddr
Definition: ethernet.h:195
#define TRACE_DEBUG(...)
Definition: debug.h:119
void stm32n6xxEthEventHandler(NetInterface *interface)
STM32N6 Ethernet MAC event handler.
error_t stm32n6xxEthInit(NetInterface *interface)
STM32N6 Ethernet MAC initialization.
uint16_t regAddr
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
#define ETH_RDES3_FD
__weak_func void stm32n6xxEthInitGpio(NetInterface *interface)
GPIO configuration.
#define osEnterIsr()
#define STM32N6XX_ETH_IRQ_SUB_PRIORITY
#define ETH_DMAMR_INTM_Val(n)
#define STM32N6XX_ETH_TX_BUFFER_COUNT
#define rxDmaDesc
void stm32n6xxEthInitDmaDesc(NetInterface *interface)
Initialize DMA descriptor lists.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define ETH_TDES3_FD
#define ETH_MACMDIOAR_GOC_Val(n)
#define txDmaDesc
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define STM32N6XX_ETH_RAM_SECTION
#define ETH_DMACxTXCR_TXPBL_Val(n)
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
#define ETH_DMACxRXCR_RXPBL_Val(n)
#define ETH_RDES3_IOC
STM32N6 Gigabit Ethernet MAC driver.
error_t stm32n6xxEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
error_t stm32n6xxEthReceivePacket(NetInterface *interface)
Receive a packet.
void ETH1_IRQHandler(void)
STM32N6 Ethernet MAC interrupt service routine.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
#define ETH_RDES3_PL
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83