apm32f10x_can.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*!
  2. * @file apm32f10x_can.h
  3. *
  4. * @brief This file contains all the functions prototypes for the CAN firmware library
  5. *
  6. * @version V1.0.4
  7. *
  8. * @date 2022-12-01
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2020-2022 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be useful and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. /* Define to prevent recursive inclusion */
  26. #ifndef __APM32F10X_CAN_H
  27. #define __APM32F10X_CAN_H
  28. /* Includes */
  29. #include "apm32f10x.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /** @addtogroup APM32F10x_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup CAN_Driver CAN Driver
  37. @{
  38. */
  39. /** @defgroup CAN_Enumerations Enumerations
  40. @{
  41. */
  42. /**
  43. * @brief CAN mode
  44. */
  45. typedef enum
  46. {
  47. CAN_MODE_NORMAL = ((uint8_t)0x00), /*!< normal mode */
  48. CAN_MODE_LOOPBACK = ((uint8_t)0x01), /*!< loopback mode */
  49. CAN_MODE_SILENT = ((uint8_t)0x02), /*!< silent mode */
  50. CAN_MODE_SILENT_LOOPBACK = ((uint8_t)0x03) /*!< loopback combined with silent mode */
  51. } CAN_MODE_T;
  52. /**
  53. * @brief CAN synchronisation jump width
  54. */
  55. typedef enum
  56. {
  57. CAN_SJW_1 = ((uint8_t)0x00), /*!< 1 time quantum */
  58. CAN_SJW_2 = ((uint8_t)0x01), /*!< 2 time quantum */
  59. CAN_SJW_3 = ((uint8_t)0x02), /*!< 3 time quantum */
  60. CAN_SJW_4 = ((uint8_t)0x03) /*!< 4 time quantum */
  61. } CAN_SJW_T;
  62. /**
  63. * @brief CAN time quantum in bit segment 1
  64. */
  65. typedef enum
  66. {
  67. CAN_TIME_SEGMENT1_1 = ((uint8_t)0x00), /*!< 1 time quanta */
  68. CAN_TIME_SEGMENT1_2 = ((uint8_t)0x01), /*!< 2 time quanta */
  69. CAN_TIME_SEGMENT1_3 = ((uint8_t)0x02), /*!< 3 time quanta */
  70. CAN_TIME_SEGMENT1_4 = ((uint8_t)0x03), /*!< 4 time quanta */
  71. CAN_TIME_SEGMENT1_5 = ((uint8_t)0x04), /*!< 5 time quanta */
  72. CAN_TIME_SEGMENT1_6 = ((uint8_t)0x05), /*!< 6 time quanta */
  73. CAN_TIME_SEGMENT1_7 = ((uint8_t)0x06), /*!< 7 time quanta */
  74. CAN_TIME_SEGMENT1_8 = ((uint8_t)0x07), /*!< 8 time quanta */
  75. CAN_TIME_SEGMENT1_9 = ((uint8_t)0x08), /*!< 9 time quanta */
  76. CAN_TIME_SEGMENT1_10 = ((uint8_t)0x09), /*!< 10 time quanta */
  77. CAN_TIME_SEGMENT1_11 = ((uint8_t)0x0A), /*!< 11 time quanta */
  78. CAN_TIME_SEGMENT1_12 = ((uint8_t)0x0B), /*!< 12 time quanta */
  79. CAN_TIME_SEGMENT1_13 = ((uint8_t)0x0C), /*!< 13 time quanta */
  80. CAN_TIME_SEGMENT1_14 = ((uint8_t)0x0D), /*!< 14 time quanta */
  81. CAN_TIME_SEGMENT1_15 = ((uint8_t)0x0E), /*!< 15 time quanta */
  82. CAN_TIME_SEGMENT1_16 = ((uint8_t)0x0F) /*!< 16 time quanta */
  83. } CAN_TIME_SEGMENT1_T;
  84. /**
  85. * @brief CAN time quantum in bit segment 2
  86. */
  87. typedef enum
  88. {
  89. CAN_TIME_SEGMENT2_1 = (uint8_t)0x00, /*!< 1 time quanta */
  90. CAN_TIME_SEGMENT2_2 = (uint8_t)0x01, /*!< 2 time quanta */
  91. CAN_TIME_SEGMENT2_3 = (uint8_t)0x02, /*!< 3 time quanta */
  92. CAN_TIME_SEGMENT2_4 = (uint8_t)0x03, /*!< 4 time quanta */
  93. CAN_TIME_SEGMENT2_5 = (uint8_t)0x04, /*!< 5 time quanta */
  94. CAN_TIME_SEGMENT2_6 = (uint8_t)0x05, /*!< 6 time quanta */
  95. CAN_TIME_SEGMENT2_7 = (uint8_t)0x06, /*!< 7 time quanta */
  96. CAN_TIME_SEGMENT2_8 = (uint8_t)0x07 /*!< 8 time quanta */
  97. } CAN_TIME_SEGMENT2_T;
  98. /**
  99. * @brief CAN filter FIFO
  100. */
  101. typedef enum
  102. {
  103. CAN_FILTER_FIFO_0 = ((uint8_t)0x00), /*!< filter FIFO 0 */
  104. CAN_FILTER_FIFO_1 = ((uint8_t)0x01) /*!< filter FIFO 1 */
  105. } CAN_FILTER_FIFO_T;
  106. /**
  107. * @brief CAN filter mode
  108. */
  109. typedef enum
  110. {
  111. CAN_FILTER_MODE_IDMASK = ((uint8_t)0x00),/*!< identifier/mask mode */
  112. CAN_FILTER_MODE_IDLIST = ((uint8_t)0x01) /*!< identifier list mode */
  113. } CAN_FILTER_MODE_T;
  114. /**
  115. * @brief CAN filter scale
  116. */
  117. typedef enum
  118. {
  119. CAN_FILTER_SCALE_16BIT = ((uint8_t)0x00), /*!< Two 16-bit filters */
  120. CAN_FILTER_SCALE_32BIT = ((uint8_t)0x01) /*!< One 32-bit filter */
  121. } CAN_FILTER_SCALE_T;
  122. /**
  123. * @brief CAN identifier type
  124. */
  125. typedef enum
  126. {
  127. CAN_TYPEID_STD = ((uint32_t)0x00000000), /*!< Standard Id */
  128. CAN_TYPEID_EXT = ((uint32_t)0x00000004) /*!< Extended Id */
  129. } CAN_TYPEID_T;
  130. /**
  131. * @brief CAN_remote_transmission_request
  132. */
  133. typedef enum
  134. {
  135. CAN_RTXR_DATA = ((uint32_t)0x00000000), /*!< Data frame */
  136. CAN_RTXR_REMOTE = ((uint32_t)0x00000002) /*!< Remote frame */
  137. } CAN_RTXR_T;
  138. /**
  139. * @brief Mailboxes definition
  140. */
  141. typedef enum
  142. {
  143. CAN_TX_MAILBIX_0 = ((uint8_t)0x00), /*!< Tx mailbox0 */
  144. CAN_TX_MAILBIX_1 = ((uint8_t)0x01), /*!< Tx mailbox1 */
  145. CAN_TX_MAILBIX_2 = ((uint8_t)0x02) /*!< Tx mailbox2 */
  146. } CAN_TX_MAILBIX_T;
  147. /**
  148. * @brief CAN receive FIFO number constants
  149. */
  150. typedef enum
  151. {
  152. CAN_RX_FIFO_0 = ((uint8_t)0x00), /*!< receive FIFO 0 */
  153. CAN_RX_FIFO_1 = ((uint8_t)0x01) /*!< receive FIFO 1 */
  154. } CAN_RX_FIFO_T;
  155. /**
  156. * @brief CAN Operating Mode
  157. */
  158. typedef enum
  159. {
  160. CAN_OPERATING_MODE_INIT = ((uint8_t)0x00), /*!< Initialization mode */
  161. CAN_OPERATING_MODE_NORMAL = ((uint8_t)0x01), /*!< Normal mode */
  162. CAN_OPERATING_MODE_SLEEP = ((uint8_t)0x02) /*!< sleep mode */
  163. } CAN_OPERATING_MODE_T;
  164. /**
  165. * @brief CAN Interrupts
  166. */
  167. typedef enum
  168. {
  169. CAN_INT_TXME = ((uint32_t)0x00000001), /*!< Transmit mailbox empty Interrupt */
  170. CAN_INT_F0MP = ((uint32_t)0x00000002), /*!< FIFO 0 message pending Interrupt */
  171. CAN_INT_F0FULL = ((uint32_t)0x00000004), /*!< FIFO 0 full Interrupt */
  172. CAN_INT_F0OVR = ((uint32_t)0x00000008), /*!< FIFO 0 overrun Interrupt */
  173. CAN_INT_F1MP = ((uint32_t)0x00000010), /*!< FIFO 1 message pending Interrupt */
  174. CAN_INT_F1FULL = ((uint32_t)0x00000020), /*!< FIFO 1 full Interrupt */
  175. CAN_INT_F1OVR = ((uint32_t)0x00000040), /*!< FIFO 1 overrun Interrupt */
  176. CAN_INT_ERRW = ((uint32_t)0x00000100), /*!< Error warning Interrupt */
  177. CAN_INT_ERRP = ((uint32_t)0x00000200), /*!< Error passive Interrupt */
  178. CAN_INT_BOF = ((uint32_t)0x00000400), /*!< Bus-off Interrupt */
  179. CAN_INT_LEC = ((uint32_t)0x00000800), /*!< Last error record code Interrupt */
  180. CAN_INT_ERR = ((uint32_t)0x00008000), /*!< Error Interrupt */
  181. CAN_INT_WUP = ((uint32_t)0x00010000), /*!< Wake-up Interrupt */
  182. CAN_INT_SLEEP = ((uint32_t)0x00020000) /*!< Sleep acknowledge Interrupt */
  183. } CAN_INT_T;
  184. /**
  185. * @brief CAN Flags
  186. */
  187. typedef enum
  188. {
  189. /** Error flag*/
  190. CAN_FLAG_ERRW = ((uint32_t)0x10F00001), /*!< Error Warning Flag */
  191. CAN_FLAG_ERRP = ((uint32_t)0x10F00002), /*!< Error Passive Flag */
  192. CAN_FLAG_BOF = ((uint32_t)0x10F00004), /*!< Bus-Off Flag */
  193. CAN_FLAG_LERRC = ((uint32_t)0x30F00070), /*!< Last error record code Flag */
  194. /** Operating Mode Flags */
  195. CAN_FLAG_WUPI = ((uint32_t)0x31000008), /*!< Wake up Flag */
  196. CAN_FLAG_SLEEP = ((uint32_t)0x31000012), /*!< Sleep acknowledge Flag */
  197. /** Receive Flags */
  198. CAN_FLAG_F0MP = ((uint32_t)0x12000003), /*!< FIFO 0 Message Pending Flag */
  199. CAN_FLAG_F0FULL = ((uint32_t)0x32000008), /*!< FIFO 0 Full Flag */
  200. CAN_FLAG_F0OVR = ((uint32_t)0x32000010), /*!< FIFO 0 Overrun Flag */
  201. CAN_FLAG_F1MP = ((uint32_t)0x14000003), /*!< FIFO 1 Message Pending Flag */
  202. CAN_FLAG_F1FULL = ((uint32_t)0x34000008), /*!< FIFO 1 Full Flag */
  203. CAN_FLAG_F1OVR = ((uint32_t)0x34000010), /*!< FIFO 1 Overrun Flag */
  204. /** Transmit Flags */
  205. CAN_FLAG_REQC0 = ((uint32_t)0x38000001), /*!< Request MailBox0 Flag */
  206. CAN_FLAG_REQC1 = ((uint32_t)0x38000100), /*!< Request MailBox1 Flag */
  207. CAN_FLAG_REQC2 = ((uint32_t)0x38010000) /*!< Request MailBox2 Flag */
  208. } CAN_FLAG_T;
  209. /**@} end of group CAN_Enumerations*/
  210. /** @defgroup CAN_Structures Structures
  211. @{
  212. */
  213. /**
  214. * @brief CAN Config structure definition
  215. */
  216. /**
  217. * @brief CAN config structure definition
  218. */
  219. typedef struct
  220. {
  221. uint8_t autoBusOffManage; /*!< Enable or disable the automatic bus-off management. */
  222. uint8_t autoWakeUpMode; /*!< Enable or disable the automatic wake-up mode. */
  223. uint8_t nonAutoRetran; /*!< Enable or disable the non-automatic retransmission mode. */
  224. uint8_t rxFIFOLockMode; /*!< Enable or disable the Receive FIFO Locked mode. */
  225. uint8_t txFIFOPriority; /*!< Enable or disable the transmit FIFO priority. */
  226. CAN_MODE_T mode; /*!< Specifies the CAN operating mode. */
  227. CAN_SJW_T syncJumpWidth; /* Specifies the maximum number of time quanta the CAN hardware
  228. is allowed to lengthen or shorten a bit to perform resynchronization.
  229. */
  230. CAN_TIME_SEGMENT1_T timeSegment1; /*!< Specifies the number of time quanta in Bit Segment 1. */
  231. CAN_TIME_SEGMENT2_T timeSegment2; /*!< Specifies the number of time quanta in Bit Segment 2. */
  232. uint16_t prescaler; /*!< Specifies the length of a time quantum. It can be 1 to 1024. */
  233. } CAN_Config_T;
  234. /**
  235. * @brief CAN Tx message structure definition
  236. */
  237. typedef struct
  238. {
  239. uint32_t stdID; /*!< Specifies the standard identifier. It can be 0 to 0x7FF. */
  240. uint32_t extID; /*!< Specifies the extended identifier. It can be 0 to 0x1FFFFFFF. */
  241. CAN_TYPEID_T typeID;
  242. CAN_RTXR_T remoteTxReq;
  243. uint8_t dataLengthCode;/*!< Specifies the data length code. It can be 0 to 8. */
  244. uint8_t data[8]; /*!< Specifies the data to be transmitted. It can be 0 to 0xFF. */
  245. } CAN_TxMessage_T;
  246. /**
  247. * @brief CAN Rx message structure definition
  248. */
  249. typedef struct
  250. {
  251. uint32_t stdID; /*!< Specifies the standard identifier. It can be 0 to 0x7FF. */
  252. uint32_t extID; /*!< Specifies the extended identifier. It can be 0 to 0x1FFFFFFF. */
  253. uint32_t typeID;
  254. uint32_t remoteTxReq;
  255. uint8_t dataLengthCode; /*!< Specifies the data length code. It can be 0 to 8. */
  256. uint8_t data[8]; /*!< Specifies the data to be transmitted. It can be 0 to 0xFF. */
  257. uint8_t filterMatchIndex;/*!< Specifies the filter match index. It can be 0 to 0xFF. */
  258. } CAN_RxMessage_T;
  259. /**
  260. * @brief CAN filter config structure definition
  261. */
  262. typedef struct
  263. {
  264. uint8_t filterNumber; /*!< Specifies the filter number. It can be 0 to 13. */
  265. uint16_t filterIdHigh; /*!< Specifies the filter identification number.It can be 0 to 0xFFFF. */
  266. uint16_t filterIdLow; /*!< Specifies the filter identification number.It can be 0 to 0xFFFF. */
  267. uint16_t filterMaskIdHigh; /*!< Specifies the filter mask identification. It can be 0 to 0xFFFF. */
  268. uint16_t filterMaskIdLow; /*!< Specifies the filter mask identification. It can be 0 to 0xFFFF. */
  269. uint16_t filterActivation; /*!< Specifies the filter Activation. It can be ENABLE or DISABLE. */
  270. CAN_FILTER_FIFO_T filterFIFO;
  271. CAN_FILTER_MODE_T filterMode;
  272. CAN_FILTER_SCALE_T filterScale;
  273. } CAN_FilterConfig_T;
  274. /**@} end of group CAN_Structures*/
  275. /** @defgroup CAN_Functions Functions
  276. @{
  277. */
  278. /* CAN reset and configuration */
  279. void CAN_Reset(CAN_T* can);
  280. uint8_t CAN_Config(CAN_T* can, CAN_Config_T* canConfig);
  281. #if defined(APM32F10X_CL)
  282. void CAN_ConfigFilter(CAN_FilterConfig_T* filterConfig);
  283. void CAN_SlaveStartBank(uint8_t bankNum);
  284. #else
  285. void CAN_ConfigFilter(CAN_T* can, CAN_FilterConfig_T* filterConfig);
  286. #endif
  287. void CAN_ConfigStructInit(CAN_Config_T* canConfig);
  288. void CAN_EnableDBGFreeze(CAN_T* can);
  289. void CAN_DisableDBGFreeze(CAN_T* can);
  290. /* CAN frames transmit */
  291. uint8_t CAN_TxMessage(CAN_T* can, CAN_TxMessage_T* TxMessage);
  292. uint8_t CAN_TxMessageStatus(CAN_T* can, CAN_TX_MAILBIX_T TxMailbox);
  293. void CAN_CancelTxMailbox(CAN_T* can, CAN_TX_MAILBIX_T TxMailbox);
  294. /* CAN frames receive */
  295. void CAN_RxMessage(CAN_T* can, CAN_RX_FIFO_T FIFONumber, CAN_RxMessage_T* RxMessage);
  296. void CAN_ReleaseFIFO(CAN_T* can, CAN_RX_FIFO_T FIFONumber);
  297. uint8_t CAN_PendingMessage(CAN_T* can, CAN_RX_FIFO_T FIFONumber);
  298. /* CAN operation modes */
  299. uint8_t CAN_OperatingMode(CAN_T* can, CAN_OPERATING_MODE_T operatingMode);
  300. uint8_t CAN_SleepMode(CAN_T* can);
  301. uint8_t CAN_WakeUpMode(CAN_T* can);
  302. /* CAN bus error management */
  303. uint8_t CAN_ReadLastErrorCode(CAN_T* can);
  304. uint8_t CAN_ReadRxErrorCounter(CAN_T* can);
  305. uint8_t CAN_ReadLSBTxErrorCounter(CAN_T* can);
  306. /* CAN interrupt and flag */
  307. void CAN_EnableInterrupt(CAN_T* can, uint32_t interrupt);
  308. void CAN_DisableInterrupt(CAN_T* can, uint32_t interrupt);
  309. uint8_t CAN_ReadStatusFlag(CAN_T* can, CAN_FLAG_T flag);
  310. void CAN_ClearStatusFlag(CAN_T* can, CAN_FLAG_T flag);
  311. uint8_t CAN_ReadIntFlag(CAN_T* can, CAN_INT_T flag);
  312. void CAN_ClearIntFlag(CAN_T* can, CAN_INT_T flag);
  313. /**@} end of group CAN_Functions*/
  314. /**@} end of group CAN_Driver */
  315. /**@} end of group APM32F10x_StdPeriphDriver */
  316. #ifdef __cplusplus
  317. }
  318. #endif
  319. #endif /* __APM32F10X_CAN_H */