You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

769 lines
34 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_can.h
  4. * @author MCD Application Team
  5. * @version V1.7.1
  6. * @date 21-April-2017
  7. * @brief Header file of CAN HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32L4xx_CAN_H
  39. #define __STM32L4xx_CAN_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32l4xx_hal_def.h"
  45. /** @addtogroup STM32L4xx_HAL_Driver
  46. * @{
  47. */
  48. /** @addtogroup CAN
  49. * @{
  50. */
  51. /* Exported types ------------------------------------------------------------*/
  52. /** @defgroup CAN_Exported_Types CAN Exported Types
  53. * @{
  54. */
  55. /**
  56. * @brief HAL State structures definition
  57. */
  58. typedef enum
  59. {
  60. HAL_CAN_STATE_RESET = 0x00, /*!< CAN not yet initialized or disabled */
  61. HAL_CAN_STATE_READY = 0x01, /*!< CAN initialized and ready for use */
  62. HAL_CAN_STATE_BUSY = 0x02, /*!< CAN process is ongoing */
  63. HAL_CAN_STATE_BUSY_TX = 0x12, /*!< CAN process is ongoing */
  64. HAL_CAN_STATE_BUSY_RX = 0x22, /*!< CAN process is ongoing */
  65. HAL_CAN_STATE_BUSY_TX_RX = 0x32, /*!< CAN process is ongoing */
  66. HAL_CAN_STATE_TIMEOUT = 0x03, /*!< Timeout state */
  67. HAL_CAN_STATE_ERROR = 0x04 /*!< CAN error state */
  68. }HAL_CAN_StateTypeDef;
  69. /**
  70. * @brief CAN init structure definition
  71. */
  72. typedef struct
  73. {
  74. uint32_t Prescaler; /*!< Specifies the length of a time quantum.
  75. This parameter must be a number between Min_Data = 1 and Max_Data = 1024 */
  76. uint32_t Mode; /*!< Specifies the CAN operating mode.
  77. This parameter can be a value of @ref CAN_operating_mode */
  78. uint32_t SJW; /*!< Specifies the maximum number of time quanta
  79. the CAN hardware is allowed to lengthen or
  80. shorten a bit to perform resynchronization.
  81. This parameter can be a value of @ref CAN_synchronisation_jump_width */
  82. uint32_t BS1; /*!< Specifies the number of time quanta in Bit Segment 1.
  83. This parameter can be a value of @ref CAN_time_quantum_in_bit_segment_1 */
  84. uint32_t BS2; /*!< Specifies the number of time quanta in Bit Segment 2.
  85. This parameter can be a value of @ref CAN_time_quantum_in_bit_segment_2 */
  86. uint32_t TTCM; /*!< Enable or disable the time triggered communication mode.
  87. This parameter can be set to ENABLE or DISABLE. */
  88. uint32_t ABOM; /*!< Enable or disable the automatic bus-off management.
  89. This parameter can be set to ENABLE or DISABLE */
  90. uint32_t AWUM; /*!< Enable or disable the automatic wake-up mode.
  91. This parameter can be set to ENABLE or DISABLE */
  92. uint32_t NART; /*!< Enable or disable the non-automatic retransmission mode.
  93. This parameter can be set to ENABLE or DISABLE */
  94. uint32_t RFLM; /*!< Enable or disable the receive FIFO Locked mode.
  95. This parameter can be set to ENABLE or DISABLE */
  96. uint32_t TXFP; /*!< Enable or disable the transmit FIFO priority.
  97. This parameter can be set to ENABLE or DISABLE */
  98. }CAN_InitTypeDef;
  99. /**
  100. * @brief CAN filter configuration structure definition
  101. */
  102. typedef struct
  103. {
  104. uint32_t FilterIdHigh; /*!< Specifies the filter identification number (MSBs for a 32-bit
  105. configuration, first one for a 16-bit configuration).
  106. This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */
  107. uint32_t FilterIdLow; /*!< Specifies the filter identification number (LSBs for a 32-bit
  108. configuration, second one for a 16-bit configuration).
  109. This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */
  110. uint32_t FilterMaskIdHigh; /*!< Specifies the filter mask number or identification number,
  111. according to the mode (MSBs for a 32-bit configuration,
  112. first one for a 16-bit configuration).
  113. This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */
  114. uint32_t FilterMaskIdLow; /*!< Specifies the filter mask number or identification number,
  115. according to the mode (LSBs for a 32-bit configuration,
  116. second one for a 16-bit configuration).
  117. This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */
  118. uint32_t FilterFIFOAssignment; /*!< Specifies the FIFO (0 or 1) which will be assigned to the filter.
  119. This parameter can be a value of @ref CAN_filter_FIFO */
  120. uint32_t FilterNumber; /*!< Specifies the filter which will be initialized.
  121. This parameter must be a number between Min_Data = 0 and Max_Data = 27 */
  122. uint32_t FilterMode; /*!< Specifies the filter mode to be initialized.
  123. This parameter can be a value of @ref CAN_filter_mode */
  124. uint32_t FilterScale; /*!< Specifies the filter scale.
  125. This parameter can be a value of @ref CAN_filter_scale */
  126. uint32_t FilterActivation; /*!< Enable or disable the filter.
  127. This parameter can be set to ENABLE or DISABLE */
  128. uint32_t BankNumber; /*!< Select the start slave bank filter.
  129. This parameter must be a number between Min_Data = 0 and Max_Data = 28 */
  130. }CAN_FilterConfTypeDef;
  131. /**
  132. * @brief CAN Tx message structure definition
  133. */
  134. typedef struct
  135. {
  136. uint32_t StdId; /*!< Specifies the standard identifier.
  137. This parameter must be a number between Min_Data = 0 and Max_Data = 0x7FF */
  138. uint32_t ExtId; /*!< Specifies the extended identifier.
  139. This parameter must be a number between Min_Data = 0 and Max_Data = 0x1FFFFFFF */
  140. uint32_t IDE; /*!< Specifies the type of identifier for the message that will be transmitted.
  141. This parameter can be a value of @ref CAN_identifier_type */
  142. uint32_t RTR; /*!< Specifies the type of frame for the message that will be transmitted.
  143. This parameter can be a value of @ref CAN_remote_transmission_request */
  144. uint32_t DLC; /*!< Specifies the length of the frame that will be transmitted.
  145. This parameter must be a number between Min_Data = 0 and Max_Data = 8 */
  146. uint8_t Data[8]; /*!< Contains the data to be transmitted.
  147. This parameter must be a number between Min_Data = 0 and Max_Data = 0xFF */
  148. }CanTxMsgTypeDef;
  149. /**
  150. * @brief CAN Rx message structure definition
  151. */
  152. typedef struct
  153. {
  154. uint32_t StdId; /*!< Specifies the standard identifier.
  155. This parameter must be a number between Min_Data = 0 and Max_Data = 0x7FF */
  156. uint32_t ExtId; /*!< Specifies the extended identifier.
  157. This parameter must be a number between Min_Data = 0 and Max_Data = 0x1FFFFFFF */
  158. uint32_t IDE; /*!< Specifies the type of identifier for the message that will be received.
  159. This parameter can be a value of @ref CAN_identifier_type */
  160. uint32_t RTR; /*!< Specifies the type of frame for the received message.
  161. This parameter can be a value of @ref CAN_remote_transmission_request */
  162. uint32_t DLC; /*!< Specifies the length of the frame that will be received.
  163. This parameter must be a number between Min_Data = 0 and Max_Data = 8 */
  164. uint8_t Data[8]; /*!< Contains the data to be received.
  165. This parameter must be a number between Min_Data = 0 and Max_Data = 0xFF */
  166. uint32_t FMI; /*!< Specifies the index of the filter the message stored in the mailbox passes through.
  167. This parameter must be a number between Min_Data = 0 and Max_Data = 0xFF */
  168. uint32_t FIFONumber; /*!< Specifies the receive FIFO number.
  169. This parameter can be CAN_FIFO0 or CAN_FIFO1 */
  170. }CanRxMsgTypeDef;
  171. /**
  172. * @brief CAN handle Structure definition
  173. */
  174. typedef struct
  175. {
  176. CAN_TypeDef *Instance; /*!< Register base address */
  177. CAN_InitTypeDef Init; /*!< CAN required parameters */
  178. CanTxMsgTypeDef* pTxMsg; /*!< Pointer to transmit structure */
  179. CanRxMsgTypeDef* pRxMsg; /*!< Pointer to reception structure */
  180. __IO HAL_CAN_StateTypeDef State; /*!< CAN communication state */
  181. HAL_LockTypeDef Lock; /*!< CAN locking object */
  182. __IO uint32_t ErrorCode; /*!< CAN Error code */
  183. }CAN_HandleTypeDef;
  184. /**
  185. * @}
  186. */
  187. /* Exported constants --------------------------------------------------------*/
  188. /** @defgroup CAN_Exported_Constants CAN Exported Constants
  189. * @{
  190. */
  191. /** @defgroup CAN_Error_Code CAN Error Code
  192. * @{
  193. */
  194. #define HAL_CAN_ERROR_NONE ((uint32_t)0x00000000) /*!< No error */
  195. #define HAL_CAN_ERROR_EWG ((uint32_t)0x00000001) /*!< EWG error */
  196. #define HAL_CAN_ERROR_EPV ((uint32_t)0x00000002) /*!< EPV error */
  197. #define HAL_CAN_ERROR_BOF ((uint32_t)0x00000004) /*!< BOF error */
  198. #define HAL_CAN_ERROR_STF ((uint32_t)0x00000008) /*!< Stuff error */
  199. #define HAL_CAN_ERROR_FOR ((uint32_t)0x00000010) /*!< Form error */
  200. #define HAL_CAN_ERROR_ACK ((uint32_t)0x00000020) /*!< Acknowledgment error */
  201. #define HAL_CAN_ERROR_BR ((uint32_t)0x00000040) /*!< Bit recessive */
  202. #define HAL_CAN_ERROR_BD ((uint32_t)0x00000080) /*!< LEC dominant */
  203. #define HAL_CAN_ERROR_CRC ((uint32_t)0x00000100) /*!< LEC transfer error */
  204. /**
  205. * @}
  206. */
  207. /** @defgroup CAN_InitStatus CAN initialization Status
  208. * @{
  209. */
  210. #define CAN_INITSTATUS_FAILED ((uint32_t)0x00000000) /*!< CAN initialization failed */
  211. #define CAN_INITSTATUS_SUCCESS ((uint32_t)0x00000001) /*!< CAN initialization OK */
  212. /**
  213. * @}
  214. */
  215. /** @defgroup CAN_operating_mode CAN Operating Mode
  216. * @{
  217. */
  218. #define CAN_MODE_NORMAL ((uint32_t)0x00000000) /*!< Normal mode */
  219. #define CAN_MODE_LOOPBACK ((uint32_t)CAN_BTR_LBKM) /*!< Loopback mode */
  220. #define CAN_MODE_SILENT ((uint32_t)CAN_BTR_SILM) /*!< Silent mode */
  221. #define CAN_MODE_SILENT_LOOPBACK ((uint32_t)(CAN_BTR_LBKM | CAN_BTR_SILM)) /*!< Loopback combined with silent mode */
  222. /**
  223. * @}
  224. */
  225. /** @defgroup CAN_synchronisation_jump_width CAN Synchronization Jump Width
  226. * @{
  227. */
  228. #define CAN_SJW_1TQ ((uint32_t)0x00000000) /*!< 1 time quantum */
  229. #define CAN_SJW_2TQ ((uint32_t)CAN_BTR_SJW_0) /*!< 2 time quantum */
  230. #define CAN_SJW_3TQ ((uint32_t)CAN_BTR_SJW_1) /*!< 3 time quantum */
  231. #define CAN_SJW_4TQ ((uint32_t)CAN_BTR_SJW) /*!< 4 time quantum */
  232. /**
  233. * @}
  234. */
  235. /** @defgroup CAN_time_quantum_in_bit_segment_1 CAN Time Quantum in Bit Segment 1
  236. * @{
  237. */
  238. #define CAN_BS1_1TQ ((uint32_t)0x00000000) /*!< 1 time quantum */
  239. #define CAN_BS1_2TQ ((uint32_t)CAN_BTR_TS1_0) /*!< 2 time quantum */
  240. #define CAN_BS1_3TQ ((uint32_t)CAN_BTR_TS1_1) /*!< 3 time quantum */
  241. #define CAN_BS1_4TQ ((uint32_t)(CAN_BTR_TS1_1 | CAN_BTR_TS1_0)) /*!< 4 time quantum */
  242. #define CAN_BS1_5TQ ((uint32_t)CAN_BTR_TS1_2) /*!< 5 time quantum */
  243. #define CAN_BS1_6TQ ((uint32_t)(CAN_BTR_TS1_2 | CAN_BTR_TS1_0)) /*!< 6 time quantum */
  244. #define CAN_BS1_7TQ ((uint32_t)(CAN_BTR_TS1_2 | CAN_BTR_TS1_1)) /*!< 7 time quantum */
  245. #define CAN_BS1_8TQ ((uint32_t)(CAN_BTR_TS1_2 | CAN_BTR_TS1_1 | CAN_BTR_TS1_0)) /*!< 8 time quantum */
  246. #define CAN_BS1_9TQ ((uint32_t)CAN_BTR_TS1_3) /*!< 9 time quantum */
  247. #define CAN_BS1_10TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_0)) /*!< 10 time quantum */
  248. #define CAN_BS1_11TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_1)) /*!< 11 time quantum */
  249. #define CAN_BS1_12TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_1 | CAN_BTR_TS1_0)) /*!< 12 time quantum */
  250. #define CAN_BS1_13TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_2)) /*!< 13 time quantum */
  251. #define CAN_BS1_14TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_2 | CAN_BTR_TS1_0)) /*!< 14 time quantum */
  252. #define CAN_BS1_15TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_2 | CAN_BTR_TS1_1)) /*!< 15 time quantum */
  253. #define CAN_BS1_16TQ ((uint32_t)CAN_BTR_TS1) /*!< 16 time quantum */
  254. /**
  255. * @}
  256. */
  257. /** @defgroup CAN_time_quantum_in_bit_segment_2 CAN Time Quantum in Bit Segment 2
  258. * @{
  259. */
  260. #define CAN_BS2_1TQ ((uint32_t)0x00000000) /*!< 1 time quantum */
  261. #define CAN_BS2_2TQ ((uint32_t)CAN_BTR_TS2_0) /*!< 2 time quantum */
  262. #define CAN_BS2_3TQ ((uint32_t)CAN_BTR_TS2_1) /*!< 3 time quantum */
  263. #define CAN_BS2_4TQ ((uint32_t)(CAN_BTR_TS2_1 | CAN_BTR_TS2_0)) /*!< 4 time quantum */
  264. #define CAN_BS2_5TQ ((uint32_t)CAN_BTR_TS2_2) /*!< 5 time quantum */
  265. #define CAN_BS2_6TQ ((uint32_t)(CAN_BTR_TS2_2 | CAN_BTR_TS2_0)) /*!< 6 time quantum */
  266. #define CAN_BS2_7TQ ((uint32_t)(CAN_BTR_TS2_2 | CAN_BTR_TS2_1)) /*!< 7 time quantum */
  267. #define CAN_BS2_8TQ ((uint32_t)CAN_BTR_TS2) /*!< 8 time quantum */
  268. /**
  269. * @}
  270. */
  271. /** @defgroup CAN_filter_mode CAN Filter Mode
  272. * @{
  273. */
  274. #define CAN_FILTERMODE_IDMASK ((uint8_t)0x00) /*!< Identifier mask mode */
  275. #define CAN_FILTERMODE_IDLIST ((uint8_t)0x01) /*!< Identifier list mode */
  276. /**
  277. * @}
  278. */
  279. /** @defgroup CAN_filter_scale CAN Filter Scale
  280. * @{
  281. */
  282. #define CAN_FILTERSCALE_16BIT ((uint8_t)0x00) /*!< Two 16-bit filters */
  283. #define CAN_FILTERSCALE_32BIT ((uint8_t)0x01) /*!< One 32-bit filter */
  284. /**
  285. * @}
  286. */
  287. /** @defgroup CAN_filter_FIFO CAN Filter FIFO
  288. * @{
  289. */
  290. #define CAN_FILTER_FIFO0 ((uint8_t)0x00) /*!< Filter FIFO 0 assignment for filter x */
  291. #define CAN_FILTER_FIFO1 ((uint8_t)0x01) /*!< Filter FIFO 1 assignment for filter x */
  292. /**
  293. * @}
  294. */
  295. /** @defgroup CAN_identifier_type CAN Identifier Type
  296. * @{
  297. */
  298. #define CAN_ID_STD ((uint32_t)0x00000000) /*!< Standard Id */
  299. #define CAN_ID_EXT ((uint32_t)0x00000004) /*!< Extended Id */
  300. /**
  301. * @}
  302. */
  303. /** @defgroup CAN_remote_transmission_request CAN Remote Transmission Request
  304. * @{
  305. */
  306. #define CAN_RTR_DATA ((uint32_t)0x00000000) /*!< Data frame */
  307. #define CAN_RTR_REMOTE ((uint32_t)0x00000002) /*!< Remote frame */
  308. /**
  309. * @}
  310. */
  311. /** @defgroup CAN_receive_FIFO_number_constants CAN Receive FIFO Number
  312. * @{
  313. */
  314. #define CAN_FIFO0 ((uint8_t)0x00) /*!< CAN FIFO 0 used to receive */
  315. #define CAN_FIFO1 ((uint8_t)0x01) /*!< CAN FIFO 1 used to receive */
  316. /**
  317. * @}
  318. */
  319. /** @defgroup CAN_flags CAN Flags
  320. * @{
  321. */
  322. /* If the flag is 0x3XXXXXXX, it means that it can be used with CAN_GetFlagStatus()
  323. and CAN_ClearFlag() functions. */
  324. /* If the flag is 0x1XXXXXXX, it means that it can only be used with
  325. CAN_GetFlagStatus() function. */
  326. /* Transmit Flags */
  327. #define CAN_FLAG_RQCP0 ((uint32_t)0x00000500) /*!< Request MailBox0 flag */
  328. #define CAN_FLAG_RQCP1 ((uint32_t)0x00000508) /*!< Request MailBox1 flag */
  329. #define CAN_FLAG_RQCP2 ((uint32_t)0x00000510) /*!< Request MailBox2 flag */
  330. #define CAN_FLAG_TXOK0 ((uint32_t)0x00000501) /*!< Transmission OK MailBox0 flag */
  331. #define CAN_FLAG_TXOK1 ((uint32_t)0x00000509) /*!< Transmission OK MailBox1 flag */
  332. #define CAN_FLAG_TXOK2 ((uint32_t)0x00000511) /*!< Transmission OK MailBox2 flag */
  333. #define CAN_FLAG_TME0 ((uint32_t)0x0000051A) /*!< Transmit mailbox 0 empty flag */
  334. #define CAN_FLAG_TME1 ((uint32_t)0x0000051B) /*!< Transmit mailbox 0 empty flag */
  335. #define CAN_FLAG_TME2 ((uint32_t)0x0000051C) /*!< Transmit mailbox 0 empty flag */
  336. /* Receive Flags */
  337. #define CAN_FLAG_FF0 ((uint32_t)0x00000203) /*!< FIFO 0 Full flag */
  338. #define CAN_FLAG_FOV0 ((uint32_t)0x00000204) /*!< FIFO 0 Overrun flag */
  339. #define CAN_FLAG_FF1 ((uint32_t)0x00000403) /*!< FIFO 1 Full flag */
  340. #define CAN_FLAG_FOV1 ((uint32_t)0x00000404) /*!< FIFO 1 Overrun flag */
  341. /* Operating Mode Flags */
  342. #define CAN_FLAG_WKU ((uint32_t)0x00000103) /*!< Wake up flag */
  343. #define CAN_FLAG_SLAK ((uint32_t)0x00000101) /*!< Sleep acknowledge flag */
  344. #define CAN_FLAG_SLAKI ((uint32_t)0x00000104) /*!< Sleep acknowledge flag */
  345. /* @note When SLAK interrupt is disabled (SLKIE=0), no polling on SLAKI is possible.
  346. In this case the SLAK bit can be polled.*/
  347. /* Error Flags */
  348. #define CAN_FLAG_EWG ((uint32_t)0x00000300) /*!< Error warning flag */
  349. #define CAN_FLAG_EPV ((uint32_t)0x00000301) /*!< Error passive flag */
  350. #define CAN_FLAG_BOF ((uint32_t)0x00000302) /*!< Bus-Off flag */
  351. /**
  352. * @}
  353. */
  354. /** @defgroup CAN_interrupts CAN Interrupts
  355. * @{
  356. */
  357. #define CAN_IT_TME ((uint32_t)CAN_IER_TMEIE) /*!< Transmit mailbox empty interrupt */
  358. /* Receive Interrupts */
  359. #define CAN_IT_FMP0 ((uint32_t)CAN_IER_FMPIE0) /*!< FIFO 0 message pending interrupt */
  360. #define CAN_IT_FF0 ((uint32_t)CAN_IER_FFIE0) /*!< FIFO 0 full interrupt */
  361. #define CAN_IT_FOV0 ((uint32_t)CAN_IER_FOVIE0) /*!< FIFO 0 overrun interrupt */
  362. #define CAN_IT_FMP1 ((uint32_t)CAN_IER_FMPIE1) /*!< FIFO 1 message pending interrupt */
  363. #define CAN_IT_FF1 ((uint32_t)CAN_IER_FFIE1) /*!< FIFO 1 full interrupt */
  364. #define CAN_IT_FOV1 ((uint32_t)CAN_IER_FOVIE1) /*!< FIFO 1 overrun interrupt */
  365. /* Operating Mode Interrupts */
  366. #define CAN_IT_WKU ((uint32_t)CAN_IER_WKUIE) /*!< Wake-up interrupt */
  367. #define CAN_IT_SLK ((uint32_t)CAN_IER_SLKIE) /*!< Sleep acknowledge interrupt */
  368. /* Error Interrupts */
  369. #define CAN_IT_EWG ((uint32_t)CAN_IER_EWGIE) /*!< Error warning interrupt */
  370. #define CAN_IT_EPV ((uint32_t)CAN_IER_EPVIE) /*!< Error passive interrupt */
  371. #define CAN_IT_BOF ((uint32_t)CAN_IER_BOFIE) /*!< Bus-off interrupt */
  372. #define CAN_IT_LEC ((uint32_t)CAN_IER_LECIE) /*!< Last error code interrupt */
  373. #define CAN_IT_ERR ((uint32_t)CAN_IER_ERRIE) /*!< Error Interrupt */
  374. /**
  375. * @}
  376. */
  377. /* Mailboxes definition */
  378. #define CAN_TXMAILBOX_0 ((uint8_t)0x00)
  379. #define CAN_TXMAILBOX_1 ((uint8_t)0x01)
  380. #define CAN_TXMAILBOX_2 ((uint8_t)0x02)
  381. /**
  382. * @}
  383. */
  384. /* Exported macros -----------------------------------------------------------*/
  385. /** @defgroup CAN_Exported_Macro CAN Exported Macros
  386. * @{
  387. */
  388. /** @brief Reset CAN handle state.
  389. * @param __HANDLE__: CAN handle.
  390. * @retval None
  391. */
  392. #define __HAL_CAN_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_CAN_STATE_RESET)
  393. /**
  394. * @brief Enable the specified CAN interrupt.
  395. * @param __HANDLE__: CAN handle.
  396. * @param __INTERRUPT__: CAN Interrupt.
  397. * @retval None
  398. */
  399. #define __HAL_CAN_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IER) |= (__INTERRUPT__))
  400. /**
  401. * @brief Disable the specified CAN interrupt.
  402. * @param __HANDLE__: CAN handle.
  403. * @param __INTERRUPT__: CAN Interrupt.
  404. * @retval None
  405. */
  406. #define __HAL_CAN_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IER) &= ~(__INTERRUPT__))
  407. /**
  408. * @brief Return the number of pending received messages.
  409. * @param __HANDLE__: CAN handle.
  410. * @param __FIFONUMBER__: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  411. * @retval The number of pending message.
  412. */
  413. #define __HAL_CAN_MSG_PENDING(__HANDLE__, __FIFONUMBER__) (((__FIFONUMBER__) == CAN_FIFO0)? \
  414. ((uint8_t)((__HANDLE__)->Instance->RF0R&(uint32_t)0x03)) : ((uint8_t)((__HANDLE__)->Instance->RF1R&(uint32_t)0x03)))
  415. /** @brief Check whether the specified CAN flag is set or not.
  416. * @param __HANDLE__: specifies the CAN Handle.
  417. * @param __FLAG__: specifies the flag to check.
  418. * This parameter can be one of the following values:
  419. * @arg CAN_TSR_RQCP0: Request MailBox0 Flag
  420. * @arg CAN_TSR_RQCP1: Request MailBox1 Flag
  421. * @arg CAN_TSR_RQCP2: Request MailBox2 Flag
  422. * @arg CAN_FLAG_TXOK0: Transmission OK MailBox0 Flag
  423. * @arg CAN_FLAG_TXOK1: Transmission OK MailBox1 Flag
  424. * @arg CAN_FLAG_TXOK2: Transmission OK MailBox2 Flag
  425. * @arg CAN_FLAG_TME0: Transmit mailbox 0 empty Flag
  426. * @arg CAN_FLAG_TME1: Transmit mailbox 1 empty Flag
  427. * @arg CAN_FLAG_TME2: Transmit mailbox 2 empty Flag
  428. * @arg CAN_FLAG_FMP0: FIFO 0 Message Pending Flag
  429. * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
  430. * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
  431. * @arg CAN_FLAG_FMP1: FIFO 1 Message Pending Flag
  432. * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
  433. * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
  434. * @arg CAN_FLAG_WKU: Wake up Flag
  435. * @arg CAN_FLAG_SLAK: Sleep acknowledge Flag
  436. * @arg CAN_FLAG_SLAKI: Sleep acknowledge Flag
  437. * @arg CAN_FLAG_EWG: Error Warning Flag
  438. * @arg CAN_FLAG_EPV: Error Passive Flag
  439. * @arg CAN_FLAG_BOF: Bus-Off Flag
  440. * @retval The new state of __FLAG__ (TRUE or FALSE).
  441. */
  442. #define __HAL_CAN_GET_FLAG(__HANDLE__, __FLAG__) \
  443. ((((__FLAG__) >> 8) == 5)? ((((__HANDLE__)->Instance->TSR) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))): \
  444. (((__FLAG__) >> 8) == 2)? ((((__HANDLE__)->Instance->RF0R) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))): \
  445. (((__FLAG__) >> 8) == 4)? ((((__HANDLE__)->Instance->RF1R) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))): \
  446. (((__FLAG__) >> 8) == 1)? ((((__HANDLE__)->Instance->MSR) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))): \
  447. ((((__HANDLE__)->Instance->ESR) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))))
  448. /** @brief Clear the specified CAN pending flag.
  449. * @param __HANDLE__: specifies the CAN Handle.
  450. * @param __FLAG__: specifies the flag to check.
  451. * This parameter can be one of the following values:
  452. * @arg CAN_TSR_RQCP0: Request MailBox0 Flag
  453. * @arg CAN_TSR_RQCP1: Request MailBox1 Flag
  454. * @arg CAN_TSR_RQCP2: Request MailBox2 Flag
  455. * @arg CAN_FLAG_TXOK0: Transmission OK MailBox0 Flag
  456. * @arg CAN_FLAG_TXOK1: Transmission OK MailBox1 Flag
  457. * @arg CAN_FLAG_TXOK2: Transmission OK MailBox2 Flag
  458. * @arg CAN_FLAG_TME0: Transmit mailbox 0 empty Flag
  459. * @arg CAN_FLAG_TME1: Transmit mailbox 1 empty Flag
  460. * @arg CAN_FLAG_TME2: Transmit mailbox 2 empty Flag
  461. * @arg CAN_FLAG_FMP0: FIFO 0 Message Pending Flag
  462. * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
  463. * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
  464. * @arg CAN_FLAG_FMP1: FIFO 1 Message Pending Flag
  465. * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
  466. * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
  467. * @arg CAN_FLAG_WKU: Wake up Flag
  468. * @arg CAN_FLAG_SLAKI: Sleep acknowledge Flag
  469. * @retval The new state of __FLAG__ (TRUE or FALSE).
  470. */
  471. #define __HAL_CAN_CLEAR_FLAG(__HANDLE__, __FLAG__) \
  472. ((((__FLAG__) >> 8U) == 5)? (((__HANDLE__)->Instance->TSR) = (1U << ((__FLAG__) & CAN_FLAG_MASK))): \
  473. (((__FLAG__) >> 8U) == 2)? (((__HANDLE__)->Instance->RF0R) = (1U << ((__FLAG__) & CAN_FLAG_MASK))): \
  474. (((__FLAG__) >> 8U) == 4)? (((__HANDLE__)->Instance->RF1R) = (1U << ((__FLAG__) & CAN_FLAG_MASK))): \
  475. (((__FLAG__) >> 8U) == 1)? (((__HANDLE__)->Instance->MSR) = (1U << ((__FLAG__) & CAN_FLAG_MASK))): 0)
  476. /** @brief Check whether the specified CAN interrupt source is enabled or not.
  477. * @param __HANDLE__: specifies the CAN Handle.
  478. * @param __INTERRUPT__: specifies the CAN interrupt source to check.
  479. * This parameter can be one of the following values:
  480. * @arg CAN_IT_TME: Transmit mailbox empty interrupt enable
  481. * @arg CAN_IT_FMP0: FIFO0 message pending interrupt enable
  482. * @arg CAN_IT_FMP1: FIFO1 message pending interrupt enable
  483. * @retval The new state of __IT__ (TRUE or FALSE).
  484. */
  485. #define __HAL_CAN_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->IER & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  486. /**
  487. * @brief Check the transmission status of a CAN Frame.
  488. * @param __HANDLE__: specifies the CAN Handle.
  489. * @param __TRANSMITMAILBOX__: the number of the mailbox that is used for transmission.
  490. * @retval The new status of transmission (TRUE or FALSE).
  491. */
  492. #define __HAL_CAN_TRANSMIT_STATUS(__HANDLE__, __TRANSMITMAILBOX__)\
  493. (((__TRANSMITMAILBOX__) == CAN_TXMAILBOX_0)? ((((__HANDLE__)->Instance->TSR) & (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0)) == (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0)) :\
  494. ((__TRANSMITMAILBOX__) == CAN_TXMAILBOX_1)? ((((__HANDLE__)->Instance->TSR) & (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1)) == (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1)) :\
  495. ((((__HANDLE__)->Instance->TSR) & (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2)) == (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2)))
  496. /**
  497. * @brief Release the specified receive FIFO.
  498. * @param __HANDLE__: CAN handle.
  499. * @param __FIFONUMBER__: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  500. * @retval None
  501. */
  502. #define __HAL_CAN_FIFO_RELEASE(__HANDLE__, __FIFONUMBER__) (((__FIFONUMBER__) == CAN_FIFO0)? \
  503. ((__HANDLE__)->Instance->RF0R |= CAN_RF0R_RFOM0) : ((__HANDLE__)->Instance->RF1R |= CAN_RF1R_RFOM1))
  504. /**
  505. * @brief Cancel a transmit request.
  506. * @param __HANDLE__: specifies the CAN Handle.
  507. * @param __TRANSMITMAILBOX__: the number of the mailbox that is used for transmission.
  508. * @retval None
  509. */
  510. #define __HAL_CAN_CANCEL_TRANSMIT(__HANDLE__, __TRANSMITMAILBOX__)\
  511. (((__TRANSMITMAILBOX__) == CAN_TXMAILBOX_0)? ((__HANDLE__)->Instance->TSR |= CAN_TSR_ABRQ0) :\
  512. ((__TRANSMITMAILBOX__) == CAN_TXMAILBOX_1)? ((__HANDLE__)->Instance->TSR |= CAN_TSR_ABRQ1) :\
  513. ((__HANDLE__)->Instance->TSR |= CAN_TSR_ABRQ2))
  514. /**
  515. * @brief Enable or disable the DBG Freeze for CAN.
  516. * @param __HANDLE__: specifies the CAN Handle.
  517. * @param __NEWSTATE__: new state of the CAN peripheral.
  518. * This parameter can be: ENABLE (CAN reception/transmission is frozen
  519. * during debug. Reception FIFO can still be accessed/controlled normally)
  520. * or DISABLE (CAN is working during debug).
  521. * @retval None
  522. */
  523. #define __HAL_CAN_DBG_FREEZE(__HANDLE__, __NEWSTATE__) (((__NEWSTATE__) == ENABLE)? \
  524. ((__HANDLE__)->Instance->MCR |= CAN_MCR_DBF) : ((__HANDLE__)->Instance->MCR &= ~CAN_MCR_DBF))
  525. /**
  526. * @}
  527. */
  528. /* Exported functions --------------------------------------------------------*/
  529. /** @addtogroup CAN_Exported_Functions CAN Exported Functions
  530. * @{
  531. */
  532. /** @defgroup CAN_Exported_Functions_Group1 Initialization and de-initialization functions
  533. * @brief Initialization and Configuration functions
  534. * @{
  535. */
  536. /* addtogroup and de-initialization functions *****************************/
  537. HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef* hcan);
  538. HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef* hcan, CAN_FilterConfTypeDef* sFilterConfig);
  539. HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef* hcan);
  540. void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan);
  541. void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan);
  542. /**
  543. * @}
  544. */
  545. /** @addtogroup CAN_Exported_Functions_Group2 Input and Output operation functions
  546. * @brief I/O operation functions
  547. * @{
  548. */
  549. /* IO operation functions *****************************************************/
  550. HAL_StatusTypeDef HAL_CAN_Transmit(CAN_HandleTypeDef *hcan, uint32_t Timeout);
  551. HAL_StatusTypeDef HAL_CAN_Transmit_IT(CAN_HandleTypeDef *hcan);
  552. HAL_StatusTypeDef HAL_CAN_Receive(CAN_HandleTypeDef *hcan, uint8_t FIFONumber, uint32_t Timeout);
  553. HAL_StatusTypeDef HAL_CAN_Receive_IT(CAN_HandleTypeDef *hcan, uint8_t FIFONumber);
  554. HAL_StatusTypeDef HAL_CAN_Sleep(CAN_HandleTypeDef *hcan);
  555. HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan);
  556. void HAL_CAN_IRQHandler(CAN_HandleTypeDef* hcan);
  557. void HAL_CAN_TxCpltCallback(CAN_HandleTypeDef* hcan);
  558. void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan);
  559. void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan);
  560. /**
  561. * @}
  562. */
  563. /** @addtogroup CAN_Exported_Functions_Group3 Peripheral State and Error functions
  564. * @brief CAN Peripheral State functions
  565. * @{
  566. */
  567. /* Peripheral State and Error functions ***************************************/
  568. uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan);
  569. HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef* hcan);
  570. /**
  571. * @}
  572. */
  573. /**
  574. * @}
  575. */
  576. /* Private types -------------------------------------------------------------*/
  577. /* Private constants ---------------------------------------------------------*/
  578. /** @defgroup CAN_Private_Constants CAN Private Constants
  579. * @{
  580. */
  581. /** @defgroup CAN_transmit_constants CAN Transmit Constants
  582. * @{
  583. */
  584. #define CAN_TXSTATUS_NOMAILBOX ((uint8_t)0x04) /*!< CAN cell did not provide CAN_TxStatus_NoMailBox */
  585. /**
  586. * @}
  587. */
  588. #define CAN_FLAG_MASK ((uint32_t)0x000000FF)
  589. /**
  590. * @}
  591. */
  592. /* Private macros ------------------------------------------------------------*/
  593. /** @defgroup CAN_Private_Macros CAN Private Macros
  594. * @{
  595. */
  596. #define IS_CAN_MODE(MODE) (((MODE) == CAN_MODE_NORMAL) || \
  597. ((MODE) == CAN_MODE_LOOPBACK)|| \
  598. ((MODE) == CAN_MODE_SILENT) || \
  599. ((MODE) == CAN_MODE_SILENT_LOOPBACK))
  600. #define IS_CAN_SJW(SJW) (((SJW) == CAN_SJW_1TQ) || ((SJW) == CAN_SJW_2TQ)|| \
  601. ((SJW) == CAN_SJW_3TQ) || ((SJW) == CAN_SJW_4TQ))
  602. #define IS_CAN_BS1(BS1) ((BS1) <= CAN_BS1_16TQ)
  603. #define IS_CAN_BS2(BS2) ((BS2) <= CAN_BS2_8TQ)
  604. #define IS_CAN_PRESCALER(PRESCALER) (((PRESCALER) >= 1) && ((PRESCALER) <= 1024))
  605. #define IS_CAN_FILTER_NUMBER(NUMBER) ((NUMBER) <= 27)
  606. #define IS_CAN_FILTER_MODE(MODE) (((MODE) == CAN_FILTERMODE_IDMASK) || \
  607. ((MODE) == CAN_FILTERMODE_IDLIST))
  608. #define IS_CAN_FILTER_SCALE(SCALE) (((SCALE) == CAN_FILTERSCALE_16BIT) || \
  609. ((SCALE) == CAN_FILTERSCALE_32BIT))
  610. #define IS_CAN_FILTER_FIFO(FIFO) (((FIFO) == CAN_FILTER_FIFO0) || \
  611. ((FIFO) == CAN_FILTER_FIFO1))
  612. #define IS_CAN_BANKNUMBER(BANKNUMBER) ((BANKNUMBER) <= 28)
  613. #define IS_CAN_TRANSMITMAILBOX(TRANSMITMAILBOX) ((TRANSMITMAILBOX) <= ((uint8_t)0x02))
  614. #define IS_CAN_STDID(STDID) ((STDID) <= ((uint32_t)0x7FF))
  615. #define IS_CAN_EXTID(EXTID) ((EXTID) <= ((uint32_t)0x1FFFFFFF))
  616. #define IS_CAN_DLC(DLC) ((DLC) <= ((uint8_t)0x08))
  617. #define IS_CAN_IDTYPE(IDTYPE) (((IDTYPE) == CAN_ID_STD) || \
  618. ((IDTYPE) == CAN_ID_EXT))
  619. #define IS_CAN_RTR(RTR) (((RTR) == CAN_RTR_DATA) || ((RTR) == CAN_RTR_REMOTE))
  620. #define IS_CAN_FIFO(FIFO) (((FIFO) == CAN_FIFO0) || ((FIFO) == CAN_FIFO1))
  621. /**
  622. * @}
  623. */
  624. /* Private functions ---------------------------------------------------------*/
  625. /**
  626. * @}
  627. */
  628. /**
  629. * @}
  630. */
  631. #ifdef __cplusplus
  632. }
  633. #endif
  634. #endif /* __STM32L4xx_CAN_H */
  635. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/