HAL_can.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. ******************************************************************************
  3. * @file HAL_can.h
  4. * @author IC Applications Department
  5. * @version V0.8
  6. * @date 2019_08_02
  7. * @brief This file provides all the CAN firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, HOLOCENE SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2016 HOLOCENE</center></h2>
  19. */
  20. /* Define to prevent recursive inclusion -------------------------------------*/
  21. #ifndef __HAL_CAN_H
  22. #define __HAL_CAN_H
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "HAL_device.h"
  25. /** @defgroup CAN_sleep_constants
  26. * @{
  27. */
  28. #define CANINITFAILED ((uint8_t)0x00) /* CAN initialization failed */
  29. #define CANINITOK ((uint8_t)0x01) /* CAN initialization ok */
  30. /**
  31. * @}
  32. */
  33. /** @defgroup CAN_sleep_constants
  34. * @{
  35. */
  36. #define CANSLEEPFAILED ((uint8_t)0x00) /* CAN did not enter the sleep mode */
  37. #define CANSLEEPOK ((uint8_t)0x01) /* CAN entered the sleep mode */
  38. /**
  39. * @}
  40. */
  41. /** @defgroup CAN_wake_up_constants
  42. * @{
  43. */
  44. #define CANWAKEUPFAILED ((uint8_t)0x00) /* CAN did not leave the sleep mode */
  45. #define CANWAKEUPOK ((uint8_t)0x01) /* CAN leaved the sleep mode */
  46. /**
  47. * @}
  48. */
  49. /**
  50. * @brief parasmeter of CAN Mode
  51. */
  52. #define CAN_BASICMode ((uint32_t)0x0)
  53. #define CAN_PELIMode ((uint32_t)0x80)
  54. #define CAN_WorkMode ((uint32_t)0x80)
  55. #define CAN_ResetMode ((uint32_t)0x1)
  56. #define CAN_ListenOnlyMode ((uint32_t)0x2)
  57. #define CAN_SeftTestMode ((uint32_t)0x4)
  58. #define CAN_FilterMode_Singal ((uint32_t)0x8)
  59. #define CAN_FilterMode_Double ((uint32_t)0xf7)
  60. #define CAN_SleepMode ((uint32_t)0x10)
  61. /**
  62. * @}
  63. */
  64. /**
  65. * @brief parasmeter of BASIC CAN interrupt
  66. */
  67. #define CAN_IT_RIE ((uint32_t)0x2)
  68. #define CAN_IT_TIE ((uint32_t)0x4)
  69. #define CAN_IT_EIE ((uint32_t)0x8)
  70. #define CAN_IT_OIE ((uint32_t)0x10)
  71. /**
  72. * @}
  73. */
  74. /**
  75. * @brief parasmeter of PELI CAN interrupt
  76. */
  77. #define CAN_IT_RI ((uint32_t)0x1)
  78. #define CAN_IT_TI ((uint32_t)0x2)
  79. #define CAN_IT_EI ((uint32_t)0x4)
  80. #define CAN_IT_DOI ((uint32_t)0x8)
  81. #define CAN_IT_WUI ((uint32_t)0x10)
  82. #define CAN_IT_EPI ((uint32_t)0x20)
  83. #define CAN_IT_ALI ((uint32_t)0x40)
  84. #define CAN_IT_BEI ((uint32_t)0x80)
  85. #define CAN_IT_ALL ((uint32_t)0xff)
  86. /**
  87. * @}
  88. */
  89. /**
  90. * @brief parasmeter of CAN Status
  91. */
  92. #define CAN_STATUS_RBS ((uint32_t)0x1)
  93. #define CAN_STATUS_DOS ((uint32_t)0x2)
  94. #define CAN_STATUS_TBS ((uint32_t)0x4)
  95. #define CAN_STATUS_TCS ((uint32_t)0x8)
  96. #define CAN_STATUS_RS ((uint32_t)0x10)
  97. #define CAN_STATUS_TS ((uint32_t)0x20)
  98. #define CAN_STATUS_ES ((uint32_t)0x40)
  99. #define CAN_STATUS_BS ((uint32_t)0x80)
  100. /**
  101. * @}
  102. */
  103. /**
  104. * @brief parasmeter of CAN Command register
  105. */
  106. #define CAN_TR 0x1
  107. #define CAN_AT 0x2
  108. #define CAN_RRB 0x4
  109. #define CAN_CDO 0x8
  110. /**
  111. * @}
  112. */
  113. /**
  114. * @brief CAN_Basic init structure definition
  115. */
  116. typedef struct
  117. {
  118. uint8_t SJW;
  119. uint8_t BRP;
  120. FlagStatus SAM;
  121. uint8_t TESG2;
  122. uint8_t TESG1;
  123. FunctionalState GTS;
  124. uint8_t CDCLK;
  125. uint8_t CLOSE_OPEN_CLK;
  126. uint8_t RXINTEN;
  127. uint8_t CBP;
  128. } CAN_Basic_InitTypeDef;
  129. /**
  130. * @}
  131. */
  132. /**
  133. * @brief CAN_Peli init structure definition
  134. */
  135. typedef struct
  136. {
  137. uint8_t SJW;
  138. uint8_t BRP;
  139. FlagStatus SAM;
  140. uint8_t TESG2;
  141. uint8_t TESG1;
  142. FunctionalState LOM;
  143. FunctionalState STM;
  144. FunctionalState SM;
  145. FunctionalState SRR;
  146. uint32_t EWLR;
  147. } CAN_Peli_InitTypeDef;
  148. /**
  149. * @}
  150. */
  151. /**
  152. * @brief CAN_Basic filter init structure definition
  153. */
  154. typedef struct
  155. {
  156. uint8_t CAN_FilterId; /*!< Specifies the filter identification number .
  157. This parameter can be a value between 0x00 and 0xFF */
  158. uint8_t CAN_FilterMaskId; /*!< Specifies the filter mask number or identification number,
  159. This parameter can be a value between 0x00 and 0xFF */
  160. } CAN_Basic_FilterInitTypeDef;
  161. /**
  162. * @}
  163. */
  164. /**
  165. * @brief CAN_Peli filter init structure definition
  166. */
  167. typedef struct
  168. {
  169. uint8_t AFM;
  170. uint8_t CAN_FilterId0; /*!< Specifies the filter identification number
  171. This parameter can be a value between 0x00 and 0xFF */
  172. uint8_t CAN_FilterId1;
  173. uint8_t CAN_FilterId2;
  174. uint8_t CAN_FilterId3;
  175. uint8_t CAN_FilterMaskId0; /*!< Specifies the filter mask number or identification number,
  176. This parameter can be a value between 0x00 and 0xFF */
  177. uint8_t CAN_FilterMaskId1;
  178. uint8_t CAN_FilterMaskId2;
  179. uint8_t CAN_FilterMaskId3;
  180. } CAN_Peli_FilterInitTypeDef;
  181. /**
  182. * @}
  183. */
  184. /**
  185. * @brief CAN_Peli transmit frame definition
  186. */
  187. typedef enum {DataFrame = 0, RemoteFrame = !DataFrame} TransFrame;
  188. /**
  189. * @}
  190. */
  191. /**
  192. * @brief CAN_Basic Tx message structure definition
  193. */
  194. typedef struct
  195. {
  196. uint8_t IDH; /*!< Specifies the standard high identifier.
  197. This parameter can be a value between 0 to 0xFF. */
  198. uint8_t IDL; /*!< Specifies the standard low identifier.
  199. This parameter can be a value between 0 to 0x7. */
  200. uint8_t RTR; /*!< Specifies the type of frame for the message that will
  201. be transmitted. This parameter can be @TransFrame */
  202. uint8_t DLC; /*!< Specifies the length of the frame that will be
  203. transmitted. This parameter can be a value between
  204. 0 to 8 */
  205. uint8_t Data[8]; /*!< Contains the data to be transmitted. It ranges from 0
  206. to 0xFF. */
  207. } CanBasicTxMsg;
  208. /**
  209. * @}
  210. */
  211. /**
  212. * @brief CAN_Basic Rx message structure definition
  213. */
  214. typedef struct
  215. {
  216. uint16_t ID; /*!< Specifies the standard identifier.
  217. This parameter can be a value between 0 to 0x7FF. */
  218. uint8_t RTR; /*!< Specifies the type of frame for the received message.
  219. This parameter can be a value of
  220. @ref TransFrame */
  221. uint8_t DLC; /*!< Specifies the length of the frame that will be received.
  222. This parameter can be a value between 0 to 8 */
  223. uint8_t Data[8]; /*!< Contains the data to be received. It ranges from 0 to
  224. 0xFF. */
  225. } CanBasicRxMsg;
  226. /**
  227. * @}
  228. */
  229. /**
  230. * @brief CAN_Peli_Tx message structure definition
  231. */
  232. typedef struct
  233. {
  234. uint8_t IDLL; /*!< Specifies the extended identifier.
  235. This parameter can be a value between 0 to 0xFF. */
  236. uint8_t IDLH;
  237. uint8_t IDHL;
  238. uint8_t IDHH;
  239. uint8_t FF; /*!< Specifies the type of identifier for the message that
  240. will be transmitted. This parameter can be a value
  241. of @ref CAN_identifier_type */
  242. uint8_t RTR; /*!< Specifies the type of frame for the message that will
  243. be transmitted. This parameter can be a value of
  244. @ref TransFrame */
  245. uint8_t DLC; /*!< Specifies the length of the frame that will be
  246. transmitted. This parameter can be a value between
  247. 0 to 8 */
  248. uint8_t Data[8]; /*!< Contains the data to be transmitted. It ranges from 0
  249. to 0xFF. */
  250. } CanPeliTxMsg;
  251. /**
  252. * @}
  253. */
  254. /**
  255. * @brief CAN Rx message structure definition
  256. */
  257. typedef struct
  258. {
  259. uint32_t ID; /*!< Specifies the extended identifier.
  260. This parameter can be a value between 0 to 0x1FFFFFFF. */
  261. uint8_t FF; /*!< Specifies the type of identifier for the message that
  262. will be received. This parameter can be a value of
  263. @ref CAN_identifier_type */
  264. uint8_t RTR; /*!< Specifies the type of frame for the received message.
  265. This parameter can be a value of
  266. @ref TransFrame */
  267. uint8_t DLC; /*!< Specifies the length of the frame that will be received.
  268. This parameter can be a value between 0 to 8 */
  269. uint8_t Data[8]; /*!< Contains the data to be received. It ranges from 0 to
  270. 0xFF. */
  271. } CanPeliRxMsg;
  272. #define CANTXFAILED ((uint8_t)0x00) /* CAN transmission failed */
  273. #define CANTXOK ((uint8_t)0x01) /* CAN transmission succeeded */
  274. #define CANTXPENDING ((uint8_t)0x02) /* CAN transmission pending */
  275. #define CAN_NO_MB ((uint8_t)0x04) /* CAN cell did not provide an empty mailbox */
  276. /************************ Basic and Peli Work all need function ********************/
  277. void CAN_Mode_Cmd(CAN_TypeDef* CANx, uint32_t CAN_MODE);
  278. void CAN_ResetMode_Cmd(CAN_TypeDef* CANx, FunctionalState NewState);
  279. void CAN_ClearDataOverflow(CAN_TypeDef* CANx);
  280. void CAN_ClearITPendingBit(CAN_TypeDef* CANx);
  281. /************************ Basic Work function ********************/
  282. void CAN_DeInit(CAN_TypeDef* CANx);
  283. uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_Basic_InitTypeDef* CAN_Basic_InitStruct);
  284. void CAN_FilterInit(CAN_TypeDef* CANx, CAN_Basic_FilterInitTypeDef* CAN_Basic_FilterInitStruct);
  285. void CAN_StructInit(CAN_Basic_InitTypeDef* CAN_Basic_InitStruct);
  286. void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState Newstate);
  287. uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanBasicTxMsg* BasicTxMessage);
  288. void CAN_CancelTransmit(CAN_TypeDef* CANx);
  289. void CAN_FIFORelease(CAN_TypeDef* CANx);
  290. void CAN_Receive(CAN_TypeDef* CANx, CanBasicRxMsg* BasicRxMessage);
  291. uint8_t CAN_Sleep(CAN_TypeDef* CANx);
  292. uint8_t CAN_WakeUp(CAN_TypeDef* CANx);
  293. FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG);
  294. ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT);
  295. /************************ Peli Work function *********************/
  296. void CAN_Peli_SleepMode_Cmd(CAN_TypeDef* CANx, FunctionalState NewState);
  297. void CAN_Peli_Init(CAN_TypeDef* CANx, CAN_Peli_InitTypeDef* CAN_Peli_InitStruct);
  298. void CAN_Peli_StructInit(CAN_Peli_InitTypeDef* CAN_Peli_InitStruct);
  299. void CAN_Peli_FilterInit(CAN_TypeDef* CANx, CAN_Peli_FilterInitTypeDef* CAN_Peli_FilterInitStruct);
  300. void CAN_Peli_FilterStructInit(CAN_Peli_FilterInitTypeDef* CAN_Peli_FilterInitStruct);
  301. void CAN_Peli_Transmit(CAN_TypeDef* CANx, CanPeliTxMsg* PeliTxMessage);
  302. void CAN_Peli_TransmitRepeat(CAN_TypeDef* CANx, CanPeliTxMsg* PeliTxMessage);
  303. void CAN_Peli_Receive(CAN_TypeDef* CANx, CanPeliRxMsg* PeliRxMessage);
  304. uint32_t CAN_Peli_GetRxFIFOInfo(CAN_TypeDef* CANx);
  305. uint8_t CAN_Peli_GetLastErrorCode(CAN_TypeDef* CANx);
  306. uint8_t CAN_Peli_GetReceiveErrorCounter(CAN_TypeDef* CANx);
  307. uint8_t CAN_Peli_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx);
  308. void CAN_Peli_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState);
  309. ITStatus CAN_Peli_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT);
  310. void CAN_AutoCfg_BaudParam(CAN_Peli_InitTypeDef *CAN_Peli_InitStruct, unsigned int SrcClk, unsigned int baud );
  311. #endif /* __HAL_CAN_H */
  312. /**
  313. * @}
  314. */
  315. /**
  316. * @}
  317. */
  318. /**
  319. * @}
  320. */
  321. /*-------------------------(C) COPYRIGHT 2016 HOLOCENE ----------------------*/