fsl_enet.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  1. /*
  2. * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_enet.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /*! @brief IPv4 PTP message IP version offset. */
  35. #define ENET_PTP1588_IPVERSION_OFFSET 0x0EU
  36. /*! @brief IPv4 PTP message UDP protocol offset. */
  37. #define ENET_PTP1588_IPV4_UDP_PROTOCOL_OFFSET 0x17U
  38. /*! @brief IPv4 PTP message UDP port offset. */
  39. #define ENET_PTP1588_IPV4_UDP_PORT_OFFSET 0x24U
  40. /*! @brief IPv4 PTP message UDP message type offset. */
  41. #define ENET_PTP1588_IPV4_UDP_MSGTYPE_OFFSET 0x2AU
  42. /*! @brief IPv4 PTP message UDP version offset. */
  43. #define ENET_PTP1588_IPV4_UDP_VERSION_OFFSET 0x2BU
  44. /*! @brief IPv4 PTP message UDP clock id offset. */
  45. #define ENET_PTP1588_IPV4_UDP_CLKID_OFFSET 0x3EU
  46. /*! @brief IPv4 PTP message UDP sequence id offset. */
  47. #define ENET_PTP1588_IPV4_UDP_SEQUENCEID_OFFSET 0x48U
  48. /*! @brief IPv4 PTP message UDP control offset. */
  49. #define ENET_PTP1588_IPV4_UDP_CTL_OFFSET 0x4AU
  50. /*! @brief IPv6 PTP message UDP protocol offset. */
  51. #define ENET_PTP1588_IPV6_UDP_PROTOCOL_OFFSET 0x14U
  52. /*! @brief IPv6 PTP message UDP port offset. */
  53. #define ENET_PTP1588_IPV6_UDP_PORT_OFFSET 0x38U
  54. /*! @brief IPv6 PTP message UDP message type offset. */
  55. #define ENET_PTP1588_IPV6_UDP_MSGTYPE_OFFSET 0x3EU
  56. /*! @brief IPv6 PTP message UDP version offset. */
  57. #define ENET_PTP1588_IPV6_UDP_VERSION_OFFSET 0x3FU
  58. /*! @brief IPv6 PTP message UDP clock id offset. */
  59. #define ENET_PTP1588_IPV6_UDP_CLKID_OFFSET 0x52U
  60. /*! @brief IPv6 PTP message UDP sequence id offset. */
  61. #define ENET_PTP1588_IPV6_UDP_SEQUENCEID_OFFSET 0x5CU
  62. /*! @brief IPv6 PTP message UDP control offset. */
  63. #define ENET_PTP1588_IPV6_UDP_CTL_OFFSET 0x5EU
  64. /*! @brief PTPv2 message Ethernet packet type offset. */
  65. #define ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET 0x0CU
  66. /*! @brief PTPv2 message Ethernet message type offset. */
  67. #define ENET_PTP1588_ETHL2_MSGTYPE_OFFSET 0x0EU
  68. /*! @brief PTPv2 message Ethernet version type offset. */
  69. #define ENET_PTP1588_ETHL2_VERSION_OFFSET 0X0FU
  70. /*! @brief PTPv2 message Ethernet clock id offset. */
  71. #define ENET_PTP1588_ETHL2_CLOCKID_OFFSET 0x22
  72. /*! @brief PTPv2 message Ethernet sequence id offset. */
  73. #define ENET_PTP1588_ETHL2_SEQUENCEID_OFFSET 0x2c
  74. /*! @brief Packet type Ethernet IEEE802.3 for PTPv2. */
  75. #define ENET_ETHERNETL2 0x88F7U
  76. /*! @brief Packet type IPv4. */
  77. #define ENET_IPV4 0x0800U
  78. /*! @brief Packet type IPv6. */
  79. #define ENET_IPV6 0x86ddU
  80. /*! @brief Packet type VLAN. */
  81. #define ENET_8021QVLAN 0x8100U
  82. /*! @brief UDP protocol type. */
  83. #define ENET_UDPVERSION 0x0011U
  84. /*! @brief Packet IP version IPv4. */
  85. #define ENET_IPV4VERSION 0x0004U
  86. /*! @brief Packet IP version IPv6. */
  87. #define ENET_IPV6VERSION 0x0006U
  88. /*! @brief Ethernet mac address length. */
  89. #define ENET_FRAME_MACLEN 6U
  90. /*! @brief Ethernet VLAN header length. */
  91. #define ENET_FRAME_VLAN_TAGLEN 4U
  92. /*! @brief MDC frequency. */
  93. #define ENET_MDC_FREQUENCY 2500000U
  94. /*! @brief NanoSecond in one second. */
  95. #define ENET_NANOSECOND_ONE_SECOND 1000000000U
  96. /*! @brief Define a common clock cycle delays used for time stamp capture. */
  97. #define ENET_1588TIME_DELAY_COUNT 10U
  98. /*! @brief Defines the macro for converting constants from host byte order to network byte order. */
  99. #define ENET_HTONS(n) __REV16(n)
  100. #define ENET_HTONL(n) __REV(n)
  101. #define ENET_NTOHS(n) __REV16(n)
  102. #define ENET_NTOHL(n) __REV(n)
  103. /* Typedef for interrupt handler. */
  104. typedef void (*enet_isr_t)(ENET_Type *base, enet_handle_t *handle);
  105. /*******************************************************************************
  106. * Prototypes
  107. ******************************************************************************/
  108. /*!
  109. * @brief Get the ENET instance from peripheral base address.
  110. *
  111. * @param base ENET peripheral base address.
  112. * @return ENET instance.
  113. */
  114. uint32_t ENET_GetInstance(ENET_Type *base);
  115. /*!
  116. * @brief Set ENET MAC controller with the configuration.
  117. *
  118. * @param base ENET peripheral base address.
  119. * @param config ENET Mac configuration.
  120. * @param bufferConfig ENET buffer configuration.
  121. * @param macAddr ENET six-byte mac address.
  122. * @param srcClock_Hz ENET module clock source, normally it's system clock.
  123. */
  124. static void ENET_SetMacController(ENET_Type *base,
  125. const enet_config_t *config,
  126. const enet_buffer_config_t *bufferConfig,
  127. uint8_t *macAddr,
  128. uint32_t srcClock_Hz);
  129. /*!
  130. * @brief Set ENET handler.
  131. *
  132. * @param base ENET peripheral base address.
  133. * @param handle The ENET handle pointer.
  134. * @param config ENET configuration stucture pointer.
  135. * @param bufferConfig ENET buffer configuration.
  136. */
  137. static void ENET_SetHandler(ENET_Type *base,
  138. enet_handle_t *handle,
  139. const enet_config_t *config,
  140. const enet_buffer_config_t *bufferConfig);
  141. /*!
  142. * @brief Set ENET MAC transmit buffer descriptors.
  143. *
  144. * @param txBdStartAlign The aligned start address of ENET transmit buffer descriptors.
  145. * is recommended to evenly divisible by 16.
  146. * @param txBuffStartAlign The aligned start address of ENET transmit buffers, must be evenly divisible by 16.
  147. * @param txBuffSizeAlign The aligned ENET transmit buffer size, must be evenly divisible by 16.
  148. * @param txBdNumber The number of ENET transmit buffers.
  149. */
  150. static void ENET_SetTxBufferDescriptors(volatile enet_tx_bd_struct_t *txBdStartAlign,
  151. uint8_t *txBuffStartAlign,
  152. uint32_t txBuffSizeAlign,
  153. uint32_t txBdNumber);
  154. /*!
  155. * @brief Set ENET MAC receive buffer descriptors.
  156. *
  157. * @param rxBdStartAlign The aligned start address of ENET receive buffer descriptors.
  158. * is recommended to evenly divisible by 16.
  159. * @param rxBuffStartAlign The aligned start address of ENET receive buffers, must be evenly divisible by 16.
  160. * @param rxBuffSizeAlign The aligned ENET receive buffer size, must be evenly divisible by 16.
  161. * @param rxBdNumber The number of ENET receive buffers.
  162. * @param enableInterrupt Enable/disables to generate the receive byte and frame interrupt.
  163. * It's used for ENET_ENHANCEDBUFFERDESCRIPTOR_MODE enabled case.
  164. */
  165. static void ENET_SetRxBufferDescriptors(volatile enet_rx_bd_struct_t *rxBdStartAlign,
  166. uint8_t *rxBuffStartAlign,
  167. uint32_t rxBuffSizeAlign,
  168. uint32_t rxBdNumber,
  169. bool enableInterrupt);
  170. /*!
  171. * @brief Updates the ENET read buffer descriptors.
  172. *
  173. * @param base ENET peripheral base address.
  174. * @param handle The ENET handle pointer.
  175. */
  176. static void ENET_UpdateReadBuffers(ENET_Type *base, enet_handle_t *handle);
  177. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  178. /*!
  179. * @brief Parses the ENET frame for time-stamp process of PTP 1588 frame.
  180. *
  181. * @param data The ENET read data for frame parse.
  182. * @param ptpTsData The ENET PTP message and time-stamp data pointer.
  183. * @param isFastEnabled The fast parse flag.
  184. * - true , Fast processing, only check if this is a PTP message.
  185. * - false, Store the PTP message data after check the PTP message.
  186. */
  187. static bool ENET_Ptp1588ParseFrame(uint8_t *data, enet_ptp_time_data_t *ptpTsData, bool isFastEnabled);
  188. /*!
  189. * @brief Updates the new PTP 1588 time-stamp to the time-stamp buffer ring.
  190. *
  191. * @param ptpTsDataRing The PTP message and time-stamp data ring pointer.
  192. * @param ptpTimeData The new PTP 1588 time-stamp data pointer.
  193. */
  194. static status_t ENET_Ptp1588UpdateTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimeData);
  195. /*!
  196. * @brief Search up the right PTP 1588 time-stamp from the time-stamp buffer ring.
  197. *
  198. * @param ptpTsDataRing The PTP message and time-stamp data ring pointer.
  199. * @param ptpTimeData The find out right PTP 1588 time-stamp data pointer with the specific PTP message.
  200. */
  201. static status_t ENET_Ptp1588SearchTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimedata);
  202. /*!
  203. * @brief Store the transmit time-stamp for event PTP frame in the time-stamp buffer ring.
  204. *
  205. * @param base ENET peripheral base address.
  206. * @param handle The ENET handle pointer.
  207. */
  208. static status_t ENET_StoreTxFrameTime(ENET_Type *base, enet_handle_t *handle);
  209. /*!
  210. * @brief Store the receive time-stamp for event PTP frame in the time-stamp buffer ring.
  211. *
  212. * @param base ENET peripheral base address.
  213. * @param handle The ENET handle pointer.
  214. * @param ptpTimeData The PTP 1588 time-stamp data pointer.
  215. */
  216. static status_t ENET_StoreRxFrameTime(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData);
  217. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  218. /*******************************************************************************
  219. * Variables
  220. ******************************************************************************/
  221. /*! @brief Pointers to enet handles for each instance. */
  222. static enet_handle_t *s_ENETHandle[FSL_FEATURE_SOC_ENET_COUNT] = {NULL};
  223. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  224. /*! @brief Pointers to enet clocks for each instance. */
  225. const clock_ip_name_t s_enetClock[] = ENET_CLOCKS;
  226. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  227. /*! @brief Pointers to enet transmit IRQ number for each instance. */
  228. static const IRQn_Type s_enetTxIrqId[] = ENET_Transmit_IRQS;
  229. /*! @brief Pointers to enet receive IRQ number for each instance. */
  230. static const IRQn_Type s_enetRxIrqId[] = ENET_Receive_IRQS;
  231. #if defined(ENET_ENHANCEDBUFFERDESCRIPTOR_MODE) && ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  232. /*! @brief Pointers to enet timestamp IRQ number for each instance. */
  233. static const IRQn_Type s_enetTsIrqId[] = ENET_1588_Timer_IRQS;
  234. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  235. /*! @brief Pointers to enet error IRQ number for each instance. */
  236. static const IRQn_Type s_enetErrIrqId[] = ENET_Error_IRQS;
  237. /*! @brief Pointers to enet bases for each instance. */
  238. static ENET_Type *const s_enetBases[] = ENET_BASE_PTRS;
  239. /* ENET ISR for transactional APIs. */
  240. static enet_isr_t s_enetTxIsr;
  241. static enet_isr_t s_enetRxIsr;
  242. static enet_isr_t s_enetErrIsr;
  243. static enet_isr_t s_enetTsIsr;
  244. /*******************************************************************************
  245. * Code
  246. ******************************************************************************/
  247. uint32_t ENET_GetInstance(ENET_Type *base)
  248. {
  249. uint32_t instance;
  250. /* Find the instance index from base address mappings. */
  251. for (instance = 0; instance < ARRAY_SIZE(s_enetBases); instance++)
  252. {
  253. if (s_enetBases[instance] == base)
  254. {
  255. break;
  256. }
  257. }
  258. assert(instance < ARRAY_SIZE(s_enetBases));
  259. return instance;
  260. }
  261. void ENET_GetDefaultConfig(enet_config_t *config)
  262. {
  263. /* Checks input parameter. */
  264. assert(config);
  265. /* Initializes the MAC configure structure to zero. */
  266. memset(config, 0, sizeof(enet_config_t));
  267. /* Sets MII mode, full duplex, 100Mbps for MAC and PHY data interface. */
  268. config->miiMode = kENET_RmiiMode;
  269. config->miiSpeed = kENET_MiiSpeed100M;
  270. config->miiDuplex = kENET_MiiFullDuplex;
  271. /* Sets the maximum receive frame length. */
  272. config->rxMaxFrameLen = ENET_FRAME_MAX_FRAMELEN;
  273. }
  274. void ENET_Init(ENET_Type *base,
  275. enet_handle_t *handle,
  276. const enet_config_t *config,
  277. const enet_buffer_config_t *bufferConfig,
  278. uint8_t *macAddr,
  279. uint32_t srcClock_Hz)
  280. {
  281. /* Checks input parameters. */
  282. assert(handle);
  283. assert(config);
  284. assert(bufferConfig);
  285. assert(bufferConfig->rxBdStartAddrAlign);
  286. assert(bufferConfig->txBdStartAddrAlign);
  287. assert(bufferConfig->rxBufferAlign);
  288. assert(bufferConfig->txBufferAlign);
  289. assert(macAddr);
  290. assert(bufferConfig->rxBuffSizeAlign >= ENET_RX_MIN_BUFFERSIZE);
  291. /* Make sure the buffers should be have the capability of process at least one maximum frame. */
  292. if (config->macSpecialConfig & kENET_ControlVLANTagEnable)
  293. {
  294. assert(bufferConfig->txBuffSizeAlign * bufferConfig->txBdNumber > (ENET_FRAME_MAX_FRAMELEN + ENET_FRAME_VLAN_TAGLEN));
  295. }
  296. else
  297. {
  298. assert(bufferConfig->txBuffSizeAlign * bufferConfig->txBdNumber > ENET_FRAME_MAX_FRAMELEN);
  299. assert(bufferConfig->rxBuffSizeAlign * bufferConfig->rxBdNumber > config->rxMaxFrameLen);
  300. }
  301. uint32_t instance = ENET_GetInstance(base);
  302. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  303. /* Ungate ENET clock. */
  304. CLOCK_EnableClock(s_enetClock[instance]);
  305. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  306. /* Reset ENET module. */
  307. ENET_Reset(base);
  308. /* Initializes the ENET transmit buffer descriptors. */
  309. ENET_SetTxBufferDescriptors(bufferConfig->txBdStartAddrAlign, bufferConfig->txBufferAlign,
  310. bufferConfig->txBuffSizeAlign, bufferConfig->txBdNumber);
  311. /* Initializes the ENET receive buffer descriptors. */
  312. ENET_SetRxBufferDescriptors(bufferConfig->rxBdStartAddrAlign, bufferConfig->rxBufferAlign,
  313. bufferConfig->rxBuffSizeAlign, bufferConfig->rxBdNumber,
  314. !!(config->interrupt & (kENET_RxFrameInterrupt | kENET_RxBufferInterrupt)));
  315. /* Initializes the ENET MAC controller. */
  316. ENET_SetMacController(base, config, bufferConfig, macAddr, srcClock_Hz);
  317. /* Set all buffers or data in handler for data transmit/receive process. */
  318. ENET_SetHandler(base, handle, config, bufferConfig);
  319. }
  320. void ENET_Deinit(ENET_Type *base)
  321. {
  322. /* Disable interrupt. */
  323. base->EIMR = 0;
  324. /* Disable ENET. */
  325. base->ECR &= ~ENET_ECR_ETHEREN_MASK;
  326. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  327. /* Disables the clock source. */
  328. CLOCK_DisableClock(s_enetClock[ENET_GetInstance(base)]);
  329. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  330. }
  331. void ENET_SetCallback(enet_handle_t *handle, enet_callback_t callback, void *userData)
  332. {
  333. assert(handle);
  334. /* Set callback and userData. */
  335. handle->callback = callback;
  336. handle->userData = userData;
  337. }
  338. static void ENET_SetHandler(ENET_Type *base,
  339. enet_handle_t *handle,
  340. const enet_config_t *config,
  341. const enet_buffer_config_t *bufferConfig)
  342. {
  343. uint32_t instance = ENET_GetInstance(base);
  344. memset(handle, 0, sizeof(enet_handle_t));
  345. handle->rxBdBase = bufferConfig->rxBdStartAddrAlign;
  346. handle->rxBdCurrent = bufferConfig->rxBdStartAddrAlign;
  347. handle->txBdBase = bufferConfig->txBdStartAddrAlign;
  348. handle->txBdCurrent = bufferConfig->txBdStartAddrAlign;
  349. handle->rxBuffSizeAlign = bufferConfig->rxBuffSizeAlign;
  350. handle->txBuffSizeAlign = bufferConfig->txBuffSizeAlign;
  351. /* Save the handle pointer in the global variables. */
  352. s_ENETHandle[instance] = handle;
  353. /* Set the IRQ handler when the interrupt is enabled. */
  354. if (config->interrupt & ENET_TX_INTERRUPT)
  355. {
  356. s_enetTxIsr = ENET_TransmitIRQHandler;
  357. EnableIRQ(s_enetTxIrqId[instance]);
  358. }
  359. if (config->interrupt & ENET_RX_INTERRUPT)
  360. {
  361. s_enetRxIsr = ENET_ReceiveIRQHandler;
  362. EnableIRQ(s_enetRxIrqId[instance]);
  363. }
  364. if (config->interrupt & ENET_ERR_INTERRUPT)
  365. {
  366. s_enetErrIsr = ENET_ErrorIRQHandler;
  367. EnableIRQ(s_enetErrIrqId[instance]);
  368. }
  369. }
  370. static void ENET_SetMacController(ENET_Type *base,
  371. const enet_config_t *config,
  372. const enet_buffer_config_t *bufferConfig,
  373. uint8_t *macAddr,
  374. uint32_t srcClock_Hz)
  375. {
  376. uint32_t rcr = 0;
  377. uint32_t tcr = 0;
  378. uint32_t ecr = 0;
  379. uint32_t macSpecialConfig = config->macSpecialConfig;
  380. uint32_t maxFrameLen = config->rxMaxFrameLen;
  381. /* Maximum frame length check. */
  382. if ((macSpecialConfig & kENET_ControlVLANTagEnable) && (maxFrameLen <= ENET_FRAME_MAX_FRAMELEN))
  383. {
  384. maxFrameLen = (ENET_FRAME_MAX_FRAMELEN + ENET_FRAME_VLAN_TAGLEN);
  385. }
  386. /* Configures MAC receive controller with user configure structure. */
  387. rcr = ENET_RCR_NLC(!!(macSpecialConfig & kENET_ControlRxPayloadCheckEnable)) |
  388. ENET_RCR_CFEN(!!(macSpecialConfig & kENET_ControlFlowControlEnable)) |
  389. ENET_RCR_FCE(!!(macSpecialConfig & kENET_ControlFlowControlEnable)) |
  390. ENET_RCR_PADEN(!!(macSpecialConfig & kENET_ControlRxPadRemoveEnable)) |
  391. ENET_RCR_BC_REJ(!!(macSpecialConfig & kENET_ControlRxBroadCastRejectEnable)) |
  392. ENET_RCR_PROM(!!(macSpecialConfig & kENET_ControlPromiscuousEnable)) | ENET_RCR_MII_MODE(1) |
  393. ENET_RCR_RMII_MODE(config->miiMode) | ENET_RCR_RMII_10T(!config->miiSpeed) |
  394. ENET_RCR_MAX_FL(maxFrameLen) | ENET_RCR_CRCFWD(1);
  395. /* Receive setting for half duplex. */
  396. if (config->miiDuplex == kENET_MiiHalfDuplex)
  397. {
  398. rcr |= ENET_RCR_DRT_MASK;
  399. }
  400. /* Sets internal loop only for MII mode. */
  401. if ((config->macSpecialConfig & kENET_ControlMIILoopEnable) && (config->miiMode == kENET_MiiMode))
  402. {
  403. rcr |= ENET_RCR_LOOP_MASK;
  404. rcr &= ~ENET_RCR_DRT_MASK;
  405. }
  406. base->RCR = rcr;
  407. /* Configures MAC transmit controller: duplex mode, mac address insertion. */
  408. tcr = base->TCR & ~(ENET_TCR_FDEN_MASK | ENET_TCR_ADDINS_MASK);
  409. tcr |= ENET_TCR_FDEN(config->miiDuplex) | ENET_TCR_ADDINS(!!(macSpecialConfig & kENET_ControlMacAddrInsert));
  410. base->TCR = tcr;
  411. /* Configures receive and transmit accelerator. */
  412. base->TACC = config->txAccelerConfig;
  413. base->RACC = config->rxAccelerConfig;
  414. /* Sets the pause duration and FIFO threshold for the flow control enabled case. */
  415. if (macSpecialConfig & kENET_ControlFlowControlEnable)
  416. {
  417. uint32_t reemReg;
  418. base->OPD = config->pauseDuration;
  419. reemReg = ENET_RSEM_RX_SECTION_EMPTY(config->rxFifoEmptyThreshold);
  420. #if defined (FSL_FEATURE_ENET_HAS_RECEIVE_STATUS_THRESHOLD) && FSL_FEATURE_ENET_HAS_RECEIVE_STATUS_THRESHOLD
  421. reemReg |= ENET_RSEM_STAT_SECTION_EMPTY(config->rxFifoStatEmptyThreshold);
  422. #endif /* FSL_FEATURE_ENET_HAS_RECEIVE_STATUS_THRESHOLD */
  423. base->RSEM = reemReg;
  424. }
  425. /* FIFO threshold setting for store and forward enable/disable case. */
  426. if (macSpecialConfig & kENET_ControlStoreAndFwdDisable)
  427. {
  428. /* Transmit fifo watermark settings. */
  429. base->TFWR = config->txFifoWatermark & ENET_TFWR_TFWR_MASK;
  430. /* Receive fifo full threshold settings. */
  431. base->RSFL = config->rxFifoFullThreshold & ENET_RSFL_RX_SECTION_FULL_MASK;
  432. }
  433. else
  434. {
  435. /* Transmit fifo watermark settings. */
  436. base->TFWR = ENET_TFWR_STRFWD_MASK;
  437. base->RSFL = 0;
  438. }
  439. /* Enable store and forward when accelerator is enabled */
  440. if (config->txAccelerConfig & (kENET_TxAccelIpCheckEnabled | kENET_TxAccelProtoCheckEnabled))
  441. {
  442. base->TFWR = ENET_TFWR_STRFWD_MASK;
  443. }
  444. if (config->rxAccelerConfig & (kENET_RxAccelIpCheckEnabled | kENET_RxAccelProtoCheckEnabled))
  445. {
  446. base->RSFL = 0;
  447. }
  448. /* Initializes transmit buffer descriptor rings start address, two start address should be aligned. */
  449. base->TDSR = (uint32_t)bufferConfig->txBdStartAddrAlign;
  450. base->RDSR = (uint32_t)bufferConfig->rxBdStartAddrAlign;
  451. /* Initializes the maximum buffer size, the buffer size should be aligned. */
  452. base->MRBR = bufferConfig->rxBuffSizeAlign;
  453. /* Configures the Mac address. */
  454. ENET_SetMacAddr(base, macAddr);
  455. /* Initialize the SMI if uninitialized. */
  456. if (!ENET_GetSMI(base))
  457. {
  458. ENET_SetSMI(base, srcClock_Hz, !!(config->macSpecialConfig & kENET_ControlSMIPreambleDisable));
  459. }
  460. /* Enables Ethernet interrupt and NVIC. */
  461. #if defined(FSL_FEATURE_ENET_HAS_INTERRUPT_COALESCE) && FSL_FEATURE_ENET_HAS_INTERRUPT_COALESCE
  462. if (config->intCoalesceCfg)
  463. {
  464. uint32_t intMask = (ENET_EIMR_TXB_MASK | ENET_EIMR_RXB_MASK);
  465. /* Clear all buffer interrupts. */
  466. base->EIMR &= ~intMask;
  467. /* Set the interrupt coalescence. */
  468. base->TXIC = ENET_TXIC_ICFT(config->intCoalesceCfg->txCoalesceFrameCount[0]) |
  469. config->intCoalesceCfg->txCoalesceTimeCount[0] | ENET_TXIC_ICCS_MASK | ENET_TXIC_ICEN_MASK;
  470. base->RXIC = ENET_RXIC_ICFT(config->intCoalesceCfg->rxCoalesceFrameCount[0]) |
  471. config->intCoalesceCfg->rxCoalesceTimeCount[0] | ENET_RXIC_ICCS_MASK | ENET_RXIC_ICEN_MASK;
  472. }
  473. #endif /* FSL_FEATURE_ENET_HAS_INTERRUPT_COALESCE */
  474. ENET_EnableInterrupts(base, config->interrupt);
  475. /* ENET control register setting. */
  476. ecr = base->ECR;
  477. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  478. /* Sets the 1588 enhanced feature. */
  479. ecr |= ENET_ECR_EN1588_MASK;
  480. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  481. /* Enables Ethernet module after all configuration except the buffer descriptor active. */
  482. ecr |= ENET_ECR_ETHEREN_MASK | ENET_ECR_DBSWP_MASK;
  483. base->ECR = ecr;
  484. }
  485. static void ENET_SetTxBufferDescriptors(volatile enet_tx_bd_struct_t *txBdStartAlign,
  486. uint8_t *txBuffStartAlign,
  487. uint32_t txBuffSizeAlign,
  488. uint32_t txBdNumber)
  489. {
  490. assert(txBdStartAlign);
  491. assert(txBuffStartAlign);
  492. uint32_t count;
  493. volatile enet_tx_bd_struct_t *curBuffDescrip = txBdStartAlign;
  494. for (count = 0; count < txBdNumber; count++)
  495. {
  496. /* Set data buffer address. */
  497. curBuffDescrip->buffer = (uint8_t *)((uint32_t)&txBuffStartAlign[count * txBuffSizeAlign]);
  498. /* Initializes data length. */
  499. curBuffDescrip->length = 0;
  500. /* Sets the crc. */
  501. curBuffDescrip->control = ENET_BUFFDESCRIPTOR_TX_TRANMITCRC_MASK;
  502. /* Sets the last buffer descriptor with the wrap flag. */
  503. if (count == txBdNumber - 1)
  504. {
  505. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_WRAP_MASK;
  506. }
  507. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  508. /* Enable transmit interrupt for store the transmit timestamp. */
  509. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_INTERRUPT_MASK;
  510. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  511. /* Increase the index. */
  512. curBuffDescrip++;
  513. }
  514. }
  515. static void ENET_SetRxBufferDescriptors(volatile enet_rx_bd_struct_t *rxBdStartAlign,
  516. uint8_t *rxBuffStartAlign,
  517. uint32_t rxBuffSizeAlign,
  518. uint32_t rxBdNumber,
  519. bool enableInterrupt)
  520. {
  521. assert(rxBdStartAlign);
  522. assert(rxBuffStartAlign);
  523. volatile enet_rx_bd_struct_t *curBuffDescrip = rxBdStartAlign;
  524. uint32_t count = 0;
  525. /* Initializes receive buffer descriptors. */
  526. for (count = 0; count < rxBdNumber; count++)
  527. {
  528. /* Set data buffer and the length. */
  529. curBuffDescrip->buffer = (uint8_t *)((uint32_t)&rxBuffStartAlign[count * rxBuffSizeAlign]);
  530. curBuffDescrip->length = 0;
  531. /* Initializes the buffer descriptors with empty bit. */
  532. curBuffDescrip->control = ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK;
  533. /* Sets the last buffer descriptor with the wrap flag. */
  534. if (count == rxBdNumber - 1)
  535. {
  536. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_RX_WRAP_MASK;
  537. }
  538. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  539. if (enableInterrupt)
  540. {
  541. /* Enable receive interrupt. */
  542. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_RX_INTERRUPT_MASK;
  543. }
  544. else
  545. {
  546. curBuffDescrip->controlExtend1 = 0;
  547. }
  548. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  549. /* Increase the index. */
  550. curBuffDescrip++;
  551. }
  552. }
  553. void ENET_SetMII(ENET_Type *base, enet_mii_speed_t speed, enet_mii_duplex_t duplex)
  554. {
  555. uint32_t rcr = base->RCR;
  556. uint32_t tcr = base->TCR;
  557. /* Sets speed mode. */
  558. if (kENET_MiiSpeed10M == speed)
  559. {
  560. rcr |= ENET_RCR_RMII_10T_MASK;
  561. }
  562. else
  563. {
  564. rcr &= ~ENET_RCR_RMII_10T_MASK;
  565. }
  566. /* Set duplex mode. */
  567. if (duplex == kENET_MiiHalfDuplex)
  568. {
  569. rcr |= ENET_RCR_DRT_MASK;
  570. tcr &= ~ENET_TCR_FDEN_MASK;
  571. }
  572. else
  573. {
  574. rcr &= ~ENET_RCR_DRT_MASK;
  575. tcr |= ENET_TCR_FDEN_MASK;
  576. }
  577. base->RCR = rcr;
  578. base->TCR = tcr;
  579. }
  580. void ENET_SetMacAddr(ENET_Type *base, uint8_t *macAddr)
  581. {
  582. uint32_t address;
  583. /* Set physical address lower register. */
  584. address = (uint32_t)(((uint32_t)macAddr[0] << 24U) | ((uint32_t)macAddr[1] << 16U) | ((uint32_t)macAddr[2] << 8U) |
  585. (uint32_t)macAddr[3]);
  586. base->PALR = address;
  587. /* Set physical address high register. */
  588. address = (uint32_t)(((uint32_t)macAddr[4] << 8U) | ((uint32_t)macAddr[5]));
  589. base->PAUR = address << ENET_PAUR_PADDR2_SHIFT;
  590. }
  591. void ENET_GetMacAddr(ENET_Type *base, uint8_t *macAddr)
  592. {
  593. assert(macAddr);
  594. uint32_t address;
  595. /* Get from physical address lower register. */
  596. address = base->PALR;
  597. macAddr[0] = 0xFFU & (address >> 24U);
  598. macAddr[1] = 0xFFU & (address >> 16U);
  599. macAddr[2] = 0xFFU & (address >> 8U);
  600. macAddr[3] = 0xFFU & address;
  601. /* Get from physical address high register. */
  602. address = (base->PAUR & ENET_PAUR_PADDR2_MASK) >> ENET_PAUR_PADDR2_SHIFT;
  603. macAddr[4] = 0xFFU & (address >> 8U);
  604. macAddr[5] = 0xFFU & address;
  605. }
  606. void ENET_SetSMI(ENET_Type *base, uint32_t srcClock_Hz, bool isPreambleDisabled)
  607. {
  608. assert(srcClock_Hz);
  609. uint32_t clkCycle = 0;
  610. uint32_t speed = 0;
  611. uint32_t mscr = 0;
  612. /* Calculate the MII speed which controls the frequency of the MDC. */
  613. speed = srcClock_Hz / (2 * ENET_MDC_FREQUENCY);
  614. /* Calculate the hold time on the MDIO output. */
  615. clkCycle = (10 + ENET_NANOSECOND_ONE_SECOND / srcClock_Hz - 1) / (ENET_NANOSECOND_ONE_SECOND / srcClock_Hz) - 1;
  616. /* Build the configuration for MDC/MDIO control. */
  617. mscr = ENET_MSCR_MII_SPEED(speed) | ENET_MSCR_DIS_PRE(isPreambleDisabled) | ENET_MSCR_HOLDTIME(clkCycle);
  618. base->MSCR = mscr;
  619. }
  620. void ENET_StartSMIWrite(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, enet_mii_write_t operation, uint32_t data)
  621. {
  622. uint32_t mmfr = 0;
  623. /* Build MII write command. */
  624. mmfr = ENET_MMFR_ST(1) | ENET_MMFR_OP(operation) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(phyReg) | ENET_MMFR_TA(2) |
  625. (data & 0xFFFF);
  626. base->MMFR = mmfr;
  627. }
  628. void ENET_StartSMIRead(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, enet_mii_read_t operation)
  629. {
  630. uint32_t mmfr = 0;
  631. /* Build MII read command. */
  632. mmfr = ENET_MMFR_ST(1) | ENET_MMFR_OP(operation) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(phyReg) | ENET_MMFR_TA(2);
  633. base->MMFR = mmfr;
  634. }
  635. #if defined(FSL_FEATURE_ENET_HAS_EXTEND_MDIO) && FSL_FEATURE_ENET_HAS_EXTEND_MDIO
  636. void ENET_StartExtC45SMIWrite(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t data)
  637. {
  638. uint32_t mmfr = 0;
  639. /* Parse the address from the input register. */
  640. uint16_t devAddr = (phyReg >> ENET_MMFR_TA_SHIFT) & 0x1FU;
  641. uint16_t regAddr = (uint16_t)(phyReg & 0xFFFFU);
  642. /* Address write firstly. */
  643. mmfr = ENET_MMFR_ST(0) | ENET_MMFR_OP(kENET_MiiAddrWrite_C45) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(devAddr) |
  644. ENET_MMFR_TA(2) | ENET_MMFR_DATA(regAddr);
  645. base->MMFR = mmfr;
  646. /* Build MII write command. */
  647. mmfr = ENET_MMFR_ST(0) | ENET_MMFR_OP(kENET_MiiWriteFrame_C45) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(devAddr) |
  648. ENET_MMFR_TA(2) | ENET_MMFR_DATA(data);
  649. base->MMFR = mmfr;
  650. }
  651. void ENET_StartExtC45SMIRead(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg)
  652. {
  653. uint32_t mmfr = 0;
  654. /* Parse the address from the input register. */
  655. uint16_t devAddr = (phyReg >> ENET_MMFR_TA_SHIFT) & 0x1FU;
  656. uint16_t regAddr = (uint16_t)(phyReg & 0xFFFFU);
  657. /* Address write firstly. */
  658. mmfr = ENET_MMFR_ST(0) | ENET_MMFR_OP(kENET_MiiAddrWrite_C45) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(devAddr) |
  659. ENET_MMFR_TA(2) | ENET_MMFR_DATA(regAddr);
  660. base->MMFR = mmfr;
  661. /* Build MII read command. */
  662. mmfr = ENET_MMFR_ST(0) | ENET_MMFR_OP(kENET_MiiReadFrame_C45) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(devAddr) |
  663. ENET_MMFR_TA(2);
  664. base->MMFR = mmfr;
  665. }
  666. #endif /* FSL_FEATURE_ENET_HAS_EXTEND_MDIO */
  667. void ENET_GetRxErrBeforeReadFrame(enet_handle_t *handle, enet_data_error_stats_t *eErrorStatic)
  668. {
  669. assert(handle);
  670. assert(handle->rxBdCurrent);
  671. assert(eErrorStatic);
  672. uint16_t control = 0;
  673. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent;
  674. do
  675. {
  676. /* The last buffer descriptor of a frame. */
  677. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  678. {
  679. control = curBuffDescrip->control;
  680. if (control & ENET_BUFFDESCRIPTOR_RX_TRUNC_MASK)
  681. {
  682. /* The receive truncate error. */
  683. eErrorStatic->statsRxTruncateErr++;
  684. }
  685. if (control & ENET_BUFFDESCRIPTOR_RX_OVERRUN_MASK)
  686. {
  687. /* The receive over run error. */
  688. eErrorStatic->statsRxOverRunErr++;
  689. }
  690. if (control & ENET_BUFFDESCRIPTOR_RX_LENVLIOLATE_MASK)
  691. {
  692. /* The receive length violation error. */
  693. eErrorStatic->statsRxLenGreaterErr++;
  694. }
  695. if (control & ENET_BUFFDESCRIPTOR_RX_NOOCTET_MASK)
  696. {
  697. /* The receive alignment error. */
  698. eErrorStatic->statsRxAlignErr++;
  699. }
  700. if (control & ENET_BUFFDESCRIPTOR_RX_CRC_MASK)
  701. {
  702. /* The receive CRC error. */
  703. eErrorStatic->statsRxFcsErr++;
  704. }
  705. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  706. uint16_t controlExt = curBuffDescrip->controlExtend1;
  707. if (controlExt & ENET_BUFFDESCRIPTOR_RX_MACERR_MASK)
  708. {
  709. /* The MAC error. */
  710. eErrorStatic->statsRxMacErr++;
  711. }
  712. if (controlExt & ENET_BUFFDESCRIPTOR_RX_PHYERR_MASK)
  713. {
  714. /* The PHY error. */
  715. eErrorStatic->statsRxPhyErr++;
  716. }
  717. if (controlExt & ENET_BUFFDESCRIPTOR_RX_COLLISION_MASK)
  718. {
  719. /* The receive collision error. */
  720. eErrorStatic->statsRxCollisionErr++;
  721. }
  722. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  723. break;
  724. }
  725. /* Increase the buffer descriptor, if it is the last one, increase to first one of the ring buffer. */
  726. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  727. {
  728. curBuffDescrip = handle->rxBdBase;
  729. }
  730. else
  731. {
  732. curBuffDescrip++;
  733. }
  734. } while (curBuffDescrip != handle->rxBdCurrent);
  735. }
  736. status_t ENET_GetRxFrameSize(enet_handle_t *handle, uint32_t *length)
  737. {
  738. assert(handle);
  739. assert(handle->rxBdCurrent);
  740. assert(length);
  741. /* Reset the length to zero. */
  742. *length = 0;
  743. uint16_t validLastMask = ENET_BUFFDESCRIPTOR_RX_LAST_MASK | ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK;
  744. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent;
  745. /* Check the current buffer descriptor's empty flag. if empty means there is no frame received. */
  746. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK)
  747. {
  748. return kStatus_ENET_RxFrameEmpty;
  749. }
  750. do
  751. {
  752. /* Find the last buffer descriptor. */
  753. if ((curBuffDescrip->control & validLastMask) == ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  754. {
  755. /* The last buffer descriptor in the frame check the status of the received frame. */
  756. if ((curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_ERR_MASK)
  757. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  758. || (curBuffDescrip->controlExtend1 & ENET_BUFFDESCRIPTOR_RX_EXT_ERR_MASK)
  759. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  760. )
  761. {
  762. return kStatus_ENET_RxFrameError;
  763. }
  764. /* FCS is removed by MAC. */
  765. *length = curBuffDescrip->length;
  766. return kStatus_Success;
  767. }
  768. /* Increase the buffer descriptor, if it is the last one, increase to first one of the ring buffer. */
  769. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  770. {
  771. curBuffDescrip = handle->rxBdBase;
  772. }
  773. else
  774. {
  775. curBuffDescrip++;
  776. }
  777. } while (curBuffDescrip != handle->rxBdCurrent);
  778. /* The frame is on processing - set to empty status to make application to receive it next time. */
  779. return kStatus_ENET_RxFrameEmpty;
  780. }
  781. status_t ENET_ReadFrame(ENET_Type *base, enet_handle_t *handle, uint8_t *data, uint32_t length)
  782. {
  783. assert(handle);
  784. assert(handle->rxBdCurrent);
  785. uint32_t len = 0;
  786. uint32_t offset = 0;
  787. uint16_t control;
  788. bool isLastBuff = false;
  789. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent;
  790. status_t result = kStatus_Success;
  791. /* For data-NULL input, only update the buffer descriptor. */
  792. if (!data)
  793. {
  794. do
  795. {
  796. /* Update the control flag. */
  797. control = handle->rxBdCurrent->control;
  798. /* Updates the receive buffer descriptors. */
  799. ENET_UpdateReadBuffers(base, handle);
  800. /* Find the last buffer descriptor for the frame. */
  801. if (control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  802. {
  803. break;
  804. }
  805. } while (handle->rxBdCurrent != curBuffDescrip);
  806. return result;
  807. }
  808. else
  809. {
  810. /* A frame on one buffer or several receive buffers are both considered. */
  811. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  812. enet_ptp_time_data_t ptpTimestamp;
  813. bool isPtpEventMessage = false;
  814. /* Parse the PTP message according to the header message. */
  815. isPtpEventMessage = ENET_Ptp1588ParseFrame(curBuffDescrip->buffer, &ptpTimestamp, false);
  816. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  817. while (!isLastBuff)
  818. {
  819. /* The last buffer descriptor of a frame. */
  820. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  821. {
  822. /* This is a valid frame. */
  823. isLastBuff = true;
  824. if (length == curBuffDescrip->length)
  825. {
  826. /* Copy the frame to user's buffer without FCS. */
  827. len = curBuffDescrip->length - offset;
  828. memcpy(data + offset, curBuffDescrip->buffer, len);
  829. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  830. /* Store the PTP 1588 timestamp for received PTP event frame. */
  831. if (isPtpEventMessage)
  832. {
  833. /* Set the timestamp to the timestamp ring. */
  834. ptpTimestamp.timeStamp.nanosecond = curBuffDescrip->timestamp;
  835. result = ENET_StoreRxFrameTime(base, handle, &ptpTimestamp);
  836. }
  837. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  838. /* Updates the receive buffer descriptors. */
  839. ENET_UpdateReadBuffers(base, handle);
  840. return result;
  841. }
  842. else
  843. {
  844. /* Updates the receive buffer descriptors. */
  845. ENET_UpdateReadBuffers(base, handle);
  846. }
  847. }
  848. else
  849. {
  850. /* Store a frame on several buffer descriptors. */
  851. isLastBuff = false;
  852. /* Length check. */
  853. if (offset >= length)
  854. {
  855. break;
  856. }
  857. memcpy(data + offset, curBuffDescrip->buffer, handle->rxBuffSizeAlign);
  858. offset += handle->rxBuffSizeAlign;
  859. /* Updates the receive buffer descriptors. */
  860. ENET_UpdateReadBuffers(base, handle);
  861. }
  862. /* Get the current buffer descriptor. */
  863. curBuffDescrip = handle->rxBdCurrent;
  864. }
  865. }
  866. return kStatus_ENET_RxFrameFail;
  867. }
  868. static void ENET_UpdateReadBuffers(ENET_Type *base, enet_handle_t *handle)
  869. {
  870. assert(handle);
  871. /* Clears status. */
  872. handle->rxBdCurrent->control &= ENET_BUFFDESCRIPTOR_RX_WRAP_MASK;
  873. /* Sets the receive buffer descriptor with the empty flag. */
  874. handle->rxBdCurrent->control |= ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK;
  875. /* Increase current buffer descriptor to the next one. */
  876. if (handle->rxBdCurrent->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  877. {
  878. handle->rxBdCurrent = handle->rxBdBase;
  879. }
  880. else
  881. {
  882. handle->rxBdCurrent++;
  883. }
  884. /* Actives the receive buffer descriptor. */
  885. base->RDAR = ENET_RDAR_RDAR_MASK;
  886. }
  887. status_t ENET_SendFrame(ENET_Type *base, enet_handle_t *handle, uint8_t *data, uint32_t length)
  888. {
  889. assert(handle);
  890. assert(handle->txBdCurrent);
  891. assert(data);
  892. assert(length <= ENET_FRAME_MAX_FRAMELEN);
  893. volatile enet_tx_bd_struct_t *curBuffDescrip = handle->txBdCurrent;
  894. uint32_t len = 0;
  895. uint32_t sizeleft = 0;
  896. /* Check if the transmit buffer is ready. */
  897. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  898. {
  899. return kStatus_ENET_TxFrameBusy;
  900. }
  901. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  902. bool isPtpEventMessage = false;
  903. /* Check PTP message with the PTP header. */
  904. isPtpEventMessage = ENET_Ptp1588ParseFrame(data, NULL, true);
  905. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  906. /* One transmit buffer is enough for one frame. */
  907. if (handle->txBuffSizeAlign >= length)
  908. {
  909. /* Copy data to the buffer for uDMA transfer. */
  910. memcpy(curBuffDescrip->buffer, data, length);
  911. /* Set data length. */
  912. curBuffDescrip->length = length;
  913. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  914. /* For enable the timestamp. */
  915. if (isPtpEventMessage)
  916. {
  917. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  918. }
  919. else
  920. {
  921. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  922. }
  923. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  924. curBuffDescrip->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
  925. /* Increase the buffer descriptor address. */
  926. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  927. {
  928. handle->txBdCurrent = handle->txBdBase;
  929. }
  930. else
  931. {
  932. handle->txBdCurrent++;
  933. }
  934. /* Active the transmit buffer descriptor. */
  935. base->TDAR = ENET_TDAR_TDAR_MASK;
  936. return kStatus_Success;
  937. }
  938. else
  939. {
  940. /* One frame requires more than one transmit buffers. */
  941. do
  942. {
  943. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  944. /* For enable the timestamp. */
  945. if (isPtpEventMessage)
  946. {
  947. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  948. }
  949. else
  950. {
  951. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  952. }
  953. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  954. /* Increase the buffer descriptor address. */
  955. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  956. {
  957. handle->txBdCurrent = handle->txBdBase;
  958. }
  959. else
  960. {
  961. handle->txBdCurrent++;
  962. }
  963. /* update the size left to be transmit. */
  964. sizeleft = length - len;
  965. if (sizeleft > handle->txBuffSizeAlign)
  966. {
  967. /* Data copy. */
  968. memcpy(curBuffDescrip->buffer, data + len, handle->txBuffSizeAlign);
  969. /* Data length update. */
  970. curBuffDescrip->length = handle->txBuffSizeAlign;
  971. len += handle->txBuffSizeAlign;
  972. /* Sets the control flag. */
  973. curBuffDescrip->control &= ~ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  974. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK;
  975. /* Active the transmit buffer descriptor*/
  976. base->TDAR = ENET_TDAR_TDAR_MASK;
  977. }
  978. else
  979. {
  980. memcpy(curBuffDescrip->buffer, data + len, sizeleft);
  981. curBuffDescrip->length = sizeleft;
  982. /* Set Last buffer wrap flag. */
  983. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  984. /* Active the transmit buffer descriptor. */
  985. base->TDAR = ENET_TDAR_TDAR_MASK;
  986. return kStatus_Success;
  987. }
  988. /* Get the current buffer descriptor address. */
  989. curBuffDescrip = handle->txBdCurrent;
  990. } while (!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK));
  991. return kStatus_ENET_TxFrameBusy;
  992. }
  993. }
  994. void ENET_AddMulticastGroup(ENET_Type *base, uint8_t *address)
  995. {
  996. assert(address);
  997. uint32_t crc = 0xFFFFFFFFU;
  998. uint32_t count1 = 0;
  999. uint32_t count2 = 0;
  1000. /* Calculates the CRC-32 polynomial on the multicast group address. */
  1001. for (count1 = 0; count1 < ENET_FRAME_MACLEN; count1++)
  1002. {
  1003. uint8_t c = address[count1];
  1004. for (count2 = 0; count2 < 0x08U; count2++)
  1005. {
  1006. if ((c ^ crc) & 1U)
  1007. {
  1008. crc >>= 1U;
  1009. c >>= 1U;
  1010. crc ^= 0xEDB88320U;
  1011. }
  1012. else
  1013. {
  1014. crc >>= 1U;
  1015. c >>= 1U;
  1016. }
  1017. }
  1018. }
  1019. /* Enable a multicast group address. */
  1020. if (!((crc >> 0x1FU) & 1U))
  1021. {
  1022. base->GALR |= 1U << ((crc >> 0x1AU) & 0x1FU);
  1023. }
  1024. else
  1025. {
  1026. base->GAUR |= 1U << ((crc >> 0x1AU) & 0x1FU);
  1027. }
  1028. }
  1029. void ENET_LeaveMulticastGroup(ENET_Type *base, uint8_t *address)
  1030. {
  1031. assert(address);
  1032. uint32_t crc = 0xFFFFFFFFU;
  1033. uint32_t count1 = 0;
  1034. uint32_t count2 = 0;
  1035. /* Calculates the CRC-32 polynomial on the multicast group address. */
  1036. for (count1 = 0; count1 < ENET_FRAME_MACLEN; count1++)
  1037. {
  1038. uint8_t c = address[count1];
  1039. for (count2 = 0; count2 < 0x08U; count2++)
  1040. {
  1041. if ((c ^ crc) & 1U)
  1042. {
  1043. crc >>= 1U;
  1044. c >>= 1U;
  1045. crc ^= 0xEDB88320U;
  1046. }
  1047. else
  1048. {
  1049. crc >>= 1U;
  1050. c >>= 1U;
  1051. }
  1052. }
  1053. }
  1054. /* Set the hash table. */
  1055. if (!((crc >> 0x1FU) & 1U))
  1056. {
  1057. base->GALR &= ~(1U << ((crc >> 0x1AU) & 0x1FU));
  1058. }
  1059. else
  1060. {
  1061. base->GAUR &= ~(1U << ((crc >> 0x1AU) & 0x1FU));
  1062. }
  1063. }
  1064. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1065. status_t ENET_GetTxErrAfterSendFrame(enet_handle_t *handle, enet_data_error_stats_t *eErrorStatic)
  1066. {
  1067. assert(handle);
  1068. assert(eErrorStatic);
  1069. uint16_t control = 0;
  1070. uint16_t controlExt = 0;
  1071. do
  1072. {
  1073. /* Get the current dirty transmit buffer descriptor. */
  1074. control = handle->txBdDirtyStatic->control;
  1075. controlExt = handle->txBdDirtyStatic->controlExtend0;
  1076. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  1077. if (control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1078. {
  1079. return kStatus_ENET_TxFrameBusy;
  1080. }
  1081. /* Increase the transmit dirty static pointer. */
  1082. if (handle->txBdDirtyStatic->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1083. {
  1084. handle->txBdDirtyStatic = handle->txBdBase;
  1085. }
  1086. else
  1087. {
  1088. handle->txBdDirtyStatic++;
  1089. }
  1090. /* If the transmit buffer descriptor is ready and the last buffer descriptor, store packet statistic. */
  1091. if (control & ENET_BUFFDESCRIPTOR_TX_LAST_MASK)
  1092. {
  1093. if (controlExt & ENET_BUFFDESCRIPTOR_TX_ERR_MASK)
  1094. {
  1095. /* Transmit error. */
  1096. eErrorStatic->statsTxErr++;
  1097. }
  1098. if (controlExt & ENET_BUFFDESCRIPTOR_TX_EXCCOLLISIONERR_MASK)
  1099. {
  1100. /* Transmit excess collision error. */
  1101. eErrorStatic->statsTxExcessCollisionErr++;
  1102. }
  1103. if (controlExt & ENET_BUFFDESCRIPTOR_TX_LATECOLLISIONERR_MASK)
  1104. {
  1105. /* Transmit late collision error. */
  1106. eErrorStatic->statsTxLateCollisionErr++;
  1107. }
  1108. if (controlExt & ENET_BUFFDESCRIPTOR_TX_UNDERFLOWERR_MASK)
  1109. {
  1110. /* Transmit under flow error. */
  1111. eErrorStatic->statsTxUnderFlowErr++;
  1112. }
  1113. if (controlExt & ENET_BUFFDESCRIPTOR_TX_OVERFLOWERR_MASK)
  1114. {
  1115. /* Transmit over flow error. */
  1116. eErrorStatic->statsTxOverFlowErr++;
  1117. }
  1118. return kStatus_Success;
  1119. }
  1120. } while (handle->txBdDirtyStatic != handle->txBdCurrent);
  1121. return kStatus_ENET_TxFrameFail;
  1122. }
  1123. static bool ENET_Ptp1588ParseFrame(uint8_t *data, enet_ptp_time_data_t *ptpTsData, bool isFastEnabled)
  1124. {
  1125. assert(data);
  1126. if (!isFastEnabled)
  1127. {
  1128. assert(ptpTsData);
  1129. }
  1130. bool isPtpMsg = false;
  1131. uint8_t *buffer = data;
  1132. uint16_t ptpType;
  1133. /* Check for VLAN frame. */
  1134. if (*(uint16_t *)(buffer + ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET) == ENET_HTONS(ENET_8021QVLAN))
  1135. {
  1136. buffer += ENET_FRAME_VLAN_TAGLEN;
  1137. }
  1138. ptpType = *(uint16_t *)(buffer + ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET);
  1139. switch (ENET_HTONS(ptpType))
  1140. { /* Ethernet layer 2. */
  1141. case ENET_ETHERNETL2:
  1142. if (*(uint8_t *)(buffer + ENET_PTP1588_ETHL2_MSGTYPE_OFFSET) <= kENET_PtpEventMsgType)
  1143. {
  1144. isPtpMsg = true;
  1145. if (!isFastEnabled)
  1146. {
  1147. /* It's a ptpv2 message and store the ptp header information. */
  1148. ptpTsData->version = (*(uint8_t *)(buffer + ENET_PTP1588_ETHL2_VERSION_OFFSET)) & 0x0F;
  1149. ptpTsData->messageType = (*(uint8_t *)(buffer + ENET_PTP1588_ETHL2_MSGTYPE_OFFSET)) & 0x0F;
  1150. ptpTsData->sequenceId = ENET_HTONS(*(uint16_t *)(buffer + ENET_PTP1588_ETHL2_SEQUENCEID_OFFSET));
  1151. memcpy((void *)&ptpTsData->sourcePortId[0], (void *)(buffer + ENET_PTP1588_ETHL2_CLOCKID_OFFSET),
  1152. kENET_PtpSrcPortIdLen);
  1153. }
  1154. }
  1155. break;
  1156. /* IPV4. */
  1157. case ENET_IPV4:
  1158. if ((*(uint8_t *)(buffer + ENET_PTP1588_IPVERSION_OFFSET) >> 4) == ENET_IPV4VERSION)
  1159. {
  1160. if (((*(uint16_t *)(buffer + ENET_PTP1588_IPV4_UDP_PORT_OFFSET)) == ENET_HTONS(kENET_PtpEventPort)) &&
  1161. (*(uint8_t *)(buffer + ENET_PTP1588_IPV4_UDP_PROTOCOL_OFFSET) == ENET_UDPVERSION))
  1162. {
  1163. /* Set the PTP message flag. */
  1164. isPtpMsg = true;
  1165. if (!isFastEnabled)
  1166. {
  1167. /* It's a IPV4 ptp message and store the ptp header information. */
  1168. ptpTsData->version = (*(uint8_t *)(buffer + ENET_PTP1588_IPV4_UDP_VERSION_OFFSET)) & 0x0F;
  1169. ptpTsData->messageType = (*(uint8_t *)(buffer + ENET_PTP1588_IPV4_UDP_MSGTYPE_OFFSET)) & 0x0F;
  1170. ptpTsData->sequenceId =
  1171. ENET_HTONS(*(uint16_t *)(buffer + ENET_PTP1588_IPV4_UDP_SEQUENCEID_OFFSET));
  1172. memcpy((void *)&ptpTsData->sourcePortId[0],
  1173. (void *)(buffer + ENET_PTP1588_IPV4_UDP_CLKID_OFFSET), kENET_PtpSrcPortIdLen);
  1174. }
  1175. }
  1176. }
  1177. break;
  1178. /* IPV6. */
  1179. case ENET_IPV6:
  1180. if ((*(uint8_t *)(buffer + ENET_PTP1588_IPVERSION_OFFSET) >> 4) == ENET_IPV6VERSION)
  1181. {
  1182. if (((*(uint16_t *)(buffer + ENET_PTP1588_IPV6_UDP_PORT_OFFSET)) == ENET_HTONS(kENET_PtpEventPort)) &&
  1183. (*(uint8_t *)(buffer + ENET_PTP1588_IPV6_UDP_PROTOCOL_OFFSET) == ENET_UDPVERSION))
  1184. {
  1185. /* Set the PTP message flag. */
  1186. isPtpMsg = true;
  1187. if (!isFastEnabled)
  1188. {
  1189. /* It's a IPV6 ptp message and store the ptp header information. */
  1190. ptpTsData->version = (*(uint8_t *)(buffer + ENET_PTP1588_IPV6_UDP_VERSION_OFFSET)) & 0x0F;
  1191. ptpTsData->messageType = (*(uint8_t *)(buffer + ENET_PTP1588_IPV6_UDP_MSGTYPE_OFFSET)) & 0x0F;
  1192. ptpTsData->sequenceId =
  1193. ENET_HTONS(*(uint16_t *)(buffer + ENET_PTP1588_IPV6_UDP_SEQUENCEID_OFFSET));
  1194. memcpy((void *)&ptpTsData->sourcePortId[0],
  1195. (void *)(buffer + ENET_PTP1588_IPV6_UDP_CLKID_OFFSET), kENET_PtpSrcPortIdLen);
  1196. }
  1197. }
  1198. }
  1199. break;
  1200. default:
  1201. break;
  1202. }
  1203. return isPtpMsg;
  1204. }
  1205. void ENET_Ptp1588Configure(ENET_Type *base, enet_handle_t *handle, enet_ptp_config_t *ptpConfig)
  1206. {
  1207. assert(handle);
  1208. assert(ptpConfig);
  1209. uint32_t instance = ENET_GetInstance(base);
  1210. /* Start the 1588 timer. */
  1211. ENET_Ptp1588StartTimer(base, ptpConfig->ptp1588ClockSrc_Hz);
  1212. /* Enables the time stamp interrupt for the master clock on a device. */
  1213. ENET_EnableInterrupts(base, kENET_TsTimerInterrupt);
  1214. /* Enables only frame interrupt for transmit side to store the transmit
  1215. frame time-stamp when the whole frame is transmitted out. */
  1216. ENET_EnableInterrupts(base, kENET_TxFrameInterrupt);
  1217. ENET_DisableInterrupts(base, kENET_TxBufferInterrupt);
  1218. /* Setting the receive and transmit state for transaction. */
  1219. handle->rxPtpTsDataRing.ptpTsData = ptpConfig->rxPtpTsData;
  1220. handle->rxPtpTsDataRing.size = ptpConfig->ptpTsRxBuffNum;
  1221. handle->rxPtpTsDataRing.front = 0;
  1222. handle->rxPtpTsDataRing.end = 0;
  1223. handle->txPtpTsDataRing.ptpTsData = ptpConfig->txPtpTsData;
  1224. handle->txPtpTsDataRing.size = ptpConfig->ptpTsTxBuffNum;
  1225. handle->txPtpTsDataRing.front = 0;
  1226. handle->txPtpTsDataRing.end = 0;
  1227. handle->msTimerSecond = 0;
  1228. handle->txBdDirtyTime = handle->txBdBase;
  1229. handle->txBdDirtyStatic = handle->txBdBase;
  1230. /* Set the IRQ handler when the interrupt is enabled. */
  1231. s_enetTxIsr = ENET_TransmitIRQHandler;
  1232. s_enetTsIsr = ENET_Ptp1588TimerIRQHandler;
  1233. EnableIRQ(s_enetTsIrqId[instance]);
  1234. EnableIRQ(s_enetTxIrqId[instance]);
  1235. }
  1236. void ENET_Ptp1588StartTimer(ENET_Type *base, uint32_t ptpClkSrc)
  1237. {
  1238. /* Restart PTP 1588 timer, master clock. */
  1239. base->ATCR = ENET_ATCR_RESTART_MASK;
  1240. /* Initializes PTP 1588 timer. */
  1241. base->ATINC = ENET_ATINC_INC(ENET_NANOSECOND_ONE_SECOND / ptpClkSrc);
  1242. base->ATPER = ENET_NANOSECOND_ONE_SECOND;
  1243. /* Sets periodical event and the event signal output assertion and Actives PTP 1588 timer. */
  1244. base->ATCR = ENET_ATCR_PEREN_MASK | ENET_ATCR_PINPER_MASK | ENET_ATCR_EN_MASK;
  1245. }
  1246. void ENET_Ptp1588GetTimer(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_t *ptpTime)
  1247. {
  1248. assert(handle);
  1249. assert(ptpTime);
  1250. uint16_t count = ENET_1588TIME_DELAY_COUNT;
  1251. uint32_t primask;
  1252. /* Disables the interrupt. */
  1253. primask = DisableGlobalIRQ();
  1254. /* Get the current PTP time. */
  1255. ptpTime->second = handle->msTimerSecond;
  1256. /* Get the nanosecond from the master timer. */
  1257. base->ATCR |= ENET_ATCR_CAPTURE_MASK;
  1258. /* Add at least six clock cycle delay to get accurate time.
  1259. It's the requirement when the 1588 clock source is slower
  1260. than the register clock.
  1261. */
  1262. while (count--)
  1263. {
  1264. __NOP();
  1265. }
  1266. /* Get the captured time. */
  1267. ptpTime->nanosecond = base->ATVR;
  1268. /* Enables the interrupt. */
  1269. EnableGlobalIRQ(primask);
  1270. }
  1271. void ENET_Ptp1588SetTimer(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_t *ptpTime)
  1272. {
  1273. assert(handle);
  1274. assert(ptpTime);
  1275. uint32_t primask;
  1276. /* Disables the interrupt. */
  1277. primask = DisableGlobalIRQ();
  1278. /* Sets PTP timer. */
  1279. handle->msTimerSecond = ptpTime->second;
  1280. base->ATVR = ptpTime->nanosecond;
  1281. /* Enables the interrupt. */
  1282. EnableGlobalIRQ(primask);
  1283. }
  1284. void ENET_Ptp1588AdjustTimer(ENET_Type *base, uint32_t corrIncrease, uint32_t corrPeriod)
  1285. {
  1286. /* Set correction for PTP timer increment. */
  1287. base->ATINC = (base->ATINC & ~ENET_ATINC_INC_CORR_MASK) | (corrIncrease << ENET_ATINC_INC_CORR_SHIFT);
  1288. /* Set correction for PTP timer period. */
  1289. base->ATCOR = (base->ATCOR & ~ENET_ATCOR_COR_MASK) | (corrPeriod << ENET_ATCOR_COR_SHIFT);
  1290. }
  1291. static status_t ENET_Ptp1588UpdateTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimeData)
  1292. {
  1293. assert(ptpTsDataRing);
  1294. assert(ptpTsDataRing->ptpTsData);
  1295. assert(ptpTimeData);
  1296. uint16_t usedBuffer = 0;
  1297. /* Check if the buffers ring is full. */
  1298. if (ptpTsDataRing->end >= ptpTsDataRing->front)
  1299. {
  1300. usedBuffer = ptpTsDataRing->end - ptpTsDataRing->front;
  1301. }
  1302. else
  1303. {
  1304. usedBuffer = ptpTsDataRing->size - (ptpTsDataRing->front - ptpTsDataRing->end);
  1305. }
  1306. if (usedBuffer == ptpTsDataRing->size)
  1307. {
  1308. return kStatus_ENET_PtpTsRingFull;
  1309. }
  1310. /* Copy the new data into the buffer. */
  1311. memcpy((ptpTsDataRing->ptpTsData + ptpTsDataRing->end), ptpTimeData, sizeof(enet_ptp_time_data_t));
  1312. /* Increase the buffer pointer to the next empty one. */
  1313. ptpTsDataRing->end = (ptpTsDataRing->end + 1) % ptpTsDataRing->size;
  1314. return kStatus_Success;
  1315. }
  1316. static status_t ENET_Ptp1588SearchTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimedata)
  1317. {
  1318. assert(ptpTsDataRing);
  1319. assert(ptpTsDataRing->ptpTsData);
  1320. assert(ptpTimedata);
  1321. uint32_t index;
  1322. uint32_t size;
  1323. uint16_t usedBuffer = 0;
  1324. /* Check the PTP 1588 timestamp ring. */
  1325. if (ptpTsDataRing->front == ptpTsDataRing->end)
  1326. {
  1327. return kStatus_ENET_PtpTsRingEmpty;
  1328. }
  1329. /* Search the element in the ring buffer */
  1330. index = ptpTsDataRing->front;
  1331. size = ptpTsDataRing->size;
  1332. while (index != ptpTsDataRing->end)
  1333. {
  1334. if (((ptpTsDataRing->ptpTsData + index)->sequenceId == ptpTimedata->sequenceId) &&
  1335. (!memcmp(((void *)&(ptpTsDataRing->ptpTsData + index)->sourcePortId[0]),
  1336. (void *)&ptpTimedata->sourcePortId[0], kENET_PtpSrcPortIdLen)) &&
  1337. ((ptpTsDataRing->ptpTsData + index)->version == ptpTimedata->version) &&
  1338. ((ptpTsDataRing->ptpTsData + index)->messageType == ptpTimedata->messageType))
  1339. {
  1340. break;
  1341. }
  1342. /* Increase the ptp ring index. */
  1343. index = (index + 1) % size;
  1344. }
  1345. if (index == ptpTsDataRing->end)
  1346. {
  1347. /* Check if buffers is full. */
  1348. if (ptpTsDataRing->end >= ptpTsDataRing->front)
  1349. {
  1350. usedBuffer = ptpTsDataRing->end - ptpTsDataRing->front;
  1351. }
  1352. else
  1353. {
  1354. usedBuffer = ptpTsDataRing->size - (ptpTsDataRing->front - ptpTsDataRing->end);
  1355. }
  1356. if (usedBuffer == ptpTsDataRing->size)
  1357. { /* Drop one in the front. */
  1358. ptpTsDataRing->front = (ptpTsDataRing->front + 1) % size;
  1359. }
  1360. return kStatus_ENET_PtpTsRingFull;
  1361. }
  1362. /* Get the right timestamp of the required ptp messag. */
  1363. ptpTimedata->timeStamp.second = (ptpTsDataRing->ptpTsData + index)->timeStamp.second;
  1364. ptpTimedata->timeStamp.nanosecond = (ptpTsDataRing->ptpTsData + index)->timeStamp.nanosecond;
  1365. /* Increase the index. */
  1366. ptpTsDataRing->front = (ptpTsDataRing->front + 1) % size;
  1367. return kStatus_Success;
  1368. }
  1369. static status_t ENET_StoreRxFrameTime(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData)
  1370. {
  1371. assert(handle);
  1372. assert(ptpTimeData);
  1373. bool ptpTimerWrap = false;
  1374. enet_ptp_time_t ptpTimer;
  1375. uint32_t primask;
  1376. /* Disables the interrupt. */
  1377. primask = DisableGlobalIRQ();
  1378. /* Get current PTP timer nanosecond value. */
  1379. ENET_Ptp1588GetTimer(base, handle, &ptpTimer);
  1380. /* Get PTP timer wrap event. */
  1381. ptpTimerWrap = base->EIR & kENET_TsTimerInterrupt;
  1382. /* Get transmit time stamp second. */
  1383. if ((ptpTimer.nanosecond > ptpTimeData->timeStamp.nanosecond) ||
  1384. ((ptpTimer.nanosecond < ptpTimeData->timeStamp.nanosecond) && ptpTimerWrap))
  1385. {
  1386. ptpTimeData->timeStamp.second = handle->msTimerSecond;
  1387. }
  1388. else
  1389. {
  1390. ptpTimeData->timeStamp.second = handle->msTimerSecond - 1;
  1391. }
  1392. /* Enable the interrupt. */
  1393. EnableGlobalIRQ(primask);
  1394. /* Store the timestamp to the receive time stamp ring. */
  1395. /* Check if the buffers ring is full. */
  1396. return ENET_Ptp1588UpdateTimeRing(&handle->rxPtpTsDataRing, ptpTimeData);
  1397. }
  1398. static status_t ENET_StoreTxFrameTime(ENET_Type *base, enet_handle_t *handle)
  1399. {
  1400. assert(handle);
  1401. uint32_t primask;
  1402. bool ptpTimerWrap;
  1403. bool isPtpEventMessage = false;
  1404. enet_ptp_time_data_t ptpTimeData;
  1405. volatile enet_tx_bd_struct_t *curBuffDescrip = handle->txBdDirtyTime;
  1406. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  1407. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1408. {
  1409. return kStatus_ENET_TxFrameBusy;
  1410. }
  1411. /* Parse the PTP message. */
  1412. isPtpEventMessage = ENET_Ptp1588ParseFrame(curBuffDescrip->buffer, &ptpTimeData, false);
  1413. if (isPtpEventMessage)
  1414. {
  1415. do
  1416. {
  1417. /* Increase current buffer descriptor to the next one. */
  1418. if (handle->txBdDirtyTime->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1419. {
  1420. handle->txBdDirtyTime = handle->txBdBase;
  1421. }
  1422. else
  1423. {
  1424. handle->txBdDirtyTime++;
  1425. }
  1426. /* Do time stamp check on the last buffer descriptor of the frame. */
  1427. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_LAST_MASK)
  1428. {
  1429. /* Disables the interrupt. */
  1430. primask = DisableGlobalIRQ();
  1431. /* Get current PTP timer nanosecond value. */
  1432. ENET_Ptp1588GetTimer(base, handle, &ptpTimeData.timeStamp);
  1433. /* Get PTP timer wrap event. */
  1434. ptpTimerWrap = base->EIR & kENET_TsTimerInterrupt;
  1435. /* Get transmit time stamp second. */
  1436. if ((ptpTimeData.timeStamp.nanosecond > curBuffDescrip->timestamp) ||
  1437. ((ptpTimeData.timeStamp.nanosecond < curBuffDescrip->timestamp) && ptpTimerWrap))
  1438. {
  1439. ptpTimeData.timeStamp.second = handle->msTimerSecond;
  1440. }
  1441. else
  1442. {
  1443. ptpTimeData.timeStamp.second = handle->msTimerSecond - 1;
  1444. }
  1445. /* Enable the interrupt. */
  1446. EnableGlobalIRQ(primask);
  1447. /* Store the timestamp to the transmit timestamp ring. */
  1448. return ENET_Ptp1588UpdateTimeRing(&handle->txPtpTsDataRing, &ptpTimeData);
  1449. }
  1450. /* Get the current transmit buffer descriptor. */
  1451. curBuffDescrip = handle->txBdDirtyTime;
  1452. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  1453. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1454. {
  1455. return kStatus_ENET_TxFrameBusy;
  1456. }
  1457. } while (handle->txBdDirtyTime != handle->txBdCurrent);
  1458. return kStatus_ENET_TxFrameFail;
  1459. }
  1460. return kStatus_Success;
  1461. }
  1462. status_t ENET_GetTxFrameTime(enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData)
  1463. {
  1464. assert(handle);
  1465. assert(ptpTimeData);
  1466. return ENET_Ptp1588SearchTimeRing(&handle->txPtpTsDataRing, ptpTimeData);
  1467. }
  1468. status_t ENET_GetRxFrameTime(enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData)
  1469. {
  1470. assert(handle);
  1471. assert(ptpTimeData);
  1472. return ENET_Ptp1588SearchTimeRing(&handle->rxPtpTsDataRing, ptpTimeData);
  1473. }
  1474. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1475. void ENET_TransmitIRQHandler(ENET_Type *base, enet_handle_t *handle)
  1476. {
  1477. assert(handle);
  1478. /* Check if the transmit interrupt happen. */
  1479. while ((kENET_TxBufferInterrupt | kENET_TxFrameInterrupt) & base->EIR)
  1480. {
  1481. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1482. if (base->EIR & kENET_TxFrameInterrupt)
  1483. {
  1484. /* Store the transmit timestamp from the buffer descriptor should be done here. */
  1485. ENET_StoreTxFrameTime(base, handle);
  1486. }
  1487. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1488. /* Clear the transmit interrupt event. */
  1489. base->EIR = kENET_TxFrameInterrupt | kENET_TxBufferInterrupt;
  1490. /* Callback function. */
  1491. if (handle->callback)
  1492. {
  1493. handle->callback(base, handle, kENET_TxEvent, handle->userData);
  1494. }
  1495. }
  1496. }
  1497. void ENET_ReceiveIRQHandler(ENET_Type *base, enet_handle_t *handle)
  1498. {
  1499. assert(handle);
  1500. /* Check if the receive interrupt happen. */
  1501. while ((kENET_RxBufferInterrupt | kENET_RxFrameInterrupt) & base->EIR)
  1502. {
  1503. /* Clear the transmit interrupt event. */
  1504. base->EIR = kENET_RxFrameInterrupt | kENET_RxBufferInterrupt;
  1505. /* Callback function. */
  1506. if (handle->callback)
  1507. {
  1508. handle->callback(base, handle, kENET_RxEvent, handle->userData);
  1509. }
  1510. }
  1511. }
  1512. void ENET_ErrorIRQHandler(ENET_Type *base, enet_handle_t *handle)
  1513. {
  1514. assert(handle);
  1515. uint32_t errMask = kENET_BabrInterrupt | kENET_BabtInterrupt | kENET_EBusERInterrupt | kENET_PayloadRxInterrupt |
  1516. kENET_LateCollisionInterrupt | kENET_RetryLimitInterrupt | kENET_UnderrunInterrupt;
  1517. /* Check if the error interrupt happen. */
  1518. if (kENET_WakeupInterrupt & base->EIR)
  1519. {
  1520. /* Clear the wakeup interrupt. */
  1521. base->EIR = kENET_WakeupInterrupt;
  1522. /* wake up and enter the normal mode. */
  1523. ENET_EnableSleepMode(base, false);
  1524. /* Callback function. */
  1525. if (handle->callback)
  1526. {
  1527. handle->callback(base, handle, kENET_WakeUpEvent, handle->userData);
  1528. }
  1529. }
  1530. else
  1531. {
  1532. /* Clear the error interrupt event status. */
  1533. errMask &= base->EIR;
  1534. base->EIR = errMask;
  1535. /* Callback function. */
  1536. if (handle->callback)
  1537. {
  1538. handle->callback(base, handle, kENET_ErrEvent, handle->userData);
  1539. }
  1540. }
  1541. }
  1542. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1543. void ENET_Ptp1588TimerIRQHandler(ENET_Type *base, enet_handle_t *handle)
  1544. {
  1545. assert(handle);
  1546. /* Check if the PTP time stamp interrupt happen. */
  1547. if (kENET_TsTimerInterrupt & base->EIR)
  1548. {
  1549. /* Clear the time stamp interrupt. */
  1550. base->EIR = kENET_TsTimerInterrupt;
  1551. /* Increase timer second counter. */
  1552. handle->msTimerSecond++;
  1553. /* Callback function. */
  1554. if (handle->callback)
  1555. {
  1556. handle->callback(base, handle, kENET_TimeStampEvent, handle->userData);
  1557. }
  1558. }
  1559. else
  1560. {
  1561. /* Clear the time stamp interrupt. */
  1562. base->EIR = kENET_TsAvailInterrupt;
  1563. /* Callback function. */
  1564. if (handle->callback)
  1565. {
  1566. handle->callback(base, handle, kENET_TimeStampAvailEvent, handle->userData);
  1567. }
  1568. }
  1569. }
  1570. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1571. void ENET_CommonFrame0IRQHandler(ENET_Type *base)
  1572. {
  1573. uint32_t event = base->EIR;
  1574. uint32_t instance = ENET_GetInstance(base);
  1575. if (event & ENET_TX_INTERRUPT)
  1576. {
  1577. s_enetTxIsr(base, s_ENETHandle[instance]);
  1578. }
  1579. if (event & ENET_RX_INTERRUPT)
  1580. {
  1581. s_enetRxIsr(base, s_ENETHandle[instance]);
  1582. }
  1583. if (event & ENET_TS_INTERRUPT)
  1584. {
  1585. s_enetTsIsr(base, s_ENETHandle[instance]);
  1586. }
  1587. if (event & ENET_ERR_INTERRUPT)
  1588. {
  1589. s_enetErrIsr(base, s_ENETHandle[instance]);
  1590. }
  1591. }
  1592. #if defined(ENET)
  1593. void ENET_Transmit_IRQHandler(void)
  1594. {
  1595. s_enetTxIsr(ENET, s_ENETHandle[0]);
  1596. }
  1597. void ENET_Receive_IRQHandler(void)
  1598. {
  1599. s_enetRxIsr(ENET, s_ENETHandle[0]);
  1600. }
  1601. void ENET_Error_IRQHandler(void)
  1602. {
  1603. s_enetErrIsr(ENET, s_ENETHandle[0]);
  1604. }
  1605. void ENET_1588_Timer_IRQHandler(void)
  1606. {
  1607. s_enetTsIsr(ENET, s_ENETHandle[0]);
  1608. }
  1609. #endif