stm32f3xx_hal_can.c 78 KB

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