fsl_enet.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008
  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. {
  1206. // Change SDK to reduce memory copy
  1207. extern void pbuf2mem(const uint8_t *data, void *dataptr, uint32_t len);
  1208. pbuf2mem(data, (void *)address, length);
  1209. }
  1210. //memcpy((void *)address, data, length);
  1211. /* Set data length. */
  1212. curBuffDescrip->length = length;
  1213. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1214. /* For enable the timestamp. */
  1215. if (isPtpEventMessage)
  1216. {
  1217. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1218. }
  1219. else
  1220. {
  1221. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1222. }
  1223. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1224. curBuffDescrip->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
  1225. /* Increase the buffer descriptor address. */
  1226. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1227. {
  1228. handle->txBdCurrent[0] = handle->txBdBase[0];
  1229. }
  1230. else
  1231. {
  1232. handle->txBdCurrent[0]++;
  1233. }
  1234. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1235. /* Add the cache clean maintain. */
  1236. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1237. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1238. #else
  1239. address = (uint32_t)curBuffDescrip->buffer;
  1240. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1241. DCACHE_CleanByRange(address, length);
  1242. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1243. /* Active the transmit buffer descriptor. */
  1244. ENET_ActiveSend(base, 0);
  1245. return kStatus_Success;
  1246. }
  1247. else
  1248. {
  1249. /* One frame requires more than one transmit buffers. */
  1250. do
  1251. {
  1252. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1253. /* For enable the timestamp. */
  1254. if (isPtpEventMessage)
  1255. {
  1256. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1257. }
  1258. else
  1259. {
  1260. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1261. }
  1262. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1263. /* Increase the buffer descriptor address. */
  1264. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1265. {
  1266. handle->txBdCurrent[0] = handle->txBdBase[0];
  1267. }
  1268. else
  1269. {
  1270. handle->txBdCurrent[0]++;
  1271. }
  1272. /* update the size left to be transmit. */
  1273. sizeleft = length - len;
  1274. if (sizeleft > handle->txBuffSizeAlign[0])
  1275. {
  1276. /* Data copy. */
  1277. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1278. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1279. #else
  1280. address = (uint32_t)curBuffDescrip->buffer;
  1281. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1282. memcpy((void *)address, data + len, handle->txBuffSizeAlign[0]);
  1283. /* Data length update. */
  1284. curBuffDescrip->length = handle->txBuffSizeAlign[0];
  1285. len += handle->txBuffSizeAlign[0];
  1286. /* Sets the control flag. */
  1287. curBuffDescrip->control &= ~ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  1288. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK;
  1289. /* Active the transmit buffer descriptor*/
  1290. ENET_ActiveSend(base, 0);
  1291. }
  1292. else
  1293. {
  1294. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1295. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1296. #else
  1297. address = (uint32_t)curBuffDescrip->buffer;
  1298. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1299. memcpy((void *)address, data + len, sizeleft);
  1300. curBuffDescrip->length = sizeleft;
  1301. /* Set Last buffer wrap flag. */
  1302. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  1303. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1304. /* Add the cache clean maintain. */
  1305. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1306. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1307. #else
  1308. address = (uint32_t)curBuffDescrip->buffer;
  1309. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1310. DCACHE_CleanByRange(address, handle->txBuffSizeAlign[0]);
  1311. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1312. /* Active the transmit buffer descriptor. */
  1313. ENET_ActiveSend(base, 0);
  1314. return kStatus_Success;
  1315. }
  1316. /* Get the current buffer descriptor address. */
  1317. curBuffDescrip = handle->txBdCurrent[0];
  1318. } while (!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK));
  1319. return kStatus_ENET_TxFrameBusy;
  1320. }
  1321. }
  1322. #if FSL_FEATURE_ENET_QUEUE > 1
  1323. void ENET_GetRxErrBeforeReadFrameMultiRing(enet_handle_t *handle,
  1324. enet_data_error_stats_t *eErrorStatic,
  1325. uint32_t ringId)
  1326. {
  1327. assert(handle);
  1328. assert(eErrorStatic);
  1329. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1330. uint16_t control = 0;
  1331. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent[ringId];
  1332. do
  1333. {
  1334. /* The last buffer descriptor of a frame. */
  1335. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1336. {
  1337. control = curBuffDescrip->control;
  1338. if (control & ENET_BUFFDESCRIPTOR_RX_TRUNC_MASK)
  1339. {
  1340. /* The receive truncate error. */
  1341. eErrorStatic->statsRxTruncateErr++;
  1342. }
  1343. if (control & ENET_BUFFDESCRIPTOR_RX_OVERRUN_MASK)
  1344. {
  1345. /* The receive over run error. */
  1346. eErrorStatic->statsRxOverRunErr++;
  1347. }
  1348. if (control & ENET_BUFFDESCRIPTOR_RX_LENVLIOLATE_MASK)
  1349. {
  1350. /* The receive length violation error. */
  1351. eErrorStatic->statsRxLenGreaterErr++;
  1352. }
  1353. if (control & ENET_BUFFDESCRIPTOR_RX_NOOCTET_MASK)
  1354. {
  1355. /* The receive alignment error. */
  1356. eErrorStatic->statsRxAlignErr++;
  1357. }
  1358. if (control & ENET_BUFFDESCRIPTOR_RX_CRC_MASK)
  1359. {
  1360. /* The receive CRC error. */
  1361. eErrorStatic->statsRxFcsErr++;
  1362. }
  1363. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1364. uint16_t controlExt = curBuffDescrip->controlExtend1;
  1365. if (controlExt & ENET_BUFFDESCRIPTOR_RX_MACERR_MASK)
  1366. {
  1367. /* The MAC error. */
  1368. eErrorStatic->statsRxMacErr++;
  1369. }
  1370. if (controlExt & ENET_BUFFDESCRIPTOR_RX_PHYERR_MASK)
  1371. {
  1372. /* The PHY error. */
  1373. eErrorStatic->statsRxPhyErr++;
  1374. }
  1375. if (controlExt & ENET_BUFFDESCRIPTOR_RX_COLLISION_MASK)
  1376. {
  1377. /* The receive collision error. */
  1378. eErrorStatic->statsRxCollisionErr++;
  1379. }
  1380. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1381. break;
  1382. }
  1383. /* Increase the buffer descriptor, if it is the last one, increase to first one of the ring buffer. */
  1384. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  1385. {
  1386. curBuffDescrip = handle->rxBdBase[ringId];
  1387. }
  1388. else
  1389. {
  1390. curBuffDescrip++;
  1391. }
  1392. } while (curBuffDescrip != handle->rxBdCurrent[ringId]);
  1393. }
  1394. status_t ENET_GetRxFrameSizeMultiRing(enet_handle_t *handle, uint32_t *length, uint32_t ringId)
  1395. {
  1396. assert(handle);
  1397. assert(length);
  1398. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1399. /* Reset the length to zero. */
  1400. *length = 0;
  1401. uint16_t validLastMask = ENET_BUFFDESCRIPTOR_RX_LAST_MASK | ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK;
  1402. volatile enet_rx_bd_struct_t *curBuffDescrip;
  1403. curBuffDescrip = handle->rxBdCurrent[ringId];
  1404. /* Check the current buffer descriptor's empty flag. if empty means there is no frame received. */
  1405. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK)
  1406. {
  1407. return kStatus_ENET_RxFrameEmpty;
  1408. }
  1409. do
  1410. {
  1411. /* Add check for abnormal case. */
  1412. if ((!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_EMPTY_MASK)) && (!curBuffDescrip->length))
  1413. {
  1414. return kStatus_ENET_RxFrameError;
  1415. }
  1416. /* Find the last buffer descriptor. */
  1417. if ((curBuffDescrip->control & validLastMask) == ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1418. {
  1419. /* The last buffer descriptor in the frame check the status of the received frame. */
  1420. if ((curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_ERR_MASK)
  1421. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1422. || (curBuffDescrip->controlExtend1 & ENET_BUFFDESCRIPTOR_RX_EXT_ERR_MASK)
  1423. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1424. )
  1425. {
  1426. return kStatus_ENET_RxFrameError;
  1427. }
  1428. /* FCS is removed by MAC. */
  1429. *length = curBuffDescrip->length;
  1430. return kStatus_Success;
  1431. }
  1432. /* Increase the buffer descriptor, if it is the last one, increase to first one of the ring buffer. */
  1433. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_WRAP_MASK)
  1434. {
  1435. curBuffDescrip = handle->rxBdBase[ringId];
  1436. }
  1437. else
  1438. {
  1439. curBuffDescrip++;
  1440. }
  1441. } while (curBuffDescrip != handle->rxBdCurrent[ringId]);
  1442. /* The frame is on processing - set to empty status to make application to receive it next time. */
  1443. return kStatus_ENET_RxFrameEmpty;
  1444. }
  1445. status_t ENET_ReadFrameMultiRing(
  1446. ENET_Type *base, enet_handle_t *handle, uint8_t *data, uint32_t length, uint32_t ringId)
  1447. {
  1448. assert(handle);
  1449. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1450. uint32_t len = 0;
  1451. uint32_t offset = 0;
  1452. uint16_t control;
  1453. bool isLastBuff = false;
  1454. volatile enet_rx_bd_struct_t *curBuffDescrip = handle->rxBdCurrent[ringId];
  1455. status_t result = kStatus_Success;
  1456. uint32_t address;
  1457. /* For data-NULL input, only update the buffer descriptor. */
  1458. if (!data)
  1459. {
  1460. do
  1461. {
  1462. /* Update the control flag. */
  1463. control = handle->rxBdCurrent[ringId]->control;
  1464. /* Updates the receive buffer descriptors. */
  1465. ENET_UpdateReadBuffers(base, handle, ringId);
  1466. /* Find the last buffer descriptor for the frame. */
  1467. if (control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1468. {
  1469. break;
  1470. }
  1471. } while (handle->rxBdCurrent[ringId] != curBuffDescrip);
  1472. return result;
  1473. }
  1474. else
  1475. {
  1476. /* A frame on one buffer or several receive buffers are both considered. */
  1477. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1478. /* Add the cache invalidate maintain. */
  1479. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1480. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1481. #else
  1482. address = (uint32_t)curBuffDescrip->buffer;
  1483. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1484. DCACHE_InvalidateByRange(address, handle->rxBuffSizeAlign[ringId]);
  1485. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1486. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1487. enet_ptp_time_data_t ptpTimestamp;
  1488. bool isPtpEventMessage = false;
  1489. /* Parse the PTP message according to the header message. */
  1490. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1491. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1492. #else
  1493. address = (uint32_t)curBuffDescrip->buffer;
  1494. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1495. isPtpEventMessage = ENET_Ptp1588ParseFrame((uint8_t *)address, &ptpTimestamp, false);
  1496. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1497. while (!isLastBuff)
  1498. {
  1499. /* The last buffer descriptor of a frame. */
  1500. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_RX_LAST_MASK)
  1501. {
  1502. /* This is a valid frame. */
  1503. isLastBuff = true;
  1504. if (length == curBuffDescrip->length)
  1505. {
  1506. /* Copy the frame to user's buffer without FCS. */
  1507. len = curBuffDescrip->length - offset;
  1508. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1509. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1510. #else
  1511. address = (uint32_t)curBuffDescrip->buffer;
  1512. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1513. memcpy(data + offset, (void *)address, len);
  1514. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1515. /* Store the PTP 1588 timestamp for received PTP event frame. */
  1516. if (isPtpEventMessage)
  1517. {
  1518. /* Set the timestamp to the timestamp ring. */
  1519. ptpTimestamp.timeStamp.nanosecond = curBuffDescrip->timestamp;
  1520. result = ENET_StoreRxFrameTime(base, handle, &ptpTimestamp);
  1521. }
  1522. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1523. /* Updates the receive buffer descriptors. */
  1524. ENET_UpdateReadBuffers(base, handle, ringId);
  1525. return result;
  1526. }
  1527. else
  1528. {
  1529. /* Updates the receive buffer descriptors. */
  1530. ENET_UpdateReadBuffers(base, handle, ringId);
  1531. }
  1532. }
  1533. else
  1534. {
  1535. /* Store a frame on several buffer descriptors. */
  1536. isLastBuff = false;
  1537. /* Length check. */
  1538. if (offset >= length)
  1539. {
  1540. break;
  1541. }
  1542. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1543. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1544. #else
  1545. address = (uint32_t)curBuffDescrip->buffer;
  1546. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1547. memcpy(data + offset, (void *)address, handle->rxBuffSizeAlign[ringId]);
  1548. offset += handle->rxBuffSizeAlign[ringId];
  1549. /* Updates the receive buffer descriptors. */
  1550. ENET_UpdateReadBuffers(base, handle, ringId);
  1551. }
  1552. /* Get the current buffer descriptor. */
  1553. curBuffDescrip = handle->rxBdCurrent[ringId];
  1554. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1555. /* Add the cache invalidate maintain. */
  1556. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1557. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1558. #else
  1559. address = (uint32_t)curBuffDescrip->buffer;
  1560. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1561. DCACHE_InvalidateByRange(address, handle->rxBuffSizeAlign[ringId]);
  1562. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1563. }
  1564. }
  1565. return kStatus_ENET_RxFrameFail;
  1566. }
  1567. status_t ENET_SendFrameMultiRing(
  1568. ENET_Type *base, enet_handle_t *handle, uint8_t *data, uint32_t length, uint32_t ringId)
  1569. {
  1570. assert(handle);
  1571. assert(data);
  1572. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1573. volatile enet_tx_bd_struct_t *curBuffDescrip;
  1574. uint32_t len = 0;
  1575. uint32_t sizeleft = 0;
  1576. uint32_t address;
  1577. /* Check the frame length. */
  1578. if (length > ENET_FRAME_MAX_FRAMELEN)
  1579. {
  1580. return kStatus_ENET_TxFrameOverLen;
  1581. }
  1582. /* Check if the transmit buffer is ready. */
  1583. curBuffDescrip = handle->txBdCurrent[ringId];
  1584. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1585. {
  1586. return kStatus_ENET_TxFrameBusy;
  1587. }
  1588. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1589. bool isPtpEventMessage = false;
  1590. /* Check PTP message with the PTP header. */
  1591. isPtpEventMessage = ENET_Ptp1588ParseFrame(data, NULL, true);
  1592. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1593. /* One transmit buffer is enough for one frame. */
  1594. if (handle->txBuffSizeAlign[ringId] >= length)
  1595. {
  1596. /* Copy data to the buffer for uDMA transfer. */
  1597. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1598. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1599. #else
  1600. address = (uint32_t)curBuffDescrip->buffer;
  1601. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1602. memcpy((void *)address, data, length);
  1603. /* Set data length. */
  1604. curBuffDescrip->length = length;
  1605. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1606. /* For enable the timestamp. */
  1607. if (isPtpEventMessage)
  1608. {
  1609. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1610. }
  1611. else
  1612. {
  1613. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1614. }
  1615. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1616. curBuffDescrip->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
  1617. /* Increase the buffer descriptor address. */
  1618. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1619. {
  1620. handle->txBdCurrent[ringId] = handle->txBdBase[ringId];
  1621. }
  1622. else
  1623. {
  1624. handle->txBdCurrent[ringId]++;
  1625. }
  1626. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1627. /* Add the cache clean maintain. */
  1628. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1629. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1630. #else
  1631. address = (uint32_t)curBuffDescrip->buffer;
  1632. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1633. DCACHE_CleanByRange(address, length);
  1634. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1635. /* Active the transmit buffer descriptor. */
  1636. ENET_ActiveSend(base, ringId);
  1637. return kStatus_Success;
  1638. }
  1639. else
  1640. {
  1641. /* One frame requires more than one transmit buffers. */
  1642. do
  1643. {
  1644. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1645. /* For enable the timestamp. */
  1646. if (isPtpEventMessage)
  1647. {
  1648. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1649. }
  1650. else
  1651. {
  1652. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  1653. }
  1654. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  1655. /* Increase the buffer descriptor address. */
  1656. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1657. {
  1658. handle->txBdCurrent[ringId] = handle->txBdBase[ringId];
  1659. }
  1660. else
  1661. {
  1662. handle->txBdCurrent[ringId]++;
  1663. }
  1664. /* update the size left to be transmit. */
  1665. sizeleft = length - len;
  1666. if (sizeleft > handle->txBuffSizeAlign[ringId])
  1667. {
  1668. /* Data copy. */
  1669. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1670. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1671. #else
  1672. address = (uint32_t)curBuffDescrip->buffer;
  1673. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1674. memcpy((void*)address, data + len, handle->txBuffSizeAlign[ringId]);
  1675. /* Data length update. */
  1676. curBuffDescrip->length = handle->txBuffSizeAlign[ringId];
  1677. len += handle->txBuffSizeAlign[ringId];
  1678. /* Sets the control flag. */
  1679. curBuffDescrip->control &= ~ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  1680. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK;
  1681. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1682. /* Add the cache clean maintain. */
  1683. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1684. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1685. #else
  1686. address = (uint32_t)curBuffDescrip->buffer;
  1687. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1688. DCACHE_CleanByRange(address, handle->txBuffSizeAlign[ringId]);
  1689. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1690. /* Active the transmit buffer descriptor*/
  1691. ENET_ActiveSend(base, ringId);
  1692. }
  1693. else
  1694. {
  1695. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1696. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1697. #else
  1698. address = (uint32_t)curBuffDescrip->buffer;
  1699. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1700. memcpy((void *)address, data + len, sizeleft);
  1701. curBuffDescrip->length = sizeleft;
  1702. /* Set Last buffer wrap flag. */
  1703. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  1704. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1705. /* Add the cache clean maintain. */
  1706. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  1707. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  1708. #else
  1709. address = (uint32_t)curBuffDescrip->buffer;
  1710. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  1711. DCACHE_CleanByRange(address, sizeleft);
  1712. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1713. /* Active the transmit buffer descriptor. */
  1714. ENET_ActiveSend(base, ringId);
  1715. return kStatus_Success;
  1716. }
  1717. /* Get the current buffer descriptor address. */
  1718. curBuffDescrip = handle->txBdCurrent[ringId];
  1719. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1720. /* Add the cache invalidate maintain. */
  1721. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  1722. } while (!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK));
  1723. return kStatus_ENET_TxFrameBusy;
  1724. }
  1725. }
  1726. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  1727. void ENET_AddMulticastGroup(ENET_Type *base, uint8_t *address)
  1728. {
  1729. assert(address);
  1730. uint32_t crc = 0xFFFFFFFFU;
  1731. uint32_t count1 = 0;
  1732. uint32_t count2 = 0;
  1733. /* Calculates the CRC-32 polynomial on the multicast group address. */
  1734. for (count1 = 0; count1 < ENET_FRAME_MACLEN; count1++)
  1735. {
  1736. uint8_t c = address[count1];
  1737. for (count2 = 0; count2 < 0x08U; count2++)
  1738. {
  1739. if ((c ^ crc) & 1U)
  1740. {
  1741. crc >>= 1U;
  1742. c >>= 1U;
  1743. crc ^= 0xEDB88320U;
  1744. }
  1745. else
  1746. {
  1747. crc >>= 1U;
  1748. c >>= 1U;
  1749. }
  1750. }
  1751. }
  1752. /* Enable a multicast group address. */
  1753. if (!((crc >> 0x1FU) & 1U))
  1754. {
  1755. base->GALR |= 1U << ((crc >> 0x1AU) & 0x1FU);
  1756. }
  1757. else
  1758. {
  1759. base->GAUR |= 1U << ((crc >> 0x1AU) & 0x1FU);
  1760. }
  1761. }
  1762. void ENET_LeaveMulticastGroup(ENET_Type *base, uint8_t *address)
  1763. {
  1764. assert(address);
  1765. uint32_t crc = 0xFFFFFFFFU;
  1766. uint32_t count1 = 0;
  1767. uint32_t count2 = 0;
  1768. /* Calculates the CRC-32 polynomial on the multicast group address. */
  1769. for (count1 = 0; count1 < ENET_FRAME_MACLEN; count1++)
  1770. {
  1771. uint8_t c = address[count1];
  1772. for (count2 = 0; count2 < 0x08U; count2++)
  1773. {
  1774. if ((c ^ crc) & 1U)
  1775. {
  1776. crc >>= 1U;
  1777. c >>= 1U;
  1778. crc ^= 0xEDB88320U;
  1779. }
  1780. else
  1781. {
  1782. crc >>= 1U;
  1783. c >>= 1U;
  1784. }
  1785. }
  1786. }
  1787. /* Set the hash table. */
  1788. if (!((crc >> 0x1FU) & 1U))
  1789. {
  1790. base->GALR &= ~(1U << ((crc >> 0x1AU) & 0x1FU));
  1791. }
  1792. else
  1793. {
  1794. base->GAUR &= ~(1U << ((crc >> 0x1AU) & 0x1FU));
  1795. }
  1796. }
  1797. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  1798. status_t ENET_GetTxErrAfterSendFrame(enet_handle_t *handle, enet_data_error_stats_t *eErrorStatic)
  1799. {
  1800. assert(handle);
  1801. assert(eErrorStatic);
  1802. uint16_t control = 0;
  1803. uint16_t controlExt = 0;
  1804. do
  1805. {
  1806. /* Get the current dirty transmit buffer descriptor. */
  1807. control = handle->txBdDirtyStatic[0]->control;
  1808. controlExt = handle->txBdDirtyStatic[0]->controlExtend0;
  1809. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  1810. if (control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1811. {
  1812. return kStatus_ENET_TxFrameBusy;
  1813. }
  1814. /* Increase the transmit dirty static pointer. */
  1815. if (handle->txBdDirtyStatic[0]->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1816. {
  1817. handle->txBdDirtyStatic[0] = handle->txBdBase[0];
  1818. }
  1819. else
  1820. {
  1821. handle->txBdDirtyStatic[0]++;
  1822. }
  1823. /* If the transmit buffer descriptor is ready and the last buffer descriptor, store packet statistic. */
  1824. if (control & ENET_BUFFDESCRIPTOR_TX_LAST_MASK)
  1825. {
  1826. if (controlExt & ENET_BUFFDESCRIPTOR_TX_ERR_MASK)
  1827. {
  1828. /* Transmit error. */
  1829. eErrorStatic->statsTxErr++;
  1830. }
  1831. if (controlExt & ENET_BUFFDESCRIPTOR_TX_EXCCOLLISIONERR_MASK)
  1832. {
  1833. /* Transmit excess collision error. */
  1834. eErrorStatic->statsTxExcessCollisionErr++;
  1835. }
  1836. if (controlExt & ENET_BUFFDESCRIPTOR_TX_LATECOLLISIONERR_MASK)
  1837. {
  1838. /* Transmit late collision error. */
  1839. eErrorStatic->statsTxLateCollisionErr++;
  1840. }
  1841. if (controlExt & ENET_BUFFDESCRIPTOR_TX_UNDERFLOWERR_MASK)
  1842. {
  1843. /* Transmit under flow error. */
  1844. eErrorStatic->statsTxUnderFlowErr++;
  1845. }
  1846. if (controlExt & ENET_BUFFDESCRIPTOR_TX_OVERFLOWERR_MASK)
  1847. {
  1848. /* Transmit over flow error. */
  1849. eErrorStatic->statsTxOverFlowErr++;
  1850. }
  1851. return kStatus_Success;
  1852. }
  1853. } while (handle->txBdDirtyStatic[0] != handle->txBdCurrent[0]);
  1854. return kStatus_ENET_TxFrameFail;
  1855. }
  1856. #if FSL_FEATURE_ENET_QUEUE > 1
  1857. status_t ENET_GetTxErrAfterSendFrameMultiRing(enet_handle_t *handle, enet_data_error_stats_t *eErrorStatic,
  1858. uint32_t ringId)
  1859. {
  1860. assert(handle);
  1861. assert(eErrorStatic);
  1862. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  1863. uint16_t control = 0;
  1864. uint16_t controlExt = 0;
  1865. do
  1866. {
  1867. /* Get the current dirty transmit buffer descriptor. */
  1868. control = handle->txBdDirtyStatic[ringId]->control;
  1869. controlExt = handle->txBdDirtyStatic[ringId]->controlExtend0;
  1870. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  1871. if (control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  1872. {
  1873. return kStatus_ENET_TxFrameBusy;
  1874. }
  1875. /* Increase the transmit dirty static pointer. */
  1876. if (handle->txBdDirtyStatic[ringId]->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  1877. {
  1878. handle->txBdDirtyStatic[ringId] = handle->txBdBase[ringId];
  1879. }
  1880. else
  1881. {
  1882. handle->txBdDirtyStatic[ringId]++;
  1883. }
  1884. /* If the transmit buffer descriptor is ready and the last buffer descriptor, store packet statistic. */
  1885. if (control & ENET_BUFFDESCRIPTOR_TX_LAST_MASK)
  1886. {
  1887. if (controlExt & ENET_BUFFDESCRIPTOR_TX_ERR_MASK)
  1888. {
  1889. /* Transmit error. */
  1890. eErrorStatic->statsTxErr++;
  1891. }
  1892. if (controlExt & ENET_BUFFDESCRIPTOR_TX_EXCCOLLISIONERR_MASK)
  1893. {
  1894. /* Transmit excess collision error. */
  1895. eErrorStatic->statsTxExcessCollisionErr++;
  1896. }
  1897. if (controlExt & ENET_BUFFDESCRIPTOR_TX_LATECOLLISIONERR_MASK)
  1898. {
  1899. /* Transmit late collision error. */
  1900. eErrorStatic->statsTxLateCollisionErr++;
  1901. }
  1902. if (controlExt & ENET_BUFFDESCRIPTOR_TX_UNDERFLOWERR_MASK)
  1903. {
  1904. /* Transmit under flow error. */
  1905. eErrorStatic->statsTxUnderFlowErr++;
  1906. }
  1907. if (controlExt & ENET_BUFFDESCRIPTOR_TX_OVERFLOWERR_MASK)
  1908. {
  1909. /* Transmit over flow error. */
  1910. eErrorStatic->statsTxOverFlowErr++;
  1911. }
  1912. return kStatus_Success;
  1913. }
  1914. } while (handle->txBdDirtyStatic[ringId] != handle->txBdCurrent[ringId]);
  1915. return kStatus_ENET_TxFrameFail;
  1916. }
  1917. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  1918. static bool ENET_Ptp1588ParseFrame(const uint8_t *data, enet_ptp_time_data_t *ptpTsData, bool isFastEnabled)
  1919. {
  1920. assert(data);
  1921. if (!isFastEnabled)
  1922. {
  1923. assert(ptpTsData);
  1924. }
  1925. bool isPtpMsg = false;
  1926. const uint8_t *buffer = data;
  1927. uint16_t ptpType;
  1928. /* Check for VLAN frame.
  1929. * Add Double vlan tag check for receiving extended QIN vlan frame. */
  1930. if (*(uint16_t *)(buffer + ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET) == (ENET_HTONS(ENET_8021QVLAN)
  1931. #if defined(FSL_FEATUR_ENET_HAS_AVB) && FSL_FEATURE_HAS_AVB
  1932. || ENET_HTONS(ENET_8021QSVLAN)
  1933. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  1934. ))
  1935. {
  1936. buffer += ENET_FRAME_VLAN_TAGLEN;
  1937. #if defined(FSL_FEATUR_ENET_HAS_AVB) && FSL_FEATURE_HAS_AVB
  1938. if (*(uint16_t *)(buffer + ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET) == ENET_HTONS(ENET_8021QVLAN)
  1939. {
  1940. buffer += ENET_FRAME_VLAN_TAGLEN;
  1941. }
  1942. #endif /* FSL_FEATURE_ENET_HAS_AVB */
  1943. }
  1944. ptpType = *(uint16_t *)(buffer + ENET_PTP1588_ETHL2_PACKETTYPE_OFFSET);
  1945. switch (ENET_HTONS(ptpType))
  1946. { /* Ethernet layer 2. */
  1947. case ENET_ETHERNETL2:
  1948. if (*(uint8_t *)(buffer + ENET_PTP1588_ETHL2_MSGTYPE_OFFSET) <= kENET_PtpEventMsgType)
  1949. {
  1950. isPtpMsg = true;
  1951. if (!isFastEnabled)
  1952. {
  1953. /* It's a ptpv2 message and store the ptp header information. */
  1954. ptpTsData->version = (*(uint8_t *)(buffer + ENET_PTP1588_ETHL2_VERSION_OFFSET)) & 0x0F;
  1955. ptpTsData->messageType = (*(uint8_t *)(buffer + ENET_PTP1588_ETHL2_MSGTYPE_OFFSET)) & 0x0F;
  1956. ptpTsData->sequenceId = ENET_HTONS(*(uint16_t *)(buffer + ENET_PTP1588_ETHL2_SEQUENCEID_OFFSET));
  1957. memcpy((void *)&ptpTsData->sourcePortId[0], (void *)(buffer + ENET_PTP1588_ETHL2_CLOCKID_OFFSET),
  1958. kENET_PtpSrcPortIdLen);
  1959. }
  1960. }
  1961. break;
  1962. /* IPV4. */
  1963. case ENET_IPV4:
  1964. if ((*(uint8_t *)(buffer + ENET_PTP1588_IPVERSION_OFFSET) >> 4) == ENET_IPV4VERSION)
  1965. {
  1966. if (((*(uint16_t *)(buffer + ENET_PTP1588_IPV4_UDP_PORT_OFFSET)) == ENET_HTONS(kENET_PtpEventPort)) &&
  1967. (*(uint8_t *)(buffer + ENET_PTP1588_IPV4_UDP_PROTOCOL_OFFSET) == ENET_UDPVERSION))
  1968. {
  1969. /* Set the PTP message flag. */
  1970. isPtpMsg = true;
  1971. if (!isFastEnabled)
  1972. {
  1973. /* It's a IPV4 ptp message and store the ptp header information. */
  1974. ptpTsData->version = (*(uint8_t *)(buffer + ENET_PTP1588_IPV4_UDP_VERSION_OFFSET)) & 0x0F;
  1975. ptpTsData->messageType = (*(uint8_t *)(buffer + ENET_PTP1588_IPV4_UDP_MSGTYPE_OFFSET)) & 0x0F;
  1976. ptpTsData->sequenceId =
  1977. ENET_HTONS(*(uint16_t *)(buffer + ENET_PTP1588_IPV4_UDP_SEQUENCEID_OFFSET));
  1978. memcpy((void *)&ptpTsData->sourcePortId[0],
  1979. (void *)(buffer + ENET_PTP1588_IPV4_UDP_CLKID_OFFSET), kENET_PtpSrcPortIdLen);
  1980. }
  1981. }
  1982. }
  1983. break;
  1984. /* IPV6. */
  1985. case ENET_IPV6:
  1986. if ((*(uint8_t *)(buffer + ENET_PTP1588_IPVERSION_OFFSET) >> 4) == ENET_IPV6VERSION)
  1987. {
  1988. if (((*(uint16_t *)(buffer + ENET_PTP1588_IPV6_UDP_PORT_OFFSET)) == ENET_HTONS(kENET_PtpEventPort)) &&
  1989. (*(uint8_t *)(buffer + ENET_PTP1588_IPV6_UDP_PROTOCOL_OFFSET) == ENET_UDPVERSION))
  1990. {
  1991. /* Set the PTP message flag. */
  1992. isPtpMsg = true;
  1993. if (!isFastEnabled)
  1994. {
  1995. /* It's a IPV6 ptp message and store the ptp header information. */
  1996. ptpTsData->version = (*(uint8_t *)(buffer + ENET_PTP1588_IPV6_UDP_VERSION_OFFSET)) & 0x0F;
  1997. ptpTsData->messageType = (*(uint8_t *)(buffer + ENET_PTP1588_IPV6_UDP_MSGTYPE_OFFSET)) & 0x0F;
  1998. ptpTsData->sequenceId =
  1999. ENET_HTONS(*(uint16_t *)(buffer + ENET_PTP1588_IPV6_UDP_SEQUENCEID_OFFSET));
  2000. memcpy((void *)&ptpTsData->sourcePortId[0],
  2001. (void *)(buffer + ENET_PTP1588_IPV6_UDP_CLKID_OFFSET), kENET_PtpSrcPortIdLen);
  2002. }
  2003. }
  2004. }
  2005. break;
  2006. default:
  2007. break;
  2008. }
  2009. return isPtpMsg;
  2010. }
  2011. void ENET_Ptp1588Configure(ENET_Type *base, enet_handle_t *handle, enet_ptp_config_t *ptpConfig)
  2012. {
  2013. assert(handle);
  2014. assert(ptpConfig);
  2015. uint8_t count;
  2016. uint32_t instance = ENET_GetInstance(base);
  2017. uint32_t mask = kENET_TxBufferInterrupt;
  2018. #if FSL_FEATURE_ENET_QUEUE > 1
  2019. mask |= kENET_TxBuffer1Interrupt | kENET_TxBuffer2Interrupt;
  2020. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2021. /* Start the 1588 timer. */
  2022. ENET_Ptp1588StartTimer(base, ptpConfig->ptp1588ClockSrc_Hz);
  2023. for (count = 0; count < handle->ringNum; count++)
  2024. {
  2025. handle->txBdDirtyTime[count] = handle->txBdBase[count];
  2026. handle->txBdDirtyStatic[count] = handle->txBdBase[count];
  2027. }
  2028. /* Setting the receive and transmit state for transaction. */
  2029. handle->rxPtpTsDataRing.ptpTsData = ptpConfig->rxPtpTsData;
  2030. handle->rxPtpTsDataRing.size = ptpConfig->ptpTsRxBuffNum;
  2031. handle->rxPtpTsDataRing.front = 0;
  2032. handle->rxPtpTsDataRing.end = 0;
  2033. handle->txPtpTsDataRing.ptpTsData = ptpConfig->txPtpTsData;
  2034. handle->txPtpTsDataRing.size = ptpConfig->ptpTsTxBuffNum;
  2035. handle->txPtpTsDataRing.front = 0;
  2036. handle->txPtpTsDataRing.end = 0;
  2037. handle->msTimerSecond = 0;
  2038. /* Set the IRQ handler when the interrupt is enabled. */
  2039. s_enetTxIsr = ENET_TransmitIRQHandler;
  2040. s_enetTsIsr = ENET_Ptp1588TimerIRQHandler;
  2041. /* Enables the time stamp interrupt and transmit frame interrupt to
  2042. * handle the time-stamp . */
  2043. ENET_EnableInterrupts(base, (ENET_TS_INTERRUPT | ENET_TX_INTERRUPT));
  2044. ENET_DisableInterrupts(base, mask);
  2045. EnableIRQ(s_enetTsIrqId[instance]);
  2046. EnableIRQ(s_enetTxIrqId[instance]);
  2047. }
  2048. void ENET_Ptp1588StartTimer(ENET_Type *base, uint32_t ptpClkSrc)
  2049. {
  2050. /* Restart PTP 1588 timer, master clock. */
  2051. base->ATCR = ENET_ATCR_RESTART_MASK;
  2052. /* Initializes PTP 1588 timer. */
  2053. base->ATINC = ENET_ATINC_INC(ENET_NANOSECOND_ONE_SECOND / ptpClkSrc);
  2054. base->ATPER = ENET_NANOSECOND_ONE_SECOND;
  2055. /* Sets periodical event and the event signal output assertion and Actives PTP 1588 timer. */
  2056. base->ATCR = ENET_ATCR_PEREN_MASK | ENET_ATCR_PINPER_MASK | ENET_ATCR_EN_MASK;
  2057. }
  2058. void ENET_Ptp1588GetTimer(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_t *ptpTime)
  2059. {
  2060. assert(handle);
  2061. assert(ptpTime);
  2062. uint16_t count = ENET_1588TIME_DELAY_COUNT;
  2063. uint32_t primask;
  2064. /* Disables the interrupt. */
  2065. primask = DisableGlobalIRQ();
  2066. /* Get the current PTP time. */
  2067. ptpTime->second = handle->msTimerSecond;
  2068. /* Get the nanosecond from the master timer. */
  2069. base->ATCR |= ENET_ATCR_CAPTURE_MASK;
  2070. /* Add at least six clock cycle delay to get accurate time.
  2071. It's the requirement when the 1588 clock source is slower
  2072. than the register clock.
  2073. */
  2074. while (count--)
  2075. {
  2076. __NOP();
  2077. }
  2078. /* Get the captured time. */
  2079. ptpTime->nanosecond = base->ATVR;
  2080. /* Enables the interrupt. */
  2081. EnableGlobalIRQ(primask);
  2082. }
  2083. void ENET_Ptp1588SetTimer(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_t *ptpTime)
  2084. {
  2085. assert(handle);
  2086. assert(ptpTime);
  2087. uint32_t primask;
  2088. /* Disables the interrupt. */
  2089. primask = DisableGlobalIRQ();
  2090. /* Sets PTP timer. */
  2091. handle->msTimerSecond = ptpTime->second;
  2092. base->ATVR = ptpTime->nanosecond;
  2093. /* Enables the interrupt. */
  2094. EnableGlobalIRQ(primask);
  2095. }
  2096. void ENET_Ptp1588AdjustTimer(ENET_Type *base, uint32_t corrIncrease, uint32_t corrPeriod)
  2097. {
  2098. /* Set correction for PTP timer increment. */
  2099. base->ATINC = (base->ATINC & ~ENET_ATINC_INC_CORR_MASK) | (corrIncrease << ENET_ATINC_INC_CORR_SHIFT);
  2100. /* Set correction for PTP timer period. */
  2101. base->ATCOR = (base->ATCOR & ~ENET_ATCOR_COR_MASK) | (corrPeriod << ENET_ATCOR_COR_SHIFT);
  2102. }
  2103. static status_t ENET_Ptp1588UpdateTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimeData)
  2104. {
  2105. assert(ptpTsDataRing);
  2106. assert(ptpTsDataRing->ptpTsData);
  2107. assert(ptpTimeData);
  2108. uint16_t usedBuffer = 0;
  2109. /* Check if the buffers ring is full. */
  2110. if (ptpTsDataRing->end >= ptpTsDataRing->front)
  2111. {
  2112. usedBuffer = ptpTsDataRing->end - ptpTsDataRing->front;
  2113. }
  2114. else
  2115. {
  2116. usedBuffer = ptpTsDataRing->size - (ptpTsDataRing->front - ptpTsDataRing->end);
  2117. }
  2118. if (usedBuffer == ptpTsDataRing->size)
  2119. {
  2120. return kStatus_ENET_PtpTsRingFull;
  2121. }
  2122. /* Copy the new data into the buffer. */
  2123. memcpy((ptpTsDataRing->ptpTsData + ptpTsDataRing->end), ptpTimeData, sizeof(enet_ptp_time_data_t));
  2124. /* Increase the buffer pointer to the next empty one. */
  2125. ptpTsDataRing->end = (ptpTsDataRing->end + 1) % ptpTsDataRing->size;
  2126. return kStatus_Success;
  2127. }
  2128. static status_t ENET_Ptp1588SearchTimeRing(enet_ptp_time_data_ring_t *ptpTsDataRing, enet_ptp_time_data_t *ptpTimedata)
  2129. {
  2130. assert(ptpTsDataRing);
  2131. assert(ptpTsDataRing->ptpTsData);
  2132. assert(ptpTimedata);
  2133. uint32_t index;
  2134. uint32_t size;
  2135. uint16_t usedBuffer = 0;
  2136. /* Check the PTP 1588 timestamp ring. */
  2137. if (ptpTsDataRing->front == ptpTsDataRing->end)
  2138. {
  2139. return kStatus_ENET_PtpTsRingEmpty;
  2140. }
  2141. /* Search the element in the ring buffer */
  2142. index = ptpTsDataRing->front;
  2143. size = ptpTsDataRing->size;
  2144. while (index != ptpTsDataRing->end)
  2145. {
  2146. if (((ptpTsDataRing->ptpTsData + index)->sequenceId == ptpTimedata->sequenceId) &&
  2147. (!memcmp(((void *)&(ptpTsDataRing->ptpTsData + index)->sourcePortId[0]),
  2148. (void *)&ptpTimedata->sourcePortId[0], kENET_PtpSrcPortIdLen)) &&
  2149. ((ptpTsDataRing->ptpTsData + index)->version == ptpTimedata->version) &&
  2150. ((ptpTsDataRing->ptpTsData + index)->messageType == ptpTimedata->messageType))
  2151. {
  2152. break;
  2153. }
  2154. /* Increase the ptp ring index. */
  2155. index = (index + 1) % size;
  2156. }
  2157. if (index == ptpTsDataRing->end)
  2158. {
  2159. /* Check if buffers is full. */
  2160. if (ptpTsDataRing->end >= ptpTsDataRing->front)
  2161. {
  2162. usedBuffer = ptpTsDataRing->end - ptpTsDataRing->front;
  2163. }
  2164. else
  2165. {
  2166. usedBuffer = ptpTsDataRing->size - (ptpTsDataRing->front - ptpTsDataRing->end);
  2167. }
  2168. if (usedBuffer == ptpTsDataRing->size)
  2169. { /* Drop one in the front. */
  2170. ptpTsDataRing->front = (ptpTsDataRing->front + 1) % size;
  2171. }
  2172. return kStatus_ENET_PtpTsRingFull;
  2173. }
  2174. /* Get the right timestamp of the required ptp messag. */
  2175. ptpTimedata->timeStamp.second = (ptpTsDataRing->ptpTsData + index)->timeStamp.second;
  2176. ptpTimedata->timeStamp.nanosecond = (ptpTsDataRing->ptpTsData + index)->timeStamp.nanosecond;
  2177. /* Increase the index. */
  2178. ptpTsDataRing->front = (ptpTsDataRing->front + 1) % size;
  2179. return kStatus_Success;
  2180. }
  2181. static status_t ENET_StoreRxFrameTime(ENET_Type *base, enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData)
  2182. {
  2183. assert(handle);
  2184. assert(ptpTimeData);
  2185. bool ptpTimerWrap = false;
  2186. enet_ptp_time_t ptpTimer;
  2187. uint32_t primask;
  2188. /* Disables the interrupt. */
  2189. primask = DisableGlobalIRQ();
  2190. /* Get current PTP timer nanosecond value. */
  2191. ENET_Ptp1588GetTimer(base, handle, &ptpTimer);
  2192. /* Get PTP timer wrap event. */
  2193. ptpTimerWrap = base->EIR & kENET_TsTimerInterrupt;
  2194. /* Get transmit time stamp second. */
  2195. if ((ptpTimer.nanosecond > ptpTimeData->timeStamp.nanosecond) ||
  2196. ((ptpTimer.nanosecond < ptpTimeData->timeStamp.nanosecond) && ptpTimerWrap))
  2197. {
  2198. ptpTimeData->timeStamp.second = handle->msTimerSecond;
  2199. }
  2200. else
  2201. {
  2202. ptpTimeData->timeStamp.second = handle->msTimerSecond - 1;
  2203. }
  2204. /* Enable the interrupt. */
  2205. EnableGlobalIRQ(primask);
  2206. /* Store the timestamp to the receive time stamp ring. */
  2207. /* Check if the buffers ring is full. */
  2208. return ENET_Ptp1588UpdateTimeRing(&handle->rxPtpTsDataRing, ptpTimeData);
  2209. }
  2210. static status_t ENET_StoreTxFrameTime(ENET_Type *base, enet_handle_t *handle, uint32_t ringId)
  2211. {
  2212. assert(handle);
  2213. uint32_t primask;
  2214. bool ptpTimerWrap;
  2215. bool isPtpEventMessage = false;
  2216. enet_ptp_time_data_t ptpTimeData;
  2217. volatile enet_tx_bd_struct_t *curBuffDescrip = handle->txBdDirtyTime[ringId];
  2218. uint32_t address;
  2219. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  2220. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  2221. {
  2222. return kStatus_ENET_TxFrameBusy;
  2223. }
  2224. /* Parse the PTP message. */
  2225. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  2226. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  2227. #else
  2228. address = (uint32_t)curBuffDescrip->buffer;
  2229. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  2230. isPtpEventMessage = ENET_Ptp1588ParseFrame((uint8_t *)address, &ptpTimeData, false);
  2231. if (isPtpEventMessage)
  2232. {
  2233. do
  2234. {
  2235. /* Increase current buffer descriptor to the next one. */
  2236. if (handle->txBdDirtyTime[ringId]->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  2237. {
  2238. handle->txBdDirtyTime[ringId] = handle->txBdBase[ringId];
  2239. }
  2240. else
  2241. {
  2242. handle->txBdDirtyTime[ringId]++;
  2243. }
  2244. /* Do time stamp check on the last buffer descriptor of the frame. */
  2245. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_LAST_MASK)
  2246. {
  2247. /* Disables the interrupt. */
  2248. primask = DisableGlobalIRQ();
  2249. /* Get current PTP timer nanosecond value. */
  2250. ENET_Ptp1588GetTimer(base, handle, &ptpTimeData.timeStamp);
  2251. /* Get PTP timer wrap event. */
  2252. ptpTimerWrap = base->EIR & kENET_TsTimerInterrupt;
  2253. /* Get transmit time stamp second. */
  2254. if ((ptpTimeData.timeStamp.nanosecond > curBuffDescrip->timestamp) ||
  2255. ((ptpTimeData.timeStamp.nanosecond < curBuffDescrip->timestamp) && ptpTimerWrap))
  2256. {
  2257. ptpTimeData.timeStamp.second = handle->msTimerSecond;
  2258. }
  2259. else
  2260. {
  2261. ptpTimeData.timeStamp.second = handle->msTimerSecond - 1;
  2262. }
  2263. /* Enable the interrupt. */
  2264. EnableGlobalIRQ(primask);
  2265. /* Store the timestamp to the transmit timestamp ring. */
  2266. return ENET_Ptp1588UpdateTimeRing(&handle->txPtpTsDataRing, &ptpTimeData);
  2267. }
  2268. /* Get the current transmit buffer descriptor. */
  2269. curBuffDescrip = handle->txBdDirtyTime[ringId];
  2270. /* Get the control status data, If the buffer descriptor has not been processed break out. */
  2271. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  2272. {
  2273. return kStatus_ENET_TxFrameBusy;
  2274. }
  2275. } while (handle->txBdDirtyTime[ringId] != handle->txBdCurrent[ringId]);
  2276. return kStatus_ENET_TxFrameFail;
  2277. }
  2278. return kStatus_Success;
  2279. }
  2280. status_t ENET_GetTxFrameTime(enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData)
  2281. {
  2282. assert(handle);
  2283. assert(ptpTimeData);
  2284. return ENET_Ptp1588SearchTimeRing(&handle->txPtpTsDataRing, ptpTimeData);
  2285. }
  2286. status_t ENET_GetRxFrameTime(enet_handle_t *handle, enet_ptp_time_data_t *ptpTimeData)
  2287. {
  2288. assert(handle);
  2289. assert(ptpTimeData);
  2290. return ENET_Ptp1588SearchTimeRing(&handle->rxPtpTsDataRing, ptpTimeData);
  2291. }
  2292. #if defined(FSL_FEATURE_ENET_HAS_AVB) && FSL_FEATURE_ENET_HAS_AVB
  2293. void ENET_AVBConfigure(ENET_Type *base, enet_handle_t *handle, const enet_avb_config_t *config)
  2294. {
  2295. assert(config);
  2296. uint8_t count = 0;
  2297. for (count = 0; count < FSL_FEATURE_ENET_QUEUE - 1; count++)
  2298. {
  2299. /* Set the AVB receive ring classification match when the match is not 0. */
  2300. if (config->rxClassifyMatch[count])
  2301. {
  2302. base->RCMR[count] = (config->rxClassifyMatch[count] & 0xFFFF) | ENET_RCMR_MATCHEN_MASK;
  2303. }
  2304. /* Set the dma controller for the extended ring. */
  2305. base->DMACFG[count] |= ENET_DMACFG_IDLE_SLOPE(config->idleSlope[count]);
  2306. }
  2307. /* Shall use the credit-based scheme for avb. */
  2308. base->QOS &= ~ENET_QOS_TX_SCHEME_MASK;
  2309. base->QOS |= ENET_QOS_RX_FLUSH0_MASK;
  2310. }
  2311. #endif /* FSL_FETAURE_ENET_HAS_AVB */
  2312. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  2313. #if FSL_FEATURE_ENET_QUEUE > 1
  2314. void ENET_TransmitIRQHandler(ENET_Type *base, enet_handle_t *handle, uint32_t ringId)
  2315. #else
  2316. void ENET_TransmitIRQHandler(ENET_Type *base, enet_handle_t *handle)
  2317. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2318. {
  2319. assert(handle);
  2320. uint32_t mask = kENET_TxBufferInterrupt | kENET_TxFrameInterrupt;
  2321. #if defined(ENET_ENHANCEDBUFFERDESCRIPTOR_MODE) || (FSL_FEATURE_ENET_QUEUE > 1)
  2322. uint32_t index = 0;
  2323. #endif /* ENET_ENHANCEDBUFFERDESCRIPTORMODE || (FSL_FEATURE_ENET_QUEUE > 1) */
  2324. /* Check if the transmit interrupt happen. */
  2325. #if FSL_FEATURE_ENET_QUEUE > 1
  2326. switch (ringId)
  2327. {
  2328. case kENET_Ring1:
  2329. mask = (kENET_TxFrame1Interrupt | kENET_TxBuffer1Interrupt);
  2330. break;
  2331. case kENET_Ring2:
  2332. mask = (kENET_TxFrame2Interrupt | kENET_TxBuffer2Interrupt);
  2333. break;
  2334. default:
  2335. break;
  2336. }
  2337. index = ringId;
  2338. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2339. while (mask & base->EIR)
  2340. {
  2341. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  2342. if (base->EIR & kENET_TxFrameInterrupt)
  2343. {
  2344. /* Store the transmit timestamp from the buffer descriptor should be done here. */
  2345. ENET_StoreTxFrameTime(base, handle, index);
  2346. }
  2347. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  2348. /* Clear the transmit interrupt event. */
  2349. base->EIR = mask;
  2350. /* Callback function. */
  2351. if (handle->callback)
  2352. {
  2353. #if FSL_FEATURE_ENET_QUEUE > 1
  2354. handle->callback(base, handle, index, kENET_TxEvent, handle->userData);
  2355. #else
  2356. handle->callback(base, handle, kENET_TxEvent, handle->userData);
  2357. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2358. }
  2359. }
  2360. }
  2361. #if FSL_FEATURE_ENET_QUEUE > 1
  2362. void ENET_ReceiveIRQHandler(ENET_Type *base, enet_handle_t *handle, uint32_t ringId)
  2363. #else
  2364. void ENET_ReceiveIRQHandler(ENET_Type *base, enet_handle_t *handle)
  2365. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2366. {
  2367. assert(handle);
  2368. uint32_t mask = kENET_RxFrameInterrupt | kENET_RxBufferInterrupt;
  2369. /* Check if the receive interrupt happen. */
  2370. #if FSL_FEATURE_ENET_QUEUE > 1
  2371. switch (ringId)
  2372. {
  2373. case kENET_Ring1:
  2374. mask = (kENET_RxFrame1Interrupt | kENET_RxBuffer1Interrupt);
  2375. break;
  2376. case kENET_Ring2:
  2377. mask = (kENET_RxFrame2Interrupt | kENET_RxBuffer2Interrupt);
  2378. break;
  2379. default:
  2380. break;
  2381. }
  2382. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2383. while (mask & base->EIR)
  2384. {
  2385. /* Clear the transmit interrupt event. */
  2386. base->EIR = mask;
  2387. /* Callback function. */
  2388. if (handle->callback)
  2389. {
  2390. #if FSL_FEATURE_ENET_QUEUE > 1
  2391. handle->callback(base, handle, ringId, kENET_RxEvent, handle->userData);
  2392. #else
  2393. handle->callback(base, handle, kENET_RxEvent, handle->userData);
  2394. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2395. }
  2396. }
  2397. }
  2398. void ENET_ErrorIRQHandler(ENET_Type *base, enet_handle_t *handle)
  2399. {
  2400. assert(handle);
  2401. uint32_t errMask = kENET_BabrInterrupt | kENET_BabtInterrupt | kENET_EBusERInterrupt | kENET_PayloadRxInterrupt |
  2402. kENET_LateCollisionInterrupt | kENET_RetryLimitInterrupt | kENET_UnderrunInterrupt;
  2403. /* Check if the error interrupt happen. */
  2404. if (kENET_WakeupInterrupt & base->EIR)
  2405. {
  2406. /* Clear the wakeup interrupt. */
  2407. base->EIR = kENET_WakeupInterrupt;
  2408. /* wake up and enter the normal mode. */
  2409. ENET_EnableSleepMode(base, false);
  2410. /* Callback function. */
  2411. if (handle->callback)
  2412. {
  2413. #if FSL_FEATURE_ENET_QUEUE > 1
  2414. handle->callback(base, handle, 0, kENET_WakeUpEvent, handle->userData);
  2415. #else
  2416. handle->callback(base, handle, kENET_WakeUpEvent, handle->userData);
  2417. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2418. }
  2419. }
  2420. else
  2421. {
  2422. /* Clear the error interrupt event status. */
  2423. errMask &= base->EIR;
  2424. base->EIR = errMask;
  2425. /* Callback function. */
  2426. if (handle->callback)
  2427. {
  2428. #if FSL_FEATURE_ENET_QUEUE > 1
  2429. handle->callback(base, handle, 0, kENET_ErrEvent, handle->userData);
  2430. #else
  2431. handle->callback(base, handle, kENET_ErrEvent, handle->userData);
  2432. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2433. }
  2434. }
  2435. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2436. exception return operation might vector to incorrect interrupt */
  2437. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2438. __DSB();
  2439. #endif
  2440. }
  2441. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  2442. void ENET_Ptp1588TimerIRQHandler(ENET_Type *base, enet_handle_t *handle)
  2443. {
  2444. assert(handle);
  2445. /* Check if the PTP time stamp interrupt happen. */
  2446. if (kENET_TsTimerInterrupt & base->EIR)
  2447. {
  2448. /* Clear the time stamp interrupt. */
  2449. base->EIR = kENET_TsTimerInterrupt;
  2450. /* Increase timer second counter. */
  2451. handle->msTimerSecond++;
  2452. /* Callback function. */
  2453. if (handle->callback)
  2454. {
  2455. #if FSL_FEATURE_ENET_QUEUE > 1
  2456. handle->callback(base, handle, 0, kENET_TimeStampEvent, handle->userData);
  2457. #else
  2458. handle->callback(base, handle, kENET_TimeStampEvent, handle->userData);
  2459. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2460. }
  2461. }
  2462. else
  2463. {
  2464. /* Clear the time stamp interrupt. */
  2465. base->EIR = kENET_TsAvailInterrupt;
  2466. /* Callback function. */
  2467. if (handle->callback)
  2468. {
  2469. #if FSL_FEATURE_ENET_QUEUE > 1
  2470. handle->callback(base, handle, 0, kENET_TimeStampAvailEvent, handle->userData);
  2471. #else
  2472. handle->callback(base, handle, kENET_TimeStampAvailEvent, handle->userData);
  2473. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2474. }
  2475. }
  2476. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2477. exception return operation might vector to incorrect interrupt */
  2478. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2479. __DSB();
  2480. #endif
  2481. }
  2482. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  2483. void ENET_CommonFrame0IRQHandler(ENET_Type *base)
  2484. {
  2485. uint32_t event = base->EIR;
  2486. uint32_t instance = ENET_GetInstance(base);
  2487. if (event & (kENET_TxBufferInterrupt | kENET_TxFrameInterrupt))
  2488. {
  2489. #if FSL_FEATURE_ENET_QUEUE > 1
  2490. s_enetTxIsr(base, s_ENETHandle[instance], 0);
  2491. #else
  2492. s_enetTxIsr(base, s_ENETHandle[instance]);
  2493. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2494. }
  2495. if (event & (kENET_RxBufferInterrupt | kENET_RxFrameInterrupt))
  2496. {
  2497. #if FSL_FEATURE_ENET_QUEUE > 1
  2498. s_enetRxIsr(base, s_ENETHandle[instance], 0);
  2499. #else
  2500. s_enetRxIsr(base, s_ENETHandle[instance]);
  2501. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2502. }
  2503. if (event & ENET_TS_INTERRUPT)
  2504. {
  2505. s_enetTsIsr(base, s_ENETHandle[instance]);
  2506. }
  2507. if (event & ENET_ERR_INTERRUPT)
  2508. {
  2509. s_enetErrIsr(base, s_ENETHandle[instance]);
  2510. }
  2511. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2512. exception return operation might vector to incorrect interrupt */
  2513. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2514. __DSB();
  2515. #endif
  2516. }
  2517. #if FSL_FEATURE_ENET_QUEUE > 1
  2518. void ENET_CommonFrame1IRQHandler(ENET_Type *base)
  2519. {
  2520. uint32_t event = base->EIR;
  2521. uint32_t instance = ENET_GetInstance(base);
  2522. if (event & (kENET_TxBuffer1Interrupt | kENET_TxFrame1Interrupt))
  2523. {
  2524. s_enetTxIsr(base, s_ENETHandle[instance], 1);
  2525. }
  2526. if (event & (kENET_RxBuffer1Interrupt | kENET_RxFrame1Interrupt))
  2527. {
  2528. s_enetRxIsr(base, s_ENETHandle[instance], 1);
  2529. }
  2530. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2531. exception return operation might vector to incorrect interrupt */
  2532. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2533. __DSB();
  2534. #endif
  2535. }
  2536. void ENET_CommonFrame2IRQHandler(ENET_Type *base)
  2537. {
  2538. uint32_t event = base->EIR;
  2539. uint32_t instance = ENET_GetInstance(base);
  2540. if (event & (kENET_TxBuffer2Interrupt | kENET_TxFrame2Interrupt))
  2541. {
  2542. s_enetTxIsr(base, s_ENETHandle[instance], 2);
  2543. }
  2544. if (event & (kENET_RxBuffer2Interrupt | kENET_RxFrame2Interrupt))
  2545. {
  2546. s_enetRxIsr(base, s_ENETHandle[instance], 2);
  2547. }
  2548. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2549. exception return operation might vector to incorrect interrupt */
  2550. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2551. __DSB();
  2552. #endif
  2553. }
  2554. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  2555. #if defined(ENET)
  2556. void ENET_Transmit_IRQHandler(void)
  2557. {
  2558. s_enetTxIsr(ENET, s_ENETHandle[0]);
  2559. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2560. exception return operation might vector to incorrect interrupt */
  2561. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2562. __DSB();
  2563. #endif
  2564. }
  2565. void ENET_Receive_IRQHandler(void)
  2566. {
  2567. s_enetRxIsr(ENET, s_ENETHandle[0]);
  2568. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2569. exception return operation might vector to incorrect interrupt */
  2570. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2571. __DSB();
  2572. #endif
  2573. }
  2574. void ENET_Error_IRQHandler(void)
  2575. {
  2576. s_enetErrIsr(ENET, s_ENETHandle[0]);
  2577. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2578. exception return operation might vector to incorrect interrupt */
  2579. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2580. __DSB();
  2581. #endif
  2582. }
  2583. void ENET_1588_Timer_IRQHandler(void)
  2584. {
  2585. s_enetTsIsr(ENET, s_ENETHandle[0]);
  2586. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2587. exception return operation might vector to incorrect interrupt */
  2588. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2589. __DSB();
  2590. #endif
  2591. }
  2592. void ENET_DriverIRQHandler(void)
  2593. {
  2594. ENET_CommonFrame0IRQHandler(ENET);
  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. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2601. exception return operation might vector to incorrect interrupt */
  2602. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2603. __DSB();
  2604. #endif
  2605. }
  2606. #endif
  2607. #if defined(ENET1)
  2608. void ENET1_DriverIRQHandler(void)
  2609. {
  2610. ENET_CommonFrame0IRQHandler(ENET1);
  2611. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2612. exception return operation might vector to incorrect interrupt */
  2613. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2614. __DSB();
  2615. #endif
  2616. }
  2617. #endif
  2618. #if defined(ENET2)
  2619. void ENET2_DriverIRQHandler(void)
  2620. {
  2621. ENET_CommonFrame0IRQHandler(ENET2);
  2622. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2623. exception return operation might vector to incorrect interrupt */
  2624. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2625. __DSB();
  2626. #endif
  2627. }
  2628. #endif
  2629. #if defined(CONNECTIVITY__ENET0)
  2630. void CONNECTIVITY_ENET0_FRAME0_EVENT_INT_DriverIRQHandler(void)
  2631. {
  2632. ENET_CommonFrame0IRQHandler(CONNECTIVITY__ENET0);
  2633. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2634. exception return operation might vector to incorrect interrupt */
  2635. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2636. __DSB();
  2637. #endif
  2638. }
  2639. #if FSL_FEATURE_ENET_QUEUE > 1
  2640. void CONNECTIVITY_ENET0_FRAME1_INT_DriverIRQHandler(void)
  2641. {
  2642. ENET_CommonFrame1IRQHandler(CONNECTIVITY__ENET0);
  2643. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2644. exception return operation might vector to incorrect interrupt */
  2645. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2646. __DSB();
  2647. #endif
  2648. }
  2649. void CONNECTIVITY_ENET0_FRAME2_INT_DriverIRQHandler(void)
  2650. {
  2651. ENET_CommonFrame2IRQHandler(CONNECTIVITY__ENET0);
  2652. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2653. exception return operation might vector to incorrect interrupt */
  2654. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2655. __DSB();
  2656. #endif
  2657. }
  2658. #endif
  2659. #endif
  2660. #if defined(CONNECTIVITY__ENET1)
  2661. void CONNECTIVITY_ENET1_FRAME0_EVENT_INT_DriverIRQHandler(void)
  2662. {
  2663. ENET_CommonFrame0IRQHandler(CONNECTIVITY__ENET1);
  2664. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2665. exception return operation might vector to incorrect interrupt */
  2666. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2667. __DSB();
  2668. #endif
  2669. }
  2670. #if FSL_FEATURE_ENET_QUEUE > 1
  2671. void CONNECTIVITY_ENET1_FRAME1_INT_DriverIRQHandler(void)
  2672. {
  2673. ENET_CommonFrame1IRQHandler(CONNECTIVITY__ENET1);
  2674. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2675. exception return operation might vector to incorrect interrupt */
  2676. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2677. __DSB();
  2678. #endif
  2679. }
  2680. void CONNECTIVITY_ENET1_FRAME2_INT_DriverIRQHandler(void)
  2681. {
  2682. ENET_CommonFrame2IRQHandler(CONNECTIVITY__ENET1);
  2683. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  2684. exception return operation might vector to incorrect interrupt */
  2685. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  2686. __DSB();
  2687. #endif
  2688. }
  2689. #endif
  2690. #endif