stm32h5xx_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file stm32h5xx_eth_driver.c
3  * @brief STM32H5 Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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.6.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "stm32h5xx.h"
36 #include "stm32h5xx_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
50 //Receive buffer
51 #pragma data_alignment = 4
53 //Transmit DMA descriptors
54 #pragma data_alignment = 4
56 //Receive DMA descriptors
57 #pragma data_alignment = 4
59 
60 //Keil MDK-ARM or GCC compiler?
61 #else
62 
63 //Transmit buffer
65  __attribute__((aligned(4)));
66 //Receive buffer
68  __attribute__((aligned(4)));
69 //Transmit DMA descriptors
71  __attribute__((aligned(4)));
72 //Receive DMA descriptors
74  __attribute__((aligned(4)));
75 
76 #endif
77 
78 //Current transmit descriptor
79 static uint_t txIndex;
80 //Current receive descriptor
81 static uint_t rxIndex;
82 
83 
84 /**
85  * @brief STM32H5 Ethernet MAC driver
86  **/
87 
89 {
91  ETH_MTU,
102  TRUE,
103  TRUE,
104  TRUE,
105  FALSE
106 };
107 
108 
109 /**
110  * @brief STM32H5 Ethernet MAC initialization
111  * @param[in] interface Underlying network interface
112  * @return Error code
113  **/
114 
116 {
117  error_t error;
118  uint32_t temp;
119 
120  //Debug message
121  TRACE_INFO("Initializing STM32H5 Ethernet MAC...\r\n");
122 
123  //Save underlying network interface
124  nicDriverInterface = interface;
125 
126  //GPIO configuration
127  stm32h5xxEthInitGpio(interface);
128 
129  //Enable Ethernet MAC clock
130  __HAL_RCC_ETH_CLK_ENABLE();
131  __HAL_RCC_ETHTX_CLK_ENABLE();
132  __HAL_RCC_ETHRX_CLK_ENABLE();
133 
134  //Reset Ethernet MAC peripheral
135  __HAL_RCC_ETH_FORCE_RESET();
136  __HAL_RCC_ETH_RELEASE_RESET();
137 
138  //Perform a software reset
139  ETH->DMAMR |= ETH_DMAMR_SWR;
140  //Wait for the reset to complete
141  while((ETH->DMAMR & ETH_DMAMR_SWR) != 0)
142  {
143  }
144 
145  //Adjust MDC clock range depending on HCLK frequency
146  ETH->MACMDIOAR = ETH_MACMDIOAR_CR_DIV124;
147 
148  //Valid Ethernet PHY or switch driver?
149  if(interface->phyDriver != NULL)
150  {
151  //Ethernet PHY initialization
152  error = interface->phyDriver->init(interface);
153  }
154  else if(interface->switchDriver != NULL)
155  {
156  //Ethernet switch initialization
157  error = interface->switchDriver->init(interface);
158  }
159  else
160  {
161  //The interface is not properly configured
162  error = ERROR_FAILURE;
163  }
164 
165  //Any error to report?
166  if(error)
167  {
168  return error;
169  }
170 
171  //Use default MAC configuration
172  ETH->MACCR = ETH_MACCR_GPSLCE | ETH_MACCR_RESERVED15 | ETH_MACCR_DO;
173 
174  //Set the maximum packet size that can be accepted
175  temp = ETH->MACECR & ~ETH_MACECR_GPSL;
176  ETH->MACECR = temp | STM32H5XX_ETH_RX_BUFFER_SIZE;
177 
178  //Configure MAC address filtering
180 
181  //Disable flow control
182  ETH->MACTFCR = 0;
183  ETH->MACRFCR = 0;
184 
185  //Configure DMA operating mode
186  ETH->DMAMR = ETH_DMAMR_INTM_0 | ETH_DMAMR_PR_1_1;
187  //Configure system bus mode
188  ETH->DMASBMR |= ETH_DMASBMR_AAL;
189  //The DMA takes the descriptor table as contiguous
190  ETH->DMACCR = ETH_DMACCR_DSL_0BIT;
191 
192  //Configure TX features
193  ETH->DMACTCR = ETH_DMACTCR_TPBL_32PBL;
194 
195  //Configure RX features
196  ETH->DMACRCR = ETH_DMACRCR_RPBL_32PBL;
197  ETH->DMACRCR |= (STM32H5XX_ETH_RX_BUFFER_SIZE << 1) & ETH_DMACRCR_RBSZ;
198 
199  //Enable store and forward mode
200  ETH->MTLTQOMR |= ETH_MTLTQOMR_TSF;
201  ETH->MTLRQOMR |= ETH_MTLRQOMR_RSF;
202 
203  //Initialize DMA descriptor lists
204  stm32h5xxEthInitDmaDesc(interface);
205 
206  //Prevent interrupts from being generated when the transmit statistic
207  //counters reach half their maximum value
208  ETH->MMCTIMR = ETH_MMCTIMR_TXLPITRCIM | ETH_MMCTIMR_TXLPIUSCIM |
209  ETH_MMCTIMR_TXGPKTIM | ETH_MMCTIMR_TXMCOLGPIM | ETH_MMCTIMR_TXSCOLGPIM;
210 
211  //Prevent interrupts from being generated when the receive statistic
212  //counters reach half their maximum value
213  ETH->MMCRIMR = ETH_MMCRIMR_RXLPITRCIM | ETH_MMCRIMR_RXLPIUSCIM |
214  ETH_MMCRIMR_RXUCGPIM | ETH_MMCRIMR_RXALGNERPIM | ETH_MMCRIMR_RXCRCERPIM;
215 
216  //Disable MAC interrupts
217  ETH->MACIER = 0;
218  //Enable the desired DMA interrupts
219  ETH->DMACIER = ETH_DMACIER_NIE | ETH_DMACIER_RIE | ETH_DMACIER_TIE;
220 
221  //Set priority grouping (4 bits for pre-emption priority, no bits for subpriority)
222  NVIC_SetPriorityGrouping(STM32H5XX_ETH_IRQ_PRIORITY_GROUPING);
223 
224  //Configure Ethernet interrupt priority
225  NVIC_SetPriority(ETH_IRQn, NVIC_EncodePriority(STM32H5XX_ETH_IRQ_PRIORITY_GROUPING,
227 
228  //Enable MAC transmission and reception
229  ETH->MACCR |= ETH_MACCR_TE | ETH_MACCR_RE;
230 
231  //Enable DMA transmission and reception
232  ETH->DMACTCR |= ETH_DMACTCR_ST;
233  ETH->DMACRCR |= ETH_DMACRCR_SR;
234 
235  //Accept any packets from the upper layer
236  osSetEvent(&interface->nicTxEvent);
237 
238  //Successful initialization
239  return NO_ERROR;
240 }
241 
242 
243 /**
244  * @brief GPIO configuration
245  * @param[in] interface Underlying network interface
246  **/
247 
248 __weak_func void stm32h5xxEthInitGpio(NetInterface *interface)
249 {
250 //Nucleo-H563ZI evaluation board?
251 #if defined(USE_NUCLEO_H563ZI)
252  GPIO_InitTypeDef GPIO_InitStructure;
253 
254  //Enable SBS clock
255  __HAL_RCC_SBS_CLK_ENABLE();
256 
257  //Enable GPIO clocks
258  __HAL_RCC_GPIOA_CLK_ENABLE();
259  __HAL_RCC_GPIOB_CLK_ENABLE();
260  __HAL_RCC_GPIOC_CLK_ENABLE();
261  __HAL_RCC_GPIOG_CLK_ENABLE();
262 
263  //Select RMII interface mode
264  HAL_SBS_ETHInterfaceSelect(SBS_ETH_RMII);
265 
266  //Configure RMII pins
267  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
268  GPIO_InitStructure.Pull = GPIO_NOPULL;
269  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
270  GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
271 
272  //Configure ETH_RMII_REF_CLK (PA1), ETH_MDIO (PA2) and ETH_RMII_CRS_DV (PA7)
273  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
274  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
275 
276  //Configure ETH_RMII_TXD1 (PB15)
277  GPIO_InitStructure.Pin = GPIO_PIN_15;
278  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
279 
280  //Configure ETH_MDC (PC1), ETH_RMII_RXD0 (PC4) and ETH_RMII_RXD1 (PC5)
281  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
282  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
283 
284  //Configure RMII_TX_EN (PG11) and ETH_RMII_TXD0 (PG13)
285  GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13;
286  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
287 
288 //Nucleo-H5E5ZJ evaluation board?
289 #elif defined(USE_NUCLEO_H5E5ZJ)
290  GPIO_InitTypeDef GPIO_InitStructure;
291 
292  //Enable SBS clock
293  __HAL_RCC_SBS_CLK_ENABLE();
294 
295  //Enable GPIO clocks
296  __HAL_RCC_GPIOA_CLK_ENABLE();
297  __HAL_RCC_GPIOB_CLK_ENABLE();
298  __HAL_RCC_GPIOC_CLK_ENABLE();
299  __HAL_RCC_GPIOD_CLK_ENABLE();
300  __HAL_RCC_GPIOG_CLK_ENABLE();
301 
302  //Select RMII interface mode
303  HAL_SBS_ETHInterfaceSelect(SBS_ETH_RMII);
304 
305  //Configure RMII pins
306  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
307  GPIO_InitStructure.Pull = GPIO_NOPULL;
308  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
309  GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
310 
311  //Configure ETH_RMII_REF_CLK (PA1), ETH_MDIO (PA2) and RMII_TX_EN (PA5)
312  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_5;
313  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
314 
315  //Configure ETH_RMII_TXD0 (PB12)
316  GPIO_InitStructure.Pin = GPIO_PIN_12;
317  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
318 
319  //Configure ETH_MDC (PC1), ETH_RMII_RXD0 (PC4) and ETH_RMII_RXD1 (PC5)
320  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
321  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
322 
323  //Configure ETH_RMII_CRS_DV (PD1)
324  GPIO_InitStructure.Pin = GPIO_PIN_1;
325  HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
326 
327  //Configure ETH_RMII_TXD1 (PG12)
328  GPIO_InitStructure.Pin = GPIO_PIN_12;
329  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
330 
331 //STM32H573I-DK evaluation board?
332 #elif defined(USE_STM32H573I_DK)
333  GPIO_InitTypeDef GPIO_InitStructure;
334 
335  //Enable SBS clock
336  __HAL_RCC_SBS_CLK_ENABLE();
337 
338  //Enable GPIO clocks
339  __HAL_RCC_GPIOA_CLK_ENABLE();
340  __HAL_RCC_GPIOC_CLK_ENABLE();
341  __HAL_RCC_GPIOG_CLK_ENABLE();
342 
343  //Select RMII interface mode
344  HAL_SBS_ETHInterfaceSelect(SBS_ETH_RMII);
345 
346  //Configure RMII pins
347  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
348  GPIO_InitStructure.Pull = GPIO_NOPULL;
349  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
350  GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
351 
352  //Configure ETH_RMII_REF_CLK (PA1), ETH_MDIO (PA2) and ETH_RMII_CRS_DV (PA7)
353  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
354  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
355 
356  //Configure ETH_MDC (PC1), ETH_RMII_RXD0 (PC4) and ETH_RMII_RXD1 (PC5)
357  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
358  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
359 
360  //Configure RMII_TX_EN (PG11), ETH_RMII_TXD1 (PG12) and ETH_RMII_TXD0 (PG13)
361  GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
362  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
363 
364 //STM32H5F5J-DK evaluation board?
365 #elif defined(USE_STM32H5F5J_DK)
366  GPIO_InitTypeDef GPIO_InitStructure;
367 
368  //Enable SBS clock
369  __HAL_RCC_SBS_CLK_ENABLE();
370 
371  //Enable GPIO clocks
372  __HAL_RCC_GPIOA_CLK_ENABLE();
373  __HAL_RCC_GPIOB_CLK_ENABLE();
374  __HAL_RCC_GPIOC_CLK_ENABLE();
375  __HAL_RCC_GPIOD_CLK_ENABLE();
376  __HAL_RCC_GPIOE_CLK_ENABLE();
377  __HAL_RCC_GPIOG_CLK_ENABLE();
378  __HAL_RCC_GPIOJ_CLK_ENABLE();
379 
380  //Select RMII interface mode
381  HAL_SBS_ETHInterfaceSelect(SBS_ETH_RMII);
382 
383  //Configure RMII pins
384  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
385  GPIO_InitStructure.Pull = GPIO_NOPULL;
386  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
387  GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
388 
389  //Configure ETH_RMII_REF_CLK (PA1),
390  GPIO_InitStructure.Pin = GPIO_PIN_1;
391  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
392 
393  //Configure RMII_TX_EN (PB11) and ETH_RMII_TXD0 (PB12)
394  GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_12;
395  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
396 
397  //Configure ETH_RMII_RXD0 (PC4)
398  GPIO_InitStructure.Pin = GPIO_PIN_4;
399  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
400 
401  //Configure ETH_RMII_CRS_DV (PD1)
402  GPIO_InitStructure.Pin = GPIO_PIN_1;
403  HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
404 
405  //Configure ETH_RMII_RXD1 (PE5)
406  GPIO_InitStructure.Pin = GPIO_PIN_5;
407  HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
408 
409  //Configure ETH_RMII_TXD1 (PG12)
410  GPIO_InitStructure.Pin = GPIO_PIN_12;
411  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
412 
413  //Configure ETH_MDIO (PJ6) and ETH_MDC (PJ7)
414  GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
415  HAL_GPIO_Init(GPIOJ, &GPIO_InitStructure);
416 #endif
417 }
418 
419 
420 /**
421  * @brief Initialize DMA descriptor lists
422  * @param[in] interface Underlying network interface
423  **/
424 
426 {
427  uint_t i;
428 
429  //Initialize TX DMA descriptor list
430  for(i = 0; i < STM32H5XX_ETH_TX_BUFFER_COUNT; i++)
431  {
432  //The descriptor is initially owned by the application
433  txDmaDesc[i].tdes0 = 0;
434  txDmaDesc[i].tdes1 = 0;
435  txDmaDesc[i].tdes2 = 0;
436  txDmaDesc[i].tdes3 = 0;
437  }
438 
439  //Initialize TX descriptor index
440  txIndex = 0;
441 
442  //Initialize RX DMA descriptor list
443  for(i = 0; i < STM32H5XX_ETH_RX_BUFFER_COUNT; i++)
444  {
445  //The descriptor is initially owned by the DMA
446  rxDmaDesc[i].rdes0 = (uint32_t) rxBuffer[i];
447  rxDmaDesc[i].rdes1 = 0;
448  rxDmaDesc[i].rdes2 = 0;
450  }
451 
452  //Initialize RX descriptor index
453  rxIndex = 0;
454 
455  //Start location of the TX descriptor list
456  ETH->DMACTDLAR = (uint32_t) &txDmaDesc[0];
457  //Length of the transmit descriptor ring
458  ETH->DMACTDRLR = STM32H5XX_ETH_TX_BUFFER_COUNT - 1;
459 
460  //Start location of the RX descriptor list
461  ETH->DMACRDLAR = (uint32_t) &rxDmaDesc[0];
462  //Length of the receive descriptor ring
463  ETH->DMACRDRLR = STM32H5XX_ETH_RX_BUFFER_COUNT - 1;
464 }
465 
466 
467 /**
468  * @brief STM32H5 Ethernet MAC timer handler
469  *
470  * This routine is periodically called by the TCP/IP stack to handle periodic
471  * operations such as polling the link state
472  *
473  * @param[in] interface Underlying network interface
474  **/
475 
477 {
478  //Valid Ethernet PHY or switch driver?
479  if(interface->phyDriver != NULL)
480  {
481  //Handle periodic operations
482  interface->phyDriver->tick(interface);
483  }
484  else if(interface->switchDriver != NULL)
485  {
486  //Handle periodic operations
487  interface->switchDriver->tick(interface);
488  }
489  else
490  {
491  //Just for sanity
492  }
493 }
494 
495 
496 /**
497  * @brief Enable interrupts
498  * @param[in] interface Underlying network interface
499  **/
500 
502 {
503  //Enable Ethernet MAC interrupts
504  NVIC_EnableIRQ(ETH_IRQn);
505 
506  //Valid Ethernet PHY or switch driver?
507  if(interface->phyDriver != NULL)
508  {
509  //Enable Ethernet PHY interrupts
510  interface->phyDriver->enableIrq(interface);
511  }
512  else if(interface->switchDriver != NULL)
513  {
514  //Enable Ethernet switch interrupts
515  interface->switchDriver->enableIrq(interface);
516  }
517  else
518  {
519  //Just for sanity
520  }
521 }
522 
523 
524 /**
525  * @brief Disable interrupts
526  * @param[in] interface Underlying network interface
527  **/
528 
530 {
531  //Disable Ethernet MAC interrupts
532  NVIC_DisableIRQ(ETH_IRQn);
533 
534  //Valid Ethernet PHY or switch driver?
535  if(interface->phyDriver != NULL)
536  {
537  //Disable Ethernet PHY interrupts
538  interface->phyDriver->disableIrq(interface);
539  }
540  else if(interface->switchDriver != NULL)
541  {
542  //Disable Ethernet switch interrupts
543  interface->switchDriver->disableIrq(interface);
544  }
545  else
546  {
547  //Just for sanity
548  }
549 }
550 
551 
552 /**
553  * @brief STM32H5 Ethernet MAC interrupt service routine
554  **/
555 
556 void ETH_IRQHandler(void)
557 {
558  bool_t flag;
559  uint32_t status;
560 
561  //Interrupt service routine prologue
562  osEnterIsr();
563 
564  //This flag will be set if a higher priority task must be woken
565  flag = FALSE;
566 
567  //Read DMA status register
568  status = ETH->DMACSR;
569 
570  //Packet transmitted?
571  if((status & ETH_DMACSR_TI) != 0)
572  {
573  //Clear TI interrupt flag
574  ETH->DMACSR = ETH_DMACSR_TI;
575 
576  //Check whether the TX buffer is available for writing
577  if((txDmaDesc[txIndex].tdes3 & ETH_TDES3_OWN) == 0)
578  {
579  //Notify the TCP/IP stack that the transmitter is ready to send
580  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
581  }
582  }
583 
584  //Packet received?
585  if((status & ETH_DMACSR_RI) != 0)
586  {
587  //Clear RI interrupt flag
588  ETH->DMACSR = ETH_DMACSR_RI;
589 
590  //Set event flag
591  nicDriverInterface->nicEvent = TRUE;
592  //Notify the TCP/IP stack of the event
593  flag |= osSetEventFromIsr(&nicDriverInterface->netContext->event);
594  }
595 
596  //Clear NIS interrupt flag
597  ETH->DMACSR = ETH_DMACSR_NIS;
598 
599  //Interrupt service routine epilogue
600  osExitIsr(flag);
601 }
602 
603 
604 /**
605  * @brief STM32H5 Ethernet MAC event handler
606  * @param[in] interface Underlying network interface
607  **/
608 
610 {
611  error_t error;
612 
613  //Process all pending packets
614  do
615  {
616  //Read incoming packet
617  error = stm32h5xxEthReceivePacket(interface);
618 
619  //No more data in the receive buffer?
620  } while(error != ERROR_BUFFER_EMPTY);
621 }
622 
623 
624 /**
625  * @brief Send a packet
626  * @param[in] interface Underlying network interface
627  * @param[in] buffer Multi-part buffer containing the data to send
628  * @param[in] offset Offset to the first data byte
629  * @param[in] ancillary Additional options passed to the stack along with
630  * the packet
631  * @return Error code
632  **/
633 
635  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
636 {
637  size_t length;
638 
639  //Retrieve the length of the packet
640  length = netBufferGetLength(buffer) - offset;
641 
642  //Check the frame length
644  {
645  //The transmitter can accept another packet
646  osSetEvent(&interface->nicTxEvent);
647  //Report an error
648  return ERROR_INVALID_LENGTH;
649  }
650 
651  //Make sure the current buffer is available for writing
652  if((txDmaDesc[txIndex].tdes3 & ETH_TDES3_OWN) != 0)
653  {
654  return ERROR_FAILURE;
655  }
656 
657  //Copy user data to the transmit buffer
658  netBufferRead(txBuffer[txIndex], buffer, offset, length);
659 
660  //Set the start address of the buffer
661  txDmaDesc[txIndex].tdes0 = (uint32_t) txBuffer[txIndex];
662  //Write the number of bytes to send
663  txDmaDesc[txIndex].tdes2 = ETH_TDES2_IOC | (length & ETH_TDES2_B1L);
664  //Give the ownership of the descriptor to the DMA
665  txDmaDesc[txIndex].tdes3 = ETH_TDES3_OWN | ETH_TDES3_FD | ETH_TDES3_LD;
666 
667  //Data synchronization barrier
668  __DSB();
669 
670  //Clear TBU flag to resume processing
671  ETH->DMACSR = ETH_DMACSR_TBU;
672  //Instruct the DMA to poll the transmit descriptor list
673  ETH->DMACTDTPR = 0;
674 
675  //Increment index and wrap around if necessary
676  if(++txIndex >= STM32H5XX_ETH_TX_BUFFER_COUNT)
677  {
678  txIndex = 0;
679  }
680 
681  //Check whether the next buffer is available for writing
682  if((txDmaDesc[txIndex].tdes3 & ETH_TDES3_OWN) == 0)
683  {
684  //The transmitter can accept another packet
685  osSetEvent(&interface->nicTxEvent);
686  }
687 
688  //Data successfully written
689  return NO_ERROR;
690 }
691 
692 
693 /**
694  * @brief Receive a packet
695  * @param[in] interface Underlying network interface
696  * @return Error code
697  **/
698 
700 {
701  error_t error;
702  size_t n;
703  uint32_t status;
704  NetRxAncillary ancillary;
705 
706  //Current buffer available for reading?
707  if((rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_OWN) == 0)
708  {
709  //FD and LD flags should be set
710  if((rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_FD) != 0 &&
711  (rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_LD) != 0)
712  {
713  //Check error bits
714  status = rxDmaDesc[rxIndex].rdes3 & (ETH_RDES3_CE | ETH_RDES3_GP |
716 
717  //The dribble bit error is valid only in the MII mode
718  if((SBS->PMCR & SBS_PMCR_ETH_SEL_PHY) != SBS_ETH_MII)
719  {
720  status &= ~ETH_RDES3_DE;
721  }
722 
723  //Make sure no error occurred
724  if(status == 0)
725  {
726  //Retrieve the length of the frame
727  n = rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_PL;
728  //Limit the number of data to read
730 
731  //Additional options can be passed to the stack along with the packet
732  ancillary = NET_DEFAULT_RX_ANCILLARY;
733 
734  //Pass the packet to the upper layer
735  nicProcessPacket(interface, rxBuffer[rxIndex], n, &ancillary);
736 
737  //Valid packet received
738  error = NO_ERROR;
739  }
740  else
741  {
742  //The received packet contains an error
743  error = ERROR_INVALID_PACKET;
744  }
745  }
746  else
747  {
748  //The packet is not valid
749  error = ERROR_INVALID_PACKET;
750  }
751 
752  //Set the start address of the buffer
753  rxDmaDesc[rxIndex].rdes0 = (uint32_t) rxBuffer[rxIndex];
754  //Give the ownership of the descriptor back to the DMA
756 
757  //Increment index and wrap around if necessary
758  if(++rxIndex >= STM32H5XX_ETH_RX_BUFFER_COUNT)
759  {
760  rxIndex = 0;
761  }
762  }
763  else
764  {
765  //No more data in the receive buffer
766  error = ERROR_BUFFER_EMPTY;
767  }
768 
769  //Clear RBU flag to resume processing
770  ETH->DMACSR = ETH_DMACSR_RBU;
771  //Instruct the DMA to poll the receive descriptor list
772  ETH->DMACRDTPR = 0;
773 
774  //Return status code
775  return error;
776 }
777 
778 
779 /**
780  * @brief Configure MAC address filtering
781  * @param[in] interface Underlying network interface
782  * @return Error code
783  **/
784 
786 {
787  uint_t i;
788  uint_t j;
789  uint_t k;
790  uint32_t crc;
791  uint32_t hashTable[2];
792  MacAddr unicastMacAddr[3];
793  MacFilterEntry *entry;
794 
795  //Debug message
796  TRACE_DEBUG("Updating MAC filter...\r\n");
797 
798  //Promiscuous mode?
799  if(interface->promiscuous)
800  {
801  //Pass all incoming frames regardless of their destination address
802  ETH->MACPFR = ETH_MACPFR_PR;
803  }
804  else
805  {
806  //Set the MAC address of the station
807  ETH->MACA0LR = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
808  ETH->MACA0HR = interface->macAddr.w[2];
809 
810  //The MAC supports 3 additional addresses for unicast perfect filtering
811  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
812  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
813  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
814 
815  //The hash table is used for multicast address filtering
816  hashTable[0] = 0;
817  hashTable[1] = 0;
818 
819  //The MAC address filter contains the list of MAC addresses to accept
820  //when receiving an Ethernet frame
821  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
822  {
823  //Point to the current entry
824  entry = &interface->macAddrFilter[i];
825 
826  //Valid entry?
827  if(entry->refCount > 0)
828  {
829  //Multicast address?
830  if(macIsMulticastAddr(&entry->addr))
831  {
832  //Compute CRC over the current MAC address
833  crc = stm32h5xxEthCalcCrc(&entry->addr, sizeof(MacAddr));
834 
835  //The upper 6 bits in the CRC register are used to index the
836  //contents of the hash table
837  k = (crc >> 26) & 0x3F;
838 
839  //Update hash table contents
840  hashTable[k / 32] |= (1 << (k % 32));
841  }
842  else
843  {
844  //Up to 3 additional MAC addresses can be specified
845  if(j < 3)
846  {
847  //Save the unicast address
848  unicastMacAddr[j++] = entry->addr;
849  }
850  }
851  }
852  }
853 
854  //Configure the first unicast address filter
855  if(j >= 1)
856  {
857  //When the AE bit is set, the entry is used for perfect filtering
858  ETH->MACA1LR = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
859  ETH->MACA1HR = unicastMacAddr[0].w[2] | ETH_MACAHR_AE;
860  }
861  else
862  {
863  //When the AE bit is cleared, the entry is ignored
864  ETH->MACA1LR = 0;
865  ETH->MACA1HR = 0;
866  }
867 
868  //Configure the second unicast address filter
869  if(j >= 2)
870  {
871  //When the AE bit is set, the entry is used for perfect filtering
872  ETH->MACA2LR = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
873  ETH->MACA2HR = unicastMacAddr[1].w[2] | ETH_MACAHR_AE;
874  }
875  else
876  {
877  //When the AE bit is cleared, the entry is ignored
878  ETH->MACA2LR = 0;
879  ETH->MACA2HR = 0;
880  }
881 
882  //Configure the third unicast address filter
883  if(j >= 3)
884  {
885  //When the AE bit is set, the entry is used for perfect filtering
886  ETH->MACA3LR = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
887  ETH->MACA3HR = unicastMacAddr[2].w[2] | ETH_MACAHR_AE;
888  }
889  else
890  {
891  //When the AE bit is cleared, the entry is ignored
892  ETH->MACA3LR = 0;
893  ETH->MACA3HR = 0;
894  }
895 
896  //Check whether frames with a multicast destination address should be
897  //accepted
898  if(interface->acceptAllMulticast)
899  {
900  //Configure the receive filter
901  ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_PM;
902  }
903  else
904  {
905  //Configure the receive filter
906  ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_HMC;
907 
908  //Configure the multicast hash table
909  ETH->MACHT0R = hashTable[0];
910  ETH->MACHT1R = hashTable[1];
911 
912  //Debug message
913  TRACE_DEBUG(" MACHT0R = 0x%08" PRIX32 "\r\n", ETH->MACHT0R);
914  TRACE_DEBUG(" MACHT1R = 0x%08" PRIX32 "\r\n", ETH->MACHT1R);
915  }
916  }
917 
918  //Successful processing
919  return NO_ERROR;
920 }
921 
922 
923 /**
924  * @brief Adjust MAC configuration parameters for proper operation
925  * @param[in] interface Underlying network interface
926  * @return Error code
927  **/
928 
930 {
931  uint32_t config;
932 
933  //Read current MAC configuration
934  config = ETH->MACCR;
935 
936  //10BASE-T or 100BASE-TX operation mode?
937  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
938  {
939  config |= ETH_MACCR_FES;
940  }
941  else
942  {
943  config &= ~ETH_MACCR_FES;
944  }
945 
946  //Half-duplex or full-duplex mode?
947  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
948  {
949  config |= ETH_MACCR_DM;
950  }
951  else
952  {
953  config &= ~ETH_MACCR_DM;
954  }
955 
956  //Update MAC configuration register
957  ETH->MACCR = config;
958 
959  //Successful processing
960  return NO_ERROR;
961 }
962 
963 
964 /**
965  * @brief Write PHY register
966  * @param[in] opcode Access type (2 bits)
967  * @param[in] phyAddr PHY address (5 bits)
968  * @param[in] regAddr Register address (5 bits)
969  * @param[in] data Register value
970  **/
971 
972 void stm32h5xxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
973  uint8_t regAddr, uint16_t data)
974 {
975  uint32_t temp;
976 
977  //Valid opcode?
978  if(opcode == SMI_OPCODE_WRITE)
979  {
980  //Take care not to alter MDC clock configuration
981  temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
982  //Set up a write operation
983  temp |= ETH_MACMDIOAR_MOC_WR | ETH_MACMDIOAR_MB;
984  //PHY address
985  temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
986  //Register address
987  temp |= (regAddr << 16) & ETH_MACMDIOAR_RDA;
988 
989  //Data to be written in the PHY register
990  ETH->MACMDIODR = data & ETH_MACMDIODR_MD;
991 
992  //Start a write operation
993  ETH->MACMDIOAR = temp;
994  //Wait for the write to complete
995  while((ETH->MACMDIOAR & ETH_MACMDIOAR_MB) != 0)
996  {
997  }
998  }
999  else
1000  {
1001  //The MAC peripheral only supports standard Clause 22 opcodes
1002  }
1003 }
1004 
1005 
1006 /**
1007  * @brief Read PHY register
1008  * @param[in] opcode Access type (2 bits)
1009  * @param[in] phyAddr PHY address (5 bits)
1010  * @param[in] regAddr Register address (5 bits)
1011  * @return Register value
1012  **/
1013 
1014 uint16_t stm32h5xxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
1015  uint8_t regAddr)
1016 {
1017  uint16_t data;
1018  uint32_t temp;
1019 
1020  //Valid opcode?
1021  if(opcode == SMI_OPCODE_READ)
1022  {
1023  //Take care not to alter MDC clock configuration
1024  temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
1025  //Set up a read operation
1026  temp |= ETH_MACMDIOAR_MOC_RD | ETH_MACMDIOAR_MB;
1027  //PHY address
1028  temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
1029  //Register address
1030  temp |= (regAddr << 16) & ETH_MACMDIOAR_RDA;
1031 
1032  //Start a read operation
1033  ETH->MACMDIOAR = temp;
1034  //Wait for the read to complete
1035  while((ETH->MACMDIOAR & ETH_MACMDIOAR_MB) != 0)
1036  {
1037  }
1038 
1039  //Get register value
1040  data = ETH->MACMDIODR & ETH_MACMDIODR_MD;
1041  }
1042  else
1043  {
1044  //The MAC peripheral only supports standard Clause 22 opcodes
1045  data = 0;
1046  }
1047 
1048  //Return the value of the PHY register
1049  return data;
1050 }
1051 
1052 
1053 /**
1054  * @brief CRC calculation
1055  * @param[in] data Pointer to the data over which to calculate the CRC
1056  * @param[in] length Number of bytes to process
1057  * @return Resulting CRC value
1058  **/
1059 
1060 uint32_t stm32h5xxEthCalcCrc(const void *data, size_t length)
1061 {
1062  uint_t i;
1063  uint_t j;
1064  uint32_t crc;
1065  const uint8_t *p;
1066 
1067  //Point to the data over which to calculate the CRC
1068  p = (uint8_t *) data;
1069  //CRC preset value
1070  crc = 0xFFFFFFFF;
1071 
1072  //Loop through data
1073  for(i = 0; i < length; i++)
1074  {
1075  //The message is processed bit by bit
1076  for(j = 0; j < 8; j++)
1077  {
1078  //Update CRC value
1079  if((((crc >> 31) ^ (p[i] >> j)) & 0x01) != 0)
1080  {
1081  crc = (crc << 1) ^ 0x04C11DB7;
1082  }
1083  else
1084  {
1085  crc = crc << 1;
1086  }
1087  }
1088  }
1089 
1090  //Return CRC value
1091  return ~crc;
1092 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
STM32H5 Ethernet MAC driver.
uint8_t opcode
Definition: dns_common.h:191
int bool_t
Definition: compiler_port.h:63
#define ETH_RDES3_DE
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define STM32H5XX_ETH_IRQ_SUB_PRIORITY
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
uint16_t stm32h5xxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define ETH_RDES3_LD
#define STM32H5XX_ETH_TX_BUFFER_COUNT
uint8_t p
Definition: ndp.h:300
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
void stm32h5xxEthEnableIrq(NetInterface *interface)
Enable interrupts.
uint8_t data[]
Definition: ethernet.h:224
#define ETH_TDES3_LD
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:266
#define STM32H5XX_ETH_TX_BUFFER_SIZE
#define ETH_RDES3_GP
#define ETH_RDES3_CE
error_t stm32h5xxEthReceivePacket(NetInterface *interface)
Receive a packet.
#define ETH_MACCR_RESERVED15
void ETH_IRQHandler(void)
STM32H5 Ethernet MAC interrupt service routine.
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:418
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define osExitIsr(flag)
#define STM32H5XX_ETH_RX_BUFFER_SIZE
#define SMI_OPCODE_WRITE
Definition: nic.h:66
error_t stm32h5xxEthInit(NetInterface *interface)
STM32H5 Ethernet MAC initialization.
#define ETH_RDES3_RE
error_t stm32h5xxEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
#define FALSE
Definition: os_port.h:46
#define ETH_RDES3_RWT
error_t
Error codes.
Definition: error.h:43
#define ETH_TDES2_B1L
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:103
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
const NicDriver stm32h5xxEthDriver
STM32H5 Ethernet MAC driver.
void stm32h5xxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
@ ERROR_INVALID_PACKET
Definition: error.h:141
#define NetInterface
Definition: net.h:40
void stm32h5xxEthInitDmaDesc(NetInterface *interface)
Initialize DMA descriptor lists.
MacAddr addr
MAC address.
Definition: ethernet.h:265
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
#define STM32H5XX_ETH_IRQ_GROUP_PRIORITY
#define NetTxAncillary
Definition: net_misc.h:36
#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
#define MIN(a, b)
Definition: os_port.h:63
#define ETH_RDES3_OWN
#define ETH_TDES3_OWN
#define rxBuffer
MacAddr
Definition: ethernet.h:197
#define TRACE_DEBUG(...)
Definition: debug.h:119
__weak_func void stm32h5xxEthInitGpio(NetInterface *interface)
GPIO configuration.
void stm32h5xxEthEventHandler(NetInterface *interface)
STM32H5 Ethernet MAC event handler.
uint16_t regAddr
#define ETH_MTU
Definition: ethernet.h:116
error_t stm32h5xxEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
uint8_t n
MAC filter table entry.
Definition: ethernet.h:264
void stm32h5xxEthTick(NetInterface *interface)
STM32H5 Ethernet MAC timer handler.
#define STM32H5XX_ETH_RX_BUFFER_COUNT
#define ETH_RDES3_FD
#define STM32H5XX_ETH_IRQ_PRIORITY_GROUPING
Transmit descriptor.
#define osEnterIsr()
#define rxDmaDesc
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define ETH_TDES3_FD
void stm32h5xxEthDisableIrq(NetInterface *interface)
Disable interrupts.
#define ETH_RDES3_OE
#define txDmaDesc
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
error_t stm32h5xxEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
Receive descriptor.
#define ETH_RDES3_IOC
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:51
uint32_t stm32h5xxEthCalcCrc(const void *data, size_t length)
CRC calculation.
@ 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