apm32f0xx_can.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*!
  2. * @file apm32f0xx_can.h
  3. *
  4. * @brief This file contains all the functions prototypes for the CAN firmware library
  5. *
  6. * @version V1.0.3
  7. *
  8. * @date 2022-09-20
  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 __APM32F0XX_CAN_H
  27. #define __APM32F0XX_CAN_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Includes */
  32. #include "apm32f0xx.h"
  33. /** @addtogroup APM32F0xx_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup CAN_Driver
  37. @{
  38. */
  39. /** @defgroup CAN_Macros Macros
  40. @{
  41. */
  42. /**@} end of group CAN_Macros */
  43. /** @defgroup CAN_Enumerations Enumerations
  44. @{
  45. */
  46. /**
  47. * @brief CAN operating mode
  48. */
  49. typedef enum
  50. {
  51. CAN_OPERATING_MODE_INIT = ((uint8_t)00), /*!< Initialization mode */
  52. CAN_OPERATING_MODE_NORMAL = ((uint8_t)01), /*!< Normal mode */
  53. CAN_OPERATING_MODE_SLEEP = ((uint8_t)02), /*!< sleep mode */
  54. } CAN_OPERATING_MODE_T;
  55. /**
  56. * @brief CAN test mode
  57. */
  58. typedef enum
  59. {
  60. CAN_MODE_NORMAL = ((uint8_t)00), /*!< normal mode */
  61. CAN_MODE_LOOPBACK = ((uint8_t)01), /*!< loopback mode */
  62. CAN_MODE_SILENT = ((uint8_t)02), /*!< silent mode */
  63. CAN_MODE_SILENT_LOOPBACK = ((uint8_t)03), /*!< loopback combined with silent mode */
  64. } CAN_MODE_T;
  65. /**
  66. * @brief CAN filter mode
  67. */
  68. typedef enum
  69. {
  70. CAN_FILTER_MODE_IDMASK = ((uint8_t)00), /*!< identifier/mask mode */
  71. CAN_FILTER_MODE_IDLIST = ((uint8_t)01), /*!< identifier list mode */
  72. } CAN_FILTER_MODE_T;
  73. /**
  74. * @brief CAN synchronisation jump width
  75. */
  76. typedef enum
  77. {
  78. CAN_SJW_1 = ((uint8_t)00), /*!< 1 time quantum */
  79. CAN_SJW_2 = ((uint8_t)01), /*!< 2 time quantum */
  80. CAN_SJW_3 = ((uint8_t)02), /*!< 3 time quantum */
  81. CAN_SJW_4 = ((uint8_t)03), /*!< 4 time quantum */
  82. } CAN_SJW_T;
  83. /**
  84. * @brief CAN_time quantum in bit_segment_1
  85. */
  86. typedef enum
  87. {
  88. CAN_TIME_SEGMENT1_1 = (uint8_t)0x00, /*!< 1 time quanta */
  89. CAN_TIME_SEGMENT1_2 = (uint8_t)0x01, /*!< 2 time quanta */
  90. CAN_TIME_SEGMENT1_3 = (uint8_t)0x02, /*!< 3 time quanta */
  91. CAN_TIME_SEGMENT1_4 = (uint8_t)0x03, /*!< 4 time quanta */
  92. CAN_TIME_SEGMENT1_5 = (uint8_t)0x04, /*!< 5 time quanta */
  93. CAN_TIME_SEGMENT1_6 = (uint8_t)0x05, /*!< 6 time quanta */
  94. CAN_TIME_SEGMENT1_7 = (uint8_t)0x06, /*!< 7 time quanta */
  95. CAN_TIME_SEGMENT1_8 = (uint8_t)0x07, /*!< 8 time quanta */
  96. CAN_TIME_SEGMENT1_9 = (uint8_t)0x08, /*!< 9 time quanta */
  97. CAN_TIME_SEGMENT1_10 = (uint8_t)0x09, /*!< 10 time quanta */
  98. CAN_TIME_SEGMENT1_11 = (uint8_t)0x0A, /*!< 11 time quanta */
  99. CAN_TIME_SEGMENT1_12 = (uint8_t)0x0B, /*!< 12 time quanta */
  100. CAN_TIME_SEGMENT1_13 = (uint8_t)0x0C, /*!< 13 time quanta */
  101. CAN_TIME_SEGMENT1_14 = (uint8_t)0x0D, /*!< 14 time quanta */
  102. CAN_TIME_SEGMENT1_15 = (uint8_t)0x0E, /*!< 15 time quanta */
  103. CAN_TIME_SEGMENT1_16 = (uint8_t)0x0F, /*!< 16 time quanta */
  104. } CAN_TIME_SEGMENT1_T;
  105. /**
  106. * @brief CAN_time_quantum_in_bit_segment_2
  107. */
  108. typedef enum
  109. {
  110. CAN_TIME_SEGMENT2_1 = (uint8_t)0x00, /*!< 1 time quanta */
  111. CAN_TIME_SEGMENT2_2 = (uint8_t)0x01, /*!< 2 time quanta */
  112. CAN_TIME_SEGMENT2_3 = (uint8_t)0x02, /*!< 3 time quanta */
  113. CAN_TIME_SEGMENT2_4 = (uint8_t)0x03, /*!< 4 time quanta */
  114. CAN_TIME_SEGMENT2_5 = (uint8_t)0x04, /*!< 5 time quanta */
  115. CAN_TIME_SEGMENT2_6 = (uint8_t)0x05, /*!< 6 time quanta */
  116. CAN_TIME_SEGMENT2_7 = (uint8_t)0x06, /*!< 7 time quanta */
  117. CAN_TIME_SEGMENT2_8 = (uint8_t)0x07, /*!< 8 time quanta */
  118. } CAN_TIME_SEGMENT2_T;
  119. /**
  120. * @brief CAN_filter_scale
  121. */
  122. typedef enum
  123. {
  124. CAN_FILTER_SCALE_16BIT = ((uint8_t)0x00), /*!< Two 16-bit filters */
  125. CAN_FILTER_SCALE_32BIT = ((uint8_t)0x01), /*!< One 32-bit filter */
  126. } CAN_FILTER_SCALE_T;
  127. /**
  128. * @brief CAN identifier type
  129. */
  130. typedef enum
  131. {
  132. CAN_TYPEID_STD = ((uint32_t)0x00000000), /*!< Standard Id */
  133. CAN_TYPEID_EXT = ((uint32_t)0x00000004), /*!< Extended Id */
  134. } CAN_TYPEID_T;
  135. /**
  136. * @brief CAN_remote_transmission_request
  137. */
  138. typedef enum
  139. {
  140. CAN_RTXR_DATA = ((uint32_t)0x00000000), /*!< Data frame */
  141. CAN_RTXR_REMOTE = ((uint32_t)0x00000002), /*!< Remote frame */
  142. } CAN_RTXR_T;
  143. /**
  144. * @brief CAN_transmit_constants
  145. */
  146. typedef enum
  147. {
  148. CAN_TX_FAILED = ((uint8_t)0x00), /*!< CAN transmission failed */
  149. CAN_TX_OK = ((uint8_t)0x01), /*!< CAN transmission succeeded */
  150. CAN_TX_WAITING = ((uint8_t)0x02), /*!< CAN waiting for transmission */
  151. CAN_TX_MAILBOX_FULL = ((uint8_t)0x04), /*!< CAN cell did not provide */
  152. } CAN_TX_T;
  153. /**
  154. * @brief CAN sleep constants
  155. */
  156. typedef enum
  157. {
  158. CAN_SLEEP_FAILED = ((uint8_t)0x00), /*!< CAN did not enter the sleep mode */
  159. CAN_SLEEP_OK = ((uint8_t)0x01), /*!< CAN entered the sleep mode */
  160. } CAN_SLEEP_T;
  161. /**
  162. * @brief CAN wake up constants
  163. */
  164. typedef enum
  165. {
  166. CAN_WAKEUP_FAILED = ((uint8_t)0x00), /*!< CAN did not leave the sleep mode */
  167. CAN_WAKEUP_OK = ((uint8_t)0x01), /*!< CAN leaved the sleep mode */
  168. } CAN_WUP_T;
  169. /**
  170. * @brief CAN receive FIFO
  171. */
  172. typedef enum
  173. {
  174. CAN_FIFO_0 = ((uint8_t)0x00), /*!< CAN FIFO 0 used to receive */
  175. CAN_FIFO_1 = ((uint8_t)0x01), /*!< CAN FIFO 1 used to receive */
  176. } CAN_FIFO_T;
  177. /**
  178. * @brief CAN_error_Code_constants
  179. */
  180. typedef enum
  181. {
  182. CAN_ERROR_CODE_NOERR = ((uint8_t)0x00), /*!< No Error */
  183. CAN_ERROR_CODE_STUFFERR = ((uint8_t)0x10), /*!< Stuff Error */
  184. CAN_ERROR_CODE_FORMERR = ((uint8_t)0x20), /*!< Form Error */
  185. CAN_ERROR_CODE_ACKERR = ((uint8_t)0x30), /*!< Acknowledgment Error */
  186. CAN_ERROR_CODE_BITRECESSIVEERR = ((uint8_t)0x40), /*!< Bit Recessive Error */
  187. CAN_ERROR_CODE_BITDOMINANTERR = ((uint8_t)0x50), /*!< Bit Dominant Error */
  188. CAN_ERROR_CODE_CRCERR = ((uint8_t)0x60), /*!< CRC Error */
  189. CAN_ERROR_CODE_SOFTWARESETERR = ((uint8_t)0x70), /*!< Software Set Error */
  190. } CAN_ERROR_CODE_T;
  191. /**
  192. * @brief Flags
  193. */
  194. typedef enum
  195. {
  196. /* Error Flags */
  197. CAN_FLAG_EWF = ((uint32_t)0x10F00001), /*!< Error Warning Flag */
  198. CAN_FLAG_EPF = ((uint32_t)0x10F00002), /*!< Error Passive Flag */
  199. CAN_FLAG_BOF = ((uint32_t)0x10F00004), /*!< Bus-Off Flag */
  200. CAN_FLAG_LEC = ((uint32_t)0x30F00070), /*!< Last error code Flag */
  201. /* Operating Mode Flags */
  202. CAN_FLAG_WUP = ((uint32_t)0x31000008), /*!< Wake up Flag */
  203. CAN_FLAG_SLAK = ((uint32_t)0x31000012), /*!< Sleep acknowledge Flag */
  204. /* Transmit Flags */
  205. CAN_FLAG_RQCP0 = ((uint32_t)0x32000001), /*!< Request MailBox0 Flag */
  206. CAN_FLAG_RQCP1 = ((uint32_t)0x32000100), /*!< Request MailBox1 Flag */
  207. CAN_FLAG_RQCP2 = ((uint32_t)0x32010000), /*!< Request MailBox2 Flag */
  208. /* Receive Flags */
  209. CAN_FLAG_FMP0 = ((uint32_t)0x14000003), /*!< FIFO 0 Message Pending Flag */
  210. CAN_FLAG_FF0 = ((uint32_t)0x34000008), /*!< FIFO 0 Full Flag */
  211. CAN_FLAG_FOV0 = ((uint32_t)0x34000010), /*!< FIFO 0 Overrun Flag */
  212. CAN_FLAG_FMP1 = ((uint32_t)0x18000003), /*!< FIFO 1 Message Pending Flag */
  213. CAN_FLAG_FF1 = ((uint32_t)0x38000008), /*!< FIFO 1 Full Flag */
  214. CAN_FLAG_FOV1 = ((uint32_t)0x38000010), /*!< FIFO 1 Overrun Flag */
  215. } CAN_FLAG_T;
  216. /**
  217. * @brief CAN interrupts
  218. */
  219. typedef enum
  220. {
  221. CAN_INT_TXME = BIT0, /*!< Transmit mailbox empty Interrupt */
  222. CAN_INT_F0MP = BIT1, /*!< FIFO 0 message pending Interrupt */
  223. CAN_INT_F0FUL = BIT2, /*!< FIFO 0 full Interrupt */
  224. CAN_INT_F0OVR = BIT3, /*!< FIFO 0 overrun Interrupt */
  225. CAN_INT_F1MP = BIT4, /*!< FIFO 1 message pending Interrupt */
  226. CAN_INT_F1FUL = BIT5, /*!< FIFO 1 full Interrupt */
  227. CAN_INT_F1OVR = BIT6, /*!< FIFO 1 overrun Interrupt */
  228. CAN_INT_EWIE = BIT8, /*!< Error warning Interrupt */
  229. CAN_INT_EPIE = BIT9, /*!< Error passive Interrupt */
  230. CAN_INT_BOIE = BIT10, /*!< Bus-off Interrupt */
  231. CAN_INT_LEC = BIT11, /*!< Last error code Interrupt */
  232. CAN_INT_ERR = BIT15, /*!< Error Interrupt */
  233. CAN_INT_WUP = BIT16, /*!< Wake-up Interrupt */
  234. CAN_INT_SLE = BIT17, /*!< Sleep acknowledge Interrupt */
  235. } CAN_INT_T;
  236. typedef enum
  237. {
  238. CAN_FILTER_NUMBER_0 = 0, /*!< Number 0 of filters */
  239. CAN_FILTER_NUMBER_1, /*!< Number 1 of filters */
  240. CAN_FILTER_NUMBER_2, /*!< Number 2 of filters */
  241. CAN_FILTER_NUMBER_3, /*!< Number 3 of filters */
  242. CAN_FILTER_NUMBER_4, /*!< Number 4 of filters */
  243. CAN_FILTER_NUMBER_5, /*!< Number 5 of filters */
  244. CAN_FILTER_NUMBER_6, /*!< Number 6 of filters */
  245. CAN_FILTER_NUMBER_7, /*!< Number 7 of filters */
  246. CAN_FILTER_NUMBER_8, /*!< Number 8 of filters */
  247. CAN_FILTER_NUMBER_9, /*!< Number 9 of filters */
  248. CAN_FILTER_NUMBER_10, /*!< Number 10 of filters */
  249. CAN_FILTER_NUMBER_11, /*!< Number 11 of filters */
  250. CAN_FILTER_NUMBER_12, /*!< Number 12 of filters */
  251. CAN_FILTER_NUMBER_13, /*!< Number 13 of filters */
  252. } CAN_FILTER_NUMBER_T;
  253. typedef enum
  254. {
  255. CAN_MAILBOX_0 = ((uint8_t)0x00), /*!< Tx mailbox0 */
  256. CAN_MAILBOX_1 = ((uint8_t)0x01), /*!< Tx mailbox1 */
  257. CAN_MAILBOX_2 = ((uint8_t)0x02), /*!< Tx mailbox2 */
  258. } CAN_MAILBOX_T;
  259. /**@} end of group CAN_Enumerations */
  260. /** @defgroup CAN_Structures Structures
  261. @{
  262. */
  263. /**
  264. * @brief CAN config structure definition
  265. */
  266. typedef struct
  267. {
  268. uint8_t timeTrigComMode; /*!< Enable or disable the time triggered communication mode */
  269. uint8_t autoBusOffManage; /*!< Enable or disable the automatic bus-off management */
  270. uint8_t autoWakeUpMode; /*!< Enable or disable the automatic wake-up mode */
  271. uint8_t nonAutoRetran; /*!< Enable or disable the non-automatic retransmission mode */
  272. uint8_t rxFIFOLockMode; /*!< Enable or disable the Receive FIFO Locked mode */
  273. uint32_t txFIFOPriority; /*!< Enable or disable the transmit FIFO priority */
  274. CAN_MODE_T mode; /*!< Specifies the CAN operating mode */
  275. CAN_SJW_T syncJumpWidth; /*!< Specifies the maximum number of time quanta the CAN hardware
  276. is allowed to lengthen or shorten a bit to perform resynchronization. */
  277. CAN_TIME_SEGMENT1_T timeSegment1; /*!< Specifies the number of time quanta in Bit Segment 1 */
  278. CAN_TIME_SEGMENT2_T timeSegment2; /*!< Specifies the number of time quanta in Bit Segment 2 */
  279. uint16_t prescaler; /*!< Specifies the length of a time quantum. It can be 1 to 1024 */
  280. } CAN_Config_T;
  281. /**
  282. * @brief CAN filter config structure definition
  283. */
  284. typedef struct
  285. {
  286. uint16_t filterIdHigh; /*!< Specifies the filter identification number. */
  287. uint16_t filterIdLow; /*!< Specifies the filter identification number. */
  288. uint16_t filterMaskIdHigh; /*!< Specifies the filter mask number or identification number. */
  289. uint16_t filterMaskIdLow; /*!< Specifies the filter mask number or identification number. */
  290. CAN_FIFO_T filterFIFO; /*!< Specifies the FIFO which will be assigned to the filter. */
  291. CAN_FILTER_NUMBER_T filterNumber; /*!< Specifies the filter which will be configured. It ranges from 0 to 13. */
  292. CAN_FILTER_MODE_T filterMode; /*!< Specifies the filter mode to be configured. */
  293. CAN_FILTER_SCALE_T filterScale; /*!< Specifies the filter scale. */
  294. uint8_t filterActivation; /*!< Enable or disable the filter. */
  295. } CAN_FilterConfig_T;
  296. /**
  297. * @brief CAN Tx message structure definition
  298. */
  299. typedef struct
  300. {
  301. uint32_t stanID; /*!< Specifies the standard identifier. */
  302. uint32_t extenID; /*!< Specifies the extended identifier. */
  303. CAN_TYPEID_T typeID; /*!< Specifies the type of identifier for the message. */
  304. CAN_RTXR_T remoteTxReq; /*!< Specifies the type of frame for the message. */
  305. uint8_t dataLengthCode; /*!< Specifies the length of the frame. It can be a value between 0 to 8. */
  306. uint8_t data[8]; /*!< Contains the data to be transmitted. It ranges from 0 to 0xFF. */
  307. } CAN_Tx_Message;
  308. /**
  309. * @brief CAN Rx message structure definition
  310. */
  311. typedef struct
  312. {
  313. uint32_t stanID; /*!< Specifies the standard identifier. */
  314. uint32_t extenID; /*!< Specifies the extended identifier. */
  315. CAN_TYPEID_T typeID; /*!< Specifies the type of identifier for the message. */
  316. CAN_RTXR_T remoteTxReq; /*!< Specifies the type of frame for the message. */
  317. uint8_t dataLengthCode; /*!< Specifies the length of the frame. It can be a value between 0 to 8. */
  318. uint8_t data[8]; /*!< Contains the data to be transmitted. It ranges from 0 to 0xFF. */
  319. uint8_t filterMatchIndex; /*!< Specifies the index of the filter the message stored in the mailbox passes through. */
  320. } CAN_Rx_Message;
  321. /**@} end of group CAN_Structures */
  322. /** @defgroup CAN_Variables Variables
  323. @{
  324. */
  325. /**@} end of group CAN_Variables */
  326. /** @defgroup CAN_Functions Functions
  327. @{
  328. */
  329. /* CAN reset and configuration */
  330. void CAN_Reset(void);
  331. uint8_t CAN_Config(CAN_Config_T* canConfig);
  332. void CAN_ConfigFilter(CAN_FilterConfig_T* filterConfig);
  333. void CAN_ConfigStructInit(CAN_Config_T* canConfig);
  334. void CAN_StartBankSlave(uint8_t bankNumber);
  335. void CAN_EnableDebugFreeze(void);
  336. void CAN_DisableDebugFreeze(void);
  337. void CAN_EnableTTComMode(void);
  338. void CAN_DisableTTComMode(void);
  339. /* CAN frames transmission */
  340. uint8_t CAN_TxMessage(CAN_Tx_Message* TxMessage);
  341. uint8_t CAN_TxMessageStatus(CAN_MAILBOX_T TxMailbox);
  342. void CAN_CancelTx(CAN_MAILBOX_T TxMailbox);
  343. /* CAN frames reception */
  344. void CAN_RxMessage(uint8_t FIFONumber, CAN_Rx_Message* RxMessage);
  345. void CAN_ReleaseFIFO(uint8_t FIFONumber);
  346. uint8_t CAN_PendingMessage(uint8_t FIFONumber);
  347. /* CAN operation modes */
  348. uint8_t CAN_OperatingMode(CAN_OPERATING_MODE_T operatingMode);
  349. uint8_t CAN_SleepMode(void);
  350. uint8_t CAN_WakeUpMode(void);
  351. /* CAN bus error management */
  352. uint8_t CAN_ReadLastErrorCode(void);
  353. uint8_t CAN_ReadRxErrorCounter(void);
  354. uint8_t CAN_ReadLSBTxErrorCounter(void);
  355. /* CAN interrupt and flag */
  356. void CAN_EnableInterrupt(uint32_t interrupt);
  357. void CAN_DisableInterrupt(uint32_t interrupt);
  358. uint8_t CAN_ReadStatusFlag(CAN_FLAG_T CAN_FLAG);
  359. void CAN_ClearStatusFlag(CAN_FLAG_T flag);
  360. uint8_t CAN_ReadIntFlag(CAN_INT_T interrupt);
  361. void CAN_ClearIntFlag(uint32_t interrupt);
  362. #ifdef __cplusplus
  363. }
  364. #endif
  365. #endif /* __APM32F0XX_CAN_H */
  366. /**@} end of group CAN_Functions */
  367. /**@} end of group CAN_Driver */
  368. /**@} end of group APM32F0xx_StdPeriphDriver */