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.
 
 
 

1441 lines
44 KiB

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