HAL_CAN.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. ******************************************************************************
  3. * @file HAL_Can.h
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief Header file of CAN HAL module.
  7. ******************************************************************************
  8. */
  9. #ifndef __HAL_CAN_H__
  10. #define __HAL_CAN_H__
  11. #include "ACM32Fxx_HAL.h"
  12. /**
  13. * @}
  14. */
  15. /******************************************************************************/
  16. /* Peripheral Registers Bits Definition */
  17. /******************************************************************************/
  18. /******************************************************************************/
  19. /* (CAN) */
  20. /******************************************************************************/
  21. /**************** Bit definition for CAN MOD register ***********************/
  22. #define CAN_MOD_RM BIT0
  23. #define CAN_MOD_LOM BIT1
  24. #define CAN_MOD_STM BIT2
  25. #define CAN_MOD_AFM BIT3
  26. #define CAN_MOD_SM BIT4
  27. /**************** Bit definition for CAN CMR register ***********************/
  28. #define CAN_CMR_TR BIT0
  29. #define CAN_CMR_AT BIT1
  30. #define CAN_CMR_RRB BIT2
  31. #define CAN_CMR_CDO BIT3
  32. #define CAN_CMR_SRR BIT4
  33. /**************** Bit definition for CAN SR register ***********************/
  34. #define CAN_SR_RBS BIT0
  35. #define CAN_SR_DOS BIT1
  36. #define CAN_SR_TBS BIT2
  37. #define CAN_SR_TCS BIT3
  38. #define CAN_SR_RS BIT4
  39. #define CAN_SR_TS BIT5
  40. #define CAN_SR_ES BIT6
  41. #define CAN_SR_BS BIT7
  42. /**************** Bit definition for CAN IR register ***********************/
  43. #define CAN_IR_RI BIT0
  44. #define CAN_IR_TI BIT1
  45. #define CAN_IR_EI BIT2
  46. #define CAN_IR_DOI BIT3
  47. #define CAN_IR_WUI BIT4
  48. #define CAN_IR_EPI BIT5
  49. #define CAN_IR_ALI BIT6
  50. #define CAN_IR_BEI BIT7
  51. /**************** Bit definition for CAN IER register ***********************/
  52. #define CAN_IER_RIE BIT0
  53. #define CAN_IER_TIE BIT1
  54. #define CAN_IER_EIE BIT2
  55. #define CAN_IER_DOIE BIT3
  56. #define CAN_IER_WUIE BIT4
  57. #define CAN_IER_EPIE BIT5
  58. #define CAN_IER_ALIE BIT6
  59. #define CAN_IER_BEIE BIT7
  60. /**
  61. * @brief CAN init structure definition
  62. */
  63. typedef struct
  64. {
  65. uint32_t CAN_Mode; /*!< Specifies the CAN operating mode.
  66. This parameter can be a value of
  67. @ref CAN_mode e.g:CAN_Mode_Normal CAN_Mode_Normal*/
  68. uint32_t CAN_SJW; /*!< Specifies the maximum number of time quanta
  69. the CAN hardware is allowed to lengthen or
  70. shorten a bit to perform resynchronization.
  71. This parameter can be a value of
  72. @ref CAN_SJW e.g:CAN_SJW_1tq--CAN_SJW_4tq*/
  73. uint32_t CAN_BRP ; /*!< Specifies the number of time quanta in Bit
  74. Segment 1. This parameter can be a value between 0 and 63 */
  75. uint32_t CAN_TSEG1; /*!< Specifies the number of time quanta in Bit
  76. Segment 1. This parameter can be a value of
  77. @ref CAN_TSEG1 e.g: CAN_TSEG1_1tq-CAN_TSEG1_16tq*/
  78. uint32_t CAN_TSEG2; /*!< Specifies the number of time quanta in Bit
  79. Segment 2.This parameter can be a value of
  80. @ref CAN_TSEG2 e.g:CAN_TSEG1_1tq-CAN_TSEG16_tq*/
  81. uint32_t CAN_SAM ; /*!< Specifies the CAN operating mode.
  82. This parameter can be a value of
  83. @ref CAN_SAM e.g:CAN_SAM_1time CAN_SAM_3time*/
  84. } CAN_InitTypeDef;
  85. /**
  86. * @brief CAN filter init structure definition
  87. */
  88. typedef struct
  89. {
  90. uint32_t CAN_FilterMode; /*!< Specifies the filter mode to be initialized.
  91. This parameter can be a value of
  92. @ref CAN_filter_mode e.g:CAN_FilterMode_Dual CAN_FilterMode_Single*/
  93. uint32_t CAN_FilterId1; /*!< Specifies the filter identification number
  94. This parameter can be a value between 0x0000 and 0x1FFFFFFF */
  95. uint32_t CAN_FilterId2; /*!< Specifies the filter identification number
  96. only CAN_FilterMode=CAN_FilterMode_Dual Enable
  97. This parameter can be a value between 0x0000 and 0x1FFFFFFF */
  98. uint32_t CAN_FilterMaskId1; /*!< Specifies the filter identification mask number
  99. This parameter can be a value between 0x0000 and 0x1FFFFFFF */
  100. uint32_t CAN_FilterMaskId2; /*!< Specifies the filter identification mask number
  101. only CAN_FilterMode=CAN_FilterMode_Dual Enable
  102. This parameter can be a value between 0x0000 and 0x1FFFFFFF */
  103. } CAN_FilterInitTypeDef;
  104. /**
  105. * @brief CAN RxTxMessege structure definition
  106. */
  107. typedef struct
  108. {
  109. uint32_t StdId; /*!< Specifies the standard identifier.
  110. This parameter can be a value between 0 to 0x7FF. */
  111. uint32_t ExtId; /*!< Specifies the extended identifier.
  112. This parameter can be a value between 0 to 0x1FFFFFFF. */
  113. uint32_t IDE; /*!< Specifies the type of identifier for the message that
  114. will be transmitted. This parameter can be a value
  115. of @ref CAN_identifier_type e.g: CAN_Id_Standard CAN_Id_Extended*/
  116. uint32_t RTR; /*!< Specifies the type of frame for the message that will
  117. be transmitted. This parameter can be a value of
  118. @ref CAN_remote_transmission e.g: CAN_RTR_Data CAN_RTR_Remote */
  119. uint32_t DLC; /*!< Specifies the length of the frame that will be
  120. transmitted. This parameter can be a value between 0 to 8 */
  121. uint8_t Data[8]; /*!< Contains the data to be transmitted. It ranges from 0 to 0xFF. */
  122. } CanTxRxMsg;
  123. /**
  124. * @brief CAN handle Structure definition
  125. */
  126. typedef struct __CAN_HandleTypeDef
  127. {
  128. CAN_TypeDef *Instance; /*!< Register base address */
  129. CAN_InitTypeDef Init; /*!< CAN required parameters */
  130. CanTxRxMsg *RxMessage; /*!< CAN RxMessage */
  131. void (*CAN_ReceiveIT_Callback)(struct __CAN_HandleTypeDef *hcan); /* CAN ReceiveIT complete callback */
  132. void (*CAN_TransmitIT_Callback)(struct __CAN_HandleTypeDef *hcan); /* CAN TransmitIT complete callback */
  133. } CAN_HandleTypeDef;
  134. typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
  135. #define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))
  136. #define IS_CAN_ALL_PERIPH(PERIPH) (((PERIPH) == CAN1))
  137. /** @defgroup CAN_identifier_type
  138. * @{
  139. */
  140. #define CAN_Id_Standard ((uint32_t)0x00000000) /*!< Standard Id */
  141. #define CAN_Id_Extended ((uint32_t)0x00000001) /*!< Extended Id */
  142. #define IS_CAN_IDTYPE(IDTYPE) (((IDTYPE) == CAN_Id_Standard) || \
  143. ((IDTYPE) == CAN_Id_Extended))
  144. /**
  145. * @}
  146. */
  147. /** @defgroup CAN_remote_transmission
  148. * @{
  149. */
  150. #define CAN_RTR_Data ((uint32_t)0x00000000) /*!< Data frame */
  151. #define CAN_RTR_Remote ((uint32_t)0x00000001) /*!< Remote frame */
  152. #define IS_CAN_RTR(RTR) (((RTR) == CAN_RTR_Data) || ((RTR) == CAN_RTR_Remote))
  153. /**
  154. * @}
  155. */
  156. /** @defgroup CAN_TxRxMessege
  157. * @{
  158. */
  159. #define IS_CAN_STDID(STDID) ((STDID) <= ((uint32_t)0x7FF))
  160. #define IS_CAN_EXTID(EXTID) ((EXTID) <= ((uint32_t)0x1FFFFFFF))
  161. #define IS_CAN_DLC(DLC) ((DLC) <= ((uint8_t)0x08))
  162. #define IS_CAN_GET_FLAG(FLAG) (((FLAG) == CAN_SR_RBS) || ((FLAG) == CAN_SR_DOS) || \
  163. ((FLAG) == CAN_SR_TBS) || ((FLAG) == CAN_SR_TCS) || \
  164. ((FLAG) == CAN_SR_RS) || ((FLAG) == CAN_SR_TS) || \
  165. ((FLAG) == CAN_SR_ES) || ((FLAG) == CAN_SR_BS))
  166. #define IS_CAN_BRP(BRP) (((BRP) <= 63))
  167. /**
  168. * @defgroup CAN_Mode
  169. * @{
  170. */
  171. #define CAN_Mode_Normal ((uint8_t)0x00) /*!< Normal mode */
  172. #define CAN_Mode_SlefTest ((uint8_t)0x01) /*!< SlefTest mode */
  173. #define IS_CAN_MODE(MODE) (((MODE) == CAN_Mode_Normal) ||\
  174. ((MODE) == CAN_Mode_SlefTest))
  175. /**
  176. * @}
  177. */
  178. /**
  179. * @defgroup CAN_Operating_Mode
  180. * @{
  181. */
  182. #define CAN_OperatingMode_Normal ((uint8_t)0x00) /*!< Initialization mode */
  183. #define CAN_OperatingMode_Initialization ((uint8_t)0x01) /*!< Normal mode */
  184. #define CAN_OperatingMode_Listen ((uint8_t)0x02) /*!< Listen mode */
  185. #define CAN_OperatingMode_SelfTest ((uint8_t)0x04) /*!< Listen mode */
  186. #define CAN_OperatingMode_Sleep ((uint8_t)0x10) /*!< sleep mode */
  187. #define IS_CAN_OPERATING_MODE(MODE) (((MODE) == CAN_OperatingMode_Initialization) ||\
  188. ((MODE) == CAN_OperatingMode_Normal)|| \
  189. ((MODE) == CAN_OperatingMode_Sleep)|| \
  190. ((MODE) == CAN_OperatingMode_SelfTest)|| \
  191. ((MODE) == CAN_OperatingMode_Listen))
  192. /**
  193. * @}
  194. */
  195. /** @defgroup CAN_SAM
  196. * @{
  197. */
  198. #define CAN_SAM_1time ((uint8_t)0x00) /*!< 1 time quantum */
  199. #define CAN_SAM_3time ((uint8_t)0x01) /*!< 2 time quantum */
  200. #define IS_CAN_SAM(SAM) (((SAM) == CAN_SAM_1time) || ((SAM) == CAN_SAM_3time))
  201. /**
  202. * @}
  203. */
  204. /** @defgroup CAN_synchronisation_jump_width
  205. * @{
  206. */
  207. #define CAN_SJW_1tq ((uint8_t)0x00) /*!< 1 time quantum */
  208. #define CAN_SJW_2tq ((uint8_t)0x01) /*!< 2 time quantum */
  209. #define CAN_SJW_3tq ((uint8_t)0x02) /*!< 3 time quantum */
  210. #define CAN_SJW_4tq ((uint8_t)0x03) /*!< 4 time quantum */
  211. #define IS_CAN_SJW(SJW) (((SJW) == CAN_SJW_1tq) || ((SJW) == CAN_SJW_2tq)|| \
  212. ((SJW) == CAN_SJW_3tq) || ((SJW) == CAN_SJW_4tq))
  213. /**
  214. * @}
  215. */
  216. /** @defgroup CAN_time_quantum_in_bit_segment_1
  217. * @{
  218. */
  219. #define CAN_TSEG1_1tq ((uint8_t)0x00) /*!< 1 time quantum */
  220. #define CAN_TSEG1_2tq ((uint8_t)0x01) /*!< 2 time quantum */
  221. #define CAN_TSEG1_3tq ((uint8_t)0x02) /*!< 3 time quantum */
  222. #define CAN_TSEG1_4tq ((uint8_t)0x03) /*!< 4 time quantum */
  223. #define CAN_TSEG1_5tq ((uint8_t)0x04) /*!< 5 time quantum */
  224. #define CAN_TSEG1_6tq ((uint8_t)0x05) /*!< 6 time quantum */
  225. #define CAN_TSEG1_7tq ((uint8_t)0x06) /*!< 7 time quantum */
  226. #define CAN_TSEG1_8tq ((uint8_t)0x07) /*!< 8 time quantum */
  227. #define CAN_TSEG1_9tq ((uint8_t)0x08) /*!< 9 time quantum */
  228. #define CAN_TSEG1_10tq ((uint8_t)0x09) /*!< 10 time quantum */
  229. #define CAN_TSEG1_11tq ((uint8_t)0x0A) /*!< 11 time quantum */
  230. #define CAN_TSEG1_12tq ((uint8_t)0x0B) /*!< 12 time quantum */
  231. #define CAN_TSEG1_13tq ((uint8_t)0x0C) /*!< 13 time quantum */
  232. #define CAN_TSEG1_14tq ((uint8_t)0x0D) /*!< 14 time quantum */
  233. #define CAN_TSEG1_15tq ((uint8_t)0x0E) /*!< 15 time quantum */
  234. #define CAN_TSEG1_16tq ((uint8_t)0x0F) /*!< 16 time quantum */
  235. #define IS_CAN_TSEG1(TSEG1) ((TSEG1) <= CAN_TSEG1_16tq)
  236. /**
  237. * @}
  238. */
  239. /** @defgroup CAN_time_quantum_in_bit_segment_2
  240. * @{
  241. */
  242. #define CAN_TSEG2_1tq ((uint8_t)0x00) /*!< 1 time quantum */
  243. #define CAN_TSEG2_2tq ((uint8_t)0x01) /*!< 2 time quantum */
  244. #define CAN_TSEG2_3tq ((uint8_t)0x02) /*!< 3 time quantum */
  245. #define CAN_TSEG2_4tq ((uint8_t)0x03) /*!< 4 time quantum */
  246. #define CAN_TSEG2_5tq ((uint8_t)0x04) /*!< 5 time quantum */
  247. #define CAN_TSEG2_6tq ((uint8_t)0x05) /*!< 6 time quantum */
  248. #define CAN_TSEG2_7tq ((uint8_t)0x06) /*!< 7 time quantum */
  249. #define CAN_TSEG2_8tq ((uint8_t)0x07) /*!< 8 time quantum */
  250. #define IS_CAN_TSEG2(TSEG) ((TSEG) <= CAN_TSEG2_8tq)
  251. /**
  252. * @}
  253. */
  254. /** @defgroup CAN_filter_mode
  255. * @{
  256. */
  257. #define CAN_FilterMode_Dual ((uint8_t)0x00) /*!< identifier list mode */
  258. #define CAN_FilterMode_Single ((uint8_t)0x01) /*!< identifier/mask mode */
  259. #define IS_CAN_FILTER_MODE(MODE) (((MODE) == CAN_FilterMode_Dual) || \
  260. ((MODE) == CAN_FilterMode_Single))
  261. /**
  262. * @}
  263. */
  264. /** @defgroup CAN_ErrorCode
  265. * @{
  266. */
  267. #define CAN_ErrorType_ErrCode ((uint8_t)0xC0) /*!< identifier list mode */
  268. #define CAN_ErrorType_Direction ((uint8_t)0x20) /*!< identifier/mask mode */
  269. #define CAN_ErrorType_SegCode ((uint8_t)0x1F) /*!< identifier/mask mode */
  270. #define IS_CAN_ErrorType(ErrorType) (((ErrorType) == CAN_ErrorType_ErrCode) || \
  271. ((ErrorType) == CAN_ErrorType_Direction)|| \
  272. ((ErrorType) == CAN_ErrorType_SegCode))
  273. /**
  274. * @}
  275. */
  276. /* Initialization and Configuration functions *********************************/
  277. void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan);
  278. void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan);
  279. HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef *hcan);
  280. HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef *hcan);
  281. void HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan,CAN_FilterInitTypeDef* CAN_FilterInitStruct);
  282. /* Transmit functions *********************************************************/
  283. HAL_StatusTypeDef HAL_CAN_Transmit(CAN_HandleTypeDef *hcan, CanTxRxMsg* TxMessage);
  284. void HAL_CAN_CancelTransmit(CAN_HandleTypeDef *hcan);
  285. /* Receive functions **********************************************************/
  286. HAL_StatusTypeDef HAL_CAN_Receive(CAN_HandleTypeDef *hcan, CanTxRxMsg* RxMessage);
  287. HAL_StatusTypeDef HAL_CAN_Receive_IT(CAN_HandleTypeDef *hcan, CanTxRxMsg* RxMessage);
  288. int8_t HAL_CAN_GetReceiveFiFoCounter(CAN_HandleTypeDef *hcan);
  289. int8_t HAL_CAN_GetReceiveFiFoAddr(CAN_HandleTypeDef *hcan);
  290. void HAL_CAN_ReleaseReceiveFIFO(CAN_HandleTypeDef *hcan);
  291. void HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, CanTxRxMsg* RxMessage);
  292. /* Operation modes functions **************************************************/
  293. HAL_StatusTypeDef HAL_CAN_OperatingModeRequest(CAN_HandleTypeDef *hcan, uint8_t CAN_OperatingMode);
  294. void HAL_CAN_ClearOverload(CAN_HandleTypeDef *hcan);
  295. void HAL_CAN_SelfReceive(CAN_HandleTypeDef *hcan);
  296. HAL_StatusTypeDef HAL_CAN_Sleep(CAN_HandleTypeDef *hcan);
  297. HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan);
  298. /* Error Code management functions **********************************/
  299. int8_t HAL_CAN_GetErrorCode(CAN_HandleTypeDef *hcan,uint32_t Error_Type);
  300. int8_t HAL_CAN_GetErrorAlarmCounter(CAN_HandleTypeDef *hcan);
  301. int8_t HAL_CAN_GetArbitrationErrorPosition(CAN_HandleTypeDef *hcan);
  302. int8_t HAL_CAN_GetReceiveErrorCounter(CAN_HandleTypeDef *hcan);
  303. int8_t HAL_CAN_GetTransmitErrorCounter(CAN_HandleTypeDef *hcan);
  304. void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan);
  305. #endif