stm32f4xx_hal_can.c 79 KB

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