stm32l4xx_hal_can.c 78 KB

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