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-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 "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
210 
211  //Prevent interrupts from being generated when the receive statistic
212  //counters reach half their maximum value
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_STM32H5XX_NUCLEO)
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 //STM32H573I-DK evaluation board?
289 #elif defined(USE_STM32H573I_DK)
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_GPIOC_CLK_ENABLE();
298  __HAL_RCC_GPIOG_CLK_ENABLE();
299 
300  //Select RMII interface mode
301  HAL_SBS_ETHInterfaceSelect(SBS_ETH_RMII);
302 
303  //Configure RMII pins
304  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
305  GPIO_InitStructure.Pull = GPIO_NOPULL;
306  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
307  GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
308 
309  //Configure ETH_RMII_REF_CLK (PA1), ETH_MDIO (PA2) and ETH_RMII_CRS_DV (PA7)
310  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
311  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
312 
313  //Configure ETH_MDC (PC1), ETH_RMII_RXD0 (PC4) and ETH_RMII_RXD1 (PC5)
314  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
315  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
316 
317  //Configure RMII_TX_EN (PG11), ETH_RMII_TXD1 (PG12) and ETH_RMII_TXD0 (PG13)
318  GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
319  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
320 #endif
321 }
322 
323 
324 /**
325  * @brief Initialize DMA descriptor lists
326  * @param[in] interface Underlying network interface
327  **/
328 
330 {
331  uint_t i;
332 
333  //Initialize TX DMA descriptor list
334  for(i = 0; i < STM32H5XX_ETH_TX_BUFFER_COUNT; i++)
335  {
336  //The descriptor is initially owned by the application
337  txDmaDesc[i].tdes0 = 0;
338  txDmaDesc[i].tdes1 = 0;
339  txDmaDesc[i].tdes2 = 0;
340  txDmaDesc[i].tdes3 = 0;
341  }
342 
343  //Initialize TX descriptor index
344  txIndex = 0;
345 
346  //Initialize RX DMA descriptor list
347  for(i = 0; i < STM32H5XX_ETH_RX_BUFFER_COUNT; i++)
348  {
349  //The descriptor is initially owned by the DMA
350  rxDmaDesc[i].rdes0 = (uint32_t) rxBuffer[i];
351  rxDmaDesc[i].rdes1 = 0;
352  rxDmaDesc[i].rdes2 = 0;
354  }
355 
356  //Initialize RX descriptor index
357  rxIndex = 0;
358 
359  //Start location of the TX descriptor list
360  ETH->DMACTDLAR = (uint32_t) &txDmaDesc[0];
361  //Length of the transmit descriptor ring
362  ETH->DMACTDRLR = STM32H5XX_ETH_TX_BUFFER_COUNT - 1;
363 
364  //Start location of the RX descriptor list
365  ETH->DMACRDLAR = (uint32_t) &rxDmaDesc[0];
366  //Length of the receive descriptor ring
367  ETH->DMACRDRLR = STM32H5XX_ETH_RX_BUFFER_COUNT - 1;
368 }
369 
370 
371 /**
372  * @brief STM32H5 Ethernet MAC timer handler
373  *
374  * This routine is periodically called by the TCP/IP stack to handle periodic
375  * operations such as polling the link state
376  *
377  * @param[in] interface Underlying network interface
378  **/
379 
381 {
382  //Valid Ethernet PHY or switch driver?
383  if(interface->phyDriver != NULL)
384  {
385  //Handle periodic operations
386  interface->phyDriver->tick(interface);
387  }
388  else if(interface->switchDriver != NULL)
389  {
390  //Handle periodic operations
391  interface->switchDriver->tick(interface);
392  }
393  else
394  {
395  //Just for sanity
396  }
397 }
398 
399 
400 /**
401  * @brief Enable interrupts
402  * @param[in] interface Underlying network interface
403  **/
404 
406 {
407  //Enable Ethernet MAC interrupts
408  NVIC_EnableIRQ(ETH_IRQn);
409 
410  //Valid Ethernet PHY or switch driver?
411  if(interface->phyDriver != NULL)
412  {
413  //Enable Ethernet PHY interrupts
414  interface->phyDriver->enableIrq(interface);
415  }
416  else if(interface->switchDriver != NULL)
417  {
418  //Enable Ethernet switch interrupts
419  interface->switchDriver->enableIrq(interface);
420  }
421  else
422  {
423  //Just for sanity
424  }
425 }
426 
427 
428 /**
429  * @brief Disable interrupts
430  * @param[in] interface Underlying network interface
431  **/
432 
434 {
435  //Disable Ethernet MAC interrupts
436  NVIC_DisableIRQ(ETH_IRQn);
437 
438  //Valid Ethernet PHY or switch driver?
439  if(interface->phyDriver != NULL)
440  {
441  //Disable Ethernet PHY interrupts
442  interface->phyDriver->disableIrq(interface);
443  }
444  else if(interface->switchDriver != NULL)
445  {
446  //Disable Ethernet switch interrupts
447  interface->switchDriver->disableIrq(interface);
448  }
449  else
450  {
451  //Just for sanity
452  }
453 }
454 
455 
456 /**
457  * @brief STM32H5 Ethernet MAC interrupt service routine
458  **/
459 
460 void ETH_IRQHandler(void)
461 {
462  bool_t flag;
463  uint32_t status;
464 
465  //Interrupt service routine prologue
466  osEnterIsr();
467 
468  //This flag will be set if a higher priority task must be woken
469  flag = FALSE;
470 
471  //Read DMA status register
472  status = ETH->DMACSR;
473 
474  //Packet transmitted?
475  if((status & ETH_DMACSR_TI) != 0)
476  {
477  //Clear TI interrupt flag
478  ETH->DMACSR = ETH_DMACSR_TI;
479 
480  //Check whether the TX buffer is available for writing
481  if((txDmaDesc[txIndex].tdes3 & ETH_TDES3_OWN) == 0)
482  {
483  //Notify the TCP/IP stack that the transmitter is ready to send
484  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
485  }
486  }
487 
488  //Packet received?
489  if((status & ETH_DMACSR_RI) != 0)
490  {
491  //Clear RI interrupt flag
492  ETH->DMACSR = ETH_DMACSR_RI;
493 
494  //Set event flag
495  nicDriverInterface->nicEvent = TRUE;
496  //Notify the TCP/IP stack of the event
497  flag |= osSetEventFromIsr(&netEvent);
498  }
499 
500  //Clear NIS interrupt flag
501  ETH->DMACSR = ETH_DMACSR_NIS;
502 
503  //Interrupt service routine epilogue
504  osExitIsr(flag);
505 }
506 
507 
508 /**
509  * @brief STM32H5 Ethernet MAC event handler
510  * @param[in] interface Underlying network interface
511  **/
512 
514 {
515  error_t error;
516 
517  //Process all pending packets
518  do
519  {
520  //Read incoming packet
521  error = stm32h5xxEthReceivePacket(interface);
522 
523  //No more data in the receive buffer?
524  } while(error != ERROR_BUFFER_EMPTY);
525 }
526 
527 
528 /**
529  * @brief Send a packet
530  * @param[in] interface Underlying network interface
531  * @param[in] buffer Multi-part buffer containing the data to send
532  * @param[in] offset Offset to the first data byte
533  * @param[in] ancillary Additional options passed to the stack along with
534  * the packet
535  * @return Error code
536  **/
537 
539  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
540 {
541  size_t length;
542 
543  //Retrieve the length of the packet
544  length = netBufferGetLength(buffer) - offset;
545 
546  //Check the frame length
548  {
549  //The transmitter can accept another packet
550  osSetEvent(&interface->nicTxEvent);
551  //Report an error
552  return ERROR_INVALID_LENGTH;
553  }
554 
555  //Make sure the current buffer is available for writing
556  if((txDmaDesc[txIndex].tdes3 & ETH_TDES3_OWN) != 0)
557  {
558  return ERROR_FAILURE;
559  }
560 
561  //Copy user data to the transmit buffer
562  netBufferRead(txBuffer[txIndex], buffer, offset, length);
563 
564  //Set the start address of the buffer
565  txDmaDesc[txIndex].tdes0 = (uint32_t) txBuffer[txIndex];
566  //Write the number of bytes to send
567  txDmaDesc[txIndex].tdes2 = ETH_TDES2_IOC | (length & ETH_TDES2_B1L);
568  //Give the ownership of the descriptor to the DMA
569  txDmaDesc[txIndex].tdes3 = ETH_TDES3_OWN | ETH_TDES3_FD | ETH_TDES3_LD;
570 
571  //Data synchronization barrier
572  __DSB();
573 
574  //Clear TBU flag to resume processing
575  ETH->DMACSR = ETH_DMACSR_TBU;
576  //Instruct the DMA to poll the transmit descriptor list
577  ETH->DMACTDTPR = 0;
578 
579  //Increment index and wrap around if necessary
580  if(++txIndex >= STM32H5XX_ETH_TX_BUFFER_COUNT)
581  {
582  txIndex = 0;
583  }
584 
585  //Check whether the next buffer is available for writing
586  if((txDmaDesc[txIndex].tdes3 & ETH_TDES3_OWN) == 0)
587  {
588  //The transmitter can accept another packet
589  osSetEvent(&interface->nicTxEvent);
590  }
591 
592  //Data successfully written
593  return NO_ERROR;
594 }
595 
596 
597 /**
598  * @brief Receive a packet
599  * @param[in] interface Underlying network interface
600  * @return Error code
601  **/
602 
604 {
605  error_t error;
606  size_t n;
607  uint32_t status;
608  NetRxAncillary ancillary;
609 
610  //Current buffer available for reading?
611  if((rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_OWN) == 0)
612  {
613  //FD and LD flags should be set
614  if((rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_FD) != 0 &&
615  (rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_LD) != 0)
616  {
617  //Check error bits
618  status = rxDmaDesc[rxIndex].rdes3 & (ETH_RDES3_CE | ETH_RDES3_GP |
620 
621  //The dribble bit error is valid only in the MII mode
622  if((SBS->PMCR & SBS_PMCR_ETH_SEL_PHY) != SBS_ETH_MII)
623  {
624  status &= ~ETH_RDES3_DE;
625  }
626 
627  //Make sure no error occurred
628  if(status == 0)
629  {
630  //Retrieve the length of the frame
631  n = rxDmaDesc[rxIndex].rdes3 & ETH_RDES3_PL;
632  //Limit the number of data to read
634 
635  //Additional options can be passed to the stack along with the packet
636  ancillary = NET_DEFAULT_RX_ANCILLARY;
637 
638  //Pass the packet to the upper layer
639  nicProcessPacket(interface, rxBuffer[rxIndex], n, &ancillary);
640 
641  //Valid packet received
642  error = NO_ERROR;
643  }
644  else
645  {
646  //The received packet contains an error
647  error = ERROR_INVALID_PACKET;
648  }
649  }
650  else
651  {
652  //The packet is not valid
653  error = ERROR_INVALID_PACKET;
654  }
655 
656  //Set the start address of the buffer
657  rxDmaDesc[rxIndex].rdes0 = (uint32_t) rxBuffer[rxIndex];
658  //Give the ownership of the descriptor back to the DMA
660 
661  //Increment index and wrap around if necessary
662  if(++rxIndex >= STM32H5XX_ETH_RX_BUFFER_COUNT)
663  {
664  rxIndex = 0;
665  }
666  }
667  else
668  {
669  //No more data in the receive buffer
670  error = ERROR_BUFFER_EMPTY;
671  }
672 
673  //Clear RBU flag to resume processing
674  ETH->DMACSR = ETH_DMACSR_RBU;
675  //Instruct the DMA to poll the receive descriptor list
676  ETH->DMACRDTPR = 0;
677 
678  //Return status code
679  return error;
680 }
681 
682 
683 /**
684  * @brief Configure MAC address filtering
685  * @param[in] interface Underlying network interface
686  * @return Error code
687  **/
688 
690 {
691  uint_t i;
692  uint_t j;
693  uint_t k;
694  uint32_t crc;
695  uint32_t hashTable[2];
696  MacAddr unicastMacAddr[3];
697  MacFilterEntry *entry;
698 
699  //Debug message
700  TRACE_DEBUG("Updating MAC filter...\r\n");
701 
702  //Promiscuous mode?
703  if(interface->promiscuous)
704  {
705  //Pass all incoming frames regardless of their destination address
706  ETH->MACPFR = ETH_MACPFR_PR;
707  }
708  else
709  {
710  //Set the MAC address of the station
711  ETH->MACA0LR = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
712  ETH->MACA0HR = interface->macAddr.w[2];
713 
714  //The MAC supports 3 additional addresses for unicast perfect filtering
715  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
716  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
717  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
718 
719  //The hash table is used for multicast address filtering
720  hashTable[0] = 0;
721  hashTable[1] = 0;
722 
723  //The MAC address filter contains the list of MAC addresses to accept
724  //when receiving an Ethernet frame
725  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
726  {
727  //Point to the current entry
728  entry = &interface->macAddrFilter[i];
729 
730  //Valid entry?
731  if(entry->refCount > 0)
732  {
733  //Multicast address?
734  if(macIsMulticastAddr(&entry->addr))
735  {
736  //Compute CRC over the current MAC address
737  crc = stm32h5xxEthCalcCrc(&entry->addr, sizeof(MacAddr));
738 
739  //The upper 6 bits in the CRC register are used to index the
740  //contents of the hash table
741  k = (crc >> 26) & 0x3F;
742 
743  //Update hash table contents
744  hashTable[k / 32] |= (1 << (k % 32));
745  }
746  else
747  {
748  //Up to 3 additional MAC addresses can be specified
749  if(j < 3)
750  {
751  //Save the unicast address
752  unicastMacAddr[j++] = entry->addr;
753  }
754  }
755  }
756  }
757 
758  //Configure the first unicast address filter
759  if(j >= 1)
760  {
761  //When the AE bit is set, the entry is used for perfect filtering
762  ETH->MACA1LR = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
763  ETH->MACA1HR = unicastMacAddr[0].w[2] | ETH_MACAHR_AE;
764  }
765  else
766  {
767  //When the AE bit is cleared, the entry is ignored
768  ETH->MACA1LR = 0;
769  ETH->MACA1HR = 0;
770  }
771 
772  //Configure the second unicast address filter
773  if(j >= 2)
774  {
775  //When the AE bit is set, the entry is used for perfect filtering
776  ETH->MACA2LR = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
777  ETH->MACA2HR = unicastMacAddr[1].w[2] | ETH_MACAHR_AE;
778  }
779  else
780  {
781  //When the AE bit is cleared, the entry is ignored
782  ETH->MACA2LR = 0;
783  ETH->MACA2HR = 0;
784  }
785 
786  //Configure the third unicast address filter
787  if(j >= 3)
788  {
789  //When the AE bit is set, the entry is used for perfect filtering
790  ETH->MACA3LR = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
791  ETH->MACA3HR = unicastMacAddr[2].w[2] | ETH_MACAHR_AE;
792  }
793  else
794  {
795  //When the AE bit is cleared, the entry is ignored
796  ETH->MACA3LR = 0;
797  ETH->MACA3HR = 0;
798  }
799 
800  //Check whether frames with a multicast destination address should be
801  //accepted
802  if(interface->acceptAllMulticast)
803  {
804  //Configure the receive filter
805  ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_PM;
806  }
807  else
808  {
809  //Configure the receive filter
810  ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_HMC;
811 
812  //Configure the multicast hash table
813  ETH->MACHT0R = hashTable[0];
814  ETH->MACHT1R = hashTable[1];
815 
816  //Debug message
817  TRACE_DEBUG(" MACHT0R = %08" PRIX32 "\r\n", ETH->MACHT0R);
818  TRACE_DEBUG(" MACHT1R = %08" PRIX32 "\r\n", ETH->MACHT1R);
819  }
820  }
821 
822  //Successful processing
823  return NO_ERROR;
824 }
825 
826 
827 /**
828  * @brief Adjust MAC configuration parameters for proper operation
829  * @param[in] interface Underlying network interface
830  * @return Error code
831  **/
832 
834 {
835  uint32_t config;
836 
837  //Read current MAC configuration
838  config = ETH->MACCR;
839 
840  //10BASE-T or 100BASE-TX operation mode?
841  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
842  {
843  config |= ETH_MACCR_FES;
844  }
845  else
846  {
847  config &= ~ETH_MACCR_FES;
848  }
849 
850  //Half-duplex or full-duplex mode?
851  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
852  {
853  config |= ETH_MACCR_DM;
854  }
855  else
856  {
857  config &= ~ETH_MACCR_DM;
858  }
859 
860  //Update MAC configuration register
861  ETH->MACCR = config;
862 
863  //Successful processing
864  return NO_ERROR;
865 }
866 
867 
868 /**
869  * @brief Write PHY register
870  * @param[in] opcode Access type (2 bits)
871  * @param[in] phyAddr PHY address (5 bits)
872  * @param[in] regAddr Register address (5 bits)
873  * @param[in] data Register value
874  **/
875 
876 void stm32h5xxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
877  uint8_t regAddr, uint16_t data)
878 {
879  uint32_t temp;
880 
881  //Valid opcode?
882  if(opcode == SMI_OPCODE_WRITE)
883  {
884  //Take care not to alter MDC clock configuration
885  temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
886  //Set up a write operation
887  temp |= ETH_MACMDIOAR_MOC_WR | ETH_MACMDIOAR_MB;
888  //PHY address
889  temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
890  //Register address
891  temp |= (regAddr << 16) & ETH_MACMDIOAR_RDA;
892 
893  //Data to be written in the PHY register
894  ETH->MACMDIODR = data & ETH_MACMDIODR_MD;
895 
896  //Start a write operation
897  ETH->MACMDIOAR = temp;
898  //Wait for the write to complete
899  while((ETH->MACMDIOAR & ETH_MACMDIOAR_MB) != 0)
900  {
901  }
902  }
903  else
904  {
905  //The MAC peripheral only supports standard Clause 22 opcodes
906  }
907 }
908 
909 
910 /**
911  * @brief Read PHY register
912  * @param[in] opcode Access type (2 bits)
913  * @param[in] phyAddr PHY address (5 bits)
914  * @param[in] regAddr Register address (5 bits)
915  * @return Register value
916  **/
917 
918 uint16_t stm32h5xxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
919  uint8_t regAddr)
920 {
921  uint16_t data;
922  uint32_t temp;
923 
924  //Valid opcode?
925  if(opcode == SMI_OPCODE_READ)
926  {
927  //Take care not to alter MDC clock configuration
928  temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
929  //Set up a read operation
930  temp |= ETH_MACMDIOAR_MOC_RD | ETH_MACMDIOAR_MB;
931  //PHY address
932  temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
933  //Register address
934  temp |= (regAddr << 16) & ETH_MACMDIOAR_RDA;
935 
936  //Start a read operation
937  ETH->MACMDIOAR = temp;
938  //Wait for the read to complete
939  while((ETH->MACMDIOAR & ETH_MACMDIOAR_MB) != 0)
940  {
941  }
942 
943  //Get register value
944  data = ETH->MACMDIODR & ETH_MACMDIODR_MD;
945  }
946  else
947  {
948  //The MAC peripheral only supports standard Clause 22 opcodes
949  data = 0;
950  }
951 
952  //Return the value of the PHY register
953  return data;
954 }
955 
956 
957 /**
958  * @brief CRC calculation
959  * @param[in] data Pointer to the data over which to calculate the CRC
960  * @param[in] length Number of bytes to process
961  * @return Resulting CRC value
962  **/
963 
964 uint32_t stm32h5xxEthCalcCrc(const void *data, size_t length)
965 {
966  uint_t i;
967  uint_t j;
968  uint32_t crc;
969  const uint8_t *p;
970 
971  //Point to the data over which to calculate the CRC
972  p = (uint8_t *) data;
973  //CRC preset value
974  crc = 0xFFFFFFFF;
975 
976  //Loop through data
977  for(i = 0; i < length; i++)
978  {
979  //The message is processed bit by bit
980  for(j = 0; j < 8; j++)
981  {
982  //Update CRC value
983  if((((crc >> 31) ^ (p[i] >> j)) & 0x01) != 0)
984  {
985  crc = (crc << 1) ^ 0x04C11DB7;
986  }
987  else
988  {
989  crc = crc << 1;
990  }
991  }
992  }
993 
994  //Return CRC value
995  return ~crc;
996 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define ETH_MMCRIMR_RXLPITRCIM
STM32H5 Ethernet MAC driver.
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:53
#define ETH_MMCRIMR_RXALGNERPIM
#define netEvent
Definition: net_legacy.h:196
#define ETH_MMCRIMR_RXCRCERPIM
#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:222
#define ETH_TDES3_LD
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
#define STM32H5XX_ETH_TX_BUFFER_SIZE
#define ETH_RDES3_GP
#define ETH_RDES3_CE
error_t stm32h5xxEthReceivePacket(NetInterface *interface)
Receive a packet.
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:392
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define osExitIsr(flag)
#define STM32H5XX_ETH_RX_BUFFER_SIZE
#define ETH_MMCRIMR_RXLPIUSCIM
#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_MMCTIMR_TXLPITRCIM
#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:104
@ 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:140
#define NetInterface
Definition: net.h:36
void stm32h5xxEthInitDmaDesc(NetInterface *interface)
Initialize DMA descriptor lists.
MacAddr addr
MAC address.
Definition: ethernet.h:263
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
#define ETH_MMCTIMR_TXLPIUSCIM
#define ETH_MMCTIMR_TXGPKTIM
#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: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 ETH_RDES3_OWN
#define ETH_TDES3_OWN
#define rxBuffer
MacAddr
Definition: ethernet.h:195
#define TRACE_DEBUG(...)
Definition: debug.h:107
__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:262
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
#define ETH_MMCRIMR_RXUCGPIM
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:50
TCP/IP stack core.
#define ETH_MACCR_RESERVED15
NIC driver.
Definition: nic.h:286
Receive descriptor.
#define ETH_RDES3_IOC
#define ETH_MMCTIMR_TXMCOLGPIM
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
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
#define ETH_MMCTIMR_TXSCOLGPIM
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83