hc32f460_can.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*****************************************************************************
  2. * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
  3. *
  4. * This software component is licensed by HDSC under BSD 3-Clause license
  5. * (the "License"); You may not use this file except in compliance with the
  6. * License. You may obtain a copy of the License at:
  7. * opensource.org/licenses/BSD-3-Clause
  8. */
  9. /******************************************************************************/
  10. /** \file hc32f460_can.h
  11. **
  12. ** A detailed description is available at
  13. ** @link CanGroup CAN description @endlink
  14. **
  15. ** - 2018-11-27 CDT First version for Device Driver Library of CAN
  16. **
  17. ******************************************************************************/
  18. #ifndef __HC32F460_CAN_H__
  19. #define __HC32F460_CAN_H__
  20. /*******************************************************************************
  21. * Include files
  22. ******************************************************************************/
  23. #include "hc32_common.h"
  24. #include "ddl_config.h"
  25. #if (DDL_CAN_ENABLE == DDL_ON)
  26. /* C binding of definitions if building with C++ compiler */
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. /**
  32. *******************************************************************************
  33. ** \defgroup CanGroup Controller Area Network(CAN)
  34. **
  35. ******************************************************************************/
  36. //@{
  37. /*******************************************************************************
  38. * Global type definitions ('typedef')
  39. ******************************************************************************/
  40. /**
  41. *******************************************************************************
  42. ** \brief The Can error types.
  43. ******************************************************************************/
  44. typedef enum
  45. {
  46. NO_ERROR = 0U,
  47. BIT_ERROR = 1U,
  48. FORM_ERROR = 2U,
  49. STUFF_ERROR = 3U,
  50. ACK_ERROR = 4U,
  51. CRC_ERROR = 5U,
  52. UNKOWN_ERROR = 6U,
  53. }en_can_error_t;
  54. /**
  55. *******************************************************************************
  56. ** \brief The Can transmit buffer select.(TCMD)
  57. ******************************************************************************/
  58. typedef enum
  59. {
  60. CanPTBSel = 0U, ///< high-priority buffer
  61. CanSTBSel = 1U, ///< secondary buffer
  62. }en_can_buffer_sel_t;
  63. /**
  64. *******************************************************************************
  65. ** \brief The Can warning limits.(AFWL)
  66. ******************************************************************************/
  67. typedef struct stc_can_warning_limit
  68. {
  69. uint8_t CanWarningLimitVal; ///< Receive buffer almost full warning limit
  70. uint8_t CanErrorWarningLimitVal; ///< Programmable error warning limit
  71. }stc_can_warning_limit_t;
  72. /**
  73. *******************************************************************************
  74. ** \brief The Acceptance Filters Frame Format Check.(ACF)
  75. ******************************************************************************/
  76. typedef enum en_can_acf_format_en
  77. {
  78. CanStdFrames = 0x02u, ///< Accepts only Standard frames
  79. CanExtFrames = 0x03u, ///< Accepts only Extended frames
  80. CanAllFrames = 0x00u, ///< Accepts both standard or extended frames
  81. }en_can_acf_format_en_t;
  82. /**
  83. *******************************************************************************
  84. ** \brief The Acceptance Filters Enable.(ACFEN)
  85. ******************************************************************************/
  86. typedef enum en_can_filter_sel
  87. {
  88. CanFilterSel1 = 0u, ///< The Acceptance Filter 1 Enable
  89. CanFilterSel2 = 1u, ///< The Acceptance Filter 2 Enable
  90. CanFilterSel3 = 2u, ///< The Acceptance Filter 3 Enable
  91. CanFilterSel4 = 3u, ///< The Acceptance Filter 4 Enable
  92. CanFilterSel5 = 4u, ///< The Acceptance Filter 5 Enable
  93. CanFilterSel6 = 5u, ///< The Acceptance Filter 6 Enable
  94. CanFilterSel7 = 6u, ///< The Acceptance Filter 7 Enable
  95. CanFilterSel8 = 7u, ///< The Acceptance Filter 8 Enable
  96. }en_can_filter_sel_t;
  97. /**
  98. *******************************************************************************
  99. ** \brief The can interrupt enable.(IE)
  100. ******************************************************************************/
  101. typedef enum
  102. {
  103. //<<Can Rx or Tx Irq En
  104. CanRxIrqEn = 0x00000080, ///< Receive interrupt enable
  105. CanRxOverIrqEn = 0x00000040, ///< RB overrun interrupt enable
  106. CanRxBufFullIrqEn = 0x00000020, ///< RB full interrupt enable
  107. CanRxBufAlmostFullIrqEn = 0x00000010, ///< RB almost full interrupt enable
  108. CanTxPrimaryIrqEn = 0x00000008, ///< Transmission primary interrupt enable
  109. CanTxSecondaryIrqEn = 0x00000004, ///< Transmission secondary enable
  110. CanErrorIrqEn = 0x00000002, ///< Error interrupt enable
  111. //<<Can Error Irq En
  112. CanErrorPassiveIrqEn = 0x00200000, ///< Error passive mode active enable
  113. CanArbiLostIrqEn = 0x00080000, ///< Arbitration lost interrupt enable
  114. CanBusErrorIrqEn = 0x00020000, ///< Bus error interrupt enable
  115. }en_can_irq_type_t;
  116. /**
  117. *******************************************************************************
  118. ** \brief The can interrupt flag.(IF)
  119. ******************************************************************************/
  120. typedef enum
  121. {
  122. //<<Can Tx or Tx Irq Flg
  123. CanTxBufFullIrqFlg = 0x00000001, ///<
  124. CanRxIrqFlg = 0x00008000, ///< Receive interrupt flag
  125. CanRxOverIrqFlg = 0x00004000, ///< RB overrun interrupt flag
  126. CanRxBufFullIrqFlg = 0x00002000, ///< RB full interrupt flag
  127. CanRxBufAlmostFullIrqFlg = 0x00001000, ///< RB almost full interrupt flag
  128. CanTxPrimaryIrqFlg = 0x00000800, ///< Transmission primary interrupt flag
  129. CanTxSecondaryIrqFlg = 0x00000400, ///< Transmission secondary interrupt flag
  130. CanErrorIrqFlg = 0x00000200, ///< Error interrupt flag
  131. CanAbortIrqFlg = 0x00000100, ///< Abort interrupt flag
  132. //<< Can Error Irq Flg
  133. CanErrorWarningIrqFlg = 0x00800000, ///< Error warning limit reached flag
  134. CanErrorPassivenodeIrqFlg = 0x00400000, ///< Error passive mode active flag
  135. CanErrorPassiveIrqFlg = 0x00100000, ///< Error passive interrupt flag
  136. CanArbiLostIrqFlg = 0x00040000, ///< Arbitration lost interrupt flag
  137. CanBusErrorIrqFlg = 0x00010000, ///< Bus error interrupt flag
  138. }en_can_irq_flag_type_t;
  139. /**
  140. *******************************************************************************
  141. ** \brief The can mode.(TCMD)
  142. ******************************************************************************/
  143. typedef enum
  144. {
  145. CanExternalLoopBackMode = 0x40, ///< Loop back mode, external
  146. CanInternalLoopBackMode = 0x20, ///< Loop back mode, internal
  147. CanTxSignalPrimaryMode = 0x10, ///< Transmission primary single shot mode for PTB
  148. CanTxSignalSecondaryMode = 0x08, ///< Transmission secondary single shot mode for STB
  149. CanListenOnlyMode = 0xFF, ///< Listen only mode
  150. }en_can_mode_t;
  151. /**
  152. *******************************************************************************
  153. ** \brief The can status.(STAT)
  154. ******************************************************************************/
  155. typedef enum
  156. {
  157. CanRxActive = 0x04, ///< Reception active
  158. CanTxActive = 0x02, ///< Transmission active
  159. CanBusoff = 0x01, ///< Bus off
  160. }en_can_status_t;
  161. /**
  162. *******************************************************************************
  163. ** \brief The Can Tx Command.(TCMD)
  164. ******************************************************************************/
  165. typedef enum
  166. {
  167. CanPTBTxCmd = 0x10, ///< Transmit primary for PTB
  168. CanPTBTxAbortCmd = 0x08, ///< Transmit primary abort for PTB
  169. CanSTBTxOneCmd = 0x04, ///< Transmit secondary one frame for STB
  170. CanSTBTxAllCmd = 0x02, ///< Transmit secondary all frames for STB
  171. CanSTBTxAbortCmd = 0x01, ///< Transmit secondary abort for STB
  172. }en_can_tx_cmd_t;
  173. /**
  174. *******************************************************************************
  175. ** \brief The Can Transmit buffer secondary operation mode.(TCTRL)
  176. ******************************************************************************/
  177. typedef enum
  178. {
  179. CanSTBFifoMode = 0, ///< FIFO mode
  180. CanSTBPrimaryMode = 1, ///< Priority decision mode
  181. }en_can_stb_mode_t;
  182. /**
  183. *******************************************************************************
  184. ** \brief The Can Self-ACKnowledge.(RCTRL)
  185. ******************************************************************************/
  186. typedef enum
  187. {
  188. CanSelfAckEnable = 1, ///< Self-ACK when LBME=1
  189. CanSelfAckDisable = 0, ///< no self-ACK
  190. }en_can_self_ack_en_t;
  191. /**
  192. *******************************************************************************
  193. ** \brief The Can Receive Buffer Overflow Mode.(RCTRL)
  194. ******************************************************************************/
  195. typedef enum
  196. {
  197. CanRxBufOverwritten = 0, ///< The oldest message will be overwritten
  198. CanRxBufNotStored = 1, ///< The new message will not be stored
  199. }en_can_rx_buf_mode_en_t;
  200. /**
  201. *******************************************************************************
  202. ** \brief The Can Receive Buffer Stores All data frames.(RCTRL)
  203. ******************************************************************************/
  204. typedef enum
  205. {
  206. CanRxNormal = 0, ///< Normal operation
  207. CanRxAll = 1, ///< RB stores correct data frames as well as data frames with error
  208. }en_can_rx_buf_all_t;
  209. /**
  210. *******************************************************************************
  211. ** \brief The Can Receive Buffer Status.(RSTAT)
  212. ******************************************************************************/
  213. typedef enum
  214. {
  215. CanRxBufEmpty = 0, ///< Empty
  216. CanRxBufnotAlmostFull = 1, ///< >empty and <almost full
  217. CanRxBufAlmostFull = 2, ///< >=almost full, but not full and no overflow
  218. CanRxBufFull = 3, ///< full
  219. }en_can_rx_buf_status_t;
  220. /**
  221. *******************************************************************************
  222. ** \brief The Can Transmission secondary Status.(TSSTAT)
  223. ******************************************************************************/
  224. typedef enum
  225. {
  226. CanTxBufEmpty = 0, ///< TTEN=0 or TTTBM=0: STB is empty
  227. ///< TTEN=1 and TTTBM=1: PTB and STB are empty
  228. CanTxBufnotHalfFull = 1, ///< TTEN=0 or TTTBM=0: STB is less than or equal to half full
  229. ///< TTEN=1 and TTTBM=1: PTB and STB are not empty and not full
  230. CanTxBufHalfFull = 2, ///< TTEN=0 or TTTBM=0: STB is more than half full
  231. ///< TTEN=1 and TTTBM=1: None
  232. CanTxBufFull = 3, ///< TTEN=0 or TTTBM=0: STB is full
  233. ///< TTEN=1 and TTTBM=1: PTB and STB are full
  234. }en_can_tx_buf_status_t;
  235. /**
  236. *******************************************************************************
  237. ** \brief Configuration structure of CAN Acceptance Filter Code and Mask.
  238. ******************************************************************************/
  239. typedef struct stc_can_filter
  240. {
  241. uint32_t u32CODE; ///< Acceptance CODE
  242. uint32_t u32MASK; ///< Acceptance MASK
  243. en_can_filter_sel_t enFilterSel; ///< The Acceptance Filters Enable
  244. en_can_acf_format_en_t enAcfFormat; ///< The Acceptance Filters Frame Format Check.
  245. }stc_can_filter_t;
  246. /**
  247. *******************************************************************************
  248. ** \brief Configuration structure of CAN Bit Timing.
  249. ******************************************************************************/
  250. typedef struct stc_can_bt
  251. {
  252. uint8_t SEG_1; ///< Bit timing segment 1(Tseg_1 = (SEG_1 + 2)*TQ)
  253. uint8_t SEG_2; ///< Bit timing segment 2(Tseg_2 = (SEG_2 + 1)*TQ)
  254. uint8_t SJW; ///< Synchronization jump width(Tsjw = (SJW + 1)*TQ)
  255. uint8_t PRESC; ///< The Prescaler divides the system clock to get the time quanta clock tq_clk(TQ)
  256. }stc_can_bt_t;
  257. /**
  258. *******************************************************************************
  259. ** \brief Configuration structure of CAN Control Frame.
  260. ******************************************************************************/
  261. typedef struct
  262. {
  263. uint32_t DLC : 4; ///< Data length code
  264. uint32_t RESERVED0 : 2; ///< Ignore
  265. uint32_t RTR : 1; ///< Remote transmission request
  266. uint32_t IDE : 1; ///< IDentifier extension
  267. uint32_t RESERVED1 : 24; ///< Ignore
  268. }stc_can_txcontrol_t;
  269. /**
  270. *******************************************************************************
  271. ** \brief Configuration structure of CAN Tx Frame.
  272. ******************************************************************************/
  273. typedef struct stc_can_txframe
  274. {
  275. union
  276. {
  277. uint32_t TBUF32_0; ///< Ignore
  278. uint32_t StdID; ///< Standard ID
  279. uint32_t ExtID; ///< Extended ID
  280. };
  281. union
  282. {
  283. uint32_t TBUF32_1; ///< Ignore
  284. stc_can_txcontrol_t Control_f; ///< CAN Tx Control
  285. };
  286. union
  287. {
  288. uint32_t TBUF32_2[2]; ///< Ignore
  289. uint8_t Data[8]; ///< CAN data
  290. };
  291. en_can_buffer_sel_t enBufferSel; ///< CAN Tx buffer select
  292. }stc_can_txframe_t;
  293. /**
  294. *******************************************************************************
  295. ** \brief Configuration structure of CAN Rx Ctrl.
  296. ******************************************************************************/
  297. typedef struct
  298. {
  299. uint8_t DLC : 4; ///< Data length code
  300. uint8_t RESERVED0 : 2; ///< Ignore
  301. uint8_t RTR : 1; ///< Remote transmission request
  302. uint8_t IDE : 1; ///< IDentifier extension
  303. }stc_can_rxcontrol_t;
  304. /**
  305. *******************************************************************************
  306. ** \brief Configuration structure of CAN status.
  307. ******************************************************************************/
  308. typedef struct
  309. {
  310. uint8_t RESERVED0 : 4; ///< Ignore
  311. uint8_t TX : 1; ///< TX is set to 1 if the loop back mode is activated
  312. uint8_t KOER : 3; ///< Kind of error
  313. }stc_can_status_t;
  314. /**
  315. *******************************************************************************
  316. ** \brief Configuration structure of CAN control, status and cycletime.
  317. ******************************************************************************/
  318. typedef struct
  319. {
  320. stc_can_rxcontrol_t Control_f; ///< @ref stc_can_rxcontrol_t
  321. stc_can_status_t Status_f; ///< @ref stc_can_status_t
  322. uint16_t CycleTime; ///< TTCAN cycletime
  323. }stc_can_cst_t;
  324. /**
  325. *******************************************************************************
  326. ** \brief Configuration structure of CAN Rx frame.
  327. ******************************************************************************/
  328. typedef struct stc_can_rxframe
  329. {
  330. union
  331. {
  332. uint32_t RBUF32_0; ///< Ignore
  333. uint32_t StdID; ///< Standard ID
  334. uint32_t ExtID; ///< Extended ID
  335. };
  336. union
  337. {
  338. uint32_t RBUF32_1; ///< Ignore
  339. stc_can_cst_t Cst; ///< @ref stc_can_cst_t
  340. };
  341. union
  342. {
  343. uint32_t RBUF32_2[2]; ///< Ignore
  344. uint8_t Data[8]; ///< CAN data
  345. };
  346. }stc_can_rxframe_t;
  347. /**
  348. *******************************************************************************
  349. ** \brief Configuration structure of CAN Rx frame.
  350. ******************************************************************************/
  351. typedef struct stc_can_init_config
  352. {
  353. en_can_rx_buf_all_t enCanRxBufAll; ///< @ref en_can_rx_buf_all_t
  354. en_can_rx_buf_mode_en_t enCanRxBufMode; ///< @ref en_can_rx_buf_mode_en_t
  355. en_can_self_ack_en_t enCanSAck; ///< @ref en_can_self_ack_en_t
  356. en_can_stb_mode_t enCanSTBMode; ///< @ref en_can_stb_mode_t
  357. stc_can_bt_t stcCanBt; ///< @ref stc_can_bt_t
  358. stc_can_warning_limit_t stcWarningLimit; ///< @ref stc_can_warning_limit_t
  359. }stc_can_init_config_t;
  360. /**
  361. *******************************************************************************
  362. ** \brief CAN TTCAN
  363. ******************************************************************************/
  364. /**
  365. *******************************************************************************
  366. ** \brief Configuration structure of CAN TTCAN pointer to a TB message slot
  367. ******************************************************************************/
  368. typedef enum
  369. {
  370. CanTTcanPTBSel = 0x00u, ///< PTB
  371. CanTTcanSTB1Sel = 0x01u, ///< STB1
  372. CanTTcanSTB2Sel = 0x02u, ///< STB2
  373. CanTTcanSTB3Sel = 0x03u, ///< STB3
  374. CanTTcanSTB4Sel = 0x04u, ///< STB4
  375. }en_can_ttcan_tbslot_t;
  376. /**
  377. *******************************************************************************
  378. ** \brief Configuration structure of CAN TTCAN Timer prescaler
  379. ******************************************************************************/
  380. typedef enum
  381. {
  382. CanTTcanTprescDiv1 = 0x00u, ///< Div1
  383. CanTTcanTprescDiv2 = 0x01u, ///< Div2
  384. CanTTcanTprescDiv3 = 0x02u, ///< Div3
  385. CanTTcanTprescDiv4 = 0x03u, ///< Div4
  386. }en_can_ttcan_Tpresc_t;
  387. /**
  388. *******************************************************************************
  389. ** \brief Configuration structure of CAN TTCAN Trigger type
  390. ******************************************************************************/
  391. typedef enum
  392. {
  393. CanTTcanImmediate = 0x00, ///< Immediate trigger for immediate transmission
  394. CanTTcanTime = 0x01, ///< Time trigger for receive trigger
  395. CanTTcanSingle = 0x02, ///< Single shot transmit trigger for exclusive time windows
  396. CanTTcanTransStart = 0x03, ///< Transmit start trigger for merged arbitrating time windows
  397. CanTTcanTransStop = 0x04, ///< Transmit stop trigger for merged arbitrating time windows
  398. }en_can_ttcan_trigger_type_t;
  399. typedef enum
  400. {
  401. CanTTcanWdtTriggerIrq = 0x80, ///< Watch trigger interrupt
  402. CanTTcanTimTriggerIrq = 0x10, ///< Time trigger interrupt
  403. }en_can_ttcan_irq_type_t;
  404. typedef struct stc_can_ttcan_ref_msg
  405. {
  406. uint8_t u8IDE; ///< Reference message IDE:1-Extended; 0-Standard;
  407. union ///< Reference message ID
  408. {
  409. uint32_t RefStdID; ///< Reference standard ID
  410. uint32_t RefExtID; ///< Reference Extended ID
  411. };
  412. }stc_can_ttcan_ref_msg_t;
  413. typedef struct stc_can_ttcan_trigger_config
  414. {
  415. en_can_ttcan_tbslot_t enTbSlot; ///< Transmit trigger TB slot pointer
  416. en_can_ttcan_trigger_type_t enTrigType; ///< Trigger type
  417. en_can_ttcan_Tpresc_t enTpresc; ///< Timer prescaler
  418. uint8_t u8Tew; ///< Transmit enable window
  419. uint16_t u16TrigTime; ///< TTCAN trigger time
  420. uint16_t u16WatchTrigTime; ///< TTCAN watch trigger time register
  421. }stc_can_ttcan_trigger_config_t;
  422. /*******************************************************************************
  423. * Global pre-processor symbols/macros ('#define')
  424. ******************************************************************************/
  425. /*******************************************************************************
  426. * Global variable definitions ('extern')
  427. ******************************************************************************/
  428. /*******************************************************************************
  429. * Global function prototypes (definition in C source)
  430. ******************************************************************************/
  431. void CAN_Init(stc_can_init_config_t *pstcCanInitCfg);
  432. void CAN_DeInit(void);
  433. void CAN_IrqCmd(en_can_irq_type_t enCanIrqType, en_functional_state_t enNewState);
  434. bool CAN_IrqFlgGet(en_can_irq_flag_type_t enCanIrqFlgType);
  435. void CAN_IrqFlgClr(en_can_irq_flag_type_t enCanIrqFlgType);
  436. void CAN_ModeConfig(en_can_mode_t enMode, en_functional_state_t enNewState);
  437. en_can_error_t CAN_ErrorStatusGet(void);
  438. bool CAN_StatusGet(en_can_status_t enCanStatus);
  439. void CAN_FilterConfig(const stc_can_filter_t *pstcFilter, en_functional_state_t enNewState);
  440. void CAN_SetFrame(stc_can_txframe_t *pstcTxFrame);
  441. en_can_tx_buf_status_t CAN_TransmitCmd(en_can_tx_cmd_t enTxCmd);
  442. en_can_rx_buf_status_t CAN_Receive(stc_can_rxframe_t *pstcRxFrame);
  443. uint8_t CAN_ArbitrationLostCap(void);
  444. uint8_t CAN_RxErrorCntGet(void);
  445. uint8_t CAN_TxErrorCntGet(void);
  446. //<< void CAN_TTCAN_Enable(void);
  447. //<< void CAN_TTCAN_Disable(void);
  448. //<< void CAN_TTCAN_IrqCmd(void);
  449. //<< void CAN_TTCAN_ReferenceMsgSet(stc_can_ttcan_ref_msg_t *pstcRefMsg);
  450. //<< void CAN_TTCAN_TriggerConfig(stc_can_ttcan_trigger_config_t *pstcTriggerCfg);
  451. //@} // CanGroup
  452. #ifdef __cplusplus
  453. }
  454. #endif
  455. #endif /* DDL_CAN_ENABLE */
  456. #endif /* __HC32F460_CAN_H__ */
  457. /*******************************************************************************
  458. * EOF (not truncated)
  459. ******************************************************************************/