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.
 
 
 

1673 lines
51 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_can.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 14-April-2017
  7. * @brief CAN HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Controller Area Network (CAN) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State and Error functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. (#) Enable the CAN controller interface clock using
  21. __HAL_RCC_CAN1_CLK_ENABLE() for CAN1, __HAL_RCC_CAN2_CLK_ENABLE() for CAN2
  22. and __HAL_RCC_CAN3_CLK_ENABLE() for CAN3
  23. -@- In case you are using CAN2 only, you have to enable the CAN1 clock.
  24. (#) CAN pins configuration
  25. (++) Enable the clock for the CAN GPIOs using the following function:
  26. __HAL_RCC_GPIOx_CLK_ENABLE()
  27. (++) Connect and configure the involved CAN pins to AF9 using the
  28. following function HAL_GPIO_Init()
  29. (#) Initialize and configure the CAN using HAL_CAN_Init() function.
  30. (#) Transmit the desired CAN frame using HAL_CAN_Transmit() function.
  31. (#) Or transmit the desired CAN frame using HAL_CAN_Transmit_IT() function.
  32. (#) Receive a CAN frame using HAL_CAN_Receive() function.
  33. (#) Or receive a CAN frame using HAL_CAN_Receive_IT() function.
  34. *** Polling mode IO operation ***
  35. =================================
  36. [..]
  37. (+) Start the CAN peripheral transmission and wait the end of this operation
  38. using HAL_CAN_Transmit(), at this stage user can specify the value of timeout
  39. according to his end application
  40. (+) Start the CAN peripheral reception and wait the end of this operation
  41. using HAL_CAN_Receive(), at this stage user can specify the value of timeout
  42. according to his end application
  43. *** Interrupt mode IO operation ***
  44. ===================================
  45. [..]
  46. (+) Start the CAN peripheral transmission using HAL_CAN_Transmit_IT()
  47. (+) Start the CAN peripheral reception using HAL_CAN_Receive_IT()
  48. (+) Use HAL_CAN_IRQHandler() called under the used CAN Interrupt subroutine
  49. (+) At CAN end of transmission HAL_CAN_TxCpltCallback() function is executed and user can
  50. add his own code by customization of function pointer HAL_CAN_TxCpltCallback
  51. (+) In case of CAN Error, HAL_CAN_ErrorCallback() function is executed and user can
  52. add his own code by customization of function pointer HAL_CAN_ErrorCallback
  53. *** CAN HAL driver macros list ***
  54. =============================================
  55. [..]
  56. Below the list of most used macros in CAN HAL driver.
  57. (+) __HAL_CAN_ENABLE_IT: Enable the specified CAN interrupts
  58. (+) __HAL_CAN_DISABLE_IT: Disable the specified CAN interrupts
  59. (+) __HAL_CAN_GET_IT_SOURCE: Check if the specified CAN interrupt source is enabled or disabled
  60. (+) __HAL_CAN_CLEAR_FLAG: Clear the CAN's pending flags
  61. (+) __HAL_CAN_GET_FLAG: Get the selected CAN's flag status
  62. [..]
  63. (@) You can refer to the CAN HAL driver header file for more useful macros
  64. @endverbatim
  65. ******************************************************************************
  66. * @attention
  67. *
  68. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  69. *
  70. * Redistribution and use in source and binary forms, with or without modification,
  71. * are permitted provided that the following conditions are met:
  72. * 1. Redistributions of source code must retain the above copyright notice,
  73. * this list of conditions and the following disclaimer.
  74. * 2. Redistributions in binary form must reproduce the above copyright notice,
  75. * this list of conditions and the following disclaimer in the documentation
  76. * and/or other materials provided with the distribution.
  77. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  78. * may be used to endorse or promote products derived from this software
  79. * without specific prior written permission.
  80. *
  81. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  82. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  83. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  84. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  85. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  86. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  87. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  88. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  89. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  90. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  91. *
  92. ******************************************************************************
  93. */
  94. /* Includes ------------------------------------------------------------------*/
  95. #include "stm32f7xx_hal.h"
  96. /** @addtogroup STM32F7xx_HAL_Driver
  97. * @{
  98. */
  99. /** @defgroup CAN CAN
  100. * @brief CAN driver modules
  101. * @{
  102. */
  103. #ifdef HAL_CAN_MODULE_ENABLED
  104. /* Private typedef -----------------------------------------------------------*/
  105. /* Private define ------------------------------------------------------------*/
  106. /** @addtogroup CAN_Private_Constants
  107. * @{
  108. */
  109. #define CAN_TIMEOUT_VALUE 10
  110. /**
  111. * @}
  112. */
  113. /* Private macro -------------------------------------------------------------*/
  114. /* Private variables ---------------------------------------------------------*/
  115. /* Private function prototypes -----------------------------------------------*/
  116. /** @addtogroup CAN_Private_Functions
  117. * @{
  118. */
  119. static HAL_StatusTypeDef CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber);
  120. static HAL_StatusTypeDef CAN_Transmit_IT(CAN_HandleTypeDef* hcan);
  121. /**
  122. * @}
  123. */
  124. /* Exported functions --------------------------------------------------------*/
  125. /** @defgroup CAN_Exported_Functions CAN Exported Functions
  126. * @{
  127. */
  128. /** @defgroup CAN_Exported_Functions_Group1 Initialization and de-initialization functions
  129. * @brief Initialization and Configuration functions
  130. *
  131. @verbatim
  132. ==============================================================================
  133. ##### Initialization and de-initialization functions #####
  134. ==============================================================================
  135. [..] This section provides functions allowing to:
  136. (+) Initialize and configure the CAN.
  137. (+) De-initialize the CAN.
  138. @endverbatim
  139. * @{
  140. */
  141. /**
  142. * @brief Initializes the CAN peripheral according to the specified
  143. * parameters in the CAN_InitStruct.
  144. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  145. * the configuration information for the specified CAN.
  146. * @retval HAL status
  147. */
  148. HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef* hcan)
  149. {
  150. uint32_t InitStatus = CAN_INITSTATUS_FAILED;
  151. uint32_t tickstart = 0;
  152. /* Check CAN handle */
  153. if(hcan == NULL)
  154. {
  155. return HAL_ERROR;
  156. }
  157. /* Check the parameters */
  158. assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
  159. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TTCM));
  160. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ABOM));
  161. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AWUM));
  162. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.NART));
  163. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.RFLM));
  164. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TXFP));
  165. assert_param(IS_CAN_MODE(hcan->Init.Mode));
  166. assert_param(IS_CAN_SJW(hcan->Init.SJW));
  167. assert_param(IS_CAN_BS1(hcan->Init.BS1));
  168. assert_param(IS_CAN_BS2(hcan->Init.BS2));
  169. assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler));
  170. if(hcan->State == HAL_CAN_STATE_RESET)
  171. {
  172. /* Allocate lock resource and initialize it */
  173. hcan->Lock = HAL_UNLOCKED;
  174. /* Init the low level hardware */
  175. HAL_CAN_MspInit(hcan);
  176. }
  177. /* Initialize the CAN state*/
  178. hcan->State = HAL_CAN_STATE_BUSY;
  179. /* Exit from sleep mode */
  180. hcan->Instance->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
  181. /* Request initialisation */
  182. hcan->Instance->MCR |= CAN_MCR_INRQ ;
  183. /* Get tick */
  184. tickstart = HAL_GetTick();
  185. /* Wait the acknowledge */
  186. while((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
  187. {
  188. if((HAL_GetTick() - tickstart ) > CAN_TIMEOUT_VALUE)
  189. {
  190. hcan->State= HAL_CAN_STATE_TIMEOUT;
  191. /* Process unlocked */
  192. __HAL_UNLOCK(hcan);
  193. return HAL_TIMEOUT;
  194. }
  195. }
  196. /* Check acknowledge */
  197. if ((hcan->Instance->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
  198. {
  199. /* Set the time triggered communication mode */
  200. if (hcan->Init.TTCM == ENABLE)
  201. {
  202. hcan->Instance->MCR |= CAN_MCR_TTCM;
  203. }
  204. else
  205. {
  206. hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_TTCM;
  207. }
  208. /* Set the automatic bus-off management */
  209. if (hcan->Init.ABOM == ENABLE)
  210. {
  211. hcan->Instance->MCR |= CAN_MCR_ABOM;
  212. }
  213. else
  214. {
  215. hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_ABOM;
  216. }
  217. /* Set the automatic wake-up mode */
  218. if (hcan->Init.AWUM == ENABLE)
  219. {
  220. hcan->Instance->MCR |= CAN_MCR_AWUM;
  221. }
  222. else
  223. {
  224. hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_AWUM;
  225. }
  226. /* Set the no automatic retransmission */
  227. if (hcan->Init.NART == ENABLE)
  228. {
  229. hcan->Instance->MCR |= CAN_MCR_NART;
  230. }
  231. else
  232. {
  233. hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_NART;
  234. }
  235. /* Set the receive FIFO locked mode */
  236. if (hcan->Init.RFLM == ENABLE)
  237. {
  238. hcan->Instance->MCR |= CAN_MCR_RFLM;
  239. }
  240. else
  241. {
  242. hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_RFLM;
  243. }
  244. /* Set the transmit FIFO priority */
  245. if (hcan->Init.TXFP == ENABLE)
  246. {
  247. hcan->Instance->MCR |= CAN_MCR_TXFP;
  248. }
  249. else
  250. {
  251. hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_TXFP;
  252. }
  253. /* Set the bit timing register */
  254. hcan->Instance->BTR = (uint32_t)((uint32_t)hcan->Init.Mode) | \
  255. ((uint32_t)hcan->Init.SJW) | \
  256. ((uint32_t)hcan->Init.BS1) | \
  257. ((uint32_t)hcan->Init.BS2) | \
  258. ((uint32_t)hcan->Init.Prescaler - 1);
  259. /* Request leave initialisation */
  260. hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_INRQ;
  261. /* Get tick */
  262. tickstart = HAL_GetTick();
  263. /* Wait the acknowledge */
  264. while((hcan->Instance->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
  265. {
  266. if((HAL_GetTick() - tickstart ) > CAN_TIMEOUT_VALUE)
  267. {
  268. hcan->State= HAL_CAN_STATE_TIMEOUT;
  269. /* Process unlocked */
  270. __HAL_UNLOCK(hcan);
  271. return HAL_TIMEOUT;
  272. }
  273. }
  274. /* Check acknowledged */
  275. if ((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
  276. {
  277. InitStatus = CAN_INITSTATUS_SUCCESS;
  278. }
  279. }
  280. if(InitStatus == CAN_INITSTATUS_SUCCESS)
  281. {
  282. /* Set CAN error code to none */
  283. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  284. /* Initialize the CAN state */
  285. hcan->State = HAL_CAN_STATE_READY;
  286. /* Return function status */
  287. return HAL_OK;
  288. }
  289. else
  290. {
  291. /* Initialize the CAN state */
  292. hcan->State = HAL_CAN_STATE_ERROR;
  293. /* Return function status */
  294. return HAL_ERROR;
  295. }
  296. }
  297. /**
  298. * @brief Configures the CAN reception filter according to the specified
  299. * parameters in the CAN_FilterInitStruct.
  300. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  301. * the configuration information for the specified CAN.
  302. * @param sFilterConfig: pointer to a CAN_FilterConfTypeDef structure that
  303. * contains the filter configuration information.
  304. * @retval None
  305. */
  306. HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef* hcan, CAN_FilterConfTypeDef* sFilterConfig)
  307. {
  308. uint32_t filternbrbitpos = 0;
  309. CAN_TypeDef *can_ip;
  310. /* Check the parameters */
  311. assert_param(IS_CAN_FILTER_NUMBER(sFilterConfig->FilterNumber));
  312. assert_param(IS_CAN_FILTER_MODE(sFilterConfig->FilterMode));
  313. assert_param(IS_CAN_FILTER_SCALE(sFilterConfig->FilterScale));
  314. assert_param(IS_CAN_FILTER_FIFO(sFilterConfig->FilterFIFOAssignment));
  315. assert_param(IS_FUNCTIONAL_STATE(sFilterConfig->FilterActivation));
  316. assert_param(IS_CAN_BANKNUMBER(sFilterConfig->BankNumber));
  317. filternbrbitpos = ((uint32_t)1) << sFilterConfig->FilterNumber;
  318. #if defined (CAN3)
  319. /* Check the CAN instance */
  320. if(hcan->Instance == CAN3)
  321. {
  322. can_ip = CAN3;
  323. }
  324. else
  325. {
  326. can_ip = CAN1;
  327. }
  328. #else
  329. can_ip = CAN1;
  330. #endif
  331. /* Initialisation mode for the filter */
  332. can_ip->FMR |= (uint32_t)CAN_FMR_FINIT;
  333. #if defined (CAN2)
  334. /* Select the start slave bank */
  335. can_ip->FMR &= ~((uint32_t)CAN_FMR_CAN2SB);
  336. can_ip->FMR |= (uint32_t)(sFilterConfig->BankNumber << 8);
  337. #endif
  338. /* Filter Deactivation */
  339. can_ip->FA1R &= ~(uint32_t)filternbrbitpos;
  340. /* Filter Scale */
  341. if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT)
  342. {
  343. /* 16-bit scale for the filter */
  344. can_ip->FS1R &= ~(uint32_t)filternbrbitpos;
  345. /* First 16-bit identifier and First 16-bit mask */
  346. /* Or First 16-bit identifier and Second 16-bit identifier */
  347. can_ip->sFilterRegister[sFilterConfig->FilterNumber].FR1 =
  348. ((0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16) |
  349. (0x0000FFFF & (uint32_t)sFilterConfig->FilterIdLow);
  350. /* Second 16-bit identifier and Second 16-bit mask */
  351. /* Or Third 16-bit identifier and Fourth 16-bit identifier */
  352. can_ip->sFilterRegister[sFilterConfig->FilterNumber].FR2 =
  353. ((0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16) |
  354. (0x0000FFFF & (uint32_t)sFilterConfig->FilterIdHigh);
  355. }
  356. if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT)
  357. {
  358. /* 32-bit scale for the filter */
  359. can_ip->FS1R |= filternbrbitpos;
  360. /* 32-bit identifier or First 32-bit identifier */
  361. can_ip->sFilterRegister[sFilterConfig->FilterNumber].FR1 =
  362. ((0x0000FFFF & (uint32_t)sFilterConfig->FilterIdHigh) << 16) |
  363. (0x0000FFFF & (uint32_t)sFilterConfig->FilterIdLow);
  364. /* 32-bit mask or Second 32-bit identifier */
  365. can_ip->sFilterRegister[sFilterConfig->FilterNumber].FR2 =
  366. ((0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16) |
  367. (0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdLow);
  368. }
  369. /* Filter Mode */
  370. if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK)
  371. {
  372. /*Id/Mask mode for the filter*/
  373. can_ip->FM1R &= ~(uint32_t)filternbrbitpos;
  374. }
  375. else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
  376. {
  377. /*Identifier list mode for the filter*/
  378. can_ip->FM1R |= (uint32_t)filternbrbitpos;
  379. }
  380. /* Filter FIFO assignment */
  381. if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0)
  382. {
  383. /* FIFO 0 assignation for the filter */
  384. can_ip->FFA1R &= ~(uint32_t)filternbrbitpos;
  385. }
  386. if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO1)
  387. {
  388. /* FIFO 1 assignation for the filter */
  389. can_ip->FFA1R |= (uint32_t)filternbrbitpos;
  390. }
  391. /* Filter activation */
  392. if (sFilterConfig->FilterActivation == ENABLE)
  393. {
  394. can_ip->FA1R |= filternbrbitpos;
  395. }
  396. /* Leave the initialisation mode for the filter */
  397. can_ip->FMR &= ~((uint32_t)CAN_FMR_FINIT);
  398. /* Return function status */
  399. return HAL_OK;
  400. }
  401. /**
  402. * @brief Deinitializes the CANx peripheral registers to their default reset values.
  403. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  404. * the configuration information for the specified CAN.
  405. * @retval HAL status
  406. */
  407. HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef* hcan)
  408. {
  409. /* Check CAN handle */
  410. if(hcan == NULL)
  411. {
  412. return HAL_ERROR;
  413. }
  414. /* Check the parameters */
  415. assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
  416. /* Change CAN state */
  417. hcan->State = HAL_CAN_STATE_BUSY;
  418. /* DeInit the low level hardware */
  419. HAL_CAN_MspDeInit(hcan);
  420. /* Change CAN state */
  421. hcan->State = HAL_CAN_STATE_RESET;
  422. /* Release Lock */
  423. __HAL_UNLOCK(hcan);
  424. /* Return function status */
  425. return HAL_OK;
  426. }
  427. /**
  428. * @brief Initializes the CAN MSP.
  429. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  430. * the configuration information for the specified CAN.
  431. * @retval None
  432. */
  433. __weak void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
  434. {
  435. /* Prevent unused argument(s) compilation warning */
  436. UNUSED(hcan);
  437. /* NOTE : This function Should not be modified, when the callback is needed,
  438. the HAL_CAN_MspInit could be implemented in the user file
  439. */
  440. }
  441. /**
  442. * @brief DeInitializes the CAN MSP.
  443. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  444. * the configuration information for the specified CAN.
  445. * @retval None
  446. */
  447. __weak void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
  448. {
  449. /* Prevent unused argument(s) compilation warning */
  450. UNUSED(hcan);
  451. /* NOTE : This function Should not be modified, when the callback is needed,
  452. the HAL_CAN_MspDeInit could be implemented in the user file
  453. */
  454. }
  455. /**
  456. * @}
  457. */
  458. /** @defgroup CAN_Exported_Functions_Group2 IO operation functions
  459. * @brief IO operation functions
  460. *
  461. @verbatim
  462. ==============================================================================
  463. ##### IO operation functions #####
  464. ==============================================================================
  465. [..] This section provides functions allowing to:
  466. (+) Transmit a CAN frame message.
  467. (+) Receive a CAN frame message.
  468. (+) Enter CAN peripheral in sleep mode.
  469. (+) Wake up the CAN peripheral from sleep mode.
  470. @endverbatim
  471. * @{
  472. */
  473. /**
  474. * @brief Initiates and transmits a CAN frame message.
  475. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  476. * the configuration information for the specified CAN.
  477. * @param Timeout: Specify Timeout value
  478. * @retval HAL status
  479. */
  480. HAL_StatusTypeDef HAL_CAN_Transmit(CAN_HandleTypeDef* hcan, uint32_t Timeout)
  481. {
  482. uint32_t transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
  483. uint32_t tickstart = 0U;
  484. /* Check the parameters */
  485. assert_param(IS_CAN_IDTYPE(hcan->pTxMsg->IDE));
  486. assert_param(IS_CAN_RTR(hcan->pTxMsg->RTR));
  487. assert_param(IS_CAN_DLC(hcan->pTxMsg->DLC));
  488. if(((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) || \
  489. ((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) || \
  490. ((hcan->Instance->TSR&CAN_TSR_TME2) == CAN_TSR_TME2))
  491. {
  492. /* Process locked */
  493. __HAL_LOCK(hcan);
  494. /* Change CAN state */
  495. switch(hcan->State)
  496. {
  497. case(HAL_CAN_STATE_BUSY_RX0):
  498. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
  499. break;
  500. case(HAL_CAN_STATE_BUSY_RX1):
  501. hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
  502. break;
  503. case(HAL_CAN_STATE_BUSY_RX0_RX1):
  504. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
  505. break;
  506. default: /* HAL_CAN_STATE_READY */
  507. hcan->State = HAL_CAN_STATE_BUSY_TX;
  508. break;
  509. }
  510. /* Select one empty transmit mailbox */
  511. if ((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
  512. {
  513. transmitmailbox = CAN_TXMAILBOX_0;
  514. }
  515. else if ((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
  516. {
  517. transmitmailbox = CAN_TXMAILBOX_1;
  518. }
  519. else
  520. {
  521. transmitmailbox = CAN_TXMAILBOX_2;
  522. }
  523. /* Set up the Id */
  524. hcan->Instance->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ;
  525. if (hcan->pTxMsg->IDE == CAN_ID_STD)
  526. {
  527. assert_param(IS_CAN_STDID(hcan->pTxMsg->StdId));
  528. hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->StdId << 21U) | \
  529. hcan->pTxMsg->RTR);
  530. }
  531. else
  532. {
  533. assert_param(IS_CAN_EXTID(hcan->pTxMsg->ExtId));
  534. hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->ExtId << 3U) | \
  535. hcan->pTxMsg->IDE | \
  536. hcan->pTxMsg->RTR);
  537. }
  538. /* Set up the DLC */
  539. hcan->pTxMsg->DLC &= (uint8_t)0x0000000F;
  540. hcan->Instance->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0U;
  541. hcan->Instance->sTxMailBox[transmitmailbox].TDTR |= hcan->pTxMsg->DLC;
  542. /* Set up the data field */
  543. hcan->Instance->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)hcan->pTxMsg->Data[3U] << 24U) |
  544. ((uint32_t)hcan->pTxMsg->Data[2U] << 16U) |
  545. ((uint32_t)hcan->pTxMsg->Data[1U] << 8U) |
  546. ((uint32_t)hcan->pTxMsg->Data[0U]));
  547. hcan->Instance->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)hcan->pTxMsg->Data[7U] << 24U) |
  548. ((uint32_t)hcan->pTxMsg->Data[6U] << 16U) |
  549. ((uint32_t)hcan->pTxMsg->Data[5U] << 8U) |
  550. ((uint32_t)hcan->pTxMsg->Data[4U]));
  551. /* Request transmission */
  552. hcan->Instance->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
  553. /* Get tick */
  554. tickstart = HAL_GetTick();
  555. /* Check End of transmission flag */
  556. while(!(__HAL_CAN_TRANSMIT_STATUS(hcan, transmitmailbox)))
  557. {
  558. /* Check for the Timeout */
  559. if(Timeout != HAL_MAX_DELAY)
  560. {
  561. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  562. {
  563. hcan->State = HAL_CAN_STATE_TIMEOUT;
  564. __HAL_CAN_CANCEL_TRANSMIT(hcan, transmitmailbox);
  565. /* Process unlocked */
  566. __HAL_UNLOCK(hcan);
  567. return HAL_TIMEOUT;
  568. }
  569. }
  570. }
  571. /* Change CAN state */
  572. switch(hcan->State)
  573. {
  574. case(HAL_CAN_STATE_BUSY_TX_RX0):
  575. hcan->State = HAL_CAN_STATE_BUSY_RX0;
  576. break;
  577. case(HAL_CAN_STATE_BUSY_TX_RX1):
  578. hcan->State = HAL_CAN_STATE_BUSY_RX1;
  579. break;
  580. case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
  581. hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
  582. break;
  583. default: /* HAL_CAN_STATE_BUSY_TX */
  584. hcan->State = HAL_CAN_STATE_READY;
  585. break;
  586. }
  587. /* Process unlocked */
  588. __HAL_UNLOCK(hcan);
  589. /* Return function status */
  590. return HAL_OK;
  591. }
  592. else
  593. {
  594. /* Change CAN state */
  595. hcan->State = HAL_CAN_STATE_ERROR;
  596. /* Return function status */
  597. return HAL_ERROR;
  598. }
  599. }
  600. /**
  601. * @brief Initiates and transmits a CAN frame message.
  602. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  603. * the configuration information for the specified CAN.
  604. * @retval HAL status
  605. */
  606. HAL_StatusTypeDef HAL_CAN_Transmit_IT(CAN_HandleTypeDef* hcan)
  607. {
  608. uint32_t transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
  609. /* Check the parameters */
  610. assert_param(IS_CAN_IDTYPE(hcan->pTxMsg->IDE));
  611. assert_param(IS_CAN_RTR(hcan->pTxMsg->RTR));
  612. assert_param(IS_CAN_DLC(hcan->pTxMsg->DLC));
  613. if(((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) || \
  614. ((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) || \
  615. ((hcan->Instance->TSR&CAN_TSR_TME2) == CAN_TSR_TME2))
  616. {
  617. /* Process Locked */
  618. __HAL_LOCK(hcan);
  619. /* Select one empty transmit mailbox */
  620. if((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
  621. {
  622. transmitmailbox = CAN_TXMAILBOX_0;
  623. }
  624. else if((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
  625. {
  626. transmitmailbox = CAN_TXMAILBOX_1;
  627. }
  628. else
  629. {
  630. transmitmailbox = CAN_TXMAILBOX_2;
  631. }
  632. /* Set up the Id */
  633. hcan->Instance->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ;
  634. if(hcan->pTxMsg->IDE == CAN_ID_STD)
  635. {
  636. assert_param(IS_CAN_STDID(hcan->pTxMsg->StdId));
  637. hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->StdId << 21U) | \
  638. hcan->pTxMsg->RTR);
  639. }
  640. else
  641. {
  642. assert_param(IS_CAN_EXTID(hcan->pTxMsg->ExtId));
  643. hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->ExtId << 3U) | \
  644. hcan->pTxMsg->IDE | \
  645. hcan->pTxMsg->RTR);
  646. }
  647. /* Set up the DLC */
  648. hcan->pTxMsg->DLC &= (uint8_t)0x0000000F;
  649. hcan->Instance->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0U;
  650. hcan->Instance->sTxMailBox[transmitmailbox].TDTR |= hcan->pTxMsg->DLC;
  651. /* Set up the data field */
  652. hcan->Instance->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)hcan->pTxMsg->Data[3U] << 24U) |
  653. ((uint32_t)hcan->pTxMsg->Data[2U] << 16U) |
  654. ((uint32_t)hcan->pTxMsg->Data[1U] << 8U) |
  655. ((uint32_t)hcan->pTxMsg->Data[0U]));
  656. hcan->Instance->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)hcan->pTxMsg->Data[7U] << 24U) |
  657. ((uint32_t)hcan->pTxMsg->Data[6U] << 16U) |
  658. ((uint32_t)hcan->pTxMsg->Data[5U] << 8U) |
  659. ((uint32_t)hcan->pTxMsg->Data[4U]));
  660. /* Change CAN state */
  661. switch(hcan->State)
  662. {
  663. case(HAL_CAN_STATE_BUSY_RX0):
  664. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
  665. break;
  666. case(HAL_CAN_STATE_BUSY_RX1):
  667. hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
  668. break;
  669. case(HAL_CAN_STATE_BUSY_RX0_RX1):
  670. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
  671. break;
  672. default: /* HAL_CAN_STATE_READY */
  673. hcan->State = HAL_CAN_STATE_BUSY_TX;
  674. break;
  675. }
  676. /* Set CAN error code to none */
  677. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  678. /* Process Unlocked */
  679. __HAL_UNLOCK(hcan);
  680. /* Request transmission */
  681. hcan->Instance->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
  682. /* Enable Error warning, Error passive, Bus-off,
  683. Last error and Error Interrupts */
  684. __HAL_CAN_ENABLE_IT(hcan, CAN_IT_EWG |
  685. CAN_IT_EPV |
  686. CAN_IT_BOF |
  687. CAN_IT_LEC |
  688. CAN_IT_ERR |
  689. CAN_IT_TME);
  690. }
  691. else
  692. {
  693. /* Change CAN state */
  694. hcan->State = HAL_CAN_STATE_ERROR;
  695. /* Return function status */
  696. return HAL_ERROR;
  697. }
  698. return HAL_OK;
  699. }
  700. /**
  701. * @brief Receives a correct CAN frame.
  702. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  703. * the configuration information for the specified CAN.
  704. * @param FIFONumber: FIFO Number value
  705. * @param Timeout: Specify Timeout value
  706. * @retval HAL status
  707. */
  708. HAL_StatusTypeDef HAL_CAN_Receive(CAN_HandleTypeDef* hcan, uint8_t FIFONumber, uint32_t Timeout)
  709. {
  710. uint32_t tickstart = 0U;
  711. CanRxMsgTypeDef* pRxMsg = NULL;
  712. /* Check the parameters */
  713. assert_param(IS_CAN_FIFO(FIFONumber));
  714. /* Check if CAN state is not busy for RX FIFO0 */
  715. if ((FIFONumber == CAN_FIFO0) && ((hcan->State == HAL_CAN_STATE_BUSY_RX0) || \
  716. (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0) || \
  717. (hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
  718. (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
  719. {
  720. return HAL_BUSY;
  721. }
  722. /* Check if CAN state is not busy for RX FIFO1 */
  723. if ((FIFONumber == CAN_FIFO1) && ((hcan->State == HAL_CAN_STATE_BUSY_RX1) || \
  724. (hcan->State == HAL_CAN_STATE_BUSY_TX_RX1) || \
  725. (hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
  726. (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
  727. {
  728. return HAL_BUSY;
  729. }
  730. /* Process locked */
  731. __HAL_LOCK(hcan);
  732. /* Change CAN state */
  733. if (FIFONumber == CAN_FIFO0)
  734. {
  735. switch(hcan->State)
  736. {
  737. case(HAL_CAN_STATE_BUSY_TX):
  738. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
  739. break;
  740. case(HAL_CAN_STATE_BUSY_RX1):
  741. hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
  742. break;
  743. case(HAL_CAN_STATE_BUSY_TX_RX1):
  744. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
  745. break;
  746. default: /* HAL_CAN_STATE_READY */
  747. hcan->State = HAL_CAN_STATE_BUSY_RX0;
  748. break;
  749. }
  750. }
  751. else /* FIFONumber == CAN_FIFO1 */
  752. {
  753. switch(hcan->State)
  754. {
  755. case(HAL_CAN_STATE_BUSY_TX):
  756. hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
  757. break;
  758. case(HAL_CAN_STATE_BUSY_RX0):
  759. hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
  760. break;
  761. case(HAL_CAN_STATE_BUSY_TX_RX0):
  762. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
  763. break;
  764. default: /* HAL_CAN_STATE_READY */
  765. hcan->State = HAL_CAN_STATE_BUSY_RX1;
  766. break;
  767. }
  768. }
  769. /* Get tick */
  770. tickstart = HAL_GetTick();
  771. /* Check pending message */
  772. while(__HAL_CAN_MSG_PENDING(hcan, FIFONumber) == 0U)
  773. {
  774. /* Check for the Timeout */
  775. if(Timeout != HAL_MAX_DELAY)
  776. {
  777. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  778. {
  779. hcan->State = HAL_CAN_STATE_TIMEOUT;
  780. /* Process unlocked */
  781. __HAL_UNLOCK(hcan);
  782. return HAL_TIMEOUT;
  783. }
  784. }
  785. }
  786. /* Set RxMsg pointer */
  787. if(FIFONumber == CAN_FIFO0)
  788. {
  789. pRxMsg = hcan->pRxMsg;
  790. }
  791. else /* FIFONumber == CAN_FIFO1 */
  792. {
  793. pRxMsg = hcan->pRx1Msg;
  794. }
  795. /* Get the Id */
  796. pRxMsg->IDE = (uint8_t)0x04 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR;
  797. if (pRxMsg->IDE == CAN_ID_STD)
  798. {
  799. pRxMsg->StdId = 0x000007FFU & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 21U);
  800. }
  801. else
  802. {
  803. pRxMsg->ExtId = 0x1FFFFFFFU & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 3U);
  804. }
  805. pRxMsg->RTR = (uint8_t)0x02 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR;
  806. /* Get the DLC */
  807. pRxMsg->DLC = (uint8_t)0x0F & hcan->Instance->sFIFOMailBox[FIFONumber].RDTR;
  808. /* Get the FMI */
  809. pRxMsg->FMI = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDTR >> 8U);
  810. /* Get the FIFONumber */
  811. pRxMsg->FIFONumber = FIFONumber;
  812. /* Get the data field */
  813. pRxMsg->Data[0] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDLR;
  814. pRxMsg->Data[1] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 8U);
  815. pRxMsg->Data[2] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 16U);
  816. pRxMsg->Data[3] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 24U);
  817. pRxMsg->Data[4] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDHR;
  818. pRxMsg->Data[5] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 8U);
  819. pRxMsg->Data[6] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 16U);
  820. pRxMsg->Data[7] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 24U);
  821. /* Release the FIFO */
  822. if(FIFONumber == CAN_FIFO0)
  823. {
  824. /* Release FIFO0 */
  825. __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO0);
  826. }
  827. else /* FIFONumber == CAN_FIFO1 */
  828. {
  829. /* Release FIFO1 */
  830. __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO1);
  831. }
  832. /* Change CAN state */
  833. if (FIFONumber == CAN_FIFO0)
  834. {
  835. switch(hcan->State)
  836. {
  837. case(HAL_CAN_STATE_BUSY_TX_RX0):
  838. hcan->State = HAL_CAN_STATE_BUSY_TX;
  839. break;
  840. case(HAL_CAN_STATE_BUSY_RX0_RX1):
  841. hcan->State = HAL_CAN_STATE_BUSY_RX1;
  842. break;
  843. case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
  844. hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
  845. break;
  846. default: /* HAL_CAN_STATE_BUSY_RX0 */
  847. hcan->State = HAL_CAN_STATE_READY;
  848. break;
  849. }
  850. }
  851. else /* FIFONumber == CAN_FIFO1 */
  852. {
  853. switch(hcan->State)
  854. {
  855. case(HAL_CAN_STATE_BUSY_TX_RX1):
  856. hcan->State = HAL_CAN_STATE_BUSY_TX;
  857. break;
  858. case(HAL_CAN_STATE_BUSY_RX0_RX1):
  859. hcan->State = HAL_CAN_STATE_BUSY_RX0;
  860. break;
  861. case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
  862. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
  863. break;
  864. default: /* HAL_CAN_STATE_BUSY_RX1 */
  865. hcan->State = HAL_CAN_STATE_READY;
  866. break;
  867. }
  868. }
  869. /* Process unlocked */
  870. __HAL_UNLOCK(hcan);
  871. /* Return function status */
  872. return HAL_OK;
  873. }
  874. /**
  875. * @brief Receives a correct CAN frame.
  876. * @param hcan: Pointer to a CAN_HandleTypeDef structure that contains
  877. * the configuration information for the specified CAN.
  878. * @param FIFONumber: Specify the FIFO number
  879. * @retval HAL status
  880. */
  881. HAL_StatusTypeDef HAL_CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber)
  882. {
  883. /* Check the parameters */
  884. assert_param(IS_CAN_FIFO(FIFONumber));
  885. /* Check if CAN state is not busy for RX FIFO0 */
  886. if((FIFONumber == CAN_FIFO0) && ((hcan->State == HAL_CAN_STATE_BUSY_RX0) || \
  887. (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0) || \
  888. (hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
  889. (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
  890. {
  891. return HAL_BUSY;
  892. }
  893. /* Check if CAN state is not busy for RX FIFO1 */
  894. if((FIFONumber == CAN_FIFO1) && ((hcan->State == HAL_CAN_STATE_BUSY_RX1) || \
  895. (hcan->State == HAL_CAN_STATE_BUSY_TX_RX1) || \
  896. (hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
  897. (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
  898. {
  899. return HAL_BUSY;
  900. }
  901. /* Process locked */
  902. __HAL_LOCK(hcan);
  903. /* Change CAN state */
  904. if(FIFONumber == CAN_FIFO0)
  905. {
  906. switch(hcan->State)
  907. {
  908. case(HAL_CAN_STATE_BUSY_TX):
  909. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
  910. break;
  911. case(HAL_CAN_STATE_BUSY_RX1):
  912. hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
  913. break;
  914. case(HAL_CAN_STATE_BUSY_TX_RX1):
  915. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
  916. break;
  917. default: /* HAL_CAN_STATE_READY */
  918. hcan->State = HAL_CAN_STATE_BUSY_RX0;
  919. break;
  920. }
  921. }
  922. else /* FIFONumber == CAN_FIFO1 */
  923. {
  924. switch(hcan->State)
  925. {
  926. case(HAL_CAN_STATE_BUSY_TX):
  927. hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
  928. break;
  929. case(HAL_CAN_STATE_BUSY_RX0):
  930. hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
  931. break;
  932. case(HAL_CAN_STATE_BUSY_TX_RX0):
  933. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
  934. break;
  935. default: /* HAL_CAN_STATE_READY */
  936. hcan->State = HAL_CAN_STATE_BUSY_RX1;
  937. break;
  938. }
  939. }
  940. /* Set CAN error code to none */
  941. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  942. /* Enable interrupts: */
  943. /* - Enable Error warning Interrupt */
  944. /* - Enable Error passive Interrupt */
  945. /* - Enable Bus-off Interrupt */
  946. /* - Enable Last error code Interrupt */
  947. /* - Enable Error Interrupt */
  948. /* - Enable Transmit mailbox empty Interrupt */
  949. __HAL_CAN_ENABLE_IT(hcan, CAN_IT_EWG |
  950. CAN_IT_EPV |
  951. CAN_IT_BOF |
  952. CAN_IT_LEC |
  953. CAN_IT_ERR |
  954. CAN_IT_TME);
  955. if(FIFONumber == CAN_FIFO0)
  956. {
  957. /* Enable FIFO 0 overrun and message pending Interrupt */
  958. __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FOV0 | CAN_IT_FMP0);
  959. }
  960. else
  961. {
  962. /* Enable FIFO 1 overrun and message pending Interrupt */
  963. __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FOV1 | CAN_IT_FMP1);
  964. }
  965. /* Return function status */
  966. return HAL_OK;
  967. }
  968. /**
  969. * @brief Enters the Sleep (low power) mode.
  970. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  971. * the configuration information for the specified CAN.
  972. * @retval HAL status.
  973. */
  974. HAL_StatusTypeDef HAL_CAN_Sleep(CAN_HandleTypeDef* hcan)
  975. {
  976. uint32_t tickstart = 0;
  977. /* Process locked */
  978. __HAL_LOCK(hcan);
  979. /* Change CAN state */
  980. hcan->State = HAL_CAN_STATE_BUSY;
  981. /* Request Sleep mode */
  982. hcan->Instance->MCR = (((hcan->Instance->MCR) & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
  983. /* Sleep mode status */
  984. if ((hcan->Instance->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) != CAN_MSR_SLAK)
  985. {
  986. /* Process unlocked */
  987. __HAL_UNLOCK(hcan);
  988. /* Return function status */
  989. return HAL_ERROR;
  990. }
  991. /* Get tick */
  992. tickstart = HAL_GetTick();
  993. /* Wait the acknowledge */
  994. while((hcan->Instance->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) != CAN_MSR_SLAK)
  995. {
  996. if((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  997. {
  998. hcan->State = HAL_CAN_STATE_TIMEOUT;
  999. /* Process unlocked */
  1000. __HAL_UNLOCK(hcan);
  1001. return HAL_TIMEOUT;
  1002. }
  1003. }
  1004. /* Change CAN state */
  1005. hcan->State = HAL_CAN_STATE_READY;
  1006. /* Process unlocked */
  1007. __HAL_UNLOCK(hcan);
  1008. /* Return function status */
  1009. return HAL_OK;
  1010. }
  1011. /**
  1012. * @brief Wakes up the CAN peripheral from sleep mode, after that the CAN peripheral
  1013. * is in the normal mode.
  1014. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  1015. * the configuration information for the specified CAN.
  1016. * @retval HAL status.
  1017. */
  1018. HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef* hcan)
  1019. {
  1020. uint32_t tickstart = 0;
  1021. /* Process locked */
  1022. __HAL_LOCK(hcan);
  1023. /* Change CAN state */
  1024. hcan->State = HAL_CAN_STATE_BUSY;
  1025. /* Wake up request */
  1026. hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_SLEEP;
  1027. /* Get tick */
  1028. tickstart = HAL_GetTick();
  1029. /* Sleep mode status */
  1030. while((hcan->Instance->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)
  1031. {
  1032. if((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  1033. {
  1034. hcan->State= HAL_CAN_STATE_TIMEOUT;
  1035. /* Process unlocked */
  1036. __HAL_UNLOCK(hcan);
  1037. return HAL_TIMEOUT;
  1038. }
  1039. }
  1040. if((hcan->Instance->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)
  1041. {
  1042. /* Process unlocked */
  1043. __HAL_UNLOCK(hcan);
  1044. /* Return function status */
  1045. return HAL_ERROR;
  1046. }
  1047. /* Change CAN state */
  1048. hcan->State = HAL_CAN_STATE_READY;
  1049. /* Process unlocked */
  1050. __HAL_UNLOCK(hcan);
  1051. /* Return function status */
  1052. return HAL_OK;
  1053. }
  1054. /**
  1055. * @brief Handles CAN interrupt request
  1056. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  1057. * the configuration information for the specified CAN.
  1058. * @retval None
  1059. */
  1060. void HAL_CAN_IRQHandler(CAN_HandleTypeDef* hcan)
  1061. {
  1062. uint32_t tmp1 = 0U, tmp2 = 0U, tmp3 = 0U;
  1063. uint32_t errorcode = HAL_CAN_ERROR_NONE;
  1064. /* Check Overrun flag for FIFO0 */
  1065. tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_FOV0);
  1066. tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FOV0);
  1067. if(tmp1 && tmp2)
  1068. {
  1069. /* Set CAN error code to FOV0 error */
  1070. errorcode |= HAL_CAN_ERROR_FOV0;
  1071. /* Clear FIFO0 Overrun Flag */
  1072. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
  1073. }
  1074. /* Check Overrun flag for FIFO1 */
  1075. tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_FOV1);
  1076. tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FOV1);
  1077. if(tmp1 && tmp2)
  1078. {
  1079. /* Set CAN error code to FOV1 error */
  1080. errorcode |= HAL_CAN_ERROR_FOV1;
  1081. /* Clear FIFO1 Overrun Flag */
  1082. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV1);
  1083. }
  1084. /* Check End of transmission flag */
  1085. if(__HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_TME))
  1086. {
  1087. tmp1 = __HAL_CAN_TRANSMIT_STATUS(hcan, CAN_TXMAILBOX_0);
  1088. tmp2 = __HAL_CAN_TRANSMIT_STATUS(hcan, CAN_TXMAILBOX_1);
  1089. tmp3 = __HAL_CAN_TRANSMIT_STATUS(hcan, CAN_TXMAILBOX_2);
  1090. if(tmp1 || tmp2 || tmp3)
  1091. {
  1092. tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK0);
  1093. tmp2 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK1);
  1094. tmp3 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK2);
  1095. /* Check Transmit success */
  1096. if(tmp1 || tmp2 || tmp3)
  1097. {
  1098. /* Call transmit function */
  1099. CAN_Transmit_IT(hcan);
  1100. }
  1101. else /* Transmit failure */
  1102. {
  1103. /* Set CAN error code to TXFAIL error */
  1104. errorcode |= HAL_CAN_ERROR_TXFAIL;
  1105. }
  1106. /* Clear transmission status flags (RQCPx and TXOKx) */
  1107. SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP0 | CAN_TSR_RQCP1 | CAN_TSR_RQCP2 | \
  1108. CAN_FLAG_TXOK0 | CAN_FLAG_TXOK1 | CAN_FLAG_TXOK2);
  1109. }
  1110. }
  1111. tmp1 = __HAL_CAN_MSG_PENDING(hcan, CAN_FIFO0);
  1112. tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FMP0);
  1113. /* Check End of reception flag for FIFO0 */
  1114. if((tmp1 != 0U) && tmp2)
  1115. {
  1116. /* Call receive function */
  1117. CAN_Receive_IT(hcan, CAN_FIFO0);
  1118. }
  1119. tmp1 = __HAL_CAN_MSG_PENDING(hcan, CAN_FIFO1);
  1120. tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FMP1);
  1121. /* Check End of reception flag for FIFO1 */
  1122. if((tmp1 != 0U) && tmp2)
  1123. {
  1124. /* Call receive function */
  1125. CAN_Receive_IT(hcan, CAN_FIFO1);
  1126. }
  1127. /* Set error code in handle */
  1128. hcan->ErrorCode |= errorcode;
  1129. tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_EWG);
  1130. tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_EWG);
  1131. tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR);
  1132. /* Check Error Warning Flag */
  1133. if(tmp1 && tmp2 && tmp3)
  1134. {
  1135. /* Set CAN error code to EWG error */
  1136. hcan->ErrorCode |= HAL_CAN_ERROR_EWG;
  1137. }
  1138. tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_EPV);
  1139. tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_EPV);
  1140. tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR);
  1141. /* Check Error Passive Flag */
  1142. if(tmp1 && tmp2 && tmp3)
  1143. {
  1144. /* Set CAN error code to EPV error */
  1145. hcan->ErrorCode |= HAL_CAN_ERROR_EPV;
  1146. }
  1147. tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_BOF);
  1148. tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_BOF);
  1149. tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR);
  1150. /* Check Bus-Off Flag */
  1151. if(tmp1 && tmp2 && tmp3)
  1152. {
  1153. /* Set CAN error code to BOF error */
  1154. hcan->ErrorCode |= HAL_CAN_ERROR_BOF;
  1155. }
  1156. tmp1 = HAL_IS_BIT_CLR(hcan->Instance->ESR, CAN_ESR_LEC);
  1157. tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_LEC);
  1158. tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR);
  1159. /* Check Last error code Flag */
  1160. if((!tmp1) && tmp2 && tmp3)
  1161. {
  1162. tmp1 = (hcan->Instance->ESR) & CAN_ESR_LEC;
  1163. switch(tmp1)
  1164. {
  1165. case(CAN_ESR_LEC_0):
  1166. /* Set CAN error code to STF error */
  1167. hcan->ErrorCode |= HAL_CAN_ERROR_STF;
  1168. break;
  1169. case(CAN_ESR_LEC_1):
  1170. /* Set CAN error code to FOR error */
  1171. hcan->ErrorCode |= HAL_CAN_ERROR_FOR;
  1172. break;
  1173. case(CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
  1174. /* Set CAN error code to ACK error */
  1175. hcan->ErrorCode |= HAL_CAN_ERROR_ACK;
  1176. break;
  1177. case(CAN_ESR_LEC_2):
  1178. /* Set CAN error code to BR error */
  1179. hcan->ErrorCode |= HAL_CAN_ERROR_BR;
  1180. break;
  1181. case(CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
  1182. /* Set CAN error code to BD error */
  1183. hcan->ErrorCode |= HAL_CAN_ERROR_BD;
  1184. break;
  1185. case(CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
  1186. /* Set CAN error code to CRC error */
  1187. hcan->ErrorCode |= HAL_CAN_ERROR_CRC;
  1188. break;
  1189. default:
  1190. break;
  1191. }
  1192. /* Clear Last error code Flag */
  1193. hcan->Instance->ESR &= ~(CAN_ESR_LEC);
  1194. }
  1195. /* Call the Error call Back in case of Errors */
  1196. if(hcan->ErrorCode != HAL_CAN_ERROR_NONE)
  1197. {
  1198. /* Clear ERRI Flag */
  1199. hcan->Instance->MSR = CAN_MSR_ERRI;
  1200. /* Set the CAN state ready to be able to start again the process */
  1201. hcan->State = HAL_CAN_STATE_READY;
  1202. /* Disable interrupts: */
  1203. /* - Disable Error warning Interrupt */
  1204. /* - Disable Error passive Interrupt */
  1205. /* - Disable Bus-off Interrupt */
  1206. /* - Disable Last error code Interrupt */
  1207. /* - Disable Error Interrupt */
  1208. /* - Disable FIFO 0 message pending Interrupt */
  1209. /* - Disable FIFO 0 Overrun Interrupt */
  1210. /* - Disable FIFO 1 message pending Interrupt */
  1211. /* - Disable FIFO 1 Overrun Interrupt */
  1212. /* - Disable Transmit mailbox empty Interrupt */
  1213. __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EWG |
  1214. CAN_IT_EPV |
  1215. CAN_IT_BOF |
  1216. CAN_IT_LEC |
  1217. CAN_IT_ERR |
  1218. CAN_IT_FMP0|
  1219. CAN_IT_FOV0|
  1220. CAN_IT_FMP1|
  1221. CAN_IT_FOV1|
  1222. CAN_IT_TME);
  1223. /* Call Error callback function */
  1224. HAL_CAN_ErrorCallback(hcan);
  1225. }
  1226. }
  1227. /**
  1228. * @brief Transmission complete callback in non blocking mode
  1229. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  1230. * the configuration information for the specified CAN.
  1231. * @retval None
  1232. */
  1233. __weak void HAL_CAN_TxCpltCallback(CAN_HandleTypeDef* hcan)
  1234. {
  1235. /* Prevent unused argument(s) compilation warning */
  1236. UNUSED(hcan);
  1237. /* NOTE : This function Should not be modified, when the callback is needed,
  1238. the HAL_CAN_TxCpltCallback could be implemented in the user file
  1239. */
  1240. }
  1241. /**
  1242. * @brief Transmission complete callback in non blocking mode
  1243. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  1244. * the configuration information for the specified CAN.
  1245. * @retval None
  1246. */
  1247. __weak void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan)
  1248. {
  1249. /* Prevent unused argument(s) compilation warning */
  1250. UNUSED(hcan);
  1251. /* NOTE : This function Should not be modified, when the callback is needed,
  1252. the HAL_CAN_RxCpltCallback could be implemented in the user file
  1253. */
  1254. }
  1255. /**
  1256. * @brief Error CAN callback.
  1257. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  1258. * the configuration information for the specified CAN.
  1259. * @retval None
  1260. */
  1261. __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
  1262. {
  1263. /* Prevent unused argument(s) compilation warning */
  1264. UNUSED(hcan);
  1265. /* NOTE : This function Should not be modified, when the callback is needed,
  1266. the HAL_CAN_ErrorCallback could be implemented in the user file
  1267. */
  1268. }
  1269. /**
  1270. * @}
  1271. */
  1272. /** @defgroup CAN_Exported_Functions_Group3 Peripheral State and Error functions
  1273. * @brief CAN Peripheral State functions
  1274. *
  1275. @verbatim
  1276. ==============================================================================
  1277. ##### Peripheral State and Error functions #####
  1278. ==============================================================================
  1279. [..]
  1280. This subsection provides functions allowing to :
  1281. (+) Check the CAN state.
  1282. (+) Check CAN Errors detected during interrupt process
  1283. @endverbatim
  1284. * @{
  1285. */
  1286. /**
  1287. * @brief return the CAN state
  1288. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  1289. * the configuration information for the specified CAN.
  1290. * @retval HAL state
  1291. */
  1292. HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef* hcan)
  1293. {
  1294. /* Return CAN state */
  1295. return hcan->State;
  1296. }
  1297. /**
  1298. * @brief Return the CAN error code
  1299. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  1300. * the configuration information for the specified CAN.
  1301. * @retval CAN Error Code
  1302. */
  1303. uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan)
  1304. {
  1305. return hcan->ErrorCode;
  1306. }
  1307. /**
  1308. * @}
  1309. */
  1310. /**
  1311. * @brief Initiates and transmits a CAN frame message.
  1312. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
  1313. * the configuration information for the specified CAN.
  1314. * @retval HAL status
  1315. */
  1316. static HAL_StatusTypeDef CAN_Transmit_IT(CAN_HandleTypeDef* hcan)
  1317. {
  1318. /* Disable Transmit mailbox empty Interrupt */
  1319. __HAL_CAN_DISABLE_IT(hcan, CAN_IT_TME);
  1320. if(hcan->State == HAL_CAN_STATE_BUSY_TX)
  1321. {
  1322. /* Disable Error warning, Error passive, Bus-off, Last error code
  1323. and Error Interrupts */
  1324. __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EWG |
  1325. CAN_IT_EPV |
  1326. CAN_IT_BOF |
  1327. CAN_IT_LEC |
  1328. CAN_IT_ERR );
  1329. }
  1330. /* Change CAN state */
  1331. switch(hcan->State)
  1332. {
  1333. case(HAL_CAN_STATE_BUSY_TX_RX0):
  1334. hcan->State = HAL_CAN_STATE_BUSY_RX0;
  1335. break;
  1336. case(HAL_CAN_STATE_BUSY_TX_RX1):
  1337. hcan->State = HAL_CAN_STATE_BUSY_RX1;
  1338. break;
  1339. case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
  1340. hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
  1341. break;
  1342. default: /* HAL_CAN_STATE_BUSY_TX */
  1343. hcan->State = HAL_CAN_STATE_READY;
  1344. break;
  1345. }
  1346. /* Transmission complete callback */
  1347. HAL_CAN_TxCpltCallback(hcan);
  1348. return HAL_OK;
  1349. }
  1350. /**
  1351. * @brief Receives a correct CAN frame.
  1352. * @param hcan: Pointer to a CAN_HandleTypeDef structure that contains
  1353. * the configuration information for the specified CAN.
  1354. * @param FIFONumber: Specify the FIFO number
  1355. * @retval HAL status
  1356. * @retval None
  1357. */
  1358. static HAL_StatusTypeDef CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber)
  1359. {
  1360. uint32_t tmp1 = 0U;
  1361. CanRxMsgTypeDef* pRxMsg = NULL;
  1362. /* Set RxMsg pointer */
  1363. if(FIFONumber == CAN_FIFO0)
  1364. {
  1365. pRxMsg = hcan->pRxMsg;
  1366. }
  1367. else /* FIFONumber == CAN_FIFO1 */
  1368. {
  1369. pRxMsg = hcan->pRx1Msg;
  1370. }
  1371. /* Get the Id */
  1372. pRxMsg->IDE = (uint8_t)0x04 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR;
  1373. if (pRxMsg->IDE == CAN_ID_STD)
  1374. {
  1375. pRxMsg->StdId = 0x000007FFU & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 21U);
  1376. }
  1377. else
  1378. {
  1379. pRxMsg->ExtId = 0x1FFFFFFFU & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 3U);
  1380. }
  1381. pRxMsg->RTR = (uint8_t)0x02 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR;
  1382. /* Get the DLC */
  1383. pRxMsg->DLC = (uint8_t)0x0F & hcan->Instance->sFIFOMailBox[FIFONumber].RDTR;
  1384. /* Get the FIFONumber */
  1385. pRxMsg->FIFONumber = FIFONumber;
  1386. /* Get the FMI */
  1387. pRxMsg->FMI = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDTR >> 8U);
  1388. /* Get the data field */
  1389. pRxMsg->Data[0] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDLR;
  1390. pRxMsg->Data[1] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 8U);
  1391. pRxMsg->Data[2] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 16U);
  1392. pRxMsg->Data[3] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 24U);
  1393. pRxMsg->Data[4] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDHR;
  1394. pRxMsg->Data[5] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 8U);
  1395. pRxMsg->Data[6] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 16U);
  1396. pRxMsg->Data[7] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 24U);
  1397. /* Release the FIFO */
  1398. /* Release FIFO0 */
  1399. if (FIFONumber == CAN_FIFO0)
  1400. {
  1401. __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO0);
  1402. /* Disable FIFO 0 overrun and message pending Interrupt */
  1403. __HAL_CAN_DISABLE_IT(hcan, CAN_IT_FOV0 | CAN_IT_FMP0);
  1404. }
  1405. /* Release FIFO1 */
  1406. else /* FIFONumber == CAN_FIFO1 */
  1407. {
  1408. __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO1);
  1409. /* Disable FIFO 1 overrun and message pending Interrupt */
  1410. __HAL_CAN_DISABLE_IT(hcan, CAN_IT_FOV1 | CAN_IT_FMP1);
  1411. }
  1412. tmp1 = hcan->State;
  1413. if((tmp1 == HAL_CAN_STATE_BUSY_RX0) || (tmp1 == HAL_CAN_STATE_BUSY_RX1))
  1414. {
  1415. /* Disable Error warning, Error passive, Bus-off, Last error code
  1416. and Error Interrupts */
  1417. __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EWG |
  1418. CAN_IT_EPV |
  1419. CAN_IT_BOF |
  1420. CAN_IT_LEC |
  1421. CAN_IT_ERR);
  1422. }
  1423. /* Change CAN state */
  1424. if (FIFONumber == CAN_FIFO0)
  1425. {
  1426. switch(hcan->State)
  1427. {
  1428. case(HAL_CAN_STATE_BUSY_TX_RX0):
  1429. hcan->State = HAL_CAN_STATE_BUSY_TX;
  1430. break;
  1431. case(HAL_CAN_STATE_BUSY_RX0_RX1):
  1432. hcan->State = HAL_CAN_STATE_BUSY_RX1;
  1433. break;
  1434. case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
  1435. hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
  1436. break;
  1437. default: /* HAL_CAN_STATE_BUSY_RX0 */
  1438. hcan->State = HAL_CAN_STATE_READY;
  1439. break;
  1440. }
  1441. }
  1442. else /* FIFONumber == CAN_FIFO1 */
  1443. {
  1444. switch(hcan->State)
  1445. {
  1446. case(HAL_CAN_STATE_BUSY_TX_RX1):
  1447. hcan->State = HAL_CAN_STATE_BUSY_TX;
  1448. break;
  1449. case(HAL_CAN_STATE_BUSY_RX0_RX1):
  1450. hcan->State = HAL_CAN_STATE_BUSY_RX0;
  1451. break;
  1452. case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
  1453. hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
  1454. break;
  1455. default: /* HAL_CAN_STATE_BUSY_RX1 */
  1456. hcan->State = HAL_CAN_STATE_READY;
  1457. break;
  1458. }
  1459. }
  1460. /* Receive complete callback */
  1461. HAL_CAN_RxCpltCallback(hcan);
  1462. /* Return function status */
  1463. return HAL_OK;
  1464. }
  1465. /**
  1466. * @}
  1467. */
  1468. #endif /* HAL_CAN_MODULE_ENABLED */
  1469. /**
  1470. * @}
  1471. */
  1472. /**
  1473. * @}
  1474. */
  1475. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/