stm32f1xx_hal_can.c 78 KB

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