]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/STM32Fxx/stm32fxx_hal_eth.c
c67ad1901590d64b2137bbe6b5e84e9e318210af
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / portable / NetworkInterface / STM32Fxx / stm32fxx_hal_eth.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32fxx_hal_eth.c\r
4   * @author  MCD Application Team\r
5   * @version V1.3.2\r
6   * @date    26-June-2015\r
7   * @brief   ETH HAL module driver.\r
8   *          This file provides firmware functions to manage the following\r
9   *          functionalities of the Ethernet (ETH) peripheral:\r
10   *           + Initialization and de-initialization functions\r
11   *           + IO operation functions\r
12   *           + Peripheral Control functions\r
13   *           + Peripheral State and Errors functions\r
14   *\r
15   @verbatim\r
16   ==============================================================================\r
17                     ##### How to use this driver #####\r
18   ==============================================================================\r
19     [..]\r
20       (#)Declare a ETH_HandleTypeDef handle structure, for example:\r
21          ETH_HandleTypeDef  heth;\r
22 \r
23       (#)Fill parameters of Init structure in heth handle\r
24 \r
25       (#)Call HAL_ETH_Init() API to initialize the Ethernet peripheral (MAC, DMA, ...)\r
26 \r
27       (#)Initialize the ETH low level resources through the HAL_ETH_MspInit() API:\r
28           (##) Enable the Ethernet interface clock using\r
29                (+++) __HAL_RCC_ETHMAC_CLK_ENABLE();\r
30                (+++) __HAL_RCC_ETHMACTX_CLK_ENABLE();\r
31                (+++) __HAL_RCC_ETHMACRX_CLK_ENABLE();\r
32 \r
33           (##) Initialize the related GPIO clocks\r
34           (##) Configure Ethernet pin-out\r
35           (##) Configure Ethernet NVIC interrupt (IT mode)\r
36 \r
37       (#)Initialize Ethernet DMA Descriptors in chain mode and point to allocated buffers:\r
38           (##) HAL_ETH_DMATxDescListInit(); for Transmission process\r
39           (##) HAL_ETH_DMARxDescListInit(); for Reception process\r
40 \r
41       (#)Enable MAC and DMA transmission and reception:\r
42           (##) HAL_ETH_Start();\r
43 \r
44       (#)Prepare ETH DMA TX Descriptors and give the hand to ETH DMA to transfer\r
45          the frame to MAC TX FIFO:\r
46          (##) HAL_ETH_TransmitFrame();\r
47 \r
48       (#)Poll for a received frame in ETH RX DMA Descriptors and get received\r
49          frame parameters\r
50          (##) HAL_ETH_GetReceivedFrame(); (should be called into an infinite loop)\r
51 \r
52       (#) Get a received frame when an ETH RX interrupt occurs:\r
53          (##) HAL_ETH_GetReceivedFrame_IT(); (called in IT mode only)\r
54 \r
55       (#) Communicate with external PHY device:\r
56          (##) Read a specific register from the PHY\r
57               HAL_ETH_ReadPHYRegister();\r
58          (##) Write data to a specific RHY register:\r
59               HAL_ETH_WritePHYRegister();\r
60 \r
61       (#) Configure the Ethernet MAC after ETH peripheral initialization\r
62           HAL_ETH_ConfigMAC(); all MAC parameters should be filled.\r
63 \r
64       (#) Configure the Ethernet DMA after ETH peripheral initialization\r
65           HAL_ETH_ConfigDMA(); all DMA parameters should be filled.\r
66 \r
67       -@- The PTP protocol and the DMA descriptors ring mode are not supported\r
68           in this driver\r
69 \r
70   @endverbatim\r
71   ******************************************************************************\r
72   * @attention\r
73   *\r
74   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
75   *\r
76   * Redistribution and use in source and binary forms, with or without modification,\r
77   * are permitted provided that the following conditions are met:\r
78   *   1. Redistributions of source code must retain the above copyright notice,\r
79   *      this list of conditions and the following disclaimer.\r
80   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
81   *      this list of conditions and the following disclaimer in the documentation\r
82   *      and/or other materials provided with the distribution.\r
83   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
84   *      may be used to endorse or promote products derived from this software\r
85   *      without specific prior written permission.\r
86   *\r
87   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
88   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
89   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
90   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
91   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
92   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
93   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
94   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
95   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
96   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
97   *\r
98   ******************************************************************************\r
99   */\r
100 \r
101 /* Includes ------------------------------------------------------------------*/\r
102 #define __STM32_HAL_LEGACY      1\r
103 \r
104 #if defined(STM32F7xx)\r
105         #include "stm32f7xx_hal.h"\r
106         #include "stm32f7xx_hal_def.h"\r
107         #define stm_is_F7       1\r
108 #elif defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\r
109         #include "stm32f4xx_hal.h"\r
110         #include "stm32f4xx_hal_def.h"\r
111         #define stm_is_F4       1\r
112 #elif defined(STM32F2xx)\r
113         #include "stm32f2xx_hal.h"\r
114         #include "stm32f2xx_hal_def.h"\r
115         #define stm_is_F2       1\r
116 #else\r
117         #error For what part should this be compiled?\r
118 #endif\r
119 \r
120 #include "stm32fxx_hal_eth.h"\r
121 \r
122 /** @addtogroup STM32F4xx_HAL_Driver\r
123   * @{\r
124   */\r
125 \r
126 /** @defgroup ETH ETH\r
127   * @brief ETH HAL module driver\r
128   * @{\r
129   */\r
130 \r
131 #if !defined( ARRAY_SIZE )\r
132         #define ARRAY_SIZE( x ) ( sizeof ( x ) / sizeof ( x )[ 0 ] )\r
133 #endif\r
134 \r
135 #ifdef HAL_ETH_MODULE_ENABLED\r
136 \r
137 #if( stm_is_F2 != 0 || stm_is_F4 != 0 || stm_is_F7 )\r
138 \r
139 /* Private typedef -----------------------------------------------------------*/\r
140 /* Private define ------------------------------------------------------------*/\r
141 /** @defgroup ETH_Private_Constants ETH Private Constants\r
142   * @{\r
143   */\r
144 \r
145 /**\r
146   * @}\r
147   */\r
148 /* Private macro -------------------------------------------------------------*/\r
149 /* Private variables ---------------------------------------------------------*/\r
150 /* Private function prototypes -----------------------------------------------*/\r
151 /** @defgroup ETH_Private_Functions ETH Private Functions\r
152   * @{\r
153   */\r
154 static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err);\r
155 static void ETH_MACAddressConfig(ETH_HandleTypeDef *heth, uint32_t MacAddr, uint8_t *Addr);\r
156 static void ETH_MACReceptionEnable(ETH_HandleTypeDef *heth);\r
157 static void ETH_MACReceptionDisable(ETH_HandleTypeDef *heth);\r
158 static void ETH_MACTransmissionEnable(ETH_HandleTypeDef *heth);\r
159 static void ETH_MACTransmissionDisable(ETH_HandleTypeDef *heth);\r
160 static void ETH_DMATransmissionEnable(ETH_HandleTypeDef *heth);\r
161 static void ETH_DMATransmissionDisable(ETH_HandleTypeDef *heth);\r
162 static void ETH_DMAReceptionEnable(ETH_HandleTypeDef *heth);\r
163 static void ETH_DMAReceptionDisable(ETH_HandleTypeDef *heth);\r
164 static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef *heth);\r
165 \r
166 /**\r
167   * @}\r
168   */\r
169 /* Private functions ---------------------------------------------------------*/\r
170 \r
171 /** @defgroup ETH_Exported_Functions ETH Exported Functions\r
172   * @{\r
173   */\r
174 \r
175 /** @defgroup ETH_Exported_Functions_Group1 Initialization and de-initialization functions\r
176   *  @brief   Initialization and Configuration functions\r
177   *\r
178   @verbatim\r
179   ===============================================================================\r
180             ##### Initialization and de-initialization functions #####\r
181   ===============================================================================\r
182   [..]  This section provides functions allowing to:\r
183       (+) Initialize and configure the Ethernet peripheral\r
184       (+) De-initialize the Ethernet peripheral\r
185 \r
186   @endverbatim\r
187   * @{\r
188   */\r
189 extern void vMACBProbePhy ( void );\r
190 \r
191 /**\r
192   * @brief  Initializes the Ethernet MAC and DMA according to default\r
193   *         parameters.\r
194   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
195   *         the configuration information for ETHERNET module\r
196   * @retval HAL status\r
197   */\r
198 HAL_StatusTypeDef HAL_ETH_Init(ETH_HandleTypeDef *heth)\r
199 {\r
200         uint32_t tmpreg = 0;\r
201         uint32_t hclk = 60000000;\r
202         uint32_t err = ETH_SUCCESS;\r
203 \r
204         /* Check the ETH peripheral state */\r
205         if( heth == NULL )\r
206         {\r
207                 return HAL_ERROR;\r
208         }\r
209 \r
210         /* Check parameters */\r
211         assert_param(IS_ETH_AUTONEGOTIATION(heth->Init.AutoNegotiation));\r
212         assert_param(IS_ETH_RX_MODE(heth->Init.RxMode));\r
213         assert_param(IS_ETH_CHECKSUM_MODE(heth->Init.ChecksumMode));\r
214         assert_param(IS_ETH_MEDIA_INTERFACE(heth->Init.MediaInterface));\r
215 \r
216         if( heth->State == HAL_ETH_STATE_RESET )\r
217         {\r
218                 /* Init the low level hardware : GPIO, CLOCK, NVIC. */\r
219                 HAL_ETH_MspInit( heth );\r
220         }\r
221 \r
222         /* Enable SYSCFG Clock */\r
223         __HAL_RCC_SYSCFG_CLK_ENABLE();\r
224 \r
225         /* Select MII or RMII Mode*/\r
226         SYSCFG->PMC &= ~(SYSCFG_PMC_MII_RMII_SEL);\r
227         SYSCFG->PMC |= (uint32_t)heth->Init.MediaInterface;\r
228 \r
229         /* Ethernet Software reset */\r
230         /* Set the SWR bit: resets all MAC subsystem internal registers and logic */\r
231         /* After reset all the registers holds their respective reset values */\r
232         /* Also enable EDFE: Enhanced descriptor format enable. */\r
233         heth->Instance->DMABMR |= ETH_DMABMR_SR | ETH_DMABMR_EDE;\r
234 \r
235         /* Wait for software reset */\r
236         while ((heth->Instance->DMABMR & ETH_DMABMR_SR) != (uint32_t)RESET)\r
237         {\r
238                 /* If your program hangs here, please check the value of 'ipconfigUSE_RMII'. */\r
239         }\r
240 \r
241         /*-------------------------------- MAC Initialization ----------------------*/\r
242         /* Get the ETHERNET MACMIIAR value */\r
243         tmpreg = heth->Instance->MACMIIAR;\r
244         /* Clear CSR Clock Range CR[2:0] bits */\r
245         tmpreg &= ETH_MACMIIAR_CR_MASK;\r
246 \r
247         /* Get hclk frequency value (168,000,000) */\r
248         hclk = HAL_RCC_GetHCLKFreq();\r
249 \r
250         /* Set CR bits depending on hclk value */\r
251         if( ( hclk >= 20000000 ) && ( hclk < 35000000 ) )\r
252         {\r
253                 /* CSR Clock Range between 20-35 MHz */\r
254                 tmpreg |= (uint32_t) ETH_MACMIIAR_CR_Div16;\r
255         }\r
256         else if( ( hclk >= 35000000 ) && ( hclk < 60000000 ) )\r
257         {\r
258         /* CSR Clock Range between 35-60 MHz */\r
259         tmpreg |= ( uint32_t ) ETH_MACMIIAR_CR_Div26;\r
260         }\r
261         else if((hclk >= 60000000 ) && ( hclk < 100000000 ) )\r
262         {\r
263                 /* CSR Clock Range between 60-100 MHz */\r
264                 tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div42;\r
265         }\r
266         else if((hclk >= 100000000 ) && ( hclk < 150000000))\r
267         {\r
268                 /* CSR Clock Range between 100-150 MHz */\r
269                 tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div62;\r
270         }\r
271         else /* ((hclk >= 150000000 ) && ( hclk <= 168000000)) */\r
272         {\r
273                 /* CSR Clock Range between 150-168 MHz */\r
274                 tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div102;\r
275         }\r
276 \r
277         /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */\r
278         heth->Instance->MACMIIAR = (uint32_t)tmpreg;\r
279 \r
280         /* Initialise the MACB and set all PHY properties */\r
281         vMACBProbePhy();\r
282 \r
283         /* Config MAC and DMA */\r
284         ETH_MACDMAConfig(heth, err);\r
285 \r
286         /* Set ETH HAL State to Ready */\r
287         heth->State= HAL_ETH_STATE_READY;\r
288 \r
289         /* Return function status */\r
290         return HAL_OK;\r
291 }\r
292 \r
293 /**\r
294   * @brief  De-Initializes the ETH peripheral.\r
295   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
296   *         the configuration information for ETHERNET module\r
297   * @retval HAL status\r
298   */\r
299 HAL_StatusTypeDef HAL_ETH_DeInit(ETH_HandleTypeDef *heth)\r
300 {\r
301         /* Set the ETH peripheral state to BUSY */\r
302         heth->State = HAL_ETH_STATE_BUSY;\r
303 \r
304         /* De-Init the low level hardware : GPIO, CLOCK, NVIC. */\r
305         HAL_ETH_MspDeInit( heth );\r
306 \r
307         /* Set ETH HAL state to Disabled */\r
308         heth->State= HAL_ETH_STATE_RESET;\r
309 \r
310         /* Release Lock */\r
311         __HAL_UNLOCK( heth );\r
312 \r
313         /* Return function status */\r
314         return HAL_OK;\r
315 }\r
316 \r
317 /**\r
318   * @brief  Initializes the DMA Tx descriptors in chain mode.\r
319   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
320   *         the configuration information for ETHERNET module\r
321   * @param  DMATxDescTab: Pointer to the first Tx desc list\r
322   * @param  TxBuff: Pointer to the first TxBuffer list\r
323   * @param  TxBuffCount: Number of the used Tx desc in the list\r
324   * @retval HAL status\r
325   */\r
326 HAL_StatusTypeDef HAL_ETH_DMATxDescListInit(ETH_HandleTypeDef *heth, ETH_DMADescTypeDef *pxDMATable, uint8_t *ucDataBuffer, uint32_t ulBufferCount)\r
327 {\r
328         uint32_t i = 0;\r
329         ETH_DMADescTypeDef *pxDMADescriptor;\r
330 \r
331         /* Process Locked */\r
332         __HAL_LOCK( heth );\r
333 \r
334         /* Set the ETH peripheral state to BUSY */\r
335         heth->State = HAL_ETH_STATE_BUSY;\r
336 \r
337         /* Set the TxDesc pointer with the first one of the pxDMATable list */\r
338         heth->TxDesc = pxDMATable;\r
339 \r
340         /* Fill each DMA descriptor with the right values */\r
341         for( i=0; i < ulBufferCount; i++ )\r
342         {\r
343                 /* Get the pointer on the ith member of the descriptor list */\r
344                 pxDMADescriptor = pxDMATable + i;\r
345 \r
346                 /* Set Second Address Chained bit */\r
347                 pxDMADescriptor->Status = ETH_DMATXDESC_TCH;\r
348 \r
349                 pxDMADescriptor->ControlBufferSize = 0;\r
350 \r
351                 /* Set Buffer1 address pointer */\r
352                 if( ucDataBuffer != NULL )\r
353                 {\r
354                         pxDMADescriptor->Buffer1Addr = ( uint32_t )( &ucDataBuffer[ i * ETH_TX_BUF_SIZE ] );\r
355                 }\r
356                 else\r
357                 {\r
358                         /* Buffer space is not provided because it uses zero-copy transmissions. */\r
359                         pxDMADescriptor->Buffer1Addr = ( uint32_t )0u;\r
360                 }\r
361 \r
362                 if (heth->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)\r
363                 {\r
364                         /* Set the DMA Tx descriptors checksum insertion for TCP, UDP, and ICMP */\r
365                         pxDMADescriptor->Status |= ETH_DMATXDESC_CHECKSUMTCPUDPICMPFULL;\r
366                 }\r
367 \r
368                 /* Initialize the next descriptor with the Next Descriptor Polling Enable */\r
369                 if(i < ( ulBufferCount - 1 ) )\r
370                 {\r
371                         /* Set next descriptor address register with next descriptor base address */\r
372                         pxDMADescriptor->Buffer2NextDescAddr = ( uint32_t ) ( pxDMATable + i + 1 );\r
373                 }\r
374                 else\r
375                 {\r
376                         /* For last descriptor, set next descriptor address register equal to the first descriptor base address */\r
377                         pxDMADescriptor->Buffer2NextDescAddr = ( uint32_t ) pxDMATable;\r
378                 }\r
379         }\r
380 \r
381         /* Set Transmit Descriptor List Address Register */\r
382         heth->Instance->DMATDLAR = ( uint32_t ) pxDMATable;\r
383 \r
384         /* Set ETH HAL State to Ready */\r
385         heth->State= HAL_ETH_STATE_READY;\r
386 \r
387         /* Process Unlocked */\r
388         __HAL_UNLOCK( heth );\r
389 \r
390         /* Return function status */\r
391         return HAL_OK;\r
392 }\r
393 \r
394 /**\r
395   * @brief  Initializes the DMA Rx descriptors in chain mode.\r
396   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
397   *         the configuration information for ETHERNET module\r
398   * @param  DMARxDescTab: Pointer to the first Rx desc list\r
399   * @param  RxBuff: Pointer to the first RxBuffer list\r
400   * @param  RxBuffCount: Number of the used Rx desc in the list\r
401   * @retval HAL status\r
402   */\r
403 HAL_StatusTypeDef HAL_ETH_DMARxDescListInit(ETH_HandleTypeDef *heth, ETH_DMADescTypeDef *pxDMATable, uint8_t *ucDataBuffer, uint32_t ulBufferCount)\r
404 {\r
405         uint32_t i = 0;\r
406         ETH_DMADescTypeDef *pxDMADescriptor;\r
407 \r
408         /* Process Locked */\r
409         __HAL_LOCK( heth );\r
410 \r
411         /* Set the ETH peripheral state to BUSY */\r
412         heth->State = HAL_ETH_STATE_BUSY;\r
413 \r
414         /* Set the RxDesc pointer with the first one of the pxDMATable list */\r
415         heth->RxDesc = pxDMATable;\r
416 \r
417         /* Fill each DMA descriptor with the right values */\r
418         for(i=0; i < ulBufferCount; i++)\r
419         {\r
420                 /* Get the pointer on the ith member of the descriptor list */\r
421                 pxDMADescriptor = pxDMATable+i;\r
422 \r
423                 /* Set Own bit of the Rx descriptor Status */\r
424                 pxDMADescriptor->Status = ETH_DMARXDESC_OWN;\r
425 \r
426                 /* Set Buffer1 size and Second Address Chained bit */\r
427                 pxDMADescriptor->ControlBufferSize = ETH_DMARXDESC_RCH | ETH_RX_BUF_SIZE;\r
428 \r
429                 /* Set Buffer1 address pointer */\r
430                 if( ucDataBuffer != NULL )\r
431                 {\r
432                         pxDMADescriptor->Buffer1Addr = ( uint32_t )( &ucDataBuffer[ i * ETH_RX_BUF_SIZE ] );\r
433                 }\r
434                 else\r
435                 {\r
436                         /* Buffer space is not provided because it uses zero-copy reception. */\r
437                         pxDMADescriptor->Buffer1Addr = ( uint32_t )0u;\r
438                 }\r
439 \r
440                 if( heth->Init.RxMode == ETH_RXINTERRUPT_MODE )\r
441                 {\r
442                         /* Enable Ethernet DMA Rx Descriptor interrupt */\r
443                         pxDMADescriptor->ControlBufferSize &= ~ETH_DMARXDESC_DIC;\r
444                 }\r
445 \r
446                 /* Initialize the next descriptor with the Next Descriptor Polling Enable */\r
447                 if(i < (ulBufferCount-1))\r
448                 {\r
449                         /* Set next descriptor address register with next descriptor base address */\r
450                         pxDMADescriptor->Buffer2NextDescAddr = (uint32_t)(pxDMATable+i+1);\r
451                 }\r
452                 else\r
453                 {\r
454                         /* For last descriptor, set next descriptor address register equal to the first descriptor base address */\r
455                         pxDMADescriptor->Buffer2NextDescAddr = ( uint32_t ) pxDMATable;\r
456                 }\r
457         }\r
458 \r
459         /* Set Receive Descriptor List Address Register */\r
460         heth->Instance->DMARDLAR = ( uint32_t ) pxDMATable;\r
461 \r
462         /* Set ETH HAL State to Ready */\r
463         heth->State= HAL_ETH_STATE_READY;\r
464 \r
465         /* Process Unlocked */\r
466         __HAL_UNLOCK( heth );\r
467 \r
468         /* Return function status */\r
469         return HAL_OK;\r
470 }\r
471 \r
472 /**\r
473   * @brief  Initializes the ETH MSP.\r
474   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
475   *         the configuration information for ETHERNET module\r
476   * @retval None\r
477   */\r
478 __weak void HAL_ETH_MspInit(ETH_HandleTypeDef *heth)\r
479 {\r
480   /* NOTE : This function Should not be modified, when the callback is needed,\r
481   the HAL_ETH_MspInit could be implemented in the user file\r
482   */\r
483 }\r
484 \r
485 /**\r
486   * @brief  DeInitializes ETH MSP.\r
487   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
488   *         the configuration information for ETHERNET module\r
489   * @retval None\r
490   */\r
491 __weak void HAL_ETH_MspDeInit(ETH_HandleTypeDef *heth)\r
492 {\r
493   /* NOTE : This function Should not be modified, when the callback is needed,\r
494   the HAL_ETH_MspDeInit could be implemented in the user file\r
495   */\r
496 }\r
497 \r
498 /**\r
499   * @}\r
500   */\r
501 \r
502 /** @defgroup ETH_Exported_Functions_Group2 IO operation functions\r
503   *  @brief   Data transfers functions\r
504   *\r
505   @verbatim\r
506   ==============================================================================\r
507                           ##### IO operation functions #####\r
508   ==============================================================================\r
509   [..]  This section provides functions allowing to:\r
510         (+) Transmit a frame\r
511             HAL_ETH_TransmitFrame();\r
512         (+) Receive a frame\r
513             HAL_ETH_GetReceivedFrame();\r
514             HAL_ETH_GetReceivedFrame_IT();\r
515         (+) Read from an External PHY register\r
516             HAL_ETH_ReadPHYRegister();\r
517         (+) Write to an External PHY register\r
518             HAL_ETH_WritePHYRegister();\r
519 \r
520   @endverbatim\r
521 \r
522   * @{\r
523   */\r
524 \r
525 /**\r
526   * @brief  Sends an Ethernet frame.\r
527   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
528   *         the configuration information for ETHERNET module\r
529   * @param  FrameLength: Amount of data to be sent\r
530   * @retval HAL status\r
531   */\r
532 HAL_StatusTypeDef HAL_ETH_TransmitFrame(ETH_HandleTypeDef *heth, uint32_t FrameLength)\r
533 {\r
534         uint32_t bufcount = 0, size = 0, i = 0;\r
535         __IO ETH_DMADescTypeDef *pxDmaTxDesc = heth->TxDesc;\r
536         /* Process Locked */\r
537         __HAL_LOCK( heth );\r
538 \r
539         /* Set the ETH peripheral state to BUSY */\r
540         heth->State = HAL_ETH_STATE_BUSY;\r
541 \r
542         if( FrameLength == 0 )\r
543         {\r
544                 /* Set ETH HAL state to READY */\r
545                 heth->State = HAL_ETH_STATE_READY;\r
546 \r
547                 /* Process Unlocked */\r
548                 __HAL_UNLOCK( heth );\r
549 \r
550                 return  HAL_ERROR;\r
551         }\r
552 \r
553         /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */\r
554         if( ( pxDmaTxDesc->Status & ETH_DMATXDESC_OWN ) != ( uint32_t ) RESET )\r
555         {\r
556                 /* OWN bit set */\r
557                 heth->State = HAL_ETH_STATE_BUSY_TX;\r
558 \r
559                 /* Process Unlocked */\r
560                 __HAL_UNLOCK( heth );\r
561 \r
562                 return HAL_ERROR;\r
563         }\r
564 \r
565         /* Get the number of needed Tx buffers for the current frame, rounding up. */\r
566         bufcount = ( FrameLength + ETH_TX_BUF_SIZE - 1 ) / ETH_TX_BUF_SIZE;\r
567 \r
568         if (bufcount == 1)\r
569         {\r
570                 /* Set LAST and FIRST segment */\r
571                 pxDmaTxDesc->Status |= ETH_DMATXDESC_FS | ETH_DMATXDESC_LS;\r
572                 /* Set frame size */\r
573                 pxDmaTxDesc->ControlBufferSize = ( FrameLength & ETH_DMATXDESC_TBS1 );\r
574                 /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */\r
575                 pxDmaTxDesc->Status |= ETH_DMATXDESC_OWN;\r
576                 /* Point to next descriptor */\r
577                 heth->TxDesc = ( ETH_DMADescTypeDef * ) ( heth->TxDesc->Buffer2NextDescAddr );\r
578         }\r
579         else\r
580         {\r
581                 for( i = 0; i < bufcount; i++ )\r
582                 {\r
583                         /* Clear FIRST and LAST segment bits */\r
584                 uint32_t ulStatus = heth->TxDesc->Status & ~( ETH_DMATXDESC_FS | ETH_DMATXDESC_LS );\r
585 \r
586                         if( i == 0 )\r
587                         {\r
588                                 /* Setting the first segment bit */\r
589                                 heth->TxDesc->Status = ulStatus | ETH_DMATXDESC_FS;\r
590                         }\r
591 \r
592                         /* Program size */\r
593                         if (i < (bufcount-1))\r
594                         {\r
595                                 heth->TxDesc->ControlBufferSize = (ETH_TX_BUF_SIZE & ETH_DMATXDESC_TBS1);\r
596                         }\r
597                         else\r
598                         {\r
599                                 /* Setting the last segment bit */\r
600                                 heth->TxDesc->Status = ulStatus | ETH_DMATXDESC_LS;\r
601                                 size = FrameLength - (bufcount-1)*ETH_TX_BUF_SIZE;\r
602                                 heth->TxDesc->ControlBufferSize = (size & ETH_DMATXDESC_TBS1);\r
603                         }\r
604 \r
605                         /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */\r
606                         heth->TxDesc->Status |= ETH_DMATXDESC_OWN;\r
607                         /* point to next descriptor */\r
608                         heth->TxDesc = (ETH_DMADescTypeDef *)( heth->TxDesc->Buffer2NextDescAddr );\r
609                 }\r
610         }\r
611 \r
612         __DSB();\r
613 \r
614         /* When Tx Buffer unavailable flag is set: clear it and resume transmission */\r
615         if( ( heth->Instance->DMASR & ETH_DMASR_TBUS ) != ( uint32_t )RESET )\r
616         {\r
617                 heth->Instance->DMACHTDR = ( uint32_t )pxDmaTxDesc;\r
618 \r
619                 /* Clear TBUS ETHERNET DMA flag */\r
620                 heth->Instance->DMASR = ETH_DMASR_TBUS;\r
621                 /* Resume DMA transmission*/\r
622                 heth->Instance->DMATPDR = 0;\r
623         }\r
624 \r
625         /* Set ETH HAL State to Ready */\r
626         heth->State = HAL_ETH_STATE_READY;\r
627 \r
628         /* Process Unlocked */\r
629         __HAL_UNLOCK( heth );\r
630 \r
631         /* Return function status */\r
632         return HAL_OK;\r
633 }\r
634 \r
635 /**\r
636   * @brief  Checks for received frames.\r
637   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
638   *         the configuration information for ETHERNET module\r
639   * @retval HAL status\r
640   */\r
641 HAL_StatusTypeDef HAL_ETH_GetReceivedFrame_IT( ETH_HandleTypeDef *heth )\r
642 {\r
643         return HAL_ETH_GetReceivedFrame( heth );\r
644 }\r
645 \r
646 HAL_StatusTypeDef HAL_ETH_GetReceivedFrame( ETH_HandleTypeDef *heth )\r
647 {\r
648 uint32_t ulCounter = 0;\r
649 ETH_DMADescTypeDef *pxDescriptor = heth->RxDesc;\r
650 HAL_StatusTypeDef xResult = HAL_ERROR;\r
651 \r
652         /* Process Locked */\r
653         __HAL_LOCK( heth );\r
654 \r
655         /* Check the ETH state to BUSY */\r
656         heth->State = HAL_ETH_STATE_BUSY;\r
657 \r
658         /* Scan descriptors owned by CPU */\r
659         while( ( ( pxDescriptor->Status & ETH_DMARXDESC_OWN ) == 0ul ) && ( ulCounter < ETH_RXBUFNB ) )\r
660         {\r
661         uint32_t ulStatus = pxDescriptor->Status;\r
662 \r
663                 /* Just for security. */\r
664                 ulCounter++;\r
665 \r
666                 if( ( ulStatus & ( ETH_DMARXDESC_FS | ETH_DMARXDESC_LS ) ) == ( uint32_t )ETH_DMARXDESC_FS )\r
667                 {\r
668                         /* First segment in frame, but not the last. */\r
669                         heth->RxFrameInfos.FSRxDesc = pxDescriptor;\r
670                         heth->RxFrameInfos.LSRxDesc = ( ETH_DMADescTypeDef *)NULL;\r
671                         heth->RxFrameInfos.SegCount = 1;\r
672                         /* Point to next descriptor. */\r
673                         pxDescriptor = (ETH_DMADescTypeDef*) (pxDescriptor->Buffer2NextDescAddr);\r
674                         heth->RxDesc = pxDescriptor;\r
675                 }\r
676                 else if( ( ulStatus & ( ETH_DMARXDESC_LS | ETH_DMARXDESC_FS ) ) == 0ul )\r
677                 {\r
678                         /* This is an intermediate segment, not first, not last. */\r
679                         /* Increment segment count. */\r
680                         heth->RxFrameInfos.SegCount++;\r
681                         /* Move to the next descriptor. */\r
682                         pxDescriptor = ( ETH_DMADescTypeDef * ) ( pxDescriptor->Buffer2NextDescAddr );\r
683                         heth->RxDesc = pxDescriptor;\r
684                 }\r
685                 /* Must be a last segment */\r
686                 else\r
687                 {\r
688                         /* This is the last segment. */\r
689                         /* Check if last segment is first segment: one segment contains the frame */\r
690                         if( heth->RxFrameInfos.SegCount == 0 )\r
691                         {\r
692                                 /* Remember the first segment. */\r
693                                 heth->RxFrameInfos.FSRxDesc = pxDescriptor;\r
694                         }\r
695 \r
696                         /* Increment segment count */\r
697                         heth->RxFrameInfos.SegCount++;\r
698 \r
699                         /* Remember the last segment. */\r
700                         heth->RxFrameInfos.LSRxDesc = pxDescriptor;\r
701 \r
702                         /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */\r
703                         heth->RxFrameInfos.length =\r
704                                 ( ( ulStatus & ETH_DMARXDESC_FL ) >> ETH_DMARXDESC_FRAMELENGTHSHIFT ) - 4;\r
705 \r
706                         /* Get the address of the buffer start address */\r
707                         heth->RxFrameInfos.buffer = heth->RxFrameInfos.FSRxDesc->Buffer1Addr;\r
708 \r
709                         /* Point to next descriptor */\r
710                         heth->RxDesc = ( ETH_DMADescTypeDef * ) pxDescriptor->Buffer2NextDescAddr;\r
711 \r
712                         /* Return OK status: a packet was received. */\r
713                         xResult = HAL_OK;\r
714                         break;\r
715                 }\r
716         }\r
717 \r
718         /* Set ETH HAL State to Ready */\r
719         heth->State = HAL_ETH_STATE_READY;\r
720 \r
721         /* Process Unlocked */\r
722         __HAL_UNLOCK( heth );\r
723 \r
724         /* Return function status */\r
725         return xResult;\r
726 }\r
727 \r
728 #define ETH_DMA_ALL_INTS \\r
729         ( ETH_DMA_IT_TST | ETH_DMA_IT_PMT | ETH_DMA_IT_MMC | ETH_DMA_IT_NIS | ETH_DMA_IT_AIS | ETH_DMA_IT_ER | \\r
730         ETH_DMA_IT_FBE | ETH_DMA_IT_ET | ETH_DMA_IT_RWT | ETH_DMA_IT_RPS | ETH_DMA_IT_RBU | ETH_DMA_IT_R | \\r
731         ETH_DMA_IT_TU | ETH_DMA_IT_RO | ETH_DMA_IT_TJT | ETH_DMA_IT_TPS | ETH_DMA_IT_T )\r
732 \r
733 //#define ETH_DMA_ALL_INTS              ETH_DMA_IT_RBU | ETH_DMA_FLAG_T | ETH_DMA_FLAG_AIS\r
734 \r
735 #define INT_MASK                ( ( uint32_t ) ~ ( ETH_DMA_IT_TBU ) )\r
736 void HAL_ETH_IRQHandler(ETH_HandleTypeDef *heth)\r
737 {\r
738         uint32_t dmasr;\r
739 \r
740         dmasr = heth->Instance->DMASR & ETH_DMA_ALL_INTS;\r
741         heth->Instance->DMASR = dmasr;\r
742 \r
743         /* Frame received */\r
744         if( ( dmasr & ( ETH_DMA_FLAG_R | ETH_DMA_IT_RBU ) ) != 0 )\r
745         {\r
746                 /* Receive complete callback */\r
747                 HAL_ETH_RxCpltCallback( heth );\r
748         }\r
749         /* Frame transmitted */\r
750         if( ( dmasr & ( ETH_DMA_FLAG_T ) ) != 0 )\r
751         {\r
752                 /* Transfer complete callback */\r
753                 HAL_ETH_TxCpltCallback( heth );\r
754         }\r
755 \r
756         /* ETH DMA Error */\r
757         if( ( dmasr & ( ETH_DMA_FLAG_AIS ) ) != 0 )\r
758         {\r
759                 /* Ethernet Error callback */\r
760                 HAL_ETH_ErrorCallback( heth );\r
761         }\r
762 }\r
763 \r
764 /**\r
765   * @brief  Tx Transfer completed callbacks.\r
766   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
767   *         the configuration information for ETHERNET module\r
768   * @retval None\r
769   */\r
770 __weak void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *heth)\r
771 {\r
772   /* NOTE : This function Should not be modified, when the callback is needed,\r
773   the HAL_ETH_TxCpltCallback could be implemented in the user file\r
774   */\r
775 }\r
776 \r
777 /**\r
778   * @brief  Rx Transfer completed callbacks.\r
779   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
780   *         the configuration information for ETHERNET module\r
781   * @retval None\r
782   */\r
783 __weak void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)\r
784 {\r
785   /* NOTE : This function Should not be modified, when the callback is needed,\r
786   the HAL_ETH_TxCpltCallback could be implemented in the user file\r
787   */\r
788 }\r
789 \r
790 /**\r
791   * @brief  Ethernet transfer error callbacks\r
792   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
793   *         the configuration information for ETHERNET module\r
794   * @retval None\r
795   */\r
796 __weak void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)\r
797 {\r
798   /* NOTE : This function Should not be modified, when the callback is needed,\r
799   the HAL_ETH_TxCpltCallback could be implemented in the user file\r
800   */\r
801 }\r
802 \r
803 /**\r
804   * @brief  Reads a PHY register\r
805   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
806   *         the configuration information for ETHERNET module\r
807   * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.\r
808   *                This parameter can be one of the following values:\r
809   *                   PHY_BCR: Transceiver Basic Control Register,\r
810   *                   PHY_BSR: Transceiver Basic Status Register.\r
811   *                   More PHY register could be read depending on the used PHY\r
812   * @param RegValue: PHY register value\r
813   * @retval HAL status\r
814   */\r
815 HAL_StatusTypeDef HAL_ETH_ReadPHYRegister(ETH_HandleTypeDef *heth, uint16_t PHYReg, uint32_t *RegValue)\r
816 {\r
817 uint32_t tmpreg = 0;\r
818 uint32_t tickstart = 0;\r
819 HAL_StatusTypeDef xResult;\r
820 \r
821         /* Check parameters */\r
822         assert_param(IS_ETH_PHY_ADDRESS(heth->Init.PhyAddress));\r
823 \r
824         /* Check the ETH peripheral state */\r
825         if( heth->State == HAL_ETH_STATE_BUSY_RD )\r
826         {\r
827                 xResult = HAL_BUSY;\r
828         }\r
829         else\r
830         {\r
831                 __HAL_LOCK( heth );\r
832 \r
833                 /* Set ETH HAL State to BUSY_RD */\r
834                 heth->State = HAL_ETH_STATE_BUSY_RD;\r
835 \r
836                 /* Get the ETHERNET MACMIIAR value */\r
837                 tmpreg = heth->Instance->MACMIIAR;\r
838 \r
839                 /* Keep only the CSR Clock Range CR[2:0] bits value */\r
840                 tmpreg &= ~ETH_MACMIIAR_CR_MASK;\r
841 \r
842                 /* Prepare the MII address register value */\r
843                 tmpreg |= ( ( ( uint32_t )heth->Init.PhyAddress << 11) & ETH_MACMIIAR_PA );    /* Set the PHY device address   */\r
844                 tmpreg |= ( ( ( uint32_t )PHYReg << 6 ) & ETH_MACMIIAR_MR );                   /* Set the PHY register address */\r
845                 tmpreg &= ~ETH_MACMIIAR_MW;                                           /* Set the read mode            */\r
846                 tmpreg |= ETH_MACMIIAR_MB;                                            /* Set the MII Busy bit         */\r
847 \r
848                 /* Write the result value into the MII Address register */\r
849                 heth->Instance->MACMIIAR = tmpreg;\r
850 \r
851                 /* Get tick */\r
852                 tickstart = HAL_GetTick();\r
853 \r
854                 /* Check for the Busy flag */\r
855                 while( 1 )\r
856                 {\r
857                         tmpreg = heth->Instance->MACMIIAR;\r
858 \r
859                         if( ( tmpreg & ETH_MACMIIAR_MB ) == 0ul )\r
860                         {\r
861                                 /* Get MACMIIDR value */\r
862                                 *RegValue = ( uint32_t ) heth->Instance->MACMIIDR;\r
863                                 xResult = HAL_OK;\r
864                                 break;\r
865                         }\r
866                         /* Check for the Timeout */\r
867                         if( ( HAL_GetTick( ) - tickstart ) > PHY_READ_TO )\r
868                         {\r
869                                 xResult = HAL_TIMEOUT;\r
870                                 break;\r
871                         }\r
872 \r
873                 }\r
874 \r
875                 /* Set ETH HAL State to READY */\r
876                 heth->State = HAL_ETH_STATE_READY;\r
877 \r
878                 /* Process Unlocked */\r
879                 __HAL_UNLOCK( heth );\r
880         }\r
881 \r
882         /* Return function status */\r
883         return xResult;\r
884 }\r
885 \r
886 /**\r
887   * @brief  Writes to a PHY register.\r
888   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
889   *         the configuration information for ETHERNET module\r
890   * @param  PHYReg: PHY register address, is the index of one of the 32 PHY register.\r
891   *          This parameter can be one of the following values:\r
892   *             PHY_BCR: Transceiver Control Register.\r
893   *             More PHY register could be written depending on the used PHY\r
894   * @param  RegValue: the value to write\r
895   * @retval HAL status\r
896   */\r
897 HAL_StatusTypeDef HAL_ETH_WritePHYRegister(ETH_HandleTypeDef *heth, uint16_t PHYReg, uint32_t RegValue)\r
898 {\r
899 uint32_t tmpreg = 0;\r
900 uint32_t tickstart = 0;\r
901 HAL_StatusTypeDef xResult;\r
902 \r
903         /* Check parameters */\r
904         assert_param( IS_ETH_PHY_ADDRESS( heth->Init.PhyAddress ) );\r
905 \r
906         /* Check the ETH peripheral state */\r
907         if( heth->State == HAL_ETH_STATE_BUSY_WR )\r
908         {\r
909                 xResult = HAL_BUSY;\r
910         }\r
911         else\r
912         {\r
913                 __HAL_LOCK( heth );\r
914 \r
915                 /* Set ETH HAL State to BUSY_WR */\r
916                 heth->State = HAL_ETH_STATE_BUSY_WR;\r
917 \r
918                 /* Get the ETHERNET MACMIIAR value */\r
919                 tmpreg = heth->Instance->MACMIIAR;\r
920 \r
921                 /* Keep only the CSR Clock Range CR[2:0] bits value */\r
922                 tmpreg &= ~ETH_MACMIIAR_CR_MASK;\r
923 \r
924                 /* Prepare the MII register address value */\r
925                 tmpreg |= ( ( ( uint32_t ) heth->Init.PhyAddress << 11 ) & ETH_MACMIIAR_PA ); /* Set the PHY device address */\r
926                 tmpreg |= ( ( ( uint32_t ) PHYReg << 6 ) & ETH_MACMIIAR_MR );                 /* Set the PHY register address */\r
927                 tmpreg |= ETH_MACMIIAR_MW;                                          /* Set the write mode */\r
928                 tmpreg |= ETH_MACMIIAR_MB;                                          /* Set the MII Busy bit */\r
929 \r
930                 /* Give the value to the MII data register */\r
931                 heth->Instance->MACMIIDR = ( uint16_t ) RegValue;\r
932 \r
933                 /* Write the result value into the MII Address register */\r
934                 heth->Instance->MACMIIAR = tmpreg;\r
935 \r
936                 /* Get tick */\r
937                 tickstart = HAL_GetTick();\r
938 \r
939                 /* Check for the Busy flag */\r
940                 while( 1 )\r
941                 {\r
942                         tmpreg = heth->Instance->MACMIIAR;\r
943 \r
944                         if( ( tmpreg & ETH_MACMIIAR_MB ) == 0ul )\r
945                         {\r
946                                 xResult = HAL_OK;\r
947                                 break;\r
948                         }\r
949                         /* Check for the Timeout */\r
950                         if( ( HAL_GetTick( ) - tickstart ) > PHY_WRITE_TO )\r
951                         {\r
952                                 xResult = HAL_TIMEOUT;\r
953                                 break;\r
954                         }\r
955                 }\r
956 \r
957                 /* Set ETH HAL State to READY */\r
958                 heth->State = HAL_ETH_STATE_READY;\r
959                 /* Process Unlocked */\r
960                 __HAL_UNLOCK( heth );\r
961         }\r
962 \r
963         /* Return function status */\r
964         return xResult;\r
965 }\r
966 \r
967 /**\r
968   * @}\r
969   */\r
970 \r
971 /** @defgroup ETH_Exported_Functions_Group3 Peripheral Control functions\r
972  *  @brief    Peripheral Control functions\r
973  *\r
974 @verbatim\r
975  ===============================================================================\r
976                   ##### Peripheral Control functions #####\r
977  ===============================================================================\r
978     [..]  This section provides functions allowing to:\r
979       (+) Enable MAC and DMA transmission and reception.\r
980           HAL_ETH_Start();\r
981       (+) Disable MAC and DMA transmission and reception.\r
982           HAL_ETH_Stop();\r
983       (+) Set the MAC configuration in runtime mode\r
984           HAL_ETH_ConfigMAC();\r
985       (+) Set the DMA configuration in runtime mode\r
986           HAL_ETH_ConfigDMA();\r
987 \r
988 @endverbatim\r
989   * @{\r
990   */\r
991 \r
992  /**\r
993   * @brief  Enables Ethernet MAC and DMA reception/transmission\r
994   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
995   *         the configuration information for ETHERNET module\r
996   * @retval HAL status\r
997   */\r
998 HAL_StatusTypeDef HAL_ETH_Start( ETH_HandleTypeDef *heth )\r
999 {\r
1000         /* Process Locked */\r
1001         __HAL_LOCK( heth );\r
1002 \r
1003         /* Set the ETH peripheral state to BUSY */\r
1004         heth->State = HAL_ETH_STATE_BUSY;\r
1005 \r
1006         /* Enable transmit state machine of the MAC for transmission on the MII */\r
1007         ETH_MACTransmissionEnable( heth );\r
1008 \r
1009         /* Enable receive state machine of the MAC for reception from the MII */\r
1010         ETH_MACReceptionEnable( heth );\r
1011 \r
1012         /* Flush Transmit FIFO */\r
1013         ETH_FlushTransmitFIFO( heth );\r
1014 \r
1015         /* Start DMA transmission */\r
1016         ETH_DMATransmissionEnable( heth );\r
1017 \r
1018         /* Start DMA reception */\r
1019         ETH_DMAReceptionEnable( heth );\r
1020 \r
1021         /* Set the ETH state to READY*/\r
1022         heth->State= HAL_ETH_STATE_READY;\r
1023 \r
1024         /* Process Unlocked */\r
1025         __HAL_UNLOCK( heth );\r
1026 \r
1027         /* Return function status */\r
1028         return HAL_OK;\r
1029 }\r
1030 \r
1031 /**\r
1032   * @brief  Stop Ethernet MAC and DMA reception/transmission\r
1033   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1034   *         the configuration information for ETHERNET module\r
1035   * @retval HAL status\r
1036   */\r
1037 HAL_StatusTypeDef HAL_ETH_Stop(ETH_HandleTypeDef *heth)\r
1038 {\r
1039   /* Process Locked */\r
1040   __HAL_LOCK( heth );\r
1041 \r
1042   /* Set the ETH peripheral state to BUSY */\r
1043   heth->State = HAL_ETH_STATE_BUSY;\r
1044 \r
1045   /* Stop DMA transmission */\r
1046   ETH_DMATransmissionDisable( heth );\r
1047 \r
1048   /* Stop DMA reception */\r
1049   ETH_DMAReceptionDisable( heth );\r
1050 \r
1051   /* Disable receive state machine of the MAC for reception from the MII */\r
1052   ETH_MACReceptionDisable( heth );\r
1053 \r
1054   /* Flush Transmit FIFO */\r
1055   ETH_FlushTransmitFIFO( heth );\r
1056 \r
1057   /* Disable transmit state machine of the MAC for transmission on the MII */\r
1058   ETH_MACTransmissionDisable( heth );\r
1059 \r
1060   /* Set the ETH state*/\r
1061   heth->State = HAL_ETH_STATE_READY;\r
1062 \r
1063   /* Process Unlocked */\r
1064   __HAL_UNLOCK( heth );\r
1065 \r
1066   /* Return function status */\r
1067   return HAL_OK;\r
1068 }\r
1069 \r
1070 static void prvWriteMACFCR( ETH_HandleTypeDef *heth, uint32_t ulValue)\r
1071 {\r
1072         /* Enable the MAC transmission */\r
1073         heth->Instance->MACFCR = ulValue;\r
1074 \r
1075         /* Wait until the write operation will be taken into account:\r
1076         at least four TX_CLK/RX_CLK clock cycles.\r
1077         Read it back, wait a ms and */\r
1078         ( void ) heth->Instance->MACFCR;\r
1079 \r
1080         HAL_Delay( ETH_REG_WRITE_DELAY );\r
1081 \r
1082         heth->Instance->MACFCR = ulValue;\r
1083 }\r
1084 \r
1085 static void prvWriteDMAOMR( ETH_HandleTypeDef *heth, uint32_t ulValue)\r
1086 {\r
1087         /* Enable the MAC transmission */\r
1088         heth->Instance->DMAOMR = ulValue;\r
1089 \r
1090         /* Wait until the write operation will be taken into account:\r
1091         at least four TX_CLK/RX_CLK clock cycles.\r
1092         Read it back, wait a ms and */\r
1093         ( void ) heth->Instance->DMAOMR;\r
1094 \r
1095         HAL_Delay( ETH_REG_WRITE_DELAY );\r
1096 \r
1097         heth->Instance->DMAOMR = ulValue;\r
1098 }\r
1099 \r
1100 static void prvWriteMACCR( ETH_HandleTypeDef *heth, uint32_t ulValue)\r
1101 {\r
1102         /* Enable the MAC transmission */\r
1103         heth->Instance->MACCR = ulValue;\r
1104 \r
1105         /* Wait until the write operation will be taken into account:\r
1106         at least four TX_CLK/RX_CLK clock cycles.\r
1107         Read it back, wait a ms and */\r
1108         ( void ) heth->Instance->MACCR;\r
1109 \r
1110         HAL_Delay( ETH_REG_WRITE_DELAY );\r
1111 \r
1112         heth->Instance->MACCR = ulValue;\r
1113 }\r
1114 \r
1115 /**\r
1116   * @brief  Set ETH MAC Configuration.\r
1117   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1118   *         the configuration information for ETHERNET module\r
1119   * @param  macconf: MAC Configuration structure\r
1120   * @retval HAL status\r
1121   */\r
1122 HAL_StatusTypeDef HAL_ETH_ConfigMAC(ETH_HandleTypeDef *heth, ETH_MACInitTypeDef *macconf)\r
1123 {\r
1124         uint32_t tmpreg = 0;\r
1125 \r
1126         /* Process Locked */\r
1127         __HAL_LOCK( heth );\r
1128 \r
1129         /* Set the ETH peripheral state to BUSY */\r
1130         heth->State= HAL_ETH_STATE_BUSY;\r
1131 \r
1132         assert_param(IS_ETH_SPEED(heth->Init.Speed));\r
1133         assert_param(IS_ETH_DUPLEX_MODE(heth->Init.DuplexMode));\r
1134 \r
1135         if (macconf != NULL)\r
1136         {\r
1137                 /* Check the parameters */\r
1138                 assert_param(IS_ETH_WATCHDOG(macconf->Watchdog));\r
1139                 assert_param(IS_ETH_JABBER(macconf->Jabber));\r
1140                 assert_param(IS_ETH_INTER_FRAME_GAP(macconf->InterFrameGap));\r
1141                 assert_param(IS_ETH_CARRIER_SENSE(macconf->CarrierSense));\r
1142                 assert_param(IS_ETH_RECEIVE_OWN(macconf->ReceiveOwn));\r
1143                 assert_param(IS_ETH_LOOPBACK_MODE(macconf->LoopbackMode));\r
1144                 assert_param(IS_ETH_CHECKSUM_OFFLOAD(macconf->ChecksumOffload));\r
1145                 assert_param(IS_ETH_RETRY_TRANSMISSION(macconf->RetryTransmission));\r
1146                 assert_param(IS_ETH_AUTOMATIC_PADCRC_STRIP(macconf->AutomaticPadCRCStrip));\r
1147                 assert_param(IS_ETH_BACKOFF_LIMIT(macconf->BackOffLimit));\r
1148                 assert_param(IS_ETH_DEFERRAL_CHECK(macconf->DeferralCheck));\r
1149                 assert_param(IS_ETH_RECEIVE_ALL(macconf->ReceiveAll));\r
1150                 assert_param(IS_ETH_SOURCE_ADDR_FILTER(macconf->SourceAddrFilter));\r
1151                 assert_param(IS_ETH_CONTROL_FRAMES(macconf->PassControlFrames));\r
1152                 assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(macconf->BroadcastFramesReception));\r
1153                 assert_param(IS_ETH_DESTINATION_ADDR_FILTER(macconf->DestinationAddrFilter));\r
1154                 assert_param(IS_ETH_PROMISCUOUS_MODE(macconf->PromiscuousMode));\r
1155                 assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(macconf->MulticastFramesFilter));\r
1156                 assert_param(IS_ETH_UNICAST_FRAMES_FILTER(macconf->UnicastFramesFilter));\r
1157                 assert_param(IS_ETH_PAUSE_TIME(macconf->PauseTime));\r
1158                 assert_param(IS_ETH_ZEROQUANTA_PAUSE(macconf->ZeroQuantaPause));\r
1159                 assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(macconf->PauseLowThreshold));\r
1160                 assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(macconf->UnicastPauseFrameDetect));\r
1161                 assert_param(IS_ETH_RECEIVE_FLOWCONTROL(macconf->ReceiveFlowControl));\r
1162                 assert_param(IS_ETH_TRANSMIT_FLOWCONTROL(macconf->TransmitFlowControl));\r
1163                 assert_param(IS_ETH_VLAN_TAG_COMPARISON(macconf->VLANTagComparison));\r
1164                 assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(macconf->VLANTagIdentifier));\r
1165 \r
1166                 /*------------------------ ETHERNET MACCR Configuration --------------------*/\r
1167                 /* Get the ETHERNET MACCR value */\r
1168                 tmpreg = heth->Instance->MACCR;\r
1169                 /* Clear WD, PCE, PS, TE and RE bits */\r
1170                 tmpreg &= ETH_MACCR_CLEAR_MASK;\r
1171 \r
1172                 tmpreg |= (uint32_t)(\r
1173                         macconf->Watchdog |\r
1174                         macconf->Jabber |\r
1175                         macconf->InterFrameGap |\r
1176                         macconf->CarrierSense |\r
1177                         heth->Init.Speed |\r
1178                         macconf->ReceiveOwn |\r
1179                         macconf->LoopbackMode |\r
1180                         heth->Init.DuplexMode |\r
1181                         macconf->ChecksumOffload |\r
1182                         macconf->RetryTransmission |\r
1183                         macconf->AutomaticPadCRCStrip |\r
1184                         macconf->BackOffLimit |\r
1185                         macconf->DeferralCheck);\r
1186 \r
1187                 /* Write to ETHERNET MACCR */\r
1188                 prvWriteMACCR( heth, tmpreg );\r
1189 \r
1190                 /*----------------------- ETHERNET MACFFR Configuration --------------------*/\r
1191                 /* Write to ETHERNET MACFFR */\r
1192                 heth->Instance->MACFFR = (uint32_t)(\r
1193                         macconf->ReceiveAll |\r
1194                         macconf->SourceAddrFilter |\r
1195                         macconf->PassControlFrames |\r
1196                         macconf->BroadcastFramesReception |\r
1197                         macconf->DestinationAddrFilter |\r
1198                         macconf->PromiscuousMode |\r
1199                         macconf->MulticastFramesFilter |\r
1200                         macconf->UnicastFramesFilter);\r
1201 \r
1202                 /* Wait until the write operation will be taken into account :\r
1203                 at least four TX_CLK/RX_CLK clock cycles */\r
1204                 tmpreg = heth->Instance->MACFFR;\r
1205                 HAL_Delay(ETH_REG_WRITE_DELAY);\r
1206                 heth->Instance->MACFFR = tmpreg;\r
1207 \r
1208                 /*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/\r
1209                 /* Write to ETHERNET MACHTHR */\r
1210                 heth->Instance->MACHTHR = (uint32_t)macconf->HashTableHigh;\r
1211 \r
1212                 /* Write to ETHERNET MACHTLR */\r
1213                 heth->Instance->MACHTLR = (uint32_t)macconf->HashTableLow;\r
1214                 /*----------------------- ETHERNET MACFCR Configuration --------------------*/\r
1215 \r
1216                 /* Get the ETHERNET MACFCR value */\r
1217                 tmpreg = heth->Instance->MACFCR;\r
1218                 /* Clear xx bits */\r
1219                 tmpreg &= ETH_MACFCR_CLEAR_MASK;\r
1220 \r
1221                 tmpreg |= (uint32_t)((\r
1222                         macconf->PauseTime << 16) |\r
1223                         macconf->ZeroQuantaPause |\r
1224                         macconf->PauseLowThreshold |\r
1225                         macconf->UnicastPauseFrameDetect |\r
1226                         macconf->ReceiveFlowControl |\r
1227                         macconf->TransmitFlowControl);\r
1228 \r
1229                 /* Write to ETHERNET MACFCR */\r
1230                 prvWriteMACFCR( heth, tmpreg );\r
1231 \r
1232                 /*----------------------- ETHERNET MACVLANTR Configuration -----------------*/\r
1233                 heth->Instance->MACVLANTR = (uint32_t)(macconf->VLANTagComparison |\r
1234                 macconf->VLANTagIdentifier);\r
1235 \r
1236                 /* Wait until the write operation will be taken into account :\r
1237                 at least four TX_CLK/RX_CLK clock cycles */\r
1238                 tmpreg = heth->Instance->MACVLANTR;\r
1239                 HAL_Delay(ETH_REG_WRITE_DELAY);\r
1240                 heth->Instance->MACVLANTR = tmpreg;\r
1241         }\r
1242         else /* macconf == NULL : here we just configure Speed and Duplex mode */\r
1243         {\r
1244                 /*------------------------ ETHERNET MACCR Configuration --------------------*/\r
1245                 /* Get the ETHERNET MACCR value */\r
1246                 tmpreg = heth->Instance->MACCR;\r
1247 \r
1248                 /* Clear FES and DM bits */\r
1249                 tmpreg &= ~((uint32_t)0x00004800);\r
1250 \r
1251                 tmpreg |= (uint32_t)(heth->Init.Speed | heth->Init.DuplexMode);\r
1252 \r
1253                 /* Write to ETHERNET MACCR */\r
1254                 prvWriteMACCR( heth, tmpreg );\r
1255         }\r
1256 \r
1257         /* Set the ETH state to Ready */\r
1258         heth->State= HAL_ETH_STATE_READY;\r
1259 \r
1260         /* Process Unlocked */\r
1261         __HAL_UNLOCK( heth );\r
1262 \r
1263         /* Return function status */\r
1264         return HAL_OK;\r
1265 }\r
1266 \r
1267 /**\r
1268   * @brief  Sets ETH DMA Configuration.\r
1269   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1270   *         the configuration information for ETHERNET module\r
1271   * @param  dmaconf: DMA Configuration structure\r
1272   * @retval HAL status\r
1273   */\r
1274 HAL_StatusTypeDef HAL_ETH_ConfigDMA(ETH_HandleTypeDef *heth, ETH_DMAInitTypeDef *dmaconf)\r
1275 {\r
1276         uint32_t tmpreg = 0;\r
1277 \r
1278         /* Process Locked */\r
1279         __HAL_LOCK( heth );\r
1280 \r
1281         /* Set the ETH peripheral state to BUSY */\r
1282         heth->State= HAL_ETH_STATE_BUSY;\r
1283 \r
1284         /* Check parameters */\r
1285         assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(dmaconf->DropTCPIPChecksumErrorFrame));\r
1286         assert_param(IS_ETH_RECEIVE_STORE_FORWARD(dmaconf->ReceiveStoreForward));\r
1287         assert_param(IS_ETH_FLUSH_RECEIVE_FRAME(dmaconf->FlushReceivedFrame));\r
1288         assert_param(IS_ETH_TRANSMIT_STORE_FORWARD(dmaconf->TransmitStoreForward));\r
1289         assert_param(IS_ETH_TRANSMIT_THRESHOLD_CONTROL(dmaconf->TransmitThresholdControl));\r
1290         assert_param(IS_ETH_FORWARD_ERROR_FRAMES(dmaconf->ForwardErrorFrames));\r
1291         assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(dmaconf->ForwardUndersizedGoodFrames));\r
1292         assert_param(IS_ETH_RECEIVE_THRESHOLD_CONTROL(dmaconf->ReceiveThresholdControl));\r
1293         assert_param(IS_ETH_SECOND_FRAME_OPERATE(dmaconf->SecondFrameOperate));\r
1294         assert_param(IS_ETH_ADDRESS_ALIGNED_BEATS(dmaconf->AddressAlignedBeats));\r
1295         assert_param(IS_ETH_FIXED_BURST(dmaconf->FixedBurst));\r
1296         assert_param(IS_ETH_RXDMA_BURST_LENGTH(dmaconf->RxDMABurstLength));\r
1297         assert_param(IS_ETH_TXDMA_BURST_LENGTH(dmaconf->TxDMABurstLength));\r
1298         assert_param(IS_ETH_ENHANCED_DESCRIPTOR_FORMAT(dmaconf->EnhancedDescriptorFormat));\r
1299         assert_param(IS_ETH_DMA_DESC_SKIP_LENGTH(dmaconf->DescriptorSkipLength));\r
1300         assert_param(IS_ETH_DMA_ARBITRATION_ROUNDROBIN_RXTX(dmaconf->DMAArbitration));\r
1301 \r
1302         /*----------------------- ETHERNET DMAOMR Configuration --------------------*/\r
1303         /* Get the ETHERNET DMAOMR value */\r
1304         tmpreg = heth->Instance->DMAOMR;\r
1305         /* Clear xx bits */\r
1306         tmpreg &= ETH_DMAOMR_CLEAR_MASK;\r
1307 \r
1308         tmpreg |= (uint32_t)(\r
1309                 dmaconf->DropTCPIPChecksumErrorFrame |\r
1310                 dmaconf->ReceiveStoreForward |\r
1311                 dmaconf->FlushReceivedFrame |\r
1312                 dmaconf->TransmitStoreForward |\r
1313                 dmaconf->TransmitThresholdControl |\r
1314                 dmaconf->ForwardErrorFrames |\r
1315                 dmaconf->ForwardUndersizedGoodFrames |\r
1316                 dmaconf->ReceiveThresholdControl |\r
1317                 dmaconf->SecondFrameOperate);\r
1318 \r
1319         /* Write to ETHERNET DMAOMR */\r
1320         prvWriteDMAOMR( heth, tmpreg );\r
1321 \r
1322         /*----------------------- ETHERNET DMABMR Configuration --------------------*/\r
1323         heth->Instance->DMABMR = (uint32_t)(dmaconf->AddressAlignedBeats |\r
1324         dmaconf->FixedBurst |\r
1325         dmaconf->RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */\r
1326         dmaconf->TxDMABurstLength |\r
1327         dmaconf->EnhancedDescriptorFormat |\r
1328         (dmaconf->DescriptorSkipLength << 2) |\r
1329         dmaconf->DMAArbitration |\r
1330         ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */\r
1331 \r
1332         /* Wait until the write operation will be taken into account:\r
1333         at least four TX_CLK/RX_CLK clock cycles */\r
1334         tmpreg = heth->Instance->DMABMR;\r
1335         HAL_Delay(ETH_REG_WRITE_DELAY);\r
1336         heth->Instance->DMABMR = tmpreg;\r
1337 \r
1338         /* Set the ETH state to Ready */\r
1339         heth->State= HAL_ETH_STATE_READY;\r
1340 \r
1341         /* Process Unlocked */\r
1342         __HAL_UNLOCK( heth );\r
1343 \r
1344         /* Return function status */\r
1345         return HAL_OK;\r
1346 }\r
1347 \r
1348 /**\r
1349   * @}\r
1350   */\r
1351 \r
1352 /** @defgroup ETH_Exported_Functions_Group4 Peripheral State functions\r
1353   *  @brief   Peripheral State functions\r
1354   *\r
1355   @verbatim\r
1356   ===============================================================================\r
1357                          ##### Peripheral State functions #####\r
1358   ===============================================================================\r
1359   [..]\r
1360   This subsection permits to get in run-time the status of the peripheral\r
1361   and the data flow.\r
1362        (+) Get the ETH handle state:\r
1363            HAL_ETH_GetState();\r
1364 \r
1365 \r
1366   @endverbatim\r
1367   * @{\r
1368   */\r
1369 \r
1370 /**\r
1371   * @brief  Return the ETH HAL state\r
1372   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1373   *         the configuration information for ETHERNET module\r
1374   * @retval HAL state\r
1375   */\r
1376 HAL_ETH_StateTypeDef HAL_ETH_GetState(ETH_HandleTypeDef *heth)\r
1377 {\r
1378   /* Return ETH state */\r
1379   return heth->State;\r
1380 }\r
1381 \r
1382 /**\r
1383   * @}\r
1384   */\r
1385 \r
1386 /**\r
1387   * @}\r
1388   */\r
1389 \r
1390 /** @addtogroup ETH_Private_Functions\r
1391   * @{\r
1392   */\r
1393 \r
1394 /**\r
1395   * @brief  Configures Ethernet MAC and DMA with default parameters.\r
1396   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1397   *         the configuration information for ETHERNET module\r
1398   * @param  err: Ethernet Init error\r
1399   * @retval HAL status\r
1400   */\r
1401 static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err)\r
1402 {\r
1403   ETH_MACInitTypeDef macinit;\r
1404   ETH_DMAInitTypeDef dmainit;\r
1405   uint32_t tmpreg = 0;\r
1406 \r
1407   if (err != ETH_SUCCESS) /* Auto-negotiation failed */\r
1408   {\r
1409     /* Set Ethernet duplex mode to Full-duplex */\r
1410     heth->Init.DuplexMode = ETH_MODE_FULLDUPLEX;\r
1411 \r
1412     /* Set Ethernet speed to 100M */\r
1413     heth->Init.Speed = ETH_SPEED_100M;\r
1414   }\r
1415 \r
1416   /* Ethernet MAC default initialization **************************************/\r
1417   macinit.Watchdog = ETH_WATCHDOG_ENABLE;\r
1418   macinit.Jabber = ETH_JABBER_ENABLE;\r
1419   macinit.InterFrameGap = ETH_INTERFRAMEGAP_96BIT;\r
1420   macinit.CarrierSense = ETH_CARRIERSENCE_ENABLE;\r
1421   macinit.ReceiveOwn = ETH_RECEIVEOWN_ENABLE;\r
1422   macinit.LoopbackMode = ETH_LOOPBACKMODE_DISABLE;\r
1423   if(heth->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)\r
1424   {\r
1425     macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_ENABLE;\r
1426   }\r
1427   else\r
1428   {\r
1429     macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_DISABLE;\r
1430   }\r
1431   macinit.RetryTransmission = ETH_RETRYTRANSMISSION_DISABLE;\r
1432   macinit.AutomaticPadCRCStrip = ETH_AUTOMATICPADCRCSTRIP_DISABLE;\r
1433   macinit.BackOffLimit = ETH_BACKOFFLIMIT_10;\r
1434   macinit.DeferralCheck = ETH_DEFFERRALCHECK_DISABLE;\r
1435   macinit.ReceiveAll = ETH_RECEIVEAll_DISABLE;\r
1436   macinit.SourceAddrFilter = ETH_SOURCEADDRFILTER_DISABLE;\r
1437   macinit.PassControlFrames = ETH_PASSCONTROLFRAMES_BLOCKALL;\r
1438   macinit.BroadcastFramesReception = ETH_BROADCASTFRAMESRECEPTION_ENABLE;\r
1439   macinit.DestinationAddrFilter = ETH_DESTINATIONADDRFILTER_NORMAL;\r
1440   macinit.PromiscuousMode = ETH_PROMISCUOUS_MODE_DISABLE;\r
1441   macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_PERFECT;\r
1442   macinit.UnicastFramesFilter = ETH_UNICASTFRAMESFILTER_PERFECT;\r
1443   macinit.HashTableHigh = 0x0;\r
1444   macinit.HashTableLow = 0x0;\r
1445   macinit.PauseTime = 0x0;\r
1446   macinit.ZeroQuantaPause = ETH_ZEROQUANTAPAUSE_DISABLE;\r
1447   macinit.PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS4;\r
1448   macinit.UnicastPauseFrameDetect = ETH_UNICASTPAUSEFRAMEDETECT_DISABLE;\r
1449   macinit.ReceiveFlowControl = ETH_RECEIVEFLOWCONTROL_DISABLE;\r
1450   macinit.TransmitFlowControl = ETH_TRANSMITFLOWCONTROL_DISABLE;\r
1451   macinit.VLANTagComparison = ETH_VLANTAGCOMPARISON_16BIT;\r
1452   macinit.VLANTagIdentifier = 0x0;\r
1453 \r
1454   /*------------------------ ETHERNET MACCR Configuration --------------------*/\r
1455   /* Get the ETHERNET MACCR value */\r
1456   tmpreg = heth->Instance->MACCR;\r
1457   /* Clear WD, PCE, PS, TE and RE bits */\r
1458   tmpreg &= ETH_MACCR_CLEAR_MASK;\r
1459   /* Set the WD bit according to ETH Watchdog value */\r
1460   /* Set the JD: bit according to ETH Jabber value */\r
1461   /* Set the IFG bit according to ETH InterFrameGap value */\r
1462   /* Set the DCRS bit according to ETH CarrierSense value */\r
1463   /* Set the FES bit according to ETH Speed value */\r
1464   /* Set the DO bit according to ETH ReceiveOwn value */\r
1465   /* Set the LM bit according to ETH LoopbackMode value */\r
1466   /* Set the DM bit according to ETH Mode value */\r
1467   /* Set the IPCO bit according to ETH ChecksumOffload value */\r
1468   /* Set the DR bit according to ETH RetryTransmission value */\r
1469   /* Set the ACS bit according to ETH AutomaticPadCRCStrip value */\r
1470   /* Set the BL bit according to ETH BackOffLimit value */\r
1471   /* Set the DC bit according to ETH DeferralCheck value */\r
1472   tmpreg |= (uint32_t)(macinit.Watchdog |\r
1473                        macinit.Jabber |\r
1474                        macinit.InterFrameGap |\r
1475                        macinit.CarrierSense |\r
1476                        heth->Init.Speed |\r
1477                        macinit.ReceiveOwn |\r
1478                        macinit.LoopbackMode |\r
1479                        heth->Init.DuplexMode |\r
1480                        macinit.ChecksumOffload |\r
1481                        macinit.RetryTransmission |\r
1482                        macinit.AutomaticPadCRCStrip |\r
1483                        macinit.BackOffLimit |\r
1484                        macinit.DeferralCheck);\r
1485 \r
1486   /* Write to ETHERNET MACCR */\r
1487   prvWriteMACCR( heth, tmpreg );\r
1488 \r
1489   /*----------------------- ETHERNET MACFFR Configuration --------------------*/\r
1490   /* Set the RA bit according to ETH ReceiveAll value */\r
1491   /* Set the SAF and SAIF bits according to ETH SourceAddrFilter value */\r
1492   /* Set the PCF bit according to ETH PassControlFrames value */\r
1493   /* Set the DBF bit according to ETH BroadcastFramesReception value */\r
1494   /* Set the DAIF bit according to ETH DestinationAddrFilter value */\r
1495   /* Set the PR bit according to ETH PromiscuousMode value */\r
1496   /* Set the PM, HMC and HPF bits according to ETH MulticastFramesFilter value */\r
1497   /* Set the HUC and HPF bits according to ETH UnicastFramesFilter value */\r
1498   /* Write to ETHERNET MACFFR */\r
1499   heth->Instance->MACFFR = (uint32_t)(macinit.ReceiveAll |\r
1500                                         macinit.SourceAddrFilter |\r
1501                                         macinit.PassControlFrames |\r
1502                                         macinit.BroadcastFramesReception |\r
1503                                         macinit.DestinationAddrFilter |\r
1504                                         macinit.PromiscuousMode |\r
1505                                         macinit.MulticastFramesFilter |\r
1506                                         macinit.UnicastFramesFilter);\r
1507 \r
1508    /* Wait until the write operation will be taken into account:\r
1509       at least four TX_CLK/RX_CLK clock cycles */\r
1510    tmpreg = heth->Instance->MACFFR;\r
1511    HAL_Delay(ETH_REG_WRITE_DELAY);\r
1512    heth->Instance->MACFFR = tmpreg;\r
1513 \r
1514    /*--------------- ETHERNET MACHTHR and MACHTLR Configuration --------------*/\r
1515    /* Write to ETHERNET MACHTHR */\r
1516    heth->Instance->MACHTHR = (uint32_t)macinit.HashTableHigh;\r
1517 \r
1518    /* Write to ETHERNET MACHTLR */\r
1519    heth->Instance->MACHTLR = (uint32_t)macinit.HashTableLow;\r
1520    /*----------------------- ETHERNET MACFCR Configuration -------------------*/\r
1521 \r
1522    /* Get the ETHERNET MACFCR value */\r
1523    tmpreg = heth->Instance->MACFCR;\r
1524    /* Clear xx bits */\r
1525    tmpreg &= ETH_MACFCR_CLEAR_MASK;\r
1526 \r
1527    /* Set the PT bit according to ETH PauseTime value */\r
1528    /* Set the DZPQ bit according to ETH ZeroQuantaPause value */\r
1529    /* Set the PLT bit according to ETH PauseLowThreshold value */\r
1530    /* Set the UP bit according to ETH UnicastPauseFrameDetect value */\r
1531    /* Set the RFE bit according to ETH ReceiveFlowControl value */\r
1532    /* Set the TFE bit according to ETH TransmitFlowControl value */\r
1533    tmpreg |= (uint32_t)((macinit.PauseTime << 16) |\r
1534                         macinit.ZeroQuantaPause |\r
1535                         macinit.PauseLowThreshold |\r
1536                         macinit.UnicastPauseFrameDetect |\r
1537                         macinit.ReceiveFlowControl |\r
1538                         macinit.TransmitFlowControl);\r
1539 \r
1540    /* Write to ETHERNET MACFCR */\r
1541    prvWriteMACFCR( heth, tmpreg );\r
1542 \r
1543    /*----------------------- ETHERNET MACVLANTR Configuration ----------------*/\r
1544    /* Set the ETV bit according to ETH VLANTagComparison value */\r
1545    /* Set the VL bit according to ETH VLANTagIdentifier value */\r
1546    heth->Instance->MACVLANTR = (uint32_t)(macinit.VLANTagComparison |\r
1547                                             macinit.VLANTagIdentifier);\r
1548 \r
1549     /* Wait until the write operation will be taken into account:\r
1550        at least four TX_CLK/RX_CLK clock cycles */\r
1551     tmpreg = heth->Instance->MACVLANTR;\r
1552     HAL_Delay(ETH_REG_WRITE_DELAY);\r
1553     heth->Instance->MACVLANTR = tmpreg;\r
1554 \r
1555     /* Ethernet DMA default initialization ************************************/\r
1556     dmainit.DropTCPIPChecksumErrorFrame = ETH_DROPTCPIPCHECKSUMERRORFRAME_ENABLE;\r
1557     dmainit.ReceiveStoreForward = ETH_RECEIVESTOREFORWARD_ENABLE;\r
1558     dmainit.FlushReceivedFrame = ETH_FLUSHRECEIVEDFRAME_ENABLE;\r
1559     dmainit.TransmitStoreForward = ETH_TRANSMITSTOREFORWARD_ENABLE;\r
1560     dmainit.TransmitThresholdControl = ETH_TRANSMITTHRESHOLDCONTROL_64BYTES;\r
1561     dmainit.ForwardErrorFrames = ETH_FORWARDERRORFRAMES_DISABLE;\r
1562     dmainit.ForwardUndersizedGoodFrames = ETH_FORWARDUNDERSIZEDGOODFRAMES_DISABLE;\r
1563     dmainit.ReceiveThresholdControl = ETH_RECEIVEDTHRESHOLDCONTROL_64BYTES;\r
1564     dmainit.SecondFrameOperate = ETH_SECONDFRAMEOPERARTE_ENABLE;\r
1565     dmainit.AddressAlignedBeats = ETH_ADDRESSALIGNEDBEATS_ENABLE;\r
1566     dmainit.FixedBurst = ETH_FIXEDBURST_ENABLE;\r
1567     dmainit.RxDMABurstLength = ETH_RXDMABURSTLENGTH_32BEAT;\r
1568     dmainit.TxDMABurstLength = ETH_TXDMABURSTLENGTH_32BEAT;\r
1569     dmainit.EnhancedDescriptorFormat = ETH_DMAENHANCEDDESCRIPTOR_ENABLE;\r
1570     dmainit.DescriptorSkipLength = 0x0;\r
1571     dmainit.DMAArbitration = ETH_DMAARBITRATION_ROUNDROBIN_RXTX_1_1;\r
1572 \r
1573     /* Get the ETHERNET DMAOMR value */\r
1574     tmpreg = heth->Instance->DMAOMR;\r
1575     /* Clear xx bits */\r
1576     tmpreg &= ETH_DMAOMR_CLEAR_MASK;\r
1577 \r
1578     /* Set the DT bit according to ETH DropTCPIPChecksumErrorFrame value */\r
1579     /* Set the RSF bit according to ETH ReceiveStoreForward value */\r
1580     /* Set the DFF bit according to ETH FlushReceivedFrame value */\r
1581     /* Set the TSF bit according to ETH TransmitStoreForward value */\r
1582     /* Set the TTC bit according to ETH TransmitThresholdControl value */\r
1583     /* Set the FEF bit according to ETH ForwardErrorFrames value */\r
1584     /* Set the FUF bit according to ETH ForwardUndersizedGoodFrames value */\r
1585     /* Set the RTC bit according to ETH ReceiveThresholdControl value */\r
1586     /* Set the OSF bit according to ETH SecondFrameOperate value */\r
1587     tmpreg |= (uint32_t)(dmainit.DropTCPIPChecksumErrorFrame |\r
1588                          dmainit.ReceiveStoreForward |\r
1589                          dmainit.FlushReceivedFrame |\r
1590                          dmainit.TransmitStoreForward |\r
1591                          dmainit.TransmitThresholdControl |\r
1592                          dmainit.ForwardErrorFrames |\r
1593                          dmainit.ForwardUndersizedGoodFrames |\r
1594                          dmainit.ReceiveThresholdControl |\r
1595                          dmainit.SecondFrameOperate);\r
1596 \r
1597     /* Write to ETHERNET DMAOMR */\r
1598     prvWriteDMAOMR( heth, tmpreg );\r
1599 \r
1600     /*----------------------- ETHERNET DMABMR Configuration ------------------*/\r
1601     /* Set the AAL bit according to ETH AddressAlignedBeats value */\r
1602     /* Set the FB bit according to ETH FixedBurst value */\r
1603     /* Set the RPBL and 4*PBL bits according to ETH RxDMABurstLength value */\r
1604     /* Set the PBL and 4*PBL bits according to ETH TxDMABurstLength value */\r
1605     /* Set the Enhanced DMA descriptors bit according to ETH EnhancedDescriptorFormat value*/\r
1606     /* Set the DSL bit according to ETH DesciptorSkipLength value */\r
1607     /* Set the PR and DA bits according to ETH DMAArbitration value */\r
1608     heth->Instance->DMABMR = (uint32_t)(dmainit.AddressAlignedBeats |\r
1609                                           dmainit.FixedBurst |\r
1610                                           dmainit.RxDMABurstLength |    /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */\r
1611                                           dmainit.TxDMABurstLength |\r
1612                                           dmainit.EnhancedDescriptorFormat |\r
1613                                           (dmainit.DescriptorSkipLength << 2) |\r
1614                                           dmainit.DMAArbitration |\r
1615                                           ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */\r
1616 \r
1617      /* Wait until the write operation will be taken into account:\r
1618         at least four TX_CLK/RX_CLK clock cycles */\r
1619      tmpreg = heth->Instance->DMABMR;\r
1620      HAL_Delay(ETH_REG_WRITE_DELAY);\r
1621      heth->Instance->DMABMR = tmpreg;\r
1622 \r
1623      if(heth->Init.RxMode == ETH_RXINTERRUPT_MODE)\r
1624      {\r
1625        /* Enable the Ethernet Rx Interrupt */\r
1626        __HAL_ETH_DMA_ENABLE_IT(( heth ), ETH_DMA_IT_NIS | ETH_DMA_IT_R);\r
1627      }\r
1628 \r
1629      /* Initialize MAC address in ethernet MAC */\r
1630      ETH_MACAddressConfig(heth, ETH_MAC_ADDRESS0, heth->Init.MACAddr);\r
1631 }\r
1632 \r
1633 /**\r
1634   * @brief  Configures the selected MAC address.\r
1635   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1636   *         the configuration information for ETHERNET module\r
1637   * @param  MacAddr: The MAC address to configure\r
1638   *          This parameter can be one of the following values:\r
1639   *             @arg ETH_MAC_Address0: MAC Address0\r
1640   *             @arg ETH_MAC_Address1: MAC Address1\r
1641   *             @arg ETH_MAC_Address2: MAC Address2\r
1642   *             @arg ETH_MAC_Address3: MAC Address3\r
1643   * @param  Addr: Pointer to MAC address buffer data (6 bytes)\r
1644   * @retval HAL status\r
1645   */\r
1646 static void ETH_MACAddressConfig(ETH_HandleTypeDef *heth, uint32_t MacAddr, uint8_t *Addr)\r
1647 {\r
1648         uint32_t tmpreg;\r
1649 \r
1650         /* Check the parameters */\r
1651         assert_param( IS_ETH_MAC_ADDRESS0123( MacAddr ) );\r
1652 \r
1653         /* Calculate the selected MAC address high register */\r
1654         tmpreg = 0x80000000ul | ( ( uint32_t )Addr[ 5 ] << 8) | (uint32_t)Addr[ 4 ];\r
1655         /* Load the selected MAC address high register */\r
1656         ( * ( __IO uint32_t * ) ( ( uint32_t ) ( ETH_MAC_ADDR_HBASE + MacAddr ) ) ) = tmpreg;\r
1657         /* Calculate the selected MAC address low register */\r
1658         tmpreg = ( ( uint32_t )Addr[ 3 ] << 24 ) | ( ( uint32_t )Addr[ 2 ] << 16 ) | ( ( uint32_t )Addr[ 1 ] << 8 ) | Addr[ 0 ];\r
1659 \r
1660         /* Load the selected MAC address low register */\r
1661         ( * ( __IO uint32_t * ) ( ( uint32_t ) ( ETH_MAC_ADDR_LBASE + MacAddr ) ) ) = tmpreg;\r
1662 }\r
1663 \r
1664 /**\r
1665   * @brief  Enables the MAC transmission.\r
1666   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1667   *         the configuration information for ETHERNET module\r
1668   * @retval None\r
1669   */\r
1670 static void ETH_MACTransmissionEnable(ETH_HandleTypeDef *heth)\r
1671 {\r
1672         uint32_t tmpreg = heth->Instance->MACCR | ETH_MACCR_TE;\r
1673 \r
1674         prvWriteMACCR( heth, tmpreg );\r
1675 }\r
1676 \r
1677 /**\r
1678   * @brief  Disables the MAC transmission.\r
1679   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1680   *         the configuration information for ETHERNET module\r
1681   * @retval None\r
1682   */\r
1683 static void ETH_MACTransmissionDisable(ETH_HandleTypeDef *heth)\r
1684 {\r
1685         uint32_t tmpreg = heth->Instance->MACCR & ~( ETH_MACCR_TE );\r
1686 \r
1687         prvWriteMACCR( heth, tmpreg );\r
1688 }\r
1689 \r
1690 /**\r
1691   * @brief  Enables the MAC reception.\r
1692   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1693   *         the configuration information for ETHERNET module\r
1694   * @retval None\r
1695   */\r
1696 static void ETH_MACReceptionEnable(ETH_HandleTypeDef *heth)\r
1697 {\r
1698         __IO uint32_t tmpreg = heth->Instance->MACCR | ETH_MACCR_RE;\r
1699 \r
1700         prvWriteMACCR( heth, tmpreg );\r
1701 }\r
1702 \r
1703 /**\r
1704   * @brief  Disables the MAC reception.\r
1705   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1706   *         the configuration information for ETHERNET module\r
1707   * @retval None\r
1708   */\r
1709 static void ETH_MACReceptionDisable(ETH_HandleTypeDef *heth)\r
1710 {\r
1711         __IO uint32_t tmpreg = heth->Instance->MACCR & ~( ETH_MACCR_RE );\r
1712 \r
1713         prvWriteMACCR( heth, tmpreg );\r
1714 }\r
1715 \r
1716 /**\r
1717   * @brief  Enables the DMA transmission.\r
1718   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1719   *         the configuration information for ETHERNET module\r
1720   * @retval None\r
1721   */\r
1722 static void ETH_DMATransmissionEnable(ETH_HandleTypeDef *heth)\r
1723 {\r
1724         /* Enable the DMA transmission */\r
1725         __IO uint32_t tmpreg = heth->Instance->DMAOMR | ETH_DMAOMR_ST;\r
1726 \r
1727         prvWriteDMAOMR( heth, tmpreg );\r
1728 }\r
1729 \r
1730 /**\r
1731   * @brief  Disables the DMA transmission.\r
1732   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1733   *         the configuration information for ETHERNET module\r
1734   * @retval None\r
1735   */\r
1736 static void ETH_DMATransmissionDisable(ETH_HandleTypeDef *heth)\r
1737 {\r
1738         /* Disable the DMA transmission */\r
1739         __IO uint32_t tmpreg = heth->Instance->DMAOMR & ~( ETH_DMAOMR_ST );\r
1740 \r
1741         prvWriteDMAOMR( heth, tmpreg );\r
1742 }\r
1743 \r
1744 /**\r
1745   * @brief  Enables the DMA reception.\r
1746   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1747   *         the configuration information for ETHERNET module\r
1748   * @retval None\r
1749   */\r
1750 static void ETH_DMAReceptionEnable(ETH_HandleTypeDef *heth)\r
1751 {\r
1752         /* Enable the DMA reception */\r
1753         __IO uint32_t tmpreg = heth->Instance->DMAOMR | ETH_DMAOMR_SR;\r
1754 \r
1755         prvWriteDMAOMR( heth, tmpreg );\r
1756 }\r
1757 \r
1758 /**\r
1759   * @brief  Disables the DMA reception.\r
1760   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1761   *         the configuration information for ETHERNET module\r
1762   * @retval None\r
1763   */\r
1764 static void ETH_DMAReceptionDisable(ETH_HandleTypeDef *heth)\r
1765 {\r
1766         /* Disable the DMA reception */\r
1767         __IO uint32_t tmpreg = heth->Instance->DMAOMR & ~( ETH_DMAOMR_SR );\r
1768 \r
1769         prvWriteDMAOMR( heth, tmpreg );\r
1770 }\r
1771 \r
1772 /**\r
1773   * @brief  Clears the ETHERNET transmit FIFO.\r
1774   * @param  heth: pointer to a ETH_HandleTypeDef structure that contains\r
1775   *         the configuration information for ETHERNET module\r
1776   * @retval None\r
1777   */\r
1778 static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef *heth)\r
1779 {\r
1780         /* Set the Flush Transmit FIFO bit */\r
1781         __IO uint32_t tmpreg = heth->Instance->DMAOMR | ETH_DMAOMR_FTF;\r
1782 \r
1783         prvWriteDMAOMR( heth, tmpreg );\r
1784 }\r
1785 \r
1786 /**\r
1787   * @}\r
1788   */\r
1789 #endif /* stm_is_F2 != 0 || stm_is_F4 != 0 || stm_is_F7 */\r
1790 \r
1791 #endif /* HAL_ETH_MODULE_ENABLED */\r
1792 /**\r
1793   * @}\r
1794   */\r
1795 \r
1796 /**\r
1797   * @}\r
1798   */\r
1799 \r
1800 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r