fsl_flexcan.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_flexcan.h"
  35. /*******************************************************************************
  36. * Definitons
  37. ******************************************************************************/
  38. #define FLEXCAN_TIME_QUANTA_NUM (10)
  39. /*! @brief FlexCAN Internal State. */
  40. enum _flexcan_state
  41. {
  42. kFLEXCAN_StateIdle = 0x0, /*!< MB/RxFIFO idle.*/
  43. kFLEXCAN_StateRxData = 0x1, /*!< MB receiving.*/
  44. kFLEXCAN_StateRxRemote = 0x2, /*!< MB receiving remote reply.*/
  45. kFLEXCAN_StateTxData = 0x3, /*!< MB transmitting.*/
  46. kFLEXCAN_StateTxRemote = 0x4, /*!< MB transmitting remote request.*/
  47. kFLEXCAN_StateRxFifo = 0x5, /*!< RxFIFO receiving.*/
  48. };
  49. /*! @brief FlexCAN message buffer CODE for Rx buffers. */
  50. enum _flexcan_mb_code_rx
  51. {
  52. kFLEXCAN_RxMbInactive = 0x0, /*!< MB is not active.*/
  53. kFLEXCAN_RxMbFull = 0x2, /*!< MB is full.*/
  54. kFLEXCAN_RxMbEmpty = 0x4, /*!< MB is active and empty.*/
  55. kFLEXCAN_RxMbOverrun = 0x6, /*!< MB is overwritten into a full buffer.*/
  56. kFLEXCAN_RxMbBusy = 0x8, /*!< FlexCAN is updating the contents of the MB.*/
  57. /*! The CPU must not access the MB.*/
  58. kFLEXCAN_RxMbRanswer = 0xA, /*!< A frame was configured to recognize a Remote Request Frame */
  59. /*! and transmit a Response Frame in return.*/
  60. kFLEXCAN_RxMbNotUsed = 0xF, /*!< Not used.*/
  61. };
  62. /*! @brief FlexCAN message buffer CODE FOR Tx buffers. */
  63. enum _flexcan_mb_code_tx
  64. {
  65. kFLEXCAN_TxMbInactive = 0x8, /*!< MB is not active.*/
  66. kFLEXCAN_TxMbAbort = 0x9, /*!< MB is aborted.*/
  67. kFLEXCAN_TxMbDataOrRemote = 0xC, /*!< MB is a TX Data Frame(when MB RTR = 0) or */
  68. /*!< MB is a TX Remote Request Frame (when MB RTR = 1).*/
  69. kFLEXCAN_TxMbTanswer = 0xE, /*!< MB is a TX Response Request Frame from */
  70. /*! an incoming Remote Request Frame.*/
  71. kFLEXCAN_TxMbNotUsed = 0xF, /*!< Not used.*/
  72. };
  73. /* Typedef for interrupt handler. */
  74. typedef void (*flexcan_isr_t)(CAN_Type *base, flexcan_handle_t *handle);
  75. /*******************************************************************************
  76. * Prototypes
  77. ******************************************************************************/
  78. /*!
  79. * @brief Get the FlexCAN instance from peripheral base address.
  80. *
  81. * @param base FlexCAN peripheral base address.
  82. * @return FlexCAN instance.
  83. */
  84. uint32_t FLEXCAN_GetInstance(CAN_Type *base);
  85. /*!
  86. * @brief Enter FlexCAN Freeze Mode.
  87. *
  88. * This function makes the FlexCAN work under Freeze Mode.
  89. *
  90. * @param base FlexCAN peripheral base address.
  91. */
  92. static void FLEXCAN_EnterFreezeMode(CAN_Type *base);
  93. /*!
  94. * @brief Exit FlexCAN Freeze Mode.
  95. *
  96. * This function makes the FlexCAN leave Freeze Mode.
  97. *
  98. * @param base FlexCAN peripheral base address.
  99. */
  100. static void FLEXCAN_ExitFreezeMode(CAN_Type *base);
  101. #if !defined(NDEBUG)
  102. /*!
  103. * @brief Check if Message Buffer is occupied by Rx FIFO.
  104. *
  105. * This function check if Message Buffer is occupied by Rx FIFO.
  106. *
  107. * @param base FlexCAN peripheral base address.
  108. * @param mbIdx The FlexCAN Message Buffer index.
  109. */
  110. static bool FLEXCAN_IsMbOccupied(CAN_Type *base, uint8_t mbIdx);
  111. #endif
  112. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641)
  113. /*!
  114. * @brief Get the first valid Message buffer ID of give FlexCAN instance.
  115. *
  116. * This function is a helper function for Errata 5641 workaround.
  117. *
  118. * @param base FlexCAN peripheral base address.
  119. * @return The first valid Message Buffer Number.
  120. */
  121. static uint32_t FLEXCAN_GetFirstValidMb(CAN_Type *base);
  122. #endif
  123. /*!
  124. * @brief Check if Message Buffer interrupt is enabled.
  125. *
  126. * This function check if Message Buffer interrupt is enabled.
  127. *
  128. * @param base FlexCAN peripheral base address.
  129. * @param mbIdx The FlexCAN Message Buffer index.
  130. */
  131. static bool FLEXCAN_IsMbIntEnabled(CAN_Type *base, uint8_t mbIdx);
  132. /*!
  133. * @brief Reset the FlexCAN Instance.
  134. *
  135. * Restores the FlexCAN module to reset state, notice that this function
  136. * will set all the registers to reset state so the FlexCAN module can not work
  137. * after calling this API.
  138. *
  139. * @param base FlexCAN peripheral base address.
  140. */
  141. static void FLEXCAN_Reset(CAN_Type *base);
  142. /*!
  143. * @brief Set Baud Rate of FlexCAN.
  144. *
  145. * This function set the baud rate of FlexCAN.
  146. *
  147. * @param base FlexCAN peripheral base address.
  148. * @param sourceClock_Hz Source Clock in Hz.
  149. * @param baudRate_Bps Baud Rate in Bps.
  150. * @param timingConfig FlexCAN timingConfig.
  151. */
  152. static void FLEXCAN_SetBaudRate(CAN_Type *base, uint32_t sourceClock_Hz, uint32_t baudRate_Bps, flexcan_timing_config_t timingConfig);
  153. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  154. /*!
  155. * @brief Set Baud Rate of FlexCAN FD frame.
  156. *
  157. * This function set the baud rate of FlexCAN FD frame.
  158. *
  159. * @param base FlexCAN peripheral base address.
  160. * @param sourceClock_Hz Source Clock in Hz.
  161. * @param baudRateFD_Bps FD frame Baud Rate in Bps.
  162. * @param timingConfig FlexCAN timingConfig.
  163. */
  164. static void FLEXCAN_SetFDBaudRate(CAN_Type *base, uint32_t sourceClock_Hz, uint32_t baudRateFD_Bps, flexcan_timing_config_t timingConfig);
  165. #endif
  166. /*******************************************************************************
  167. * Variables
  168. ******************************************************************************/
  169. /* Array of FlexCAN peripheral base address. */
  170. static CAN_Type *const s_flexcanBases[] = CAN_BASE_PTRS;
  171. /* Array of FlexCAN IRQ number. */
  172. static const IRQn_Type s_flexcanRxWarningIRQ[] = CAN_Rx_Warning_IRQS;
  173. static const IRQn_Type s_flexcanTxWarningIRQ[] = CAN_Tx_Warning_IRQS;
  174. static const IRQn_Type s_flexcanWakeUpIRQ[] = CAN_Wake_Up_IRQS;
  175. static const IRQn_Type s_flexcanErrorIRQ[] = CAN_Error_IRQS;
  176. static const IRQn_Type s_flexcanBusOffIRQ[] = CAN_Bus_Off_IRQS;
  177. static const IRQn_Type s_flexcanMbIRQ[] = CAN_ORed_Message_buffer_IRQS;
  178. /* Array of FlexCAN handle. */
  179. static flexcan_handle_t *s_flexcanHandle[ARRAY_SIZE(s_flexcanBases)];
  180. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  181. /* Array of FlexCAN clock name. */
  182. static const clock_ip_name_t s_flexcanClock[] = FLEXCAN_CLOCKS;
  183. #if defined(FLEXCAN_PERIPH_CLOCKS)
  184. /* Array of FlexCAN serial clock name. */
  185. static const clock_ip_name_t s_flexcanPeriphClock[] = FLEXCAN_PERIPH_CLOCKS;
  186. #endif /* FLEXCAN_PERIPH_CLOCKS */
  187. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  188. /* FlexCAN ISR for transactional APIs. */
  189. static flexcan_isr_t s_flexcanIsr;
  190. /*******************************************************************************
  191. * Code
  192. ******************************************************************************/
  193. uint32_t FLEXCAN_GetInstance(CAN_Type *base)
  194. {
  195. uint32_t instance;
  196. /* Find the instance index from base address mappings. */
  197. for (instance = 0; instance < ARRAY_SIZE(s_flexcanBases); instance++)
  198. {
  199. if (s_flexcanBases[instance] == base)
  200. {
  201. break;
  202. }
  203. }
  204. assert(instance < ARRAY_SIZE(s_flexcanBases));
  205. return instance;
  206. }
  207. static void FLEXCAN_EnterFreezeMode(CAN_Type *base)
  208. {
  209. /* Set Freeze, Halt bits. */
  210. base->MCR |= CAN_MCR_HALT_MASK;
  211. /* Wait until the FlexCAN Module enter freeze mode. */
  212. while (!(base->MCR & CAN_MCR_FRZACK_MASK))
  213. {
  214. }
  215. }
  216. static void FLEXCAN_ExitFreezeMode(CAN_Type *base)
  217. {
  218. /* Clear Freeze, Halt bits. */
  219. base->MCR &= ~CAN_MCR_HALT_MASK;
  220. /* Wait until the FlexCAN Module exit freeze mode. */
  221. while (base->MCR & CAN_MCR_FRZACK_MASK)
  222. {
  223. }
  224. }
  225. #if !defined(NDEBUG)
  226. static bool FLEXCAN_IsMbOccupied(CAN_Type *base, uint8_t mbIdx)
  227. {
  228. uint8_t lastOccupiedMb;
  229. /* Is Rx FIFO enabled? */
  230. if (base->MCR & CAN_MCR_RFEN_MASK)
  231. {
  232. /* Get RFFN value. */
  233. lastOccupiedMb = ((base->CTRL2 & CAN_CTRL2_RFFN_MASK) >> CAN_CTRL2_RFFN_SHIFT);
  234. /* Calculate the number of last Message Buffer occupied by Rx FIFO. */
  235. lastOccupiedMb = ((lastOccupiedMb + 1) * 2) + 5;
  236. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641)
  237. if (mbIdx <= (lastOccupiedMb + 1))
  238. #else
  239. if (mbIdx <= lastOccupiedMb)
  240. #endif
  241. {
  242. return true;
  243. }
  244. else
  245. {
  246. return false;
  247. }
  248. }
  249. else
  250. {
  251. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641)
  252. if (0 == mbIdx)
  253. {
  254. return true;
  255. }
  256. else
  257. {
  258. return false;
  259. }
  260. #else
  261. return false;
  262. #endif
  263. }
  264. }
  265. #endif
  266. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641)
  267. static uint32_t FLEXCAN_GetFirstValidMb(CAN_Type *base)
  268. {
  269. uint32_t firstValidMbNum;
  270. if (base->MCR & CAN_MCR_RFEN_MASK)
  271. {
  272. firstValidMbNum = ((base->CTRL2 & CAN_CTRL2_RFFN_MASK) >> CAN_CTRL2_RFFN_SHIFT);
  273. firstValidMbNum = ((firstValidMbNum + 1) * 2) + 6;
  274. }
  275. else
  276. {
  277. firstValidMbNum = 0;
  278. }
  279. return firstValidMbNum;
  280. }
  281. #endif
  282. static bool FLEXCAN_IsMbIntEnabled(CAN_Type *base, uint8_t mbIdx)
  283. {
  284. /* Assertion. */
  285. assert(mbIdx < FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base));
  286. #if (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  287. if (mbIdx < 32)
  288. {
  289. #endif
  290. if (base->IMASK1 & ((uint32_t)(1 << mbIdx)))
  291. {
  292. return true;
  293. }
  294. else
  295. {
  296. return false;
  297. }
  298. #if (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  299. }
  300. else
  301. {
  302. if (base->IMASK2 & ((uint32_t)(1 << (mbIdx - 32))))
  303. {
  304. return true;
  305. }
  306. else
  307. {
  308. return false;
  309. }
  310. }
  311. #endif
  312. }
  313. static void FLEXCAN_Reset(CAN_Type *base)
  314. {
  315. /* The module must should be first exit from low power
  316. * mode, and then soft reset can be applied.
  317. */
  318. assert(!(base->MCR & CAN_MCR_MDIS_MASK));
  319. uint8_t i;
  320. #if (FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT != 0)
  321. /* De-assert DOZE Enable Bit. */
  322. base->MCR &= ~CAN_MCR_DOZE_MASK;
  323. #endif
  324. /* Wait until FlexCAN exit from any Low Power Mode. */
  325. while (base->MCR & CAN_MCR_LPMACK_MASK)
  326. {
  327. }
  328. /* Assert Soft Reset Signal. */
  329. base->MCR |= CAN_MCR_SOFTRST_MASK;
  330. /* Wait until FlexCAN reset completes. */
  331. while (base->MCR & CAN_MCR_SOFTRST_MASK)
  332. {
  333. }
  334. /* Reset MCR rigister. */
  335. #if (defined(FSL_FEATURE_FLEXCAN_HAS_GLITCH_FILTER) && FSL_FEATURE_FLEXCAN_HAS_GLITCH_FILTER)
  336. base->MCR |= CAN_MCR_WRNEN_MASK | CAN_MCR_WAKSRC_MASK |
  337. CAN_MCR_MAXMB(FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base) - 1);
  338. #else
  339. base->MCR |= CAN_MCR_WRNEN_MASK | CAN_MCR_MAXMB(FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base) - 1);
  340. #endif
  341. /* Reset CTRL1 and CTRL2 rigister. */
  342. base->CTRL1 = CAN_CTRL1_SMP_MASK;
  343. base->CTRL2 = CAN_CTRL2_TASD(0x16) | CAN_CTRL2_RRS_MASK | CAN_CTRL2_EACEN_MASK;
  344. /* Clean all individual Rx Mask of Message Buffers. */
  345. for (i = 0; i < FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base); i++)
  346. {
  347. base->RXIMR[i] = 0x3FFFFFFF;
  348. }
  349. /* Clean Global Mask of Message Buffers. */
  350. base->RXMGMASK = 0x3FFFFFFF;
  351. /* Clean Global Mask of Message Buffer 14. */
  352. base->RX14MASK = 0x3FFFFFFF;
  353. /* Clean Global Mask of Message Buffer 15. */
  354. base->RX15MASK = 0x3FFFFFFF;
  355. /* Clean Global Mask of Rx FIFO. */
  356. base->RXFGMASK = 0x3FFFFFFF;
  357. /* Clean all Message Buffer CS fields. */
  358. for (i = 0; i < FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base); i++)
  359. {
  360. base->MB[i].CS = 0x0;
  361. }
  362. }
  363. static void FLEXCAN_SetBaudRate(CAN_Type *base, uint32_t sourceClock_Hz, uint32_t baudRate_Bps, flexcan_timing_config_t timingConfig)
  364. {
  365. /* FlexCAN timing setting formula:
  366. * quantum = 1 + (PSEG1 + 1) + (PSEG2 + 1) + (PROPSEG + 1);
  367. */
  368. uint32_t quantum = 1 + (timingConfig.phaseSeg1 + 1) + (timingConfig.phaseSeg2 + 1) + (timingConfig.propSeg + 1);
  369. uint32_t priDiv = baudRate_Bps * quantum;
  370. /* Assertion: Desired baud rate is too high. */
  371. assert(baudRate_Bps <= 1000000U);
  372. /* Assertion: Source clock should greater than baud rate * quantum. */
  373. assert(priDiv <= sourceClock_Hz);
  374. if (0 == priDiv)
  375. {
  376. priDiv = 1;
  377. }
  378. priDiv = (sourceClock_Hz / priDiv) - 1;
  379. /* Desired baud rate is too low. */
  380. if (priDiv > 0xFF)
  381. {
  382. priDiv = 0xFF;
  383. }
  384. timingConfig.preDivider = priDiv;
  385. /* Update actual timing characteristic. */
  386. FLEXCAN_SetTimingConfig(base, &timingConfig);
  387. }
  388. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  389. static void FLEXCAN_SetFDBaudRate(CAN_Type *base, uint32_t sourceClock_Hz, uint32_t baudRateFD_Bps, flexcan_timing_config_t timingConfig)
  390. {
  391. /* FlexCAN timing setting formula:
  392. * quantum = 1 + (PSEG1 + 1) + (PSEG2 + 1) + (PROPSEG + 1);
  393. */
  394. uint32_t quantum = 1 + (timingConfig.phaseSeg1 + 1) + (timingConfig.phaseSeg2 + 1) + (timingConfig.propSeg + 1);
  395. uint32_t priDiv = baudRateFD_Bps * quantum;
  396. /* Assertion: Desired baud rate is too high. */
  397. assert(baudRateFD_Bps <= 1000000U);
  398. /* Assertion: Source clock should greater than baud rate * FLEXCAN_TIME_QUANTA_NUM. */
  399. assert(priDiv <= sourceClock_Hz);
  400. if (0 == priDiv)
  401. {
  402. priDiv = 1;
  403. }
  404. priDiv = (sourceClock_Hz / priDiv) - 1;
  405. /* Desired baud rate is too low. */
  406. if (priDiv > 0xFF)
  407. {
  408. priDiv = 0xFF;
  409. }
  410. timingConfig.preDivider = priDiv;
  411. /* Update actual timing characteristic. */
  412. FLEXCAN_SetFDTimingConfig(base, &timingConfig);
  413. }
  414. #endif
  415. void FLEXCAN_Init(CAN_Type *base, const flexcan_config_t *config, uint32_t sourceClock_Hz)
  416. {
  417. uint32_t mcrTemp;
  418. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  419. uint32_t instance;
  420. #endif
  421. /* Assertion. */
  422. assert(config);
  423. assert((config->maxMbNum > 0) && (config->maxMbNum <= FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base)));
  424. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  425. instance = FLEXCAN_GetInstance(base);
  426. /* Enable FlexCAN clock. */
  427. CLOCK_EnableClock(s_flexcanClock[instance]);
  428. #if defined(FLEXCAN_PERIPH_CLOCKS)
  429. /* Enable FlexCAN serial clock. */
  430. CLOCK_EnableClock(s_flexcanPeriphClock[instance]);
  431. #endif /* FLEXCAN_PERIPH_CLOCKS */
  432. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  433. #if (!defined(FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE)) || !FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE
  434. /* Disable FlexCAN Module. */
  435. FLEXCAN_Enable(base, false);
  436. /* Protocol-Engine clock source selection, This bit must be set
  437. * when FlexCAN Module in Disable Mode.
  438. */
  439. base->CTRL1 = (kFLEXCAN_ClkSrcOsc == config->clkSrc) ? base->CTRL1 & ~CAN_CTRL1_CLKSRC_MASK :
  440. base->CTRL1 | CAN_CTRL1_CLKSRC_MASK;
  441. #endif /* FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE */
  442. /* Enable FlexCAN Module for configuartion. */
  443. FLEXCAN_Enable(base, true);
  444. /* Reset to known status. */
  445. FLEXCAN_Reset(base);
  446. /* Save current MCR value and enable to enter Freeze mode(enabled by default). */
  447. mcrTemp = base->MCR;
  448. /* Set the maximum number of Message Buffers */
  449. mcrTemp = (mcrTemp & ~CAN_MCR_MAXMB_MASK) | CAN_MCR_MAXMB(config->maxMbNum - 1);
  450. /* Enable Loop Back Mode? */
  451. base->CTRL1 = (config->enableLoopBack) ? base->CTRL1 | CAN_CTRL1_LPB_MASK : base->CTRL1 & ~CAN_CTRL1_LPB_MASK;
  452. /* Enable Self Wake Up Mode? */
  453. mcrTemp = (config->enableSelfWakeup) ? mcrTemp | CAN_MCR_SLFWAK_MASK : mcrTemp & ~CAN_MCR_SLFWAK_MASK;
  454. /* Enable Individual Rx Masking? */
  455. mcrTemp = (config->enableIndividMask) ? mcrTemp | CAN_MCR_IRMQ_MASK : mcrTemp & ~CAN_MCR_IRMQ_MASK;
  456. #if (defined(FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT) && FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT)
  457. /* Enable Doze Mode? */
  458. mcrTemp = (config->enableDoze) ? mcrTemp | CAN_MCR_DOZE_MASK : mcrTemp & ~CAN_MCR_DOZE_MASK;
  459. #endif
  460. /* Save MCR Configuation. */
  461. base->MCR = mcrTemp;
  462. /* Baud Rate Configuration.*/
  463. FLEXCAN_SetBaudRate(base, sourceClock_Hz, config->baudRate, config->timingConfig);
  464. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  465. FLEXCAN_SetFDBaudRate(base, sourceClock_Hz, config->baudRateFD, config->timingConfig);
  466. #endif
  467. }
  468. void FLEXCAN_Deinit(CAN_Type *base)
  469. {
  470. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  471. uint32_t instance;
  472. #endif
  473. /* Reset all Register Contents. */
  474. FLEXCAN_Reset(base);
  475. /* Disable FlexCAN module. */
  476. FLEXCAN_Enable(base, false);
  477. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  478. instance = FLEXCAN_GetInstance(base);
  479. #if defined(FLEXCAN_PERIPH_CLOCKS)
  480. /* Disable FlexCAN serial clock. */
  481. CLOCK_DisableClock(s_flexcanPeriphClock[instance]);
  482. #endif /* FLEXCAN_PERIPH_CLOCKS */
  483. /* Disable FlexCAN clock. */
  484. CLOCK_DisableClock(s_flexcanClock[instance]);
  485. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  486. }
  487. void FLEXCAN_GetDefaultConfig(flexcan_config_t *config)
  488. {
  489. /* Assertion. */
  490. assert(config);
  491. /* Initialize FlexCAN Module config struct with default value. */
  492. #if (!defined(FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE)) || !FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE
  493. config->clkSrc = kFLEXCAN_ClkSrcOsc;
  494. #endif /* FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE */
  495. config->baudRate = 1000000U;
  496. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  497. config->baudRateFD = 1000000U;
  498. #endif
  499. config->maxMbNum = 16;
  500. config->enableLoopBack = false;
  501. config->enableSelfWakeup = false;
  502. config->enableIndividMask = false;
  503. #if (defined(FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT) && FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT)
  504. config->enableDoze = false;
  505. #endif
  506. /* Default protocol timing configuration, time quantum is 10. */
  507. config->timingConfig.phaseSeg1 = 3;
  508. config->timingConfig.phaseSeg2 = 2;
  509. config->timingConfig.propSeg = 1;
  510. config->timingConfig.rJumpwidth = 1;
  511. }
  512. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  513. void FLEXCAN_FDEnable(CAN_Type *base, flexcan_mb_size_t dataSize, bool brs)
  514. {
  515. if (brs)
  516. {
  517. base->FDCTRL &= CAN_FDCTRL_FDRATE_MASK;
  518. }
  519. else
  520. {
  521. base->FDCTRL &= ~CAN_FDCTRL_FDRATE_MASK;
  522. }
  523. /* Enter Freeze Mode. */
  524. FLEXCAN_EnterFreezeMode(base);
  525. base->MCR |= CAN_MCR_FDEN_MASK;
  526. base->FDCTRL |= CAN_FDCTRL_MBDSR0(dataSize);
  527. /* Exit Freeze Mode. */
  528. FLEXCAN_ExitFreezeMode(base);
  529. }
  530. #endif
  531. void FLEXCAN_SetTimingConfig(CAN_Type *base, const flexcan_timing_config_t *config)
  532. {
  533. /* Assertion. */
  534. assert(config);
  535. /* Enter Freeze Mode. */
  536. FLEXCAN_EnterFreezeMode(base);
  537. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  538. /* Cleaning previous Timing Setting. */
  539. base->CBT &= ~(CAN_CBT_EPRESDIV_MASK | CAN_CBT_ERJW_MASK | CAN_CBT_EPSEG1_MASK | CAN_CBT_EPSEG2_MASK |
  540. CAN_CBT_EPROPSEG_MASK);
  541. /* Updating Timing Setting according to configuration structure. */
  542. base->CBT |=
  543. (CAN_CBT_EPRESDIV(config->preDivider) | CAN_CBT_ERJW(config->rJumpwidth) | CAN_CBT_EPSEG1(config->phaseSeg1) |
  544. CAN_CBT_EPSEG2(config->phaseSeg2) | CAN_CBT_EPROPSEG(config->propSeg));
  545. #else
  546. /* Cleaning previous Timing Setting. */
  547. base->CTRL1 &= ~(CAN_CTRL1_PRESDIV_MASK | CAN_CTRL1_RJW_MASK | CAN_CTRL1_PSEG1_MASK | CAN_CTRL1_PSEG2_MASK |
  548. CAN_CTRL1_PROPSEG_MASK);
  549. /* Updating Timing Setting according to configuration structure. */
  550. base->CTRL1 |=
  551. (CAN_CTRL1_PRESDIV(config->preDivider) | CAN_CTRL1_RJW(config->rJumpwidth) |
  552. CAN_CTRL1_PSEG1(config->phaseSeg1) | CAN_CTRL1_PSEG2(config->phaseSeg2) | CAN_CTRL1_PROPSEG(config->propSeg));
  553. #endif
  554. /* Exit Freeze Mode. */
  555. FLEXCAN_ExitFreezeMode(base);
  556. }
  557. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  558. void FLEXCAN_SetFDTimingConfig(CAN_Type *base, const flexcan_timing_config_t *config)
  559. {
  560. /* Assertion. */
  561. assert(config);
  562. /* Enter Freeze Mode. */
  563. FLEXCAN_EnterFreezeMode(base);
  564. /* Cleaning previous Timing Setting. */
  565. base->FDCBT &= ~(CAN_FDCBT_FPRESDIV_MASK | CAN_FDCBT_FRJW_MASK | CAN_FDCBT_FPSEG1_MASK | CAN_FDCBT_FPSEG2_MASK |
  566. CAN_FDCBT_FPROPSEG_MASK);
  567. /* Updating Timing Setting according to configuration structure. */
  568. base->FDCBT |= (CAN_FDCBT_FPRESDIV(config->preDivider) | CAN_FDCBT_FRJW(config->rJumpwidth) |
  569. CAN_FDCBT_FPSEG1(config->phaseSeg1) | CAN_FDCBT_FPSEG2(config->phaseSeg2) |
  570. CAN_FDCBT_FPROPSEG(config->propSeg));
  571. /* Exit Freeze Mode. */
  572. FLEXCAN_ExitFreezeMode(base);
  573. }
  574. #endif
  575. void FLEXCAN_SetRxMbGlobalMask(CAN_Type *base, uint32_t mask)
  576. {
  577. /* Enter Freeze Mode. */
  578. FLEXCAN_EnterFreezeMode(base);
  579. /* Setting Rx Message Buffer Global Mask value. */
  580. base->RXMGMASK = mask;
  581. base->RX14MASK = mask;
  582. base->RX15MASK = mask;
  583. /* Exit Freeze Mode. */
  584. FLEXCAN_ExitFreezeMode(base);
  585. }
  586. void FLEXCAN_SetRxFifoGlobalMask(CAN_Type *base, uint32_t mask)
  587. {
  588. /* Enter Freeze Mode. */
  589. FLEXCAN_EnterFreezeMode(base);
  590. /* Setting Rx FIFO Global Mask value. */
  591. base->RXFGMASK = mask;
  592. /* Exit Freeze Mode. */
  593. FLEXCAN_ExitFreezeMode(base);
  594. }
  595. void FLEXCAN_SetRxIndividualMask(CAN_Type *base, uint8_t maskIdx, uint32_t mask)
  596. {
  597. assert(maskIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  598. /* Enter Freeze Mode. */
  599. FLEXCAN_EnterFreezeMode(base);
  600. /* Setting Rx Individual Mask value. */
  601. base->RXIMR[maskIdx] = mask;
  602. /* Exit Freeze Mode. */
  603. FLEXCAN_ExitFreezeMode(base);
  604. }
  605. void FLEXCAN_SetTxMbConfig(CAN_Type *base, uint8_t mbIdx, bool enable)
  606. {
  607. /* Assertion. */
  608. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  609. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  610. /* Inactivate Message Buffer. */
  611. if (enable)
  612. {
  613. base->MB[mbIdx].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  614. }
  615. else
  616. {
  617. base->MB[mbIdx].CS = 0;
  618. }
  619. /* Clean Message Buffer content. */
  620. base->MB[mbIdx].ID = 0x0;
  621. base->MB[mbIdx].WORD0 = 0x0;
  622. base->MB[mbIdx].WORD1 = 0x0;
  623. }
  624. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  625. void FLEXCAN_SetFDTxMbConfig(CAN_Type *base, uint8_t mbIdx, bool enable)
  626. {
  627. /* Assertion. */
  628. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  629. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  630. uint8_t cnt = 0;
  631. uint32_t dataSize;
  632. dataSize = (base->FDCTRL & CAN_FDCTRL_MBDSR0_MASK) >> CAN_FDCTRL_MBDSR0_SHIFT;
  633. /* Inactivate Message Buffer. */
  634. if (enable)
  635. {
  636. switch (dataSize)
  637. {
  638. case kFLEXCAN_8BperMB:
  639. base->MB_8B[mbIdx].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  640. break;
  641. case kFLEXCAN_16BperMB:
  642. base->MB_16B[mbIdx].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  643. break;
  644. case kFLEXCAN_32BperMB:
  645. base->MB_32B[mbIdx].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  646. break;
  647. case kFLEXCAN_64BperMB:
  648. base->MB_64B[mbIdx].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  649. break;
  650. default:
  651. break;
  652. }
  653. }
  654. else
  655. {
  656. switch (dataSize)
  657. {
  658. case kFLEXCAN_8BperMB:
  659. base->MB_8B[mbIdx].CS = 0;
  660. break;
  661. case kFLEXCAN_16BperMB:
  662. base->MB_16B[mbIdx].CS = 0;
  663. break;
  664. case kFLEXCAN_32BperMB:
  665. base->MB_32B[mbIdx].CS = 0;
  666. break;
  667. case kFLEXCAN_64BperMB:
  668. base->MB_64B[mbIdx].CS = 0;
  669. break;
  670. default:
  671. break;
  672. }
  673. }
  674. /* Clean ID and Message Buffer content. */
  675. switch (dataSize)
  676. {
  677. case kFLEXCAN_8BperMB:
  678. base->MB_8B[mbIdx].ID = 0x0;
  679. for (cnt = 0; cnt < 2; cnt++)
  680. {
  681. base->MB_8B[mbIdx].WORD[cnt] = 0x0;
  682. }
  683. break;
  684. case kFLEXCAN_16BperMB:
  685. base->MB_16B[mbIdx].ID = 0x0;
  686. for (cnt = 0; cnt < 4; cnt++)
  687. {
  688. base->MB_16B[mbIdx].WORD[cnt] = 0x0;
  689. }
  690. break;
  691. case kFLEXCAN_32BperMB:
  692. base->MB_32B[mbIdx].ID = 0x0;
  693. for (cnt = 0; cnt < 8; cnt++)
  694. {
  695. base->MB_32B[mbIdx].WORD[cnt] = 0x0;
  696. }
  697. break;
  698. case kFLEXCAN_64BperMB:
  699. base->MB_64B[mbIdx].ID = 0x0;
  700. for (cnt = 0; cnt < 16; cnt++)
  701. {
  702. base->MB_64B[mbIdx].WORD[cnt] = 0x0;
  703. }
  704. break;
  705. default:
  706. break;
  707. }
  708. }
  709. #endif
  710. void FLEXCAN_SetRxMbConfig(CAN_Type *base, uint8_t mbIdx, const flexcan_rx_mb_config_t *config, bool enable)
  711. {
  712. /* Assertion. */
  713. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  714. assert(((config) || (false == enable)));
  715. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  716. uint32_t cs_temp = 0;
  717. /* Inactivate Message Buffer. */
  718. base->MB[mbIdx].CS = 0;
  719. /* Clean Message Buffer content. */
  720. base->MB[mbIdx].ID = 0x0;
  721. base->MB[mbIdx].WORD0 = 0x0;
  722. base->MB[mbIdx].WORD1 = 0x0;
  723. if (enable)
  724. {
  725. /* Setup Message Buffer ID. */
  726. base->MB[mbIdx].ID = config->id;
  727. /* Setup Message Buffer format. */
  728. if (kFLEXCAN_FrameFormatExtend == config->format)
  729. {
  730. cs_temp |= CAN_CS_IDE_MASK;
  731. }
  732. /* Setup Message Buffer type. */
  733. if (kFLEXCAN_FrameTypeRemote == config->type)
  734. {
  735. cs_temp |= CAN_CS_RTR_MASK;
  736. }
  737. /* Activate Rx Message Buffer. */
  738. cs_temp |= CAN_CS_CODE(kFLEXCAN_RxMbEmpty);
  739. base->MB[mbIdx].CS = cs_temp;
  740. }
  741. }
  742. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  743. void FLEXCAN_SetFDRxMbConfig(CAN_Type *base, uint8_t mbIdx, const flexcan_rx_mb_config_t *config, bool enable)
  744. {
  745. /* Assertion. */
  746. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  747. assert(((config) || (false == enable)));
  748. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  749. uint32_t cs_temp = 0;
  750. uint8_t cnt = 0;
  751. uint32_t dataSize;
  752. dataSize = (base->FDCTRL & CAN_FDCTRL_MBDSR0_MASK) >> CAN_FDCTRL_MBDSR0_SHIFT;
  753. /* Inactivate Message Buffer and clean ID, Message Buffer content. */
  754. switch (dataSize)
  755. {
  756. case kFLEXCAN_8BperMB:
  757. base->MB_8B[mbIdx].CS = 0;
  758. base->MB_8B[mbIdx].ID = 0x0;
  759. for (cnt = 0; cnt < 2; cnt++)
  760. {
  761. base->MB_8B[mbIdx].WORD[cnt] = 0x0;
  762. }
  763. break;
  764. case kFLEXCAN_16BperMB:
  765. base->MB_16B[mbIdx].CS = 0;
  766. base->MB_16B[mbIdx].ID = 0x0;
  767. for (cnt = 0; cnt < 4; cnt++)
  768. {
  769. base->MB_16B[mbIdx].WORD[cnt] = 0x0;
  770. }
  771. break;
  772. case kFLEXCAN_32BperMB:
  773. base->MB_32B[mbIdx].CS = 0;
  774. base->MB_32B[mbIdx].ID = 0x0;
  775. for (cnt = 0; cnt < 8; cnt++)
  776. {
  777. base->MB_32B[mbIdx].WORD[cnt] = 0x0;
  778. }
  779. break;
  780. case kFLEXCAN_64BperMB:
  781. base->MB_64B[mbIdx].CS = 0;
  782. base->MB_64B[mbIdx].ID = 0x0;
  783. for (cnt = 0; cnt < 16; cnt++)
  784. {
  785. base->MB_64B[mbIdx].WORD[cnt] = 0x0;
  786. }
  787. break;
  788. default:
  789. break;
  790. }
  791. if (enable)
  792. {
  793. /* Setup Message Buffer ID. */
  794. switch (dataSize)
  795. {
  796. case kFLEXCAN_8BperMB:
  797. base->MB_8B[mbIdx].ID = config->id;
  798. break;
  799. case kFLEXCAN_16BperMB:
  800. base->MB_16B[mbIdx].ID = config->id;
  801. break;
  802. case kFLEXCAN_32BperMB:
  803. base->MB_32B[mbIdx].ID = config->id;
  804. break;
  805. case kFLEXCAN_64BperMB:
  806. base->MB_64B[mbIdx].ID = config->id;
  807. break;
  808. default:
  809. break;
  810. }
  811. /* Setup Message Buffer format. */
  812. if (kFLEXCAN_FrameFormatExtend == config->format)
  813. {
  814. cs_temp |= CAN_CS_IDE_MASK;
  815. }
  816. /* Activate Rx Message Buffer. */
  817. cs_temp |= CAN_CS_CODE(kFLEXCAN_RxMbEmpty);
  818. switch (dataSize)
  819. {
  820. case kFLEXCAN_8BperMB:
  821. base->MB_8B[mbIdx].CS = cs_temp;
  822. break;
  823. case kFLEXCAN_16BperMB:
  824. base->MB_16B[mbIdx].CS = cs_temp;
  825. break;
  826. case kFLEXCAN_32BperMB:
  827. base->MB_32B[mbIdx].CS = cs_temp;
  828. break;
  829. case kFLEXCAN_64BperMB:
  830. base->MB_64B[mbIdx].CS = cs_temp;
  831. break;
  832. default:
  833. break;
  834. }
  835. }
  836. }
  837. #endif
  838. void FLEXCAN_SetRxFifoConfig(CAN_Type *base, const flexcan_rx_fifo_config_t *config, bool enable)
  839. {
  840. /* Assertion. */
  841. assert((config) || (false == enable));
  842. volatile uint32_t *idFilterRegion = (volatile uint32_t *)(&base->MB[6].CS);
  843. uint8_t setup_mb, i, rffn = 0;
  844. /* Enter Freeze Mode. */
  845. FLEXCAN_EnterFreezeMode(base);
  846. if (enable)
  847. {
  848. assert(config->idFilterNum <= 128);
  849. /* Get the setup_mb value. */
  850. setup_mb = (base->MCR & CAN_MCR_MAXMB_MASK) >> CAN_MCR_MAXMB_SHIFT;
  851. setup_mb = (setup_mb < FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base)) ?
  852. setup_mb :
  853. FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base);
  854. /* Determine RFFN value. */
  855. for (i = 0; i <= 0xF; i++)
  856. {
  857. if ((8 * (i + 1)) >= config->idFilterNum)
  858. {
  859. rffn = i;
  860. assert(((setup_mb - 8) - (2 * rffn)) > 0);
  861. base->CTRL2 = (base->CTRL2 & ~CAN_CTRL2_RFFN_MASK) | CAN_CTRL2_RFFN(rffn);
  862. break;
  863. }
  864. }
  865. }
  866. else
  867. {
  868. rffn = (base->CTRL2 & CAN_CTRL2_RFFN_MASK) >> CAN_CTRL2_RFFN_SHIFT;
  869. }
  870. /* Clean ID filter table occuyied Message Buffer Region. */
  871. rffn = (rffn + 1) * 8;
  872. for (i = 0; i < rffn; i++)
  873. {
  874. idFilterRegion[i] = 0x0;
  875. }
  876. if (enable)
  877. {
  878. /* Disable unused Rx FIFO Filter. */
  879. for (i = config->idFilterNum; i < rffn; i++)
  880. {
  881. idFilterRegion[i] = 0xFFFFFFFFU;
  882. }
  883. /* Copy ID filter table to Message Buffer Region. */
  884. for (i = 0; i < config->idFilterNum; i++)
  885. {
  886. idFilterRegion[i] = config->idFilterTable[i];
  887. }
  888. /* Setup ID Fitlter Type. */
  889. switch (config->idFilterType)
  890. {
  891. case kFLEXCAN_RxFifoFilterTypeA:
  892. base->MCR = (base->MCR & ~CAN_MCR_IDAM_MASK) | CAN_MCR_IDAM(0x0);
  893. break;
  894. case kFLEXCAN_RxFifoFilterTypeB:
  895. base->MCR = (base->MCR & ~CAN_MCR_IDAM_MASK) | CAN_MCR_IDAM(0x1);
  896. break;
  897. case kFLEXCAN_RxFifoFilterTypeC:
  898. base->MCR = (base->MCR & ~CAN_MCR_IDAM_MASK) | CAN_MCR_IDAM(0x2);
  899. break;
  900. case kFLEXCAN_RxFifoFilterTypeD:
  901. /* All frames rejected. */
  902. base->MCR = (base->MCR & ~CAN_MCR_IDAM_MASK) | CAN_MCR_IDAM(0x3);
  903. break;
  904. default:
  905. break;
  906. }
  907. /* Setting Message Reception Priority. */
  908. base->CTRL2 = (config->priority == kFLEXCAN_RxFifoPrioHigh) ? base->CTRL2 & ~CAN_CTRL2_MRP_MASK :
  909. base->CTRL2 | CAN_CTRL2_MRP_MASK;
  910. /* Enable Rx Message FIFO. */
  911. base->MCR |= CAN_MCR_RFEN_MASK;
  912. }
  913. else
  914. {
  915. /* Disable Rx Message FIFO. */
  916. base->MCR &= ~CAN_MCR_RFEN_MASK;
  917. /* Clean MB0 ~ MB5. */
  918. FLEXCAN_SetRxMbConfig(base, 0, NULL, false);
  919. FLEXCAN_SetRxMbConfig(base, 1, NULL, false);
  920. FLEXCAN_SetRxMbConfig(base, 2, NULL, false);
  921. FLEXCAN_SetRxMbConfig(base, 3, NULL, false);
  922. FLEXCAN_SetRxMbConfig(base, 4, NULL, false);
  923. FLEXCAN_SetRxMbConfig(base, 5, NULL, false);
  924. }
  925. /* Exit Freeze Mode. */
  926. FLEXCAN_ExitFreezeMode(base);
  927. }
  928. #if (defined(FSL_FEATURE_FLEXCAN_HAS_RX_FIFO_DMA) && FSL_FEATURE_FLEXCAN_HAS_RX_FIFO_DMA)
  929. void FLEXCAN_EnableRxFifoDMA(CAN_Type *base, bool enable)
  930. {
  931. if (enable)
  932. {
  933. /* Enter Freeze Mode. */
  934. FLEXCAN_EnterFreezeMode(base);
  935. /* Enable FlexCAN DMA. */
  936. base->MCR |= CAN_MCR_DMA_MASK;
  937. /* Exit Freeze Mode. */
  938. FLEXCAN_ExitFreezeMode(base);
  939. }
  940. else
  941. {
  942. /* Enter Freeze Mode. */
  943. FLEXCAN_EnterFreezeMode(base);
  944. /* Disable FlexCAN DMA. */
  945. base->MCR &= ~CAN_MCR_DMA_MASK;
  946. /* Exit Freeze Mode. */
  947. FLEXCAN_ExitFreezeMode(base);
  948. }
  949. }
  950. #endif /* FSL_FEATURE_FLEXCAN_HAS_RX_FIFO_DMA */
  951. status_t FLEXCAN_WriteTxMb(CAN_Type *base, uint8_t mbIdx, const flexcan_frame_t *txFrame)
  952. {
  953. /* Assertion. */
  954. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  955. assert(txFrame);
  956. assert(txFrame->length <= 8);
  957. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  958. uint32_t cs_temp = 0;
  959. /* Check if Message Buffer is available. */
  960. if (CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) != (base->MB[mbIdx].CS & CAN_CS_CODE_MASK))
  961. {
  962. /* Inactive Tx Message Buffer. */
  963. base->MB[mbIdx].CS = (base->MB[mbIdx].CS & ~CAN_CS_CODE_MASK) | CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  964. /* Fill Message ID field. */
  965. base->MB[mbIdx].ID = txFrame->id;
  966. /* Fill Message Format field. */
  967. if (kFLEXCAN_FrameFormatExtend == txFrame->format)
  968. {
  969. cs_temp |= CAN_CS_SRR_MASK | CAN_CS_IDE_MASK;
  970. }
  971. /* Fill Message Type field. */
  972. if (kFLEXCAN_FrameTypeRemote == txFrame->type)
  973. {
  974. cs_temp |= CAN_CS_RTR_MASK;
  975. }
  976. cs_temp |= CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) | CAN_CS_DLC(txFrame->length);
  977. /* Load Message Payload. */
  978. base->MB[mbIdx].WORD0 = txFrame->dataWord0;
  979. base->MB[mbIdx].WORD1 = txFrame->dataWord1;
  980. /* Activate Tx Message Buffer. */
  981. base->MB[mbIdx].CS = cs_temp;
  982. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641)
  983. base->MB[FLEXCAN_GetFirstValidMb(base)].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  984. base->MB[FLEXCAN_GetFirstValidMb(base)].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  985. #endif
  986. return kStatus_Success;
  987. }
  988. else
  989. {
  990. /* Tx Message Buffer is activated, return immediately. */
  991. return kStatus_Fail;
  992. }
  993. }
  994. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  995. status_t FLEXCAN_WriteFDTxMb(CAN_Type *base, uint8_t mbIdx, const flexcan_fd_frame_t *txFrame)
  996. {
  997. /* Assertion. */
  998. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  999. assert(txFrame);
  1000. assert(txFrame->length <= 15);
  1001. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1002. uint32_t cs_temp = 0;
  1003. uint8_t cnt = 0;
  1004. uint32_t can_cs = 0;
  1005. uint32_t dataSize;
  1006. dataSize = (base->FDCTRL & CAN_FDCTRL_MBDSR0_MASK) >> CAN_FDCTRL_MBDSR0_SHIFT;
  1007. switch (dataSize)
  1008. {
  1009. case kFLEXCAN_8BperMB:
  1010. can_cs = base->MB_8B[mbIdx].CS;
  1011. break;
  1012. case kFLEXCAN_16BperMB:
  1013. can_cs = base->MB_16B[mbIdx].CS;
  1014. break;
  1015. case kFLEXCAN_32BperMB:
  1016. can_cs = base->MB_32B[mbIdx].CS;
  1017. break;
  1018. case kFLEXCAN_64BperMB:
  1019. can_cs = base->MB_64B[mbIdx].CS;
  1020. break;
  1021. default:
  1022. break;
  1023. }
  1024. /* Check if Message Buffer is available. */
  1025. if (CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) != (can_cs & CAN_CS_CODE_MASK))
  1026. {
  1027. /* Inactive Tx Message Buffer and Fill Message ID field. */
  1028. switch (dataSize)
  1029. {
  1030. case kFLEXCAN_8BperMB:
  1031. base->MB_8B[mbIdx].CS = (can_cs & ~CAN_CS_CODE_MASK) | CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1032. base->MB_8B[mbIdx].ID = txFrame->id;
  1033. break;
  1034. case kFLEXCAN_16BperMB:
  1035. base->MB_16B[mbIdx].CS = (can_cs & ~CAN_CS_CODE_MASK) | CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1036. base->MB_16B[mbIdx].ID = txFrame->id;
  1037. break;
  1038. case kFLEXCAN_32BperMB:
  1039. base->MB_32B[mbIdx].CS = (can_cs & ~CAN_CS_CODE_MASK) | CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1040. base->MB_32B[mbIdx].ID = txFrame->id;
  1041. break;
  1042. case kFLEXCAN_64BperMB:
  1043. base->MB_64B[mbIdx].CS = (can_cs & ~CAN_CS_CODE_MASK) | CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1044. base->MB_64B[mbIdx].ID = txFrame->id;
  1045. break;
  1046. default:
  1047. break;
  1048. }
  1049. /* Fill Message Format field. */
  1050. if (kFLEXCAN_FrameFormatExtend == txFrame->format)
  1051. {
  1052. cs_temp |= CAN_CS_SRR_MASK | CAN_CS_IDE_MASK;
  1053. }
  1054. cs_temp |= CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) | CAN_CS_DLC(txFrame->length) | CAN_CS_EDL(1);
  1055. /* Load Message Payload and Activate Tx Message Buffer. */
  1056. switch (dataSize)
  1057. {
  1058. case kFLEXCAN_8BperMB:
  1059. for (cnt = 0; cnt < 2; cnt++)
  1060. {
  1061. base->MB_8B[mbIdx].WORD[cnt] = txFrame->dataWord[cnt];
  1062. }
  1063. base->MB_8B[mbIdx].CS = cs_temp;
  1064. break;
  1065. case kFLEXCAN_16BperMB:
  1066. for (cnt = 0; cnt < 4; cnt++)
  1067. {
  1068. base->MB_16B[mbIdx].WORD[cnt] = txFrame->dataWord[cnt];
  1069. }
  1070. base->MB_16B[mbIdx].CS = cs_temp;
  1071. break;
  1072. case kFLEXCAN_32BperMB:
  1073. for (cnt = 0; cnt < 8; cnt++)
  1074. {
  1075. base->MB_32B[mbIdx].WORD[cnt] = txFrame->dataWord[cnt];
  1076. }
  1077. base->MB_32B[mbIdx].CS = cs_temp;
  1078. break;
  1079. case kFLEXCAN_64BperMB:
  1080. for (cnt = 0; cnt < 16; cnt++)
  1081. {
  1082. base->MB_64B[mbIdx].WORD[cnt] = txFrame->dataWord[cnt];
  1083. }
  1084. base->MB_64B[mbIdx].CS = cs_temp;
  1085. break;
  1086. default:
  1087. break;
  1088. }
  1089. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641)
  1090. base->MB[FLEXCAN_GetFirstValidMb(base)].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1091. base->MB[FLEXCAN_GetFirstValidMb(base)].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1092. #endif
  1093. return kStatus_Success;
  1094. }
  1095. else
  1096. {
  1097. /* Tx Message Buffer is activated, return immediately. */
  1098. return kStatus_Fail;
  1099. }
  1100. }
  1101. #endif
  1102. status_t FLEXCAN_ReadRxMb(CAN_Type *base, uint8_t mbIdx, flexcan_frame_t *rxFrame)
  1103. {
  1104. /* Assertion. */
  1105. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1106. assert(rxFrame);
  1107. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1108. uint32_t cs_temp;
  1109. uint8_t rx_code;
  1110. /* Read CS field of Rx Message Buffer to lock Message Buffer. */
  1111. cs_temp = base->MB[mbIdx].CS;
  1112. /* Get Rx Message Buffer Code field. */
  1113. rx_code = (cs_temp & CAN_CS_CODE_MASK) >> CAN_CS_CODE_SHIFT;
  1114. /* Check to see if Rx Message Buffer is full. */
  1115. if ((kFLEXCAN_RxMbFull == rx_code) || (kFLEXCAN_RxMbOverrun == rx_code))
  1116. {
  1117. /* Store Message ID. */
  1118. rxFrame->id = base->MB[mbIdx].ID & (CAN_ID_EXT_MASK | CAN_ID_STD_MASK);
  1119. /* Get the message ID and format. */
  1120. rxFrame->format = (cs_temp & CAN_CS_IDE_MASK) ? kFLEXCAN_FrameFormatExtend : kFLEXCAN_FrameFormatStandard;
  1121. /* Get the message type. */
  1122. rxFrame->type = (cs_temp & CAN_CS_RTR_MASK) ? kFLEXCAN_FrameTypeRemote : kFLEXCAN_FrameTypeData;
  1123. /* Get the message length. */
  1124. rxFrame->length = (cs_temp & CAN_CS_DLC_MASK) >> CAN_CS_DLC_SHIFT;
  1125. /* Store Message Payload. */
  1126. rxFrame->dataWord0 = base->MB[mbIdx].WORD0;
  1127. rxFrame->dataWord1 = base->MB[mbIdx].WORD1;
  1128. /* Read free-running timer to unlock Rx Message Buffer. */
  1129. (void)base->TIMER;
  1130. if (kFLEXCAN_RxMbFull == rx_code)
  1131. {
  1132. return kStatus_Success;
  1133. }
  1134. else
  1135. {
  1136. return kStatus_FLEXCAN_RxOverflow;
  1137. }
  1138. }
  1139. else
  1140. {
  1141. /* Read free-running timer to unlock Rx Message Buffer. */
  1142. (void)base->TIMER;
  1143. return kStatus_Fail;
  1144. }
  1145. }
  1146. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1147. status_t FLEXCAN_ReadFDRxMb(CAN_Type *base, uint8_t mbIdx, flexcan_fd_frame_t *rxFrame)
  1148. {
  1149. /* Assertion. */
  1150. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1151. assert(rxFrame);
  1152. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1153. uint32_t cs_temp;
  1154. uint8_t rx_code;
  1155. uint8_t cnt = 0;
  1156. uint32_t can_id = 0;
  1157. uint32_t dataSize;
  1158. dataSize = (base->FDCTRL & CAN_FDCTRL_MBDSR0_MASK) >> CAN_FDCTRL_MBDSR0_SHIFT;
  1159. /* Read CS field of Rx Message Buffer to lock Message Buffer. */
  1160. switch (dataSize)
  1161. {
  1162. case kFLEXCAN_8BperMB:
  1163. cs_temp = base->MB_8B[mbIdx].CS;
  1164. can_id = base->MB_8B[mbIdx].ID;
  1165. break;
  1166. case kFLEXCAN_16BperMB:
  1167. cs_temp = base->MB_16B[mbIdx].CS;
  1168. can_id = base->MB_16B[mbIdx].ID;
  1169. break;
  1170. case kFLEXCAN_32BperMB:
  1171. cs_temp = base->MB_32B[mbIdx].CS;
  1172. can_id = base->MB_32B[mbIdx].ID;
  1173. break;
  1174. case kFLEXCAN_64BperMB:
  1175. cs_temp = base->MB_64B[mbIdx].CS;
  1176. can_id = base->MB_64B[mbIdx].ID;
  1177. break;
  1178. default:
  1179. break;
  1180. }
  1181. /* Get Rx Message Buffer Code field. */
  1182. rx_code = (cs_temp & CAN_CS_CODE_MASK) >> CAN_CS_CODE_SHIFT;
  1183. /* Check to see if Rx Message Buffer is full. */
  1184. if ((kFLEXCAN_RxMbFull == rx_code) || (kFLEXCAN_RxMbOverrun == rx_code))
  1185. {
  1186. /* Store Message ID. */
  1187. rxFrame->id = can_id & (CAN_ID_EXT_MASK | CAN_ID_STD_MASK);
  1188. /* Get the message ID and format. */
  1189. rxFrame->format = (cs_temp & CAN_CS_IDE_MASK) ? kFLEXCAN_FrameFormatExtend : kFLEXCAN_FrameFormatStandard;
  1190. /* Get the message type. */
  1191. rxFrame->type = (cs_temp & CAN_CS_RTR_MASK) ? kFLEXCAN_FrameTypeRemote : kFLEXCAN_FrameTypeData;
  1192. /* Get the message length. */
  1193. rxFrame->length = (cs_temp & CAN_CS_DLC_MASK) >> CAN_CS_DLC_SHIFT;
  1194. /* Store Message Payload. */
  1195. switch (dataSize)
  1196. {
  1197. case kFLEXCAN_8BperMB:
  1198. for (cnt = 0; cnt < 2; cnt++)
  1199. {
  1200. rxFrame->dataWord[cnt] = base->MB_8B[mbIdx].WORD[cnt];
  1201. }
  1202. break;
  1203. case kFLEXCAN_16BperMB:
  1204. for (cnt = 0; cnt < 4; cnt++)
  1205. {
  1206. rxFrame->dataWord[cnt] = base->MB_16B[mbIdx].WORD[cnt];
  1207. }
  1208. break;
  1209. case kFLEXCAN_32BperMB:
  1210. for (cnt = 0; cnt < 8; cnt++)
  1211. {
  1212. rxFrame->dataWord[cnt] = base->MB_32B[mbIdx].WORD[cnt];
  1213. }
  1214. break;
  1215. case kFLEXCAN_64BperMB:
  1216. for (cnt = 0; cnt < 16; cnt++)
  1217. {
  1218. rxFrame->dataWord[cnt] = base->MB_64B[mbIdx].WORD[cnt];
  1219. }
  1220. break;
  1221. default:
  1222. break;
  1223. }
  1224. /* Read free-running timer to unlock Rx Message Buffer. */
  1225. (void)base->TIMER;
  1226. if (kFLEXCAN_RxMbFull == rx_code)
  1227. {
  1228. return kStatus_Success;
  1229. }
  1230. else
  1231. {
  1232. return kStatus_FLEXCAN_RxOverflow;
  1233. }
  1234. }
  1235. else
  1236. {
  1237. /* Read free-running timer to unlock Rx Message Buffer. */
  1238. (void)base->TIMER;
  1239. return kStatus_Fail;
  1240. }
  1241. }
  1242. #endif
  1243. status_t FLEXCAN_ReadRxFifo(CAN_Type *base, flexcan_frame_t *rxFrame)
  1244. {
  1245. /* Assertion. */
  1246. assert(rxFrame);
  1247. uint32_t cs_temp;
  1248. /* Check if Rx FIFO is Enabled. */
  1249. if (base->MCR & CAN_MCR_RFEN_MASK)
  1250. {
  1251. /* Read CS field of Rx Message Buffer to lock Message Buffer. */
  1252. cs_temp = base->MB[0].CS;
  1253. /* Read data from Rx FIFO output port. */
  1254. /* Store Message ID. */
  1255. rxFrame->id = base->MB[0].ID & (CAN_ID_EXT_MASK | CAN_ID_STD_MASK);
  1256. /* Get the message ID and format. */
  1257. rxFrame->format = (cs_temp & CAN_CS_IDE_MASK) ? kFLEXCAN_FrameFormatExtend : kFLEXCAN_FrameFormatStandard;
  1258. /* Get the message type. */
  1259. rxFrame->type = (cs_temp & CAN_CS_RTR_MASK) ? kFLEXCAN_FrameTypeRemote : kFLEXCAN_FrameTypeData;
  1260. /* Get the message length. */
  1261. rxFrame->length = (cs_temp & CAN_CS_DLC_MASK) >> CAN_CS_DLC_SHIFT;
  1262. /* Store Message Payload. */
  1263. rxFrame->dataWord0 = base->MB[0].WORD0;
  1264. rxFrame->dataWord1 = base->MB[0].WORD1;
  1265. /* Store ID Filter Hit Index. */
  1266. rxFrame->idhit = (uint8_t)(base->RXFIR & CAN_RXFIR_IDHIT_MASK);
  1267. /* Read free-running timer to unlock Rx Message Buffer. */
  1268. (void)base->TIMER;
  1269. return kStatus_Success;
  1270. }
  1271. else
  1272. {
  1273. return kStatus_Fail;
  1274. }
  1275. }
  1276. status_t FLEXCAN_TransferSendBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_frame_t *txFrame)
  1277. {
  1278. /* Write Tx Message Buffer to initiate a data sending. */
  1279. if (kStatus_Success == FLEXCAN_WriteTxMb(base, mbIdx, txFrame))
  1280. {
  1281. /* Wait until CAN Message send out. */
  1282. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1283. while (!FLEXCAN_GetMbStatusFlags(base, (uint64_t)1 << mbIdx))
  1284. #else
  1285. while (!FLEXCAN_GetMbStatusFlags(base, 1 << mbIdx))
  1286. #endif
  1287. {
  1288. }
  1289. /* Clean Tx Message Buffer Flag. */
  1290. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1291. FLEXCAN_ClearMbStatusFlags(base, (uint64_t)1 << mbIdx);
  1292. #else
  1293. FLEXCAN_ClearMbStatusFlags(base, 1 << mbIdx);
  1294. #endif
  1295. return kStatus_Success;
  1296. }
  1297. else
  1298. {
  1299. return kStatus_Fail;
  1300. }
  1301. }
  1302. status_t FLEXCAN_TransferReceiveBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_frame_t *rxFrame)
  1303. {
  1304. /* Wait until Rx Message Buffer non-empty. */
  1305. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1306. while (!FLEXCAN_GetMbStatusFlags(base, (uint64_t)1 << mbIdx))
  1307. #else
  1308. while (!FLEXCAN_GetMbStatusFlags(base, 1 << mbIdx))
  1309. #endif
  1310. {
  1311. }
  1312. /* Clean Rx Message Buffer Flag. */
  1313. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1314. FLEXCAN_ClearMbStatusFlags(base, (uint64_t)1 << mbIdx);
  1315. #else
  1316. FLEXCAN_ClearMbStatusFlags(base, 1 << mbIdx);
  1317. #endif
  1318. /* Read Received CAN Message. */
  1319. return FLEXCAN_ReadRxMb(base, mbIdx, rxFrame);
  1320. }
  1321. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1322. status_t FLEXCAN_TransferFDSendBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_fd_frame_t *txFrame)
  1323. {
  1324. /* Write Tx Message Buffer to initiate a data sending. */
  1325. if (kStatus_Success == FLEXCAN_WriteFDTxMb(base, mbIdx, txFrame))
  1326. {
  1327. /* Wait until CAN Message send out. */
  1328. while (!FLEXCAN_GetMbStatusFlags(base, 1 << mbIdx))
  1329. {
  1330. }
  1331. /* Clean Tx Message Buffer Flag. */
  1332. FLEXCAN_ClearMbStatusFlags(base, 1 << mbIdx);
  1333. return kStatus_Success;
  1334. }
  1335. else
  1336. {
  1337. return kStatus_Fail;
  1338. }
  1339. }
  1340. status_t FLEXCAN_TransferFDReceiveBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_fd_frame_t *rxFrame)
  1341. {
  1342. /* Wait until Rx Message Buffer non-empty. */
  1343. while (!FLEXCAN_GetMbStatusFlags(base, 1 << mbIdx))
  1344. {
  1345. }
  1346. /* Clean Rx Message Buffer Flag. */
  1347. FLEXCAN_ClearMbStatusFlags(base, 1 << mbIdx);
  1348. /* Read Received CAN Message. */
  1349. return FLEXCAN_ReadFDRxMb(base, mbIdx, rxFrame);
  1350. }
  1351. #endif
  1352. status_t FLEXCAN_TransferReceiveFifoBlocking(CAN_Type *base, flexcan_frame_t *rxFrame)
  1353. {
  1354. status_t rxFifoStatus;
  1355. /* Wait until Rx FIFO non-empty. */
  1356. while (!FLEXCAN_GetMbStatusFlags(base, kFLEXCAN_RxFifoFrameAvlFlag))
  1357. {
  1358. }
  1359. /* */
  1360. rxFifoStatus = FLEXCAN_ReadRxFifo(base, rxFrame);
  1361. /* Clean Rx Fifo available flag. */
  1362. FLEXCAN_ClearMbStatusFlags(base, kFLEXCAN_RxFifoFrameAvlFlag);
  1363. return rxFifoStatus;
  1364. }
  1365. void FLEXCAN_TransferCreateHandle(CAN_Type *base,
  1366. flexcan_handle_t *handle,
  1367. flexcan_transfer_callback_t callback,
  1368. void *userData)
  1369. {
  1370. assert(handle);
  1371. uint8_t instance;
  1372. /* Clean FlexCAN transfer handle. */
  1373. memset(handle, 0, sizeof(*handle));
  1374. /* Get instance from peripheral base address. */
  1375. instance = FLEXCAN_GetInstance(base);
  1376. /* Save the context in global variables to support the double weak mechanism. */
  1377. s_flexcanHandle[instance] = handle;
  1378. /* Register Callback function. */
  1379. handle->callback = callback;
  1380. handle->userData = userData;
  1381. s_flexcanIsr = FLEXCAN_TransferHandleIRQ;
  1382. /* We Enable Error & Status interrupt here, because this interrupt just
  1383. * report current status of FlexCAN module through Callback function.
  1384. * It is insignificance without a available callback function.
  1385. */
  1386. if (handle->callback != NULL)
  1387. {
  1388. FLEXCAN_EnableInterrupts(base, kFLEXCAN_BusOffInterruptEnable | kFLEXCAN_ErrorInterruptEnable |
  1389. kFLEXCAN_RxWarningInterruptEnable | kFLEXCAN_TxWarningInterruptEnable |
  1390. kFLEXCAN_WakeUpInterruptEnable);
  1391. }
  1392. else
  1393. {
  1394. FLEXCAN_DisableInterrupts(base, kFLEXCAN_BusOffInterruptEnable | kFLEXCAN_ErrorInterruptEnable |
  1395. kFLEXCAN_RxWarningInterruptEnable | kFLEXCAN_TxWarningInterruptEnable |
  1396. kFLEXCAN_WakeUpInterruptEnable);
  1397. }
  1398. /* Enable interrupts in NVIC. */
  1399. EnableIRQ((IRQn_Type)(s_flexcanRxWarningIRQ[instance]));
  1400. EnableIRQ((IRQn_Type)(s_flexcanTxWarningIRQ[instance]));
  1401. EnableIRQ((IRQn_Type)(s_flexcanWakeUpIRQ[instance]));
  1402. EnableIRQ((IRQn_Type)(s_flexcanErrorIRQ[instance]));
  1403. EnableIRQ((IRQn_Type)(s_flexcanBusOffIRQ[instance]));
  1404. EnableIRQ((IRQn_Type)(s_flexcanMbIRQ[instance]));
  1405. }
  1406. status_t FLEXCAN_TransferSendNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *xfer)
  1407. {
  1408. /* Assertion. */
  1409. assert(handle);
  1410. assert(xfer);
  1411. assert(xfer->mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1412. assert(!FLEXCAN_IsMbOccupied(base, xfer->mbIdx));
  1413. /* Check if Message Buffer is idle. */
  1414. if (kFLEXCAN_StateIdle == handle->mbState[xfer->mbIdx])
  1415. {
  1416. /* Distinguish transmit type. */
  1417. if (kFLEXCAN_FrameTypeRemote == xfer->frame->type)
  1418. {
  1419. handle->mbState[xfer->mbIdx] = kFLEXCAN_StateTxRemote;
  1420. /* Register user Frame buffer to receive remote Frame. */
  1421. handle->mbFrameBuf[xfer->mbIdx] = xfer->frame;
  1422. }
  1423. else
  1424. {
  1425. handle->mbState[xfer->mbIdx] = kFLEXCAN_StateTxData;
  1426. }
  1427. if (kStatus_Success == FLEXCAN_WriteTxMb(base, xfer->mbIdx, xfer->frame))
  1428. {
  1429. /* Enable Message Buffer Interrupt. */
  1430. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1431. FLEXCAN_EnableMbInterrupts(base, (uint64_t)1 << xfer->mbIdx);
  1432. #else
  1433. FLEXCAN_EnableMbInterrupts(base, 1 << xfer->mbIdx);
  1434. #endif
  1435. return kStatus_Success;
  1436. }
  1437. else
  1438. {
  1439. handle->mbState[xfer->mbIdx] = kFLEXCAN_StateIdle;
  1440. return kStatus_Fail;
  1441. }
  1442. }
  1443. else
  1444. {
  1445. return kStatus_FLEXCAN_TxBusy;
  1446. }
  1447. }
  1448. status_t FLEXCAN_TransferReceiveNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *xfer)
  1449. {
  1450. /* Assertion. */
  1451. assert(handle);
  1452. assert(xfer);
  1453. assert(xfer->mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1454. assert(!FLEXCAN_IsMbOccupied(base, xfer->mbIdx));
  1455. /* Check if Message Buffer is idle. */
  1456. if (kFLEXCAN_StateIdle == handle->mbState[xfer->mbIdx])
  1457. {
  1458. handle->mbState[xfer->mbIdx] = kFLEXCAN_StateRxData;
  1459. /* Register Message Buffer. */
  1460. handle->mbFrameBuf[xfer->mbIdx] = xfer->frame;
  1461. /* Enable Message Buffer Interrupt. */
  1462. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1463. FLEXCAN_EnableMbInterrupts(base, (uint64_t)1 << xfer->mbIdx);
  1464. #else
  1465. FLEXCAN_EnableMbInterrupts(base, 1 << xfer->mbIdx);
  1466. #endif
  1467. return kStatus_Success;
  1468. }
  1469. else
  1470. {
  1471. return kStatus_FLEXCAN_RxBusy;
  1472. }
  1473. }
  1474. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1475. status_t FLEXCAN_TransferFDSendNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *xfer)
  1476. {
  1477. /* Assertion. */
  1478. assert(handle);
  1479. assert(xfer);
  1480. assert(xfer->mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1481. assert(!FLEXCAN_IsMbOccupied(base, xfer->mbIdx));
  1482. /* Check if Message Buffer is idle. */
  1483. if (kFLEXCAN_StateIdle == handle->mbState[xfer->mbIdx])
  1484. {
  1485. /* Distinguish transmit type. */
  1486. if (kFLEXCAN_FrameTypeRemote == xfer->frame->type)
  1487. {
  1488. handle->mbState[xfer->mbIdx] = kFLEXCAN_StateTxRemote;
  1489. /* Register user Frame buffer to receive remote Frame. */
  1490. handle->mbFDFrameBuf[xfer->mbIdx] = xfer->framefd;
  1491. }
  1492. else
  1493. {
  1494. handle->mbState[xfer->mbIdx] = kFLEXCAN_StateTxData;
  1495. }
  1496. if (kStatus_Success == FLEXCAN_WriteFDTxMb(base, xfer->mbIdx, xfer->framefd))
  1497. {
  1498. /* Enable Message Buffer Interrupt. */
  1499. FLEXCAN_EnableMbInterrupts(base, 1 << xfer->mbIdx);
  1500. return kStatus_Success;
  1501. }
  1502. else
  1503. {
  1504. handle->mbState[xfer->mbIdx] = kFLEXCAN_StateIdle;
  1505. return kStatus_Fail;
  1506. }
  1507. }
  1508. else
  1509. {
  1510. return kStatus_FLEXCAN_TxBusy;
  1511. }
  1512. }
  1513. status_t FLEXCAN_TransferFDReceiveNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *xfer)
  1514. {
  1515. /* Assertion. */
  1516. assert(handle);
  1517. assert(xfer);
  1518. assert(xfer->mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1519. assert(!FLEXCAN_IsMbOccupied(base, xfer->mbIdx));
  1520. /* Check if Message Buffer is idle. */
  1521. if (kFLEXCAN_StateIdle == handle->mbState[xfer->mbIdx])
  1522. {
  1523. handle->mbState[xfer->mbIdx] = kFLEXCAN_StateRxData;
  1524. /* Register Message Buffer. */
  1525. handle->mbFDFrameBuf[xfer->mbIdx] = xfer->framefd;
  1526. /* Enable Message Buffer Interrupt. */
  1527. FLEXCAN_EnableMbInterrupts(base, 1 << xfer->mbIdx);
  1528. return kStatus_Success;
  1529. }
  1530. else
  1531. {
  1532. return kStatus_FLEXCAN_RxBusy;
  1533. }
  1534. }
  1535. #endif
  1536. status_t FLEXCAN_TransferReceiveFifoNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_fifo_transfer_t *xfer)
  1537. {
  1538. /* Assertion. */
  1539. assert(handle);
  1540. assert(xfer);
  1541. /* Check if Message Buffer is idle. */
  1542. if (kFLEXCAN_StateIdle == handle->rxFifoState)
  1543. {
  1544. handle->rxFifoState = kFLEXCAN_StateRxFifo;
  1545. /* Register Message Buffer. */
  1546. handle->rxFifoFrameBuf = xfer->frame;
  1547. /* Enable Message Buffer Interrupt. */
  1548. FLEXCAN_EnableMbInterrupts(
  1549. base, kFLEXCAN_RxFifoOverflowFlag | kFLEXCAN_RxFifoWarningFlag | kFLEXCAN_RxFifoFrameAvlFlag);
  1550. return kStatus_Success;
  1551. }
  1552. else
  1553. {
  1554. return kStatus_FLEXCAN_RxFifoBusy;
  1555. }
  1556. }
  1557. void FLEXCAN_TransferAbortSend(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx)
  1558. {
  1559. /* Assertion. */
  1560. assert(handle);
  1561. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1562. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1563. /* Disable Message Buffer Interrupt. */
  1564. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1565. FLEXCAN_DisableMbInterrupts(base, (uint64_t)1 << mbIdx);
  1566. #else
  1567. FLEXCAN_DisableMbInterrupts(base, 1 << mbIdx);
  1568. #endif
  1569. /* Un-register handle. */
  1570. handle->mbFrameBuf[mbIdx] = 0x0;
  1571. /* Clean Message Buffer. */
  1572. FLEXCAN_SetTxMbConfig(base, mbIdx, true);
  1573. handle->mbState[mbIdx] = kFLEXCAN_StateIdle;
  1574. }
  1575. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1576. void FLEXCAN_TransferFDAbortSend(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx)
  1577. {
  1578. /* Assertion. */
  1579. assert(handle);
  1580. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1581. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1582. /* Disable Message Buffer Interrupt. */
  1583. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1584. FLEXCAN_DisableMbInterrupts(base, (uint64_t)1 << mbIdx);
  1585. #else
  1586. FLEXCAN_DisableMbInterrupts(base, 1 << mbIdx);
  1587. #endif
  1588. /* Un-register handle. */
  1589. handle->mbFDFrameBuf[mbIdx] = 0x0;
  1590. /* Clean Message Buffer. */
  1591. FLEXCAN_SetFDTxMbConfig(base, mbIdx, true);
  1592. handle->mbState[mbIdx] = kFLEXCAN_StateIdle;
  1593. }
  1594. void FLEXCAN_TransferFDAbortReceive(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx)
  1595. {
  1596. /* Assertion. */
  1597. assert(handle);
  1598. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1599. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1600. /* Disable Message Buffer Interrupt. */
  1601. FLEXCAN_DisableMbInterrupts(base, 1 << mbIdx);
  1602. /* Un-register handle. */
  1603. handle->mbFDFrameBuf[mbIdx] = 0x0;
  1604. handle->mbState[mbIdx] = kFLEXCAN_StateIdle;
  1605. }
  1606. #endif
  1607. void FLEXCAN_TransferAbortReceive(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx)
  1608. {
  1609. /* Assertion. */
  1610. assert(handle);
  1611. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1612. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1613. /* Disable Message Buffer Interrupt. */
  1614. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1615. FLEXCAN_DisableMbInterrupts(base, (uint64_t)1 << mbIdx);
  1616. #else
  1617. FLEXCAN_DisableMbInterrupts(base, 1 << mbIdx);
  1618. #endif
  1619. /* Un-register handle. */
  1620. handle->mbFrameBuf[mbIdx] = 0x0;
  1621. handle->mbState[mbIdx] = kFLEXCAN_StateIdle;
  1622. }
  1623. void FLEXCAN_TransferAbortReceiveFifo(CAN_Type *base, flexcan_handle_t *handle)
  1624. {
  1625. /* Assertion. */
  1626. assert(handle);
  1627. /* Check if Rx FIFO is enabled. */
  1628. if (base->MCR & CAN_MCR_RFEN_MASK)
  1629. {
  1630. /* Disable Rx Message FIFO Interrupts. */
  1631. FLEXCAN_DisableMbInterrupts(
  1632. base, kFLEXCAN_RxFifoOverflowFlag | kFLEXCAN_RxFifoWarningFlag | kFLEXCAN_RxFifoFrameAvlFlag);
  1633. /* Un-register handle. */
  1634. handle->rxFifoFrameBuf = 0x0;
  1635. }
  1636. handle->rxFifoState = kFLEXCAN_StateIdle;
  1637. }
  1638. void FLEXCAN_TransferHandleIRQ(CAN_Type *base, flexcan_handle_t *handle)
  1639. {
  1640. /* Assertion. */
  1641. assert(handle);
  1642. status_t status = kStatus_FLEXCAN_UnHandled;
  1643. uint32_t result;
  1644. /* Store Current FlexCAN Module Error and Status. */
  1645. result = base->ESR1;
  1646. do
  1647. {
  1648. /* Solve FlexCAN Error and Status Interrupt. */
  1649. if (result & (kFLEXCAN_TxWarningIntFlag | kFLEXCAN_RxWarningIntFlag | kFLEXCAN_BusOffIntFlag |
  1650. kFLEXCAN_ErrorIntFlag | kFLEXCAN_WakeUpIntFlag))
  1651. {
  1652. status = kStatus_FLEXCAN_ErrorStatus;
  1653. /* Clear FlexCAN Error and Status Interrupt. */
  1654. FLEXCAN_ClearStatusFlags(base, kFLEXCAN_TxWarningIntFlag | kFLEXCAN_RxWarningIntFlag |
  1655. kFLEXCAN_BusOffIntFlag | kFLEXCAN_ErrorIntFlag | kFLEXCAN_WakeUpIntFlag);
  1656. }
  1657. /* Solve FlexCAN Rx FIFO & Message Buffer Interrupt. */
  1658. else
  1659. {
  1660. /* For this implementation, we solve the Message with lowest MB index first. */
  1661. for (result = 0; result < FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base); result++)
  1662. {
  1663. /* Get the lowest unhandled Message Buffer */
  1664. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1665. if ((FLEXCAN_GetMbStatusFlags(base, (uint64_t)1 << result)) && (FLEXCAN_IsMbIntEnabled(base, result)))
  1666. #else
  1667. if ((FLEXCAN_GetMbStatusFlags(base, 1 << result)) && (FLEXCAN_IsMbIntEnabled(base, result)))
  1668. #endif
  1669. {
  1670. break;
  1671. }
  1672. }
  1673. /* Does not find Message to deal with. */
  1674. if (result == FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base))
  1675. {
  1676. break;
  1677. }
  1678. /* Solve Rx FIFO interrupt. */
  1679. if ((kFLEXCAN_StateIdle != handle->rxFifoState) && ((1 << result) <= kFLEXCAN_RxFifoOverflowFlag))
  1680. {
  1681. switch (1 << result)
  1682. {
  1683. case kFLEXCAN_RxFifoOverflowFlag:
  1684. status = kStatus_FLEXCAN_RxFifoOverflow;
  1685. break;
  1686. case kFLEXCAN_RxFifoWarningFlag:
  1687. status = kStatus_FLEXCAN_RxFifoWarning;
  1688. break;
  1689. case kFLEXCAN_RxFifoFrameAvlFlag:
  1690. status = FLEXCAN_ReadRxFifo(base, handle->rxFifoFrameBuf);
  1691. if (kStatus_Success == status)
  1692. {
  1693. status = kStatus_FLEXCAN_RxFifoIdle;
  1694. }
  1695. FLEXCAN_TransferAbortReceiveFifo(base, handle);
  1696. break;
  1697. default:
  1698. status = kStatus_FLEXCAN_UnHandled;
  1699. break;
  1700. }
  1701. }
  1702. else
  1703. {
  1704. /* Get current State of Message Buffer. */
  1705. switch (handle->mbState[result])
  1706. {
  1707. /* Solve Rx Data Frame. */
  1708. case kFLEXCAN_StateRxData:
  1709. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1710. status = FLEXCAN_ReadFDRxMb(base, result, handle->mbFDFrameBuf[result]);
  1711. #else
  1712. status = FLEXCAN_ReadRxMb(base, result, handle->mbFrameBuf[result]);
  1713. #endif
  1714. if (kStatus_Success == status)
  1715. {
  1716. status = kStatus_FLEXCAN_RxIdle;
  1717. }
  1718. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1719. FLEXCAN_TransferFDAbortReceive(base, handle, result);
  1720. #else
  1721. FLEXCAN_TransferAbortReceive(base, handle, result);
  1722. #endif
  1723. break;
  1724. /* Solve Rx Remote Frame. */
  1725. case kFLEXCAN_StateRxRemote:
  1726. status = FLEXCAN_ReadRxMb(base, result, handle->mbFrameBuf[result]);
  1727. if (kStatus_Success == status)
  1728. {
  1729. status = kStatus_FLEXCAN_RxIdle;
  1730. }
  1731. FLEXCAN_TransferAbortReceive(base, handle, result);
  1732. break;
  1733. /* Solve Tx Data Frame. */
  1734. case kFLEXCAN_StateTxData:
  1735. status = kStatus_FLEXCAN_TxIdle;
  1736. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1737. FLEXCAN_TransferFDAbortSend(base, handle, result);
  1738. #else
  1739. FLEXCAN_TransferAbortSend(base, handle, result);
  1740. #endif
  1741. break;
  1742. /* Solve Tx Remote Frame. */
  1743. case kFLEXCAN_StateTxRemote:
  1744. handle->mbState[result] = kFLEXCAN_StateRxRemote;
  1745. status = kStatus_FLEXCAN_TxSwitchToRx;
  1746. break;
  1747. default:
  1748. status = kStatus_FLEXCAN_UnHandled;
  1749. break;
  1750. }
  1751. }
  1752. /* Clear resolved Message Buffer IRQ. */
  1753. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1754. FLEXCAN_ClearMbStatusFlags(base, (uint64_t)1 << result);
  1755. #else
  1756. FLEXCAN_ClearMbStatusFlags(base, 1 << result);
  1757. #endif
  1758. }
  1759. /* Calling Callback Function if has one. */
  1760. if (handle->callback != NULL)
  1761. {
  1762. handle->callback(base, handle, status, result, handle->userData);
  1763. }
  1764. /* Reset return status */
  1765. status = kStatus_FLEXCAN_UnHandled;
  1766. /* Store Current FlexCAN Module Error and Status. */
  1767. result = base->ESR1;
  1768. }
  1769. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1770. while ((0 != FLEXCAN_GetMbStatusFlags(base, 0xFFFFFFFFFFFFFFFFU)) ||
  1771. (0 != (result & (kFLEXCAN_TxWarningIntFlag | kFLEXCAN_RxWarningIntFlag | kFLEXCAN_BusOffIntFlag |
  1772. kFLEXCAN_ErrorIntFlag | kFLEXCAN_WakeUpIntFlag))));
  1773. #else
  1774. while ((0 != FLEXCAN_GetMbStatusFlags(base, 0xFFFFFFFFU)) ||
  1775. (0 != (result & (kFLEXCAN_TxWarningIntFlag | kFLEXCAN_RxWarningIntFlag | kFLEXCAN_BusOffIntFlag |
  1776. kFLEXCAN_ErrorIntFlag | kFLEXCAN_WakeUpIntFlag))));
  1777. #endif
  1778. }
  1779. #if defined(CAN0)
  1780. void CAN0_DriverIRQHandler(void)
  1781. {
  1782. assert(s_flexcanHandle[0]);
  1783. s_flexcanIsr(CAN0, s_flexcanHandle[0]);
  1784. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1785. exception return operation might vector to incorrect interrupt */
  1786. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1787. __DSB();
  1788. #endif
  1789. }
  1790. #endif
  1791. #if defined(CAN1)
  1792. void CAN1_DriverIRQHandler(void)
  1793. {
  1794. assert(s_flexcanHandle[1]);
  1795. s_flexcanIsr(CAN1, s_flexcanHandle[1]);
  1796. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1797. exception return operation might vector to incorrect interrupt */
  1798. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1799. __DSB();
  1800. #endif
  1801. }
  1802. #endif
  1803. #if defined(CAN2)
  1804. void CAN2_DriverIRQHandler(void)
  1805. {
  1806. assert(s_flexcanHandle[2]);
  1807. s_flexcanIsr(CAN2, s_flexcanHandle[2]);
  1808. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1809. exception return operation might vector to incorrect interrupt */
  1810. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1811. __DSB();
  1812. #endif
  1813. }
  1814. #endif
  1815. #if defined(CAN3)
  1816. void CAN3_DriverIRQHandler(void)
  1817. {
  1818. assert(s_flexcanHandle[3]);
  1819. s_flexcanIsr(CAN3, s_flexcanHandle[3]);
  1820. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1821. exception return operation might vector to incorrect interrupt */
  1822. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1823. __DSB();
  1824. #endif
  1825. }
  1826. #endif
  1827. #if defined(CAN4)
  1828. void CAN4_DriverIRQHandler(void)
  1829. {
  1830. assert(s_flexcanHandle[4]);
  1831. s_flexcanIsr(CAN4, s_flexcanHandle[4]);
  1832. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1833. exception return operation might vector to incorrect interrupt */
  1834. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1835. __DSB();
  1836. #endif
  1837. }
  1838. #endif
  1839. #if defined(DMA__CAN0)
  1840. void DMA_FLEXCAN0_INT_DriverIRQHandler(void)
  1841. {
  1842. assert(s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN0)]);
  1843. s_flexcanIsr(DMA__CAN0, s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN0)]);
  1844. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1845. exception return operation might vector to incorrect interrupt */
  1846. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1847. __DSB();
  1848. #endif
  1849. }
  1850. #endif
  1851. #if defined(DMA__CAN1)
  1852. void DMA_FLEXCAN1_INT_DriverIRQHandler(void)
  1853. {
  1854. assert(s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN1)]);
  1855. s_flexcanIsr(DMA__CAN1, s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN1)]);
  1856. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1857. exception return operation might vector to incorrect interrupt */
  1858. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1859. __DSB();
  1860. #endif
  1861. }
  1862. #endif
  1863. #if defined(DMA__CAN2)
  1864. void DMA_FLEXCAN2_INT_DriverIRQHandler(void)
  1865. {
  1866. assert(s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN2)]);
  1867. s_flexcanIsr(DMA__CAN2, s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN2)]);
  1868. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1869. exception return operation might vector to incorrect interrupt */
  1870. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1871. __DSB();
  1872. #endif
  1873. }
  1874. #endif