eth.c 71 KB

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