fsl_flexcan.h 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_FLEXCAN_H_
  31. #define _FSL_FLEXCAN_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup flexcan_driver
  35. * @{
  36. */
  37. /******************************************************************************
  38. * Definitions
  39. *****************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief FlexCAN driver version 2.2.0. */
  43. #define FLEXCAN_DRIVER_VERSION (MAKE_VERSION(2, 2, 0))
  44. /*@}*/
  45. /*! @brief FlexCAN Frame ID helper macro. */
  46. #define FLEXCAN_ID_STD(id) \
  47. (((uint32_t)(((uint32_t)(id)) << CAN_ID_STD_SHIFT)) & CAN_ID_STD_MASK) /*!< Standard Frame ID helper macro. */
  48. #define FLEXCAN_ID_EXT(id) \
  49. (((uint32_t)(((uint32_t)(id)) << CAN_ID_EXT_SHIFT)) & \
  50. (CAN_ID_EXT_MASK | CAN_ID_STD_MASK)) /*!< Extend Frame ID helper macro. */
  51. /*! @brief FlexCAN Rx Message Buffer Mask helper macro. */
  52. #define FLEXCAN_RX_MB_STD_MASK(id, rtr, ide) \
  53. (((uint32_t)((uint32_t)(rtr) << 31) | (uint32_t)((uint32_t)(ide) << 30)) | \
  54. FLEXCAN_ID_STD(id)) /*!< Standard Rx Message Buffer Mask helper macro. */
  55. #define FLEXCAN_RX_MB_EXT_MASK(id, rtr, ide) \
  56. (((uint32_t)((uint32_t)(rtr) << 31) | (uint32_t)((uint32_t)(ide) << 30)) | \
  57. FLEXCAN_ID_EXT(id)) /*!< Extend Rx Message Buffer Mask helper macro. */
  58. /*! @brief FlexCAN Rx FIFO Mask helper macro. */
  59. #define FLEXCAN_RX_FIFO_STD_MASK_TYPE_A(id, rtr, ide) \
  60. (((uint32_t)((uint32_t)(rtr) << 31) | (uint32_t)((uint32_t)(ide) << 30)) | \
  61. (FLEXCAN_ID_STD(id) << 1)) /*!< Standard Rx FIFO Mask helper macro Type A helper macro. */
  62. #define FLEXCAN_RX_FIFO_STD_MASK_TYPE_B_HIGH(id, rtr, ide) \
  63. (((uint32_t)((uint32_t)(rtr) << 31) | (uint32_t)((uint32_t)(ide) << 30)) | \
  64. (((uint32_t)(id) & 0x7FF) << 19)) /*!< Standard Rx FIFO Mask helper macro Type B upper part helper macro. */
  65. #define FLEXCAN_RX_FIFO_STD_MASK_TYPE_B_LOW(id, rtr, ide) \
  66. (((uint32_t)((uint32_t)(rtr) << 15) | (uint32_t)((uint32_t)(ide) << 14)) | \
  67. (((uint32_t)(id) & 0x7FF) << 3)) /*!< Standard Rx FIFO Mask helper macro Type B lower part helper macro. */
  68. #define FLEXCAN_RX_FIFO_STD_MASK_TYPE_C_HIGH(id) \
  69. (((uint32_t)(id) & 0x7F8) << 21) /*!< Standard Rx FIFO Mask helper macro Type C upper part helper macro. */
  70. #define FLEXCAN_RX_FIFO_STD_MASK_TYPE_C_MID_HIGH(id) \
  71. (((uint32_t)(id) & 0x7F8) << 13) /*!< Standard Rx FIFO Mask helper macro Type C mid-upper part helper macro. */
  72. #define FLEXCAN_RX_FIFO_STD_MASK_TYPE_C_MID_LOW(id) \
  73. (((uint32_t)(id) & 0x7F8) << 5) /*!< Standard Rx FIFO Mask helper macro Type C mid-lower part helper macro. */
  74. #define FLEXCAN_RX_FIFO_STD_MASK_TYPE_C_LOW(id) \
  75. (((uint32_t)(id) & 0x7F8) >> 3) /*!< Standard Rx FIFO Mask helper macro Type C lower part helper macro. */
  76. #define FLEXCAN_RX_FIFO_EXT_MASK_TYPE_A(id, rtr, ide) \
  77. (((uint32_t)((uint32_t)(rtr) << 31) | (uint32_t)((uint32_t)(ide) << 30)) | \
  78. (FLEXCAN_ID_EXT(id) << 1)) /*!< Extend Rx FIFO Mask helper macro Type A helper macro. */
  79. #define FLEXCAN_RX_FIFO_EXT_MASK_TYPE_B_HIGH(id, rtr, ide) \
  80. (((uint32_t)((uint32_t)(rtr) << 31) | (uint32_t)((uint32_t)(ide) << 30)) | \
  81. ((FLEXCAN_ID_EXT(id) & 0x1FFF8000) << 1)) /*!< Extend Rx FIFO Mask helper macro Type B upper part helper macro. */
  82. #define FLEXCAN_RX_FIFO_EXT_MASK_TYPE_B_LOW(id, rtr, ide) \
  83. (((uint32_t)((uint32_t)(rtr) << 15) | (uint32_t)((uint32_t)(ide) << 14)) | \
  84. ((FLEXCAN_ID_EXT(id) & 0x1FFF8000) >> \
  85. 15)) /*!< Extend Rx FIFO Mask helper macro Type B lower part helper macro. */
  86. #define FLEXCAN_RX_FIFO_EXT_MASK_TYPE_C_HIGH(id) \
  87. ((FLEXCAN_ID_EXT(id) & 0x1FE00000) << 3) /*!< Extend Rx FIFO Mask helper macro Type C upper part helper macro. */
  88. #define FLEXCAN_RX_FIFO_EXT_MASK_TYPE_C_MID_HIGH(id) \
  89. ((FLEXCAN_ID_EXT(id) & 0x1FE00000) >> \
  90. 5) /*!< Extend Rx FIFO Mask helper macro Type C mid-upper part helper macro. */
  91. #define FLEXCAN_RX_FIFO_EXT_MASK_TYPE_C_MID_LOW(id) \
  92. ((FLEXCAN_ID_EXT(id) & 0x1FE00000) >> \
  93. 13) /*!< Extend Rx FIFO Mask helper macro Type C mid-lower part helper macro. */
  94. #define FLEXCAN_RX_FIFO_EXT_MASK_TYPE_C_LOW(id) \
  95. ((FLEXCAN_ID_EXT(id) & 0x1FE00000) >> 21) /*!< Extend Rx FIFO Mask helper macro Type C lower part helper macro. */
  96. /*! @brief FlexCAN Rx FIFO Filter helper macro. */
  97. #define FLEXCAN_RX_FIFO_STD_FILTER_TYPE_A(id, rtr, ide) \
  98. FLEXCAN_RX_FIFO_STD_MASK_TYPE_A(id, rtr, ide) /*!< Standard Rx FIFO Filter helper macro Type A helper macro. */
  99. #define FLEXCAN_RX_FIFO_STD_FILTER_TYPE_B_HIGH(id, rtr, ide) \
  100. FLEXCAN_RX_FIFO_STD_MASK_TYPE_B_HIGH( \
  101. id, rtr, ide) /*!< Standard Rx FIFO Filter helper macro Type B upper part helper macro. */
  102. #define FLEXCAN_RX_FIFO_STD_FILTER_TYPE_B_LOW(id, rtr, ide) \
  103. FLEXCAN_RX_FIFO_STD_MASK_TYPE_B_LOW( \
  104. id, rtr, ide) /*!< Standard Rx FIFO Filter helper macro Type B lower part helper macro. */
  105. #define FLEXCAN_RX_FIFO_STD_FILTER_TYPE_C_HIGH(id) \
  106. FLEXCAN_RX_FIFO_STD_MASK_TYPE_C_HIGH( \
  107. id) /*!< Standard Rx FIFO Filter helper macro Type C upper part helper macro. */
  108. #define FLEXCAN_RX_FIFO_STD_FILTER_TYPE_C_MID_HIGH(id) \
  109. FLEXCAN_RX_FIFO_STD_MASK_TYPE_C_MID_HIGH( \
  110. id) /*!< Standard Rx FIFO Filter helper macro Type C mid-upper part helper macro. */
  111. #define FLEXCAN_RX_FIFO_STD_FILTER_TYPE_C_MID_LOW(id) \
  112. FLEXCAN_RX_FIFO_STD_MASK_TYPE_C_MID_LOW( \
  113. id) /*!< Standard Rx FIFO Filter helper macro Type C mid-lower part helper macro. */
  114. #define FLEXCAN_RX_FIFO_STD_FILTER_TYPE_C_LOW(id) \
  115. FLEXCAN_RX_FIFO_STD_MASK_TYPE_C_LOW(id) /*!< Standard Rx FIFO Filter helper macro Type C lower part helper macro. \
  116. */
  117. #define FLEXCAN_RX_FIFO_EXT_FILTER_TYPE_A(id, rtr, ide) \
  118. FLEXCAN_RX_FIFO_EXT_MASK_TYPE_A(id, rtr, ide) /*!< Extend Rx FIFO Filter helper macro Type A helper macro. */
  119. #define FLEXCAN_RX_FIFO_EXT_FILTER_TYPE_B_HIGH(id, rtr, ide) \
  120. FLEXCAN_RX_FIFO_EXT_MASK_TYPE_B_HIGH( \
  121. id, rtr, ide) /*!< Extend Rx FIFO Filter helper macro Type B upper part helper macro. */
  122. #define FLEXCAN_RX_FIFO_EXT_FILTER_TYPE_B_LOW(id, rtr, ide) \
  123. FLEXCAN_RX_FIFO_EXT_MASK_TYPE_B_LOW( \
  124. id, rtr, ide) /*!< Extend Rx FIFO Filter helper macro Type B lower part helper macro. */
  125. #define FLEXCAN_RX_FIFO_EXT_FILTER_TYPE_C_HIGH(id) \
  126. FLEXCAN_RX_FIFO_EXT_MASK_TYPE_C_HIGH(id) /*!< Extend Rx FIFO Filter helper macro Type C upper part helper macro. \
  127. */
  128. #define FLEXCAN_RX_FIFO_EXT_FILTER_TYPE_C_MID_HIGH(id) \
  129. FLEXCAN_RX_FIFO_EXT_MASK_TYPE_C_MID_HIGH( \
  130. id) /*!< Extend Rx FIFO Filter helper macro Type C mid-upper part helper macro. */
  131. #define FLEXCAN_RX_FIFO_EXT_FILTER_TYPE_C_MID_LOW(id) \
  132. FLEXCAN_RX_FIFO_EXT_MASK_TYPE_C_MID_LOW( \
  133. id) /*!< Extend Rx FIFO Filter helper macro Type C mid-lower part helper macro. */
  134. #define FLEXCAN_RX_FIFO_EXT_FILTER_TYPE_C_LOW(id) \
  135. FLEXCAN_RX_FIFO_EXT_MASK_TYPE_C_LOW(id) /*!< Extend Rx FIFO Filter helper macro Type C lower part helper macro. */
  136. /*! @brief FlexCAN transfer status. */
  137. enum _flexcan_status
  138. {
  139. kStatus_FLEXCAN_TxBusy = MAKE_STATUS(kStatusGroup_FLEXCAN, 0), /*!< Tx Message Buffer is Busy. */
  140. kStatus_FLEXCAN_TxIdle = MAKE_STATUS(kStatusGroup_FLEXCAN, 1), /*!< Tx Message Buffer is Idle. */
  141. kStatus_FLEXCAN_TxSwitchToRx = MAKE_STATUS(
  142. kStatusGroup_FLEXCAN, 2), /*!< Remote Message is send out and Message buffer changed to Receive one. */
  143. kStatus_FLEXCAN_RxBusy = MAKE_STATUS(kStatusGroup_FLEXCAN, 3), /*!< Rx Message Buffer is Busy. */
  144. kStatus_FLEXCAN_RxIdle = MAKE_STATUS(kStatusGroup_FLEXCAN, 4), /*!< Rx Message Buffer is Idle. */
  145. kStatus_FLEXCAN_RxOverflow = MAKE_STATUS(kStatusGroup_FLEXCAN, 5), /*!< Rx Message Buffer is Overflowed. */
  146. kStatus_FLEXCAN_RxFifoBusy = MAKE_STATUS(kStatusGroup_FLEXCAN, 6), /*!< Rx Message FIFO is Busy. */
  147. kStatus_FLEXCAN_RxFifoIdle = MAKE_STATUS(kStatusGroup_FLEXCAN, 7), /*!< Rx Message FIFO is Idle. */
  148. kStatus_FLEXCAN_RxFifoOverflow = MAKE_STATUS(kStatusGroup_FLEXCAN, 8), /*!< Rx Message FIFO is overflowed. */
  149. kStatus_FLEXCAN_RxFifoWarning = MAKE_STATUS(kStatusGroup_FLEXCAN, 9), /*!< Rx Message FIFO is almost overflowed. */
  150. kStatus_FLEXCAN_ErrorStatus = MAKE_STATUS(kStatusGroup_FLEXCAN, 10), /*!< FlexCAN Module Error and Status. */
  151. kStatus_FLEXCAN_UnHandled = MAKE_STATUS(kStatusGroup_FLEXCAN, 11), /*!< UnHadled Interrupt asserted. */
  152. };
  153. /*! @brief FlexCAN frame format. */
  154. typedef enum _flexcan_frame_format
  155. {
  156. kFLEXCAN_FrameFormatStandard = 0x0U, /*!< Standard frame format attribute. */
  157. kFLEXCAN_FrameFormatExtend = 0x1U, /*!< Extend frame format attribute. */
  158. } flexcan_frame_format_t;
  159. /*! @brief FlexCAN frame type. */
  160. typedef enum _flexcan_frame_type
  161. {
  162. kFLEXCAN_FrameTypeData = 0x0U, /*!< Data frame type attribute. */
  163. kFLEXCAN_FrameTypeRemote = 0x1U, /*!< Remote frame type attribute. */
  164. } flexcan_frame_type_t;
  165. #if (!defined(FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE)) || !FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE
  166. /*! @brief FlexCAN clock source. */
  167. typedef enum _flexcan_clock_source
  168. {
  169. kFLEXCAN_ClkSrcOsc = 0x0U, /*!< FlexCAN Protocol Engine clock from Oscillator. */
  170. kFLEXCAN_ClkSrcPeri = 0x1U, /*!< FlexCAN Protocol Engine clock from Peripheral Clock. */
  171. } flexcan_clock_source_t;
  172. #endif /* FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE */
  173. /*! @brief FlexCAN Rx Fifo Filter type. */
  174. typedef enum _flexcan_rx_fifo_filter_type
  175. {
  176. kFLEXCAN_RxFifoFilterTypeA = 0x0U, /*!< One full ID (standard and extended) per ID Filter element. */
  177. kFLEXCAN_RxFifoFilterTypeB =
  178. 0x1U, /*!< Two full standard IDs or two partial 14-bit ID slices per ID Filter Table element. */
  179. kFLEXCAN_RxFifoFilterTypeC =
  180. 0x2U, /*!< Four partial 8-bit Standard or extended ID slices per ID Filter Table element. */
  181. kFLEXCAN_RxFifoFilterTypeD = 0x3U, /*!< All frames rejected. */
  182. } flexcan_rx_fifo_filter_type_t;
  183. /*!
  184. * @brief FlexCAN Rx FIFO priority.
  185. *
  186. * The matching process starts from the Rx MB(or Rx FIFO) with higher priority.
  187. * If no MB(or Rx FIFO filter) is satisfied, the matching process goes on with
  188. * the Rx FIFO(or Rx MB) with lower priority.
  189. */
  190. typedef enum _flexcan_rx_fifo_priority
  191. {
  192. kFLEXCAN_RxFifoPrioLow = 0x0U, /*!< Matching process start from Rx Message Buffer first*/
  193. kFLEXCAN_RxFifoPrioHigh = 0x1U, /*!< Matching process start from Rx FIFO first*/
  194. } flexcan_rx_fifo_priority_t;
  195. /*!
  196. * @brief FlexCAN interrupt configuration structure, default settings all disabled.
  197. *
  198. * This structure contains the settings for all of the FlexCAN Module interrupt configurations.
  199. * Note: FlexCAN Message Buffers and Rx FIFO have their own interrupts.
  200. */
  201. enum _flexcan_interrupt_enable
  202. {
  203. kFLEXCAN_BusOffInterruptEnable = CAN_CTRL1_BOFFMSK_MASK, /*!< Bus Off interrupt. */
  204. kFLEXCAN_ErrorInterruptEnable = CAN_CTRL1_ERRMSK_MASK, /*!< Error interrupt. */
  205. kFLEXCAN_RxWarningInterruptEnable = CAN_CTRL1_RWRNMSK_MASK, /*!< Rx Warning interrupt. */
  206. kFLEXCAN_TxWarningInterruptEnable = CAN_CTRL1_TWRNMSK_MASK, /*!< Tx Warning interrupt. */
  207. kFLEXCAN_WakeUpInterruptEnable = CAN_MCR_WAKMSK_MASK, /*!< Wake Up interrupt. */
  208. };
  209. /*!
  210. * @brief FlexCAN status flags.
  211. *
  212. * This provides constants for the FlexCAN status flags for use in the FlexCAN functions.
  213. * Note: The CPU read action clears FlEXCAN_ErrorFlag, therefore user need to
  214. * read FlEXCAN_ErrorFlag and distinguish which error is occur using
  215. * @ref _flexcan_error_flags enumerations.
  216. */
  217. enum _flexcan_flags
  218. {
  219. kFLEXCAN_SynchFlag = CAN_ESR1_SYNCH_MASK, /*!< CAN Synchronization Status. */
  220. kFLEXCAN_TxWarningIntFlag = CAN_ESR1_TWRNINT_MASK, /*!< Tx Warning Interrupt Flag. */
  221. kFLEXCAN_RxWarningIntFlag = CAN_ESR1_RWRNINT_MASK, /*!< Rx Warning Interrupt Flag. */
  222. kFLEXCAN_TxErrorWarningFlag = CAN_ESR1_TXWRN_MASK, /*!< Tx Error Warning Status. */
  223. kFLEXCAN_RxErrorWarningFlag = CAN_ESR1_RXWRN_MASK, /*!< Rx Error Warning Status. */
  224. kFLEXCAN_IdleFlag = CAN_ESR1_IDLE_MASK, /*!< CAN IDLE Status Flag. */
  225. kFLEXCAN_FaultConfinementFlag = CAN_ESR1_FLTCONF_MASK, /*!< Fault Confinement State Flag. */
  226. kFLEXCAN_TransmittingFlag = CAN_ESR1_TX_MASK, /*!< FlexCAN In Transmission Status. */
  227. kFLEXCAN_ReceivingFlag = CAN_ESR1_RX_MASK, /*!< FlexCAN In Reception Status. */
  228. kFLEXCAN_BusOffIntFlag = CAN_ESR1_BOFFINT_MASK, /*!< Bus Off Interrupt Flag. */
  229. kFLEXCAN_ErrorIntFlag = CAN_ESR1_ERRINT_MASK, /*!< Error Interrupt Flag. */
  230. kFLEXCAN_WakeUpIntFlag = CAN_ESR1_WAKINT_MASK, /*!< Wake-Up Interrupt Flag. */
  231. kFLEXCAN_ErrorFlag = CAN_ESR1_BIT1ERR_MASK | /*!< All FlexCAN Error Status. */
  232. CAN_ESR1_BIT0ERR_MASK |
  233. CAN_ESR1_ACKERR_MASK | CAN_ESR1_CRCERR_MASK | CAN_ESR1_FRMERR_MASK | CAN_ESR1_STFERR_MASK,
  234. };
  235. /*!
  236. * @brief FlexCAN error status flags.
  237. *
  238. * The FlexCAN Error Status enumerations is used to report current error of the FlexCAN bus.
  239. * This enumerations should be used with KFLEXCAN_ErrorFlag in @ref _flexcan_flags enumerations
  240. * to ditermine which error is generated.
  241. */
  242. enum _flexcan_error_flags
  243. {
  244. kFLEXCAN_StuffingError = CAN_ESR1_STFERR_MASK, /*!< Stuffing Error. */
  245. kFLEXCAN_FormError = CAN_ESR1_FRMERR_MASK, /*!< Form Error. */
  246. kFLEXCAN_CrcError = CAN_ESR1_CRCERR_MASK, /*!< Cyclic Redundancy Check Error. */
  247. kFLEXCAN_AckError = CAN_ESR1_ACKERR_MASK, /*!< Received no ACK on transmission. */
  248. kFLEXCAN_Bit0Error = CAN_ESR1_BIT0ERR_MASK, /*!< Unable to send dominant bit. */
  249. kFLEXCAN_Bit1Error = CAN_ESR1_BIT1ERR_MASK, /*!< Unable to send recessive bit. */
  250. };
  251. /*!
  252. * @brief FlexCAN Rx FIFO status flags.
  253. *
  254. * The FlexCAN Rx FIFO Status enumerations are used to determine the status of the
  255. * Rx FIFO. Because Rx FIFO occupy the MB0 ~ MB7 (Rx Fifo filter also occupies
  256. * more Message Buffer space), Rx FIFO status flags are mapped to the corresponding
  257. * Message Buffer status flags.
  258. */
  259. enum _flexcan_rx_fifo_flags
  260. {
  261. kFLEXCAN_RxFifoOverflowFlag = CAN_IFLAG1_BUF7I_MASK, /*!< Rx FIFO overflow flag. */
  262. kFLEXCAN_RxFifoWarningFlag = CAN_IFLAG1_BUF6I_MASK, /*!< Rx FIFO almost full flag. */
  263. kFLEXCAN_RxFifoFrameAvlFlag = CAN_IFLAG1_BUF5I_MASK, /*!< Frames available in Rx FIFO flag. */
  264. };
  265. #if defined(__CC_ARM)
  266. #pragma anon_unions
  267. #endif
  268. /*! @brief FlexCAN message frame structure. */
  269. typedef struct _flexcan_frame
  270. {
  271. struct
  272. {
  273. uint32_t timestamp : 16; /*!< FlexCAN internal Free-Running Counter Time Stamp. */
  274. uint32_t length : 4; /*!< CAN frame payload length in bytes(Range: 0~8). */
  275. uint32_t type : 1; /*!< CAN Frame Type(DATA or REMOTE). */
  276. uint32_t format : 1; /*!< CAN Frame Identifier(STD or EXT format). */
  277. uint32_t : 1; /*!< Reserved. */
  278. uint32_t idhit : 9; /*!< CAN Rx FIFO filter hit id(This value is only used in Rx FIFO receive mode). */
  279. };
  280. struct
  281. {
  282. uint32_t id : 29; /*!< CAN Frame Identifier, should be set using FLEXCAN_ID_EXT() or FLEXCAN_ID_STD() macro. */
  283. uint32_t : 3; /*!< Reserved. */
  284. };
  285. union
  286. {
  287. struct
  288. {
  289. uint32_t dataWord0; /*!< CAN Frame payload word0. */
  290. uint32_t dataWord1; /*!< CAN Frame payload word1. */
  291. };
  292. struct
  293. {
  294. uint8_t dataByte3; /*!< CAN Frame payload byte3. */
  295. uint8_t dataByte2; /*!< CAN Frame payload byte2. */
  296. uint8_t dataByte1; /*!< CAN Frame payload byte1. */
  297. uint8_t dataByte0; /*!< CAN Frame payload byte0. */
  298. uint8_t dataByte7; /*!< CAN Frame payload byte7. */
  299. uint8_t dataByte6; /*!< CAN Frame payload byte6. */
  300. uint8_t dataByte5; /*!< CAN Frame payload byte5. */
  301. uint8_t dataByte4; /*!< CAN Frame payload byte4. */
  302. };
  303. };
  304. } flexcan_frame_t;
  305. /*! @brief FlexCAN module configuration structure. */
  306. typedef struct _flexcan_config
  307. {
  308. uint32_t baudRate; /*!< FlexCAN baud rate in bps. */
  309. #if (!defined(FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE)) || !FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE
  310. flexcan_clock_source_t clkSrc; /*!< Clock source for FlexCAN Protocol Engine. */
  311. #endif /* FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE */
  312. uint8_t maxMbNum; /*!< The maximum number of Message Buffers used by user. */
  313. bool enableLoopBack; /*!< Enable or Disable Loop Back Self Test Mode. */
  314. bool enableSelfWakeup; /*!< Enable or Disable Self Wakeup Mode. */
  315. bool enableIndividMask; /*!< Enable or Disable Rx Individual Mask. */
  316. #if (defined(FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT) && FSL_FEATURE_FLEXCAN_HAS_DOZE_MODE_SUPPORT)
  317. bool enableDoze; /*!< Enable or Disable Doze Mode. */
  318. #endif
  319. } flexcan_config_t;
  320. /*! @brief FlexCAN protocol timing characteristic configuration structure. */
  321. typedef struct _flexcan_timing_config
  322. {
  323. uint8_t preDivider; /*!< Clock Pre-scaler Division Factor. */
  324. uint8_t rJumpwidth; /*!< Re-sync Jump Width. */
  325. uint8_t phaseSeg1; /*!< Phase Segment 1. */
  326. uint8_t phaseSeg2; /*!< Phase Segment 2. */
  327. uint8_t propSeg; /*!< Propagation Segment. */
  328. } flexcan_timing_config_t;
  329. /*!
  330. * @brief FlexCAN Receive Message Buffer configuration structure
  331. *
  332. * This structure is used as the parameter of FLEXCAN_SetRxMbConfig() function.
  333. * The FLEXCAN_SetRxMbConfig() function is used to configure FlexCAN Receive
  334. * Message Buffer. The function abort previous receiving process, clean the
  335. * Message Buffer and activate the Rx Message Buffer using given Message Buffer
  336. * setting.
  337. */
  338. typedef struct _flexcan_rx_mb_config
  339. {
  340. uint32_t id; /*!< CAN Message Buffer Frame Identifier, should be set using
  341. FLEXCAN_ID_EXT() or FLEXCAN_ID_STD() macro. */
  342. flexcan_frame_format_t format; /*!< CAN Frame Identifier format(Standard of Extend). */
  343. flexcan_frame_type_t type; /*!< CAN Frame Type(Data or Remote). */
  344. } flexcan_rx_mb_config_t;
  345. /*! @brief FlexCAN Rx FIFO configuration structure. */
  346. typedef struct _flexcan_rx_fifo_config
  347. {
  348. uint32_t *idFilterTable; /*!< Pointer to the FlexCAN Rx FIFO identifier filter table. */
  349. uint8_t idFilterNum; /*!< The quantity of filter elements. */
  350. flexcan_rx_fifo_filter_type_t idFilterType; /*!< The FlexCAN Rx FIFO Filter type. */
  351. flexcan_rx_fifo_priority_t priority; /*!< The FlexCAN Rx FIFO receive priority. */
  352. } flexcan_rx_fifo_config_t;
  353. /*! @brief FlexCAN Message Buffer transfer. */
  354. typedef struct _flexcan_mb_transfer
  355. {
  356. flexcan_frame_t *frame; /*!< The buffer of CAN Message to be transfer. */
  357. uint8_t mbIdx; /*!< The index of Message buffer used to transfer Message. */
  358. } flexcan_mb_transfer_t;
  359. /*! @brief FlexCAN Rx FIFO transfer. */
  360. typedef struct _flexcan_fifo_transfer
  361. {
  362. flexcan_frame_t *frame; /*!< The buffer of CAN Message to be received from Rx FIFO. */
  363. } flexcan_fifo_transfer_t;
  364. /*! @brief FlexCAN handle structure definition. */
  365. typedef struct _flexcan_handle flexcan_handle_t;
  366. /*! @brief FlexCAN transfer callback function.
  367. *
  368. * The FlexCAN transfer callback returns a value from the underlying layer.
  369. * If the status equals to kStatus_FLEXCAN_ErrorStatus, the result parameter is the Content of
  370. * FlexCAN status register which can be used to get the working status(or error status) of FlexCAN module.
  371. * If the status equals to other FlexCAN Message Buffer transfer status, the result is the index of
  372. * Message Buffer that generate transfer event.
  373. * If the status equals to other FlexCAN Message Buffer transfer status, the result is meaningless and should be
  374. * Ignored.
  375. */
  376. typedef void (*flexcan_transfer_callback_t)(
  377. CAN_Type *base, flexcan_handle_t *handle, status_t status, uint32_t result, void *userData);
  378. /*! @brief FlexCAN handle structure. */
  379. struct _flexcan_handle
  380. {
  381. flexcan_transfer_callback_t callback; /*!< Callback function. */
  382. void *userData; /*!< FlexCAN callback function parameter.*/
  383. flexcan_frame_t *volatile mbFrameBuf[CAN_WORD1_COUNT];
  384. /*!< The buffer for received data from Message Buffers. */
  385. flexcan_frame_t *volatile rxFifoFrameBuf; /*!< The buffer for received data from Rx FIFO. */
  386. volatile uint8_t mbState[CAN_WORD1_COUNT]; /*!< Message Buffer transfer state. */
  387. volatile uint8_t rxFifoState; /*!< Rx FIFO transfer state. */
  388. };
  389. /******************************************************************************
  390. * API
  391. *****************************************************************************/
  392. #if defined(__cplusplus)
  393. extern "C" {
  394. #endif
  395. /*!
  396. * @name Initialization and deinitialization
  397. * @{
  398. */
  399. /*!
  400. * @brief Initializes a FlexCAN instance.
  401. *
  402. * This function initializes the FlexCAN module with user-defined settings.
  403. * This example shows how to set up the flexcan_config_t parameters and how
  404. * to call the FLEXCAN_Init function by passing in these parameters.
  405. * @code
  406. * flexcan_config_t flexcanConfig;
  407. * flexcanConfig.clkSrc = kFLEXCAN_ClkSrcOsc;
  408. * flexcanConfig.baudRate = 125000U;
  409. * flexcanConfig.maxMbNum = 16;
  410. * flexcanConfig.enableLoopBack = false;
  411. * flexcanConfig.enableSelfWakeup = false;
  412. * flexcanConfig.enableIndividMask = false;
  413. * flexcanConfig.enableDoze = false;
  414. * FLEXCAN_Init(CAN0, &flexcanConfig, 8000000UL);
  415. * @endcode
  416. *
  417. * @param base FlexCAN peripheral base address.
  418. * @param config Pointer to the user-defined configuration structure.
  419. * @param sourceClock_Hz FlexCAN Protocol Engine clock source frequency in Hz.
  420. */
  421. void FLEXCAN_Init(CAN_Type *base, const flexcan_config_t *config, uint32_t sourceClock_Hz);
  422. /*!
  423. * @brief De-initializes a FlexCAN instance.
  424. *
  425. * This function disables the FlexCAN module clock and sets all register values
  426. * to the reset value.
  427. *
  428. * @param base FlexCAN peripheral base address.
  429. */
  430. void FLEXCAN_Deinit(CAN_Type *base);
  431. /*!
  432. * @brief Gets the default configuration structure.
  433. *
  434. * This function initializes the FlexCAN configuration structure to default values. The default
  435. * values are as follows.
  436. * flexcanConfig->clkSrc = KFLEXCAN_ClkSrcOsc;
  437. * flexcanConfig->baudRate = 125000U;
  438. * flexcanConfig->maxMbNum = 16;
  439. * flexcanConfig->enableLoopBack = false;
  440. * flexcanConfig->enableSelfWakeup = false;
  441. * flexcanConfig->enableIndividMask = false;
  442. * flexcanConfig->enableDoze = false;
  443. *
  444. * @param config Pointer to the FlexCAN configuration structure.
  445. */
  446. void FLEXCAN_GetDefaultConfig(flexcan_config_t *config);
  447. /* @} */
  448. /*!
  449. * @name Configuration.
  450. * @{
  451. */
  452. /*!
  453. * @brief Sets the FlexCAN protocol timing characteristic.
  454. *
  455. * This function gives user settings to CAN bus timing characteristic.
  456. * The function is for an experienced user. For less experienced users, call
  457. * the FLEXCAN_Init() and fill the baud rate field with a desired value.
  458. * This provides the default timing characteristics to the module.
  459. *
  460. * Note that calling FLEXCAN_SetTimingConfig() overrides the baud rate set
  461. * in FLEXCAN_Init().
  462. *
  463. * @param base FlexCAN peripheral base address.
  464. * @param config Pointer to the timing configuration structure.
  465. */
  466. void FLEXCAN_SetTimingConfig(CAN_Type *base, const flexcan_timing_config_t *config);
  467. /*!
  468. * @brief Sets the FlexCAN receive message buffer global mask.
  469. *
  470. * This function sets the global mask for the FlexCAN message buffer in a matching process.
  471. * The configuration is only effective when the Rx individual mask is disabled in the FLEXCAN_Init().
  472. *
  473. * @param base FlexCAN peripheral base address.
  474. * @param mask Rx Message Buffer Global Mask value.
  475. */
  476. void FLEXCAN_SetRxMbGlobalMask(CAN_Type *base, uint32_t mask);
  477. /*!
  478. * @brief Sets the FlexCAN receive FIFO global mask.
  479. *
  480. * This function sets the global mask for FlexCAN FIFO in a matching process.
  481. *
  482. * @param base FlexCAN peripheral base address.
  483. * @param mask Rx Fifo Global Mask value.
  484. */
  485. void FLEXCAN_SetRxFifoGlobalMask(CAN_Type *base, uint32_t mask);
  486. /*!
  487. * @brief Sets the FlexCAN receive individual mask.
  488. *
  489. * This function sets the individual mask for the FlexCAN matching process.
  490. * The configuration is only effective when the Rx individual mask is enabled in the FLEXCAN_Init().
  491. * If the Rx FIFO is disabled, the individual mask is applied to the corresponding Message Buffer.
  492. * If the Rx FIFO is enabled, the individual mask for Rx FIFO occupied Message Buffer is applied to
  493. * the Rx Filter with the same index. Note that only the first 32
  494. * individual masks can be used as the Rx FIFO filter mask.
  495. *
  496. * @param base FlexCAN peripheral base address.
  497. * @param maskIdx The Index of individual Mask.
  498. * @param mask Rx Individual Mask value.
  499. */
  500. void FLEXCAN_SetRxIndividualMask(CAN_Type *base, uint8_t maskIdx, uint32_t mask);
  501. /*!
  502. * @brief Configures a FlexCAN transmit message buffer.
  503. *
  504. * This function aborts the previous transmission, cleans the Message Buffer, and
  505. * configures it as a Transmit Message Buffer.
  506. *
  507. * @param base FlexCAN peripheral base address.
  508. * @param mbIdx The Message Buffer index.
  509. * @param enable Enable/disable Tx Message Buffer.
  510. * - true: Enable Tx Message Buffer.
  511. * - false: Disable Tx Message Buffer.
  512. */
  513. void FLEXCAN_SetTxMbConfig(CAN_Type *base, uint8_t mbIdx, bool enable);
  514. /*!
  515. * @brief Configures a FlexCAN Receive Message Buffer.
  516. *
  517. * This function cleans a FlexCAN build-in Message Buffer and configures it
  518. * as a Receive Message Buffer.
  519. *
  520. * @param base FlexCAN peripheral base address.
  521. * @param mbIdx The Message Buffer index.
  522. * @param config Pointer to the FlexCAN Message Buffer configuration structure.
  523. * @param enable Enable/disable Rx Message Buffer.
  524. * - true: Enable Rx Message Buffer.
  525. * - false: Disable Rx Message Buffer.
  526. */
  527. void FLEXCAN_SetRxMbConfig(CAN_Type *base, uint8_t mbIdx, const flexcan_rx_mb_config_t *config, bool enable);
  528. /*!
  529. * @brief Configures the FlexCAN Rx FIFO.
  530. *
  531. * This function configures the Rx FIFO with given Rx FIFO configuration.
  532. *
  533. * @param base FlexCAN peripheral base address.
  534. * @param config Pointer to the FlexCAN Rx FIFO configuration structure.
  535. * @param enable Enable/disable Rx FIFO.
  536. * - true: Enable Rx FIFO.
  537. * - false: Disable Rx FIFO.
  538. */
  539. void FLEXCAN_SetRxFifoConfig(CAN_Type *base, const flexcan_rx_fifo_config_t *config, bool enable);
  540. /* @} */
  541. /*!
  542. * @name Status
  543. * @{
  544. */
  545. /*!
  546. * @brief Gets the FlexCAN module interrupt flags.
  547. *
  548. * This function gets all FlexCAN status flags. The flags are returned as the logical
  549. * OR value of the enumerators @ref _flexcan_flags. To check the specific status,
  550. * compare the return value with enumerators in @ref _flexcan_flags.
  551. *
  552. * @param base FlexCAN peripheral base address.
  553. * @return FlexCAN status flags which are ORed by the enumerators in the _flexcan_flags.
  554. */
  555. static inline uint32_t FLEXCAN_GetStatusFlags(CAN_Type *base)
  556. {
  557. return base->ESR1;
  558. }
  559. /*!
  560. * @brief Clears status flags with the provided mask.
  561. *
  562. * This function clears the FlexCAN status flags with a provided mask. An automatically cleared flag
  563. * can't be cleared by this function.
  564. *
  565. * @param base FlexCAN peripheral base address.
  566. * @param mask The status flags to be cleared, it is logical OR value of @ref _flexcan_flags.
  567. */
  568. static inline void FLEXCAN_ClearStatusFlags(CAN_Type *base, uint32_t mask)
  569. {
  570. /* Write 1 to clear status flag. */
  571. base->ESR1 = mask;
  572. }
  573. /*!
  574. * @brief Gets the FlexCAN Bus Error Counter value.
  575. *
  576. * This function gets the FlexCAN Bus Error Counter value for both Tx and
  577. * Rx direction. These values may be needed in the upper layer error handling.
  578. *
  579. * @param base FlexCAN peripheral base address.
  580. * @param txErrBuf Buffer to store Tx Error Counter value.
  581. * @param rxErrBuf Buffer to store Rx Error Counter value.
  582. */
  583. static inline void FLEXCAN_GetBusErrCount(CAN_Type *base, uint8_t *txErrBuf, uint8_t *rxErrBuf)
  584. {
  585. if (txErrBuf)
  586. {
  587. *txErrBuf = (uint8_t)((base->ECR & CAN_ECR_TXERRCNT_MASK) >> CAN_ECR_TXERRCNT_SHIFT);
  588. }
  589. if (rxErrBuf)
  590. {
  591. *rxErrBuf = (uint8_t)((base->ECR & CAN_ECR_RXERRCNT_MASK) >> CAN_ECR_RXERRCNT_SHIFT);
  592. }
  593. }
  594. /*!
  595. * @brief Gets the FlexCAN Message Buffer interrupt flags.
  596. *
  597. * This function gets the interrupt flags of a given Message Buffers.
  598. *
  599. * @param base FlexCAN peripheral base address.
  600. * @param mask The ORed FlexCAN Message Buffer mask.
  601. * @return The status of given Message Buffers.
  602. */
  603. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  604. static inline uint64_t FLEXCAN_GetMbStatusFlags(CAN_Type *base, uint64_t mask)
  605. #else
  606. static inline uint32_t FLEXCAN_GetMbStatusFlags(CAN_Type *base, uint32_t mask)
  607. #endif
  608. {
  609. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  610. return ((((uint64_t)base->IFLAG1) & mask) | ((((uint64_t)base->IFLAG2) << 32) & mask));
  611. #else
  612. return (base->IFLAG1 & mask);
  613. #endif
  614. }
  615. /*!
  616. * @brief Clears the FlexCAN Message Buffer interrupt flags.
  617. *
  618. * This function clears the interrupt flags of a given Message Buffers.
  619. *
  620. * @param base FlexCAN peripheral base address.
  621. * @param mask The ORed FlexCAN Message Buffer mask.
  622. */
  623. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  624. static inline void FLEXCAN_ClearMbStatusFlags(CAN_Type *base, uint64_t mask)
  625. #else
  626. static inline void FLEXCAN_ClearMbStatusFlags(CAN_Type *base, uint32_t mask)
  627. #endif
  628. {
  629. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  630. base->IFLAG1 = (uint32_t)(mask & 0xFFFFFFFFU);
  631. base->IFLAG2 = (uint32_t)(mask >> 32);
  632. #else
  633. base->IFLAG1 = mask;
  634. #endif
  635. }
  636. /* @} */
  637. /*!
  638. * @name Interrupts
  639. * @{
  640. */
  641. /*!
  642. * @brief Enables FlexCAN interrupts according to the provided mask.
  643. *
  644. * This function enables the FlexCAN interrupts according to the provided mask. The mask
  645. * is a logical OR of enumeration members, see @ref _flexcan_interrupt_enable.
  646. *
  647. * @param base FlexCAN peripheral base address.
  648. * @param mask The interrupts to enable. Logical OR of @ref _flexcan_interrupt_enable.
  649. */
  650. static inline void FLEXCAN_EnableInterrupts(CAN_Type *base, uint32_t mask)
  651. {
  652. /* Solve Wake Up Interrupt. */
  653. if (mask & kFLEXCAN_WakeUpInterruptEnable)
  654. {
  655. base->MCR |= CAN_MCR_WAKMSK_MASK;
  656. }
  657. /* Solve others. */
  658. base->CTRL1 |= (mask & (~((uint32_t)kFLEXCAN_WakeUpInterruptEnable)));
  659. }
  660. /*!
  661. * @brief Disables FlexCAN interrupts according to the provided mask.
  662. *
  663. * This function disables the FlexCAN interrupts according to the provided mask. The mask
  664. * is a logical OR of enumeration members, see @ref _flexcan_interrupt_enable.
  665. *
  666. * @param base FlexCAN peripheral base address.
  667. * @param mask The interrupts to disable. Logical OR of @ref _flexcan_interrupt_enable.
  668. */
  669. static inline void FLEXCAN_DisableInterrupts(CAN_Type *base, uint32_t mask)
  670. {
  671. /* Solve Wake Up Interrupt. */
  672. if (mask & kFLEXCAN_WakeUpInterruptEnable)
  673. {
  674. base->MCR &= ~CAN_MCR_WAKMSK_MASK;
  675. }
  676. /* Solve others. */
  677. base->CTRL1 &= ~(mask & (~((uint32_t)kFLEXCAN_WakeUpInterruptEnable)));
  678. }
  679. /*!
  680. * @brief Enables FlexCAN Message Buffer interrupts.
  681. *
  682. * This function enables the interrupts of given Message Buffers.
  683. *
  684. * @param base FlexCAN peripheral base address.
  685. * @param mask The ORed FlexCAN Message Buffer mask.
  686. */
  687. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  688. static inline void FLEXCAN_EnableMbInterrupts(CAN_Type *base, uint64_t mask)
  689. #else
  690. static inline void FLEXCAN_EnableMbInterrupts(CAN_Type *base, uint32_t mask)
  691. #endif
  692. {
  693. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  694. base->IMASK1 |= (uint32_t)(mask & 0xFFFFFFFFU);
  695. base->IMASK2 |= (uint32_t)(mask >> 32);
  696. #else
  697. base->IMASK1 |= mask;
  698. #endif
  699. }
  700. /*!
  701. * @brief Disables FlexCAN Message Buffer interrupts.
  702. *
  703. * This function disables the interrupts of given Message Buffers.
  704. *
  705. * @param base FlexCAN peripheral base address.
  706. * @param mask The ORed FlexCAN Message Buffer mask.
  707. */
  708. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  709. static inline void FLEXCAN_DisableMbInterrupts(CAN_Type *base, uint64_t mask)
  710. #else
  711. static inline void FLEXCAN_DisableMbInterrupts(CAN_Type *base, uint32_t mask)
  712. #endif
  713. {
  714. #if (defined(FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER)) && (FSL_FEATURE_FLEXCAN_HAS_EXTENDED_FLAG_REGISTER > 0)
  715. base->IMASK1 &= ~((uint32_t)(mask & 0xFFFFFFFFU));
  716. base->IMASK2 &= ~((uint32_t)(mask >> 32));
  717. #else
  718. base->IMASK1 &= ~mask;
  719. #endif
  720. }
  721. /* @} */
  722. #if (defined(FSL_FEATURE_FLEXCAN_HAS_RX_FIFO_DMA) && FSL_FEATURE_FLEXCAN_HAS_RX_FIFO_DMA)
  723. /*!
  724. * @name DMA Control
  725. * @{
  726. */
  727. /*!
  728. * @brief Enables or disables the FlexCAN Rx FIFO DMA request.
  729. *
  730. * This function enables or disables the DMA feature of FlexCAN build-in Rx FIFO.
  731. *
  732. * @param base FlexCAN peripheral base address.
  733. * @param enable true to enable, false to disable.
  734. */
  735. void FLEXCAN_EnableRxFifoDMA(CAN_Type *base, bool enable);
  736. /*!
  737. * @brief Gets the Rx FIFO Head address.
  738. *
  739. * This function returns the FlexCAN Rx FIFO Head address, which is mainly used for the DMA/eDMA use case.
  740. *
  741. * @param base FlexCAN peripheral base address.
  742. * @return FlexCAN Rx FIFO Head address.
  743. */
  744. static inline uint32_t FLEXCAN_GetRxFifoHeadAddr(CAN_Type *base)
  745. {
  746. return (uint32_t) & (base->MB[0].CS);
  747. }
  748. /* @} */
  749. #endif /* FSL_FEATURE_FLEXCAN_HAS_RX_FIFO_DMA */
  750. /*!
  751. * @name Bus Operations
  752. * @{
  753. */
  754. /*!
  755. * @brief Enables or disables the FlexCAN module operation.
  756. *
  757. * This function enables or disables the FlexCAN module.
  758. *
  759. * @param base FlexCAN base pointer.
  760. * @param enable true to enable, false to disable.
  761. */
  762. static inline void FLEXCAN_Enable(CAN_Type *base, bool enable)
  763. {
  764. if (enable)
  765. {
  766. base->MCR &= ~CAN_MCR_MDIS_MASK;
  767. /* Wait FlexCAN exit from low-power mode. */
  768. while (base->MCR & CAN_MCR_LPMACK_MASK)
  769. {
  770. }
  771. }
  772. else
  773. {
  774. base->MCR |= CAN_MCR_MDIS_MASK;
  775. /* Wait FlexCAN enter low-power mode. */
  776. while (!(base->MCR & CAN_MCR_LPMACK_MASK))
  777. {
  778. }
  779. }
  780. }
  781. /*!
  782. * @brief Writes a FlexCAN Message to the Transmit Message Buffer.
  783. *
  784. * This function writes a CAN Message to the specified Transmit Message Buffer
  785. * and changes the Message Buffer state to start CAN Message transmit. After
  786. * that the function returns immediately.
  787. *
  788. * @param base FlexCAN peripheral base address.
  789. * @param mbIdx The FlexCAN Message Buffer index.
  790. * @param txFrame Pointer to CAN message frame to be sent.
  791. * @retval kStatus_Success - Write Tx Message Buffer Successfully.
  792. * @retval kStatus_Fail - Tx Message Buffer is currently in use.
  793. */
  794. status_t FLEXCAN_WriteTxMb(CAN_Type *base, uint8_t mbIdx, const flexcan_frame_t *txFrame);
  795. /*!
  796. * @brief Reads a FlexCAN Message from Receive Message Buffer.
  797. *
  798. * This function reads a CAN message from a specified Receive Message Buffer.
  799. * The function fills a receive CAN message frame structure with
  800. * just received data and activates the Message Buffer again.
  801. * The function returns immediately.
  802. *
  803. * @param base FlexCAN peripheral base address.
  804. * @param mbIdx The FlexCAN Message Buffer index.
  805. * @param rxFrame Pointer to CAN message frame structure for reception.
  806. * @retval kStatus_Success - Rx Message Buffer is full and has been read successfully.
  807. * @retval kStatus_FLEXCAN_RxOverflow - Rx Message Buffer is already overflowed and has been read successfully.
  808. * @retval kStatus_Fail - Rx Message Buffer is empty.
  809. */
  810. status_t FLEXCAN_ReadRxMb(CAN_Type *base, uint8_t mbIdx, flexcan_frame_t *rxFrame);
  811. /*!
  812. * @brief Reads a FlexCAN Message from Rx FIFO.
  813. *
  814. * This function reads a CAN message from the FlexCAN build-in Rx FIFO.
  815. *
  816. * @param base FlexCAN peripheral base address.
  817. * @param rxFrame Pointer to CAN message frame structure for reception.
  818. * @retval kStatus_Success - Read Message from Rx FIFO successfully.
  819. * @retval kStatus_Fail - Rx FIFO is not enabled.
  820. */
  821. status_t FLEXCAN_ReadRxFifo(CAN_Type *base, flexcan_frame_t *rxFrame);
  822. /* @} */
  823. /*!
  824. * @name Transactional
  825. * @{
  826. */
  827. /*!
  828. * @brief Performs a polling send transaction on the CAN bus.
  829. *
  830. * Note that a transfer handle does not need to be created before calling this API.
  831. *
  832. * @param base FlexCAN peripheral base pointer.
  833. * @param mbIdx The FlexCAN Message Buffer index.
  834. * @param txFrame Pointer to CAN message frame to be sent.
  835. * @retval kStatus_Success - Write Tx Message Buffer Successfully.
  836. * @retval kStatus_Fail - Tx Message Buffer is currently in use.
  837. */
  838. status_t FLEXCAN_TransferSendBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_frame_t *txFrame);
  839. /*!
  840. * @brief Performs a polling receive transaction on the CAN bus.
  841. *
  842. * Note that a transfer handle does not need to be created before calling this API.
  843. *
  844. * @param base FlexCAN peripheral base pointer.
  845. * @param mbIdx The FlexCAN Message Buffer index.
  846. * @param rxFrame Pointer to CAN message frame structure for reception.
  847. * @retval kStatus_Success - Rx Message Buffer is full and has been read successfully.
  848. * @retval kStatus_FLEXCAN_RxOverflow - Rx Message Buffer is already overflowed and has been read successfully.
  849. * @retval kStatus_Fail - Rx Message Buffer is empty.
  850. */
  851. status_t FLEXCAN_TransferReceiveBlocking(CAN_Type *base, uint8_t mbIdx, flexcan_frame_t *rxFrame);
  852. /*!
  853. * @brief Performs a polling receive transaction from Rx FIFO on the CAN bus.
  854. *
  855. * Note that a transfer handle does not need to be created before calling this API.
  856. *
  857. * @param base FlexCAN peripheral base pointer.
  858. * @param rxFrame Pointer to CAN message frame structure for reception.
  859. * @retval kStatus_Success - Read Message from Rx FIFO successfully.
  860. * @retval kStatus_Fail - Rx FIFO is not enabled.
  861. */
  862. status_t FLEXCAN_TransferReceiveFifoBlocking(CAN_Type *base, flexcan_frame_t *rxFrame);
  863. /*!
  864. * @brief Initializes the FlexCAN handle.
  865. *
  866. * This function initializes the FlexCAN handle, which can be used for other FlexCAN
  867. * transactional APIs. Usually, for a specified FlexCAN instance,
  868. * call this API once to get the initialized handle.
  869. *
  870. * @param base FlexCAN peripheral base address.
  871. * @param handle FlexCAN handle pointer.
  872. * @param callback The callback function.
  873. * @param userData The parameter of the callback function.
  874. */
  875. void FLEXCAN_TransferCreateHandle(CAN_Type *base,
  876. flexcan_handle_t *handle,
  877. flexcan_transfer_callback_t callback,
  878. void *userData);
  879. /*!
  880. * @brief Sends a message using IRQ.
  881. *
  882. * This function sends a message using IRQ. This is a non-blocking function, which returns
  883. * right away. When messages have been sent out, the send callback function is called.
  884. *
  885. * @param base FlexCAN peripheral base address.
  886. * @param handle FlexCAN handle pointer.
  887. * @param xfer FlexCAN Message Buffer transfer structure. See the #flexcan_mb_transfer_t.
  888. * @retval kStatus_Success Start Tx Message Buffer sending process successfully.
  889. * @retval kStatus_Fail Write Tx Message Buffer failed.
  890. * @retval kStatus_FLEXCAN_TxBusy Tx Message Buffer is in use.
  891. */
  892. status_t FLEXCAN_TransferSendNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *xfer);
  893. /*!
  894. * @brief Receives a message using IRQ.
  895. *
  896. * This function receives a message using IRQ. This is non-blocking function, which returns
  897. * right away. When the message has been received, the receive callback function is called.
  898. *
  899. * @param base FlexCAN peripheral base address.
  900. * @param handle FlexCAN handle pointer.
  901. * @param xfer FlexCAN Message Buffer transfer structure. See the #flexcan_mb_transfer_t.
  902. * @retval kStatus_Success - Start Rx Message Buffer receiving process successfully.
  903. * @retval kStatus_FLEXCAN_RxBusy - Rx Message Buffer is in use.
  904. */
  905. status_t FLEXCAN_TransferReceiveNonBlocking(CAN_Type *base, flexcan_handle_t *handle, flexcan_mb_transfer_t *xfer);
  906. /*!
  907. * @brief Receives a message from Rx FIFO using IRQ.
  908. *
  909. * This function receives a message using IRQ. This is a non-blocking function, which returns
  910. * right away. When all messages have been received, the receive callback function is called.
  911. *
  912. * @param base FlexCAN peripheral base address.
  913. * @param handle FlexCAN handle pointer.
  914. * @param xfer FlexCAN Rx FIFO transfer structure. See the @ref flexcan_fifo_transfer_t.
  915. * @retval kStatus_Success - Start Rx FIFO receiving process successfully.
  916. * @retval kStatus_FLEXCAN_RxFifoBusy - Rx FIFO is currently in use.
  917. */
  918. status_t FLEXCAN_TransferReceiveFifoNonBlocking(CAN_Type *base,
  919. flexcan_handle_t *handle,
  920. flexcan_fifo_transfer_t *xfer);
  921. /*!
  922. * @brief Aborts the interrupt driven message send process.
  923. *
  924. * This function aborts the interrupt driven message send process.
  925. *
  926. * @param base FlexCAN peripheral base address.
  927. * @param handle FlexCAN handle pointer.
  928. * @param mbIdx The FlexCAN Message Buffer index.
  929. */
  930. void FLEXCAN_TransferAbortSend(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx);
  931. /*!
  932. * @brief Aborts the interrupt driven message receive process.
  933. *
  934. * This function aborts the interrupt driven message receive process.
  935. *
  936. * @param base FlexCAN peripheral base address.
  937. * @param handle FlexCAN handle pointer.
  938. * @param mbIdx The FlexCAN Message Buffer index.
  939. */
  940. void FLEXCAN_TransferAbortReceive(CAN_Type *base, flexcan_handle_t *handle, uint8_t mbIdx);
  941. /*!
  942. * @brief Aborts the interrupt driven message receive from Rx FIFO process.
  943. *
  944. * This function aborts the interrupt driven message receive from Rx FIFO process.
  945. *
  946. * @param base FlexCAN peripheral base address.
  947. * @param handle FlexCAN handle pointer.
  948. */
  949. void FLEXCAN_TransferAbortReceiveFifo(CAN_Type *base, flexcan_handle_t *handle);
  950. /*!
  951. * @brief FlexCAN IRQ handle function.
  952. *
  953. * This function handles the FlexCAN Error, the Message Buffer, and the Rx FIFO IRQ request.
  954. *
  955. * @param base FlexCAN peripheral base address.
  956. * @param handle FlexCAN handle pointer.
  957. */
  958. void FLEXCAN_TransferHandleIRQ(CAN_Type *base, flexcan_handle_t *handle);
  959. /* @} */
  960. #if defined(__cplusplus)
  961. }
  962. #endif
  963. /*! @}*/
  964. #endif /* _FSL_FLEXCAN_H_ */