hpm_can_drv.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Copyright (c) 2021-2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_CAN_DRV_H
  8. #define HPM_CAN_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_can_regs.h"
  11. #include "hpm_soc_feature.h"
  12. /**
  13. * @brief CAN driver APIs
  14. * @defgroup can_interface CAN driver APIs
  15. * @ingroup can_interfaces
  16. * @{
  17. *
  18. */
  19. /***********************************************************************************************************************
  20. *
  21. * Definitions
  22. *
  23. **********************************************************************************************************************/
  24. /**
  25. * @brief CAN error codes
  26. */
  27. enum {
  28. status_can_bit_error = MAKE_STATUS(status_group_can, 0), /**< CAN bit error */
  29. status_can_form_error = MAKE_STATUS(status_group_can, 1), /**< CAN Form error */
  30. status_can_stuff_error = MAKE_STATUS(status_group_can, 2), /**< CAN stuff error */
  31. status_can_ack_error = MAKE_STATUS(status_group_can, 3), /**< CAN ACK error */
  32. status_can_crc_error = MAKE_STATUS(status_group_can, 4), /**< CAN CRC error */
  33. status_can_other_error = MAKE_STATUS(status_group_can, 5), /**< CAN other error */
  34. status_can_tx_fifo_full = MAKE_STATUS(status_group_can, 6), /**< CAN TX fifo full */
  35. status_can_filter_index_invalid = MAKE_STATUS(status_group_can, 7), /**< CAN filter index is invalid */
  36. status_can_filter_num_invalid = MAKE_STATUS(status_group_can, 8), /**< CAN filter number is invalid */
  37. };
  38. /**
  39. * @brief CAN Event definitions
  40. */
  41. #define CAN_EVENT_RECEIVE (CAN_RTIF_RIF_MASK) /**< CAN Receive event */
  42. #define CAN_EVENT_RX_BUF_OVERRUN (CAN_RTIF_ROIF_MASK) /**< CAN RX buffer overrun event */
  43. #define CAN_EVENT_RX_BUF_FULL (CAN_RTIF_RFIF_MASK) /**< CAN RX buffer full event */
  44. #define CAN_EVENT_RX_BUF_ALMOST_FULL (CAN_RTIF_RAFIF_MASK) /**< CAN RX buffer almost full event */
  45. #define CAN_EVENT_TX_PRIMARY_BUF (CAN_RTIF_TPIF_MASK) /**< CAN TX primary buffer event */
  46. #define CAN_EVENT_TX_SECONDARY_BUF (CAN_RTIF_TSIF_MASK) /**< CAN TX secondary buffer event */
  47. #define CAN_EVENT_ERROR (CAN_RTIF_EIF_MASK) /**< CAN error event */
  48. #define CAN_EVENT_ABORT (CAN_RTIF_AIF_MASK) /**< CAN abort event */
  49. /**
  50. * @brief CAN Secondary Transmit buffer Status
  51. */
  52. #define CAN_STB_IS_EMPTY (0U) /**< CAN Sencondary Transmit buffer is empty */
  53. #define CAN_STB_LESS_EQUAL_HALF_FULL (1U) /**< CAN Sencondary Transmit buffer <= 1/2 * FULL */
  54. #define CAN_STB_MORE_THAN_HALF_FULL (2U) /**< CAN Sencondary Transmit buffer > 1/2 * FULL */
  55. #define CAN_STB_IS_FULL (3U) /**< CAN Sencondary Transmit buffer is full */
  56. /**
  57. * @brief CAN Receive Buffer States
  58. */
  59. #define CAN_RXBUF_IS_EMPTY (0U) /**< CAN Receive buffer is empty */
  60. #define CAN_RXBUF_HAS_DATA (1U) /**< CAN Receive buffer has data */
  61. #define CAN_RXBUF_ALMOST_FULL (2U) /**< CAN Receive buffer is almost full */
  62. #define CAN_RXBUF_IS_FULL (3U) /**< CAN Receive buffer is full */
  63. /**
  64. * @brief CAN Error interrupts/flags
  65. *
  66. */
  67. #define CAN_ERROR_WARNING_LIMIT_FLAG (CAN_ERRINT_EWARN_MASK) /**< CAN Error Limit reached */
  68. #define CAN_ERROR_PASSIVE_MODE_ACTIVE_FLAG (CAN_ERRINT_EPASS_MASK) /**< CAN Passive mode active */
  69. #define CAN_ERROR_PASSIVE_INT_ENABLE (CAN_ERRINT_EPIE_MASK) /**< CAN Passive Interrupt Enable */
  70. #define CAN_ERROR_PASSIVE_INT_FLAG (CAN_ERRINT_EPIF_MASK) /**< CAN Passive Interrupt Flag */
  71. #define CAN_ERROR_ARBITRAITION_LOST_INT_ENABLE (CAN_ERRINT_ALIE_MASK) /**< CAN Abitration Lost Interrupt Enable */
  72. #define CAN_ERROR_ARBITRAITION_LOST_INT_FLAG (CAN_ERRINT_ALIE_MASK) /**< CAN arbitration Lost Interrupt Flag */
  73. #define CAN_ERROR_BUS_ERROR_INT_ENABLE (CAN_ERRINT_BEIE_MASK) /**< CAN BUS error Interrupt Enable */
  74. #define CAN_ERROR_BUS_ERROR_INT_FLAG (CAN_ERRINT_BEIF_MASK) /**< CAN BUS error Interrupt flag */
  75. /**
  76. * @brief CAN Error Kinds
  77. *
  78. */
  79. #define CAN_KIND_OF_ERROR_NO_ERROR (0U) /**< No error */
  80. #define CAN_KIND_OF_ERROR_BIT_ERROR (1U) /**< Bit error */
  81. #define CAN_KIND_OF_ERROR_FORM_ERROR (2U) /**< Form error */
  82. #define CAN_KIND_OF_ERROR_STUFF_ERROR (3U) /**< Stuff error */
  83. #define CAN_KIND_OF_ERROR_ACK_ERROR (4U) /**< Acknowledgment error */
  84. #define CAN_KIND_OF_ERROR_CRC_ERROR (5U) /**< CRC error */
  85. #define CAN_KIND_OF_ERROR_OTHER_ERROR (6U) /**< Other errors */
  86. #define CAN_KIND_OF_ERROR_BUS_OFF (7U) /**< BUS off error */
  87. /**
  88. * @brief CAN loopback types
  89. */
  90. typedef enum _can_mode {
  91. can_mode_normal, /**< Non-loopback mode */
  92. can_mode_loopback_internal, /**< Internal loopback mode */
  93. can_mode_loopback_external, /**< External loopback mode */
  94. can_mode_listen_only, /**< CAN listen mode */
  95. } can_node_mode_t;
  96. /**
  97. * @brief CAN bit timing options
  98. */
  99. typedef enum _can_bit_timing_option {
  100. can_bit_timing_can2_0, /**< CAN 2.0 bit timing option */
  101. can_bit_timing_canfd_norminal, /**< CANFD norminal timing option */
  102. can_bit_timing_canfd_data, /**< CANFD data timing option */
  103. } can_bit_timing_option_t;
  104. /**
  105. * @brief CAN DLC definitions
  106. */
  107. enum {
  108. can_payload_size_0 = 0, /**< Payload size is 0 */
  109. can_payload_size_1, /**< Payload size is 1 */
  110. can_payload_size_2, /**< Payload size is 2 */
  111. can_payload_size_3, /**< Payload size is 3 */
  112. can_payload_size_4, /**< Payload size is 4 */
  113. can_payload_size_5, /**< Payload size is 5 */
  114. can_payload_size_6, /**< Payload size is 6 */
  115. can_payload_size_7, /**< Payload size is 7 */
  116. can_payload_size_8, /**< Payload size is 8 */
  117. can_payload_size_12, /**< Payload size is 12 */
  118. can_payload_size_16, /**< Payload size is 16 */
  119. can_payload_size_20, /**< Payload size is 20 */
  120. can_payload_size_24, /**< Payload size is 24 */
  121. can_payload_size_32, /**< Payload size is 32 */
  122. can_payload_size_48, /**< Payload size is 48 */
  123. can_payload_size_64, /**< Payload size is 64 */
  124. can_payload_size_max = can_payload_size_64,
  125. };
  126. /**
  127. * @brief CAN Bit timing parameters
  128. */
  129. typedef struct {
  130. uint16_t prescaler; /**< Prescaler value */
  131. uint16_t num_seg1; /**< Seg1 value */
  132. uint16_t num_seg2; /**< Seg2 value */
  133. uint16_t num_sjw; /**< SJW value */
  134. } can_bit_timing_param_t;
  135. /**
  136. * @brief CAN receive buffer data structure
  137. */
  138. typedef union _can_rx_buf {
  139. uint32_t buffer[20]; /**< CAN buffer size */
  140. struct {
  141. struct {
  142. uint32_t id: 29; /**< CAN ID */
  143. uint32_t : 1;
  144. uint32_t error_state_indicator: 1; /**< Error state indicator */
  145. };
  146. struct {
  147. uint32_t dlc: 4; /**< Data length option */
  148. uint32_t bitrate_switch: 1; /**< bitrate switch flag */
  149. uint32_t canfd_frame: 1; /**< CANFD frame flag */
  150. uint32_t remote_frame: 1; /**< Remote frame flag */
  151. uint32_t extend_id: 1; /**< Extended ID flag */
  152. uint32_t : 4;
  153. uint32_t loopback_message: 1; /**< Loopback message flag */
  154. uint32_t error_type: 3; /**< Error type */
  155. uint32_t cycle_time: 16; /**< Cycle time */
  156. };
  157. uint8_t data[]; /**< Data pointer */
  158. };
  159. } can_receive_buf_t;
  160. /**
  161. * @brief CAN transmit buffer data structure
  162. */
  163. typedef union _can_tx_buf {
  164. uint32_t buffer[18]; /**< CAN transmit buffer */
  165. struct {
  166. struct {
  167. uint32_t id: 29; /**< CAN ID */
  168. uint32_t : 2;
  169. uint32_t transmit_timestamp_enable: 1; /**< Timestamp enable flag */
  170. };
  171. struct {
  172. uint32_t dlc: 4; /**< Data length option */
  173. uint32_t bitrate_switch: 1; /**< bitrate switch flag */
  174. uint32_t canfd_frame: 1; /**< CANFD frame flag */
  175. uint32_t remote_frame: 1; /**< remote_frame flag */
  176. uint32_t extend_id: 1; /**< Extended ID flag */
  177. uint32_t : 24;
  178. };
  179. uint8_t data[]; /**< Data pointer */
  180. };
  181. } can_transmit_buf_t;
  182. /**
  183. * @brief CAN acceptance filter modes
  184. */
  185. typedef enum _can_filter_id_mode {
  186. can_filter_id_mode_both_frames, /**< Accept both standard frame and extended frame */
  187. can_filter_id_mode_standard_frames, /**< Accept standard frame only */
  188. can_filter_id_mode_extended_frames, /**< Accept extended frame only */
  189. } can_filter_id_mode_t;
  190. /**
  191. * @brief CAN acceptance filter configuration
  192. */
  193. typedef struct {
  194. uint16_t index; /**< CAN filter index */
  195. can_filter_id_mode_t id_mode; /**< CAN filter id mode */
  196. bool enable; /**< CAN filter enable flag */
  197. uint32_t code; /**< CAN filter code */
  198. uint32_t mask; /**< CAN filter mask */
  199. } can_filter_config_t;
  200. /**
  201. * @brief CAN configuration
  202. */
  203. typedef struct {
  204. union {
  205. struct {
  206. uint32_t baudrate; /**< CAN2.0 baudrate / CANFD nominal baudrate */
  207. uint32_t baudrate_fd; /**< CANFD data baudrate */
  208. /**< minimum sampling point, value range (0-1000), samplepoint_min/1000 will be used in driver */
  209. uint16_t can20_samplepoint_min;
  210. /**< maximum sampling point, value range (0-1000), samplepoint_max/1000 will be used in driver */
  211. uint16_t can20_samplepoint_max;
  212. /**< minimum sampling point, value range (0-1000), samplepoint_min/1000 will be used in driver */
  213. uint16_t canfd_samplepoint_min;
  214. /**< maximum sampling point, value range (0-1000), samplepoint_max/1000 will be used in driver */
  215. uint16_t canfd_samplepoint_max;
  216. };
  217. struct {
  218. can_bit_timing_param_t can_timing; /**< CAN2.0 /CANFD nominal low-level bit timing parameters */
  219. can_bit_timing_param_t canfd_timing;/**< CANFD low-level bit timing parameters */
  220. };
  221. };
  222. can_node_mode_t mode; /**< CAN work mode */
  223. bool use_lowlevel_timing_setting; /**< Use low-level timing setting */
  224. bool enable_canfd; /**< Enable CAN FD */
  225. bool enable_self_ack; /**< CAN self-ack flag */
  226. bool disable_re_transmission_for_ptb; /**< disable re-transmission for primary transmit buffer */
  227. bool disable_re_transmission_for_stb; /**< disable re-transmission for secondary transmit buffer */
  228. bool enable_tdc; /**< Enable transmittor delay compensation */
  229. uint8_t filter_list_num; /**< element number of CAN filters in filter list */
  230. can_filter_config_t *filter_list; /**< CAN filter list pointer */
  231. uint8_t irq_txrx_enable_mask; /**< CAN TX and RX IRQ Enable Mask */
  232. uint8_t irq_error_enable_mask; /**< CAN Error IRQ Enable Mask */
  233. bool enable_tx_buffer_priority_mode; /**< Enable Priority-based priority */
  234. bool enable_can_fd_iso_mode; /**< Enable CAN-FD ISO mode */
  235. } can_config_t;
  236. #ifdef __cplusplus
  237. extern "C" {
  238. #endif
  239. /**
  240. * @brief Reset CAN controller
  241. *
  242. * @param [in] base CAN base address
  243. * @param [in] enable reset flag for CAN controller
  244. * @arg true reset the CAN controller
  245. * @arg false clear the CAN controller reset flag
  246. */
  247. static inline void can_reset(CAN_Type *base, bool enable)
  248. {
  249. if (enable) {
  250. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_RESET_MASK;
  251. } else {
  252. base->CMD_STA_CMD_CTRL &= ~CAN_CMD_STA_CMD_CTRL_RESET_MASK;
  253. }
  254. }
  255. /**
  256. * @brief Set CAN mode
  257. *
  258. * @param [in] base CAN base address
  259. * @param [in] mode CAN loopback mode
  260. * @arg can_mode_normal Normal work mode
  261. * @arg can_mode_loopback_internal internal loopback mode
  262. * @arg can_mode_loopback_external external loopback mode
  263. * @arg can_mode_listen_only CAN listen-only mode
  264. */
  265. static inline void can_set_node_mode(CAN_Type *base, can_node_mode_t mode)
  266. {
  267. uint32_t cfg_stat = base->CMD_STA_CMD_CTRL & ~(CAN_CMD_STA_CMD_CTRL_LBME_MASK | CAN_CMD_STA_CMD_CTRL_LBMI_MASK | CAN_CMD_STA_CMD_CTRL_LOM_MASK);
  268. if (mode == can_mode_loopback_internal) {
  269. cfg_stat |= CAN_CMD_STA_CMD_CTRL_LBMI_MASK;
  270. } else if (mode == can_mode_loopback_external) {
  271. cfg_stat |= CAN_CMD_STA_CMD_CTRL_LBME_MASK;
  272. } else if (mode == can_mode_listen_only) {
  273. cfg_stat |= CAN_CMD_STA_CMD_CTRL_LOM_MASK;
  274. } else {
  275. /* CAN normal work mode, no change needed here */
  276. }
  277. base->CMD_STA_CMD_CTRL = cfg_stat;
  278. }
  279. /**
  280. * @brief Enable CAN listen-only mode
  281. * @param [in] base CAN base address
  282. * @param [in] enable CAN listen-only mode enable flag
  283. * @arg true enable listen-only mode
  284. * @arg false disable listen-only mode
  285. */
  286. static inline void can_enable_listen_only_mode(CAN_Type *base, bool enable)
  287. {
  288. if (enable) {
  289. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_LOM_MASK;
  290. } else {
  291. base->CMD_STA_CMD_CTRL &= ~CAN_CMD_STA_CMD_CTRL_LOM_MASK;
  292. }
  293. }
  294. /**
  295. * @brief Enter CAN standby mode
  296. * @param [in] base CAN base address
  297. * @param [in] enable Can standby mode enable flag
  298. * @arg true enable standby mode
  299. * @arg false disable standby mode
  300. */
  301. static inline void can_enter_standby_mode(CAN_Type *base, bool enable)
  302. {
  303. if (enable) {
  304. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_STBY_MASK;
  305. } else {
  306. base->CMD_STA_CMD_CTRL &= ~CAN_CMD_STA_CMD_CTRL_STBY_MASK;
  307. }
  308. }
  309. /**
  310. * @brief Select CAN TX buffer
  311. * @param [in] base CAN base address
  312. * @param [in] select_secondary_buffer CAN secondary transmit buffer selection flag
  313. * @arg true Select the secondary transmit buffer
  314. * @arg false Select the primary transmit buffer
  315. */
  316. static inline void can_select_tx_buffer(CAN_Type *base, bool select_secondary_buffer)
  317. {
  318. if (select_secondary_buffer) {
  319. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_TBSEL_MASK;
  320. } else {
  321. base->CMD_STA_CMD_CTRL &= ~CAN_CMD_STA_CMD_CTRL_TBSEL_MASK;
  322. }
  323. }
  324. /**
  325. * @brief Start CAN high priority message transmit
  326. * @param [in] base CAN base address
  327. *
  328. */
  329. static inline void can_start_high_priority_message_transmit(CAN_Type *base)
  330. {
  331. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_TPE_MASK;
  332. }
  333. /**
  334. * @brief Abort CAN high priority message transmit
  335. * @param [in] base CAN base address
  336. */
  337. static inline void can_abort_high_priority_message_transmit(CAN_Type *base)
  338. {
  339. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_TPA_MASK;
  340. }
  341. /**
  342. * @brief Start CAN message transmit
  343. * @param [in] base CAN base address
  344. */
  345. static inline void can_start_message_transmit(CAN_Type *base)
  346. {
  347. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_TSONE_MASK;
  348. }
  349. /**
  350. * @brief Start CAN all messages transmit
  351. * @param [in] base CAN base address
  352. */
  353. static inline void can_start_all_message_transmit(CAN_Type *base)
  354. {
  355. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_TSALL_MASK;
  356. }
  357. /**
  358. * @brief Abort CAN message transmit
  359. * @param [in] base CAN base address
  360. */
  361. static inline void can_abort_message_transmit(CAN_Type *base)
  362. {
  363. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_TSA_MASK;
  364. }
  365. /**
  366. * @brief Switch to next CAN tx buffer
  367. * @param [in] base CAN base address
  368. */
  369. static inline void can_switch_to_next_tx_buffer(CAN_Type *base)
  370. {
  371. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_TSNEXT_MASK;
  372. }
  373. /**
  374. * @brief Select CAN tx buffer priority mode
  375. * @param [in] base CAN base address
  376. * @param [in] enable_priority_decision CAN tx buffer priority mode selection flag
  377. * @arg true priority decision mode
  378. * @arg false FIFO mode
  379. */
  380. static inline void can_select_tx_buffer_priority_mode(CAN_Type *base, bool enable_priority_decision)
  381. {
  382. if (enable_priority_decision) {
  383. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_TSMODE_MASK;
  384. } else {
  385. base->CMD_STA_CMD_CTRL &= ~CAN_CMD_STA_CMD_CTRL_TSMODE_MASK;
  386. }
  387. }
  388. /**
  389. * @brief Get CAN secondary transmit buffer status
  390. * @param [in] base CAN base address
  391. * @retval secondary transmit buffer status, 0: empty, 1: <= 1/2 full, 2: > 1/2 full, 3: full
  392. *
  393. */
  394. static inline uint8_t can_get_secondary_transmit_buffer_status(CAN_Type *base)
  395. {
  396. return CAN_CMD_STA_CMD_CTRL_TSSTAT_GET(base->CMD_STA_CMD_CTRL);
  397. }
  398. /**
  399. * @brief Check whether the primary transmit buffer is full
  400. * @param [in] base CAN base address
  401. * @return true for full
  402. */
  403. static inline bool can_is_primary_transmit_buffer_full(CAN_Type *base)
  404. {
  405. return ((base->CMD_STA_CMD_CTRL & CAN_CMD_STA_CMD_CTRL_TPE_MASK) != 0U);
  406. }
  407. /**
  408. * @brief Check whether the secondary transmit buffer is full
  409. * @param [in] base CAN base address
  410. * @return true for full
  411. */
  412. static inline bool can_is_secondary_transmit_buffer_full(CAN_Type *base)
  413. {
  414. return (CAN_CMD_STA_CMD_CTRL_TSSTAT_GET(base->CMD_STA_CMD_CTRL) == CAN_STB_IS_FULL);
  415. }
  416. /**
  417. * @brief Check whether the CAN is in BUS off mode
  418. *
  419. * @param [in] base CAN base address
  420. * @return true for bus off mode
  421. * @return false for non-bus off mode
  422. */
  423. static inline bool can_is_in_bus_off_mode(CAN_Type *base)
  424. {
  425. return ((base->CMD_STA_CMD_CTRL & CAN_CMD_STA_CMD_CTRL_BUSOFF_MASK) != 0U);
  426. }
  427. /**
  428. * @brief Enable can self ack
  429. * @param [in] base CAN base address
  430. * @param [in] enable Self-ack enable flag, true or false
  431. *
  432. */
  433. static inline void can_enable_self_ack(CAN_Type *base, bool enable)
  434. {
  435. if (enable) {
  436. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_SACK_MASK;
  437. } else {
  438. base->CMD_STA_CMD_CTRL &= ~CAN_CMD_STA_CMD_CTRL_SACK_MASK;
  439. }
  440. }
  441. /**
  442. * @brief Set CAN receive buffer overflow mode
  443. * @param [in] base CAN base address
  444. * @param [in] override_enable receive buffer overflow mode option
  445. * @arg true the oldest message will be overwritten
  446. * @arg false the new message will not be stored
  447. */
  448. static inline void can_set_receive_buffer_overflow_mode(CAN_Type *base, bool override_enable)
  449. {
  450. if (override_enable) {
  451. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_ROM_MASK;
  452. } else {
  453. base->CMD_STA_CMD_CTRL &= ~CAN_CMD_STA_CMD_CTRL_ROM_MASK;
  454. }
  455. }
  456. /**
  457. * @brief Check whether CAN receive buffer is overflow
  458. * @param [in] base CAN base address
  459. * @retval true or false
  460. */
  461. static inline bool can_is_receive_buffer_overflow(CAN_Type *base)
  462. {
  463. return IS_HPM_BITMASK_SET(base->CMD_STA_CMD_CTRL, CAN_CMD_STA_CMD_CTRL_ROV_MASK);
  464. }
  465. /**
  466. * @brief Release CAN receive buffer
  467. *
  468. * @param [in] base CAN base address
  469. */
  470. static inline void can_release_receive_buffer(CAN_Type *base)
  471. {
  472. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_RREL_MASK;
  473. }
  474. /**
  475. * @brief Allow can store all date in receive buffer
  476. * @param [in] base CAN base address
  477. * @param [in] allow flag to determine whether to store error data frame
  478. */
  479. static inline void can_allow_store_all_data_in_receive_buffer(CAN_Type *base, bool allow)
  480. {
  481. if (allow) {
  482. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_RBALL_MASK;
  483. } else {
  484. base->CMD_STA_CMD_CTRL &= ~CAN_CMD_STA_CMD_CTRL_RBALL_MASK;
  485. }
  486. }
  487. /**
  488. * @brief Get CAN receive buffer status
  489. * @param [in] base CAN base address
  490. * @retval Receive buffer status, 0: empty, 1: > empty, < almost full, 2: >= almost full, 3: full
  491. */
  492. static inline uint8_t can_get_receive_buffer_status(CAN_Type *base)
  493. {
  494. return CAN_CMD_STA_CMD_CTRL_RSTAT_GET(base->CMD_STA_CMD_CTRL);
  495. }
  496. /**
  497. * @brief Check whether there are data available in receive buffer
  498. * @param [in] base CAN base address
  499. * @return true if the receive buffer is not empty
  500. */
  501. static inline bool can_is_data_available_in_receive_buffer(CAN_Type *base)
  502. {
  503. return (CAN_CMD_STA_CMD_CTRL_RSTAT_GET(base->CMD_STA_CMD_CTRL) != CAN_RXBUF_IS_EMPTY);
  504. }
  505. /**
  506. * @brief Enable CAN FD ISO mode
  507. * @param [in] base CAN base address
  508. * @param enable CAN-FD ISO mode enable flag
  509. */
  510. static inline void can_enable_can_fd_iso_mode(CAN_Type *base, bool enable)
  511. {
  512. if (enable) {
  513. base->CMD_STA_CMD_CTRL |= CAN_CMD_STA_CMD_CTRL_FD_ISO_MASK;
  514. } else {
  515. base->CMD_STA_CMD_CTRL &= ~CAN_CMD_STA_CMD_CTRL_FD_ISO_MASK;
  516. }
  517. }
  518. /**
  519. * @brief Enable CAN TX/RX interrupt
  520. * @param [in] base CAN base address
  521. * @param [in] mask CAN interrupt mask
  522. */
  523. static inline void can_enable_tx_rx_irq(CAN_Type *base, uint8_t mask)
  524. {
  525. base->RTIE |= mask;
  526. }
  527. /**
  528. * @brief Disable CAN TX/RX interrupt
  529. * @param [in] base CAN base address
  530. * @param [in] mask CAN interrupt mask
  531. */
  532. static inline void can_disable_tx_rx_irq(CAN_Type *base, uint8_t mask)
  533. {
  534. base->RTIE &= ~mask;
  535. }
  536. /**
  537. * @brief Clear CAN TX/RX flags
  538. * @param [in] base CAN base address
  539. * @param [in] flags CAN TX/RX interrupt flags
  540. */
  541. static inline void can_clear_tx_rx_flags(CAN_Type *base, uint8_t flags)
  542. {
  543. base->RTIF = flags;
  544. }
  545. /**
  546. * @brief Get CAN TX/RX flags
  547. *
  548. * @param [in] base CAN base address
  549. * @retval CAN Tx/RX interrupt flags
  550. */
  551. static inline uint8_t can_get_tx_rx_flags(CAN_Type *base)
  552. {
  553. return base->RTIF;
  554. }
  555. /**
  556. * @brief Enable CAN error interrupt
  557. * @param [in] base CAN base address
  558. * @param [in] mask CAN error interrupt mask
  559. */
  560. static inline void can_enable_error_irq(CAN_Type *base, uint8_t mask)
  561. {
  562. base->ERRINT |= mask;
  563. }
  564. /**
  565. * @brief Disable CAN error interrupt
  566. *
  567. * @param [in] base CAN base address
  568. * @param [in] mask CAN error interrupt mask
  569. */
  570. static inline void can_disable_error_irq(CAN_Type *base, uint8_t mask)
  571. {
  572. base->ERRINT &= ~mask;
  573. }
  574. /**
  575. * @brief Get CAN error interrupt flags
  576. * @param [in] base CAN base address
  577. * @retval CAN error interrupt flags
  578. */
  579. static inline uint8_t can_get_error_interrupt_flags(CAN_Type *base)
  580. {
  581. return (base->ERRINT & (uint8_t) ~(CAN_ERRINT_EPIE_MASK | CAN_ERRINT_ALIE_MASK | CAN_ERRINT_BEIE_MASK));
  582. }
  583. /**
  584. * @brief Get CAN clear interrupt flags
  585. * @param [in] base CAN base address
  586. * @param [in] flags CAN error interrupt flags
  587. */
  588. static inline void can_clear_error_interrupt_flags(CAN_Type *base, uint8_t flags)
  589. {
  590. flags &= (uint8_t)~(CAN_ERRINT_EPIE_MASK | CAN_ERRINT_ALIE_MASK | CAN_ERRINT_BEIE_MASK);
  591. base->ERRINT |= flags;
  592. }
  593. /**
  594. * @brief Get last CAN error kind
  595. * @param [in] base CAN base address
  596. * @retval CAN last error kind
  597. */
  598. static inline uint8_t can_get_last_error_kind(CAN_Type *base)
  599. {
  600. return CAN_EALCAP_KOER_GET(base->EALCAP);
  601. }
  602. /**
  603. * @brief Get CAN last arbitrary lost position
  604. * @param [in] base CAN base address
  605. * @retval CAN last arbitrary lost position
  606. */
  607. static inline uint8_t can_get_last_arbitration_lost_position(CAN_Type *base)
  608. {
  609. return CAN_EALCAP_ALC_GET(base->EALCAP);
  610. }
  611. /**
  612. * @brief Set CAN transmitter delay compensation
  613. *
  614. * @param [in] base CAN base address
  615. * @param [in] sample_point delay compensation sample point
  616. * @param [in] enable delay compensation enable flag
  617. */
  618. static inline void can_set_transmitter_delay_compensation(CAN_Type *base, uint8_t sample_point, bool enable)
  619. {
  620. #if defined(CAN_SOC_CANFD_TDC_REQUIRE_STUFF_EXCEPTION_WORKAROUND) && (CAN_SOC_CANFD_TDC_REQUIRE_STUFF_EXCEPTION_WORKAROUND == 1)
  621. base->TDC = CAN_TDC_TDCEN_SET((uint8_t) enable);
  622. #else
  623. base->TDC = CAN_TDC_SSPOFF_SET(sample_point) | CAN_TDC_TDCEN_SET((uint8_t) enable);
  624. #endif
  625. }
  626. /**
  627. * @brief Set CAN warning limits
  628. * @param [in] base CAN base address
  629. * @param [in] almost_full_limit
  630. * @param [in] error_warning_limit
  631. */
  632. static inline void can_set_warning_limits(CAN_Type *base, uint8_t almost_full_limit, uint8_t error_warning_limit)
  633. {
  634. base->LIMIT = CAN_LIMIT_AFWL_SET(almost_full_limit) | CAN_LIMIT_EWL_SET(error_warning_limit);
  635. }
  636. /**
  637. * @brief Get CAN receive error count
  638. * @param [in] base CAN base address
  639. * @retval CAN receive error count
  640. */
  641. static inline uint8_t can_get_receive_error_count(CAN_Type *base)
  642. {
  643. return base->RECNT;
  644. }
  645. /**
  646. * @brief Get CAN transmit error count
  647. * @param [in] base CAN base address
  648. * @retval CAN transmit error count
  649. */
  650. static inline uint8_t can_get_transmit_error_count(CAN_Type *base)
  651. {
  652. return base->TECNT;
  653. }
  654. /**
  655. * @brief Disable CAN filter
  656. *
  657. * @param [in] base CAN base address
  658. * @param index CAN filter index
  659. */
  660. static inline void can_disable_filter(CAN_Type *base, uint32_t index)
  661. {
  662. base->ACF_EN &= (uint16_t) ~(1U << index);
  663. }
  664. /**
  665. * @brief Get default CAN configuration parameters
  666. * @param [out] config CAN configuration structure
  667. * @retval API execution status, status_success or status_invalid_argument
  668. */
  669. hpm_stat_t can_get_default_config(can_config_t *config);
  670. /**
  671. * @brief Initialize the CAN controller
  672. * @param [in] base CAN base address
  673. * @param [in] config CAN configuration
  674. * @param [in] src_clk_freq CAN IP source clock frequency
  675. * @retval API execution status, status_success or status_invalid_argument
  676. */
  677. hpm_stat_t can_init(CAN_Type *base, can_config_t *config, uint32_t src_clk_freq);
  678. /**
  679. * @brief Configure the Slow Speed Bit timing using low-level interface
  680. * @param [in] base CAN base address
  681. * @param [in] param CAN bit timing parameter
  682. */
  683. static inline void can_set_slow_speed_timing(CAN_Type *base, const can_bit_timing_param_t *param)
  684. {
  685. base->S_PRESC = CAN_S_PRESC_S_PRESC_SET(param->prescaler - 1U) | CAN_S_PRESC_S_SEG_1_SET(param->num_seg1 - 2U) |
  686. CAN_S_PRESC_S_SEG_2_SET(param->num_seg2 - 1U) | CAN_S_PRESC_S_SJW_SET(param->num_sjw - 1U);
  687. }
  688. /**
  689. * @brief Configure the Fast Speed Bit timing using low-level interface
  690. * @param [in] base CAN base address
  691. * @param [in] param CAN bit timing parameter
  692. */
  693. static inline void can_set_fast_speed_timing(CAN_Type *base, const can_bit_timing_param_t *param)
  694. {
  695. base->F_PRESC = CAN_F_PRESC_F_PRESC_SET(param->prescaler - 1U) | CAN_F_PRESC_F_SEG_1_SET(param->num_seg1 - 2U) |
  696. CAN_F_PRESC_F_SEG_2_SET(param->num_seg2 - 1U) | CAN_F_PRESC_F_SJW_SET(param->num_sjw - 1U);
  697. }
  698. /**
  699. * @brief Configure the CAN bit timing for CAN BUS
  700. * @param [in] base CAN base address
  701. * @param [in] option CAN bit timing option
  702. * @param [in] src_clk_freq CAN IP source clock frequency
  703. * @param [in] baudrate CAN baudrate in MHz
  704. * @param [in] samplepoint_min Minimum Sample point, range(0-1000), samplepoint_min / 1000 will be used in calculation
  705. * @param [in] samplepoint_max Maximum Sample point, range(0-1000), samplepoint_max / 1000 will be used in calculation
  706. * @retval API execution status, status_success or status_invalid_argument
  707. */
  708. hpm_stat_t can_set_bit_timing(CAN_Type *base, can_bit_timing_option_t option,
  709. uint32_t src_clk_freq, uint32_t baudrate,
  710. uint16_t samplepoint_min, uint16_t samplepoint_max);
  711. /**
  712. * @brief Calculate the CAN bit timing for CAN BUS
  713. * @param [in] src_clk_freq CAN IP source clock frequency
  714. * @param [in] option CAN bit timing option
  715. * @param [in] baudrate CAN baudrate in MHz
  716. * @param [in] samplepoint_min Minimum Sample point, range(0-1000), samplepoint_min / 1000 will be used in calculation
  717. * @param [in] samplepoint_max Maximum Sample point, range(0-1000), samplepoint_max / 1000 will be used in calculation
  718. * @param [out] timing_param Calculated CAN bit timing parameter
  719. * @retval API execution status, status_success or status_invalid_argument
  720. */
  721. hpm_stat_t can_calculate_bit_timing(uint32_t src_clk_freq, can_bit_timing_option_t option, uint32_t baudrate,
  722. uint16_t samplepoint_min, uint16_t samplepoint_max,
  723. can_bit_timing_param_t *timing_param);
  724. /**
  725. * @brief Configure the acceptable filter
  726. * @param [in] base CAN base address
  727. * @param [in] config CAN filter configuration
  728. * @retval API execution status, status_success or status_invalid_argument
  729. */
  730. hpm_stat_t can_set_filter(CAN_Type *base, const can_filter_config_t *config);
  731. /**
  732. * @brief Send CAN message using blocking transfer
  733. * @param [in] base CAN base address
  734. * @param [in] message CAN message to be sent out
  735. * @retval API execution status, status_success or status_invalid_argument
  736. */
  737. hpm_stat_t can_send_message_blocking(CAN_Type *base, const can_transmit_buf_t *message);
  738. /**
  739. * @brief Send high-priority message using blocking transfer
  740. * @param [in] base CAN base address
  741. * @param [in] message CAN message to be sent out
  742. * @retval API execution status, status_success or status_invalid_argument
  743. */
  744. hpm_stat_t can_send_high_priority_message_blocking(CAN_Type *base, const can_transmit_buf_t *message);
  745. /**
  746. * @brief Send CAN message using non-blocking transfer
  747. * @param [in] base CAN base address
  748. * @param [in] message CAN message to be sent out
  749. * @retval API execution status, status_success or status_invalid_argument
  750. */
  751. hpm_stat_t can_send_message_nonblocking(CAN_Type *base, const can_transmit_buf_t *message);
  752. /**
  753. * @brief Send high-priority message using non-blocking transfer
  754. * @param [in] base CAN base address
  755. * @param [in] message CAN message to be sent out
  756. * @retval API execution status, status_success or status_invalid_argument
  757. */
  758. hpm_stat_t can_send_high_priority_message_nonblocking(CAN_Type *base, const can_transmit_buf_t *message);
  759. /**
  760. * @brief Receive CAN message using blocking transfer
  761. * @param [in] base CAN base address
  762. * @param [out] message CAN message buffer
  763. * @retval API execution status
  764. * @arg status_success API exection is successful
  765. * @arg status_invalid_argument Invalid parameters
  766. * @arg status_can_bit_error CAN bit error happened during receiving message
  767. * @arg status_can_form_error CAN form error happened during receiving message
  768. * @arg status_can_stuff_error CAN stuff error happened during receiving message
  769. * @arg status_can_ack_error CAN ack error happened during receiving message
  770. * @arg status_can_crc_error CAN crc error happened during receiving message
  771. * @arg status_can_other_error Other error happened during receiving message
  772. */
  773. hpm_stat_t can_receive_message_blocking(CAN_Type *base, can_receive_buf_t *message);
  774. /**
  775. * @brief Read Received CAN message
  776. * @note This API assumes that the received CAN message is available.
  777. * It can be used in the interrupt handler
  778. * @param [in] base CAN base address
  779. * @param [out] message CAN message buffer
  780. * @retval status_success API exection is successful
  781. * @retval status_invalid_argument Invalid parameters
  782. * @retval status_can_bit_error CAN bit error happened during receiving message
  783. * @retval status_can_form_error CAN form error happened during receiving message
  784. * @retval status_can_stuff_error CAN stuff error happened during receiving message
  785. * @retval status_can_ack_error CAN ack error happened during receiving message
  786. * @retval status_can_crc_error CAN crc error happened during receiving message
  787. * @retval status_can_other_error Other error happened during receiving message
  788. */
  789. hpm_stat_t can_read_received_message(CAN_Type *base, can_receive_buf_t *message);
  790. /**
  791. * @}
  792. *
  793. */
  794. #ifdef __cplusplus
  795. }
  796. #endif
  797. #endif /* HPM_CAN_DRV_H */