HAL_can.h 10 KB

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