ch32v30x_can.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v30x_can.h
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2021/06/06
  6. * Description : This file contains all the functions prototypes for the
  7. * CAN firmware library.
  8. *********************************************************************************
  9. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  10. * Attention: This software (modified or not) and binary are used for
  11. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  12. *******************************************************************************/
  13. #ifndef __CH32V30x_CAN_H
  14. #define __CH32V30x_CAN_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "ch32v30x.h"
  19. /* CAN init structure definition */
  20. typedef struct
  21. {
  22. uint16_t CAN_Prescaler; /* Specifies the length of a time quantum.
  23. It ranges from 1 to 1024. */
  24. uint8_t CAN_Mode; /* Specifies the CAN operating mode.
  25. This parameter can be a value of
  26. @ref CAN_operating_mode */
  27. uint8_t CAN_SJW; /* Specifies the maximum number of time quanta
  28. the CAN hardware is allowed to lengthen or
  29. shorten a bit to perform resynchronization.
  30. This parameter can be a value of
  31. @ref CAN_synchronisation_jump_width */
  32. uint8_t CAN_BS1; /* Specifies the number of time quanta in Bit
  33. Segment 1. This parameter can be a value of
  34. @ref CAN_time_quantum_in_bit_segment_1 */
  35. uint8_t CAN_BS2; /* Specifies the number of time quanta in Bit
  36. Segment 2.
  37. This parameter can be a value of
  38. @ref CAN_time_quantum_in_bit_segment_2 */
  39. FunctionalState CAN_TTCM; /* Enable or disable the time triggered
  40. communication mode. This parameter can be set
  41. either to ENABLE or DISABLE. */
  42. FunctionalState CAN_ABOM; /* Enable or disable the automatic bus-off
  43. management. This parameter can be set either
  44. to ENABLE or DISABLE. */
  45. FunctionalState CAN_AWUM; /* Enable or disable the automatic wake-up mode.
  46. This parameter can be set either to ENABLE or
  47. DISABLE. */
  48. FunctionalState CAN_NART; /* Enable or disable the no-automatic
  49. retransmission mode. This parameter can be
  50. set either to ENABLE or DISABLE. */
  51. FunctionalState CAN_RFLM; /* Enable or disable the Receive FIFO Locked mode.
  52. This parameter can be set either to ENABLE
  53. or DISABLE. */
  54. FunctionalState CAN_TXFP; /* Enable or disable the transmit FIFO priority.
  55. This parameter can be set either to ENABLE
  56. or DISABLE. */
  57. } CAN_InitTypeDef;
  58. /* CAN filter init structure definition */
  59. typedef struct
  60. {
  61. uint16_t CAN_FilterIdHigh; /* Specifies the filter identification number (MSBs for a 32-bit
  62. configuration, first one for a 16-bit configuration).
  63. This parameter can be a value between 0x0000 and 0xFFFF */
  64. uint16_t CAN_FilterIdLow; /* Specifies the filter identification number (LSBs for a 32-bit
  65. configuration, second one for a 16-bit configuration).
  66. This parameter can be a value between 0x0000 and 0xFFFF */
  67. uint16_t CAN_FilterMaskIdHigh; /* Specifies the filter mask number or identification number,
  68. according to the mode (MSBs for a 32-bit configuration,
  69. first one for a 16-bit configuration).
  70. This parameter can be a value between 0x0000 and 0xFFFF */
  71. uint16_t CAN_FilterMaskIdLow; /* Specifies the filter mask number or identification number,
  72. according to the mode (LSBs for a 32-bit configuration,
  73. second one for a 16-bit configuration).
  74. This parameter can be a value between 0x0000 and 0xFFFF */
  75. uint16_t CAN_FilterFIFOAssignment; /* Specifies the FIFO (0 or 1) which will be assigned to the filter.
  76. This parameter can be a value of @ref CAN_filter_FIFO */
  77. uint8_t CAN_FilterNumber; /* Specifies the filter which will be initialized. It ranges from 0 to 13. */
  78. uint8_t CAN_FilterMode; /* Specifies the filter mode to be initialized.
  79. This parameter can be a value of @ref CAN_filter_mode */
  80. uint8_t CAN_FilterScale; /* Specifies the filter scale.
  81. This parameter can be a value of @ref CAN_filter_scale */
  82. FunctionalState CAN_FilterActivation; /* Enable or disable the filter.
  83. This parameter can be set either to ENABLE or DISABLE. */
  84. } CAN_FilterInitTypeDef;
  85. /* CAN Tx message structure definition */
  86. typedef struct
  87. {
  88. uint32_t StdId; /* Specifies the standard identifier.
  89. This parameter can be a value between 0 to 0x7FF. */
  90. uint32_t ExtId; /* Specifies the extended identifier.
  91. This parameter can be a value between 0 to 0x1FFFFFFF. */
  92. uint8_t IDE; /* Specifies the type of identifier for the message that
  93. will be transmitted. This parameter can be a value
  94. of @ref CAN_identifier_type */
  95. uint8_t RTR; /* Specifies the type of frame for the message that will
  96. be transmitted. This parameter can be a value of
  97. @ref CAN_remote_transmission_request */
  98. uint8_t DLC; /* Specifies the length of the frame that will be
  99. transmitted. This parameter can be a value between
  100. 0 to 8 */
  101. uint8_t Data[8]; /* Contains the data to be transmitted. It ranges from 0
  102. to 0xFF. */
  103. } CanTxMsg;
  104. /* CAN Rx message structure definition */
  105. typedef struct
  106. {
  107. uint32_t StdId; /* Specifies the standard identifier.
  108. This parameter can be a value between 0 to 0x7FF. */
  109. uint32_t ExtId; /* Specifies the extended identifier.
  110. This parameter can be a value between 0 to 0x1FFFFFFF. */
  111. uint8_t IDE; /* Specifies the type of identifier for the message that
  112. will be received. This parameter can be a value of
  113. @ref CAN_identifier_type */
  114. uint8_t RTR; /* Specifies the type of frame for the received message.
  115. This parameter can be a value of
  116. @ref CAN_remote_transmission_request */
  117. uint8_t DLC; /* Specifies the length of the frame that will be received.
  118. This parameter can be a value between 0 to 8 */
  119. uint8_t Data[8]; /* Contains the data to be received. It ranges from 0 to
  120. 0xFF. */
  121. uint8_t FMI; /* Specifies the index of the filter the message stored in
  122. the mailbox passes through. This parameter can be a
  123. value between 0 to 0xFF */
  124. } CanRxMsg;
  125. /* CAN_sleep_constants */
  126. #define CAN_InitStatus_Failed ((uint8_t)0x00) /* CAN initialization failed */
  127. #define CAN_InitStatus_Success ((uint8_t)0x01) /* CAN initialization OK */
  128. /* CAN_Mode */
  129. #define CAN_Mode_Normal ((uint8_t)0x00) /* normal mode */
  130. #define CAN_Mode_LoopBack ((uint8_t)0x01) /* loopback mode */
  131. #define CAN_Mode_Silent ((uint8_t)0x02) /* silent mode */
  132. #define CAN_Mode_Silent_LoopBack ((uint8_t)0x03) /* loopback combined with silent mode */
  133. /* CAN_Operating_Mode */
  134. #define CAN_OperatingMode_Initialization ((uint8_t)0x00) /* Initialization mode */
  135. #define CAN_OperatingMode_Normal ((uint8_t)0x01) /* Normal mode */
  136. #define CAN_OperatingMode_Sleep ((uint8_t)0x02) /* sleep mode */
  137. /* CAN_Mode_Status */
  138. #define CAN_ModeStatus_Failed ((uint8_t)0x00) /* CAN entering the specific mode failed */
  139. #define CAN_ModeStatus_Success ((uint8_t)!CAN_ModeStatus_Failed) /* CAN entering the specific mode Succeed */
  140. /* CAN_synchronisation_jump_width */
  141. #define CAN_SJW_1tq ((uint8_t)0x00) /* 1 time quantum */
  142. #define CAN_SJW_2tq ((uint8_t)0x01) /* 2 time quantum */
  143. #define CAN_SJW_3tq ((uint8_t)0x02) /* 3 time quantum */
  144. #define CAN_SJW_4tq ((uint8_t)0x03) /* 4 time quantum */
  145. /* CAN_time_quantum_in_bit_segment_1 */
  146. #define CAN_BS1_1tq ((uint8_t)0x00) /* 1 time quantum */
  147. #define CAN_BS1_2tq ((uint8_t)0x01) /* 2 time quantum */
  148. #define CAN_BS1_3tq ((uint8_t)0x02) /* 3 time quantum */
  149. #define CAN_BS1_4tq ((uint8_t)0x03) /* 4 time quantum */
  150. #define CAN_BS1_5tq ((uint8_t)0x04) /* 5 time quantum */
  151. #define CAN_BS1_6tq ((uint8_t)0x05) /* 6 time quantum */
  152. #define CAN_BS1_7tq ((uint8_t)0x06) /* 7 time quantum */
  153. #define CAN_BS1_8tq ((uint8_t)0x07) /* 8 time quantum */
  154. #define CAN_BS1_9tq ((uint8_t)0x08) /* 9 time quantum */
  155. #define CAN_BS1_10tq ((uint8_t)0x09) /* 10 time quantum */
  156. #define CAN_BS1_11tq ((uint8_t)0x0A) /* 11 time quantum */
  157. #define CAN_BS1_12tq ((uint8_t)0x0B) /* 12 time quantum */
  158. #define CAN_BS1_13tq ((uint8_t)0x0C) /* 13 time quantum */
  159. #define CAN_BS1_14tq ((uint8_t)0x0D) /* 14 time quantum */
  160. #define CAN_BS1_15tq ((uint8_t)0x0E) /* 15 time quantum */
  161. #define CAN_BS1_16tq ((uint8_t)0x0F) /* 16 time quantum */
  162. /* CAN_time_quantum_in_bit_segment_2 */
  163. #define CAN_BS2_1tq ((uint8_t)0x00) /* 1 time quantum */
  164. #define CAN_BS2_2tq ((uint8_t)0x01) /* 2 time quantum */
  165. #define CAN_BS2_3tq ((uint8_t)0x02) /* 3 time quantum */
  166. #define CAN_BS2_4tq ((uint8_t)0x03) /* 4 time quantum */
  167. #define CAN_BS2_5tq ((uint8_t)0x04) /* 5 time quantum */
  168. #define CAN_BS2_6tq ((uint8_t)0x05) /* 6 time quantum */
  169. #define CAN_BS2_7tq ((uint8_t)0x06) /* 7 time quantum */
  170. #define CAN_BS2_8tq ((uint8_t)0x07) /* 8 time quantum */
  171. /* CAN_filter_mode */
  172. #define CAN_FilterMode_IdMask ((uint8_t)0x00) /* identifier/mask mode */
  173. #define CAN_FilterMode_IdList ((uint8_t)0x01) /* identifier list mode */
  174. /* CAN_filter_scale */
  175. #define CAN_FilterScale_16bit ((uint8_t)0x00) /* Two 16-bit filters */
  176. #define CAN_FilterScale_32bit ((uint8_t)0x01) /* One 32-bit filter */
  177. /* CAN_filter_FIFO */
  178. #define CAN_Filter_FIFO0 ((uint8_t)0x00) /* Filter FIFO 0 assignment for filter x */
  179. #define CAN_Filter_FIFO1 ((uint8_t)0x01) /* Filter FIFO 1 assignment for filter x */
  180. /* CAN_identifier_type */
  181. #define CAN_Id_Standard ((uint32_t)0x00000000) /* Standard Id */
  182. #define CAN_Id_Extended ((uint32_t)0x00000004) /* Extended Id */
  183. /* CAN_remote_transmission_request */
  184. #define CAN_RTR_Data ((uint32_t)0x00000000) /* Data frame */
  185. #define CAN_RTR_Remote ((uint32_t)0x00000002) /* Remote frame */
  186. /* CAN_transmit_constants */
  187. #define CAN_TxStatus_Failed ((uint8_t)0x00)/* CAN transmission failed */
  188. #define CAN_TxStatus_Ok ((uint8_t)0x01) /* CAN transmission succeeded */
  189. #define CAN_TxStatus_Pending ((uint8_t)0x02) /* CAN transmission pending */
  190. #define CAN_TxStatus_NoMailBox ((uint8_t)0x04) /* CAN cell did not provide an empty mailbox */
  191. /* CAN_receive_FIFO_number_constants */
  192. #define CAN_FIFO0 ((uint8_t)0x00) /* CAN FIFO 0 used to receive */
  193. #define CAN_FIFO1 ((uint8_t)0x01) /* CAN FIFO 1 used to receive */
  194. /* CAN_sleep_constants */
  195. #define CAN_Sleep_Failed ((uint8_t)0x00) /* CAN did not enter the sleep mode */
  196. #define CAN_Sleep_Ok ((uint8_t)0x01) /* CAN entered the sleep mode */
  197. /* CAN_wake_up_constants */
  198. #define CAN_WakeUp_Failed ((uint8_t)0x00) /* CAN did not leave the sleep mode */
  199. #define CAN_WakeUp_Ok ((uint8_t)0x01) /* CAN leaved the sleep mode */
  200. /* CAN_Error_Code_constants */
  201. #define CAN_ErrorCode_NoErr ((uint8_t)0x00) /* No Error */
  202. #define CAN_ErrorCode_StuffErr ((uint8_t)0x10) /* Stuff Error */
  203. #define CAN_ErrorCode_FormErr ((uint8_t)0x20) /* Form Error */
  204. #define CAN_ErrorCode_ACKErr ((uint8_t)0x30) /* Acknowledgment Error */
  205. #define CAN_ErrorCode_BitRecessiveErr ((uint8_t)0x40) /* Bit Recessive Error */
  206. #define CAN_ErrorCode_BitDominantErr ((uint8_t)0x50) /* Bit Dominant Error */
  207. #define CAN_ErrorCode_CRCErr ((uint8_t)0x60) /* CRC Error */
  208. #define CAN_ErrorCode_SoftwareSetErr ((uint8_t)0x70) /* Software Set Error */
  209. /* CAN_flags */
  210. /* If the flag is 0x3XXXXXXX, it means that it can be used with CAN_GetFlagStatus()
  211. * and CAN_ClearFlag() functions.
  212. * If the flag is 0x1XXXXXXX, it means that it can only be used with CAN_GetFlagStatus() function.
  213. */
  214. /* Transmit Flags */
  215. #define CAN_FLAG_RQCP0 ((uint32_t)0x38000001) /* Request MailBox0 Flag */
  216. #define CAN_FLAG_RQCP1 ((uint32_t)0x38000100) /* Request MailBox1 Flag */
  217. #define CAN_FLAG_RQCP2 ((uint32_t)0x38010000) /* Request MailBox2 Flag */
  218. /* Receive Flags */
  219. #define CAN_FLAG_FMP0 ((uint32_t)0x12000003) /* FIFO 0 Message Pending Flag */
  220. #define CAN_FLAG_FF0 ((uint32_t)0x32000008) /* FIFO 0 Full Flag */
  221. #define CAN_FLAG_FOV0 ((uint32_t)0x32000010) /* FIFO 0 Overrun Flag */
  222. #define CAN_FLAG_FMP1 ((uint32_t)0x14000003) /* FIFO 1 Message Pending Flag */
  223. #define CAN_FLAG_FF1 ((uint32_t)0x34000008) /* FIFO 1 Full Flag */
  224. #define CAN_FLAG_FOV1 ((uint32_t)0x34000010) /* FIFO 1 Overrun Flag */
  225. /* Operating Mode Flags */
  226. #define CAN_FLAG_WKU ((uint32_t)0x31000008) /* Wake up Flag */
  227. #define CAN_FLAG_SLAK ((uint32_t)0x31000012) /* Sleep acknowledge Flag */
  228. /* Note:
  229. *When SLAK intterupt is disabled (SLKIE=0), no polling on SLAKI is possible.
  230. *In this case the SLAK bit can be polled.
  231. */
  232. /* Error Flags */
  233. #define CAN_FLAG_EWG ((uint32_t)0x10F00001) /* Error Warning Flag */
  234. #define CAN_FLAG_EPV ((uint32_t)0x10F00002) /* Error Passive Flag */
  235. #define CAN_FLAG_BOF ((uint32_t)0x10F00004) /* Bus-Off Flag */
  236. #define CAN_FLAG_LEC ((uint32_t)0x30F00070) /* Last error code Flag */
  237. /* CAN_interrupts */
  238. #define CAN_IT_TME ((uint32_t)0x00000001) /* Transmit mailbox empty Interrupt*/
  239. /* Receive Interrupts */
  240. #define CAN_IT_FMP0 ((uint32_t)0x00000002) /* FIFO 0 message pending Interrupt*/
  241. #define CAN_IT_FF0 ((uint32_t)0x00000004) /* FIFO 0 full Interrupt*/
  242. #define CAN_IT_FOV0 ((uint32_t)0x00000008) /* FIFO 0 overrun Interrupt*/
  243. #define CAN_IT_FMP1 ((uint32_t)0x00000010) /* FIFO 1 message pending Interrupt*/
  244. #define CAN_IT_FF1 ((uint32_t)0x00000020) /* FIFO 1 full Interrupt*/
  245. #define CAN_IT_FOV1 ((uint32_t)0x00000040) /* FIFO 1 overrun Interrupt*/
  246. /* Operating Mode Interrupts */
  247. #define CAN_IT_WKU ((uint32_t)0x00010000) /* Wake-up Interrupt*/
  248. #define CAN_IT_SLK ((uint32_t)0x00020000) /* Sleep acknowledge Interrupt*/
  249. /* Error Interrupts */
  250. #define CAN_IT_EWG ((uint32_t)0x00000100) /* Error warning Interrupt*/
  251. #define CAN_IT_EPV ((uint32_t)0x00000200) /* Error passive Interrupt*/
  252. #define CAN_IT_BOF ((uint32_t)0x00000400) /* Bus-off Interrupt*/
  253. #define CAN_IT_LEC ((uint32_t)0x00000800) /* Last error code Interrupt*/
  254. #define CAN_IT_ERR ((uint32_t)0x00008000) /* Error Interrupt*/
  255. /* Flags named as Interrupts : kept only for FW compatibility */
  256. #define CAN_IT_RQCP0 CAN_IT_TME
  257. #define CAN_IT_RQCP1 CAN_IT_TME
  258. #define CAN_IT_RQCP2 CAN_IT_TME
  259. /* CAN_Legacy */
  260. #define CANINITFAILED CAN_InitStatus_Failed
  261. #define CANINITOK CAN_InitStatus_Success
  262. #define CAN_FilterFIFO0 CAN_Filter_FIFO0
  263. #define CAN_FilterFIFO1 CAN_Filter_FIFO1
  264. #define CAN_ID_STD CAN_Id_Standard
  265. #define CAN_ID_EXT CAN_Id_Extended
  266. #define CAN_RTR_DATA CAN_RTR_Data
  267. #define CAN_RTR_REMOTE CAN_RTR_Remote
  268. #define CANTXFAILE CAN_TxStatus_Failed
  269. #define CANTXOK CAN_TxStatus_Ok
  270. #define CANTXPENDING CAN_TxStatus_Pending
  271. #define CAN_NO_MB CAN_TxStatus_NoMailBox
  272. #define CANSLEEPFAILED CAN_Sleep_Failed
  273. #define CANSLEEPOK CAN_Sleep_Ok
  274. #define CANWAKEUPFAILED CAN_WakeUp_Failed
  275. #define CANWAKEUPOK CAN_WakeUp_Ok
  276. void CAN_DeInit(CAN_TypeDef* CANx);
  277. uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct);
  278. void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct);
  279. void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct);
  280. void CAN_SlaveStartBank(uint8_t CAN_BankNumber);
  281. void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState);
  282. void CAN_TTComModeCmd(CAN_TypeDef* CANx, FunctionalState NewState);
  283. uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage);
  284. uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox);
  285. void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox);
  286. void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage);
  287. void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber);
  288. uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber);
  289. uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode);
  290. uint8_t CAN_Sleep(CAN_TypeDef* CANx);
  291. uint8_t CAN_WakeUp(CAN_TypeDef* CANx);
  292. uint8_t CAN_GetLastErrorCode(CAN_TypeDef* CANx);
  293. uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef* CANx);
  294. uint8_t CAN_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx);
  295. void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState);
  296. FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG);
  297. void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG);
  298. ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT);
  299. void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT);
  300. #ifdef __cplusplus
  301. }
  302. #endif
  303. #endif