stm32f4xx_hal_can.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_can.c
  4. * @author MCD Application Team
  5. * @brief CAN HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Controller Area Network (CAN) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Configuration functions
  10. * + Control functions
  11. * + Interrupts management
  12. * + Callbacks functions
  13. * + Peripheral State and Error functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. (#) Initialize the CAN low level resources by implementing the
  21. HAL_CAN_MspInit():
  22. (++) Enable the CAN interface clock using __HAL_RCC_CANx_CLK_ENABLE()
  23. (++) Configure CAN pins
  24. (+++) Enable the clock for the CAN GPIOs
  25. (+++) Configure CAN pins as alternate function open-drain
  26. (++) In case of using interrupts (e.g. HAL_CAN_ActivateNotification())
  27. (+++) Configure the CAN interrupt priority using
  28. HAL_NVIC_SetPriority()
  29. (+++) Enable the CAN IRQ handler using HAL_NVIC_EnableIRQ()
  30. (+++) In CAN IRQ handler, call HAL_CAN_IRQHandler()
  31. (#) Initialize the CAN peripheral using HAL_CAN_Init() function. This
  32. function resorts to HAL_CAN_MspInit() for low-level initialization.
  33. (#) Configure the reception filters using the following configuration
  34. functions:
  35. (++) HAL_CAN_ConfigFilter()
  36. (#) Start the CAN module using HAL_CAN_Start() function. At this level
  37. the node is active on the bus: it receive messages, and can send
  38. messages.
  39. (#) To manage messages transmission, the following Tx control functions
  40. can be used:
  41. (++) HAL_CAN_AddTxMessage() to request transmission of a new
  42. message.
  43. (++) HAL_CAN_AbortTxRequest() to abort transmission of a pending
  44. message.
  45. (++) HAL_CAN_GetTxMailboxesFreeLevel() to get the number of free Tx
  46. mailboxes.
  47. (++) HAL_CAN_IsTxMessagePending() to check if a message is pending
  48. in a Tx mailbox.
  49. (++) HAL_CAN_GetTxTimestamp() to get the timestamp of Tx message
  50. sent, if time triggered communication mode is enabled.
  51. (#) When a message is received into the CAN Rx FIFOs, it can be retrieved
  52. using the HAL_CAN_GetRxMessage() function. The function
  53. HAL_CAN_GetRxFifoFillLevel() allows to know how many Rx message are
  54. stored in the Rx Fifo.
  55. (#) Calling the HAL_CAN_Stop() function stops the CAN module.
  56. (#) The deinitialization is achieved with HAL_CAN_DeInit() function.
  57. *** Polling mode operation ***
  58. ==============================
  59. [..]
  60. (#) Reception:
  61. (++) Monitor reception of message using HAL_CAN_GetRxFifoFillLevel()
  62. until at least one message is received.
  63. (++) Then get the message using HAL_CAN_GetRxMessage().
  64. (#) Transmission:
  65. (++) Monitor the Tx mailboxes availability until at least one Tx
  66. mailbox is free, using HAL_CAN_GetTxMailboxesFreeLevel().
  67. (++) Then request transmission of a message using
  68. HAL_CAN_AddTxMessage().
  69. *** Interrupt mode operation ***
  70. ================================
  71. [..]
  72. (#) Notifications are activated using HAL_CAN_ActivateNotification()
  73. function. Then, the process can be controlled through the
  74. available user callbacks: HAL_CAN_xxxCallback(), using same APIs
  75. HAL_CAN_GetRxMessage() and HAL_CAN_AddTxMessage().
  76. (#) Notifications can be deactivated using
  77. HAL_CAN_DeactivateNotification() function.
  78. (#) Special care should be taken for CAN_IT_RX_FIFO0_MSG_PENDING and
  79. CAN_IT_RX_FIFO1_MSG_PENDING notifications. These notifications trig
  80. the callbacks HAL_CAN_RxFIFO0MsgPendingCallback() and
  81. HAL_CAN_RxFIFO1MsgPendingCallback(). User has two possible options
  82. here.
  83. (++) Directly get the Rx message in the callback, using
  84. HAL_CAN_GetRxMessage().
  85. (++) Or deactivate the notification in the callback without
  86. getting the Rx message. The Rx message can then be got later
  87. using HAL_CAN_GetRxMessage(). Once the Rx message have been
  88. read, the notification can be activated again.
  89. *** Sleep mode ***
  90. ==================
  91. [..]
  92. (#) The CAN peripheral can be put in sleep mode (low power), using
  93. HAL_CAN_RequestSleep(). The sleep mode will be entered as soon as the
  94. current CAN activity (transmission or reception of a CAN frame) will
  95. be completed.
  96. (#) A notification can be activated to be informed when the sleep mode
  97. will be entered.
  98. (#) It can be checked if the sleep mode is entered using
  99. HAL_CAN_IsSleepActive().
  100. Note that the CAN state (accessible from the API HAL_CAN_GetState())
  101. is HAL_CAN_STATE_SLEEP_PENDING as soon as the sleep mode request is
  102. submitted (the sleep mode is not yet entered), and become
  103. HAL_CAN_STATE_SLEEP_ACTIVE when the sleep mode is effective.
  104. (#) The wake-up from sleep mode can be trigged by two ways:
  105. (++) Using HAL_CAN_WakeUp(). When returning from this function,
  106. the sleep mode is exited (if return status is HAL_OK).
  107. (++) When a start of Rx CAN frame is detected by the CAN peripheral,
  108. if automatic wake up mode is enabled.
  109. @endverbatim
  110. ******************************************************************************
  111. * @attention
  112. *
  113. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  114. *
  115. * Redistribution and use in source and binary forms, with or without modification,
  116. * are permitted provided that the following conditions are met:
  117. * 1. Redistributions of source code must retain the above copyright notice,
  118. * this list of conditions and the following disclaimer.
  119. * 2. Redistributions in binary form must reproduce the above copyright notice,
  120. * this list of conditions and the following disclaimer in the documentation
  121. * and/or other materials provided with the distribution.
  122. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  123. * may be used to endorse or promote products derived from this software
  124. * without specific prior written permission.
  125. *
  126. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  127. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  128. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  129. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  130. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  131. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  132. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  133. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  134. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  135. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  136. *
  137. ******************************************************************************
  138. */
  139. /* Includes ------------------------------------------------------------------*/
  140. #include "stm32f4xx_hal.h"
  141. /** @addtogroup STM32F4xx_HAL_Driver
  142. * @{
  143. */
  144. #if defined(CAN1)
  145. /** @defgroup CAN CAN
  146. * @brief CAN driver modules
  147. * @{
  148. */
  149. #ifdef HAL_CAN_MODULE_ENABLED
  150. #ifdef HAL_CAN_LEGACY_MODULE_ENABLED
  151. #error "The CAN driver cannot be used with its legacy, Please enable only one CAN module at once"
  152. #endif
  153. /* Private typedef -----------------------------------------------------------*/
  154. /* Private define ------------------------------------------------------------*/
  155. /** @defgroup CAN_Private_Constants CAN Private Constants
  156. * @{
  157. */
  158. #define CAN_TIMEOUT_VALUE 10U
  159. /**
  160. * @}
  161. */
  162. /* Private macro -------------------------------------------------------------*/
  163. /* Private variables ---------------------------------------------------------*/
  164. /* Private function prototypes -----------------------------------------------*/
  165. /* Exported functions --------------------------------------------------------*/
  166. /** @defgroup CAN_Exported_Functions CAN Exported Functions
  167. * @{
  168. */
  169. /** @defgroup CAN_Exported_Functions_Group1 Initialization and de-initialization functions
  170. * @brief Initialization and Configuration functions
  171. *
  172. @verbatim
  173. ==============================================================================
  174. ##### Initialization and de-initialization functions #####
  175. ==============================================================================
  176. [..] This section provides functions allowing to:
  177. (+) HAL_CAN_Init : Initialize and configure the CAN.
  178. (+) HAL_CAN_DeInit : De-initialize the CAN.
  179. (+) HAL_CAN_MspInit : Initialize the CAN MSP.
  180. (+) HAL_CAN_MspDeInit : DeInitialize the CAN MSP.
  181. @endverbatim
  182. * @{
  183. */
  184. /**
  185. * @brief Initializes the CAN peripheral according to the specified
  186. * parameters in the CAN_InitStruct.
  187. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  188. * the configuration information for the specified CAN.
  189. * @retval HAL status
  190. */
  191. HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef *hcan)
  192. {
  193. uint32_t tickstart = 0U;
  194. /* Check CAN handle */
  195. if (hcan == NULL)
  196. {
  197. return HAL_ERROR;
  198. }
  199. /* Check the parameters */
  200. assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
  201. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TimeTriggeredMode));
  202. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoBusOff));
  203. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoWakeUp));
  204. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoRetransmission));
  205. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ReceiveFifoLocked));
  206. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TransmitFifoPriority));
  207. assert_param(IS_CAN_MODE(hcan->Init.Mode));
  208. assert_param(IS_CAN_SJW(hcan->Init.SyncJumpWidth));
  209. assert_param(IS_CAN_BS1(hcan->Init.TimeSeg1));
  210. assert_param(IS_CAN_BS2(hcan->Init.TimeSeg2));
  211. assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler));
  212. if (hcan->State == HAL_CAN_STATE_RESET)
  213. {
  214. /* Init the low level hardware: CLOCK, NVIC */
  215. HAL_CAN_MspInit(hcan);
  216. }
  217. /* Exit from sleep mode */
  218. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  219. /* Get tick */
  220. tickstart = HAL_GetTick();
  221. /* Check Sleep mode leave acknowledge */
  222. while ((hcan->Instance->MSR & CAN_MSR_SLAK) != RESET)
  223. {
  224. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  225. {
  226. /* Update error code */
  227. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  228. /* Change CAN state */
  229. hcan->State = HAL_CAN_STATE_ERROR;
  230. return HAL_ERROR;
  231. }
  232. }
  233. /* Request initialisation */
  234. SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
  235. /* Get tick */
  236. tickstart = HAL_GetTick();
  237. /* Wait initialisation acknowledge */
  238. while ((hcan->Instance->MSR & CAN_MSR_INAK) == RESET)
  239. {
  240. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  241. {
  242. /* Update error code */
  243. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  244. /* Change CAN state */
  245. hcan->State = HAL_CAN_STATE_ERROR;
  246. return HAL_ERROR;
  247. }
  248. }
  249. /* Set the time triggered communication mode */
  250. if (hcan->Init.TimeTriggeredMode == ENABLE)
  251. {
  252. SET_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
  253. }
  254. else
  255. {
  256. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
  257. }
  258. /* Set the automatic bus-off management */
  259. if (hcan->Init.AutoBusOff == ENABLE)
  260. {
  261. SET_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
  262. }
  263. else
  264. {
  265. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
  266. }
  267. /* Set the automatic wake-up mode */
  268. if (hcan->Init.AutoWakeUp == ENABLE)
  269. {
  270. SET_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
  271. }
  272. else
  273. {
  274. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
  275. }
  276. /* Set the automatic retransmission */
  277. if (hcan->Init.AutoRetransmission == ENABLE)
  278. {
  279. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_NART);
  280. }
  281. else
  282. {
  283. SET_BIT(hcan->Instance->MCR, CAN_MCR_NART);
  284. }
  285. /* Set the receive FIFO locked mode */
  286. if (hcan->Init.ReceiveFifoLocked == ENABLE)
  287. {
  288. SET_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
  289. }
  290. else
  291. {
  292. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
  293. }
  294. /* Set the transmit FIFO priority */
  295. if (hcan->Init.TransmitFifoPriority == ENABLE)
  296. {
  297. SET_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
  298. }
  299. else
  300. {
  301. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
  302. }
  303. /* Set the bit timing register */
  304. WRITE_REG(hcan->Instance->BTR, (uint32_t)(hcan->Init.Mode |
  305. hcan->Init.SyncJumpWidth |
  306. hcan->Init.TimeSeg1 |
  307. hcan->Init.TimeSeg2 |
  308. (hcan->Init.Prescaler - 1U)));
  309. /* Initialize the error code */
  310. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  311. /* Initialize the CAN state */
  312. hcan->State = HAL_CAN_STATE_READY;
  313. /* Return function status */
  314. return HAL_OK;
  315. }
  316. /**
  317. * @brief Deinitializes the CAN peripheral registers to their default
  318. * reset values.
  319. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  320. * the configuration information for the specified CAN.
  321. * @retval HAL status
  322. */
  323. HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef *hcan)
  324. {
  325. /* Check CAN handle */
  326. if (hcan == NULL)
  327. {
  328. return HAL_ERROR;
  329. }
  330. /* Check the parameters */
  331. assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
  332. /* Stop the CAN module */
  333. HAL_CAN_Stop(hcan);
  334. /* DeInit the low level hardware: CLOCK, NVIC */
  335. HAL_CAN_MspDeInit(hcan);
  336. /* Reset the CAN peripheral */
  337. SET_BIT(hcan->Instance->MCR, CAN_MCR_RESET);
  338. /* Reset the CAN ErrorCode */
  339. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  340. /* Change CAN state */
  341. hcan->State = HAL_CAN_STATE_RESET;
  342. /* Return function status */
  343. return HAL_OK;
  344. }
  345. /**
  346. * @brief Initializes the CAN MSP.
  347. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  348. * the configuration information for the specified CAN.
  349. * @retval None
  350. */
  351. __weak void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
  352. {
  353. /* Prevent unused argument(s) compilation warning */
  354. UNUSED(hcan);
  355. /* NOTE : This function Should not be modified, when the callback is needed,
  356. the HAL_CAN_MspInit could be implemented in the user file
  357. */
  358. }
  359. /**
  360. * @brief DeInitializes the CAN MSP.
  361. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  362. * the configuration information for the specified CAN.
  363. * @retval None
  364. */
  365. __weak void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
  366. {
  367. /* Prevent unused argument(s) compilation warning */
  368. UNUSED(hcan);
  369. /* NOTE : This function Should not be modified, when the callback is needed,
  370. the HAL_CAN_MspDeInit could be implemented in the user file
  371. */
  372. }
  373. /**
  374. * @}
  375. */
  376. /** @defgroup CAN_Exported_Functions_Group2 Configuration functions
  377. * @brief Configuration functions.
  378. *
  379. @verbatim
  380. ==============================================================================
  381. ##### Configuration functions #####
  382. ==============================================================================
  383. [..] This section provides functions allowing to:
  384. (+) HAL_CAN_ConfigFilter : Configure the CAN reception filters
  385. @endverbatim
  386. * @{
  387. */
  388. /**
  389. * @brief Configures the CAN reception filter according to the specified
  390. * parameters in the CAN_FilterInitStruct.
  391. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  392. * the configuration information for the specified CAN.
  393. * @param sFilterConfig pointer to a CAN_FilterTypeDef structure that
  394. * contains the filter configuration information.
  395. * @retval None
  396. */
  397. HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan, CAN_FilterTypeDef *sFilterConfig)
  398. {
  399. uint32_t filternbrbitpos = 0U;
  400. CAN_TypeDef *can_ip = hcan->Instance;
  401. if ((hcan->State == HAL_CAN_STATE_READY) ||
  402. (hcan->State == HAL_CAN_STATE_LISTENING))
  403. {
  404. /* Check the parameters */
  405. assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdHigh));
  406. assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdLow));
  407. assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdHigh));
  408. assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdLow));
  409. assert_param(IS_CAN_FILTER_MODE(sFilterConfig->FilterMode));
  410. assert_param(IS_CAN_FILTER_SCALE(sFilterConfig->FilterScale));
  411. assert_param(IS_CAN_FILTER_FIFO(sFilterConfig->FilterFIFOAssignment));
  412. assert_param(IS_FUNCTIONAL_STATE(sFilterConfig->FilterActivation));
  413. #if defined(CAN3)
  414. /* Check the CAN instance */
  415. if (hcan->Instance == CAN3)
  416. {
  417. /* CAN3 is single instance with 14 dedicated filters banks */
  418. /* Check the parameters */
  419. assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank));
  420. }
  421. else
  422. {
  423. /* CAN1 and CAN2 are dual instances with 28 common filters banks */
  424. /* Select master instance to access the filter banks */
  425. can_ip = CAN1;
  426. /* Check the parameters */
  427. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank));
  428. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank));
  429. }
  430. #elif defined(CAN2)
  431. /* CAN1 and CAN2 are dual instances with 28 common filters banks */
  432. /* Select master instance to access the filter banks */
  433. can_ip = CAN1;
  434. /* Check the parameters */
  435. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank));
  436. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank));
  437. #else
  438. /* CAN1 is single instance with 14 dedicated filters banks */
  439. /* Check the parameters */
  440. assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank));
  441. #endif
  442. /* Initialisation mode for the filter */
  443. SET_BIT(can_ip->FMR, CAN_FMR_FINIT);
  444. #if defined(CAN3)
  445. /* Check the CAN instance */
  446. if (can_ip == CAN1)
  447. {
  448. /* Select the start filter number of CAN2 slave instance */
  449. CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB);
  450. SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos);
  451. }
  452. #elif defined(CAN2)
  453. /* Select the start filter number of CAN2 slave instance */
  454. CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB);
  455. SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos);
  456. #endif
  457. /* Convert filter number into bit position */
  458. filternbrbitpos = (1U) << sFilterConfig->FilterBank;
  459. /* Filter Deactivation */
  460. CLEAR_BIT(can_ip->FA1R, filternbrbitpos);
  461. /* Filter Scale */
  462. if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT)
  463. {
  464. /* 16-bit scale for the filter */
  465. CLEAR_BIT(can_ip->FS1R, filternbrbitpos);
  466. /* First 16-bit identifier and First 16-bit mask */
  467. /* Or First 16-bit identifier and Second 16-bit identifier */
  468. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
  469. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16U) |
  470. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
  471. /* Second 16-bit identifier and Second 16-bit mask */
  472. /* Or Third 16-bit identifier and Fourth 16-bit identifier */
  473. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
  474. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
  475. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh);
  476. }
  477. if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT)
  478. {
  479. /* 32-bit scale for the filter */
  480. SET_BIT(can_ip->FS1R, filternbrbitpos);
  481. /* 32-bit identifier or First 32-bit identifier */
  482. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
  483. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh) << 16U) |
  484. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
  485. /* 32-bit mask or Second 32-bit identifier */
  486. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
  487. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
  488. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow);
  489. }
  490. /* Filter Mode */
  491. if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK)
  492. {
  493. /* Id/Mask mode for the filter*/
  494. CLEAR_BIT(can_ip->FM1R, filternbrbitpos);
  495. }
  496. else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
  497. {
  498. /* Identifier list mode for the filter*/
  499. SET_BIT(can_ip->FM1R, filternbrbitpos);
  500. }
  501. /* Filter FIFO assignment */
  502. if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0)
  503. {
  504. /* FIFO 0 assignation for the filter */
  505. CLEAR_BIT(can_ip->FFA1R, filternbrbitpos);
  506. }
  507. else
  508. {
  509. /* FIFO 1 assignation for the filter */
  510. SET_BIT(can_ip->FFA1R, filternbrbitpos);
  511. }
  512. /* Filter activation */
  513. if (sFilterConfig->FilterActivation == ENABLE)
  514. {
  515. SET_BIT(can_ip->FA1R, filternbrbitpos);
  516. }
  517. /* Leave the initialisation mode for the filter */
  518. CLEAR_BIT(can_ip->FMR, CAN_FMR_FINIT);
  519. /* Return function status */
  520. return HAL_OK;
  521. }
  522. else
  523. {
  524. /* Update error code */
  525. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  526. return HAL_ERROR;
  527. }
  528. }
  529. /**
  530. * @}
  531. */
  532. /** @defgroup CAN_Exported_Functions_Group3 Control functions
  533. * @brief Control functions
  534. *
  535. @verbatim
  536. ==============================================================================
  537. ##### Control functions #####
  538. ==============================================================================
  539. [..] This section provides functions allowing to:
  540. (+) HAL_CAN_Start : Start the CAN module
  541. (+) HAL_CAN_Stop : Stop the CAN module
  542. (+) HAL_CAN_RequestSleep : Request sleep mode entry.
  543. (+) HAL_CAN_WakeUp : Wake up from sleep mode.
  544. (+) HAL_CAN_IsSleepActive : Check is sleep mode is active.
  545. (+) HAL_CAN_AddTxMessage : Add a message to the Tx mailboxes
  546. and activate the corresponding
  547. transmission request
  548. (+) HAL_CAN_AbortTxRequest : Abort transmission request
  549. (+) HAL_CAN_GetTxMailboxesFreeLevel : Return Tx mailboxes free level
  550. (+) HAL_CAN_IsTxMessagePending : Check if a transmission request is
  551. pending on the selected Tx mailbox
  552. (+) HAL_CAN_GetRxMessage : Get a CAN frame from the Rx FIFO
  553. (+) HAL_CAN_GetRxFifoFillLevel : Return Rx FIFO fill level
  554. @endverbatim
  555. * @{
  556. */
  557. /**
  558. * @brief Start the CAN module.
  559. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  560. * the configuration information for the specified CAN.
  561. * @retval HAL status
  562. */
  563. HAL_StatusTypeDef HAL_CAN_Start(CAN_HandleTypeDef *hcan)
  564. {
  565. uint32_t tickstart = 0U;
  566. if (hcan->State == HAL_CAN_STATE_READY)
  567. {
  568. /* Change CAN peripheral state */
  569. hcan->State = HAL_CAN_STATE_LISTENING;
  570. /* Request leave initialisation */
  571. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
  572. /* Get tick */
  573. tickstart = HAL_GetTick();
  574. /* Wait the acknowledge */
  575. while ((hcan->Instance->MSR & CAN_MSR_INAK) != RESET)
  576. {
  577. /* Check for the Timeout */
  578. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  579. {
  580. /* Update error code */
  581. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  582. /* Change CAN state */
  583. hcan->State = HAL_CAN_STATE_ERROR;
  584. return HAL_ERROR;
  585. }
  586. }
  587. /* Reset the CAN ErrorCode */
  588. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  589. /* Return function status */
  590. return HAL_OK;
  591. }
  592. else
  593. {
  594. /* Update error code */
  595. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_READY;
  596. return HAL_ERROR;
  597. }
  598. }
  599. /**
  600. * @brief Stop the CAN module and enable access to configuration registers.
  601. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  602. * the configuration information for the specified CAN.
  603. * @retval HAL status
  604. */
  605. HAL_StatusTypeDef HAL_CAN_Stop(CAN_HandleTypeDef *hcan)
  606. {
  607. uint32_t tickstart = 0U;
  608. if (hcan->State == HAL_CAN_STATE_LISTENING)
  609. {
  610. /* Request initialisation */
  611. SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
  612. /* Get tick */
  613. tickstart = HAL_GetTick();
  614. /* Wait the acknowledge */
  615. while ((hcan->Instance->MSR & CAN_MSR_INAK) == RESET)
  616. {
  617. /* Check for the Timeout */
  618. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  619. {
  620. /* Update error code */
  621. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  622. /* Change CAN state */
  623. hcan->State = HAL_CAN_STATE_ERROR;
  624. return HAL_ERROR;
  625. }
  626. }
  627. /* Exit from sleep mode */
  628. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  629. /* Change CAN peripheral state */
  630. hcan->State = HAL_CAN_STATE_READY;
  631. /* Return function status */
  632. return HAL_OK;
  633. }
  634. else
  635. {
  636. /* Update error code */
  637. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_STARTED;
  638. return HAL_ERROR;
  639. }
  640. }
  641. /**
  642. * @brief Request the sleep mode (low power) entry.
  643. * When returning from this function, Sleep mode will be entered
  644. * as soon as the current CAN activity (transmission or reception
  645. * of a CAN frame) has been completed.
  646. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  647. * the configuration information for the specified CAN.
  648. * @retval HAL status.
  649. */
  650. HAL_StatusTypeDef HAL_CAN_RequestSleep(CAN_HandleTypeDef *hcan)
  651. {
  652. if ((hcan->State == HAL_CAN_STATE_READY) ||
  653. (hcan->State == HAL_CAN_STATE_LISTENING))
  654. {
  655. /* Request Sleep mode */
  656. SET_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  657. /* Return function status */
  658. return HAL_OK;
  659. }
  660. else
  661. {
  662. /* Update error code */
  663. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  664. /* Return function status */
  665. return HAL_ERROR;
  666. }
  667. }
  668. /**
  669. * @brief Wake up from sleep mode.
  670. * When returning with HAL_OK status from this function, Sleep mode
  671. * is exited.
  672. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  673. * the configuration information for the specified CAN.
  674. * @retval HAL status.
  675. */
  676. HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan)
  677. {
  678. __IO uint32_t count = 0;
  679. uint32_t timeout = 1000000U;
  680. if ((hcan->State == HAL_CAN_STATE_READY) ||
  681. (hcan->State == HAL_CAN_STATE_LISTENING))
  682. {
  683. /* Wake up request */
  684. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  685. /* Wait sleep mode is exited */
  686. do
  687. {
  688. /* Check if timeout is reached */
  689. if (++count > timeout)
  690. {
  691. /* Update error code */
  692. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  693. return HAL_ERROR;
  694. }
  695. }
  696. while ((hcan->Instance->MSR & CAN_MSR_SLAK) != RESET);
  697. /* Return function status */
  698. return HAL_OK;
  699. }
  700. else
  701. {
  702. /* Update error code */
  703. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  704. return HAL_ERROR;
  705. }
  706. }
  707. /**
  708. * @brief Check is sleep mode is active.
  709. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  710. * the configuration information for the specified CAN.
  711. * @retval Status
  712. * - 0 : Sleep mode is not active.
  713. * - 1 : Sleep mode is active.
  714. */
  715. uint32_t HAL_CAN_IsSleepActive(CAN_HandleTypeDef *hcan)
  716. {
  717. uint32_t status = 0U;
  718. if ((hcan->State == HAL_CAN_STATE_READY) ||
  719. (hcan->State == HAL_CAN_STATE_LISTENING))
  720. {
  721. /* Check Sleep mode */
  722. if ((hcan->Instance->MSR & CAN_MSR_SLAK) != RESET)
  723. {
  724. status = 1U;
  725. }
  726. }
  727. /* Return function status */
  728. return status;
  729. }
  730. /**
  731. * @brief Add a message to the first free Tx mailbox and activate the
  732. * corresponding transmission request.
  733. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  734. * the configuration information for the specified CAN.
  735. * @param pHeader pointer to a CAN_TxHeaderTypeDef structure.
  736. * @param aData array containing the payload of the Tx frame.
  737. * @param pTxMailbox pointer to a variable where the function will return
  738. * the TxMailbox used to store the Tx message.
  739. * This parameter can be a value of @arg CAN_Tx_Mailboxes.
  740. * @retval HAL status
  741. */
  742. HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderTypeDef *pHeader, uint8_t aData[], uint32_t *pTxMailbox)
  743. {
  744. uint32_t transmitmailbox;
  745. /* Check the parameters */
  746. assert_param(IS_CAN_IDTYPE(pHeader->IDE));
  747. assert_param(IS_CAN_RTR(pHeader->RTR));
  748. assert_param(IS_CAN_DLC(pHeader->DLC));
  749. if (pHeader->IDE == CAN_ID_STD)
  750. {
  751. assert_param(IS_CAN_STDID(pHeader->StdId));
  752. }
  753. else
  754. {
  755. assert_param(IS_CAN_EXTID(pHeader->ExtId));
  756. }
  757. assert_param(IS_FUNCTIONAL_STATE(pHeader->TransmitGlobalTime));
  758. if ((hcan->State == HAL_CAN_STATE_READY) ||
  759. (hcan->State == HAL_CAN_STATE_LISTENING))
  760. {
  761. /* Check that all the Tx mailboxes are not full */
  762. if (((hcan->Instance->TSR & CAN_TSR_TME0) != RESET) ||
  763. ((hcan->Instance->TSR & CAN_TSR_TME1) != RESET) ||
  764. ((hcan->Instance->TSR & CAN_TSR_TME2) != RESET))
  765. {
  766. /* Select an empty transmit mailbox */
  767. transmitmailbox = (hcan->Instance->TSR & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;
  768. /* Store the Tx mailbox */
  769. *pTxMailbox = 1U << transmitmailbox;
  770. /* Set up the Id */
  771. if (pHeader->IDE == CAN_ID_STD)
  772. {
  773. hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) |
  774. pHeader->RTR);
  775. }
  776. else
  777. {
  778. hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->ExtId << CAN_TI0R_EXID_Pos) |
  779. pHeader->IDE |
  780. pHeader->RTR);
  781. }
  782. /* Set up the DLC */
  783. hcan->Instance->sTxMailBox[transmitmailbox].TDTR = (pHeader->DLC);
  784. /* Set up the Transmit Global Time mode */
  785. if (pHeader->TransmitGlobalTime == ENABLE)
  786. {
  787. SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TDTR, CAN_TDT0R_TGT);
  788. }
  789. /* Set up the data field */
  790. WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDHR,
  791. ((uint32_t)aData[7] << CAN_TDH0R_DATA7_Pos) |
  792. ((uint32_t)aData[6] << CAN_TDH0R_DATA6_Pos) |
  793. ((uint32_t)aData[5] << CAN_TDH0R_DATA5_Pos) |
  794. ((uint32_t)aData[4] << CAN_TDH0R_DATA4_Pos));
  795. WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDLR,
  796. ((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) |
  797. ((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) |
  798. ((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) |
  799. ((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos));
  800. /* Request transmission */
  801. SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TIR, CAN_TI0R_TXRQ);
  802. /* Return function status */
  803. return HAL_OK;
  804. }
  805. else
  806. {
  807. /* Update error code */
  808. hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
  809. return HAL_ERROR;
  810. }
  811. }
  812. else
  813. {
  814. /* Update error code */
  815. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  816. return HAL_ERROR;
  817. }
  818. }
  819. /**
  820. * @brief Abort transmission requests
  821. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  822. * the configuration information for the specified CAN.
  823. * @param TxMailboxes List of the Tx Mailboxes to abort.
  824. * This parameter can be any combination of @arg CAN_Tx_Mailboxes.
  825. * @retval HAL status
  826. */
  827. HAL_StatusTypeDef HAL_CAN_AbortTxRequest(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
  828. {
  829. /* Check function parameters */
  830. assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
  831. if ((hcan->State == HAL_CAN_STATE_READY) ||
  832. (hcan->State == HAL_CAN_STATE_LISTENING))
  833. {
  834. /* Check Tx Mailbox 0 */
  835. if ((TxMailboxes & CAN_TX_MAILBOX0) != RESET)
  836. {
  837. /* Add cancellation request for Tx Mailbox 0 */
  838. SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ0);
  839. }
  840. /* Check Tx Mailbox 1 */
  841. if ((TxMailboxes & CAN_TX_MAILBOX1) != RESET)
  842. {
  843. /* Add cancellation request for Tx Mailbox 1 */
  844. SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ1);
  845. }
  846. /* Check Tx Mailbox 2 */
  847. if ((TxMailboxes & CAN_TX_MAILBOX2) != RESET)
  848. {
  849. /* Add cancellation request for Tx Mailbox 2 */
  850. SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ2);
  851. }
  852. /* Return function status */
  853. return HAL_OK;
  854. }
  855. else
  856. {
  857. /* Update error code */
  858. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  859. return HAL_ERROR;
  860. }
  861. }
  862. /**
  863. * @brief Return Tx Mailboxes free level: number of free Tx Mailboxes.
  864. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  865. * the configuration information for the specified CAN.
  866. * @retval Number of free Tx Mailboxes.
  867. */
  868. uint32_t HAL_CAN_GetTxMailboxesFreeLevel(CAN_HandleTypeDef *hcan)
  869. {
  870. uint32_t freelevel = 0U;
  871. if ((hcan->State == HAL_CAN_STATE_READY) ||
  872. (hcan->State == HAL_CAN_STATE_LISTENING))
  873. {
  874. /* Check Tx Mailbox 0 status */
  875. if ((hcan->Instance->TSR & CAN_TSR_TME0) != RESET)
  876. {
  877. freelevel++;
  878. }
  879. /* Check Tx Mailbox 1 status */
  880. if ((hcan->Instance->TSR & CAN_TSR_TME1) != RESET)
  881. {
  882. freelevel++;
  883. }
  884. /* Check Tx Mailbox 2 status */
  885. if ((hcan->Instance->TSR & CAN_TSR_TME2) != RESET)
  886. {
  887. freelevel++;
  888. }
  889. }
  890. /* Return Tx Mailboxes free level */
  891. return freelevel;
  892. }
  893. /**
  894. * @brief Check if a transmission request is pending on the selected Tx
  895. * Mailboxes.
  896. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  897. * the configuration information for the specified CAN.
  898. * @param TxMailboxes List of Tx Mailboxes to check.
  899. * This parameter can be any combination of @arg CAN_Tx_Mailboxes.
  900. * @retval Status
  901. * - 0 : No pending transmission request on any selected Tx Mailboxes.
  902. * - 1 : Pending transmission request on at least one of the selected
  903. * Tx Mailbox.
  904. */
  905. uint32_t HAL_CAN_IsTxMessagePending(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
  906. {
  907. uint32_t status = 0U;
  908. /* Check function parameters */
  909. assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
  910. if ((hcan->State == HAL_CAN_STATE_READY) ||
  911. (hcan->State == HAL_CAN_STATE_LISTENING))
  912. {
  913. /* Check pending transmission request on the selected Tx Mailboxes */
  914. if ((hcan->Instance->TSR & (TxMailboxes << CAN_TSR_TME0_Pos)) != (TxMailboxes << CAN_TSR_TME0_Pos))
  915. {
  916. status = 1U;
  917. }
  918. }
  919. /* Return status */
  920. return status;
  921. }
  922. /**
  923. * @brief Return timestamp of Tx message sent, if time triggered communication
  924. mode is enabled.
  925. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  926. * the configuration information for the specified CAN.
  927. * @param TxMailbox Tx Mailbox where the timestamp of message sent will be
  928. * read.
  929. * This parameter can be one value of @arg CAN_Tx_Mailboxes.
  930. * @retval Timestamp of message sent from Tx Mailbox.
  931. */
  932. uint32_t HAL_CAN_GetTxTimestamp(CAN_HandleTypeDef *hcan, uint32_t TxMailbox)
  933. {
  934. uint32_t timestamp = 0U;
  935. uint32_t transmitmailbox;
  936. /* Check function parameters */
  937. assert_param(IS_CAN_TX_MAILBOX(TxMailbox));
  938. if ((hcan->State == HAL_CAN_STATE_READY) ||
  939. (hcan->State == HAL_CAN_STATE_LISTENING))
  940. {
  941. /* Select the Tx mailbox */
  942. transmitmailbox = POSITION_VAL(TxMailbox);
  943. /* Get timestamp */
  944. timestamp = (hcan->Instance->sTxMailBox[transmitmailbox].TDTR & CAN_TDT0R_TIME) >> CAN_TDT0R_TIME_Pos;
  945. }
  946. /* Return the timestamp */
  947. return timestamp;
  948. }
  949. /**
  950. * @brief Get an CAN frame from the Rx FIFO zone into the message RAM.
  951. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  952. * the configuration information for the specified CAN.
  953. * @param RxFifo Fifo number of the received message to be read.
  954. * This parameter can be a value of @arg CAN_receive_FIFO_number.
  955. * @param pHeader pointer to a CAN_RxHeaderTypeDef structure where the header
  956. * of the Rx frame will be stored.
  957. * @param aData array where the payload of the Rx frame will be stored.
  958. * @retval HAL status
  959. */
  960. HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo, CAN_RxHeaderTypeDef *pHeader, uint8_t aData[])
  961. {
  962. assert_param(IS_CAN_RX_FIFO(RxFifo));
  963. if ((hcan->State == HAL_CAN_STATE_READY) ||
  964. (hcan->State == HAL_CAN_STATE_LISTENING))
  965. {
  966. /* Check the Rx FIFO */
  967. if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
  968. {
  969. /* Check that the Rx FIFO 0 is not empty */
  970. if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) == RESET)
  971. {
  972. /* Update error code */
  973. hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
  974. return HAL_ERROR;
  975. }
  976. }
  977. else if (RxFifo == CAN_RX_FIFO1) /* Rx element is assigned to Rx FIFO 1 */
  978. {
  979. /* Check that the Rx FIFO 1 is not empty */
  980. if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) == RESET)
  981. {
  982. /* Update error code */
  983. hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
  984. return HAL_ERROR;
  985. }
  986. }
  987. /* Get the header */
  988. pHeader->IDE = CAN_RI0R_IDE & hcan->Instance->sFIFOMailBox[RxFifo].RIR;
  989. if (pHeader->IDE == CAN_ID_STD)
  990. {
  991. pHeader->StdId = (CAN_RI0R_STID & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_TI0R_STID_Pos;
  992. }
  993. else
  994. {
  995. pHeader->ExtId = ((CAN_RI0R_EXID | CAN_RI0R_STID) & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_EXID_Pos;
  996. }
  997. pHeader->RTR = (CAN_RI0R_RTR & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_RTR_Pos;
  998. pHeader->DLC = (CAN_RDT0R_DLC & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos;
  999. pHeader->FilterMatchIndex = (CAN_RDT0R_FMI & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_FMI_Pos;
  1000. pHeader->Timestamp = (CAN_RDT0R_TIME & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_TIME_Pos;
  1001. /* Get the data */
  1002. aData[0] = (CAN_RDL0R_DATA0 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA0_Pos;
  1003. aData[1] = (CAN_RDL0R_DATA1 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA1_Pos;
  1004. aData[2] = (CAN_RDL0R_DATA2 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA2_Pos;
  1005. aData[3] = (CAN_RDL0R_DATA3 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA3_Pos;
  1006. aData[4] = (CAN_RDH0R_DATA4 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA4_Pos;
  1007. aData[5] = (CAN_RDH0R_DATA5 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA5_Pos;
  1008. aData[6] = (CAN_RDH0R_DATA6 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA6_Pos;
  1009. aData[7] = (CAN_RDH0R_DATA7 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA7_Pos;
  1010. /* Release the FIFO */
  1011. if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
  1012. {
  1013. /* Release RX FIFO 0 */
  1014. SET_BIT(hcan->Instance->RF0R, CAN_RF0R_RFOM0);
  1015. }
  1016. else if (RxFifo == CAN_RX_FIFO1) /* Rx element is assigned to Rx FIFO 1 */
  1017. {
  1018. /* Release RX FIFO 1 */
  1019. SET_BIT(hcan->Instance->RF1R, CAN_RF1R_RFOM1);
  1020. }
  1021. /* Return function status */
  1022. return HAL_OK;
  1023. }
  1024. else
  1025. {
  1026. /* Update error code */
  1027. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1028. return HAL_ERROR;
  1029. }
  1030. }
  1031. /**
  1032. * @brief Return Rx FIFO fill level.
  1033. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1034. * the configuration information for the specified CAN.
  1035. * @param RxFifo Rx FIFO.
  1036. * This parameter can be a value of @arg CAN_receive_FIFO_number.
  1037. * @retval Number of messages available in Rx FIFO.
  1038. */
  1039. uint32_t HAL_CAN_GetRxFifoFillLevel(CAN_HandleTypeDef *hcan, uint32_t RxFifo)
  1040. {
  1041. uint32_t filllevel = 0U;
  1042. /* Check function parameters */
  1043. assert_param(IS_CAN_RX_FIFO(RxFifo));
  1044. if ((hcan->State == HAL_CAN_STATE_READY) ||
  1045. (hcan->State == HAL_CAN_STATE_LISTENING))
  1046. {
  1047. if (RxFifo == CAN_RX_FIFO0)
  1048. {
  1049. filllevel = hcan->Instance->RF0R & CAN_RF0R_FMP0;
  1050. }
  1051. else /* RxFifo == CAN_RX_FIFO1 */
  1052. {
  1053. filllevel = hcan->Instance->RF1R & CAN_RF1R_FMP1;
  1054. }
  1055. }
  1056. /* Return Rx FIFO fill level */
  1057. return filllevel;
  1058. }
  1059. /**
  1060. * @}
  1061. */
  1062. /** @defgroup CAN_Exported_Functions_Group4 Interrupts management
  1063. * @brief Interrupts management
  1064. *
  1065. @verbatim
  1066. ==============================================================================
  1067. ##### Interrupts management #####
  1068. ==============================================================================
  1069. [..] This section provides functions allowing to:
  1070. (+) HAL_CAN_ActivateNotification : Enable interrupts
  1071. (+) HAL_CAN_DeactivateNotification : Disable interrupts
  1072. (+) HAL_CAN_IRQHandler : Handles CAN interrupt request
  1073. @endverbatim
  1074. * @{
  1075. */
  1076. /**
  1077. * @brief Enable interrupts.
  1078. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1079. * the configuration information for the specified CAN.
  1080. * @param ActiveITs indicates which interrupts will be enabled.
  1081. * This parameter can be any combination of @arg CAN_Interrupts.
  1082. * @retval HAL status
  1083. */
  1084. HAL_StatusTypeDef HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t ActiveITs)
  1085. {
  1086. /* Check function parameters */
  1087. assert_param(IS_CAN_IT(ActiveITs));
  1088. if ((hcan->State == HAL_CAN_STATE_READY) ||
  1089. (hcan->State == HAL_CAN_STATE_LISTENING))
  1090. {
  1091. /* Enable the selected interrupts */
  1092. __HAL_CAN_ENABLE_IT(hcan, ActiveITs);
  1093. /* Return function status */
  1094. return HAL_OK;
  1095. }
  1096. else
  1097. {
  1098. /* Update error code */
  1099. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1100. return HAL_ERROR;
  1101. }
  1102. }
  1103. /**
  1104. * @brief Disable interrupts.
  1105. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1106. * the configuration information for the specified CAN.
  1107. * @param InactiveITs indicates which interrupts will be disabled.
  1108. * This parameter can be any combination of @arg CAN_Interrupts.
  1109. * @retval HAL status
  1110. */
  1111. HAL_StatusTypeDef HAL_CAN_DeactivateNotification(CAN_HandleTypeDef *hcan, uint32_t InactiveITs)
  1112. {
  1113. /* Check function parameters */
  1114. assert_param(IS_CAN_IT(InactiveITs));
  1115. if ((hcan->State == HAL_CAN_STATE_READY) ||
  1116. (hcan->State == HAL_CAN_STATE_LISTENING))
  1117. {
  1118. /* Disable the selected interrupts */
  1119. __HAL_CAN_DISABLE_IT(hcan, InactiveITs);
  1120. /* Return function status */
  1121. return HAL_OK;
  1122. }
  1123. else
  1124. {
  1125. /* Update error code */
  1126. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1127. return HAL_ERROR;
  1128. }
  1129. }
  1130. /**
  1131. * @brief Handles CAN interrupt request
  1132. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1133. * the configuration information for the specified CAN.
  1134. * @retval None
  1135. */
  1136. void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan)
  1137. {
  1138. uint32_t errorcode = HAL_CAN_ERROR_NONE;
  1139. uint32_t interrupts = READ_REG(hcan->Instance->IER);
  1140. uint32_t msrflags = READ_REG(hcan->Instance->MSR);
  1141. uint32_t tsrflags = READ_REG(hcan->Instance->TSR);
  1142. uint32_t rf0rflags = READ_REG(hcan->Instance->RF0R);
  1143. uint32_t rf1rflags = READ_REG(hcan->Instance->RF1R);
  1144. uint32_t esrflags = READ_REG(hcan->Instance->ESR);
  1145. /* Transmit Mailbox empty interrupt management *****************************/
  1146. if ((interrupts & CAN_IT_TX_MAILBOX_EMPTY) != RESET)
  1147. {
  1148. /* Transmit Mailbox 0 management *****************************************/
  1149. if ((tsrflags & CAN_TSR_RQCP0) != RESET)
  1150. {
  1151. /* Clear the Transmission Complete flag (and TXOK0,ALST0,TERR0 bits) */
  1152. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP0);
  1153. if ((tsrflags & CAN_TSR_TXOK0) != RESET)
  1154. {
  1155. /* Transmission Mailbox 0 complete callback */
  1156. /* Call weak (surcharged) callback */
  1157. HAL_CAN_TxMailbox0CompleteCallback(hcan);
  1158. }
  1159. else
  1160. {
  1161. if ((tsrflags & CAN_TSR_ALST0) != RESET)
  1162. {
  1163. /* Update error code */
  1164. errorcode |= HAL_CAN_ERROR_TX_ALST0;
  1165. }
  1166. else if ((tsrflags & CAN_TSR_TERR0) != RESET)
  1167. {
  1168. /* Update error code */
  1169. errorcode |= HAL_CAN_ERROR_TX_TERR0;
  1170. }
  1171. else
  1172. {
  1173. /* Transmission Mailbox 0 abort callback */
  1174. /* Call weak (surcharged) callback */
  1175. HAL_CAN_TxMailbox0AbortCallback(hcan);
  1176. }
  1177. }
  1178. }
  1179. /* Transmit Mailbox 1 management *****************************************/
  1180. if ((tsrflags & CAN_TSR_RQCP1) != RESET)
  1181. {
  1182. /* Clear the Transmission Complete flag (and TXOK1,ALST1,TERR1 bits) */
  1183. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP1);
  1184. if ((tsrflags & CAN_TSR_TXOK1) != RESET)
  1185. {
  1186. /* Transmission Mailbox 1 complete callback */
  1187. /* Call weak (surcharged) callback */
  1188. HAL_CAN_TxMailbox1CompleteCallback(hcan);
  1189. }
  1190. else
  1191. {
  1192. if ((tsrflags & CAN_TSR_ALST1) != RESET)
  1193. {
  1194. /* Update error code */
  1195. errorcode |= HAL_CAN_ERROR_TX_ALST1;
  1196. }
  1197. else if ((tsrflags & CAN_TSR_TERR1) != RESET)
  1198. {
  1199. /* Update error code */
  1200. errorcode |= HAL_CAN_ERROR_TX_TERR1;
  1201. }
  1202. else
  1203. {
  1204. /* Transmission Mailbox 1 abort callback */
  1205. /* Call weak (surcharged) callback */
  1206. HAL_CAN_TxMailbox1AbortCallback(hcan);
  1207. }
  1208. }
  1209. }
  1210. /* Transmit Mailbox 2 management *****************************************/
  1211. if ((tsrflags & CAN_TSR_RQCP2) != RESET)
  1212. {
  1213. /* Clear the Transmission Complete flag (and TXOK2,ALST2,TERR2 bits) */
  1214. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP2);
  1215. if ((tsrflags & CAN_TSR_TXOK2) != RESET)
  1216. {
  1217. /* Transmission Mailbox 2 complete callback */
  1218. /* Call weak (surcharged) callback */
  1219. HAL_CAN_TxMailbox2CompleteCallback(hcan);
  1220. }
  1221. else
  1222. {
  1223. if ((tsrflags & CAN_TSR_ALST2) != RESET)
  1224. {
  1225. /* Update error code */
  1226. errorcode |= HAL_CAN_ERROR_TX_ALST2;
  1227. }
  1228. else if ((tsrflags & CAN_TSR_TERR2) != RESET)
  1229. {
  1230. /* Update error code */
  1231. errorcode |= HAL_CAN_ERROR_TX_TERR2;
  1232. }
  1233. else
  1234. {
  1235. /* Transmission Mailbox 2 abort callback */
  1236. /* Call weak (surcharged) callback */
  1237. HAL_CAN_TxMailbox2AbortCallback(hcan);
  1238. }
  1239. }
  1240. }
  1241. }
  1242. /* Receive FIFO 0 overrun interrupt management *****************************/
  1243. if ((interrupts & CAN_IT_RX_FIFO0_OVERRUN) != RESET)
  1244. {
  1245. if ((rf0rflags & CAN_RF0R_FOVR0) != RESET)
  1246. {
  1247. /* Set CAN error code to Rx Fifo 0 overrun error */
  1248. errorcode |= HAL_CAN_ERROR_RX_FOV0;
  1249. /* Clear FIFO0 Overrun Flag */
  1250. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
  1251. }
  1252. }
  1253. /* Receive FIFO 0 full interrupt management ********************************/
  1254. if ((interrupts & CAN_IT_RX_FIFO0_FULL) != RESET)
  1255. {
  1256. if ((rf0rflags & CAN_RF0R_FULL0) != RESET)
  1257. {
  1258. /* Clear FIFO 0 full Flag */
  1259. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0);
  1260. /* Receive FIFO 0 full Callback */
  1261. /* Call weak (surcharged) callback */
  1262. HAL_CAN_RxFifo0FullCallback(hcan);
  1263. }
  1264. }
  1265. /* Receive FIFO 0 message pending interrupt management *********************/
  1266. if ((interrupts & CAN_IT_RX_FIFO0_MSG_PENDING) != RESET)
  1267. {
  1268. /* Check if message is still pending */
  1269. if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) != RESET)
  1270. {
  1271. /* Receive FIFO 0 mesage pending Callback */
  1272. /* Call weak (surcharged) callback */
  1273. HAL_CAN_RxFifo0MsgPendingCallback(hcan);
  1274. }
  1275. }
  1276. /* Receive FIFO 1 overrun interrupt management *****************************/
  1277. if ((interrupts & CAN_IT_RX_FIFO1_OVERRUN) != RESET)
  1278. {
  1279. if ((rf1rflags & CAN_RF1R_FOVR1) != RESET)
  1280. {
  1281. /* Set CAN error code to Rx Fifo 1 overrun error */
  1282. errorcode |= HAL_CAN_ERROR_RX_FOV1;
  1283. /* Clear FIFO1 Overrun Flag */
  1284. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV1);
  1285. }
  1286. }
  1287. /* Receive FIFO 1 full interrupt management ********************************/
  1288. if ((interrupts & CAN_IT_RX_FIFO1_FULL) != RESET)
  1289. {
  1290. if ((rf1rflags & CAN_RF1R_FULL1) != RESET)
  1291. {
  1292. /* Clear FIFO 1 full Flag */
  1293. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF1);
  1294. /* Receive FIFO 1 full Callback */
  1295. /* Call weak (surcharged) callback */
  1296. HAL_CAN_RxFifo1FullCallback(hcan);
  1297. }
  1298. }
  1299. /* Receive FIFO 1 message pending interrupt management *********************/
  1300. if ((interrupts & CAN_IT_RX_FIFO1_MSG_PENDING) != RESET)
  1301. {
  1302. /* Check if message is still pending */
  1303. if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) != RESET)
  1304. {
  1305. /* Receive FIFO 1 mesage pending Callback */
  1306. /* Call weak (surcharged) callback */
  1307. HAL_CAN_RxFifo1MsgPendingCallback(hcan);
  1308. }
  1309. }
  1310. /* Sleep interrupt management *********************************************/
  1311. if ((interrupts & CAN_IT_SLEEP_ACK) != RESET)
  1312. {
  1313. if ((msrflags & CAN_MSR_SLAKI) != RESET)
  1314. {
  1315. /* Clear Sleep interrupt Flag */
  1316. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_SLAKI);
  1317. /* Sleep Callback */
  1318. /* Call weak (surcharged) callback */
  1319. HAL_CAN_SleepCallback(hcan);
  1320. }
  1321. }
  1322. /* WakeUp interrupt management *********************************************/
  1323. if ((interrupts & CAN_IT_WAKEUP) != RESET)
  1324. {
  1325. if ((msrflags & CAN_MSR_WKUI) != RESET)
  1326. {
  1327. /* Clear WakeUp Flag */
  1328. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_WKU);
  1329. /* WakeUp Callback */
  1330. /* Call weak (surcharged) callback */
  1331. HAL_CAN_WakeUpFromRxMsgCallback(hcan);
  1332. }
  1333. }
  1334. /* Error interrupts management *********************************************/
  1335. if ((interrupts & CAN_IT_ERROR) != RESET)
  1336. {
  1337. if ((msrflags & CAN_MSR_ERRI) != RESET)
  1338. {
  1339. /* Check Error Warning Flag */
  1340. if (((interrupts & CAN_IT_ERROR_WARNING) != RESET) &&
  1341. ((esrflags & CAN_ESR_EWGF) != RESET))
  1342. {
  1343. /* Set CAN error code to Error Warning */
  1344. errorcode |= HAL_CAN_ERROR_EWG;
  1345. /* No need for clear of Error Warning Flag as read-only */
  1346. }
  1347. /* Check Error Passive Flag */
  1348. if (((interrupts & CAN_IT_ERROR_PASSIVE) != RESET) &&
  1349. ((esrflags & CAN_ESR_EPVF) != RESET))
  1350. {
  1351. /* Set CAN error code to Error Passive */
  1352. errorcode |= HAL_CAN_ERROR_EPV;
  1353. /* No need for clear of Error Passive Flag as read-only */
  1354. }
  1355. /* Check Bus-off Flag */
  1356. if (((interrupts & CAN_IT_BUSOFF) != RESET) &&
  1357. ((esrflags & CAN_ESR_BOFF) != RESET))
  1358. {
  1359. /* Set CAN error code to Bus-Off */
  1360. errorcode |= HAL_CAN_ERROR_BOF;
  1361. /* No need for clear of Error Bus-Off as read-only */
  1362. }
  1363. /* Check Last Error Code Flag */
  1364. if (((interrupts & CAN_IT_LAST_ERROR_CODE) != RESET) &&
  1365. ((esrflags & CAN_ESR_LEC) != RESET))
  1366. {
  1367. switch (esrflags & CAN_ESR_LEC)
  1368. {
  1369. case (CAN_ESR_LEC_0):
  1370. /* Set CAN error code to Stuff error */
  1371. errorcode |= HAL_CAN_ERROR_STF;
  1372. break;
  1373. case (CAN_ESR_LEC_1):
  1374. /* Set CAN error code to Form error */
  1375. errorcode |= HAL_CAN_ERROR_FOR;
  1376. break;
  1377. case (CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
  1378. /* Set CAN error code to Acknowledgement error */
  1379. errorcode |= HAL_CAN_ERROR_ACK;
  1380. break;
  1381. case (CAN_ESR_LEC_2):
  1382. /* Set CAN error code to Bit recessive error */
  1383. errorcode |= HAL_CAN_ERROR_BR;
  1384. break;
  1385. case (CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
  1386. /* Set CAN error code to Bit Dominant error */
  1387. errorcode |= HAL_CAN_ERROR_BD;
  1388. break;
  1389. case (CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
  1390. /* Set CAN error code to CRC error */
  1391. errorcode |= HAL_CAN_ERROR_CRC;
  1392. break;
  1393. default:
  1394. break;
  1395. }
  1396. /* Clear Last error code Flag */
  1397. CLEAR_BIT(hcan->Instance->ESR, CAN_ESR_LEC);
  1398. }
  1399. }
  1400. /* Clear ERRI Flag */
  1401. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_ERRI);
  1402. }
  1403. /* Call the Error call Back in case of Errors */
  1404. if (errorcode != HAL_CAN_ERROR_NONE)
  1405. {
  1406. /* Update error code in handle */
  1407. hcan->ErrorCode |= errorcode;
  1408. /* Call Error callback function */
  1409. /* Call weak (surcharged) callback */
  1410. HAL_CAN_ErrorCallback(hcan);
  1411. }
  1412. }
  1413. /**
  1414. * @}
  1415. */
  1416. /** @defgroup CAN_Exported_Functions_Group5 Callback functions
  1417. * @brief CAN Callback functions
  1418. *
  1419. @verbatim
  1420. ==============================================================================
  1421. ##### Callback functions #####
  1422. ==============================================================================
  1423. [..]
  1424. This subsection provides the following callback functions:
  1425. (+) HAL_CAN_TxMailbox0CompleteCallback
  1426. (+) HAL_CAN_TxMailbox1CompleteCallback
  1427. (+) HAL_CAN_TxMailbox2CompleteCallback
  1428. (+) HAL_CAN_TxMailbox0AbortCallback
  1429. (+) HAL_CAN_TxMailbox1AbortCallback
  1430. (+) HAL_CAN_TxMailbox2AbortCallback
  1431. (+) HAL_CAN_RxFifo0MsgPendingCallback
  1432. (+) HAL_CAN_RxFifo0FullCallback
  1433. (+) HAL_CAN_RxFifo1MsgPendingCallback
  1434. (+) HAL_CAN_RxFifo1FullCallback
  1435. (+) HAL_CAN_SleepCallback
  1436. (+) HAL_CAN_WakeUpFromRxMsgCallback
  1437. (+) HAL_CAN_ErrorCallback
  1438. @endverbatim
  1439. * @{
  1440. */
  1441. /**
  1442. * @brief Transmission Mailbox 0 complete callback.
  1443. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1444. * the configuration information for the specified CAN.
  1445. * @retval None
  1446. */
  1447. __weak void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
  1448. {
  1449. /* Prevent unused argument(s) compilation warning */
  1450. UNUSED(hcan);
  1451. /* NOTE : This function Should not be modified, when the callback is needed,
  1452. the HAL_CAN_TxMailbox0CompleteCallback could be implemented in the
  1453. user file
  1454. */
  1455. }
  1456. /**
  1457. * @brief Transmission Mailbox 1 complete callback.
  1458. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1459. * the configuration information for the specified CAN.
  1460. * @retval None
  1461. */
  1462. __weak void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
  1463. {
  1464. /* Prevent unused argument(s) compilation warning */
  1465. UNUSED(hcan);
  1466. /* NOTE : This function Should not be modified, when the callback is needed,
  1467. the HAL_CAN_TxMailbox1CompleteCallback could be implemented in the
  1468. user file
  1469. */
  1470. }
  1471. /**
  1472. * @brief Transmission Mailbox 2 complete callback.
  1473. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1474. * the configuration information for the specified CAN.
  1475. * @retval None
  1476. */
  1477. __weak void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
  1478. {
  1479. /* Prevent unused argument(s) compilation warning */
  1480. UNUSED(hcan);
  1481. /* NOTE : This function Should not be modified, when the callback is needed,
  1482. the HAL_CAN_TxMailbox2CompleteCallback could be implemented in the
  1483. user file
  1484. */
  1485. }
  1486. /**
  1487. * @brief Transmission Mailbox 0 Cancellation callback.
  1488. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1489. * the configuration information for the specified CAN.
  1490. * @retval None
  1491. */
  1492. __weak void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan)
  1493. {
  1494. /* Prevent unused argument(s) compilation warning */
  1495. UNUSED(hcan);
  1496. /* NOTE : This function Should not be modified, when the callback is needed,
  1497. the HAL_CAN_TxMailbox0AbortCallback could be implemented in the
  1498. user file
  1499. */
  1500. }
  1501. /**
  1502. * @brief Transmission Mailbox 1 Cancellation callback.
  1503. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1504. * the configuration information for the specified CAN.
  1505. * @retval None
  1506. */
  1507. __weak void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan)
  1508. {
  1509. /* Prevent unused argument(s) compilation warning */
  1510. UNUSED(hcan);
  1511. /* NOTE : This function Should not be modified, when the callback is needed,
  1512. the HAL_CAN_TxMailbox1AbortCallback could be implemented in the
  1513. user file
  1514. */
  1515. }
  1516. /**
  1517. * @brief Transmission Mailbox 2 Cancellation callback.
  1518. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1519. * the configuration information for the specified CAN.
  1520. * @retval None
  1521. */
  1522. __weak void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan)
  1523. {
  1524. /* Prevent unused argument(s) compilation warning */
  1525. UNUSED(hcan);
  1526. /* NOTE : This function Should not be modified, when the callback is needed,
  1527. the HAL_CAN_TxMailbox2AbortCallback could be implemented in the
  1528. user file
  1529. */
  1530. }
  1531. /**
  1532. * @brief Rx FIFO 0 message pending callback.
  1533. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1534. * the configuration information for the specified CAN.
  1535. * @retval None
  1536. */
  1537. __weak void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
  1538. {
  1539. /* Prevent unused argument(s) compilation warning */
  1540. UNUSED(hcan);
  1541. /* NOTE : This function Should not be modified, when the callback is needed,
  1542. the HAL_CAN_RxFifo0MsgPendingCallback could be implemented in the
  1543. user file
  1544. */
  1545. }
  1546. /**
  1547. * @brief Rx FIFO 0 full callback.
  1548. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1549. * the configuration information for the specified CAN.
  1550. * @retval None
  1551. */
  1552. __weak void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan)
  1553. {
  1554. /* Prevent unused argument(s) compilation warning */
  1555. UNUSED(hcan);
  1556. /* NOTE : This function Should not be modified, when the callback is needed,
  1557. the HAL_CAN_RxFifo0FullCallback could be implemented in the user
  1558. file
  1559. */
  1560. }
  1561. /**
  1562. * @brief Rx FIFO 1 message pending callback.
  1563. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1564. * the configuration information for the specified CAN.
  1565. * @retval None
  1566. */
  1567. __weak void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
  1568. {
  1569. /* Prevent unused argument(s) compilation warning */
  1570. UNUSED(hcan);
  1571. /* NOTE : This function Should not be modified, when the callback is needed,
  1572. the HAL_CAN_RxFifo1MsgPendingCallback could be implemented in the
  1573. user file
  1574. */
  1575. }
  1576. /**
  1577. * @brief Rx FIFO 1 full callback.
  1578. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1579. * the configuration information for the specified CAN.
  1580. * @retval None
  1581. */
  1582. __weak void HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef *hcan)
  1583. {
  1584. /* Prevent unused argument(s) compilation warning */
  1585. UNUSED(hcan);
  1586. /* NOTE : This function Should not be modified, when the callback is needed,
  1587. the HAL_CAN_RxFifo1FullCallback could be implemented in the user
  1588. file
  1589. */
  1590. }
  1591. /**
  1592. * @brief Sleep callback.
  1593. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1594. * the configuration information for the specified CAN.
  1595. * @retval None
  1596. */
  1597. __weak void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan)
  1598. {
  1599. /* Prevent unused argument(s) compilation warning */
  1600. UNUSED(hcan);
  1601. /* NOTE : This function Should not be modified, when the callback is needed,
  1602. the HAL_CAN_SleepCallback could be implemented in the user file
  1603. */
  1604. }
  1605. /**
  1606. * @brief WakeUp from Rx message callback.
  1607. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1608. * the configuration information for the specified CAN.
  1609. * @retval None
  1610. */
  1611. __weak void HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan)
  1612. {
  1613. /* Prevent unused argument(s) compilation warning */
  1614. UNUSED(hcan);
  1615. /* NOTE : This function Should not be modified, when the callback is needed,
  1616. the HAL_CAN_WakeUpFromRxMsgCallback could be implemented in the
  1617. user file
  1618. */
  1619. }
  1620. /**
  1621. * @brief Error CAN callback.
  1622. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1623. * the configuration information for the specified CAN.
  1624. * @retval None
  1625. */
  1626. __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
  1627. {
  1628. /* Prevent unused argument(s) compilation warning */
  1629. UNUSED(hcan);
  1630. /* NOTE : This function Should not be modified, when the callback is needed,
  1631. the HAL_CAN_ErrorCallback could be implemented in the user file
  1632. */
  1633. }
  1634. /**
  1635. * @}
  1636. */
  1637. /** @defgroup CAN_Exported_Functions_Group6 Peripheral State and Error functions
  1638. * @brief CAN Peripheral State functions
  1639. *
  1640. @verbatim
  1641. ==============================================================================
  1642. ##### Peripheral State and Error functions #####
  1643. ==============================================================================
  1644. [..]
  1645. This subsection provides functions allowing to :
  1646. (+) HAL_CAN_GetState() : Return the CAN state.
  1647. (+) HAL_CAN_GetError() : Return the CAN error codes if any.
  1648. (+) HAL_CAN_ResetError(): Reset the CAN error codes if any.
  1649. @endverbatim
  1650. * @{
  1651. */
  1652. /**
  1653. * @brief Return the CAN state.
  1654. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1655. * the configuration information for the specified CAN.
  1656. * @retval HAL state
  1657. */
  1658. HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef *hcan)
  1659. {
  1660. HAL_CAN_StateTypeDef state = hcan->State;
  1661. if ((hcan->State == HAL_CAN_STATE_READY) ||
  1662. (hcan->State == HAL_CAN_STATE_LISTENING))
  1663. {
  1664. /* Check sleep mode acknowledge flag */
  1665. if ((hcan->Instance->MSR & CAN_MSR_SLAK) != RESET)
  1666. {
  1667. /* Sleep mode is active */
  1668. state = HAL_CAN_STATE_SLEEP_ACTIVE;
  1669. }
  1670. /* Check sleep mode request flag */
  1671. else if ((hcan->Instance->MCR & CAN_MCR_SLEEP) != RESET)
  1672. {
  1673. /* Sleep mode request is pending */
  1674. state = HAL_CAN_STATE_SLEEP_PENDING;
  1675. }
  1676. }
  1677. /* Return CAN state */
  1678. return state;
  1679. }
  1680. /**
  1681. * @brief Return the CAN error code.
  1682. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1683. * the configuration information for the specified CAN.
  1684. * @retval CAN Error Code
  1685. */
  1686. uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan)
  1687. {
  1688. /* Return CAN error code */
  1689. return hcan->ErrorCode;
  1690. }
  1691. /**
  1692. * @brief Reset the CAN error code.
  1693. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1694. * the configuration information for the specified CAN.
  1695. * @retval HAL status
  1696. */
  1697. HAL_StatusTypeDef HAL_CAN_ResetError(CAN_HandleTypeDef *hcan)
  1698. {
  1699. HAL_StatusTypeDef status = HAL_OK;
  1700. if ((hcan->State == HAL_CAN_STATE_READY) ||
  1701. (hcan->State == HAL_CAN_STATE_LISTENING))
  1702. {
  1703. /* Reset CAN error code */
  1704. hcan->ErrorCode = 0U;
  1705. }
  1706. else
  1707. {
  1708. /* Update error code */
  1709. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1710. status = HAL_ERROR;
  1711. }
  1712. /* Return the status */
  1713. return status;
  1714. }
  1715. /**
  1716. * @}
  1717. */
  1718. /**
  1719. * @}
  1720. */
  1721. #endif /* HAL_CAN_MODULE_ENABLED */
  1722. /**
  1723. * @}
  1724. */
  1725. #endif /* CAN1 */
  1726. /**
  1727. * @}
  1728. */
  1729. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/