apm32f10x_can.h 13 KB

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