fsl_enet.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_enet.h"
  35. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  36. #include "fsl_cache.h"
  37. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  38. /*******************************************************************************
  39. * Definitions
  40. ******************************************************************************/
  41. /* Component ID definition, used by tools. */
  42. #ifndef FSL_COMPONENT_ID
  43. #define FSL_COMPONENT_ID "platform.drivers.enet"
  44. #endif
  45. /*! @brief IPv4 PTP message IP version offset. */
  46. #define ENET_PTP1588_IPVERSION_OFFSET 0x0EU
  47. /*! @brief IPv4 PTP message UDP protocol offset. */
  48. #define ENET_PTP1588_IPV4_UDP_PROTOCOL_OFFSET 0x17U
  49. /*! @brief IPv4 PTP message UDP port offset. */
  50. #define ENET_PTP1588_IPV4_UDP_PORT_OFFSET 0x24U
  51. /*! @brief IPv4 PTP message UDP message type offset. */
  52. #define ENET_PTP1588_IPV4_UDP_MSGTYPE_OFFSET 0x2AU
  53. /*! @brief IPv4 PTP message UDP version offset. */
  54. #define ENET_PTP1588_IPV4_UDP_VERSION_OFFSET 0x2BU
  55. /*! @brief IPv4 PTP message UDP clock id offset. */
  56. #define ENET_PTP1588_IPV4_UDP_CLKID_OFFSET 0x3EU
  57. /*! @brief IPv4 PTP message UDP sequence id offset. */
  58. #define ENET_PTP1588_IPV4_UDP_SEQUENCEID_OFFSET 0x48U
  59. /*! @brief IPv4 PTP message UDP control offset. */
  60. #define ENET_PTP1588_IPV4_UDP_CTL_OFFSET 0x4AU
  61. /*! @brief IPv6 PTP message UDP protocol offset. */
  62. #define ENET_PTP1588_IPV6_UDP_PROTOCOL_OFFSET 0x14U
  63. /*! @brief IPv6 PTP message UDP port offset. */
  64. #define ENET_PTP1588_IPV6_UDP_PORT_OFFSET 0x38U
  65. /*! @brief IPv6 PTP message UDP message type offset. */
  66. #define ENET_PTP1588_IPV6_UDP_MSGTYPE_OFFSET 0x3EU
  67. /*! @brief IPv6 PTP message UDP version offset. */
  68. #define ENET_PTP1588_IPV6_UDP_VERSION_OFFSET 0x3FU
  69. /*! @brief IPv6 PTP message UDP clock id offset. */
  70. #define ENET_PTP1588_IPV6_UDP_CLKID_OFFSET 0x52U
  71. /*! @brief IPv6 PTP message UDP sequence id offset. */
  72. #define ENET_PTP1588_IPV6_UDP_SEQUENCEID_OFFSET 0x5CU
  73. /*! @brief IPv6 PTP message UDP control offset. */
  74. #define ENET_PTP1588_IPV6_UDP_CTL_OFFSET 0x5EU
  75. /*! @brief PTPv2 message Ethernet packet type offset. */
  76. #define ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET 0x0CU
  77. /*! @brief PTPv2 message Ethernet message type offset. */
  78. #define ENET_PTP1588_ETHL2_MSGTYPE_OFFSET 0x0EU
  79. /*! @brief PTPv2 message Ethernet version type offset. */
  80. #define ENET_PTP1588_ETHL2_VERSION_OFFSET 0X0FU
  81. /*! @brief PTPv2 message Ethernet clock id offset. */
  82. #define ENET_PTP1588_ETHL2_CLOCKID_OFFSET 0x22
  83. /*! @brief PTPv2 message Ethernet sequence id offset. */
  84. #define ENET_PTP1588_ETHL2_SEQUENCEID_OFFSET 0x2c
  85. /*! @brief Packet type Ethernet IEEE802.3 for PTPv2. */
  86. #define ENET_ETHERNETL2 0x88F7U
  87. /*! @brief Packet type IPv4. */
  88. #define ENET_IPV4 0x0800U
  89. /*! @brief Packet type IPv6. */
  90. #define ENET_IPV6 0x86ddU
  91. /*! @brief Packet type VLAN. */
  92. #define ENET_8021QVLAN 0x8100U
  93. /*! @brief UDP protocol type. */
  94. #define ENET_UDPVERSION 0x0011U
  95. /*! @brief Packet IP version IPv4. */
  96. #define ENET_IPV4VERSION 0x0004U
  97. /*! @brief Packet IP version IPv6. */
  98. #define ENET_IPV6VERSION 0x0006U
  99. /*! @brief Ethernet mac address length. */
  100. #define ENET_FRAME_MACLEN 6U
  101. /*! @brief Ethernet VLAN header length. */
  102. #define ENET_FRAME_VLAN_TAGLEN 4U
  103. /*! @brief MDC frequency. */
  104. #define ENET_MDC_FREQUENCY 2500000U
  105. /*! @brief NanoSecond in one second. */
  106. #define ENET_NANOSECOND_ONE_SECOND 1000000000U
  107. /*! @brief Define a common clock cycle delays used for time stamp capture. */
  108. #ifndef ENET_1588TIME_DELAY_COUNT
  109. #define ENET_1588TIME_DELAY_COUNT 10U
  110. #endif
  111. /*! @brief Defines the macro for converting constants from host byte order to network byte order. */
  112. #define ENET_HTONS(n) __REV16(n)
  113. #define ENET_HTONL(n) __REV(n)
  114. #define ENET_NTOHS(n) __REV16(n)
  115. #define ENET_NTOHL(n) __REV(n)
  116. /*! @brief Define the ENET ring/class bumber . */
  117. enum _enet_ring_number
  118. {
  119. kENET_Ring0 = 0U, /*!< ENET ring/class 0. */
  120. #if FSL_FEATURE_ENET_QUEUE > 1
  121. kENET_Ring1 = 1U, /*!< ENET ring/class 1. */
  122. kENET_Ring2 = 2U /*!< ENET ring/class 2. */
  123. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  124. };
  125. /*! @brief Define interrupt IRQ handler. */
  126. #if FSL_FEATURE_ENET_QUEUE > 1
  127. typedef void (*enet_isr_ring_t)(ENET_Type *base, enet_handle_t *handle, uint32_t ringId);
  128. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  129. typedef void (*enet_isr_t)(ENET_Type *base, enet_handle_t *handle);
  130. /*******************************************************************************
  131. * Prototypes
  132. ******************************************************************************/
  133. /*!
  134. * @brief Get the ENET instance from peripheral base address.
  135. *
  136. * @param base ENET peripheral base address.
  137. * @return ENET instance.
  138. */
  139. uint32_t ENET_GetInstance(ENET_Type *base);
  140. /*!
  141. * @brief Set ENET MAC controller with the configuration.
  142. *
  143. * @param base ENET peripheral base address.
  144. * @param handle The ENET handle pointer.
  145. * @param config ENET Mac configuration.
  146. * @param bufferConfig ENET buffer configuration.
  147. * @param macAddr ENET six-byte mac address.
  148. * @param srcClock_Hz ENET module clock source, normally it's system clock.
  149. */
  150. static void ENET_SetMacController(ENET_Type *base,
  151. enet_handle_t *handle,
  152. const enet_config_t *config,
  153. const enet_buffer_config_t *bufferConfig,
  154. uint8_t *macAddr,
  155. uint32_t srcClock_Hz);
  156. /*!
  157. * @brief Set ENET handler.
  158. *
  159. * @param base ENET peripheral base address.
  160. * @param handle The ENET handle pointer.
  161. * @param config ENET configuration stucture pointer.
  162. * @param bufferConfig ENET buffer configuration.
  163. */
  164. static void ENET_SetHandler(ENET_Type *base,
  165. enet_handle_t *handle,
  166. const enet_config_t *config,
  167. const enet_buffer_config_t *bufferConfig);
  168. /*!
  169. * @brief Set ENET MAC transmit buffer descriptors.
  170. *
  171. * @param handle The ENET handle pointer.
  172. * @param config The ENET configuration structure.
  173. * @param bufferConfig The ENET buffer configuration.
  174. */
  175. static void ENET_SetTxBufferDescriptors(enet_handle_t *handle, const enet_config_t *config, const enet_buffer_config_t *bufferConfig);
  176. /*!
  177. * @brief Set ENET MAC receive buffer descriptors.
  178. *
  179. * @param handle The ENET handle pointer.
  180. * @param config The ENET configuration structure.
  181. * @param bufferConfig The ENET buffer configuration.
  182. */
  183. static void ENET_SetRxBufferDescriptors(enet_handle_t *handle, const enet_config_t *config, const enet_buffer_config_t *bufferConfig);
  184. /*!
  185. * @brief Updates the ENET read buffer descriptors.
  186. *
  187. * @param base ENET peripheral base address.
  188. * @param handle The ENET handle pointer.
  189. * @param ringId The descriptor ring index, range from 0 ~ FSL_FEATURE_ENET_QUEUE - 1.
  190. * 0 ----- for single ring kinetis platform.
  191. * 0 ~ 2 for mulit-ring supported IMX8qm.
  192. */
  193. static void ENET_UpdateReadBuffers(ENET_Type *base, enet_handle_t *handle, uint32_t ringId);
  194. /*!
  195. * @brief Activates ENET send for multiple tx rings.
  196. *
  197. * @param base ENET peripheral base address.
  198. * @param ringId The descriptor ring index, range from 0 ~ FSL_FEATURE_ENET_QUEUE - 1.
  199. * 0 ----- for single ring kinetis platform.
  200. * 0 ~ 2 for mulit-ring supported IMX8qm.
  201. *
  202. * @note This must be called after the MAC configuration and
  203. * state are ready. It must be called after the ENET_Init() and
  204. * this should be called when the ENET receive required.
  205. */
  206. static void ENET_ActiveSend(ENET_Type *base, uint32_t ringId);
  207. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  208. /*!
  209. * @brief Parses the ENET frame for time-stamp process of PTP 1588 frame.
  210. *
  211. * @param data The ENET read data for frame parse.
  212. * @param ptpTsData The ENET PTP message and time-stamp data pointer.
  213. * @param isFastEnabled The fast parse flag.
  214. * - true , Fast processing, only check if this is a PTP message.
  215. * - false, Store the PTP message data after check the PTP message.
  216. */
  217. static bool ENET_Ptp1588ParseFrame(const uint8_t *data, enet_ptp_time_data_t *ptpTsData, bool isFastEnabled);
  218. /*!
  219. * @brief Updates the new PTP 1588 time-stamp to the time-stamp buffer ring.
  220. *
  221. * @param ptpTsDataRing The PTP message and time-stamp data ring pointer.
  222. * @param ptpTimeData The new PTP 1588 time-stamp data pointer.
  223. */
  224. static status_t ENET_Ptp1588UpdateTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimeData);
  225. /*!
  226. * @brief Search up the right PTP 1588 time-stamp from the time-stamp buffer ring.
  227. *
  228. * @param ptpTsDataRing The PTP message and time-stamp data ring pointer.
  229. * @param ptpTimeData The find out right PTP 1588 time-stamp data pointer with the specific PTP message.
  230. */
  231. static status_t ENET_Ptp1588SearchTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimedata);
  232. /*!
  233. * @brief Store the transmit time-stamp for event PTP frame in the time-stamp buffer ring.
  234. *
  235. * @param base ENET peripheral base address.
  236. * @param handle The ENET handle pointer.
  237. * @param ringId The descriptor ring index, range from 0 ~ FSL_FEATURE_ENET_QUEUE - 1.
  238. * 0 ----- for single ring kinetis platform.
  239. * 0 ~ 2 for mulit-ring supported IMX8qm.
  240. */
  241. static status_t ENET_StoreTxFrameTime(ENET_Type *base, enet_handle_t *handle, uint32_t ringId);
  242. /*!
  243. * @brief Store the receive time-stamp for event PTP frame in the time-stamp buffer ring.
  244. *
  245. * @param base ENET peripheral base address.
  246. * @param handle The ENET handle pointer.
  247. * @param ptpTimeData The PTP 1588 time-stamp data pointer.
  248. */
  249. static status_t ENET_StoreRxFrameTime(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData);
  250. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_AVB
  251. /*!
  252. * @brief Gets the ring index for transmission.
  253. *
  254. * @param base ENET peripheral base address.
  255. * @param data The ENET transmit data.
  256. * @param handle The ENET handle pointer.
  257. *
  258. * @note This must be called after the MAC configuration and
  259. * state are ready. It must be called after the ENET_Init() and
  260. * this should be called when the ENET receive required.
  261. */
  262. static uint8_t ENET_GetTxRingId(ENET_Type *base, uint8_t *data, enet_handle_t *handle);
  263. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  264. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  265. /*******************************************************************************
  266. * Variables
  267. ******************************************************************************/
  268. /*! @brief Pointers to enet handles for each instance. */
  269. static enet_handle_t *s_ENETHandle[FSL_FEATURE_SOC_ENET_COUNT] = {NULL};
  270. /*! @brief Pointers to enet clocks for each instance. */
  271. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  272. const clock_ip_name_t s_enetClock[] = ENET_CLOCKS;
  273. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  274. /*! @brief Pointers to enet transmit IRQ number for each instance. */
  275. static const IRQn_Type s_enetTxIrqId[] = ENET_Transmit_IRQS;
  276. /*! @brief Pointers to enet receive IRQ number for each instance. */
  277. static const IRQn_Type s_enetRxIrqId[] = ENET_Receive_IRQS;
  278. #if defined(ENET_ENHANCEDBUFFERDESCRIPTOR_MODE) && ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  279. /*! @brief Pointers to enet timestamp IRQ number for each instance. */
  280. static const IRQn_Type s_enetTsIrqId[] = ENET_1588_Timer_IRQS;
  281. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  282. /*! @brief Pointers to enet error IRQ number for each instance. */
  283. static const IRQn_Type s_enetErrIrqId[] = ENET_Error_IRQS;
  284. /*! @brief Pointers to enet bases for each instance. */
  285. static ENET_Type *const s_enetBases[] = ENET_BASE_PTRS;
  286. /* ENET ISR for transactional APIs. */
  287. #if FSL_FEATURE_ENET_QUEUE > 1
  288. static enet_isr_ring_t s_enetTxIsr;
  289. static enet_isr_ring_t s_enetRxIsr;
  290. #else
  291. static enet_isr_t s_enetTxIsr;
  292. static enet_isr_t s_enetRxIsr;
  293. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  294. static enet_isr_t s_enetErrIsr;
  295. static enet_isr_t s_enetTsIsr;
  296. /*******************************************************************************
  297. * Code
  298. ******************************************************************************/
  299. uint32_t ENET_GetInstance(ENET_Type *base)
  300. {
  301. uint32_t instance;
  302. /* Find the instance index from base address mappings. */
  303. for (instance = 0; instance < ARRAY_SIZE(s_enetBases); instance++)
  304. {
  305. if (s_enetBases[instance] == base)
  306. {
  307. break;
  308. }
  309. }
  310. assert(instance < ARRAY_SIZE(s_enetBases));
  311. return instance;
  312. }
  313. void ENET_GetDefaultConfig(enet_config_t *config)
  314. {
  315. /* Checks input parameter. */
  316. assert(config);
  317. /* Initializes the MAC configure structure to zero. */
  318. memset(config, 0, sizeof(enet_config_t));
  319. /* Sets MII mode, full duplex, 100Mbps for MAC and PHY data interface. */
  320. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  321. config->miiMode = kENET_RgmiiMode;
  322. #else
  323. config->miiMode = kENET_RmiiMode;
  324. #endif
  325. config->miiSpeed = kENET_MiiSpeed100M;
  326. config->miiDuplex = kENET_MiiFullDuplex;
  327. config->ringNum = 1;
  328. /* Sets the maximum receive frame length. */
  329. config->rxMaxFrameLen = ENET_FRAME_MAX_FRAMELEN;
  330. }
  331. void ENET_Init(ENET_Type *base,
  332. enet_handle_t *handle,
  333. const enet_config_t *config,
  334. const enet_buffer_config_t *bufferConfig,
  335. uint8_t *macAddr,
  336. uint32_t srcClock_Hz)
  337. {
  338. /* Checks input parameters. */
  339. assert(handle);
  340. assert(config);
  341. assert(bufferConfig);
  342. assert(macAddr);
  343. assert(config->ringNum <= FSL_FEATURE_ENET_QUEUE);
  344. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  345. uint32_t instance = ENET_GetInstance(base);
  346. /* Ungate ENET clock. */
  347. CLOCK_EnableClock(s_enetClock[instance]);
  348. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  349. /* Reset ENET module. */
  350. ENET_Reset(base);
  351. /* Initializes the ENET transmit buffer descriptors. */
  352. ENET_SetTxBufferDescriptors(handle, config, bufferConfig);
  353. /* Initializes the ENET receive buffer descriptors. */
  354. ENET_SetRxBufferDescriptors(handle, config, bufferConfig);
  355. /* Initializes the ENET MAC controller with basic function. */
  356. ENET_SetMacController(base, handle, config, bufferConfig, macAddr, srcClock_Hz);
  357. /* Set all buffers or data in handler for data transmit/receive process. */
  358. ENET_SetHandler(base, handle, config, bufferConfig);
  359. }
  360. void ENET_Deinit(ENET_Type *base)
  361. {
  362. /* Disable interrupt. */
  363. base->EIMR = 0;
  364. /* Disable ENET. */
  365. base->ECR &= ~ENET_ECR_ETHEREN_MASK;
  366. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  367. /* Disables the clock source. */
  368. CLOCK_DisableClock(s_enetClock[ENET_GetInstance(base)]);
  369. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  370. }
  371. void ENET_SetCallback(enet_handle_t *handle, enet_callback_t callback, void *userData)
  372. {
  373. assert(handle);
  374. /* Set callback and userData. */
  375. handle->callback = callback;
  376. handle->userData = userData;
  377. }
  378. static void ENET_SetHandler(ENET_Type *base,
  379. enet_handle_t *handle,
  380. const enet_config_t *config,
  381. const enet_buffer_config_t *bufferConfig)
  382. {
  383. uint8_t count;
  384. uint32_t instance = ENET_GetInstance(base);
  385. const enet_buffer_config_t *buffCfg = bufferConfig;
  386. /* Store transfer parameters in handle pointer. */
  387. memset(handle, 0, sizeof(enet_handle_t));
  388. handle->ringNum = (config->ringNum > FSL_FEATURE_ENET_QUEUE) ? FSL_FEATURE_ENET_QUEUE : config->ringNum;
  389. for (count = 0; count < handle->ringNum; count++)
  390. {
  391. assert(buffCfg->rxBuffSizeAlign * buffCfg->rxBdNumber > config->rxMaxFrameLen);
  392. handle->rxBdBase[count] = buffCfg->rxBdStartAddrAlign;
  393. handle->rxBdCurrent[count] = buffCfg->rxBdStartAddrAlign;
  394. handle->rxBuffSizeAlign[count] = buffCfg->rxBuffSizeAlign;
  395. handle->txBdBase[count] = buffCfg->txBdStartAddrAlign;
  396. handle->txBdCurrent[count] = buffCfg->txBdStartAddrAlign;
  397. handle->txBuffSizeAlign[count] = buffCfg->txBuffSizeAlign;
  398. buffCfg++;
  399. }
  400. /* Save the handle pointer in the global variables. */
  401. s_ENETHandle[instance] = handle;
  402. /* Set the IRQ handler when the interrupt is enabled. */
  403. if (config->interrupt & ENET_TX_INTERRUPT)
  404. {
  405. s_enetTxIsr = ENET_TransmitIRQHandler;
  406. EnableIRQ(s_enetTxIrqId[instance]);
  407. }
  408. if (config->interrupt & ENET_RX_INTERRUPT)
  409. {
  410. s_enetRxIsr = ENET_ReceiveIRQHandler;
  411. EnableIRQ(s_enetRxIrqId[instance]);
  412. }
  413. if (config->interrupt & ENET_ERR_INTERRUPT)
  414. {
  415. s_enetErrIsr = ENET_ErrorIRQHandler;
  416. EnableIRQ(s_enetErrIrqId[instance]);
  417. }
  418. }
  419. static void ENET_SetMacController(ENET_Type *base,
  420. enet_handle_t *handle,
  421. const enet_config_t *config,
  422. const enet_buffer_config_t *bufferConfig,
  423. uint8_t *macAddr,
  424. uint32_t srcClock_Hz)
  425. {
  426. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  427. /* Check the MII mode/speed/duplex setting. */
  428. if (config->miiSpeed == kENET_MiiSpeed1000M)
  429. {
  430. /* Only RGMII mode has the 1000M bit/s. The 1000M only support full duplex. */
  431. assert(config->miiMode == kENET_RgmiiMode);
  432. assert(config->miiDuplex == kENET_MiiFullDuplex);
  433. }
  434. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  435. uint32_t rcr = 0;
  436. uint32_t tcr = 0;
  437. uint32_t ecr = base->ECR;
  438. uint32_t macSpecialConfig = config->macSpecialConfig;
  439. uint32_t maxFrameLen = config->rxMaxFrameLen;
  440. /* Maximum frame length check. */
  441. if ((macSpecialConfig & kENET_ControlVLANTagEnable) && (maxFrameLen <= ENET_FRAME_MAX_FRAMELEN))
  442. {
  443. maxFrameLen = (ENET_FRAME_MAX_FRAMELEN + ENET_FRAME_VLAN_TAGLEN);
  444. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  445. if (macSpecialConfig & kENET_ControlSVLANEnable)
  446. {
  447. /* Double vlan tag (SVLAN) supported. */
  448. maxFrameLen += ENET_FRAME_VLAN_TAGLEN;
  449. }
  450. ecr |= ((macSpecialConfig & kENET_ControlSVLANEnable) ? (ENET_ECR_SVLANEN_MASK | ENET_ECR_SVLANDBL_MASK) : 0) |
  451. ((macSpecialConfig & kENET_ControlVLANUseSecondTag) ? ENET_ECR_VLANUSE2ND_MASK : 0);
  452. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  453. }
  454. /* Configures MAC receive controller with user configure structure. */
  455. rcr = ((macSpecialConfig & kENET_ControlRxPayloadCheckEnable) ? ENET_RCR_NLC_MASK : 0) |
  456. ((macSpecialConfig & kENET_ControlFlowControlEnable) ? ENET_RCR_CFEN_MASK : 0) |
  457. ((macSpecialConfig & kENET_ControlFlowControlEnable) ? ENET_RCR_FCE_MASK : 0) |
  458. ((macSpecialConfig & kENET_ControlRxPadRemoveEnable) ? ENET_RCR_PADEN_MASK : 0) |
  459. ((macSpecialConfig & kENET_ControlRxBroadCastRejectEnable) ? ENET_RCR_BC_REJ_MASK : 0) |
  460. ((macSpecialConfig & kENET_ControlPromiscuousEnable) ? ENET_RCR_PROM_MASK : 0) |
  461. ENET_RCR_MAX_FL(maxFrameLen) | ENET_RCR_CRCFWD_MASK;
  462. /* Set the RGMII or RMII, MII mode and control register. */
  463. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  464. if (config->miiMode == kENET_RgmiiMode)
  465. {
  466. rcr |= ENET_RCR_RGMII_EN_MASK;
  467. rcr &= ~ENET_RCR_MII_MODE_MASK;
  468. }
  469. else
  470. {
  471. rcr &= ~ENET_RCR_RGMII_EN_MASK;
  472. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  473. rcr |= ENET_RCR_MII_MODE_MASK;
  474. if (config->miiMode == kENET_RmiiMode)
  475. {
  476. rcr |= ENET_RCR_RMII_MODE_MASK;
  477. }
  478. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  479. }
  480. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  481. /* Speed. */
  482. if (config->miiSpeed == kENET_MiiSpeed10M)
  483. {
  484. rcr |= ENET_RCR_RMII_10T_MASK;
  485. }
  486. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  487. if (config->miiSpeed == kENET_MiiSpeed1000M)
  488. {
  489. ecr |= ENET_ECR_SPEED_MASK;
  490. }
  491. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  492. /* Receive setting for half duplex. */
  493. if (config->miiDuplex == kENET_MiiHalfDuplex)
  494. {
  495. rcr |= ENET_RCR_DRT_MASK;
  496. }
  497. /* Sets internal loop only for MII mode. */
  498. if ((config->macSpecialConfig & kENET_ControlMIILoopEnable) && (config->miiMode != kENET_RmiiMode))
  499. {
  500. rcr |= ENET_RCR_LOOP_MASK;
  501. rcr &= ~ENET_RCR_DRT_MASK;
  502. }
  503. base->RCR = rcr;
  504. /* Configures MAC transmit controller: duplex mode, mac address insertion. */
  505. tcr = base->TCR & ~(ENET_TCR_FDEN_MASK | ENET_TCR_ADDINS_MASK);
  506. tcr |= (config->miiDuplex ? ENET_TCR_FDEN_MASK : 0) |
  507. ((macSpecialConfig & kENET_ControlMacAddrInsert) ? ENET_TCR_ADDINS_MASK : 0);
  508. base->TCR = tcr;
  509. /* Configures receive and transmit accelerator. */
  510. base->TACC = config->txAccelerConfig;
  511. base->RACC = config->rxAccelerConfig;
  512. /* Sets the pause duration and FIFO threshold for the flow control enabled case. */
  513. if (macSpecialConfig & kENET_ControlFlowControlEnable)
  514. {
  515. uint32_t reemReg;
  516. base->OPD = config->pauseDuration;
  517. reemReg = ENET_RSEM_RX_SECTION_EMPTY(config->rxFifoEmptyThreshold);
  518. #if defined(FSL_FEATURE_ENET_HAS_RECEIVE_STATUS_THRESHOLD) && FSL_FEATURE_ENET_HAS_RECEIVE_STATUS_THRESHOLD
  519. reemReg |= ENET_RSEM_STAT_SECTION_EMPTY(config->rxFifoStatEmptyThreshold);
  520. #endif /* FSL_FEATURE_ENET_HAS_RECEIVE_STATUS_THRESHOLD */
  521. base->RSEM = reemReg;
  522. }
  523. /* FIFO threshold setting for store and forward enable/disable case. */
  524. if (macSpecialConfig & kENET_ControlStoreAndFwdDisable)
  525. {
  526. /* Transmit fifo watermark settings. */
  527. base->TFWR = config->txFifoWatermark & ENET_TFWR_TFWR_MASK;
  528. /* Receive fifo full threshold settings. */
  529. base->RSFL = config->rxFifoFullThreshold & ENET_RSFL_RX_SECTION_FULL_MASK;
  530. }
  531. else
  532. {
  533. /* Transmit fifo watermark settings. */
  534. base->TFWR = ENET_TFWR_STRFWD_MASK;
  535. base->RSFL = 0;
  536. }
  537. /* Enable store and forward when accelerator is enabled */
  538. if (config->txAccelerConfig & (kENET_TxAccelIpCheckEnabled | kENET_TxAccelProtoCheckEnabled))
  539. {
  540. base->TFWR = ENET_TFWR_STRFWD_MASK;
  541. }
  542. if (config->rxAccelerConfig & (kENET_RxAccelIpCheckEnabled | kENET_RxAccelProtoCheckEnabled))
  543. {
  544. base->RSFL = 0;
  545. }
  546. /* Initializes the ring 0. */
  547. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  548. base->TDSR = MEMORY_ConvertMemoryMapAddress((uint32_t)bufferConfig->txBdStartAddrAlign, kMEMORY_Local2DMA);
  549. base->RDSR = MEMORY_ConvertMemoryMapAddress((uint32_t)bufferConfig->rxBdStartAddrAlign, kMEMORY_Local2DMA);
  550. #else
  551. base->TDSR = (uint32_t)bufferConfig->txBdStartAddrAlign;
  552. base->RDSR = (uint32_t)bufferConfig->rxBdStartAddrAlign;
  553. #endif
  554. base->MRBR = bufferConfig->rxBuffSizeAlign;
  555. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  556. const enet_buffer_config_t *buffCfg = bufferConfig;
  557. if (config->ringNum > 1)
  558. {
  559. /* Initializes the ring 1. */
  560. buffCfg++;
  561. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  562. base->TDSR1 = MEMORY_ConvertMemoryMapAddress((uint32_t)buffCfg->txBdStartAddrAlign, kMEMORY_Local2DMA);
  563. base->RDSR1 = MEMORY_ConvertMemoryMapAddress((uint32_t)buffCfg->rxBdStartAddrAlign, kMEMORY_Local2DMA);
  564. #else
  565. base->TDSR1 = (uint32_t)buffCfg->txBdStartAddrAlign;
  566. base->RDSR1 = (uint32_t)buffCfg->rxBdStartAddrAlign;
  567. #endif
  568. base->MRBR1 = buffCfg->rxBuffSizeAlign;
  569. /* Enable the DMAC for ring 1 and with no rx classification set. */
  570. base->DMACFG[0] = ENET_DMACFG_DMA_CLASS_EN_MASK;
  571. }
  572. if (config->ringNum > 2)
  573. {
  574. /* Initializes the ring 2. */
  575. buffCfg++;
  576. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  577. base->TDSR2 = MEMORY_ConvertMemoryMapAddress((uint32_t)buffCfg->txBdStartAddrAlign, kMEMORY_Local2DMA);
  578. base->RDSR2 = MEMORY_ConvertMemoryMapAddress((uint32_t)buffCfg->rxBdStartAddrAlign, kMEMORY_Local2DMA);
  579. #else
  580. base->TDSR2 = (uint32_t)buffCfg->txBdStartAddrAlign;
  581. base->RDSR2 = (uint32_t)buffCfg->rxBdStartAddrAlign;
  582. #endif
  583. base->MRBR2 = buffCfg->rxBuffSizeAlign;
  584. /* Enable the DMAC for ring 2 and with no rx classification set. */
  585. base->DMACFG[1] = ENET_DMACFG_DMA_CLASS_EN_MASK;
  586. }
  587. /* Default the class/ring 1 and 2 are not enabled and the receive classification is disabled
  588. * so we set the default transmit scheme with the round-robin mode. beacuse the legacy bd mode
  589. * only support the round-robin mode. if the avb feature is required, just call the setup avb
  590. * feature API. */
  591. base->QOS |= ENET_QOS_TX_SCHEME(1);
  592. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  593. /* Configures the Mac address. */
  594. ENET_SetMacAddr(base, macAddr);
  595. /* Initialize the SMI if uninitialized. */
  596. if (!ENET_GetSMI(base))
  597. {
  598. ENET_SetSMI(base, srcClock_Hz, !!(config->macSpecialConfig & kENET_ControlSMIPreambleDisable));
  599. }
  600. /* Enables Ethernet interrupt, enables the interrupt coalsecing if it is required. */
  601. #if defined(FSL_FEATURE_ENET_HAS_INTERRUPT_COALESCE) && FSL_FEATURE_ENET_HAS_INTERRUPT_COALESCE
  602. if (config->intCoalesceCfg)
  603. {
  604. uint32_t intMask = (ENET_EIMR_TXB_MASK | ENET_EIMR_RXB_MASK);
  605. #if FSL_FEATURE_ENET_QUEUE > 1
  606. uint8_t queue = 0;
  607. intMask |= ENET_EIMR_TXB2_MASK | ENET_EIMR_RXB2_MASK | ENET_EIMR_TXB1_MASK | ENET_EIMR_RXB1_MASK;
  608. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  609. /* Clear all buffer interrupts. */
  610. base->EIMR &= ~intMask;
  611. /* Set the interrupt coalescence. */
  612. #if FSL_FEATURE_ENET_QUEUE > 1
  613. for (queue = 0; queue < FSL_FEATURE_ENET_QUEUE; queue++)
  614. {
  615. base->TXIC[queue] = ENET_TXIC_ICFT(config->intCoalesceCfg->txCoalesceFrameCount[queue]) |
  616. config->intCoalesceCfg->txCoalesceTimeCount[queue] | ENET_TXIC_ICCS_MASK |
  617. ENET_TXIC_ICEN_MASK;
  618. base->RXIC[queue] = ENET_RXIC_ICFT(config->intCoalesceCfg->rxCoalesceFrameCount[queue]) |
  619. config->intCoalesceCfg->rxCoalesceTimeCount[queue] | ENET_RXIC_ICCS_MASK |
  620. ENET_RXIC_ICEN_MASK;
  621. }
  622. #else
  623. base->TXIC = ENET_TXIC_ICFT(config->intCoalesceCfg->txCoalesceFrameCount[0]) |
  624. config->intCoalesceCfg->txCoalesceTimeCount[0] | ENET_TXIC_ICCS_MASK | ENET_TXIC_ICEN_MASK;
  625. base->RXIC = ENET_RXIC_ICFT(config->intCoalesceCfg->rxCoalesceFrameCount[0]) |
  626. config->intCoalesceCfg->rxCoalesceTimeCount[0] | ENET_RXIC_ICCS_MASK | ENET_RXIC_ICEN_MASK;
  627. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  628. }
  629. #endif /* FSL_FEATURE_ENET_HAS_INTERRUPT_COALESCE */
  630. ENET_EnableInterrupts(base, config->interrupt);
  631. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  632. /* Sets the 1588 enhanced feature. */
  633. ecr |= ENET_ECR_EN1588_MASK;
  634. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  635. /* Enables Ethernet module after all configuration except the buffer descriptor active. */
  636. ecr |= ENET_ECR_ETHEREN_MASK | ENET_ECR_DBSWP_MASK;
  637. base->ECR = ecr;
  638. }
  639. static void ENET_SetTxBufferDescriptors(enet_handle_t *handle, const enet_config_t *config, const enet_buffer_config_t *bufferConfig)
  640. {
  641. assert(config);
  642. assert(bufferConfig);
  643. /* Default single ring is supported. */
  644. uint8_t ringNum;
  645. uint32_t count;
  646. uint32_t txBuffSizeAlign;
  647. uint8_t *txBuffer;
  648. const enet_buffer_config_t *buffCfg = bufferConfig;
  649. /* Check the input parameters. */
  650. for (ringNum = 0; ringNum < config->ringNum; ringNum++)
  651. {
  652. if ((buffCfg->txBdStartAddrAlign > 0) && (buffCfg->txBufferAlign > 0))
  653. {
  654. volatile enet_tx_bd_struct_t *curBuffDescrip = buffCfg->txBdStartAddrAlign;
  655. txBuffSizeAlign = buffCfg->txBuffSizeAlign;
  656. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  657. txBuffer = (uint8_t *)MEMORY_ConvertMemoryMapAddress((uint32_t)buffCfg->txBufferAlign, kMEMORY_Local2DMA);
  658. #else
  659. txBuffer = buffCfg->txBufferAlign;
  660. #endif
  661. for (count = 0; count < buffCfg->txBdNumber; count++)
  662. {
  663. /* Set data buffer address. */
  664. curBuffDescrip->buffer = (uint8_t *)((uint32_t)&txBuffer[count * txBuffSizeAlign]);
  665. /* Initializes data length. */
  666. curBuffDescrip->length = 0;
  667. /* Sets the crc. */
  668. curBuffDescrip->control = ENET_BUFFDESCRIPTOR_TX_TRANMITCRC_MASK;
  669. /* Sets the last buffer descriptor with the wrap flag. */
  670. if (count == buffCfg->txBdNumber - 1)
  671. {
  672. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_WRAP_MASK;
  673. }
  674. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  675. /* Enable transmit interrupt for store the transmit timestamp. */
  676. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_INTERRUPT_MASK;
  677. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  678. /* Set the type of the frame when the credit-based scheme is used. */
  679. curBuffDescrip->controlExtend1 |= ENET_BD_FTYPE(ringNum);
  680. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  681. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  682. /* Increase the index. */
  683. curBuffDescrip++;
  684. }
  685. }
  686. buffCfg++;
  687. }
  688. }
  689. static void ENET_SetRxBufferDescriptors(enet_handle_t *handle, const enet_config_t *config, const enet_buffer_config_t *bufferConfig)
  690. {
  691. assert(config);
  692. assert(bufferConfig);
  693. /* Default single ring is supported. */
  694. uint8_t ringNum;
  695. uint32_t count;
  696. uint32_t rxBuffSizeAlign;
  697. uint8_t *rxBuffer;
  698. const enet_buffer_config_t *buffCfg = bufferConfig;
  699. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  700. uint32_t mask = (kENET_RxFrameInterrupt | kENET_RxBufferInterrupt);
  701. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  702. /* Check the input parameters. */
  703. for (ringNum = 0; ringNum < config->ringNum; ringNum++)
  704. {
  705. assert(buffCfg->rxBuffSizeAlign >= ENET_RX_MIN_BUFFERSIZE);
  706. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  707. #if FSL_FEATURE_ENET_QUEUE > 1
  708. if (ringNum == 1)
  709. {
  710. mask = (kENET_RxFrame1Interrupt | kENET_RxBuffer1Interrupt);
  711. }
  712. else if (ringNum == 2)
  713. {
  714. mask = (kENET_RxFrame2Interrupt | kENET_RxBuffer2Interrupt);
  715. }
  716. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  717. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  718. if ((buffCfg->rxBdStartAddrAlign > 0) && (buffCfg->rxBufferAlign > 0))
  719. {
  720. volatile enet_rx_bd_struct_t *curBuffDescrip = buffCfg->rxBdStartAddrAlign;
  721. rxBuffSizeAlign = buffCfg->rxBuffSizeAlign;
  722. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  723. rxBuffer = (uint8_t *)MEMORY_ConvertMemoryMapAddress((uint32_t)buffCfg->rxBufferAlign, kMEMORY_Local2DMA);
  724. #else
  725. rxBuffer = buffCfg->rxBufferAlign;
  726. #endif
  727. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  728. /* Invalidate rx buffers before DMA transfer data into them. */
  729. DCACHE_InvalidateByRange((uint32_t)rxBuffer, (buffCfg->rxBdNumber * rxBuffSizeAlign));
  730. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  731. for (count = 0; count < buffCfg->rxBdNumber; count++)
  732. {
  733. /* Set data buffer and the length. */
  734. curBuffDescrip->buffer = (uint8_t *)((uint32_t)&rxBuffer[count * rxBuffSizeAlign]);
  735. curBuffDescrip->length = 0;
  736. /* Initializes the buffer descriptors with empty bit. */
  737. curBuffDescrip->control = ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK;
  738. /* Sets the last buffer descriptor with the wrap flag. */
  739. if (count == buffCfg->rxBdNumber - 1)
  740. {
  741. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_RX_WRAP_MASK;
  742. }
  743. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  744. if (config->interrupt & mask)
  745. {
  746. /* Enable receive interrupt. */
  747. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_RX_INTERRUPT_MASK;
  748. }
  749. else
  750. {
  751. curBuffDescrip->controlExtend1 = 0;
  752. }
  753. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  754. /* Increase the index. */
  755. curBuffDescrip++;
  756. }
  757. }
  758. buffCfg++;
  759. }
  760. }
  761. static void ENET_ActiveSend(ENET_Type *base, uint32_t ringId)
  762. {
  763. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  764. switch (ringId)
  765. {
  766. case kENET_Ring0:
  767. base->TDAR = ENET_TDAR_TDAR_MASK;
  768. break;
  769. #if FSL_FEATURE_ENET_QUEUE > 1
  770. case kENET_Ring1:
  771. base->TDAR1 = ENET_TDAR1_TDAR_MASK;
  772. break;
  773. case kENET_Ring2:
  774. base->TDAR2 = ENET_TDAR2_TDAR_MASK;
  775. break;
  776. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  777. default:
  778. base->TDAR = ENET_TDAR_TDAR_MASK;
  779. break;
  780. }
  781. }
  782. void ENET_SetMII(ENET_Type *base, enet_mii_speed_t speed, enet_mii_duplex_t duplex)
  783. {
  784. uint32_t rcr = base->RCR;
  785. uint32_t tcr = base->TCR;
  786. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  787. uint32_t ecr = base->ECR;
  788. if (kENET_MiiSpeed1000M == speed)
  789. {
  790. assert(duplex == kENET_MiiFullDuplex);
  791. ecr |= ENET_ECR_SPEED_MASK;
  792. }
  793. else
  794. {
  795. ecr &= ~ENET_ECR_SPEED_MASK;
  796. }
  797. base->ECR = ecr;
  798. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  799. /* Sets speed mode. */
  800. if (kENET_MiiSpeed10M == speed)
  801. {
  802. rcr |= ENET_RCR_RMII_10T_MASK;
  803. }
  804. else
  805. {
  806. rcr &= ~ENET_RCR_RMII_10T_MASK;
  807. }
  808. /* Set duplex mode. */
  809. if (duplex == kENET_MiiHalfDuplex)
  810. {
  811. rcr |= ENET_RCR_DRT_MASK;
  812. tcr &= ~ENET_TCR_FDEN_MASK;
  813. }
  814. else
  815. {
  816. rcr &= ~ENET_RCR_DRT_MASK;
  817. tcr |= ENET_TCR_FDEN_MASK;
  818. }
  819. base->RCR = rcr;
  820. base->TCR = tcr;
  821. }
  822. void ENET_SetMacAddr(ENET_Type *base, uint8_t *macAddr)
  823. {
  824. uint32_t address;
  825. /* Set physical address lower register. */
  826. address = (uint32_t)(((uint32_t)macAddr[0] << 24U) | ((uint32_t)macAddr[1] << 16U) | ((uint32_t)macAddr[2] << 8U) |
  827. (uint32_t)macAddr[3]);
  828. base->PALR = address;
  829. /* Set physical address high register. */
  830. address = (uint32_t)(((uint32_t)macAddr[4] << 8U) | ((uint32_t)macAddr[5]));
  831. base->PAUR = address << ENET_PAUR_PADDR2_SHIFT;
  832. }
  833. void ENET_GetMacAddr(ENET_Type *base, uint8_t *macAddr)
  834. {
  835. assert(macAddr);
  836. uint32_t address;
  837. /* Get from physical address lower register. */
  838. address = base->PALR;
  839. macAddr[0] = 0xFFU & (address >> 24U);
  840. macAddr[1] = 0xFFU & (address >> 16U);
  841. macAddr[2] = 0xFFU & (address >> 8U);
  842. macAddr[3] = 0xFFU & address;
  843. /* Get from physical address high register. */
  844. address = (base->PAUR & ENET_PAUR_PADDR2_MASK) >> ENET_PAUR_PADDR2_SHIFT;
  845. macAddr[4] = 0xFFU & (address >> 8U);
  846. macAddr[5] = 0xFFU & address;
  847. }
  848. void ENET_SetSMI(ENET_Type *base, uint32_t srcClock_Hz, bool isPreambleDisabled)
  849. {
  850. assert(srcClock_Hz);
  851. uint32_t clkCycle = 0;
  852. uint32_t speed = 0;
  853. uint32_t mscr = 0;
  854. /* Calculate the MII speed which controls the frequency of the MDC. */
  855. speed = srcClock_Hz / (2 * ENET_MDC_FREQUENCY);
  856. /* Calculate the hold time on the MDIO output. */
  857. clkCycle = (10 + ENET_NANOSECOND_ONE_SECOND / srcClock_Hz - 1) / (ENET_NANOSECOND_ONE_SECOND / srcClock_Hz) - 1;
  858. /* Build the configuration for MDC/MDIO control. */
  859. mscr = ENET_MSCR_MII_SPEED(speed) | ENET_MSCR_HOLDTIME(clkCycle) | (isPreambleDisabled ? ENET_MSCR_DIS_PRE_MASK : 0);
  860. base->MSCR = mscr;
  861. }
  862. void ENET_StartSMIWrite(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, enet_mii_write_t operation, uint32_t data)
  863. {
  864. uint32_t mmfr = 0;
  865. /* Build MII write command. */
  866. mmfr = ENET_MMFR_ST(1) | ENET_MMFR_OP(operation) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(phyReg) | ENET_MMFR_TA(2) |
  867. (data & 0xFFFF);
  868. base->MMFR = mmfr;
  869. }
  870. void ENET_StartSMIRead(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, enet_mii_read_t operation)
  871. {
  872. uint32_t mmfr = 0;
  873. /* Build MII read command. */
  874. mmfr = ENET_MMFR_ST(1) | ENET_MMFR_OP(operation) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(phyReg) | ENET_MMFR_TA(2);
  875. base->MMFR = mmfr;
  876. }
  877. #if defined(FSL_FEATURE_ENET_HAS_EXTEND_MDIO) && FSL_FEATURE_ENET_HAS_EXTEND_MDIO
  878. void ENET_StartExtC45SMIWrite(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t data)
  879. {
  880. uint32_t mmfr = 0;
  881. /* Parse the address from the input register. */
  882. uint16_t devAddr = (phyReg >> ENET_MMFR_TA_SHIFT) & 0x1FU;
  883. uint16_t regAddr = (uint16_t)(phyReg & 0xFFFFU);
  884. /* Address write firstly. */
  885. mmfr = ENET_MMFR_ST(0) | ENET_MMFR_OP(kENET_MiiAddrWrite_C45) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(devAddr) |
  886. ENET_MMFR_TA(2) | ENET_MMFR_DATA(regAddr);
  887. base->MMFR = mmfr;
  888. /* Build MII write command. */
  889. mmfr = ENET_MMFR_ST(0) | ENET_MMFR_OP(kENET_MiiWriteFrame_C45) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(devAddr) |
  890. ENET_MMFR_TA(2) | ENET_MMFR_DATA(data);
  891. base->MMFR = mmfr;
  892. }
  893. void ENET_StartExtC45SMIRead(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg)
  894. {
  895. uint32_t mmfr = 0;
  896. /* Parse the address from the input register. */
  897. uint16_t devAddr = (phyReg >> ENET_MMFR_TA_SHIFT) & 0x1FU;
  898. uint16_t regAddr = (uint16_t)(phyReg & 0xFFFFU);
  899. /* Address write firstly. */
  900. mmfr = ENET_MMFR_ST(0) | ENET_MMFR_OP(kENET_MiiAddrWrite_C45) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(devAddr) |
  901. ENET_MMFR_TA(2) | ENET_MMFR_DATA(regAddr);
  902. base->MMFR = mmfr;
  903. /* Build MII read command. */
  904. mmfr = ENET_MMFR_ST(0) | ENET_MMFR_OP(kENET_MiiReadFrame_C45) | ENET_MMFR_PA(phyAddr) | ENET_MMFR_RA(devAddr) |
  905. ENET_MMFR_TA(2);
  906. base->MMFR = mmfr;
  907. }
  908. #endif /* FSL_FEATURE_ENET_HAS_EXTEND_MDIO */
  909. void ENET_GetRxErrBeforeReadFrame(enet_handle_t *handle, enet_data_error_stats_t *eErrorStatic)
  910. {
  911. assert(handle);
  912. assert(handle->rxBdCurrent[0]);
  913. assert(eErrorStatic);
  914. uint16_t control = 0;
  915. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent[0];
  916. do
  917. {
  918. /* The last buffer descriptor of a frame. */
  919. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  920. {
  921. control = curBuffDescrip->control;
  922. if (control & ENET_BUFFDESCRIPTOR_RX_TRUNC_MASK)
  923. {
  924. /* The receive truncate error. */
  925. eErrorStatic->statsRxTruncateErr++;
  926. }
  927. if (control & ENET_BUFFDESCRIPTOR_RX_OVERRUN_MASK)
  928. {
  929. /* The receive over run error. */
  930. eErrorStatic->statsRxOverRunErr++;
  931. }
  932. if (control & ENET_BUFFDESCRIPTOR_RX_LENVLIOLATE_MASK)
  933. {
  934. /* The receive length violation error. */
  935. eErrorStatic->statsRxLenGreaterErr++;
  936. }
  937. if (control & ENET_BUFFDESCRIPTOR_RX_NOOCTET_MASK)
  938. {
  939. /* The receive alignment error. */
  940. eErrorStatic->statsRxAlignErr++;
  941. }
  942. if (control & ENET_BUFFDESCRIPTOR_RX_CRC_MASK)
  943. {
  944. /* The receive CRC error. */
  945. eErrorStatic->statsRxFcsErr++;
  946. }
  947. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  948. uint16_t controlExt = curBuffDescrip->controlExtend1;
  949. if (controlExt & ENET_BUFFDESCRIPTOR_RX_MACERR_MASK)
  950. {
  951. /* The MAC error. */
  952. eErrorStatic->statsRxMacErr++;
  953. }
  954. if (controlExt & ENET_BUFFDESCRIPTOR_RX_PHYERR_MASK)
  955. {
  956. /* The PHY error. */
  957. eErrorStatic->statsRxPhyErr++;
  958. }
  959. if (controlExt & ENET_BUFFDESCRIPTOR_RX_COLLISION_MASK)
  960. {
  961. /* The receive collision error. */
  962. eErrorStatic->statsRxCollisionErr++;
  963. }
  964. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  965. break;
  966. }
  967. /* Increase the buffer descriptor, if it is the last one, increase to first one of the ring buffer. */
  968. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  969. {
  970. curBuffDescrip = handle->rxBdBase[0];
  971. }
  972. else
  973. {
  974. curBuffDescrip++;
  975. }
  976. } while (curBuffDescrip != handle->rxBdCurrent[0]);
  977. }
  978. status_t ENET_GetRxFrameSize(enet_handle_t *handle, uint32_t *length)
  979. {
  980. assert(handle);
  981. assert(handle->rxBdCurrent[0]);
  982. assert(length);
  983. /* Reset the length to zero. */
  984. *length = 0;
  985. uint16_t validLastMask = ENET_BUFFDESCRIPTOR_RX_LAST_MASK | ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK;
  986. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent[0];
  987. /* Check the current buffer descriptor's empty flag. if empty means there is no frame received. */
  988. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK)
  989. {
  990. return kStatus_ENET_RxFrameEmpty;
  991. }
  992. do
  993. {
  994. /* Add check for abnormal case. */
  995. if ((!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK)) && (!curBuffDescrip->length))
  996. {
  997. return kStatus_ENET_RxFrameError;
  998. }
  999. /* Find the last buffer descriptor. */
  1000. if ((curBuffDescrip->control & validLastMask) == ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1001. {
  1002. /* The last buffer descriptor in the frame check the status of the received frame. */
  1003. if ((curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_ERR_MASK)
  1004. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1005. || (curBuffDescrip->controlExtend1 & ENET_BUFFDESCRIPTOR_RX_EXT_ERR_MASK)
  1006. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1007. )
  1008. {
  1009. return kStatus_ENET_RxFrameError;
  1010. }
  1011. /* FCS is removed by MAC. */
  1012. *length = curBuffDescrip->length;
  1013. return kStatus_Success;
  1014. }
  1015. /* Increase the buffer descriptor, if it is the last one, increase to first one of the ring buffer. */
  1016. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  1017. {
  1018. curBuffDescrip = handle->rxBdBase[0];
  1019. }
  1020. else
  1021. {
  1022. curBuffDescrip++;
  1023. }
  1024. } while (curBuffDescrip != handle->rxBdCurrent[0]);
  1025. /* The frame is on processing - set to empty status to make application to receive it next time. */
  1026. return kStatus_ENET_RxFrameEmpty;
  1027. }
  1028. status_t ENET_ReadFrame(ENET_Type *base, enet_handle_t *handle, uint8_t *data, uint32_t length)
  1029. {
  1030. assert(handle);
  1031. assert(handle->rxBdCurrent[0]);
  1032. uint32_t len = 0;
  1033. uint32_t offset = 0;
  1034. uint16_t control;
  1035. bool isLastBuff = false;
  1036. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent[0];
  1037. status_t result = kStatus_Success;
  1038. uint32_t address;
  1039. /* For data-NULL input, only update the buffer descriptor. */
  1040. if (!data)
  1041. {
  1042. do
  1043. {
  1044. /* Update the control flag. */
  1045. control = handle->rxBdCurrent[0]->control;
  1046. /* Updates the receive buffer descriptors. */
  1047. ENET_UpdateReadBuffers(base, handle, 0);
  1048. /* Find the last buffer descriptor for the frame. */
  1049. if (control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1050. {
  1051. break;
  1052. }
  1053. } while (handle->rxBdCurrent[0] != curBuffDescrip);
  1054. return result;
  1055. }
  1056. else
  1057. {
  1058. /* A frame on one buffer or several receive buffers are both considered. */
  1059. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1060. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1061. #else
  1062. address = (uint32_t)curBuffDescrip->buffer;
  1063. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1064. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1065. /* Add the cache invalidate maintain. */
  1066. DCACHE_InvalidateByRange(address, handle->rxBuffSizeAlign[0]);
  1067. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1068. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1069. enet_ptp_time_data_t ptpTimestamp;
  1070. bool isPtpEventMessage = false;
  1071. /* Parse the PTP message according to the header message. */
  1072. isPtpEventMessage = ENET_Ptp1588ParseFrame((uint8_t *)address, &ptpTimestamp, false);
  1073. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1074. while (!isLastBuff)
  1075. {
  1076. /* The last buffer descriptor of a frame. */
  1077. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1078. {
  1079. /* This is a valid frame. */
  1080. isLastBuff = true;
  1081. if (length == curBuffDescrip->length)
  1082. {
  1083. /* Copy the frame to user's buffer without FCS. */
  1084. len = curBuffDescrip->length - offset;
  1085. memcpy(data + offset, (void *)address, len);
  1086. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1087. /* Store the PTP 1588 timestamp for received PTP event frame. */
  1088. if (isPtpEventMessage)
  1089. {
  1090. /* Set the timestamp to the timestamp ring. */
  1091. ptpTimestamp.timeStamp.nanosecond = curBuffDescrip->timestamp;
  1092. result = ENET_StoreRxFrameTime(base, handle, &ptpTimestamp);
  1093. }
  1094. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1095. /* Updates the receive buffer descriptors. */
  1096. ENET_UpdateReadBuffers(base, handle, 0);
  1097. return result;
  1098. }
  1099. else
  1100. {
  1101. /* Updates the receive buffer descriptors. */
  1102. ENET_UpdateReadBuffers(base, handle, 0);
  1103. }
  1104. }
  1105. else
  1106. {
  1107. /* Store a frame on several buffer descriptors. */
  1108. isLastBuff = false;
  1109. /* Length check. */
  1110. if (offset >= length)
  1111. {
  1112. break;
  1113. }
  1114. memcpy(data + offset, (void *)address, handle->rxBuffSizeAlign[0]);
  1115. offset += handle->rxBuffSizeAlign[0];
  1116. /* Updates the receive buffer descriptors. */
  1117. ENET_UpdateReadBuffers(base, handle, 0);
  1118. }
  1119. /* Get the current buffer descriptor. */
  1120. curBuffDescrip = handle->rxBdCurrent[0];
  1121. /* Add the cache invalidate maintain. */
  1122. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1123. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1124. #else
  1125. address = (uint32_t)curBuffDescrip->buffer;
  1126. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1127. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1128. DCACHE_InvalidateByRange(address, handle->rxBuffSizeAlign[0]);
  1129. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1130. }
  1131. }
  1132. return kStatus_ENET_RxFrameFail;
  1133. }
  1134. static void ENET_UpdateReadBuffers(ENET_Type *base, enet_handle_t *handle, uint32_t ringId)
  1135. {
  1136. assert(handle);
  1137. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1138. /* Clears status. */
  1139. handle->rxBdCurrent[ringId]->control &= ENET_BUFFDESCRIPTOR_RX_WRAP_MASK;
  1140. /* Sets the receive buffer descriptor with the empty flag. */
  1141. handle->rxBdCurrent[ringId]->control |= ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK;
  1142. /* Increase current buffer descriptor to the next one. */
  1143. if (handle->rxBdCurrent[ringId]->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  1144. {
  1145. handle->rxBdCurrent[ringId] = handle->rxBdBase[ringId];
  1146. }
  1147. else
  1148. {
  1149. handle->rxBdCurrent[ringId]++;
  1150. }
  1151. /* Actives the receive buffer descriptor. */
  1152. switch (ringId)
  1153. {
  1154. case kENET_Ring0:
  1155. base->RDAR = ENET_RDAR_RDAR_MASK;
  1156. break;
  1157. #if FSL_FEATURE_ENET_QUEUE > 1
  1158. case kENET_Ring1:
  1159. base->RDAR1 = ENET_RDAR1_RDAR_MASK;
  1160. break;
  1161. case kENET_Ring2:
  1162. base->RDAR2 = ENET_RDAR2_RDAR_MASK;
  1163. break;
  1164. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  1165. default:
  1166. base->RDAR = ENET_RDAR_RDAR_MASK;
  1167. break;
  1168. }
  1169. }
  1170. status_t ENET_SendFrame(ENET_Type *base, enet_handle_t *handle, const uint8_t *data, uint32_t length)
  1171. {
  1172. assert(handle);
  1173. assert(data);
  1174. volatile enet_tx_bd_struct_t *curBuffDescrip;
  1175. uint32_t len = 0;
  1176. uint32_t sizeleft = 0;
  1177. uint32_t address;
  1178. /* Check the frame length. */
  1179. if (length > ENET_FRAME_MAX_FRAMELEN)
  1180. {
  1181. return kStatus_ENET_TxFrameOverLen;
  1182. }
  1183. /* Check if the transmit buffer is ready. */
  1184. curBuffDescrip = handle->txBdCurrent[0];
  1185. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1186. {
  1187. return kStatus_ENET_TxFrameBusy;
  1188. }
  1189. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1190. bool isPtpEventMessage = false;
  1191. /* Check PTP message with the PTP header. */
  1192. isPtpEventMessage = ENET_Ptp1588ParseFrame(data, NULL, true);
  1193. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1194. /* One transmit buffer is enough for one frame. */
  1195. if (handle->txBuffSizeAlign[0] >= length)
  1196. {
  1197. /* Copy data to the buffer for uDMA transfer. */
  1198. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1199. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1200. #else
  1201. address = (uint32_t)curBuffDescrip->buffer;
  1202. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1203. memcpy((void *)address, data, length);
  1204. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1205. DCACHE_CleanByRange(address, length);
  1206. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1207. /* Set data length. */
  1208. curBuffDescrip->length = length;
  1209. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1210. /* For enable the timestamp. */
  1211. if (isPtpEventMessage)
  1212. {
  1213. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1214. }
  1215. else
  1216. {
  1217. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1218. }
  1219. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1220. curBuffDescrip->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
  1221. /* Increase the buffer descriptor address. */
  1222. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1223. {
  1224. handle->txBdCurrent[0] = handle->txBdBase[0];
  1225. }
  1226. else
  1227. {
  1228. handle->txBdCurrent[0]++;
  1229. }
  1230. /* Active the transmit buffer descriptor. */
  1231. ENET_ActiveSend(base, 0);
  1232. return kStatus_Success;
  1233. }
  1234. else
  1235. {
  1236. /* One frame requires more than one transmit buffers. */
  1237. do
  1238. {
  1239. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1240. /* For enable the timestamp. */
  1241. if (isPtpEventMessage)
  1242. {
  1243. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1244. }
  1245. else
  1246. {
  1247. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1248. }
  1249. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1250. /* Increase the buffer descriptor address. */
  1251. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1252. {
  1253. handle->txBdCurrent[0] = handle->txBdBase[0];
  1254. }
  1255. else
  1256. {
  1257. handle->txBdCurrent[0]++;
  1258. }
  1259. /* update the size left to be transmit. */
  1260. sizeleft = length - len;
  1261. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1262. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1263. #else
  1264. address = (uint32_t)curBuffDescrip->buffer;
  1265. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1266. if (sizeleft > handle->txBuffSizeAlign[0])
  1267. {
  1268. /* Data copy. */
  1269. memcpy((void *)address, data + len, handle->txBuffSizeAlign[0]);
  1270. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1271. /* Add the cache clean maintain. */
  1272. DCACHE_CleanByRange(address, handle->txBuffSizeAlign[0]);
  1273. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1274. /* Data length update. */
  1275. curBuffDescrip->length = handle->txBuffSizeAlign[0];
  1276. len += handle->txBuffSizeAlign[0];
  1277. /* Sets the control flag. */
  1278. curBuffDescrip->control &= ~ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  1279. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK;
  1280. /* Active the transmit buffer descriptor*/
  1281. ENET_ActiveSend(base, 0);
  1282. }
  1283. else
  1284. {
  1285. memcpy((void *)address, data + len, sizeleft);
  1286. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1287. /* Add the cache clean maintain. */
  1288. DCACHE_CleanByRange(address, sizeleft);
  1289. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1290. curBuffDescrip->length = sizeleft;
  1291. /* Set Last buffer wrap flag. */
  1292. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  1293. /* Active the transmit buffer descriptor. */
  1294. ENET_ActiveSend(base, 0);
  1295. return kStatus_Success;
  1296. }
  1297. /* Get the current buffer descriptor address. */
  1298. curBuffDescrip = handle->txBdCurrent[0];
  1299. } while (!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK));
  1300. return kStatus_ENET_TxFrameBusy;
  1301. }
  1302. }
  1303. #if FSL_FEATURE_ENET_QUEUE > 1
  1304. void ENET_GetRxErrBeforeReadFrameMultiRing(enet_handle_t *handle,
  1305. enet_data_error_stats_t *eErrorStatic,
  1306. uint32_t ringId)
  1307. {
  1308. assert(handle);
  1309. assert(eErrorStatic);
  1310. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1311. uint16_t control = 0;
  1312. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent[ringId];
  1313. do
  1314. {
  1315. /* The last buffer descriptor of a frame. */
  1316. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1317. {
  1318. control = curBuffDescrip->control;
  1319. if (control & ENET_BUFFDESCRIPTOR_RX_TRUNC_MASK)
  1320. {
  1321. /* The receive truncate error. */
  1322. eErrorStatic->statsRxTruncateErr++;
  1323. }
  1324. if (control & ENET_BUFFDESCRIPTOR_RX_OVERRUN_MASK)
  1325. {
  1326. /* The receive over run error. */
  1327. eErrorStatic->statsRxOverRunErr++;
  1328. }
  1329. if (control & ENET_BUFFDESCRIPTOR_RX_LENVLIOLATE_MASK)
  1330. {
  1331. /* The receive length violation error. */
  1332. eErrorStatic->statsRxLenGreaterErr++;
  1333. }
  1334. if (control & ENET_BUFFDESCRIPTOR_RX_NOOCTET_MASK)
  1335. {
  1336. /* The receive alignment error. */
  1337. eErrorStatic->statsRxAlignErr++;
  1338. }
  1339. if (control & ENET_BUFFDESCRIPTOR_RX_CRC_MASK)
  1340. {
  1341. /* The receive CRC error. */
  1342. eErrorStatic->statsRxFcsErr++;
  1343. }
  1344. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1345. uint16_t controlExt = curBuffDescrip->controlExtend1;
  1346. if (controlExt & ENET_BUFFDESCRIPTOR_RX_MACERR_MASK)
  1347. {
  1348. /* The MAC error. */
  1349. eErrorStatic->statsRxMacErr++;
  1350. }
  1351. if (controlExt & ENET_BUFFDESCRIPTOR_RX_PHYERR_MASK)
  1352. {
  1353. /* The PHY error. */
  1354. eErrorStatic->statsRxPhyErr++;
  1355. }
  1356. if (controlExt & ENET_BUFFDESCRIPTOR_RX_COLLISION_MASK)
  1357. {
  1358. /* The receive collision error. */
  1359. eErrorStatic->statsRxCollisionErr++;
  1360. }
  1361. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1362. break;
  1363. }
  1364. /* Increase the buffer descriptor, if it is the last one, increase to first one of the ring buffer. */
  1365. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  1366. {
  1367. curBuffDescrip = handle->rxBdBase[ringId];
  1368. }
  1369. else
  1370. {
  1371. curBuffDescrip++;
  1372. }
  1373. } while (curBuffDescrip != handle->rxBdCurrent[ringId]);
  1374. }
  1375. status_t ENET_GetRxFrameSizeMultiRing(enet_handle_t *handle, uint32_t *length, uint32_t ringId)
  1376. {
  1377. assert(handle);
  1378. assert(length);
  1379. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1380. /* Reset the length to zero. */
  1381. *length = 0;
  1382. uint16_t validLastMask = ENET_BUFFDESCRIPTOR_RX_LAST_MASK | ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK;
  1383. volatile enet_rx_bd_struct_t *curBuffDescrip;
  1384. curBuffDescrip = handle->rxBdCurrent[ringId];
  1385. /* Check the current buffer descriptor's empty flag. if empty means there is no frame received. */
  1386. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK)
  1387. {
  1388. return kStatus_ENET_RxFrameEmpty;
  1389. }
  1390. do
  1391. {
  1392. /* Add check for abnormal case. */
  1393. if ((!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK)) && (!curBuffDescrip->length))
  1394. {
  1395. return kStatus_ENET_RxFrameError;
  1396. }
  1397. /* Find the last buffer descriptor. */
  1398. if ((curBuffDescrip->control & validLastMask) == ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1399. {
  1400. /* The last buffer descriptor in the frame check the status of the received frame. */
  1401. if ((curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_ERR_MASK)
  1402. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1403. || (curBuffDescrip->controlExtend1 & ENET_BUFFDESCRIPTOR_RX_EXT_ERR_MASK)
  1404. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1405. )
  1406. {
  1407. return kStatus_ENET_RxFrameError;
  1408. }
  1409. /* FCS is removed by MAC. */
  1410. *length = curBuffDescrip->length;
  1411. return kStatus_Success;
  1412. }
  1413. /* Increase the buffer descriptor, if it is the last one, increase to first one of the ring buffer. */
  1414. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  1415. {
  1416. curBuffDescrip = handle->rxBdBase[ringId];
  1417. }
  1418. else
  1419. {
  1420. curBuffDescrip++;
  1421. }
  1422. } while (curBuffDescrip != handle->rxBdCurrent[ringId]);
  1423. /* The frame is on processing - set to empty status to make application to receive it next time. */
  1424. return kStatus_ENET_RxFrameEmpty;
  1425. }
  1426. status_t ENET_ReadFrameMultiRing(
  1427. ENET_Type *base, enet_handle_t *handle, uint8_t *data, uint32_t length, uint32_t ringId)
  1428. {
  1429. assert(handle);
  1430. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1431. uint32_t len = 0;
  1432. uint32_t offset = 0;
  1433. uint16_t control;
  1434. bool isLastBuff = false;
  1435. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent[ringId];
  1436. status_t result = kStatus_Success;
  1437. uint32_t address;
  1438. /* For data-NULL input, only update the buffer descriptor. */
  1439. if (!data)
  1440. {
  1441. do
  1442. {
  1443. /* Update the control flag. */
  1444. control = handle->rxBdCurrent[ringId]->control;
  1445. /* Updates the receive buffer descriptors. */
  1446. ENET_UpdateReadBuffers(base, handle, ringId);
  1447. /* Find the last buffer descriptor for the frame. */
  1448. if (control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1449. {
  1450. break;
  1451. }
  1452. } while (handle->rxBdCurrent[ringId] != curBuffDescrip);
  1453. return result;
  1454. }
  1455. else
  1456. {
  1457. /* A frame on one buffer or several receive buffers are both considered. */
  1458. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1459. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1460. #else
  1461. address = (uint32_t)curBuffDescrip->buffer;
  1462. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1463. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1464. /* Add the cache invalidate maintain. */
  1465. DCACHE_InvalidateByRange(address, handle->rxBuffSizeAlign[ringId]);
  1466. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1467. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1468. enet_ptp_time_data_t ptpTimestamp;
  1469. bool isPtpEventMessage = false;
  1470. /* Parse the PTP message according to the header message. */
  1471. isPtpEventMessage = ENET_Ptp1588ParseFrame((uint8_t *)address, &ptpTimestamp, false);
  1472. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1473. while (!isLastBuff)
  1474. {
  1475. /* The last buffer descriptor of a frame. */
  1476. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1477. {
  1478. /* This is a valid frame. */
  1479. isLastBuff = true;
  1480. if (length == curBuffDescrip->length)
  1481. {
  1482. /* Copy the frame to user's buffer without FCS. */
  1483. len = curBuffDescrip->length - offset;
  1484. memcpy(data + offset, (void *)address, len);
  1485. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1486. /* Store the PTP 1588 timestamp for received PTP event frame. */
  1487. if (isPtpEventMessage)
  1488. {
  1489. /* Set the timestamp to the timestamp ring. */
  1490. ptpTimestamp.timeStamp.nanosecond = curBuffDescrip->timestamp;
  1491. result = ENET_StoreRxFrameTime(base, handle, &ptpTimestamp);
  1492. }
  1493. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1494. /* Updates the receive buffer descriptors. */
  1495. ENET_UpdateReadBuffers(base, handle, ringId);
  1496. return result;
  1497. }
  1498. else
  1499. {
  1500. /* Updates the receive buffer descriptors. */
  1501. ENET_UpdateReadBuffers(base, handle, ringId);
  1502. }
  1503. }
  1504. else
  1505. {
  1506. /* Store a frame on several buffer descriptors. */
  1507. isLastBuff = false;
  1508. /* Length check. */
  1509. if (offset >= length)
  1510. {
  1511. break;
  1512. }
  1513. memcpy(data + offset, (void *)address, handle->rxBuffSizeAlign[ringId]);
  1514. offset += handle->rxBuffSizeAlign[ringId];
  1515. /* Updates the receive buffer descriptors. */
  1516. ENET_UpdateReadBuffers(base, handle, ringId);
  1517. }
  1518. /* Get the current buffer descriptor. */
  1519. curBuffDescrip = handle->rxBdCurrent[ringId];
  1520. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1521. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1522. #else
  1523. address = (uint32_t)curBuffDescrip->buffer;
  1524. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1525. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1526. /* Add the cache invalidate maintain. */
  1527. DCACHE_InvalidateByRange(address, handle->rxBuffSizeAlign[ringId]);
  1528. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1529. }
  1530. }
  1531. return kStatus_ENET_RxFrameFail;
  1532. }
  1533. status_t ENET_SendFrameMultiRing(
  1534. ENET_Type *base, enet_handle_t *handle, uint8_t *data, uint32_t length, uint32_t ringId)
  1535. {
  1536. assert(handle);
  1537. assert(data);
  1538. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1539. volatile enet_tx_bd_struct_t *curBuffDescrip;
  1540. uint32_t len = 0;
  1541. uint32_t sizeleft = 0;
  1542. uint32_t address;
  1543. /* Check the frame length. */
  1544. if (length > ENET_FRAME_MAX_FRAMELEN)
  1545. {
  1546. return kStatus_ENET_TxFrameOverLen;
  1547. }
  1548. /* Check if the transmit buffer is ready. */
  1549. curBuffDescrip = handle->txBdCurrent[ringId];
  1550. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1551. {
  1552. return kStatus_ENET_TxFrameBusy;
  1553. }
  1554. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1555. bool isPtpEventMessage = false;
  1556. /* Check PTP message with the PTP header. */
  1557. isPtpEventMessage = ENET_Ptp1588ParseFrame(data, NULL, true);
  1558. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1559. /* One transmit buffer is enough for one frame. */
  1560. if (handle->txBuffSizeAlign[ringId] >= length)
  1561. {
  1562. /* Copy data to the buffer for uDMA transfer. */
  1563. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1564. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1565. #else
  1566. address = (uint32_t)curBuffDescrip->buffer;
  1567. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1568. memcpy((void *)address, data, length);
  1569. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1570. /* Add the cache clean maintain. */
  1571. DCACHE_CleanByRange(address, length);
  1572. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1573. /* Set data length. */
  1574. curBuffDescrip->length = length;
  1575. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1576. /* For enable the timestamp. */
  1577. if (isPtpEventMessage)
  1578. {
  1579. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1580. }
  1581. else
  1582. {
  1583. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1584. }
  1585. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1586. curBuffDescrip->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
  1587. /* Increase the buffer descriptor address. */
  1588. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1589. {
  1590. handle->txBdCurrent[ringId] = handle->txBdBase[ringId];
  1591. }
  1592. else
  1593. {
  1594. handle->txBdCurrent[ringId]++;
  1595. }
  1596. /* Active the transmit buffer descriptor. */
  1597. ENET_ActiveSend(base, ringId);
  1598. return kStatus_Success;
  1599. }
  1600. else
  1601. {
  1602. /* One frame requires more than one transmit buffers. */
  1603. do
  1604. {
  1605. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1606. /* For enable the timestamp. */
  1607. if (isPtpEventMessage)
  1608. {
  1609. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1610. }
  1611. else
  1612. {
  1613. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1614. }
  1615. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1616. /* Increase the buffer descriptor address. */
  1617. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1618. {
  1619. handle->txBdCurrent[ringId] = handle->txBdBase[ringId];
  1620. }
  1621. else
  1622. {
  1623. handle->txBdCurrent[ringId]++;
  1624. }
  1625. /* update the size left to be transmit. */
  1626. sizeleft = length - len;
  1627. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1628. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1629. #else
  1630. address = (uint32_t)curBuffDescrip->buffer;
  1631. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1632. if (sizeleft > handle->txBuffSizeAlign[ringId])
  1633. {
  1634. /* Data copy. */
  1635. memcpy((void*)address, data + len, handle->txBuffSizeAlign[ringId]);
  1636. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1637. /* Add the cache clean maintain. */
  1638. DCACHE_CleanByRange(address, handle->txBuffSizeAlign[ringId]);
  1639. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1640. /* Data length update. */
  1641. curBuffDescrip->length = handle->txBuffSizeAlign[ringId];
  1642. len += handle->txBuffSizeAlign[ringId];
  1643. /* Sets the control flag. */
  1644. curBuffDescrip->control &= ~ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  1645. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK;
  1646. /* Active the transmit buffer descriptor*/
  1647. ENET_ActiveSend(base, ringId);
  1648. }
  1649. else
  1650. {
  1651. memcpy((void *)address, data + len, sizeleft);
  1652. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1653. /* Add the cache clean maintain. */
  1654. DCACHE_CleanByRange(address, sizeleft);
  1655. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1656. curBuffDescrip->length = sizeleft;
  1657. /* Set Last buffer wrap flag. */
  1658. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  1659. /* Active the transmit buffer descriptor. */
  1660. ENET_ActiveSend(base, ringId);
  1661. return kStatus_Success;
  1662. }
  1663. /* Get the current buffer descriptor address. */
  1664. curBuffDescrip = handle->txBdCurrent[ringId];
  1665. } while (!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK));
  1666. return kStatus_ENET_TxFrameBusy;
  1667. }
  1668. }
  1669. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  1670. void ENET_AddMulticastGroup(ENET_Type *base, uint8_t *address)
  1671. {
  1672. assert(address);
  1673. uint32_t crc = 0xFFFFFFFFU;
  1674. uint32_t count1 = 0;
  1675. uint32_t count2 = 0;
  1676. /* Calculates the CRC-32 polynomial on the multicast group address. */
  1677. for (count1 = 0; count1 < ENET_FRAME_MACLEN; count1++)
  1678. {
  1679. uint8_t c = address[count1];
  1680. for (count2 = 0; count2 < 0x08U; count2++)
  1681. {
  1682. if ((c ^ crc) & 1U)
  1683. {
  1684. crc >>= 1U;
  1685. c >>= 1U;
  1686. crc ^= 0xEDB88320U;
  1687. }
  1688. else
  1689. {
  1690. crc >>= 1U;
  1691. c >>= 1U;
  1692. }
  1693. }
  1694. }
  1695. /* Enable a multicast group address. */
  1696. if (!((crc >> 0x1FU) & 1U))
  1697. {
  1698. base->GALR |= 1U << ((crc >> 0x1AU) & 0x1FU);
  1699. }
  1700. else
  1701. {
  1702. base->GAUR |= 1U << ((crc >> 0x1AU) & 0x1FU);
  1703. }
  1704. }
  1705. void ENET_LeaveMulticastGroup(ENET_Type *base, uint8_t *address)
  1706. {
  1707. assert(address);
  1708. uint32_t crc = 0xFFFFFFFFU;
  1709. uint32_t count1 = 0;
  1710. uint32_t count2 = 0;
  1711. /* Calculates the CRC-32 polynomial on the multicast group address. */
  1712. for (count1 = 0; count1 < ENET_FRAME_MACLEN; count1++)
  1713. {
  1714. uint8_t c = address[count1];
  1715. for (count2 = 0; count2 < 0x08U; count2++)
  1716. {
  1717. if ((c ^ crc) & 1U)
  1718. {
  1719. crc >>= 1U;
  1720. c >>= 1U;
  1721. crc ^= 0xEDB88320U;
  1722. }
  1723. else
  1724. {
  1725. crc >>= 1U;
  1726. c >>= 1U;
  1727. }
  1728. }
  1729. }
  1730. /* Set the hash table. */
  1731. if (!((crc >> 0x1FU) & 1U))
  1732. {
  1733. base->GALR &= ~(1U << ((crc >> 0x1AU) & 0x1FU));
  1734. }
  1735. else
  1736. {
  1737. base->GAUR &= ~(1U << ((crc >> 0x1AU) & 0x1FU));
  1738. }
  1739. }
  1740. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1741. status_t ENET_GetTxErrAfterSendFrame(enet_handle_t *handle, enet_data_error_stats_t *eErrorStatic)
  1742. {
  1743. assert(handle);
  1744. assert(eErrorStatic);
  1745. uint16_t control = 0;
  1746. uint16_t controlExt = 0;
  1747. do
  1748. {
  1749. /* Get the current dirty transmit buffer descriptor. */
  1750. control = handle->txBdDirtyStatic[0]->control;
  1751. controlExt = handle->txBdDirtyStatic[0]->controlExtend0;
  1752. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  1753. if (control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1754. {
  1755. return kStatus_ENET_TxFrameBusy;
  1756. }
  1757. /* Increase the transmit dirty static pointer. */
  1758. if (handle->txBdDirtyStatic[0]->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1759. {
  1760. handle->txBdDirtyStatic[0] = handle->txBdBase[0];
  1761. }
  1762. else
  1763. {
  1764. handle->txBdDirtyStatic[0]++;
  1765. }
  1766. /* If the transmit buffer descriptor is ready and the last buffer descriptor, store packet statistic. */
  1767. if (control & ENET_BUFFDESCRIPTOR_TX_LAST_MASK)
  1768. {
  1769. if (controlExt & ENET_BUFFDESCRIPTOR_TX_ERR_MASK)
  1770. {
  1771. /* Transmit error. */
  1772. eErrorStatic->statsTxErr++;
  1773. }
  1774. if (controlExt & ENET_BUFFDESCRIPTOR_TX_EXCCOLLISIONERR_MASK)
  1775. {
  1776. /* Transmit excess collision error. */
  1777. eErrorStatic->statsTxExcessCollisionErr++;
  1778. }
  1779. if (controlExt & ENET_BUFFDESCRIPTOR_TX_LATECOLLISIONERR_MASK)
  1780. {
  1781. /* Transmit late collision error. */
  1782. eErrorStatic->statsTxLateCollisionErr++;
  1783. }
  1784. if (controlExt & ENET_BUFFDESCRIPTOR_TX_UNDERFLOWERR_MASK)
  1785. {
  1786. /* Transmit under flow error. */
  1787. eErrorStatic->statsTxUnderFlowErr++;
  1788. }
  1789. if (controlExt & ENET_BUFFDESCRIPTOR_TX_OVERFLOWERR_MASK)
  1790. {
  1791. /* Transmit over flow error. */
  1792. eErrorStatic->statsTxOverFlowErr++;
  1793. }
  1794. return kStatus_Success;
  1795. }
  1796. } while (handle->txBdDirtyStatic[0] != handle->txBdCurrent[0]);
  1797. return kStatus_ENET_TxFrameFail;
  1798. }
  1799. #if FSL_FEATURE_ENET_QUEUE > 1
  1800. status_t ENET_GetTxErrAfterSendFrameMultiRing(enet_handle_t *handle, enet_data_error_stats_t *eErrorStatic,
  1801. uint32_t ringId)
  1802. {
  1803. assert(handle);
  1804. assert(eErrorStatic);
  1805. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1806. uint16_t control = 0;
  1807. uint16_t controlExt = 0;
  1808. do
  1809. {
  1810. /* Get the current dirty transmit buffer descriptor. */
  1811. control = handle->txBdDirtyStatic[ringId]->control;
  1812. controlExt = handle->txBdDirtyStatic[ringId]->controlExtend0;
  1813. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  1814. if (control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1815. {
  1816. return kStatus_ENET_TxFrameBusy;
  1817. }
  1818. /* Increase the transmit dirty static pointer. */
  1819. if (handle->txBdDirtyStatic[ringId]->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1820. {
  1821. handle->txBdDirtyStatic[ringId] = handle->txBdBase[ringId];
  1822. }
  1823. else
  1824. {
  1825. handle->txBdDirtyStatic[ringId]++;
  1826. }
  1827. /* If the transmit buffer descriptor is ready and the last buffer descriptor, store packet statistic. */
  1828. if (control & ENET_BUFFDESCRIPTOR_TX_LAST_MASK)
  1829. {
  1830. if (controlExt & ENET_BUFFDESCRIPTOR_TX_ERR_MASK)
  1831. {
  1832. /* Transmit error. */
  1833. eErrorStatic->statsTxErr++;
  1834. }
  1835. if (controlExt & ENET_BUFFDESCRIPTOR_TX_EXCCOLLISIONERR_MASK)
  1836. {
  1837. /* Transmit excess collision error. */
  1838. eErrorStatic->statsTxExcessCollisionErr++;
  1839. }
  1840. if (controlExt & ENET_BUFFDESCRIPTOR_TX_LATECOLLISIONERR_MASK)
  1841. {
  1842. /* Transmit late collision error. */
  1843. eErrorStatic->statsTxLateCollisionErr++;
  1844. }
  1845. if (controlExt & ENET_BUFFDESCRIPTOR_TX_UNDERFLOWERR_MASK)
  1846. {
  1847. /* Transmit under flow error. */
  1848. eErrorStatic->statsTxUnderFlowErr++;
  1849. }
  1850. if (controlExt & ENET_BUFFDESCRIPTOR_TX_OVERFLOWERR_MASK)
  1851. {
  1852. /* Transmit over flow error. */
  1853. eErrorStatic->statsTxOverFlowErr++;
  1854. }
  1855. return kStatus_Success;
  1856. }
  1857. } while (handle->txBdDirtyStatic[ringId] != handle->txBdCurrent[ringId]);
  1858. return kStatus_ENET_TxFrameFail;
  1859. }
  1860. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  1861. static bool ENET_Ptp1588ParseFrame(const uint8_t *data, enet_ptp_time_data_t *ptpTsData, bool isFastEnabled)
  1862. {
  1863. assert(data);
  1864. if (!isFastEnabled)
  1865. {
  1866. assert(ptpTsData);
  1867. }
  1868. bool isPtpMsg = false;
  1869. const uint8_t *buffer = data;
  1870. uint16_t ptpType;
  1871. /* Check for VLAN frame.
  1872. * Add Double vlan tag check for receiving extended QIN vlan frame. */
  1873. if (*(uint16_t *)(buffer + ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET) == (ENET_HTONS(ENET_8021QVLAN)
  1874. #if defined(FSL_FEATUR_ENET_HAS_AVB) && FSL_FEATURE_HAS_AVB
  1875. || ENET_HTONS(ENET_8021QSVLAN)
  1876. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  1877. ))
  1878. {
  1879. buffer += ENET_FRAME_VLAN_TAGLEN;
  1880. #if defined(FSL_FEATUR_ENET_HAS_AVB) && FSL_FEATURE_HAS_AVB
  1881. if (*(uint16_t *)(buffer + ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET) == ENET_HTONS(ENET_8021QVLAN)
  1882. {
  1883. buffer += ENET_FRAME_VLAN_TAGLEN;
  1884. }
  1885. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  1886. }
  1887. ptpType = *(uint16_t *)(buffer + ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET);
  1888. switch (ENET_HTONS(ptpType))
  1889. { /* Ethernet layer 2. */
  1890. case ENET_ETHERNETL2:
  1891. if ((*(uint8_t *)(buffer + ENET_PTP1588_ETHL2_MSGTYPE_OFFSET) & 0x0F) <= kENET_PtpEventMsgType)
  1892. {
  1893. isPtpMsg = true;
  1894. if (!isFastEnabled)
  1895. {
  1896. /* It's a ptpv2 message and store the ptp header information. */
  1897. ptpTsData->version = (*(uint8_t *)(buffer + ENET_PTP1588_ETHL2_VERSION_OFFSET)) & 0x0F;
  1898. ptpTsData->messageType = (*(uint8_t *)(buffer + ENET_PTP1588_ETHL2_MSGTYPE_OFFSET)) & 0x0F;
  1899. ptpTsData->sequenceId = ENET_HTONS(*(uint16_t *)(buffer + ENET_PTP1588_ETHL2_SEQUENCEID_OFFSET));
  1900. memcpy((void *)&ptpTsData->sourcePortId[0], (void *)(buffer + ENET_PTP1588_ETHL2_CLOCKID_OFFSET),
  1901. kENET_PtpSrcPortIdLen);
  1902. }
  1903. }
  1904. break;
  1905. /* IPV4. */
  1906. case ENET_IPV4:
  1907. if ((*(uint8_t *)(buffer + ENET_PTP1588_IPVERSION_OFFSET) >> 4) == ENET_IPV4VERSION)
  1908. {
  1909. if (((*(uint16_t *)(buffer + ENET_PTP1588_IPV4_UDP_PORT_OFFSET)) == ENET_HTONS(kENET_PtpEventPort)) &&
  1910. (*(uint8_t *)(buffer + ENET_PTP1588_IPV4_UDP_PROTOCOL_OFFSET) == ENET_UDPVERSION))
  1911. {
  1912. /* Set the PTP message flag. */
  1913. isPtpMsg = true;
  1914. if (!isFastEnabled)
  1915. {
  1916. /* It's a IPV4 ptp message and store the ptp header information. */
  1917. ptpTsData->version = (*(uint8_t *)(buffer + ENET_PTP1588_IPV4_UDP_VERSION_OFFSET)) & 0x0F;
  1918. ptpTsData->messageType = (*(uint8_t *)(buffer + ENET_PTP1588_IPV4_UDP_MSGTYPE_OFFSET)) & 0x0F;
  1919. ptpTsData->sequenceId =
  1920. ENET_HTONS(*(uint16_t *)(buffer + ENET_PTP1588_IPV4_UDP_SEQUENCEID_OFFSET));
  1921. memcpy((void *)&ptpTsData->sourcePortId[0],
  1922. (void *)(buffer + ENET_PTP1588_IPV4_UDP_CLKID_OFFSET), kENET_PtpSrcPortIdLen);
  1923. }
  1924. }
  1925. }
  1926. break;
  1927. /* IPV6. */
  1928. case ENET_IPV6:
  1929. if ((*(uint8_t *)(buffer + ENET_PTP1588_IPVERSION_OFFSET) >> 4) == ENET_IPV6VERSION)
  1930. {
  1931. if (((*(uint16_t *)(buffer + ENET_PTP1588_IPV6_UDP_PORT_OFFSET)) == ENET_HTONS(kENET_PtpEventPort)) &&
  1932. (*(uint8_t *)(buffer + ENET_PTP1588_IPV6_UDP_PROTOCOL_OFFSET) == ENET_UDPVERSION))
  1933. {
  1934. /* Set the PTP message flag. */
  1935. isPtpMsg = true;
  1936. if (!isFastEnabled)
  1937. {
  1938. /* It's a IPV6 ptp message and store the ptp header information. */
  1939. ptpTsData->version = (*(uint8_t *)(buffer + ENET_PTP1588_IPV6_UDP_VERSION_OFFSET)) & 0x0F;
  1940. ptpTsData->messageType = (*(uint8_t *)(buffer + ENET_PTP1588_IPV6_UDP_MSGTYPE_OFFSET)) & 0x0F;
  1941. ptpTsData->sequenceId =
  1942. ENET_HTONS(*(uint16_t *)(buffer + ENET_PTP1588_IPV6_UDP_SEQUENCEID_OFFSET));
  1943. memcpy((void *)&ptpTsData->sourcePortId[0],
  1944. (void *)(buffer + ENET_PTP1588_IPV6_UDP_CLKID_OFFSET), kENET_PtpSrcPortIdLen);
  1945. }
  1946. }
  1947. }
  1948. break;
  1949. default:
  1950. break;
  1951. }
  1952. return isPtpMsg;
  1953. }
  1954. void ENET_Ptp1588Configure(ENET_Type *base, enet_handle_t *handle, enet_ptp_config_t *ptpConfig)
  1955. {
  1956. assert(handle);
  1957. assert(ptpConfig);
  1958. uint8_t count;
  1959. uint32_t instance = ENET_GetInstance(base);
  1960. uint32_t mask = kENET_TxBufferInterrupt;
  1961. #if FSL_FEATURE_ENET_QUEUE > 1
  1962. mask |= kENET_TxBuffer1Interrupt | kENET_TxBuffer2Interrupt;
  1963. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  1964. /* Start the 1588 timer. */
  1965. ENET_Ptp1588StartTimer(base, ptpConfig->ptp1588ClockSrc_Hz);
  1966. for (count = 0; count < handle->ringNum; count++)
  1967. {
  1968. handle->txBdDirtyTime[count] = handle->txBdBase[count];
  1969. handle->txBdDirtyStatic[count] = handle->txBdBase[count];
  1970. }
  1971. /* Setting the receive and transmit state for transaction. */
  1972. handle->rxPtpTsDataRing.ptpTsData = ptpConfig->rxPtpTsData;
  1973. handle->rxPtpTsDataRing.size = ptpConfig->ptpTsRxBuffNum;
  1974. handle->rxPtpTsDataRing.front = 0;
  1975. handle->rxPtpTsDataRing.end = 0;
  1976. handle->txPtpTsDataRing.ptpTsData = ptpConfig->txPtpTsData;
  1977. handle->txPtpTsDataRing.size = ptpConfig->ptpTsTxBuffNum;
  1978. handle->txPtpTsDataRing.front = 0;
  1979. handle->txPtpTsDataRing.end = 0;
  1980. handle->msTimerSecond = 0;
  1981. /* Set the IRQ handler when the interrupt is enabled. */
  1982. s_enetTxIsr = ENET_TransmitIRQHandler;
  1983. s_enetTsIsr = ENET_Ptp1588TimerIRQHandler;
  1984. /* Enables the time stamp interrupt and transmit frame interrupt to
  1985. * handle the time-stamp . */
  1986. ENET_EnableInterrupts(base, (ENET_TS_INTERRUPT | ENET_TX_INTERRUPT));
  1987. ENET_DisableInterrupts(base, mask);
  1988. EnableIRQ(s_enetTsIrqId[instance]);
  1989. EnableIRQ(s_enetTxIrqId[instance]);
  1990. }
  1991. void ENET_Ptp1588StartTimer(ENET_Type *base, uint32_t ptpClkSrc)
  1992. {
  1993. /* Restart PTP 1588 timer, master clock. */
  1994. base->ATCR = ENET_ATCR_RESTART_MASK;
  1995. /* Initializes PTP 1588 timer. */
  1996. base->ATINC = ENET_ATINC_INC(ENET_NANOSECOND_ONE_SECOND / ptpClkSrc);
  1997. base->ATPER = ENET_NANOSECOND_ONE_SECOND;
  1998. /* Sets periodical event and the event signal output assertion and Actives PTP 1588 timer. */
  1999. base->ATCR = ENET_ATCR_PEREN_MASK | ENET_ATCR_PINPER_MASK | ENET_ATCR_EN_MASK;
  2000. }
  2001. void ENET_Ptp1588GetTimer(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_t *ptpTime)
  2002. {
  2003. assert(handle);
  2004. assert(ptpTime);
  2005. uint16_t count = ENET_1588TIME_DELAY_COUNT;
  2006. uint32_t primask;
  2007. /* Disables the interrupt. */
  2008. primask = DisableGlobalIRQ();
  2009. /* Get the current PTP time. */
  2010. ptpTime->second = handle->msTimerSecond;
  2011. /* Get the nanosecond from the master timer. */
  2012. base->ATCR |= ENET_ATCR_CAPTURE_MASK;
  2013. /* Add at least six clock cycle delay to get accurate time.
  2014. It's the requirement when the 1588 clock source is slower
  2015. than the register clock.
  2016. */
  2017. while (count--)
  2018. {
  2019. __NOP();
  2020. }
  2021. /* Get the captured time. */
  2022. ptpTime->nanosecond = base->ATVR;
  2023. /* Enables the interrupt. */
  2024. EnableGlobalIRQ(primask);
  2025. }
  2026. void ENET_Ptp1588SetTimer(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_t *ptpTime)
  2027. {
  2028. assert(handle);
  2029. assert(ptpTime);
  2030. uint32_t primask;
  2031. /* Disables the interrupt. */
  2032. primask = DisableGlobalIRQ();
  2033. /* Sets PTP timer. */
  2034. handle->msTimerSecond = ptpTime->second;
  2035. base->ATVR = ptpTime->nanosecond;
  2036. /* Enables the interrupt. */
  2037. EnableGlobalIRQ(primask);
  2038. }
  2039. void ENET_Ptp1588AdjustTimer(ENET_Type *base, uint32_t corrIncrease, uint32_t corrPeriod)
  2040. {
  2041. /* Set correction for PTP timer increment. */
  2042. base->ATINC = (base->ATINC & ~ENET_ATINC_INC_CORR_MASK) | (corrIncrease << ENET_ATINC_INC_CORR_SHIFT);
  2043. /* Set correction for PTP timer period. */
  2044. base->ATCOR = (base->ATCOR & ~ENET_ATCOR_COR_MASK) | (corrPeriod << ENET_ATCOR_COR_SHIFT);
  2045. }
  2046. static status_t ENET_Ptp1588UpdateTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimeData)
  2047. {
  2048. assert(ptpTsDataRing);
  2049. assert(ptpTsDataRing->ptpTsData);
  2050. assert(ptpTimeData);
  2051. uint16_t usedBuffer = 0;
  2052. /* Check if the buffers ring is full. */
  2053. if (ptpTsDataRing->end >= ptpTsDataRing->front)
  2054. {
  2055. usedBuffer = ptpTsDataRing->end - ptpTsDataRing->front;
  2056. }
  2057. else
  2058. {
  2059. usedBuffer = ptpTsDataRing->size - (ptpTsDataRing->front - ptpTsDataRing->end);
  2060. }
  2061. if (usedBuffer == ptpTsDataRing->size)
  2062. {
  2063. return kStatus_ENET_PtpTsRingFull;
  2064. }
  2065. /* Copy the new data into the buffer. */
  2066. memcpy((ptpTsDataRing->ptpTsData + ptpTsDataRing->end), ptpTimeData, sizeof(enet_ptp_time_data_t));
  2067. /* Increase the buffer pointer to the next empty one. */
  2068. ptpTsDataRing->end = (ptpTsDataRing->end + 1) % ptpTsDataRing->size;
  2069. return kStatus_Success;
  2070. }
  2071. static status_t ENET_Ptp1588SearchTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimedata)
  2072. {
  2073. assert(ptpTsDataRing);
  2074. assert(ptpTsDataRing->ptpTsData);
  2075. assert(ptpTimedata);
  2076. uint32_t index;
  2077. uint32_t size;
  2078. uint16_t usedBuffer = 0;
  2079. /* Check the PTP 1588 timestamp ring. */
  2080. if (ptpTsDataRing->front == ptpTsDataRing->end)
  2081. {
  2082. return kStatus_ENET_PtpTsRingEmpty;
  2083. }
  2084. /* Search the element in the ring buffer */
  2085. index = ptpTsDataRing->front;
  2086. size = ptpTsDataRing->size;
  2087. while (index != ptpTsDataRing->end)
  2088. {
  2089. if (((ptpTsDataRing->ptpTsData + index)->sequenceId == ptpTimedata->sequenceId) &&
  2090. (!memcmp(((void *)&(ptpTsDataRing->ptpTsData + index)->sourcePortId[0]),
  2091. (void *)&ptpTimedata->sourcePortId[0], kENET_PtpSrcPortIdLen)) &&
  2092. ((ptpTsDataRing->ptpTsData + index)->version == ptpTimedata->version) &&
  2093. ((ptpTsDataRing->ptpTsData + index)->messageType == ptpTimedata->messageType))
  2094. {
  2095. break;
  2096. }
  2097. /* Increase the ptp ring index. */
  2098. index = (index + 1) % size;
  2099. }
  2100. if (index == ptpTsDataRing->end)
  2101. {
  2102. /* Check if buffers is full. */
  2103. if (ptpTsDataRing->end >= ptpTsDataRing->front)
  2104. {
  2105. usedBuffer = ptpTsDataRing->end - ptpTsDataRing->front;
  2106. }
  2107. else
  2108. {
  2109. usedBuffer = ptpTsDataRing->size - (ptpTsDataRing->front - ptpTsDataRing->end);
  2110. }
  2111. if (usedBuffer == ptpTsDataRing->size)
  2112. { /* Drop one in the front. */
  2113. ptpTsDataRing->front = (ptpTsDataRing->front + 1) % size;
  2114. }
  2115. return kStatus_ENET_PtpTsRingFull;
  2116. }
  2117. /* Get the right timestamp of the required ptp messag. */
  2118. ptpTimedata->timeStamp.second = (ptpTsDataRing->ptpTsData + index)->timeStamp.second;
  2119. ptpTimedata->timeStamp.nanosecond = (ptpTsDataRing->ptpTsData + index)->timeStamp.nanosecond;
  2120. /* Increase the index. */
  2121. ptpTsDataRing->front = (ptpTsDataRing->front + 1) % size;
  2122. return kStatus_Success;
  2123. }
  2124. static status_t ENET_StoreRxFrameTime(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData)
  2125. {
  2126. assert(handle);
  2127. assert(ptpTimeData);
  2128. bool ptpTimerWrap = false;
  2129. enet_ptp_time_t ptpTimer;
  2130. uint32_t primask;
  2131. /* Disables the interrupt. */
  2132. primask = DisableGlobalIRQ();
  2133. /* Get current PTP timer nanosecond value. */
  2134. ENET_Ptp1588GetTimer(base, handle, &ptpTimer);
  2135. /* Get PTP timer wrap event. */
  2136. ptpTimerWrap = base->EIR & kENET_TsTimerInterrupt;
  2137. /* Get transmit time stamp second. */
  2138. if ((ptpTimer.nanosecond > ptpTimeData->timeStamp.nanosecond) ||
  2139. ((ptpTimer.nanosecond < ptpTimeData->timeStamp.nanosecond) && ptpTimerWrap))
  2140. {
  2141. ptpTimeData->timeStamp.second = handle->msTimerSecond;
  2142. }
  2143. else
  2144. {
  2145. ptpTimeData->timeStamp.second = handle->msTimerSecond - 1;
  2146. }
  2147. /* Enable the interrupt. */
  2148. EnableGlobalIRQ(primask);
  2149. /* Store the timestamp to the receive time stamp ring. */
  2150. /* Check if the buffers ring is full. */
  2151. return ENET_Ptp1588UpdateTimeRing(&handle->rxPtpTsDataRing, ptpTimeData);
  2152. }
  2153. static status_t ENET_StoreTxFrameTime(ENET_Type *base, enet_handle_t *handle, uint32_t ringId)
  2154. {
  2155. assert(handle);
  2156. uint32_t primask;
  2157. bool ptpTimerWrap;
  2158. bool isPtpEventMessage = false;
  2159. enet_ptp_time_data_t ptpTimeData;
  2160. volatile enet_tx_bd_struct_t *curBuffDescrip = handle->txBdDirtyTime[ringId];
  2161. uint32_t address;
  2162. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  2163. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  2164. {
  2165. return kStatus_ENET_TxFrameBusy;
  2166. }
  2167. /* Parse the PTP message. */
  2168. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  2169. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  2170. #else
  2171. address = (uint32_t)curBuffDescrip->buffer;
  2172. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  2173. isPtpEventMessage = ENET_Ptp1588ParseFrame((uint8_t *)address, &ptpTimeData, false);
  2174. if (isPtpEventMessage)
  2175. {
  2176. /* Only store tx timestamp for ptp event message. */
  2177. do
  2178. {
  2179. /* Increase current buffer descriptor to the next one. */
  2180. if (handle->txBdDirtyTime[ringId]->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  2181. {
  2182. handle->txBdDirtyTime[ringId] = handle->txBdBase[ringId];
  2183. }
  2184. else
  2185. {
  2186. handle->txBdDirtyTime[ringId]++;
  2187. }
  2188. /* Do time stamp check on the last buffer descriptor of the frame. */
  2189. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_LAST_MASK)
  2190. {
  2191. /* Disables the interrupt. */
  2192. primask = DisableGlobalIRQ();
  2193. /* Get current PTP timer nanosecond value. */
  2194. ENET_Ptp1588GetTimer(base, handle, &ptpTimeData.timeStamp);
  2195. /* Get PTP timer wrap event. */
  2196. ptpTimerWrap = base->EIR & kENET_TsTimerInterrupt;
  2197. /* Get transmit time stamp second. */
  2198. if ((ptpTimeData.timeStamp.nanosecond > curBuffDescrip->timestamp) ||
  2199. ((ptpTimeData.timeStamp.nanosecond < curBuffDescrip->timestamp) && ptpTimerWrap))
  2200. {
  2201. ptpTimeData.timeStamp.second = handle->msTimerSecond;
  2202. }
  2203. else
  2204. {
  2205. ptpTimeData.timeStamp.second = handle->msTimerSecond - 1;
  2206. }
  2207. /* Save transmit time stamp nanosecond. */
  2208. ptpTimeData.timeStamp.nanosecond = curBuffDescrip->timestamp;
  2209. /* Enable the interrupt. */
  2210. EnableGlobalIRQ(primask);
  2211. /* Store the timestamp to the transmit timestamp ring. */
  2212. return ENET_Ptp1588UpdateTimeRing(&handle->txPtpTsDataRing, &ptpTimeData);
  2213. }
  2214. /* Get the current transmit buffer descriptor. */
  2215. curBuffDescrip = handle->txBdDirtyTime[ringId];
  2216. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  2217. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  2218. {
  2219. return kStatus_ENET_TxFrameBusy;
  2220. }
  2221. } while (handle->txBdDirtyTime[ringId] != handle->txBdCurrent[ringId]);
  2222. return kStatus_ENET_TxFrameFail;
  2223. }
  2224. else
  2225. {
  2226. /* Only increase current buffer descriptor to the next one. */
  2227. if (handle->txBdDirtyTime[ringId]->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  2228. {
  2229. handle->txBdDirtyTime[ringId] = handle->txBdBase[ringId];
  2230. }
  2231. else
  2232. {
  2233. handle->txBdDirtyTime[ringId]++;
  2234. }
  2235. }
  2236. return kStatus_Success;
  2237. }
  2238. status_t ENET_GetTxFrameTime(enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData)
  2239. {
  2240. assert(handle);
  2241. assert(ptpTimeData);
  2242. return ENET_Ptp1588SearchTimeRing(&handle->txPtpTsDataRing, ptpTimeData);
  2243. }
  2244. status_t ENET_GetRxFrameTime(enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData)
  2245. {
  2246. assert(handle);
  2247. assert(ptpTimeData);
  2248. return ENET_Ptp1588SearchTimeRing(&handle->rxPtpTsDataRing, ptpTimeData);
  2249. }
  2250. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  2251. void ENET_AVBConfigure(ENET_Type *base, enet_handle_t *handle, const enet_avb_config_t *config)
  2252. {
  2253. assert(config);
  2254. uint8_t count = 0;
  2255. for (count = 0; count < FSL_FEATURE_ENET_QUEUE - 1; count++)
  2256. {
  2257. /* Set the AVB receive ring classification match when the match is not 0. */
  2258. if (config->rxClassifyMatch[count])
  2259. {
  2260. base->RCMR[count] = (config->rxClassifyMatch[count] & 0xFFFF) | ENET_RCMR_MATCHEN_MASK;
  2261. }
  2262. /* Set the dma controller for the extended ring. */
  2263. base->DMACFG[count] |= ENET_DMACFG_IDLE_SLOPE(config->idleSlope[count]);
  2264. }
  2265. /* Shall use the credit-based scheme for avb. */
  2266. base->QOS &= ~ENET_QOS_TX_SCHEME_MASK;
  2267. base->QOS |= ENET_QOS_RX_FLUSH0_MASK;
  2268. }
  2269. #endif /* FSL_FETAURE_ENET_HAS_AVB */
  2270. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  2271. #if FSL_FEATURE_ENET_QUEUE > 1
  2272. void ENET_TransmitIRQHandler(ENET_Type *base, enet_handle_t *handle, uint32_t ringId)
  2273. #else
  2274. void ENET_TransmitIRQHandler(ENET_Type *base, enet_handle_t *handle)
  2275. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2276. {
  2277. assert(handle);
  2278. uint32_t mask = kENET_TxBufferInterrupt | kENET_TxFrameInterrupt;
  2279. #if defined(ENET_ENHANCEDBUFFERDESCRIPTOR_MODE) || (FSL_FEATURE_ENET_QUEUE > 1)
  2280. uint32_t index = 0;
  2281. #endif /* ENET_ENHANCEDBUFFERDESCRIPTORMODE || (FSL_FEATURE_ENET_QUEUE > 1) */
  2282. /* Check if the transmit interrupt happen. */
  2283. #if FSL_FEATURE_ENET_QUEUE > 1
  2284. switch (ringId)
  2285. {
  2286. case kENET_Ring1:
  2287. mask = (kENET_TxFrame1Interrupt | kENET_TxBuffer1Interrupt);
  2288. break;
  2289. case kENET_Ring2:
  2290. mask = (kENET_TxFrame2Interrupt | kENET_TxBuffer2Interrupt);
  2291. break;
  2292. default:
  2293. break;
  2294. }
  2295. index = ringId;
  2296. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2297. while (mask & base->EIR)
  2298. {
  2299. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  2300. if (base->EIR & kENET_TxFrameInterrupt)
  2301. {
  2302. /* Store the transmit timestamp from the buffer descriptor should be done here. */
  2303. ENET_StoreTxFrameTime(base, handle, index);
  2304. }
  2305. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  2306. /* Clear the transmit interrupt event. */
  2307. base->EIR = mask;
  2308. /* Callback function. */
  2309. if (handle->callback)
  2310. {
  2311. #if FSL_FEATURE_ENET_QUEUE > 1
  2312. handle->callback(base, handle, index, kENET_TxEvent, handle->userData);
  2313. #else
  2314. handle->callback(base, handle, kENET_TxEvent, handle->userData);
  2315. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2316. }
  2317. }
  2318. }
  2319. #if FSL_FEATURE_ENET_QUEUE > 1
  2320. void ENET_ReceiveIRQHandler(ENET_Type *base, enet_handle_t *handle, uint32_t ringId)
  2321. #else
  2322. void ENET_ReceiveIRQHandler(ENET_Type *base, enet_handle_t *handle)
  2323. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2324. {
  2325. assert(handle);
  2326. uint32_t mask = kENET_RxFrameInterrupt | kENET_RxBufferInterrupt;
  2327. /* Check if the receive interrupt happen. */
  2328. #if FSL_FEATURE_ENET_QUEUE > 1
  2329. switch (ringId)
  2330. {
  2331. case kENET_Ring1:
  2332. mask = (kENET_RxFrame1Interrupt | kENET_RxBuffer1Interrupt);
  2333. break;
  2334. case kENET_Ring2:
  2335. mask = (kENET_RxFrame2Interrupt | kENET_RxBuffer2Interrupt);
  2336. break;
  2337. default:
  2338. break;
  2339. }
  2340. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2341. while (mask & base->EIR)
  2342. {
  2343. /* Clear the transmit interrupt event. */
  2344. base->EIR = mask;
  2345. /* Callback function. */
  2346. if (handle->callback)
  2347. {
  2348. #if FSL_FEATURE_ENET_QUEUE > 1
  2349. handle->callback(base, handle, ringId, kENET_RxEvent, handle->userData);
  2350. #else
  2351. handle->callback(base, handle, kENET_RxEvent, handle->userData);
  2352. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2353. }
  2354. }
  2355. }
  2356. void ENET_ErrorIRQHandler(ENET_Type *base, enet_handle_t *handle)
  2357. {
  2358. assert(handle);
  2359. uint32_t errMask = kENET_BabrInterrupt | kENET_BabtInterrupt | kENET_EBusERInterrupt | kENET_PayloadRxInterrupt |
  2360. kENET_LateCollisionInterrupt | kENET_RetryLimitInterrupt | kENET_UnderrunInterrupt;
  2361. /* Check if the error interrupt happen. */
  2362. if (kENET_WakeupInterrupt & base->EIR)
  2363. {
  2364. /* Clear the wakeup interrupt. */
  2365. base->EIR = kENET_WakeupInterrupt;
  2366. /* wake up and enter the normal mode. */
  2367. ENET_EnableSleepMode(base, false);
  2368. /* Callback function. */
  2369. if (handle->callback)
  2370. {
  2371. #if FSL_FEATURE_ENET_QUEUE > 1
  2372. handle->callback(base, handle, 0, kENET_WakeUpEvent, handle->userData);
  2373. #else
  2374. handle->callback(base, handle, kENET_WakeUpEvent, handle->userData);
  2375. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2376. }
  2377. }
  2378. else
  2379. {
  2380. /* Clear the error interrupt event status. */
  2381. errMask &= base->EIR;
  2382. base->EIR = errMask;
  2383. /* Callback function. */
  2384. if (handle->callback)
  2385. {
  2386. #if FSL_FEATURE_ENET_QUEUE > 1
  2387. handle->callback(base, handle, 0, kENET_ErrEvent, handle->userData);
  2388. #else
  2389. handle->callback(base, handle, kENET_ErrEvent, handle->userData);
  2390. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2391. }
  2392. }
  2393. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2394. exception return operation might vector to incorrect interrupt */
  2395. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2396. __DSB();
  2397. #endif
  2398. }
  2399. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  2400. void ENET_Ptp1588TimerIRQHandler(ENET_Type *base, enet_handle_t *handle)
  2401. {
  2402. assert(handle);
  2403. /* Check if the PTP time stamp interrupt happen. */
  2404. if (kENET_TsTimerInterrupt & base->EIR)
  2405. {
  2406. /* Clear the time stamp interrupt. */
  2407. base->EIR = kENET_TsTimerInterrupt;
  2408. /* Increase timer second counter. */
  2409. handle->msTimerSecond++;
  2410. /* Callback function. */
  2411. if (handle->callback)
  2412. {
  2413. #if FSL_FEATURE_ENET_QUEUE > 1
  2414. handle->callback(base, handle, 0, kENET_TimeStampEvent, handle->userData);
  2415. #else
  2416. handle->callback(base, handle, kENET_TimeStampEvent, handle->userData);
  2417. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2418. }
  2419. }
  2420. else
  2421. {
  2422. /* Clear the time stamp interrupt. */
  2423. base->EIR = kENET_TsAvailInterrupt;
  2424. /* Callback function. */
  2425. if (handle->callback)
  2426. {
  2427. #if FSL_FEATURE_ENET_QUEUE > 1
  2428. handle->callback(base, handle, 0, kENET_TimeStampAvailEvent, handle->userData);
  2429. #else
  2430. handle->callback(base, handle, kENET_TimeStampAvailEvent, handle->userData);
  2431. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2432. }
  2433. }
  2434. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2435. exception return operation might vector to incorrect interrupt */
  2436. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2437. __DSB();
  2438. #endif
  2439. }
  2440. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  2441. void ENET_CommonFrame0IRQHandler(ENET_Type *base)
  2442. {
  2443. uint32_t event = base->EIR;
  2444. uint32_t instance = ENET_GetInstance(base);
  2445. if (event & (kENET_TxBufferInterrupt | kENET_TxFrameInterrupt))
  2446. {
  2447. #if FSL_FEATURE_ENET_QUEUE > 1
  2448. s_enetTxIsr(base, s_ENETHandle[instance], 0);
  2449. #else
  2450. s_enetTxIsr(base, s_ENETHandle[instance]);
  2451. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2452. }
  2453. if (event & (kENET_RxBufferInterrupt | kENET_RxFrameInterrupt))
  2454. {
  2455. #if FSL_FEATURE_ENET_QUEUE > 1
  2456. s_enetRxIsr(base, s_ENETHandle[instance], 0);
  2457. #else
  2458. s_enetRxIsr(base, s_ENETHandle[instance]);
  2459. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2460. }
  2461. if (event & ENET_TS_INTERRUPT)
  2462. {
  2463. s_enetTsIsr(base, s_ENETHandle[instance]);
  2464. }
  2465. if (event & ENET_ERR_INTERRUPT)
  2466. {
  2467. s_enetErrIsr(base, s_ENETHandle[instance]);
  2468. }
  2469. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2470. exception return operation might vector to incorrect interrupt */
  2471. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2472. __DSB();
  2473. #endif
  2474. }
  2475. #if FSL_FEATURE_ENET_QUEUE > 1
  2476. void ENET_CommonFrame1IRQHandler(ENET_Type *base)
  2477. {
  2478. uint32_t event = base->EIR;
  2479. uint32_t instance = ENET_GetInstance(base);
  2480. if (event & (kENET_TxBuffer1Interrupt | kENET_TxFrame1Interrupt))
  2481. {
  2482. s_enetTxIsr(base, s_ENETHandle[instance], 1);
  2483. }
  2484. if (event & (kENET_RxBuffer1Interrupt | kENET_RxFrame1Interrupt))
  2485. {
  2486. s_enetRxIsr(base, s_ENETHandle[instance], 1);
  2487. }
  2488. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2489. exception return operation might vector to incorrect interrupt */
  2490. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2491. __DSB();
  2492. #endif
  2493. }
  2494. void ENET_CommonFrame2IRQHandler(ENET_Type *base)
  2495. {
  2496. uint32_t event = base->EIR;
  2497. uint32_t instance = ENET_GetInstance(base);
  2498. if (event & (kENET_TxBuffer2Interrupt | kENET_TxFrame2Interrupt))
  2499. {
  2500. s_enetTxIsr(base, s_ENETHandle[instance], 2);
  2501. }
  2502. if (event & (kENET_RxBuffer2Interrupt | kENET_RxFrame2Interrupt))
  2503. {
  2504. s_enetRxIsr(base, s_ENETHandle[instance], 2);
  2505. }
  2506. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2507. exception return operation might vector to incorrect interrupt */
  2508. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2509. __DSB();
  2510. #endif
  2511. }
  2512. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2513. #if defined(ENET)
  2514. void ENET_Transmit_IRQHandler(void)
  2515. {
  2516. s_enetTxIsr(ENET, s_ENETHandle[0]);
  2517. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2518. exception return operation might vector to incorrect interrupt */
  2519. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2520. __DSB();
  2521. #endif
  2522. }
  2523. void ENET_Receive_IRQHandler(void)
  2524. {
  2525. s_enetRxIsr(ENET, s_ENETHandle[0]);
  2526. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2527. exception return operation might vector to incorrect interrupt */
  2528. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2529. __DSB();
  2530. #endif
  2531. }
  2532. void ENET_Error_IRQHandler(void)
  2533. {
  2534. s_enetErrIsr(ENET, s_ENETHandle[0]);
  2535. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2536. exception return operation might vector to incorrect interrupt */
  2537. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2538. __DSB();
  2539. #endif
  2540. }
  2541. void ENET_1588_Timer_IRQHandler(void)
  2542. {
  2543. s_enetTsIsr(ENET, s_ENETHandle[0]);
  2544. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2545. exception return operation might vector to incorrect interrupt */
  2546. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2547. __DSB();
  2548. #endif
  2549. }
  2550. void ENET_DriverIRQHandler(void)
  2551. {
  2552. ENET_CommonFrame0IRQHandler(ENET);
  2553. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2554. exception return operation might vector to incorrect interrupt */
  2555. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2556. __DSB();
  2557. #endif
  2558. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2559. exception return operation might vector to incorrect interrupt */
  2560. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2561. __DSB();
  2562. #endif
  2563. }
  2564. #endif
  2565. #if defined(ENET1)
  2566. void ENET1_DriverIRQHandler(void)
  2567. {
  2568. ENET_CommonFrame0IRQHandler(ENET1);
  2569. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2570. exception return operation might vector to incorrect interrupt */
  2571. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2572. __DSB();
  2573. #endif
  2574. }
  2575. #endif
  2576. #if defined(ENET2)
  2577. void ENET2_DriverIRQHandler(void)
  2578. {
  2579. ENET_CommonFrame0IRQHandler(ENET2);
  2580. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2581. exception return operation might vector to incorrect interrupt */
  2582. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2583. __DSB();
  2584. #endif
  2585. }
  2586. #endif
  2587. #if defined(CONNECTIVITY__ENET0)
  2588. void CONNECTIVITY_ENET0_FRAME0_EVENT_INT_DriverIRQHandler(void)
  2589. {
  2590. ENET_CommonFrame0IRQHandler(CONNECTIVITY__ENET0);
  2591. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2592. exception return operation might vector to incorrect interrupt */
  2593. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2594. __DSB();
  2595. #endif
  2596. }
  2597. #if FSL_FEATURE_ENET_QUEUE > 1
  2598. void CONNECTIVITY_ENET0_FRAME1_INT_DriverIRQHandler(void)
  2599. {
  2600. ENET_CommonFrame1IRQHandler(CONNECTIVITY__ENET0);
  2601. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2602. exception return operation might vector to incorrect interrupt */
  2603. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2604. __DSB();
  2605. #endif
  2606. }
  2607. void CONNECTIVITY_ENET0_FRAME2_INT_DriverIRQHandler(void)
  2608. {
  2609. ENET_CommonFrame2IRQHandler(CONNECTIVITY__ENET0);
  2610. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2611. exception return operation might vector to incorrect interrupt */
  2612. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2613. __DSB();
  2614. #endif
  2615. }
  2616. #endif
  2617. #endif
  2618. #if defined(CONNECTIVITY__ENET1)
  2619. void CONNECTIVITY_ENET1_FRAME0_EVENT_INT_DriverIRQHandler(void)
  2620. {
  2621. ENET_CommonFrame0IRQHandler(CONNECTIVITY__ENET1);
  2622. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2623. exception return operation might vector to incorrect interrupt */
  2624. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2625. __DSB();
  2626. #endif
  2627. }
  2628. #if FSL_FEATURE_ENET_QUEUE > 1
  2629. void CONNECTIVITY_ENET1_FRAME1_INT_DriverIRQHandler(void)
  2630. {
  2631. ENET_CommonFrame1IRQHandler(CONNECTIVITY__ENET1);
  2632. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2633. exception return operation might vector to incorrect interrupt */
  2634. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2635. __DSB();
  2636. #endif
  2637. }
  2638. void CONNECTIVITY_ENET1_FRAME2_INT_DriverIRQHandler(void)
  2639. {
  2640. ENET_CommonFrame2IRQHandler(CONNECTIVITY__ENET1);
  2641. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2642. exception return operation might vector to incorrect interrupt */
  2643. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2644. __DSB();
  2645. #endif
  2646. }
  2647. #endif
  2648. #endif