hal_can.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_can.h
  3. /// @author AE TEAM
  4. /// @brief THIS FILE CONTAINS ALL THE FUNCTIONS PROTOTYPES FOR THE CAN
  5. /// FIRMWARE LIBRARY.
  6. ////////////////////////////////////////////////////////////////////////////////
  7. /// @attention
  8. ///
  9. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  10. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  11. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  12. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  13. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  14. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  15. ///
  16. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // Define to prevent recursive inclusion
  19. #ifndef __HAL_CAN_H
  20. #define __HAL_CAN_H
  21. // Files includes
  22. #include "types.h"
  23. #include "reg_can.h"
  24. ////////////////////////////////////////////////////////////////////////////////
  25. /// @addtogroup MM32_Hardware_Abstract_Layer
  26. /// @{
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /// @defgroup CAN_HAL
  29. /// @brief CAN HAL modules
  30. /// @{
  31. ////////////////////////////////////////////////////////////////////////////////
  32. /// @defgroup CAN_Exported_Types
  33. /// @{
  34. ////////////////////////////////////////////////////////////////////////////////
  35. /// @brief CAN_Initialization
  36. ////////////////////////////////////////////////////////////////////////////////
  37. typedef enum {
  38. CANINITFAILED = 0x00000000, ///< CAN initialization failed
  39. CANINITOK = 0x00000001 ///< CAN initialization ok
  40. } emCAN_INIT_Typedef;
  41. ////////////////////////////////////////////////////////////////////////////////
  42. /// @brief CAN_sleep_constants
  43. ////////////////////////////////////////////////////////////////////////////////
  44. typedef enum {
  45. CANSLEEPFAILED = 0x00000000, ///< CAN did not enter the sleep mode
  46. CANSLEEPOK = 0x00000001 ///< CAN entered the sleep mode
  47. } emCAN_SLEEP_conts_Typedef;
  48. ////////////////////////////////////////////////////////////////////////////////
  49. /// @brief CAN_wake_up_constants
  50. ////////////////////////////////////////////////////////////////////////////////
  51. typedef enum {
  52. CANWAKEUPFAILED = 0x00000000, ///< CAN did not leave the sleep mode
  53. CANWAKEUPOK = 0x00000001 ///< CAN leaved the sleep mode
  54. } emCAN_WAKE_conts_Typedef;
  55. ////////////////////////////////////////////////////////////////////////////////
  56. /// @brief CAN_Mode
  57. ////////////////////////////////////////////////////////////////////////////////
  58. typedef enum {
  59. CAN_BASICMode = 0x00000000,
  60. CAN_PELIMode = 0x00000080,
  61. CAN_WorkMode = 0x00000080,
  62. CAN_ResetMode = 0x00000001,
  63. CAN_ListenOnlyMode = 0x00000002,
  64. CAN_SeftTestMode = 0x00000004,
  65. CAN_FilterMode_Singal = 0x00000008,
  66. CAN_FilterMode_Double = 0x000000f7,
  67. CAN_SleepMode = 0x00000010
  68. } emCAN_CAN_Mode_Typedef;
  69. ////////////////////////////////////////////////////////////////////////////////
  70. /// @brief BASIC_CAN_interrupt
  71. ////////////////////////////////////////////////////////////////////////////////
  72. typedef enum {
  73. CAN_IT_RIE = CAN_CR_RIE, ///< Overflow interrupt enable
  74. CAN_IT_TIE = CAN_CR_TIE, ///< Transmit interrupt enable
  75. CAN_IT_EIE = CAN_CR_EIE, ///< Error interrupt enable
  76. CAN_IT_OIE = CAN_CR_OIE ///< Receive interrupt enable
  77. } emCAN_BASIC_IntEn_Typedef;
  78. ////////////////////////////////////////////////////////////////////////////////
  79. /// @brief PELI_CAN_interrupt
  80. ////////////////////////////////////////////////////////////////////////////////
  81. typedef enum {
  82. CAN_IT_RI = CAN_IR_RI, ///< Overflow interrupt enable
  83. CAN_IT_TI = CAN_IR_TI, ///< Transmit interrupt enable
  84. CAN_IT_EI = CAN_IR_EI, ///< Error interrupt enable
  85. CAN_IT_DOI = CAN_IR_DOI, ///< Receive interrupt enable
  86. CAN_IT_WUI = 0x00001010, ///< Receive interrupt enable
  87. CAN_IT_EPI = CAN_IR_EPI, ///< Receive interrupt enable
  88. CAN_IT_ALI = CAN_IR_ALI, ///< Receive interrupt enable
  89. CAN_IT_BEI = CAN_IR_BEI, ///< Receive interrupt enable
  90. CAN_IT_ALL = 0xFFFF ///< Receive interrupt enable
  91. } emCAN_PELI_IntEn_Typedef;
  92. ////////////////////////////////////////////////////////////////////////////////
  93. /// @brief CAN_Status
  94. ////////////////////////////////////////////////////////////////////////////////
  95. typedef enum {
  96. CAN_STATUS_RBS = CAN_SR_RBS,
  97. CAN_STATUS_DOS = CAN_SR_DOS,
  98. CAN_STATUS_TBS = CAN_SR_TBS,
  99. CAN_STATUS_TCS = CAN_SR_TCS,
  100. CAN_STATUS_RS = CAN_SR_RS,
  101. CAN_STATUS_TS = CAN_SR_TS,
  102. CAN_STATUS_ES = CAN_SR_ES,
  103. CAN_STATUS_BS = CAN_SR_BS
  104. } emCAN_Status_Typedef;
  105. ////////////////////////////////////////////////////////////////////////////////
  106. /// @brief CAN_Command_register
  107. ////////////////////////////////////////////////////////////////////////////////
  108. typedef enum {
  109. CAN_TR = CAN_CMR_TR, ///< Transmission request
  110. CAN_AT = CAN_CMR_AT,
  111. CAN_RRB = CAN_CMR_RRB,
  112. CAN_CDO = CAN_CMR_CDO
  113. } emCAN_Command_Typedef;
  114. ////////////////////////////////////////////////////////////////////////////////
  115. /// @brief CAN_Peli transmit frame definition
  116. ////////////////////////////////////////////////////////////////////////////////
  117. typedef enum {
  118. DataFrame = 0, ///< Data Frame
  119. RemoteFrame = !DataFrame
  120. } TransFrame;
  121. ////////////////////////////////////////////////////////////////////////////////
  122. /// @brief CAN_Basic init structure definition
  123. ////////////////////////////////////////////////////////////////////////////////
  124. typedef struct {
  125. u8 SJW;
  126. u8 BRP;
  127. FlagStatus SAM;
  128. u8 TESG2;
  129. u8 TESG1;
  130. FunctionalState GTS;
  131. u8 CDCLK;
  132. u8 CLOSE_OPEN_CLK;
  133. u8 RXINTEN;
  134. u8 CBP;
  135. } CAN_Basic_InitTypeDef;
  136. ////////////////////////////////////////////////////////////////////////////////
  137. /// @brief CAN_Peli init structure definition
  138. ////////////////////////////////////////////////////////////////////////////////
  139. typedef struct {
  140. u8 SJW;
  141. u8 BRP;
  142. FlagStatus SAM;
  143. u8 TESG2;
  144. u8 TESG1;
  145. FunctionalState LOM;
  146. FunctionalState STM;
  147. FunctionalState SM;
  148. FunctionalState SRR;
  149. u32 EWLR;
  150. } CAN_Peli_InitTypeDef;
  151. ////////////////////////////////////////////////////////////////////////////////
  152. /// @brief CAN_Basic filter init structure definition
  153. ////////////////////////////////////////////////////////////////////////////////
  154. typedef struct {
  155. u8 CAN_FilterId; ///< Specifies the filter identification number. This parameter can be a value between 0x00 and 0xFF.
  156. u8 CAN_FilterMaskId; ///< Specifies the filter mask number or identification number, This parameter can be a value between
  157. ///< 0x00 and 0xFF.
  158. } CAN_Basic_FilterInitTypeDef;
  159. ////////////////////////////////////////////////////////////////////////////////
  160. /// @brief CAN_Peli filter init structure definition
  161. ////////////////////////////////////////////////////////////////////////////////
  162. typedef struct {
  163. u8 AFM;
  164. u8 CAN_FilterId0; ///< Specifies the filter identification number, This parameter can be a value between 0x00 and 0xFF
  165. u8 CAN_FilterId1;
  166. u8 CAN_FilterId2;
  167. u8 CAN_FilterId3;
  168. u8 CAN_FilterMaskId0; ///< Specifies the filter mask number or identification number, This parameter can be a value between
  169. ///< 0x00 and 0xFF
  170. u8 CAN_FilterMaskId1;
  171. u8 CAN_FilterMaskId2;
  172. u8 CAN_FilterMaskId3;
  173. } CAN_Peli_FilterInitTypeDef;
  174. ////////////////////////////////////////////////////////////////////////////////
  175. /// @brief CAN_Basic Tx message structure definition
  176. ////////////////////////////////////////////////////////////////////////////////
  177. typedef struct {
  178. u8 IDH; ///< Specifies the standard high identifier. This parameter can be a value between 0 to 0xFF.
  179. u8 IDL; ///< Specifies the standard low identifier. This parameter can be a value between 0 to 0x7.
  180. u8 RTR; ///< Specifies the type of frame for the message that will be transmitted. This parameter can be @TransFrame.
  181. u8 DLC; ///< Specifies the length of the frame that will be transmitted. This parameter can be a value between 0 to 8.
  182. u8 Data[8]; ///< Contains the data to be transmitted. It ranges from 0 to 0xFF.
  183. } CanBasicTxMsg;
  184. ////////////////////////////////////////////////////////////////////////////////
  185. /// @brief CAN_Basic Rx message structure definition
  186. ////////////////////////////////////////////////////////////////////////////////
  187. typedef struct {
  188. u16 ID; ///< Specifies the standard identifier. This parameter can be a value between 0 to 0x7FF.
  189. u8 RTR; ///< Specifies the type of frame for the received message. This parameter can be a value of @ref TransFrame
  190. u8 DLC; ///< Specifies the length of the frame that will be received. This parameter can be a value between 0 to 8
  191. u8 Data[8]; ///< Contains the data to be received. It ranges from 0 to 0xFF.
  192. } CanBasicRxMsg;
  193. ///////////////////////////////////////////////////////////////////////////////
  194. /// @brief CAN_Peli_Tx message structure definition
  195. ///////////////////////////////////////////////////////////////////////////////
  196. typedef struct {
  197. u8 IDLL; ///< Specifies the extended identifier.
  198. ///< This parameter can be a value between 0 to 0xFF.
  199. u8 IDLH;
  200. u8 IDHL;
  201. u8 IDHH;
  202. u8 FF; ///< Specifies the type of identifier for the message that will be transmitted. This parameter can be a value of @ref
  203. ///< CAN_identifier_type
  204. u8 RTR; ///< Specifies the type of frame for the message that will be transmitted. This parameter can be a value of @ref
  205. ///< TransFrame.
  206. u8 DLC; ///< Specifies the length of the frame that will be transmitted. This parameter can be a value between 0 to 8.
  207. u8 Data[8]; ///< Contains the data to be transmitted. It ranges from 0 to 0xFF.
  208. } CanPeliTxMsg;
  209. ////////////////////////////////////////////////////////////////////////////////
  210. /// @brief CAN Rx message structure definition
  211. ////////////////////////////////////////////////////////////////////////////////
  212. typedef struct {
  213. u32 ID; ///< Specifies the extended identifier. This parameter can be a value between 0 to 0x1FFFFFFF.
  214. u8 FF; ///< Specifies the type of identifier for the message that will be received. This parameter can be a value of @ref
  215. ///< CAN_identifier_type.
  216. u8 RTR; ///< Specifies the type of frame for the received message. This parameter can be a value of @ref TransFrame.
  217. u8 DLC; ///< Specifies the length of the frame that will be received. This parameter can be a value between 0 to 8.
  218. u8 Data[8]; ///< Contains the data to be received. It ranges from 0 to0xFF.
  219. } CanPeliRxMsg;
  220. /// @}
  221. ////////////////////////////////////////////////////////////////////////////////
  222. /// @defgroup CAN_Exported_Constants
  223. /// @{
  224. ////////////////////////////////////////////////////////////////////////////////
  225. /// @defgroup parasmeter_of_CAN_transmission_register
  226. /// @{
  227. #define CANTXFAILED (0x00U) ///< CAN transmission failed
  228. #define CANTXOK (0x01U) ///< CAN transmission succeeded
  229. #define CANTXPENDING (0x02U) ///< CAN transmission pending
  230. #define CAN_NO_MB (0x04U) ///< CAN cell did not provide an empty mailbox
  231. /// @}
  232. /// @}
  233. ////////////////////////////////////////////////////////////////////////////////
  234. /// @defgroup CAN_Exported_Variables
  235. /// @{
  236. #ifdef _HAL_CAN_C_
  237. #define GLOBAL
  238. #else
  239. #define GLOBAL extern
  240. #endif
  241. #undef GLOBAL
  242. /// @}
  243. ////////////////////////////////////////////////////////////////////////////////
  244. /// @defgroup CAN_Exported_Functions
  245. /// @{
  246. // Basic and Peli Work all need function ---------------------------------------
  247. void CAN_Mode_Cmd(CAN_TypeDef* can, u32 mode);
  248. void CAN_ResetMode_Cmd(CAN_TypeDef* can, FunctionalState state);
  249. void CAN_ClearDataOverflow(CAN_TypeDef* can);
  250. void CAN_ClearITPendingBit(CAN_TypeDef* can);
  251. // Basic Work function ---------------------------------------------------------
  252. void CAN_DeInit(CAN_TypeDef* can);
  253. void CAN_FilterInit(CAN_Basic_FilterInitTypeDef* basic_filter_init_struct);
  254. void CAN_StructInit(CAN_Basic_InitTypeDef* basic_init_struct);
  255. void CAN_ITConfig(CAN_TypeDef* can, u32 it, FunctionalState state);
  256. void CAN_CancelTransmit(CAN_TypeDef* can);
  257. void CAN_FIFORelease(CAN_TypeDef* can);
  258. void CAN_Receive(CAN_TypeDef* can, CanBasicRxMsg* basic_receive_message);
  259. u8 CAN_Transmit(CAN_TypeDef* can, CanBasicTxMsg* basic_transmit_message);
  260. u8 CAN_Init(CAN_TypeDef* can, CAN_Basic_InitTypeDef* basic_init_struct);
  261. u8 CAN_Sleep(CAN_TypeDef* can);
  262. u8 CAN_WakeUp(CAN_TypeDef* can);
  263. FlagStatus CAN_GetFlagStatus(CAN_TypeDef* can, u32 flag);
  264. ITStatus CAN_GetITStatus(CAN_TypeDef* can, u32 it);
  265. // Peli Work function ----------------------------------------------------------
  266. void CAN_Peli_SleepMode_Cmd(FunctionalState state);
  267. void CAN_Peli_Init(CAN_Peli_InitTypeDef* init_struct);
  268. void CAN_Peli_StructInit(CAN_Peli_InitTypeDef* peli_init_struct);
  269. void CAN_Peli_FilterInit(CAN_Peli_FilterInitTypeDef* peli_filter_init_struct);
  270. void CAN_Peli_FilterStructInit(CAN_Peli_FilterInitTypeDef* peli_filter_init_struct);
  271. void CAN_Peli_Transmit(CanPeliTxMsg* peli_transmit_message);
  272. void CAN_Peli_TransmitRepeat(CanPeliTxMsg* peli_transmit_message);
  273. void CAN_Peli_Receive(CanPeliRxMsg* peli_receive_message);
  274. void CAN_Peli_ITConfig(u32 it, FunctionalState state);
  275. void CAN_AutoCfg_BaudParam(CAN_Peli_InitTypeDef* init_struct, u32 src_clk, u32 baud);
  276. u32 CAN_Peli_GetRxFIFOInfo(void);
  277. u8 CAN_Peli_GetLastErrorCode(void);
  278. u8 CAN_Peli_GetReceiveErrorCounter(void);
  279. u8 CAN_Peli_GetLSBTransmitErrorCounter(void);
  280. ITStatus CAN_Peli_GetITStatus(u32 it);
  281. /// @}
  282. /// @}
  283. /// @}
  284. ////////////////////////////////////////////////////////////////////////////////
  285. #endif
  286. ////////////////////////////////////////////////////////////////////////////////