fsl_mcan.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_MCAN_H_
  31. #define _FSL_MCAN_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup mcan
  35. * @{
  36. */
  37. /******************************************************************************
  38. * Definitions
  39. *****************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief MCAN driver version 2.0.0. */
  43. #define MCAN_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  44. /*@}*/
  45. /*! @brief MCAN transfer status. */
  46. enum _mcan_status
  47. {
  48. kStatus_MCAN_TxBusy = MAKE_STATUS(kStatusGroup_MCAN, 0), /*!< Tx Buffer is Busy. */
  49. kStatus_MCAN_TxIdle = MAKE_STATUS(kStatusGroup_MCAN, 1), /*!< Tx Buffer is Idle. */
  50. kStatus_MCAN_RxBusy = MAKE_STATUS(kStatusGroup_MCAN, 2), /*!< Rx Buffer is Busy. */
  51. kStatus_MCAN_RxIdle = MAKE_STATUS(kStatusGroup_MCAN, 3), /*!< Rx Buffer is Idle. */
  52. kStatus_MCAN_RxFifo0New = MAKE_STATUS(kStatusGroup_MCAN, 4), /*!< New message written to Rx FIFO 0. */
  53. kStatus_MCAN_RxFifo0Idle = MAKE_STATUS(kStatusGroup_MCAN, 5), /*!< Rx FIFO 0 is Idle. */
  54. kStatus_MCAN_RxFifo0Watermark = MAKE_STATUS(kStatusGroup_MCAN, 6), /*!< Rx FIFO 0 fill level reached watermark. */
  55. kStatus_MCAN_RxFifo0Full = MAKE_STATUS(kStatusGroup_MCAN, 7), /*!< Rx FIFO 0 full. */
  56. kStatus_MCAN_RxFifo0Lost = MAKE_STATUS(kStatusGroup_MCAN, 8), /*!< Rx FIFO 0 message lost. */
  57. kStatus_MCAN_RxFifo1New = MAKE_STATUS(kStatusGroup_MCAN, 9), /*!< New message written to Rx FIFO 1. */
  58. kStatus_MCAN_RxFifo1Idle = MAKE_STATUS(kStatusGroup_MCAN, 10), /*!< Rx FIFO 1 is Idle. */
  59. kStatus_MCAN_RxFifo1Watermark = MAKE_STATUS(kStatusGroup_MCAN, 11), /*!< Rx FIFO 1 fill level reached watermark. */
  60. kStatus_MCAN_RxFifo1Full = MAKE_STATUS(kStatusGroup_MCAN, 12), /*!< Rx FIFO 1 full. */
  61. kStatus_MCAN_RxFifo1Lost = MAKE_STATUS(kStatusGroup_MCAN, 13), /*!< Rx FIFO 1 message lost. */
  62. kStatus_MCAN_RxFifo0Busy = MAKE_STATUS(kStatusGroup_MCAN, 14), /*!< Rx FIFO 0 is busy. */
  63. kStatus_MCAN_RxFifo1Busy = MAKE_STATUS(kStatusGroup_MCAN, 15), /*!< Rx FIFO 1 is busy. */
  64. kStatus_MCAN_ErrorStatus = MAKE_STATUS(kStatusGroup_MCAN, 16), /*!< MCAN Module Error and Status. */
  65. kStatus_MCAN_UnHandled = MAKE_STATUS(kStatusGroup_MCAN, 17), /*!< UnHadled Interrupt asserted. */
  66. };
  67. /*!
  68. * @brief MCAN status flags.
  69. *
  70. * This provides constants for the MCAN status flags for use in the MCAN functions.
  71. * Note: The CPU read action clears MCAN_ErrorFlag, therefore user need to
  72. * read MCAN_ErrorFlag and distinguish which error is occur using
  73. * @ref _mcan_error_flags enumerations.
  74. */
  75. enum _mcan_flags
  76. {
  77. kMCAN_AccesstoRsvdFlag = CAN_IR_ARA_MASK, /*!< CAN Synchronization Status. */
  78. kMCAN_ProtocolErrDIntFlag = CAN_IR_PED_MASK, /*!< Tx Warning Interrupt Flag. */
  79. kMCAN_ProtocolErrAIntFlag = CAN_IR_PEA_MASK, /*!< Rx Warning Interrupt Flag. */
  80. kMCAN_BusOffIntFlag = CAN_IR_BO_MASK, /*!< Tx Error Warning Status. */
  81. kMCAN_ErrorWarningIntFlag = CAN_IR_EW_MASK, /*!< Rx Error Warning Status. */
  82. kMCAN_ErrorPassiveIntFlag = CAN_IR_EP_MASK, /*!< Rx Error Warning Status. */
  83. };
  84. /*!
  85. * @brief MCAN Rx FIFO status flags.
  86. *
  87. * The MCAN Rx FIFO Status enumerations are used to determine the status of the
  88. * Rx FIFO.
  89. */
  90. enum _mcan_rx_fifo_flags
  91. {
  92. kMCAN_RxFifo0NewFlag = CAN_IR_RF0N_MASK, /*!< Rx FIFO 0 new message flag. */
  93. kMCAN_RxFifo0WatermarkFlag = CAN_IR_RF0W_MASK, /*!< Rx FIFO 0 watermark reached flag. */
  94. kMCAN_RxFifo0FullFlag = CAN_IR_RF0F_MASK, /*!< Rx FIFO 0 full flag. */
  95. kMCAN_RxFifo0LostFlag = CAN_IR_RF0L_MASK, /*!< Rx FIFO 0 message lost flag. */
  96. kMCAN_RxFifo1NewFlag = CAN_IR_RF1N_MASK, /*!< Rx FIFO 0 new message flag. */
  97. kMCAN_RxFifo1WatermarkFlag = CAN_IR_RF1W_MASK, /*!< Rx FIFO 0 watermark reached flag. */
  98. kMCAN_RxFifo1FullFlag = CAN_IR_RF1F_MASK, /*!< Rx FIFO 0 full flag. */
  99. kMCAN_RxFifo1LostFlag = CAN_IR_RF1L_MASK, /*!< Rx FIFO 0 message lost flag. */
  100. };
  101. /*!
  102. * @brief MCAN Tx status flags.
  103. *
  104. * The MCAN Tx Status enumerations are used to determine the status of the
  105. * Tx Buffer/Event FIFO.
  106. */
  107. enum _mcan_tx_flags
  108. {
  109. kMCAN_TxTransmitCompleteFlag = CAN_IR_TC_MASK, /*!< Transmission completed flag. */
  110. kMCAN_TxTransmitCancelFinishFlag = CAN_IR_TCF_MASK, /*!< Transmission cancellation finished flag. */
  111. kMCAN_TxEventFifoLostFlag = CAN_IR_TEFL_MASK, /*!< Tx Event FIFO element lost. */
  112. kMCAN_TxEventFifoFullFlag = CAN_IR_TEFF_MASK, /*!< Tx Event FIFO full. */
  113. kMCAN_TxEventFifoWatermarkFlag = CAN_IR_TEFW_MASK, /*!< Tx Event FIFO fill level reached watermark. */
  114. kMCAN_TxEventFifoNewFlag = CAN_IR_TEFN_MASK, /*!< Tx Handler wrote Tx Event FIFO element flag. */
  115. kMCAN_TxEventFifoEmptyFlag = CAN_IR_TFE_MASK, /*!< Tx FIFO empty flag. */
  116. };
  117. /*!
  118. * @brief MCAN interrupt configuration structure, default settings all disabled.
  119. *
  120. * This structure contains the settings for all of the MCAN Module interrupt configurations.
  121. */
  122. enum _mcan_interrupt_enable
  123. {
  124. kMCAN_BusOffInterruptEnable = CAN_IE_BOE_MASK, /*!< Bus Off interrupt. */
  125. kMCAN_ErrorInterruptEnable = CAN_IE_EPE_MASK, /*!< Error interrupt. */
  126. kMCAN_WarningInterruptEnable = CAN_IE_EWE_MASK, /*!< Rx Warning interrupt. */
  127. };
  128. /*! @brief MCAN frame format. */
  129. typedef enum _mcan_frame_idformat
  130. {
  131. kMCAN_FrameIDStandard = 0x0U, /*!< Standard frame format attribute. */
  132. kMCAN_FrameIDExtend = 0x1U, /*!< Extend frame format attribute. */
  133. } mcan_frame_idformat_t;
  134. /*! @brief MCAN frame type. */
  135. typedef enum _mcan_frame_type
  136. {
  137. kMCAN_FrameTypeData = 0x0U, /*!< Data frame type attribute. */
  138. kMCAN_FrameTypeRemote = 0x1U, /*!< Remote frame type attribute. */
  139. } mcan_frame_type_t;
  140. /*! @brief MCAN frame datafield size. */
  141. typedef enum _mcan_bytes_in_datafield
  142. {
  143. kMCAN_8ByteDatafield = 0x0U, /*!< 8 byte data field. */
  144. kMCAN_12ByteDatafield = 0x1U, /*!< 12 byte data field. */
  145. kMCAN_16ByteDatafield = 0x2U, /*!< 16 byte data field. */
  146. kMCAN_20ByteDatafield = 0x3U, /*!< 20 byte data field. */
  147. kMCAN_24ByteDatafield = 0x4U, /*!< 24 byte data field. */
  148. kMCAN_32ByteDatafield = 0x5U, /*!< 32 byte data field. */
  149. kMCAN_48ByteDatafield = 0x6U, /*!< 48 byte data field. */
  150. kMCAN_64ByteDatafield = 0x7U, /*!< 64 byte data field. */
  151. } mcan_bytes_in_datafield_t;
  152. #if defined(__CC_ARM)
  153. #pragma anon_unions
  154. #endif
  155. /*! @brief MCAN Tx Buffer structure. */
  156. typedef struct _mcan_tx_buffer_frame
  157. {
  158. struct
  159. {
  160. uint32_t id : 29; /*!< CAN Frame Identifier. */
  161. uint32_t rtr : 1; /*!< CAN Frame Type(DATA or REMOTE). */
  162. uint32_t xtd : 1; /*!< CAN Frame Type(STD or EXT). */
  163. uint32_t esi : 1; /*!< CAN Frame Error State Indicator. */
  164. };
  165. struct
  166. {
  167. uint32_t : 16;
  168. uint32_t dlc : 4; /*!< Data Length Code. */
  169. uint32_t brs : 1; /*!< Bit Rate Switch. */
  170. uint32_t fdf : 1; /*!< CAN FD format. */
  171. uint32_t : 1; /*!< Reserved. */
  172. uint32_t efc : 1; /*!< Event FIFO control. */
  173. uint32_t mm : 8; /*!< Message Marker. */
  174. };
  175. uint8_t *data;
  176. uint8_t size;
  177. } mcan_tx_buffer_frame_t;
  178. /*! @brief MCAN Rx FIFO/Buffer structure. */
  179. typedef struct _mcan_rx_buffer_frame
  180. {
  181. struct
  182. {
  183. uint32_t id : 29; /*!< CAN Frame Identifier. */
  184. uint32_t rtr : 1; /*!< CAN Frame Type(DATA or REMOTE). */
  185. uint32_t xtd : 1; /*!< CAN Frame Type(STD or EXT). */
  186. uint32_t esi : 1; /*!< CAN Frame Error State Indicator. */
  187. };
  188. struct
  189. {
  190. uint32_t rxts : 16; /*!< Rx Timestamp. */
  191. uint32_t dlc : 4; /*!< Data Length Code. */
  192. uint32_t brs : 1; /*!< Bit Rate Switch. */
  193. uint32_t fdf : 1; /*!< CAN FD format. */
  194. uint32_t : 2; /*!< Reserved. */
  195. uint32_t fidx : 7; /*!< Filter Index. */
  196. uint32_t anmf : 1; /*!< Accepted Non-matching Frame. */
  197. };
  198. uint8_t *data;
  199. uint8_t size;
  200. } mcan_rx_buffer_frame_t;
  201. /*! @brief MCAN Rx FIFO block number. */
  202. typedef enum _mcan_fifo_type
  203. {
  204. kMCAN_Fifo0 = 0x0U, /*!< CAN Rx FIFO 0. */
  205. kMCAN_Fifo1 = 0x1U, /*!< CAN Rx FIFO 1. */
  206. } mcan_fifo_type_t;
  207. /*! @brief MCAN FIFO Operation Mode. */
  208. typedef enum _mcan_fifo_opmode_config
  209. {
  210. kMCAN_FifoBlocking = 0, /*!< FIFO blocking mode. */
  211. kMCAN_FifoOverwrite = 1, /*!< FIFO overwrite mode. */
  212. } mcan_fifo_opmode_config_t;
  213. /*! @brief MCAN Tx FIFO/Queue Mode. */
  214. typedef enum _mcan_txmode_config
  215. {
  216. kMCAN_txFifo = 0, /*!< Tx FIFO operation. */
  217. kMCAN_txQueue = 1, /*!< Tx Queue operation. */
  218. } mcan_txmode_config_t;
  219. /*! @brief MCAN remote frames treatment. */
  220. typedef enum _mcan_remote_frame_config
  221. {
  222. kMCAN_filterFrame = 0, /*!< Filter remote frames. */
  223. kMCAN_rejectFrame = 1, /*!< Reject all remote frames. */
  224. } mcan_remote_frame_config_t;
  225. /*! @brief MCAN non-masking frames treatment. */
  226. typedef enum _mcan_nonmasking_frame_config
  227. {
  228. kMCAN_acceptinFifo0 = 0, /*!< Accept non-masking frames in Rx FIFO 0. */
  229. kMCAN_acceptinFifo1 = 1, /*!< Accept non-masking frames in Rx FIFO 1. */
  230. kMCAN_reject0 = 2, /*!< Reject non-masking frames. */
  231. kMCAN_reject1 = 3, /*!< Reject non-masking frames. */
  232. } mcan_nonmasking_frame_config_t;
  233. /*! @brief MCAN Filter Element Configuration. */
  234. typedef enum _mcan_fec_config
  235. {
  236. kMCAN_disable = 0, /*!< Disable filter element. */
  237. kMCAN_storeinFifo0 = 1, /*!< Store in Rx FIFO 0 if filter matches. */
  238. kMCAN_storeinFifo1 = 2, /*!< Store in Rx FIFO 1 if filter matches. */
  239. kMCAN_reject = 3, /*!< Reject ID if filter matches. */
  240. kMCAN_setprio = 4, /*!< Set priority if filter matches. */
  241. kMCAN_setpriofifo0 = 5, /*!< Set priority and store in FIFO 0 if filter matches. */
  242. kMCAN_setpriofifo1 = 6, /*!< Set priority and store in FIFO 1 if filter matches. */
  243. kMCAN_storeinbuffer = 7, /*!< Store into Rx Buffer or as debug message. */
  244. } mcan_fec_config_t;
  245. /*! @brief MCAN Rx FIFO configuration. */
  246. typedef struct _mcan_rx_fifo_config
  247. {
  248. uint32_t address; /*!< FIFOn start address. */
  249. uint32_t elementSize; /*!< FIFOn element number. */
  250. uint32_t watermark; /*!< FIFOn watermark level. */
  251. mcan_fifo_opmode_config_t opmode; /*!< FIFOn blocking/overwrite mode. */
  252. mcan_bytes_in_datafield_t datafieldSize; /*!< Data field size per frame, size>8 is for CANFD. */
  253. } mcan_rx_fifo_config_t;
  254. /*! @brief MCAN Rx Buffer configuration. */
  255. typedef struct _mcan_rx_buffer_config
  256. {
  257. uint32_t address; /*!< Rx Buffer start address. */
  258. mcan_bytes_in_datafield_t datafieldSize; /*!< Data field size per frame, size>8 is for CANFD. */
  259. } mcan_rx_buffer_config_t;
  260. /*! @brief MCAN Tx Event FIFO configuration. */
  261. typedef struct _mcan_tx_fifo_config
  262. {
  263. uint32_t address; /*!< Event fifo start address. */
  264. uint32_t elementSize; /*!< FIFOn element number. */
  265. uint32_t watermark; /*!< FIFOn watermark level. */
  266. } mcan_tx_fifo_config_t;
  267. /*! @brief MCAN Tx Buffer configuration. */
  268. typedef struct _mcan_tx_buffer_config
  269. {
  270. uint32_t address; /*!< Tx Buffers Start Address. */
  271. uint32_t dedicatedSize; /*!< Number of Dedicated Transmit Buffers. */
  272. uint32_t fqSize; /*!< Transmit FIFO/Queue Size. */
  273. mcan_txmode_config_t mode; /*!< Tx FIFO/Queue Mode.*/
  274. mcan_bytes_in_datafield_t datafieldSize; /*!< Data field size per frame, size>8 is for CANFD. */
  275. } mcan_tx_buffer_config_t;
  276. /*! @brief MCAN Filter Type. */
  277. typedef enum _mcan_std_filter_type
  278. {
  279. kMCAN_range = 0, /*!< Range filter from SFID1 to SFID2. */
  280. kMCAN_dual = 1, /*!< Dual ID filter for SFID1 or SFID2. */
  281. kMCAN_classic = 2, /*!< Classic filter: SFID1 = filter, SFID2 = mask. */
  282. kMCAN_disableORrange2 = 3, /*!< Filter element disabled for standard filter
  283. or Range filter, XIDAM mask not applied for extended filter. */
  284. } mcan_filter_type_t;
  285. /*! @brief MCAN Standard Message ID Filter Element. */
  286. typedef struct _mcan_std_filter_element_config
  287. {
  288. uint32_t sfid2 : 11; /*!< Standard Filter ID 2. */
  289. uint32_t : 5; /*!< Reserved. */
  290. uint32_t sfid1 : 11; /*!< Standard Filter ID 1. */
  291. mcan_fec_config_t sfec : 3; /*!< Standard Filter Element Configuration. */
  292. mcan_filter_type_t sft : 2; /*!< Standard Filter Type/ */
  293. } mcan_std_filter_element_config_t;
  294. /*! @brief MCAN Extended Message ID Filter Element. */
  295. typedef struct _mcan_ext_filter_element_config
  296. {
  297. uint32_t efid1 : 29; /*!< Extended Filter ID 1. */
  298. mcan_fec_config_t efec : 3; /*!< Extended Filter Element Configuration. */
  299. uint32_t efid2 : 29; /*!< Extended Filter ID 2. */
  300. uint32_t : 1; /*!< Reserved. */
  301. mcan_filter_type_t eft : 2; /*!< Extended Filter Type. */
  302. } mcan_ext_filter_element_config_t;
  303. /*! @brief MCAN Rx filter configuration. */
  304. typedef struct _mcan_frame_filter_config
  305. {
  306. uint32_t address; /*!< Filter start address. */
  307. uint32_t listSize; /*!< Filter list size. */
  308. mcan_frame_idformat_t idFormat; /*!< Frame format. */
  309. mcan_remote_frame_config_t remFrame; /*!< Remote frame treatment. */
  310. mcan_nonmasking_frame_config_t nmFrame; /*!< Non-masking frame treatment. */
  311. } mcan_frame_filter_config_t;
  312. /*! @brief MCAN module configuration structure. */
  313. typedef struct _mcan_config
  314. {
  315. uint32_t baudRateA; /*!< Baud rate of Arbitration phase in bps. */
  316. uint32_t baudRateD; /*!< Baud rate of Data phase in bps. */
  317. bool enableCanfdNormal; /*!< Enable or Disable CANFD normal. */
  318. bool enableCanfdSwitch; /*!< Enable or Disable CANFD with baudrate switch. */
  319. bool enableLoopBackInt; /*!< Enable or Disable Internal Back. */
  320. bool enableLoopBackExt; /*!< Enable or Disable External Loop Back. */
  321. bool enableBusMon; /*!< Enable or Disable Bus Monitoring Mode. */
  322. } mcan_config_t;
  323. /*! @brief MCAN protocol timing characteristic configuration structure. */
  324. typedef struct _mcan_timing_config
  325. {
  326. uint16_t preDivider; /*!< Clock Pre-scaler Division Factor. */
  327. uint8_t rJumpwidth; /*!< Re-sync Jump Width. */
  328. uint8_t seg1; /*!< Data Time Segment 1. */
  329. uint8_t seg2; /*!< Data Time Segment 2. */
  330. } mcan_timing_config_t;
  331. /*! @brief MCAN Buffer transfer. */
  332. typedef struct _mcan_buffer_transfer
  333. {
  334. mcan_tx_buffer_frame_t *frame; /*!< The buffer of CAN Message to be transfer. */
  335. uint8_t bufferIdx; /*!< The index of Message buffer used to transfer Message. */
  336. } mcan_buffer_transfer_t;
  337. /*! @brief MCAN Rx FIFO transfer. */
  338. typedef struct _mcan_fifo_transfer
  339. {
  340. mcan_rx_buffer_frame_t *frame; /*!< The buffer of CAN Message to be received from Rx FIFO. */
  341. } mcan_fifo_transfer_t;
  342. /*! @brief MCAN handle structure definition. */
  343. typedef struct _mcan_handle mcan_handle_t;
  344. /*! @brief MCAN transfer callback function.
  345. *
  346. * The MCAN transfer callback returns a value from the underlying layer.
  347. * If the status equals to kStatus_MCAN_ErrorStatus, the result parameter is the Content of
  348. * MCAN status register which can be used to get the working status(or error status) of MCAN module.
  349. * If the status equals to other MCAN Message Buffer transfer status, the result is the index of
  350. * Message Buffer that generate transfer event.
  351. * If the status equals to other MCAN Message Buffer transfer status, the result is meaningless and should be
  352. * Ignored.
  353. */
  354. typedef void (*mcan_transfer_callback_t)(
  355. CAN_Type *base, mcan_handle_t *handle, status_t status, uint32_t result, void *userData);
  356. /*! @brief MCAN handle structure. */
  357. struct _mcan_handle
  358. {
  359. mcan_transfer_callback_t callback; /*!< Callback function. */
  360. void *userData; /*!< MCAN callback function parameter.*/
  361. mcan_tx_buffer_frame_t *volatile bufferFrameBuf[64]; /*!< The buffer for received data from Buffers. */
  362. mcan_rx_buffer_frame_t *volatile rxFifoFrameBuf; /*!< The buffer for received data from Rx FIFO. */
  363. volatile uint8_t txbufferIdx; /*!< Message Buffer transfer state. */
  364. volatile uint8_t bufferState[64]; /*!< Message Buffer transfer state. */
  365. volatile uint8_t rxFifoState; /*!< Rx FIFO transfer state. */
  366. };
  367. /******************************************************************************
  368. * API
  369. *****************************************************************************/
  370. #if defined(__cplusplus)
  371. extern "C" {
  372. #endif
  373. /*!
  374. * @name Initialization and deinitialization
  375. * @{
  376. */
  377. /*!
  378. * @brief Initializes an MCAN instance.
  379. *
  380. * This function initializes the MCAN module with user-defined settings.
  381. * This example shows how to set up the mcan_config_t parameters and how
  382. * to call the MCAN_Init function by passing in these parameters.
  383. * @code
  384. * mcan_config_t config;
  385. * config->baudRateA = 500000U;
  386. * config->baudRateD = 500000U;
  387. * config->enableCanfdNormal = false;
  388. * config->enableCanfdSwitch = false;
  389. * config->enableLoopBackInt = false;
  390. * config->enableLoopBackExt = false;
  391. * config->enableBusMon = false;
  392. * MCAN_Init(CANFD0, &config, 8000000UL);
  393. * @endcode
  394. *
  395. * @param base MCAN peripheral base address.
  396. * @param config Pointer to the user-defined configuration structure.
  397. * @param sourceClock_Hz MCAN Protocol Engine clock source frequency in Hz.
  398. */
  399. void MCAN_Init(CAN_Type *base, const mcan_config_t *config, uint32_t sourceClock_Hz);
  400. /*!
  401. * @brief Deinitializes an MCAN instance.
  402. *
  403. * This function deinitializes the MCAN module.
  404. *
  405. * @param base MCAN peripheral base address.
  406. */
  407. void MCAN_Deinit(CAN_Type *base);
  408. /*!
  409. * @brief Gets the default configuration structure.
  410. *
  411. * This function initializes the MCAN configuration structure to default values. The default
  412. * values are as follows.
  413. * config->baudRateA = 500000U;
  414. * config->baudRateD = 500000U;
  415. * config->enableCanfdNormal = false;
  416. * config->enableCanfdSwitch = false;
  417. * config->enableLoopBackInt = false;
  418. * config->enableLoopBackExt = false;
  419. * config->enableBusMon = false;
  420. *
  421. * @param config Pointer to the MCAN configuration structure.
  422. */
  423. void MCAN_GetDefaultConfig(mcan_config_t *config);
  424. /*!
  425. * @brief MCAN enters normal mode.
  426. *
  427. * After initialization, INIT bit in CCCR register must be cleared to enter
  428. * normal mode thus synchronizes to the CAN bus and ready for communication.
  429. *
  430. * @param base MCAN peripheral base address.
  431. */
  432. void MCAN_EnterNormalMode(CAN_Type *base);
  433. /*!
  434. * @name Configuration.
  435. * @{
  436. */
  437. /*!
  438. * @brief Sets the MCAN Message RAM base address.
  439. *
  440. * This function sets the Message RAM base address.
  441. *
  442. * @param base MCAN peripheral base address.
  443. * @param value Desired Message RAM base.
  444. */
  445. static inline void MCAN_SetMsgRAMBase(CAN_Type *base, uint32_t value)
  446. {
  447. assert((value >= 0x20000000U) && (value <= 0x20027FFFU));
  448. base->MRBA = CAN_MRBA_BA(value);
  449. }
  450. /*!
  451. * @brief Gets the MCAN Message RAM base address.
  452. *
  453. * This function gets the Message RAM base address.
  454. *
  455. * @param base MCAN peripheral base address.
  456. * @return Message RAM base address.
  457. */
  458. static inline uint32_t MCAN_GetMsgRAMBase(CAN_Type *base)
  459. {
  460. return base->MRBA;
  461. }
  462. /*!
  463. * @brief Sets the MCAN protocol arbitration phase timing characteristic.
  464. *
  465. * This function gives user settings to CAN bus timing characteristic.
  466. * The function is for an experienced user. For less experienced users, call
  467. * the MCAN_Init() and fill the baud rate field with a desired value.
  468. * This provides the default arbitration phase timing characteristics.
  469. *
  470. * Note that calling MCAN_SetArbitrationTimingConfig() overrides the baud rate
  471. * set in MCAN_Init().
  472. *
  473. * @param base MCAN peripheral base address.
  474. * @param config Pointer to the timing configuration structure.
  475. */
  476. void MCAN_SetArbitrationTimingConfig(CAN_Type *base, const mcan_timing_config_t *config);
  477. #if (defined(FSL_FEATURE_CAN_SUPPORT_CANFD) && FSL_FEATURE_CAN_SUPPORT_CANFD)
  478. /*!
  479. * @brief Sets the MCAN protocol data phase timing characteristic.
  480. *
  481. * This function gives user settings to CAN bus timing characteristic.
  482. * The function is for an experienced user. For less experienced users, call
  483. * the MCAN_Init() and fill the baud rate field with a desired value.
  484. * This provides the default data phase timing characteristics.
  485. *
  486. * Note that calling MCAN_SetArbitrationTimingConfig() overrides the baud rate
  487. * set in MCAN_Init().
  488. *
  489. * @param base MCAN peripheral base address.
  490. * @param config Pointer to the timing configuration structure.
  491. */
  492. void MCAN_SetDataTimingConfig(CAN_Type *base, const mcan_timing_config_t *config);
  493. #endif /* FSL_FEATURE_CAN_SUPPORT_CANFD */
  494. /*!
  495. * @brief Configures an MCAN receive fifo 0 buffer.
  496. *
  497. * This function sets start address, element size, watermark, operation mode
  498. * and datafield size of the recieve fifo 0.
  499. *
  500. * @param base MCAN peripheral base address.
  501. * @param config The receive fifo 0 configuration structure.
  502. */
  503. void MCAN_SetRxFifo0Config(CAN_Type *base, const mcan_rx_fifo_config_t *config);
  504. /*!
  505. * @brief Configures an MCAN receive fifo 1 buffer.
  506. *
  507. * This function sets start address, element size, watermark, operation mode
  508. * and datafield size of the recieve fifo 1.
  509. *
  510. * @param base MCAN peripheral base address.
  511. * @param config The receive fifo 1 configuration structure.
  512. */
  513. void MCAN_SetRxFifo1Config(CAN_Type *base, const mcan_rx_fifo_config_t *config);
  514. /*!
  515. * @brief Configures an MCAN receive buffer.
  516. *
  517. * This function sets start address and datafield size of the recieve buffer.
  518. *
  519. * @param base MCAN peripheral base address.
  520. * @param config The receive buffer configuration structure.
  521. */
  522. void MCAN_SetRxBufferConfig(CAN_Type *base, const mcan_rx_buffer_config_t *config);
  523. /*!
  524. * @brief Configures an MCAN transmit event fifo.
  525. *
  526. * This function sets start address, element size, watermark of the transmit event fifo.
  527. *
  528. * @param base MCAN peripheral base address.
  529. * @param config The transmit event fifo configuration structure.
  530. */
  531. void MCAN_SetTxEventfifoConfig(CAN_Type *base, const mcan_tx_fifo_config_t *config);
  532. /*!
  533. * @brief Configures an MCAN transmit buffer.
  534. *
  535. * This function sets start address, element size, fifo/queue mode and datafield
  536. * size of the transmit buffer.
  537. *
  538. * @param base MCAN peripheral base address.
  539. * @param config The transmit buffer configuration structure.
  540. */
  541. void MCAN_SetTxBufferConfig(CAN_Type *base, const mcan_tx_buffer_config_t *config);
  542. /*!
  543. * @brief Set filter configuration.
  544. *
  545. * This function sets remote and non masking frames in global filter configuration,
  546. * also the start address, list size in standard/extended ID filter configuration.
  547. *
  548. * @param base MCAN peripheral base address.
  549. * @param config The MCAN filter configuration.
  550. */
  551. void MCAN_SetFilterConfig(CAN_Type *base, const mcan_frame_filter_config_t *config);
  552. /*!
  553. * @brief Set filter configuration.
  554. *
  555. * This function sets remote and non masking frames in global filter configuration,
  556. * also the start address, list size in standard/extended ID filter configuration.
  557. *
  558. * @param base MCAN peripheral base address.
  559. * @param config The MCAN filter configuration.
  560. */
  561. void MCAN_SetSTDFilterElement(CAN_Type *base,
  562. const mcan_frame_filter_config_t *config,
  563. const mcan_std_filter_element_config_t *filter,
  564. uint8_t idx);
  565. /*!
  566. * @brief Set filter configuration.
  567. *
  568. * This function sets remote and non masking frames in global filter configuration,
  569. * also the start address, list size in standard/extended ID filter configuration.
  570. *
  571. * @param base MCAN peripheral base address.
  572. * @param config The MCAN filter configuration.
  573. */
  574. void MCAN_SetEXTFilterElement(CAN_Type *base,
  575. const mcan_frame_filter_config_t *config,
  576. const mcan_ext_filter_element_config_t *filter,
  577. uint8_t idx);
  578. /*!
  579. * @name Status
  580. * @{
  581. */
  582. /*!
  583. * @brief Gets the MCAN module interrupt flags.
  584. *
  585. * This function gets all MCAN interrupt status flags.
  586. *
  587. * @param base MCAN peripheral base address.
  588. * @param mask The ORed MCAN interrupt mask.
  589. * @return MCAN status flags which are ORed.
  590. */
  591. static inline uint32_t MCAN_GetStatusFlag(CAN_Type *base, uint32_t mask)
  592. {
  593. return (bool)(base->IR & mask);
  594. }
  595. /*!
  596. * @brief Clears the MCAN module interrupt flags.
  597. *
  598. * This function clears MCAN interrupt status flags.
  599. *
  600. * @param base MCAN peripheral base address.
  601. * @param mask The ORed MCAN interrupt mask.
  602. */
  603. static inline void MCAN_ClearStatusFlag(CAN_Type *base, uint32_t mask)
  604. {
  605. /* Write 1 to clear status flag. */
  606. base->IR |= mask;
  607. }
  608. /*!
  609. * @brief Gets the new data flag of specific Rx Buffer.
  610. *
  611. * This function gets new data flag of specific Rx Buffer.
  612. *
  613. * @param base MCAN peripheral base address.
  614. * @param idx Rx Buffer index.
  615. * @return Rx Buffer new data status flag.
  616. */
  617. static inline bool MCAN_GetRxBufferStatusFlag(CAN_Type *base, uint8_t idx)
  618. {
  619. assert(idx <= 63U);
  620. if (idx <= 31U)
  621. {
  622. return (bool)(base->NDAT1 & (1U << idx));
  623. }
  624. else
  625. {
  626. return (bool)(base->NDAT2 & (1U << (idx - 31U)));
  627. }
  628. }
  629. /*!
  630. * @brief Clears the new data flag of specific Rx Buffer.
  631. *
  632. * This function clears new data flag of specific Rx Buffer.
  633. *
  634. * @param base MCAN peripheral base address.
  635. * @param idx Rx Buffer index.
  636. */
  637. static inline void MCAN_ClearRxBufferStatusFlag(CAN_Type *base, uint8_t idx)
  638. {
  639. assert(idx <= 63U);
  640. if (idx <= 31U)
  641. {
  642. base->NDAT1 &= ~(1U << idx);
  643. }
  644. else
  645. {
  646. base->NDAT2 &= ~(1U << (idx - 31U));
  647. }
  648. }
  649. /* @} */
  650. /*!
  651. * @name Interrupts
  652. * @{
  653. */
  654. /*!
  655. * @brief Enables MCAN interrupts according to the provided interrupt line and mask.
  656. *
  657. * This function enables the MCAN interrupts according to the provided interrupt line and mask.
  658. * The mask is a logical OR of enumeration members.
  659. *
  660. * @param base MCAN peripheral base address.
  661. * @param line Interrupt line number, 0 or 1.
  662. * @param mask The interrupts to enable.
  663. */
  664. static inline void MCAN_EnableInterrupts(CAN_Type *base, uint32_t line, uint32_t mask)
  665. {
  666. base->ILE |= (1U << line);
  667. if (0 == line)
  668. {
  669. base->ILS &= ~mask;
  670. }
  671. else
  672. {
  673. base->ILS |= mask;
  674. }
  675. base->IE |= mask;
  676. }
  677. /*!
  678. * @brief Enables MCAN Tx Buffer interrupts according to the provided index.
  679. *
  680. * This function enables the MCAN Tx Buffer interrupts.
  681. *
  682. * @param base MCAN peripheral base address.
  683. * @param idx Tx Buffer index.
  684. */
  685. static inline void MCAN_EnableTransmitBufferInterrupts(CAN_Type *base, uint8_t idx)
  686. {
  687. base->TXBTIE |= (uint32_t)(1U << idx);
  688. }
  689. /*!
  690. * @brief Disables MCAN Tx Buffer interrupts according to the provided index.
  691. *
  692. * This function disables the MCAN Tx Buffer interrupts.
  693. *
  694. * @param base MCAN peripheral base address.
  695. * @param idx Tx Buffer index.
  696. */
  697. static inline void MCAN_DisableTransmitBufferInterrupts(CAN_Type *base, uint8_t idx)
  698. {
  699. base->TXBTIE &= (uint32_t)(~(1U << idx));
  700. }
  701. /*!
  702. * @brief Disables MCAN interrupts according to the provided mask.
  703. *
  704. * This function disables the MCAN interrupts according to the provided mask.
  705. * The mask is a logical OR of enumeration members.
  706. *
  707. * @param base MCAN peripheral base address.
  708. * @param mask The interrupts to disable.
  709. */
  710. static inline void MCAN_DisableInterrupts(CAN_Type *base, uint32_t mask)
  711. {
  712. base->IE &= ~mask;
  713. }
  714. /* @} */
  715. /*!
  716. * @name Bus Operations
  717. * @{
  718. */
  719. /*!
  720. * @brief Writes an MCAN Message to the Transmit Buffer.
  721. *
  722. * This function writes a CAN Message to the specified Transmit Message Buffer
  723. * and changes the Message Buffer state to start CAN Message transmit. After
  724. * that the function returns immediately.
  725. *
  726. * @param base MCAN peripheral base address.
  727. * @param idx The MCAN Tx Buffer index.
  728. * @param txFrame Pointer to CAN message frame to be sent.
  729. */
  730. status_t MCAN_WriteTxBuffer(CAN_Type *base, uint8_t idx, const mcan_tx_buffer_frame_t *txFrame);
  731. /*!
  732. * @brief Reads an MCAN Message from Rx FIFO.
  733. *
  734. * This function reads a CAN message from the Rx FIFO in the Message RAM.
  735. *
  736. * @param base MCAN peripheral base address.
  737. * @param fifoBlock Rx FIFO block 0 or 1.
  738. * @param rxFrame Pointer to CAN message frame structure for reception.
  739. * @retval kStatus_Success - Read Message from Rx FIFO successfully.
  740. */
  741. status_t MCAN_ReadRxFifo(CAN_Type *base, uint8_t fifoBlock, mcan_rx_buffer_frame_t *rxFrame);
  742. /* @} */
  743. /*!
  744. * @name Transactional
  745. * @{
  746. */
  747. /*!
  748. * @brief Tx Buffer add request to send message out.
  749. *
  750. * This function add sending request to corresponding Tx Buffer.
  751. *
  752. * @param base MCAN peripheral base address.
  753. * @param idx Tx Buffer index.
  754. */
  755. static inline void MCAN_TransmitAddRequest(CAN_Type *base, uint8_t idx)
  756. {
  757. base->TXBAR |= (uint32_t)(1U << idx);
  758. }
  759. /*!
  760. * @brief Tx Buffer cancel sending request.
  761. *
  762. * This function clears Tx buffer request pending bit.
  763. *
  764. * @param base MCAN peripheral base address.
  765. * @param idx Tx Buffer index.
  766. */
  767. static inline void MCAN_TransmitCancelRequest(CAN_Type *base, uint8_t idx)
  768. {
  769. base->TXBCR |= (uint32_t)(1U << idx);
  770. }
  771. /*!
  772. * @brief Performs a polling send transaction on the CAN bus.
  773. *
  774. * Note that a transfer handle does not need to be created before calling this API.
  775. *
  776. * @param base MCAN peripheral base pointer.
  777. * @param idx The MCAN buffer index.
  778. * @param txFrame Pointer to CAN message frame to be sent.
  779. * @retval kStatus_Success - Write Tx Message Buffer Successfully.
  780. * @retval kStatus_Fail - Tx Message Buffer is currently in use.
  781. */
  782. status_t MCAN_TransferSendBlocking(CAN_Type *base, uint8_t idx, mcan_tx_buffer_frame_t *txFrame);
  783. /*!
  784. * @brief Performs a polling receive transaction from Rx FIFO on the CAN bus.
  785. *
  786. * Note that a transfer handle does not need to be created before calling this API.
  787. *
  788. * @param base MCAN peripheral base pointer.
  789. * @param fifoBlock Rx FIFO block, 0 or 1.
  790. * @param rxFrame Pointer to CAN message frame structure for reception.
  791. * @retval kStatus_Success - Read Message from Rx FIFO successfully.
  792. * @retval kStatus_Fail - No new message in Rx FIFO.
  793. */
  794. status_t MCAN_TransferReceiveFifoBlocking(CAN_Type *base, uint8_t fifoBlock, mcan_rx_buffer_frame_t *rxFrame);
  795. /*!
  796. * @brief Initializes the MCAN handle.
  797. *
  798. * This function initializes the MCAN handle, which can be used for other MCAN
  799. * transactional APIs. Usually, for a specified MCAN instance,
  800. * call this API once to get the initialized handle.
  801. *
  802. * @param base MCAN peripheral base address.
  803. * @param handle MCAN handle pointer.
  804. * @param callback The callback function.
  805. * @param userData The parameter of the callback function.
  806. */
  807. void MCAN_TransferCreateHandle(CAN_Type *base,
  808. mcan_handle_t *handle,
  809. mcan_transfer_callback_t callback,
  810. void *userData);
  811. /*!
  812. * @brief Sends a message using IRQ.
  813. *
  814. * This function sends a message using IRQ. This is a non-blocking function, which returns
  815. * right away. When messages have been sent out, the send callback function is called.
  816. *
  817. * @param base MCAN peripheral base address.
  818. * @param handle MCAN handle pointer.
  819. * @param xfer MCAN Buffer transfer structure. See the #mcan_buffer_transfer_t.
  820. * @retval kStatus_Success Start Tx Buffer sending process successfully.
  821. * @retval kStatus_Fail Write Tx Buffer failed.
  822. * @retval kStatus_MCAN_TxBusy Tx Buffer is in use.
  823. */
  824. status_t MCAN_TransferSendNonBlocking(CAN_Type *base, mcan_handle_t *handle, mcan_buffer_transfer_t *xfer);
  825. /*!
  826. * @brief Receives a message from Rx FIFO using IRQ.
  827. *
  828. * This function receives a message using IRQ. This is a non-blocking function, which returns
  829. * right away. When all messages have been received, the receive callback function is called.
  830. *
  831. * @param base MCAN peripheral base address.
  832. * @param handle MCAN handle pointer.
  833. * @param fifoBlock Rx FIFO block, 0 or 1.
  834. * @param xfer MCAN Rx FIFO transfer structure. See the @ref mcan_fifo_transfer_t.
  835. * @retval kStatus_Success - Start Rx FIFO receiving process successfully.
  836. * @retval kStatus_MCAN_RxFifo0Busy - Rx FIFO 0 is currently in use.
  837. * @retval kStatus_MCAN_RxFifo1Busy - Rx FIFO 1 is currently in use.
  838. */
  839. status_t MCAN_TransferReceiveFifoNonBlocking(CAN_Type *base,
  840. uint8_t fifoBlock,
  841. mcan_handle_t *handle,
  842. mcan_fifo_transfer_t *xfer);
  843. /*!
  844. * @brief Aborts the interrupt driven message send process.
  845. *
  846. * This function aborts the interrupt driven message send process.
  847. *
  848. * @param base MCAN peripheral base address.
  849. * @param handle MCAN handle pointer.
  850. * @param bufferIdx The MCAN Buffer index.
  851. */
  852. void MCAN_TransferAbortSend(CAN_Type *base, mcan_handle_t *handle, uint8_t bufferIdx);
  853. /*!
  854. * @brief Aborts the interrupt driven message receive from Rx FIFO process.
  855. *
  856. * This function aborts the interrupt driven message receive from Rx FIFO process.
  857. *
  858. * @param base MCAN peripheral base address.
  859. * @param fifoBlock MCAN Fifo block, 0 or 1.
  860. * @param handle MCAN handle pointer.
  861. */
  862. void MCAN_TransferAbortReceiveFifo(CAN_Type *base, uint8_t fifoBlock, mcan_handle_t *handle);
  863. /*!
  864. * @brief MCAN IRQ handle function.
  865. *
  866. * This function handles the MCAN Error, the Buffer, and the Rx FIFO IRQ request.
  867. *
  868. * @param base MCAN peripheral base address.
  869. * @param handle MCAN handle pointer.
  870. */
  871. void MCAN_TransferHandleIRQ(CAN_Type *base, mcan_handle_t *handle);
  872. /* @} */
  873. #if defined(__cplusplus)
  874. }
  875. #endif
  876. /*! @}*/
  877. #endif /* _FSL_MCAN_H_ */