stm32f1xx_hal_can.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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 "stm32f1xx_hal.h"
  186. /** @addtogroup STM32F1xx_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(CAN2)
  726. /* CAN1 and CAN2 are dual instances with 28 common filters banks */
  727. /* Select master instance to access the filter banks */
  728. can_ip = CAN1;
  729. /* Check the parameters */
  730. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank));
  731. assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank));
  732. #else
  733. /* CAN1 is single instance with 14 dedicated filters banks */
  734. /* Check the parameters */
  735. assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank));
  736. #endif
  737. /* Initialisation mode for the filter */
  738. SET_BIT(can_ip->FMR, CAN_FMR_FINIT);
  739. #if defined(CAN2)
  740. /* Select the start filter number of CAN2 slave instance */
  741. CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB);
  742. SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos);
  743. #endif
  744. /* Convert filter number into bit position */
  745. filternbrbitpos = (uint32_t)1 << (sFilterConfig->FilterBank & 0x1FU);
  746. /* Filter Deactivation */
  747. CLEAR_BIT(can_ip->FA1R, filternbrbitpos);
  748. /* Filter Scale */
  749. if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT)
  750. {
  751. /* 16-bit scale for the filter */
  752. CLEAR_BIT(can_ip->FS1R, filternbrbitpos);
  753. /* First 16-bit identifier and First 16-bit mask */
  754. /* Or First 16-bit identifier and Second 16-bit identifier */
  755. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
  756. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16U) |
  757. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
  758. /* Second 16-bit identifier and Second 16-bit mask */
  759. /* Or Third 16-bit identifier and Fourth 16-bit identifier */
  760. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
  761. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
  762. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh);
  763. }
  764. if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT)
  765. {
  766. /* 32-bit scale for the filter */
  767. SET_BIT(can_ip->FS1R, filternbrbitpos);
  768. /* 32-bit identifier or First 32-bit identifier */
  769. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
  770. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh) << 16U) |
  771. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
  772. /* 32-bit mask or Second 32-bit identifier */
  773. can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
  774. ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
  775. (0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow);
  776. }
  777. /* Filter Mode */
  778. if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK)
  779. {
  780. /* Id/Mask mode for the filter*/
  781. CLEAR_BIT(can_ip->FM1R, filternbrbitpos);
  782. }
  783. else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
  784. {
  785. /* Identifier list mode for the filter*/
  786. SET_BIT(can_ip->FM1R, filternbrbitpos);
  787. }
  788. /* Filter FIFO assignment */
  789. if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0)
  790. {
  791. /* FIFO 0 assignation for the filter */
  792. CLEAR_BIT(can_ip->FFA1R, filternbrbitpos);
  793. }
  794. else
  795. {
  796. /* FIFO 1 assignation for the filter */
  797. SET_BIT(can_ip->FFA1R, filternbrbitpos);
  798. }
  799. /* Filter activation */
  800. if (sFilterConfig->FilterActivation == CAN_FILTER_ENABLE)
  801. {
  802. SET_BIT(can_ip->FA1R, filternbrbitpos);
  803. }
  804. /* Leave the initialisation mode for the filter */
  805. CLEAR_BIT(can_ip->FMR, CAN_FMR_FINIT);
  806. /* Return function status */
  807. return HAL_OK;
  808. }
  809. else
  810. {
  811. /* Update error code */
  812. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  813. return HAL_ERROR;
  814. }
  815. }
  816. /**
  817. * @}
  818. */
  819. /** @defgroup CAN_Exported_Functions_Group3 Control functions
  820. * @brief Control functions
  821. *
  822. @verbatim
  823. ==============================================================================
  824. ##### Control functions #####
  825. ==============================================================================
  826. [..] This section provides functions allowing to:
  827. (+) HAL_CAN_Start : Start the CAN module
  828. (+) HAL_CAN_Stop : Stop the CAN module
  829. (+) HAL_CAN_RequestSleep : Request sleep mode entry.
  830. (+) HAL_CAN_WakeUp : Wake up from sleep mode.
  831. (+) HAL_CAN_IsSleepActive : Check is sleep mode is active.
  832. (+) HAL_CAN_AddTxMessage : Add a message to the Tx mailboxes
  833. and activate the corresponding
  834. transmission request
  835. (+) HAL_CAN_AbortTxRequest : Abort transmission request
  836. (+) HAL_CAN_GetTxMailboxesFreeLevel : Return Tx mailboxes free level
  837. (+) HAL_CAN_IsTxMessagePending : Check if a transmission request is
  838. pending on the selected Tx mailbox
  839. (+) HAL_CAN_GetRxMessage : Get a CAN frame from the Rx FIFO
  840. (+) HAL_CAN_GetRxFifoFillLevel : Return Rx FIFO fill level
  841. @endverbatim
  842. * @{
  843. */
  844. /**
  845. * @brief Start the CAN module.
  846. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  847. * the configuration information for the specified CAN.
  848. * @retval HAL status
  849. */
  850. HAL_StatusTypeDef HAL_CAN_Start(CAN_HandleTypeDef *hcan)
  851. {
  852. uint32_t tickstart;
  853. if (hcan->State == HAL_CAN_STATE_READY)
  854. {
  855. /* Change CAN peripheral state */
  856. hcan->State = HAL_CAN_STATE_LISTENING;
  857. /* Request leave initialisation */
  858. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
  859. /* Get tick */
  860. tickstart = HAL_GetTick();
  861. /* Wait the acknowledge */
  862. while ((hcan->Instance->MSR & CAN_MSR_INAK) != 0U)
  863. {
  864. /* Check for the Timeout */
  865. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  866. {
  867. /* Update error code */
  868. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  869. /* Change CAN state */
  870. hcan->State = HAL_CAN_STATE_ERROR;
  871. return HAL_ERROR;
  872. }
  873. }
  874. /* Reset the CAN ErrorCode */
  875. hcan->ErrorCode = HAL_CAN_ERROR_NONE;
  876. /* Return function status */
  877. return HAL_OK;
  878. }
  879. else
  880. {
  881. /* Update error code */
  882. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_READY;
  883. return HAL_ERROR;
  884. }
  885. }
  886. /**
  887. * @brief Stop the CAN module and enable access to configuration registers.
  888. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  889. * the configuration information for the specified CAN.
  890. * @retval HAL status
  891. */
  892. HAL_StatusTypeDef HAL_CAN_Stop(CAN_HandleTypeDef *hcan)
  893. {
  894. uint32_t tickstart;
  895. if (hcan->State == HAL_CAN_STATE_LISTENING)
  896. {
  897. /* Request initialisation */
  898. SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
  899. /* Get tick */
  900. tickstart = HAL_GetTick();
  901. /* Wait the acknowledge */
  902. while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U)
  903. {
  904. /* Check for the Timeout */
  905. if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
  906. {
  907. /* Update error code */
  908. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  909. /* Change CAN state */
  910. hcan->State = HAL_CAN_STATE_ERROR;
  911. return HAL_ERROR;
  912. }
  913. }
  914. /* Exit from sleep mode */
  915. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  916. /* Change CAN peripheral state */
  917. hcan->State = HAL_CAN_STATE_READY;
  918. /* Return function status */
  919. return HAL_OK;
  920. }
  921. else
  922. {
  923. /* Update error code */
  924. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_STARTED;
  925. return HAL_ERROR;
  926. }
  927. }
  928. /**
  929. * @brief Request the sleep mode (low power) entry.
  930. * When returning from this function, Sleep mode will be entered
  931. * as soon as the current CAN activity (transmission or reception
  932. * of a CAN frame) has been completed.
  933. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  934. * the configuration information for the specified CAN.
  935. * @retval HAL status.
  936. */
  937. HAL_StatusTypeDef HAL_CAN_RequestSleep(CAN_HandleTypeDef *hcan)
  938. {
  939. HAL_CAN_StateTypeDef state = hcan->State;
  940. if ((state == HAL_CAN_STATE_READY) ||
  941. (state == HAL_CAN_STATE_LISTENING))
  942. {
  943. /* Request Sleep mode */
  944. SET_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  945. /* Return function status */
  946. return HAL_OK;
  947. }
  948. else
  949. {
  950. /* Update error code */
  951. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  952. /* Return function status */
  953. return HAL_ERROR;
  954. }
  955. }
  956. /**
  957. * @brief Wake up from sleep mode.
  958. * When returning with HAL_OK status from this function, Sleep mode
  959. * is exited.
  960. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  961. * the configuration information for the specified CAN.
  962. * @retval HAL status.
  963. */
  964. HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan)
  965. {
  966. __IO uint32_t count = 0;
  967. uint32_t timeout = 1000000U;
  968. HAL_CAN_StateTypeDef state = hcan->State;
  969. if ((state == HAL_CAN_STATE_READY) ||
  970. (state == HAL_CAN_STATE_LISTENING))
  971. {
  972. /* Wake up request */
  973. CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
  974. /* Wait sleep mode is exited */
  975. do
  976. {
  977. /* Increment counter */
  978. count++;
  979. /* Check if timeout is reached */
  980. if (count > timeout)
  981. {
  982. /* Update error code */
  983. hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
  984. return HAL_ERROR;
  985. }
  986. }
  987. while ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U);
  988. /* Return function status */
  989. return HAL_OK;
  990. }
  991. else
  992. {
  993. /* Update error code */
  994. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  995. return HAL_ERROR;
  996. }
  997. }
  998. /**
  999. * @brief Check is sleep mode is active.
  1000. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1001. * the configuration information for the specified CAN.
  1002. * @retval Status
  1003. * - 0 : Sleep mode is not active.
  1004. * - 1 : Sleep mode is active.
  1005. */
  1006. uint32_t HAL_CAN_IsSleepActive(CAN_HandleTypeDef *hcan)
  1007. {
  1008. uint32_t status = 0U;
  1009. HAL_CAN_StateTypeDef state = hcan->State;
  1010. if ((state == HAL_CAN_STATE_READY) ||
  1011. (state == HAL_CAN_STATE_LISTENING))
  1012. {
  1013. /* Check Sleep mode */
  1014. if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
  1015. {
  1016. status = 1U;
  1017. }
  1018. }
  1019. /* Return function status */
  1020. return status;
  1021. }
  1022. /**
  1023. * @brief Add a message to the first free Tx mailbox and activate the
  1024. * corresponding transmission request.
  1025. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1026. * the configuration information for the specified CAN.
  1027. * @param pHeader pointer to a CAN_TxHeaderTypeDef structure.
  1028. * @param aData array containing the payload of the Tx frame.
  1029. * @param pTxMailbox pointer to a variable where the function will return
  1030. * the TxMailbox used to store the Tx message.
  1031. * This parameter can be a value of @arg CAN_Tx_Mailboxes.
  1032. * @retval HAL status
  1033. */
  1034. HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderTypeDef *pHeader, uint8_t aData[], uint32_t *pTxMailbox)
  1035. {
  1036. uint32_t transmitmailbox = 0;
  1037. HAL_CAN_StateTypeDef state = hcan->State;
  1038. uint32_t tsr = READ_REG(hcan->Instance->TSR);
  1039. /* Check the parameters */
  1040. assert_param(IS_CAN_IDTYPE(pHeader->IDE));
  1041. assert_param(IS_CAN_RTR(pHeader->RTR));
  1042. assert_param(IS_CAN_DLC(pHeader->DLC));
  1043. if (pHeader->IDE == CAN_ID_STD)
  1044. {
  1045. assert_param(IS_CAN_STDID(pHeader->StdId));
  1046. }
  1047. else
  1048. {
  1049. assert_param(IS_CAN_EXTID(pHeader->ExtId));
  1050. }
  1051. assert_param(IS_FUNCTIONAL_STATE(pHeader->TransmitGlobalTime));
  1052. if ((state == HAL_CAN_STATE_READY) ||
  1053. (state == HAL_CAN_STATE_LISTENING))
  1054. {
  1055. /* Check that all the Tx mailboxes are not full */
  1056. if (((tsr & CAN_TSR_TME0) != 0U) ||
  1057. ((tsr & CAN_TSR_TME1) != 0U) ||
  1058. ((tsr & CAN_TSR_TME2) != 0U))
  1059. {
  1060. /* Select an empty transmit mailbox */
  1061. //读寄存器会一直返回邮箱0
  1062. //transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;
  1063. if((tsr & CAN_TSR_TME0) != 0U)
  1064. {
  1065. transmitmailbox = 0;
  1066. }
  1067. else if((tsr & CAN_TSR_TME1) != 0U)
  1068. {
  1069. transmitmailbox = 1;
  1070. }
  1071. else if((tsr & CAN_TSR_TME2) != 0U)
  1072. {
  1073. transmitmailbox = 2;
  1074. }
  1075. /* Check transmit mailbox value */
  1076. if (transmitmailbox > 2U)
  1077. {
  1078. /* Update error code */
  1079. hcan->ErrorCode |= HAL_CAN_ERROR_INTERNAL;
  1080. return HAL_ERROR;
  1081. }
  1082. /* Store the Tx mailbox */
  1083. *pTxMailbox = (uint32_t)1 << transmitmailbox;
  1084. /* Set up the Id */
  1085. if (pHeader->IDE == CAN_ID_STD)
  1086. {
  1087. hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) |
  1088. pHeader->RTR);
  1089. }
  1090. else
  1091. {
  1092. hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->ExtId << CAN_TI0R_EXID_Pos) |
  1093. pHeader->IDE |
  1094. pHeader->RTR);
  1095. }
  1096. /* Set up the DLC */
  1097. hcan->Instance->sTxMailBox[transmitmailbox].TDTR = (pHeader->DLC);
  1098. /* Set up the Transmit Global Time mode */
  1099. if (pHeader->TransmitGlobalTime == ENABLE)
  1100. {
  1101. SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TDTR, CAN_TDT0R_TGT);
  1102. }
  1103. /* Set up the data field */
  1104. WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDHR,
  1105. ((uint32_t)aData[7] << CAN_TDH0R_DATA7_Pos) |
  1106. ((uint32_t)aData[6] << CAN_TDH0R_DATA6_Pos) |
  1107. ((uint32_t)aData[5] << CAN_TDH0R_DATA5_Pos) |
  1108. ((uint32_t)aData[4] << CAN_TDH0R_DATA4_Pos));
  1109. WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDLR,
  1110. ((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) |
  1111. ((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) |
  1112. ((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) |
  1113. ((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos));
  1114. /* Request transmission */
  1115. SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TIR, CAN_TI0R_TXRQ);
  1116. /* Return function status */
  1117. return HAL_OK;
  1118. }
  1119. else
  1120. {
  1121. /* Update error code */
  1122. hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
  1123. return HAL_ERROR;
  1124. }
  1125. }
  1126. else
  1127. {
  1128. /* Update error code */
  1129. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1130. return HAL_ERROR;
  1131. }
  1132. }
  1133. /**
  1134. * @brief Abort transmission requests
  1135. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1136. * the configuration information for the specified CAN.
  1137. * @param TxMailboxes List of the Tx Mailboxes to abort.
  1138. * This parameter can be any combination of @arg CAN_Tx_Mailboxes.
  1139. * @retval HAL status
  1140. */
  1141. HAL_StatusTypeDef HAL_CAN_AbortTxRequest(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
  1142. {
  1143. HAL_CAN_StateTypeDef state = hcan->State;
  1144. /* Check function parameters */
  1145. assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
  1146. if ((state == HAL_CAN_STATE_READY) ||
  1147. (state == HAL_CAN_STATE_LISTENING))
  1148. {
  1149. /* Check Tx Mailbox 0 */
  1150. if ((TxMailboxes & CAN_TX_MAILBOX0) != 0U)
  1151. {
  1152. /* Add cancellation request for Tx Mailbox 0 */
  1153. SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ0);
  1154. }
  1155. /* Check Tx Mailbox 1 */
  1156. if ((TxMailboxes & CAN_TX_MAILBOX1) != 0U)
  1157. {
  1158. /* Add cancellation request for Tx Mailbox 1 */
  1159. SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ1);
  1160. }
  1161. /* Check Tx Mailbox 2 */
  1162. if ((TxMailboxes & CAN_TX_MAILBOX2) != 0U)
  1163. {
  1164. /* Add cancellation request for Tx Mailbox 2 */
  1165. SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ2);
  1166. }
  1167. /* Return function status */
  1168. return HAL_OK;
  1169. }
  1170. else
  1171. {
  1172. /* Update error code */
  1173. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1174. return HAL_ERROR;
  1175. }
  1176. }
  1177. /**
  1178. * @brief Return Tx Mailboxes free level: number of free Tx Mailboxes.
  1179. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1180. * the configuration information for the specified CAN.
  1181. * @retval Number of free Tx Mailboxes.
  1182. */
  1183. uint32_t HAL_CAN_GetTxMailboxesFreeLevel(CAN_HandleTypeDef *hcan)
  1184. {
  1185. uint32_t freelevel = 0U;
  1186. HAL_CAN_StateTypeDef state = hcan->State;
  1187. if ((state == HAL_CAN_STATE_READY) ||
  1188. (state == HAL_CAN_STATE_LISTENING))
  1189. {
  1190. /* Check Tx Mailbox 0 status */
  1191. if ((hcan->Instance->TSR & CAN_TSR_TME0) != 0U)
  1192. {
  1193. freelevel++;
  1194. }
  1195. /* Check Tx Mailbox 1 status */
  1196. if ((hcan->Instance->TSR & CAN_TSR_TME1) != 0U)
  1197. {
  1198. freelevel++;
  1199. }
  1200. /* Check Tx Mailbox 2 status */
  1201. if ((hcan->Instance->TSR & CAN_TSR_TME2) != 0U)
  1202. {
  1203. freelevel++;
  1204. }
  1205. }
  1206. /* Return Tx Mailboxes free level */
  1207. return freelevel;
  1208. }
  1209. /**
  1210. * @brief Check if a transmission request is pending on the selected Tx
  1211. * Mailboxes.
  1212. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1213. * the configuration information for the specified CAN.
  1214. * @param TxMailboxes List of Tx Mailboxes to check.
  1215. * This parameter can be any combination of @arg CAN_Tx_Mailboxes.
  1216. * @retval Status
  1217. * - 0 : No pending transmission request on any selected Tx Mailboxes.
  1218. * - 1 : Pending transmission request on at least one of the selected
  1219. * Tx Mailbox.
  1220. */
  1221. uint32_t HAL_CAN_IsTxMessagePending(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
  1222. {
  1223. uint32_t status = 0U;
  1224. HAL_CAN_StateTypeDef state = hcan->State;
  1225. /* Check function parameters */
  1226. assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
  1227. if ((state == HAL_CAN_STATE_READY) ||
  1228. (state == HAL_CAN_STATE_LISTENING))
  1229. {
  1230. /* Check pending transmission request on the selected Tx Mailboxes */
  1231. if ((hcan->Instance->TSR & (TxMailboxes << CAN_TSR_TME0_Pos)) != (TxMailboxes << CAN_TSR_TME0_Pos))
  1232. {
  1233. status = 1U;
  1234. }
  1235. }
  1236. /* Return status */
  1237. return status;
  1238. }
  1239. /**
  1240. * @brief Return timestamp of Tx message sent, if time triggered communication
  1241. mode is enabled.
  1242. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1243. * the configuration information for the specified CAN.
  1244. * @param TxMailbox Tx Mailbox where the timestamp of message sent will be
  1245. * read.
  1246. * This parameter can be one value of @arg CAN_Tx_Mailboxes.
  1247. * @retval Timestamp of message sent from Tx Mailbox.
  1248. */
  1249. uint32_t HAL_CAN_GetTxTimestamp(CAN_HandleTypeDef *hcan, uint32_t TxMailbox)
  1250. {
  1251. uint32_t timestamp = 0U;
  1252. uint32_t transmitmailbox;
  1253. HAL_CAN_StateTypeDef state = hcan->State;
  1254. /* Check function parameters */
  1255. assert_param(IS_CAN_TX_MAILBOX(TxMailbox));
  1256. if ((state == HAL_CAN_STATE_READY) ||
  1257. (state == HAL_CAN_STATE_LISTENING))
  1258. {
  1259. /* Select the Tx mailbox */
  1260. transmitmailbox = POSITION_VAL(TxMailbox);
  1261. /* Get timestamp */
  1262. timestamp = (hcan->Instance->sTxMailBox[transmitmailbox].TDTR & CAN_TDT0R_TIME) >> CAN_TDT0R_TIME_Pos;
  1263. }
  1264. /* Return the timestamp */
  1265. return timestamp;
  1266. }
  1267. /**
  1268. * @brief Get an CAN frame from the Rx FIFO zone into the message RAM.
  1269. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1270. * the configuration information for the specified CAN.
  1271. * @param RxFifo Fifo number of the received message to be read.
  1272. * This parameter can be a value of @arg CAN_receive_FIFO_number.
  1273. * @param pHeader pointer to a CAN_RxHeaderTypeDef structure where the header
  1274. * of the Rx frame will be stored.
  1275. * @param aData array where the payload of the Rx frame will be stored.
  1276. * @retval HAL status
  1277. */
  1278. HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo, CAN_RxHeaderTypeDef *pHeader, uint8_t aData[])
  1279. {
  1280. HAL_CAN_StateTypeDef state = hcan->State;
  1281. assert_param(IS_CAN_RX_FIFO(RxFifo));
  1282. if ((state == HAL_CAN_STATE_READY) ||
  1283. (state == HAL_CAN_STATE_LISTENING))
  1284. {
  1285. /* Check the Rx FIFO */
  1286. if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
  1287. {
  1288. /* Check that the Rx FIFO 0 is not empty */
  1289. if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) == 0U)
  1290. {
  1291. /* Update error code */
  1292. hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
  1293. return HAL_ERROR;
  1294. }
  1295. }
  1296. else /* Rx element is assigned to Rx FIFO 1 */
  1297. {
  1298. /* Check that the Rx FIFO 1 is not empty */
  1299. if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) == 0U)
  1300. {
  1301. /* Update error code */
  1302. hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
  1303. return HAL_ERROR;
  1304. }
  1305. }
  1306. /* Get the header */
  1307. pHeader->IDE = CAN_RI0R_IDE & hcan->Instance->sFIFOMailBox[RxFifo].RIR;
  1308. if (pHeader->IDE == CAN_ID_STD)
  1309. {
  1310. pHeader->StdId = (CAN_RI0R_STID & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_TI0R_STID_Pos;
  1311. }
  1312. else
  1313. {
  1314. pHeader->ExtId = ((CAN_RI0R_EXID | CAN_RI0R_STID) & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_EXID_Pos;
  1315. }
  1316. pHeader->RTR = (CAN_RI0R_RTR & hcan->Instance->sFIFOMailBox[RxFifo].RIR);
  1317. pHeader->DLC = (CAN_RDT0R_DLC & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos;
  1318. pHeader->FilterMatchIndex = (CAN_RDT0R_FMI & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_FMI_Pos;
  1319. pHeader->Timestamp = (CAN_RDT0R_TIME & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_TIME_Pos;
  1320. /* Get the data */
  1321. aData[0] = (uint8_t)((CAN_RDL0R_DATA0 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA0_Pos);
  1322. aData[1] = (uint8_t)((CAN_RDL0R_DATA1 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA1_Pos);
  1323. aData[2] = (uint8_t)((CAN_RDL0R_DATA2 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA2_Pos);
  1324. aData[3] = (uint8_t)((CAN_RDL0R_DATA3 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA3_Pos);
  1325. aData[4] = (uint8_t)((CAN_RDH0R_DATA4 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA4_Pos);
  1326. aData[5] = (uint8_t)((CAN_RDH0R_DATA5 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA5_Pos);
  1327. aData[6] = (uint8_t)((CAN_RDH0R_DATA6 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA6_Pos);
  1328. aData[7] = (uint8_t)((CAN_RDH0R_DATA7 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA7_Pos);
  1329. /* Release the FIFO */
  1330. if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
  1331. {
  1332. /* Release RX FIFO 0 */
  1333. SET_BIT(hcan->Instance->RF0R, CAN_RF0R_RFOM0);
  1334. }
  1335. else /* Rx element is assigned to Rx FIFO 1 */
  1336. {
  1337. /* Release RX FIFO 1 */
  1338. SET_BIT(hcan->Instance->RF1R, CAN_RF1R_RFOM1);
  1339. }
  1340. /* Return function status */
  1341. return HAL_OK;
  1342. }
  1343. else
  1344. {
  1345. /* Update error code */
  1346. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1347. return HAL_ERROR;
  1348. }
  1349. }
  1350. /**
  1351. * @brief Return Rx FIFO fill level.
  1352. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1353. * the configuration information for the specified CAN.
  1354. * @param RxFifo Rx FIFO.
  1355. * This parameter can be a value of @arg CAN_receive_FIFO_number.
  1356. * @retval Number of messages available in Rx FIFO.
  1357. */
  1358. uint32_t HAL_CAN_GetRxFifoFillLevel(CAN_HandleTypeDef *hcan, uint32_t RxFifo)
  1359. {
  1360. uint32_t filllevel = 0U;
  1361. HAL_CAN_StateTypeDef state = hcan->State;
  1362. /* Check function parameters */
  1363. assert_param(IS_CAN_RX_FIFO(RxFifo));
  1364. if ((state == HAL_CAN_STATE_READY) ||
  1365. (state == HAL_CAN_STATE_LISTENING))
  1366. {
  1367. if (RxFifo == CAN_RX_FIFO0)
  1368. {
  1369. filllevel = hcan->Instance->RF0R & CAN_RF0R_FMP0;
  1370. }
  1371. else /* RxFifo == CAN_RX_FIFO1 */
  1372. {
  1373. filllevel = hcan->Instance->RF1R & CAN_RF1R_FMP1;
  1374. }
  1375. }
  1376. /* Return Rx FIFO fill level */
  1377. return filllevel;
  1378. }
  1379. /**
  1380. * @}
  1381. */
  1382. /** @defgroup CAN_Exported_Functions_Group4 Interrupts management
  1383. * @brief Interrupts management
  1384. *
  1385. @verbatim
  1386. ==============================================================================
  1387. ##### Interrupts management #####
  1388. ==============================================================================
  1389. [..] This section provides functions allowing to:
  1390. (+) HAL_CAN_ActivateNotification : Enable interrupts
  1391. (+) HAL_CAN_DeactivateNotification : Disable interrupts
  1392. (+) HAL_CAN_IRQHandler : Handles CAN interrupt request
  1393. @endverbatim
  1394. * @{
  1395. */
  1396. /**
  1397. * @brief Enable interrupts.
  1398. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1399. * the configuration information for the specified CAN.
  1400. * @param ActiveITs indicates which interrupts will be enabled.
  1401. * This parameter can be any combination of @arg CAN_Interrupts.
  1402. * @retval HAL status
  1403. */
  1404. HAL_StatusTypeDef HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t ActiveITs)
  1405. {
  1406. HAL_CAN_StateTypeDef state = hcan->State;
  1407. /* Check function parameters */
  1408. assert_param(IS_CAN_IT(ActiveITs));
  1409. if ((state == HAL_CAN_STATE_READY) ||
  1410. (state == HAL_CAN_STATE_LISTENING))
  1411. {
  1412. /* Enable the selected interrupts */
  1413. __HAL_CAN_ENABLE_IT(hcan, ActiveITs);
  1414. /* Return function status */
  1415. return HAL_OK;
  1416. }
  1417. else
  1418. {
  1419. /* Update error code */
  1420. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1421. return HAL_ERROR;
  1422. }
  1423. }
  1424. /**
  1425. * @brief Disable interrupts.
  1426. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1427. * the configuration information for the specified CAN.
  1428. * @param InactiveITs indicates which interrupts will be disabled.
  1429. * This parameter can be any combination of @arg CAN_Interrupts.
  1430. * @retval HAL status
  1431. */
  1432. HAL_StatusTypeDef HAL_CAN_DeactivateNotification(CAN_HandleTypeDef *hcan, uint32_t InactiveITs)
  1433. {
  1434. HAL_CAN_StateTypeDef state = hcan->State;
  1435. /* Check function parameters */
  1436. assert_param(IS_CAN_IT(InactiveITs));
  1437. if ((state == HAL_CAN_STATE_READY) ||
  1438. (state == HAL_CAN_STATE_LISTENING))
  1439. {
  1440. /* Disable the selected interrupts */
  1441. __HAL_CAN_DISABLE_IT(hcan, InactiveITs);
  1442. /* Return function status */
  1443. return HAL_OK;
  1444. }
  1445. else
  1446. {
  1447. /* Update error code */
  1448. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  1449. return HAL_ERROR;
  1450. }
  1451. }
  1452. /**
  1453. * @brief Handles CAN interrupt request
  1454. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1455. * the configuration information for the specified CAN.
  1456. * @retval None
  1457. */
  1458. void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan)
  1459. {
  1460. uint32_t errorcode = HAL_CAN_ERROR_NONE;
  1461. uint32_t interrupts = READ_REG(hcan->Instance->IER);
  1462. uint32_t msrflags = READ_REG(hcan->Instance->MSR);
  1463. uint32_t tsrflags = READ_REG(hcan->Instance->TSR);
  1464. uint32_t rf0rflags = READ_REG(hcan->Instance->RF0R);
  1465. uint32_t rf1rflags = READ_REG(hcan->Instance->RF1R);
  1466. uint32_t esrflags = READ_REG(hcan->Instance->ESR);
  1467. /* Transmit Mailbox empty interrupt management *****************************/
  1468. if ((interrupts & CAN_IT_TX_MAILBOX_EMPTY) != 0U)
  1469. {
  1470. /* Transmit Mailbox 0 management *****************************************/
  1471. if ((tsrflags & CAN_TSR_RQCP0) != 0U)
  1472. {
  1473. /* Clear the Transmission Complete flag (and TXOK0,ALST0,TERR0 bits) */
  1474. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP0);
  1475. if ((tsrflags & CAN_TSR_TXOK0) != 0U)
  1476. {
  1477. /* Transmission Mailbox 0 complete callback */
  1478. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1479. /* Call registered callback*/
  1480. hcan->TxMailbox0CompleteCallback(hcan);
  1481. #else
  1482. /* Call weak (surcharged) callback */
  1483. HAL_CAN_TxMailbox0CompleteCallback(hcan);
  1484. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1485. }
  1486. else
  1487. {
  1488. if ((tsrflags & CAN_TSR_ALST0) != 0U)
  1489. {
  1490. /* Update error code */
  1491. errorcode |= HAL_CAN_ERROR_TX_ALST0;
  1492. }
  1493. else if ((tsrflags & CAN_TSR_TERR0) != 0U)
  1494. {
  1495. /* Update error code */
  1496. errorcode |= HAL_CAN_ERROR_TX_TERR0;
  1497. }
  1498. else
  1499. {
  1500. /* Transmission Mailbox 0 abort callback */
  1501. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1502. /* Call registered callback*/
  1503. hcan->TxMailbox0AbortCallback(hcan);
  1504. #else
  1505. /* Call weak (surcharged) callback */
  1506. HAL_CAN_TxMailbox0AbortCallback(hcan);
  1507. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1508. }
  1509. }
  1510. }
  1511. /* Transmit Mailbox 1 management *****************************************/
  1512. if ((tsrflags & CAN_TSR_RQCP1) != 0U)
  1513. {
  1514. /* Clear the Transmission Complete flag (and TXOK1,ALST1,TERR1 bits) */
  1515. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP1);
  1516. if ((tsrflags & CAN_TSR_TXOK1) != 0U)
  1517. {
  1518. /* Transmission Mailbox 1 complete callback */
  1519. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1520. /* Call registered callback*/
  1521. hcan->TxMailbox1CompleteCallback(hcan);
  1522. #else
  1523. /* Call weak (surcharged) callback */
  1524. HAL_CAN_TxMailbox1CompleteCallback(hcan);
  1525. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1526. }
  1527. else
  1528. {
  1529. if ((tsrflags & CAN_TSR_ALST1) != 0U)
  1530. {
  1531. /* Update error code */
  1532. errorcode |= HAL_CAN_ERROR_TX_ALST1;
  1533. }
  1534. else if ((tsrflags & CAN_TSR_TERR1) != 0U)
  1535. {
  1536. /* Update error code */
  1537. errorcode |= HAL_CAN_ERROR_TX_TERR1;
  1538. }
  1539. else
  1540. {
  1541. /* Transmission Mailbox 1 abort callback */
  1542. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1543. /* Call registered callback*/
  1544. hcan->TxMailbox1AbortCallback(hcan);
  1545. #else
  1546. /* Call weak (surcharged) callback */
  1547. HAL_CAN_TxMailbox1AbortCallback(hcan);
  1548. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1549. }
  1550. }
  1551. }
  1552. /* Transmit Mailbox 2 management *****************************************/
  1553. if ((tsrflags & CAN_TSR_RQCP2) != 0U)
  1554. {
  1555. /* Clear the Transmission Complete flag (and TXOK2,ALST2,TERR2 bits) */
  1556. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP2);
  1557. if ((tsrflags & CAN_TSR_TXOK2) != 0U)
  1558. {
  1559. /* Transmission Mailbox 2 complete callback */
  1560. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1561. /* Call registered callback*/
  1562. hcan->TxMailbox2CompleteCallback(hcan);
  1563. #else
  1564. /* Call weak (surcharged) callback */
  1565. HAL_CAN_TxMailbox2CompleteCallback(hcan);
  1566. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1567. }
  1568. else
  1569. {
  1570. if ((tsrflags & CAN_TSR_ALST2) != 0U)
  1571. {
  1572. /* Update error code */
  1573. errorcode |= HAL_CAN_ERROR_TX_ALST2;
  1574. }
  1575. else if ((tsrflags & CAN_TSR_TERR2) != 0U)
  1576. {
  1577. /* Update error code */
  1578. errorcode |= HAL_CAN_ERROR_TX_TERR2;
  1579. }
  1580. else
  1581. {
  1582. /* Transmission Mailbox 2 abort callback */
  1583. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1584. /* Call registered callback*/
  1585. hcan->TxMailbox2AbortCallback(hcan);
  1586. #else
  1587. /* Call weak (surcharged) callback */
  1588. HAL_CAN_TxMailbox2AbortCallback(hcan);
  1589. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1590. }
  1591. }
  1592. }
  1593. }
  1594. /* Receive FIFO 0 overrun interrupt management *****************************/
  1595. if ((interrupts & CAN_IT_RX_FIFO0_OVERRUN) != 0U)
  1596. {
  1597. if ((rf0rflags & CAN_RF0R_FOVR0) != 0U)
  1598. {
  1599. /* Set CAN error code to Rx Fifo 0 overrun error */
  1600. errorcode |= HAL_CAN_ERROR_RX_FOV0;
  1601. /* Clear FIFO0 Overrun Flag */
  1602. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
  1603. }
  1604. }
  1605. /* Receive FIFO 0 full interrupt management ********************************/
  1606. if ((interrupts & CAN_IT_RX_FIFO0_FULL) != 0U)
  1607. {
  1608. if ((rf0rflags & CAN_RF0R_FULL0) != 0U)
  1609. {
  1610. /* Clear FIFO 0 full Flag */
  1611. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0);
  1612. /* Receive FIFO 0 full Callback */
  1613. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1614. /* Call registered callback*/
  1615. hcan->RxFifo0FullCallback(hcan);
  1616. #else
  1617. /* Call weak (surcharged) callback */
  1618. HAL_CAN_RxFifo0FullCallback(hcan);
  1619. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1620. }
  1621. }
  1622. /* Receive FIFO 0 message pending interrupt management *********************/
  1623. if ((interrupts & CAN_IT_RX_FIFO0_MSG_PENDING) != 0U)
  1624. {
  1625. /* Check if message is still pending */
  1626. if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) != 0U)
  1627. {
  1628. /* Receive FIFO 0 mesage pending Callback */
  1629. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1630. /* Call registered callback*/
  1631. hcan->RxFifo0MsgPendingCallback(hcan);
  1632. #else
  1633. /* Call weak (surcharged) callback */
  1634. HAL_CAN_RxFifo0MsgPendingCallback(hcan);
  1635. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1636. }
  1637. }
  1638. /* Receive FIFO 1 overrun interrupt management *****************************/
  1639. if ((interrupts & CAN_IT_RX_FIFO1_OVERRUN) != 0U)
  1640. {
  1641. if ((rf1rflags & CAN_RF1R_FOVR1) != 0U)
  1642. {
  1643. /* Set CAN error code to Rx Fifo 1 overrun error */
  1644. errorcode |= HAL_CAN_ERROR_RX_FOV1;
  1645. /* Clear FIFO1 Overrun Flag */
  1646. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV1);
  1647. }
  1648. }
  1649. /* Receive FIFO 1 full interrupt management ********************************/
  1650. if ((interrupts & CAN_IT_RX_FIFO1_FULL) != 0U)
  1651. {
  1652. if ((rf1rflags & CAN_RF1R_FULL1) != 0U)
  1653. {
  1654. /* Clear FIFO 1 full Flag */
  1655. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF1);
  1656. /* Receive FIFO 1 full Callback */
  1657. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1658. /* Call registered callback*/
  1659. hcan->RxFifo1FullCallback(hcan);
  1660. #else
  1661. /* Call weak (surcharged) callback */
  1662. HAL_CAN_RxFifo1FullCallback(hcan);
  1663. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1664. }
  1665. }
  1666. /* Receive FIFO 1 message pending interrupt management *********************/
  1667. if ((interrupts & CAN_IT_RX_FIFO1_MSG_PENDING) != 0U)
  1668. {
  1669. /* Check if message is still pending */
  1670. if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) != 0U)
  1671. {
  1672. /* Receive FIFO 1 mesage pending Callback */
  1673. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1674. /* Call registered callback*/
  1675. hcan->RxFifo1MsgPendingCallback(hcan);
  1676. #else
  1677. /* Call weak (surcharged) callback */
  1678. HAL_CAN_RxFifo1MsgPendingCallback(hcan);
  1679. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1680. }
  1681. }
  1682. /* Sleep interrupt management *********************************************/
  1683. if ((interrupts & CAN_IT_SLEEP_ACK) != 0U)
  1684. {
  1685. if ((msrflags & CAN_MSR_SLAKI) != 0U)
  1686. {
  1687. /* Clear Sleep interrupt Flag */
  1688. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_SLAKI);
  1689. /* Sleep Callback */
  1690. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1691. /* Call registered callback*/
  1692. hcan->SleepCallback(hcan);
  1693. #else
  1694. /* Call weak (surcharged) callback */
  1695. HAL_CAN_SleepCallback(hcan);
  1696. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1697. }
  1698. }
  1699. /* WakeUp interrupt management *********************************************/
  1700. if ((interrupts & CAN_IT_WAKEUP) != 0U)
  1701. {
  1702. if ((msrflags & CAN_MSR_WKUI) != 0U)
  1703. {
  1704. /* Clear WakeUp Flag */
  1705. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_WKU);
  1706. /* WakeUp Callback */
  1707. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1708. /* Call registered callback*/
  1709. hcan->WakeUpFromRxMsgCallback(hcan);
  1710. #else
  1711. /* Call weak (surcharged) callback */
  1712. HAL_CAN_WakeUpFromRxMsgCallback(hcan);
  1713. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1714. }
  1715. }
  1716. /* Error interrupts management *********************************************/
  1717. if ((interrupts & CAN_IT_ERROR) != 0U)
  1718. {
  1719. if ((msrflags & CAN_MSR_ERRI) != 0U)
  1720. {
  1721. /* Check Error Warning Flag */
  1722. if (((interrupts & CAN_IT_ERROR_WARNING) != 0U) &&
  1723. ((esrflags & CAN_ESR_EWGF) != 0U))
  1724. {
  1725. /* Set CAN error code to Error Warning */
  1726. errorcode |= HAL_CAN_ERROR_EWG;
  1727. /* No need for clear of Error Warning Flag as read-only */
  1728. }
  1729. /* Check Error Passive Flag */
  1730. if (((interrupts & CAN_IT_ERROR_PASSIVE) != 0U) &&
  1731. ((esrflags & CAN_ESR_EPVF) != 0U))
  1732. {
  1733. /* Set CAN error code to Error Passive */
  1734. errorcode |= HAL_CAN_ERROR_EPV;
  1735. /* No need for clear of Error Passive Flag as read-only */
  1736. }
  1737. /* Check Bus-off Flag */
  1738. if (((interrupts & CAN_IT_BUSOFF) != 0U) &&
  1739. ((esrflags & CAN_ESR_BOFF) != 0U))
  1740. {
  1741. /* Set CAN error code to Bus-Off */
  1742. errorcode |= HAL_CAN_ERROR_BOF;
  1743. /* No need for clear of Error Bus-Off as read-only */
  1744. }
  1745. /* Check Last Error Code Flag */
  1746. if (((interrupts & CAN_IT_LAST_ERROR_CODE) != 0U) &&
  1747. ((esrflags & CAN_ESR_LEC) != 0U))
  1748. {
  1749. switch (esrflags & CAN_ESR_LEC)
  1750. {
  1751. case (CAN_ESR_LEC_0):
  1752. /* Set CAN error code to Stuff error */
  1753. errorcode |= HAL_CAN_ERROR_STF;
  1754. break;
  1755. case (CAN_ESR_LEC_1):
  1756. /* Set CAN error code to Form error */
  1757. errorcode |= HAL_CAN_ERROR_FOR;
  1758. break;
  1759. case (CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
  1760. /* Set CAN error code to Acknowledgement error */
  1761. errorcode |= HAL_CAN_ERROR_ACK;
  1762. break;
  1763. case (CAN_ESR_LEC_2):
  1764. /* Set CAN error code to Bit recessive error */
  1765. errorcode |= HAL_CAN_ERROR_BR;
  1766. break;
  1767. case (CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
  1768. /* Set CAN error code to Bit Dominant error */
  1769. errorcode |= HAL_CAN_ERROR_BD;
  1770. break;
  1771. case (CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
  1772. /* Set CAN error code to CRC error */
  1773. errorcode |= HAL_CAN_ERROR_CRC;
  1774. break;
  1775. default:
  1776. break;
  1777. }
  1778. /* Clear Last error code Flag */
  1779. CLEAR_BIT(hcan->Instance->ESR, CAN_ESR_LEC);
  1780. }
  1781. }
  1782. /* Clear ERRI Flag */
  1783. __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_ERRI);
  1784. }
  1785. /* Call the Error call Back in case of Errors */
  1786. if (errorcode != HAL_CAN_ERROR_NONE)
  1787. {
  1788. /* Update error code in handle */
  1789. hcan->ErrorCode |= errorcode;
  1790. /* Call Error callback function */
  1791. #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
  1792. /* Call registered callback*/
  1793. hcan->ErrorCallback(hcan);
  1794. #else
  1795. /* Call weak (surcharged) callback */
  1796. HAL_CAN_ErrorCallback(hcan);
  1797. #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
  1798. }
  1799. }
  1800. /**
  1801. * @}
  1802. */
  1803. /** @defgroup CAN_Exported_Functions_Group5 Callback functions
  1804. * @brief CAN Callback functions
  1805. *
  1806. @verbatim
  1807. ==============================================================================
  1808. ##### Callback functions #####
  1809. ==============================================================================
  1810. [..]
  1811. This subsection provides the following callback functions:
  1812. (+) HAL_CAN_TxMailbox0CompleteCallback
  1813. (+) HAL_CAN_TxMailbox1CompleteCallback
  1814. (+) HAL_CAN_TxMailbox2CompleteCallback
  1815. (+) HAL_CAN_TxMailbox0AbortCallback
  1816. (+) HAL_CAN_TxMailbox1AbortCallback
  1817. (+) HAL_CAN_TxMailbox2AbortCallback
  1818. (+) HAL_CAN_RxFifo0MsgPendingCallback
  1819. (+) HAL_CAN_RxFifo0FullCallback
  1820. (+) HAL_CAN_RxFifo1MsgPendingCallback
  1821. (+) HAL_CAN_RxFifo1FullCallback
  1822. (+) HAL_CAN_SleepCallback
  1823. (+) HAL_CAN_WakeUpFromRxMsgCallback
  1824. (+) HAL_CAN_ErrorCallback
  1825. @endverbatim
  1826. * @{
  1827. */
  1828. /**
  1829. * @brief Transmission Mailbox 0 complete callback.
  1830. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1831. * the configuration information for the specified CAN.
  1832. * @retval None
  1833. */
  1834. __weak void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
  1835. {
  1836. /* Prevent unused argument(s) compilation warning */
  1837. UNUSED(hcan);
  1838. /* NOTE : This function Should not be modified, when the callback is needed,
  1839. the HAL_CAN_TxMailbox0CompleteCallback could be implemented in the
  1840. user file
  1841. */
  1842. }
  1843. /**
  1844. * @brief Transmission Mailbox 1 complete callback.
  1845. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1846. * the configuration information for the specified CAN.
  1847. * @retval None
  1848. */
  1849. __weak void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
  1850. {
  1851. /* Prevent unused argument(s) compilation warning */
  1852. UNUSED(hcan);
  1853. /* NOTE : This function Should not be modified, when the callback is needed,
  1854. the HAL_CAN_TxMailbox1CompleteCallback could be implemented in the
  1855. user file
  1856. */
  1857. }
  1858. /**
  1859. * @brief Transmission Mailbox 2 complete callback.
  1860. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1861. * the configuration information for the specified CAN.
  1862. * @retval None
  1863. */
  1864. __weak void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
  1865. {
  1866. /* Prevent unused argument(s) compilation warning */
  1867. UNUSED(hcan);
  1868. /* NOTE : This function Should not be modified, when the callback is needed,
  1869. the HAL_CAN_TxMailbox2CompleteCallback could be implemented in the
  1870. user file
  1871. */
  1872. }
  1873. /**
  1874. * @brief Transmission Mailbox 0 Cancellation callback.
  1875. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1876. * the configuration information for the specified CAN.
  1877. * @retval None
  1878. */
  1879. __weak void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan)
  1880. {
  1881. /* Prevent unused argument(s) compilation warning */
  1882. UNUSED(hcan);
  1883. /* NOTE : This function Should not be modified, when the callback is needed,
  1884. the HAL_CAN_TxMailbox0AbortCallback could be implemented in the
  1885. user file
  1886. */
  1887. }
  1888. /**
  1889. * @brief Transmission Mailbox 1 Cancellation callback.
  1890. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1891. * the configuration information for the specified CAN.
  1892. * @retval None
  1893. */
  1894. __weak void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan)
  1895. {
  1896. /* Prevent unused argument(s) compilation warning */
  1897. UNUSED(hcan);
  1898. /* NOTE : This function Should not be modified, when the callback is needed,
  1899. the HAL_CAN_TxMailbox1AbortCallback could be implemented in the
  1900. user file
  1901. */
  1902. }
  1903. /**
  1904. * @brief Transmission Mailbox 2 Cancellation callback.
  1905. * @param hcan pointer to an CAN_HandleTypeDef structure that contains
  1906. * the configuration information for the specified CAN.
  1907. * @retval None
  1908. */
  1909. __weak void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan)
  1910. {
  1911. /* Prevent unused argument(s) compilation warning */
  1912. UNUSED(hcan);
  1913. /* NOTE : This function Should not be modified, when the callback is needed,
  1914. the HAL_CAN_TxMailbox2AbortCallback could be implemented in the
  1915. user file
  1916. */
  1917. }
  1918. /**
  1919. * @brief Rx FIFO 0 message pending callback.
  1920. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1921. * the configuration information for the specified CAN.
  1922. * @retval None
  1923. */
  1924. __weak void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
  1925. {
  1926. /* Prevent unused argument(s) compilation warning */
  1927. UNUSED(hcan);
  1928. /* NOTE : This function Should not be modified, when the callback is needed,
  1929. the HAL_CAN_RxFifo0MsgPendingCallback could be implemented in the
  1930. user file
  1931. */
  1932. }
  1933. /**
  1934. * @brief Rx FIFO 0 full callback.
  1935. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1936. * the configuration information for the specified CAN.
  1937. * @retval None
  1938. */
  1939. __weak void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan)
  1940. {
  1941. /* Prevent unused argument(s) compilation warning */
  1942. UNUSED(hcan);
  1943. /* NOTE : This function Should not be modified, when the callback is needed,
  1944. the HAL_CAN_RxFifo0FullCallback could be implemented in the user
  1945. file
  1946. */
  1947. }
  1948. /**
  1949. * @brief Rx FIFO 1 message pending callback.
  1950. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1951. * the configuration information for the specified CAN.
  1952. * @retval None
  1953. */
  1954. __weak void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
  1955. {
  1956. /* Prevent unused argument(s) compilation warning */
  1957. UNUSED(hcan);
  1958. /* NOTE : This function Should not be modified, when the callback is needed,
  1959. the HAL_CAN_RxFifo1MsgPendingCallback could be implemented in the
  1960. user file
  1961. */
  1962. }
  1963. /**
  1964. * @brief Rx FIFO 1 full callback.
  1965. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1966. * the configuration information for the specified CAN.
  1967. * @retval None
  1968. */
  1969. __weak void HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef *hcan)
  1970. {
  1971. /* Prevent unused argument(s) compilation warning */
  1972. UNUSED(hcan);
  1973. /* NOTE : This function Should not be modified, when the callback is needed,
  1974. the HAL_CAN_RxFifo1FullCallback could be implemented in the user
  1975. file
  1976. */
  1977. }
  1978. /**
  1979. * @brief Sleep callback.
  1980. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1981. * the configuration information for the specified CAN.
  1982. * @retval None
  1983. */
  1984. __weak void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan)
  1985. {
  1986. /* Prevent unused argument(s) compilation warning */
  1987. UNUSED(hcan);
  1988. /* NOTE : This function Should not be modified, when the callback is needed,
  1989. the HAL_CAN_SleepCallback could be implemented in the user file
  1990. */
  1991. }
  1992. /**
  1993. * @brief WakeUp from Rx message callback.
  1994. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  1995. * the configuration information for the specified CAN.
  1996. * @retval None
  1997. */
  1998. __weak void HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan)
  1999. {
  2000. /* Prevent unused argument(s) compilation warning */
  2001. UNUSED(hcan);
  2002. /* NOTE : This function Should not be modified, when the callback is needed,
  2003. the HAL_CAN_WakeUpFromRxMsgCallback could be implemented in the
  2004. user file
  2005. */
  2006. }
  2007. /**
  2008. * @brief Error CAN callback.
  2009. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  2010. * the configuration information for the specified CAN.
  2011. * @retval None
  2012. */
  2013. __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
  2014. {
  2015. /* Prevent unused argument(s) compilation warning */
  2016. UNUSED(hcan);
  2017. /* NOTE : This function Should not be modified, when the callback is needed,
  2018. the HAL_CAN_ErrorCallback could be implemented in the user file
  2019. */
  2020. }
  2021. /**
  2022. * @}
  2023. */
  2024. /** @defgroup CAN_Exported_Functions_Group6 Peripheral State and Error functions
  2025. * @brief CAN Peripheral State functions
  2026. *
  2027. @verbatim
  2028. ==============================================================================
  2029. ##### Peripheral State and Error functions #####
  2030. ==============================================================================
  2031. [..]
  2032. This subsection provides functions allowing to :
  2033. (+) HAL_CAN_GetState() : Return the CAN state.
  2034. (+) HAL_CAN_GetError() : Return the CAN error codes if any.
  2035. (+) HAL_CAN_ResetError(): Reset the CAN error codes if any.
  2036. @endverbatim
  2037. * @{
  2038. */
  2039. /**
  2040. * @brief Return the CAN state.
  2041. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  2042. * the configuration information for the specified CAN.
  2043. * @retval HAL state
  2044. */
  2045. HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef *hcan)
  2046. {
  2047. HAL_CAN_StateTypeDef state = hcan->State;
  2048. if ((state == HAL_CAN_STATE_READY) ||
  2049. (state == HAL_CAN_STATE_LISTENING))
  2050. {
  2051. /* Check sleep mode acknowledge flag */
  2052. if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
  2053. {
  2054. /* Sleep mode is active */
  2055. state = HAL_CAN_STATE_SLEEP_ACTIVE;
  2056. }
  2057. /* Check sleep mode request flag */
  2058. else if ((hcan->Instance->MCR & CAN_MCR_SLEEP) != 0U)
  2059. {
  2060. /* Sleep mode request is pending */
  2061. state = HAL_CAN_STATE_SLEEP_PENDING;
  2062. }
  2063. else
  2064. {
  2065. /* Neither sleep mode request nor sleep mode acknowledge */
  2066. }
  2067. }
  2068. /* Return CAN state */
  2069. return state;
  2070. }
  2071. /**
  2072. * @brief Return the CAN error code.
  2073. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  2074. * the configuration information for the specified CAN.
  2075. * @retval CAN Error Code
  2076. */
  2077. uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan)
  2078. {
  2079. /* Return CAN error code */
  2080. return hcan->ErrorCode;
  2081. }
  2082. /**
  2083. * @brief Reset the CAN error code.
  2084. * @param hcan pointer to a CAN_HandleTypeDef structure that contains
  2085. * the configuration information for the specified CAN.
  2086. * @retval HAL status
  2087. */
  2088. HAL_StatusTypeDef HAL_CAN_ResetError(CAN_HandleTypeDef *hcan)
  2089. {
  2090. HAL_StatusTypeDef status = HAL_OK;
  2091. HAL_CAN_StateTypeDef state = hcan->State;
  2092. if ((state == HAL_CAN_STATE_READY) ||
  2093. (state == HAL_CAN_STATE_LISTENING))
  2094. {
  2095. /* Reset CAN error code */
  2096. hcan->ErrorCode = 0U;
  2097. }
  2098. else
  2099. {
  2100. /* Update error code */
  2101. hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
  2102. status = HAL_ERROR;
  2103. }
  2104. /* Return the status */
  2105. return status;
  2106. }
  2107. /**
  2108. * @}
  2109. */
  2110. /**
  2111. * @}
  2112. */
  2113. #endif /* HAL_CAN_MODULE_ENABLED */
  2114. /**
  2115. * @}
  2116. */
  2117. #endif /* CAN1 */
  2118. /**
  2119. * @}
  2120. */
  2121. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/