hpm_mcan_drv.h 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. /*
  2. * Copyright (c) 2023-2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_MCAN_DRV_H
  8. #define HPM_MCAN_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_mcan_regs.h"
  11. #include "hpm_mcan_soc.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /**
  16. * @brief MCAN driver APIs
  17. * @defgroup mcan_interface MCAN driver APIs
  18. * @ingroup mcan_interfaces
  19. * @{
  20. *
  21. */
  22. enum {
  23. status_mcan_filter_index_out_of_range = MAKE_STATUS(status_group_mcan, 0),
  24. status_mcan_rxfifo_empty = MAKE_STATUS(status_group_mcan, 1),
  25. status_mcan_rxfifo_full = MAKE_STATUS(status_group_mcan, 2),
  26. status_mcan_txbuf_full = MAKE_STATUS(status_group_mcan, 3),
  27. status_mcan_txfifo_full = MAKE_STATUS(status_group_mcan, 4),
  28. status_mcan_rxfifo0_busy = MAKE_STATUS(status_group_mcan, 5),
  29. status_mcan_rxfifo1_busy = MAKE_STATUS(status_group_mcan, 6),
  30. status_mcan_txbuf_index_out_of_range = MAKE_STATUS(status_group_mcan, 7),
  31. status_mcan_rxbuf_index_out_of_range = MAKE_STATUS(status_group_mcan, 8),
  32. status_mcan_rxbuf_empty = MAKE_STATUS(status_group_mcan, 9),
  33. status_mcan_tx_evt_fifo_empty = MAKE_STATUS(status_group_mcan, 10),
  34. status_mcan_timestamp_not_exist = MAKE_STATUS(status_group_mcan, 11),
  35. status_mcan_ram_out_of_range = MAKE_STATUS(status_group_mcan, 12),
  36. status_mcan_timeout = MAKE_STATUS(status_group_mcan, 13),
  37. status_mcan_invalid_bit_timing = MAKE_STATUS(status_group_mcan, 14),
  38. };
  39. /**
  40. * @brief CAN Interrupt Mask
  41. */
  42. #define MCAN_INT_ACCESS_TO_RESERVED_ADDR MCAN_IR_ARA_MASK /*!< Access to Reserved Address */
  43. #define MCAN_INT_PROTOCOL_ERR_IN_DATA_PHASE MCAN_IR_PED_MASK /*!< Protocol Error Happened at Data Phase */
  44. #define MCAN_INT_PROTOCOL_ERR_IN_ARB_PHASE MCAN_IR_PEA_MASK /*!< Protocol Error Happened at Arbitration Phase */
  45. #define MCAN_INT_WATCHDOG_INT MCAN_IR_WDI_MASK /*!< Watchdog interrupt */
  46. #define MCAN_INT_BUS_OFF_STATUS MCAN_IR_BO_MASK /*!< Bus-off State Change */
  47. #define MCAN_INT_WARNING_STATUS MCAN_IR_EW_MASK /*!< Error Warning State Change */
  48. #define MCAN_INT_ERROR_PASSIVE MCAN_IR_EP_MASK /*!< Error Passive State Change */
  49. #define MCAN_INT_ERROR_LOGGING_OVERFLOW MCAN_IR_ELO_MASK /*!< Error Logging Overflow */
  50. #define MCAN_INT_BIT_ERROR_UNCORRECTED MCAN_IR_BEU_MASK /*!< Bit Error was not corrected */
  51. #define MCAN_INT_BIT_ERROR_CORRECTED MCAN_IR_BEC_MASK /*!< Bit Error was corrected */
  52. #define MCAN_INT_MSG_STORE_TO_RXBUF MCAN_IR_DRX_MASK /*!< Message was stored to RX Buffer */
  53. #define MCAN_INT_TIMEOUT_OCCURRED MCAN_IR_TOO_MASK /*!< Timeout Interrupt */
  54. #define MCAN_INT_MSG_RAM_ACCESS_FAILURE MCAN_IR_MRAF_MASK /*!< Message RAM Access Failure */
  55. #define MCAN_INT_TIMESTAMP_WRAPAROUND MCAN_IR_TSW_MASK /*!< Timestamp Counter Wrap Around */
  56. #define MCAN_INT_TX_EVT_FIFO_EVT_LOST MCAN_IR_TEFL_MASK /*!< TX Event FIFO Element Lost */
  57. #define MCAN_INT_TX_EVT_FIFO_FULL MCAN_IR_TEFF_MASK /*!< TX Event FIFO Full */
  58. #define MCAN_INT_TX_EVT_FIFO_WMK_REACHED MCAN_IR_TEFW_MASK /*!< TX Event FIFO Watermark Reached */
  59. #define MCAN_INT_TX_EVT_FIFO_NEW_ENTRY MCAN_IR_TEFN_MASK /*!< TX Event FIFO New Entry */
  60. #define MCAN_INT_TXFIFO_EMPTY MCAN_IR_TFE_MASK /*!< TX FIFO Empty */
  61. #define MCAN_INT_TX_CANCEL_FINISHED MCAN_IR_TCF_MASK /*!< Transmission Cancellation Finished */
  62. #define MCAN_INT_TX_COMPLETED MCAN_IR_TC_MASK /*!< Transmission Completed */
  63. #define MCAN_INT_HIGH_PRIORITY_MSG MCAN_IR_HPM_MASK /*!< High Priority Message */
  64. #define MCAN_INT_RXFIFO1_MSG_LOST MCAN_IR_RF1L_MASK /*!< RX FIFO0 Message Lost */
  65. #define MCAN_INT_RXFIFO1_FULL MCAN_IR_RF1F_MASK /*!< RX FIFO1 Full */
  66. #define MCAN_INT_RXFIFO1_WMK_REACHED MCAN_IR_RF1W_MASK /*!< RX FIFO1 Watermark Reached */
  67. #define MCAN_INT_RXFIFO1_NEW_MSG MCAN_IR_RF1N_MASK /*!< RX FIFO1 New Message */
  68. #define MCAN_INT_RXFIFO0_MSG_LOST MCAN_IR_RF0L_MASK /*!< RX FIFO0 Message Lost */
  69. #define MCAN_INT_RXFIFO0_FULL MCAN_IR_RF0F_MASK /*!< RX FIFO0 Full */
  70. #define MCAN_INT_RXFIFO0_WMK_REACHED MCAN_IR_RF0W_MASK /*!< RX FIFO0 Watermark Reached */
  71. #define MCAN_INT_RXFIFO0_NEW_MSG MCAN_IR_RF0N_MASK /*!< RX FIFO0 New Message */
  72. /**
  73. * @brief MCAN Receive Event Flags
  74. */
  75. #define MCAN_EVENT_RECEIVE (MCAN_INT_RXFIFO0_NEW_MSG | MCAN_INT_RXFIFO1_NEW_MSG | MCAN_INT_MSG_STORE_TO_RXBUF)
  76. /**
  77. * @brief MCAN Transmit Event Flags
  78. */
  79. #define MCAN_EVENT_TRANSMIT (MCAN_INT_TX_COMPLETED)
  80. /**
  81. * @brief MCAN Error Event Flags
  82. */
  83. #define MCAN_EVENT_ERROR (MCAN_INT_BUS_OFF_STATUS | MCAN_INT_WARNING_STATUS \
  84. | MCAN_INT_ERROR_PASSIVE | MCAN_INT_BIT_ERROR_UNCORRECTED \
  85. | MCAN_INT_PROTOCOL_ERR_IN_DATA_PHASE | MCAN_INT_PROTOCOL_ERR_IN_ARB_PHASE)
  86. /**
  87. * @brief Maximum Transmission Retry Count
  88. */
  89. #define MCAN_TX_RETRY_COUNT_MAX (10000000UL)
  90. /**
  91. * @brief Maximum Receive Wait Retry Count
  92. */
  93. #define MCAN_RX_RETRY_COUNT_MAX (80000000UL)
  94. /**
  95. * @brief MCAN Last Error Code
  96. */
  97. typedef enum mcan_last_error_code {
  98. mcan_last_error_code_no_error = 0, /*!< No error happened */
  99. mcan_last_error_code_stuff_error, /*!< Stuff Error */
  100. mcan_last_error_code_format_error, /*!< Format Error */
  101. mcan_last_error_code_ack_error, /*!< Acknowledge Error */
  102. mcan_last_error_code_bit1_error, /*!< Sent logic 1 but monitored value is logic 0 */
  103. mcan_last_error_code_bit0_error, /*!< Sent logic 0 but monitored value is logic 1 */
  104. mcan_last_error_code_crc_error, /*!< CRC checksum for received message is wrong */
  105. mcan_last_error_code_no_change, /*!< Error code was not changed */
  106. } mcan_last_err_code_t;
  107. /**
  108. * @brief MCAN Communication State
  109. */
  110. typedef enum mcan_activity_enum {
  111. mcan_activity_sync = 0, /*!< Node is synchronizing on CAN communication */
  112. mcan_activity_idle, /*!< Node is neither receiver nor transmitter */
  113. mcan_activity_receiver, /*!< Node is operating as receiver */
  114. mcan_activity_transmitter, /*!< Node is operating as transmitter */
  115. } mcan_activity_state_t;
  116. /***********************************************************************************************************************
  117. * @brief Default CAN RAM definitions
  118. **********************************************************************************************************************/
  119. #define MCAN_RAM_WORD_SIZE (640U) /*!< RAM WORD Size */
  120. /* CAN Filter Element Size Definitions */
  121. #define MCAN_FILTER_ELEM_STD_ID_SIZE (4U) /*!< Standard Filter Element Size in Bytes */
  122. #define MCAN_FILTER_ELEM_EXT_ID_SIZE (8U) /*!< Extended Filter Element SIze in Bytes */
  123. #define MCAN_STD_FILTER_ELEM_SIZE_MAX (128U) /*!< Maximum Standard Filter Element Count */
  124. #define MCAN_EXT_FILTER_ELEM_SIZE_MAX (64U) /*!< Maximum Extended Filter Element Count */
  125. /* MCAN Default Field Size Definitions for CAN2.0 */
  126. #define MCAN_TXRX_ELEM_SIZE_CAN_MAX (4U * sizeof(uint32_t))
  127. #define MCAN_FILTER_SIZE_CAN_DEFAULT (32U)
  128. #define MCAN_TXBUF_SIZE_CAN_DEFAULT (32U)
  129. #define MCAN_RXFIFO_SIZE_CAN_DEFAULT (32U)
  130. #define MCAN_RXBUF_SIZE_CAN_DEFAULT (16U)
  131. /* MCAN Default Field Size Definitions for CANFD */
  132. #define MCAN_TXRX_ELEM_SIZE_CANFD_MAX (18U * sizeof(uint32_t))
  133. #define MCAN_FILTER_SIZE_CANFD_DEFAULT (16U)
  134. #define MCAN_TXBUF_SIZE_CANFD_DEFAULT (8U)
  135. #define MCAN_RXFIFO_SIZE_CANFD_DEFAULT (8U)
  136. #define MCAN_RXBUF_SIZE_CANFD_DEFAULT (4U)
  137. #define MCAN_TXEVT_ELEM_SIZE (8U)
  138. /**
  139. * @brief CAN Bit Timing Parameters
  140. */
  141. typedef struct mcan_bit_timing_param_struct {
  142. uint16_t prescaler; /*!< Data Bit Rate Prescaler */
  143. uint16_t num_seg1; /*!< Time segment before sample point (including prop_seg and phase_sge1 */
  144. uint16_t num_seg2; /*!< Time segment after sample point */
  145. uint8_t num_sjw; /*!< Data (Re)Synchronization Jump Width */
  146. bool enable_tdc; /*!< Enable TDC flag, for CANFD data bit timing only */
  147. } mcan_bit_timing_param_t;
  148. /**
  149. * @brief CAN Bit Timing Options
  150. */
  151. typedef enum mcan_bit_timing_option {
  152. mcan_bit_timing_can2_0, /**< CAN 2.0 bit timing option */
  153. mcan_bit_timing_canfd_nominal, /**< CANFD nominal timing option */
  154. mcan_bit_timing_canfd_data, /**< CANFD data timing option */
  155. } mcan_bit_timing_option_t;
  156. /**
  157. * @brief CAN Message Header Size
  158. */
  159. #define MCAN_MESSAGE_HEADER_SIZE_IN_BYTES (8U)
  160. #define MCAN_MESSAGE_HEADER_SIZE_IN_WORDS (2U)
  161. /**
  162. * @brief CAN Transmit Message Frame
  163. *
  164. * @note Users need to pay attention to the CAN Identifier settings
  165. * For standard identifier, the use_ext_id should be set to 0 and the std_id should be used
  166. * For extended identifier, the use_ext_id should be set to 1 and the ext_id should be used
  167. */
  168. typedef struct mcan_tx_message_struct {
  169. union {
  170. struct {
  171. uint32_t ext_id: 29; /*!< Extended CAN Identifier */
  172. uint32_t rtr: 1; /*!< Remote Transmission Request */
  173. uint32_t use_ext_id: 1; /*!< Extended Identifier */
  174. uint32_t error_state_indicator: 1; /*!< Error State Indicator */
  175. };
  176. struct {
  177. uint32_t : 18;
  178. uint32_t std_id: 11; /*!< Standard CAN Identifier */
  179. uint32_t : 3;
  180. };
  181. };
  182. struct {
  183. uint32_t : 8;
  184. uint32_t message_marker_h: 8; /*!< Message Marker[15:8] */
  185. uint32_t dlc: 4; /*!< Data Length Code */
  186. uint32_t bitrate_switch: 1; /*!< Bit Rate Switch */
  187. uint32_t canfd_frame: 1; /*!< CANFD frame */
  188. uint32_t timestamp_capture_enable: 1; /*!< Timestamp Capture Enable for TSU */
  189. uint32_t event_fifo_control: 1; /*!< Event FIFO control */
  190. uint32_t message_marker_l: 8; /*!< Message Marker[7:0] */
  191. };
  192. union {
  193. uint8_t data_8[64]; /*!< Data buffer as byte array */
  194. uint32_t data_32[16]; /*!< Data buffer as word array */
  195. };
  196. } mcan_tx_frame_t;
  197. /**
  198. * @brief CAN Receive Message Frame
  199. *
  200. * @note Users need to pay attention to the CAN Identifier settings
  201. * For standard identifier, the use_ext_id should be set to 0 and the std_id should be used
  202. * For extended identifier, the use_ext_id should be set to 1 and the ext_id should be used
  203. */
  204. typedef struct mcan_rx_message_struct {
  205. union {
  206. struct {
  207. uint32_t ext_id: 29; /*!< Extended CAN Identifier */
  208. uint32_t rtr: 1; /*!< Remote Frame Flag */
  209. uint32_t use_ext_id: 1; /*!< Extended Identifier */
  210. uint32_t error_state_indicator: 1; /*!< Error State Indicator */
  211. };
  212. struct {
  213. uint32_t : 18;
  214. uint32_t std_id: 11; /*!< Standard CAN Identifier */
  215. uint32_t : 3;
  216. };
  217. };
  218. struct {
  219. union {
  220. uint16_t rx_timestamp; /*!< Received timestamp */
  221. struct {
  222. uint16_t rx_timestamp_pointer: 4; /*!< Timestamp Pointer */
  223. uint16_t rx_timestamp_captured: 1; /*!< Timestamp Captured flag */
  224. uint16_t : 11;
  225. };
  226. };
  227. };
  228. struct {
  229. uint16_t dlc: 4; /*!< Data Length Code */
  230. uint16_t bitrate_switch: 1; /*!< Bit rate switch flag */
  231. uint16_t canfd_frame: 1; /*!< CANFD Frame flag */
  232. uint16_t : 1;
  233. uint16_t filter_index: 7; /*!< CAN filter index */
  234. uint16_t accepted_non_matching_frame: 1; /*!< Accept non-matching Frame flag */
  235. };
  236. union {
  237. uint8_t data_8[64]; /*!< Data buffer as byte array */
  238. uint32_t data_32[16]; /*!< Data buffer as word array */
  239. };
  240. } mcan_rx_message_t;
  241. /**
  242. * @brief TX Event Fifo Element Structure
  243. */
  244. typedef union mcan_tx_event_fifo_elem_struct {
  245. struct {
  246. /* First word */
  247. union {
  248. struct {
  249. uint32_t ext_id: 29; /*!< CAN Identifier */
  250. uint32_t rtr: 1; /*!< Remote Transmission Request */
  251. uint32_t extend_id: 1; /*!< Extended Identifier */
  252. uint32_t error_state_indicator: 1; /*!< Error State Indicator */
  253. };
  254. struct {
  255. uint32_t : 18;
  256. uint32_t std_id: 11;
  257. uint32_t : 3;
  258. };
  259. };
  260. /* first 16-bit of the 2nd word */
  261. union {
  262. uint16_t tx_timestamp; /*!< Tx Timestamp */
  263. struct {
  264. uint16_t tx_timestamp_pointer: 4; /*!< TX timestamp pointer */
  265. uint16_t tx_timestamp_captured: 1; /*!< TX timestamp captured flag */
  266. uint16_t : 11;
  267. };
  268. };
  269. /* high-half 16-bit of the 2nd word */
  270. struct {
  271. uint16_t dlc: 4; /*!< Data length code */
  272. uint16_t bitrate_switch: 1; /*!< Bitrate Switch flag */
  273. uint16_t canfd_frame: 1; /*!< CANFD frame */
  274. uint16_t event_type: 2; /*!< Event type */
  275. uint16_t message_marker: 8; /*!< Message Marker */
  276. };
  277. };
  278. uint32_t words[2];
  279. } mcan_tx_event_fifo_elem_t;
  280. /**
  281. * @brief CAN Identifier Types
  282. */
  283. #define MCAN_CAN_ID_TYPE_STANDARD (0U) /*!< Standard Identifier */
  284. #define MCAN_CAN_ID_TYPE_EXTENDED (1U) /*!< Extended Identifier */
  285. /**
  286. * @brief MCAN Filter Configuration
  287. */
  288. typedef union mcan_filter_config_struct {
  289. struct {
  290. uint16_t list_start_addr; /*!< List Start address (CAN Message Buffer Offset) */
  291. uint16_t list_size; /*!< Element Count */
  292. };
  293. uint32_t reg_val; /*!< Register value */
  294. } mcan_filter_config_t;
  295. /**
  296. * @brief MCAN RXFIFO Configuration
  297. */
  298. typedef union mcan_rxfifo_config_struct {
  299. struct {
  300. uint32_t start_addr: 16; /*!< Start address (CAN Message Buffer Offset) */
  301. uint32_t fifo_size: 8; /*!< FIFO element count */
  302. uint32_t watermark: 7; /*!< FIFO watermark */
  303. uint32_t operation_mode: 1; /*!< Operation mode */
  304. };
  305. uint32_t reg_val; /*!< Register value */
  306. } mcan_rxfifo_config_t;
  307. /**
  308. * @brief MCAN RXBUF Configuration
  309. */
  310. typedef struct {
  311. uint32_t start_addr; /*!< Start address (CAN Message Buffer Offset) */
  312. } mcan_rxbuf_config_t;
  313. /**
  314. * @brief MCAN Data Field Size Definitions
  315. */
  316. #define MCAN_DATA_FIELD_SIZE_8BYTES (0U)
  317. #define MCAN_DATA_FIELD_SIZE_12BYTES (1U)
  318. #define MCAN_DATA_FIELD_SIZE_16BYTES (2U)
  319. #define MCAN_DATA_FIELD_SIZE_20BYTES (3U)
  320. #define MCAN_DATA_FIELD_SIZE_24BYTES (4U)
  321. #define MCAN_DATA_FIELD_SIZE_32BYTES (5U)
  322. #define MCAN_DATA_FIELD_SIZE_48BYTES (6U)
  323. #define MCAN_DATA_FIELD_SIZE_64BYTES (7U)
  324. /**
  325. * @brief MCAN FIFO Operation Mode types
  326. */
  327. #define MCAN_FIFO_OPERATION_MODE_BLOCKING (0U)
  328. #define MCAN_FIFO_OPERATION_MODE_OVERWRITE (1U)
  329. /**
  330. * @brief MCAN RXBUF or RXFIFO Element Configuration
  331. */
  332. typedef union mcan_rx_fifo_or_buf_elem_config_struct {
  333. struct {
  334. uint32_t fifo0_data_field_size: 4; /*!< FIFO0 data field size option */
  335. uint32_t fifo1_data_field_size: 4; /*!< FIFO1 data field size option */
  336. uint32_t buf_data_field_size: 4; /*!< Buffer Data field size option */
  337. uint32_t : 20;
  338. };
  339. uint32_t reg_val; /*!< Register value */
  340. } mcan_rx_fifo_or_buf_elem_config_t;
  341. /**
  342. * @brief MCAN TXBUF operation mode types
  343. */
  344. #define MCAN_TXBUF_OPERATION_MODE_FIFO (0U)
  345. #define MCAN_TXBUF_OPERATION_MODE_QUEUE (1U)
  346. typedef union mcan_txbuf_config_struct {
  347. struct {
  348. uint32_t start_addr: 16; /*!< Start address (CAN Message Buffer Offset) */
  349. uint32_t dedicated_tx_buf_size: 6; /*!< Dedicated TX buffer size */
  350. uint32_t : 2;
  351. uint32_t fifo_queue_size: 6; /*!< FIFO or Queue Size */
  352. uint32_t tx_fifo_queue_mode: 1; /*!< FIFO or Queue mode selection */
  353. uint32_t : 1;
  354. };
  355. uint32_t reg_val; /*!< register value */
  356. } mcan_txbuf_config_t;
  357. typedef struct mcan_txbuf_elem_config_struct {
  358. uint32_t data_field_size; /*!< Data Field size option */
  359. } mcan_txbuf_elem_config_t;
  360. /**
  361. * @brief MCAN TX Event FIFO Structure
  362. */
  363. typedef union {
  364. struct {
  365. uint16_t start_addr; /*!< Start Address(CAN Message Buffer Offset */
  366. uint8_t fifo_size; /*!< FIFO element count */
  367. uint8_t fifo_watermark; /*!< FIFO watermark */
  368. };
  369. uint32_t reg_val; /*!< register value */
  370. } mcan_tx_evt_fifo_config_t;
  371. /**
  372. * @brief MCAN RAM Flexible Configuration
  373. *
  374. * @note This Configuration provides the full MCAN RAM configuration, this configuration is recommended only for
  375. * experienced developers who is skilled at the MCAN IP
  376. */
  377. typedef struct mcan_ram_flexible_config_struct {
  378. bool enable_std_filter; /*!< Enable Standard Identifier Filter */
  379. bool enable_ext_filter; /*!< Enable Extended Identifier Filter */
  380. bool enable_rxfifo0; /*!< Enable RXFIFO */
  381. bool enable_rxfifo1; /*!< Enable RXFIF1 */
  382. bool enable_rxbuf; /*!< Enable RXBUF */
  383. bool enable_txbuf; /*!< Enable TXBUF */
  384. bool enable_tx_evt_fifo; /*!< Enable TX Event FIFO */
  385. mcan_filter_config_t std_filter_config; /*!< Standard Identifier Filter Configuration */
  386. mcan_filter_config_t ext_filter_config; /*!< Extended Identifier Filter Configuration */
  387. mcan_txbuf_config_t txbuf_config; /*!< TXBUF Configuration */
  388. mcan_txbuf_elem_config_t txbuf_elem_config; /*!< TXBUF Element Configuration */
  389. mcan_tx_evt_fifo_config_t tx_evt_fifo_config; /*!< TX Event FIFO Configuration */
  390. mcan_rxfifo_config_t rxfifo0_config; /*!< RXFIFO0 Configuration */
  391. mcan_rxfifo_config_t rxfifo1_config; /*!< RXFIFO1 Configuration */
  392. mcan_rxbuf_config_t rxbuf_config; /*!< RXBUF Configuration */
  393. mcan_rx_fifo_or_buf_elem_config_t rx_elem_config; /*!< RX Element Configuration */
  394. } mcan_ram_flexible_config_t;
  395. /**
  396. * @brief MCAN RAM configuration
  397. *
  398. * @note: This Configuration focuses on the minimum required information for MCAN RAM configuration
  399. * The Start address of each BUF/FIFO will be automatically calculated by the MCAN Driver API
  400. * This RAM configuration is recommended for the most developers
  401. */
  402. typedef struct mcan_ram_config_struct {
  403. bool enable_std_filter; /*!< Enable Standard Identifier Filter */
  404. uint8_t std_filter_elem_count; /*!< Standard Identifier Filter Element Count */
  405. bool enable_ext_filter; /*!< Enable Extended Identifier Filter */
  406. uint8_t ext_filter_elem_count; /*!< Extended Identifier Filter Element Count */
  407. struct {
  408. uint32_t enable: 4; /*!< Enable Flag for RXFIFO */
  409. uint32_t elem_count: 8; /*!< Element Count for RXFIFO */
  410. uint32_t watermark: 8; /*!< Watermark for RXFIFO */
  411. uint32_t operation_mode: 4; /*!< Operation Mode for RXFIFO */
  412. uint32_t data_field_size: 8; /*!< Data field size option for RXFIFO */
  413. } rxfifos[2];
  414. bool enable_rxbuf; /*!< Enable RXBUF */
  415. uint8_t rxbuf_elem_count; /*!< RXBUF Element count */
  416. uint16_t rxbuf_data_field_size; /*!< RXBUF Data Field Size option */
  417. bool enable_txbuf; /*!< Enable TXBUF */
  418. uint8_t txbuf_data_field_size; /*!< TXBUF Data Field Size option */
  419. uint8_t txbuf_dedicated_txbuf_elem_count; /*!< Dedicated TXBUF element count */
  420. uint8_t txbuf_fifo_or_queue_elem_count; /*!< FIFO/Queue element count */
  421. uint8_t txfifo_or_txqueue_mode; /*!< TXFIFO/QUEUE mode */
  422. bool enable_tx_evt_fifo; /*!< Enable TX Event FIFO */
  423. uint8_t tx_evt_fifo_elem_count; /*!< TX Event FIFO Element count */
  424. uint8_t tx_evt_fifo_watermark; /*!< TX Event FIFO Watermark */
  425. } mcan_ram_config_t;
  426. /**
  427. * @brief MCAN Accept Non-Matching Frame options
  428. */
  429. #define MCAN_ACCEPT_NON_MATCHING_FRAME_OPTION_IN_RXFIFO0 (0U) /*!< Save non-matching frame to RXFIFO0 */
  430. #define MCAN_ACCEPT_NON_MATCHING_FRAME_OPTION_IN_RXFIFO1 (1U) /*!< Save non-matching frame to RXFIFO1 */
  431. #define MCAN_ACCEPT_NON_MATCHING_FRAME_OPTION_REJECT (2U) /*!< Reject non-matching frame */
  432. /**
  433. * @brief MCAN Global CAN configuration
  434. */
  435. typedef struct mcan_global_filter_config_struct {
  436. uint8_t accept_non_matching_std_frame_option; /*!< Accept non-matching standard frame option */
  437. uint8_t accept_non_matching_ext_frame_option; /*!< Accept non-matching extended frame option */
  438. bool reject_remote_std_frame; /*!< Reject Remote Standard Frame */
  439. bool reject_remote_ext_frame; /*!< Reject Remote Extended Frame */
  440. } mcan_global_filter_config_t;
  441. /**
  442. * @brief MCAN Filter type definitions
  443. */
  444. #define MCAN_FILTER_TYPE_RANGE_FILTER (0U) /*!< CAN Identifier Range filter */
  445. #define MCAN_FILTER_TYPE_SPECIFIED_ID_FILTER (1U) /*!< CAN Identifier ID filter */
  446. #define MCAN_FILTER_TYPE_CLASSIC_FILTER (2U) /*!< CAN classic filter */
  447. #define MCAN_FILTER_TYPE_FILTER_DISABLED (3U) /*!< CAN filter disabled */
  448. #define MCAN_FILTER_TYPE_DUAL_ID_EXT_FILTER_IGNORE_MASK /*!< CAN Identifier Range filter, ignoring extended ID mask */
  449. /**
  450. * @brief MCAN Filter Configuration Options
  451. */
  452. #define MCAN_FILTER_ELEM_CFG_DISABLED (0) /*!< Disable Filter Element */
  453. #define MCAN_FILTER_ELEM_CFG_STORE_IN_RX_FIFO0_IF_MATCH (1U) /*!< Store data into RXFIFO0 if matching */
  454. #define MCAN_FILTER_ELEM_CFG_STORE_IN_RX_FIFO1_IF_MATCH (2U) /*!< Store data into RXFIFO1 if matching */
  455. #define MCAN_FILTER_ELEM_CFG_REJECT_ID_IF_MATCH (3U) /*!< Reject ID if matching */
  456. #define MCAN_FILTER_ELEM_CFG_SET_PRIORITY_IF_MATCH (4U) /*!< Set Priority if matching */
  457. /*!< Set Priority and store into RXFIFO0 if matching */
  458. #define MCAN_FILTER_ELEM_CFG_SET_PRIORITY_AND_STORE_IN_FIFO0_IF_MATCH (5U)
  459. /*!< Set Priority and store into RXFIFO1 if matching */
  460. #define MCAN_FILTER_ELEM_CFG_SET_PRIORITY_AND_STORE_IN_FIFO1_IF_MATCH (6U)
  461. #define MCAN_FILTER_ELEM_CFG_STORE_INTO_RX_BUFFER_OR_AS_DBG_MSG (7U) /*!< Store into RXBUF if matching */
  462. /**
  463. * @brief MCAN Filter Element definitions
  464. */
  465. typedef struct mcan_std_id_filter_elem_struct {
  466. uint8_t filter_type; /*!< Filter type */
  467. uint8_t filter_config; /*!< Filter configuration */
  468. uint8_t can_id_type; /*!< CAN ID type */
  469. uint8_t sync_message; /*!< Sync Message */
  470. union {
  471. /* This definition takes effect if the filter type is range filter */
  472. struct {
  473. uint32_t start_id; /*!< Start of ID range */
  474. uint32_t end_id; /*!< End of ID range */
  475. };
  476. /* This definition takes effect if the filter type is dual id filter */
  477. struct {
  478. uint32_t id1; /*!< ID1 */
  479. uint32_t id2; /*!< ID2 */
  480. };
  481. /* This definition takes effect if the filter type is classic filter */
  482. struct {
  483. uint32_t filter_id; /*!< Filter ID */
  484. uint32_t filter_mask; /*!< Filter Mask */
  485. };
  486. /* This definition takes effect if the filter configuration is "store into RX Buffer or as debug message"
  487. *
  488. * In this definition, only the exact ID matching mode is activated
  489. */
  490. struct {
  491. uint32_t match_id; /*!< Matching ID */
  492. uint32_t offset: 6; /*!< RX Buffer Index */
  493. uint32_t filter_event: 3; /*!< Filter event, set to 0 */
  494. uint32_t store_location: 2; /*!< Store location, 0 - RX buffer */
  495. uint32_t : 21;
  496. };
  497. };
  498. } mcan_filter_elem_t;
  499. /**
  500. * @brief MCAN Filter Element List structure
  501. */
  502. typedef struct mcan_filter_elem_list_struct {
  503. uint32_t mcan_filter_elem_count; /*!< Number of Filter element */
  504. const mcan_filter_elem_t *filter_elem_list; /*!< Filter element list */
  505. } mcan_filter_elem_list_t;
  506. /**
  507. * @brief MCAN Configuration for all filters
  508. *
  509. * @note The MCAN RAM related settings are excluded
  510. */
  511. typedef struct mcan_all_filters_config_struct {
  512. mcan_global_filter_config_t global_filter_config; /*!< Global Filter configuration */
  513. uint32_t ext_id_mask; /*!< Extended ID mask */
  514. mcan_filter_elem_list_t std_id_filter_list; /*!< Standard Identifier Configuration List */
  515. mcan_filter_elem_list_t ext_id_filter_list; /*!< Extended Identifier Configuration List */
  516. } mcan_all_filters_config_t;
  517. /**
  518. * @brief CAN Node Mode types
  519. */
  520. typedef enum mcan_node_mode_enum {
  521. mcan_mode_normal = 0, /*!< CAN works in normal mode */
  522. mcan_mode_loopback_internal = 1, /*!< CAN works in internal loopback mode */
  523. mcan_mode_loopback_external = 2, /*!< CAN works in external loopback mode */
  524. mcan_mode_listen_only = 3, /*!< CAN works in listen-only mode */
  525. } mcan_node_mode_t;
  526. /**
  527. * @brief MCAN Timestamp Value
  528. */
  529. typedef struct mcan_timestamp_value_struct {
  530. bool is_16bit; /*!< Timestamp is 16-bit */
  531. bool is_32bit; /*!< Timestamp is 32-bit */
  532. bool is_64bit; /*!< Timestamp is 64-bit */
  533. bool is_empty; /*!< Timestamp is empty */
  534. union {
  535. uint16_t ts_16bit; /*!< 16-bit timestamp value */
  536. uint32_t ts_32bit; /*!< 32-bit timestamp value */
  537. uint64_t ts_64bit; /*!< 64-bit timestamp value */
  538. uint32_t words[2]; /*!< timestamp words */
  539. };
  540. } mcan_timestamp_value_t;
  541. /**
  542. * @brief MCAN TSU Configuration
  543. */
  544. typedef struct mcan_tsu_config_struct {
  545. uint16_t prescaler; /*!< Prescaler for MCAN clock, Clock source: AHB clock */
  546. bool capture_on_sof; /*!< Capture On SOF, true - Capture on SOF, false - Capture on EOF */
  547. bool use_ext_timebase; /*!< Use External Timebase */
  548. uint8_t ext_timebase_src; /*!< External Timebase source, see the hpm_mcan_soc.h for more details */
  549. uint8_t tbsel_option; /*!< Timebase selection option, see the hpm_mcan_soc.h for more details */
  550. bool enable_tsu; /*!< Enable Timestamp Unit */
  551. bool enable_64bit_timestamp; /*!< Enable 64bit Timestamp */
  552. } mcan_tsu_config_t;
  553. /**
  554. * @brief MCAN Timestamp Select
  555. */
  556. #define MCAN_TIMESTAMP_SEL_MIN (0U)
  557. #define MCAN_TIMESTAMP_SEL_VALUE_ALWAYS_ZERO (0U) /*!< Timestamp value always equal to 0 */
  558. #define MCAN_TIMESTAMP_SEL_VALUE_INCREMENT (1U) /*!< Timestamp value increments according to prescaler */
  559. #define MCAN_TIMESTAMP_SEL_EXT_TS_VAL_USED (2U) /*!< External Timestamp value used */
  560. #define MCAN_TIMESTAMP_SEL_MAX (MCAN_TIMESTAMP_SEL_EXT_TS_VAL_USED)
  561. /**
  562. * @brief MCAN Internal timestamp configuration
  563. */
  564. typedef struct mcan_internal_timestamp_config_struct {
  565. uint8_t counter_prescaler; /*!< Timestamp Counter Prescaler, clock source: AHB clock */
  566. uint8_t timestamp_selection; /*!< Timestamp Select */
  567. } mcan_internal_timestamp_config_t;
  568. /**
  569. * @brief MCAN Timeout Selection Options
  570. */
  571. typedef enum mcan_timeout_sel_enum {
  572. mcan_timeout_continuous_operation = 0, /*!< Continuously count down timeout after writing to TOCV register */
  573. mcan_timeout_triggered_by_tx_evt_fifo = 1, /*!< Count down if the TX EVT FIFO is not empty */
  574. mcan_timeout_triggered_by_rx_fifo0 = 2, /*!< Count down if the RX FIFO0 is not empty */
  575. mcan_timeout_triggered_by_rx_fifo1 = 3, /*!< Count down if the RX FIFO1 is not empty */
  576. } mcan_timeout_sel_t;
  577. /**
  578. * @brief MCAN Timeout configuration structure
  579. */
  580. typedef struct mcan_timeout_config_struct {
  581. bool enable_timeout_counter; /*!< Enable Timeout Counter */
  582. mcan_timeout_sel_t timeout_sel; /*!< Timeout source selection */
  583. uint16_t timeout_period; /*!< Timeout period */
  584. } mcan_timeout_config_t;
  585. /**
  586. * @brief MCAN Transmitter Delay Compensation Configuration
  587. */
  588. typedef struct mcan_tdc_config_t {
  589. uint8_t ssp_offset; /*!< SSP offset */
  590. uint8_t filter_window_length; /*!< Filter Window Length */
  591. } mcan_tdc_config_t;
  592. /**
  593. * @brief MCAN Configuration Structure
  594. */
  595. typedef struct mcan_config_struct {
  596. union {
  597. /* This struct takes effect if "use_lowlevel_timing_setting = false" */
  598. struct {
  599. uint32_t baudrate; /*!< CAN 2.0 baudrate/CAN-FD Nominal Baudrate, in terms of bps */
  600. uint32_t baudrate_fd; /*!< CANFD data baudrate, in terms of bps */
  601. uint16_t can20_samplepoint_min; /*!< Value = Minimum CAN2.0 sample point * 10 */
  602. uint16_t can20_samplepoint_max; /*!< Value = Maximum CAN2.0 sample point * 10 */
  603. uint16_t canfd_samplepoint_min; /*!< Value = Minimum CANFD sample point * 10 */
  604. uint16_t canfd_samplepoint_max; /*!< Value = Maximum CANFD sample point * 10 */
  605. };
  606. /* This struct takes effect if "use_lowlevel_timing_setting = true" */
  607. struct {
  608. mcan_bit_timing_param_t can_timing; /*!< CAN2.0/CANFD nominal timing setting */
  609. mcan_bit_timing_param_t canfd_timing; /*!< CANFD data timing setting */
  610. };
  611. };
  612. bool use_lowlevel_timing_setting; /*!< Use Low-level timing setting */
  613. mcan_node_mode_t mode; /*!< CAN node mode */
  614. bool enable_non_iso_mode; /*!< Enable Non-ISO FD mode */
  615. bool enable_transmit_pause; /*!< Enable Transmit Pause */
  616. bool enable_edge_filtering; /*!< Enable Edge Filtering */
  617. bool disable_protocol_exception_handling; /*!< Disable Protocol Exception Handling */
  618. bool enable_wide_message_marker; /*!< Enable Wide Message Marker */
  619. bool use_timestamping_unit; /*!< Use external Timestamp Unit */
  620. bool enable_canfd; /*!< Enable CANFD mode */
  621. bool enable_tdc; /*!< Enable transmitter delay compensation */
  622. bool enable_restricted_operation_mode; /*!< Enable Restricted Operation Mode: Receive only */
  623. bool disable_auto_retransmission; /*!< Disable auto retransmission */
  624. mcan_tdc_config_t tdc_config; /*!< Transmitter Delay Compensation Configuration */
  625. mcan_internal_timestamp_config_t timestamp_cfg; /*!< Internal Timestamp Configuration */
  626. mcan_tsu_config_t tsu_config; /*!< TSU configuration */
  627. mcan_ram_config_t ram_config; /*!< MCAN RAM configuration */
  628. mcan_all_filters_config_t all_filters_config; /*!< All Filter configuration */
  629. mcan_timeout_config_t timeout_cfg; /*!< Timeout configuration */
  630. uint32_t interrupt_mask; /*!< Interrupt Enable mask */
  631. uint32_t txbuf_trans_interrupt_mask; /*!< Tx Buffer Transmission Interrupt Enable mask */
  632. uint32_t txbuf_cancel_finish_interrupt_mask; /*!< TX Buffer Cancellation Finished Interrupt Enable Mask */
  633. } mcan_config_t;
  634. /**
  635. * @brief MCAN Timeout Selection Options
  636. */
  637. #define MCAN_TIMEOUT_SEL_TYPE_CONTINUOUS_OPERATION (0U)
  638. #define MCAN_TIMEOUT_SEL_TYPE_TIMEOUT_CTRL_BY_TX_EVT_FIFO (1U)
  639. #define MCAN_TIMEOUT_SEL_TYPE_TIMEOUT_CTRL_BY_RX_FIFO0 (2U)
  640. #define MCAN_TIMEOUT_SEL_TYPE_TIMEOUT_CTRL_BY_RX_FIFO1 (3U)
  641. /**
  642. * @brief MCAN Timeout Counter Configuration
  643. */
  644. typedef struct mcan_timeout_counter_config_struct {
  645. bool enable_timeout_counter; /*!< Enable Timeout counter */
  646. uint8_t timeout_selection; /*!< Timeout source selection */
  647. uint16_t timeout_period; /*!< Timeout period */
  648. } mcan_timeout_counter_config_t;
  649. /**
  650. * @brief MCAN Error Count Information
  651. */
  652. typedef struct mcan_error_count_struct {
  653. uint8_t transmit_error_count; /*!< Transmit Error Count */
  654. uint8_t receive_error_count; /*!< Receive Error Count */
  655. bool receive_error_passive; /*!< The Receive Error Counter has reached the error passive level */
  656. uint8_t can_error_logging_count; /*!< CAN Error Logging count */
  657. } mcan_error_count_t;
  658. /**
  659. * @brief MCAN Protocol Status
  660. */
  661. typedef struct mcan_protocol_status {
  662. uint8_t tdc_val; /*!< Transmitter Delay Compensation Value */
  663. mcan_activity_state_t activity; /*!< Current communication state */
  664. mcan_last_err_code_t last_error_code; /*!< Last Error code */
  665. bool protocol_exception_evt_occurred; /*!< Protocol Exception Event occurred */
  666. bool canfd_msg_received; /*!< CANFD message was received */
  667. bool brs_flag_set_in_last_rcv_canfd_msg; /*!< Bitrate Switch bit is set in last received CANFD message */
  668. bool esi_flag_set_in_last_rcv_canfd_msg; /*!< Error State Indicator bit is set in last received CANFD message */
  669. bool in_bus_off_state; /*!< Node is in bus-off state */
  670. bool in_warning_state; /*!< Node is in warning state */
  671. bool in_error_passive_state; /*!< Node is in error passive state */
  672. } mcan_protocol_status_t;
  673. /**
  674. * @brief MCAN Message Storage Indicator Types
  675. */
  676. #define MCAN_MESSAGE_STORAGE_INDICATOR_NO_FIFO_SELECTED (0U)
  677. #define MCAN_MESSAGE_STORAGE_INDICATOR_FIFO_MESSAGE_LOST (1U)
  678. #define MCAN_MESSAGE_STORAGE_INDICATOR_RXFIFO0 (2U)
  679. #define MCAN_MESSAGE_STORAGE_INDICATOR_RXFIFO1 (3U)
  680. /**
  681. * @brief MCAN High Priority Message Status Information
  682. */
  683. typedef struct mcan_high_priority_message_status_struct {
  684. uint8_t filter_list_type; /*!< Filter List Type, 0 - STD filter, 1 - EXT filter */
  685. uint8_t filter_index; /*!< Filter Elem List */
  686. uint8_t message_storage_indicator; /*!< Message Storage Indicator */
  687. uint8_t buffer_index;
  688. } mcan_high_priority_message_status_t;
  689. /**
  690. * @brief Enable Transmit Pause
  691. * @param [in] ptr MCAN base
  692. */
  693. static inline void mcan_enable_transmit_pause(MCAN_Type *ptr)
  694. {
  695. ptr->CCCR |= MCAN_CCCR_TXP_MASK;
  696. }
  697. /**
  698. * @brief Disable Transmit Pause
  699. * @param [in] ptr MCAN base
  700. */
  701. static inline void mcan_disable_transmit_pause(MCAN_Type *ptr)
  702. {
  703. ptr->CCCR &= ~MCAN_CCCR_TXP_MASK;
  704. }
  705. /**
  706. * @brief Enable Edge Filtering
  707. * @param [in] ptr MCAN base
  708. */
  709. static inline void mcan_enable_edge_filtering(MCAN_Type *ptr)
  710. {
  711. ptr->CCCR |= MCAN_CCCR_EFBI_MASK;
  712. }
  713. /**
  714. * @brief Disable Edge Filtering
  715. * @param [in] ptr MCAN base
  716. */
  717. static inline void mcan_disable_edge_filtering(MCAN_Type *ptr)
  718. {
  719. ptr->CCCR &= ~MCAN_CCCR_EFBI_MASK;
  720. }
  721. /**
  722. * @brief Enable Protocol Exception Handling
  723. * @param [in] ptr MCAN base
  724. */
  725. static inline void mcan_enable_protocol_exception_handling(MCAN_Type *ptr)
  726. {
  727. ptr->CCCR &= ~MCAN_CCCR_PXHD_MASK;
  728. }
  729. /**
  730. * @brief Disable Protocol Exception Handling
  731. * @param [in] ptr MCAN base
  732. */
  733. static inline void mcan_disable_protocol_exception_handling(MCAN_Type *ptr)
  734. {
  735. ptr->CCCR |= MCAN_CCCR_PXHD_MASK;
  736. }
  737. /**
  738. * @brief Enable Wide Message Marker
  739. * @param [in] ptr MCAN base
  740. */
  741. static inline void mcan_enable_wide_message_marker(MCAN_Type *ptr)
  742. {
  743. ptr->CCCR |= MCAN_CCCR_WMM_MASK;
  744. }
  745. /**
  746. * @brief Disable Wide Message Marker
  747. * @param [in] ptr MCAN base
  748. */
  749. static inline void mcan_disable_wide_message_marker(MCAN_Type *ptr)
  750. {
  751. ptr->CCCR &= ~MCAN_CCCR_WMM_MASK;
  752. }
  753. /**
  754. * @brief Enable External Timestamp Unit
  755. * @param [in] ptr MCAN base
  756. */
  757. static inline void mcan_enable_tsu(MCAN_Type *ptr)
  758. {
  759. ptr->CCCR |= MCAN_CCCR_UTSU_MASK;
  760. }
  761. /**
  762. * @brief Disable External Timestamp Unit
  763. * @param [in] ptr MCAN base
  764. */
  765. static inline void mcan_disable_tsu(MCAN_Type *ptr)
  766. {
  767. ptr->CCCR &= ~MCAN_CCCR_UTSU_MASK;
  768. }
  769. /**
  770. * @brief Check whether TSU is used
  771. * @param [in] ptr MCAN base
  772. * @retval true if TSU is used
  773. * @retval false if TSU is not used
  774. */
  775. static inline bool mcan_is_tsu_used(MCAN_Type *ptr)
  776. {
  777. return ((ptr->CCCR & MCAN_CCCR_UTSU_MASK) != 0U);
  778. }
  779. /**
  780. * @brief Check whether 64-bit TSU timestamp is used
  781. * @param [in] ptr MCAN base
  782. * @retval true if 64-bit timestamp is used
  783. * @retval false if 32-bit timestamp is used
  784. */
  785. static inline bool mcan_is_64bit_tsu_timestamp_used(MCAN_Type *ptr)
  786. {
  787. return ((ptr->TSCFG & MCAN_TSCFG_EN64_MASK) != 0U);
  788. }
  789. /**
  790. * @brief Enable Bit Rate Switch
  791. * @param [in] ptr MCAN base
  792. */
  793. static inline void mcan_enable_bitrate_switch(MCAN_Type *ptr)
  794. {
  795. ptr->CCCR |= MCAN_CCCR_BRSE_MASK;
  796. }
  797. /**
  798. * @brief Disable Bit Rate Switch
  799. * @param [in] ptr MCAN base
  800. */
  801. static inline void mcan_disable_bitrate_switch(MCAN_Type *ptr)
  802. {
  803. ptr->CCCR &= ~MCAN_CCCR_BRSE_MASK;
  804. }
  805. /**
  806. * @brief Enable Auto Retransmission
  807. * @param [in] ptr MCAN base
  808. */
  809. static inline void mcan_enable_auto_retransmission(MCAN_Type *ptr)
  810. {
  811. ptr->CCCR &= ~MCAN_CCCR_DAR_MASK;
  812. }
  813. /**
  814. * @brief Disable Auto Transmission
  815. * @param [in] ptr MCAN base
  816. */
  817. static inline void mcan_disable_auto_retransmission(MCAN_Type *ptr)
  818. {
  819. ptr->CCCR |= MCAN_CCCR_DAR_MASK;
  820. }
  821. /**
  822. * @brief Enable Bus monitoring Mode
  823. * @param [in] ptr MCAN base
  824. */
  825. static inline void mcan_enable_bus_monitoring_mode(MCAN_Type *ptr)
  826. {
  827. ptr->CCCR |= MCAN_CCCR_MON_MASK;
  828. }
  829. /**
  830. * @brief Stop MCAN clock
  831. * @param [in] ptr MCAN base
  832. */
  833. static inline void mcan_stop_clock(MCAN_Type *ptr)
  834. {
  835. ptr->CCCR |= MCAN_CCCR_CSR_MASK;
  836. }
  837. /**
  838. * @brief Enable MCAN clock
  839. * @param [in] ptr MCAN base
  840. */
  841. static inline void mcan_enable_clock(MCAN_Type *ptr)
  842. {
  843. ptr->CCCR &= ~MCAN_CCCR_CSR_MASK;
  844. }
  845. static inline bool mcan_is_clock_enabled(MCAN_Type *ptr)
  846. {
  847. return ((ptr->CCCR & MCAN_CCCR_CSR_MASK) == 0UL);
  848. }
  849. /**
  850. * @brief Disable Bus monitoring Mode
  851. * @param [in] ptr MCAN base
  852. */
  853. static inline void mcan_disable_bus_monitoring_mode(MCAN_Type *ptr)
  854. {
  855. ptr->CCCR &= ~MCAN_CCCR_MON_MASK;
  856. }
  857. /**
  858. * @brief Check whether CAN clock is stopped or not
  859. * @param [in] ptr MCAN base
  860. */
  861. static inline bool mcan_is_clock_stopped(MCAN_Type *ptr)
  862. {
  863. return ((ptr->CCCR & MCAN_CCCR_CSA_MASK) != 0U);
  864. }
  865. /**
  866. * @brief Enable Restricted Operation Mode
  867. * @param [in] ptr MCAN base
  868. */
  869. static inline void mcan_enable_restricted_operation_mode(MCAN_Type *ptr)
  870. {
  871. ptr->CCCR |= MCAN_CCCR_ASM_MASK;
  872. }
  873. /**
  874. * @brief Disable Restricted Operation Mode
  875. * @param [in] ptr MCAN base
  876. */
  877. static inline void mcan_disable_restricted_operation_mode(MCAN_Type *ptr)
  878. {
  879. ptr->CCCR &= ~MCAN_CCCR_ASM_MASK;
  880. }
  881. /**
  882. * @brief Enable Write Access to Protected Configuration Registers
  883. * @param [in] ptr MCAN base
  884. */
  885. static inline void mcan_enable_write_to_prot_config_registers(MCAN_Type *ptr)
  886. {
  887. ptr->CCCR |= MCAN_CCCR_CCE_MASK;
  888. }
  889. /**
  890. * @brief Disable Write Access to Protected Configuration Registers
  891. * @param [in] ptr MCAN base
  892. */
  893. static inline void mcan_disable_write_to_prot_config_registers(MCAN_Type *ptr)
  894. {
  895. ptr->CCCR &= ~MCAN_CCCR_CCE_MASK;
  896. }
  897. /**
  898. * @brief Get Timestamp Counter Value
  899. * @param [in] ptr MCAN base
  900. * @return timestamp value
  901. */
  902. static inline uint16_t mcan_get_timestamp_counter_value(MCAN_Type *ptr)
  903. {
  904. return ptr->TSCV;
  905. }
  906. /**
  907. * @brief Switch MCAN to Initialization mode
  908. * @param [in] ptr MCAN base
  909. */
  910. static inline void mcan_enter_init_mode(MCAN_Type *ptr)
  911. {
  912. ptr->CCCR |= MCAN_CCCR_INIT_MASK;
  913. }
  914. /**
  915. * @brief Switch MCAN to Normal mode
  916. * @param [in] ptr MCAN base
  917. */
  918. static inline void mcan_enter_normal_mode(MCAN_Type *ptr)
  919. {
  920. ptr->CCCR &= ~MCAN_CCCR_INIT_MASK;
  921. }
  922. /**
  923. * @brief Get Timeout value
  924. * @param [in] ptr MCAN base
  925. * @return timeout value
  926. */
  927. static inline uint16_t mcan_get_timeout_counter_value(MCAN_Type *ptr)
  928. {
  929. return ptr->TOCV;
  930. }
  931. /**
  932. * @brief Reset Timeout counter value
  933. *
  934. * @param [in] ptr MCAN base
  935. */
  936. static inline void mcan_reset_timeout_counter_value(MCAN_Type *ptr)
  937. {
  938. *((volatile uint32_t *) &ptr->TOCV) = 0;
  939. }
  940. /**
  941. * @brief Get Error Counter Information
  942. * @param [in] ptr MCAN base
  943. * @param [out] err_cnt Error Count buffer
  944. */
  945. static inline void mcan_get_error_counter(MCAN_Type *ptr, mcan_error_count_t *err_cnt)
  946. {
  947. uint32_t ecr = ptr->ECR;
  948. err_cnt->transmit_error_count = MCAN_ECR_TEC_GET(ecr);
  949. err_cnt->receive_error_count = MCAN_ECR_REC_GET(ecr);
  950. err_cnt->receive_error_passive = (MCAN_ECR_RP_GET(ecr) == 1U);
  951. err_cnt->can_error_logging_count = MCAN_ECR_CEL_GET(ecr);
  952. }
  953. /**
  954. * @brief Get Last Error Code
  955. * @param [in] ptr MCAN base
  956. * @return Last Error code
  957. */
  958. static inline uint8_t mcan_get_last_error_code(MCAN_Type *ptr)
  959. {
  960. return MCAN_PSR_LEC_GET(ptr->PSR);
  961. }
  962. /**
  963. * @brief Get Last Data Phase Error Code
  964. * @param [in] ptr MCAN base
  965. * @return Last Error Code in Data phase
  966. */
  967. static inline uint8_t mcan_get_last_data_error_code(MCAN_Type *ptr)
  968. {
  969. return MCAN_PSR_DLEC_GET(ptr->PSR);
  970. }
  971. /**
  972. * @brief Get CAN Activity
  973. * @param [in] ptr MCAN base
  974. * @return CAN IP activity
  975. */
  976. static inline uint8_t mcan_get_activity(MCAN_Type *ptr)
  977. {
  978. return MCAN_PSR_ACT_GET(ptr->PSR);
  979. }
  980. /**
  981. * @brief Check whether the CAN node is under error passive state
  982. * @param [in] ptr MCAN base
  983. * @return true is CAN is under error passive state
  984. */
  985. static inline bool mcan_is_in_err_passive_state(MCAN_Type *ptr)
  986. {
  987. return (MCAN_PSR_EP_GET(ptr->PSR) == 1U);
  988. }
  989. /**
  990. * @brief Check whether the CAN mode is under Warning State
  991. * @param [in] ptr MCAN base
  992. * @return true if the CAN node is under Error Warning State
  993. */
  994. static inline bool mcan_is_in_error_warning_state(MCAN_Type *ptr)
  995. {
  996. return (MCAN_PSR_EW_GET(ptr->PSR) == 1U);
  997. }
  998. /**
  999. * @brief Check whether the CAN node is under Bus-off state
  1000. * @param [in] ptr MCAN base
  1001. * @return true if the CAN node is under Bus-off state
  1002. */
  1003. static inline bool mcan_is_in_busoff_state(MCAN_Type *ptr)
  1004. {
  1005. return (MCAN_PSR_BO_GET(ptr->PSR) == 1U);
  1006. }
  1007. /**
  1008. * @brief Get the Last Data Phase Error
  1009. * @param [in] ptr MCAN base
  1010. * @deprecated This API will be removed in later SDK release
  1011. * @return The last Data Phase Error
  1012. */
  1013. static inline uint8_t mcan_get_data_phase_last_error_code(MCAN_Type *ptr)
  1014. {
  1015. return MCAN_PSR_DLEC_GET(ptr->PSR);
  1016. }
  1017. /**
  1018. * @brief Check Whether the Error Status Indicator Flag is set in the last received CANFD message
  1019. * @param [in] ptr MCAN base
  1020. * @return true if the Error Status Indicator Flag is set in the last received CANFD Message
  1021. */
  1022. static inline bool mcan_is_error_state_indicator_flag_set_in_last_received_canfd_msg(MCAN_Type *ptr)
  1023. {
  1024. return (MCAN_PSR_RESI_GET(ptr->PSR) == 1U);
  1025. }
  1026. /**
  1027. * @brief Check whether the Bitrate Switch Flag is set in the last received CANFD message
  1028. * @param [in] ptr MCAN base
  1029. * @return true if Bit rate switch flag is set in the last received CANFD message
  1030. */
  1031. static inline bool mcan_is_bitrate_switch_flag_set_in_last_received_canfd_msg(MCAN_Type *ptr)
  1032. {
  1033. return (MCAN_PSR_RBRS_GET(ptr->PSR) == 1U);
  1034. }
  1035. /**
  1036. * @brief Check whether CANFD messages were received
  1037. * @param [in] ptr MCAN base
  1038. * @return true if a CANFD message was received
  1039. */
  1040. static inline bool mcan_is_canfd_message_received(MCAN_Type *ptr)
  1041. {
  1042. return (MCAN_PSR_RFDF_GET(ptr->PSR) == 1U);
  1043. }
  1044. /**
  1045. * @brief Check whether Protocol Exception Events were occurred
  1046. * @param [in] ptr MCAN base
  1047. * @return true if Protocol Exception Events were occurred
  1048. */
  1049. static inline bool mcan_is_protocol_exception_event_occurred(MCAN_Type *ptr)
  1050. {
  1051. return (MCAN_PSR_PXE_GET(ptr->PSR) == 1U);
  1052. }
  1053. /**
  1054. * @brief Get the Transmitter Delay Compensation Value
  1055. * @param [in] ptr MCAN base
  1056. * @return Transmitter Delay Compensation value
  1057. */
  1058. static inline uint8_t mcan_get_transmitter_delay_compensation_value(MCAN_Type *ptr)
  1059. {
  1060. return MCAN_PSR_TDCV_GET(ptr->PSR);
  1061. }
  1062. /**
  1063. * @brief Get Interrupt Flags
  1064. * @param [in] ptr MCAN base
  1065. * @return Interrupt Flags
  1066. */
  1067. static inline uint32_t mcan_get_interrupt_flags(MCAN_Type *ptr)
  1068. {
  1069. return ptr->IR;
  1070. }
  1071. /**
  1072. * @brief Clear Interrupt Flags
  1073. * @param [in] ptr MCAN base
  1074. * @param [in] mask Interrupt Mask
  1075. */
  1076. static inline void mcan_clear_interrupt_flags(MCAN_Type *ptr, uint32_t mask)
  1077. {
  1078. ptr->IR = mask;
  1079. }
  1080. /**
  1081. * @brief Enable Interrupts
  1082. * @param [in] ptr MCAN base
  1083. * @param [in] mask Interrupt mask
  1084. */
  1085. static inline void mcan_enable_interrupts(MCAN_Type *ptr, uint32_t mask)
  1086. {
  1087. ptr->ILS &= ~mask;
  1088. ptr->IE |= mask;
  1089. ptr->ILE = 1U;
  1090. }
  1091. /**
  1092. * @brief Enable TXBUF Interrupt
  1093. * @deprecated This API is deprecated, will be removed in later SDK release
  1094. * @param [in] ptr MCAN base
  1095. * @param [in] mask Interrupt mask
  1096. */
  1097. static inline void mcan_enable_txbuf_interrupt(MCAN_Type *ptr, uint32_t mask)
  1098. {
  1099. ptr->TXBTIE |= mask;
  1100. }
  1101. /**
  1102. * @brief Disable TXBUF Interrupt
  1103. * @deprecated This API is deprecated, will be removed in later SDK release
  1104. * @param [in] ptr MCAN base
  1105. * @param [in] mask Interrupt mask
  1106. */
  1107. static inline void mcan_disable_txbuf_interrupt(MCAN_Type *ptr, uint32_t mask)
  1108. {
  1109. ptr->TXBTIE &= ~mask;
  1110. }
  1111. /**
  1112. * @brief Disable Interrupts
  1113. * @param [in] ptr MCAN base
  1114. * @param [in] mask Interrupt mask
  1115. */
  1116. static inline void mcan_disable_interrupts(MCAN_Type *ptr, uint32_t mask)
  1117. {
  1118. ptr->IE &= ~mask;
  1119. }
  1120. /**
  1121. * @brief Enable TXBUF Transmission interrupt
  1122. * @param [in] ptr MCAN base
  1123. * @param [in] mask Interrupt mask
  1124. */
  1125. static inline void mcan_enable_txbuf_transmission_interrupt(MCAN_Type *ptr, uint32_t mask)
  1126. {
  1127. ptr->TXBTIE |= mask;
  1128. }
  1129. /**
  1130. * @brief Disable TXBUF Transmission interrupt
  1131. * @param [in] ptr MCAN base
  1132. * @param [in] mask Interrupt mask
  1133. */
  1134. static inline void mcan_disable_txbuf_transmission_interrupt(MCAN_Type *ptr, uint32_t mask)
  1135. {
  1136. ptr->TXBTIE &= ~mask;
  1137. }
  1138. /**
  1139. * @brief Enable TXBUF Cancellation Finish interrupt
  1140. * @param [in] ptr MCAN base
  1141. * @param [in] mask Interrupt mask
  1142. */
  1143. static inline void mcan_enable_txbuf_cancel_finish_interrupt(MCAN_Type *ptr, uint32_t mask)
  1144. {
  1145. ptr->TXBCIE |= mask;
  1146. }
  1147. /**
  1148. * @brief Disable TXBUF Cancellation Finish interrupt
  1149. * @param [in] ptr MCAN base
  1150. * @param [in] mask Interrupt mask
  1151. */
  1152. static inline void mcan_disable_txbuf_cancel_finish_interrupt(MCAN_Type *ptr, uint32_t mask)
  1153. {
  1154. ptr->TXBCIE &= ~mask;
  1155. }
  1156. /**
  1157. * @brief Select Interrupt Line
  1158. * @param [in] ptr MCAN base
  1159. * @param [in] mask Interrupt mask
  1160. * @param [in] line_index Interrupt Line Index
  1161. */
  1162. static inline void mcan_interrupt_line_select(MCAN_Type *ptr, uint32_t mask, uint8_t line_index)
  1163. {
  1164. if (line_index == 0) {
  1165. ptr->ILS &= ~mask;
  1166. } else {
  1167. ptr->ILS |= mask;
  1168. }
  1169. ptr->ILE = (1UL << line_index);
  1170. }
  1171. /**
  1172. * @brief Check whether a TXFIFO/TXBUF transmission request is pending
  1173. * @param [in] ptr CAN Base
  1174. * @param [in] index TXBUF/TXFIFO Index
  1175. * @return True is the specified TXFIFO/TXBUF Transmission request is pending
  1176. */
  1177. static inline bool mcan_is_transmit_request_pending(MCAN_Type *ptr, uint32_t index)
  1178. {
  1179. return ((ptr->TXBRP & ((1UL << index))) != 0U);
  1180. }
  1181. /**
  1182. * @brief Check whether TXFIFO is full
  1183. * @param [in] ptr MCAN base
  1184. * @return true if TXFIFO is full
  1185. */
  1186. static inline bool mcan_is_txfifo_full(MCAN_Type *ptr)
  1187. {
  1188. return ((ptr->TXFQS & MCAN_TXFQS_TFQF_MASK) != 0U);
  1189. }
  1190. /**
  1191. * @brief Get the TXFIFO Put Index
  1192. * @param [in] ptr MCAN base
  1193. * @return The TX FIFO Put Index
  1194. */
  1195. static inline uint32_t mcan_get_txfifo_put_index(MCAN_Type *ptr)
  1196. {
  1197. return MCAN_TXFQS_TFQPI_GET(ptr->TXFQS);
  1198. }
  1199. /**
  1200. * @brief Request A Transmission via specified TXBUF Index
  1201. * @param [in] ptr MCAN Base
  1202. * @param [in] index TXBUF index
  1203. */
  1204. static inline void mcan_send_add_request(MCAN_Type *ptr, uint32_t index)
  1205. {
  1206. ptr->TXBAR = (1UL << index);
  1207. }
  1208. /**
  1209. * @brief Request several transmission via specified TXBUF/FIFO Bit masks
  1210. * MCAN IP will transmit data in the buffer if corresponding bit in index_bitmask is asserted
  1211. * @param [in] ptr MCAN Base
  1212. * @param [in] index_bitmask TXFIFO/BUF bit masks
  1213. */
  1214. static inline void mcan_send_add_multiple_requests(MCAN_Type *ptr, uint32_t index_bitmask)
  1215. {
  1216. ptr->TXBAR = index_bitmask;
  1217. }
  1218. /**
  1219. * @brief Cancel the TXBUF Send request
  1220. * @param [in] ptr MCAN Base
  1221. * @param [in] index TXBUF index
  1222. */
  1223. static inline void mcan_cancel_tx_buf_send_request(MCAN_Type *ptr, uint32_t index)
  1224. {
  1225. ptr->TXBCR = (1UL << index);
  1226. }
  1227. /**
  1228. * @brief Check whether the Transmission completed via specified TXBUF/TXFIFO
  1229. * @param [in] ptr MCAN base
  1230. * @param [in] index TXBUF Index
  1231. * @return True is the Transmission completed via specified TXBUF
  1232. */
  1233. static inline bool mcan_is_transmit_occurred(MCAN_Type *ptr, uint32_t index)
  1234. {
  1235. return ((ptr->TXBTO & (1UL << index)) != 0U);
  1236. }
  1237. /**
  1238. * @brief Check Whether there are data available in specified RXBUF
  1239. * @param [in] ptr MCAN Base
  1240. * @param [in] index RXBUF index
  1241. * @return True if there are data available
  1242. */
  1243. static inline bool mcan_is_rxbuf_data_available(MCAN_Type *ptr, uint32_t index)
  1244. {
  1245. bool result;
  1246. if (index < 32U) {
  1247. result = (ptr->NDAT1 & (1UL << index)) != 0U;
  1248. } else if (index < 64U) {
  1249. result = (ptr->NDAT2 & (1UL << (index - 32U))) != 0U;
  1250. } else {
  1251. result = false;
  1252. }
  1253. return result;
  1254. }
  1255. /**
  1256. * @brief Clear RXBUF Data available Flag for specified RXBUF
  1257. * @param [in] ptr MCAN base
  1258. * @param [in] index RXBUF index
  1259. */
  1260. static inline void mcan_clear_rxbuf_data_available_flag(MCAN_Type *ptr, uint32_t index)
  1261. {
  1262. if (index < 32U) {
  1263. ptr->NDAT1 = (1UL << index);
  1264. } else if (index < 64U) {
  1265. ptr->NDAT2 = (1UL << (index - 32U));
  1266. } else {
  1267. /* Do nothing */
  1268. }
  1269. }
  1270. /**
  1271. * @brief Check whether specified Interrupt is set
  1272. * @param [in] ptr MCAN Base
  1273. * @param [in] mask Interrupt Flags
  1274. * @retval true if corresponding bits are set
  1275. */
  1276. static inline bool mcan_is_interrupt_flag_set(MCAN_Type *ptr, uint32_t mask)
  1277. {
  1278. return ((ptr->IR & mask) != 0U);
  1279. }
  1280. /**
  1281. * @brief Check whether the TSU timestamp is available
  1282. *
  1283. * @param [in] ptr MCAN base
  1284. * @param [in] index Timestamp pointer
  1285. * @retval true TSU Timestamp is available
  1286. * @retval false TSU timestamp is unavailable
  1287. */
  1288. static inline bool mcan_is_tsu_timestamp_available(MCAN_Type *ptr, uint32_t index)
  1289. {
  1290. bool is_available = false;
  1291. if (index < ARRAY_SIZE(ptr->TS_SEL)) {
  1292. is_available = ((ptr->TSS1 & (1UL << index)) != 0U);
  1293. }
  1294. return is_available;
  1295. }
  1296. /**
  1297. * @brief Read 32bit Timestamp value from TSU
  1298. * @param [in] ptr MCAN Base
  1299. * @param [in] index Timestamp pointer
  1300. * @return Timestamp value
  1301. */
  1302. uint32_t mcan_read_32bit_tsu_timestamp(MCAN_Type *ptr, uint32_t index);
  1303. /**
  1304. * @brief Read 64bit Timestamp value from TSU
  1305. * @param [in] ptr MCAN Base
  1306. * @param [in] index Timestamp pointer
  1307. * @return Timestamp value
  1308. */
  1309. uint64_t mcan_read_64bit_tsu_timestamp(MCAN_Type *ptr, uint32_t index);
  1310. /**
  1311. * @brief Get High Priority Message Status
  1312. * @param [in] ptr MCAN base
  1313. * @param [out] status Pointer to High Priority Message Status Buffer
  1314. */
  1315. void mcan_get_high_priority_message_status(MCAN_Type *ptr, mcan_high_priority_message_status_t *status);
  1316. /**
  1317. * @brief Get Default CAN configuration
  1318. * @param [in] ptr MCAN base
  1319. * @param [out] config CAN configuration
  1320. */
  1321. void mcan_get_default_config(MCAN_Type *ptr, mcan_config_t *config);
  1322. /**
  1323. * @brief Get message Size from Data Length Code
  1324. * @param [in] dlc Data Length Code
  1325. * @return Message Size in Bytes
  1326. */
  1327. uint8_t mcan_get_message_size_from_dlc(uint8_t dlc);
  1328. /**
  1329. * @brief Get the Data field size from data field size option
  1330. *
  1331. * @param [in] data_field_size_option Data size option
  1332. * @return data field size in bytes
  1333. */
  1334. uint8_t mcan_get_data_field_size(uint8_t data_field_size_option);
  1335. /**
  1336. * @brief Get Default Full MCAN RAM configuration
  1337. * If the device is configured as classic CAN node, the default CAN RAM settings are as below:
  1338. * - Standard Identifier Filter Elements: 32
  1339. * - Extended Identifier Filter Elements: 32
  1340. * - TXBUF Elements Info:
  1341. * - Element Count:32
  1342. * - Dedicated TXBUF element count: 16
  1343. * - TXFIFO/QQueue element count: 16
  1344. * - Data Field Size: 8
  1345. * .
  1346. * - RXFIFO0 Elements Info:
  1347. * - Element Count :32
  1348. * - Data Field Size: 8
  1349. * .
  1350. * - RXFIFO1 Elements Info:
  1351. * - Element Count : 32
  1352. * - Data Field Size: 8
  1353. * .
  1354. * - RXBUF Element Info:
  1355. * - Element Count: 16
  1356. * - Data Field Size : 8
  1357. * .
  1358. * - TX Event FIFO Element Count: 32
  1359. * .
  1360. * If the device is configured as CANFD node, the default CAN RAM settings are as below:
  1361. * - Standard Identifier Filter Elements: 16
  1362. * - Extended Identifier Filter Elements: 16
  1363. * - TXBUF Elements Info:
  1364. * - Element Count:8
  1365. * - Dedicated TXBUF element count: 4
  1366. * - TXFIFO/QQueue element count: 4
  1367. * - Data Field Size: 64
  1368. * .
  1369. * - RXFIFO0 Elements Info:
  1370. * - Element Count : 8
  1371. * - Data Field Size: 64
  1372. * .
  1373. * - RXFIFO1 Elements Info:
  1374. * - Element Count : 8
  1375. * - Data Field Size: 64
  1376. * .
  1377. * - RXBUF Element Info:
  1378. * - Element Count: 4
  1379. * - Data Field Size : 64
  1380. * .
  1381. * - TX Event FIFO Element Count: 8
  1382. * .
  1383. * @param [in] ptr MCAN base
  1384. * @param [out] ram_config CAN RAM Configuration
  1385. * @param [in] enable_canfd CANFD enable flag
  1386. */
  1387. void mcan_get_default_ram_flexible_config(MCAN_Type *ptr, mcan_ram_flexible_config_t *ram_config, bool enable_canfd);
  1388. /**
  1389. * @brief Get Default MCAN RAM configuration
  1390. * If the device is configured as classic CAN node, the default CAN RAM settings are as below:
  1391. * - Standard Identifier Filter Elements: 32
  1392. * - Extended Identifier Filter Elements: 32
  1393. * - TXBUF Elements Info:
  1394. * - Element Count:32
  1395. * - Dedicated TXBUF element count: 16
  1396. * - TXFIFO/QQueue element count: 16
  1397. * - Data Field Size: 8
  1398. * .
  1399. * - RXFIFO0 Elements Info:
  1400. * - Element Count :32
  1401. * - Data Field Size: 8
  1402. * .
  1403. * - RXFIFO1 Elements Info:
  1404. * - Element Count : 32
  1405. * - Data Field Size: 8
  1406. * .
  1407. * - RXBUF Element Info:
  1408. * - Element Count: 16
  1409. * - Data Field Size : 8
  1410. * .
  1411. * - TX Event FIFO Element Count: 32
  1412. * .
  1413. * If the device is configured as CANFD node, the default CAN RAM settings are as below:
  1414. * - Standard Identifier Filter Elements: 16
  1415. * - Extended Identifier Filter Elements: 16
  1416. * - TXBUF Elements Info:
  1417. * - Element Count:8
  1418. * - Dedicated TXBUF element count: 4
  1419. * - TXFIFO/QQueue element count: 4
  1420. * - Data Field Size: 64
  1421. * .
  1422. * - RXFIFO0 Elements Info:
  1423. * - Element Count : 8
  1424. * - Data Field Size: 64
  1425. * .
  1426. * - RXFIFO1 Elements Info:
  1427. * - Element Count : 8
  1428. * - Data Field Size: 64
  1429. * .
  1430. * - RXBUF Element Info:
  1431. * - Element Count: 4
  1432. * - Data Field Size : 64
  1433. * .
  1434. * - TX Event FIFO Element Count: 8
  1435. * .
  1436. * @param [in] ptr MCAN base
  1437. * @param [out] simple_config Simple CAN RAM Configuration
  1438. * @param [in] enable_canfd CANFD enable flag
  1439. */
  1440. void mcan_get_default_ram_config(MCAN_Type *ptr, mcan_ram_config_t *simple_config, bool enable_canfd);
  1441. /**
  1442. * @brief Initialize CAN controller
  1443. * @param [in] ptr MCAN base
  1444. * @param [in] config CAN configuration
  1445. * @param [in] src_clk_freq CAN clock frequency
  1446. * @retval status_success if operation is successful
  1447. * @retval status_invalid_argument if any parameters are invalid
  1448. */
  1449. hpm_stat_t mcan_init(MCAN_Type *ptr, mcan_config_t *config, uint32_t src_clk_freq);
  1450. /**
  1451. * @brief De-Initialize CAN controller
  1452. *
  1453. * @param [in] ptr MCAN base
  1454. */
  1455. void mcan_deinit(MCAN_Type *ptr);
  1456. /**
  1457. * @brief Configure MCAN RAM will Full RAM configuration
  1458. * @param [in] ptr MCAN base
  1459. * @param [in] config MCAN RAM Full Configuration
  1460. * @return status_success if no errors reported
  1461. */
  1462. hpm_stat_t mcan_config_ram_with_flexible_config(MCAN_Type *ptr, mcan_ram_flexible_config_t *config);
  1463. /**
  1464. * @brief Configure MCAN RAM will Simplified RAM configuration
  1465. * @param [in] ptr MCAN base
  1466. * @param [in] config MCAN RAM configuration
  1467. * @return status_success if no errors reported
  1468. */
  1469. hpm_stat_t mcan_config_ram(MCAN_Type *ptr, mcan_ram_config_t *config);
  1470. /**
  1471. * @brief Configure All CAN filters
  1472. * @param [in] ptr MCAN base
  1473. * @param [in] config All CAN Filter configuration
  1474. * @return status_success if no errors reported
  1475. */
  1476. hpm_stat_t mcan_config_all_filters(MCAN_Type *ptr, mcan_all_filters_config_t *config);
  1477. /**
  1478. * @brief Configure Transmitter Delay Compensation
  1479. * @param [in] ptr MCAN base
  1480. * @param [in] config Transmitter Delay compensation configure
  1481. * @return status_success if no errors reported
  1482. */
  1483. hpm_stat_t mcan_config_transmitter_delay_compensation(MCAN_Type *ptr, mcan_tdc_config_t *config);
  1484. /**
  1485. * @brief Configure Global Filter
  1486. * @param [in] ptr MCAN base
  1487. * @param [in] filter_config Global Filter Configuration
  1488. * @return status_success if no errors reported
  1489. */
  1490. hpm_stat_t mcan_set_global_filter_config(MCAN_Type *ptr, mcan_global_filter_config_t *filter_config);
  1491. /**
  1492. * @brief Set CAN filter element
  1493. * @param [in] ptr MCAN base
  1494. * @param [in] filter_elem CAN filter element
  1495. * @param [in] index CAN Filter element index
  1496. * @return status_success if no errors reported
  1497. */
  1498. hpm_stat_t mcan_set_filter_element(MCAN_Type *ptr, const mcan_filter_elem_t *filter_elem, uint32_t index);
  1499. /**
  1500. * @brief Write Frame to CAN TX Buffer
  1501. * @param [in] ptr MCAN base
  1502. * @param [in] index TX Buffer Index
  1503. * @param [in] tx_frame TX frame
  1504. * @return status_success if no errors reported
  1505. */
  1506. hpm_stat_t mcan_write_txbuf(MCAN_Type *ptr, uint32_t index, mcan_tx_frame_t *tx_frame);
  1507. /**
  1508. * @brief Write Frame CAN to TXFIFO
  1509. * @param [in] ptr MCAN base
  1510. * @param [in] tx_frame TX frame
  1511. * @return status_success if no errors reported
  1512. */
  1513. hpm_stat_t mcan_write_txfifo(MCAN_Type *ptr, mcan_tx_frame_t *tx_frame);
  1514. /**
  1515. * @brief Read message from CAN RXBUF
  1516. * @param [in] ptr MCAN base
  1517. * @param [in] index Index of RXBUF
  1518. * @param [out] rx_frame Buffer to hold RX frame
  1519. * @return status_success if no errors reported
  1520. */
  1521. hpm_stat_t mcan_read_rxbuf(MCAN_Type *ptr, uint32_t index, mcan_rx_message_t *rx_frame);
  1522. /**
  1523. * @brief Read message from CAN RXBUF
  1524. * @param [in] ptr MCAN base
  1525. * @param [in] fifo_index RXFIFO index, 0 - RXFO0, 1 - RXFIFO1
  1526. * @param [out] rx_frame Buffer to hold RX frame
  1527. * @return status_success if no errors reported
  1528. */
  1529. hpm_stat_t mcan_read_rxfifo(MCAN_Type *ptr, uint32_t fifo_index, mcan_rx_message_t *rx_frame);
  1530. /**
  1531. * @brief Read TX Event from CAN TX EVENT FIFO
  1532. * @param [in] ptr MCAN base
  1533. * @param [out] tx_evt TX Event Buffer
  1534. * @retval status_success if no errors happened
  1535. * @retval status_mcan_tx_evt_fifo_empty if there are no TX events available
  1536. * @retval status_invalid_argument if any parameters are invalid
  1537. */
  1538. hpm_stat_t mcan_read_tx_evt_fifo(MCAN_Type *ptr, mcan_tx_event_fifo_elem_t *tx_evt);
  1539. /**
  1540. * @brief Transmit CAN message in blocking way
  1541. * @param [in] ptr MCAN base
  1542. * @param [in] tx_frame CAN Transmit Message buffer
  1543. * @return status_success if no errors reported
  1544. */
  1545. hpm_stat_t mcan_transmit_blocking(MCAN_Type *ptr, mcan_tx_frame_t *tx_frame);
  1546. /**
  1547. * @brief Request TXFIFO and fill data into TXFIFO
  1548. * @note This API can be used to prepare the data for several CAN frames prior to transmission.
  1549. * After this operation, software can call `mcan_send_add_request(ptr, *fifo_index)` API to trigger a transmission
  1550. * or call `mcan_send_add_requests(ptr, fifo_index_masks)` to trigger several transmissions
  1551. * @param [in] ptr MCAN base
  1552. * @param [in] tx_frame CAN Transmit Message buffer
  1553. * @param [out] fifo_index The index of the element in FIFO assigned to the tx_frame
  1554. * @return status_success if no errors reported
  1555. */
  1556. hpm_stat_t mcan_request_and_fill_txfifo(MCAN_Type *ptr, mcan_tx_frame_t *tx_frame, uint32_t *fifo_index);
  1557. /**
  1558. * @brief Transmit CAN message via TX FIFO in non-blocking way
  1559. * @param [in] ptr MCAN base
  1560. * @param [in] tx_frame CAN Transmit Message buffer
  1561. * @param [out] fifo_index The index of the element in FIFO assigned to the tx_frame
  1562. *
  1563. * @return status_success if no errors reported
  1564. */
  1565. hpm_stat_t mcan_transmit_via_txfifo_nonblocking(MCAN_Type *ptr, mcan_tx_frame_t *tx_frame, uint32_t *fifo_index);
  1566. /**
  1567. * @brief Transmit CAN message via TX in non-blocking way
  1568. * @param [in] ptr MCAN base
  1569. * @param [in] index Index of TX Buffer
  1570. * @param [in] tx_frame CAN Transmit Message buffer
  1571. * @return status_success if no errors reported
  1572. */
  1573. hpm_stat_t mcan_transmit_via_txbuf_nonblocking(MCAN_Type *ptr, uint32_t index, mcan_tx_frame_t *tx_frame);
  1574. /**
  1575. * @brief Receive CAN Frame from RXBUF in blocking way
  1576. * @param [in] ptr MCAN base
  1577. * @param [in] index RXBUF index
  1578. * @param [out] rx_frame Buffer to hold Received Frame
  1579. * @return status_success if no errors reported
  1580. */
  1581. hpm_stat_t mcan_receive_from_buf_blocking(MCAN_Type *ptr, uint32_t index, mcan_rx_message_t *rx_frame);
  1582. /**
  1583. * @brief Receive CAN Frame from RXFIFO in blocking way
  1584. * @param [in] ptr MCAN base
  1585. * @param [in] fifo_index RXFIFO index, 0 - RXFIFO0, 1 - RXFIFO1
  1586. * @param [out] rx_frame Buffer to hold Received Frame
  1587. * @return status_success if no errors reported
  1588. */
  1589. hpm_stat_t mcan_receive_from_fifo_blocking(MCAN_Type *ptr, uint32_t fifo_index, mcan_rx_message_t *rx_frame);
  1590. /**
  1591. * @brief Get Timestamp from MCAN TX Event
  1592. * @param [in] ptr MCAN base
  1593. * @param [in] tx_evt TX Event Element
  1594. * @param [out] timestamp Timestamp value
  1595. * @retval status_success if no errors happened
  1596. * @retval status_invalid_argument if any parameters are invalid
  1597. * @retval status_mcan_timestamp_not_exist if no timestamp information is available
  1598. */
  1599. hpm_stat_t mcan_get_timestamp_from_tx_event(MCAN_Type *ptr,
  1600. const mcan_tx_event_fifo_elem_t *tx_evt,
  1601. mcan_timestamp_value_t *timestamp);
  1602. /**
  1603. * @brief Get Timestamp from MCAN RX frame
  1604. * @param [in] ptr MCAN base
  1605. * @param [in] rx_msg Received message
  1606. * @param [out] timestamp Timestamp value
  1607. * @retval status_success if no errors happened
  1608. * @retval status_invalid_argument if any parameters are invalid
  1609. * @retval status_mcan_timestamp_not_exist if no timestamp information is available
  1610. */
  1611. hpm_stat_t mcan_get_timestamp_from_received_message(MCAN_Type *ptr,
  1612. const mcan_rx_message_t *rx_msg,
  1613. mcan_timestamp_value_t *timestamp);
  1614. /**
  1615. * @brief Parse the Protocol Status register value
  1616. * @param [in] psr Protocol Status Register Value
  1617. * @param [out] protocol_status Translated Protocol Status
  1618. *
  1619. * @retval status_invalid_argument if any parameters are invalid
  1620. * @retval status_success if no errors happened
  1621. */
  1622. hpm_stat_t mcan_parse_protocol_status(uint32_t psr, mcan_protocol_status_t *protocol_status);
  1623. /**
  1624. * @brief Get MCAN Protocol Status
  1625. * @param [in] ptr MCAN base
  1626. * @param [out] protocol_status Translated Protocol status
  1627. *
  1628. * @retval status_invalid_argument if any parameters are invalid
  1629. * @retval status_success if no errors happened
  1630. */
  1631. hpm_stat_t mcan_get_protocol_status(MCAN_Type *ptr, mcan_protocol_status_t *protocol_status);
  1632. /**
  1633. * @}
  1634. *
  1635. */
  1636. #ifdef __cplusplus
  1637. }
  1638. #endif
  1639. #endif /* HPM_MCAN_DRV_H */