stm32l4xx_hal_can.c 63 KB

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