2 ******************************************************************************
3 * @file stm32f7xx_hal_eth.c
4 * @author MCD Application Team
7 * @brief ETH HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Ethernet (ETH) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State and Errors functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 (#)Declare a ETH_HandleTypeDef handle structure, for example:
21 ETH_HandleTypeDef heth;
23 (#)Fill parameters of Init structure in heth handle
25 (#)Call HAL_ETH_Init() API to initialize the Ethernet peripheral (MAC, DMA, ...)
27 (#)Initialize the ETH low level resources through the HAL_ETH_MspInit() API:
28 (##) Enable the Ethernet interface clock using
29 (+++) __HAL_RCC_ETHMAC_CLK_ENABLE();
30 (+++) __HAL_RCC_ETHMACTX_CLK_ENABLE();
31 (+++) __HAL_RCC_ETHMACRX_CLK_ENABLE();
33 (##) Initialize the related GPIO clocks
34 (##) Configure Ethernet pin-out
35 (##) Configure Ethernet NVIC interrupt (IT mode)
37 (#)Initialize Ethernet DMA Descriptors in chain mode and point to allocated buffers:
38 (##) HAL_ETH_DMATxDescListInit(); for Transmission process
39 (##) HAL_ETH_DMARxDescListInit(); for Reception process
41 (#)Enable MAC and DMA transmission and reception:
44 (#)Prepare ETH DMA TX Descriptors and give the hand to ETH DMA to transfer
45 the frame to MAC TX FIFO:
46 (##) HAL_ETH_TransmitFrame();
48 (#)Poll for a received frame in ETH RX DMA Descriptors and get received
50 (##) HAL_ETH_GetReceivedFrame(); (should be called into an infinite loop)
52 (#) Get a received frame when an ETH RX interrupt occurs:
53 (##) HAL_ETH_GetReceivedFrame_IT(); (called in IT mode only)
55 (#) Communicate with external PHY device:
56 (##) Read a specific register from the PHY
57 HAL_ETH_ReadPHYRegister();
58 (##) Write data to a specific RHY register:
59 HAL_ETH_WritePHYRegister();
61 (#) Configure the Ethernet MAC after ETH peripheral initialization
62 HAL_ETH_ConfigMAC(); all MAC parameters should be filled.
64 (#) Configure the Ethernet DMA after ETH peripheral initialization
65 HAL_ETH_ConfigDMA(); all DMA parameters should be filled.
68 ******************************************************************************
71 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
73 * Redistribution and use in source and binary forms, with or without modification,
74 * are permitted provided that the following conditions are met:
75 * 1. Redistributions of source code must retain the above copyright notice,
76 * this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright notice,
78 * this list of conditions and the following disclaimer in the documentation
79 * and/or other materials provided with the distribution.
80 * 3. Neither the name of STMicroelectronics nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
84 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
85 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
87 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
90 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
91 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
92 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
93 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
95 ******************************************************************************
98 /* Includes ------------------------------------------------------------------*/
99 #include "stm32f7xx_hal.h"
101 /** @addtogroup STM32F7xx_HAL_Driver
105 /** @defgroup ETH ETH
106 * @brief ETH HAL module driver
110 #ifdef HAL_ETH_MODULE_ENABLED
113 /* Private typedef -----------------------------------------------------------*/
114 /* Private define ------------------------------------------------------------*/
115 /** @defgroup ETH_Private_Constants ETH Private Constants
118 #define ETH_TIMEOUT_SWRESET ((uint32_t)500)
119 #define ETH_TIMEOUT_LINKED_STATE ((uint32_t)5000)
120 #define ETH_TIMEOUT_AUTONEGO_COMPLETED ((uint32_t)5000)
125 /* Private macro -------------------------------------------------------------*/
126 /* Private variables ---------------------------------------------------------*/
127 /* Private function prototypes -----------------------------------------------*/
128 /** @defgroup ETH_Private_Functions ETH Private Functions
131 static void ETH_MACDMAConfig(ETH_HandleTypeDef
*heth
, uint32_t err
);
132 static void ETH_MACAddressConfig(ETH_HandleTypeDef
*heth
, uint32_t MacAddr
, uint8_t *Addr
);
133 static void ETH_MACReceptionEnable(ETH_HandleTypeDef
*heth
);
134 static void ETH_MACReceptionDisable(ETH_HandleTypeDef
*heth
);
135 static void ETH_MACTransmissionEnable(ETH_HandleTypeDef
*heth
);
136 static void ETH_MACTransmissionDisable(ETH_HandleTypeDef
*heth
);
137 static void ETH_DMATransmissionEnable(ETH_HandleTypeDef
*heth
);
138 static void ETH_DMATransmissionDisable(ETH_HandleTypeDef
*heth
);
139 static void ETH_DMAReceptionEnable(ETH_HandleTypeDef
*heth
);
140 static void ETH_DMAReceptionDisable(ETH_HandleTypeDef
*heth
);
141 static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef
*heth
);
146 /* Private functions ---------------------------------------------------------*/
148 /** @defgroup ETH_Exported_Functions ETH Exported Functions
152 /** @defgroup ETH_Exported_Functions_Group1 Initialization and de-initialization functions
153 * @brief Initialization and Configuration functions
156 ===============================================================================
157 ##### Initialization and de-initialization functions #####
158 ===============================================================================
159 [..] This section provides functions allowing to:
160 (+) Initialize and configure the Ethernet peripheral
161 (+) De-initialize the Ethernet peripheral
168 * @brief Initializes the Ethernet MAC and DMA according to default
170 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
171 * the configuration information for ETHERNET module
174 HAL_StatusTypeDef
HAL_ETH_Init(ETH_HandleTypeDef
*heth
)
176 uint32_t tempreg
= 0, phyreg
= 0;
177 uint32_t hclk
= 60000000;
178 uint32_t tickstart
= 0;
179 uint32_t err
= ETH_SUCCESS
;
181 /* Check the ETH peripheral state */
187 /* Check parameters */
188 assert_param(IS_ETH_AUTONEGOTIATION(heth
->Init
.AutoNegotiation
));
189 assert_param(IS_ETH_RX_MODE(heth
->Init
.RxMode
));
190 assert_param(IS_ETH_CHECKSUM_MODE(heth
->Init
.ChecksumMode
));
191 assert_param(IS_ETH_MEDIA_INTERFACE(heth
->Init
.MediaInterface
));
193 if(heth
->State
== HAL_ETH_STATE_RESET
)
195 /* Allocate lock resource and initialize it */
196 heth
->Lock
= HAL_UNLOCKED
;
197 /* Init the low level hardware : GPIO, CLOCK, NVIC. */
198 HAL_ETH_MspInit(heth
);
201 /* Enable SYSCFG Clock */
202 __HAL_RCC_SYSCFG_CLK_ENABLE();
204 /* Select MII or RMII Mode*/
205 SYSCFG
->PMC
&= ~(SYSCFG_PMC_MII_RMII_SEL
);
206 SYSCFG
->PMC
|= (uint32_t)heth
->Init
.MediaInterface
;
208 /* Ethernet Software reset */
209 /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
210 /* After reset all the registers holds their respective reset values */
211 (heth
->Instance
)->DMABMR
|= ETH_DMABMR_SR
;
214 tickstart
= HAL_GetTick();
216 /* Wait for software reset */
217 while (((heth
->Instance
)->DMABMR
& ETH_DMABMR_SR
) != (uint32_t)RESET
)
219 /* Check for the Timeout */
220 if((HAL_GetTick() - tickstart
) > ETH_TIMEOUT_SWRESET
)
222 heth
->State
= HAL_ETH_STATE_TIMEOUT
;
224 /* Process Unlocked */
227 /* Note: The SWR is not performed if the ETH_RX_CLK or the ETH_TX_CLK are
228 not available, please check your external PHY or the IO configuration */
234 /*-------------------------------- MAC Initialization ----------------------*/
235 /* Get the ETHERNET MACMIIAR value */
236 tempreg
= (heth
->Instance
)->MACMIIAR
;
237 /* Clear CSR Clock Range CR[2:0] bits */
238 tempreg
&= ETH_MACMIIAR_CR_MASK
;
240 /* Get hclk frequency value */
241 hclk
= HAL_RCC_GetHCLKFreq();
243 /* Set CR bits depending on hclk value */
244 if((hclk
>= 20000000)&&(hclk
< 35000000))
246 /* CSR Clock Range between 20-35 MHz */
247 tempreg
|= (uint32_t)ETH_MACMIIAR_CR_Div16
;
249 else if((hclk
>= 35000000)&&(hclk
< 60000000))
251 /* CSR Clock Range between 35-60 MHz */
252 tempreg
|= (uint32_t)ETH_MACMIIAR_CR_Div26
;
254 else if((hclk
>= 60000000)&&(hclk
< 100000000))
256 /* CSR Clock Range between 60-100 MHz */
257 tempreg
|= (uint32_t)ETH_MACMIIAR_CR_Div42
;
259 else if((hclk
>= 100000000)&&(hclk
< 150000000))
261 /* CSR Clock Range between 100-150 MHz */
262 tempreg
|= (uint32_t)ETH_MACMIIAR_CR_Div62
;
264 else /* ((hclk >= 150000000)&&(hclk <= 216000000)) */
266 /* CSR Clock Range between 150-216 MHz */
267 tempreg
|= (uint32_t)ETH_MACMIIAR_CR_Div102
;
270 /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
271 (heth
->Instance
)->MACMIIAR
= (uint32_t)tempreg
;
273 /*-------------------- PHY initialization and configuration ----------------*/
274 /* Put the PHY in reset mode */
275 if((HAL_ETH_WritePHYRegister(heth
, PHY_BCR
, PHY_RESET
)) != HAL_OK
)
277 /* In case of write timeout */
280 /* Config MAC and DMA */
281 ETH_MACDMAConfig(heth
, err
);
283 /* Set the ETH peripheral state to READY */
284 heth
->State
= HAL_ETH_STATE_READY
;
286 /* Return HAL_ERROR */
290 /* Delay to assure PHY reset */
291 HAL_Delay(PHY_RESET_DELAY
);
293 if((heth
->Init
).AutoNegotiation
!= ETH_AUTONEGOTIATION_DISABLE
)
296 tickstart
= HAL_GetTick();
298 /* We wait for linked status */
301 HAL_ETH_ReadPHYRegister(heth
, PHY_BSR
, &phyreg
);
303 /* Check for the Timeout */
304 if((HAL_GetTick() - tickstart
) > ETH_TIMEOUT_LINKED_STATE
)
306 /* In case of write timeout */
309 /* Config MAC and DMA */
310 ETH_MACDMAConfig(heth
, err
);
312 heth
->State
= HAL_ETH_STATE_READY
;
314 /* Process Unlocked */
319 } while (((phyreg
& PHY_LINKED_STATUS
) != PHY_LINKED_STATUS
));
322 /* Enable Auto-Negotiation */
323 if((HAL_ETH_WritePHYRegister(heth
, PHY_BCR
, PHY_AUTONEGOTIATION
)) != HAL_OK
)
325 /* In case of write timeout */
328 /* Config MAC and DMA */
329 ETH_MACDMAConfig(heth
, err
);
331 /* Set the ETH peripheral state to READY */
332 heth
->State
= HAL_ETH_STATE_READY
;
334 /* Return HAL_ERROR */
339 tickstart
= HAL_GetTick();
341 /* Wait until the auto-negotiation will be completed */
344 HAL_ETH_ReadPHYRegister(heth
, PHY_BSR
, &phyreg
);
346 /* Check for the Timeout */
347 if((HAL_GetTick() - tickstart
) > ETH_TIMEOUT_AUTONEGO_COMPLETED
)
349 /* In case of write timeout */
352 /* Config MAC and DMA */
353 ETH_MACDMAConfig(heth
, err
);
355 heth
->State
= HAL_ETH_STATE_READY
;
357 /* Process Unlocked */
363 } while (((phyreg
& PHY_AUTONEGO_COMPLETE
) != PHY_AUTONEGO_COMPLETE
));
365 /* Read the result of the auto-negotiation */
366 if((HAL_ETH_ReadPHYRegister(heth
, PHY_SR
, &phyreg
)) != HAL_OK
)
368 /* In case of write timeout */
371 /* Config MAC and DMA */
372 ETH_MACDMAConfig(heth
, err
);
374 /* Set the ETH peripheral state to READY */
375 heth
->State
= HAL_ETH_STATE_READY
;
377 /* Return HAL_ERROR */
381 /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
382 if((phyreg
& PHY_DUPLEX_STATUS
) != (uint32_t)RESET
)
384 /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
385 (heth
->Init
).DuplexMode
= ETH_MODE_FULLDUPLEX
;
389 /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
390 (heth
->Init
).DuplexMode
= ETH_MODE_HALFDUPLEX
;
392 /* Configure the MAC with the speed fixed by the auto-negotiation process */
393 if((phyreg
& PHY_SPEED_STATUS
) == PHY_SPEED_STATUS
)
395 /* Set Ethernet speed to 10M following the auto-negotiation */
396 (heth
->Init
).Speed
= ETH_SPEED_10M
;
400 /* Set Ethernet speed to 100M following the auto-negotiation */
401 (heth
->Init
).Speed
= ETH_SPEED_100M
;
404 else /* AutoNegotiation Disable */
406 /* Check parameters */
407 assert_param(IS_ETH_SPEED(heth
->Init
.Speed
));
408 assert_param(IS_ETH_DUPLEX_MODE(heth
->Init
.DuplexMode
));
410 /* Set MAC Speed and Duplex Mode */
411 if(HAL_ETH_WritePHYRegister(heth
, PHY_BCR
, ((uint16_t)((heth
->Init
).DuplexMode
>> 3) |
412 (uint16_t)((heth
->Init
).Speed
>> 1))) != HAL_OK
)
414 /* In case of write timeout */
417 /* Config MAC and DMA */
418 ETH_MACDMAConfig(heth
, err
);
420 /* Set the ETH peripheral state to READY */
421 heth
->State
= HAL_ETH_STATE_READY
;
423 /* Return HAL_ERROR */
427 /* Delay to assure PHY configuration */
428 HAL_Delay(PHY_CONFIG_DELAY
);
431 /* Config MAC and DMA */
432 ETH_MACDMAConfig(heth
, err
);
434 /* Set ETH HAL State to Ready */
435 heth
->State
= HAL_ETH_STATE_READY
;
437 /* Return function status */
442 * @brief De-Initializes the ETH peripheral.
443 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
444 * the configuration information for ETHERNET module
447 HAL_StatusTypeDef
HAL_ETH_DeInit(ETH_HandleTypeDef
*heth
)
449 /* Set the ETH peripheral state to BUSY */
450 heth
->State
= HAL_ETH_STATE_BUSY
;
452 /* De-Init the low level hardware : GPIO, CLOCK, NVIC. */
453 HAL_ETH_MspDeInit(heth
);
455 /* Set ETH HAL state to Disabled */
456 heth
->State
= HAL_ETH_STATE_RESET
;
461 /* Return function status */
466 * @brief Initializes the DMA Tx descriptors in chain mode.
467 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
468 * the configuration information for ETHERNET module
469 * @param DMATxDescTab: Pointer to the first Tx desc list
470 * @param TxBuff: Pointer to the first TxBuffer list
471 * @param TxBuffCount: Number of the used Tx desc in the list
474 HAL_StatusTypeDef
HAL_ETH_DMATxDescListInit(ETH_HandleTypeDef
*heth
, ETH_DMADescTypeDef
*DMATxDescTab
, uint8_t *TxBuff
, uint32_t TxBuffCount
)
477 ETH_DMADescTypeDef
*dmatxdesc
;
482 /* Set the ETH peripheral state to BUSY */
483 heth
->State
= HAL_ETH_STATE_BUSY
;
485 /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
486 heth
->TxDesc
= DMATxDescTab
;
488 /* Fill each DMATxDesc descriptor with the right values */
489 for(i
=0; i
< TxBuffCount
; i
++)
491 /* Get the pointer on the ith member of the Tx Desc list */
492 dmatxdesc
= DMATxDescTab
+ i
;
494 /* Set Second Address Chained bit */
495 dmatxdesc
->Status
= ETH_DMATXDESC_TCH
;
497 /* Set Buffer1 address pointer */
498 dmatxdesc
->Buffer1Addr
= (uint32_t)(&TxBuff
[i
*ETH_TX_BUF_SIZE
]);
500 if ((heth
->Init
).ChecksumMode
== ETH_CHECKSUM_BY_HARDWARE
)
502 /* Set the DMA Tx descriptors checksum insertion */
503 dmatxdesc
->Status
|= ETH_DMATXDESC_CHECKSUMTCPUDPICMPFULL
;
506 /* Initialize the next descriptor with the Next Descriptor Polling Enable */
507 if(i
< (TxBuffCount
-1))
509 /* Set next descriptor address register with next descriptor base address */
510 dmatxdesc
->Buffer2NextDescAddr
= (uint32_t)(DMATxDescTab
+i
+1);
514 /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
515 dmatxdesc
->Buffer2NextDescAddr
= (uint32_t) DMATxDescTab
;
519 /* Set Transmit Descriptor List Address Register */
520 (heth
->Instance
)->DMATDLAR
= (uint32_t) DMATxDescTab
;
522 /* Set ETH HAL State to Ready */
523 heth
->State
= HAL_ETH_STATE_READY
;
525 /* Process Unlocked */
528 /* Return function status */
533 * @brief Initializes the DMA Rx descriptors in chain mode.
534 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
535 * the configuration information for ETHERNET module
536 * @param DMARxDescTab: Pointer to the first Rx desc list
537 * @param RxBuff: Pointer to the first RxBuffer list
538 * @param RxBuffCount: Number of the used Rx desc in the list
541 HAL_StatusTypeDef
HAL_ETH_DMARxDescListInit(ETH_HandleTypeDef
*heth
, ETH_DMADescTypeDef
*DMARxDescTab
, uint8_t *RxBuff
, uint32_t RxBuffCount
)
544 ETH_DMADescTypeDef
*DMARxDesc
;
549 /* Set the ETH peripheral state to BUSY */
550 heth
->State
= HAL_ETH_STATE_BUSY
;
552 /* Set the Ethernet RxDesc pointer with the first one of the DMARxDescTab list */
553 heth
->RxDesc
= DMARxDescTab
;
555 /* Fill each DMARxDesc descriptor with the right values */
556 for(i
=0; i
< RxBuffCount
; i
++)
558 /* Get the pointer on the ith member of the Rx Desc list */
559 DMARxDesc
= DMARxDescTab
+i
;
561 /* Set Own bit of the Rx descriptor Status */
562 DMARxDesc
->Status
= ETH_DMARXDESC_OWN
;
564 /* Set Buffer1 size and Second Address Chained bit */
565 DMARxDesc
->ControlBufferSize
= ETH_DMARXDESC_RCH
| ETH_RX_BUF_SIZE
;
567 /* Set Buffer1 address pointer */
568 DMARxDesc
->Buffer1Addr
= (uint32_t)(&RxBuff
[i
*ETH_RX_BUF_SIZE
]);
570 if((heth
->Init
).RxMode
== ETH_RXINTERRUPT_MODE
)
572 /* Enable Ethernet DMA Rx Descriptor interrupt */
573 DMARxDesc
->ControlBufferSize
&= ~ETH_DMARXDESC_DIC
;
576 /* Initialize the next descriptor with the Next Descriptor Polling Enable */
577 if(i
< (RxBuffCount
-1))
579 /* Set next descriptor address register with next descriptor base address */
580 DMARxDesc
->Buffer2NextDescAddr
= (uint32_t)(DMARxDescTab
+i
+1);
584 /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
585 DMARxDesc
->Buffer2NextDescAddr
= (uint32_t)(DMARxDescTab
);
589 /* Set Receive Descriptor List Address Register */
590 (heth
->Instance
)->DMARDLAR
= (uint32_t) DMARxDescTab
;
592 /* Set ETH HAL State to Ready */
593 heth
->State
= HAL_ETH_STATE_READY
;
595 /* Process Unlocked */
598 /* Return function status */
603 * @brief Initializes the ETH MSP.
604 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
605 * the configuration information for ETHERNET module
608 __weak
void HAL_ETH_MspInit(ETH_HandleTypeDef
*heth
)
610 /* Prevent unused argument(s) compilation warning */
613 /* NOTE : This function Should not be modified, when the callback is needed,
614 the HAL_ETH_MspInit could be implemented in the user file
619 * @brief DeInitializes ETH MSP.
620 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
621 * the configuration information for ETHERNET module
624 __weak
void HAL_ETH_MspDeInit(ETH_HandleTypeDef
*heth
)
626 /* Prevent unused argument(s) compilation warning */
629 /* NOTE : This function Should not be modified, when the callback is needed,
630 the HAL_ETH_MspDeInit could be implemented in the user file
638 /** @defgroup ETH_Exported_Functions_Group2 IO operation functions
639 * @brief Data transfers functions
642 ==============================================================================
643 ##### IO operation functions #####
644 ==============================================================================
645 [..] This section provides functions allowing to:
647 HAL_ETH_TransmitFrame();
649 HAL_ETH_GetReceivedFrame();
650 HAL_ETH_GetReceivedFrame_IT();
651 (+) Read from an External PHY register
652 HAL_ETH_ReadPHYRegister();
653 (+) Write to an External PHY register
654 HAL_ETH_WritePHYRegister();
662 * @brief Sends an Ethernet frame.
663 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
664 * the configuration information for ETHERNET module
665 * @param FrameLength: Amount of data to be sent
668 HAL_StatusTypeDef
HAL_ETH_TransmitFrame(ETH_HandleTypeDef
*heth
, uint32_t FrameLength
)
670 uint32_t bufcount
= 0, size
= 0, i
= 0;
675 /* Set the ETH peripheral state to BUSY */
676 heth
->State
= HAL_ETH_STATE_BUSY
;
678 if (FrameLength
== 0)
680 /* Set ETH HAL state to READY */
681 heth
->State
= HAL_ETH_STATE_READY
;
683 /* Process Unlocked */
689 /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
690 if(((heth
->TxDesc
)->Status
& ETH_DMATXDESC_OWN
) != (uint32_t)RESET
)
693 heth
->State
= HAL_ETH_STATE_BUSY_TX
;
695 /* Process Unlocked */
701 /* Get the number of needed Tx buffers for the current frame */
702 if (FrameLength
> ETH_TX_BUF_SIZE
)
704 bufcount
= FrameLength
/ETH_TX_BUF_SIZE
;
705 if (FrameLength
% ETH_TX_BUF_SIZE
)
716 /* Set LAST and FIRST segment */
717 heth
->TxDesc
->Status
|=ETH_DMATXDESC_FS
|ETH_DMATXDESC_LS
;
719 heth
->TxDesc
->ControlBufferSize
= (FrameLength
& ETH_DMATXDESC_TBS1
);
720 /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
721 heth
->TxDesc
->Status
|= ETH_DMATXDESC_OWN
;
722 /* Point to next descriptor */
723 heth
->TxDesc
= (ETH_DMADescTypeDef
*)(heth
->TxDesc
->Buffer2NextDescAddr
);
727 for (i
=0; i
< bufcount
; i
++)
729 /* Clear FIRST and LAST segment bits */
730 heth
->TxDesc
->Status
&= ~(ETH_DMATXDESC_FS
| ETH_DMATXDESC_LS
);
734 /* Setting the first segment bit */
735 heth
->TxDesc
->Status
|= ETH_DMATXDESC_FS
;
739 heth
->TxDesc
->ControlBufferSize
= (ETH_TX_BUF_SIZE
& ETH_DMATXDESC_TBS1
);
741 if (i
== (bufcount
-1))
743 /* Setting the last segment bit */
744 heth
->TxDesc
->Status
|= ETH_DMATXDESC_LS
;
745 size
= FrameLength
- (bufcount
-1)*ETH_TX_BUF_SIZE
;
746 heth
->TxDesc
->ControlBufferSize
= (size
& ETH_DMATXDESC_TBS1
);
749 /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
750 heth
->TxDesc
->Status
|= ETH_DMATXDESC_OWN
;
751 /* point to next descriptor */
752 heth
->TxDesc
= (ETH_DMADescTypeDef
*)(heth
->TxDesc
->Buffer2NextDescAddr
);
756 /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
757 if (((heth
->Instance
)->DMASR
& ETH_DMASR_TBUS
) != (uint32_t)RESET
)
759 /* Clear TBUS ETHERNET DMA flag */
760 (heth
->Instance
)->DMASR
= ETH_DMASR_TBUS
;
761 /* Resume DMA transmission*/
762 (heth
->Instance
)->DMATPDR
= 0;
765 /* Set ETH HAL State to Ready */
766 heth
->State
= HAL_ETH_STATE_READY
;
768 /* Process Unlocked */
771 /* Return function status */
776 * @brief Checks for received frames.
777 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
778 * the configuration information for ETHERNET module
781 HAL_StatusTypeDef
HAL_ETH_GetReceivedFrame(ETH_HandleTypeDef
*heth
)
783 uint32_t framelength
= 0;
788 /* Check the ETH state to BUSY */
789 heth
->State
= HAL_ETH_STATE_BUSY
;
791 /* Check if segment is not owned by DMA */
792 /* (((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET) && ((heth->RxDesc->Status & ETH_DMARXDESC_LS) != (uint32_t)RESET)) */
793 if(((heth
->RxDesc
->Status
& ETH_DMARXDESC_OWN
) == (uint32_t)RESET
))
795 /* Check if last segment */
796 if(((heth
->RxDesc
->Status
& ETH_DMARXDESC_LS
) != (uint32_t)RESET
))
798 /* increment segment count */
799 (heth
->RxFrameInfos
).SegCount
++;
801 /* Check if last segment is first segment: one segment contains the frame */
802 if ((heth
->RxFrameInfos
).SegCount
== 1)
804 (heth
->RxFrameInfos
).FSRxDesc
=heth
->RxDesc
;
807 heth
->RxFrameInfos
.LSRxDesc
= heth
->RxDesc
;
809 /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
810 framelength
= (((heth
->RxDesc
)->Status
& ETH_DMARXDESC_FL
) >> ETH_DMARXDESC_FRAMELENGTHSHIFT
) - 4;
811 heth
->RxFrameInfos
.length
= framelength
;
813 /* Get the address of the buffer start address */
814 heth
->RxFrameInfos
.buffer
= ((heth
->RxFrameInfos
).FSRxDesc
)->Buffer1Addr
;
815 /* point to next descriptor */
816 heth
->RxDesc
= (ETH_DMADescTypeDef
*) ((heth
->RxDesc
)->Buffer2NextDescAddr
);
818 /* Set HAL State to Ready */
819 heth
->State
= HAL_ETH_STATE_READY
;
821 /* Process Unlocked */
824 /* Return function status */
827 /* Check if first segment */
828 else if((heth
->RxDesc
->Status
& ETH_DMARXDESC_FS
) != (uint32_t)RESET
)
830 (heth
->RxFrameInfos
).FSRxDesc
= heth
->RxDesc
;
831 (heth
->RxFrameInfos
).LSRxDesc
= NULL
;
832 (heth
->RxFrameInfos
).SegCount
= 1;
833 /* Point to next descriptor */
834 heth
->RxDesc
= (ETH_DMADescTypeDef
*) (heth
->RxDesc
->Buffer2NextDescAddr
);
836 /* Check if intermediate segment */
839 (heth
->RxFrameInfos
).SegCount
++;
840 /* Point to next descriptor */
841 heth
->RxDesc
= (ETH_DMADescTypeDef
*) (heth
->RxDesc
->Buffer2NextDescAddr
);
845 /* Set ETH HAL State to Ready */
846 heth
->State
= HAL_ETH_STATE_READY
;
848 /* Process Unlocked */
851 /* Return function status */
856 * @brief Gets the Received frame in interrupt mode.
857 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
858 * the configuration information for ETHERNET module
861 HAL_StatusTypeDef
HAL_ETH_GetReceivedFrame_IT(ETH_HandleTypeDef
*heth
)
863 uint32_t descriptorscancounter
= 0;
868 /* Set ETH HAL State to BUSY */
869 heth
->State
= HAL_ETH_STATE_BUSY
;
871 /* Scan descriptors owned by CPU */
872 while (((heth
->RxDesc
->Status
& ETH_DMARXDESC_OWN
) == (uint32_t)RESET
) && (descriptorscancounter
< ETH_RXBUFNB
))
874 /* Just for security */
875 descriptorscancounter
++;
877 /* Check if first segment in frame */
878 /* ((heth->RxDesc->Status & ETH_DMARXDESC_FS) != (uint32_t)RESET) && ((heth->RxDesc->Status & ETH_DMARXDESC_LS) == (uint32_t)RESET)) */
879 if((heth
->RxDesc
->Status
& (ETH_DMARXDESC_FS
| ETH_DMARXDESC_LS
)) == (uint32_t)ETH_DMARXDESC_FS
)
881 heth
->RxFrameInfos
.FSRxDesc
= heth
->RxDesc
;
882 heth
->RxFrameInfos
.SegCount
= 1;
883 /* Point to next descriptor */
884 heth
->RxDesc
= (ETH_DMADescTypeDef
*) (heth
->RxDesc
->Buffer2NextDescAddr
);
886 /* Check if intermediate segment */
887 /* ((heth->RxDesc->Status & ETH_DMARXDESC_LS) == (uint32_t)RESET)&& ((heth->RxDesc->Status & ETH_DMARXDESC_FS) == (uint32_t)RESET)) */
888 else if ((heth
->RxDesc
->Status
& (ETH_DMARXDESC_LS
| ETH_DMARXDESC_FS
)) == (uint32_t)RESET
)
890 /* Increment segment count */
891 (heth
->RxFrameInfos
.SegCount
)++;
892 /* Point to next descriptor */
893 heth
->RxDesc
= (ETH_DMADescTypeDef
*)(heth
->RxDesc
->Buffer2NextDescAddr
);
895 /* Should be last segment */
899 heth
->RxFrameInfos
.LSRxDesc
= heth
->RxDesc
;
901 /* Increment segment count */
902 (heth
->RxFrameInfos
.SegCount
)++;
904 /* Check if last segment is first segment: one segment contains the frame */
905 if ((heth
->RxFrameInfos
.SegCount
) == 1)
907 heth
->RxFrameInfos
.FSRxDesc
= heth
->RxDesc
;
910 /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
911 heth
->RxFrameInfos
.length
= (((heth
->RxDesc
)->Status
& ETH_DMARXDESC_FL
) >> ETH_DMARXDESC_FRAMELENGTHSHIFT
) - 4;
913 /* Get the address of the buffer start address */
914 heth
->RxFrameInfos
.buffer
=((heth
->RxFrameInfos
).FSRxDesc
)->Buffer1Addr
;
916 /* Point to next descriptor */
917 heth
->RxDesc
= (ETH_DMADescTypeDef
*) (heth
->RxDesc
->Buffer2NextDescAddr
);
919 /* Set HAL State to Ready */
920 heth
->State
= HAL_ETH_STATE_READY
;
922 /* Process Unlocked */
925 /* Return function status */
930 /* Set HAL State to Ready */
931 heth
->State
= HAL_ETH_STATE_READY
;
933 /* Process Unlocked */
936 /* Return function status */
941 * @brief This function handles ETH interrupt request.
942 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
943 * the configuration information for ETHERNET module
946 void HAL_ETH_IRQHandler(ETH_HandleTypeDef
*heth
)
949 if (__HAL_ETH_DMA_GET_FLAG(heth
, ETH_DMA_FLAG_R
))
951 /* Receive complete callback */
952 HAL_ETH_RxCpltCallback(heth
);
954 /* Clear the Eth DMA Rx IT pending bits */
955 __HAL_ETH_DMA_CLEAR_IT(heth
, ETH_DMA_IT_R
);
957 /* Set HAL State to Ready */
958 heth
->State
= HAL_ETH_STATE_READY
;
960 /* Process Unlocked */
964 /* Frame transmitted */
965 else if (__HAL_ETH_DMA_GET_FLAG(heth
, ETH_DMA_FLAG_T
))
967 /* Transfer complete callback */
968 HAL_ETH_TxCpltCallback(heth
);
970 /* Clear the Eth DMA Tx IT pending bits */
971 __HAL_ETH_DMA_CLEAR_IT(heth
, ETH_DMA_IT_T
);
973 /* Set HAL State to Ready */
974 heth
->State
= HAL_ETH_STATE_READY
;
976 /* Process Unlocked */
980 /* Clear the interrupt flags */
981 __HAL_ETH_DMA_CLEAR_IT(heth
, ETH_DMA_IT_NIS
);
984 if(__HAL_ETH_DMA_GET_FLAG(heth
, ETH_DMA_FLAG_AIS
))
986 /* Ethernet Error callback */
987 HAL_ETH_ErrorCallback(heth
);
989 /* Clear the interrupt flags */
990 __HAL_ETH_DMA_CLEAR_IT(heth
, ETH_DMA_FLAG_AIS
);
992 /* Set HAL State to Ready */
993 heth
->State
= HAL_ETH_STATE_READY
;
995 /* Process Unlocked */
1001 * @brief Tx Transfer completed callbacks.
1002 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1003 * the configuration information for ETHERNET module
1006 __weak
void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef
*heth
)
1008 /* Prevent unused argument(s) compilation warning */
1011 /* NOTE : This function Should not be modified, when the callback is needed,
1012 the HAL_ETH_TxCpltCallback could be implemented in the user file
1017 * @brief Rx Transfer completed callbacks.
1018 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1019 * the configuration information for ETHERNET module
1022 __weak
void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef
*heth
)
1024 /* Prevent unused argument(s) compilation warning */
1027 /* NOTE : This function Should not be modified, when the callback is needed,
1028 the HAL_ETH_TxCpltCallback could be implemented in the user file
1033 * @brief Ethernet transfer error callbacks
1034 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1035 * the configuration information for ETHERNET module
1038 __weak
void HAL_ETH_ErrorCallback(ETH_HandleTypeDef
*heth
)
1040 /* Prevent unused argument(s) compilation warning */
1043 /* NOTE : This function Should not be modified, when the callback is needed,
1044 the HAL_ETH_TxCpltCallback could be implemented in the user file
1049 * @brief Reads a PHY register
1050 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1051 * the configuration information for ETHERNET module
1052 * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
1053 * This parameter can be one of the following values:
1054 * PHY_BCR: Transceiver Basic Control Register,
1055 * PHY_BSR: Transceiver Basic Status Register.
1056 * More PHY register could be read depending on the used PHY
1057 * @param RegValue: PHY register value
1058 * @retval HAL status
1060 HAL_StatusTypeDef
HAL_ETH_ReadPHYRegister(ETH_HandleTypeDef
*heth
, uint16_t PHYReg
, uint32_t *RegValue
)
1062 uint32_t tmpreg
= 0;
1063 uint32_t tickstart
= 0;
1065 /* Check parameters */
1066 assert_param(IS_ETH_PHY_ADDRESS(heth
->Init
.PhyAddress
));
1068 /* Check the ETH peripheral state */
1069 if(heth
->State
== HAL_ETH_STATE_BUSY_RD
)
1073 /* Set ETH HAL State to BUSY_RD */
1074 heth
->State
= HAL_ETH_STATE_BUSY_RD
;
1076 /* Get the ETHERNET MACMIIAR value */
1077 tmpreg
= heth
->Instance
->MACMIIAR
;
1079 /* Keep only the CSR Clock Range CR[2:0] bits value */
1080 tmpreg
&= ~ETH_MACMIIAR_CR_MASK
;
1082 /* Prepare the MII address register value */
1083 tmpreg
|=(((uint32_t)heth
->Init
.PhyAddress
<< 11) & ETH_MACMIIAR_PA
); /* Set the PHY device address */
1084 tmpreg
|=(((uint32_t)PHYReg
<<6) & ETH_MACMIIAR_MR
); /* Set the PHY register address */
1085 tmpreg
&= ~ETH_MACMIIAR_MW
; /* Set the read mode */
1086 tmpreg
|= ETH_MACMIIAR_MB
; /* Set the MII Busy bit */
1088 /* Write the result value into the MII Address register */
1089 heth
->Instance
->MACMIIAR
= tmpreg
;
1092 tickstart
= HAL_GetTick();
1094 /* Check for the Busy flag */
1095 while((tmpreg
& ETH_MACMIIAR_MB
) == ETH_MACMIIAR_MB
)
1097 /* Check for the Timeout */
1098 if((HAL_GetTick() - tickstart
) > PHY_READ_TO
)
1100 heth
->State
= HAL_ETH_STATE_READY
;
1102 /* Process Unlocked */
1108 tmpreg
= heth
->Instance
->MACMIIAR
;
1111 /* Get MACMIIDR value */
1112 *RegValue
= (uint16_t)(heth
->Instance
->MACMIIDR
);
1114 /* Set ETH HAL State to READY */
1115 heth
->State
= HAL_ETH_STATE_READY
;
1117 /* Return function status */
1122 * @brief Writes to a PHY register.
1123 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1124 * the configuration information for ETHERNET module
1125 * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
1126 * This parameter can be one of the following values:
1127 * PHY_BCR: Transceiver Control Register.
1128 * More PHY register could be written depending on the used PHY
1129 * @param RegValue: the value to write
1130 * @retval HAL status
1132 HAL_StatusTypeDef
HAL_ETH_WritePHYRegister(ETH_HandleTypeDef
*heth
, uint16_t PHYReg
, uint32_t RegValue
)
1134 uint32_t tmpreg
= 0;
1135 uint32_t tickstart
= 0;
1137 /* Check parameters */
1138 assert_param(IS_ETH_PHY_ADDRESS(heth
->Init
.PhyAddress
));
1140 /* Check the ETH peripheral state */
1141 if(heth
->State
== HAL_ETH_STATE_BUSY_WR
)
1145 /* Set ETH HAL State to BUSY_WR */
1146 heth
->State
= HAL_ETH_STATE_BUSY_WR
;
1148 /* Get the ETHERNET MACMIIAR value */
1149 tmpreg
= heth
->Instance
->MACMIIAR
;
1151 /* Keep only the CSR Clock Range CR[2:0] bits value */
1152 tmpreg
&= ~ETH_MACMIIAR_CR_MASK
;
1154 /* Prepare the MII register address value */
1155 tmpreg
|=(((uint32_t)heth
->Init
.PhyAddress
<<11) & ETH_MACMIIAR_PA
); /* Set the PHY device address */
1156 tmpreg
|=(((uint32_t)PHYReg
<<6) & ETH_MACMIIAR_MR
); /* Set the PHY register address */
1157 tmpreg
|= ETH_MACMIIAR_MW
; /* Set the write mode */
1158 tmpreg
|= ETH_MACMIIAR_MB
; /* Set the MII Busy bit */
1160 /* Give the value to the MII data register */
1161 heth
->Instance
->MACMIIDR
= (uint16_t)RegValue
;
1163 /* Write the result value into the MII Address register */
1164 heth
->Instance
->MACMIIAR
= tmpreg
;
1167 tickstart
= HAL_GetTick();
1169 /* Check for the Busy flag */
1170 while((tmpreg
& ETH_MACMIIAR_MB
) == ETH_MACMIIAR_MB
)
1172 /* Check for the Timeout */
1173 if((HAL_GetTick() - tickstart
) > PHY_WRITE_TO
)
1175 heth
->State
= HAL_ETH_STATE_READY
;
1177 /* Process Unlocked */
1183 tmpreg
= heth
->Instance
->MACMIIAR
;
1186 /* Set ETH HAL State to READY */
1187 heth
->State
= HAL_ETH_STATE_READY
;
1189 /* Return function status */
1197 /** @defgroup ETH_Exported_Functions_Group3 Peripheral Control functions
1198 * @brief Peripheral Control functions
1201 ===============================================================================
1202 ##### Peripheral Control functions #####
1203 ===============================================================================
1204 [..] This section provides functions allowing to:
1205 (+) Enable MAC and DMA transmission and reception.
1207 (+) Disable MAC and DMA transmission and reception.
1209 (+) Set the MAC configuration in runtime mode
1210 HAL_ETH_ConfigMAC();
1211 (+) Set the DMA configuration in runtime mode
1212 HAL_ETH_ConfigDMA();
1219 * @brief Enables Ethernet MAC and DMA reception/transmission
1220 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1221 * the configuration information for ETHERNET module
1222 * @retval HAL status
1224 HAL_StatusTypeDef
HAL_ETH_Start(ETH_HandleTypeDef
*heth
)
1226 /* Process Locked */
1229 /* Set the ETH peripheral state to BUSY */
1230 heth
->State
= HAL_ETH_STATE_BUSY
;
1232 /* Enable transmit state machine of the MAC for transmission on the MII */
1233 ETH_MACTransmissionEnable(heth
);
1235 /* Enable receive state machine of the MAC for reception from the MII */
1236 ETH_MACReceptionEnable(heth
);
1238 /* Flush Transmit FIFO */
1239 ETH_FlushTransmitFIFO(heth
);
1241 /* Start DMA transmission */
1242 ETH_DMATransmissionEnable(heth
);
1244 /* Start DMA reception */
1245 ETH_DMAReceptionEnable(heth
);
1247 /* Set the ETH state to READY*/
1248 heth
->State
= HAL_ETH_STATE_READY
;
1250 /* Process Unlocked */
1253 /* Return function status */
1258 * @brief Stop Ethernet MAC and DMA reception/transmission
1259 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1260 * the configuration information for ETHERNET module
1261 * @retval HAL status
1263 HAL_StatusTypeDef
HAL_ETH_Stop(ETH_HandleTypeDef
*heth
)
1265 /* Process Locked */
1268 /* Set the ETH peripheral state to BUSY */
1269 heth
->State
= HAL_ETH_STATE_BUSY
;
1271 /* Stop DMA transmission */
1272 ETH_DMATransmissionDisable(heth
);
1274 /* Stop DMA reception */
1275 ETH_DMAReceptionDisable(heth
);
1277 /* Disable receive state machine of the MAC for reception from the MII */
1278 ETH_MACReceptionDisable(heth
);
1280 /* Flush Transmit FIFO */
1281 ETH_FlushTransmitFIFO(heth
);
1283 /* Disable transmit state machine of the MAC for transmission on the MII */
1284 ETH_MACTransmissionDisable(heth
);
1286 /* Set the ETH state*/
1287 heth
->State
= HAL_ETH_STATE_READY
;
1289 /* Process Unlocked */
1292 /* Return function status */
1297 * @brief Set ETH MAC Configuration.
1298 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1299 * the configuration information for ETHERNET module
1300 * @param macconf: MAC Configuration structure
1301 * @retval HAL status
1303 HAL_StatusTypeDef
HAL_ETH_ConfigMAC(ETH_HandleTypeDef
*heth
, ETH_MACInitTypeDef
*macconf
)
1305 uint32_t tmpreg
= 0;
1307 /* Process Locked */
1310 /* Set the ETH peripheral state to BUSY */
1311 heth
->State
= HAL_ETH_STATE_BUSY
;
1313 assert_param(IS_ETH_SPEED(heth
->Init
.Speed
));
1314 assert_param(IS_ETH_DUPLEX_MODE(heth
->Init
.DuplexMode
));
1316 if (macconf
!= NULL
)
1318 /* Check the parameters */
1319 assert_param(IS_ETH_WATCHDOG(macconf
->Watchdog
));
1320 assert_param(IS_ETH_JABBER(macconf
->Jabber
));
1321 assert_param(IS_ETH_INTER_FRAME_GAP(macconf
->InterFrameGap
));
1322 assert_param(IS_ETH_CARRIER_SENSE(macconf
->CarrierSense
));
1323 assert_param(IS_ETH_RECEIVE_OWN(macconf
->ReceiveOwn
));
1324 assert_param(IS_ETH_LOOPBACK_MODE(macconf
->LoopbackMode
));
1325 assert_param(IS_ETH_CHECKSUM_OFFLOAD(macconf
->ChecksumOffload
));
1326 assert_param(IS_ETH_RETRY_TRANSMISSION(macconf
->RetryTransmission
));
1327 assert_param(IS_ETH_AUTOMATIC_PADCRC_STRIP(macconf
->AutomaticPadCRCStrip
));
1328 assert_param(IS_ETH_BACKOFF_LIMIT(macconf
->BackOffLimit
));
1329 assert_param(IS_ETH_DEFERRAL_CHECK(macconf
->DeferralCheck
));
1330 assert_param(IS_ETH_RECEIVE_ALL(macconf
->ReceiveAll
));
1331 assert_param(IS_ETH_SOURCE_ADDR_FILTER(macconf
->SourceAddrFilter
));
1332 assert_param(IS_ETH_CONTROL_FRAMES(macconf
->PassControlFrames
));
1333 assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(macconf
->BroadcastFramesReception
));
1334 assert_param(IS_ETH_DESTINATION_ADDR_FILTER(macconf
->DestinationAddrFilter
));
1335 assert_param(IS_ETH_PROMISCUOUS_MODE(macconf
->PromiscuousMode
));
1336 assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(macconf
->MulticastFramesFilter
));
1337 assert_param(IS_ETH_UNICAST_FRAMES_FILTER(macconf
->UnicastFramesFilter
));
1338 assert_param(IS_ETH_PAUSE_TIME(macconf
->PauseTime
));
1339 assert_param(IS_ETH_ZEROQUANTA_PAUSE(macconf
->ZeroQuantaPause
));
1340 assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(macconf
->PauseLowThreshold
));
1341 assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(macconf
->UnicastPauseFrameDetect
));
1342 assert_param(IS_ETH_RECEIVE_FLOWCONTROL(macconf
->ReceiveFlowControl
));
1343 assert_param(IS_ETH_TRANSMIT_FLOWCONTROL(macconf
->TransmitFlowControl
));
1344 assert_param(IS_ETH_VLAN_TAG_COMPARISON(macconf
->VLANTagComparison
));
1345 assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(macconf
->VLANTagIdentifier
));
1347 /*------------------------ ETHERNET MACCR Configuration --------------------*/
1348 /* Get the ETHERNET MACCR value */
1349 tmpreg
= (heth
->Instance
)->MACCR
;
1350 /* Clear WD, PCE, PS, TE and RE bits */
1351 tmpreg
&= ETH_MACCR_CLEAR_MASK
;
1353 tmpreg
|= (uint32_t)(macconf
->Watchdog
|
1355 macconf
->InterFrameGap
|
1356 macconf
->CarrierSense
|
1357 (heth
->Init
).Speed
|
1358 macconf
->ReceiveOwn
|
1359 macconf
->LoopbackMode
|
1360 (heth
->Init
).DuplexMode
|
1361 macconf
->ChecksumOffload
|
1362 macconf
->RetryTransmission
|
1363 macconf
->AutomaticPadCRCStrip
|
1364 macconf
->BackOffLimit
|
1365 macconf
->DeferralCheck
);
1367 /* Write to ETHERNET MACCR */
1368 (heth
->Instance
)->MACCR
= (uint32_t)tmpreg
;
1370 /* Wait until the write operation will be taken into account :
1371 at least four TX_CLK/RX_CLK clock cycles */
1372 tmpreg
= (heth
->Instance
)->MACCR
;
1373 HAL_Delay(ETH_REG_WRITE_DELAY
);
1374 (heth
->Instance
)->MACCR
= tmpreg
;
1376 /*----------------------- ETHERNET MACFFR Configuration --------------------*/
1377 /* Write to ETHERNET MACFFR */
1378 (heth
->Instance
)->MACFFR
= (uint32_t)(macconf
->ReceiveAll
|
1379 macconf
->SourceAddrFilter
|
1380 macconf
->PassControlFrames
|
1381 macconf
->BroadcastFramesReception
|
1382 macconf
->DestinationAddrFilter
|
1383 macconf
->PromiscuousMode
|
1384 macconf
->MulticastFramesFilter
|
1385 macconf
->UnicastFramesFilter
);
1387 /* Wait until the write operation will be taken into account :
1388 at least four TX_CLK/RX_CLK clock cycles */
1389 tmpreg
= (heth
->Instance
)->MACFFR
;
1390 HAL_Delay(ETH_REG_WRITE_DELAY
);
1391 (heth
->Instance
)->MACFFR
= tmpreg
;
1393 /*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
1394 /* Write to ETHERNET MACHTHR */
1395 (heth
->Instance
)->MACHTHR
= (uint32_t)macconf
->HashTableHigh
;
1397 /* Write to ETHERNET MACHTLR */
1398 (heth
->Instance
)->MACHTLR
= (uint32_t)macconf
->HashTableLow
;
1399 /*----------------------- ETHERNET MACFCR Configuration --------------------*/
1401 /* Get the ETHERNET MACFCR value */
1402 tmpreg
= (heth
->Instance
)->MACFCR
;
1404 tmpreg
&= ETH_MACFCR_CLEAR_MASK
;
1406 tmpreg
|= (uint32_t)((macconf
->PauseTime
<< 16) |
1407 macconf
->ZeroQuantaPause
|
1408 macconf
->PauseLowThreshold
|
1409 macconf
->UnicastPauseFrameDetect
|
1410 macconf
->ReceiveFlowControl
|
1411 macconf
->TransmitFlowControl
);
1413 /* Write to ETHERNET MACFCR */
1414 (heth
->Instance
)->MACFCR
= (uint32_t)tmpreg
;
1416 /* Wait until the write operation will be taken into account :
1417 at least four TX_CLK/RX_CLK clock cycles */
1418 tmpreg
= (heth
->Instance
)->MACFCR
;
1419 HAL_Delay(ETH_REG_WRITE_DELAY
);
1420 (heth
->Instance
)->MACFCR
= tmpreg
;
1422 /*----------------------- ETHERNET MACVLANTR Configuration -----------------*/
1423 (heth
->Instance
)->MACVLANTR
= (uint32_t)(macconf
->VLANTagComparison
|
1424 macconf
->VLANTagIdentifier
);
1426 /* Wait until the write operation will be taken into account :
1427 at least four TX_CLK/RX_CLK clock cycles */
1428 tmpreg
= (heth
->Instance
)->MACVLANTR
;
1429 HAL_Delay(ETH_REG_WRITE_DELAY
);
1430 (heth
->Instance
)->MACVLANTR
= tmpreg
;
1432 else /* macconf == NULL : here we just configure Speed and Duplex mode */
1434 /*------------------------ ETHERNET MACCR Configuration --------------------*/
1435 /* Get the ETHERNET MACCR value */
1436 tmpreg
= (heth
->Instance
)->MACCR
;
1438 /* Clear FES and DM bits */
1439 tmpreg
&= ~((uint32_t)0x00004800);
1441 tmpreg
|= (uint32_t)(heth
->Init
.Speed
| heth
->Init
.DuplexMode
);
1443 /* Write to ETHERNET MACCR */
1444 (heth
->Instance
)->MACCR
= (uint32_t)tmpreg
;
1446 /* Wait until the write operation will be taken into account:
1447 at least four TX_CLK/RX_CLK clock cycles */
1448 tmpreg
= (heth
->Instance
)->MACCR
;
1449 HAL_Delay(ETH_REG_WRITE_DELAY
);
1450 (heth
->Instance
)->MACCR
= tmpreg
;
1453 /* Set the ETH state to Ready */
1454 heth
->State
= HAL_ETH_STATE_READY
;
1456 /* Process Unlocked */
1459 /* Return function status */
1464 * @brief Sets ETH DMA Configuration.
1465 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1466 * the configuration information for ETHERNET module
1467 * @param dmaconf: DMA Configuration structure
1468 * @retval HAL status
1470 HAL_StatusTypeDef
HAL_ETH_ConfigDMA(ETH_HandleTypeDef
*heth
, ETH_DMAInitTypeDef
*dmaconf
)
1472 uint32_t tmpreg
= 0;
1474 /* Process Locked */
1477 /* Set the ETH peripheral state to BUSY */
1478 heth
->State
= HAL_ETH_STATE_BUSY
;
1480 /* Check parameters */
1481 assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(dmaconf
->DropTCPIPChecksumErrorFrame
));
1482 assert_param(IS_ETH_RECEIVE_STORE_FORWARD(dmaconf
->ReceiveStoreForward
));
1483 assert_param(IS_ETH_FLUSH_RECEIVE_FRAME(dmaconf
->FlushReceivedFrame
));
1484 assert_param(IS_ETH_TRANSMIT_STORE_FORWARD(dmaconf
->TransmitStoreForward
));
1485 assert_param(IS_ETH_TRANSMIT_THRESHOLD_CONTROL(dmaconf
->TransmitThresholdControl
));
1486 assert_param(IS_ETH_FORWARD_ERROR_FRAMES(dmaconf
->ForwardErrorFrames
));
1487 assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(dmaconf
->ForwardUndersizedGoodFrames
));
1488 assert_param(IS_ETH_RECEIVE_THRESHOLD_CONTROL(dmaconf
->ReceiveThresholdControl
));
1489 assert_param(IS_ETH_SECOND_FRAME_OPERATE(dmaconf
->SecondFrameOperate
));
1490 assert_param(IS_ETH_ADDRESS_ALIGNED_BEATS(dmaconf
->AddressAlignedBeats
));
1491 assert_param(IS_ETH_FIXED_BURST(dmaconf
->FixedBurst
));
1492 assert_param(IS_ETH_RXDMA_BURST_LENGTH(dmaconf
->RxDMABurstLength
));
1493 assert_param(IS_ETH_TXDMA_BURST_LENGTH(dmaconf
->TxDMABurstLength
));
1494 assert_param(IS_ETH_ENHANCED_DESCRIPTOR_FORMAT(dmaconf
->EnhancedDescriptorFormat
));
1495 assert_param(IS_ETH_DMA_DESC_SKIP_LENGTH(dmaconf
->DescriptorSkipLength
));
1496 assert_param(IS_ETH_DMA_ARBITRATION_ROUNDROBIN_RXTX(dmaconf
->DMAArbitration
));
1498 /*----------------------- ETHERNET DMAOMR Configuration --------------------*/
1499 /* Get the ETHERNET DMAOMR value */
1500 tmpreg
= (heth
->Instance
)->DMAOMR
;
1502 tmpreg
&= ETH_DMAOMR_CLEAR_MASK
;
1504 tmpreg
|= (uint32_t)(dmaconf
->DropTCPIPChecksumErrorFrame
|
1505 dmaconf
->ReceiveStoreForward
|
1506 dmaconf
->FlushReceivedFrame
|
1507 dmaconf
->TransmitStoreForward
|
1508 dmaconf
->TransmitThresholdControl
|
1509 dmaconf
->ForwardErrorFrames
|
1510 dmaconf
->ForwardUndersizedGoodFrames
|
1511 dmaconf
->ReceiveThresholdControl
|
1512 dmaconf
->SecondFrameOperate
);
1514 /* Write to ETHERNET DMAOMR */
1515 (heth
->Instance
)->DMAOMR
= (uint32_t)tmpreg
;
1517 /* Wait until the write operation will be taken into account:
1518 at least four TX_CLK/RX_CLK clock cycles */
1519 tmpreg
= (heth
->Instance
)->DMAOMR
;
1520 HAL_Delay(ETH_REG_WRITE_DELAY
);
1521 (heth
->Instance
)->DMAOMR
= tmpreg
;
1523 /*----------------------- ETHERNET DMABMR Configuration --------------------*/
1524 (heth
->Instance
)->DMABMR
= (uint32_t)(dmaconf
->AddressAlignedBeats
|
1525 dmaconf
->FixedBurst
|
1526 dmaconf
->RxDMABurstLength
| /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
1527 dmaconf
->TxDMABurstLength
|
1528 dmaconf
->EnhancedDescriptorFormat
|
1529 (dmaconf
->DescriptorSkipLength
<< 2) |
1530 dmaconf
->DMAArbitration
|
1531 ETH_DMABMR_USP
); /* Enable use of separate PBL for Rx and Tx */
1533 /* Wait until the write operation will be taken into account:
1534 at least four TX_CLK/RX_CLK clock cycles */
1535 tmpreg
= (heth
->Instance
)->DMABMR
;
1536 HAL_Delay(ETH_REG_WRITE_DELAY
);
1537 (heth
->Instance
)->DMABMR
= tmpreg
;
1539 /* Set the ETH state to Ready */
1540 heth
->State
= HAL_ETH_STATE_READY
;
1542 /* Process Unlocked */
1545 /* Return function status */
1553 /** @defgroup ETH_Exported_Functions_Group4 Peripheral State functions
1554 * @brief Peripheral State functions
1557 ===============================================================================
1558 ##### Peripheral State functions #####
1559 ===============================================================================
1561 This subsection permits to get in run-time the status of the peripheral
1563 (+) Get the ETH handle state:
1572 * @brief Return the ETH HAL state
1573 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1574 * the configuration information for ETHERNET module
1577 HAL_ETH_StateTypeDef
HAL_ETH_GetState(ETH_HandleTypeDef
*heth
)
1579 /* Return ETH state */
1591 /** @addtogroup ETH_Private_Functions
1596 * @brief Configures Ethernet MAC and DMA with default parameters.
1597 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1598 * the configuration information for ETHERNET module
1599 * @param err: Ethernet Init error
1600 * @retval HAL status
1602 static void ETH_MACDMAConfig(ETH_HandleTypeDef
*heth
, uint32_t err
)
1604 ETH_MACInitTypeDef macinit
;
1605 ETH_DMAInitTypeDef dmainit
;
1606 uint32_t tmpreg
= 0;
1608 if (err
!= ETH_SUCCESS
) /* Auto-negotiation failed */
1610 /* Set Ethernet duplex mode to Full-duplex */
1611 (heth
->Init
).DuplexMode
= ETH_MODE_FULLDUPLEX
;
1613 /* Set Ethernet speed to 100M */
1614 (heth
->Init
).Speed
= ETH_SPEED_100M
;
1617 /* Ethernet MAC default initialization **************************************/
1618 macinit
.Watchdog
= ETH_WATCHDOG_ENABLE
;
1619 macinit
.Jabber
= ETH_JABBER_ENABLE
;
1620 macinit
.InterFrameGap
= ETH_INTERFRAMEGAP_96BIT
;
1621 macinit
.CarrierSense
= ETH_CARRIERSENCE_ENABLE
;
1622 macinit
.ReceiveOwn
= ETH_RECEIVEOWN_ENABLE
;
1623 macinit
.LoopbackMode
= ETH_LOOPBACKMODE_DISABLE
;
1624 if(heth
->Init
.ChecksumMode
== ETH_CHECKSUM_BY_HARDWARE
)
1626 macinit
.ChecksumOffload
= ETH_CHECKSUMOFFLAOD_ENABLE
;
1630 macinit
.ChecksumOffload
= ETH_CHECKSUMOFFLAOD_DISABLE
;
1632 macinit
.RetryTransmission
= ETH_RETRYTRANSMISSION_DISABLE
;
1633 macinit
.AutomaticPadCRCStrip
= ETH_AUTOMATICPADCRCSTRIP_DISABLE
;
1634 macinit
.BackOffLimit
= ETH_BACKOFFLIMIT_10
;
1635 macinit
.DeferralCheck
= ETH_DEFFERRALCHECK_DISABLE
;
1636 macinit
.ReceiveAll
= ETH_RECEIVEAll_DISABLE
;
1637 macinit
.SourceAddrFilter
= ETH_SOURCEADDRFILTER_DISABLE
;
1638 macinit
.PassControlFrames
= ETH_PASSCONTROLFRAMES_BLOCKALL
;
1639 macinit
.BroadcastFramesReception
= ETH_BROADCASTFRAMESRECEPTION_ENABLE
;
1640 macinit
.DestinationAddrFilter
= ETH_DESTINATIONADDRFILTER_NORMAL
;
1641 macinit
.PromiscuousMode
= ETH_PROMISCUOUS_MODE_DISABLE
;
1642 macinit
.MulticastFramesFilter
= ETH_MULTICASTFRAMESFILTER_PERFECT
;
1643 macinit
.UnicastFramesFilter
= ETH_UNICASTFRAMESFILTER_PERFECT
;
1644 macinit
.HashTableHigh
= 0x0;
1645 macinit
.HashTableLow
= 0x0;
1646 macinit
.PauseTime
= 0x0;
1647 macinit
.ZeroQuantaPause
= ETH_ZEROQUANTAPAUSE_DISABLE
;
1648 macinit
.PauseLowThreshold
= ETH_PAUSELOWTHRESHOLD_MINUS4
;
1649 macinit
.UnicastPauseFrameDetect
= ETH_UNICASTPAUSEFRAMEDETECT_DISABLE
;
1650 macinit
.ReceiveFlowControl
= ETH_RECEIVEFLOWCONTROL_DISABLE
;
1651 macinit
.TransmitFlowControl
= ETH_TRANSMITFLOWCONTROL_DISABLE
;
1652 macinit
.VLANTagComparison
= ETH_VLANTAGCOMPARISON_16BIT
;
1653 macinit
.VLANTagIdentifier
= 0x0;
1655 /*------------------------ ETHERNET MACCR Configuration --------------------*/
1656 /* Get the ETHERNET MACCR value */
1657 tmpreg
= (heth
->Instance
)->MACCR
;
1658 /* Clear WD, PCE, PS, TE and RE bits */
1659 tmpreg
&= ETH_MACCR_CLEAR_MASK
;
1660 /* Set the WD bit according to ETH Watchdog value */
1661 /* Set the JD: bit according to ETH Jabber value */
1662 /* Set the IFG bit according to ETH InterFrameGap value */
1663 /* Set the DCRS bit according to ETH CarrierSense value */
1664 /* Set the FES bit according to ETH Speed value */
1665 /* Set the DO bit according to ETH ReceiveOwn value */
1666 /* Set the LM bit according to ETH LoopbackMode value */
1667 /* Set the DM bit according to ETH Mode value */
1668 /* Set the IPCO bit according to ETH ChecksumOffload value */
1669 /* Set the DR bit according to ETH RetryTransmission value */
1670 /* Set the ACS bit according to ETH AutomaticPadCRCStrip value */
1671 /* Set the BL bit according to ETH BackOffLimit value */
1672 /* Set the DC bit according to ETH DeferralCheck value */
1673 tmpreg
|= (uint32_t)(macinit
.Watchdog
|
1675 macinit
.InterFrameGap
|
1676 macinit
.CarrierSense
|
1677 (heth
->Init
).Speed
|
1678 macinit
.ReceiveOwn
|
1679 macinit
.LoopbackMode
|
1680 (heth
->Init
).DuplexMode
|
1681 macinit
.ChecksumOffload
|
1682 macinit
.RetryTransmission
|
1683 macinit
.AutomaticPadCRCStrip
|
1684 macinit
.BackOffLimit
|
1685 macinit
.DeferralCheck
);
1687 /* Write to ETHERNET MACCR */
1688 (heth
->Instance
)->MACCR
= (uint32_t)tmpreg
;
1690 /* Wait until the write operation will be taken into account:
1691 at least four TX_CLK/RX_CLK clock cycles */
1692 tmpreg
= (heth
->Instance
)->MACCR
;
1693 HAL_Delay(ETH_REG_WRITE_DELAY
);
1694 (heth
->Instance
)->MACCR
= tmpreg
;
1696 /*----------------------- ETHERNET MACFFR Configuration --------------------*/
1697 /* Set the RA bit according to ETH ReceiveAll value */
1698 /* Set the SAF and SAIF bits according to ETH SourceAddrFilter value */
1699 /* Set the PCF bit according to ETH PassControlFrames value */
1700 /* Set the DBF bit according to ETH BroadcastFramesReception value */
1701 /* Set the DAIF bit according to ETH DestinationAddrFilter value */
1702 /* Set the PR bit according to ETH PromiscuousMode value */
1703 /* Set the PM, HMC and HPF bits according to ETH MulticastFramesFilter value */
1704 /* Set the HUC and HPF bits according to ETH UnicastFramesFilter value */
1705 /* Write to ETHERNET MACFFR */
1706 (heth
->Instance
)->MACFFR
= (uint32_t)(macinit
.ReceiveAll
|
1707 macinit
.SourceAddrFilter
|
1708 macinit
.PassControlFrames
|
1709 macinit
.BroadcastFramesReception
|
1710 macinit
.DestinationAddrFilter
|
1711 macinit
.PromiscuousMode
|
1712 macinit
.MulticastFramesFilter
|
1713 macinit
.UnicastFramesFilter
);
1715 /* Wait until the write operation will be taken into account:
1716 at least four TX_CLK/RX_CLK clock cycles */
1717 tmpreg
= (heth
->Instance
)->MACFFR
;
1718 HAL_Delay(ETH_REG_WRITE_DELAY
);
1719 (heth
->Instance
)->MACFFR
= tmpreg
;
1721 /*--------------- ETHERNET MACHTHR and MACHTLR Configuration --------------*/
1722 /* Write to ETHERNET MACHTHR */
1723 (heth
->Instance
)->MACHTHR
= (uint32_t)macinit
.HashTableHigh
;
1725 /* Write to ETHERNET MACHTLR */
1726 (heth
->Instance
)->MACHTLR
= (uint32_t)macinit
.HashTableLow
;
1727 /*----------------------- ETHERNET MACFCR Configuration -------------------*/
1729 /* Get the ETHERNET MACFCR value */
1730 tmpreg
= (heth
->Instance
)->MACFCR
;
1732 tmpreg
&= ETH_MACFCR_CLEAR_MASK
;
1734 /* Set the PT bit according to ETH PauseTime value */
1735 /* Set the DZPQ bit according to ETH ZeroQuantaPause value */
1736 /* Set the PLT bit according to ETH PauseLowThreshold value */
1737 /* Set the UP bit according to ETH UnicastPauseFrameDetect value */
1738 /* Set the RFE bit according to ETH ReceiveFlowControl value */
1739 /* Set the TFE bit according to ETH TransmitFlowControl value */
1740 tmpreg
|= (uint32_t)((macinit
.PauseTime
<< 16) |
1741 macinit
.ZeroQuantaPause
|
1742 macinit
.PauseLowThreshold
|
1743 macinit
.UnicastPauseFrameDetect
|
1744 macinit
.ReceiveFlowControl
|
1745 macinit
.TransmitFlowControl
);
1747 /* Write to ETHERNET MACFCR */
1748 (heth
->Instance
)->MACFCR
= (uint32_t)tmpreg
;
1750 /* Wait until the write operation will be taken into account:
1751 at least four TX_CLK/RX_CLK clock cycles */
1752 tmpreg
= (heth
->Instance
)->MACFCR
;
1753 HAL_Delay(ETH_REG_WRITE_DELAY
);
1754 (heth
->Instance
)->MACFCR
= tmpreg
;
1756 /*----------------------- ETHERNET MACVLANTR Configuration ----------------*/
1757 /* Set the ETV bit according to ETH VLANTagComparison value */
1758 /* Set the VL bit according to ETH VLANTagIdentifier value */
1759 (heth
->Instance
)->MACVLANTR
= (uint32_t)(macinit
.VLANTagComparison
|
1760 macinit
.VLANTagIdentifier
);
1762 /* Wait until the write operation will be taken into account:
1763 at least four TX_CLK/RX_CLK clock cycles */
1764 tmpreg
= (heth
->Instance
)->MACVLANTR
;
1765 HAL_Delay(ETH_REG_WRITE_DELAY
);
1766 (heth
->Instance
)->MACVLANTR
= tmpreg
;
1768 /* Ethernet DMA default initialization ************************************/
1769 dmainit
.DropTCPIPChecksumErrorFrame
= ETH_DROPTCPIPCHECKSUMERRORFRAME_ENABLE
;
1770 dmainit
.ReceiveStoreForward
= ETH_RECEIVESTOREFORWARD_ENABLE
;
1771 dmainit
.FlushReceivedFrame
= ETH_FLUSHRECEIVEDFRAME_ENABLE
;
1772 dmainit
.TransmitStoreForward
= ETH_TRANSMITSTOREFORWARD_ENABLE
;
1773 dmainit
.TransmitThresholdControl
= ETH_TRANSMITTHRESHOLDCONTROL_64BYTES
;
1774 dmainit
.ForwardErrorFrames
= ETH_FORWARDERRORFRAMES_DISABLE
;
1775 dmainit
.ForwardUndersizedGoodFrames
= ETH_FORWARDUNDERSIZEDGOODFRAMES_DISABLE
;
1776 dmainit
.ReceiveThresholdControl
= ETH_RECEIVEDTHRESHOLDCONTROL_64BYTES
;
1777 dmainit
.SecondFrameOperate
= ETH_SECONDFRAMEOPERARTE_ENABLE
;
1778 dmainit
.AddressAlignedBeats
= ETH_ADDRESSALIGNEDBEATS_ENABLE
;
1779 dmainit
.FixedBurst
= ETH_FIXEDBURST_ENABLE
;
1780 dmainit
.RxDMABurstLength
= ETH_RXDMABURSTLENGTH_32BEAT
;
1781 dmainit
.TxDMABurstLength
= ETH_TXDMABURSTLENGTH_32BEAT
;
1782 dmainit
.EnhancedDescriptorFormat
= ETH_DMAENHANCEDDESCRIPTOR_ENABLE
;
1783 dmainit
.DescriptorSkipLength
= 0x0;
1784 dmainit
.DMAArbitration
= ETH_DMAARBITRATION_ROUNDROBIN_RXTX_1_1
;
1786 /* Get the ETHERNET DMAOMR value */
1787 tmpreg
= (heth
->Instance
)->DMAOMR
;
1789 tmpreg
&= ETH_DMAOMR_CLEAR_MASK
;
1791 /* Set the DT bit according to ETH DropTCPIPChecksumErrorFrame value */
1792 /* Set the RSF bit according to ETH ReceiveStoreForward value */
1793 /* Set the DFF bit according to ETH FlushReceivedFrame value */
1794 /* Set the TSF bit according to ETH TransmitStoreForward value */
1795 /* Set the TTC bit according to ETH TransmitThresholdControl value */
1796 /* Set the FEF bit according to ETH ForwardErrorFrames value */
1797 /* Set the FUF bit according to ETH ForwardUndersizedGoodFrames value */
1798 /* Set the RTC bit according to ETH ReceiveThresholdControl value */
1799 /* Set the OSF bit according to ETH SecondFrameOperate value */
1800 tmpreg
|= (uint32_t)(dmainit
.DropTCPIPChecksumErrorFrame
|
1801 dmainit
.ReceiveStoreForward
|
1802 dmainit
.FlushReceivedFrame
|
1803 dmainit
.TransmitStoreForward
|
1804 dmainit
.TransmitThresholdControl
|
1805 dmainit
.ForwardErrorFrames
|
1806 dmainit
.ForwardUndersizedGoodFrames
|
1807 dmainit
.ReceiveThresholdControl
|
1808 dmainit
.SecondFrameOperate
);
1810 /* Write to ETHERNET DMAOMR */
1811 (heth
->Instance
)->DMAOMR
= (uint32_t)tmpreg
;
1813 /* Wait until the write operation will be taken into account:
1814 at least four TX_CLK/RX_CLK clock cycles */
1815 tmpreg
= (heth
->Instance
)->DMAOMR
;
1816 HAL_Delay(ETH_REG_WRITE_DELAY
);
1817 (heth
->Instance
)->DMAOMR
= tmpreg
;
1819 /*----------------------- ETHERNET DMABMR Configuration ------------------*/
1820 /* Set the AAL bit according to ETH AddressAlignedBeats value */
1821 /* Set the FB bit according to ETH FixedBurst value */
1822 /* Set the RPBL and 4*PBL bits according to ETH RxDMABurstLength value */
1823 /* Set the PBL and 4*PBL bits according to ETH TxDMABurstLength value */
1824 /* Set the Enhanced DMA descriptors bit according to ETH EnhancedDescriptorFormat value*/
1825 /* Set the DSL bit according to ETH DesciptorSkipLength value */
1826 /* Set the PR and DA bits according to ETH DMAArbitration value */
1827 (heth
->Instance
)->DMABMR
= (uint32_t)(dmainit
.AddressAlignedBeats
|
1828 dmainit
.FixedBurst
|
1829 dmainit
.RxDMABurstLength
| /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
1830 dmainit
.TxDMABurstLength
|
1831 dmainit
.EnhancedDescriptorFormat
|
1832 (dmainit
.DescriptorSkipLength
<< 2) |
1833 dmainit
.DMAArbitration
|
1834 ETH_DMABMR_USP
); /* Enable use of separate PBL for Rx and Tx */
1836 /* Wait until the write operation will be taken into account:
1837 at least four TX_CLK/RX_CLK clock cycles */
1838 tmpreg
= (heth
->Instance
)->DMABMR
;
1839 HAL_Delay(ETH_REG_WRITE_DELAY
);
1840 (heth
->Instance
)->DMABMR
= tmpreg
;
1842 if((heth
->Init
).RxMode
== ETH_RXINTERRUPT_MODE
)
1844 /* Enable the Ethernet Rx Interrupt */
1845 __HAL_ETH_DMA_ENABLE_IT((heth
), ETH_DMA_IT_NIS
| ETH_DMA_IT_R
);
1848 /* Initialize MAC address in ethernet MAC */
1849 ETH_MACAddressConfig(heth
, ETH_MAC_ADDRESS0
, heth
->Init
.MACAddr
);
1853 * @brief Configures the selected MAC address.
1854 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1855 * the configuration information for ETHERNET module
1856 * @param MacAddr: The MAC address to configure
1857 * This parameter can be one of the following values:
1858 * @arg ETH_MAC_Address0: MAC Address0
1859 * @arg ETH_MAC_Address1: MAC Address1
1860 * @arg ETH_MAC_Address2: MAC Address2
1861 * @arg ETH_MAC_Address3: MAC Address3
1862 * @param Addr: Pointer to MAC address buffer data (6 bytes)
1863 * @retval HAL status
1865 static void ETH_MACAddressConfig(ETH_HandleTypeDef
*heth
, uint32_t MacAddr
, uint8_t *Addr
)
1869 /* Check the parameters */
1870 assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr
));
1872 /* Calculate the selected MAC address high register */
1873 tmpreg
= ((uint32_t)Addr
[5] << 8) | (uint32_t)Addr
[4];
1874 /* Load the selected MAC address high register */
1875 (*(__IO
uint32_t *)((uint32_t)(ETH_MAC_ADDR_HBASE
+ MacAddr
))) = tmpreg
;
1876 /* Calculate the selected MAC address low register */
1877 tmpreg
= ((uint32_t)Addr
[3] << 24) | ((uint32_t)Addr
[2] << 16) | ((uint32_t)Addr
[1] << 8) | Addr
[0];
1879 /* Load the selected MAC address low register */
1880 (*(__IO
uint32_t *)((uint32_t)(ETH_MAC_ADDR_LBASE
+ MacAddr
))) = tmpreg
;
1884 * @brief Enables the MAC transmission.
1885 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1886 * the configuration information for ETHERNET module
1889 static void ETH_MACTransmissionEnable(ETH_HandleTypeDef
*heth
)
1891 __IO
uint32_t tmpreg
= 0;
1893 /* Enable the MAC transmission */
1894 (heth
->Instance
)->MACCR
|= ETH_MACCR_TE
;
1896 /* Wait until the write operation will be taken into account:
1897 at least four TX_CLK/RX_CLK clock cycles */
1898 tmpreg
= (heth
->Instance
)->MACCR
;
1899 HAL_Delay(ETH_REG_WRITE_DELAY
);
1900 (heth
->Instance
)->MACCR
= tmpreg
;
1904 * @brief Disables the MAC transmission.
1905 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1906 * the configuration information for ETHERNET module
1909 static void ETH_MACTransmissionDisable(ETH_HandleTypeDef
*heth
)
1911 __IO
uint32_t tmpreg
= 0;
1913 /* Disable the MAC transmission */
1914 (heth
->Instance
)->MACCR
&= ~ETH_MACCR_TE
;
1916 /* Wait until the write operation will be taken into account:
1917 at least four TX_CLK/RX_CLK clock cycles */
1918 tmpreg
= (heth
->Instance
)->MACCR
;
1919 HAL_Delay(ETH_REG_WRITE_DELAY
);
1920 (heth
->Instance
)->MACCR
= tmpreg
;
1924 * @brief Enables the MAC reception.
1925 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1926 * the configuration information for ETHERNET module
1929 static void ETH_MACReceptionEnable(ETH_HandleTypeDef
*heth
)
1931 __IO
uint32_t tmpreg
= 0;
1933 /* Enable the MAC reception */
1934 (heth
->Instance
)->MACCR
|= ETH_MACCR_RE
;
1936 /* Wait until the write operation will be taken into account:
1937 at least four TX_CLK/RX_CLK clock cycles */
1938 tmpreg
= (heth
->Instance
)->MACCR
;
1939 HAL_Delay(ETH_REG_WRITE_DELAY
);
1940 (heth
->Instance
)->MACCR
= tmpreg
;
1944 * @brief Disables the MAC reception.
1945 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1946 * the configuration information for ETHERNET module
1949 static void ETH_MACReceptionDisable(ETH_HandleTypeDef
*heth
)
1951 __IO
uint32_t tmpreg
= 0;
1953 /* Disable the MAC reception */
1954 (heth
->Instance
)->MACCR
&= ~ETH_MACCR_RE
;
1956 /* Wait until the write operation will be taken into account:
1957 at least four TX_CLK/RX_CLK clock cycles */
1958 tmpreg
= (heth
->Instance
)->MACCR
;
1959 HAL_Delay(ETH_REG_WRITE_DELAY
);
1960 (heth
->Instance
)->MACCR
= tmpreg
;
1964 * @brief Enables the DMA transmission.
1965 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1966 * the configuration information for ETHERNET module
1969 static void ETH_DMATransmissionEnable(ETH_HandleTypeDef
*heth
)
1971 /* Enable the DMA transmission */
1972 (heth
->Instance
)->DMAOMR
|= ETH_DMAOMR_ST
;
1976 * @brief Disables the DMA transmission.
1977 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1978 * the configuration information for ETHERNET module
1981 static void ETH_DMATransmissionDisable(ETH_HandleTypeDef
*heth
)
1983 /* Disable the DMA transmission */
1984 (heth
->Instance
)->DMAOMR
&= ~ETH_DMAOMR_ST
;
1988 * @brief Enables the DMA reception.
1989 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1990 * the configuration information for ETHERNET module
1993 static void ETH_DMAReceptionEnable(ETH_HandleTypeDef
*heth
)
1995 /* Enable the DMA reception */
1996 (heth
->Instance
)->DMAOMR
|= ETH_DMAOMR_SR
;
2000 * @brief Disables the DMA reception.
2001 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
2002 * the configuration information for ETHERNET module
2005 static void ETH_DMAReceptionDisable(ETH_HandleTypeDef
*heth
)
2007 /* Disable the DMA reception */
2008 (heth
->Instance
)->DMAOMR
&= ~ETH_DMAOMR_SR
;
2012 * @brief Clears the ETHERNET transmit FIFO.
2013 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
2014 * the configuration information for ETHERNET module
2017 static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef
*heth
)
2019 __IO
uint32_t tmpreg
= 0;
2021 /* Set the Flush Transmit FIFO bit */
2022 (heth
->Instance
)->DMAOMR
|= ETH_DMAOMR_FTF
;
2024 /* Wait until the write operation will be taken into account:
2025 at least four TX_CLK/RX_CLK clock cycles */
2026 tmpreg
= (heth
->Instance
)->DMAOMR
;
2027 HAL_Delay(ETH_REG_WRITE_DELAY
);
2028 (heth
->Instance
)->DMAOMR
= tmpreg
;
2036 #endif /* HAL_ETH_MODULE_ENABLED */
2045 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/