Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

3565 righe
125 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_flexcan.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.flexcan"
  15. #endif
  16. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_6032) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_6032)
  17. #define RXINTERMISSION (CAN_DBG1_CFSM(0x2f))
  18. #define TXINTERMISSION (CAN_DBG1_CFSM(0x14))
  19. #define BUSIDLE (CAN_DBG1_CFSM(0x02))
  20. #define CBN_VALUE3 (CAN_DBG1_CBN(0x03))
  21. #define DELAY_BUSIDLE (200)
  22. #endif
  23. #define IDEAL_SP_LOW (750U)
  24. #define IDEAL_SP_MID (800U)
  25. #define IDEAL_SP_HIGH (875U)
  26. #define IDEAL_SP_FACTOR (1000U)
  27. #define MAX_PROPSEG (CAN_CTRL1_PROPSEG_MASK >> CAN_CTRL1_PROPSEG_SHIFT)
  28. #define MAX_PSEG1 (CAN_CTRL1_PSEG1_MASK >> CAN_CTRL1_PSEG1_SHIFT)
  29. #define MAX_PSEG2 (CAN_CTRL1_PSEG2_MASK >> CAN_CTRL1_PSEG2_SHIFT)
  30. #define MAX_RJW (CAN_CTRL1_RJW_MASK >> CAN_CTRL1_RJW_SHIFT)
  31. #define MAX_PRESDIV (CAN_CTRL1_PRESDIV_MASK >> CAN_CTRL1_PRESDIV_SHIFT)
  32. #define CTRL1_MAX_TIME_QUANTA (1U + MAX_PROPSEG + 1U + MAX_PSEG1 + 1U + MAX_PSEG2 + 1U)
  33. #define CTRL1_MIN_TIME_QUANTA (8U)
  34. #define MAX_EPROPSEG (CAN_CBT_EPROPSEG_MASK >> CAN_CBT_EPROPSEG_SHIFT)
  35. #define MAX_EPSEG1 (CAN_CBT_EPSEG1_MASK >> CAN_CBT_EPSEG1_SHIFT)
  36. #define MAX_EPSEG2 (CAN_CBT_EPSEG2_MASK >> CAN_CBT_EPSEG2_SHIFT)
  37. #define MAX_ERJW (CAN_CBT_ERJW_MASK >> CAN_CBT_ERJW_SHIFT)
  38. #define MAX_EPRESDIV (CAN_CBT_EPRESDIV_MASK >> CAN_CBT_EPRESDIV_SHIFT)
  39. #define CBT_MAX_TIME_QUANTA (1U + MAX_EPROPSEG + 1U + MAX_EPSEG1 + 1U + MAX_EPSEG2 + 1U)
  40. #define CBT_MIN_TIME_QUANTA (8U)
  41. #define MAX_FPROPSEG (CAN_FDCBT_FPROPSEG_MASK >> CAN_FDCBT_FPROPSEG_SHIFT)
  42. #define MAX_FPSEG1 (CAN_FDCBT_FPSEG1_MASK >> CAN_FDCBT_FPSEG1_SHIFT)
  43. #define MAX_FPSEG2 (CAN_FDCBT_FPSEG2_MASK >> CAN_FDCBT_FPSEG2_SHIFT)
  44. #define MAX_FRJW (CAN_FDCBT_FRJW_MASK >> CAN_FDCBT_FRJW_SHIFT)
  45. #define MAX_FPRESDIV (CAN_FDCBT_FPRESDIV_MASK >> CAN_FDCBT_FPRESDIV_SHIFT)
  46. #define FDCBT_MAX_TIME_QUANTA (1U + MAX_FPROPSEG + 0U + MAX_FPSEG1 + 1U + MAX_FPSEG2 + 1U)
  47. #define FDCBT_MIN_TIME_QUANTA (5U)
  48. #define MAX_CANFD_BAUDRATE (8000000U)
  49. #define MAX_CAN_BAUDRATE (1000000U)
  50. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_9595) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_9595)
  51. #define CAN_ESR1_FLTCONF_BUSOFF CAN_ESR1_FLTCONF(2U)
  52. #endif
  53. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  54. #ifndef CAN_CLOCK_CHECK_NO_AFFECTS
  55. /* If no define such MACRO, it mean that the CAN in current device have no clock affect issue. */
  56. #define CAN_CLOCK_CHECK_NO_AFFECTS (true)
  57. #endif /* CAN_CLOCK_CHECK_NO_AFFECTS */
  58. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  59. /*! @brief FlexCAN Internal State. */
  60. enum _flexcan_state
  61. {
  62. kFLEXCAN_StateIdle = 0x0, /*!< MB/RxFIFO idle.*/
  63. kFLEXCAN_StateRxData = 0x1, /*!< MB receiving.*/
  64. kFLEXCAN_StateRxRemote = 0x2, /*!< MB receiving remote reply.*/
  65. kFLEXCAN_StateTxData = 0x3, /*!< MB transmitting.*/
  66. kFLEXCAN_StateTxRemote = 0x4, /*!< MB transmitting remote request.*/
  67. kFLEXCAN_StateRxFifo = 0x5, /*!< RxFIFO receiving.*/
  68. };
  69. /*! @brief FlexCAN message buffer CODE for Rx buffers. */
  70. enum _flexcan_mb_code_rx
  71. {
  72. kFLEXCAN_RxMbInactive = 0x0, /*!< MB is not active.*/
  73. kFLEXCAN_RxMbFull = 0x2, /*!< MB is full.*/
  74. kFLEXCAN_RxMbEmpty = 0x4, /*!< MB is active and empty.*/
  75. kFLEXCAN_RxMbOverrun = 0x6, /*!< MB is overwritten into a full buffer.*/
  76. kFLEXCAN_RxMbBusy = 0x8, /*!< FlexCAN is updating the contents of the MB.*/
  77. /*! The CPU must not access the MB.*/
  78. kFLEXCAN_RxMbRanswer = 0xA, /*!< A frame was configured to recognize a Remote Request Frame */
  79. /*! and transmit a Response Frame in return.*/
  80. kFLEXCAN_RxMbNotUsed = 0xF, /*!< Not used.*/
  81. };
  82. /*! @brief FlexCAN message buffer CODE FOR Tx buffers. */
  83. enum _flexcan_mb_code_tx
  84. {
  85. kFLEXCAN_TxMbInactive = 0x8, /*!< MB is not active.*/
  86. kFLEXCAN_TxMbAbort = 0x9, /*!< MB is aborted.*/
  87. kFLEXCAN_TxMbDataOrRemote = 0xC, /*!< MB is a TX Data Frame(when MB RTR = 0) or */
  88. /*!< MB is a TX Remote Request Frame (when MB RTR = 1).*/
  89. kFLEXCAN_TxMbTanswer = 0xE, /*!< MB is a TX Response Request Frame from */
  90. /*! an incoming Remote Request Frame.*/
  91. kFLEXCAN_TxMbNotUsed = 0xF, /*!< Not used.*/
  92. };
  93. /* Typedef for interrupt handler. */
  94. typedef void (*flexcan_isr_t)(CAN_Type *base, flexcan_handle_t *handle);
  95. /*******************************************************************************
  96. * Prototypes
  97. ******************************************************************************/
  98. #if !defined(NDEBUG)
  99. /*!
  100. * @brief Check if Message Buffer is occupied by Rx FIFO.
  101. *
  102. * This function check if Message Buffer is occupied by Rx FIFO.
  103. *
  104. * @param base FlexCAN peripheral base address.
  105. * @param mbIdx The FlexCAN Message Buffer index.
  106. */
  107. static bool FLEXCAN_IsMbOccupied(CAN_Type *base, uint8_t mbIdx);
  108. #endif
  109. #if ((defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) || \
  110. (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829))
  111. /*!
  112. * @brief Get the first valid Message buffer ID of give FlexCAN instance.
  113. *
  114. * This function is a helper function for Errata 5641 workaround.
  115. *
  116. * @param base FlexCAN peripheral base address.
  117. * @return The first valid Message Buffer Number.
  118. */
  119. static uint8_t FLEXCAN_GetFirstValidMb(CAN_Type *base);
  120. #endif
  121. /*!
  122. * @brief Check if Message Buffer interrupt is enabled.
  123. *
  124. * This function check if Message Buffer interrupt is enabled.
  125. *
  126. * @param base FlexCAN peripheral base address.
  127. * @param mbIdx The FlexCAN Message Buffer index.
  128. * @return TRUE if the index MB interrupt mask enabled, FALSE if the index MB interrupt mask disabled.
  129. *
  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,
  153. uint32_t sourceClock_Hz,
  154. uint32_t baudRate_Bps,
  155. flexcan_timing_config_t timingConfig);
  156. /*!
  157. * @brief Calculates the segment values for a single bit time for classical CAN
  158. *
  159. * @param baudRate The data speed in bps
  160. * @param tqNum Number of time quantas per bit
  161. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  162. *
  163. * @return TRUE if Calculates the segment success, FALSE if Calculates the segment success
  164. */
  165. static bool FLEXCAN_GetSegments(uint32_t baudRate, uint32_t tqNum, flexcan_timing_config_t *pTimingConfig);
  166. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  167. /*!
  168. * @brief Set Baud Rate of FlexCAN FD frame.
  169. *
  170. * This function set the baud rate of FlexCAN FD frame.
  171. *
  172. * @param base FlexCAN peripheral base address.
  173. * @param sourceClock_Hz Source Clock in Hz.
  174. * @param baudRateFD_Bps FD frame Baud Rate in Bps.
  175. * @param timingConfig FlexCAN timingConfig.
  176. */
  177. static void FLEXCAN_SetFDBaudRate(CAN_Type *base,
  178. uint32_t sourceClock_Hz,
  179. uint32_t baudRateFD_Bps,
  180. flexcan_timing_config_t timingConfig);
  181. /*!
  182. * @brief Get Mailbox offset number by dword.
  183. *
  184. * This function gets the offset number of the specified mailbox.
  185. * Mailbox is not consecutive between memory regions when payload is not 8 bytes
  186. * so need to calculate the specified mailbox address.
  187. * For example, in the first memory region, MB[0].CS address is 0x4002_4080. For 32 bytes
  188. * payload frame, the second mailbox is ((1/12)*512 + 1%12*40)/4 = 10, meaning 10 dword
  189. * after the 0x4002_4080, which is actually the address of mailbox MB[1].CS.
  190. *
  191. * @param base FlexCAN peripheral base address.
  192. * @param mbIdx Mailbox index.
  193. */
  194. static uint32_t FLEXCAN_GetFDMailboxOffset(CAN_Type *base, uint8_t mbIdx);
  195. /*!
  196. * @brief Calculates the segment values for a single bit time for CANFD bus control baud Rate
  197. *
  198. * @param baudRate The canfd bus control speed in bps
  199. * @param tqNum Number of time quanta per bit
  200. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  201. *
  202. * @return TRUE if Calculates the segment success, FALSE if Calculates the segment success
  203. */
  204. static bool FLEXCAN_FDGetSegments(uint32_t baudRate, uint32_t tqNum, flexcan_timing_config_t *pTimingConfig);
  205. /*!
  206. * @brief Calculates the segment values for a single bit time for CANFD bus data baud Rate
  207. *
  208. * @param baudRatebrs The canfd bus data speed in bps
  209. * @param tqNum Number of time quanta per bit
  210. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  211. *
  212. * @return TRUE if Calculates the segment success, FALSE if Calculates the segment success
  213. */
  214. static bool FLEXCAN_FDGetSegmentswithBRS(uint32_t baudRatebrs, uint32_t tqNum, flexcan_timing_config_t *pTimingConfig);
  215. /*!
  216. * @brief Calculates the improved timing values by specific baudrates for CAN by CBT register
  217. *
  218. * @param baudRate The classical CAN speed in bps defined by user
  219. * @param sourceClock_Hz The Source clock data speed in bps. Zero to disable baudrate switching
  220. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  221. *
  222. * @return TRUE if timing configuration found, FALSE if failed to find configuration
  223. */
  224. static bool FLEXCAN_CalculateImprovedTimingValuesByCBT(uint32_t baudRate,
  225. uint32_t sourceClock_Hz,
  226. flexcan_timing_config_t *pTimingConfig);
  227. #endif
  228. /*!
  229. * @brief Check unhandle interrupt events
  230. *
  231. * @param base FlexCAN peripheral base address.
  232. * @return TRUE if unhandled interrupt action exist, FALSE if no unhandlered interrupt action exist.
  233. */
  234. static bool FLEXCAN_CheckUnhandleInterruptEvents(CAN_Type *base);
  235. /*!
  236. * @brief Sub Handler Data Trasfered Events
  237. *
  238. * @param base FlexCAN peripheral base address.
  239. * @param handle FlexCAN handle pointer.
  240. * @param pResult Pointer to the Handle result.
  241. * @return the status after handle each data transfered event.
  242. */
  243. static status_t FLEXCAN_SubHandlerForDataTransfered(CAN_Type *base, flexcan_handle_t *handle, uint32_t *pResult);
  244. /*******************************************************************************
  245. * Variables
  246. ******************************************************************************/
  247. /* Array of FlexCAN peripheral base address. */
  248. static CAN_Type *const s_flexcanBases[] = CAN_BASE_PTRS;
  249. /* Array of FlexCAN IRQ number. */
  250. static const IRQn_Type s_flexcanRxWarningIRQ[] = CAN_Rx_Warning_IRQS;
  251. static const IRQn_Type s_flexcanTxWarningIRQ[] = CAN_Tx_Warning_IRQS;
  252. static const IRQn_Type s_flexcanWakeUpIRQ[] = CAN_Wake_Up_IRQS;
  253. static const IRQn_Type s_flexcanErrorIRQ[] = CAN_Error_IRQS;
  254. static const IRQn_Type s_flexcanBusOffIRQ[] = CAN_Bus_Off_IRQS;
  255. static const IRQn_Type s_flexcanMbIRQ[] = CAN_ORed_Message_buffer_IRQS;
  256. /* Array of FlexCAN handle. */
  257. static flexcan_handle_t *s_flexcanHandle[ARRAY_SIZE(s_flexcanBases)];
  258. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  259. /* Array of FlexCAN clock name. */
  260. static const clock_ip_name_t s_flexcanClock[] = FLEXCAN_CLOCKS;
  261. #if defined(FLEXCAN_PERIPH_CLOCKS)
  262. /* Array of FlexCAN serial clock name. */
  263. static const clock_ip_name_t s_flexcanPeriphClock[] = FLEXCAN_PERIPH_CLOCKS;
  264. #endif /* FLEXCAN_PERIPH_CLOCKS */
  265. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  266. /* FlexCAN ISR for transactional APIs. */
  267. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  268. static flexcan_isr_t s_flexcanIsr = (flexcan_isr_t)DefaultISR;
  269. #else
  270. static flexcan_isr_t s_flexcanIsr;
  271. #endif
  272. /*******************************************************************************
  273. * Code
  274. ******************************************************************************/
  275. /*!
  276. * brief Get the FlexCAN instance from peripheral base address.
  277. *
  278. * param base FlexCAN peripheral base address.
  279. * return FlexCAN instance.
  280. */
  281. uint32_t FLEXCAN_GetInstance(CAN_Type *base)
  282. {
  283. uint32_t instance;
  284. /* Find the instance index from base address mappings. */
  285. for (instance = 0; instance < ARRAY_SIZE(s_flexcanBases); instance++)
  286. {
  287. if (s_flexcanBases[instance] == base)
  288. {
  289. break;
  290. }
  291. }
  292. assert(instance < ARRAY_SIZE(s_flexcanBases));
  293. return instance;
  294. }
  295. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_9595) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_9595)
  296. void FLEXCAN_EnterFreezeMode(CAN_Type *base)
  297. {
  298. uint32_t u32TimeoutCount = 0U;
  299. uint32_t u32TempMCR = 0U;
  300. uint32_t u32TempIMASK1 = 0U;
  301. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  302. uint32_t u32TempIMASK2 = 0U;
  303. #endif
  304. /* Step1: set FRZ enable in MCR. */
  305. base->MCR |= CAN_MCR_FRZ_MASK;
  306. /* Step2: to check if MDIS bit set in MCR. if yes, clear it. */
  307. if (0U != (base->MCR & CAN_MCR_MDIS_MASK))
  308. {
  309. base->MCR &= ~CAN_MCR_MDIS_MASK;
  310. }
  311. /* Step3: polling LPMACK. */
  312. u32TimeoutCount = (uint32_t)FLEXCAN_WAIT_TIMEOUT;
  313. while ((0U == (base->MCR & CAN_MCR_LPMACK_MASK)) && (u32TimeoutCount > 0U))
  314. {
  315. u32TimeoutCount--;
  316. }
  317. /* Step4: to check FLTCONF in ESR1 register */
  318. if (0U == (base->ESR1 & CAN_ESR1_FLTCONF_BUSOFF))
  319. {
  320. /* Step5B: Set Halt bits. */
  321. base->MCR |= CAN_MCR_HALT_MASK;
  322. /* Step6B: Poll the MCR register until the Freeze Acknowledge (FRZACK) bit is set, timeout need more than 178
  323. * CAN bit length, so 20 multiply timeout is enough. */
  324. u32TimeoutCount = (uint32_t)FLEXCAN_WAIT_TIMEOUT * 20U;
  325. while ((0U == (base->MCR & CAN_MCR_FRZACK_MASK)) && (u32TimeoutCount > 0U))
  326. {
  327. u32TimeoutCount--;
  328. }
  329. }
  330. else
  331. {
  332. /* backup MCR and IMASK register. Errata document not descript it, but we need backup for step 8A and 9A. */
  333. u32TempMCR = base->MCR;
  334. u32TempIMASK1 = base->IMASK1;
  335. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  336. u32TempIMASK2 = base->IMASK2;
  337. #endif
  338. /* Step5A: Set the Soft Reset bit ((SOFTRST) in the MCR.*/
  339. base->MCR |= CAN_MCR_SOFTRST_MASK;
  340. /* Step6A: Poll the MCR register until the Soft Reset (SOFTRST) bit is cleared. */
  341. u32TimeoutCount = (uint32_t)FLEXCAN_WAIT_TIMEOUT;
  342. while ((CAN_MCR_SOFTRST_MASK == (base->MCR & CAN_MCR_SOFTRST_MASK)) && (u32TimeoutCount > 0U))
  343. {
  344. u32TimeoutCount--;
  345. }
  346. /* Step7A: Poll the MCR register until the Freeze Acknowledge (FRZACK) bit is set. */
  347. u32TimeoutCount = (uint32_t)FLEXCAN_WAIT_TIMEOUT;
  348. while ((0U == (base->MCR & CAN_MCR_FRZACK_MASK)) && (u32TimeoutCount > 0U))
  349. {
  350. u32TimeoutCount--;
  351. }
  352. /* Step8A: reconfig MCR. */
  353. base->MCR = u32TempMCR;
  354. /* Step9A: reconfig IMASK. */
  355. base->IMASK1 = u32TempIMASK1;
  356. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  357. base->IMASK2 = u32TempIMASK2;
  358. #endif
  359. }
  360. }
  361. #elif (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_8341) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_8341)
  362. void FLEXCAN_EnterFreezeMode(CAN_Type *base)
  363. {
  364. uint32_t u32TimeoutCount = 0U;
  365. uint32_t u32TempMCR = 0U;
  366. uint32_t u32TempIMASK1 = 0U;
  367. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  368. uint32_t u32TempIMASK2 = 0U;
  369. #endif
  370. /* Step1: set FRZ and HALT bit enable in MCR. */
  371. base->MCR |= CAN_MCR_FRZ_MASK;
  372. base->MCR |= CAN_MCR_HALT_MASK;
  373. /* Step2: to check if MDIS bit set in MCR. if yes, clear it. */
  374. if (0U != (base->MCR & CAN_MCR_MDIS_MASK))
  375. {
  376. base->MCR &= ~CAN_MCR_MDIS_MASK;
  377. }
  378. /* Step3: Poll the MCR register until the Freeze Acknowledge (FRZACK) bit is set. */
  379. u32TimeoutCount = (uint32_t)FLEXCAN_WAIT_TIMEOUT * 100U;
  380. while ((0U == (base->MCR & CAN_MCR_FRZACK_MASK)) && (u32TimeoutCount > 0U))
  381. {
  382. u32TimeoutCount--;
  383. }
  384. /* Step4: check whether the timeout reached. if no skip step5 to step8. */
  385. if (0U == u32TimeoutCount)
  386. {
  387. /* backup MCR and IMASK register. Errata document not descript it, but we need backup for step 8A and 9A. */
  388. u32TempMCR = base->MCR;
  389. u32TempIMASK1 = base->IMASK1;
  390. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  391. u32TempIMASK2 = base->IMASK2;
  392. #endif
  393. /* Step5: Set the Soft Reset bit ((SOFTRST) in the MCR.*/
  394. base->MCR |= CAN_MCR_SOFTRST_MASK;
  395. /* Step6: Poll the MCR register until the Soft Reset (SOFTRST) bit is cleared. */
  396. while (CAN_MCR_SOFTRST_MASK == (base->MCR & CAN_MCR_SOFTRST_MASK))
  397. {
  398. }
  399. /* Step7: reconfig MCR. */
  400. base->MCR = u32TempMCR;
  401. /* Step8: reconfig IMASK. */
  402. base->IMASK1 = u32TempIMASK1;
  403. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  404. base->IMASK2 = u32TempIMASK2;
  405. #endif
  406. }
  407. }
  408. #else
  409. void FLEXCAN_EnterFreezeMode(CAN_Type *base)
  410. {
  411. /* Set Freeze, Halt bits. */
  412. base->MCR |= CAN_MCR_FRZ_MASK;
  413. base->MCR |= CAN_MCR_HALT_MASK;
  414. while (0U == (base->MCR & CAN_MCR_FRZACK_MASK))
  415. {
  416. }
  417. }
  418. #endif
  419. void FLEXCAN_ExitFreezeMode(CAN_Type *base)
  420. {
  421. #if (defined(FSL_FEATURE_FLEXCAN_HAS_MEMORY_ERROR_CONTROL) && FSL_FEATURE_FLEXCAN_HAS_MEMORY_ERROR_CONTROL)
  422. /* Enable to update in MCER. */
  423. base->CTRL2 |= CAN_CTRL2_ECRWRE_MASK;
  424. base->MECR &= ~CAN_MECR_ECRWRDIS_MASK;
  425. #endif
  426. /* Clear Freeze, Halt bits. */
  427. base->MCR &= ~CAN_MCR_HALT_MASK;
  428. base->MCR &= ~CAN_MCR_FRZ_MASK;
  429. /* Wait until the FlexCAN Module exit freeze mode. */
  430. while (0U != (base->MCR & CAN_MCR_FRZACK_MASK))
  431. {
  432. }
  433. }
  434. #if !defined(NDEBUG)
  435. static bool FLEXCAN_IsMbOccupied(CAN_Type *base, uint8_t mbIdx)
  436. {
  437. uint8_t lastOccupiedMb;
  438. bool fgRet;
  439. /* Is Rx FIFO enabled? */
  440. if (0U != (base->MCR & CAN_MCR_RFEN_MASK))
  441. {
  442. /* Get RFFN value. */
  443. lastOccupiedMb = (uint8_t)((base->CTRL2 & CAN_CTRL2_RFFN_MASK) >> CAN_CTRL2_RFFN_SHIFT);
  444. /* Calculate the number of last Message Buffer occupied by Rx FIFO. */
  445. lastOccupiedMb = ((lastOccupiedMb + 1U) * 2U) + 5U;
  446. #if ((defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) || \
  447. (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829))
  448. /* the first valid MB should be occupied by ERRATA 5461 or 5829. */
  449. lastOccupiedMb += 1U;
  450. #endif
  451. fgRet = (mbIdx <= lastOccupiedMb);
  452. }
  453. else
  454. {
  455. #if ((defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) || \
  456. (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829))
  457. if (0U == mbIdx)
  458. {
  459. fgRet = true;
  460. }
  461. else
  462. #endif
  463. {
  464. fgRet = false;
  465. }
  466. }
  467. return fgRet;
  468. }
  469. #endif
  470. #if ((defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) || \
  471. (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829))
  472. static uint8_t FLEXCAN_GetFirstValidMb(CAN_Type *base)
  473. {
  474. uint8_t firstValidMbNum;
  475. if (0U != (base->MCR & CAN_MCR_RFEN_MASK))
  476. {
  477. firstValidMbNum = (uint8_t)((base->CTRL2 & CAN_CTRL2_RFFN_MASK) >> CAN_CTRL2_RFFN_SHIFT);
  478. firstValidMbNum = ((firstValidMbNum + 1U) * 2U) + 6U;
  479. }
  480. else
  481. {
  482. firstValidMbNum = 0U;
  483. }
  484. return firstValidMbNum;
  485. }
  486. #endif
  487. static bool FLEXCAN_IsMbIntEnabled(CAN_Type *base, uint8_t mbIdx)
  488. {
  489. /* Assertion. */
  490. assert(mbIdx < (uint8_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base));
  491. uint32_t flag = 1U;
  492. bool fgRet = false;
  493. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  494. if (mbIdx >= 32U)
  495. {
  496. fgRet = (0U != (base->IMASK2 & (flag << (mbIdx - 32U))));
  497. }
  498. else
  499. #endif
  500. {
  501. fgRet = (0U != (base->IMASK1 & (flag << mbIdx)));
  502. }
  503. return fgRet;
  504. }
  505. static void FLEXCAN_Reset(CAN_Type *base)
  506. {
  507. /* The module must should be first exit from low power
  508. * mode, and then soft reset can be applied.
  509. */
  510. assert(0U == (base->MCR & CAN_MCR_MDIS_MASK));
  511. uint8_t i;
  512. #if (defined(FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT) && FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT)
  513. if (0 != (FSL_FEATURE_FLEXCAN_INSTANCE_HAS_DOZE_MODE_SUPPORTn(base)))
  514. {
  515. /* De-assert DOZE Enable Bit. */
  516. base->MCR &= ~CAN_MCR_DOZE_MASK;
  517. }
  518. #endif
  519. /* Wait until FlexCAN exit from any Low Power Mode. */
  520. while (0U != (base->MCR & CAN_MCR_LPMACK_MASK))
  521. {
  522. }
  523. /* Assert Soft Reset Signal. */
  524. base->MCR |= CAN_MCR_SOFTRST_MASK;
  525. /* Wait until FlexCAN reset completes. */
  526. while (0U != (base->MCR & CAN_MCR_SOFTRST_MASK))
  527. {
  528. }
  529. /* Reset MCR register. */
  530. #if (defined(FSL_FEATURE_FLEXCAN_HAS_GLITCH_FILTER) && FSL_FEATURE_FLEXCAN_HAS_GLITCH_FILTER)
  531. base->MCR |= CAN_MCR_WRNEN_MASK | CAN_MCR_WAKSRC_MASK |
  532. CAN_MCR_MAXMB((uint32_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base) - 1U);
  533. #else
  534. base->MCR |=
  535. CAN_MCR_WRNEN_MASK | CAN_MCR_MAXMB((uint32_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base) - 1U);
  536. #endif
  537. /* Reset CTRL1 and CTRL2 register. */
  538. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  539. /* SMP bit cannot be asserted when CAN FD is enabled */
  540. if (0 != FSL_FEATURE_FLEXCAN_INSTANCE_HAS_FLEXIBLE_DATA_RATEn(base))
  541. {
  542. base->CTRL1 = 0x0;
  543. }
  544. else
  545. {
  546. base->CTRL1 = CAN_CTRL1_SMP_MASK;
  547. }
  548. #else
  549. base->CTRL1 = CAN_CTRL1_SMP_MASK;
  550. #endif
  551. base->CTRL2 = CAN_CTRL2_TASD(0x16) | CAN_CTRL2_RRS_MASK | CAN_CTRL2_EACEN_MASK;
  552. /* Clean all individual Rx Mask of Message Buffers. */
  553. for (i = 0; i < (uint32_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base); i++)
  554. {
  555. base->RXIMR[i] = 0x3FFFFFFF;
  556. }
  557. /* Clean Global Mask of Message Buffers. */
  558. base->RXMGMASK = 0x3FFFFFFF;
  559. /* Clean Global Mask of Message Buffer 14. */
  560. base->RX14MASK = 0x3FFFFFFF;
  561. /* Clean Global Mask of Message Buffer 15. */
  562. base->RX15MASK = 0x3FFFFFFF;
  563. /* Clean Global Mask of Rx FIFO. */
  564. base->RXFGMASK = 0x3FFFFFFF;
  565. /* Clean all Message Buffer CS fields. */
  566. for (i = 0; i < (uint32_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base); i++)
  567. {
  568. base->MB[i].CS = 0x0;
  569. }
  570. }
  571. static void FLEXCAN_SetBaudRate(CAN_Type *base,
  572. uint32_t sourceClock_Hz,
  573. uint32_t baudRate_Bps,
  574. flexcan_timing_config_t timingConfig)
  575. {
  576. /* FlexCAN timing setting formula:
  577. * quantum = 1 + (PSEG1 + 1) + (PSEG2 + 1) + (PROPSEG + 1);
  578. */
  579. uint32_t quantum = (1U + ((uint32_t)timingConfig.phaseSeg1 + 1U) + ((uint32_t)timingConfig.phaseSeg2 + 1U) +
  580. ((uint32_t)timingConfig.propSeg + 1U));
  581. uint32_t priDiv = baudRate_Bps * quantum;
  582. /* Assertion: Desired baud rate is too high. */
  583. assert(baudRate_Bps <= 1000000U);
  584. /* Assertion: Source clock should greater than baud rate * quantum. */
  585. assert(priDiv <= sourceClock_Hz);
  586. if (0U == priDiv)
  587. {
  588. priDiv = 1;
  589. }
  590. priDiv = (sourceClock_Hz / priDiv) - 1U;
  591. /* Desired baud rate is too low. */
  592. if (priDiv > 0xFFU)
  593. {
  594. priDiv = 0xFF;
  595. }
  596. timingConfig.preDivider = (uint16_t)priDiv;
  597. /* Update actual timing characteristic. */
  598. FLEXCAN_SetTimingConfig(base, (const flexcan_timing_config_t *)(uint32_t)&timingConfig);
  599. }
  600. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  601. static void FLEXCAN_SetFDBaudRate(CAN_Type *base,
  602. uint32_t sourceClock_Hz,
  603. uint32_t baudRateFD_Bps,
  604. flexcan_timing_config_t timingConfig)
  605. {
  606. /* FlexCAN FD timing setting formula:
  607. * quantum = 1 + (FPSEG1 + 1) + (FPSEG2 + 1) + FPROPSEG;
  608. */
  609. uint32_t quantum = (1U + ((uint32_t)timingConfig.fphaseSeg1 + 1U) + ((uint32_t)timingConfig.fphaseSeg2 + 1U) +
  610. (uint32_t)timingConfig.fpropSeg);
  611. uint32_t priDiv = baudRateFD_Bps * quantum;
  612. /* Assertion: Desired baud rate is too high. */
  613. assert(baudRateFD_Bps <= 8000000U);
  614. /* Assertion: Source clock should greater than baud rate * FLEXCAN_TIME_QUANTA_NUM. */
  615. assert(priDiv <= sourceClock_Hz);
  616. if (0U == priDiv)
  617. {
  618. priDiv = 1;
  619. }
  620. priDiv = (sourceClock_Hz / priDiv) - 1U;
  621. /* Desired baud rate is too low. */
  622. if (priDiv > 0xFFU)
  623. {
  624. priDiv = 0xFF;
  625. }
  626. timingConfig.fpreDivider = (uint16_t)priDiv;
  627. /* Update actual timing characteristic. */
  628. FLEXCAN_SetFDTimingConfig(base, (const flexcan_timing_config_t *)(uint32_t)&timingConfig);
  629. }
  630. #endif
  631. /*!
  632. * brief Initializes a FlexCAN instance.
  633. *
  634. * This function initializes the FlexCAN module with user-defined settings.
  635. * This example shows how to set up the flexcan_config_t parameters and how
  636. * to call the FLEXCAN_Init function by passing in these parameters.
  637. * code
  638. * flexcan_config_t flexcanConfig;
  639. * flexcanConfig.clkSrc = kFLEXCAN_ClkSrc0;
  640. * flexcanConfig.baudRate = 1000000U;
  641. * flexcanConfig.maxMbNum = 16;
  642. * flexcanConfig.enableLoopBack = false;
  643. * flexcanConfig.enableSelfWakeup = false;
  644. * flexcanConfig.enableIndividMask = false;
  645. * flexcanConfig.disableSelfReception = false;
  646. * flexcanConfig.enableListenOnlyMode = false;
  647. * flexcanConfig.enableDoze = false;
  648. * flexcanConfig.timingConfig = timingConfig;
  649. * FLEXCAN_Init(CAN0, &flexcanConfig, 8000000UL);
  650. * endcode
  651. *
  652. * param base FlexCAN peripheral base address.
  653. * param pConfig Pointer to the user-defined configuration structure.
  654. * param sourceClock_Hz FlexCAN Protocol Engine clock source frequency in Hz.
  655. */
  656. void FLEXCAN_Init(CAN_Type *base, const flexcan_config_t *pConfig, uint32_t sourceClock_Hz)
  657. {
  658. /* Assertion. */
  659. assert(NULL != pConfig);
  660. assert((pConfig->maxMbNum > 0U) &&
  661. (pConfig->maxMbNum <= (uint8_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base)));
  662. uint32_t mcrTemp;
  663. uint32_t ctrl1Temp;
  664. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  665. uint32_t instance;
  666. #endif
  667. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  668. instance = FLEXCAN_GetInstance(base);
  669. /* Enable FlexCAN clock. */
  670. (void)CLOCK_EnableClock(s_flexcanClock[instance]);
  671. /*
  672. * Check the CAN clock in this device whether affected by Other clock gate
  673. * If it affected, we'd better to change other clock source,
  674. * If user insist on using that clock source, user need open these gate at same time,
  675. * In this scene, User need to care the power consumption.
  676. */
  677. assert(CAN_CLOCK_CHECK_NO_AFFECTS);
  678. #if defined(FLEXCAN_PERIPH_CLOCKS)
  679. /* Enable FlexCAN serial clock. */
  680. (void)CLOCK_EnableClock(s_flexcanPeriphClock[instance]);
  681. #endif /* FLEXCAN_PERIPH_CLOCKS */
  682. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  683. #if defined(CAN_CTRL1_CLKSRC_MASK)
  684. #if (defined(FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE) && FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE)
  685. if (0 == FSL_FEATURE_FLEXCAN_INSTANCE_SUPPORT_ENGINE_CLK_SEL_REMOVEn(base))
  686. #endif /* FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE */
  687. {
  688. /* Disable FlexCAN Module. */
  689. FLEXCAN_Enable(base, false);
  690. /* Protocol-Engine clock source selection, This bit must be set
  691. * when FlexCAN Module in Disable Mode.
  692. */
  693. base->CTRL1 = (kFLEXCAN_ClkSrc0 == pConfig->clkSrc) ? (base->CTRL1 & ~CAN_CTRL1_CLKSRC_MASK) :
  694. (base->CTRL1 | CAN_CTRL1_CLKSRC_MASK);
  695. }
  696. #endif /* CAN_CTRL1_CLKSRC_MASK */
  697. /* Enable FlexCAN Module for configuration. */
  698. FLEXCAN_Enable(base, true);
  699. /* Reset to known status. */
  700. FLEXCAN_Reset(base);
  701. /* Save current CTRL1 value and enable to enter Freeze mode(enabled by default). */
  702. ctrl1Temp = base->CTRL1;
  703. /* Save current MCR value and enable to enter Freeze mode(enabled by default). */
  704. mcrTemp = base->MCR;
  705. /* Enable Loop Back Mode? */
  706. ctrl1Temp = (pConfig->enableLoopBack) ? (ctrl1Temp | CAN_CTRL1_LPB_MASK) : (ctrl1Temp & ~CAN_CTRL1_LPB_MASK);
  707. /* Enable Timer Sync? */
  708. ctrl1Temp = (pConfig->enableTimerSync) ? (ctrl1Temp | CAN_CTRL1_TSYN_MASK) : (ctrl1Temp & ~CAN_CTRL1_TSYN_MASK);
  709. /* Enable Listen Only Mode? */
  710. ctrl1Temp = (pConfig->enableListenOnlyMode) ? ctrl1Temp | CAN_CTRL1_LOM_MASK : ctrl1Temp & ~CAN_CTRL1_LOM_MASK;
  711. /* Set the maximum number of Message Buffers */
  712. mcrTemp = (mcrTemp & ~CAN_MCR_MAXMB_MASK) | CAN_MCR_MAXMB((uint32_t)pConfig->maxMbNum - 1U);
  713. /* Enable Self Wake Up Mode and configure the wake up source. */
  714. mcrTemp = (pConfig->enableSelfWakeup) ? (mcrTemp | CAN_MCR_SLFWAK_MASK) : (mcrTemp & ~CAN_MCR_SLFWAK_MASK);
  715. mcrTemp = (kFLEXCAN_WakeupSrcFiltered == pConfig->wakeupSrc) ? (mcrTemp | CAN_MCR_WAKSRC_MASK) :
  716. (mcrTemp & ~CAN_MCR_WAKSRC_MASK);
  717. /* Enable Individual Rx Masking? */
  718. mcrTemp = (pConfig->enableIndividMask) ? (mcrTemp | CAN_MCR_IRMQ_MASK) : (mcrTemp & ~CAN_MCR_IRMQ_MASK);
  719. /* Disable Self Reception? */
  720. mcrTemp = (pConfig->disableSelfReception) ? mcrTemp | CAN_MCR_SRXDIS_MASK : mcrTemp & ~CAN_MCR_SRXDIS_MASK;
  721. #if (defined(FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT) && FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT)
  722. if (0 != FSL_FEATURE_FLEXCAN_INSTANCE_HAS_DOZE_MODE_SUPPORTn(base))
  723. {
  724. /* Enable Doze Mode? */
  725. mcrTemp = (pConfig->enableDoze) ? (mcrTemp | CAN_MCR_DOZE_MASK) : (mcrTemp & ~CAN_MCR_DOZE_MASK);
  726. }
  727. #endif
  728. /* Write back CTRL1 Configuration to register. */
  729. base->CTRL1 = ctrl1Temp;
  730. #if (defined(FSL_FEATURE_FLEXCAN_HAS_MEMORY_ERROR_CONTROL) && FSL_FEATURE_FLEXCAN_HAS_MEMORY_ERROR_CONTROL)
  731. /* Enable to update in MCER. */
  732. base->CTRL2 |= CAN_CTRL2_ECRWRE_MASK;
  733. base->MECR &= ~CAN_MECR_ECRWRDIS_MASK;
  734. #endif
  735. /* Write back MCR Configuration to register. */
  736. base->MCR = mcrTemp;
  737. /* Baud Rate Configuration.*/
  738. FLEXCAN_SetBaudRate(base, sourceClock_Hz, pConfig->baudRate, pConfig->timingConfig);
  739. }
  740. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  741. /*!
  742. * brief Initializes a FlexCAN instance.
  743. *
  744. * This function initializes the FlexCAN module with user-defined settings.
  745. * This example shows how to set up the flexcan_config_t parameters and how
  746. * to call the FLEXCAN_FDInit function by passing in these parameters.
  747. * code
  748. * flexcan_config_t flexcanConfig;
  749. * flexcanConfig.clkSrc = kFLEXCAN_ClkSrc0;
  750. * flexcanConfig.baudRate = 1000000U;
  751. * flexcanConfig.baudRateFD = 2000000U;
  752. * flexcanConfig.maxMbNum = 16;
  753. * flexcanConfig.enableLoopBack = false;
  754. * flexcanConfig.enableSelfWakeup = false;
  755. * flexcanConfig.enableIndividMask = false;
  756. * flexcanConfig.disableSelfReception = false;
  757. * flexcanConfig.enableListenOnlyMode = false;
  758. * flexcanConfig.enableDoze = false;
  759. * flexcanConfig.timingConfig = timingConfig;
  760. * FLEXCAN_FDInit(CAN0, &flexcanConfig, 8000000UL, kFLEXCAN_16BperMB, false);
  761. * endcode
  762. *
  763. * param base FlexCAN peripheral base address.
  764. * param pConfig Pointer to the user-defined configuration structure.
  765. * param sourceClock_Hz FlexCAN Protocol Engine clock source frequency in Hz.
  766. * param dataSize FlexCAN FD frame payload size.
  767. * param brs If bitrate switch is enabled in FD mode.
  768. */
  769. void FLEXCAN_FDInit(
  770. CAN_Type *base, const flexcan_config_t *pConfig, uint32_t sourceClock_Hz, flexcan_mb_size_t dataSize, bool brs)
  771. {
  772. assert((uint32_t)dataSize <= 3U);
  773. uint32_t fdctrl = 0U;
  774. /* Initialization of classical CAN. */
  775. FLEXCAN_Init(base, pConfig, sourceClock_Hz);
  776. /* Extra bitrate setting for CANFD. */
  777. FLEXCAN_SetFDBaudRate(base, sourceClock_Hz, pConfig->baudRateFD, pConfig->timingConfig);
  778. /* read FDCTRL register. */
  779. fdctrl = base->FDCTRL;
  780. /* Enable FD operation and set bitrate switch. */
  781. if (brs)
  782. {
  783. fdctrl |= CAN_FDCTRL_FDRATE_MASK;
  784. }
  785. else
  786. {
  787. fdctrl &= ~CAN_FDCTRL_FDRATE_MASK;
  788. }
  789. if (brs && !(pConfig->enableLoopBack))
  790. {
  791. /* Before use "|=" operation for multi-bits field, CPU should Clean previous Setting. */
  792. fdctrl = (fdctrl & ~CAN_FDCTRL_TDCOFF_MASK) | CAN_FDCTRL_TDCOFF(0x2U);
  793. }
  794. /* Before use "|=" operation for multi-bits field, CPU should clean previous Setting. */
  795. fdctrl = (fdctrl & ~CAN_FDCTRL_MBDSR0_MASK) | CAN_FDCTRL_MBDSR0(dataSize);
  796. #if defined(CAN_FDCTRL_MBDSR1_MASK)
  797. fdctrl = (fdctrl & ~CAN_FDCTRL_MBDSR1_MASK) | CAN_FDCTRL_MBDSR1(dataSize);
  798. #endif
  799. #if defined(CAN_FDCTRL_MBDSR2_MASK)
  800. fdctrl = (fdctrl & ~CAN_FDCTRL_MBDSR2_MASK) | CAN_FDCTRL_MBDSR2(dataSize);
  801. #endif
  802. #if defined(CAN_FDCTRL_MBDSR3_MASK)
  803. fdctrl = (fdctrl & ~CAN_FDCTRL_MBDSR3_MASK) | CAN_FDCTRL_MBDSR3(dataSize);
  804. #endif
  805. /* Enter Freeze Mode. */
  806. FLEXCAN_EnterFreezeMode(base);
  807. base->MCR |= CAN_MCR_FDEN_MASK;
  808. /* update the FDCTL register. */
  809. base->FDCTRL = fdctrl;
  810. /* Exit Freeze Mode. */
  811. FLEXCAN_ExitFreezeMode(base);
  812. }
  813. #endif
  814. /*!
  815. * brief De-initializes a FlexCAN instance.
  816. *
  817. * This function disables the FlexCAN module clock and sets all register values
  818. * to the reset value.
  819. *
  820. * param base FlexCAN peripheral base address.
  821. */
  822. void FLEXCAN_Deinit(CAN_Type *base)
  823. {
  824. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  825. uint32_t instance;
  826. #endif
  827. /* Reset all Register Contents. */
  828. FLEXCAN_Reset(base);
  829. /* Disable FlexCAN module. */
  830. FLEXCAN_Enable(base, false);
  831. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  832. instance = FLEXCAN_GetInstance(base);
  833. #if defined(FLEXCAN_PERIPH_CLOCKS)
  834. /* Disable FlexCAN serial clock. */
  835. (void)CLOCK_DisableClock(s_flexcanPeriphClock[instance]);
  836. #endif /* FLEXCAN_PERIPH_CLOCKS */
  837. /* Disable FlexCAN clock. */
  838. (void)CLOCK_DisableClock(s_flexcanClock[instance]);
  839. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  840. }
  841. /*!
  842. * brief Gets the default configuration structure.
  843. *
  844. * This function initializes the FlexCAN configuration structure to default values. The default
  845. * values are as follows.
  846. * flexcanConfig->clkSrc = kFLEXCAN_ClkSrc0;
  847. * flexcanConfig->baudRate = 1000000U;
  848. * flexcanConfig->baudRateFD = 2000000U;
  849. * flexcanConfig->maxMbNum = 16;
  850. * flexcanConfig->enableLoopBack = false;
  851. * flexcanConfig->enableSelfWakeup = false;
  852. * flexcanConfig->enableIndividMask = false;
  853. * flexcanConfig->disableSelfReception = false;
  854. * flexcanConfig->enableListenOnlyMode = false;
  855. * flexcanConfig->enableDoze = false;
  856. * flexcanConfig.timingConfig = timingConfig;
  857. *
  858. * param pConfig Pointer to the FlexCAN configuration structure.
  859. */
  860. void FLEXCAN_GetDefaultConfig(flexcan_config_t *pConfig)
  861. {
  862. /* Assertion. */
  863. assert(NULL != pConfig);
  864. /* Initializes the configure structure to zero. */
  865. (void)memset(pConfig, 0, sizeof(*pConfig));
  866. /* Initialize FlexCAN Module config struct with default value. */
  867. pConfig->clkSrc = kFLEXCAN_ClkSrc0;
  868. pConfig->baudRate = 1000000U;
  869. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  870. pConfig->baudRateFD = 2000000U;
  871. #endif
  872. pConfig->maxMbNum = 16;
  873. pConfig->enableLoopBack = false;
  874. pConfig->enableTimerSync = true;
  875. pConfig->enableSelfWakeup = false;
  876. pConfig->wakeupSrc = kFLEXCAN_WakeupSrcUnfiltered;
  877. pConfig->enableIndividMask = false;
  878. pConfig->disableSelfReception = false;
  879. pConfig->enableListenOnlyMode = false;
  880. #if (defined(FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT) && FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT)
  881. pConfig->enableDoze = false;
  882. #endif
  883. /* Default protocol timing configuration, time quantum is 10. */
  884. pConfig->timingConfig.phaseSeg1 = 3;
  885. pConfig->timingConfig.phaseSeg2 = 2;
  886. pConfig->timingConfig.propSeg = 1;
  887. pConfig->timingConfig.rJumpwidth = 1;
  888. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  889. pConfig->timingConfig.fphaseSeg1 = 3;
  890. pConfig->timingConfig.fphaseSeg2 = 3;
  891. pConfig->timingConfig.fpropSeg = 1;
  892. pConfig->timingConfig.frJumpwidth = 1;
  893. #endif
  894. }
  895. /*!
  896. * brief Sets the FlexCAN protocol timing characteristic.
  897. *
  898. * This function gives user settings to CAN bus timing characteristic.
  899. * The function is for an experienced user. For less experienced users, call
  900. * the FLEXCAN_Init() and fill the baud rate field with a desired value.
  901. * This provides the default timing characteristics to the module.
  902. *
  903. * Note that calling FLEXCAN_SetTimingConfig() overrides the baud rate set
  904. * in FLEXCAN_Init().
  905. *
  906. * param base FlexCAN peripheral base address.
  907. * param pConfig Pointer to the timing configuration structure.
  908. */
  909. void FLEXCAN_SetTimingConfig(CAN_Type *base, const flexcan_timing_config_t *pConfig)
  910. {
  911. /* Assertion. */
  912. assert(NULL != pConfig);
  913. /* Enter Freeze Mode. */
  914. FLEXCAN_EnterFreezeMode(base);
  915. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  916. if (0 != FSL_FEATURE_FLEXCAN_INSTANCE_HAS_FLEXIBLE_DATA_RATEn(base))
  917. {
  918. /* Cleaning previous Timing Setting. */
  919. base->CBT &= ~(CAN_CBT_EPRESDIV_MASK | CAN_CBT_ERJW_MASK | CAN_CBT_EPSEG1_MASK | CAN_CBT_EPSEG2_MASK |
  920. CAN_CBT_EPROPSEG_MASK);
  921. /* Updating Timing Setting according to configuration structure. */
  922. base->CBT |= (CAN_CBT_EPRESDIV(pConfig->preDivider) | CAN_CBT_ERJW(pConfig->rJumpwidth) |
  923. CAN_CBT_EPSEG1(pConfig->phaseSeg1) | CAN_CBT_EPSEG2(pConfig->phaseSeg2) |
  924. CAN_CBT_EPROPSEG(pConfig->propSeg));
  925. }
  926. else
  927. {
  928. /* Cleaning previous Timing Setting. */
  929. base->CTRL1 &= ~(CAN_CTRL1_PRESDIV_MASK | CAN_CTRL1_RJW_MASK | CAN_CTRL1_PSEG1_MASK | CAN_CTRL1_PSEG2_MASK |
  930. CAN_CTRL1_PROPSEG_MASK);
  931. /* Updating Timing Setting according to configuration structure. */
  932. base->CTRL1 |= (CAN_CTRL1_PRESDIV(pConfig->preDivider) | CAN_CTRL1_RJW(pConfig->rJumpwidth) |
  933. CAN_CTRL1_PSEG1(pConfig->phaseSeg1) | CAN_CTRL1_PSEG2(pConfig->phaseSeg2) |
  934. CAN_CTRL1_PROPSEG(pConfig->propSeg));
  935. }
  936. #else
  937. /* Cleaning previous Timing Setting. */
  938. base->CTRL1 &= ~(CAN_CTRL1_PRESDIV_MASK | CAN_CTRL1_RJW_MASK | CAN_CTRL1_PSEG1_MASK | CAN_CTRL1_PSEG2_MASK |
  939. CAN_CTRL1_PROPSEG_MASK);
  940. /* Updating Timing Setting according to configuration structure. */
  941. base->CTRL1 |= (CAN_CTRL1_PRESDIV(pConfig->preDivider) | CAN_CTRL1_RJW(pConfig->rJumpwidth) |
  942. CAN_CTRL1_PSEG1(pConfig->phaseSeg1) | CAN_CTRL1_PSEG2(pConfig->phaseSeg2) |
  943. CAN_CTRL1_PROPSEG(pConfig->propSeg));
  944. #endif
  945. /* Exit Freeze Mode. */
  946. FLEXCAN_ExitFreezeMode(base);
  947. }
  948. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  949. /*!
  950. * brief Sets the FlexCAN FD protocol timing characteristic.
  951. *
  952. * This function gives user settings to CAN bus timing characteristic.
  953. * The function is for an experienced user. For less experienced users, call
  954. * the FLEXCAN_Init() and fill the baud rate field with a desired value.
  955. * This provides the default timing characteristics to the module.
  956. *
  957. * Note that calling FLEXCAN_SetFDTimingConfig() overrides the baud rate set
  958. * in FLEXCAN_Init().
  959. *
  960. * param base FlexCAN peripheral base address.
  961. * param pConfig Pointer to the timing configuration structure.
  962. */
  963. void FLEXCAN_SetFDTimingConfig(CAN_Type *base, const flexcan_timing_config_t *pConfig)
  964. {
  965. /* Assertion. */
  966. assert(NULL != pConfig);
  967. /* Enter Freeze Mode. */
  968. FLEXCAN_EnterFreezeMode(base);
  969. base->CBT |= CAN_CBT_BTF(1);
  970. /* Cleaning previous Timing Setting. */
  971. base->FDCBT &= ~(CAN_FDCBT_FPRESDIV_MASK | CAN_FDCBT_FRJW_MASK | CAN_FDCBT_FPSEG1_MASK | CAN_FDCBT_FPSEG2_MASK |
  972. CAN_FDCBT_FPROPSEG_MASK);
  973. /* Updating Timing Setting according to configuration structure. */
  974. base->FDCBT |= (CAN_FDCBT_FPRESDIV(pConfig->fpreDivider) | CAN_FDCBT_FRJW(pConfig->frJumpwidth) |
  975. CAN_FDCBT_FPSEG1(pConfig->fphaseSeg1) | CAN_FDCBT_FPSEG2(pConfig->fphaseSeg2) |
  976. CAN_FDCBT_FPROPSEG(pConfig->fpropSeg));
  977. /* Exit Freeze Mode. */
  978. FLEXCAN_ExitFreezeMode(base);
  979. }
  980. #endif
  981. /*!
  982. * brief Sets the FlexCAN receive message buffer global mask.
  983. *
  984. * This function sets the global mask for the FlexCAN message buffer in a matching process.
  985. * The configuration is only effective when the Rx individual mask is disabled in the FLEXCAN_Init().
  986. *
  987. * param base FlexCAN peripheral base address.
  988. * param mask Rx Message Buffer Global Mask value.
  989. */
  990. void FLEXCAN_SetRxMbGlobalMask(CAN_Type *base, uint32_t mask)
  991. {
  992. /* Enter Freeze Mode. */
  993. FLEXCAN_EnterFreezeMode(base);
  994. /* Setting Rx Message Buffer Global Mask value. */
  995. base->RXMGMASK = mask;
  996. base->RX14MASK = mask;
  997. base->RX15MASK = mask;
  998. /* Exit Freeze Mode. */
  999. FLEXCAN_ExitFreezeMode(base);
  1000. }
  1001. /*!
  1002. * brief Sets the FlexCAN receive FIFO global mask.
  1003. *
  1004. * This function sets the global mask for FlexCAN FIFO in a matching process.
  1005. *
  1006. * param base FlexCAN peripheral base address.
  1007. * param mask Rx Fifo Global Mask value.
  1008. */
  1009. void FLEXCAN_SetRxFifoGlobalMask(CAN_Type *base, uint32_t mask)
  1010. {
  1011. /* Enter Freeze Mode. */
  1012. FLEXCAN_EnterFreezeMode(base);
  1013. /* Setting Rx FIFO Global Mask value. */
  1014. base->RXFGMASK = mask;
  1015. /* Exit Freeze Mode. */
  1016. FLEXCAN_ExitFreezeMode(base);
  1017. }
  1018. /*!
  1019. * brief Sets the FlexCAN receive individual mask.
  1020. *
  1021. * This function sets the individual mask for the FlexCAN matching process.
  1022. * The configuration is only effective when the Rx individual mask is enabled in the FLEXCAN_Init().
  1023. * If the Rx FIFO is disabled, the individual mask is applied to the corresponding Message Buffer.
  1024. * If the Rx FIFO is enabled, the individual mask for Rx FIFO occupied Message Buffer is applied to
  1025. * the Rx Filter with the same index. Note that only the first 32
  1026. * individual masks can be used as the Rx FIFO filter mask.
  1027. *
  1028. * param base FlexCAN peripheral base address.
  1029. * param maskIdx The Index of individual Mask.
  1030. * param mask Rx Individual Mask value.
  1031. */
  1032. void FLEXCAN_SetRxIndividualMask(CAN_Type *base, uint8_t maskIdx, uint32_t mask)
  1033. {
  1034. assert(maskIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1035. /* Enter Freeze Mode. */
  1036. FLEXCAN_EnterFreezeMode(base);
  1037. /* Setting Rx Individual Mask value. */
  1038. base->RXIMR[maskIdx] = mask;
  1039. /* Exit Freeze Mode. */
  1040. FLEXCAN_ExitFreezeMode(base);
  1041. }
  1042. /*!
  1043. * brief Configures a FlexCAN transmit message buffer.
  1044. *
  1045. * This function aborts the previous transmission, cleans the Message Buffer, and
  1046. * configures it as a Transmit Message Buffer.
  1047. *
  1048. * param base FlexCAN peripheral base address.
  1049. * param mbIdx The Message Buffer index.
  1050. * param enable Enable/disable Tx Message Buffer.
  1051. * - true: Enable Tx Message Buffer.
  1052. * - false: Disable Tx Message Buffer.
  1053. */
  1054. void FLEXCAN_SetTxMbConfig(CAN_Type *base, uint8_t mbIdx, bool enable)
  1055. {
  1056. /* Assertion. */
  1057. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1058. #if !defined(NDEBUG)
  1059. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1060. #endif
  1061. /* Inactivate Message Buffer. */
  1062. if (enable)
  1063. {
  1064. base->MB[mbIdx].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1065. }
  1066. else
  1067. {
  1068. base->MB[mbIdx].CS = 0;
  1069. }
  1070. /* Clean Message Buffer content. */
  1071. base->MB[mbIdx].ID = 0x0;
  1072. base->MB[mbIdx].WORD0 = 0x0;
  1073. base->MB[mbIdx].WORD1 = 0x0;
  1074. }
  1075. /*!
  1076. * @brief Calculates the segment values for a single bit time for classical CAN
  1077. *
  1078. * @param baudRate The data speed in bps
  1079. * @param tqNum Number of time quantas per bit
  1080. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  1081. *
  1082. * @return TRUE if Calculates the segment success, FALSE if Calculates the segment success
  1083. */
  1084. static bool FLEXCAN_GetSegments(uint32_t baudRate, uint32_t tqNum, flexcan_timing_config_t *pTimingConfig)
  1085. {
  1086. uint32_t ideal_sp;
  1087. uint32_t p1;
  1088. bool fgRet = false;
  1089. /* Get ideal sample point. For the Bit field in CTRL1 register can't calculate higher ideal SP, we set it as the
  1090. * lowest one(75%).*/
  1091. ideal_sp = IDEAL_SP_LOW;
  1092. /* distribute time quanta. */
  1093. p1 = tqNum * (uint32_t)ideal_sp;
  1094. pTimingConfig->propSeg = (uint8_t)(p1 / (uint32_t)IDEAL_SP_FACTOR - 2U);
  1095. if (pTimingConfig->propSeg <= (MAX_PSEG1 + MAX_PROPSEG))
  1096. {
  1097. if (pTimingConfig->propSeg > MAX_PROPSEG)
  1098. {
  1099. pTimingConfig->phaseSeg1 = pTimingConfig->propSeg - MAX_PROPSEG;
  1100. pTimingConfig->propSeg = MAX_PROPSEG;
  1101. }
  1102. else
  1103. {
  1104. pTimingConfig->phaseSeg1 = 0;
  1105. }
  1106. /* The value of prog Seg should be not larger than tqNum -4U. */
  1107. if ((pTimingConfig->propSeg + pTimingConfig->phaseSeg1) < ((uint8_t)tqNum - 4U))
  1108. {
  1109. pTimingConfig->phaseSeg2 = (uint8_t)tqNum - (pTimingConfig->phaseSeg1 + pTimingConfig->propSeg + 4U);
  1110. if (pTimingConfig->phaseSeg2 <= MAX_PSEG1)
  1111. {
  1112. if ((pTimingConfig->phaseSeg1 < pTimingConfig->phaseSeg2) &&
  1113. (pTimingConfig->propSeg > (pTimingConfig->phaseSeg2 - pTimingConfig->phaseSeg1)))
  1114. {
  1115. pTimingConfig->propSeg -= (pTimingConfig->phaseSeg2 - pTimingConfig->phaseSeg1);
  1116. pTimingConfig->phaseSeg1 = pTimingConfig->phaseSeg2;
  1117. }
  1118. /* subtract one TQ for sync seg. */
  1119. /* sjw is 20% of total TQ, rounded to nearest int. */
  1120. pTimingConfig->rJumpwidth = ((uint8_t)tqNum + 4U) / 5U - 1U;
  1121. /* The max tqNum for CBT will reach to 129, ERJW would not be larger than 26. */
  1122. /* Considering that max ERJW is 31, rJumpwidth will always be smaller than MAX_ERJW. */
  1123. if (pTimingConfig->rJumpwidth > MAX_RJW)
  1124. {
  1125. pTimingConfig->rJumpwidth = MAX_RJW;
  1126. }
  1127. fgRet = true;
  1128. }
  1129. }
  1130. }
  1131. return fgRet;
  1132. }
  1133. /*!
  1134. * @brief Calculates the improved timing values by specific baudrates for classical CAN
  1135. *
  1136. * @param baudRate The classical CAN speed in bps defined by user
  1137. * @param sourceClock_Hz The Source clock data speed in bps. Zero to disable baudrate switching
  1138. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  1139. *
  1140. * @return TRUE if timing configuration found, FALSE if failed to find configuration
  1141. */
  1142. bool FLEXCAN_CalculateImprovedTimingValues(uint32_t baudRate,
  1143. uint32_t sourceClock_Hz,
  1144. flexcan_timing_config_t *pTimingConfig)
  1145. {
  1146. /* observe baud rate maximums. */
  1147. assert(baudRate <= MAX_CAN_BAUDRATE);
  1148. uint32_t clk; /* the clock is tqNumb x baudRateFD. */
  1149. uint32_t tqNum; /* Numbers of TQ. */
  1150. bool fgRet = false;
  1151. /* Auto Improved Protocal timing for CTRL1. */
  1152. tqNum = CTRL1_MAX_TIME_QUANTA;
  1153. do
  1154. {
  1155. clk = baudRate * tqNum;
  1156. if (clk > sourceClock_Hz)
  1157. {
  1158. continue; /* tqNum too large, clk has been exceed sourceClock_Hz. */
  1159. }
  1160. if ((sourceClock_Hz / clk * clk) != sourceClock_Hz)
  1161. {
  1162. continue; /* Non-supporting: the frequency of clock source is not divisible by target baud rate, the user
  1163. should change a divisible baud rate. */
  1164. }
  1165. pTimingConfig->preDivider = (uint16_t)(sourceClock_Hz / clk) - 1U;
  1166. if (pTimingConfig->preDivider > MAX_PRESDIV)
  1167. {
  1168. break; /* The frequency of source clock is too large or the baud rate is too small, the pre-divider could
  1169. not handle it. */
  1170. }
  1171. /* Try to get the best timing configuration. */
  1172. if (FLEXCAN_GetSegments(baudRate, tqNum, pTimingConfig))
  1173. {
  1174. fgRet = true;
  1175. break;
  1176. }
  1177. } while (--tqNum >= CTRL1_MIN_TIME_QUANTA);
  1178. return fgRet;
  1179. }
  1180. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1181. static uint32_t FLEXCAN_GetFDMailboxOffset(CAN_Type *base, uint8_t mbIdx)
  1182. {
  1183. uint32_t offset = 0;
  1184. uint32_t dataSize = (base->FDCTRL & CAN_FDCTRL_MBDSR0_MASK) >> CAN_FDCTRL_MBDSR0_SHIFT;
  1185. switch (dataSize)
  1186. {
  1187. case (uint32_t)kFLEXCAN_8BperMB:
  1188. offset = (((uint32_t)mbIdx / 32U) * 512U + ((uint32_t)mbIdx % 32U) * 16U);
  1189. break;
  1190. case (uint32_t)kFLEXCAN_16BperMB:
  1191. offset = (((uint32_t)mbIdx / 21U) * 512U + ((uint32_t)mbIdx % 21U) * 24U);
  1192. break;
  1193. case (uint32_t)kFLEXCAN_32BperMB:
  1194. offset = (((uint32_t)mbIdx / 12U) * 512U + ((uint32_t)mbIdx % 12U) * 40U);
  1195. break;
  1196. case (uint32_t)kFLEXCAN_64BperMB:
  1197. offset = (((uint32_t)mbIdx / 7U) * 512U + ((uint32_t)mbIdx % 7U) * 72U);
  1198. break;
  1199. default:
  1200. /* All the cases have been listed above, the default clause should not be reached. */
  1201. assert(false);
  1202. break;
  1203. }
  1204. /* To get the dword aligned offset, need to divide by 4. */
  1205. offset = offset / 4U;
  1206. return offset;
  1207. }
  1208. /*!
  1209. * @brief Calculates the segment values for a single bit time for CANFD bus control baud Rate
  1210. *
  1211. * @param baudRate The canfd bus control speed in bps
  1212. * @param tqNum Number of time quanta per bit
  1213. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  1214. *
  1215. * @return TRUE if Calculates the segment success, FALSE if Calculates the segment success
  1216. */
  1217. static bool FLEXCAN_FDGetSegments(uint32_t baudRate, uint32_t tqNum, flexcan_timing_config_t *pTimingConfig)
  1218. {
  1219. uint32_t ideal_sp;
  1220. uint32_t p1;
  1221. bool fgRet = false;
  1222. /* Get ideal sample point. */
  1223. if (baudRate >= 1000000U)
  1224. {
  1225. ideal_sp = IDEAL_SP_LOW;
  1226. }
  1227. else if (baudRate >= 800000U)
  1228. {
  1229. ideal_sp = IDEAL_SP_MID;
  1230. }
  1231. else
  1232. {
  1233. ideal_sp = IDEAL_SP_HIGH;
  1234. }
  1235. /* distribute time quanta. */
  1236. p1 = tqNum * (uint32_t)ideal_sp;
  1237. pTimingConfig->propSeg = (uint8_t)(p1 / (uint32_t)IDEAL_SP_FACTOR - 2U);
  1238. if (pTimingConfig->propSeg <= (MAX_EPSEG1 + MAX_EPROPSEG))
  1239. {
  1240. if (pTimingConfig->propSeg > MAX_EPROPSEG)
  1241. {
  1242. pTimingConfig->phaseSeg1 = pTimingConfig->propSeg - MAX_EPROPSEG;
  1243. pTimingConfig->propSeg = MAX_EPROPSEG;
  1244. }
  1245. else
  1246. {
  1247. pTimingConfig->phaseSeg1 = 0;
  1248. }
  1249. /* The value of prog Seg should be not larger than tqNum -4U. */
  1250. if ((pTimingConfig->propSeg + pTimingConfig->phaseSeg1) < ((uint8_t)tqNum - 4U))
  1251. {
  1252. pTimingConfig->phaseSeg2 = (uint8_t)tqNum - (pTimingConfig->phaseSeg1 + pTimingConfig->propSeg + 4U);
  1253. if (pTimingConfig->phaseSeg2 <= MAX_EPSEG2)
  1254. {
  1255. if ((pTimingConfig->phaseSeg1 < pTimingConfig->phaseSeg2) &&
  1256. (pTimingConfig->propSeg > (pTimingConfig->phaseSeg2 - pTimingConfig->phaseSeg1)))
  1257. {
  1258. pTimingConfig->propSeg -= (pTimingConfig->phaseSeg2 - pTimingConfig->phaseSeg1);
  1259. pTimingConfig->phaseSeg1 = pTimingConfig->phaseSeg2;
  1260. }
  1261. /* subtract one TQ for sync seg. */
  1262. /* sjw is 20% of total TQ, rounded to nearest int. */
  1263. pTimingConfig->rJumpwidth = ((uint8_t)tqNum + 4U) / 5U - 1U;
  1264. /* The max tqNum for CBT will reach to 129, ERJW would not be larger than 26. */
  1265. /* Considering that max ERJW is 31, rJumpwidth will always be smaller than MAX_ERJW. */
  1266. if (pTimingConfig->rJumpwidth > MAX_ERJW)
  1267. {
  1268. pTimingConfig->rJumpwidth = MAX_ERJW;
  1269. }
  1270. fgRet = true;
  1271. }
  1272. }
  1273. }
  1274. return fgRet;
  1275. }
  1276. /*!
  1277. * @brief Calculates the segment values for a single bit time for CANFD bus data baud Rate
  1278. *
  1279. * @param baudRatebrs The canfd bus data speed in bps
  1280. * @param tqNum Number of time quanta per bit
  1281. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  1282. *
  1283. * @return TRUE if Calculates the segment success, FALSE if Calculates the segment success
  1284. */
  1285. static bool FLEXCAN_FDGetSegmentswithBRS(uint32_t baudRatebrs, uint32_t tqNum, flexcan_timing_config_t *pTimingConfig)
  1286. {
  1287. uint32_t ideal_sp;
  1288. uint32_t p1;
  1289. bool fgRet = false;
  1290. /* get ideal sample point. */
  1291. if (baudRatebrs >= 1000000U)
  1292. {
  1293. ideal_sp = IDEAL_SP_LOW;
  1294. }
  1295. else if (baudRatebrs >= 800000U)
  1296. {
  1297. ideal_sp = IDEAL_SP_MID;
  1298. }
  1299. else
  1300. {
  1301. ideal_sp = IDEAL_SP_HIGH;
  1302. }
  1303. /* distribute time quanta. */
  1304. p1 = tqNum * (uint32_t)ideal_sp;
  1305. pTimingConfig->fpropSeg = (uint8_t)(p1 / (uint32_t)IDEAL_SP_FACTOR - 1U);
  1306. if (pTimingConfig->fpropSeg <= (MAX_FPSEG1 + MAX_FPROPSEG))
  1307. {
  1308. if (pTimingConfig->fpropSeg > MAX_FPROPSEG)
  1309. {
  1310. pTimingConfig->fphaseSeg1 = pTimingConfig->fpropSeg - MAX_FPROPSEG;
  1311. pTimingConfig->fpropSeg = MAX_FPROPSEG;
  1312. }
  1313. else
  1314. {
  1315. pTimingConfig->fphaseSeg1 = 0;
  1316. }
  1317. /* The value of prog Seg should be not larger than tqNum -3U. */
  1318. if ((pTimingConfig->fpropSeg + pTimingConfig->fphaseSeg1) < ((uint8_t)tqNum - 3U))
  1319. {
  1320. pTimingConfig->fphaseSeg2 = (uint8_t)tqNum - (pTimingConfig->fphaseSeg1 + pTimingConfig->fpropSeg + 3U);
  1321. if ((pTimingConfig->fphaseSeg1 < pTimingConfig->fphaseSeg2) &&
  1322. (pTimingConfig->fpropSeg > (pTimingConfig->fphaseSeg2 - pTimingConfig->fphaseSeg1)))
  1323. {
  1324. pTimingConfig->fpropSeg -= (pTimingConfig->fphaseSeg2 - pTimingConfig->fphaseSeg1);
  1325. pTimingConfig->fphaseSeg1 = pTimingConfig->fphaseSeg2;
  1326. }
  1327. /* subtract one TQ for sync seg. */
  1328. /* sjw is 20% of total TQ, rounded to nearest int. */
  1329. pTimingConfig->frJumpwidth = ((uint8_t)tqNum + 4U) / 5U - 1U;
  1330. if (pTimingConfig->frJumpwidth > MAX_FRJW)
  1331. {
  1332. pTimingConfig->frJumpwidth = MAX_FRJW;
  1333. }
  1334. fgRet = true;
  1335. }
  1336. }
  1337. return fgRet;
  1338. }
  1339. /*!
  1340. * @brief Calculates the improved timing values by specific baudrates for CAN by CBT register
  1341. *
  1342. * @param baudRate The classical CAN speed in bps defined by user
  1343. * @param sourceClock_Hz The Source clock data speed in bps. Zero to disable baudrate switching
  1344. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  1345. *
  1346. * @return TRUE if timing configuration found, FALSE if failed to find configuration
  1347. */
  1348. static bool FLEXCAN_CalculateImprovedTimingValuesByCBT(uint32_t baudRate,
  1349. uint32_t sourceClock_Hz,
  1350. flexcan_timing_config_t *pTimingConfig)
  1351. {
  1352. /* observe baud rate maximums. */
  1353. assert(baudRate <= MAX_CAN_BAUDRATE);
  1354. uint32_t clk; /* the clock is tqNumb x baudRateFD. */
  1355. uint32_t tqNum; /* Numbers of TQ. */
  1356. bool fgRet = false;
  1357. tqNum = CBT_MAX_TIME_QUANTA;
  1358. /* Auto Improved Protocal timing. */
  1359. do
  1360. {
  1361. clk = baudRate * tqNum;
  1362. if (clk > sourceClock_Hz)
  1363. {
  1364. continue; /* tqNum too large, clk has been exceed sourceClock_Hz. */
  1365. }
  1366. if ((sourceClock_Hz / clk * clk) != sourceClock_Hz)
  1367. {
  1368. continue; /* Non-supporting: the frequency of clock source is not divisible by target baud rate, the user
  1369. should change a divisible baud rate. */
  1370. }
  1371. pTimingConfig->preDivider = (uint16_t)(sourceClock_Hz / clk) - 1U;
  1372. if (pTimingConfig->preDivider > MAX_EPRESDIV)
  1373. {
  1374. break; /* The frequency of source clock is too large or the baud rate is too small, the pre-divider could
  1375. not handle it. */
  1376. }
  1377. /* Try to get the best timing configuration. */
  1378. if (FLEXCAN_FDGetSegments(baudRate, tqNum, pTimingConfig))
  1379. {
  1380. fgRet = true;
  1381. break;
  1382. }
  1383. } while (--tqNum >= CBT_MIN_TIME_QUANTA);
  1384. return fgRet;
  1385. }
  1386. /*!
  1387. * @brief Calculates the improved timing values by specific baudrates for CANFD
  1388. *
  1389. * @param baudRate The CANFD bus control speed in bps defined by user
  1390. * @param baudRateFD The CANFD bus data speed in bps defined by user
  1391. * @param sourceClock_Hz The Source clock data speed in bps. Zero to disable baudrate switching
  1392. * @param pTimingConfig Pointer to the FlexCAN timing configuration structure.
  1393. *
  1394. * @return TRUE if timing configuration found, FALSE if failed to find configuration
  1395. */
  1396. bool FLEXCAN_FDCalculateImprovedTimingValues(uint32_t baudRate,
  1397. uint32_t baudRateFD,
  1398. uint32_t sourceClock_Hz,
  1399. flexcan_timing_config_t *pTimingConfig)
  1400. {
  1401. /* observe baud rate maximums */
  1402. assert(baudRate <= MAX_CAN_BAUDRATE);
  1403. assert(baudRateFD <= MAX_CANFD_BAUDRATE);
  1404. uint32_t clk;
  1405. uint32_t tqNum; /* Numbers of TQ. */
  1406. bool fgRet = false;
  1407. if (FLEXCAN_CalculateImprovedTimingValuesByCBT(baudRate, sourceClock_Hz, pTimingConfig))
  1408. {
  1409. if (0U != baudRateFD)
  1410. {
  1411. /* Auto Improved Protocal timing for CBT. */
  1412. tqNum = FDCBT_MAX_TIME_QUANTA;
  1413. do
  1414. {
  1415. clk = baudRateFD * tqNum;
  1416. if (clk > sourceClock_Hz)
  1417. {
  1418. continue; /* tqNum too large, clk x tqNum has been exceed sourceClock_Hz. */
  1419. }
  1420. if ((sourceClock_Hz / clk * clk) != sourceClock_Hz)
  1421. {
  1422. continue; /* Non-supporting: the frequency of clock source is not divisible by target baud rate,
  1423. the user should change a divisible baud rate. */
  1424. }
  1425. pTimingConfig->fpreDivider = (uint16_t)(sourceClock_Hz / clk - 1U);
  1426. if (pTimingConfig->fpreDivider > MAX_FPRESDIV)
  1427. {
  1428. break; /* The frequency of source clock is too large or the baud rate is too small, the pre-divider
  1429. could not handle it. */
  1430. }
  1431. /* Get the best CANFD data bus timing configuration. */
  1432. if (FLEXCAN_FDGetSegmentswithBRS(baudRateFD, tqNum, pTimingConfig))
  1433. {
  1434. fgRet = true;
  1435. break;
  1436. }
  1437. } while (--tqNum >= FDCBT_MIN_TIME_QUANTA);
  1438. }
  1439. else
  1440. {
  1441. fgRet = true; /* User don't use Brs feature. */
  1442. }
  1443. }
  1444. return fgRet;
  1445. }
  1446. /*!
  1447. * brief Configures a FlexCAN transmit message buffer.
  1448. *
  1449. * This function aborts the previous transmission, cleans the Message Buffer, and
  1450. * configures it as a Transmit Message Buffer.
  1451. *
  1452. * param base FlexCAN peripheral base address.
  1453. * param mbIdx The Message Buffer index.
  1454. * param enable Enable/disable Tx Message Buffer.
  1455. * - true: Enable Tx Message Buffer.
  1456. * - false: Disable Tx Message Buffer.
  1457. */
  1458. void FLEXCAN_SetFDTxMbConfig(CAN_Type *base, uint8_t mbIdx, bool enable)
  1459. {
  1460. /* Assertion. */
  1461. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1462. #if !defined(NDEBUG)
  1463. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1464. #endif
  1465. uint8_t cnt = 0;
  1466. uint8_t payload_dword = 1;
  1467. uint32_t dataSize;
  1468. dataSize = (base->FDCTRL & CAN_FDCTRL_MBDSR0_MASK) >> CAN_FDCTRL_MBDSR0_SHIFT;
  1469. volatile uint32_t *mbAddr = &(base->MB[0].CS);
  1470. uint32_t offset = FLEXCAN_GetFDMailboxOffset(base, mbIdx);
  1471. #if ((defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) || \
  1472. (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829))
  1473. uint32_t availoffset = FLEXCAN_GetFDMailboxOffset(base, FLEXCAN_GetFirstValidMb(base));
  1474. #endif
  1475. /* Inactivate Message Buffer. */
  1476. if (enable)
  1477. {
  1478. /* Inactivate by writing CS. */
  1479. mbAddr[offset] = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1480. }
  1481. else
  1482. {
  1483. mbAddr[offset] = 0x0;
  1484. }
  1485. /* Calculate the DWORD number, dataSize 0/1/2/3 corresponds to 8/16/32/64
  1486. Bytes payload. */
  1487. for (cnt = 0; cnt < (dataSize + 1U); cnt++)
  1488. {
  1489. payload_dword *= 2U;
  1490. }
  1491. /* Clean ID. */
  1492. mbAddr[offset + 1U] = 0x0U;
  1493. /* Clean Message Buffer content, DWORD by DWORD. */
  1494. for (cnt = 0; cnt < payload_dword; cnt++)
  1495. {
  1496. mbAddr[offset + 2U + cnt] = 0x0U;
  1497. }
  1498. #if ((defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) || \
  1499. (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829))
  1500. mbAddr[availoffset] = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1501. #endif
  1502. }
  1503. #endif /* FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE */
  1504. /*!
  1505. * brief Configures a FlexCAN Receive Message Buffer.
  1506. *
  1507. * This function cleans a FlexCAN build-in Message Buffer and configures it
  1508. * as a Receive Message Buffer.
  1509. *
  1510. * param base FlexCAN peripheral base address.
  1511. * param mbIdx The Message Buffer index.
  1512. * param pRxMbConfig Pointer to the FlexCAN Message Buffer configuration structure.
  1513. * param enable Enable/disable Rx Message Buffer.
  1514. * - true: Enable Rx Message Buffer.
  1515. * - false: Disable Rx Message Buffer.
  1516. */
  1517. void FLEXCAN_SetRxMbConfig(CAN_Type *base, uint8_t mbIdx, const flexcan_rx_mb_config_t *pRxMbConfig, bool enable)
  1518. {
  1519. /* Assertion. */
  1520. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1521. assert(((NULL != pRxMbConfig) || (false == enable)));
  1522. #if !defined(NDEBUG)
  1523. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1524. #endif
  1525. uint32_t cs_temp = 0;
  1526. /* Inactivate Message Buffer. */
  1527. base->MB[mbIdx].CS = 0;
  1528. /* Clean Message Buffer content. */
  1529. base->MB[mbIdx].ID = 0x0;
  1530. base->MB[mbIdx].WORD0 = 0x0;
  1531. base->MB[mbIdx].WORD1 = 0x0;
  1532. if (enable)
  1533. {
  1534. /* Setup Message Buffer ID. */
  1535. base->MB[mbIdx].ID = pRxMbConfig->id;
  1536. /* Setup Message Buffer format. */
  1537. if (kFLEXCAN_FrameFormatExtend == pRxMbConfig->format)
  1538. {
  1539. cs_temp |= CAN_CS_IDE_MASK;
  1540. }
  1541. /* Setup Message Buffer type. */
  1542. if (kFLEXCAN_FrameTypeRemote == pRxMbConfig->type)
  1543. {
  1544. cs_temp |= CAN_CS_RTR_MASK;
  1545. }
  1546. /* Activate Rx Message Buffer. */
  1547. cs_temp |= CAN_CS_CODE(kFLEXCAN_RxMbEmpty);
  1548. base->MB[mbIdx].CS = cs_temp;
  1549. }
  1550. }
  1551. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1552. /*!
  1553. * brief Configures a FlexCAN Receive Message Buffer.
  1554. *
  1555. * This function cleans a FlexCAN build-in Message Buffer and configures it
  1556. * as a Receive Message Buffer.
  1557. *
  1558. * param base FlexCAN peripheral base address.
  1559. * param mbIdx The Message Buffer index.
  1560. * param pRxMbConfig Pointer to the FlexCAN Message Buffer configuration structure.
  1561. * param enable Enable/disable Rx Message Buffer.
  1562. * - true: Enable Rx Message Buffer.
  1563. * - false: Disable Rx Message Buffer.
  1564. */
  1565. void FLEXCAN_SetFDRxMbConfig(CAN_Type *base, uint8_t mbIdx, const flexcan_rx_mb_config_t *pRxMbConfig, bool enable)
  1566. {
  1567. /* Assertion. */
  1568. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1569. assert(((NULL != pRxMbConfig) || (false == enable)));
  1570. #if !defined(NDEBUG)
  1571. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1572. #endif
  1573. uint32_t cs_temp = 0;
  1574. uint8_t cnt = 0;
  1575. volatile uint32_t *mbAddr = &(base->MB[0].CS);
  1576. uint32_t offset = FLEXCAN_GetFDMailboxOffset(base, mbIdx);
  1577. uint8_t payload_dword;
  1578. uint32_t dataSize = (base->FDCTRL & CAN_FDCTRL_MBDSR0_MASK) >> CAN_FDCTRL_MBDSR0_SHIFT;
  1579. /* Inactivate Message Buffer. */
  1580. mbAddr[offset] = 0U;
  1581. /* Clean Message Buffer content. */
  1582. mbAddr[offset + 1U] = 0U;
  1583. /* Calculate the DWORD number, dataSize 0/1/2/3 corresponds to 8/16/32/64
  1584. Bytes payload. */
  1585. payload_dword = (2U << dataSize);
  1586. for (cnt = 0; cnt < payload_dword; cnt++)
  1587. {
  1588. mbAddr[offset + 2U + cnt] = 0x0;
  1589. }
  1590. if (enable)
  1591. {
  1592. /* Setup Message Buffer ID. */
  1593. mbAddr[offset + 1U] = pRxMbConfig->id;
  1594. /* Setup Message Buffer format. */
  1595. if (kFLEXCAN_FrameFormatExtend == pRxMbConfig->format)
  1596. {
  1597. cs_temp |= CAN_CS_IDE_MASK;
  1598. }
  1599. /* Setup Message Buffer type. */
  1600. if (kFLEXCAN_FrameTypeRemote == pRxMbConfig->type)
  1601. {
  1602. cs_temp |= CAN_CS_RTR_MASK;
  1603. }
  1604. /* Activate Rx Message Buffer. */
  1605. cs_temp |= CAN_CS_CODE(kFLEXCAN_RxMbEmpty);
  1606. mbAddr[offset] = cs_temp;
  1607. }
  1608. }
  1609. #endif
  1610. /*!
  1611. * brief Configures the FlexCAN Rx FIFO.
  1612. *
  1613. * This function configures the Rx FIFO with given Rx FIFO configuration.
  1614. *
  1615. * param base FlexCAN peripheral base address.
  1616. * param pRxFifoConfig Pointer to the FlexCAN Rx FIFO configuration structure.
  1617. * param enable Enable/disable Rx FIFO.
  1618. * - true: Enable Rx FIFO.
  1619. * - false: Disable Rx FIFO.
  1620. */
  1621. void FLEXCAN_SetRxFifoConfig(CAN_Type *base, const flexcan_rx_fifo_config_t *pRxFifoConfig, bool enable)
  1622. {
  1623. /* Assertion. */
  1624. assert((NULL != pRxFifoConfig) || (false == enable));
  1625. volatile uint32_t *mbAddr;
  1626. uint8_t i, j, k, rffn = 0, numMbOccupy;
  1627. uint32_t setup_mb = 0;
  1628. /* Enter Freeze Mode. */
  1629. FLEXCAN_EnterFreezeMode(base);
  1630. if (enable)
  1631. {
  1632. assert(pRxFifoConfig->idFilterNum <= 128U);
  1633. /* Get the setup_mb value. */
  1634. setup_mb = (uint8_t)((base->MCR & CAN_MCR_MAXMB_MASK) >> CAN_MCR_MAXMB_SHIFT);
  1635. setup_mb = (setup_mb < (uint32_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base)) ?
  1636. setup_mb :
  1637. (uint32_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base);
  1638. /* Determine RFFN value. */
  1639. for (i = 0; i <= 0xFU; i++)
  1640. {
  1641. if ((8U * (i + 1U)) >= pRxFifoConfig->idFilterNum)
  1642. {
  1643. rffn = i;
  1644. assert(((setup_mb - 8U) - (2U * rffn)) > 0U);
  1645. base->CTRL2 = (base->CTRL2 & ~CAN_CTRL2_RFFN_MASK) | CAN_CTRL2_RFFN(rffn);
  1646. break;
  1647. }
  1648. }
  1649. /* caculate the Number of Mailboxes occupied by RX Legacy FIFO and the filter. */
  1650. numMbOccupy = 6U + (rffn + 1U) * 2U;
  1651. /* Copy ID filter table to Message Buffer Region (Fix MISRA_C-2012 Rule 18.1). */
  1652. j = 0U;
  1653. for (i = 6U; i < numMbOccupy; i++)
  1654. {
  1655. /* Get address for current mail box. */
  1656. mbAddr = &(base->MB[i].CS);
  1657. /* One Mail box contain 4U DWORD registers. */
  1658. for (k = 0; k < 4U; k++)
  1659. {
  1660. /* Fill all valid filter in the mail box occupied by filter.
  1661. * Disable unused Rx FIFO Filter, the other rest of register in the last Mail box occupied by fiter set
  1662. * as 0xffffffff.
  1663. */
  1664. mbAddr[k] = (j < pRxFifoConfig->idFilterNum) ? (pRxFifoConfig->idFilterTable[j]) : 0xFFFFFFFFU;
  1665. /* Try to fill next filter in current Mail Box. */
  1666. j++;
  1667. }
  1668. }
  1669. /* Setup ID Fitlter Type. */
  1670. switch (pRxFifoConfig->idFilterType)
  1671. {
  1672. case kFLEXCAN_RxFifoFilterTypeA:
  1673. base->MCR = (base->MCR & ~CAN_MCR_IDAM_MASK) | CAN_MCR_IDAM(0x0);
  1674. break;
  1675. case kFLEXCAN_RxFifoFilterTypeB:
  1676. base->MCR = (base->MCR & ~CAN_MCR_IDAM_MASK) | CAN_MCR_IDAM(0x1);
  1677. break;
  1678. case kFLEXCAN_RxFifoFilterTypeC:
  1679. base->MCR = (base->MCR & ~CAN_MCR_IDAM_MASK) | CAN_MCR_IDAM(0x2);
  1680. break;
  1681. case kFLEXCAN_RxFifoFilterTypeD:
  1682. /* All frames rejected. */
  1683. base->MCR = (base->MCR & ~CAN_MCR_IDAM_MASK) | CAN_MCR_IDAM(0x3);
  1684. break;
  1685. default:
  1686. /* All the cases have been listed above, the default clause should not be reached. */
  1687. assert(false);
  1688. break;
  1689. }
  1690. /* Setting Message Reception Priority. */
  1691. base->CTRL2 = (pRxFifoConfig->priority == kFLEXCAN_RxFifoPrioHigh) ? (base->CTRL2 & ~CAN_CTRL2_MRP_MASK) :
  1692. (base->CTRL2 | CAN_CTRL2_MRP_MASK);
  1693. /* Enable Rx Message FIFO. */
  1694. base->MCR |= CAN_MCR_RFEN_MASK;
  1695. }
  1696. else
  1697. {
  1698. rffn = (uint8_t)((base->CTRL2 & CAN_CTRL2_RFFN_MASK) >> CAN_CTRL2_RFFN_SHIFT);
  1699. /* caculate the Number of Mailboxes occupied by RX Legacy FIFO and the filter. */
  1700. numMbOccupy = 6U + (rffn + 1U) * 2U;
  1701. /* Disable Rx Message FIFO. */
  1702. base->MCR &= ~CAN_MCR_RFEN_MASK;
  1703. /* Clean MB0 ~ MB5 and all MB occupied by ID filters (Fix MISRA_C-2012 Rule 18.1). */
  1704. for (i = 0; i < numMbOccupy; i++)
  1705. {
  1706. FLEXCAN_SetRxMbConfig(base, i, NULL, false);
  1707. }
  1708. }
  1709. /* Exit Freeze Mode. */
  1710. FLEXCAN_ExitFreezeMode(base);
  1711. }
  1712. #if (defined(FSL_FEATURE_FLEXCAN_HAS_RX_FIFO_DMA) && FSL_FEATURE_FLEXCAN_HAS_RX_FIFO_DMA)
  1713. /*!
  1714. * brief Enables or disables the FlexCAN Rx FIFO DMA request.
  1715. *
  1716. * This function enables or disables the DMA feature of FlexCAN build-in Rx FIFO.
  1717. *
  1718. * param base FlexCAN peripheral base address.
  1719. * param enable true to enable, false to disable.
  1720. */
  1721. void FLEXCAN_EnableRxFifoDMA(CAN_Type *base, bool enable)
  1722. {
  1723. if (enable)
  1724. {
  1725. /* Enter Freeze Mode. */
  1726. FLEXCAN_EnterFreezeMode(base);
  1727. /* Enable FlexCAN DMA. */
  1728. base->MCR |= CAN_MCR_DMA_MASK;
  1729. /* Exit Freeze Mode. */
  1730. FLEXCAN_ExitFreezeMode(base);
  1731. }
  1732. else
  1733. {
  1734. /* Enter Freeze Mode. */
  1735. FLEXCAN_EnterFreezeMode(base);
  1736. /* Disable FlexCAN DMA. */
  1737. base->MCR &= ~CAN_MCR_DMA_MASK;
  1738. /* Exit Freeze Mode. */
  1739. FLEXCAN_ExitFreezeMode(base);
  1740. }
  1741. }
  1742. #endif /* FSL_FEATURE_FLEXCAN_HAS_RX_FIFO_DMA */
  1743. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_6032) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_6032)
  1744. /*!
  1745. * FlexCAN: A frame with wrong ID or payload is transmitted into
  1746. * the CAN bus when the Message Buffer under transmission is
  1747. * either aborted or deactivated while the CAN bus is in the Bus Idle state
  1748. *
  1749. * This function to do workaround for ERR006032
  1750. *
  1751. * param base FlexCAN peripheral base address.
  1752. * param mbIdx The FlexCAN Message Buffer index.
  1753. */
  1754. static void FLEXCAN_ERRATA_6032(CAN_Type *base, volatile uint32_t *mbCSAddr)
  1755. {
  1756. uint32_t dbg_temp = 0U;
  1757. uint32_t u32TempCS = 0U;
  1758. uint32_t u32Timeout = DELAY_BUSIDLE;
  1759. uint32_t u32TempIMASK1 = base->IMASK1;
  1760. /*after backup all interruption, disable ALL interruption*/
  1761. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1762. uint32_t u32TempIMASK2 = base->IMASK2;
  1763. base->IMASK2 = 0;
  1764. #endif
  1765. base->IMASK1 = 0;
  1766. dbg_temp = (uint32_t)(base->DBG1);
  1767. switch (dbg_temp & CAN_DBG1_CFSM_MASK)
  1768. {
  1769. case RXINTERMISSION:
  1770. if (CBN_VALUE3 == (dbg_temp & CAN_DBG1_CBN_MASK))
  1771. {
  1772. /*wait until CFSM is different from RXINTERMISSION */
  1773. while (RXINTERMISSION == (base->DBG1 & CAN_DBG1_CFSM_MASK))
  1774. {
  1775. __NOP();
  1776. }
  1777. }
  1778. break;
  1779. case TXINTERMISSION:
  1780. if (CBN_VALUE3 == (dbg_temp & CAN_DBG1_CBN_MASK))
  1781. {
  1782. /*wait until CFSM is different from TXINTERMISSION*/
  1783. while (TXINTERMISSION == (base->DBG1 & CAN_DBG1_CFSM_MASK))
  1784. {
  1785. __NOP();
  1786. }
  1787. }
  1788. break;
  1789. default:
  1790. /* To avoid MISRA-C 2012 rule 16.4 issue. */
  1791. break;
  1792. }
  1793. /*Anyway, BUSIDLE need to delay*/
  1794. if (BUSIDLE == (base->DBG1 & CAN_DBG1_CFSM_MASK))
  1795. {
  1796. while (u32Timeout-- > 0U)
  1797. {
  1798. __NOP();
  1799. }
  1800. /*Write 0x0 into Code field of CS word.*/
  1801. u32TempCS = (uint32_t)(*mbCSAddr);
  1802. u32TempCS &= ~CAN_CS_CODE_MASK;
  1803. *mbCSAddr = u32TempCS;
  1804. }
  1805. /*restore interruption*/
  1806. base->IMASK1 = u32TempIMASK1;
  1807. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  1808. base->IMASK2 = u32TempIMASK2;
  1809. #endif
  1810. }
  1811. #endif
  1812. /*!
  1813. * brief Writes a FlexCAN Message to the Transmit Message Buffer.
  1814. *
  1815. * This function writes a CAN Message to the specified Transmit Message Buffer
  1816. * and changes the Message Buffer state to start CAN Message transmit. After
  1817. * that the function returns immediately.
  1818. *
  1819. * param base FlexCAN peripheral base address.
  1820. * param mbIdx The FlexCAN Message Buffer index.
  1821. * param pTxFrame Pointer to CAN message frame to be sent.
  1822. * retval kStatus_Success - Write Tx Message Buffer Successfully.
  1823. * retval kStatus_Fail - Tx Message Buffer is currently in use.
  1824. */
  1825. status_t FLEXCAN_WriteTxMb(CAN_Type *base, uint8_t mbIdx, const flexcan_frame_t *pTxFrame)
  1826. {
  1827. /* Assertion. */
  1828. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1829. assert(NULL != pTxFrame);
  1830. assert(pTxFrame->length <= 8U);
  1831. #if !defined(NDEBUG)
  1832. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1833. #endif
  1834. uint32_t cs_temp = 0;
  1835. status_t status;
  1836. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_6032) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_6032)
  1837. FLEXCAN_ERRATA_6032(base, &(base->MB[mbIdx].CS));
  1838. #endif
  1839. /* Check if Message Buffer is available. */
  1840. if (CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) != (base->MB[mbIdx].CS & CAN_CS_CODE_MASK))
  1841. {
  1842. /* Inactive Tx Message Buffer. */
  1843. base->MB[mbIdx].CS = (base->MB[mbIdx].CS & ~CAN_CS_CODE_MASK) | CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1844. /* Fill Message ID field. */
  1845. base->MB[mbIdx].ID = pTxFrame->id;
  1846. /* Fill Message Format field. */
  1847. if ((uint32_t)kFLEXCAN_FrameFormatExtend == pTxFrame->format)
  1848. {
  1849. cs_temp |= CAN_CS_SRR_MASK | CAN_CS_IDE_MASK;
  1850. }
  1851. /* Fill Message Type field. */
  1852. if ((uint32_t)kFLEXCAN_FrameTypeRemote == pTxFrame->type)
  1853. {
  1854. cs_temp |= CAN_CS_RTR_MASK;
  1855. }
  1856. cs_temp |= CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) | CAN_CS_DLC(pTxFrame->length);
  1857. /* Load Message Payload. */
  1858. base->MB[mbIdx].WORD0 = pTxFrame->dataWord0;
  1859. base->MB[mbIdx].WORD1 = pTxFrame->dataWord1;
  1860. /* Activate Tx Message Buffer. */
  1861. base->MB[mbIdx].CS = cs_temp;
  1862. #if ((defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) || \
  1863. (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829))
  1864. base->MB[FLEXCAN_GetFirstValidMb(base)].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1865. base->MB[FLEXCAN_GetFirstValidMb(base)].CS = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1866. #endif
  1867. status = kStatus_Success;
  1868. }
  1869. else
  1870. {
  1871. /* Tx Message Buffer is activated, return immediately. */
  1872. status = kStatus_Fail;
  1873. }
  1874. return status;
  1875. }
  1876. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  1877. /*!
  1878. * brief Writes a FlexCAN FD Message to the Transmit Message Buffer.
  1879. *
  1880. * This function writes a CAN FD Message to the specified Transmit Message Buffer
  1881. * and changes the Message Buffer state to start CAN FD Message transmit. After
  1882. * that the function returns immediately.
  1883. *
  1884. * param base FlexCAN peripheral base address.
  1885. * param mbIdx The FlexCAN FD Message Buffer index.
  1886. * param pTxFrame Pointer to CAN FD message frame to be sent.
  1887. * retval kStatus_Success - Write Tx Message Buffer Successfully.
  1888. * retval kStatus_Fail - Tx Message Buffer is currently in use.
  1889. */
  1890. status_t FLEXCAN_WriteFDTxMb(CAN_Type *base, uint8_t mbIdx, const flexcan_fd_frame_t *pTxFrame)
  1891. {
  1892. /* Assertion. */
  1893. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1894. assert(NULL != pTxFrame);
  1895. #if !defined(NDEBUG)
  1896. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1897. #endif
  1898. status_t status;
  1899. uint32_t cs_temp = 0;
  1900. uint8_t cnt = 0;
  1901. uint32_t can_cs = 0;
  1902. uint8_t payload_dword = 1;
  1903. uint32_t dataSize = (base->FDCTRL & CAN_FDCTRL_MBDSR0_MASK) >> CAN_FDCTRL_MBDSR0_SHIFT;
  1904. #if ((defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) || \
  1905. (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829))
  1906. uint32_t availoffset = FLEXCAN_GetFDMailboxOffset(base, FLEXCAN_GetFirstValidMb(base));
  1907. #endif
  1908. volatile uint32_t *mbAddr = &(base->MB[0].CS);
  1909. uint32_t offset = FLEXCAN_GetFDMailboxOffset(base, mbIdx);
  1910. #if (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_6032) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_6032)
  1911. FLEXCAN_ERRATA_6032(base, &(mbAddr[offset]));
  1912. #endif
  1913. can_cs = mbAddr[offset];
  1914. /* Check if Message Buffer is available. */
  1915. if (CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) != (can_cs & CAN_CS_CODE_MASK))
  1916. {
  1917. /* Inactive Tx Message Buffer and Fill Message ID field. */
  1918. mbAddr[offset] = (can_cs & ~CAN_CS_CODE_MASK) | CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1919. mbAddr[offset + 1U] = pTxFrame->id;
  1920. /* Fill Message Format field. */
  1921. if ((uint32_t)kFLEXCAN_FrameFormatExtend == pTxFrame->format)
  1922. {
  1923. cs_temp |= CAN_CS_SRR_MASK | CAN_CS_IDE_MASK;
  1924. }
  1925. /* Fill Message Type field. */
  1926. if ((uint32_t)kFLEXCAN_FrameTypeRemote == pTxFrame->type)
  1927. {
  1928. cs_temp |= CAN_CS_RTR_MASK;
  1929. }
  1930. cs_temp |= CAN_CS_CODE(kFLEXCAN_TxMbDataOrRemote) | CAN_CS_DLC(pTxFrame->length) | CAN_CS_EDL(1) |
  1931. CAN_CS_BRS(pTxFrame->brs);
  1932. /* Calculate the DWORD number, dataSize 0/1/2/3 corresponds to 8/16/32/64
  1933. Bytes payload. */
  1934. for (cnt = 0; cnt < (dataSize + 1U); cnt++)
  1935. {
  1936. payload_dword *= 2U;
  1937. }
  1938. /* Load Message Payload and Activate Tx Message Buffer. */
  1939. for (cnt = 0; cnt < payload_dword; cnt++)
  1940. {
  1941. mbAddr[offset + 2U + cnt] = pTxFrame->dataWord[cnt];
  1942. }
  1943. mbAddr[offset] = cs_temp;
  1944. #if ((defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5641) || \
  1945. (defined(FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829) && FSL_FEATURE_FLEXCAN_HAS_ERRATA_5829))
  1946. mbAddr[availoffset] = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1947. mbAddr[availoffset] = CAN_CS_CODE(kFLEXCAN_TxMbInactive);
  1948. #endif
  1949. status = kStatus_Success;
  1950. }
  1951. else
  1952. {
  1953. /* Tx Message Buffer is activated, return immediately. */
  1954. status = kStatus_Fail;
  1955. }
  1956. return status;
  1957. }
  1958. #endif
  1959. /*!
  1960. * brief Reads a FlexCAN Message from Receive Message Buffer.
  1961. *
  1962. * This function reads a CAN message from a specified Receive Message Buffer.
  1963. * The function fills a receive CAN message frame structure with
  1964. * just received data and activates the Message Buffer again.
  1965. * The function returns immediately.
  1966. *
  1967. * param base FlexCAN peripheral base address.
  1968. * param mbIdx The FlexCAN Message Buffer index.
  1969. * param pRxFrame Pointer to CAN message frame structure for reception.
  1970. * retval kStatus_Success - Rx Message Buffer is full and has been read successfully.
  1971. * retval kStatus_FLEXCAN_RxOverflow - Rx Message Buffer is already overflowed and has been read successfully.
  1972. * retval kStatus_Fail - Rx Message Buffer is empty.
  1973. */
  1974. status_t FLEXCAN_ReadRxMb(CAN_Type *base, uint8_t mbIdx, flexcan_frame_t *pRxFrame)
  1975. {
  1976. /* Assertion. */
  1977. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  1978. assert(NULL != pRxFrame);
  1979. #if !defined(NDEBUG)
  1980. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  1981. #endif
  1982. uint32_t cs_temp;
  1983. uint32_t rx_code;
  1984. status_t status;
  1985. /* Read CS field of Rx Message Buffer to lock Message Buffer. */
  1986. cs_temp = base->MB[mbIdx].CS;
  1987. /* Get Rx Message Buffer Code field. */
  1988. rx_code = (cs_temp & CAN_CS_CODE_MASK) >> CAN_CS_CODE_SHIFT;
  1989. /* Check to see if Rx Message Buffer is full. */
  1990. if (((uint32_t)kFLEXCAN_RxMbFull == rx_code) || ((uint32_t)kFLEXCAN_RxMbOverrun == rx_code))
  1991. {
  1992. /* Store Message ID. */
  1993. pRxFrame->id = base->MB[mbIdx].ID & (CAN_ID_EXT_MASK | CAN_ID_STD_MASK);
  1994. /* Get the message ID and format. */
  1995. pRxFrame->format = (cs_temp & CAN_CS_IDE_MASK) != 0U ? (uint8_t)kFLEXCAN_FrameFormatExtend :
  1996. (uint8_t)kFLEXCAN_FrameFormatStandard;
  1997. /* Get the message type. */
  1998. pRxFrame->type =
  1999. (cs_temp & CAN_CS_RTR_MASK) != 0U ? (uint8_t)kFLEXCAN_FrameTypeRemote : (uint8_t)kFLEXCAN_FrameTypeData;
  2000. /* Get the message length. */
  2001. pRxFrame->length = (uint8_t)((cs_temp & CAN_CS_DLC_MASK) >> CAN_CS_DLC_SHIFT);
  2002. /* Get the time stamp. */
  2003. pRxFrame->timestamp = (uint16_t)((cs_temp & CAN_CS_TIME_STAMP_MASK) >> CAN_CS_TIME_STAMP_SHIFT);
  2004. /* Store Message Payload. */
  2005. pRxFrame->dataWord0 = base->MB[mbIdx].WORD0;
  2006. pRxFrame->dataWord1 = base->MB[mbIdx].WORD1;
  2007. /* Read free-running timer to unlock Rx Message Buffer. */
  2008. (void)base->TIMER;
  2009. if ((uint32_t)kFLEXCAN_RxMbFull == rx_code)
  2010. {
  2011. status = kStatus_Success;
  2012. }
  2013. else
  2014. {
  2015. status = kStatus_FLEXCAN_RxOverflow;
  2016. }
  2017. }
  2018. else
  2019. {
  2020. /* Read free-running timer to unlock Rx Message Buffer. */
  2021. (void)base->TIMER;
  2022. status = kStatus_Fail;
  2023. }
  2024. return status;
  2025. }
  2026. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  2027. /*!
  2028. * brief Reads a FlexCAN FD Message from Receive Message Buffer.
  2029. *
  2030. * This function reads a CAN FD message from a specified Receive Message Buffer.
  2031. * The function fills a receive CAN FD message frame structure with
  2032. * just received data and activates the Message Buffer again.
  2033. * The function returns immediately.
  2034. *
  2035. * param base FlexCAN peripheral base address.
  2036. * param mbIdx The FlexCAN FD Message Buffer index.
  2037. * param pRxFrame Pointer to CAN FD message frame structure for reception.
  2038. * retval kStatus_Success - Rx Message Buffer is full and has been read successfully.
  2039. * retval kStatus_FLEXCAN_RxOverflow - Rx Message Buffer is already overflowed and has been read successfully.
  2040. * retval kStatus_Fail - Rx Message Buffer is empty.
  2041. */
  2042. status_t FLEXCAN_ReadFDRxMb(CAN_Type *base, uint8_t mbIdx, flexcan_fd_frame_t *pRxFrame)
  2043. {
  2044. /* Assertion. */
  2045. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  2046. assert(NULL != pRxFrame);
  2047. #if !defined(NDEBUG)
  2048. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  2049. #endif
  2050. status_t status;
  2051. uint32_t cs_temp;
  2052. uint8_t rx_code;
  2053. uint8_t cnt = 0;
  2054. uint32_t can_id = 0;
  2055. uint32_t dataSize;
  2056. dataSize = (base->FDCTRL & CAN_FDCTRL_MBDSR0_MASK) >> CAN_FDCTRL_MBDSR0_SHIFT;
  2057. uint8_t payload_dword = 1;
  2058. volatile uint32_t *mbAddr = &(base->MB[0].CS);
  2059. uint32_t offset = FLEXCAN_GetFDMailboxOffset(base, mbIdx);
  2060. /* Read CS field of Rx Message Buffer to lock Message Buffer. */
  2061. cs_temp = mbAddr[offset];
  2062. can_id = mbAddr[offset + 1U];
  2063. /* Get Rx Message Buffer Code field. */
  2064. rx_code = (uint8_t)((cs_temp & CAN_CS_CODE_MASK) >> CAN_CS_CODE_SHIFT);
  2065. /* Check to see if Rx Message Buffer is full. */
  2066. if (((uint8_t)kFLEXCAN_RxMbFull == rx_code) || ((uint8_t)kFLEXCAN_RxMbOverrun == rx_code))
  2067. {
  2068. /* Store Message ID. */
  2069. pRxFrame->id = can_id & (CAN_ID_EXT_MASK | CAN_ID_STD_MASK);
  2070. /* Get the message ID and format. */
  2071. pRxFrame->format = (cs_temp & CAN_CS_IDE_MASK) != 0U ? (uint8_t)kFLEXCAN_FrameFormatExtend :
  2072. (uint8_t)kFLEXCAN_FrameFormatStandard;
  2073. /* Get the message type. */
  2074. pRxFrame->type =
  2075. (cs_temp & CAN_CS_RTR_MASK) != 0U ? (uint8_t)kFLEXCAN_FrameTypeRemote : (uint8_t)kFLEXCAN_FrameTypeData;
  2076. /* Get the message length. */
  2077. pRxFrame->length = (uint8_t)((cs_temp & CAN_CS_DLC_MASK) >> CAN_CS_DLC_SHIFT);
  2078. /* Get the time stamp. */
  2079. pRxFrame->timestamp = (uint16_t)((cs_temp & CAN_CS_TIME_STAMP_MASK) >> CAN_CS_TIME_STAMP_SHIFT);
  2080. /* Calculate the DWORD number, dataSize 0/1/2/3 corresponds to 8/16/32/64
  2081. Bytes payload. */
  2082. for (cnt = 0; cnt < (dataSize + 1U); cnt++)
  2083. {
  2084. payload_dword *= 2U;
  2085. }
  2086. /* Store Message Payload. */
  2087. for (cnt = 0; cnt < payload_dword; cnt++)
  2088. {
  2089. pRxFrame->dataWord[cnt] = mbAddr[offset + 2U + cnt];
  2090. }
  2091. /* Read free-running timer to unlock Rx Message Buffer. */
  2092. (void)base->TIMER;
  2093. if ((uint32_t)kFLEXCAN_RxMbFull == rx_code)
  2094. {
  2095. status = kStatus_Success;
  2096. }
  2097. else
  2098. {
  2099. status = kStatus_FLEXCAN_RxOverflow;
  2100. }
  2101. }
  2102. else
  2103. {
  2104. /* Read free-running timer to unlock Rx Message Buffer. */
  2105. (void)base->TIMER;
  2106. status = kStatus_Fail;
  2107. }
  2108. return status;
  2109. }
  2110. #endif
  2111. /*!
  2112. * brief Reads a FlexCAN Message from Rx FIFO.
  2113. *
  2114. * This function reads a CAN message from the FlexCAN build-in Rx FIFO.
  2115. *
  2116. * param base FlexCAN peripheral base address.
  2117. * param pRxFrame Pointer to CAN message frame structure for reception.
  2118. * retval kStatus_Success - Read Message from Rx FIFO successfully.
  2119. * retval kStatus_Fail - Rx FIFO is not enabled.
  2120. */
  2121. status_t FLEXCAN_ReadRxFifo(CAN_Type *base, flexcan_frame_t *pRxFrame)
  2122. {
  2123. /* Assertion. */
  2124. assert(NULL != pRxFrame);
  2125. uint32_t cs_temp;
  2126. status_t status;
  2127. /* Check if Rx FIFO is Enabled. */
  2128. if (0U != (base->MCR & CAN_MCR_RFEN_MASK))
  2129. {
  2130. /* Read CS field of Rx Message Buffer to lock Message Buffer. */
  2131. cs_temp = base->MB[0].CS;
  2132. /* Read data from Rx FIFO output port. */
  2133. /* Store Message ID. */
  2134. pRxFrame->id = base->MB[0].ID & (CAN_ID_EXT_MASK | CAN_ID_STD_MASK);
  2135. /* Get the message ID and format. */
  2136. pRxFrame->format = (cs_temp & CAN_CS_IDE_MASK) != 0U ? (uint8_t)kFLEXCAN_FrameFormatExtend :
  2137. (uint8_t)kFLEXCAN_FrameFormatStandard;
  2138. /* Get the message type. */
  2139. pRxFrame->type =
  2140. (cs_temp & CAN_CS_RTR_MASK) != 0U ? (uint8_t)kFLEXCAN_FrameTypeRemote : (uint8_t)kFLEXCAN_FrameTypeData;
  2141. /* Get the message length. */
  2142. pRxFrame->length = (uint8_t)((cs_temp & CAN_CS_DLC_MASK) >> CAN_CS_DLC_SHIFT);
  2143. /* Get the time stamp. */
  2144. pRxFrame->timestamp = (uint16_t)((cs_temp & CAN_CS_TIME_STAMP_MASK) >> CAN_CS_TIME_STAMP_SHIFT);
  2145. /* Store Message Payload. */
  2146. pRxFrame->dataWord0 = base->MB[0].WORD0;
  2147. pRxFrame->dataWord1 = base->MB[0].WORD1;
  2148. /* Store ID Filter Hit Index. */
  2149. pRxFrame->idhit = (uint16_t)(base->RXFIR & CAN_RXFIR_IDHIT_MASK);
  2150. /* Read free-running timer to unlock Rx Message Buffer. */
  2151. (void)base->TIMER;
  2152. status = kStatus_Success;
  2153. }
  2154. else
  2155. {
  2156. status = kStatus_Fail;
  2157. }
  2158. return status;
  2159. }
  2160. /*!
  2161. * brief Performs a polling send transaction on the CAN bus.
  2162. *
  2163. * Note that a transfer handle does not need to be created before calling this API.
  2164. *
  2165. * param base FlexCAN peripheral base pointer.
  2166. * param mbIdx The FlexCAN Message Buffer index.
  2167. * param pTxFrame Pointer to CAN message frame to be sent.
  2168. * retval kStatus_Success - Write Tx Message Buffer Successfully.
  2169. * retval kStatus_Fail - Tx Message Buffer is currently in use.
  2170. */
  2171. status_t FLEXCAN_TransferSendBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_frame_t *pTxFrame)
  2172. {
  2173. status_t status;
  2174. /* Write Tx Message Buffer to initiate a data sending. */
  2175. if (kStatus_Success == FLEXCAN_WriteTxMb(base, mbIdx, (const flexcan_frame_t *)(uint32_t)pTxFrame))
  2176. {
  2177. /* Wait until CAN Message send out. */
  2178. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2179. uint64_t u64flag = 1;
  2180. while (0U == FLEXCAN_GetMbStatusFlags(base, u64flag << mbIdx))
  2181. #else
  2182. uint32_t u32flag = 1;
  2183. while (0U == FLEXCAN_GetMbStatusFlags(base, u32flag << mbIdx))
  2184. #endif
  2185. {
  2186. }
  2187. /* Clean Tx Message Buffer Flag. */
  2188. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2189. FLEXCAN_ClearMbStatusFlags(base, u64flag << mbIdx);
  2190. #else
  2191. FLEXCAN_ClearMbStatusFlags(base, u32flag << mbIdx);
  2192. #endif
  2193. /*After TX MB tranfered success, update the Timestamp from MB[mbIdx].CS register*/
  2194. pTxFrame->timestamp = (uint16_t)((base->MB[mbIdx].CS & CAN_CS_TIME_STAMP_MASK) >> CAN_CS_TIME_STAMP_SHIFT);
  2195. status = kStatus_Success;
  2196. }
  2197. else
  2198. {
  2199. status = kStatus_Fail;
  2200. }
  2201. return status;
  2202. }
  2203. /*!
  2204. * brief Performs a polling receive transaction on the CAN bus.
  2205. *
  2206. * Note that a transfer handle does not need to be created before calling this API.
  2207. *
  2208. * param base FlexCAN peripheral base pointer.
  2209. * param mbIdx The FlexCAN Message Buffer index.
  2210. * param pRxFrame Pointer to CAN message frame structure for reception.
  2211. * retval kStatus_Success - Rx Message Buffer is full and has been read successfully.
  2212. * retval kStatus_FLEXCAN_RxOverflow - Rx Message Buffer is already overflowed and has been read successfully.
  2213. * retval kStatus_Fail - Rx Message Buffer is empty.
  2214. */
  2215. status_t FLEXCAN_TransferReceiveBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_frame_t *pRxFrame)
  2216. {
  2217. /* Wait until Rx Message Buffer non-empty. */
  2218. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2219. uint64_t u64flag = 1;
  2220. while (0U == FLEXCAN_GetMbStatusFlags(base, u64flag << mbIdx))
  2221. #else
  2222. uint32_t u32flag = 1;
  2223. while (0U == FLEXCAN_GetMbStatusFlags(base, u32flag << mbIdx))
  2224. #endif
  2225. {
  2226. }
  2227. /* Clean Rx Message Buffer Flag. */
  2228. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2229. FLEXCAN_ClearMbStatusFlags(base, u64flag << mbIdx);
  2230. #else
  2231. FLEXCAN_ClearMbStatusFlags(base, u32flag << mbIdx);
  2232. #endif
  2233. /* Read Received CAN Message. */
  2234. return FLEXCAN_ReadRxMb(base, mbIdx, pRxFrame);
  2235. }
  2236. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  2237. /*!
  2238. * brief Performs a polling send transaction on the CAN bus.
  2239. *
  2240. * Note that a transfer handle does not need to be created before calling this API.
  2241. *
  2242. * param base FlexCAN peripheral base pointer.
  2243. * param mbIdx The FlexCAN FD Message Buffer index.
  2244. * param pTxFrame Pointer to CAN FD message frame to be sent.
  2245. * retval kStatus_Success - Write Tx Message Buffer Successfully.
  2246. * retval kStatus_Fail - Tx Message Buffer is currently in use.
  2247. */
  2248. status_t FLEXCAN_TransferFDSendBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_fd_frame_t *pTxFrame)
  2249. {
  2250. status_t status;
  2251. /* Write Tx Message Buffer to initiate a data sending. */
  2252. if (kStatus_Success == FLEXCAN_WriteFDTxMb(base, mbIdx, (const flexcan_fd_frame_t *)(uint32_t)pTxFrame))
  2253. {
  2254. /* Wait until CAN Message send out. */
  2255. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2256. uint64_t u64flag = 1;
  2257. while (0U == FLEXCAN_GetMbStatusFlags(base, u64flag << mbIdx))
  2258. #else
  2259. uint32_t u32flag = 1;
  2260. while (0U == FLEXCAN_GetMbStatusFlags(base, u32flag << mbIdx))
  2261. #endif
  2262. {
  2263. }
  2264. /* Clean Tx Message Buffer Flag. */
  2265. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2266. FLEXCAN_ClearMbStatusFlags(base, u64flag << mbIdx);
  2267. #else
  2268. FLEXCAN_ClearMbStatusFlags(base, u32flag << mbIdx);
  2269. #endif
  2270. /*After TX MB tranfered success, update the Timestamp from base->MB[offset for CANFD].CS register*/
  2271. volatile uint32_t *mbAddr = &(base->MB[0].CS);
  2272. uint32_t offset = FLEXCAN_GetFDMailboxOffset(base, mbIdx);
  2273. pTxFrame->timestamp = (uint16_t)((mbAddr[offset] & CAN_CS_TIME_STAMP_MASK) >> CAN_CS_TIME_STAMP_SHIFT);
  2274. status = kStatus_Success;
  2275. }
  2276. else
  2277. {
  2278. status = kStatus_Fail;
  2279. }
  2280. return status;
  2281. }
  2282. /*!
  2283. * brief Performs a polling receive transaction on the CAN bus.
  2284. *
  2285. * Note that a transfer handle does not need to be created before calling this API.
  2286. *
  2287. * param base FlexCAN peripheral base pointer.
  2288. * param mbIdx The FlexCAN FD Message Buffer index.
  2289. * param pRxFrame Pointer to CAN FD message frame structure for reception.
  2290. * retval kStatus_Success - Rx Message Buffer is full and has been read successfully.
  2291. * retval kStatus_FLEXCAN_RxOverflow - Rx Message Buffer is already overflowed and has been read successfully.
  2292. * retval kStatus_Fail - Rx Message Buffer is empty.
  2293. */
  2294. status_t FLEXCAN_TransferFDReceiveBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_fd_frame_t *pRxFrame)
  2295. {
  2296. /* Wait until Rx Message Buffer non-empty. */
  2297. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2298. uint64_t u64flag = 1;
  2299. while (0U == FLEXCAN_GetMbStatusFlags(base, u64flag << mbIdx))
  2300. #else
  2301. uint32_t u32flag = 1;
  2302. while (0U == FLEXCAN_GetMbStatusFlags(base, u32flag << mbIdx))
  2303. #endif
  2304. {
  2305. }
  2306. /* Clean Rx Message Buffer Flag. */
  2307. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2308. FLEXCAN_ClearMbStatusFlags(base, u64flag << mbIdx);
  2309. #else
  2310. FLEXCAN_ClearMbStatusFlags(base, u32flag << mbIdx);
  2311. #endif
  2312. /* Read Received CAN Message. */
  2313. return FLEXCAN_ReadFDRxMb(base, mbIdx, pRxFrame);
  2314. }
  2315. #endif
  2316. /*!
  2317. * brief Performs a polling receive transaction from Rx FIFO on the CAN bus.
  2318. *
  2319. * Note that a transfer handle does not need to be created before calling this API.
  2320. *
  2321. * param base FlexCAN peripheral base pointer.
  2322. * param pRxFrame Pointer to CAN message frame structure for reception.
  2323. * retval kStatus_Success - Read Message from Rx FIFO successfully.
  2324. * retval kStatus_Fail - Rx FIFO is not enabled.
  2325. */
  2326. status_t FLEXCAN_TransferReceiveFifoBlocking(CAN_Type *base, flexcan_frame_t *pRxFrame)
  2327. {
  2328. status_t rxFifoStatus;
  2329. /* Wait until Rx FIFO non-empty. */
  2330. while (0U == FLEXCAN_GetMbStatusFlags(base, (uint32_t)kFLEXCAN_RxFifoFrameAvlFlag))
  2331. {
  2332. }
  2333. /* */
  2334. rxFifoStatus = FLEXCAN_ReadRxFifo(base, pRxFrame);
  2335. /* Clean Rx Fifo available flag. */
  2336. FLEXCAN_ClearMbStatusFlags(base, (uint32_t)kFLEXCAN_RxFifoFrameAvlFlag);
  2337. return rxFifoStatus;
  2338. }
  2339. /*!
  2340. * brief Initializes the FlexCAN handle.
  2341. *
  2342. * This function initializes the FlexCAN handle, which can be used for other FlexCAN
  2343. * transactional APIs. Usually, for a specified FlexCAN instance,
  2344. * call this API once to get the initialized handle.
  2345. *
  2346. * param base FlexCAN peripheral base address.
  2347. * param handle FlexCAN handle pointer.
  2348. * param callback The callback function.
  2349. * param userData The parameter of the callback function.
  2350. */
  2351. void FLEXCAN_TransferCreateHandle(CAN_Type *base,
  2352. flexcan_handle_t *handle,
  2353. flexcan_transfer_callback_t callback,
  2354. void *userData)
  2355. {
  2356. assert(NULL != handle);
  2357. uint8_t instance;
  2358. /* Clean FlexCAN transfer handle. */
  2359. (void)memset(handle, 0, sizeof(*handle));
  2360. /* Get instance from peripheral base address. */
  2361. instance = (uint8_t)FLEXCAN_GetInstance(base);
  2362. /* Save the context in global variables to support the double weak mechanism. */
  2363. s_flexcanHandle[instance] = handle;
  2364. /* Register Callback function. */
  2365. handle->callback = callback;
  2366. handle->userData = userData;
  2367. s_flexcanIsr = FLEXCAN_TransferHandleIRQ;
  2368. /* We Enable Error & Status interrupt here, because this interrupt just
  2369. * report current status of FlexCAN module through Callback function.
  2370. * It is insignificance without a available callback function.
  2371. */
  2372. if (handle->callback != NULL)
  2373. {
  2374. FLEXCAN_EnableInterrupts(
  2375. base, (uint32_t)kFLEXCAN_BusOffInterruptEnable | (uint32_t)kFLEXCAN_ErrorInterruptEnable |
  2376. (uint32_t)kFLEXCAN_RxWarningInterruptEnable | (uint32_t)kFLEXCAN_TxWarningInterruptEnable |
  2377. (uint32_t)kFLEXCAN_WakeUpInterruptEnable);
  2378. }
  2379. else
  2380. {
  2381. FLEXCAN_DisableInterrupts(
  2382. base, (uint32_t)kFLEXCAN_BusOffInterruptEnable | (uint32_t)kFLEXCAN_ErrorInterruptEnable |
  2383. (uint32_t)kFLEXCAN_RxWarningInterruptEnable | (uint32_t)kFLEXCAN_TxWarningInterruptEnable |
  2384. (uint32_t)kFLEXCAN_WakeUpInterruptEnable);
  2385. }
  2386. /* Enable interrupts in NVIC. */
  2387. (void)EnableIRQ((IRQn_Type)(s_flexcanRxWarningIRQ[instance]));
  2388. (void)EnableIRQ((IRQn_Type)(s_flexcanTxWarningIRQ[instance]));
  2389. (void)EnableIRQ((IRQn_Type)(s_flexcanWakeUpIRQ[instance]));
  2390. (void)EnableIRQ((IRQn_Type)(s_flexcanErrorIRQ[instance]));
  2391. (void)EnableIRQ((IRQn_Type)(s_flexcanBusOffIRQ[instance]));
  2392. (void)EnableIRQ((IRQn_Type)(s_flexcanMbIRQ[instance]));
  2393. }
  2394. /*!
  2395. * brief Sends a message using IRQ.
  2396. *
  2397. * This function sends a message using IRQ. This is a non-blocking function, which returns
  2398. * right away. When messages have been sent out, the send callback function is called.
  2399. *
  2400. * param base FlexCAN peripheral base address.
  2401. * param handle FlexCAN handle pointer.
  2402. * param pMbXfer FlexCAN Message Buffer transfer structure. See the #flexcan_mb_transfer_t.
  2403. * retval kStatus_Success Start Tx Message Buffer sending process successfully.
  2404. * retval kStatus_Fail Write Tx Message Buffer failed.
  2405. * retval kStatus_FLEXCAN_TxBusy Tx Message Buffer is in use.
  2406. */
  2407. status_t FLEXCAN_TransferSendNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *pMbXfer)
  2408. {
  2409. /* Assertion. */
  2410. assert(NULL != handle);
  2411. assert(NULL != pMbXfer);
  2412. assert(pMbXfer->mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  2413. #if !defined(NDEBUG)
  2414. assert(!FLEXCAN_IsMbOccupied(base, pMbXfer->mbIdx));
  2415. #endif
  2416. status_t status;
  2417. /* Check if Message Buffer is idle. */
  2418. if ((uint8_t)kFLEXCAN_StateIdle == handle->mbState[pMbXfer->mbIdx])
  2419. {
  2420. /* Distinguish transmit type. */
  2421. if ((uint32_t)kFLEXCAN_FrameTypeRemote == pMbXfer->frame->type)
  2422. {
  2423. handle->mbState[pMbXfer->mbIdx] = (uint8_t)kFLEXCAN_StateTxRemote;
  2424. }
  2425. else
  2426. {
  2427. handle->mbState[pMbXfer->mbIdx] = (uint8_t)kFLEXCAN_StateTxData;
  2428. }
  2429. if (kStatus_Success ==
  2430. FLEXCAN_WriteTxMb(base, pMbXfer->mbIdx, (const flexcan_frame_t *)(uint32_t)pMbXfer->frame))
  2431. {
  2432. /* Enable Message Buffer Interrupt. */
  2433. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2434. uint64_t u64mask = 1;
  2435. FLEXCAN_EnableMbInterrupts(base, u64mask << pMbXfer->mbIdx);
  2436. #else
  2437. uint32_t u32mask = 1;
  2438. FLEXCAN_EnableMbInterrupts(base, u32mask << pMbXfer->mbIdx);
  2439. #endif
  2440. status = kStatus_Success;
  2441. }
  2442. else
  2443. {
  2444. handle->mbState[pMbXfer->mbIdx] = (uint8_t)kFLEXCAN_StateIdle;
  2445. status = kStatus_Fail;
  2446. }
  2447. }
  2448. else
  2449. {
  2450. status = kStatus_FLEXCAN_TxBusy;
  2451. }
  2452. return status;
  2453. }
  2454. /*!
  2455. * brief Receives a message using IRQ.
  2456. *
  2457. * This function receives a message using IRQ. This is non-blocking function, which returns
  2458. * right away. When the message has been received, the receive callback function is called.
  2459. *
  2460. * param base FlexCAN peripheral base address.
  2461. * param handle FlexCAN handle pointer.
  2462. * param pMbXfer FlexCAN Message Buffer transfer structure. See the #flexcan_mb_transfer_t.
  2463. * retval kStatus_Success - Start Rx Message Buffer receiving process successfully.
  2464. * retval kStatus_FLEXCAN_RxBusy - Rx Message Buffer is in use.
  2465. */
  2466. status_t FLEXCAN_TransferReceiveNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *pMbXfer)
  2467. {
  2468. status_t status;
  2469. /* Assertion. */
  2470. assert(NULL != handle);
  2471. assert(NULL != pMbXfer);
  2472. assert(pMbXfer->mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  2473. #if !defined(NDEBUG)
  2474. assert(!FLEXCAN_IsMbOccupied(base, pMbXfer->mbIdx));
  2475. #endif
  2476. /* Check if Message Buffer is idle. */
  2477. if ((uint8_t)kFLEXCAN_StateIdle == handle->mbState[pMbXfer->mbIdx])
  2478. {
  2479. handle->mbState[pMbXfer->mbIdx] = (uint8_t)kFLEXCAN_StateRxData;
  2480. /* Register Message Buffer. */
  2481. handle->mbFrameBuf[pMbXfer->mbIdx] = pMbXfer->frame;
  2482. /* Enable Message Buffer Interrupt. */
  2483. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2484. uint64_t u64mask = 1;
  2485. FLEXCAN_EnableMbInterrupts(base, u64mask << pMbXfer->mbIdx);
  2486. #else
  2487. uint32_t u32mask = 1;
  2488. FLEXCAN_EnableMbInterrupts(base, u32mask << pMbXfer->mbIdx);
  2489. #endif
  2490. status = kStatus_Success;
  2491. }
  2492. else
  2493. {
  2494. status = kStatus_FLEXCAN_RxBusy;
  2495. }
  2496. return status;
  2497. }
  2498. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  2499. /*!
  2500. * brief Sends a message using IRQ.
  2501. *
  2502. * This function sends a message using IRQ. This is a non-blocking function, which returns
  2503. * right away. When messages have been sent out, the send callback function is called.
  2504. *
  2505. * param base FlexCAN peripheral base address.
  2506. * param handle FlexCAN handle pointer.
  2507. * param pMbXfer FlexCAN FD Message Buffer transfer structure. See the #flexcan_mb_transfer_t.
  2508. * retval kStatus_Success Start Tx Message Buffer sending process successfully.
  2509. * retval kStatus_Fail Write Tx Message Buffer failed.
  2510. * retval kStatus_FLEXCAN_TxBusy Tx Message Buffer is in use.
  2511. */
  2512. status_t FLEXCAN_TransferFDSendNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *pMbXfer)
  2513. {
  2514. /* Assertion. */
  2515. assert(NULL != handle);
  2516. assert(NULL != pMbXfer);
  2517. assert(pMbXfer->mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  2518. #if !defined(NDEBUG)
  2519. assert(!FLEXCAN_IsMbOccupied(base, pMbXfer->mbIdx));
  2520. #endif
  2521. status_t status;
  2522. /* Check if Message Buffer is idle. */
  2523. if ((uint8_t)kFLEXCAN_StateIdle == handle->mbState[pMbXfer->mbIdx])
  2524. {
  2525. /* Distinguish transmit type. */
  2526. if ((uint32_t)kFLEXCAN_FrameTypeRemote == pMbXfer->framefd->type)
  2527. {
  2528. handle->mbState[pMbXfer->mbIdx] = (uint8_t)kFLEXCAN_StateTxRemote;
  2529. }
  2530. else
  2531. {
  2532. handle->mbState[pMbXfer->mbIdx] = (uint8_t)kFLEXCAN_StateTxData;
  2533. }
  2534. if (kStatus_Success ==
  2535. FLEXCAN_WriteFDTxMb(base, pMbXfer->mbIdx, (const flexcan_fd_frame_t *)(uint32_t)pMbXfer->framefd))
  2536. {
  2537. /* Enable Message Buffer Interrupt. */
  2538. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2539. uint64_t u64mask = 1;
  2540. FLEXCAN_EnableMbInterrupts(base, u64mask << pMbXfer->mbIdx);
  2541. #else
  2542. uint32_t u32mask = 1;
  2543. FLEXCAN_EnableMbInterrupts(base, u32mask << pMbXfer->mbIdx);
  2544. #endif
  2545. status = kStatus_Success;
  2546. }
  2547. else
  2548. {
  2549. handle->mbState[pMbXfer->mbIdx] = (uint8_t)kFLEXCAN_StateIdle;
  2550. status = kStatus_Fail;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. status = kStatus_FLEXCAN_TxBusy;
  2556. }
  2557. return status;
  2558. }
  2559. /*!
  2560. * brief Receives a message using IRQ.
  2561. *
  2562. * This function receives a message using IRQ. This is non-blocking function, which returns
  2563. * right away. When the message has been received, the receive callback function is called.
  2564. *
  2565. * param base FlexCAN peripheral base address.
  2566. * param handle FlexCAN handle pointer.
  2567. * param pMbXfer FlexCAN FD Message Buffer transfer structure. See the #flexcan_mb_transfer_t.
  2568. * retval kStatus_Success - Start Rx Message Buffer receiving process successfully.
  2569. * retval kStatus_FLEXCAN_RxBusy - Rx Message Buffer is in use.
  2570. */
  2571. status_t FLEXCAN_TransferFDReceiveNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *pMbXfer)
  2572. {
  2573. /* Assertion. */
  2574. assert(NULL != handle);
  2575. assert(NULL != pMbXfer);
  2576. assert(pMbXfer->mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  2577. #if !defined(NDEBUG)
  2578. assert(!FLEXCAN_IsMbOccupied(base, pMbXfer->mbIdx));
  2579. #endif
  2580. status_t status;
  2581. /* Check if Message Buffer is idle. */
  2582. if ((uint8_t)kFLEXCAN_StateIdle == handle->mbState[pMbXfer->mbIdx])
  2583. {
  2584. handle->mbState[pMbXfer->mbIdx] = (uint8_t)kFLEXCAN_StateRxData;
  2585. /* Register Message Buffer. */
  2586. handle->mbFDFrameBuf[pMbXfer->mbIdx] = pMbXfer->framefd;
  2587. /* Enable Message Buffer Interrupt. */
  2588. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2589. uint64_t u64mask = 1;
  2590. FLEXCAN_EnableMbInterrupts(base, u64mask << pMbXfer->mbIdx);
  2591. #else
  2592. uint32_t u32mask = 1;
  2593. FLEXCAN_EnableMbInterrupts(base, u32mask << pMbXfer->mbIdx);
  2594. #endif
  2595. status = kStatus_Success;
  2596. }
  2597. else
  2598. {
  2599. status = kStatus_FLEXCAN_RxBusy;
  2600. }
  2601. return status;
  2602. }
  2603. #endif
  2604. /*!
  2605. * brief Receives a message from Rx FIFO using IRQ.
  2606. *
  2607. * This function receives a message using IRQ. This is a non-blocking function, which returns
  2608. * right away. When all messages have been received, the receive callback function is called.
  2609. *
  2610. * param base FlexCAN peripheral base address.
  2611. * param handle FlexCAN handle pointer.
  2612. * param pFifoXfer FlexCAN Rx FIFO transfer structure. See the ref flexcan_fifo_transfer_t.
  2613. * retval kStatus_Success - Start Rx FIFO receiving process successfully.
  2614. * retval kStatus_FLEXCAN_RxFifoBusy - Rx FIFO is currently in use.
  2615. */
  2616. status_t FLEXCAN_TransferReceiveFifoNonBlocking(CAN_Type *base,
  2617. flexcan_handle_t *handle,
  2618. flexcan_fifo_transfer_t *pFifoXfer)
  2619. {
  2620. /* Assertion. */
  2621. assert(NULL != handle);
  2622. assert(NULL != pFifoXfer);
  2623. status_t status;
  2624. /* Check if Message Buffer is idle. */
  2625. if ((uint8_t)kFLEXCAN_StateIdle == handle->rxFifoState)
  2626. {
  2627. handle->rxFifoState = (uint8_t)kFLEXCAN_StateRxFifo;
  2628. /* Register Message Buffer. */
  2629. handle->rxFifoFrameBuf = pFifoXfer->frame;
  2630. /* Enable Message Buffer Interrupt. */
  2631. FLEXCAN_EnableMbInterrupts(base, (uint32_t)kFLEXCAN_RxFifoOverflowFlag | (uint32_t)kFLEXCAN_RxFifoWarningFlag |
  2632. (uint32_t)kFLEXCAN_RxFifoFrameAvlFlag);
  2633. status = kStatus_Success;
  2634. }
  2635. else
  2636. {
  2637. status = kStatus_FLEXCAN_RxFifoBusy;
  2638. }
  2639. return status;
  2640. }
  2641. /*!
  2642. * brief Aborts the interrupt driven message send process.
  2643. *
  2644. * This function aborts the interrupt driven message send process.
  2645. *
  2646. * param base FlexCAN peripheral base address.
  2647. * param handle FlexCAN handle pointer.
  2648. * param mbIdx The FlexCAN Message Buffer index.
  2649. */
  2650. void FLEXCAN_TransferAbortSend(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx)
  2651. {
  2652. uint16_t timestamp;
  2653. /* Assertion. */
  2654. assert(NULL != handle);
  2655. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  2656. #if !defined(NDEBUG)
  2657. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  2658. #endif
  2659. /* Disable Message Buffer Interrupt. */
  2660. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2661. uint64_t u64mask = 1;
  2662. FLEXCAN_DisableMbInterrupts(base, u64mask << mbIdx);
  2663. #else
  2664. uint32_t u32mask = 1;
  2665. FLEXCAN_DisableMbInterrupts(base, u32mask << mbIdx);
  2666. #endif
  2667. /* Update the TX frame 's time stamp by MB[mbIdx].cs. */
  2668. timestamp = (uint16_t)((base->MB[mbIdx].CS & CAN_CS_TIME_STAMP_MASK) >> CAN_CS_TIME_STAMP_SHIFT);
  2669. handle->timestamp[mbIdx] = timestamp;
  2670. /* Clean Message Buffer. */
  2671. FLEXCAN_SetTxMbConfig(base, mbIdx, true);
  2672. handle->mbState[mbIdx] = (uint8_t)kFLEXCAN_StateIdle;
  2673. }
  2674. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  2675. /*!
  2676. * brief Aborts the interrupt driven message send process.
  2677. *
  2678. * This function aborts the interrupt driven message send process.
  2679. *
  2680. * param base FlexCAN peripheral base address.
  2681. * param handle FlexCAN handle pointer.
  2682. * param mbIdx The FlexCAN FD Message Buffer index.
  2683. */
  2684. void FLEXCAN_TransferFDAbortSend(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx)
  2685. {
  2686. volatile uint32_t *mbAddr;
  2687. uint32_t offset;
  2688. uint16_t timestamp;
  2689. /* Assertion. */
  2690. assert(NULL != handle);
  2691. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  2692. #if !defined(NDEBUG)
  2693. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  2694. #endif
  2695. /* Disable Message Buffer Interrupt. */
  2696. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2697. uint64_t u64mask = 1;
  2698. FLEXCAN_DisableMbInterrupts(base, u64mask << mbIdx);
  2699. #else
  2700. uint32_t u32mask = 1;
  2701. FLEXCAN_DisableMbInterrupts(base, u32mask << mbIdx);
  2702. #endif
  2703. /* Update the TX frame 's time stamp by base->MB[offset for CANFD].CS. */
  2704. mbAddr = &(base->MB[0].CS);
  2705. offset = FLEXCAN_GetFDMailboxOffset(base, mbIdx);
  2706. timestamp = (uint16_t)((mbAddr[offset] & CAN_CS_TIME_STAMP_MASK) >> CAN_CS_TIME_STAMP_SHIFT);
  2707. handle->timestamp[mbIdx] = timestamp;
  2708. /* Clean Message Buffer. */
  2709. FLEXCAN_SetFDTxMbConfig(base, mbIdx, true);
  2710. handle->mbState[mbIdx] = (uint8_t)kFLEXCAN_StateIdle;
  2711. }
  2712. /*!
  2713. * brief Aborts the interrupt driven message receive process.
  2714. *
  2715. * This function aborts the interrupt driven message receive process.
  2716. *
  2717. * param base FlexCAN peripheral base address.
  2718. * param handle FlexCAN handle pointer.
  2719. * param mbIdx The FlexCAN FD Message Buffer index.
  2720. */
  2721. void FLEXCAN_TransferFDAbortReceive(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx)
  2722. {
  2723. /* Assertion. */
  2724. assert(NULL != handle);
  2725. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  2726. #if !defined(NDEBUG)
  2727. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  2728. #endif
  2729. /* Disable Message Buffer Interrupt. */
  2730. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2731. uint64_t u64mask = 1;
  2732. FLEXCAN_DisableMbInterrupts(base, u64mask << mbIdx);
  2733. #else
  2734. uint32_t u32mask = 1;
  2735. FLEXCAN_DisableMbInterrupts(base, u32mask << mbIdx);
  2736. #endif
  2737. /* Un-register handle. */
  2738. handle->mbFDFrameBuf[mbIdx] = NULL;
  2739. handle->mbState[mbIdx] = (uint8_t)kFLEXCAN_StateIdle;
  2740. }
  2741. #endif
  2742. /*!
  2743. * brief Aborts the interrupt driven message receive process.
  2744. *
  2745. * This function aborts the interrupt driven message receive process.
  2746. *
  2747. * param base FlexCAN peripheral base address.
  2748. * param handle FlexCAN handle pointer.
  2749. * param mbIdx The FlexCAN Message Buffer index.
  2750. */
  2751. void FLEXCAN_TransferAbortReceive(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx)
  2752. {
  2753. /* Assertion. */
  2754. assert(NULL != handle);
  2755. assert(mbIdx <= (base->MCR & CAN_MCR_MAXMB_MASK));
  2756. #if !defined(NDEBUG)
  2757. assert(!FLEXCAN_IsMbOccupied(base, mbIdx));
  2758. #endif
  2759. /* Disable Message Buffer Interrupt. */
  2760. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2761. uint64_t u64mask = 1;
  2762. FLEXCAN_DisableMbInterrupts(base, (u64mask << mbIdx));
  2763. #else
  2764. uint32_t u32mask = 1;
  2765. FLEXCAN_DisableMbInterrupts(base, (u32mask << mbIdx));
  2766. #endif
  2767. /* Un-register handle. */
  2768. handle->mbFrameBuf[mbIdx] = NULL;
  2769. handle->mbState[mbIdx] = (uint8_t)kFLEXCAN_StateIdle;
  2770. }
  2771. /*!
  2772. * brief Aborts the interrupt driven message receive from Rx FIFO process.
  2773. *
  2774. * This function aborts the interrupt driven message receive from Rx FIFO process.
  2775. *
  2776. * param base FlexCAN peripheral base address.
  2777. * param handle FlexCAN handle pointer.
  2778. */
  2779. void FLEXCAN_TransferAbortReceiveFifo(CAN_Type *base, flexcan_handle_t *handle)
  2780. {
  2781. /* Assertion. */
  2782. assert(NULL != handle);
  2783. /* Check if Rx FIFO is enabled. */
  2784. if (0U != (base->MCR & CAN_MCR_RFEN_MASK))
  2785. {
  2786. /* Disable Rx Message FIFO Interrupts. */
  2787. FLEXCAN_DisableMbInterrupts(base, (uint32_t)kFLEXCAN_RxFifoOverflowFlag | (uint32_t)kFLEXCAN_RxFifoWarningFlag |
  2788. (uint32_t)kFLEXCAN_RxFifoFrameAvlFlag);
  2789. /* Un-register handle. */
  2790. handle->rxFifoFrameBuf = NULL;
  2791. }
  2792. handle->rxFifoState = (uint8_t)kFLEXCAN_StateIdle;
  2793. }
  2794. /*!
  2795. * brief Gets the detail index of Mailbox's Timestamp by handle.
  2796. *
  2797. * Then function can only be used when calling non-blocking Data transfer (TX/RX) API,
  2798. * After TX/RX data transfer done (User can get the status by handler's callback function),
  2799. * we can get the detail index of Mailbox's timestamp by handle,
  2800. * Detail non-blocking data transfer API (TX/RX) contain.
  2801. * -FLEXCAN_TransferSendNonBlocking
  2802. * -FLEXCAN_TransferFDSendNonBlocking
  2803. * -FLEXCAN_TransferReceiveNonBlocking
  2804. * -FLEXCAN_TransferFDReceiveNonBlocking
  2805. * -FLEXCAN_TransferReceiveFifoNonBlocking
  2806. *
  2807. * param handle FlexCAN handle pointer.
  2808. * param mbIdx The FlexCAN FD Message Buffer index.
  2809. * return the index of mailbox 's timestamp stored in the handle.
  2810. *
  2811. */
  2812. uint32_t FLEXCAN_GetTimeStamp(flexcan_handle_t *handle, uint8_t mbIdx)
  2813. {
  2814. /* Assertion. */
  2815. assert(NULL != handle);
  2816. return (uint32_t)(handle->timestamp[mbIdx]);
  2817. }
  2818. static bool FLEXCAN_CheckUnhandleInterruptEvents(CAN_Type *base)
  2819. {
  2820. uint64_t tempmask;
  2821. uint64_t tempflag;
  2822. bool fgRet = false;
  2823. /* Checking exist error flag. */
  2824. if (0U == (FLEXCAN_GetStatusFlags(base) &
  2825. ((uint32_t)kFLEXCAN_TxWarningIntFlag | (uint32_t)kFLEXCAN_RxWarningIntFlag |
  2826. (uint32_t)kFLEXCAN_BusOffIntFlag | (uint32_t)kFLEXCAN_ErrorIntFlag | (uint32_t)kFLEXCAN_WakeUpIntFlag)))
  2827. {
  2828. tempmask = (uint64_t)base->IMASK1;
  2829. tempflag = (uint64_t)base->IFLAG1;
  2830. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2831. /* Checking whether exist MB interrupt status and legacy RX FIFO interrupt status. */
  2832. tempmask |= ((uint64_t)base->IMASK2) << 32;
  2833. tempflag |= ((uint64_t)base->IFLAG2) << 32;
  2834. #endif
  2835. fgRet = (0U != (tempmask & tempflag));
  2836. }
  2837. else
  2838. {
  2839. fgRet = true;
  2840. }
  2841. return fgRet;
  2842. }
  2843. static status_t FLEXCAN_SubHandlerForDataTransfered(CAN_Type *base, flexcan_handle_t *handle, uint32_t *pResult)
  2844. {
  2845. status_t status = kStatus_FLEXCAN_UnHandled;
  2846. uint32_t result = 0xFFU;
  2847. /* For this implementation, we solve the Message with lowest MB index first. */
  2848. for (result = 0U; result < (uint32_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base); result++)
  2849. {
  2850. /* Get the lowest unhandled Message Buffer */
  2851. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2852. uint64_t u64flag = 1;
  2853. if (0U != FLEXCAN_GetMbStatusFlags(base, u64flag << result))
  2854. #else
  2855. uint32_t u32flag = 1;
  2856. if (0U != FLEXCAN_GetMbStatusFlags(base, u32flag << result))
  2857. #endif
  2858. {
  2859. if (FLEXCAN_IsMbIntEnabled(base, (uint8_t)result))
  2860. {
  2861. break;
  2862. }
  2863. }
  2864. }
  2865. /* find Message to deal with. */
  2866. if (result < (uint32_t)FSL_FEATURE_FLEXCAN_HAS_MESSAGE_BUFFER_MAX_NUMBERn(base))
  2867. {
  2868. /* Solve Legacy Rx FIFO interrupt. */
  2869. if (((uint8_t)kFLEXCAN_StateIdle != handle->rxFifoState) && (result <= (uint32_t)CAN_IFLAG1_BUF7I_SHIFT))
  2870. {
  2871. uint32_t u32mask = 1;
  2872. switch (u32mask << result)
  2873. {
  2874. case kFLEXCAN_RxFifoOverflowFlag:
  2875. status = kStatus_FLEXCAN_RxFifoOverflow;
  2876. break;
  2877. case kFLEXCAN_RxFifoWarningFlag:
  2878. status = kStatus_FLEXCAN_RxFifoWarning;
  2879. break;
  2880. case kFLEXCAN_RxFifoFrameAvlFlag:
  2881. status = FLEXCAN_ReadRxFifo(base, handle->rxFifoFrameBuf);
  2882. if (kStatus_Success == status)
  2883. {
  2884. /* Align the current (index 0) rxfifo timestamp to the timestamp array by handle. */
  2885. handle->timestamp[0] = handle->rxFifoFrameBuf->timestamp;
  2886. status = kStatus_FLEXCAN_RxFifoIdle;
  2887. }
  2888. FLEXCAN_TransferAbortReceiveFifo(base, handle);
  2889. break;
  2890. default:
  2891. status = kStatus_FLEXCAN_UnHandled;
  2892. break;
  2893. }
  2894. }
  2895. else
  2896. {
  2897. /* Get current State of Message Buffer. */
  2898. switch (handle->mbState[result])
  2899. {
  2900. /* Solve Rx Data Frame. */
  2901. case (uint8_t)kFLEXCAN_StateRxData:
  2902. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  2903. if (0U != (base->MCR & CAN_MCR_FDEN_MASK))
  2904. {
  2905. status = FLEXCAN_ReadFDRxMb(base, (uint8_t)result, handle->mbFDFrameBuf[result]);
  2906. if (kStatus_Success == status)
  2907. {
  2908. /* Align the current index of RX MB timestamp to the timestamp array by handle. */
  2909. handle->timestamp[result] = handle->mbFDFrameBuf[result]->timestamp;
  2910. status = kStatus_FLEXCAN_RxIdle;
  2911. }
  2912. }
  2913. else
  2914. #endif
  2915. {
  2916. status = FLEXCAN_ReadRxMb(base, (uint8_t)result, handle->mbFrameBuf[result]);
  2917. if (kStatus_Success == status)
  2918. {
  2919. /* Align the current index of RX MB timestamp to the timestamp array by handle. */
  2920. handle->timestamp[result] = handle->mbFrameBuf[result]->timestamp;
  2921. status = kStatus_FLEXCAN_RxIdle;
  2922. }
  2923. }
  2924. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  2925. if (0U != (base->MCR & CAN_MCR_FDEN_MASK))
  2926. {
  2927. FLEXCAN_TransferFDAbortReceive(base, handle, (uint8_t)result);
  2928. }
  2929. else
  2930. #endif
  2931. {
  2932. FLEXCAN_TransferAbortReceive(base, handle, (uint8_t)result);
  2933. }
  2934. break;
  2935. /* Sove Rx Remote Frame. User need to Read the frame in Mail box in time by Read from MB API. */
  2936. case (uint8_t)kFLEXCAN_StateRxRemote:
  2937. status = kStatus_FLEXCAN_RxRemote;
  2938. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  2939. if (0U != (base->MCR & CAN_MCR_FDEN_MASK))
  2940. {
  2941. FLEXCAN_TransferFDAbortReceive(base, handle, (uint8_t)result);
  2942. }
  2943. else
  2944. #endif
  2945. {
  2946. FLEXCAN_TransferAbortReceive(base, handle, (uint8_t)result);
  2947. }
  2948. break;
  2949. /* Solve Tx Data Frame. */
  2950. case (uint8_t)kFLEXCAN_StateTxData:
  2951. status = kStatus_FLEXCAN_TxIdle;
  2952. #if (defined(FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE) && FSL_FEATURE_FLEXCAN_HAS_FLEXIBLE_DATA_RATE)
  2953. if (0U != (base->MCR & CAN_MCR_FDEN_MASK))
  2954. {
  2955. FLEXCAN_TransferFDAbortSend(base, handle, (uint8_t)result);
  2956. }
  2957. else
  2958. #endif
  2959. {
  2960. FLEXCAN_TransferAbortSend(base, handle, (uint8_t)result);
  2961. }
  2962. break;
  2963. /* Solve Tx Remote Frame. */
  2964. case (uint8_t)kFLEXCAN_StateTxRemote:
  2965. handle->mbState[result] = (uint8_t)kFLEXCAN_StateRxRemote;
  2966. status = kStatus_FLEXCAN_TxSwitchToRx;
  2967. break;
  2968. default:
  2969. status = kStatus_FLEXCAN_UnHandled;
  2970. break;
  2971. }
  2972. }
  2973. /* Clear resolved Message Buffer IRQ. */
  2974. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  2975. uint64_t u64flag = 1;
  2976. FLEXCAN_ClearMbStatusFlags(base, u64flag << result);
  2977. #else
  2978. uint32_t u32flag = 1;
  2979. FLEXCAN_ClearMbStatusFlags(base, u32flag << result);
  2980. #endif
  2981. }
  2982. *pResult = result;
  2983. return status;
  2984. }
  2985. /*!
  2986. * brief FlexCAN IRQ handle function.
  2987. *
  2988. * This function handles the FlexCAN Error, the Message Buffer, and the Rx FIFO IRQ request.
  2989. *
  2990. * param base FlexCAN peripheral base address.
  2991. * param handle FlexCAN handle pointer.
  2992. */
  2993. void FLEXCAN_TransferHandleIRQ(CAN_Type *base, flexcan_handle_t *handle)
  2994. {
  2995. /* Assertion. */
  2996. assert(NULL != handle);
  2997. status_t status;
  2998. uint32_t result = 0xFFU;
  2999. uint32_t EsrStatus = 0U;
  3000. do
  3001. {
  3002. /* Get Current FlexCAN Module Error and Status. */
  3003. EsrStatus = FLEXCAN_GetStatusFlags(base);
  3004. /* To handle FlexCAN Error and Status Interrupt first. */
  3005. if (0U != (EsrStatus & ((uint32_t)kFLEXCAN_TxWarningIntFlag | (uint32_t)kFLEXCAN_RxWarningIntFlag |
  3006. (uint32_t)kFLEXCAN_BusOffIntFlag | (uint32_t)kFLEXCAN_ErrorIntFlag)))
  3007. {
  3008. status = kStatus_FLEXCAN_ErrorStatus;
  3009. /* Clear FlexCAN Error and Status Interrupt. */
  3010. FLEXCAN_ClearStatusFlags(base, (uint32_t)kFLEXCAN_TxWarningIntFlag | (uint32_t)kFLEXCAN_RxWarningIntFlag |
  3011. (uint32_t)kFLEXCAN_BusOffIntFlag | (uint32_t)kFLEXCAN_ErrorIntFlag);
  3012. result = EsrStatus;
  3013. }
  3014. else if (0U != (EsrStatus & (uint32_t)kFLEXCAN_WakeUpIntFlag))
  3015. {
  3016. status = kStatus_FLEXCAN_WakeUp;
  3017. FLEXCAN_ClearStatusFlags(base, (uint32_t)kFLEXCAN_WakeUpIntFlag);
  3018. }
  3019. else
  3020. {
  3021. /* to handle real data transfer. */
  3022. status = FLEXCAN_SubHandlerForDataTransfered(base, handle, &result);
  3023. }
  3024. /* Calling Callback Function if has one. */
  3025. if (handle->callback != NULL)
  3026. {
  3027. handle->callback(base, handle, status, result, handle->userData);
  3028. }
  3029. } while (FLEXCAN_CheckUnhandleInterruptEvents(base));
  3030. }
  3031. #if defined(CAN0)
  3032. void CAN0_DriverIRQHandler(void)
  3033. {
  3034. assert(NULL != s_flexcanHandle[0]);
  3035. s_flexcanIsr(CAN0, s_flexcanHandle[0]);
  3036. SDK_ISR_EXIT_BARRIER;
  3037. }
  3038. #endif
  3039. #if defined(CAN1)
  3040. void CAN1_DriverIRQHandler(void)
  3041. {
  3042. assert(NULL != s_flexcanHandle[1]);
  3043. s_flexcanIsr(CAN1, s_flexcanHandle[1]);
  3044. SDK_ISR_EXIT_BARRIER;
  3045. }
  3046. #endif
  3047. #if defined(CAN2)
  3048. void CAN2_DriverIRQHandler(void)
  3049. {
  3050. assert(NULL != s_flexcanHandle[2]);
  3051. s_flexcanIsr(CAN2, s_flexcanHandle[2]);
  3052. SDK_ISR_EXIT_BARRIER;
  3053. }
  3054. #endif
  3055. #if defined(CAN3)
  3056. void CAN3_DriverIRQHandler(void)
  3057. {
  3058. assert(NULL != s_flexcanHandle[3]);
  3059. s_flexcanIsr(CAN3, s_flexcanHandle[3]);
  3060. SDK_ISR_EXIT_BARRIER;
  3061. }
  3062. #endif
  3063. #if defined(CAN4)
  3064. void CAN4_DriverIRQHandler(void)
  3065. {
  3066. assert(NULL != s_flexcanHandle[4]);
  3067. s_flexcanIsr(CAN4, s_flexcanHandle[4]);
  3068. SDK_ISR_EXIT_BARRIER;
  3069. }
  3070. #endif
  3071. #if defined(DMA__CAN0)
  3072. void DMA_FLEXCAN0_INT_DriverIRQHandler(void)
  3073. {
  3074. assert(NULL != s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN0)]);
  3075. s_flexcanIsr(DMA__CAN0, s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN0)]);
  3076. SDK_ISR_EXIT_BARRIER;
  3077. }
  3078. #endif
  3079. #if defined(DMA__CAN1)
  3080. void DMA_FLEXCAN1_INT_DriverIRQHandler(void)
  3081. {
  3082. assert(NULL != s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN1)]);
  3083. s_flexcanIsr(DMA__CAN1, s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN1)]);
  3084. SDK_ISR_EXIT_BARRIER;
  3085. }
  3086. #endif
  3087. #if defined(DMA__CAN2)
  3088. void DMA_FLEXCAN2_INT_DriverIRQHandler(void)
  3089. {
  3090. assert(NULL != s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN2)]);
  3091. s_flexcanIsr(DMA__CAN2, s_flexcanHandle[FLEXCAN_GetInstance(DMA__CAN2)]);
  3092. SDK_ISR_EXIT_BARRIER;
  3093. }
  3094. #endif
  3095. #if defined(ADMA__CAN0)
  3096. void ADMA_FLEXCAN0_INT_DriverIRQHandler(void)
  3097. {
  3098. assert(NULL != s_flexcanHandle[FLEXCAN_GetInstance(ADMA__CAN0)]);
  3099. s_flexcanIsr(ADMA__CAN0, s_flexcanHandle[FLEXCAN_GetInstance(ADMA__CAN0)]);
  3100. SDK_ISR_EXIT_BARRIER;
  3101. }
  3102. #endif
  3103. #if defined(ADMA__CAN1)
  3104. void ADMA_FLEXCAN1_INT_DriverIRQHandler(void)
  3105. {
  3106. assert(NULL != s_flexcanHandle[FLEXCAN_GetInstance(ADMA__CAN1)]);
  3107. s_flexcanIsr(ADMA__CAN1, s_flexcanHandle[FLEXCAN_GetInstance(ADMA__CAN1)]);
  3108. SDK_ISR_EXIT_BARRIER;
  3109. }
  3110. #endif
  3111. #if defined(ADMA__CAN2)
  3112. void ADMA_FLEXCAN2_INT_DriverIRQHandler(void)
  3113. {
  3114. assert(NULL != s_flexcanHandle[FLEXCAN_GetInstance(ADMA__CAN2)]);
  3115. s_flexcanIsr(ADMA__CAN2, s_flexcanHandle[FLEXCAN_GetInstance(ADMA__CAN2)]);
  3116. SDK_ISR_EXIT_BARRIER;
  3117. }
  3118. #endif