stm32f1xx_hal_can.c 78 KB

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