stm32f1xx_hal_eth.c 70 KB

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