stm32h7xx_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file stm32h7xx_eth_driver.c
3  * @brief STM32H7 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 "stm32h7xx.h"
36 #include "stm32h7xx_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 = STM32H7XX_ETH_RAM_SECTION
51 //Receive buffer
52 #pragma data_alignment = 4
53 #pragma location = STM32H7XX_ETH_RAM_SECTION
55 //Transmit DMA descriptors
56 #pragma data_alignment = 4
57 #pragma location = STM32H7XX_ETH_RAM_SECTION
59 //Receive DMA descriptors
60 #pragma data_alignment = 4
61 #pragma location = STM32H7XX_ETH_RAM_SECTION
63 
64 //Keil MDK-ARM or GCC compiler?
65 #else
66 
67 //Transmit buffer
69  __attribute__((aligned(4), __section__(STM32H7XX_ETH_RAM_SECTION)));
70 //Receive buffer
72  __attribute__((aligned(4), __section__(STM32H7XX_ETH_RAM_SECTION)));
73 //Transmit DMA descriptors
75  __attribute__((aligned(4), __section__(STM32H7XX_ETH_RAM_SECTION)));
76 //Receive DMA descriptors
78  __attribute__((aligned(4), __section__(STM32H7XX_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 STM32H7 Ethernet MAC driver
90  **/
91 
93 {
95  ETH_MTU,
106  TRUE,
107  TRUE,
108  TRUE,
109  FALSE
110 };
111 
112 
113 /**
114  * @brief STM32H7 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 STM32H7 Ethernet MAC...\r\n");
126 
127  //Save underlying network interface
128  nicDriverInterface = interface;
129 
130  //GPIO configuration
131  stm32h7xxEthInitGpio(interface);
132 
133  //Enable Ethernet MAC clock
134  __HAL_RCC_ETH1MAC_CLK_ENABLE();
135  __HAL_RCC_ETH1TX_CLK_ENABLE();
136  __HAL_RCC_ETH1RX_CLK_ENABLE();
137 
138  //Reset Ethernet MAC peripheral
139  __HAL_RCC_ETH1MAC_FORCE_RESET();
140  __HAL_RCC_ETH1MAC_RELEASE_RESET();
141 
142  //Perform a software reset
143  ETH->DMAMR |= ETH_DMAMR_SWR;
144  //Wait for the reset to complete
145  while((ETH->DMAMR & ETH_DMAMR_SWR) != 0)
146  {
147  }
148 
149  //Adjust MDC clock range depending on HCLK frequency
150  ETH->MACMDIOAR = ETH_MACMDIOAR_CR_DIV124;
151 
152  //Valid Ethernet PHY or switch driver?
153  if(interface->phyDriver != NULL)
154  {
155  //Ethernet PHY initialization
156  error = interface->phyDriver->init(interface);
157  }
158  else if(interface->switchDriver != NULL)
159  {
160  //Ethernet switch initialization
161  error = interface->switchDriver->init(interface);
162  }
163  else
164  {
165  //The interface is not properly configured
166  error = ERROR_FAILURE;
167  }
168 
169  //Any error to report?
170  if(error)
171  {
172  return error;
173  }
174 
175  //Use default MAC configuration
176  ETH->MACCR = ETH_MACCR_GPSLCE | ETH_MACCR_RESERVED15 | ETH_MACCR_DO;
177 
178  //Set the maximum packet size that can be accepted
179  temp = ETH->MACECR & ~ETH_MACECR_GPSL;
180  ETH->MACECR = temp | STM32H7XX_ETH_RX_BUFFER_SIZE;
181 
182  //Configure MAC address filtering
184 
185  //Disable flow control
186  ETH->MACTFCR = 0;
187  ETH->MACRFCR = 0;
188 
189  //Configure DMA operating mode
190  ETH->DMAMR = ETH_DMAMR_INTM_0 | ETH_DMAMR_PR_1_1;
191  //Configure system bus mode
192  ETH->DMASBMR |= ETH_DMASBMR_AAL;
193  //The DMA takes the descriptor table as contiguous
194  ETH->DMACCR = ETH_DMACCR_DSL_0BIT;
195 
196  //Configure TX features
197  ETH->DMACTCR = ETH_DMACTCR_TPBL_32PBL;
198 
199  //Configure RX features
200  ETH->DMACRCR = ETH_DMACRCR_RPBL_32PBL;
201  ETH->DMACRCR |= (STM32H7XX_ETH_RX_BUFFER_SIZE << 1) & ETH_DMACRCR_RBSZ;
202 
203  //Enable store and forward mode
204  ETH->MTLTQOMR |= ETH_MTLTQOMR_TSF;
205  ETH->MTLRQOMR |= ETH_MTLRQOMR_RSF;
206 
207  //Initialize DMA descriptor lists
208  stm32h7xxEthInitDmaDesc(interface);
209 
210  //Prevent interrupts from being generated when the transmit statistic
211  //counters reach half their maximum value
214 
215  //Prevent interrupts from being generated when the receive statistic
216  //counters reach half their maximum value
219 
220  //Disable MAC interrupts
221  ETH->MACIER = 0;
222  //Enable the desired DMA interrupts
223  ETH->DMACIER = ETH_DMACIER_NIE | ETH_DMACIER_RIE | ETH_DMACIER_TIE;
224 
225  //Set priority grouping (4 bits for pre-emption priority, no bits for subpriority)
226  NVIC_SetPriorityGrouping(STM32H7XX_ETH_IRQ_PRIORITY_GROUPING);
227 
228  //Configure Ethernet interrupt priority
229  NVIC_SetPriority(ETH_IRQn, NVIC_EncodePriority(STM32H7XX_ETH_IRQ_PRIORITY_GROUPING,
231 
232  //Enable MAC transmission and reception
233  ETH->MACCR |= ETH_MACCR_TE | ETH_MACCR_RE;
234 
235  //Enable DMA transmission and reception
236  ETH->DMACTCR |= ETH_DMACTCR_ST;
237  ETH->DMACRCR |= ETH_DMACRCR_SR;
238 
239  //Accept any packets from the upper layer
240  osSetEvent(&interface->nicTxEvent);
241 
242  //Successful initialization
243  return NO_ERROR;
244 }
245 
246 
247 /**
248  * @brief GPIO configuration
249  * @param[in] interface Underlying network interface
250  **/
251 
252 __weak_func void stm32h7xxEthInitGpio(NetInterface *interface)
253 {
254 //Nucleo-H723ZG, Nucleo-H743ZI, Nucleo-H743ZI2, Nucleo-H753ZI, Nucleo-H745ZI-Q,
255 //or Nucleo-H755ZI-Q evaluation board?
256 #if defined(USE_NUCLEO_H723ZG) || defined(USE_NUCLEO_H743ZI) || \
257  defined(USE_NUCLEO_H743ZI2) || defined(USE_NUCLEO_H745ZI_Q)
258  GPIO_InitTypeDef GPIO_InitStructure;
259 
260  //Enable SYSCFG clock
261  __HAL_RCC_SYSCFG_CLK_ENABLE();
262 
263  //Enable GPIO clocks
264  __HAL_RCC_GPIOA_CLK_ENABLE();
265  __HAL_RCC_GPIOB_CLK_ENABLE();
266  __HAL_RCC_GPIOC_CLK_ENABLE();
267  __HAL_RCC_GPIOG_CLK_ENABLE();
268 
269  //Select RMII interface mode
270  HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_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_ETH;
277 
278  //Configure ETH_RMII_REF_CLK (PA1), ETH_MDIO (PA2) and ETH_RMII_CRS_DV (PA7)
279  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
280  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
281 
282  //Configure ETH_RMII_TXD1 (PB13)
283  GPIO_InitStructure.Pin = GPIO_PIN_13;
284  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
285 
286  //Configure ETH_MDC (PC1), ETH_RMII_RXD0 (PC4) and ETH_RMII_RXD1 (PC5)
287  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
288  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
289 
290  //Configure RMII_TX_EN (PG11) and ETH_RMII_TXD0 (PG13)
291  GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13;
292  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
293 
294 //STM32H735G-DK evaluation board?
295 #elif defined(USE_STM32H735G_DK)
296  GPIO_InitTypeDef GPIO_InitStructure;
297 
298  //Enable SYSCFG clock
299  __HAL_RCC_SYSCFG_CLK_ENABLE();
300 
301  //Enable GPIO clocks
302  __HAL_RCC_GPIOA_CLK_ENABLE();
303  __HAL_RCC_GPIOB_CLK_ENABLE();
304  __HAL_RCC_GPIOC_CLK_ENABLE();
305 
306  //Select RMII interface mode
307  HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_RMII);
308 
309  //Configure RMII pins
310  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
311  GPIO_InitStructure.Pull = GPIO_NOPULL;
312  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
313  GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
314 
315  //Configure ETH_RMII_REF_CLK (PA1), ETH_MDIO (PA2) and ETH_RMII_CRS_DV (PA7)
316  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
317  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
318 
319  //Configure RMII_RX_ER (PB10), RMII_TX_EN (PB11), ETH_RMII_TXD1 (PB12)
320  //and ETH_RMII_TXD0 (PB13)
321  GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
322  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
323 
324  //Configure ETH_MDC (PC1), ETH_RMII_RXD0 (PC4) and ETH_RMII_RXD1 (PC5)
325  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
326  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
327 
328 //STM32H745I-Discovery or STM32H750B-DK evaluation board?
329 #elif defined(USE_STM32H745I_DISCO) || defined(USE_STM32H750B_DISCO)
330  GPIO_InitTypeDef GPIO_InitStructure;
331 
332  //Enable SYSCFG clock
333  __HAL_RCC_SYSCFG_CLK_ENABLE();
334 
335  //Enable GPIO clocks
336  __HAL_RCC_GPIOA_CLK_ENABLE();
337  __HAL_RCC_GPIOB_CLK_ENABLE();
338  __HAL_RCC_GPIOC_CLK_ENABLE();
339  __HAL_RCC_GPIOE_CLK_ENABLE();
340  __HAL_RCC_GPIOG_CLK_ENABLE();
341  __HAL_RCC_GPIOH_CLK_ENABLE();
342  __HAL_RCC_GPIOI_CLK_ENABLE();
343 
344  //Select MII interface mode
345  HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_MII);
346 
347  //Configure MII pins
348  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
349  GPIO_InitStructure.Pull = GPIO_NOPULL;
350  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
351  GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
352 
353  //Configure ETH_MII_RX_CLK (PA1), ETH_MDIO (PA2) and ETH_MII_RX_DV (PA7)
354  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
355  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
356 
357  //Configure ETH_MII_RXD2 (PB0), ETH_MII_RXD3 (PB1) and ETH_MII_RX_ER (PB2)
358  GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2;
359  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
360 
361  //Configure ETH_MDC (PC1), ETH_MII_TXD2 (PC2), ETH_MII_TX_CLK (PC3),
362  //ETH_MII_RXD0 (PC4) and ETH_MII_RXD1 (PC5)
363  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 |
364  GPIO_PIN_5;
365  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
366 
367  //Configure ETH_MII_TXD3 (PE2)
368  GPIO_InitStructure.Pin = GPIO_PIN_2;
369  HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
370 
371  //Configure ETH_MII_TX_EN (PG11), ETH_MII_TXD1 (PG12) and ETH_MII_TXD0 (PG13)
372  GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
373  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
374 
375  //Configure ETH_MII_CRS (PH2) and ETH_MII_COL (PH3)
376  //GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_3;
377  //HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
378 
379  //Configure ETH_MII_RX_ER (PI10)
380  GPIO_InitStructure.Pin = GPIO_PIN_10;
381  HAL_GPIO_Init(GPIOI, &GPIO_InitStructure);
382 
383 //STM32H743I-EVAL, STM32H747I-EVAL or STM32H747I-Discovery evaluation board?
384 #elif defined(USE_STM32H743I_EVAL) || defined(USE_STM32H747I_EVAL) || \
385  defined(USE_STM32H747I_DISCO)
386  GPIO_InitTypeDef GPIO_InitStructure;
387 
388  //Enable SYSCFG clock
389  __HAL_RCC_SYSCFG_CLK_ENABLE();
390 
391  //Enable GPIO clocks
392  __HAL_RCC_GPIOA_CLK_ENABLE();
393  __HAL_RCC_GPIOC_CLK_ENABLE();
394  __HAL_RCC_GPIOG_CLK_ENABLE();
395 
396  //Select RMII interface mode
397  HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_RMII);
398 
399  //Configure RMII pins
400  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
401  GPIO_InitStructure.Pull = GPIO_NOPULL;
402  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
403  GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
404 
405  //Configure ETH_RMII_REF_CLK (PA1), ETH_MDIO (PA2) and ETH_RMII_CRS_DV (PA7)
406  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
407  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
408 
409  //Configure ETH_MDC (PC1), ETH_RMII_RXD0 (PC4) and ETH_RMII_RXD1 (PC5)
410  GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
411  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
412 
413  //Configure RMII_TX_EN (PG11), ETH_RMII_TXD1 (PG12) and ETH_RMII_TXD0 (PG13)
414  GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
415  HAL_GPIO_Init(GPIOG, &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 < STM32H7XX_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 < STM32H7XX_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 = STM32H7XX_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 = STM32H7XX_ETH_RX_BUFFER_COUNT - 1;
464 }
465 
466 
467 /**
468  * @brief STM32H7 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 STM32H7 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(&netEvent);
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 STM32H7 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 = stm32h7xxEthReceivePacket(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 >= STM32H7XX_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((SYSCFG->PMCR & SYSCFG_PMCR_EPIS_SEL) != SYSCFG_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 >= STM32H7XX_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 = stm32h7xxEthCalcCrc(&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 = %08" PRIX32 "\r\n", ETH->MACHT0R);
914  TRACE_DEBUG(" MACHT1R = %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 stm32h7xxEthWritePhyReg(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 stm32h7xxEthReadPhyReg(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 stm32h7xxEthCalcCrc(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.
#define ETH_MMCRIMR_RXLPITRCIM
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
#define STM32H7XX_ETH_TX_BUFFER_COUNT
@ 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_RDES3_LD
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 stm32h7xxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
uint8_t data[]
Definition: ethernet.h:222
#define STM32H7XX_ETH_RX_BUFFER_COUNT
#define ETH_TDES3_LD
void stm32h7xxEthEventHandler(NetInterface *interface)
STM32H7 Ethernet MAC event handler.
#define STM32H7XX_ETH_TX_BUFFER_SIZE
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
error_t stm32h7xxEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
__weak_func void stm32h7xxEthInitGpio(NetInterface *interface)
GPIO configuration.
#define ETH_RDES3_GP
#define ETH_RDES3_CE
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 ETH_MMCRIMR_RXLPIUSCIM
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define ETH_RDES3_RE
#define STM32H7XX_ETH_RAM_SECTION
#define FALSE
Definition: os_port.h:46
#define ETH_MMCTIMR_TXLPITRCIM
#define ETH_RDES3_RWT
error_t stm32h7xxEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
error_t stm32h7xxEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
error_t
Error codes.
Definition: error.h:43
#define ETH_TDES2_B1L
void ETH_IRQHandler(void)
STM32H7 Ethernet MAC interrupt service routine.
#define STM32H7XX_ETH_IRQ_PRIORITY_GROUPING
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
uint16_t stm32h7xxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
@ ERROR_INVALID_PACKET
Definition: error.h:140
#define NetInterface
Definition: net.h:36
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 NetTxAncillary
Definition: net_misc.h:36
#define ETH_RDES3_BUF1V
#define SMI_OPCODE_READ
Definition: nic.h:67
#define ETH_TDES2_IOC
void stm32h7xxEthEnableIrq(NetInterface *interface)
Enable interrupts.
#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
void stm32h7xxEthTick(NetInterface *interface)
STM32H7 Ethernet MAC timer handler.
#define MIN(a, b)
Definition: os_port.h:63
#define ETH_RDES3_OWN
error_t stm32h7xxEthInit(NetInterface *interface)
STM32H7 Ethernet MAC initialization.
void stm32h7xxEthDisableIrq(NetInterface *interface)
Disable interrupts.
STM32H7 Ethernet MAC driver.
#define ETH_TDES3_OWN
#define rxBuffer
void stm32h7xxEthInitDmaDesc(NetInterface *interface)
Initialize DMA descriptor lists.
MacAddr
Definition: ethernet.h:195
#define STM32H7XX_ETH_IRQ_GROUP_PRIORITY
error_t stm32h7xxEthReceivePacket(NetInterface *interface)
Receive a packet.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define STM32H7XX_ETH_IRQ_SUB_PRIORITY
uint16_t regAddr
#define ETH_MTU
Definition: ethernet.h:116
const NicDriver stm32h7xxEthDriver
STM32H7 Ethernet MAC driver.
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
#define ETH_RDES3_FD
Transmit descriptor.
#define ETH_MMCRIMR_RXUCGPIM
#define osEnterIsr()
#define STM32H7XX_ETH_RX_BUFFER_SIZE
#define rxDmaDesc
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define ETH_TDES3_FD
#define ETH_RDES3_OE
#define txDmaDesc
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
uint32_t stm32h7xxEthCalcCrc(const void *data, size_t length)
CRC calculation.
#define ETH_MACCR_RESERVED15
Receive descriptor.
NIC driver.
Definition: nic.h:286
#define ETH_RDES3_IOC
#define ETH_MMCTIMR_TXMCOLGPIM
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
#define ETH_MMCTIMR_TXSCOLGPIM
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83