stm32f4xx_hal_can.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464
  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. *** Callback registration ***
  110. =============================================
  111. The compilation define USE_HAL_CAN_REGISTER_CALLBACKS when set to 1
  112. allows the user to configure dynamically the driver callbacks.
  113. Use Function @ref HAL_CAN_RegisterCallback() to register an interrupt callback.
  114. Function @ref HAL_CAN_RegisterCallback() allows to register following callbacks:
  115. (+) TxMailbox0CompleteCallback : Tx Mailbox 0 Complete Callback.
  116. (+) TxMailbox1CompleteCallback : Tx Mailbox 1 Complete Callback.
  117. (+) TxMailbox2CompleteCallback : Tx Mailbox 2 Complete Callback.
  118. (+) TxMailbox0AbortCallback : Tx Mailbox 0 Abort Callback.
  119. (+) TxMailbox1AbortCallback : Tx Mailbox 1 Abort Callback.
  120. (+) TxMailbox2AbortCallback : Tx Mailbox 2 Abort Callback.
  121. (+) RxFifo0MsgPendingCallback : Rx Fifo 0 Message Pending Callback.
  122. (+) RxFifo0FullCallback : Rx Fifo 0 Full Callback.
  123. (+) RxFifo1MsgPendingCallback : Rx Fifo 1 Message Pending Callback.
  124. (+) RxFifo1FullCallback : Rx Fifo 1 Full Callback.
  125. (+) SleepCallback : Sleep Callback.
  126. (+) WakeUpFromRxMsgCallback : Wake Up From Rx Message Callback.
  127. (+) ErrorCallback : Error Callback.
  128. (+) MspInitCallback : CAN MspInit.
  129. (+) MspDeInitCallback : CAN MspDeInit.
  130. This function takes as parameters the HAL peripheral handle, the Callback ID
  131. and a pointer to the user callback function.
  132. Use function @ref HAL_CAN_UnRegisterCallback() to reset a callback to the default
  133. weak function.
  134. @ref HAL_CAN_UnRegisterCallback takes as parameters the HAL peripheral handle,
  135. and the Callback ID.
  136. This function allows to reset following callbacks:
  137. (+) TxMailbox0CompleteCallback : Tx Mailbox 0 Complete Callback.
  138. (+) TxMailbox1CompleteCallback : Tx Mailbox 1 Complete Callback.
  139. (+) TxMailbox2CompleteCallback : Tx Mailbox 2 Complete Callback.
  140. (+) TxMailbox0AbortCallback : Tx Mailbox 0 Abort Callback.
  141. (+) TxMailbox1AbortCallback : Tx Mailbox 1 Abort Callback.
  142. (+) TxMailbox2AbortCallback : Tx Mailbox 2 Abort Callback.
  143. (+) RxFifo0MsgPendingCallback : Rx Fifo 0 Message Pending Callback.
  144. (+) RxFifo0FullCallback : Rx Fifo 0 Full Callback.
  145. (+) RxFifo1MsgPendingCallback : Rx Fifo 1 Message Pending Callback.
  146. (+) RxFifo1FullCallback : Rx Fifo 1 Full Callback.
  147. (+) SleepCallback : Sleep Callback.
  148. (+) WakeUpFromRxMsgCallback : Wake Up From Rx Message Callback.
  149. (+) ErrorCallback : Error Callback.
  150. (+) MspInitCallback : CAN MspInit.
  151. (+) MspDeInitCallback : CAN MspDeInit.
  152. By default, after the @ref HAL_CAN_Init() and when the state is HAL_CAN_STATE_RESET,
  153. all callbacks are set to the corresponding weak functions:
  154. example @ref HAL_CAN_ErrorCallback().
  155. Exception done for MspInit and MspDeInit functions that are
  156. reset to the legacy weak function in the @ref HAL_CAN_Init()/ @ref HAL_CAN_DeInit() only when
  157. these callbacks are null (not registered beforehand).
  158. if not, MspInit or MspDeInit are not null, the @ref HAL_CAN_Init()/ @ref HAL_CAN_DeInit()
  159. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  160. Callbacks can be registered/unregistered in HAL_CAN_STATE_READY state only.
  161. Exception done MspInit/MspDeInit that can be registered/unregistered
  162. in HAL_CAN_STATE_READY or HAL_CAN_STATE_RESET state,
  163. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  164. In that case first register the MspInit/MspDeInit user callbacks
  165. using @ref HAL_CAN_RegisterCallback() before calling @ref HAL_CAN_DeInit()
  166. or @ref HAL_CAN_Init() function.
  167. When The compilation define USE_HAL_CAN_REGISTER_CALLBACKS is set to 0 or
  168. not defined, the callback registration feature is not available and all callbacks
  169. are set to the corresponding weak functions.
  170. @endverbatim
  171. ******************************************************************************
  172. * @attention
  173. *
  174. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  175. * All rights reserved.</center></h2>
  176. *
  177. * This software component is licensed by ST under BSD 3-Clause license,
  178. * the "License"; You may not use this file except in compliance with the
  179. * License. You may obtain a copy of the License at:
  180. * opensource.org/licenses/BSD-3-Clause
  181. *
  182. ******************************************************************************
  183. */
  184. /* Includes ------------------------------------------------------------------*/
  185. #include "stm32f4xx_hal.h"
  186. /** @addtogroup STM32F4xx_HAL_Driver
  187. * @{
  188. */
  189. #if defined(CAN1)
  190. /** @defgroup CAN CAN
  191. * @brief CAN driver modules
  192. * @{
  193. */
  194. #ifdef HAL_CAN_MODULE_ENABLED
  195. #ifdef HAL_CAN_LEGACY_MODULE_ENABLED
  196. #error "The CAN driver cannot be used with its legacy, Please enable only one CAN module at once"
  197. #endif
  198. /* Private typedef -----------------------------------------------------------*/
  199. /* Private define ------------------------------------------------------------*/
  200. /** @defgroup CAN_Private_Constants CAN Private Constants
  201. * @{
  202. */
  203. #define CAN_TIMEOUT_VALUE 10U
  204. /**
  205. * @}
  206. */
  207. /* Private macro -------------------------------------------------------------*/
  208. /* Private variables ---------------------------------------------------------*/
  209. /* Private function prototypes -----------------------------------------------*/
  210. /* Exported functions --------------------------------------------------------*/
  211. /** @defgroup CAN_Exported_Functions CAN Exported Functions
  212. * @{
  213. */
  214. /** @defgroup CAN_Exported_Functions_Group1 Initialization and de-initialization functions
  215. * @brief Initialization and Configuration functions
  216. *
  217. @verbatim
  218. ==============================================================================
  219. ##### Initialization and de-initialization functions #####
  220. ==============================================================================
  221. [..] This section provides functions allowing to:
  222. (+) HAL_CAN_Init : Initialize and configure the CAN.
  223. (+) HAL_CAN_DeInit : De-initialize the CAN.
  224. (+) HAL_CAN_MspInit : Initialize the CAN MSP.
  225. (+) HAL_CAN_MspDeInit : DeInitialize the CAN MSP.
  226. @endverbatim
  227. * @{
  228. */
  229. /**
  230. * @brief Initializes the CAN peripheral according to the specified
  231. * parameters in the CAN_InitStruct.
  232. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  233. * the configuration information for the specified CAN.
  234. * @retval HAL status
  235. */
  236. HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef *hcan)
  237. {
  238. uint32_t tickstart;
  239. /* Check CAN handle */
  240. if (hcan == NULL)
  241. {
  242. return HAL_ERROR;
  243. }
  244. /* Check the parameters */
  245. assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
  246. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TimeTriggeredMode));
  247. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoBusOff));
  248. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoWakeUp));
  249. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoRetransmission));
  250. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ReceiveFifoLocked));
  251. assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TransmitFifoPriority));
  252. assert_param(IS_CAN_MODE(hcan->Init.Mode));
  253. assert_param(IS_CAN_SJW(hcan->Init.SyncJumpWidth));
  254. assert_param(IS_CAN_BS1(hcan->Init.TimeSeg1));
  255. assert_param(IS_CAN_BS2(hcan->Init.TimeSeg2));
  256. assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler));
  257. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  258. if (hcan->State == HAL_CAN_STATE_RESET)
  259. {
  260. /* Reset callbacks to legacy functions */
  261. hcan->RxFifo0MsgPendingCallback = HAL_CAN_RxFifo0MsgPendingCallback; /* Legacy weak RxFifo0MsgPendingCallback */
  262. hcan->RxFifo0FullCallback = HAL_CAN_RxFifo0FullCallback; /* Legacy weak RxFifo0FullCallback */
  263. hcan->RxFifo1MsgPendingCallback = HAL_CAN_RxFifo1MsgPendingCallback; /* Legacy weak RxFifo1MsgPendingCallback */
  264. hcan->RxFifo1FullCallback = HAL_CAN_RxFifo1FullCallback; /* Legacy weak RxFifo1FullCallback */
  265. hcan->TxMailbox0CompleteCallback = HAL_CAN_TxMailbox0CompleteCallback; /* Legacy weak TxMailbox0CompleteCallback */
  266. hcan->TxMailbox1CompleteCallback = HAL_CAN_TxMailbox1CompleteCallback; /* Legacy weak TxMailbox1CompleteCallback */
  267. hcan->TxMailbox2CompleteCallback = HAL_CAN_TxMailbox2CompleteCallback; /* Legacy weak TxMailbox2CompleteCallback */
  268. hcan->TxMailbox0AbortCallback = HAL_CAN_TxMailbox0AbortCallback; /* Legacy weak TxMailbox0AbortCallback */
  269. hcan->TxMailbox1AbortCallback = HAL_CAN_TxMailbox1AbortCallback; /* Legacy weak TxMailbox1AbortCallback */
  270. hcan->TxMailbox2AbortCallback = HAL_CAN_TxMailbox2AbortCallback; /* Legacy weak TxMailbox2AbortCallback */
  271. hcan->SleepCallback = HAL_CAN_SleepCallback; /* Legacy weak SleepCallback */
  272. hcan->WakeUpFromRxMsgCallback = HAL_CAN_WakeUpFromRxMsgCallback; /* Legacy weak WakeUpFromRxMsgCallback */
  273. hcan->ErrorCallback = HAL_CAN_ErrorCallback; /* Legacy weak ErrorCallback */
  274. if (hcan->MspInitCallback == NULL)
  275. {
  276. hcan->MspInitCallback = HAL_CAN_MspInit; /* Legacy weak MspInit */
  277. }
  278. /* Init the low level hardware: CLOCK, NVIC */
  279. hcan->MspInitCallback(hcan);
  280. }
  281. #else
  282. if (hcan->State == HAL_CAN_STATE_RESET)
  283. {
  284. /* Init the low level hardware: CLOCK, NVIC */
  285. HAL_CAN_MspInit(hcan);
  286. }
  287. #endif /* (USE_HAL_CAN_REGISTER_CALLBACKS) */
  288. /* Exit from sleep mode */
  289. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  290. /* Get tick */
  291. tickstart = HAL_GetTick();
  292. /* Check Sleep mode leave acknowledge */
  293. while ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
  294. {
  295. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  296. {
  297. /* Update error code */
  298. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  299. /* Change CAN state */
  300. hcan->State = HAL_CAN_STATE_ERROR;
  301. return HAL_ERROR;
  302. }
  303. }
  304. /* Request initialisation */
  305. SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
  306. /* Get tick */
  307. tickstart = HAL_GetTick();
  308. /* Wait initialisation acknowledge */
  309. while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U)
  310. {
  311. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  312. {
  313. /* Update error code */
  314. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  315. /* Change CAN state */
  316. hcan->State = HAL_CAN_STATE_ERROR;
  317. return HAL_ERROR;
  318. }
  319. }
  320. /* Set the time triggered communication mode */
  321. if (hcan->Init.TimeTriggeredMode == ENABLE)
  322. {
  323. SET_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
  324. }
  325. else
  326. {
  327. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
  328. }
  329. /* Set the automatic bus-off management */
  330. if (hcan->Init.AutoBusOff == ENABLE)
  331. {
  332. SET_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
  333. }
  334. else
  335. {
  336. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
  337. }
  338. /* Set the automatic wake-up mode */
  339. if (hcan->Init.AutoWakeUp == ENABLE)
  340. {
  341. SET_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
  342. }
  343. else
  344. {
  345. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
  346. }
  347. /* Set the automatic retransmission */
  348. if (hcan->Init.AutoRetransmission == ENABLE)
  349. {
  350. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_NART);
  351. }
  352. else
  353. {
  354. SET_BIT(hcan->Instance->MCR, CAN_MCR_NART);
  355. }
  356. /* Set the receive FIFO locked mode */
  357. if (hcan->Init.ReceiveFifoLocked == ENABLE)
  358. {
  359. SET_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
  360. }
  361. else
  362. {
  363. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
  364. }
  365. /* Set the transmit FIFO priority */
  366. if (hcan->Init.TransmitFifoPriority == ENABLE)
  367. {
  368. SET_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
  369. }
  370. else
  371. {
  372. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
  373. }
  374. /* Set the bit timing register */
  375. WRITE_REG(hcan->Instance->BTR, (uint32_t)(hcan->Init.Mode |
  376. hcan->Init.SyncJumpWidth |
  377. hcan->Init.TimeSeg1 |
  378. hcan->Init.TimeSeg2 |
  379. (hcan->Init.Prescaler - 1U)));
  380. /* Initialize the error code */
  381. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  382. /* Initialize the CAN state */
  383. hcan->State = HAL_CAN_STATE_READY;
  384. /* Return function status */
  385. return HAL_OK;
  386. }
  387. /**
  388. * @brief Deinitializes the CAN peripheral registers to their default
  389. * reset values.
  390. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  391. * the configuration information for the specified CAN.
  392. * @retval HAL status
  393. */
  394. HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef *hcan)
  395. {
  396. /* Check CAN handle */
  397. if (hcan == NULL)
  398. {
  399. return HAL_ERROR;
  400. }
  401. /* Check the parameters */
  402. assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
  403. /* Stop the CAN module */
  404. (void)HAL_CAN_Stop(hcan);
  405. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  406. if (hcan->MspDeInitCallback == NULL)
  407. {
  408. hcan->MspDeInitCallback = HAL_CAN_MspDeInit; /* Legacy weak MspDeInit */
  409. }
  410. /* DeInit the low level hardware: CLOCK, NVIC */
  411. hcan->MspDeInitCallback(hcan);
  412. #else
  413. /* DeInit the low level hardware: CLOCK, NVIC */
  414. HAL_CAN_MspDeInit(hcan);
  415. #endif /* (USE_HAL_CAN_REGISTER_CALLBACKS) */
  416. /* Reset the CAN peripheral */
  417. SET_BIT(hcan->Instance->MCR, CAN_MCR_RESET);
  418. /* Reset the CAN ErrorCode */
  419. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  420. /* Change CAN state */
  421. hcan->State = HAL_CAN_STATE_RESET;
  422. /* Return function status */
  423. return HAL_OK;
  424. }
  425. /**
  426. * @brief Initializes the CAN MSP.
  427. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  428. * the configuration information for the specified CAN.
  429. * @retval None
  430. */
  431. __weak void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
  432. {
  433. /* Prevent unused argument(s) compilation warning */
  434. UNUSED(hcan);
  435. /* NOTE : This function Should not be modified, when the callback is needed,
  436. the HAL_CAN_MspInit could be implemented in the user file
  437. */
  438. }
  439. /**
  440. * @brief DeInitializes the CAN MSP.
  441. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  442. * the configuration information for the specified CAN.
  443. * @retval None
  444. */
  445. __weak void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
  446. {
  447. /* Prevent unused argument(s) compilation warning */
  448. UNUSED(hcan);
  449. /* NOTE : This function Should not be modified, when the callback is needed,
  450. the HAL_CAN_MspDeInit could be implemented in the user file
  451. */
  452. }
  453. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  454. /**
  455. * @brief Register a CAN CallBack.
  456. * To be used instead of the weak predefined callback
  457. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  458. * the configuration information for CAN module
  459. * @param CallbackID ID of the callback to be registered
  460. * This parameter can be one of the following values:
  461. * @arg @ref HAL_CAN_TX_MAILBOX0_COMPLETE_CALLBACK_CB_ID Tx Mailbox 0 Complete callback ID
  462. * @arg @ref HAL_CAN_TX_MAILBOX1_COMPLETE_CALLBACK_CB_ID Tx Mailbox 1 Complete callback ID
  463. * @arg @ref HAL_CAN_TX_MAILBOX2_COMPLETE_CALLBACK_CB_ID Tx Mailbox 2 Complete callback ID
  464. * @arg @ref HAL_CAN_TX_MAILBOX0_ABORT_CALLBACK_CB_ID Tx Mailbox 0 Abort callback ID
  465. * @arg @ref HAL_CAN_TX_MAILBOX1_ABORT_CALLBACK_CB_ID Tx Mailbox 1 Abort callback ID
  466. * @arg @ref HAL_CAN_TX_MAILBOX2_ABORT_CALLBACK_CB_ID Tx Mailbox 2 Abort callback ID
  467. * @arg @ref HAL_CAN_RX_FIFO0_MSG_PENDING_CALLBACK_CB_ID Rx Fifo 0 message pending callback ID
  468. * @arg @ref HAL_CAN_RX_FIFO0_FULL_CALLBACK_CB_ID Rx Fifo 0 full callback ID
  469. * @arg @ref HAL_CAN_RX_FIFO1_MSGPENDING_CALLBACK_CB_ID Rx Fifo 1 message pending callback ID
  470. * @arg @ref HAL_CAN_RX_FIFO1_FULL_CALLBACK_CB_ID Rx Fifo 1 full callback ID
  471. * @arg @ref HAL_CAN_SLEEP_CALLBACK_CB_ID Sleep callback ID
  472. * @arg @ref HAL_CAN_WAKEUP_FROM_RX_MSG_CALLBACK_CB_ID Wake Up from Rx message callback ID
  473. * @arg @ref HAL_CAN_ERROR_CALLBACK_CB_ID Error callback ID
  474. * @arg @ref HAL_CAN_MSPINIT_CB_ID MspInit callback ID
  475. * @arg @ref HAL_CAN_MSPDEINIT_CB_ID MspDeInit callback ID
  476. * @param pCallback pointer to the Callback function
  477. * @retval HAL status
  478. */
  479. HAL_StatusTypeDef HAL_CAN_RegisterCallback(CAN_HandleTypeDef *hcan, HAL_CAN_CallbackIDTypeDef CallbackID, void (* pCallback)(CAN_HandleTypeDef *_hcan))
  480. {
  481. HAL_StatusTypeDef status = HAL_OK;
  482. if (pCallback == NULL)
  483. {
  484. /* Update the error code */
  485. hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
  486. return HAL_ERROR;
  487. }
  488. if (hcan->State == HAL_CAN_STATE_READY)
  489. {
  490. switch (CallbackID)
  491. {
  492. case HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID :
  493. hcan->TxMailbox0CompleteCallback = pCallback;
  494. break;
  495. case HAL_CAN_TX_MAILBOX1_COMPLETE_CB_ID :
  496. hcan->TxMailbox1CompleteCallback = pCallback;
  497. break;
  498. case HAL_CAN_TX_MAILBOX2_COMPLETE_CB_ID :
  499. hcan->TxMailbox2CompleteCallback = pCallback;
  500. break;
  501. case HAL_CAN_TX_MAILBOX0_ABORT_CB_ID :
  502. hcan->TxMailbox0AbortCallback = pCallback;
  503. break;
  504. case HAL_CAN_TX_MAILBOX1_ABORT_CB_ID :
  505. hcan->TxMailbox1AbortCallback = pCallback;
  506. break;
  507. case HAL_CAN_TX_MAILBOX2_ABORT_CB_ID :
  508. hcan->TxMailbox2AbortCallback = pCallback;
  509. break;
  510. case HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID :
  511. hcan->RxFifo0MsgPendingCallback = pCallback;
  512. break;
  513. case HAL_CAN_RX_FIFO0_FULL_CB_ID :
  514. hcan->RxFifo0FullCallback = pCallback;
  515. break;
  516. case HAL_CAN_RX_FIFO1_MSG_PENDING_CB_ID :
  517. hcan->RxFifo1MsgPendingCallback = pCallback;
  518. break;
  519. case HAL_CAN_RX_FIFO1_FULL_CB_ID :
  520. hcan->RxFifo1FullCallback = pCallback;
  521. break;
  522. case HAL_CAN_SLEEP_CB_ID :
  523. hcan->SleepCallback = pCallback;
  524. break;
  525. case HAL_CAN_WAKEUP_FROM_RX_MSG_CB_ID :
  526. hcan->WakeUpFromRxMsgCallback = pCallback;
  527. break;
  528. case HAL_CAN_ERROR_CB_ID :
  529. hcan->ErrorCallback = pCallback;
  530. break;
  531. case HAL_CAN_MSPINIT_CB_ID :
  532. hcan->MspInitCallback = pCallback;
  533. break;
  534. case HAL_CAN_MSPDEINIT_CB_ID :
  535. hcan->MspDeInitCallback = pCallback;
  536. break;
  537. default :
  538. /* Update the error code */
  539. hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
  540. /* Return error status */
  541. status = HAL_ERROR;
  542. break;
  543. }
  544. }
  545. else if (hcan->State == HAL_CAN_STATE_RESET)
  546. {
  547. switch (CallbackID)
  548. {
  549. case HAL_CAN_MSPINIT_CB_ID :
  550. hcan->MspInitCallback = pCallback;
  551. break;
  552. case HAL_CAN_MSPDEINIT_CB_ID :
  553. hcan->MspDeInitCallback = pCallback;
  554. break;
  555. default :
  556. /* Update the error code */
  557. hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
  558. /* Return error status */
  559. status = HAL_ERROR;
  560. break;
  561. }
  562. }
  563. else
  564. {
  565. /* Update the error code */
  566. hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
  567. /* Return error status */
  568. status = HAL_ERROR;
  569. }
  570. return status;
  571. }
  572. /**
  573. * @brief Unregister a CAN CallBack.
  574. * CAN callabck is redirected to the weak predefined callback
  575. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  576. * the configuration information for CAN module
  577. * @param CallbackID ID of the callback to be unregistered
  578. * This parameter can be one of the following values:
  579. * @arg @ref HAL_CAN_TX_MAILBOX0_COMPLETE_CALLBACK_CB_ID Tx Mailbox 0 Complete callback ID
  580. * @arg @ref HAL_CAN_TX_MAILBOX1_COMPLETE_CALLBACK_CB_ID Tx Mailbox 1 Complete callback ID
  581. * @arg @ref HAL_CAN_TX_MAILBOX2_COMPLETE_CALLBACK_CB_ID Tx Mailbox 2 Complete callback ID
  582. * @arg @ref HAL_CAN_TX_MAILBOX0_ABORT_CALLBACK_CB_ID Tx Mailbox 0 Abort callback ID
  583. * @arg @ref HAL_CAN_TX_MAILBOX1_ABORT_CALLBACK_CB_ID Tx Mailbox 1 Abort callback ID
  584. * @arg @ref HAL_CAN_TX_MAILBOX2_ABORT_CALLBACK_CB_ID Tx Mailbox 2 Abort callback ID
  585. * @arg @ref HAL_CAN_RX_FIFO0_MSG_PENDING_CALLBACK_CB_ID Rx Fifo 0 message pending callback ID
  586. * @arg @ref HAL_CAN_RX_FIFO0_FULL_CALLBACK_CB_ID Rx Fifo 0 full callback ID
  587. * @arg @ref HAL_CAN_RX_FIFO1_MSGPENDING_CALLBACK_CB_ID Rx Fifo 1 message pending callback ID
  588. * @arg @ref HAL_CAN_RX_FIFO1_FULL_CALLBACK_CB_ID Rx Fifo 1 full callback ID
  589. * @arg @ref HAL_CAN_SLEEP_CALLBACK_CB_ID Sleep callback ID
  590. * @arg @ref HAL_CAN_WAKEUP_FROM_RX_MSG_CALLBACK_CB_ID Wake Up from Rx message callback ID
  591. * @arg @ref HAL_CAN_ERROR_CALLBACK_CB_ID Error callback ID
  592. * @arg @ref HAL_CAN_MSPINIT_CB_ID MspInit callback ID
  593. * @arg @ref HAL_CAN_MSPDEINIT_CB_ID MspDeInit callback ID
  594. * @retval HAL status
  595. */
  596. HAL_StatusTypeDef HAL_CAN_UnRegisterCallback(CAN_HandleTypeDef *hcan, HAL_CAN_CallbackIDTypeDef CallbackID)
  597. {
  598. HAL_StatusTypeDef status = HAL_OK;
  599. if (hcan->State == HAL_CAN_STATE_READY)
  600. {
  601. switch (CallbackID)
  602. {
  603. case HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID :
  604. hcan->TxMailbox0CompleteCallback = HAL_CAN_TxMailbox0CompleteCallback;
  605. break;
  606. case HAL_CAN_TX_MAILBOX1_COMPLETE_CB_ID :
  607. hcan->TxMailbox1CompleteCallback = HAL_CAN_TxMailbox1CompleteCallback;
  608. break;
  609. case HAL_CAN_TX_MAILBOX2_COMPLETE_CB_ID :
  610. hcan->TxMailbox2CompleteCallback = HAL_CAN_TxMailbox2CompleteCallback;
  611. break;
  612. case HAL_CAN_TX_MAILBOX0_ABORT_CB_ID :
  613. hcan->TxMailbox0AbortCallback = HAL_CAN_TxMailbox0AbortCallback;
  614. break;
  615. case HAL_CAN_TX_MAILBOX1_ABORT_CB_ID :
  616. hcan->TxMailbox1AbortCallback = HAL_CAN_TxMailbox1AbortCallback;
  617. break;
  618. case HAL_CAN_TX_MAILBOX2_ABORT_CB_ID :
  619. hcan->TxMailbox2AbortCallback = HAL_CAN_TxMailbox2AbortCallback;
  620. break;
  621. case HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID :
  622. hcan->RxFifo0MsgPendingCallback = HAL_CAN_RxFifo0MsgPendingCallback;
  623. break;
  624. case HAL_CAN_RX_FIFO0_FULL_CB_ID :
  625. hcan->RxFifo0FullCallback = HAL_CAN_RxFifo0FullCallback;
  626. break;
  627. case HAL_CAN_RX_FIFO1_MSG_PENDING_CB_ID :
  628. hcan->RxFifo1MsgPendingCallback = HAL_CAN_RxFifo1MsgPendingCallback;
  629. break;
  630. case HAL_CAN_RX_FIFO1_FULL_CB_ID :
  631. hcan->RxFifo1FullCallback = HAL_CAN_RxFifo1FullCallback;
  632. break;
  633. case HAL_CAN_SLEEP_CB_ID :
  634. hcan->SleepCallback = HAL_CAN_SleepCallback;
  635. break;
  636. case HAL_CAN_WAKEUP_FROM_RX_MSG_CB_ID :
  637. hcan->WakeUpFromRxMsgCallback = HAL_CAN_WakeUpFromRxMsgCallback;
  638. break;
  639. case HAL_CAN_ERROR_CB_ID :
  640. hcan->ErrorCallback = HAL_CAN_ErrorCallback;
  641. break;
  642. case HAL_CAN_MSPINIT_CB_ID :
  643. hcan->MspInitCallback = HAL_CAN_MspInit;
  644. break;
  645. case HAL_CAN_MSPDEINIT_CB_ID :
  646. hcan->MspDeInitCallback = HAL_CAN_MspDeInit;
  647. break;
  648. default :
  649. /* Update the error code */
  650. hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
  651. /* Return error status */
  652. status = HAL_ERROR;
  653. break;
  654. }
  655. }
  656. else if (hcan->State == HAL_CAN_STATE_RESET)
  657. {
  658. switch (CallbackID)
  659. {
  660. case HAL_CAN_MSPINIT_CB_ID :
  661. hcan->MspInitCallback = HAL_CAN_MspInit;
  662. break;
  663. case HAL_CAN_MSPDEINIT_CB_ID :
  664. hcan->MspDeInitCallback = HAL_CAN_MspDeInit;
  665. break;
  666. default :
  667. /* Update the error code */
  668. hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
  669. /* Return error status */
  670. status = HAL_ERROR;
  671. break;
  672. }
  673. }
  674. else
  675. {
  676. /* Update the error code */
  677. hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
  678. /* Return error status */
  679. status = HAL_ERROR;
  680. }
  681. return status;
  682. }
  683. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  684. /**
  685. * @}
  686. */
  687. /** @defgroup CAN_Exported_Functions_Group2 Configuration functions
  688. * @brief Configuration functions.
  689. *
  690. @verbatim
  691. ==============================================================================
  692. ##### Configuration functions #####
  693. ==============================================================================
  694. [..] This section provides functions allowing to:
  695. (+) HAL_CAN_ConfigFilter : Configure the CAN reception filters
  696. @endverbatim
  697. * @{
  698. */
  699. /**
  700. * @brief Configures the CAN reception filter according to the specified
  701. * parameters in the CAN_FilterInitStruct.
  702. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  703. * the configuration information for the specified CAN.
  704. * @param sFilterConfig pointer to a CAN_FilterTypeDef structure that
  705. * contains the filter configuration information.
  706. * @retval None
  707. */
  708. HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan, CAN_FilterTypeDef *sFilterConfig)
  709. {
  710. uint32_t filternbrbitpos;
  711. CAN_TypeDef *can_ip = hcan->Instance;
  712. HAL_CAN_StateTypeDef state = hcan->State;
  713. if ((state == HAL_CAN_STATE_READY) ||
  714. (state == HAL_CAN_STATE_LISTENING))
  715. {
  716. /* Check the parameters */
  717. assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdHigh));
  718. assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdLow));
  719. assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdHigh));
  720. assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdLow));
  721. assert_param(IS_CAN_FILTER_MODE(sFilterConfig->FilterMode));
  722. assert_param(IS_CAN_FILTER_SCALE(sFilterConfig->FilterScale));
  723. assert_param(IS_CAN_FILTER_FIFO(sFilterConfig->FilterFIFOAssignment));
  724. assert_param(IS_CAN_FILTER_ACTIVATION(sFilterConfig->FilterActivation));
  725. #if defined(CAN3)
  726. /* Check the CAN instance */
  727. if (hcan->Instance == CAN3)
  728. {
  729. /* CAN3 is single instance with 14 dedicated filters banks */
  730. /* Check the parameters */
  731. assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank));
  732. }
  733. else
  734. {
  735. /* CAN1 and CAN2 are dual instances with 28 common filters banks */
  736. /* Select master instance to access the filter banks */
  737. can_ip = CAN1;
  738. /* Check the parameters */
  739. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank));
  740. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank));
  741. }
  742. #elif defined(CAN2)
  743. /* CAN1 and CAN2 are dual instances with 28 common filters banks */
  744. /* Select master instance to access the filter banks */
  745. can_ip = CAN1;
  746. /* Check the parameters */
  747. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank));
  748. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank));
  749. #else
  750. /* CAN1 is single instance with 14 dedicated filters banks */
  751. /* Check the parameters */
  752. assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank));
  753. #endif
  754. /* Initialisation mode for the filter */
  755. SET_BIT(can_ip->FMR, CAN_FMR_FINIT);
  756. #if defined(CAN3)
  757. /* Check the CAN instance */
  758. if (can_ip == CAN1)
  759. {
  760. /* Select the start filter number of CAN2 slave instance */
  761. CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB);
  762. SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos);
  763. }
  764. #elif defined(CAN2)
  765. /* Select the start filter number of CAN2 slave instance */
  766. CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB);
  767. SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos);
  768. #endif
  769. /* Convert filter number into bit position */
  770. filternbrbitpos = (uint32_t)1 << (sFilterConfig->FilterBank & 0x1FU);
  771. /* Filter Deactivation */
  772. CLEAR_BIT(can_ip->FA1R, filternbrbitpos);
  773. /* Filter Scale */
  774. if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT)
  775. {
  776. /* 16-bit scale for the filter */
  777. CLEAR_BIT(can_ip->FS1R, filternbrbitpos);
  778. /* First 16-bit identifier and First 16-bit mask */
  779. /* Or First 16-bit identifier and Second 16-bit identifier */
  780. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
  781. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16U) |
  782. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
  783. /* Second 16-bit identifier and Second 16-bit mask */
  784. /* Or Third 16-bit identifier and Fourth 16-bit identifier */
  785. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
  786. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
  787. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh);
  788. }
  789. if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT)
  790. {
  791. /* 32-bit scale for the filter */
  792. SET_BIT(can_ip->FS1R, filternbrbitpos);
  793. /* 32-bit identifier or First 32-bit identifier */
  794. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
  795. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh) << 16U) |
  796. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
  797. /* 32-bit mask or Second 32-bit identifier */
  798. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
  799. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
  800. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow);
  801. }
  802. /* Filter Mode */
  803. if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK)
  804. {
  805. /* Id/Mask mode for the filter*/
  806. CLEAR_BIT(can_ip->FM1R, filternbrbitpos);
  807. }
  808. else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
  809. {
  810. /* Identifier list mode for the filter*/
  811. SET_BIT(can_ip->FM1R, filternbrbitpos);
  812. }
  813. /* Filter FIFO assignment */
  814. if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0)
  815. {
  816. /* FIFO 0 assignation for the filter */
  817. CLEAR_BIT(can_ip->FFA1R, filternbrbitpos);
  818. }
  819. else
  820. {
  821. /* FIFO 1 assignation for the filter */
  822. SET_BIT(can_ip->FFA1R, filternbrbitpos);
  823. }
  824. /* Filter activation */
  825. if (sFilterConfig->FilterActivation == CAN_FILTER_ENABLE)
  826. {
  827. SET_BIT(can_ip->FA1R, filternbrbitpos);
  828. }
  829. /* Leave the initialisation mode for the filter */
  830. CLEAR_BIT(can_ip->FMR, CAN_FMR_FINIT);
  831. /* Return function status */
  832. return HAL_OK;
  833. }
  834. else
  835. {
  836. /* Update error code */
  837. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  838. return HAL_ERROR;
  839. }
  840. }
  841. /**
  842. * @}
  843. */
  844. /** @defgroup CAN_Exported_Functions_Group3 Control functions
  845. * @brief Control functions
  846. *
  847. @verbatim
  848. ==============================================================================
  849. ##### Control functions #####
  850. ==============================================================================
  851. [..] This section provides functions allowing to:
  852. (+) HAL_CAN_Start : Start the CAN module
  853. (+) HAL_CAN_Stop : Stop the CAN module
  854. (+) HAL_CAN_RequestSleep : Request sleep mode entry.
  855. (+) HAL_CAN_WakeUp : Wake up from sleep mode.
  856. (+) HAL_CAN_IsSleepActive : Check is sleep mode is active.
  857. (+) HAL_CAN_AddTxMessage : Add a message to the Tx mailboxes
  858. and activate the corresponding
  859. transmission request
  860. (+) HAL_CAN_AbortTxRequest : Abort transmission request
  861. (+) HAL_CAN_GetTxMailboxesFreeLevel : Return Tx mailboxes free level
  862. (+) HAL_CAN_IsTxMessagePending : Check if a transmission request is
  863. pending on the selected Tx mailbox
  864. (+) HAL_CAN_GetRxMessage : Get a CAN frame from the Rx FIFO
  865. (+) HAL_CAN_GetRxFifoFillLevel : Return Rx FIFO fill level
  866. @endverbatim
  867. * @{
  868. */
  869. /**
  870. * @brief Start the CAN module.
  871. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  872. * the configuration information for the specified CAN.
  873. * @retval HAL status
  874. */
  875. HAL_StatusTypeDef HAL_CAN_Start(CAN_HandleTypeDef *hcan)
  876. {
  877. uint32_t tickstart;
  878. if (hcan->State == HAL_CAN_STATE_READY)
  879. {
  880. /* Change CAN peripheral state */
  881. hcan->State = HAL_CAN_STATE_LISTENING;
  882. /* Request leave initialisation */
  883. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
  884. /* Get tick */
  885. tickstart = HAL_GetTick();
  886. /* Wait the acknowledge */
  887. while ((hcan->Instance->MSR & CAN_MSR_INAK) != 0U)
  888. {
  889. /* Check for the Timeout */
  890. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  891. {
  892. /* Update error code */
  893. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  894. /* Change CAN state */
  895. hcan->State = HAL_CAN_STATE_ERROR;
  896. return HAL_ERROR;
  897. }
  898. }
  899. /* Reset the CAN ErrorCode */
  900. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  901. /* Return function status */
  902. return HAL_OK;
  903. }
  904. else
  905. {
  906. /* Update error code */
  907. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_READY;
  908. return HAL_ERROR;
  909. }
  910. }
  911. /**
  912. * @brief Stop the CAN module and enable access to configuration registers.
  913. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  914. * the configuration information for the specified CAN.
  915. * @retval HAL status
  916. */
  917. HAL_StatusTypeDef HAL_CAN_Stop(CAN_HandleTypeDef *hcan)
  918. {
  919. uint32_t tickstart;
  920. if (hcan->State == HAL_CAN_STATE_LISTENING)
  921. {
  922. /* Request initialisation */
  923. SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
  924. /* Get tick */
  925. tickstart = HAL_GetTick();
  926. /* Wait the acknowledge */
  927. while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U)
  928. {
  929. /* Check for the Timeout */
  930. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  931. {
  932. /* Update error code */
  933. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  934. /* Change CAN state */
  935. hcan->State = HAL_CAN_STATE_ERROR;
  936. return HAL_ERROR;
  937. }
  938. }
  939. /* Exit from sleep mode */
  940. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  941. /* Change CAN peripheral state */
  942. hcan->State = HAL_CAN_STATE_READY;
  943. /* Return function status */
  944. return HAL_OK;
  945. }
  946. else
  947. {
  948. /* Update error code */
  949. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_STARTED;
  950. return HAL_ERROR;
  951. }
  952. }
  953. /**
  954. * @brief Request the sleep mode (low power) entry.
  955. * When returning from this function, Sleep mode will be entered
  956. * as soon as the current CAN activity (transmission or reception
  957. * of a CAN frame) has been completed.
  958. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  959. * the configuration information for the specified CAN.
  960. * @retval HAL status.
  961. */
  962. HAL_StatusTypeDef HAL_CAN_RequestSleep(CAN_HandleTypeDef *hcan)
  963. {
  964. HAL_CAN_StateTypeDef state = hcan->State;
  965. if ((state == HAL_CAN_STATE_READY) ||
  966. (state == HAL_CAN_STATE_LISTENING))
  967. {
  968. /* Request Sleep mode */
  969. SET_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  970. /* Return function status */
  971. return HAL_OK;
  972. }
  973. else
  974. {
  975. /* Update error code */
  976. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  977. /* Return function status */
  978. return HAL_ERROR;
  979. }
  980. }
  981. /**
  982. * @brief Wake up from sleep mode.
  983. * When returning with HAL_OK status from this function, Sleep mode
  984. * is exited.
  985. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  986. * the configuration information for the specified CAN.
  987. * @retval HAL status.
  988. */
  989. HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan)
  990. {
  991. __IO uint32_t count = 0;
  992. uint32_t timeout = 1000000U;
  993. HAL_CAN_StateTypeDef state = hcan->State;
  994. if ((state == HAL_CAN_STATE_READY) ||
  995. (state == HAL_CAN_STATE_LISTENING))
  996. {
  997. /* Wake up request */
  998. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  999. /* Wait sleep mode is exited */
  1000. do
  1001. {
  1002. /* Increment counter */
  1003. count++;
  1004. /* Check if timeout is reached */
  1005. if (count > timeout)
  1006. {
  1007. /* Update error code */
  1008. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  1009. return HAL_ERROR;
  1010. }
  1011. }
  1012. while ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U);
  1013. /* Return function status */
  1014. return HAL_OK;
  1015. }
  1016. else
  1017. {
  1018. /* Update error code */
  1019. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1020. return HAL_ERROR;
  1021. }
  1022. }
  1023. /**
  1024. * @brief Check is sleep mode is active.
  1025. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1026. * the configuration information for the specified CAN.
  1027. * @retval Status
  1028. * - 0 : Sleep mode is not active.
  1029. * - 1 : Sleep mode is active.
  1030. */
  1031. uint32_t HAL_CAN_IsSleepActive(CAN_HandleTypeDef *hcan)
  1032. {
  1033. uint32_t status = 0U;
  1034. HAL_CAN_StateTypeDef state = hcan->State;
  1035. if ((state == HAL_CAN_STATE_READY) ||
  1036. (state == HAL_CAN_STATE_LISTENING))
  1037. {
  1038. /* Check Sleep mode */
  1039. if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
  1040. {
  1041. status = 1U;
  1042. }
  1043. }
  1044. /* Return function status */
  1045. return status;
  1046. }
  1047. /**
  1048. * @brief Add a message to the first free Tx mailbox and activate the
  1049. * corresponding transmission request.
  1050. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1051. * the configuration information for the specified CAN.
  1052. * @param pHeader pointer to a CAN_TxHeaderTypeDef structure.
  1053. * @param aData array containing the payload of the Tx frame.
  1054. * @param pTxMailbox pointer to a variable where the function will return
  1055. * the TxMailbox used to store the Tx message.
  1056. * This parameter can be a value of @arg CAN_Tx_Mailboxes.
  1057. * @retval HAL status
  1058. */
  1059. HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderTypeDef *pHeader, uint8_t aData[], uint32_t *pTxMailbox)
  1060. {
  1061. uint32_t transmitmailbox;
  1062. HAL_CAN_StateTypeDef state = hcan->State;
  1063. uint32_t tsr = READ_REG(hcan->Instance->TSR);
  1064. /* Check the parameters */
  1065. assert_param(IS_CAN_IDTYPE(pHeader->IDE));
  1066. assert_param(IS_CAN_RTR(pHeader->RTR));
  1067. assert_param(IS_CAN_DLC(pHeader->DLC));
  1068. if (pHeader->IDE == CAN_ID_STD)
  1069. {
  1070. assert_param(IS_CAN_STDID(pHeader->StdId));
  1071. }
  1072. else
  1073. {
  1074. assert_param(IS_CAN_EXTID(pHeader->ExtId));
  1075. }
  1076. assert_param(IS_FUNCTIONAL_STATE(pHeader->TransmitGlobalTime));
  1077. if ((state == HAL_CAN_STATE_READY) ||
  1078. (state == HAL_CAN_STATE_LISTENING))
  1079. {
  1080. /* Check that all the Tx mailboxes are not full */
  1081. if (((tsr & CAN_TSR_TME0) != 0U) ||
  1082. ((tsr & CAN_TSR_TME1) != 0U) ||
  1083. ((tsr & CAN_TSR_TME2) != 0U))
  1084. {
  1085. /* Select an empty transmit mailbox */
  1086. transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;
  1087. /* Check transmit mailbox value */
  1088. if (transmitmailbox > 2U)
  1089. {
  1090. /* Update error code */
  1091. hcan->ErrorCode |= HAL_CAN_ERROR_INTERNAL;
  1092. return HAL_ERROR;
  1093. }
  1094. /* Store the Tx mailbox */
  1095. *pTxMailbox = (uint32_t)1 << transmitmailbox;
  1096. /* Set up the Id */
  1097. if (pHeader->IDE == CAN_ID_STD)
  1098. {
  1099. hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) |
  1100. pHeader->RTR);
  1101. }
  1102. else
  1103. {
  1104. hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->ExtId << CAN_TI0R_EXID_Pos) |
  1105. pHeader->IDE |
  1106. pHeader->RTR);
  1107. }
  1108. /* Set up the DLC */
  1109. hcan->Instance->sTxMailBox[transmitmailbox].TDTR = (pHeader->DLC);
  1110. /* Set up the Transmit Global Time mode */
  1111. if (pHeader->TransmitGlobalTime == ENABLE)
  1112. {
  1113. SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TDTR, CAN_TDT0R_TGT);
  1114. }
  1115. /* Set up the data field */
  1116. WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDHR,
  1117. ((uint32_t)aData[7] << CAN_TDH0R_DATA7_Pos) |
  1118. ((uint32_t)aData[6] << CAN_TDH0R_DATA6_Pos) |
  1119. ((uint32_t)aData[5] << CAN_TDH0R_DATA5_Pos) |
  1120. ((uint32_t)aData[4] << CAN_TDH0R_DATA4_Pos));
  1121. WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDLR,
  1122. ((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) |
  1123. ((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) |
  1124. ((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) |
  1125. ((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos));
  1126. /* Request transmission */
  1127. SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TIR, CAN_TI0R_TXRQ);
  1128. /* Return function status */
  1129. return HAL_OK;
  1130. }
  1131. else
  1132. {
  1133. /* Update error code */
  1134. hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
  1135. return HAL_ERROR;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. /* Update error code */
  1141. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1142. return HAL_ERROR;
  1143. }
  1144. }
  1145. /**
  1146. * @brief Abort transmission requests
  1147. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1148. * the configuration information for the specified CAN.
  1149. * @param TxMailboxes List of the Tx Mailboxes to abort.
  1150. * This parameter can be any combination of @arg CAN_Tx_Mailboxes.
  1151. * @retval HAL status
  1152. */
  1153. HAL_StatusTypeDef HAL_CAN_AbortTxRequest(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
  1154. {
  1155. HAL_CAN_StateTypeDef state = hcan->State;
  1156. /* Check function parameters */
  1157. assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
  1158. if ((state == HAL_CAN_STATE_READY) ||
  1159. (state == HAL_CAN_STATE_LISTENING))
  1160. {
  1161. /* Check Tx Mailbox 0 */
  1162. if ((TxMailboxes & CAN_TX_MAILBOX0) != 0U)
  1163. {
  1164. /* Add cancellation request for Tx Mailbox 0 */
  1165. SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ0);
  1166. }
  1167. /* Check Tx Mailbox 1 */
  1168. if ((TxMailboxes & CAN_TX_MAILBOX1) != 0U)
  1169. {
  1170. /* Add cancellation request for Tx Mailbox 1 */
  1171. SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ1);
  1172. }
  1173. /* Check Tx Mailbox 2 */
  1174. if ((TxMailboxes & CAN_TX_MAILBOX2) != 0U)
  1175. {
  1176. /* Add cancellation request for Tx Mailbox 2 */
  1177. SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ2);
  1178. }
  1179. /* Return function status */
  1180. return HAL_OK;
  1181. }
  1182. else
  1183. {
  1184. /* Update error code */
  1185. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1186. return HAL_ERROR;
  1187. }
  1188. }
  1189. /**
  1190. * @brief Return Tx Mailboxes free level: number of free Tx Mailboxes.
  1191. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1192. * the configuration information for the specified CAN.
  1193. * @retval Number of free Tx Mailboxes.
  1194. */
  1195. uint32_t HAL_CAN_GetTxMailboxesFreeLevel(CAN_HandleTypeDef *hcan)
  1196. {
  1197. uint32_t freelevel = 0U;
  1198. HAL_CAN_StateTypeDef state = hcan->State;
  1199. if ((state == HAL_CAN_STATE_READY) ||
  1200. (state == HAL_CAN_STATE_LISTENING))
  1201. {
  1202. /* Check Tx Mailbox 0 status */
  1203. if ((hcan->Instance->TSR & CAN_TSR_TME0) != 0U)
  1204. {
  1205. freelevel++;
  1206. }
  1207. /* Check Tx Mailbox 1 status */
  1208. if ((hcan->Instance->TSR & CAN_TSR_TME1) != 0U)
  1209. {
  1210. freelevel++;
  1211. }
  1212. /* Check Tx Mailbox 2 status */
  1213. if ((hcan->Instance->TSR & CAN_TSR_TME2) != 0U)
  1214. {
  1215. freelevel++;
  1216. }
  1217. }
  1218. /* Return Tx Mailboxes free level */
  1219. return freelevel;
  1220. }
  1221. /**
  1222. * @brief Check if a transmission request is pending on the selected Tx
  1223. * Mailboxes.
  1224. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1225. * the configuration information for the specified CAN.
  1226. * @param TxMailboxes List of Tx Mailboxes to check.
  1227. * This parameter can be any combination of @arg CAN_Tx_Mailboxes.
  1228. * @retval Status
  1229. * - 0 : No pending transmission request on any selected Tx Mailboxes.
  1230. * - 1 : Pending transmission request on at least one of the selected
  1231. * Tx Mailbox.
  1232. */
  1233. uint32_t HAL_CAN_IsTxMessagePending(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
  1234. {
  1235. uint32_t status = 0U;
  1236. HAL_CAN_StateTypeDef state = hcan->State;
  1237. /* Check function parameters */
  1238. assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
  1239. if ((state == HAL_CAN_STATE_READY) ||
  1240. (state == HAL_CAN_STATE_LISTENING))
  1241. {
  1242. /* Check pending transmission request on the selected Tx Mailboxes */
  1243. if ((hcan->Instance->TSR & (TxMailboxes << CAN_TSR_TME0_Pos)) != (TxMailboxes << CAN_TSR_TME0_Pos))
  1244. {
  1245. status = 1U;
  1246. }
  1247. }
  1248. /* Return status */
  1249. return status;
  1250. }
  1251. /**
  1252. * @brief Return timestamp of Tx message sent, if time triggered communication
  1253. mode is enabled.
  1254. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1255. * the configuration information for the specified CAN.
  1256. * @param TxMailbox Tx Mailbox where the timestamp of message sent will be
  1257. * read.
  1258. * This parameter can be one value of @arg CAN_Tx_Mailboxes.
  1259. * @retval Timestamp of message sent from Tx Mailbox.
  1260. */
  1261. uint32_t HAL_CAN_GetTxTimestamp(CAN_HandleTypeDef *hcan, uint32_t TxMailbox)
  1262. {
  1263. uint32_t timestamp = 0U;
  1264. uint32_t transmitmailbox;
  1265. HAL_CAN_StateTypeDef state = hcan->State;
  1266. /* Check function parameters */
  1267. assert_param(IS_CAN_TX_MAILBOX(TxMailbox));
  1268. if ((state == HAL_CAN_STATE_READY) ||
  1269. (state == HAL_CAN_STATE_LISTENING))
  1270. {
  1271. /* Select the Tx mailbox */
  1272. transmitmailbox = POSITION_VAL(TxMailbox);
  1273. /* Get timestamp */
  1274. timestamp = (hcan->Instance->sTxMailBox[transmitmailbox].TDTR & CAN_TDT0R_TIME) >> CAN_TDT0R_TIME_Pos;
  1275. }
  1276. /* Return the timestamp */
  1277. return timestamp;
  1278. }
  1279. /**
  1280. * @brief Get an CAN frame from the Rx FIFO zone into the message RAM.
  1281. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1282. * the configuration information for the specified CAN.
  1283. * @param RxFifo Fifo number of the received message to be read.
  1284. * This parameter can be a value of @arg CAN_receive_FIFO_number.
  1285. * @param pHeader pointer to a CAN_RxHeaderTypeDef structure where the header
  1286. * of the Rx frame will be stored.
  1287. * @param aData array where the payload of the Rx frame will be stored.
  1288. * @retval HAL status
  1289. */
  1290. HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo, CAN_RxHeaderTypeDef *pHeader, uint8_t aData[])
  1291. {
  1292. HAL_CAN_StateTypeDef state = hcan->State;
  1293. assert_param(IS_CAN_RX_FIFO(RxFifo));
  1294. if ((state == HAL_CAN_STATE_READY) ||
  1295. (state == HAL_CAN_STATE_LISTENING))
  1296. {
  1297. /* Check the Rx FIFO */
  1298. if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
  1299. {
  1300. /* Check that the Rx FIFO 0 is not empty */
  1301. if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) == 0U)
  1302. {
  1303. /* Update error code */
  1304. hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
  1305. return HAL_ERROR;
  1306. }
  1307. }
  1308. else /* Rx element is assigned to Rx FIFO 1 */
  1309. {
  1310. /* Check that the Rx FIFO 1 is not empty */
  1311. if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) == 0U)
  1312. {
  1313. /* Update error code */
  1314. hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
  1315. return HAL_ERROR;
  1316. }
  1317. }
  1318. /* Get the header */
  1319. pHeader->IDE = CAN_RI0R_IDE & hcan->Instance->sFIFOMailBox[RxFifo].RIR;
  1320. if (pHeader->IDE == CAN_ID_STD)
  1321. {
  1322. pHeader->StdId = (CAN_RI0R_STID & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_TI0R_STID_Pos;
  1323. }
  1324. else
  1325. {
  1326. pHeader->ExtId = ((CAN_RI0R_EXID | CAN_RI0R_STID) & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_EXID_Pos;
  1327. }
  1328. pHeader->RTR = (CAN_RI0R_RTR & hcan->Instance->sFIFOMailBox[RxFifo].RIR);
  1329. pHeader->DLC = (CAN_RDT0R_DLC & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos;
  1330. pHeader->FilterMatchIndex = (CAN_RDT0R_FMI & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_FMI_Pos;
  1331. pHeader->Timestamp = (CAN_RDT0R_TIME & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_TIME_Pos;
  1332. /* Get the data */
  1333. aData[0] = (uint8_t)((CAN_RDL0R_DATA0 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA0_Pos);
  1334. aData[1] = (uint8_t)((CAN_RDL0R_DATA1 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA1_Pos);
  1335. aData[2] = (uint8_t)((CAN_RDL0R_DATA2 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA2_Pos);
  1336. aData[3] = (uint8_t)((CAN_RDL0R_DATA3 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA3_Pos);
  1337. aData[4] = (uint8_t)((CAN_RDH0R_DATA4 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA4_Pos);
  1338. aData[5] = (uint8_t)((CAN_RDH0R_DATA5 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA5_Pos);
  1339. aData[6] = (uint8_t)((CAN_RDH0R_DATA6 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA6_Pos);
  1340. aData[7] = (uint8_t)((CAN_RDH0R_DATA7 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA7_Pos);
  1341. /* Release the FIFO */
  1342. if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
  1343. {
  1344. /* Release RX FIFO 0 */
  1345. SET_BIT(hcan->Instance->RF0R, CAN_RF0R_RFOM0);
  1346. }
  1347. else /* Rx element is assigned to Rx FIFO 1 */
  1348. {
  1349. /* Release RX FIFO 1 */
  1350. SET_BIT(hcan->Instance->RF1R, CAN_RF1R_RFOM1);
  1351. }
  1352. /* Return function status */
  1353. return HAL_OK;
  1354. }
  1355. else
  1356. {
  1357. /* Update error code */
  1358. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1359. return HAL_ERROR;
  1360. }
  1361. }
  1362. /**
  1363. * @brief Return Rx FIFO fill level.
  1364. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1365. * the configuration information for the specified CAN.
  1366. * @param RxFifo Rx FIFO.
  1367. * This parameter can be a value of @arg CAN_receive_FIFO_number.
  1368. * @retval Number of messages available in Rx FIFO.
  1369. */
  1370. uint32_t HAL_CAN_GetRxFifoFillLevel(CAN_HandleTypeDef *hcan, uint32_t RxFifo)
  1371. {
  1372. uint32_t filllevel = 0U;
  1373. HAL_CAN_StateTypeDef state = hcan->State;
  1374. /* Check function parameters */
  1375. assert_param(IS_CAN_RX_FIFO(RxFifo));
  1376. if ((state == HAL_CAN_STATE_READY) ||
  1377. (state == HAL_CAN_STATE_LISTENING))
  1378. {
  1379. if (RxFifo == CAN_RX_FIFO0)
  1380. {
  1381. filllevel = hcan->Instance->RF0R & CAN_RF0R_FMP0;
  1382. }
  1383. else /* RxFifo == CAN_RX_FIFO1 */
  1384. {
  1385. filllevel = hcan->Instance->RF1R & CAN_RF1R_FMP1;
  1386. }
  1387. }
  1388. /* Return Rx FIFO fill level */
  1389. return filllevel;
  1390. }
  1391. /**
  1392. * @}
  1393. */
  1394. /** @defgroup CAN_Exported_Functions_Group4 Interrupts management
  1395. * @brief Interrupts management
  1396. *
  1397. @verbatim
  1398. ==============================================================================
  1399. ##### Interrupts management #####
  1400. ==============================================================================
  1401. [..] This section provides functions allowing to:
  1402. (+) HAL_CAN_ActivateNotification : Enable interrupts
  1403. (+) HAL_CAN_DeactivateNotification : Disable interrupts
  1404. (+) HAL_CAN_IRQHandler : Handles CAN interrupt request
  1405. @endverbatim
  1406. * @{
  1407. */
  1408. /**
  1409. * @brief Enable interrupts.
  1410. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1411. * the configuration information for the specified CAN.
  1412. * @param ActiveITs indicates which interrupts will be enabled.
  1413. * This parameter can be any combination of @arg CAN_Interrupts.
  1414. * @retval HAL status
  1415. */
  1416. HAL_StatusTypeDef HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t ActiveITs)
  1417. {
  1418. HAL_CAN_StateTypeDef state = hcan->State;
  1419. /* Check function parameters */
  1420. assert_param(IS_CAN_IT(ActiveITs));
  1421. if ((state == HAL_CAN_STATE_READY) ||
  1422. (state == HAL_CAN_STATE_LISTENING))
  1423. {
  1424. /* Enable the selected interrupts */
  1425. __HAL_CAN_ENABLE_IT(hcan, ActiveITs);
  1426. /* Return function status */
  1427. return HAL_OK;
  1428. }
  1429. else
  1430. {
  1431. /* Update error code */
  1432. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1433. return HAL_ERROR;
  1434. }
  1435. }
  1436. /**
  1437. * @brief Disable interrupts.
  1438. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1439. * the configuration information for the specified CAN.
  1440. * @param InactiveITs indicates which interrupts will be disabled.
  1441. * This parameter can be any combination of @arg CAN_Interrupts.
  1442. * @retval HAL status
  1443. */
  1444. HAL_StatusTypeDef HAL_CAN_DeactivateNotification(CAN_HandleTypeDef *hcan, uint32_t InactiveITs)
  1445. {
  1446. HAL_CAN_StateTypeDef state = hcan->State;
  1447. /* Check function parameters */
  1448. assert_param(IS_CAN_IT(InactiveITs));
  1449. if ((state == HAL_CAN_STATE_READY) ||
  1450. (state == HAL_CAN_STATE_LISTENING))
  1451. {
  1452. /* Disable the selected interrupts */
  1453. __HAL_CAN_DISABLE_IT(hcan, InactiveITs);
  1454. /* Return function status */
  1455. return HAL_OK;
  1456. }
  1457. else
  1458. {
  1459. /* Update error code */
  1460. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1461. return HAL_ERROR;
  1462. }
  1463. }
  1464. /**
  1465. * @brief Handles CAN interrupt request
  1466. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1467. * the configuration information for the specified CAN.
  1468. * @retval None
  1469. */
  1470. void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan)
  1471. {
  1472. uint32_t errorcode = HAL_CAN_ERROR_NONE;
  1473. uint32_t interrupts = READ_REG(hcan->Instance->IER);
  1474. uint32_t msrflags = READ_REG(hcan->Instance->MSR);
  1475. uint32_t tsrflags = READ_REG(hcan->Instance->TSR);
  1476. uint32_t rf0rflags = READ_REG(hcan->Instance->RF0R);
  1477. uint32_t rf1rflags = READ_REG(hcan->Instance->RF1R);
  1478. uint32_t esrflags = READ_REG(hcan->Instance->ESR);
  1479. /* Transmit Mailbox empty interrupt management *****************************/
  1480. if ((interrupts & CAN_IT_TX_MAILBOX_EMPTY) != 0U)
  1481. {
  1482. /* Transmit Mailbox 0 management *****************************************/
  1483. if ((tsrflags & CAN_TSR_RQCP0) != 0U)
  1484. {
  1485. /* Clear the Transmission Complete flag (and TXOK0,ALST0,TERR0 bits) */
  1486. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP0);
  1487. if ((tsrflags & CAN_TSR_TXOK0) != 0U)
  1488. {
  1489. /* Transmission Mailbox 0 complete callback */
  1490. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1491. /* Call registered callback*/
  1492. hcan->TxMailbox0CompleteCallback(hcan);
  1493. #else
  1494. /* Call weak (surcharged) callback */
  1495. HAL_CAN_TxMailbox0CompleteCallback(hcan);
  1496. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1497. }
  1498. else
  1499. {
  1500. if ((tsrflags & CAN_TSR_ALST0) != 0U)
  1501. {
  1502. /* Update error code */
  1503. errorcode |= HAL_CAN_ERROR_TX_ALST0;
  1504. }
  1505. else if ((tsrflags & CAN_TSR_TERR0) != 0U)
  1506. {
  1507. /* Update error code */
  1508. errorcode |= HAL_CAN_ERROR_TX_TERR0;
  1509. }
  1510. else
  1511. {
  1512. /* Transmission Mailbox 0 abort callback */
  1513. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1514. /* Call registered callback*/
  1515. hcan->TxMailbox0AbortCallback(hcan);
  1516. #else
  1517. /* Call weak (surcharged) callback */
  1518. HAL_CAN_TxMailbox0AbortCallback(hcan);
  1519. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1520. }
  1521. }
  1522. }
  1523. /* Transmit Mailbox 1 management *****************************************/
  1524. if ((tsrflags & CAN_TSR_RQCP1) != 0U)
  1525. {
  1526. /* Clear the Transmission Complete flag (and TXOK1,ALST1,TERR1 bits) */
  1527. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP1);
  1528. if ((tsrflags & CAN_TSR_TXOK1) != 0U)
  1529. {
  1530. /* Transmission Mailbox 1 complete callback */
  1531. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1532. /* Call registered callback*/
  1533. hcan->TxMailbox1CompleteCallback(hcan);
  1534. #else
  1535. /* Call weak (surcharged) callback */
  1536. HAL_CAN_TxMailbox1CompleteCallback(hcan);
  1537. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1538. }
  1539. else
  1540. {
  1541. if ((tsrflags & CAN_TSR_ALST1) != 0U)
  1542. {
  1543. /* Update error code */
  1544. errorcode |= HAL_CAN_ERROR_TX_ALST1;
  1545. }
  1546. else if ((tsrflags & CAN_TSR_TERR1) != 0U)
  1547. {
  1548. /* Update error code */
  1549. errorcode |= HAL_CAN_ERROR_TX_TERR1;
  1550. }
  1551. else
  1552. {
  1553. /* Transmission Mailbox 1 abort callback */
  1554. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1555. /* Call registered callback*/
  1556. hcan->TxMailbox1AbortCallback(hcan);
  1557. #else
  1558. /* Call weak (surcharged) callback */
  1559. HAL_CAN_TxMailbox1AbortCallback(hcan);
  1560. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1561. }
  1562. }
  1563. }
  1564. /* Transmit Mailbox 2 management *****************************************/
  1565. if ((tsrflags & CAN_TSR_RQCP2) != 0U)
  1566. {
  1567. /* Clear the Transmission Complete flag (and TXOK2,ALST2,TERR2 bits) */
  1568. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP2);
  1569. if ((tsrflags & CAN_TSR_TXOK2) != 0U)
  1570. {
  1571. /* Transmission Mailbox 2 complete callback */
  1572. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1573. /* Call registered callback*/
  1574. hcan->TxMailbox2CompleteCallback(hcan);
  1575. #else
  1576. /* Call weak (surcharged) callback */
  1577. HAL_CAN_TxMailbox2CompleteCallback(hcan);
  1578. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1579. }
  1580. else
  1581. {
  1582. if ((tsrflags & CAN_TSR_ALST2) != 0U)
  1583. {
  1584. /* Update error code */
  1585. errorcode |= HAL_CAN_ERROR_TX_ALST2;
  1586. }
  1587. else if ((tsrflags & CAN_TSR_TERR2) != 0U)
  1588. {
  1589. /* Update error code */
  1590. errorcode |= HAL_CAN_ERROR_TX_TERR2;
  1591. }
  1592. else
  1593. {
  1594. /* Transmission Mailbox 2 abort callback */
  1595. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1596. /* Call registered callback*/
  1597. hcan->TxMailbox2AbortCallback(hcan);
  1598. #else
  1599. /* Call weak (surcharged) callback */
  1600. HAL_CAN_TxMailbox2AbortCallback(hcan);
  1601. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1602. }
  1603. }
  1604. }
  1605. }
  1606. /* Receive FIFO 0 overrun interrupt management *****************************/
  1607. if ((interrupts & CAN_IT_RX_FIFO0_OVERRUN) != 0U)
  1608. {
  1609. if ((rf0rflags & CAN_RF0R_FOVR0) != 0U)
  1610. {
  1611. /* Set CAN error code to Rx Fifo 0 overrun error */
  1612. errorcode |= HAL_CAN_ERROR_RX_FOV0;
  1613. /* Clear FIFO0 Overrun Flag */
  1614. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
  1615. }
  1616. }
  1617. /* Receive FIFO 0 full interrupt management ********************************/
  1618. if ((interrupts & CAN_IT_RX_FIFO0_FULL) != 0U)
  1619. {
  1620. if ((rf0rflags & CAN_RF0R_FULL0) != 0U)
  1621. {
  1622. /* Clear FIFO 0 full Flag */
  1623. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0);
  1624. /* Receive FIFO 0 full Callback */
  1625. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1626. /* Call registered callback*/
  1627. hcan->RxFifo0FullCallback(hcan);
  1628. #else
  1629. /* Call weak (surcharged) callback */
  1630. HAL_CAN_RxFifo0FullCallback(hcan);
  1631. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1632. }
  1633. }
  1634. /* Receive FIFO 0 message pending interrupt management *********************/
  1635. if ((interrupts & CAN_IT_RX_FIFO0_MSG_PENDING) != 0U)
  1636. {
  1637. /* Check if message is still pending */
  1638. if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) != 0U)
  1639. {
  1640. /* Receive FIFO 0 mesage pending Callback */
  1641. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1642. /* Call registered callback*/
  1643. hcan->RxFifo0MsgPendingCallback(hcan);
  1644. #else
  1645. /* Call weak (surcharged) callback */
  1646. HAL_CAN_RxFifo0MsgPendingCallback(hcan);
  1647. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1648. }
  1649. }
  1650. /* Receive FIFO 1 overrun interrupt management *****************************/
  1651. if ((interrupts & CAN_IT_RX_FIFO1_OVERRUN) != 0U)
  1652. {
  1653. if ((rf1rflags & CAN_RF1R_FOVR1) != 0U)
  1654. {
  1655. /* Set CAN error code to Rx Fifo 1 overrun error */
  1656. errorcode |= HAL_CAN_ERROR_RX_FOV1;
  1657. /* Clear FIFO1 Overrun Flag */
  1658. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV1);
  1659. }
  1660. }
  1661. /* Receive FIFO 1 full interrupt management ********************************/
  1662. if ((interrupts & CAN_IT_RX_FIFO1_FULL) != 0U)
  1663. {
  1664. if ((rf1rflags & CAN_RF1R_FULL1) != 0U)
  1665. {
  1666. /* Clear FIFO 1 full Flag */
  1667. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF1);
  1668. /* Receive FIFO 1 full Callback */
  1669. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1670. /* Call registered callback*/
  1671. hcan->RxFifo1FullCallback(hcan);
  1672. #else
  1673. /* Call weak (surcharged) callback */
  1674. HAL_CAN_RxFifo1FullCallback(hcan);
  1675. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1676. }
  1677. }
  1678. /* Receive FIFO 1 message pending interrupt management *********************/
  1679. if ((interrupts & CAN_IT_RX_FIFO1_MSG_PENDING) != 0U)
  1680. {
  1681. /* Check if message is still pending */
  1682. if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) != 0U)
  1683. {
  1684. /* Receive FIFO 1 mesage pending Callback */
  1685. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1686. /* Call registered callback*/
  1687. hcan->RxFifo1MsgPendingCallback(hcan);
  1688. #else
  1689. /* Call weak (surcharged) callback */
  1690. HAL_CAN_RxFifo1MsgPendingCallback(hcan);
  1691. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1692. }
  1693. }
  1694. /* Sleep interrupt management *********************************************/
  1695. if ((interrupts & CAN_IT_SLEEP_ACK) != 0U)
  1696. {
  1697. if ((msrflags & CAN_MSR_SLAKI) != 0U)
  1698. {
  1699. /* Clear Sleep interrupt Flag */
  1700. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_SLAKI);
  1701. /* Sleep Callback */
  1702. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1703. /* Call registered callback*/
  1704. hcan->SleepCallback(hcan);
  1705. #else
  1706. /* Call weak (surcharged) callback */
  1707. HAL_CAN_SleepCallback(hcan);
  1708. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1709. }
  1710. }
  1711. /* WakeUp interrupt management *********************************************/
  1712. if ((interrupts & CAN_IT_WAKEUP) != 0U)
  1713. {
  1714. if ((msrflags & CAN_MSR_WKUI) != 0U)
  1715. {
  1716. /* Clear WakeUp Flag */
  1717. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_WKU);
  1718. /* WakeUp Callback */
  1719. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1720. /* Call registered callback*/
  1721. hcan->WakeUpFromRxMsgCallback(hcan);
  1722. #else
  1723. /* Call weak (surcharged) callback */
  1724. HAL_CAN_WakeUpFromRxMsgCallback(hcan);
  1725. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1726. }
  1727. }
  1728. /* Error interrupts management *********************************************/
  1729. if ((interrupts & CAN_IT_ERROR) != 0U)
  1730. {
  1731. if ((msrflags & CAN_MSR_ERRI) != 0U)
  1732. {
  1733. /* Check Error Warning Flag */
  1734. if (((interrupts & CAN_IT_ERROR_WARNING) != 0U) &&
  1735. ((esrflags & CAN_ESR_EWGF) != 0U))
  1736. {
  1737. /* Set CAN error code to Error Warning */
  1738. errorcode |= HAL_CAN_ERROR_EWG;
  1739. /* No need for clear of Error Warning Flag as read-only */
  1740. }
  1741. /* Check Error Passive Flag */
  1742. if (((interrupts & CAN_IT_ERROR_PASSIVE) != 0U) &&
  1743. ((esrflags & CAN_ESR_EPVF) != 0U))
  1744. {
  1745. /* Set CAN error code to Error Passive */
  1746. errorcode |= HAL_CAN_ERROR_EPV;
  1747. /* No need for clear of Error Passive Flag as read-only */
  1748. }
  1749. /* Check Bus-off Flag */
  1750. if (((interrupts & CAN_IT_BUSOFF) != 0U) &&
  1751. ((esrflags & CAN_ESR_BOFF) != 0U))
  1752. {
  1753. /* Set CAN error code to Bus-Off */
  1754. errorcode |= HAL_CAN_ERROR_BOF;
  1755. /* No need for clear of Error Bus-Off as read-only */
  1756. }
  1757. /* Check Last Error Code Flag */
  1758. if (((interrupts & CAN_IT_LAST_ERROR_CODE) != 0U) &&
  1759. ((esrflags & CAN_ESR_LEC) != 0U))
  1760. {
  1761. switch (esrflags & CAN_ESR_LEC)
  1762. {
  1763. case (CAN_ESR_LEC_0):
  1764. /* Set CAN error code to Stuff error */
  1765. errorcode |= HAL_CAN_ERROR_STF;
  1766. break;
  1767. case (CAN_ESR_LEC_1):
  1768. /* Set CAN error code to Form error */
  1769. errorcode |= HAL_CAN_ERROR_FOR;
  1770. break;
  1771. case (CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
  1772. /* Set CAN error code to Acknowledgement error */
  1773. errorcode |= HAL_CAN_ERROR_ACK;
  1774. break;
  1775. case (CAN_ESR_LEC_2):
  1776. /* Set CAN error code to Bit recessive error */
  1777. errorcode |= HAL_CAN_ERROR_BR;
  1778. break;
  1779. case (CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
  1780. /* Set CAN error code to Bit Dominant error */
  1781. errorcode |= HAL_CAN_ERROR_BD;
  1782. break;
  1783. case (CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
  1784. /* Set CAN error code to CRC error */
  1785. errorcode |= HAL_CAN_ERROR_CRC;
  1786. break;
  1787. default:
  1788. break;
  1789. }
  1790. /* Clear Last error code Flag */
  1791. CLEAR_BIT(hcan->Instance->ESR, CAN_ESR_LEC);
  1792. }
  1793. }
  1794. /* Clear ERRI Flag */
  1795. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_ERRI);
  1796. }
  1797. /* Call the Error call Back in case of Errors */
  1798. if (errorcode != HAL_CAN_ERROR_NONE)
  1799. {
  1800. /* Update error code in handle */
  1801. hcan->ErrorCode |= errorcode;
  1802. /* Call Error callback function */
  1803. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1804. /* Call registered callback*/
  1805. hcan->ErrorCallback(hcan);
  1806. #else
  1807. /* Call weak (surcharged) callback */
  1808. HAL_CAN_ErrorCallback(hcan);
  1809. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1810. }
  1811. }
  1812. /**
  1813. * @}
  1814. */
  1815. /** @defgroup CAN_Exported_Functions_Group5 Callback functions
  1816. * @brief CAN Callback functions
  1817. *
  1818. @verbatim
  1819. ==============================================================================
  1820. ##### Callback functions #####
  1821. ==============================================================================
  1822. [..]
  1823. This subsection provides the following callback functions:
  1824. (+) HAL_CAN_TxMailbox0CompleteCallback
  1825. (+) HAL_CAN_TxMailbox1CompleteCallback
  1826. (+) HAL_CAN_TxMailbox2CompleteCallback
  1827. (+) HAL_CAN_TxMailbox0AbortCallback
  1828. (+) HAL_CAN_TxMailbox1AbortCallback
  1829. (+) HAL_CAN_TxMailbox2AbortCallback
  1830. (+) HAL_CAN_RxFifo0MsgPendingCallback
  1831. (+) HAL_CAN_RxFifo0FullCallback
  1832. (+) HAL_CAN_RxFifo1MsgPendingCallback
  1833. (+) HAL_CAN_RxFifo1FullCallback
  1834. (+) HAL_CAN_SleepCallback
  1835. (+) HAL_CAN_WakeUpFromRxMsgCallback
  1836. (+) HAL_CAN_ErrorCallback
  1837. @endverbatim
  1838. * @{
  1839. */
  1840. /**
  1841. * @brief Transmission Mailbox 0 complete callback.
  1842. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1843. * the configuration information for the specified CAN.
  1844. * @retval None
  1845. */
  1846. __weak void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
  1847. {
  1848. /* Prevent unused argument(s) compilation warning */
  1849. UNUSED(hcan);
  1850. /* NOTE : This function Should not be modified, when the callback is needed,
  1851. the HAL_CAN_TxMailbox0CompleteCallback could be implemented in the
  1852. user file
  1853. */
  1854. }
  1855. /**
  1856. * @brief Transmission Mailbox 1 complete callback.
  1857. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1858. * the configuration information for the specified CAN.
  1859. * @retval None
  1860. */
  1861. __weak void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
  1862. {
  1863. /* Prevent unused argument(s) compilation warning */
  1864. UNUSED(hcan);
  1865. /* NOTE : This function Should not be modified, when the callback is needed,
  1866. the HAL_CAN_TxMailbox1CompleteCallback could be implemented in the
  1867. user file
  1868. */
  1869. }
  1870. /**
  1871. * @brief Transmission Mailbox 2 complete callback.
  1872. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1873. * the configuration information for the specified CAN.
  1874. * @retval None
  1875. */
  1876. __weak void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
  1877. {
  1878. /* Prevent unused argument(s) compilation warning */
  1879. UNUSED(hcan);
  1880. /* NOTE : This function Should not be modified, when the callback is needed,
  1881. the HAL_CAN_TxMailbox2CompleteCallback could be implemented in the
  1882. user file
  1883. */
  1884. }
  1885. /**
  1886. * @brief Transmission Mailbox 0 Cancellation callback.
  1887. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1888. * the configuration information for the specified CAN.
  1889. * @retval None
  1890. */
  1891. __weak void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan)
  1892. {
  1893. /* Prevent unused argument(s) compilation warning */
  1894. UNUSED(hcan);
  1895. /* NOTE : This function Should not be modified, when the callback is needed,
  1896. the HAL_CAN_TxMailbox0AbortCallback could be implemented in the
  1897. user file
  1898. */
  1899. }
  1900. /**
  1901. * @brief Transmission Mailbox 1 Cancellation callback.
  1902. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1903. * the configuration information for the specified CAN.
  1904. * @retval None
  1905. */
  1906. __weak void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan)
  1907. {
  1908. /* Prevent unused argument(s) compilation warning */
  1909. UNUSED(hcan);
  1910. /* NOTE : This function Should not be modified, when the callback is needed,
  1911. the HAL_CAN_TxMailbox1AbortCallback could be implemented in the
  1912. user file
  1913. */
  1914. }
  1915. /**
  1916. * @brief Transmission Mailbox 2 Cancellation callback.
  1917. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1918. * the configuration information for the specified CAN.
  1919. * @retval None
  1920. */
  1921. __weak void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan)
  1922. {
  1923. /* Prevent unused argument(s) compilation warning */
  1924. UNUSED(hcan);
  1925. /* NOTE : This function Should not be modified, when the callback is needed,
  1926. the HAL_CAN_TxMailbox2AbortCallback could be implemented in the
  1927. user file
  1928. */
  1929. }
  1930. /**
  1931. * @brief Rx FIFO 0 message pending callback.
  1932. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1933. * the configuration information for the specified CAN.
  1934. * @retval None
  1935. */
  1936. __weak void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
  1937. {
  1938. /* Prevent unused argument(s) compilation warning */
  1939. UNUSED(hcan);
  1940. /* NOTE : This function Should not be modified, when the callback is needed,
  1941. the HAL_CAN_RxFifo0MsgPendingCallback could be implemented in the
  1942. user file
  1943. */
  1944. }
  1945. /**
  1946. * @brief Rx FIFO 0 full callback.
  1947. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1948. * the configuration information for the specified CAN.
  1949. * @retval None
  1950. */
  1951. __weak void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan)
  1952. {
  1953. /* Prevent unused argument(s) compilation warning */
  1954. UNUSED(hcan);
  1955. /* NOTE : This function Should not be modified, when the callback is needed,
  1956. the HAL_CAN_RxFifo0FullCallback could be implemented in the user
  1957. file
  1958. */
  1959. }
  1960. /**
  1961. * @brief Rx FIFO 1 message pending callback.
  1962. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1963. * the configuration information for the specified CAN.
  1964. * @retval None
  1965. */
  1966. __weak void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
  1967. {
  1968. /* Prevent unused argument(s) compilation warning */
  1969. UNUSED(hcan);
  1970. /* NOTE : This function Should not be modified, when the callback is needed,
  1971. the HAL_CAN_RxFifo1MsgPendingCallback could be implemented in the
  1972. user file
  1973. */
  1974. }
  1975. /**
  1976. * @brief Rx FIFO 1 full callback.
  1977. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1978. * the configuration information for the specified CAN.
  1979. * @retval None
  1980. */
  1981. __weak void HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef *hcan)
  1982. {
  1983. /* Prevent unused argument(s) compilation warning */
  1984. UNUSED(hcan);
  1985. /* NOTE : This function Should not be modified, when the callback is needed,
  1986. the HAL_CAN_RxFifo1FullCallback could be implemented in the user
  1987. file
  1988. */
  1989. }
  1990. /**
  1991. * @brief Sleep callback.
  1992. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1993. * the configuration information for the specified CAN.
  1994. * @retval None
  1995. */
  1996. __weak void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan)
  1997. {
  1998. /* Prevent unused argument(s) compilation warning */
  1999. UNUSED(hcan);
  2000. /* NOTE : This function Should not be modified, when the callback is needed,
  2001. the HAL_CAN_SleepCallback could be implemented in the user file
  2002. */
  2003. }
  2004. /**
  2005. * @brief WakeUp from Rx message callback.
  2006. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  2007. * the configuration information for the specified CAN.
  2008. * @retval None
  2009. */
  2010. __weak void HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan)
  2011. {
  2012. /* Prevent unused argument(s) compilation warning */
  2013. UNUSED(hcan);
  2014. /* NOTE : This function Should not be modified, when the callback is needed,
  2015. the HAL_CAN_WakeUpFromRxMsgCallback could be implemented in the
  2016. user file
  2017. */
  2018. }
  2019. /**
  2020. * @brief Error CAN callback.
  2021. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  2022. * the configuration information for the specified CAN.
  2023. * @retval None
  2024. */
  2025. __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
  2026. {
  2027. /* Prevent unused argument(s) compilation warning */
  2028. UNUSED(hcan);
  2029. /* NOTE : This function Should not be modified, when the callback is needed,
  2030. the HAL_CAN_ErrorCallback could be implemented in the user file
  2031. */
  2032. }
  2033. /**
  2034. * @}
  2035. */
  2036. /** @defgroup CAN_Exported_Functions_Group6 Peripheral State and Error functions
  2037. * @brief CAN Peripheral State functions
  2038. *
  2039. @verbatim
  2040. ==============================================================================
  2041. ##### Peripheral State and Error functions #####
  2042. ==============================================================================
  2043. [..]
  2044. This subsection provides functions allowing to :
  2045. (+) HAL_CAN_GetState() : Return the CAN state.
  2046. (+) HAL_CAN_GetError() : Return the CAN error codes if any.
  2047. (+) HAL_CAN_ResetError(): Reset the CAN error codes if any.
  2048. @endverbatim
  2049. * @{
  2050. */
  2051. /**
  2052. * @brief Return the CAN state.
  2053. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  2054. * the configuration information for the specified CAN.
  2055. * @retval HAL state
  2056. */
  2057. HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef *hcan)
  2058. {
  2059. HAL_CAN_StateTypeDef state = hcan->State;
  2060. if ((state == HAL_CAN_STATE_READY) ||
  2061. (state == HAL_CAN_STATE_LISTENING))
  2062. {
  2063. /* Check sleep mode acknowledge flag */
  2064. if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
  2065. {
  2066. /* Sleep mode is active */
  2067. state = HAL_CAN_STATE_SLEEP_ACTIVE;
  2068. }
  2069. /* Check sleep mode request flag */
  2070. else if ((hcan->Instance->MCR & CAN_MCR_SLEEP) != 0U)
  2071. {
  2072. /* Sleep mode request is pending */
  2073. state = HAL_CAN_STATE_SLEEP_PENDING;
  2074. }
  2075. else
  2076. {
  2077. /* Neither sleep mode request nor sleep mode acknowledge */
  2078. }
  2079. }
  2080. /* Return CAN state */
  2081. return state;
  2082. }
  2083. /**
  2084. * @brief Return the CAN error code.
  2085. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  2086. * the configuration information for the specified CAN.
  2087. * @retval CAN Error Code
  2088. */
  2089. uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan)
  2090. {
  2091. /* Return CAN error code */
  2092. return hcan->ErrorCode;
  2093. }
  2094. /**
  2095. * @brief Reset the CAN error code.
  2096. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  2097. * the configuration information for the specified CAN.
  2098. * @retval HAL status
  2099. */
  2100. HAL_StatusTypeDef HAL_CAN_ResetError(CAN_HandleTypeDef *hcan)
  2101. {
  2102. HAL_StatusTypeDef status = HAL_OK;
  2103. HAL_CAN_StateTypeDef state = hcan->State;
  2104. if ((state == HAL_CAN_STATE_READY) ||
  2105. (state == HAL_CAN_STATE_LISTENING))
  2106. {
  2107. /* Reset CAN error code */
  2108. hcan->ErrorCode = 0U;
  2109. }
  2110. else
  2111. {
  2112. /* Update error code */
  2113. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  2114. status = HAL_ERROR;
  2115. }
  2116. /* Return the status */
  2117. return status;
  2118. }
  2119. /**
  2120. * @}
  2121. */
  2122. /**
  2123. * @}
  2124. */
  2125. #endif /* HAL_CAN_MODULE_ENABLED */
  2126. /**
  2127. * @}
  2128. */
  2129. #endif /* CAN1 */
  2130. /**
  2131. * @}
  2132. */
  2133. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/