fsl_enet.c 110 KB

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