apm32f4xx_can.h 13 KB

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