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.
 
 
 
 

1702 lines
57 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_can.c
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 11-January-2013
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Controller area network (CAN) peripheral:
  9. * + Initialization and Configuration
  10. * + CAN Frames Transmission
  11. * + CAN Frames Reception
  12. * + Operation modes switch
  13. * + Error management
  14. * + Interrupts and flags
  15. *
  16. @verbatim
  17. ===============================================================================
  18. ##### How to use this driver #####
  19. ===============================================================================
  20. [..]
  21. (#) Enable the CAN controller interface clock using
  22. RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); for CAN1
  23. and RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE); for CAN2
  24. -@- In case you are using CAN2 only, you have to enable the CAN1 clock.
  25. (#) CAN pins configuration
  26. (++) Enable the clock for the CAN GPIOs using the following function:
  27. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
  28. (++) Connect the involved CAN pins to AF9 using the following function
  29. GPIO_PinAFConfig(GPIOx, GPIO_PinSourcex, GPIO_AF_CANx);
  30. (++) Configure these CAN pins in alternate function mode by calling
  31. the function GPIO_Init();
  32. (#) Initialise and configure the CAN using CAN_Init() and
  33. CAN_FilterInit() functions.
  34. (#) Transmit the desired CAN frame using CAN_Transmit() function.
  35. (#) Check the transmission of a CAN frame using CAN_TransmitStatus()
  36. function.
  37. (#) Cancel the transmission of a CAN frame using CAN_CancelTransmit()
  38. function.
  39. (#) Receive a CAN frame using CAN_Recieve() function.
  40. (#) Release the receive FIFOs using CAN_FIFORelease() function.
  41. (#) Return the number of pending received frames using
  42. CAN_MessagePending() function.
  43. (#) To control CAN events you can use one of the following two methods:
  44. (++) Check on CAN flags using the CAN_GetFlagStatus() function.
  45. (++) Use CAN interrupts through the function CAN_ITConfig() at
  46. initialization phase and CAN_GetITStatus() function into
  47. interrupt routines to check if the event has occurred or not.
  48. After checking on a flag you should clear it using CAN_ClearFlag()
  49. function. And after checking on an interrupt event you should
  50. clear it using CAN_ClearITPendingBit() function.
  51. @endverbatim
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  56. *
  57. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  58. * You may not use this file except in compliance with the License.
  59. * You may obtain a copy of the License at:
  60. *
  61. * http://www.st.com/software_license_agreement_liberty_v2
  62. *
  63. * Unless required by applicable law or agreed to in writing, software
  64. * distributed under the License is distributed on an "AS IS" BASIS,
  65. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  66. * See the License for the specific language governing permissions and
  67. * limitations under the License.
  68. *
  69. ******************************************************************************
  70. */
  71. /* Includes ------------------------------------------------------------------*/
  72. #include "stm32f4xx_can.h"
  73. #include "stm32f4xx_rcc.h"
  74. /** @addtogroup STM32F4xx_StdPeriph_Driver
  75. * @{
  76. */
  77. /** @defgroup CAN
  78. * @brief CAN driver modules
  79. * @{
  80. */
  81. /* Private typedef -----------------------------------------------------------*/
  82. /* Private define ------------------------------------------------------------*/
  83. /* CAN Master Control Register bits */
  84. #define MCR_DBF ((uint32_t)0x00010000) /* software master reset */
  85. /* CAN Mailbox Transmit Request */
  86. #define TMIDxR_TXRQ ((uint32_t)0x00000001) /* Transmit mailbox request */
  87. /* CAN Filter Master Register bits */
  88. #define FMR_FINIT ((uint32_t)0x00000001) /* Filter init mode */
  89. /* Time out for INAK bit */
  90. #define INAK_TIMEOUT ((uint32_t)0x0000FFFF)
  91. /* Time out for SLAK bit */
  92. #define SLAK_TIMEOUT ((uint32_t)0x0000FFFF)
  93. /* Flags in TSR register */
  94. #define CAN_FLAGS_TSR ((uint32_t)0x08000000)
  95. /* Flags in RF1R register */
  96. #define CAN_FLAGS_RF1R ((uint32_t)0x04000000)
  97. /* Flags in RF0R register */
  98. #define CAN_FLAGS_RF0R ((uint32_t)0x02000000)
  99. /* Flags in MSR register */
  100. #define CAN_FLAGS_MSR ((uint32_t)0x01000000)
  101. /* Flags in ESR register */
  102. #define CAN_FLAGS_ESR ((uint32_t)0x00F00000)
  103. /* Mailboxes definition */
  104. #define CAN_TXMAILBOX_0 ((uint8_t)0x00)
  105. #define CAN_TXMAILBOX_1 ((uint8_t)0x01)
  106. #define CAN_TXMAILBOX_2 ((uint8_t)0x02)
  107. #define CAN_MODE_MASK ((uint32_t) 0x00000003)
  108. /* Private macro -------------------------------------------------------------*/
  109. /* Private variables ---------------------------------------------------------*/
  110. /* Private function prototypes -----------------------------------------------*/
  111. /* Private functions ---------------------------------------------------------*/
  112. static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit);
  113. /** @defgroup CAN_Private_Functions
  114. * @{
  115. */
  116. /** @defgroup CAN_Group1 Initialization and Configuration functions
  117. * @brief Initialization and Configuration functions
  118. *
  119. @verbatim
  120. ===============================================================================
  121. ##### Initialization and Configuration functions #####
  122. ===============================================================================
  123. [..] This section provides functions allowing to
  124. (+) Initialize the CAN peripherals : Prescaler, operating mode, the maximum
  125. number of time quanta to perform resynchronization, the number of time
  126. quanta in Bit Segment 1 and 2 and many other modes.
  127. Refer to @ref CAN_InitTypeDef for more details.
  128. (+) Configures the CAN reception filter.
  129. (+) Select the start bank filter for slave CAN.
  130. (+) Enables or disables the Debug Freeze mode for CAN
  131. (+)Enables or disables the CAN Time Trigger Operation communication mode
  132. @endverbatim
  133. * @{
  134. */
  135. /**
  136. * @brief Deinitializes the CAN peripheral registers to their default reset values.
  137. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  138. * @retval None.
  139. */
  140. void CAN_DeInit(CAN_TypeDef* CANx)
  141. {
  142. /* Check the parameters */
  143. assert_param(IS_CAN_ALL_PERIPH(CANx));
  144. if (CANx == CAN1)
  145. {
  146. /* Enable CAN1 reset state */
  147. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, ENABLE);
  148. /* Release CAN1 from reset state */
  149. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, DISABLE);
  150. }
  151. else
  152. {
  153. /* Enable CAN2 reset state */
  154. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, ENABLE);
  155. /* Release CAN2 from reset state */
  156. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, DISABLE);
  157. }
  158. }
  159. /**
  160. * @brief Initializes the CAN peripheral according to the specified
  161. * parameters in the CAN_InitStruct.
  162. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  163. * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure that contains
  164. * the configuration information for the CAN peripheral.
  165. * @retval Constant indicates initialization succeed which will be
  166. * CAN_InitStatus_Failed or CAN_InitStatus_Success.
  167. */
  168. uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
  169. {
  170. uint8_t InitStatus = CAN_InitStatus_Failed;
  171. uint32_t wait_ack = 0x00000000;
  172. /* Check the parameters */
  173. assert_param(IS_CAN_ALL_PERIPH(CANx));
  174. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
  175. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
  176. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
  177. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
  178. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
  179. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
  180. assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
  181. assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
  182. assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
  183. assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
  184. assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
  185. /* Exit from sleep mode */
  186. CANx->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
  187. /* Request initialisation */
  188. CANx->MCR |= CAN_MCR_INRQ ;
  189. /* Wait the acknowledge */
  190. while (((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
  191. {
  192. wait_ack++;
  193. }
  194. /* Check acknowledge */
  195. if ((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
  196. {
  197. InitStatus = CAN_InitStatus_Failed;
  198. }
  199. else
  200. {
  201. /* Set the time triggered communication mode */
  202. if (CAN_InitStruct->CAN_TTCM == ENABLE)
  203. {
  204. CANx->MCR |= CAN_MCR_TTCM;
  205. }
  206. else
  207. {
  208. CANx->MCR &= ~(uint32_t)CAN_MCR_TTCM;
  209. }
  210. /* Set the automatic bus-off management */
  211. if (CAN_InitStruct->CAN_ABOM == ENABLE)
  212. {
  213. CANx->MCR |= CAN_MCR_ABOM;
  214. }
  215. else
  216. {
  217. CANx->MCR &= ~(uint32_t)CAN_MCR_ABOM;
  218. }
  219. /* Set the automatic wake-up mode */
  220. if (CAN_InitStruct->CAN_AWUM == ENABLE)
  221. {
  222. CANx->MCR |= CAN_MCR_AWUM;
  223. }
  224. else
  225. {
  226. CANx->MCR &= ~(uint32_t)CAN_MCR_AWUM;
  227. }
  228. /* Set the no automatic retransmission */
  229. if (CAN_InitStruct->CAN_NART == ENABLE)
  230. {
  231. CANx->MCR |= CAN_MCR_NART;
  232. }
  233. else
  234. {
  235. CANx->MCR &= ~(uint32_t)CAN_MCR_NART;
  236. }
  237. /* Set the receive FIFO locked mode */
  238. if (CAN_InitStruct->CAN_RFLM == ENABLE)
  239. {
  240. CANx->MCR |= CAN_MCR_RFLM;
  241. }
  242. else
  243. {
  244. CANx->MCR &= ~(uint32_t)CAN_MCR_RFLM;
  245. }
  246. /* Set the transmit FIFO priority */
  247. if (CAN_InitStruct->CAN_TXFP == ENABLE)
  248. {
  249. CANx->MCR |= CAN_MCR_TXFP;
  250. }
  251. else
  252. {
  253. CANx->MCR &= ~(uint32_t)CAN_MCR_TXFP;
  254. }
  255. /* Set the bit timing register */
  256. CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | \
  257. ((uint32_t)CAN_InitStruct->CAN_SJW << 24) | \
  258. ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | \
  259. ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) | \
  260. ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
  261. /* Request leave initialisation */
  262. CANx->MCR &= ~(uint32_t)CAN_MCR_INRQ;
  263. /* Wait the acknowledge */
  264. wait_ack = 0;
  265. while (((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
  266. {
  267. wait_ack++;
  268. }
  269. /* ...and check acknowledged */
  270. if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
  271. {
  272. InitStatus = CAN_InitStatus_Failed;
  273. }
  274. else
  275. {
  276. InitStatus = CAN_InitStatus_Success ;
  277. }
  278. }
  279. /* At this step, return the status of initialization */
  280. return InitStatus;
  281. }
  282. /**
  283. * @brief Configures the CAN reception filter according to the specified
  284. * parameters in the CAN_FilterInitStruct.
  285. * @param CAN_FilterInitStruct: pointer to a CAN_FilterInitTypeDef structure that
  286. * contains the configuration information.
  287. * @retval None
  288. */
  289. void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
  290. {
  291. uint32_t filter_number_bit_pos = 0;
  292. /* Check the parameters */
  293. assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
  294. assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
  295. assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
  296. assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
  297. assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));
  298. filter_number_bit_pos = ((uint32_t)1) << CAN_FilterInitStruct->CAN_FilterNumber;
  299. /* Initialisation mode for the filter */
  300. CAN1->FMR |= FMR_FINIT;
  301. /* Filter Deactivation */
  302. CAN1->FA1R &= ~(uint32_t)filter_number_bit_pos;
  303. /* Filter Scale */
  304. if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
  305. {
  306. /* 16-bit scale for the filter */
  307. CAN1->FS1R &= ~(uint32_t)filter_number_bit_pos;
  308. /* First 16-bit identifier and First 16-bit mask */
  309. /* Or First 16-bit identifier and Second 16-bit identifier */
  310. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
  311. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
  312. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
  313. /* Second 16-bit identifier and Second 16-bit mask */
  314. /* Or Third 16-bit identifier and Fourth 16-bit identifier */
  315. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
  316. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
  317. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh);
  318. }
  319. if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
  320. {
  321. /* 32-bit scale for the filter */
  322. CAN1->FS1R |= filter_number_bit_pos;
  323. /* 32-bit identifier or First 32-bit identifier */
  324. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
  325. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
  326. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
  327. /* 32-bit mask or Second 32-bit identifier */
  328. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
  329. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
  330. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow);
  331. }
  332. /* Filter Mode */
  333. if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
  334. {
  335. /*Id/Mask mode for the filter*/
  336. CAN1->FM1R &= ~(uint32_t)filter_number_bit_pos;
  337. }
  338. else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
  339. {
  340. /*Identifier list mode for the filter*/
  341. CAN1->FM1R |= (uint32_t)filter_number_bit_pos;
  342. }
  343. /* Filter FIFO assignment */
  344. if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO0)
  345. {
  346. /* FIFO 0 assignation for the filter */
  347. CAN1->FFA1R &= ~(uint32_t)filter_number_bit_pos;
  348. }
  349. if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO1)
  350. {
  351. /* FIFO 1 assignation for the filter */
  352. CAN1->FFA1R |= (uint32_t)filter_number_bit_pos;
  353. }
  354. /* Filter activation */
  355. if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
  356. {
  357. CAN1->FA1R |= filter_number_bit_pos;
  358. }
  359. /* Leave the initialisation mode for the filter */
  360. CAN1->FMR &= ~FMR_FINIT;
  361. }
  362. /**
  363. * @brief Fills each CAN_InitStruct member with its default value.
  364. * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure which ill be initialized.
  365. * @retval None
  366. */
  367. void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
  368. {
  369. /* Reset CAN init structure parameters values */
  370. /* Initialize the time triggered communication mode */
  371. CAN_InitStruct->CAN_TTCM = DISABLE;
  372. /* Initialize the automatic bus-off management */
  373. CAN_InitStruct->CAN_ABOM = DISABLE;
  374. /* Initialize the automatic wake-up mode */
  375. CAN_InitStruct->CAN_AWUM = DISABLE;
  376. /* Initialize the no automatic retransmission */
  377. CAN_InitStruct->CAN_NART = DISABLE;
  378. /* Initialize the receive FIFO locked mode */
  379. CAN_InitStruct->CAN_RFLM = DISABLE;
  380. /* Initialize the transmit FIFO priority */
  381. CAN_InitStruct->CAN_TXFP = DISABLE;
  382. /* Initialize the CAN_Mode member */
  383. CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
  384. /* Initialize the CAN_SJW member */
  385. CAN_InitStruct->CAN_SJW = CAN_SJW_1tq;
  386. /* Initialize the CAN_BS1 member */
  387. CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
  388. /* Initialize the CAN_BS2 member */
  389. CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
  390. /* Initialize the CAN_Prescaler member */
  391. CAN_InitStruct->CAN_Prescaler = 1;
  392. }
  393. /**
  394. * @brief Select the start bank filter for slave CAN.
  395. * @param CAN_BankNumber: Select the start slave bank filter from 1..27.
  396. * @retval None
  397. */
  398. void CAN_SlaveStartBank(uint8_t CAN_BankNumber)
  399. {
  400. /* Check the parameters */
  401. assert_param(IS_CAN_BANKNUMBER(CAN_BankNumber));
  402. /* Enter Initialisation mode for the filter */
  403. CAN1->FMR |= FMR_FINIT;
  404. /* Select the start slave bank */
  405. CAN1->FMR &= (uint32_t)0xFFFFC0F1 ;
  406. CAN1->FMR |= (uint32_t)(CAN_BankNumber)<<8;
  407. /* Leave Initialisation mode for the filter */
  408. CAN1->FMR &= ~FMR_FINIT;
  409. }
  410. /**
  411. * @brief Enables or disables the DBG Freeze for CAN.
  412. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  413. * @param NewState: new state of the CAN peripheral.
  414. * This parameter can be: ENABLE (CAN reception/transmission is frozen
  415. * during debug. Reception FIFOs can still be accessed/controlled normally)
  416. * or DISABLE (CAN is working during debug).
  417. * @retval None
  418. */
  419. void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState)
  420. {
  421. /* Check the parameters */
  422. assert_param(IS_CAN_ALL_PERIPH(CANx));
  423. assert_param(IS_FUNCTIONAL_STATE(NewState));
  424. if (NewState != DISABLE)
  425. {
  426. /* Enable Debug Freeze */
  427. CANx->MCR |= MCR_DBF;
  428. }
  429. else
  430. {
  431. /* Disable Debug Freeze */
  432. CANx->MCR &= ~MCR_DBF;
  433. }
  434. }
  435. /**
  436. * @brief Enables or disables the CAN Time TriggerOperation communication mode.
  437. * @note DLC must be programmed as 8 in order Time Stamp (2 bytes) to be
  438. * sent over the CAN bus.
  439. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  440. * @param NewState: Mode new state. This parameter can be: ENABLE or DISABLE.
  441. * When enabled, Time stamp (TIME[15:0]) value is sent in the last two
  442. * data bytes of the 8-byte message: TIME[7:0] in data byte 6 and TIME[15:8]
  443. * in data byte 7.
  444. * @retval None
  445. */
  446. void CAN_TTComModeCmd(CAN_TypeDef* CANx, FunctionalState NewState)
  447. {
  448. /* Check the parameters */
  449. assert_param(IS_CAN_ALL_PERIPH(CANx));
  450. assert_param(IS_FUNCTIONAL_STATE(NewState));
  451. if (NewState != DISABLE)
  452. {
  453. /* Enable the TTCM mode */
  454. CANx->MCR |= CAN_MCR_TTCM;
  455. /* Set TGT bits */
  456. CANx->sTxMailBox[0].TDTR |= ((uint32_t)CAN_TDT0R_TGT);
  457. CANx->sTxMailBox[1].TDTR |= ((uint32_t)CAN_TDT1R_TGT);
  458. CANx->sTxMailBox[2].TDTR |= ((uint32_t)CAN_TDT2R_TGT);
  459. }
  460. else
  461. {
  462. /* Disable the TTCM mode */
  463. CANx->MCR &= (uint32_t)(~(uint32_t)CAN_MCR_TTCM);
  464. /* Reset TGT bits */
  465. CANx->sTxMailBox[0].TDTR &= ((uint32_t)~CAN_TDT0R_TGT);
  466. CANx->sTxMailBox[1].TDTR &= ((uint32_t)~CAN_TDT1R_TGT);
  467. CANx->sTxMailBox[2].TDTR &= ((uint32_t)~CAN_TDT2R_TGT);
  468. }
  469. }
  470. /**
  471. * @}
  472. */
  473. /** @defgroup CAN_Group2 CAN Frames Transmission functions
  474. * @brief CAN Frames Transmission functions
  475. *
  476. @verbatim
  477. ===============================================================================
  478. ##### CAN Frames Transmission functions #####
  479. ===============================================================================
  480. [..] This section provides functions allowing to
  481. (+) Initiate and transmit a CAN frame message (if there is an empty mailbox).
  482. (+) Check the transmission status of a CAN Frame
  483. (+) Cancel a transmit request
  484. @endverbatim
  485. * @{
  486. */
  487. /**
  488. * @brief Initiates and transmits a CAN frame message.
  489. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  490. * @param TxMessage: pointer to a structure which contains CAN Id, CAN DLC and CAN data.
  491. * @retval The number of the mailbox that is used for transmission or
  492. * CAN_TxStatus_NoMailBox if there is no empty mailbox.
  493. */
  494. uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)
  495. {
  496. uint8_t transmit_mailbox = 0;
  497. /* Check the parameters */
  498. assert_param(IS_CAN_ALL_PERIPH(CANx));
  499. assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
  500. assert_param(IS_CAN_RTR(TxMessage->RTR));
  501. assert_param(IS_CAN_DLC(TxMessage->DLC));
  502. /* Select one empty transmit mailbox */
  503. if ((CANx->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
  504. {
  505. transmit_mailbox = 0;
  506. }
  507. else if ((CANx->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
  508. {
  509. transmit_mailbox = 1;
  510. }
  511. else if ((CANx->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
  512. {
  513. transmit_mailbox = 2;
  514. }
  515. else
  516. {
  517. transmit_mailbox = CAN_TxStatus_NoMailBox;
  518. }
  519. if (transmit_mailbox != CAN_TxStatus_NoMailBox)
  520. {
  521. /* Set up the Id */
  522. CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
  523. if (TxMessage->IDE == CAN_Id_Standard)
  524. {
  525. assert_param(IS_CAN_STDID(TxMessage->StdId));
  526. CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->StdId << 21) | \
  527. TxMessage->RTR);
  528. }
  529. else
  530. {
  531. assert_param(IS_CAN_EXTID(TxMessage->ExtId));
  532. CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->ExtId << 3) | \
  533. TxMessage->IDE | \
  534. TxMessage->RTR);
  535. }
  536. /* Set up the DLC */
  537. TxMessage->DLC &= (uint8_t)0x0000000F;
  538. CANx->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0xFFFFFFF0;
  539. CANx->sTxMailBox[transmit_mailbox].TDTR |= TxMessage->DLC;
  540. /* Set up the data field */
  541. CANx->sTxMailBox[transmit_mailbox].TDLR = (((uint32_t)TxMessage->Data[3] << 24) |
  542. ((uint32_t)TxMessage->Data[2] << 16) |
  543. ((uint32_t)TxMessage->Data[1] << 8) |
  544. ((uint32_t)TxMessage->Data[0]));
  545. CANx->sTxMailBox[transmit_mailbox].TDHR = (((uint32_t)TxMessage->Data[7] << 24) |
  546. ((uint32_t)TxMessage->Data[6] << 16) |
  547. ((uint32_t)TxMessage->Data[5] << 8) |
  548. ((uint32_t)TxMessage->Data[4]));
  549. /* Request transmission */
  550. CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
  551. }
  552. return transmit_mailbox;
  553. }
  554. /**
  555. * @brief Checks the transmission status of a CAN Frame.
  556. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  557. * @param TransmitMailbox: the number of the mailbox that is used for transmission.
  558. * @retval CAN_TxStatus_Ok if the CAN driver transmits the message,
  559. * CAN_TxStatus_Failed in an other case.
  560. */
  561. uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox)
  562. {
  563. uint32_t state = 0;
  564. /* Check the parameters */
  565. assert_param(IS_CAN_ALL_PERIPH(CANx));
  566. assert_param(IS_CAN_TRANSMITMAILBOX(TransmitMailbox));
  567. switch (TransmitMailbox)
  568. {
  569. case (CAN_TXMAILBOX_0):
  570. state = CANx->TSR & (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0);
  571. break;
  572. case (CAN_TXMAILBOX_1):
  573. state = CANx->TSR & (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1);
  574. break;
  575. case (CAN_TXMAILBOX_2):
  576. state = CANx->TSR & (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2);
  577. break;
  578. default:
  579. state = CAN_TxStatus_Failed;
  580. break;
  581. }
  582. switch (state)
  583. {
  584. /* transmit pending */
  585. case (0x0): state = CAN_TxStatus_Pending;
  586. break;
  587. /* transmit failed */
  588. case (CAN_TSR_RQCP0 | CAN_TSR_TME0): state = CAN_TxStatus_Failed;
  589. break;
  590. case (CAN_TSR_RQCP1 | CAN_TSR_TME1): state = CAN_TxStatus_Failed;
  591. break;
  592. case (CAN_TSR_RQCP2 | CAN_TSR_TME2): state = CAN_TxStatus_Failed;
  593. break;
  594. /* transmit succeeded */
  595. case (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0):state = CAN_TxStatus_Ok;
  596. break;
  597. case (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1):state = CAN_TxStatus_Ok;
  598. break;
  599. case (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2):state = CAN_TxStatus_Ok;
  600. break;
  601. default: state = CAN_TxStatus_Failed;
  602. break;
  603. }
  604. return (uint8_t) state;
  605. }
  606. /**
  607. * @brief Cancels a transmit request.
  608. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  609. * @param Mailbox: Mailbox number.
  610. * @retval None
  611. */
  612. void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox)
  613. {
  614. /* Check the parameters */
  615. assert_param(IS_CAN_ALL_PERIPH(CANx));
  616. assert_param(IS_CAN_TRANSMITMAILBOX(Mailbox));
  617. /* abort transmission */
  618. switch (Mailbox)
  619. {
  620. case (CAN_TXMAILBOX_0): CANx->TSR |= CAN_TSR_ABRQ0;
  621. break;
  622. case (CAN_TXMAILBOX_1): CANx->TSR |= CAN_TSR_ABRQ1;
  623. break;
  624. case (CAN_TXMAILBOX_2): CANx->TSR |= CAN_TSR_ABRQ2;
  625. break;
  626. default:
  627. break;
  628. }
  629. }
  630. /**
  631. * @}
  632. */
  633. /** @defgroup CAN_Group3 CAN Frames Reception functions
  634. * @brief CAN Frames Reception functions
  635. *
  636. @verbatim
  637. ===============================================================================
  638. ##### CAN Frames Reception functions #####
  639. ===============================================================================
  640. [..] This section provides functions allowing to
  641. (+) Receive a correct CAN frame
  642. (+) Release a specified receive FIFO (2 FIFOs are available)
  643. (+) Return the number of the pending received CAN frames
  644. @endverbatim
  645. * @{
  646. */
  647. /**
  648. * @brief Receives a correct CAN frame.
  649. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  650. * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  651. * @param RxMessage: pointer to a structure receive frame which contains CAN Id,
  652. * CAN DLC, CAN data and FMI number.
  653. * @retval None
  654. */
  655. void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage)
  656. {
  657. /* Check the parameters */
  658. assert_param(IS_CAN_ALL_PERIPH(CANx));
  659. assert_param(IS_CAN_FIFO(FIFONumber));
  660. /* Get the Id */
  661. RxMessage->IDE = (uint8_t)0x04 & CANx->sFIFOMailBox[FIFONumber].RIR;
  662. if (RxMessage->IDE == CAN_Id_Standard)
  663. {
  664. RxMessage->StdId = (uint32_t)0x000007FF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 21);
  665. }
  666. else
  667. {
  668. RxMessage->ExtId = (uint32_t)0x1FFFFFFF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 3);
  669. }
  670. RxMessage->RTR = (uint8_t)0x02 & CANx->sFIFOMailBox[FIFONumber].RIR;
  671. /* Get the DLC */
  672. RxMessage->DLC = (uint8_t)0x0F & CANx->sFIFOMailBox[FIFONumber].RDTR;
  673. /* Get the FMI */
  674. RxMessage->FMI = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDTR >> 8);
  675. /* Get the data field */
  676. RxMessage->Data[0] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDLR;
  677. RxMessage->Data[1] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 8);
  678. RxMessage->Data[2] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 16);
  679. RxMessage->Data[3] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 24);
  680. RxMessage->Data[4] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDHR;
  681. RxMessage->Data[5] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 8);
  682. RxMessage->Data[6] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 16);
  683. RxMessage->Data[7] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 24);
  684. /* Release the FIFO */
  685. /* Release FIFO0 */
  686. if (FIFONumber == CAN_FIFO0)
  687. {
  688. CANx->RF0R |= CAN_RF0R_RFOM0;
  689. }
  690. /* Release FIFO1 */
  691. else /* FIFONumber == CAN_FIFO1 */
  692. {
  693. CANx->RF1R |= CAN_RF1R_RFOM1;
  694. }
  695. }
  696. /**
  697. * @brief Releases the specified receive FIFO.
  698. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  699. * @param FIFONumber: FIFO to release, CAN_FIFO0 or CAN_FIFO1.
  700. * @retval None
  701. */
  702. void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber)
  703. {
  704. /* Check the parameters */
  705. assert_param(IS_CAN_ALL_PERIPH(CANx));
  706. assert_param(IS_CAN_FIFO(FIFONumber));
  707. /* Release FIFO0 */
  708. if (FIFONumber == CAN_FIFO0)
  709. {
  710. CANx->RF0R |= CAN_RF0R_RFOM0;
  711. }
  712. /* Release FIFO1 */
  713. else /* FIFONumber == CAN_FIFO1 */
  714. {
  715. CANx->RF1R |= CAN_RF1R_RFOM1;
  716. }
  717. }
  718. /**
  719. * @brief Returns the number of pending received messages.
  720. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  721. * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  722. * @retval NbMessage : which is the number of pending message.
  723. */
  724. uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber)
  725. {
  726. uint8_t message_pending=0;
  727. /* Check the parameters */
  728. assert_param(IS_CAN_ALL_PERIPH(CANx));
  729. assert_param(IS_CAN_FIFO(FIFONumber));
  730. if (FIFONumber == CAN_FIFO0)
  731. {
  732. message_pending = (uint8_t)(CANx->RF0R&(uint32_t)0x03);
  733. }
  734. else if (FIFONumber == CAN_FIFO1)
  735. {
  736. message_pending = (uint8_t)(CANx->RF1R&(uint32_t)0x03);
  737. }
  738. else
  739. {
  740. message_pending = 0;
  741. }
  742. return message_pending;
  743. }
  744. /**
  745. * @}
  746. */
  747. /** @defgroup CAN_Group4 CAN Operation modes functions
  748. * @brief CAN Operation modes functions
  749. *
  750. @verbatim
  751. ===============================================================================
  752. ##### CAN Operation modes functions #####
  753. ===============================================================================
  754. [..] This section provides functions allowing to select the CAN Operation modes
  755. (+) sleep mode
  756. (+) normal mode
  757. (+) initialization mode
  758. @endverbatim
  759. * @{
  760. */
  761. /**
  762. * @brief Selects the CAN Operation mode.
  763. * @param CAN_OperatingMode: CAN Operating Mode.
  764. * This parameter can be one of @ref CAN_OperatingMode_TypeDef enumeration.
  765. * @retval status of the requested mode which can be
  766. * - CAN_ModeStatus_Failed: CAN failed entering the specific mode
  767. * - CAN_ModeStatus_Success: CAN Succeed entering the specific mode
  768. */
  769. uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode)
  770. {
  771. uint8_t status = CAN_ModeStatus_Failed;
  772. /* Timeout for INAK or also for SLAK bits*/
  773. uint32_t timeout = INAK_TIMEOUT;
  774. /* Check the parameters */
  775. assert_param(IS_CAN_ALL_PERIPH(CANx));
  776. assert_param(IS_CAN_OPERATING_MODE(CAN_OperatingMode));
  777. if (CAN_OperatingMode == CAN_OperatingMode_Initialization)
  778. {
  779. /* Request initialisation */
  780. CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_SLEEP)) | CAN_MCR_INRQ);
  781. /* Wait the acknowledge */
  782. while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK) && (timeout != 0))
  783. {
  784. timeout--;
  785. }
  786. if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK)
  787. {
  788. status = CAN_ModeStatus_Failed;
  789. }
  790. else
  791. {
  792. status = CAN_ModeStatus_Success;
  793. }
  794. }
  795. else if (CAN_OperatingMode == CAN_OperatingMode_Normal)
  796. {
  797. /* Request leave initialisation and sleep mode and enter Normal mode */
  798. CANx->MCR &= (uint32_t)(~(CAN_MCR_SLEEP|CAN_MCR_INRQ));
  799. /* Wait the acknowledge */
  800. while (((CANx->MSR & CAN_MODE_MASK) != 0) && (timeout!=0))
  801. {
  802. timeout--;
  803. }
  804. if ((CANx->MSR & CAN_MODE_MASK) != 0)
  805. {
  806. status = CAN_ModeStatus_Failed;
  807. }
  808. else
  809. {
  810. status = CAN_ModeStatus_Success;
  811. }
  812. }
  813. else if (CAN_OperatingMode == CAN_OperatingMode_Sleep)
  814. {
  815. /* Request Sleep mode */
  816. CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
  817. /* Wait the acknowledge */
  818. while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK) && (timeout!=0))
  819. {
  820. timeout--;
  821. }
  822. if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK)
  823. {
  824. status = CAN_ModeStatus_Failed;
  825. }
  826. else
  827. {
  828. status = CAN_ModeStatus_Success;
  829. }
  830. }
  831. else
  832. {
  833. status = CAN_ModeStatus_Failed;
  834. }
  835. return (uint8_t) status;
  836. }
  837. /**
  838. * @brief Enters the Sleep (low power) mode.
  839. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  840. * @retval CAN_Sleep_Ok if sleep entered, CAN_Sleep_Failed otherwise.
  841. */
  842. uint8_t CAN_Sleep(CAN_TypeDef* CANx)
  843. {
  844. uint8_t sleepstatus = CAN_Sleep_Failed;
  845. /* Check the parameters */
  846. assert_param(IS_CAN_ALL_PERIPH(CANx));
  847. /* Request Sleep mode */
  848. CANx->MCR = (((CANx->MCR) & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
  849. /* Sleep mode status */
  850. if ((CANx->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) == CAN_MSR_SLAK)
  851. {
  852. /* Sleep mode not entered */
  853. sleepstatus = CAN_Sleep_Ok;
  854. }
  855. /* return sleep mode status */
  856. return (uint8_t)sleepstatus;
  857. }
  858. /**
  859. * @brief Wakes up the CAN peripheral from sleep mode .
  860. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  861. * @retval CAN_WakeUp_Ok if sleep mode left, CAN_WakeUp_Failed otherwise.
  862. */
  863. uint8_t CAN_WakeUp(CAN_TypeDef* CANx)
  864. {
  865. uint32_t wait_slak = SLAK_TIMEOUT;
  866. uint8_t wakeupstatus = CAN_WakeUp_Failed;
  867. /* Check the parameters */
  868. assert_param(IS_CAN_ALL_PERIPH(CANx));
  869. /* Wake up request */
  870. CANx->MCR &= ~(uint32_t)CAN_MCR_SLEEP;
  871. /* Sleep mode status */
  872. while(((CANx->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)&&(wait_slak!=0x00))
  873. {
  874. wait_slak--;
  875. }
  876. if((CANx->MSR & CAN_MSR_SLAK) != CAN_MSR_SLAK)
  877. {
  878. /* wake up done : Sleep mode exited */
  879. wakeupstatus = CAN_WakeUp_Ok;
  880. }
  881. /* return wakeup status */
  882. return (uint8_t)wakeupstatus;
  883. }
  884. /**
  885. * @}
  886. */
  887. /** @defgroup CAN_Group5 CAN Bus Error management functions
  888. * @brief CAN Bus Error management functions
  889. *
  890. @verbatim
  891. ===============================================================================
  892. ##### CAN Bus Error management functions #####
  893. ===============================================================================
  894. [..] This section provides functions allowing to
  895. (+) Return the CANx's last error code (LEC)
  896. (+) Return the CANx Receive Error Counter (REC)
  897. (+) Return the LSB of the 9-bit CANx Transmit Error Counter(TEC).
  898. -@- If TEC is greater than 255, The CAN is in bus-off state.
  899. -@- if REC or TEC are greater than 96, an Error warning flag occurs.
  900. -@- if REC or TEC are greater than 127, an Error Passive Flag occurs.
  901. @endverbatim
  902. * @{
  903. */
  904. /**
  905. * @brief Returns the CANx's last error code (LEC).
  906. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  907. * @retval Error code:
  908. * - CAN_ERRORCODE_NoErr: No Error
  909. * - CAN_ERRORCODE_StuffErr: Stuff Error
  910. * - CAN_ERRORCODE_FormErr: Form Error
  911. * - CAN_ERRORCODE_ACKErr : Acknowledgment Error
  912. * - CAN_ERRORCODE_BitRecessiveErr: Bit Recessive Error
  913. * - CAN_ERRORCODE_BitDominantErr: Bit Dominant Error
  914. * - CAN_ERRORCODE_CRCErr: CRC Error
  915. * - CAN_ERRORCODE_SoftwareSetErr: Software Set Error
  916. */
  917. uint8_t CAN_GetLastErrorCode(CAN_TypeDef* CANx)
  918. {
  919. uint8_t errorcode=0;
  920. /* Check the parameters */
  921. assert_param(IS_CAN_ALL_PERIPH(CANx));
  922. /* Get the error code*/
  923. errorcode = (((uint8_t)CANx->ESR) & (uint8_t)CAN_ESR_LEC);
  924. /* Return the error code*/
  925. return errorcode;
  926. }
  927. /**
  928. * @brief Returns the CANx Receive Error Counter (REC).
  929. * @note In case of an error during reception, this counter is incremented
  930. * by 1 or by 8 depending on the error condition as defined by the CAN
  931. * standard. After every successful reception, the counter is
  932. * decremented by 1 or reset to 120 if its value was higher than 128.
  933. * When the counter value exceeds 127, the CAN controller enters the
  934. * error passive state.
  935. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  936. * @retval CAN Receive Error Counter.
  937. */
  938. uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef* CANx)
  939. {
  940. uint8_t counter=0;
  941. /* Check the parameters */
  942. assert_param(IS_CAN_ALL_PERIPH(CANx));
  943. /* Get the Receive Error Counter*/
  944. counter = (uint8_t)((CANx->ESR & CAN_ESR_REC)>> 24);
  945. /* Return the Receive Error Counter*/
  946. return counter;
  947. }
  948. /**
  949. * @brief Returns the LSB of the 9-bit CANx Transmit Error Counter(TEC).
  950. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  951. * @retval LSB of the 9-bit CAN Transmit Error Counter.
  952. */
  953. uint8_t CAN_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx)
  954. {
  955. uint8_t counter=0;
  956. /* Check the parameters */
  957. assert_param(IS_CAN_ALL_PERIPH(CANx));
  958. /* Get the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
  959. counter = (uint8_t)((CANx->ESR & CAN_ESR_TEC)>> 16);
  960. /* Return the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
  961. return counter;
  962. }
  963. /**
  964. * @}
  965. */
  966. /** @defgroup CAN_Group6 Interrupts and flags management functions
  967. * @brief Interrupts and flags management functions
  968. *
  969. @verbatim
  970. ===============================================================================
  971. ##### Interrupts and flags management functions #####
  972. ===============================================================================
  973. [..] This section provides functions allowing to configure the CAN Interrupts
  974. and to get the status and clear flags and Interrupts pending bits.
  975. The CAN provides 14 Interrupts sources and 15 Flags:
  976. *** Flags ***
  977. =============
  978. [..] The 15 flags can be divided on 4 groups:
  979. (+) Transmit Flags
  980. (++) CAN_FLAG_RQCP0,
  981. (++) CAN_FLAG_RQCP1,
  982. (++) CAN_FLAG_RQCP2 : Request completed MailBoxes 0, 1 and 2 Flags
  983. Set when when the last request (transmit or abort)
  984. has been performed.
  985. (+) Receive Flags
  986. (++) CAN_FLAG_FMP0,
  987. (++) CAN_FLAG_FMP1 : FIFO 0 and 1 Message Pending Flags
  988. set to signal that messages are pending in the receive
  989. FIFO.
  990. These Flags are cleared only by hardware.
  991. (++) CAN_FLAG_FF0,
  992. (++) CAN_FLAG_FF1 : FIFO 0 and 1 Full Flags
  993. set when three messages are stored in the selected
  994. FIFO.
  995. (++) CAN_FLAG_FOV0
  996. (++) CAN_FLAG_FOV1 : FIFO 0 and 1 Overrun Flags
  997. set when a new message has been received and passed
  998. the filter while the FIFO was full.
  999. (+) Operating Mode Flags
  1000. (++) CAN_FLAG_WKU : Wake up Flag
  1001. set to signal that a SOF bit has been detected while
  1002. the CAN hardware was in Sleep mode.
  1003. (++) CAN_FLAG_SLAK : Sleep acknowledge Flag
  1004. Set to signal that the CAN has entered Sleep Mode.
  1005. (+) Error Flags
  1006. (++) CAN_FLAG_EWG : Error Warning Flag
  1007. Set when the warning limit has been reached (Receive
  1008. Error Counter or Transmit Error Counter greater than 96).
  1009. This Flag is cleared only by hardware.
  1010. (++) CAN_FLAG_EPV : Error Passive Flag
  1011. Set when the Error Passive limit has been reached
  1012. (Receive Error Counter or Transmit Error Counter
  1013. greater than 127).
  1014. This Flag is cleared only by hardware.
  1015. (++) CAN_FLAG_BOF : Bus-Off Flag
  1016. set when CAN enters the bus-off state. The bus-off
  1017. state is entered on TEC overflow, greater than 255.
  1018. This Flag is cleared only by hardware.
  1019. (++) CAN_FLAG_LEC : Last error code Flag
  1020. set If a message has been transferred (reception or
  1021. transmission) with error, and the error code is hold.
  1022. *** Interrupts ***
  1023. ==================
  1024. [..] The 14 interrupts can be divided on 4 groups:
  1025. (+) Transmit interrupt
  1026. (++) CAN_IT_TME : Transmit mailbox empty Interrupt
  1027. if enabled, this interrupt source is pending when
  1028. no transmit request are pending for Tx mailboxes.
  1029. (+) Receive Interrupts
  1030. (++) CAN_IT_FMP0,
  1031. (++) CAN_IT_FMP1 : FIFO 0 and FIFO1 message pending Interrupts
  1032. if enabled, these interrupt sources are pending
  1033. when messages are pending in the receive FIFO.
  1034. The corresponding interrupt pending bits are cleared
  1035. only by hardware.
  1036. (++) CAN_IT_FF0,
  1037. (++) CAN_IT_FF1 : FIFO 0 and FIFO1 full Interrupts
  1038. if enabled, these interrupt sources are pending
  1039. when three messages are stored in the selected FIFO.
  1040. (++) CAN_IT_FOV0,
  1041. (++) CAN_IT_FOV1 : FIFO 0 and FIFO1 overrun Interrupts
  1042. if enabled, these interrupt sources are pending
  1043. when a new message has been received and passed
  1044. the filter while the FIFO was full.
  1045. (+) Operating Mode Interrupts
  1046. (++) CAN_IT_WKU : Wake-up Interrupt
  1047. if enabled, this interrupt source is pending when
  1048. a SOF bit has been detected while the CAN hardware
  1049. was in Sleep mode.
  1050. (++) CAN_IT_SLK : Sleep acknowledge Interrupt
  1051. if enabled, this interrupt source is pending when
  1052. the CAN has entered Sleep Mode.
  1053. (+) Error Interrupts
  1054. (++) CAN_IT_EWG : Error warning Interrupt
  1055. if enabled, this interrupt source is pending when
  1056. the warning limit has been reached (Receive Error
  1057. Counter or Transmit Error Counter=96).
  1058. (++) CAN_IT_EPV : Error passive Interrupt
  1059. if enabled, this interrupt source is pending when
  1060. the Error Passive limit has been reached (Receive
  1061. Error Counter or Transmit Error Counter>127).
  1062. (++) CAN_IT_BOF : Bus-off Interrupt
  1063. if enabled, this interrupt source is pending when
  1064. CAN enters the bus-off state. The bus-off state is
  1065. entered on TEC overflow, greater than 255.
  1066. This Flag is cleared only by hardware.
  1067. (++) CAN_IT_LEC : Last error code Interrupt
  1068. if enabled, this interrupt source is pending when
  1069. a message has been transferred (reception or
  1070. transmission) with error, and the error code is hold.
  1071. (++) CAN_IT_ERR : Error Interrupt
  1072. if enabled, this interrupt source is pending when
  1073. an error condition is pending.
  1074. [..] Managing the CAN controller events :
  1075. The user should identify which mode will be used in his application to
  1076. manage the CAN controller events: Polling mode or Interrupt mode.
  1077. (#) In the Polling Mode it is advised to use the following functions:
  1078. (++) CAN_GetFlagStatus() : to check if flags events occur.
  1079. (++) CAN_ClearFlag() : to clear the flags events.
  1080. (#) In the Interrupt Mode it is advised to use the following functions:
  1081. (++) CAN_ITConfig() : to enable or disable the interrupt source.
  1082. (++) CAN_GetITStatus() : to check if Interrupt occurs.
  1083. (++) CAN_ClearITPendingBit() : to clear the Interrupt pending Bit
  1084. (corresponding Flag).
  1085. -@@- This function has no impact on CAN_IT_FMP0 and CAN_IT_FMP1 Interrupts
  1086. pending bits since there are cleared only by hardware.
  1087. @endverbatim
  1088. * @{
  1089. */
  1090. /**
  1091. * @brief Enables or disables the specified CANx interrupts.
  1092. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1093. * @param CAN_IT: specifies the CAN interrupt sources to be enabled or disabled.
  1094. * This parameter can be:
  1095. * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
  1096. * @arg CAN_IT_FMP0: FIFO 0 message pending Interrupt
  1097. * @arg CAN_IT_FF0: FIFO 0 full Interrupt
  1098. * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
  1099. * @arg CAN_IT_FMP1: FIFO 1 message pending Interrupt
  1100. * @arg CAN_IT_FF1: FIFO 1 full Interrupt
  1101. * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
  1102. * @arg CAN_IT_WKU: Wake-up Interrupt
  1103. * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
  1104. * @arg CAN_IT_EWG: Error warning Interrupt
  1105. * @arg CAN_IT_EPV: Error passive Interrupt
  1106. * @arg CAN_IT_BOF: Bus-off Interrupt
  1107. * @arg CAN_IT_LEC: Last error code Interrupt
  1108. * @arg CAN_IT_ERR: Error Interrupt
  1109. * @param NewState: new state of the CAN interrupts.
  1110. * This parameter can be: ENABLE or DISABLE.
  1111. * @retval None
  1112. */
  1113. void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState)
  1114. {
  1115. /* Check the parameters */
  1116. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1117. assert_param(IS_CAN_IT(CAN_IT));
  1118. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1119. if (NewState != DISABLE)
  1120. {
  1121. /* Enable the selected CANx interrupt */
  1122. CANx->IER |= CAN_IT;
  1123. }
  1124. else
  1125. {
  1126. /* Disable the selected CANx interrupt */
  1127. CANx->IER &= ~CAN_IT;
  1128. }
  1129. }
  1130. /**
  1131. * @brief Checks whether the specified CAN flag is set or not.
  1132. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1133. * @param CAN_FLAG: specifies the flag to check.
  1134. * This parameter can be one of the following values:
  1135. * @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
  1136. * @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
  1137. * @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
  1138. * @arg CAN_FLAG_FMP0: FIFO 0 Message Pending Flag
  1139. * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
  1140. * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
  1141. * @arg CAN_FLAG_FMP1: FIFO 1 Message Pending Flag
  1142. * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
  1143. * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
  1144. * @arg CAN_FLAG_WKU: Wake up Flag
  1145. * @arg CAN_FLAG_SLAK: Sleep acknowledge Flag
  1146. * @arg CAN_FLAG_EWG: Error Warning Flag
  1147. * @arg CAN_FLAG_EPV: Error Passive Flag
  1148. * @arg CAN_FLAG_BOF: Bus-Off Flag
  1149. * @arg CAN_FLAG_LEC: Last error code Flag
  1150. * @retval The new state of CAN_FLAG (SET or RESET).
  1151. */
  1152. FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
  1153. {
  1154. FlagStatus bitstatus = RESET;
  1155. /* Check the parameters */
  1156. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1157. assert_param(IS_CAN_GET_FLAG(CAN_FLAG));
  1158. if((CAN_FLAG & CAN_FLAGS_ESR) != (uint32_t)RESET)
  1159. {
  1160. /* Check the status of the specified CAN flag */
  1161. if ((CANx->ESR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1162. {
  1163. /* CAN_FLAG is set */
  1164. bitstatus = SET;
  1165. }
  1166. else
  1167. {
  1168. /* CAN_FLAG is reset */
  1169. bitstatus = RESET;
  1170. }
  1171. }
  1172. else if((CAN_FLAG & CAN_FLAGS_MSR) != (uint32_t)RESET)
  1173. {
  1174. /* Check the status of the specified CAN flag */
  1175. if ((CANx->MSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1176. {
  1177. /* CAN_FLAG is set */
  1178. bitstatus = SET;
  1179. }
  1180. else
  1181. {
  1182. /* CAN_FLAG is reset */
  1183. bitstatus = RESET;
  1184. }
  1185. }
  1186. else if((CAN_FLAG & CAN_FLAGS_TSR) != (uint32_t)RESET)
  1187. {
  1188. /* Check the status of the specified CAN flag */
  1189. if ((CANx->TSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1190. {
  1191. /* CAN_FLAG is set */
  1192. bitstatus = SET;
  1193. }
  1194. else
  1195. {
  1196. /* CAN_FLAG is reset */
  1197. bitstatus = RESET;
  1198. }
  1199. }
  1200. else if((CAN_FLAG & CAN_FLAGS_RF0R) != (uint32_t)RESET)
  1201. {
  1202. /* Check the status of the specified CAN flag */
  1203. if ((CANx->RF0R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1204. {
  1205. /* CAN_FLAG is set */
  1206. bitstatus = SET;
  1207. }
  1208. else
  1209. {
  1210. /* CAN_FLAG is reset */
  1211. bitstatus = RESET;
  1212. }
  1213. }
  1214. else /* If(CAN_FLAG & CAN_FLAGS_RF1R != (uint32_t)RESET) */
  1215. {
  1216. /* Check the status of the specified CAN flag */
  1217. if ((uint32_t)(CANx->RF1R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1218. {
  1219. /* CAN_FLAG is set */
  1220. bitstatus = SET;
  1221. }
  1222. else
  1223. {
  1224. /* CAN_FLAG is reset */
  1225. bitstatus = RESET;
  1226. }
  1227. }
  1228. /* Return the CAN_FLAG status */
  1229. return bitstatus;
  1230. }
  1231. /**
  1232. * @brief Clears the CAN's pending flags.
  1233. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1234. * @param CAN_FLAG: specifies the flag to clear.
  1235. * This parameter can be one of the following values:
  1236. * @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
  1237. * @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
  1238. * @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
  1239. * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
  1240. * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
  1241. * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
  1242. * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
  1243. * @arg CAN_FLAG_WKU: Wake up Flag
  1244. * @arg CAN_FLAG_SLAK: Sleep acknowledge Flag
  1245. * @arg CAN_FLAG_LEC: Last error code Flag
  1246. * @retval None
  1247. */
  1248. void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
  1249. {
  1250. uint32_t flagtmp=0;
  1251. /* Check the parameters */
  1252. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1253. assert_param(IS_CAN_CLEAR_FLAG(CAN_FLAG));
  1254. if (CAN_FLAG == CAN_FLAG_LEC) /* ESR register */
  1255. {
  1256. /* Clear the selected CAN flags */
  1257. CANx->ESR = (uint32_t)RESET;
  1258. }
  1259. else /* MSR or TSR or RF0R or RF1R */
  1260. {
  1261. flagtmp = CAN_FLAG & 0x000FFFFF;
  1262. if ((CAN_FLAG & CAN_FLAGS_RF0R)!=(uint32_t)RESET)
  1263. {
  1264. /* Receive Flags */
  1265. CANx->RF0R = (uint32_t)(flagtmp);
  1266. }
  1267. else if ((CAN_FLAG & CAN_FLAGS_RF1R)!=(uint32_t)RESET)
  1268. {
  1269. /* Receive Flags */
  1270. CANx->RF1R = (uint32_t)(flagtmp);
  1271. }
  1272. else if ((CAN_FLAG & CAN_FLAGS_TSR)!=(uint32_t)RESET)
  1273. {
  1274. /* Transmit Flags */
  1275. CANx->TSR = (uint32_t)(flagtmp);
  1276. }
  1277. else /* If((CAN_FLAG & CAN_FLAGS_MSR)!=(uint32_t)RESET) */
  1278. {
  1279. /* Operating mode Flags */
  1280. CANx->MSR = (uint32_t)(flagtmp);
  1281. }
  1282. }
  1283. }
  1284. /**
  1285. * @brief Checks whether the specified CANx interrupt has occurred or not.
  1286. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1287. * @param CAN_IT: specifies the CAN interrupt source to check.
  1288. * This parameter can be one of the following values:
  1289. * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
  1290. * @arg CAN_IT_FMP0: FIFO 0 message pending Interrupt
  1291. * @arg CAN_IT_FF0: FIFO 0 full Interrupt
  1292. * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
  1293. * @arg CAN_IT_FMP1: FIFO 1 message pending Interrupt
  1294. * @arg CAN_IT_FF1: FIFO 1 full Interrupt
  1295. * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
  1296. * @arg CAN_IT_WKU: Wake-up Interrupt
  1297. * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
  1298. * @arg CAN_IT_EWG: Error warning Interrupt
  1299. * @arg CAN_IT_EPV: Error passive Interrupt
  1300. * @arg CAN_IT_BOF: Bus-off Interrupt
  1301. * @arg CAN_IT_LEC: Last error code Interrupt
  1302. * @arg CAN_IT_ERR: Error Interrupt
  1303. * @retval The current state of CAN_IT (SET or RESET).
  1304. */
  1305. ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT)
  1306. {
  1307. ITStatus itstatus = RESET;
  1308. /* Check the parameters */
  1309. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1310. assert_param(IS_CAN_IT(CAN_IT));
  1311. /* check the interrupt enable bit */
  1312. if((CANx->IER & CAN_IT) != RESET)
  1313. {
  1314. /* in case the Interrupt is enabled, .... */
  1315. switch (CAN_IT)
  1316. {
  1317. case CAN_IT_TME:
  1318. /* Check CAN_TSR_RQCPx bits */
  1319. itstatus = CheckITStatus(CANx->TSR, CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2);
  1320. break;
  1321. case CAN_IT_FMP0:
  1322. /* Check CAN_RF0R_FMP0 bit */
  1323. itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FMP0);
  1324. break;
  1325. case CAN_IT_FF0:
  1326. /* Check CAN_RF0R_FULL0 bit */
  1327. itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FULL0);
  1328. break;
  1329. case CAN_IT_FOV0:
  1330. /* Check CAN_RF0R_FOVR0 bit */
  1331. itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FOVR0);
  1332. break;
  1333. case CAN_IT_FMP1:
  1334. /* Check CAN_RF1R_FMP1 bit */
  1335. itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FMP1);
  1336. break;
  1337. case CAN_IT_FF1:
  1338. /* Check CAN_RF1R_FULL1 bit */
  1339. itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FULL1);
  1340. break;
  1341. case CAN_IT_FOV1:
  1342. /* Check CAN_RF1R_FOVR1 bit */
  1343. itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FOVR1);
  1344. break;
  1345. case CAN_IT_WKU:
  1346. /* Check CAN_MSR_WKUI bit */
  1347. itstatus = CheckITStatus(CANx->MSR, CAN_MSR_WKUI);
  1348. break;
  1349. case CAN_IT_SLK:
  1350. /* Check CAN_MSR_SLAKI bit */
  1351. itstatus = CheckITStatus(CANx->MSR, CAN_MSR_SLAKI);
  1352. break;
  1353. case CAN_IT_EWG:
  1354. /* Check CAN_ESR_EWGF bit */
  1355. itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EWGF);
  1356. break;
  1357. case CAN_IT_EPV:
  1358. /* Check CAN_ESR_EPVF bit */
  1359. itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EPVF);
  1360. break;
  1361. case CAN_IT_BOF:
  1362. /* Check CAN_ESR_BOFF bit */
  1363. itstatus = CheckITStatus(CANx->ESR, CAN_ESR_BOFF);
  1364. break;
  1365. case CAN_IT_LEC:
  1366. /* Check CAN_ESR_LEC bit */
  1367. itstatus = CheckITStatus(CANx->ESR, CAN_ESR_LEC);
  1368. break;
  1369. case CAN_IT_ERR:
  1370. /* Check CAN_MSR_ERRI bit */
  1371. itstatus = CheckITStatus(CANx->MSR, CAN_MSR_ERRI);
  1372. break;
  1373. default:
  1374. /* in case of error, return RESET */
  1375. itstatus = RESET;
  1376. break;
  1377. }
  1378. }
  1379. else
  1380. {
  1381. /* in case the Interrupt is not enabled, return RESET */
  1382. itstatus = RESET;
  1383. }
  1384. /* Return the CAN_IT status */
  1385. return itstatus;
  1386. }
  1387. /**
  1388. * @brief Clears the CANx's interrupt pending bits.
  1389. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1390. * @param CAN_IT: specifies the interrupt pending bit to clear.
  1391. * This parameter can be one of the following values:
  1392. * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
  1393. * @arg CAN_IT_FF0: FIFO 0 full Interrupt
  1394. * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
  1395. * @arg CAN_IT_FF1: FIFO 1 full Interrupt
  1396. * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
  1397. * @arg CAN_IT_WKU: Wake-up Interrupt
  1398. * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
  1399. * @arg CAN_IT_EWG: Error warning Interrupt
  1400. * @arg CAN_IT_EPV: Error passive Interrupt
  1401. * @arg CAN_IT_BOF: Bus-off Interrupt
  1402. * @arg CAN_IT_LEC: Last error code Interrupt
  1403. * @arg CAN_IT_ERR: Error Interrupt
  1404. * @retval None
  1405. */
  1406. void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT)
  1407. {
  1408. /* Check the parameters */
  1409. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1410. assert_param(IS_CAN_CLEAR_IT(CAN_IT));
  1411. switch (CAN_IT)
  1412. {
  1413. case CAN_IT_TME:
  1414. /* Clear CAN_TSR_RQCPx (rc_w1)*/
  1415. CANx->TSR = CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2;
  1416. break;
  1417. case CAN_IT_FF0:
  1418. /* Clear CAN_RF0R_FULL0 (rc_w1)*/
  1419. CANx->RF0R = CAN_RF0R_FULL0;
  1420. break;
  1421. case CAN_IT_FOV0:
  1422. /* Clear CAN_RF0R_FOVR0 (rc_w1)*/
  1423. CANx->RF0R = CAN_RF0R_FOVR0;
  1424. break;
  1425. case CAN_IT_FF1:
  1426. /* Clear CAN_RF1R_FULL1 (rc_w1)*/
  1427. CANx->RF1R = CAN_RF1R_FULL1;
  1428. break;
  1429. case CAN_IT_FOV1:
  1430. /* Clear CAN_RF1R_FOVR1 (rc_w1)*/
  1431. CANx->RF1R = CAN_RF1R_FOVR1;
  1432. break;
  1433. case CAN_IT_WKU:
  1434. /* Clear CAN_MSR_WKUI (rc_w1)*/
  1435. CANx->MSR = CAN_MSR_WKUI;
  1436. break;
  1437. case CAN_IT_SLK:
  1438. /* Clear CAN_MSR_SLAKI (rc_w1)*/
  1439. CANx->MSR = CAN_MSR_SLAKI;
  1440. break;
  1441. case CAN_IT_EWG:
  1442. /* Clear CAN_MSR_ERRI (rc_w1) */
  1443. CANx->MSR = CAN_MSR_ERRI;
  1444. /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
  1445. break;
  1446. case CAN_IT_EPV:
  1447. /* Clear CAN_MSR_ERRI (rc_w1) */
  1448. CANx->MSR = CAN_MSR_ERRI;
  1449. /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
  1450. break;
  1451. case CAN_IT_BOF:
  1452. /* Clear CAN_MSR_ERRI (rc_w1) */
  1453. CANx->MSR = CAN_MSR_ERRI;
  1454. /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
  1455. break;
  1456. case CAN_IT_LEC:
  1457. /* Clear LEC bits */
  1458. CANx->ESR = RESET;
  1459. /* Clear CAN_MSR_ERRI (rc_w1) */
  1460. CANx->MSR = CAN_MSR_ERRI;
  1461. break;
  1462. case CAN_IT_ERR:
  1463. /*Clear LEC bits */
  1464. CANx->ESR = RESET;
  1465. /* Clear CAN_MSR_ERRI (rc_w1) */
  1466. CANx->MSR = CAN_MSR_ERRI;
  1467. /* @note BOFF, EPVF and EWGF Flags are cleared by hardware depending on the CAN Bus status*/
  1468. break;
  1469. default:
  1470. break;
  1471. }
  1472. }
  1473. /**
  1474. * @}
  1475. */
  1476. /**
  1477. * @brief Checks whether the CAN interrupt has occurred or not.
  1478. * @param CAN_Reg: specifies the CAN interrupt register to check.
  1479. * @param It_Bit: specifies the interrupt source bit to check.
  1480. * @retval The new state of the CAN Interrupt (SET or RESET).
  1481. */
  1482. static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit)
  1483. {
  1484. ITStatus pendingbitstatus = RESET;
  1485. if ((CAN_Reg & It_Bit) != (uint32_t)RESET)
  1486. {
  1487. /* CAN_IT is set */
  1488. pendingbitstatus = SET;
  1489. }
  1490. else
  1491. {
  1492. /* CAN_IT is reset */
  1493. pendingbitstatus = RESET;
  1494. }
  1495. return pendingbitstatus;
  1496. }
  1497. /**
  1498. * @}
  1499. */
  1500. /**
  1501. * @}
  1502. */
  1503. /**
  1504. * @}
  1505. */
  1506. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/