apm32f10x_can.h 12 KB

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