stm32h7xx_hal_eth.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_eth.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 21-April-2017
  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 deinitialization 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. The ETH HAL driver can be used as follows:
  21. (#)Declare a ETH_HandleTypeDef handle structure, for example:
  22. ETH_HandleTypeDef heth;
  23. (#)Fill parameters of Init structure in heth handle
  24. (#)Call HAL_ETH_Init() API to initialize the Ethernet peripheral (MAC, DMA, ...)
  25. (#)Initialize the ETH low level resources through the HAL_ETH_MspInit() API:
  26. (##) Enable the Ethernet interface clock using
  27. (+++) __HAL_RCC_ETH1MAC_CLK_ENABLE()
  28. (+++) __HAL_RCC_ETH1TX_CLK_ENABLE()
  29. (+++) __HAL_RCC_ETH1RX_CLK_ENABLE()
  30. (##) Initialize the related GPIO clocks
  31. (##) Configure Ethernet pinout
  32. (##) Configure Ethernet NVIC interrupt (in Interrupt mode)
  33. (#) Ethernet data reception is asynchronous, so call the following API
  34. to start the listening mode:
  35. (##) HAL_ETH_Start():
  36. This API starts the MAC and DMA transmission and reception process,
  37. without enabling end of transfer interrupts, in this mode user
  38. has to poll for data availability by calling HAL_ETH_IsRxDataAvailable()
  39. (##) HAL_ETH_Start_IT():
  40. This API starts the MAC and DMA transmission and reception process,
  41. end of transfer interrupts are enabled in this mode,
  42. HAL_ETH_RxCpltCallback() will be executed when an Ethernet packet is received
  43. (#) When data is received (HAL_ETH_IsRxDataAvailable() returns 1 or Rx interrupt
  44. occurred), user can call the following APIs to get received data:
  45. (##) HAL_ETH_GetRxDataBuffer(): Get buffer address of received frame
  46. (##) HAL_ETH_GetRxDataLength(): Get received frame length
  47. (##) HAL_ETH_GetRxDataInfo(): Get received frame additional info,
  48. please refer to ETH_RxPacketInfo typedef structure
  49. (#) For transmission path, two APIs are available:
  50. (##) HAL_ETH_Transmit(): Transmit an ETH frame in blocking mode
  51. (##) HAL_ETH_Transmit_IT(): Transmit an ETH frame in interrupt mode,
  52. HAL_ETH_TxCpltCallback() will be executed when end of transfer occur
  53. (#) Communication with an external PHY device:
  54. (##) HAL_ETH_ReadPHYRegister(): Read a register from an external PHY
  55. (##) HAL_ETH_WritePHYRegister(): Write data to an external RHY register
  56. (#) Configure the Ethernet MAC after ETH peripheral initialization
  57. (##) HAL_ETH_GetMACConfig(): Get MAC actual configuration into ETH_MACConfigTypeDef
  58. (##) HAL_ETH_SetMACConfig(): Set MAC configuration based on ETH_MACConfigTypeDef
  59. (#) Configure the Ethernet DMA after ETH peripheral initialization
  60. (##) HAL_ETH_GetDMAConfig(): Get DMA actual configuration into ETH_DMAConfigTypeDef
  61. (##) HAL_ETH_SetDMAConfig(): Set DMA configuration based on ETH_DMAConfigTypeDef
  62. -@- The PTP protocol offload APIs are not supported in this driver.
  63. @endverbatim
  64. ******************************************************************************
  65. * @attention
  66. *
  67. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  68. *
  69. * Redistribution and use in source and binary forms, with or without modification,
  70. * are permitted provided that the following conditions are met:
  71. * 1. Redistributions of source code must retain the above copyright notice,
  72. * this list of conditions and the following disclaimer.
  73. * 2. Redistributions in binary form must reproduce the above copyright notice,
  74. * this list of conditions and the following disclaimer in the documentation
  75. * and/or other materials provided with the distribution.
  76. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  77. * may be used to endorse or promote products derived from this software
  78. * without specific prior written permission.
  79. *
  80. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  81. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  83. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  84. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  85. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  86. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  88. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  89. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  90. *
  91. ******************************************************************************
  92. */
  93. /* Includes ------------------------------------------------------------------*/
  94. #include "stm32h7xx_hal.h"
  95. /** @addtogroup STM32H7xx_HAL_Driver
  96. * @{
  97. */
  98. /** @defgroup ETH ETH
  99. * @brief ETH HAL module driver
  100. * @{
  101. */
  102. #ifdef HAL_ETH_MODULE_ENABLED
  103. /* Private typedef -----------------------------------------------------------*/
  104. /* Private define ------------------------------------------------------------*/
  105. /** @addtogroup ETH_Private_Constants ETH Private Constants
  106. * @{
  107. */
  108. #define ETH_MACCR_MASK ((uint32_t)0xFFFB7F7CU)
  109. #define ETH_MACECR_MASK ((uint32_t)0x3F077FFFU)
  110. #define ETH_MACPFR_MASK ((uint32_t)0x800007FFF)
  111. #define ETH_MACWTR_MASK ((uint32_t)0x0000010FU)
  112. #define ETH_MACTFCR_MASK ((uint32_t)0xFFFF00F2U)
  113. #define ETH_MACRFCR_MASK ((uint32_t)0x00000003U)
  114. #define ETH_MTLTQOMR_MASK ((uint32_t)0x00000072U)
  115. #define ETH_MTLRQOMR_MASK ((uint32_t)0x0000007BU)
  116. #define ETH_DMAMR_MASK ((uint32_t)0x00007802U)
  117. #define ETH_DMASBMR_MASK ((uint32_t)0x0000D001U)
  118. #define ETH_DMACCR_MASK ((uint32_t)0x00013FFFU)
  119. #define ETH_DMACTCR_MASK ((uint32_t)0x003F1010U)
  120. #define ETH_DMACRCR_MASK ((uint32_t)0x803F0000U)
  121. #define ETH_MACPCSR_MASK (ETH_MACPCSR_PWRDWN | ETH_MACPCSR_RWKPKTEN | \
  122. ETH_MACPCSR_MGKPKTEN | ETH_MACPCSR_GLBLUCAST | \
  123. ETH_MACPCSR_RWKPFE)
  124. /* Timeout values */
  125. #define ETH_SWRESET_TIMEOUT ((uint32_t)500U)
  126. #define ETH_MDIO_BUS_TIMEOUT ((uint32_t)1000U)
  127. #define ETH_DMARXNDESCWBF_ERRORS_MASK ((uint32_t)(ETH_DMARXNDESCWBF_DE | ETH_DMARXNDESCWBF_RE | \
  128. ETH_DMARXNDESCWBF_OE | ETH_DMARXNDESCWBF_RWT |\
  129. ETH_DMARXNDESCWBF_GP | ETH_DMARXNDESCWBF_CE))
  130. #define ETH_MAC_US_TICK ((uint32_t)1000000U)
  131. /**
  132. * @}
  133. */
  134. /* Private macros ------------------------------------------------------------*/
  135. /** @defgroup ETH_Private_Macros ETH Private Macros
  136. * @{
  137. */
  138. /* Helper macros for TX descriptor handling */
  139. #define INCR_TX_DESC_INDEX(inx, offset) do {\
  140. (inx) += (offset);\
  141. if ((inx) >= ETH_TX_DESC_CNT){\
  142. (inx) = ((inx) - ETH_TX_DESC_CNT);}\
  143. } while (0)
  144. /* Helper macros for RX descriptor handling */
  145. #define INCR_RX_DESC_INDEX(inx, offset) do {\
  146. (inx) += (offset);\
  147. if ((inx) >= ETH_RX_DESC_CNT){\
  148. (inx) = ((inx) - ETH_RX_DESC_CNT);}\
  149. } while (0)
  150. /**
  151. * @}
  152. */
  153. /* Private function prototypes -----------------------------------------------*/
  154. /** @defgroup ETH_Private_Functions ETH Private Functions
  155. * @{
  156. */
  157. static void ETH_MAC_MDIO_ClkConfig(ETH_HandleTypeDef *heth);
  158. static void ETH_SetMACConfig(ETH_HandleTypeDef *heth, ETH_MACConfigTypeDef *macconf);
  159. static void ETH_SetDMAConfig(ETH_HandleTypeDef *heth, ETH_DMAConfigTypeDef *dmaconf);
  160. static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth);
  161. static void ETH_DMATxDescListInit(ETH_HandleTypeDef *heth);
  162. static void ETH_DMARxDescListInit(ETH_HandleTypeDef *heth);
  163. static uint32_t ETH_Prepare_Tx_Descriptors(ETH_HandleTypeDef *heth, ETH_TxPacketConfig *pTxConfig, uint32_t ItMode);
  164. /**
  165. * @}
  166. */
  167. /* Exported functions ---------------------------------------------------------*/
  168. /** @defgroup ETH_Exported_Functions ETH Exported Functions
  169. * @{
  170. */
  171. /** @defgroup ETH_Exported_Functions_Group1 Initialization and deinitialization functions
  172. * @brief Initialization and Configuration functions
  173. *
  174. @verbatim
  175. ===============================================================================
  176. ##### Initialization and Configuration functions #####
  177. ===============================================================================
  178. [..] This subsection provides a set of functions allowing to initialize and
  179. deinitialize the ETH peripheral:
  180. (+) User must Implement HAL_ETH_MspInit() function in which he configures
  181. all related peripherals resources (CLOCK, GPIO and NVIC ).
  182. (+) Call the function HAL_ETH_Init() to configure the selected device with
  183. the selected configuration:
  184. (++) MAC address
  185. (++) Media interface (MII or RMII)
  186. (++) Rx DMA Descriptors Tab
  187. (++) Tx DMA Descriptors Tab
  188. (++) Length of Rx Buffers
  189. (+) Call the function HAL_ETH_DescAssignMemory() to assign data buffers
  190. for each Rx DMA Descriptor
  191. (+) Call the function HAL_ETH_DeInit() to restore the default configuration
  192. of the selected ETH peripheral.
  193. @endverbatim
  194. * @{
  195. */
  196. /**
  197. * @brief Initialize the Ethernet peripheral registers.
  198. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  199. * the configuration information for ETHERNET module
  200. * @retval HAL status
  201. */
  202. HAL_StatusTypeDef HAL_ETH_Init(ETH_HandleTypeDef *heth)
  203. {
  204. uint32_t tickstart = 0;
  205. if(heth == NULL)
  206. {
  207. return HAL_ERROR;
  208. }
  209. /* Check the ETH peripheral state */
  210. if(heth->gState == HAL_ETH_STATE_RESET)
  211. {
  212. /* Init the low level hardware : GPIO, CLOCK, NVIC. */
  213. HAL_ETH_MspInit(heth);
  214. }
  215. heth->gState = HAL_ETH_STATE_BUSY;
  216. __HAL_RCC_SYSCFG_CLK_ENABLE();
  217. if(heth->Init.MediaInterface == HAL_ETH_MII_MODE)
  218. {
  219. HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_MII);
  220. }
  221. else
  222. {
  223. HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_RMII);
  224. }
  225. /* Ethernet Software reset */
  226. /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
  227. /* After reset all the registers holds their respective reset values */
  228. SET_BIT(heth->Instance->DMAMR, ETH_DMAMR_SWR);
  229. /* Get tick */
  230. tickstart = HAL_GetTick();
  231. /* Wait for software reset */
  232. while (READ_BIT(heth->Instance->DMAMR, ETH_DMAMR_SWR))
  233. {
  234. if(((HAL_GetTick() - tickstart ) > ETH_SWRESET_TIMEOUT))
  235. {
  236. /* Set Error Code */
  237. heth->ErrorCode = HAL_ETH_ERROR_TIMEOUT;
  238. /* Set State as Error */
  239. heth->gState = HAL_ETH_STATE_ERROR;
  240. /* Return Error */
  241. return HAL_ERROR;
  242. }
  243. }
  244. /*------------------ MDIO CSR Clock Range Configuration --------------------*/
  245. ETH_MAC_MDIO_ClkConfig(heth);
  246. /*------------------ MAC LPI 1US Tic Counter Configuration --------------------*/
  247. WRITE_REG(heth->Instance->MAC1USTCR, ((HAL_RCC_GetHCLKFreq() / ETH_MAC_US_TICK) - 1));
  248. /*------------------ MAC, MTL and DMA default Configuration ----------------*/
  249. ETH_MACDMAConfig(heth);
  250. /* SET DSL to 64 bit */
  251. MODIFY_REG(heth->Instance->DMACCR, ETH_DMACCR_DSL, ETH_DMACCR_DSL_64BIT);
  252. /* Set Receive Buffers Length (must be a multiple of 4) */
  253. if (heth->Init.RxBuffLen % 4)
  254. {
  255. /* Set Error Code */
  256. heth->ErrorCode = HAL_ETH_ERROR_PARAM;
  257. /* Set State as Error */
  258. heth->gState = HAL_ETH_STATE_ERROR;
  259. /* Return Error */
  260. return HAL_ERROR;
  261. }
  262. else
  263. {
  264. MODIFY_REG(heth->Instance->DMACRCR, ETH_DMACRCR_RBSZ, ((heth->Init.RxBuffLen) << 1));
  265. }
  266. /*------------------ DMA Tx Descriptors Configuration ----------------------*/
  267. ETH_DMATxDescListInit(heth);
  268. /*------------------ DMA Rx Descriptors Configuration ----------------------*/
  269. ETH_DMARxDescListInit(heth);
  270. /*--------------------- ETHERNET MAC Address Configuration ------------------*/
  271. /* Set MAC addr bits 32 to 47 */
  272. heth->Instance->MACA0HR = ((heth->Init.MACAddr[5] << 8) | heth->Init.MACAddr[4]);
  273. /* Set MAC addr bits 0 to 31 */
  274. heth->Instance->MACA0LR = ((heth->Init.MACAddr[3] << 24) | (heth->Init.MACAddr[2] << 16) | (heth->Init.MACAddr[1] << 8) | heth->Init.MACAddr[0]);
  275. heth->ErrorCode = HAL_ETH_ERROR_NONE;
  276. heth->gState = HAL_ETH_STATE_READY;
  277. heth->RxState = HAL_ETH_STATE_READY;
  278. return HAL_OK;
  279. }
  280. /**
  281. * @brief DeInitializes the ETH peripheral.
  282. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  283. * the configuration information for ETHERNET module
  284. * @retval HAL status
  285. */
  286. HAL_StatusTypeDef HAL_ETH_DeInit(ETH_HandleTypeDef *heth)
  287. {
  288. /* Set the ETH peripheral state to BUSY */
  289. heth->gState = HAL_ETH_STATE_BUSY;
  290. /* De-Init the low level hardware : GPIO, CLOCK, NVIC. */
  291. HAL_ETH_MspDeInit(heth);
  292. /* Set ETH HAL state to Disabled */
  293. heth->gState= HAL_ETH_STATE_RESET;
  294. /* Return function status */
  295. return HAL_OK;
  296. }
  297. /**
  298. * @brief Initializes the ETH MSP.
  299. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  300. * the configuration information for ETHERNET module
  301. * @retval None
  302. */
  303. __weak void HAL_ETH_MspInit(ETH_HandleTypeDef *heth)
  304. {
  305. /* Prevent unused argument(s) compilation warning */
  306. UNUSED(heth);
  307. /* NOTE : This function Should not be modified, when the callback is needed,
  308. the HAL_ETH_MspInit could be implemented in the user file
  309. */
  310. }
  311. /**
  312. * @brief DeInitializes ETH MSP.
  313. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  314. * the configuration information for ETHERNET module
  315. * @retval None
  316. */
  317. __weak void HAL_ETH_MspDeInit(ETH_HandleTypeDef *heth)
  318. {
  319. /* Prevent unused argument(s) compilation warning */
  320. UNUSED(heth);
  321. /* NOTE : This function Should not be modified, when the callback is needed,
  322. the HAL_ETH_MspDeInit could be implemented in the user file
  323. */
  324. }
  325. /**
  326. * @brief Assign memory buffers to a DMA Rx descriptor
  327. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  328. * the configuration information for ETHERNET module
  329. * @param Index : index of the DMA Rx descriptor
  330. * this parameter can be a value from 0x0 to (ETH_RX_DESC_CNT -1)
  331. * @param pBuffer1: address of buffer1
  332. * @param pBuffer2: address of buffer 2 if available
  333. * @retval HAL status
  334. */
  335. HAL_StatusTypeDef HAL_ETH_DescAssignMemory(ETH_HandleTypeDef *heth, uint32_t Index, uint8_t *pBuffer1, uint8_t *pBuffer2)
  336. {
  337. ETH_DMADescTypeDef *dmarxdesc = (ETH_DMADescTypeDef *)heth->RxDescList.RxDesc[Index];
  338. if((pBuffer1 == NULL) || (Index >= ETH_RX_DESC_CNT))
  339. {
  340. /* Set Error Code */
  341. heth->ErrorCode = HAL_ETH_ERROR_PARAM;
  342. /* Return Error */
  343. return HAL_ERROR;
  344. }
  345. /* write buffer address to RDES0 */
  346. WRITE_REG(dmarxdesc->DESC0, (uint32_t)pBuffer1);
  347. /* store buffer address */
  348. WRITE_REG(dmarxdesc->BackupAddr0, (uint32_t)pBuffer1);
  349. /* set buffer address valid bit to RDES3 */
  350. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_BUF1V);
  351. if(pBuffer2 != NULL)
  352. {
  353. /* write buffer 2 address to RDES1 */
  354. WRITE_REG(dmarxdesc->DESC2, (uint32_t)pBuffer2);
  355. /* store buffer 2 address */
  356. WRITE_REG(dmarxdesc->BackupAddr1, (uint32_t)pBuffer2);
  357. /* set buffer 2 address valid bit to RDES3 */
  358. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_BUF2V);
  359. }
  360. /* set OWN bit to RDES3 */
  361. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_OWN);
  362. return HAL_OK;
  363. }
  364. /**
  365. * @}
  366. */
  367. /** @defgroup ETH_Exported_Functions_Group2 IO operation functions
  368. * @brief ETH Transmit and Receive functions
  369. *
  370. @verbatim
  371. ==============================================================================
  372. ##### IO operation functions #####
  373. ==============================================================================
  374. [..]
  375. This subsection provides a set of functions allowing to manage the ETH
  376. data transfer.
  377. @endverbatim
  378. * @{
  379. */
  380. /**
  381. * @brief Enables Ethernet MAC and DMA reception and transmission
  382. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  383. * the configuration information for ETHERNET module
  384. * @retval HAL status
  385. */
  386. HAL_StatusTypeDef HAL_ETH_Start(ETH_HandleTypeDef *heth)
  387. {
  388. if(heth->gState == HAL_ETH_STATE_READY)
  389. {
  390. heth->gState = HAL_ETH_STATE_BUSY;
  391. /* Enable the MAC transmission */
  392. SET_BIT(heth->Instance->MACCR, ETH_MACCR_TE);
  393. /* Enable the MAC reception */
  394. SET_BIT(heth->Instance->MACCR, ETH_MACCR_RE);
  395. /* Set the Flush Transmit FIFO bit */
  396. SET_BIT(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_FTQ);
  397. /* Enable the DMA transmission */
  398. SET_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_ST);
  399. /* Enable the DMA reception */
  400. SET_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_SR);
  401. /* Clear Tx and Rx process stopped flags */
  402. heth->Instance->DMACSR |= (ETH_DMACSR_TPS | ETH_DMACSR_RPS);
  403. heth->gState = HAL_ETH_STATE_READY;
  404. heth->RxState = HAL_ETH_STATE_BUSY_RX;
  405. return HAL_OK;
  406. }
  407. else
  408. {
  409. return HAL_ERROR;
  410. }
  411. }
  412. /**
  413. * @brief Enables Ethernet MAC and DMA reception/transmission in Interrupt mode
  414. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  415. * the configuration information for ETHERNET module
  416. * @retval HAL status
  417. */
  418. HAL_StatusTypeDef HAL_ETH_Start_IT(ETH_HandleTypeDef *heth)
  419. {
  420. uint32_t descindex = 0, counter;
  421. ETH_DMADescTypeDef *dmarxdesc = (ETH_DMADescTypeDef *)heth->RxDescList.RxDesc[descindex];
  422. if(heth->gState == HAL_ETH_STATE_READY)
  423. {
  424. heth->gState = HAL_ETH_STATE_BUSY;
  425. /* Set IOC bit to all Rx descriptors */
  426. for(counter= 0; counter < ETH_RX_DESC_CNT; counter++)
  427. {
  428. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_IOC);
  429. INCR_RX_DESC_INDEX(descindex, 1);
  430. dmarxdesc = (ETH_DMADescTypeDef *)heth->RxDescList.RxDesc[descindex];
  431. }
  432. /* save IT mode to ETH Handle */
  433. heth->RxDescList.ItMode = 1U;
  434. /* Enable the MAC transmission */
  435. SET_BIT(heth->Instance->MACCR, ETH_MACCR_TE);
  436. /* Enable the MAC reception */
  437. SET_BIT(heth->Instance->MACCR, ETH_MACCR_RE);
  438. /* Set the Flush Transmit FIFO bit */
  439. SET_BIT(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_FTQ);
  440. /* Enable the DMA transmission */
  441. SET_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_ST);
  442. /* Enable the DMA reception */
  443. SET_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_SR);
  444. /* Clear Tx and Rx process stopped flags */
  445. heth->Instance->DMACSR |= (ETH_DMACSR_TPS | ETH_DMACSR_RPS);
  446. heth->gState = HAL_ETH_STATE_READY;
  447. heth->RxState = HAL_ETH_STATE_BUSY_RX;
  448. /* Enable ETH DMA interrupts:
  449. - Tx complete interrupt
  450. - Rx complete interrupt
  451. - Fatal bus interrupt
  452. */
  453. __HAL_ETH_DMA_ENABLE_IT(heth, (ETH_DMACIER_NIE | ETH_DMACIER_RIE | ETH_DMACIER_TIE |
  454. ETH_DMACIER_FBEE | ETH_DMACIER_AIE));
  455. return HAL_OK;
  456. }
  457. else
  458. {
  459. return HAL_ERROR;
  460. }
  461. }
  462. /**
  463. * @brief Stop Ethernet MAC and DMA reception/transmission
  464. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  465. * the configuration information for ETHERNET module
  466. * @retval HAL status
  467. */
  468. HAL_StatusTypeDef HAL_ETH_Stop(ETH_HandleTypeDef *heth)
  469. {
  470. if(heth->gState != HAL_ETH_STATE_RESET)
  471. {
  472. /* Set the ETH peripheral state to BUSY */
  473. heth->gState = HAL_ETH_STATE_BUSY;
  474. /* Disable the DMA transmission */
  475. CLEAR_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_ST);
  476. /* Disable the DMA reception */
  477. CLEAR_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_SR);
  478. /* Disable the MAC reception */
  479. CLEAR_BIT( heth->Instance->MACCR, ETH_MACCR_RE);
  480. /* Set the Flush Transmit FIFO bit */
  481. SET_BIT(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_FTQ);
  482. /* Disable the MAC transmission */
  483. CLEAR_BIT(heth->Instance->MACCR, ETH_MACCR_TE);
  484. heth->gState = HAL_ETH_STATE_READY;
  485. heth->RxState = HAL_ETH_STATE_READY;
  486. /* Return function status */
  487. return HAL_OK;
  488. }
  489. else
  490. {
  491. return HAL_ERROR;
  492. }
  493. }
  494. /**
  495. * @brief Stop Ethernet MAC and DMA reception/transmission in Interrupt mode
  496. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  497. * the configuration information for ETHERNET module
  498. * @retval HAL status
  499. */
  500. HAL_StatusTypeDef HAL_ETH_Stop_IT(ETH_HandleTypeDef *heth)
  501. {
  502. ETH_DMADescTypeDef *dmarxdesc = (ETH_DMADescTypeDef *)heth->RxDescList.RxDesc[0];
  503. uint32_t index;
  504. if(heth->gState != HAL_ETH_STATE_RESET)
  505. {
  506. /* Set the ETH peripheral state to BUSY */
  507. heth->gState = HAL_ETH_STATE_BUSY;
  508. /* Disable intrrupts:
  509. - Tx complete interrupt
  510. - Rx complete interrupt */
  511. __HAL_ETH_DMA_DISABLE_IT(heth, (ETH_DMA_NORMAL_IT | ETH_DMA_RX_IT | ETH_DMA_TX_IT));
  512. /* Disable the DMA transmission */
  513. CLEAR_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_ST);
  514. /* Disable the DMA reception */
  515. CLEAR_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_SR);
  516. /* Disable the MAC reception */
  517. CLEAR_BIT( heth->Instance->MACCR, ETH_MACCR_RE);
  518. /* Set the Flush Transmit FIFO bit */
  519. SET_BIT(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_FTQ);
  520. /* Disable the MAC transmission */
  521. CLEAR_BIT(heth->Instance->MACCR, ETH_MACCR_TE);
  522. /* Clear IOC bit to all Rx descriptors */
  523. for(index = 0; index < ETH_RX_DESC_CNT; index++)
  524. {
  525. CLEAR_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_IOC);
  526. }
  527. heth->RxDescList.ItMode = 0U;
  528. heth->gState = HAL_ETH_STATE_READY;
  529. heth->RxState = HAL_ETH_STATE_READY;
  530. /* Return function status */
  531. return HAL_OK;
  532. }
  533. else
  534. {
  535. return HAL_ERROR;
  536. }
  537. }
  538. /**
  539. * @brief Sends an Ethernet Packet in polling mode.
  540. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  541. * the configuration information for ETHERNET module
  542. * @param pTxConfig: Hold the configuration of packet to be transmitted
  543. * @param Timeout: timeout value
  544. * @retval HAL status
  545. */
  546. HAL_StatusTypeDef HAL_ETH_Transmit(ETH_HandleTypeDef *heth, ETH_TxPacketConfig *pTxConfig, uint32_t Timeout)
  547. {
  548. uint32_t tickstart = 0;
  549. uint32_t lastdescid;
  550. ETH_TxDescListTypeDef *dmatxdesclist = &heth->TxDescList;
  551. ETH_DMADescTypeDef *dmatxdesc;
  552. if(pTxConfig == NULL)
  553. {
  554. heth->ErrorCode |= HAL_ETH_ERROR_PARAM;
  555. return HAL_ERROR;
  556. }
  557. if((heth->gState == HAL_ETH_STATE_READY) || (heth->gState == HAL_ETH_STATE_BUSY_TX))
  558. {
  559. /* Set ETH HAL State to Busy */
  560. heth->gState = HAL_ETH_STATE_BUSY_TX;
  561. /* Config DMA Tx descriptor by Tx Packet info */
  562. if (ETH_Prepare_Tx_Descriptors(heth, pTxConfig, 0) != HAL_ETH_ERROR_NONE)
  563. {
  564. /* Set the ETH error code */
  565. heth->ErrorCode |= HAL_ETH_ERROR_BUSY;
  566. return HAL_ERROR;
  567. }
  568. /* Save last Tx desc index to be used for Tx status */
  569. lastdescid= heth->TxDescList.CurTxDesc;
  570. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[lastdescid];
  571. /* Incr current tx desc index */
  572. INCR_TX_DESC_INDEX(heth->TxDescList.CurTxDesc, 1);
  573. /* Start transmission */
  574. /* issue a poll command to Tx DMA by writing address of next immediate free descriptor */
  575. WRITE_REG(heth->Instance->DMACTDTPR, (uint32_t)(heth->TxDescList.TxDesc[heth->TxDescList.CurTxDesc]));
  576. tickstart = HAL_GetTick();
  577. /* Wait for data to be transmitted or timeout occured */
  578. while((dmatxdesc->DESC3 & ETH_DMATXNDESCWBF_OWN) != (uint32_t)RESET)
  579. {
  580. if(heth->Instance->DMACSR & ETH_DMACSR_FBE)
  581. {
  582. heth->ErrorCode |= HAL_ETH_ERROR_DMA;
  583. heth->DMAErrorCode = heth->Instance->DMACSR;
  584. /* Set ETH HAL State to Ready */
  585. heth->gState = HAL_ETH_STATE_ERROR;
  586. /* Return function status */
  587. return HAL_ERROR;
  588. }
  589. /* Check for the Timeout */
  590. if(Timeout != HAL_MAX_DELAY)
  591. {
  592. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  593. {
  594. heth->ErrorCode |= HAL_ETH_ERROR_TIMEOUT;
  595. heth->gState = HAL_ETH_STATE_READY;
  596. return HAL_ERROR;
  597. }
  598. }
  599. }
  600. /* Set ETH HAL State to Ready */
  601. heth->gState = HAL_ETH_STATE_READY;
  602. /* Return function status */
  603. return HAL_OK;
  604. }
  605. else
  606. {
  607. return HAL_ERROR;
  608. }
  609. }
  610. /**
  611. * @brief Sends an Ethernet Packet in interrupt mode.
  612. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  613. * the configuration information for ETHERNET module
  614. * @param pTxConfig: Hold the configuration of packet to be transmitted
  615. * @retval HAL status
  616. */
  617. HAL_StatusTypeDef HAL_ETH_Transmit_IT(ETH_HandleTypeDef *heth, ETH_TxPacketConfig *pTxConfig)
  618. {
  619. if(pTxConfig == NULL)
  620. {
  621. return HAL_ERROR;
  622. }
  623. if((heth->gState == HAL_ETH_STATE_READY) || (heth->gState == HAL_ETH_STATE_BUSY_TX))
  624. {
  625. /* Set ETH HAL State to Busy */
  626. heth->gState = HAL_ETH_STATE_BUSY_TX;
  627. /* Config DMA Tx descriptor by Tx Packet info */
  628. if (ETH_Prepare_Tx_Descriptors(heth, pTxConfig, 1) != HAL_ETH_ERROR_NONE)
  629. {
  630. heth->ErrorCode = HAL_ETH_ERROR_BUSY;
  631. return HAL_ERROR;
  632. }
  633. /* Incr current tx desc index */
  634. INCR_TX_DESC_INDEX(heth->TxDescList.CurTxDesc, 1);
  635. /* Start transmission */
  636. /* issue a poll command to Tx DMA by writing address of next immediate free descriptor */
  637. WRITE_REG(heth->Instance->DMACTDTPR, (uint32_t)(heth->TxDescList.TxDesc[heth->TxDescList.CurTxDesc]));
  638. return HAL_OK;
  639. }
  640. else
  641. {
  642. return HAL_ERROR;
  643. }
  644. }
  645. /**
  646. * @brief Checks for received Packets.
  647. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  648. * the configuration information for ETHERNET module
  649. * @retval 1: A Packet is received
  650. * 0: no Packet received
  651. */
  652. uint8_t HAL_ETH_IsRxDataAvailable(ETH_HandleTypeDef *heth)
  653. {
  654. ETH_RxDescListTypeDef *dmarxdesclist = &heth->RxDescList;
  655. uint32_t descidx = dmarxdesclist->CurRxDesc;
  656. ETH_DMADescTypeDef *dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  657. uint32_t descscancnt = 0;
  658. uint32_t appdesccnt = 0, firstappdescidx = 0;
  659. if(dmarxdesclist->AppDescNbr != 0)
  660. {
  661. /* data already received by not yet processed*/
  662. return 0;
  663. }
  664. /* Check if descriptor is not owned by DMA */
  665. while((READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_OWN) == (uint32_t)RESET) && (descscancnt < ETH_RX_DESC_CNT))
  666. {
  667. descscancnt++;
  668. /* Check if last descriptor */
  669. if(READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_LD) != (uint32_t)RESET)
  670. {
  671. /* Increment the number of descriptors to be passed to the application */
  672. appdesccnt += 1U;
  673. if(appdesccnt == 1)
  674. {
  675. WRITE_REG(firstappdescidx, descidx);
  676. }
  677. /* Increment current rx descriptor index */
  678. INCR_RX_DESC_INDEX(descidx, 1);
  679. /* Check for Context descriptor */
  680. /* Get current descriptor address */
  681. dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  682. if((READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_OWN) == (uint32_t)RESET) &&
  683. (READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_CTXT) != (uint32_t)RESET))
  684. {
  685. /* Increment the number of descriptors to be passed to the application */
  686. dmarxdesclist->AppContextDesc = 1;
  687. /* Increment current rx descriptor index */
  688. INCR_RX_DESC_INDEX(descidx, 1);
  689. }
  690. /* Fill information to Rx descriptors list */
  691. dmarxdesclist->CurRxDesc = descidx;
  692. dmarxdesclist->FirstAppDesc = firstappdescidx;
  693. dmarxdesclist->AppDescNbr = appdesccnt;
  694. /* Return function status */
  695. return 1;
  696. }
  697. /* Check if first descriptor */
  698. else if(READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_FD) != (uint32_t)RESET)
  699. {
  700. WRITE_REG(firstappdescidx, descidx);
  701. /* Increment the number of descriptors to be passed to the application */
  702. appdesccnt = 1U;
  703. /* Increment current rx descriptor index */
  704. INCR_RX_DESC_INDEX(descidx, 1U);
  705. /* Get current descriptor address */
  706. dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  707. }
  708. /* It should be an intermediate descriptor */
  709. else
  710. {
  711. /* Increment the number of descriptors to be passed to the application */
  712. appdesccnt += 1U;
  713. /* Increment current rx descriptor index */
  714. INCR_RX_DESC_INDEX(descidx, 1U);
  715. /* Get current descriptor address */
  716. dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  717. }
  718. }
  719. /* Build Descriptors if an incomplete Packet is received */
  720. if(appdesccnt > 0)
  721. {
  722. descidx = firstappdescidx;
  723. dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  724. for(descscancnt = 0; descscancnt < appdesccnt; descscancnt++)
  725. {
  726. WRITE_REG(dmarxdesc->DESC0, dmarxdesc->BackupAddr0);
  727. WRITE_REG(dmarxdesc->DESC3, ETH_DMARXNDESCRF_BUF1V);
  728. if (READ_REG(dmarxdesc->BackupAddr1))
  729. {
  730. WRITE_REG(dmarxdesc->DESC2, dmarxdesc->BackupAddr1);
  731. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_BUF2V);
  732. }
  733. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_OWN);
  734. if(dmarxdesclist->ItMode)
  735. {
  736. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_IOC);
  737. }
  738. /* Increment rx descriptor index */
  739. INCR_RX_DESC_INDEX(descidx, 1);
  740. /* Get descriptor address */
  741. dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  742. }
  743. }
  744. /* Fill information to Rx descriptors list: No received Packet */
  745. dmarxdesclist->AppDescNbr = 0U;
  746. return 0;
  747. }
  748. /**
  749. * @brief This function gets the buffer address of last received Packet.
  750. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  751. * the configuration information for ETHERNET module
  752. * @param RxBuffer: Pointer to a ETH_BufferTypeDef structure
  753. * @retval HAL status
  754. */
  755. HAL_StatusTypeDef HAL_ETH_GetRxDataBuffer(ETH_HandleTypeDef *heth, ETH_BufferTypeDef *RxBuffer)
  756. {
  757. ETH_RxDescListTypeDef *dmarxdesclist = &heth->RxDescList;
  758. uint32_t descidx = dmarxdesclist->FirstAppDesc;
  759. uint32_t index, accumulatedlen = 0, lastdesclen=0;
  760. __IO ETH_DMADescTypeDef *dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  761. if(dmarxdesclist->AppDescNbr ==0)
  762. {
  763. if(HAL_ETH_IsRxDataAvailable(heth) == 0)
  764. {
  765. /* No data to be transferred to the application */
  766. return HAL_ERROR;
  767. }
  768. }
  769. /* Get intermediate descriptors buffers: in case of the Packet is splitted into multi descriptors */
  770. for(index = 0; index < (dmarxdesclist->AppDescNbr - 1); index++)
  771. {
  772. /* Both rx descriptor buffers are valid */
  773. if(dmarxdesc->BackupAddr1 != 0)
  774. {
  775. if(RxBuffer == NULL)
  776. {
  777. return HAL_ERROR;
  778. }
  779. RxBuffer->buffer = (uint8_t *) dmarxdesc->BackupAddr0;
  780. RxBuffer->len = heth->Init.RxBuffLen;
  781. RxBuffer = (ETH_BufferTypeDef *)RxBuffer->next;
  782. if(RxBuffer == NULL)
  783. {
  784. return HAL_ERROR;
  785. }
  786. RxBuffer->buffer = (uint8_t *) dmarxdesc->BackupAddr1;
  787. RxBuffer->len = heth->Init.RxBuffLen;
  788. }
  789. /* Only buffer 1 address is valid */
  790. else
  791. {
  792. RxBuffer->buffer = (uint8_t *) dmarxdesc->BackupAddr0;
  793. RxBuffer->len = heth->Init.RxBuffLen;
  794. }
  795. /* get total length until this descriptor */
  796. accumulatedlen = READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_PL);
  797. /* Increment to next descriptor */
  798. INCR_RX_DESC_INDEX(descidx, 1);
  799. dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  800. /* Point to next buffer */
  801. RxBuffer = (ETH_BufferTypeDef *)RxBuffer->next;
  802. }
  803. /* Get last descriptor buffers */
  804. if(RxBuffer == NULL)
  805. {
  806. return HAL_ERROR;
  807. }
  808. /* last descriptor data length */
  809. lastdesclen = READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_PL) - accumulatedlen;
  810. /* data is in only one buffer */
  811. if(lastdesclen <= heth->Init.RxBuffLen)
  812. {
  813. RxBuffer->buffer = (uint8_t *) dmarxdesc->BackupAddr0;
  814. RxBuffer->len = lastdesclen;
  815. }
  816. /* data is in two buffers */
  817. else if(dmarxdesc->BackupAddr1 != 0)
  818. {
  819. RxBuffer->buffer = (uint8_t *) dmarxdesc->BackupAddr0;
  820. RxBuffer->len = heth->Init.RxBuffLen;
  821. RxBuffer = (ETH_BufferTypeDef *)RxBuffer->next;
  822. if(RxBuffer == NULL)
  823. {
  824. return HAL_ERROR;
  825. }
  826. RxBuffer->buffer = (uint8_t *) dmarxdesc->BackupAddr1;
  827. RxBuffer->len = lastdesclen - (heth->Init.RxBuffLen);
  828. }
  829. else /* Buffer 2 not valid*/
  830. {
  831. return HAL_ERROR;
  832. }
  833. return HAL_OK;
  834. }
  835. /**
  836. * @brief This function gets the length of last received Packet.
  837. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  838. * the configuration information for ETHERNET module
  839. * @param Length: parameter to hold Rx packet length
  840. * @retval HAL Status
  841. */
  842. HAL_StatusTypeDef HAL_ETH_GetRxDataLength(ETH_HandleTypeDef *heth, uint32_t *Length)
  843. {
  844. ETH_RxDescListTypeDef *dmarxdesclist = &heth->RxDescList;
  845. uint32_t descidx = dmarxdesclist->FirstAppDesc;
  846. __IO ETH_DMADescTypeDef *dmarxdesc;
  847. if(dmarxdesclist->AppDescNbr ==0)
  848. {
  849. if(HAL_ETH_IsRxDataAvailable(heth) == 0)
  850. {
  851. /* No data to be transferred to the application */
  852. return HAL_ERROR;
  853. }
  854. }
  855. /* Get index of last descriptor */
  856. INCR_RX_DESC_INDEX(descidx, (dmarxdesclist->AppDescNbr-1));
  857. /* Point to last descriptor */
  858. dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  859. *Length = READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_PL);
  860. return HAL_OK;
  861. }
  862. /**
  863. * @brief Get the Rx data info (Packet type, VLAN tag, Filters status, ...)
  864. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  865. * the configuration information for ETHERNET module
  866. * @param RxPacketInfo: parameter to hold info of received buffer
  867. * @retval HAL status
  868. */
  869. HAL_StatusTypeDef HAL_ETH_GetRxDataInfo(ETH_HandleTypeDef *heth, ETH_RxPacketInfo *RxPacketInfo)
  870. {
  871. ETH_RxDescListTypeDef *dmarxdesclist = &heth->RxDescList;
  872. uint32_t descidx = dmarxdesclist->FirstAppDesc;
  873. __IO ETH_DMADescTypeDef *dmarxdesc;
  874. if(dmarxdesclist->AppDescNbr ==0)
  875. {
  876. if(HAL_ETH_IsRxDataAvailable(heth) == 0)
  877. {
  878. /* No data to be transferred to the application */
  879. return HAL_ERROR;
  880. }
  881. }
  882. /* Get index of last descriptor */
  883. INCR_RX_DESC_INDEX(descidx, ((dmarxdesclist->AppDescNbr) - 1U));
  884. /* Point to last descriptor */
  885. dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descidx];
  886. if(dmarxdesc->DESC3 & ETH_DMARXNDESCWBF_ES)
  887. {
  888. RxPacketInfo->ErrorCode = READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_ERRORS_MASK);
  889. }
  890. else
  891. {
  892. if(READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_RS0V))
  893. {
  894. if(READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_LT) == ETH_DMARXNDESCWBF_LT_DVLAN)
  895. {
  896. RxPacketInfo->VlanTag = READ_BIT(dmarxdesc->DESC0, ETH_DMARXNDESCWBF_OVT);
  897. RxPacketInfo->InnerVlanTag = READ_BIT(dmarxdesc->DESC0, ETH_DMARXNDESCWBF_IVT) >> 16;
  898. }
  899. else if (READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_LT) == ETH_DMARXNDESCWBF_LT_VLAN)
  900. {
  901. RxPacketInfo->VlanTag = READ_BIT(dmarxdesc->DESC0, ETH_DMARXNDESCWBF_OVT);
  902. }
  903. }
  904. if(READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_RS1V))
  905. {
  906. /* Get Payload type */
  907. RxPacketInfo->PayloadType =READ_BIT( dmarxdesc->DESC1, ETH_DMARXNDESCWBF_PT);
  908. /* Get Header type */
  909. RxPacketInfo->HeaderType = READ_BIT(dmarxdesc->DESC1, (ETH_DMARXNDESCWBF_IPV4 | ETH_DMARXNDESCWBF_IPV6));
  910. /* Get Checksum status */
  911. RxPacketInfo->Checksum = READ_BIT(dmarxdesc->DESC1, (ETH_DMARXNDESCWBF_IPCE | ETH_DMARXNDESCWBF_IPCB | ETH_DMARXNDESCWBF_IPHE));
  912. }
  913. if(READ_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCWBF_RS2V))
  914. {
  915. RxPacketInfo->MacFilterStatus = READ_BIT(dmarxdesc->DESC2, (ETH_DMARXNDESCWBF_HF | ETH_DMARXNDESCWBF_DAF | ETH_DMARXNDESCWBF_SAF | ETH_DMARXNDESCWBF_VF));
  916. RxPacketInfo->L3FilterStatus = READ_BIT(dmarxdesc->DESC2, (ETH_DMARXNDESCWBF_L3FM | ETH_DMARXNDESCWBF_L3L4FM));
  917. RxPacketInfo->L4FilterStatus = READ_BIT(dmarxdesc->DESC2, (ETH_DMARXNDESCWBF_L4FM | ETH_DMARXNDESCWBF_L3L4FM));
  918. }
  919. }
  920. /* Get the segment count */
  921. WRITE_REG(RxPacketInfo->SegmentCnt, dmarxdesclist->AppDescNbr);
  922. return HAL_OK;
  923. }
  924. /**
  925. * @brief This function gives back Rx Desc of the last received Packet
  926. * to the DMA, so ETH DMA will be able to use these descriptors
  927. * to receive next Packets.
  928. * It should be called after processing the received Packet.
  929. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  930. * the configuration information for ETHERNET module
  931. * @retval HAL status.
  932. */
  933. HAL_StatusTypeDef HAL_ETH_BuildRxDescriptors(ETH_HandleTypeDef *heth)
  934. {
  935. ETH_RxDescListTypeDef *dmarxdesclist = &heth->RxDescList;
  936. uint32_t descindex = dmarxdesclist->FirstAppDesc;
  937. __IO ETH_DMADescTypeDef *dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descindex];
  938. uint32_t totalappdescnbr = dmarxdesclist->AppDescNbr;
  939. uint32_t descscan;
  940. if(dmarxdesclist->AppDescNbr ==0)
  941. {
  942. /* No Rx descriptors to build */
  943. return HAL_ERROR;
  944. }
  945. if(dmarxdesclist->AppContextDesc)
  946. {
  947. /* A context descriptor is available */
  948. totalappdescnbr += 1;
  949. }
  950. for(descscan =0; descscan < totalappdescnbr; descscan++)
  951. {
  952. WRITE_REG(dmarxdesc->DESC0, dmarxdesc->BackupAddr0);
  953. WRITE_REG(dmarxdesc->DESC3, ETH_DMARXNDESCRF_BUF1V);
  954. if (READ_REG(dmarxdesc->BackupAddr1))
  955. {
  956. WRITE_REG(dmarxdesc->DESC2, dmarxdesc->BackupAddr1);
  957. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_BUF2V);
  958. }
  959. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_OWN);
  960. if(dmarxdesclist->ItMode)
  961. {
  962. SET_BIT(dmarxdesc->DESC3, ETH_DMARXNDESCRF_IOC);
  963. }
  964. if(descscan < (dmarxdesclist->AppDescNbr -1))
  965. {
  966. /* Increment rx descriptor index */
  967. INCR_RX_DESC_INDEX(descindex, 1);
  968. /* Get descriptor address */
  969. dmarxdesc = (ETH_DMADescTypeDef *)dmarxdesclist->RxDesc[descindex];
  970. }
  971. }
  972. /* Set the Tail pointer address to the last rx descriptor hold by the app */
  973. WRITE_REG(heth->Instance->DMACRDTPR, (uint32_t)dmarxdesc);
  974. /* reset the Application desc number */
  975. WRITE_REG(dmarxdesclist->AppDescNbr, 0);
  976. return HAL_OK;
  977. }
  978. /**
  979. * @brief This function handles ETH interrupt request.
  980. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  981. * the configuration information for ETHERNET module
  982. * @retval HAL status
  983. */
  984. void HAL_ETH_IRQHandler(ETH_HandleTypeDef *heth)
  985. {
  986. /* Packet received */
  987. if (__HAL_ETH_DMA_GET_IT(heth, ETH_DMACSR_RI) && __HAL_ETH_DMA_GET_IT_SOURCE(heth, ETH_DMACIER_RIE))
  988. {
  989. /* Call this function to update handle fields */
  990. HAL_ETH_IsRxDataAvailable(heth);
  991. /* Receive complete callback */
  992. HAL_ETH_RxCpltCallback(heth);
  993. /* Clear the Eth DMA Rx IT pending bits */
  994. __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMACSR_RI | ETH_DMACSR_NIS);
  995. }
  996. /* Packet transmitted */
  997. if (__HAL_ETH_DMA_GET_IT(heth, ETH_DMACSR_TI) && __HAL_ETH_DMA_GET_IT_SOURCE(heth, ETH_DMACIER_TIE))
  998. {
  999. /* Transfer complete callback */
  1000. HAL_ETH_TxCpltCallback(heth);
  1001. /* Clear the Eth DMA Tx IT pending bits */
  1002. __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMACSR_TI | ETH_DMACSR_NIS);
  1003. heth->ErrorCode = HAL_ETH_ERROR_NONE;
  1004. heth->gState = HAL_ETH_STATE_READY;
  1005. }
  1006. /* ETH DMA Error */
  1007. if(__HAL_ETH_DMA_GET_IT(heth, ETH_DMACSR_AIS) && __HAL_ETH_DMA_GET_IT_SOURCE(heth, ETH_DMACIER_AIE))
  1008. {
  1009. heth->ErrorCode |= HAL_ETH_ERROR_DMA;
  1010. /* if fatal bus error occured */
  1011. if (__HAL_ETH_DMA_GET_IT(heth, ETH_DMACSR_FBE) && __HAL_ETH_DMA_GET_IT_SOURCE(heth, ETH_DMACIER_FBEE))
  1012. {
  1013. /* Get DMA error code */
  1014. heth->DMAErrorCode = __HAL_ETH_DMA_GET_IT(heth, (ETH_DMACSR_FBE | ETH_DMACSR_TPS | ETH_DMACSR_RPS));
  1015. /* Disable all interrupts */
  1016. __HAL_ETH_DMA_DISABLE_IT(heth, ETH_DMACIER_NIE | ETH_DMACIER_AIE);
  1017. /* Set HAL state to ERROR */
  1018. heth->gState = HAL_ETH_STATE_ERROR;
  1019. }
  1020. else
  1021. {
  1022. /* Get DMA error status */
  1023. heth->DMAErrorCode = __HAL_ETH_DMA_GET_IT(heth, (ETH_DMACSR_CDE | ETH_DMACSR_ETI | ETH_DMACSR_RWT |
  1024. ETH_DMACSR_RBU | ETH_DMACSR_AIS));
  1025. /* Clear the interrupt summary flag */
  1026. __HAL_ETH_DMA_CLEAR_IT(heth, (ETH_DMACSR_CDE | ETH_DMACSR_ETI | ETH_DMACSR_RWT |
  1027. ETH_DMACSR_RBU | ETH_DMACSR_AIS));
  1028. }
  1029. /* Ethernet Error callback */
  1030. HAL_ETH_DMAErrorCallback(heth);
  1031. }
  1032. /* ETH MAC Error IT */
  1033. if(__HAL_ETH_MAC_GET_IT(heth, (ETH_MACIER_RXSTSIE | ETH_MACIER_TXSTSIE)))
  1034. {
  1035. /* Get MAC Rx Tx status and clear Status register pending bit */
  1036. heth->MACErrorCode = READ_REG(heth->Instance->MACRXTXSR);
  1037. heth->gState = HAL_ETH_STATE_ERROR;
  1038. /* Ethernet PMT callback */
  1039. HAL_ETH_MACErrorCallback(heth);
  1040. heth->MACErrorCode = (uint32_t)(0x0U);
  1041. }
  1042. /* ETH PMT IT */
  1043. if(__HAL_ETH_MAC_GET_IT(heth, ETH_MAC_PMT_IT))
  1044. {
  1045. /* Get MAC Wake-up source and clear the status register pending bit */
  1046. heth->MACWakeUpEvent = READ_BIT(heth->Instance->MACPCSR, (ETH_MACPCSR_RWKPRCVD | ETH_MACPCSR_MGKPRCVD));
  1047. /* Ethernet PMT callback */
  1048. HAL_ETH_PMTCallback(heth);
  1049. heth->MACWakeUpEvent = (uint32_t)(0x0U);
  1050. }
  1051. /* ETH EEE IT */
  1052. if(__HAL_ETH_MAC_GET_IT(heth, ETH_MAC_LPI_IT))
  1053. {
  1054. /* Get MAC LPI interrupt source and clear the status register pending bit */
  1055. heth->MACLPIEvent = READ_BIT(heth->Instance->MACPCSR, 0x0000000FU);
  1056. /* Ethernet EEE callback */
  1057. HAL_ETH_EEECallback(heth);
  1058. heth->MACLPIEvent = (uint32_t)(0x0U);
  1059. }
  1060. /* check ETH WAKEUP exti flag */
  1061. if(__HAL_ETH_WAKEUP_EXTI_GET_FLAG(ETH_WAKEUP_EXTI_LINE) != RESET)
  1062. {
  1063. /* Clear ETH WAKEUP Exti pending bit */
  1064. __HAL_ETH_WAKEUP_EXTI_CLEAR_FLAG(ETH_WAKEUP_EXTI_LINE);
  1065. /* ETH WAKEUP interrupt user callback */
  1066. HAL_ETH_WakeUpCallback(heth);
  1067. }
  1068. }
  1069. /**
  1070. * @brief Tx Transfer completed callbacks.
  1071. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1072. * the configuration information for ETHERNET module
  1073. * @retval None
  1074. */
  1075. __weak void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *heth)
  1076. {
  1077. /* Prevent unused argument(s) compilation warning */
  1078. UNUSED(heth);
  1079. /* NOTE : This function Should not be modified, when the callback is needed,
  1080. the HAL_ETH_TxCpltCallback could be implemented in the user file
  1081. */
  1082. }
  1083. /**
  1084. * @brief Rx Transfer completed callbacks.
  1085. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1086. * the configuration information for ETHERNET module
  1087. * @retval None
  1088. */
  1089. __weak void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
  1090. {
  1091. /* Prevent unused argument(s) compilation warning */
  1092. UNUSED(heth);
  1093. /* NOTE : This function Should not be modified, when the callback is needed,
  1094. the HAL_ETH_RxCpltCallback could be implemented in the user file
  1095. */
  1096. }
  1097. /**
  1098. * @brief Ethernet DMA transfer error callbacks
  1099. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1100. * the configuration information for ETHERNET module
  1101. * @retval None
  1102. */
  1103. __weak void HAL_ETH_DMAErrorCallback(ETH_HandleTypeDef *heth)
  1104. {
  1105. /* NOTE : This function Should not be modified, when the callback is needed,
  1106. the HAL_ETH_DMAErrorCallback could be implemented in the user file
  1107. */
  1108. }
  1109. /**
  1110. * @brief Ethernet MAC transfer error callbacks
  1111. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1112. * the configuration information for ETHERNET module
  1113. * @retval None
  1114. */
  1115. __weak void HAL_ETH_MACErrorCallback(ETH_HandleTypeDef *heth)
  1116. {
  1117. /* Prevent unused argument(s) compilation warning */
  1118. UNUSED(heth);
  1119. /* NOTE : This function Should not be modified, when the callback is needed,
  1120. the HAL_ETH_MACErrorCallback could be implemented in the user file
  1121. */
  1122. }
  1123. /**
  1124. * @brief Ethernet Power Management module IT callback
  1125. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1126. * the configuration information for ETHERNET module
  1127. * @retval None
  1128. */
  1129. __weak void HAL_ETH_PMTCallback(ETH_HandleTypeDef *heth)
  1130. {
  1131. /* Prevent unused argument(s) compilation warning */
  1132. UNUSED(heth);
  1133. /* NOTE : This function Should not be modified, when the callback is needed,
  1134. the HAL_ETH_PMTCallback could be implemented in the user file
  1135. */
  1136. }
  1137. /**
  1138. * @brief Energy Efficient Etherent IT callback
  1139. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1140. * the configuration information for ETHERNET module
  1141. * @retval None
  1142. */
  1143. __weak void HAL_ETH_EEECallback(ETH_HandleTypeDef *heth)
  1144. {
  1145. /* Prevent unused argument(s) compilation warning */
  1146. UNUSED(heth);
  1147. /* NOTE : This function Should not be modified, when the callback is needed,
  1148. the HAL_ETH_EEECallback could be implemented in the user file
  1149. */
  1150. }
  1151. /**
  1152. * @brief ETH WAKEUP interrupt callback
  1153. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1154. * the configuration information for ETHERNET module
  1155. * @retval None
  1156. */
  1157. __weak void HAL_ETH_WakeUpCallback(ETH_HandleTypeDef *heth)
  1158. {
  1159. /* Prevent unused argument(s) compilation warning */
  1160. UNUSED(heth);
  1161. /* NOTE : This function Should not be modified, when the callback is needed,
  1162. the HAL_ETH_WakeUpCallback could be implemented in the user file
  1163. */
  1164. }
  1165. /**
  1166. * @brief Read a PHY register
  1167. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1168. * the configuration information for ETHERNET module
  1169. * @param PHYAddr: PHY port address, must be a value from 0 to 31
  1170. * @param PHYReg: PHY register address, must be a value from 0 to 31
  1171. * @param pRegValue: parameter to hold read value
  1172. * @retval HAL status
  1173. */
  1174. HAL_StatusTypeDef HAL_ETH_ReadPHYRegister(ETH_HandleTypeDef *heth, uint32_t PHYAddr, uint32_t PHYReg, uint32_t *pRegValue)
  1175. {
  1176. uint32_t tmpreg = 0;
  1177. uint32_t tickstart = 0;
  1178. /* Check for the Busy flag */
  1179. if(READ_BIT(heth->Instance->MACMDIOAR, ETH_MACMDIOAR_MB))
  1180. {
  1181. return HAL_ERROR;
  1182. }
  1183. /* Get the MACMDIOAR value */
  1184. WRITE_REG(tmpreg, heth->Instance->MACMDIOAR);
  1185. /* Prepare the MDIO Address Register value
  1186. - Set the PHY device address
  1187. - Set the PHY register address
  1188. - Set the read mode
  1189. - Set the MII Busy bit */
  1190. MODIFY_REG(tmpreg, ETH_MACMDIOAR_PA, (PHYAddr <<21));
  1191. MODIFY_REG(tmpreg, ETH_MACMDIOAR_RDA, (PHYReg << 16));
  1192. MODIFY_REG(tmpreg, ETH_MACMDIOAR_MOC, ETH_MACMDIOAR_MOC_RD);
  1193. SET_BIT(tmpreg, ETH_MACMDIOAR_MB);
  1194. /* Write the result value into the MDII Address register */
  1195. WRITE_REG(heth->Instance->MACMDIOAR, tmpreg);
  1196. tickstart = HAL_GetTick();
  1197. /* Wait for the Busy flag */
  1198. while(READ_BIT(heth->Instance->MACMDIOAR, ETH_MACMDIOAR_MB))
  1199. {
  1200. if(((HAL_GetTick() - tickstart ) > ETH_MDIO_BUS_TIMEOUT))
  1201. {
  1202. return HAL_ERROR;
  1203. }
  1204. }
  1205. /* Get MACMIIDR value */
  1206. WRITE_REG(*pRegValue, (uint16_t)heth->Instance->MACMDIODR);
  1207. return HAL_OK;
  1208. }
  1209. /**
  1210. * @brief Writes to a PHY register.
  1211. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1212. * the configuration information for ETHERNET module
  1213. * @param PHYAddr: PHY port address, must be a value from 0 to 31
  1214. * @param PHYReg: PHY register address, must be a value from 0 to 31
  1215. * @param RegValue: the value to write
  1216. * @retval HAL status
  1217. */
  1218. HAL_StatusTypeDef HAL_ETH_WritePHYRegister(ETH_HandleTypeDef *heth, uint32_t PHYAddr, uint32_t PHYReg, uint32_t RegValue)
  1219. {
  1220. uint32_t tmpreg = 0;
  1221. uint32_t tickstart = 0;
  1222. /* Check for the Busy flag */
  1223. if(READ_BIT(heth->Instance->MACMDIOAR, ETH_MACMDIOAR_MB))
  1224. {
  1225. return HAL_ERROR;
  1226. }
  1227. /* Get the MACMDIOAR value */
  1228. WRITE_REG(tmpreg, heth->Instance->MACMDIOAR);
  1229. /* Prepare the MDIO Address Register value
  1230. - Set the PHY device address
  1231. - Set the PHY register address
  1232. - Set the write mode
  1233. - Set the MII Busy bit */
  1234. MODIFY_REG(tmpreg, ETH_MACMDIOAR_PA, (PHYAddr <<21));
  1235. MODIFY_REG(tmpreg, ETH_MACMDIOAR_RDA, (PHYReg << 16));
  1236. MODIFY_REG(tmpreg, ETH_MACMDIOAR_MOC, ETH_MACMDIOAR_MOC_WR);
  1237. SET_BIT(tmpreg, ETH_MACMDIOAR_MB);
  1238. /* Give the value to the MII data register */
  1239. WRITE_REG(ETH->MACMDIODR, (uint16_t)RegValue);
  1240. /* Write the result value into the MII Address register */
  1241. WRITE_REG(ETH->MACMDIOAR, tmpreg);
  1242. tickstart = HAL_GetTick();
  1243. /* Wait for the Busy flag */
  1244. while(READ_BIT(heth->Instance->MACMDIOAR, ETH_MACMDIOAR_MB))
  1245. {
  1246. if(((HAL_GetTick() - tickstart ) > ETH_MDIO_BUS_TIMEOUT))
  1247. {
  1248. return HAL_ERROR;
  1249. }
  1250. }
  1251. return HAL_OK;
  1252. }
  1253. /**
  1254. * @}
  1255. */
  1256. /** @defgroup ETH_Exported_Functions_Group3 Peripheral Control functions
  1257. * @brief ETH control functions
  1258. *
  1259. @verbatim
  1260. ==============================================================================
  1261. ##### Peripheral Control functions #####
  1262. ==============================================================================
  1263. [..]
  1264. This subsection provides a set of functions allowing to control the ETH
  1265. peripheral.
  1266. @endverbatim
  1267. * @{
  1268. */
  1269. /**
  1270. * @brief Get the configuration of the MAC and MTL subsystems.
  1271. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1272. * the configuration information for ETHERNET module
  1273. * @param macconf: pointer to a ETH_MACConfigTypeDef structure that will hold
  1274. * the configuration of the MAC.
  1275. * @retval HAL Status
  1276. */
  1277. HAL_StatusTypeDef HAL_ETH_GetMACConfig(ETH_HandleTypeDef *heth, ETH_MACConfigTypeDef *macconf)
  1278. {
  1279. if (macconf == NULL)
  1280. {
  1281. return HAL_ERROR;
  1282. }
  1283. /* Get MAC parameters */
  1284. macconf->PreambleLength = READ_BIT(heth->Instance->MACCR, ETH_MACCR_PRELEN);
  1285. macconf->DeferralCheck = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_DC)>> 4);
  1286. macconf->BackOffLimit = READ_BIT(heth->Instance->MACCR, ETH_MACCR_BL);
  1287. macconf->RetryTransmission = (FunctionalState)(!(READ_BIT(heth->Instance->MACCR, ETH_MACCR_DR) >> 8));
  1288. macconf->CarrierSenseDuringTransmit = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_DCRS) >> 9);
  1289. macconf->ReceiveOwn = (FunctionalState)(!(READ_BIT(heth->Instance->MACCR, ETH_MACCR_DO) >> 10));
  1290. macconf->CarrierSenseBeforeTransmit = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_ECRSFD) >> 11);
  1291. macconf->LoopbackMode = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_LM) >> 12);
  1292. macconf->DuplexMode = READ_BIT(heth->Instance->MACCR, ETH_MACCR_DM);
  1293. macconf->Speed = READ_BIT(heth->Instance->MACCR, ETH_MACCR_FES);
  1294. macconf->JumboPacket = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_JE) >> 16);
  1295. macconf->Jabber = (FunctionalState)(!(READ_BIT(heth->Instance->MACCR, ETH_MACCR_JD) >>17));
  1296. macconf->Watchdog = (FunctionalState)(!(READ_BIT(heth->Instance->MACCR, ETH_MACCR_WD) >>19));
  1297. macconf->AutomaticPadCRCStrip = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_ACS) >> 20);
  1298. macconf->CRCStripTypePacket = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_CST) >> 21);
  1299. macconf->Support2KPacket = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_S2KP) >> 22);
  1300. macconf->GiantPacketSizeLimitControl = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_GPSLCE) >> 23);
  1301. macconf->InterPacketGapVal = READ_BIT(heth->Instance->MACCR, ETH_MACCR_IPG);
  1302. macconf->ChecksumOffload = (FunctionalState)(READ_BIT(heth->Instance->MACCR, ETH_MACCR_IPC) >> 27);
  1303. macconf->SourceAddrControl = READ_BIT(heth->Instance->MACCR, ETH_MACCR_SARC);
  1304. macconf->GiantPacketSizeLimit = READ_BIT(heth->Instance->MACECR, ETH_MACECR_GPSL);
  1305. macconf->CRCCheckingRxPackets = (FunctionalState)(!(READ_BIT(heth->Instance->MACECR, ETH_MACECR_DCRCC) >> 16));
  1306. macconf->SlowProtocolDetect = (FunctionalState)(READ_BIT(heth->Instance->MACECR, ETH_MACECR_SPEN) >> 17);
  1307. macconf->UnicastSlowProtocolPacketDetect = (FunctionalState)(READ_BIT(heth->Instance->MACECR, ETH_MACECR_USP) >> 18);
  1308. macconf->ExtendedInterPacketGap = (FunctionalState)(READ_BIT(heth->Instance->MACECR, ETH_MACECR_EIPGEN) >> 24);
  1309. macconf->ExtendedInterPacketGapVal = READ_BIT(heth->Instance->MACECR, ETH_MACECR_EIPG) >> 25;
  1310. macconf->ProgrammableWatchdog = (FunctionalState)(READ_BIT(heth->Instance->MACWTR, ETH_MACWTR_PWE) >> 8);
  1311. macconf->WatchdogTimeout = READ_BIT(heth->Instance->MACWTR, ETH_MACWTR_WTO);
  1312. macconf->TransmitFlowControl = (FunctionalState)(READ_BIT(heth->Instance->MACTFCR, ETH_MACTFCR_TFE) >> 1);
  1313. macconf->ZeroQuantaPause = (FunctionalState)(!(READ_BIT(heth->Instance->MACTFCR, ETH_MACTFCR_DZPQ) >> 7));
  1314. macconf->PauseLowThreshold = READ_BIT(heth->Instance->MACTFCR, ETH_MACTFCR_PLT);
  1315. macconf->PauseTime = (READ_BIT(heth->Instance->MACTFCR, ETH_MACTFCR_PT) >> 16);
  1316. macconf->ReceiveFlowControl = (FunctionalState)READ_BIT(heth->Instance->MACRFCR, ETH_MACRFCR_RFE);
  1317. macconf->UnicastPausePacketDetect = (FunctionalState)(READ_BIT(heth->Instance->MACRFCR, ETH_MACRFCR_UP) >> 1);
  1318. macconf->TransmitQueueMode = READ_BIT(heth->Instance->MTLTQOMR, (ETH_MTLTQOMR_TTC | ETH_MTLTQOMR_TSF));
  1319. macconf->ReceiveQueueMode = READ_BIT(heth->Instance->MTLRQOMR, (ETH_MTLRQOMR_RTC | ETH_MTLRQOMR_RSF));
  1320. macconf->ForwardRxUndersizedGoodPacket = (FunctionalState)(READ_BIT(heth->Instance->MTLRQOMR, ETH_MTLRQOMR_FUP) >> 3);
  1321. macconf->ForwardRxErrorPacket = (FunctionalState)(READ_BIT(heth->Instance->MTLRQOMR, ETH_MTLRQOMR_FEP) >> 4);
  1322. macconf->DropTCPIPChecksumErrorPacket = (FunctionalState)(!(READ_BIT(heth->Instance->MTLRQOMR, ETH_MTLRQOMR_DISTCPEF) >> 6));
  1323. return HAL_OK;
  1324. }
  1325. /**
  1326. * @brief Get the configuration of the DMA.
  1327. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1328. * the configuration information for ETHERNET module
  1329. * @param dmaconf: pointer to a ETH_DMAConfigTypeDef structure that will hold
  1330. * the configuration of the ETH DMA.
  1331. * @retval HAL Status
  1332. */
  1333. HAL_StatusTypeDef HAL_ETH_GetDMAConfig(ETH_HandleTypeDef *heth, ETH_DMAConfigTypeDef *dmaconf)
  1334. {
  1335. if (dmaconf == NULL)
  1336. {
  1337. return HAL_ERROR;
  1338. }
  1339. dmaconf->AddressAlignedBeats = (FunctionalState)(READ_BIT(heth->Instance->DMASBMR, ETH_DMASBMR_AAL) >> 12);
  1340. dmaconf->BurstMode = READ_BIT(heth->Instance->DMASBMR, ETH_DMASBMR_FB | ETH_DMASBMR_MB);
  1341. dmaconf->RebuildINCRxBurst = (FunctionalState)(READ_BIT(heth->Instance->DMASBMR, ETH_DMASBMR_RB)>> 15);
  1342. dmaconf->DMAArbitration = READ_BIT(heth->Instance->DMAMR, (ETH_DMAMR_TXPR |ETH_DMAMR_PR | ETH_DMAMR_DA));
  1343. dmaconf->PBLx8Mode = (FunctionalState)(READ_BIT(heth->Instance->DMACCR, ETH_DMACCR_8PBL)>> 16);
  1344. dmaconf->MaximumSegmentSize = READ_BIT(heth->Instance->DMACCR, ETH_DMACCR_MSS);
  1345. dmaconf->FlushRxPacket = (FunctionalState)(READ_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_RPF) >> 31);
  1346. dmaconf->RxDMABurstLength = READ_BIT(heth->Instance->DMACRCR, ETH_DMACRCR_RPBL);
  1347. dmaconf->SecondPacketOperate = (FunctionalState)(READ_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_OSP) >> 4);
  1348. dmaconf->TCPSegmentation = (FunctionalState)(READ_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_TSE) >> 12);
  1349. dmaconf->TxDMABurstLength = READ_BIT(heth->Instance->DMACTCR, ETH_DMACTCR_TPBL);
  1350. return HAL_OK;
  1351. }
  1352. /**
  1353. * @brief Set the MAC configuration.
  1354. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1355. * the configuration information for ETHERNET module
  1356. * @param macconf: pointer to a ETH_MACConfigTypeDef structure that contains
  1357. * the configuration of the MAC.
  1358. * @retval HAL status
  1359. */
  1360. HAL_StatusTypeDef HAL_ETH_SetMACConfig(ETH_HandleTypeDef *heth, ETH_MACConfigTypeDef *macconf)
  1361. {
  1362. if(macconf == NULL)
  1363. {
  1364. return HAL_ERROR;
  1365. }
  1366. if(heth->RxState == HAL_ETH_STATE_READY)
  1367. {
  1368. ETH_SetMACConfig(heth, macconf);
  1369. return HAL_OK;
  1370. }
  1371. else
  1372. {
  1373. return HAL_ERROR;
  1374. }
  1375. }
  1376. /**
  1377. * @brief Set the ETH DMA configuration.
  1378. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1379. * the configuration information for ETHERNET module
  1380. * @param dmaconf: pointer to a ETH_DMAConfigTypeDef structure that will hold
  1381. * the configuration of the ETH DMA.
  1382. * @retval HAL status
  1383. */
  1384. HAL_StatusTypeDef HAL_ETH_SetDMAConfig(ETH_HandleTypeDef *heth, ETH_DMAConfigTypeDef *dmaconf)
  1385. {
  1386. if(dmaconf == NULL)
  1387. {
  1388. return HAL_ERROR;
  1389. }
  1390. if(heth->RxState == HAL_ETH_STATE_READY)
  1391. {
  1392. ETH_SetDMAConfig(heth, dmaconf);
  1393. return HAL_OK;
  1394. }
  1395. else
  1396. {
  1397. return HAL_ERROR;
  1398. }
  1399. }
  1400. /**
  1401. * @brief Configures the Clock range of ETH MDIO interface.
  1402. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1403. * the configuration information for ETHERNET module
  1404. * @retval None
  1405. */
  1406. void HAL_ETH_SetMDIOClockRange(ETH_HandleTypeDef *heth)
  1407. {
  1408. uint32_t tmpreg = 0;
  1409. uint32_t hclk = 0;
  1410. /* Get the ETHERNET MACMDIOAR value */
  1411. tmpreg = (heth->Instance)->MACMDIOAR;
  1412. /* Clear CSR Clock Range bits */
  1413. tmpreg &= ~ETH_MACMDIOAR_CR;
  1414. /* Get hclk frequency value */
  1415. hclk = HAL_RCC_GetHCLKFreq();
  1416. /* Set CR bits depending on hclk value */
  1417. if((hclk >= 20000000)&&(hclk < 35000000))
  1418. {
  1419. /* CSR Clock Range between 20-35 MHz */
  1420. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV16;
  1421. }
  1422. else if((hclk >= 35000000)&&(hclk < 60000000))
  1423. {
  1424. /* CSR Clock Range between 35-60 MHz */
  1425. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV26;
  1426. }
  1427. else if((hclk >= 60000000)&&(hclk < 100000000))
  1428. {
  1429. /* CSR Clock Range between 60-100 MHz */
  1430. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV42;
  1431. }
  1432. else if((hclk >= 100000000)&&(hclk < 150000000))
  1433. {
  1434. /* CSR Clock Range between 100-150 MHz */
  1435. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV62;
  1436. }
  1437. else /* (hclk >= 150000000)&&(hclk <= 200000000) */
  1438. {
  1439. /* CSR Clock Range between 150-200 MHz */
  1440. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV102;
  1441. }
  1442. /* Configure the CSR Clock Range */
  1443. (heth->Instance)->MACMDIOAR = (uint32_t)tmpreg;
  1444. }
  1445. /**
  1446. * @brief Set the ETH MAC (L2) Filters configuration.
  1447. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1448. * the configuration information for ETHERNET module
  1449. * @param pFilterConfig: pointer to a ETH_MACFilterConfigTypeDef structure that contains
  1450. * the configuration of the ETH MAC filters.
  1451. * @retval HAL status
  1452. */
  1453. HAL_StatusTypeDef HAL_ETH_SetMACFilterConfig(ETH_HandleTypeDef *heth, ETH_MACFilterConfigTypeDef *pFilterConfig)
  1454. {
  1455. uint32_t filterconfig = 0;
  1456. if(pFilterConfig == NULL)
  1457. {
  1458. return HAL_ERROR;
  1459. }
  1460. filterconfig = (pFilterConfig->PromiscuousMode |
  1461. ((uint32_t)pFilterConfig->HashUnicast << 1) |
  1462. ((uint32_t)pFilterConfig->HashMulticast << 2) |
  1463. ((uint32_t)pFilterConfig->DestAddrInverseFiltering << 3) |
  1464. ((uint32_t)pFilterConfig->PassAllMulticast << 4) |
  1465. ((uint32_t)!pFilterConfig->BroadcastFilter << 5) |
  1466. ((uint32_t)pFilterConfig->SrcAddrInverseFiltering << 8) |
  1467. ((uint32_t)pFilterConfig->SrcAddrFiltering << 9) |
  1468. ((uint32_t)pFilterConfig->HachOrPerfectFilter << 10) |
  1469. ((uint32_t)pFilterConfig->ReceiveAllMode << 31) |
  1470. pFilterConfig->ControlPacketsFilter);
  1471. MODIFY_REG(heth->Instance->MACPFR, ETH_MACPFR_MASK, filterconfig);
  1472. return HAL_OK;
  1473. }
  1474. /**
  1475. * @brief Get the ETH MAC (L2) Filters configuration.
  1476. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1477. * the configuration information for ETHERNET module
  1478. * @param pFilterConfig: pointer to a ETH_MACFilterConfigTypeDef structure that will hold
  1479. * the configuration of the ETH MAC filters.
  1480. * @retval HAL status
  1481. */
  1482. HAL_StatusTypeDef HAL_ETH_GetMACFilterConfig(ETH_HandleTypeDef *heth, ETH_MACFilterConfigTypeDef *pFilterConfig)
  1483. {
  1484. if(pFilterConfig == NULL)
  1485. {
  1486. return HAL_ERROR;
  1487. }
  1488. pFilterConfig->PromiscuousMode = (FunctionalState)(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_PR));
  1489. pFilterConfig->HashUnicast = (FunctionalState)(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_HUC) >> 1);
  1490. pFilterConfig->HashMulticast = (FunctionalState)(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_HMC) >> 2);
  1491. pFilterConfig->DestAddrInverseFiltering = (FunctionalState)(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_DAIF) >> 3);
  1492. pFilterConfig->PassAllMulticast = (FunctionalState)(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_PM) >> 4);
  1493. pFilterConfig->BroadcastFilter = (FunctionalState)(!(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_DBF) >> 5));
  1494. pFilterConfig->ControlPacketsFilter = READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_PCF);
  1495. pFilterConfig->SrcAddrInverseFiltering = (FunctionalState)(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_SAIF) >> 8);
  1496. pFilterConfig->SrcAddrFiltering = (FunctionalState)(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_SAF) >> 9);
  1497. pFilterConfig->HachOrPerfectFilter = (FunctionalState)(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_HPF) >> 10);
  1498. pFilterConfig->ReceiveAllMode = (FunctionalState)(READ_BIT(heth->Instance->MACPFR, ETH_MACPFR_RA) >> 31);
  1499. return HAL_OK;
  1500. }
  1501. /**
  1502. * @brief Set the source MAC Address to be matched.
  1503. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1504. * the configuration information for ETHERNET module
  1505. * @param AddrNbr: The MAC address to configure
  1506. * This parameter must be a value of the following:
  1507. * ETH_MAC_ADDRESS1
  1508. * ETH_MAC_ADDRESS2
  1509. * ETH_MAC_ADDRESS3
  1510. * @param pMACAddr: Pointer to MAC address buffer data (6 bytes)
  1511. * @retval HAL status
  1512. */
  1513. HAL_StatusTypeDef HAL_ETH_SetSourceMACAddrMatch(ETH_HandleTypeDef *heth, uint32_t AddrNbr, uint8_t *pMACAddr)
  1514. {
  1515. uint32_t macaddrhr, macaddrlr;
  1516. if(pMACAddr == NULL)
  1517. {
  1518. return HAL_ERROR;
  1519. }
  1520. /* Get mac addr high reg offset */
  1521. macaddrhr = ((uint32_t)&(heth->Instance->MACA0HR) + AddrNbr);
  1522. /* Get mac addr low reg offset */
  1523. macaddrlr = ((uint32_t)&(heth->Instance->MACA0LR) + AddrNbr);
  1524. /* Set MAC addr bits 32 to 47 */
  1525. (*(__IO uint32_t *)macaddrhr) = ((pMACAddr[5] << 8) | pMACAddr[4]);
  1526. /* Set MAC addr bits 0 to 31 */
  1527. (*(__IO uint32_t *)macaddrlr) = ((pMACAddr[3] << 24) | (pMACAddr[2] << 16) | (pMACAddr[1] << 8) | pMACAddr[0]);
  1528. /* Enable address and set source address bit */
  1529. (*(__IO uint32_t *)macaddrhr) |= (ETH_MACAHR_SA | ETH_MACAHR_AE);
  1530. return HAL_OK;
  1531. }
  1532. /**
  1533. * @brief Set the ETH Hash Table Value.
  1534. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1535. * the configuration information for ETHERNET module
  1536. * @param pHashTable: pointer to a table of two 32 bit values, that contains
  1537. * the 64 bits of the hash table.
  1538. * @retval HAL status
  1539. */
  1540. HAL_StatusTypeDef HAL_ETH_SetHashTable(ETH_HandleTypeDef *heth, uint32_t *pHashTable)
  1541. {
  1542. if(pHashTable == NULL)
  1543. {
  1544. return HAL_ERROR;
  1545. }
  1546. heth->Instance->MACHT0R = pHashTable[0];
  1547. heth->Instance->MACHT1R = pHashTable[1];
  1548. return HAL_OK;
  1549. }
  1550. /**
  1551. * @brief Set the VLAN Identifier for Rx packets
  1552. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1553. * the configuration information for ETHERNET module
  1554. * @param ComparisonBits: 12 or 16 bit comparison mode
  1555. must be a value of @ref ETH_VLAN_Tag_Comparison
  1556. * @param VLANIdentifier: VLAN Identifier value
  1557. * @retval None
  1558. */
  1559. void HAL_ETH_SetRxVLANIdentifier(ETH_HandleTypeDef *heth, uint32_t ComparisonBits, uint32_t VLANIdentifier)
  1560. {
  1561. if(ComparisonBits == ETH_VLANTAGCOMPARISON_16BIT)
  1562. {
  1563. MODIFY_REG(heth->Instance->MACVTR, ETH_MACVTR_VL , VLANIdentifier);
  1564. CLEAR_BIT(heth->Instance->MACVTR, ETH_MACVTR_ETV);
  1565. }
  1566. else
  1567. {
  1568. MODIFY_REG(heth->Instance->MACVTR, ETH_MACVTR_VL_VID , VLANIdentifier);
  1569. SET_BIT(heth->Instance->MACVTR, ETH_MACVTR_ETV);
  1570. }
  1571. }
  1572. /**
  1573. * @brief Enters the Power down mode.
  1574. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1575. * the configuration information for ETHERNET module
  1576. * @param pPowerDownConfig: a pointer to ETH_PowerDownConfigTypeDef structure
  1577. * that contains the Power Down configration
  1578. * @retval None.
  1579. */
  1580. void HAL_ETH_EnterPowerDownMode(ETH_HandleTypeDef *heth, ETH_PowerDownConfigTypeDef *pPowerDownConfig)
  1581. {
  1582. uint32_t powerdownconfig;
  1583. powerdownconfig = (((uint32_t)pPowerDownConfig->MagicPacket << 1) |
  1584. ((uint32_t)pPowerDownConfig->WakeUpPacket << 2) |
  1585. ((uint32_t)pPowerDownConfig->GlobalUnicast << 9) |
  1586. ((uint32_t)pPowerDownConfig->WakeUpForward << 10) |
  1587. ETH_MACPCSR_PWRDWN);
  1588. /* Enable PMT interrupt */
  1589. __HAL_ETH_MAC_ENABLE_IT(heth, ETH_MACIER_PMTIE);
  1590. MODIFY_REG(heth->Instance->MACPCSR, ETH_MACPCSR_MASK, powerdownconfig);
  1591. }
  1592. /**
  1593. * @brief Exits from the Power down mode.
  1594. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1595. * the configuration information for ETHERNET module
  1596. * @retval None.
  1597. */
  1598. void HAL_ETH_ExitPowerDownMode(ETH_HandleTypeDef *heth)
  1599. {
  1600. /* clear wake up sources */
  1601. CLEAR_BIT(heth->Instance->MACPCSR, ETH_MACPCSR_RWKPKTEN | ETH_MACPCSR_MGKPKTEN | ETH_MACPCSR_GLBLUCAST | ETH_MACPCSR_RWKPFE);
  1602. if(READ_BIT(heth->Instance->MACPCSR, ETH_MACPCSR_PWRDWN))
  1603. {
  1604. /* Exit power down mode */
  1605. CLEAR_BIT(heth->Instance->MACPCSR, ETH_MACPCSR_PWRDWN);
  1606. }
  1607. /* Disable PMT interrupt */
  1608. __HAL_ETH_MAC_DISABLE_IT(heth, ETH_MACIER_PMTIE);
  1609. }
  1610. /**
  1611. * @brief Set the WakeUp filter.
  1612. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1613. * the configuration information for ETHERNET module
  1614. * @param pFilter: pointer to filter registers values
  1615. * @param Count: number of filter registers, must be from 1 to 8.
  1616. * @retval None.
  1617. */
  1618. HAL_StatusTypeDef HAL_ETH_SetWakeUpFilter(ETH_HandleTypeDef *heth, uint32_t *pFilter, uint32_t Count)
  1619. {
  1620. uint32_t regindex;
  1621. if(pFilter == NULL)
  1622. {
  1623. return HAL_ERROR;
  1624. }
  1625. /* Reset Filter Pointer */
  1626. SET_BIT(heth->Instance->MACPCSR, ETH_MACPCSR_RWKFILTRST);
  1627. /* Wake up packet filter config */
  1628. for(regindex = 0; regindex < Count; regindex++)
  1629. {
  1630. /* Write filter regs */
  1631. WRITE_REG(heth->Instance->MACRWKPFR, pFilter[regindex]);
  1632. }
  1633. return HAL_OK;
  1634. }
  1635. /**
  1636. * @}
  1637. */
  1638. /** @defgroup ETH_Exported_Functions_Group4 Peripheral State and Errors functions
  1639. * @brief ETH State and Errors functions
  1640. *
  1641. @verbatim
  1642. ==============================================================================
  1643. ##### Peripheral State and Errors functions #####
  1644. ==============================================================================
  1645. [..]
  1646. This subsection provides a set of functions allowing to return the State of
  1647. ETH communication process, return Peripheral Errors occurred during communication
  1648. process
  1649. @endverbatim
  1650. * @{
  1651. */
  1652. /**
  1653. * @brief Returns the ETH state.
  1654. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1655. * the configuration information for ETHERNET module
  1656. * @retval HAL state
  1657. */
  1658. HAL_ETH_StateTypeDef HAL_ETH_GetState(ETH_HandleTypeDef *heth)
  1659. {
  1660. uint32_t gstate= 0x00U, rxstate = 0x00U;
  1661. gstate = heth->gState;
  1662. rxstate = heth->RxState;
  1663. return (HAL_ETH_StateTypeDef)(gstate | rxstate);
  1664. }
  1665. /**
  1666. * @brief Returns the ETH error code
  1667. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1668. * the configuration information for ETHERNET module
  1669. * @retval ETH Error Code
  1670. */
  1671. uint32_t HAL_ETH_GetError(ETH_HandleTypeDef *heth)
  1672. {
  1673. return heth->ErrorCode;
  1674. }
  1675. /**
  1676. * @brief Returns the ETH DMA error code
  1677. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1678. * the configuration information for ETHERNET module
  1679. * @retval ETH DMA Error Code
  1680. */
  1681. uint32_t HAL_ETH_GetDMAError(ETH_HandleTypeDef *heth)
  1682. {
  1683. return heth->DMAErrorCode;
  1684. }
  1685. /**
  1686. * @brief Returns the ETH MAC error code
  1687. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1688. * the configuration information for ETHERNET module
  1689. * @retval ETH MAC Error Code
  1690. */
  1691. uint32_t HAL_ETH_GetMACError(ETH_HandleTypeDef *heth)
  1692. {
  1693. return heth->MACErrorCode;
  1694. }
  1695. /**
  1696. * @brief Returns the ETH MAC WakeUp event source
  1697. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1698. * the configuration information for ETHERNET module
  1699. * @retval ETH MAC WakeUp event source
  1700. */
  1701. uint32_t HAL_ETH_GetMACWakeUpSource(ETH_HandleTypeDef *heth)
  1702. {
  1703. return heth->MACWakeUpEvent;
  1704. }
  1705. /**
  1706. * @}
  1707. */
  1708. /**
  1709. * @}
  1710. */
  1711. static void ETH_SetMACConfig(ETH_HandleTypeDef *heth, ETH_MACConfigTypeDef *macconf)
  1712. {
  1713. uint32_t macregval = 0;
  1714. /*------------------------ MACCR Configuration --------------------*/
  1715. macregval =(macconf->InterPacketGapVal |
  1716. macconf->SourceAddrControl |
  1717. (uint32_t)(macconf->ChecksumOffload << 27) |
  1718. (uint32_t)(macconf->GiantPacketSizeLimitControl << 23) |
  1719. (uint32_t)(macconf->Support2KPacket << 22) |
  1720. (uint32_t)(macconf->CRCStripTypePacket << 21) |
  1721. (uint32_t)(macconf->AutomaticPadCRCStrip << 20) |
  1722. (uint32_t)(!macconf->Watchdog << 19) |
  1723. (uint32_t)(!macconf->Jabber << 17) |
  1724. (uint32_t)(macconf->JumboPacket << 16) |
  1725. macconf->Speed |
  1726. macconf->DuplexMode |
  1727. (uint32_t)(macconf->LoopbackMode << 12) |
  1728. (uint32_t)(macconf->CarrierSenseBeforeTransmit << 11)|
  1729. (uint32_t)(!macconf->ReceiveOwn << 10)|
  1730. (uint32_t)(macconf->CarrierSenseDuringTransmit << 9)|
  1731. (uint32_t)(!macconf->RetryTransmission << 8)|
  1732. macconf->BackOffLimit |
  1733. (uint32_t)(macconf->DeferralCheck << 4)|
  1734. macconf->PreambleLength);
  1735. /* Write to MACCR */
  1736. MODIFY_REG(heth->Instance->MACCR, ETH_MACCR_MASK, macregval);
  1737. /*------------------------ MACECR Configuration --------------------*/
  1738. macregval = ((macconf->ExtendedInterPacketGapVal << 25)|
  1739. (uint32_t)(macconf->ExtendedInterPacketGap << 24)|
  1740. (uint32_t)(macconf->UnicastSlowProtocolPacketDetect << 18)|
  1741. (uint32_t)(macconf->SlowProtocolDetect << 17)|
  1742. (uint32_t)(!macconf->CRCCheckingRxPackets << 16) |
  1743. macconf->GiantPacketSizeLimit);
  1744. /* Write to MACECR */
  1745. MODIFY_REG(heth->Instance->MACECR, ETH_MACECR_MASK, macregval);
  1746. /*------------------------ MACWTR Configuration --------------------*/
  1747. macregval = ((uint32_t)(macconf->ProgrammableWatchdog << 8) |
  1748. macconf->WatchdogTimeout);
  1749. /* Write to MACWTR */
  1750. MODIFY_REG(heth->Instance->MACWTR, ETH_MACWTR_MASK, macregval);
  1751. /*------------------------ MACTFCR Configuration --------------------*/
  1752. macregval = ((uint32_t)(macconf->TransmitFlowControl << 1) |
  1753. macconf->PauseLowThreshold |
  1754. (uint32_t)(!macconf->ZeroQuantaPause << 7) |
  1755. (macconf->PauseTime << 16));
  1756. /* Write to MACTFCR */
  1757. MODIFY_REG(heth->Instance->MACTFCR, ETH_MACTFCR_MASK, macregval);
  1758. /*------------------------ MACRFCR Configuration --------------------*/
  1759. macregval = ((uint32_t)macconf->ReceiveFlowControl |
  1760. (uint32_t)(macconf->UnicastPausePacketDetect << 1));
  1761. /* Write to MACRFCR */
  1762. MODIFY_REG(heth->Instance->MACRFCR, ETH_MACRFCR_MASK, macregval);
  1763. /*------------------------ MTLTQOMR Configuration --------------------*/
  1764. /* Write to MTLTQOMR */
  1765. MODIFY_REG(heth->Instance->MTLTQOMR, ETH_MTLTQOMR_MASK, macconf->TransmitQueueMode);
  1766. /*------------------------ MTLRQOMR Configuration --------------------*/
  1767. macregval = (macconf->ReceiveQueueMode |
  1768. (uint32_t)(!macconf->DropTCPIPChecksumErrorPacket << 6) |
  1769. (uint32_t)(macconf->ForwardRxErrorPacket << 4) |
  1770. (uint32_t)(macconf->ForwardRxUndersizedGoodPacket << 3));
  1771. /* Write to MTLRQOMR */
  1772. MODIFY_REG(heth->Instance->MTLRQOMR, ETH_MTLRQOMR_MASK, macregval);
  1773. }
  1774. static void ETH_SetDMAConfig(ETH_HandleTypeDef *heth, ETH_DMAConfigTypeDef *dmaconf)
  1775. {
  1776. uint32_t dmaregval = 0;
  1777. /*------------------------ DMAMR Configuration --------------------*/
  1778. MODIFY_REG(heth->Instance->DMAMR, ETH_DMAMR_MASK, dmaconf->DMAArbitration);
  1779. /*------------------------ DMASBMR Configuration --------------------*/
  1780. dmaregval = ((uint32_t)(dmaconf->AddressAlignedBeats << 12) |
  1781. dmaconf->BurstMode |
  1782. (uint32_t)(dmaconf->RebuildINCRxBurst << 15));
  1783. MODIFY_REG(heth->Instance->DMASBMR, ETH_DMASBMR_MASK, dmaregval);
  1784. /*------------------------ DMACCR Configuration --------------------*/
  1785. dmaregval = ((uint32_t)(dmaconf->PBLx8Mode <<16) |
  1786. dmaconf->MaximumSegmentSize);
  1787. MODIFY_REG(heth->Instance->DMACCR, ETH_DMACCR_MASK, dmaregval);
  1788. /*------------------------ DMACTCR Configuration --------------------*/
  1789. dmaregval = (dmaconf->TxDMABurstLength |
  1790. (uint32_t)(dmaconf->SecondPacketOperate << 4)|
  1791. (uint32_t)(dmaconf->TCPSegmentation << 12));
  1792. MODIFY_REG(heth->Instance->DMACTCR, ETH_DMACTCR_MASK, dmaregval);
  1793. /*------------------------ DMACRCR Configuration --------------------*/
  1794. dmaregval = ((uint32_t)(dmaconf->FlushRxPacket << 31) |
  1795. dmaconf->RxDMABurstLength);
  1796. /* Write to DMACRCR */
  1797. MODIFY_REG(heth->Instance->DMACRCR, ETH_DMACRCR_MASK, dmaregval);
  1798. }
  1799. /**
  1800. * @brief Configures Ethernet MAC and DMA with default parameters.
  1801. * called by HAL_ETH_Init() API.
  1802. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1803. * the configuration information for ETHERNET module
  1804. * @retval HAL status
  1805. */
  1806. static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth)
  1807. {
  1808. ETH_MACConfigTypeDef macDefaultConf;
  1809. ETH_DMAConfigTypeDef dmaDefaultConf;
  1810. /*--------------- ETHERNET MAC registers default Configuration --------------*/
  1811. macDefaultConf.AutomaticPadCRCStrip = ENABLE;
  1812. macDefaultConf.BackOffLimit = ETH_BACKOFFLIMIT_10;
  1813. macDefaultConf.CarrierSenseBeforeTransmit = DISABLE;
  1814. macDefaultConf.CarrierSenseDuringTransmit = DISABLE;
  1815. macDefaultConf.ChecksumOffload = ENABLE;
  1816. macDefaultConf.CRCCheckingRxPackets = ENABLE;
  1817. macDefaultConf.CRCStripTypePacket = ENABLE;
  1818. macDefaultConf.DeferralCheck = DISABLE;
  1819. macDefaultConf.DropTCPIPChecksumErrorPacket = ENABLE;
  1820. macDefaultConf.DuplexMode = ETH_FULLDUPLEX_MODE;
  1821. macDefaultConf.ExtendedInterPacketGap = DISABLE;
  1822. macDefaultConf.ExtendedInterPacketGapVal = 0x0;
  1823. macDefaultConf.ForwardRxErrorPacket = DISABLE;
  1824. macDefaultConf.ForwardRxUndersizedGoodPacket = DISABLE;
  1825. macDefaultConf.GiantPacketSizeLimit = 0x618;
  1826. macDefaultConf.GiantPacketSizeLimitControl = DISABLE;
  1827. macDefaultConf.InterPacketGapVal = ETH_INTERPACKETGAP_96BIT;
  1828. macDefaultConf.Jabber = ENABLE;
  1829. macDefaultConf.JumboPacket = DISABLE;
  1830. macDefaultConf.LoopbackMode = DISABLE;
  1831. macDefaultConf.PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS_4;
  1832. macDefaultConf.PauseTime = 0x0;
  1833. macDefaultConf.PreambleLength = ETH_PREAMBLELENGTH_7;
  1834. macDefaultConf.ProgrammableWatchdog = DISABLE;
  1835. macDefaultConf.ReceiveFlowControl = DISABLE;
  1836. macDefaultConf.ReceiveOwn = ENABLE;
  1837. macDefaultConf.ReceiveQueueMode = ETH_RECEIVESTOREFORWARD;
  1838. macDefaultConf.RetryTransmission = ENABLE;
  1839. macDefaultConf.SlowProtocolDetect = DISABLE;
  1840. macDefaultConf.SourceAddrControl = ETH_SOURCEADDRESS_REPLACE_ADDR0;
  1841. macDefaultConf.Speed = ETH_SPEED_100M;
  1842. macDefaultConf.Support2KPacket = DISABLE;
  1843. macDefaultConf.TransmitQueueMode = ETH_TRANSMITSTOREFORWARD;
  1844. macDefaultConf.TransmitFlowControl = DISABLE;
  1845. macDefaultConf.UnicastPausePacketDetect = DISABLE;
  1846. macDefaultConf.UnicastSlowProtocolPacketDetect = DISABLE;
  1847. macDefaultConf.Watchdog = ENABLE;
  1848. macDefaultConf.WatchdogTimeout = ETH_MACWTR_WTO_2KB;
  1849. macDefaultConf.ZeroQuantaPause = ENABLE;
  1850. /* MAC default configuration */
  1851. ETH_SetMACConfig(heth, &macDefaultConf);
  1852. /*--------------- ETHERNET DMA registers default Configuration --------------*/
  1853. dmaDefaultConf.AddressAlignedBeats = ENABLE;
  1854. dmaDefaultConf.BurstMode = ETH_BURSTLENGTH_FIXED;
  1855. dmaDefaultConf.DMAArbitration = ETH_DMAARBITRATION_RX1_TX1;
  1856. dmaDefaultConf.FlushRxPacket = DISABLE;
  1857. dmaDefaultConf.PBLx8Mode = DISABLE;
  1858. dmaDefaultConf.RebuildINCRxBurst = DISABLE;
  1859. dmaDefaultConf.RxDMABurstLength = ETH_RXDMABURSTLENGTH_32BEAT;
  1860. dmaDefaultConf.SecondPacketOperate = DISABLE;
  1861. dmaDefaultConf.TxDMABurstLength = ETH_TXDMABURSTLENGTH_32BEAT;
  1862. dmaDefaultConf.TCPSegmentation = DISABLE;
  1863. dmaDefaultConf.MaximumSegmentSize = 536;
  1864. /* DMA default configuration */
  1865. ETH_SetDMAConfig(heth, &dmaDefaultConf);
  1866. }
  1867. /**
  1868. * @brief Configures the Clock range of SMI interface.
  1869. * called by HAL_ETH_Init() API.
  1870. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1871. * the configuration information for ETHERNET module
  1872. * @retval None
  1873. */
  1874. static void ETH_MAC_MDIO_ClkConfig(ETH_HandleTypeDef *heth)
  1875. {
  1876. uint32_t tmpreg = 0;
  1877. uint32_t hclk = 0;
  1878. /* Get the ETHERNET MACMDIOAR value */
  1879. tmpreg = (heth->Instance)->MACMDIOAR;
  1880. /* Clear CSR Clock Range bits */
  1881. tmpreg &= ~ETH_MACMDIOAR_CR;
  1882. /* Get hclk frequency value */
  1883. hclk = HAL_RCC_GetHCLKFreq();
  1884. /* Set CR bits depending on hclk value */
  1885. if((hclk >= 20000000)&&(hclk < 35000000))
  1886. {
  1887. /* CSR Clock Range between 20-35 MHz */
  1888. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV16;
  1889. }
  1890. else if((hclk >= 35000000)&&(hclk < 60000000))
  1891. {
  1892. /* CSR Clock Range between 35-60 MHz */
  1893. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV26;
  1894. }
  1895. else if((hclk >= 60000000)&&(hclk < 100000000))
  1896. {
  1897. /* CSR Clock Range between 60-100 MHz */
  1898. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV42;
  1899. }
  1900. else if((hclk >= 100000000)&&(hclk < 150000000))
  1901. {
  1902. /* CSR Clock Range between 100-150 MHz */
  1903. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV62;
  1904. }
  1905. else /* (hclk >= 150000000)&&(hclk <= 200000000) */
  1906. {
  1907. /* CSR Clock Range between 150-200 MHz */
  1908. tmpreg |= (uint32_t)ETH_MACMDIOAR_CR_DIV102;
  1909. }
  1910. /* Configure the CSR Clock Range */
  1911. (heth->Instance)->MACMDIOAR = (uint32_t)tmpreg;
  1912. }
  1913. /**
  1914. * @brief Initializes the DMA Tx descriptors.
  1915. * called by HAL_ETH_Init() API.
  1916. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1917. * the configuration information for ETHERNET module
  1918. * @retval None
  1919. */
  1920. static void ETH_DMATxDescListInit(ETH_HandleTypeDef *heth)
  1921. {
  1922. ETH_DMADescTypeDef *dmatxdesc;
  1923. uint32_t i;
  1924. /* Fill each DMATxDesc descriptor with the right values */
  1925. for(i=0; i < ETH_TX_DESC_CNT; i++)
  1926. {
  1927. dmatxdesc = heth->Init.TxDesc + i;
  1928. WRITE_REG(dmatxdesc->DESC0, 0x0);
  1929. WRITE_REG(dmatxdesc->DESC1, 0x0);
  1930. WRITE_REG(dmatxdesc->DESC2, 0x0);
  1931. WRITE_REG(dmatxdesc->DESC3, 0x0);
  1932. WRITE_REG(heth->TxDescList.TxDesc[i], (uint32_t)dmatxdesc);
  1933. }
  1934. heth->TxDescList.CurTxDesc = 0;
  1935. /* Set Transmit Descriptor Ring Length */
  1936. WRITE_REG(heth->Instance->DMACTDRLR, (ETH_TX_DESC_CNT -1));
  1937. /* Set Transmit Descriptor List Address */
  1938. WRITE_REG(heth->Instance->DMACTDLAR, (uint32_t) heth->Init.TxDesc);
  1939. /* Set Transmit Descriptor Tail pointer */
  1940. WRITE_REG(heth->Instance->DMACTDTPR, (uint32_t) heth->Init.TxDesc);
  1941. }
  1942. /**
  1943. * @brief Initializes the DMA Rx descriptors in chain mode.
  1944. * called by HAL_ETH_Init() API.
  1945. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1946. * the configuration information for ETHERNET module
  1947. * @retval None
  1948. */
  1949. static void ETH_DMARxDescListInit(ETH_HandleTypeDef *heth)
  1950. {
  1951. ETH_DMADescTypeDef *dmarxdesc;
  1952. uint32_t i;
  1953. for(i = 0; i < ETH_RX_DESC_CNT; i++)
  1954. {
  1955. dmarxdesc = heth->Init.RxDesc + i;
  1956. WRITE_REG(dmarxdesc->DESC0, 0x0);
  1957. WRITE_REG(dmarxdesc->DESC1, 0x0);
  1958. WRITE_REG(dmarxdesc->DESC2, 0x0);
  1959. WRITE_REG(dmarxdesc->DESC3, 0x0);
  1960. WRITE_REG(dmarxdesc->BackupAddr0, 0x0);
  1961. WRITE_REG(dmarxdesc->BackupAddr1, 0x0);
  1962. /* Set Rx descritors adresses */
  1963. WRITE_REG(heth->RxDescList.RxDesc[i], (uint32_t)dmarxdesc);
  1964. }
  1965. WRITE_REG(heth->RxDescList.CurRxDesc, 0);
  1966. WRITE_REG(heth->RxDescList.FirstAppDesc, 0);
  1967. WRITE_REG(heth->RxDescList.AppDescNbr, 0);
  1968. WRITE_REG(heth->RxDescList.ItMode, 0);
  1969. WRITE_REG(heth->RxDescList.AppContextDesc, 0);
  1970. /* Set Receive Descriptor Ring Length */
  1971. WRITE_REG(heth->Instance->DMACRDRLR, (ETH_RX_DESC_CNT - 1));
  1972. /* Set Receive Descriptor List Address */
  1973. WRITE_REG(heth->Instance->DMACRDLAR, (uint32_t) heth->Init.RxDesc);
  1974. /* Set Receive Descriptor Tail pointer Address */
  1975. WRITE_REG(heth->Instance->DMACRDTPR, (uint32_t) dmarxdesc);
  1976. }
  1977. /**
  1978. * @brief Prepare Tx DMA descriptor before transmission.
  1979. * called by HAL_ETH_Transmit_IT and HAL_ETH_Transmit_IT() API.
  1980. * @param heth: pointer to a ETH_HandleTypeDef structure that contains
  1981. * the configuration information for ETHERNET module
  1982. * @param pTxConfig: Tx packet configuration
  1983. * @param ItMode: Enable or disable Tx EOT interrept
  1984. * @retval None
  1985. */
  1986. static uint32_t ETH_Prepare_Tx_Descriptors(ETH_HandleTypeDef *heth, ETH_TxPacketConfig *pTxConfig, uint32_t ItMode)
  1987. {
  1988. ETH_TxDescListTypeDef *dmatxdesclist = &heth->TxDescList;
  1989. uint32_t descidx = dmatxdesclist->CurTxDesc;
  1990. uint32_t firstdescidx = dmatxdesclist->CurTxDesc;
  1991. uint32_t descnbr = 0, idx = 0;
  1992. ETH_DMADescTypeDef *dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  1993. ETH_BufferTypeDef *txbuffer = pTxConfig->TxBuffer;
  1994. /* Current Tx Descriptor Owned by DMA: cannot be used by the application */
  1995. if(READ_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCWBF_OWN) == ETH_DMATXNDESCWBF_OWN)
  1996. {
  1997. return HAL_ETH_ERROR_BUSY;
  1998. }
  1999. /***************************************************************************/
  2000. /***************** Context descriptor configuration (Optional) **********/
  2001. /***************************************************************************/
  2002. /* If VLAN tag is enabled for this packet */
  2003. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_VLANTAG))
  2004. {
  2005. /* Set vlan tag value */
  2006. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXCDESC_VT, pTxConfig->VlanTag);
  2007. /* Set vlan tag valid bit */
  2008. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_VLTV);
  2009. /* Set the descriptor as the vlan input source */
  2010. SET_BIT(heth->Instance->MACVIR, ETH_MACVIR_VLTI);
  2011. /* if inner VLAN is enabled */
  2012. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_INNERVLANTAG))
  2013. {
  2014. /* Set inner vlan tag value */
  2015. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXCDESC_IVT, (pTxConfig->InnerVlanTag << 16));
  2016. /* Set inner vlan tag valid bit */
  2017. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_IVLTV);
  2018. /* Set Vlan Tag control */
  2019. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXCDESC_IVTIR, pTxConfig->InnerVlanCtrl);
  2020. /* Set the descriptor as the inner vlan input source */
  2021. SET_BIT(heth->Instance->MACIVIR, ETH_MACIVIR_VLTI);
  2022. /* Enable double VLAN processing */
  2023. SET_BIT(heth->Instance->MACVTR, ETH_MACVTR_EDVLP);
  2024. }
  2025. }
  2026. /* if tcp segementation is enabled for this packet */
  2027. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_TSO))
  2028. {
  2029. /* Set MSS value */
  2030. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXCDESC_MSS, pTxConfig->MaxSegmentSize);
  2031. /* Set MSS valid bit */
  2032. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_TCMSSV);
  2033. }
  2034. if((READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_VLANTAG)) || (READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_TSO)))
  2035. {
  2036. /* Set as context descriptor */
  2037. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_CTXT);
  2038. /* Set own bit */
  2039. SET_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_OWN);
  2040. /* Increment current tx descriptor index */
  2041. INCR_TX_DESC_INDEX(descidx, 1);
  2042. /* Get current descriptor address */
  2043. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  2044. descnbr += 1U;
  2045. /* Current Tx Descriptor Owned by DMA: cannot be used by the application */
  2046. if(READ_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCWBF_OWN) == ETH_DMATXNDESCWBF_OWN)
  2047. {
  2048. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[firstdescidx];
  2049. /* Clear own bit */
  2050. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXCDESC_OWN);
  2051. return HAL_ETH_ERROR_BUSY;
  2052. }
  2053. }
  2054. /***************************************************************************/
  2055. /***************** Normal descriptors configuration *****************/
  2056. /***************************************************************************/
  2057. descnbr += 1U;
  2058. /* Set header or buffer 1 address */
  2059. WRITE_REG(dmatxdesc->DESC0, (uint32_t)txbuffer->buffer);
  2060. /* Set header or buffer 1 Length */
  2061. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B1L, txbuffer->len);
  2062. if(txbuffer->next != NULL)
  2063. {
  2064. txbuffer = txbuffer->next;
  2065. /* Set buffer 2 address */
  2066. WRITE_REG(dmatxdesc->DESC1, (uint32_t)txbuffer->buffer);
  2067. /* Set buffer 2 Length */
  2068. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B2L, (txbuffer->len << 16));
  2069. }
  2070. else
  2071. {
  2072. WRITE_REG(dmatxdesc->DESC1, 0x0);
  2073. /* Set buffer 2 Length */
  2074. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B2L, 0x0);
  2075. }
  2076. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_TSO))
  2077. {
  2078. /* Set TCP Header length */
  2079. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_THL, (pTxConfig->TCPHeaderLen << 19));
  2080. /* Set TCP payload length */
  2081. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_TPL, pTxConfig->PayloadLen);
  2082. /* Set TCP Segmentation Enabled bit */
  2083. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_TSE);
  2084. }
  2085. else
  2086. {
  2087. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_FL, pTxConfig->Length);
  2088. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_CSUM))
  2089. {
  2090. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CIC, pTxConfig->ChecksumCtrl);
  2091. }
  2092. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_CRCPAD))
  2093. {
  2094. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CPC, pTxConfig->CRCPadCtrl);
  2095. }
  2096. }
  2097. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_VLANTAG))
  2098. {
  2099. /* Set Vlan Tag control */
  2100. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_VTIR, pTxConfig->VlanCtrl);
  2101. }
  2102. /* Mark it as First Descriptor */
  2103. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_FD);
  2104. /* Mark it as NORMAL descriptor */
  2105. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CTXT);
  2106. /* set OWN bit of FIRST descriptor */
  2107. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_OWN);
  2108. /* If source address insertion/replacement is enabled for this packet */
  2109. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_SAIC))
  2110. {
  2111. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_SAIC, pTxConfig->SrcAddrCtrl);
  2112. }
  2113. /* only if the packet is splitted into more than one descriptors > 1 */
  2114. while (txbuffer->next != NULL)
  2115. {
  2116. /* Increment current tx descriptor index */
  2117. INCR_TX_DESC_INDEX(descidx, 1);
  2118. /* Get current descriptor address */
  2119. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  2120. /* Current Tx Descriptor Owned by DMA: cannot be used by the application */
  2121. if(READ_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_OWN) == ETH_DMATXNDESCRF_OWN)
  2122. {
  2123. descidx = firstdescidx;
  2124. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  2125. /* clear previous desc own bit */
  2126. for(idx = 0; idx < descnbr; idx ++)
  2127. {
  2128. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_OWN);
  2129. /* Increment current tx descriptor index */
  2130. INCR_TX_DESC_INDEX(descidx, 1);
  2131. /* Get current descriptor address */
  2132. dmatxdesc = (ETH_DMADescTypeDef *)dmatxdesclist->TxDesc[descidx];
  2133. }
  2134. return HAL_ETH_ERROR_BUSY;
  2135. }
  2136. descnbr += 1U;
  2137. /* Get the next Tx buffer in the list */
  2138. txbuffer = (ETH_BufferTypeDef *)txbuffer->next;
  2139. /* Set header or buffer 1 address */
  2140. WRITE_REG(dmatxdesc->DESC0, (uint32_t)txbuffer->buffer);
  2141. /* Set header or buffer 1 Length */
  2142. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B1L, txbuffer->len);
  2143. if (txbuffer->next != NULL)
  2144. {
  2145. /* Get the next Tx buffer in the list */
  2146. txbuffer = (ETH_BufferTypeDef *)txbuffer->next;
  2147. /* Set buffer 2 address */
  2148. WRITE_REG(dmatxdesc->DESC1, (uint32_t)txbuffer->buffer);
  2149. /* Set buffer 2 Length */
  2150. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B2L, (txbuffer->len << 16));
  2151. }
  2152. else
  2153. {
  2154. WRITE_REG(dmatxdesc->DESC1, 0x0);
  2155. /* Set buffer 2 Length */
  2156. MODIFY_REG(dmatxdesc->DESC2, ETH_DMATXNDESCRF_B2L, 0x0);
  2157. }
  2158. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_TSO))
  2159. {
  2160. /* Set TCP payload length */
  2161. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_TPL, pTxConfig->PayloadLen);
  2162. /* Set TCP Segmentation Enabled bit */
  2163. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_TSE);
  2164. }
  2165. else
  2166. {
  2167. /* Set the packet length */
  2168. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_FL, pTxConfig->Length);
  2169. if(READ_BIT(pTxConfig->Attributes, ETH_TX_PACKETS_FEATURES_CSUM))
  2170. {
  2171. /* Checksum Insertion Control */
  2172. MODIFY_REG(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CIC, pTxConfig->ChecksumCtrl);
  2173. }
  2174. }
  2175. /* Set Own bit */
  2176. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_OWN);
  2177. /* Mark it as NORMAL descriptor */
  2178. CLEAR_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_CTXT);
  2179. }
  2180. if(ItMode)
  2181. {
  2182. /* Set Interrupt on completition bit */
  2183. SET_BIT(dmatxdesc->DESC2, ETH_DMATXNDESCRF_IOC);
  2184. }
  2185. else
  2186. {
  2187. /* Clear Interrupt on completition bit */
  2188. CLEAR_BIT(dmatxdesc->DESC2, ETH_DMATXNDESCRF_IOC);
  2189. }
  2190. /* Mark it as LAST descriptor */
  2191. SET_BIT(dmatxdesc->DESC3, ETH_DMATXNDESCRF_LD);
  2192. dmatxdesclist->CurTxDesc = descidx;
  2193. /* Return function status */
  2194. return HAL_ETH_ERROR_NONE;
  2195. }
  2196. #endif /* HAL_ETH_MODULE_ENABLED */
  2197. /**
  2198. * @}
  2199. */
  2200. /**
  2201. * @}
  2202. */
  2203. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/