123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436 |
- /**
- ******************************************************************************
- * @file stm32l4xx_hal_can.c
- * @author MCD Application Team
- * @brief CAN HAL module driver.
- * This file provides firmware functions to manage the following
- * functionalities of the Controller Area Network (CAN) peripheral:
- * + Initialization and de-initialization functions
- * + Configuration functions
- * + Control functions
- * + Interrupts management
- * + Callbacks functions
- * + Peripheral State and Error functions
- *
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2017 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- @verbatim
- ==============================================================================
- ##### How to use this driver #####
- ==============================================================================
- [..]
- (#) Initialize the CAN low level resources by implementing the
- HAL_CAN_MspInit():
- (++) Enable the CAN interface clock using __HAL_RCC_CANx_CLK_ENABLE()
- (++) Configure CAN pins
- (+++) Enable the clock for the CAN GPIOs
- (+++) Configure CAN pins as alternate function
- (++) In case of using interrupts (e.g. HAL_CAN_ActivateNotification())
- (+++) Configure the CAN interrupt priority using
- HAL_NVIC_SetPriority()
- (+++) Enable the CAN IRQ handler using HAL_NVIC_EnableIRQ()
- (+++) In CAN IRQ handler, call HAL_CAN_IRQHandler()
- (#) Initialize the CAN peripheral using HAL_CAN_Init() function. This
- function resorts to HAL_CAN_MspInit() for low-level initialization.
- (#) Configure the reception filters using the following configuration
- functions:
- (++) HAL_CAN_ConfigFilter()
- (#) Start the CAN module using HAL_CAN_Start() function. At this level
- the node is active on the bus: it receive messages, and can send
- messages.
- (#) To manage messages transmission, the following Tx control functions
- can be used:
- (++) HAL_CAN_AddTxMessage() to request transmission of a new
- message.
- (++) HAL_CAN_AbortTxRequest() to abort transmission of a pending
- message.
- (++) HAL_CAN_GetTxMailboxesFreeLevel() to get the number of free Tx
- mailboxes.
- (++) HAL_CAN_IsTxMessagePending() to check if a message is pending
- in a Tx mailbox.
- (++) HAL_CAN_GetTxTimestamp() to get the timestamp of Tx message
- sent, if time triggered communication mode is enabled.
- (#) When a message is received into the CAN Rx FIFOs, it can be retrieved
- using the HAL_CAN_GetRxMessage() function. The function
- HAL_CAN_GetRxFifoFillLevel() allows to know how many Rx message are
- stored in the Rx Fifo.
- (#) Calling the HAL_CAN_Stop() function stops the CAN module.
- (#) The deinitialization is achieved with HAL_CAN_DeInit() function.
- *** Polling mode operation ***
- ==============================
- [..]
- (#) Reception:
- (++) Monitor reception of message using HAL_CAN_GetRxFifoFillLevel()
- until at least one message is received.
- (++) Then get the message using HAL_CAN_GetRxMessage().
- (#) Transmission:
- (++) Monitor the Tx mailboxes availability until at least one Tx
- mailbox is free, using HAL_CAN_GetTxMailboxesFreeLevel().
- (++) Then request transmission of a message using
- HAL_CAN_AddTxMessage().
- *** Interrupt mode operation ***
- ================================
- [..]
- (#) Notifications are activated using HAL_CAN_ActivateNotification()
- function. Then, the process can be controlled through the
- available user callbacks: HAL_CAN_xxxCallback(), using same APIs
- HAL_CAN_GetRxMessage() and HAL_CAN_AddTxMessage().
- (#) Notifications can be deactivated using
- HAL_CAN_DeactivateNotification() function.
- (#) Special care should be taken for CAN_IT_RX_FIFO0_MSG_PENDING and
- CAN_IT_RX_FIFO1_MSG_PENDING notifications. These notifications trig
- the callbacks HAL_CAN_RxFIFO0MsgPendingCallback() and
- HAL_CAN_RxFIFO1MsgPendingCallback(). User has two possible options
- here.
- (++) Directly get the Rx message in the callback, using
- HAL_CAN_GetRxMessage().
- (++) Or deactivate the notification in the callback without
- getting the Rx message. The Rx message can then be got later
- using HAL_CAN_GetRxMessage(). Once the Rx message have been
- read, the notification can be activated again.
- *** Sleep mode ***
- ==================
- [..]
- (#) The CAN peripheral can be put in sleep mode (low power), using
- HAL_CAN_RequestSleep(). The sleep mode will be entered as soon as the
- current CAN activity (transmission or reception of a CAN frame) will
- be completed.
- (#) A notification can be activated to be informed when the sleep mode
- will be entered.
- (#) It can be checked if the sleep mode is entered using
- HAL_CAN_IsSleepActive().
- Note that the CAN state (accessible from the API HAL_CAN_GetState())
- is HAL_CAN_STATE_SLEEP_PENDING as soon as the sleep mode request is
- submitted (the sleep mode is not yet entered), and become
- HAL_CAN_STATE_SLEEP_ACTIVE when the sleep mode is effective.
- (#) The wake-up from sleep mode can be triggered by two ways:
- (++) Using HAL_CAN_WakeUp(). When returning from this function,
- the sleep mode is exited (if return status is HAL_OK).
- (++) When a start of Rx CAN frame is detected by the CAN peripheral,
- if automatic wake up mode is enabled.
- *** Callback registration ***
- =============================================
- The compilation define USE_HAL_CAN_REGISTER_CALLBACKS when set to 1
- allows the user to configure dynamically the driver callbacks.
- Use Function HAL_CAN_RegisterCallback() to register an interrupt callback.
- Function HAL_CAN_RegisterCallback() allows to register following callbacks:
- (+) TxMailbox0CompleteCallback : Tx Mailbox 0 Complete Callback.
- (+) TxMailbox1CompleteCallback : Tx Mailbox 1 Complete Callback.
- (+) TxMailbox2CompleteCallback : Tx Mailbox 2 Complete Callback.
- (+) TxMailbox0AbortCallback : Tx Mailbox 0 Abort Callback.
- (+) TxMailbox1AbortCallback : Tx Mailbox 1 Abort Callback.
- (+) TxMailbox2AbortCallback : Tx Mailbox 2 Abort Callback.
- (+) RxFifo0MsgPendingCallback : Rx Fifo 0 Message Pending Callback.
- (+) RxFifo0FullCallback : Rx Fifo 0 Full Callback.
- (+) RxFifo1MsgPendingCallback : Rx Fifo 1 Message Pending Callback.
- (+) RxFifo1FullCallback : Rx Fifo 1 Full Callback.
- (+) SleepCallback : Sleep Callback.
- (+) WakeUpFromRxMsgCallback : Wake Up From Rx Message Callback.
- (+) ErrorCallback : Error Callback.
- (+) MspInitCallback : CAN MspInit.
- (+) MspDeInitCallback : CAN MspDeInit.
- This function takes as parameters the HAL peripheral handle, the Callback ID
- and a pointer to the user callback function.
- Use function HAL_CAN_UnRegisterCallback() to reset a callback to the default
- weak function.
- HAL_CAN_UnRegisterCallback takes as parameters the HAL peripheral handle,
- and the Callback ID.
- This function allows to reset following callbacks:
- (+) TxMailbox0CompleteCallback : Tx Mailbox 0 Complete Callback.
- (+) TxMailbox1CompleteCallback : Tx Mailbox 1 Complete Callback.
- (+) TxMailbox2CompleteCallback : Tx Mailbox 2 Complete Callback.
- (+) TxMailbox0AbortCallback : Tx Mailbox 0 Abort Callback.
- (+) TxMailbox1AbortCallback : Tx Mailbox 1 Abort Callback.
- (+) TxMailbox2AbortCallback : Tx Mailbox 2 Abort Callback.
- (+) RxFifo0MsgPendingCallback : Rx Fifo 0 Message Pending Callback.
- (+) RxFifo0FullCallback : Rx Fifo 0 Full Callback.
- (+) RxFifo1MsgPendingCallback : Rx Fifo 1 Message Pending Callback.
- (+) RxFifo1FullCallback : Rx Fifo 1 Full Callback.
- (+) SleepCallback : Sleep Callback.
- (+) WakeUpFromRxMsgCallback : Wake Up From Rx Message Callback.
- (+) ErrorCallback : Error Callback.
- (+) MspInitCallback : CAN MspInit.
- (+) MspDeInitCallback : CAN MspDeInit.
- By default, after the HAL_CAN_Init() and when the state is HAL_CAN_STATE_RESET,
- all callbacks are set to the corresponding weak functions:
- example HAL_CAN_ErrorCallback().
- Exception done for MspInit and MspDeInit functions that are
- reset to the legacy weak function in the HAL_CAN_Init()/ HAL_CAN_DeInit() only when
- these callbacks are null (not registered beforehand).
- if not, MspInit or MspDeInit are not null, the HAL_CAN_Init()/ HAL_CAN_DeInit()
- keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
- Callbacks can be registered/unregistered in HAL_CAN_STATE_READY state only.
- Exception done MspInit/MspDeInit that can be registered/unregistered
- in HAL_CAN_STATE_READY or HAL_CAN_STATE_RESET state,
- thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
- In that case first register the MspInit/MspDeInit user callbacks
- using HAL_CAN_RegisterCallback() before calling HAL_CAN_DeInit()
- or HAL_CAN_Init() function.
- When The compilation define USE_HAL_CAN_REGISTER_CALLBACKS is set to 0 or
- not defined, the callback registration feature is not available and all callbacks
- are set to the corresponding weak functions.
- @endverbatim
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32l4xx_hal.h"
- /** @addtogroup STM32L4xx_HAL_Driver
- * @{
- */
- #if defined(CAN1)
- /** @defgroup CAN CAN
- * @brief CAN driver modules
- * @{
- */
- #ifdef HAL_CAN_MODULE_ENABLED
- #ifdef HAL_CAN_LEGACY_MODULE_ENABLED
- #error "The CAN driver cannot be used with its legacy, Please enable only one CAN module at once"
- #endif /* HAL_CAN_LEGACY_MODULE_ENABLED */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /** @defgroup CAN_Private_Constants CAN Private Constants
- * @{
- */
- #define CAN_TIMEOUT_VALUE 10U
- #define CAN_WAKEUP_TIMEOUT_COUNTER 1000000U
- /**
- * @}
- */
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Exported functions --------------------------------------------------------*/
- /** @defgroup CAN_Exported_Functions CAN Exported Functions
- * @{
- */
- /** @defgroup CAN_Exported_Functions_Group1 Initialization and de-initialization functions
- * @brief Initialization and Configuration functions
- *
- @verbatim
- ==============================================================================
- ##### Initialization and de-initialization functions #####
- ==============================================================================
- [..] This section provides functions allowing to:
- (+) HAL_CAN_Init : Initialize and configure the CAN.
- (+) HAL_CAN_DeInit : De-initialize the CAN.
- (+) HAL_CAN_MspInit : Initialize the CAN MSP.
- (+) HAL_CAN_MspDeInit : DeInitialize the CAN MSP.
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the CAN peripheral according to the specified
- * parameters in the CAN_InitStruct.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef *hcan)
- {
- uint32_t tickstart;
- /* Check CAN handle */
- if (hcan == NULL)
- {
- return HAL_ERROR;
- }
- /* Check the parameters */
- assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
- assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TimeTriggeredMode));
- assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoBusOff));
- assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoWakeUp));
- assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoRetransmission));
- assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ReceiveFifoLocked));
- assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TransmitFifoPriority));
- assert_param(IS_CAN_MODE(hcan->Init.Mode));
- assert_param(IS_CAN_SJW(hcan->Init.SyncJumpWidth));
- assert_param(IS_CAN_BS1(hcan->Init.TimeSeg1));
- assert_param(IS_CAN_BS2(hcan->Init.TimeSeg2));
- assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler));
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- if (hcan->State == HAL_CAN_STATE_RESET)
- {
- /* Reset callbacks to legacy functions */
- hcan->RxFifo0MsgPendingCallback = HAL_CAN_RxFifo0MsgPendingCallback; /* Legacy weak RxFifo0MsgPendingCallback */
- hcan->RxFifo0FullCallback = HAL_CAN_RxFifo0FullCallback; /* Legacy weak RxFifo0FullCallback */
- hcan->RxFifo1MsgPendingCallback = HAL_CAN_RxFifo1MsgPendingCallback; /* Legacy weak RxFifo1MsgPendingCallback */
- hcan->RxFifo1FullCallback = HAL_CAN_RxFifo1FullCallback; /* Legacy weak RxFifo1FullCallback */
- hcan->TxMailbox0CompleteCallback = HAL_CAN_TxMailbox0CompleteCallback; /* Legacy weak TxMailbox0CompleteCallback */
- hcan->TxMailbox1CompleteCallback = HAL_CAN_TxMailbox1CompleteCallback; /* Legacy weak TxMailbox1CompleteCallback */
- hcan->TxMailbox2CompleteCallback = HAL_CAN_TxMailbox2CompleteCallback; /* Legacy weak TxMailbox2CompleteCallback */
- hcan->TxMailbox0AbortCallback = HAL_CAN_TxMailbox0AbortCallback; /* Legacy weak TxMailbox0AbortCallback */
- hcan->TxMailbox1AbortCallback = HAL_CAN_TxMailbox1AbortCallback; /* Legacy weak TxMailbox1AbortCallback */
- hcan->TxMailbox2AbortCallback = HAL_CAN_TxMailbox2AbortCallback; /* Legacy weak TxMailbox2AbortCallback */
- hcan->SleepCallback = HAL_CAN_SleepCallback; /* Legacy weak SleepCallback */
- hcan->WakeUpFromRxMsgCallback = HAL_CAN_WakeUpFromRxMsgCallback; /* Legacy weak WakeUpFromRxMsgCallback */
- hcan->ErrorCallback = HAL_CAN_ErrorCallback; /* Legacy weak ErrorCallback */
- if (hcan->MspInitCallback == NULL)
- {
- hcan->MspInitCallback = HAL_CAN_MspInit; /* Legacy weak MspInit */
- }
- /* Init the low level hardware: CLOCK, NVIC */
- hcan->MspInitCallback(hcan);
- }
- #else
- if (hcan->State == HAL_CAN_STATE_RESET)
- {
- /* Init the low level hardware: CLOCK, NVIC */
- HAL_CAN_MspInit(hcan);
- }
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- /* Request initialisation */
- SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
- /* Get tick */
- tickstart = HAL_GetTick();
- /* Wait initialisation acknowledge */
- while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U)
- {
- if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
- /* Change CAN state */
- hcan->State = HAL_CAN_STATE_ERROR;
- return HAL_ERROR;
- }
- }
- /* Exit from sleep mode */
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
- /* Get tick */
- tickstart = HAL_GetTick();
- /* Check Sleep mode leave acknowledge */
- while ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
- {
- if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
- /* Change CAN state */
- hcan->State = HAL_CAN_STATE_ERROR;
- return HAL_ERROR;
- }
- }
- /* Set the time triggered communication mode */
- if (hcan->Init.TimeTriggeredMode == ENABLE)
- {
- SET_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
- }
- else
- {
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
- }
- /* Set the automatic bus-off management */
- if (hcan->Init.AutoBusOff == ENABLE)
- {
- SET_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
- }
- else
- {
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
- }
- /* Set the automatic wake-up mode */
- if (hcan->Init.AutoWakeUp == ENABLE)
- {
- SET_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
- }
- else
- {
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
- }
- /* Set the automatic retransmission */
- if (hcan->Init.AutoRetransmission == ENABLE)
- {
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_NART);
- }
- else
- {
- SET_BIT(hcan->Instance->MCR, CAN_MCR_NART);
- }
- /* Set the receive FIFO locked mode */
- if (hcan->Init.ReceiveFifoLocked == ENABLE)
- {
- SET_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
- }
- else
- {
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
- }
- /* Set the transmit FIFO priority */
- if (hcan->Init.TransmitFifoPriority == ENABLE)
- {
- SET_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
- }
- else
- {
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
- }
- /* Set the bit timing register */
- WRITE_REG(hcan->Instance->BTR, (uint32_t)(hcan->Init.Mode |
- hcan->Init.SyncJumpWidth |
- hcan->Init.TimeSeg1 |
- hcan->Init.TimeSeg2 |
- (hcan->Init.Prescaler - 1U)));
- /* Initialize the error code */
- hcan->ErrorCode = HAL_CAN_ERROR_NONE;
- /* Initialize the CAN state */
- hcan->State = HAL_CAN_STATE_READY;
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Deinitializes the CAN peripheral registers to their default
- * reset values.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef *hcan)
- {
- /* Check CAN handle */
- if (hcan == NULL)
- {
- return HAL_ERROR;
- }
- /* Check the parameters */
- assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
- /* Stop the CAN module */
- (void)HAL_CAN_Stop(hcan);
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- if (hcan->MspDeInitCallback == NULL)
- {
- hcan->MspDeInitCallback = HAL_CAN_MspDeInit; /* Legacy weak MspDeInit */
- }
- /* DeInit the low level hardware: CLOCK, NVIC */
- hcan->MspDeInitCallback(hcan);
- #else
- /* DeInit the low level hardware: CLOCK, NVIC */
- HAL_CAN_MspDeInit(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- /* Reset the CAN peripheral */
- SET_BIT(hcan->Instance->MCR, CAN_MCR_RESET);
- /* Reset the CAN ErrorCode */
- hcan->ErrorCode = HAL_CAN_ERROR_NONE;
- /* Change CAN state */
- hcan->State = HAL_CAN_STATE_RESET;
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Initializes the CAN MSP.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_MspInit could be implemented in the user file
- */
- }
- /**
- * @brief DeInitializes the CAN MSP.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_MspDeInit could be implemented in the user file
- */
- }
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /**
- * @brief Register a CAN CallBack.
- * To be used instead of the weak predefined callback
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for CAN module
- * @param CallbackID ID of the callback to be registered
- * This parameter can be one of the following values:
- * @arg @ref HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID Tx Mailbox 0 Complete callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX1_COMPLETE_CB_ID Tx Mailbox 1 Complete callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX2_COMPLETE_CB_ID Tx Mailbox 2 Complete callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX0_ABORT_CB_ID Tx Mailbox 0 Abort callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX1_ABORT_CB_ID Tx Mailbox 1 Abort callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX2_ABORT_CB_ID Tx Mailbox 2 Abort callback ID
- * @arg @ref HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID Rx Fifo 0 message pending callback ID
- * @arg @ref HAL_CAN_RX_FIFO0_FULL_CB_ID Rx Fifo 0 full callback ID
- * @arg @ref HAL_CAN_RX_FIFO1_MSG_PENDING_CB_ID Rx Fifo 1 message pending callback ID
- * @arg @ref HAL_CAN_RX_FIFO1_FULL_CB_ID Rx Fifo 1 full callback ID
- * @arg @ref HAL_CAN_SLEEP_CB_ID Sleep callback ID
- * @arg @ref HAL_CAN_WAKEUP_FROM_RX_MSG_CB_ID Wake Up from Rx message callback ID
- * @arg @ref HAL_CAN_ERROR_CB_ID Error callback ID
- * @arg @ref HAL_CAN_MSPINIT_CB_ID MspInit callback ID
- * @arg @ref HAL_CAN_MSPDEINIT_CB_ID MspDeInit callback ID
- * @param pCallback pointer to the Callback function
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_RegisterCallback(CAN_HandleTypeDef *hcan, HAL_CAN_CallbackIDTypeDef CallbackID,
- void (* pCallback)(CAN_HandleTypeDef *_hcan))
- {
- HAL_StatusTypeDef status = HAL_OK;
- if (pCallback == NULL)
- {
- /* Update the error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
- return HAL_ERROR;
- }
- if (hcan->State == HAL_CAN_STATE_READY)
- {
- switch (CallbackID)
- {
- case HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID :
- hcan->TxMailbox0CompleteCallback = pCallback;
- break;
- case HAL_CAN_TX_MAILBOX1_COMPLETE_CB_ID :
- hcan->TxMailbox1CompleteCallback = pCallback;
- break;
- case HAL_CAN_TX_MAILBOX2_COMPLETE_CB_ID :
- hcan->TxMailbox2CompleteCallback = pCallback;
- break;
- case HAL_CAN_TX_MAILBOX0_ABORT_CB_ID :
- hcan->TxMailbox0AbortCallback = pCallback;
- break;
- case HAL_CAN_TX_MAILBOX1_ABORT_CB_ID :
- hcan->TxMailbox1AbortCallback = pCallback;
- break;
- case HAL_CAN_TX_MAILBOX2_ABORT_CB_ID :
- hcan->TxMailbox2AbortCallback = pCallback;
- break;
- case HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID :
- hcan->RxFifo0MsgPendingCallback = pCallback;
- break;
- case HAL_CAN_RX_FIFO0_FULL_CB_ID :
- hcan->RxFifo0FullCallback = pCallback;
- break;
- case HAL_CAN_RX_FIFO1_MSG_PENDING_CB_ID :
- hcan->RxFifo1MsgPendingCallback = pCallback;
- break;
- case HAL_CAN_RX_FIFO1_FULL_CB_ID :
- hcan->RxFifo1FullCallback = pCallback;
- break;
- case HAL_CAN_SLEEP_CB_ID :
- hcan->SleepCallback = pCallback;
- break;
- case HAL_CAN_WAKEUP_FROM_RX_MSG_CB_ID :
- hcan->WakeUpFromRxMsgCallback = pCallback;
- break;
- case HAL_CAN_ERROR_CB_ID :
- hcan->ErrorCallback = pCallback;
- break;
- case HAL_CAN_MSPINIT_CB_ID :
- hcan->MspInitCallback = pCallback;
- break;
- case HAL_CAN_MSPDEINIT_CB_ID :
- hcan->MspDeInitCallback = pCallback;
- break;
- default :
- /* Update the error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else if (hcan->State == HAL_CAN_STATE_RESET)
- {
- switch (CallbackID)
- {
- case HAL_CAN_MSPINIT_CB_ID :
- hcan->MspInitCallback = pCallback;
- break;
- case HAL_CAN_MSPDEINIT_CB_ID :
- hcan->MspDeInitCallback = pCallback;
- break;
- default :
- /* Update the error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else
- {
- /* Update the error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- return status;
- }
- /**
- * @brief Unregister a CAN CallBack.
- * CAN callback is redirected to the weak predefined callback
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for CAN module
- * @param CallbackID ID of the callback to be unregistered
- * This parameter can be one of the following values:
- * @arg @ref HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID Tx Mailbox 0 Complete callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX1_COMPLETE_CB_ID Tx Mailbox 1 Complete callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX2_COMPLETE_CB_ID Tx Mailbox 2 Complete callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX0_ABORT_CB_ID Tx Mailbox 0 Abort callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX1_ABORT_CB_ID Tx Mailbox 1 Abort callback ID
- * @arg @ref HAL_CAN_TX_MAILBOX2_ABORT_CB_ID Tx Mailbox 2 Abort callback ID
- * @arg @ref HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID Rx Fifo 0 message pending callback ID
- * @arg @ref HAL_CAN_RX_FIFO0_FULL_CB_ID Rx Fifo 0 full callback ID
- * @arg @ref HAL_CAN_RX_FIFO1_MSG_PENDING_CB_ID Rx Fifo 1 message pending callback ID
- * @arg @ref HAL_CAN_RX_FIFO1_FULL_CB_ID Rx Fifo 1 full callback ID
- * @arg @ref HAL_CAN_SLEEP_CB_ID Sleep callback ID
- * @arg @ref HAL_CAN_WAKEUP_FROM_RX_MSG_CB_ID Wake Up from Rx message callback ID
- * @arg @ref HAL_CAN_ERROR_CB_ID Error callback ID
- * @arg @ref HAL_CAN_MSPINIT_CB_ID MspInit callback ID
- * @arg @ref HAL_CAN_MSPDEINIT_CB_ID MspDeInit callback ID
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_UnRegisterCallback(CAN_HandleTypeDef *hcan, HAL_CAN_CallbackIDTypeDef CallbackID)
- {
- HAL_StatusTypeDef status = HAL_OK;
- if (hcan->State == HAL_CAN_STATE_READY)
- {
- switch (CallbackID)
- {
- case HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID :
- hcan->TxMailbox0CompleteCallback = HAL_CAN_TxMailbox0CompleteCallback;
- break;
- case HAL_CAN_TX_MAILBOX1_COMPLETE_CB_ID :
- hcan->TxMailbox1CompleteCallback = HAL_CAN_TxMailbox1CompleteCallback;
- break;
- case HAL_CAN_TX_MAILBOX2_COMPLETE_CB_ID :
- hcan->TxMailbox2CompleteCallback = HAL_CAN_TxMailbox2CompleteCallback;
- break;
- case HAL_CAN_TX_MAILBOX0_ABORT_CB_ID :
- hcan->TxMailbox0AbortCallback = HAL_CAN_TxMailbox0AbortCallback;
- break;
- case HAL_CAN_TX_MAILBOX1_ABORT_CB_ID :
- hcan->TxMailbox1AbortCallback = HAL_CAN_TxMailbox1AbortCallback;
- break;
- case HAL_CAN_TX_MAILBOX2_ABORT_CB_ID :
- hcan->TxMailbox2AbortCallback = HAL_CAN_TxMailbox2AbortCallback;
- break;
- case HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID :
- hcan->RxFifo0MsgPendingCallback = HAL_CAN_RxFifo0MsgPendingCallback;
- break;
- case HAL_CAN_RX_FIFO0_FULL_CB_ID :
- hcan->RxFifo0FullCallback = HAL_CAN_RxFifo0FullCallback;
- break;
- case HAL_CAN_RX_FIFO1_MSG_PENDING_CB_ID :
- hcan->RxFifo1MsgPendingCallback = HAL_CAN_RxFifo1MsgPendingCallback;
- break;
- case HAL_CAN_RX_FIFO1_FULL_CB_ID :
- hcan->RxFifo1FullCallback = HAL_CAN_RxFifo1FullCallback;
- break;
- case HAL_CAN_SLEEP_CB_ID :
- hcan->SleepCallback = HAL_CAN_SleepCallback;
- break;
- case HAL_CAN_WAKEUP_FROM_RX_MSG_CB_ID :
- hcan->WakeUpFromRxMsgCallback = HAL_CAN_WakeUpFromRxMsgCallback;
- break;
- case HAL_CAN_ERROR_CB_ID :
- hcan->ErrorCallback = HAL_CAN_ErrorCallback;
- break;
- case HAL_CAN_MSPINIT_CB_ID :
- hcan->MspInitCallback = HAL_CAN_MspInit;
- break;
- case HAL_CAN_MSPDEINIT_CB_ID :
- hcan->MspDeInitCallback = HAL_CAN_MspDeInit;
- break;
- default :
- /* Update the error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else if (hcan->State == HAL_CAN_STATE_RESET)
- {
- switch (CallbackID)
- {
- case HAL_CAN_MSPINIT_CB_ID :
- hcan->MspInitCallback = HAL_CAN_MspInit;
- break;
- case HAL_CAN_MSPDEINIT_CB_ID :
- hcan->MspDeInitCallback = HAL_CAN_MspDeInit;
- break;
- default :
- /* Update the error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else
- {
- /* Update the error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
- /* Return error status */
- status = HAL_ERROR;
- }
- return status;
- }
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- /**
- * @}
- */
- /** @defgroup CAN_Exported_Functions_Group2 Configuration functions
- * @brief Configuration functions.
- *
- @verbatim
- ==============================================================================
- ##### Configuration functions #####
- ==============================================================================
- [..] This section provides functions allowing to:
- (+) HAL_CAN_ConfigFilter : Configure the CAN reception filters
- @endverbatim
- * @{
- */
- /**
- * @brief Configures the CAN reception filter according to the specified
- * parameters in the CAN_FilterInitStruct.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @param sFilterConfig pointer to a CAN_FilterTypeDef structure that
- * contains the filter configuration information.
- * @retval None
- */
- HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan, const CAN_FilterTypeDef *sFilterConfig)
- {
- uint32_t filternbrbitpos;
- CAN_TypeDef *can_ip = hcan->Instance;
- HAL_CAN_StateTypeDef state = hcan->State;
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Check the parameters */
- assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdHigh));
- assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdLow));
- assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdHigh));
- assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdLow));
- assert_param(IS_CAN_FILTER_MODE(sFilterConfig->FilterMode));
- assert_param(IS_CAN_FILTER_SCALE(sFilterConfig->FilterScale));
- assert_param(IS_CAN_FILTER_FIFO(sFilterConfig->FilterFIFOAssignment));
- assert_param(IS_CAN_FILTER_ACTIVATION(sFilterConfig->FilterActivation));
- #if defined(CAN2)
- /* CAN1 and CAN2 are dual instances with 28 common filters banks */
- /* Select master instance to access the filter banks */
- can_ip = CAN1;
- /* Check the parameters */
- assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank));
- assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank));
- #else
- /* CAN1 is single instance with 14 dedicated filters banks */
- /* Check the parameters */
- assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank));
- #endif /* CAN3 */
- /* Initialisation mode for the filter */
- SET_BIT(can_ip->FMR, CAN_FMR_FINIT);
- #if defined(CAN2)
- /* Select the start filter number of CAN2 slave instance */
- CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB);
- SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos);
- #endif /* CAN3 */
- /* Convert filter number into bit position */
- filternbrbitpos = (uint32_t)1 << (sFilterConfig->FilterBank & 0x1FU);
- /* Filter Deactivation */
- CLEAR_BIT(can_ip->FA1R, filternbrbitpos);
- /* Filter Scale */
- if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT)
- {
- /* 16-bit scale for the filter */
- CLEAR_BIT(can_ip->FS1R, filternbrbitpos);
- /* First 16-bit identifier and First 16-bit mask */
- /* Or First 16-bit identifier and Second 16-bit identifier */
- can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
- ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16U) |
- (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
- /* Second 16-bit identifier and Second 16-bit mask */
- /* Or Third 16-bit identifier and Fourth 16-bit identifier */
- can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
- ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
- (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh);
- }
- if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT)
- {
- /* 32-bit scale for the filter */
- SET_BIT(can_ip->FS1R, filternbrbitpos);
- /* 32-bit identifier or First 32-bit identifier */
- can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
- ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh) << 16U) |
- (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
- /* 32-bit mask or Second 32-bit identifier */
- can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
- ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
- (0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow);
- }
- /* Filter Mode */
- if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK)
- {
- /* Id/Mask mode for the filter*/
- CLEAR_BIT(can_ip->FM1R, filternbrbitpos);
- }
- else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
- {
- /* Identifier list mode for the filter*/
- SET_BIT(can_ip->FM1R, filternbrbitpos);
- }
- /* Filter FIFO assignment */
- if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0)
- {
- /* FIFO 0 assignation for the filter */
- CLEAR_BIT(can_ip->FFA1R, filternbrbitpos);
- }
- else
- {
- /* FIFO 1 assignation for the filter */
- SET_BIT(can_ip->FFA1R, filternbrbitpos);
- }
- /* Filter activation */
- if (sFilterConfig->FilterActivation == CAN_FILTER_ENABLE)
- {
- SET_BIT(can_ip->FA1R, filternbrbitpos);
- }
- /* Leave the initialisation mode for the filter */
- CLEAR_BIT(can_ip->FMR, CAN_FMR_FINIT);
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
- return HAL_ERROR;
- }
- }
- /**
- * @}
- */
- /** @defgroup CAN_Exported_Functions_Group3 Control functions
- * @brief Control functions
- *
- @verbatim
- ==============================================================================
- ##### Control functions #####
- ==============================================================================
- [..] This section provides functions allowing to:
- (+) HAL_CAN_Start : Start the CAN module
- (+) HAL_CAN_Stop : Stop the CAN module
- (+) HAL_CAN_RequestSleep : Request sleep mode entry.
- (+) HAL_CAN_WakeUp : Wake up from sleep mode.
- (+) HAL_CAN_IsSleepActive : Check is sleep mode is active.
- (+) HAL_CAN_AddTxMessage : Add a message to the Tx mailboxes
- and activate the corresponding
- transmission request
- (+) HAL_CAN_AbortTxRequest : Abort transmission request
- (+) HAL_CAN_GetTxMailboxesFreeLevel : Return Tx mailboxes free level
- (+) HAL_CAN_IsTxMessagePending : Check if a transmission request is
- pending on the selected Tx mailbox
- (+) HAL_CAN_GetRxMessage : Get a CAN frame from the Rx FIFO
- (+) HAL_CAN_GetRxFifoFillLevel : Return Rx FIFO fill level
- @endverbatim
- * @{
- */
- /**
- * @brief Start the CAN module.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_Start(CAN_HandleTypeDef *hcan)
- {
- uint32_t tickstart;
- if (hcan->State == HAL_CAN_STATE_READY)
- {
- /* Change CAN peripheral state */
- hcan->State = HAL_CAN_STATE_LISTENING;
- /* Request leave initialisation */
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
- /* Get tick */
- tickstart = HAL_GetTick();
- /* Wait the acknowledge */
- while ((hcan->Instance->MSR & CAN_MSR_INAK) != 0U)
- {
- /* Check for the Timeout */
- if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
- /* Change CAN state */
- hcan->State = HAL_CAN_STATE_ERROR;
- return HAL_ERROR;
- }
- }
- /* Reset the CAN ErrorCode */
- hcan->ErrorCode = HAL_CAN_ERROR_NONE;
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_READY;
- return HAL_ERROR;
- }
- }
- /**
- * @brief Stop the CAN module and enable access to configuration registers.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_Stop(CAN_HandleTypeDef *hcan)
- {
- uint32_t tickstart;
- if (hcan->State == HAL_CAN_STATE_LISTENING)
- {
- /* Request initialisation */
- SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
- /* Get tick */
- tickstart = HAL_GetTick();
- /* Wait the acknowledge */
- while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U)
- {
- /* Check for the Timeout */
- if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
- /* Change CAN state */
- hcan->State = HAL_CAN_STATE_ERROR;
- return HAL_ERROR;
- }
- }
- /* Exit from sleep mode */
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
- /* Change CAN peripheral state */
- hcan->State = HAL_CAN_STATE_READY;
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_STARTED;
- return HAL_ERROR;
- }
- }
- /**
- * @brief Request the sleep mode (low power) entry.
- * When returning from this function, Sleep mode will be entered
- * as soon as the current CAN activity (transmission or reception
- * of a CAN frame) has been completed.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval HAL status.
- */
- HAL_StatusTypeDef HAL_CAN_RequestSleep(CAN_HandleTypeDef *hcan)
- {
- HAL_CAN_StateTypeDef state = hcan->State;
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Request Sleep mode */
- SET_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
- /* Return function status */
- return HAL_ERROR;
- }
- }
- /**
- * @brief Wake up from sleep mode.
- * When returning with HAL_OK status from this function, Sleep mode
- * is exited.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval HAL status.
- */
- HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan)
- {
- __IO uint32_t count = 0;
- HAL_CAN_StateTypeDef state = hcan->State;
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Wake up request */
- CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
- /* Wait sleep mode is exited */
- do
- {
- /* Increment counter */
- count++;
- /* Check if timeout is reached */
- if (count > CAN_WAKEUP_TIMEOUT_COUNTER)
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
- return HAL_ERROR;
- }
- } while ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U);
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
- return HAL_ERROR;
- }
- }
- /**
- * @brief Check is sleep mode is active.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval Status
- * - 0 : Sleep mode is not active.
- * - 1 : Sleep mode is active.
- */
- uint32_t HAL_CAN_IsSleepActive(const CAN_HandleTypeDef *hcan)
- {
- uint32_t status = 0U;
- HAL_CAN_StateTypeDef state = hcan->State;
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Check Sleep mode */
- if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
- {
- status = 1U;
- }
- }
- /* Return function status */
- return status;
- }
- /**
- * @brief Add a message to the first free Tx mailbox and activate the
- * corresponding transmission request.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @param pHeader pointer to a CAN_TxHeaderTypeDef structure.
- * @param aData array containing the payload of the Tx frame.
- * @param pTxMailbox pointer to a variable where the function will return
- * the TxMailbox used to store the Tx message.
- * This parameter can be a value of @arg CAN_Tx_Mailboxes.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, const CAN_TxHeaderTypeDef *pHeader,
- const uint8_t aData[], uint32_t *pTxMailbox)
- {
- uint32_t transmitmailbox;
- HAL_CAN_StateTypeDef state = hcan->State;
- uint32_t tsr = READ_REG(hcan->Instance->TSR);
- /* Check the parameters */
- assert_param(IS_CAN_IDTYPE(pHeader->IDE));
- assert_param(IS_CAN_RTR(pHeader->RTR));
- assert_param(IS_CAN_DLC(pHeader->DLC));
- if (pHeader->IDE == CAN_ID_STD)
- {
- assert_param(IS_CAN_STDID(pHeader->StdId));
- }
- else
- {
- assert_param(IS_CAN_EXTID(pHeader->ExtId));
- }
- assert_param(IS_FUNCTIONAL_STATE(pHeader->TransmitGlobalTime));
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Check that all the Tx mailboxes are not full */
- if (((tsr & CAN_TSR_TME0) != 0U) ||
- ((tsr & CAN_TSR_TME1) != 0U) ||
- ((tsr & CAN_TSR_TME2) != 0U))
- {
- /* Select an empty transmit mailbox */
- transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;
- /* Store the Tx mailbox */
- *pTxMailbox = (uint32_t)1 << transmitmailbox;
- /* Set up the Id */
- if (pHeader->IDE == CAN_ID_STD)
- {
- hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) |
- pHeader->RTR);
- }
- else
- {
- hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->ExtId << CAN_TI0R_EXID_Pos) |
- pHeader->IDE |
- pHeader->RTR);
- }
- /* Set up the DLC */
- hcan->Instance->sTxMailBox[transmitmailbox].TDTR = (pHeader->DLC);
- /* Set up the Transmit Global Time mode */
- if (pHeader->TransmitGlobalTime == ENABLE)
- {
- SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TDTR, CAN_TDT0R_TGT);
- }
- /* Set up the data field */
- WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDHR,
- ((uint32_t)aData[7] << CAN_TDH0R_DATA7_Pos) |
- ((uint32_t)aData[6] << CAN_TDH0R_DATA6_Pos) |
- ((uint32_t)aData[5] << CAN_TDH0R_DATA5_Pos) |
- ((uint32_t)aData[4] << CAN_TDH0R_DATA4_Pos));
- WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDLR,
- ((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) |
- ((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) |
- ((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) |
- ((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos));
- /* Request transmission */
- SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TIR, CAN_TI0R_TXRQ);
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
- return HAL_ERROR;
- }
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
- return HAL_ERROR;
- }
- }
- /**
- * @brief Abort transmission requests
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @param TxMailboxes List of the Tx Mailboxes to abort.
- * This parameter can be any combination of @arg CAN_Tx_Mailboxes.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_AbortTxRequest(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
- {
- HAL_CAN_StateTypeDef state = hcan->State;
- /* Check function parameters */
- assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Check Tx Mailbox 0 */
- if ((TxMailboxes & CAN_TX_MAILBOX0) != 0U)
- {
- /* Add cancellation request for Tx Mailbox 0 */
- SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ0);
- }
- /* Check Tx Mailbox 1 */
- if ((TxMailboxes & CAN_TX_MAILBOX1) != 0U)
- {
- /* Add cancellation request for Tx Mailbox 1 */
- SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ1);
- }
- /* Check Tx Mailbox 2 */
- if ((TxMailboxes & CAN_TX_MAILBOX2) != 0U)
- {
- /* Add cancellation request for Tx Mailbox 2 */
- SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ2);
- }
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
- return HAL_ERROR;
- }
- }
- /**
- * @brief Return Tx Mailboxes free level: number of free Tx Mailboxes.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval Number of free Tx Mailboxes.
- */
- uint32_t HAL_CAN_GetTxMailboxesFreeLevel(const CAN_HandleTypeDef *hcan)
- {
- uint32_t freelevel = 0U;
- HAL_CAN_StateTypeDef state = hcan->State;
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Check Tx Mailbox 0 status */
- if ((hcan->Instance->TSR & CAN_TSR_TME0) != 0U)
- {
- freelevel++;
- }
- /* Check Tx Mailbox 1 status */
- if ((hcan->Instance->TSR & CAN_TSR_TME1) != 0U)
- {
- freelevel++;
- }
- /* Check Tx Mailbox 2 status */
- if ((hcan->Instance->TSR & CAN_TSR_TME2) != 0U)
- {
- freelevel++;
- }
- }
- /* Return Tx Mailboxes free level */
- return freelevel;
- }
- /**
- * @brief Check if a transmission request is pending on the selected Tx
- * Mailboxes.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @param TxMailboxes List of Tx Mailboxes to check.
- * This parameter can be any combination of @arg CAN_Tx_Mailboxes.
- * @retval Status
- * - 0 : No pending transmission request on any selected Tx Mailboxes.
- * - 1 : Pending transmission request on at least one of the selected
- * Tx Mailbox.
- */
- uint32_t HAL_CAN_IsTxMessagePending(const CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
- {
- uint32_t status = 0U;
- HAL_CAN_StateTypeDef state = hcan->State;
- /* Check function parameters */
- assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Check pending transmission request on the selected Tx Mailboxes */
- if ((hcan->Instance->TSR & (TxMailboxes << CAN_TSR_TME0_Pos)) != (TxMailboxes << CAN_TSR_TME0_Pos))
- {
- status = 1U;
- }
- }
- /* Return status */
- return status;
- }
- /**
- * @brief Return timestamp of Tx message sent, if time triggered communication
- mode is enabled.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @param TxMailbox Tx Mailbox where the timestamp of message sent will be
- * read.
- * This parameter can be one value of @arg CAN_Tx_Mailboxes.
- * @retval Timestamp of message sent from Tx Mailbox.
- */
- uint32_t HAL_CAN_GetTxTimestamp(const CAN_HandleTypeDef *hcan, uint32_t TxMailbox)
- {
- uint32_t timestamp = 0U;
- uint32_t transmitmailbox;
- HAL_CAN_StateTypeDef state = hcan->State;
- /* Check function parameters */
- assert_param(IS_CAN_TX_MAILBOX(TxMailbox));
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Select the Tx mailbox */
- transmitmailbox = POSITION_VAL(TxMailbox);
- /* Get timestamp */
- timestamp = (hcan->Instance->sTxMailBox[transmitmailbox].TDTR & CAN_TDT0R_TIME) >> CAN_TDT0R_TIME_Pos;
- }
- /* Return the timestamp */
- return timestamp;
- }
- /**
- * @brief Get an CAN frame from the Rx FIFO zone into the message RAM.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @param RxFifo Fifo number of the received message to be read.
- * This parameter can be a value of @arg CAN_receive_FIFO_number.
- * @param pHeader pointer to a CAN_RxHeaderTypeDef structure where the header
- * of the Rx frame will be stored.
- * @param aData array where the payload of the Rx frame will be stored.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo,
- CAN_RxHeaderTypeDef *pHeader, uint8_t aData[])
- {
- HAL_CAN_StateTypeDef state = hcan->State;
- assert_param(IS_CAN_RX_FIFO(RxFifo));
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Check the Rx FIFO */
- if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
- {
- /* Check that the Rx FIFO 0 is not empty */
- if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) == 0U)
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
- return HAL_ERROR;
- }
- }
- else /* Rx element is assigned to Rx FIFO 1 */
- {
- /* Check that the Rx FIFO 1 is not empty */
- if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) == 0U)
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
- return HAL_ERROR;
- }
- }
- /* Get the header */
- pHeader->IDE = CAN_RI0R_IDE & hcan->Instance->sFIFOMailBox[RxFifo].RIR;
- if (pHeader->IDE == CAN_ID_STD)
- {
- pHeader->StdId = (CAN_RI0R_STID & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_TI0R_STID_Pos;
- }
- else
- {
- pHeader->ExtId = ((CAN_RI0R_EXID | CAN_RI0R_STID) &
- hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_EXID_Pos;
- }
- pHeader->RTR = (CAN_RI0R_RTR & hcan->Instance->sFIFOMailBox[RxFifo].RIR);
- if (((CAN_RDT0R_DLC & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos) >= 8U)
- {
- /* Truncate DLC to 8 if received field is over range */
- pHeader->DLC = 8U;
- }
- else
- {
- pHeader->DLC = (CAN_RDT0R_DLC & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos;
- }
- pHeader->FilterMatchIndex = (CAN_RDT0R_FMI & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_FMI_Pos;
- pHeader->Timestamp = (CAN_RDT0R_TIME & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_TIME_Pos;
- /* Get the data */
- aData[0] = (uint8_t)((CAN_RDL0R_DATA0 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA0_Pos);
- aData[1] = (uint8_t)((CAN_RDL0R_DATA1 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA1_Pos);
- aData[2] = (uint8_t)((CAN_RDL0R_DATA2 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA2_Pos);
- aData[3] = (uint8_t)((CAN_RDL0R_DATA3 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA3_Pos);
- aData[4] = (uint8_t)((CAN_RDH0R_DATA4 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA4_Pos);
- aData[5] = (uint8_t)((CAN_RDH0R_DATA5 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA5_Pos);
- aData[6] = (uint8_t)((CAN_RDH0R_DATA6 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA6_Pos);
- aData[7] = (uint8_t)((CAN_RDH0R_DATA7 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA7_Pos);
- /* Release the FIFO */
- if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
- {
- /* Release RX FIFO 0 */
- SET_BIT(hcan->Instance->RF0R, CAN_RF0R_RFOM0);
- }
- else /* Rx element is assigned to Rx FIFO 1 */
- {
- /* Release RX FIFO 1 */
- SET_BIT(hcan->Instance->RF1R, CAN_RF1R_RFOM1);
- }
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
- return HAL_ERROR;
- }
- }
- /**
- * @brief Return Rx FIFO fill level.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @param RxFifo Rx FIFO.
- * This parameter can be a value of @arg CAN_receive_FIFO_number.
- * @retval Number of messages available in Rx FIFO.
- */
- uint32_t HAL_CAN_GetRxFifoFillLevel(const CAN_HandleTypeDef *hcan, uint32_t RxFifo)
- {
- uint32_t filllevel = 0U;
- HAL_CAN_StateTypeDef state = hcan->State;
- /* Check function parameters */
- assert_param(IS_CAN_RX_FIFO(RxFifo));
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- if (RxFifo == CAN_RX_FIFO0)
- {
- filllevel = hcan->Instance->RF0R & CAN_RF0R_FMP0;
- }
- else /* RxFifo == CAN_RX_FIFO1 */
- {
- filllevel = hcan->Instance->RF1R & CAN_RF1R_FMP1;
- }
- }
- /* Return Rx FIFO fill level */
- return filllevel;
- }
- /**
- * @}
- */
- /** @defgroup CAN_Exported_Functions_Group4 Interrupts management
- * @brief Interrupts management
- *
- @verbatim
- ==============================================================================
- ##### Interrupts management #####
- ==============================================================================
- [..] This section provides functions allowing to:
- (+) HAL_CAN_ActivateNotification : Enable interrupts
- (+) HAL_CAN_DeactivateNotification : Disable interrupts
- (+) HAL_CAN_IRQHandler : Handles CAN interrupt request
- @endverbatim
- * @{
- */
- /**
- * @brief Enable interrupts.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @param ActiveITs indicates which interrupts will be enabled.
- * This parameter can be any combination of @arg CAN_Interrupts.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t ActiveITs)
- {
- HAL_CAN_StateTypeDef state = hcan->State;
- /* Check function parameters */
- assert_param(IS_CAN_IT(ActiveITs));
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Enable the selected interrupts */
- __HAL_CAN_ENABLE_IT(hcan, ActiveITs);
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
- return HAL_ERROR;
- }
- }
- /**
- * @brief Disable interrupts.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @param InactiveITs indicates which interrupts will be disabled.
- * This parameter can be any combination of @arg CAN_Interrupts.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_DeactivateNotification(CAN_HandleTypeDef *hcan, uint32_t InactiveITs)
- {
- HAL_CAN_StateTypeDef state = hcan->State;
- /* Check function parameters */
- assert_param(IS_CAN_IT(InactiveITs));
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Disable the selected interrupts */
- __HAL_CAN_DISABLE_IT(hcan, InactiveITs);
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
- return HAL_ERROR;
- }
- }
- /**
- * @brief Handles CAN interrupt request
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan)
- {
- uint32_t errorcode = HAL_CAN_ERROR_NONE;
- uint32_t interrupts = READ_REG(hcan->Instance->IER);
- uint32_t msrflags = READ_REG(hcan->Instance->MSR);
- uint32_t tsrflags = READ_REG(hcan->Instance->TSR);
- uint32_t rf0rflags = READ_REG(hcan->Instance->RF0R);
- uint32_t rf1rflags = READ_REG(hcan->Instance->RF1R);
- uint32_t esrflags = READ_REG(hcan->Instance->ESR);
- /* Transmit Mailbox empty interrupt management *****************************/
- if ((interrupts & CAN_IT_TX_MAILBOX_EMPTY) != 0U)
- {
- /* Transmit Mailbox 0 management *****************************************/
- if ((tsrflags & CAN_TSR_RQCP0) != 0U)
- {
- /* Clear the Transmission Complete flag (and TXOK0,ALST0,TERR0 bits) */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP0);
- if ((tsrflags & CAN_TSR_TXOK0) != 0U)
- {
- /* Transmission Mailbox 0 complete callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox0CompleteCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox0CompleteCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- else
- {
- if ((tsrflags & CAN_TSR_ALST0) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_ALST0;
- }
- else if ((tsrflags & CAN_TSR_TERR0) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_TERR0;
- }
- else
- {
- /* Transmission Mailbox 0 abort callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox0AbortCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox0AbortCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- }
- /* Transmit Mailbox 1 management *****************************************/
- if ((tsrflags & CAN_TSR_RQCP1) != 0U)
- {
- /* Clear the Transmission Complete flag (and TXOK1,ALST1,TERR1 bits) */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP1);
- if ((tsrflags & CAN_TSR_TXOK1) != 0U)
- {
- /* Transmission Mailbox 1 complete callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox1CompleteCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox1CompleteCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- else
- {
- if ((tsrflags & CAN_TSR_ALST1) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_ALST1;
- }
- else if ((tsrflags & CAN_TSR_TERR1) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_TERR1;
- }
- else
- {
- /* Transmission Mailbox 1 abort callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox1AbortCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox1AbortCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- }
- /* Transmit Mailbox 2 management *****************************************/
- if ((tsrflags & CAN_TSR_RQCP2) != 0U)
- {
- /* Clear the Transmission Complete flag (and TXOK2,ALST2,TERR2 bits) */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP2);
- if ((tsrflags & CAN_TSR_TXOK2) != 0U)
- {
- /* Transmission Mailbox 2 complete callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox2CompleteCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox2CompleteCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- else
- {
- if ((tsrflags & CAN_TSR_ALST2) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_ALST2;
- }
- else if ((tsrflags & CAN_TSR_TERR2) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_TERR2;
- }
- else
- {
- /* Transmission Mailbox 2 abort callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox2AbortCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox2AbortCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- }
- }
- /* Receive FIFO 0 overrun interrupt management *****************************/
- if ((interrupts & CAN_IT_RX_FIFO0_OVERRUN) != 0U)
- {
- if ((rf0rflags & CAN_RF0R_FOVR0) != 0U)
- {
- /* Set CAN error code to Rx Fifo 0 overrun error */
- errorcode |= HAL_CAN_ERROR_RX_FOV0;
- /* Clear FIFO0 Overrun Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
- }
- }
- /* Receive FIFO 0 full interrupt management ********************************/
- if ((interrupts & CAN_IT_RX_FIFO0_FULL) != 0U)
- {
- if ((rf0rflags & CAN_RF0R_FULL0) != 0U)
- {
- /* Clear FIFO 0 full Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0);
- /* Receive FIFO 0 full Callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->RxFifo0FullCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_RxFifo0FullCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /* Receive FIFO 0 message pending interrupt management *********************/
- if ((interrupts & CAN_IT_RX_FIFO0_MSG_PENDING) != 0U)
- {
- /* Check if message is still pending */
- if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) != 0U)
- {
- /* Receive FIFO 0 message pending Callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->RxFifo0MsgPendingCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_RxFifo0MsgPendingCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /* Receive FIFO 1 overrun interrupt management *****************************/
- if ((interrupts & CAN_IT_RX_FIFO1_OVERRUN) != 0U)
- {
- if ((rf1rflags & CAN_RF1R_FOVR1) != 0U)
- {
- /* Set CAN error code to Rx Fifo 1 overrun error */
- errorcode |= HAL_CAN_ERROR_RX_FOV1;
- /* Clear FIFO1 Overrun Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV1);
- }
- }
- /* Receive FIFO 1 full interrupt management ********************************/
- if ((interrupts & CAN_IT_RX_FIFO1_FULL) != 0U)
- {
- if ((rf1rflags & CAN_RF1R_FULL1) != 0U)
- {
- /* Clear FIFO 1 full Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF1);
- /* Receive FIFO 1 full Callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->RxFifo1FullCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_RxFifo1FullCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /* Receive FIFO 1 message pending interrupt management *********************/
- if ((interrupts & CAN_IT_RX_FIFO1_MSG_PENDING) != 0U)
- {
- /* Check if message is still pending */
- if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) != 0U)
- {
- /* Receive FIFO 1 message pending Callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->RxFifo1MsgPendingCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_RxFifo1MsgPendingCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /* Sleep interrupt management *********************************************/
- if ((interrupts & CAN_IT_SLEEP_ACK) != 0U)
- {
- if ((msrflags & CAN_MSR_SLAKI) != 0U)
- {
- /* Clear Sleep interrupt Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_SLAKI);
- /* Sleep Callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->SleepCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_SleepCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /* WakeUp interrupt management *********************************************/
- if ((interrupts & CAN_IT_WAKEUP) != 0U)
- {
- if ((msrflags & CAN_MSR_WKUI) != 0U)
- {
- /* Clear WakeUp Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_WKU);
- /* WakeUp Callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->WakeUpFromRxMsgCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_WakeUpFromRxMsgCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /* Error interrupts management *********************************************/
- if ((interrupts & CAN_IT_ERROR) != 0U)
- {
- if ((msrflags & CAN_MSR_ERRI) != 0U)
- {
- /* Check Error Warning Flag */
- if (((interrupts & CAN_IT_ERROR_WARNING) != 0U) &&
- ((esrflags & CAN_ESR_EWGF) != 0U))
- {
- /* Set CAN error code to Error Warning */
- errorcode |= HAL_CAN_ERROR_EWG;
- /* No need for clear of Error Warning Flag as read-only */
- }
- /* Check Error Passive Flag */
- if (((interrupts & CAN_IT_ERROR_PASSIVE) != 0U) &&
- ((esrflags & CAN_ESR_EPVF) != 0U))
- {
- /* Set CAN error code to Error Passive */
- errorcode |= HAL_CAN_ERROR_EPV;
- /* No need for clear of Error Passive Flag as read-only */
- }
- /* Check Bus-off Flag */
- if (((interrupts & CAN_IT_BUSOFF) != 0U) &&
- ((esrflags & CAN_ESR_BOFF) != 0U))
- {
- /* Set CAN error code to Bus-Off */
- errorcode |= HAL_CAN_ERROR_BOF;
- /* No need for clear of Error Bus-Off as read-only */
- }
- /* Check Last Error Code Flag */
- if (((interrupts & CAN_IT_LAST_ERROR_CODE) != 0U) &&
- ((esrflags & CAN_ESR_LEC) != 0U))
- {
- switch (esrflags & CAN_ESR_LEC)
- {
- case (CAN_ESR_LEC_0):
- /* Set CAN error code to Stuff error */
- errorcode |= HAL_CAN_ERROR_STF;
- break;
- case (CAN_ESR_LEC_1):
- /* Set CAN error code to Form error */
- errorcode |= HAL_CAN_ERROR_FOR;
- break;
- case (CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
- /* Set CAN error code to Acknowledgement error */
- errorcode |= HAL_CAN_ERROR_ACK;
- break;
- case (CAN_ESR_LEC_2):
- /* Set CAN error code to Bit recessive error */
- errorcode |= HAL_CAN_ERROR_BR;
- break;
- case (CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
- /* Set CAN error code to Bit Dominant error */
- errorcode |= HAL_CAN_ERROR_BD;
- break;
- case (CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
- /* Set CAN error code to CRC error */
- errorcode |= HAL_CAN_ERROR_CRC;
- break;
- default:
- break;
- }
- /* Clear Last error code Flag */
- CLEAR_BIT(hcan->Instance->ESR, CAN_ESR_LEC);
- }
- }
- /* Clear ERRI Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_ERRI);
- }
- /* Call the Error call Back in case of Errors */
- if (errorcode != HAL_CAN_ERROR_NONE)
- {
- /* Update error code in handle */
- hcan->ErrorCode |= errorcode;
- /* Call Error callback function */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->ErrorCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_ErrorCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /**
- * @}
- */
- /** @defgroup CAN_Exported_Functions_Group5 Callback functions
- * @brief CAN Callback functions
- *
- @verbatim
- ==============================================================================
- ##### Callback functions #####
- ==============================================================================
- [..]
- This subsection provides the following callback functions:
- (+) HAL_CAN_TxMailbox0CompleteCallback
- (+) HAL_CAN_TxMailbox1CompleteCallback
- (+) HAL_CAN_TxMailbox2CompleteCallback
- (+) HAL_CAN_TxMailbox0AbortCallback
- (+) HAL_CAN_TxMailbox1AbortCallback
- (+) HAL_CAN_TxMailbox2AbortCallback
- (+) HAL_CAN_RxFifo0MsgPendingCallback
- (+) HAL_CAN_RxFifo0FullCallback
- (+) HAL_CAN_RxFifo1MsgPendingCallback
- (+) HAL_CAN_RxFifo1FullCallback
- (+) HAL_CAN_SleepCallback
- (+) HAL_CAN_WakeUpFromRxMsgCallback
- (+) HAL_CAN_ErrorCallback
- @endverbatim
- * @{
- */
- /**
- * @brief Transmission Mailbox 0 complete callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_TxMailbox0CompleteCallback could be implemented in the
- user file
- */
- }
- /**
- * @brief Transmission Mailbox 1 complete callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_TxMailbox1CompleteCallback could be implemented in the
- user file
- */
- }
- /**
- * @brief Transmission Mailbox 2 complete callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_TxMailbox2CompleteCallback could be implemented in the
- user file
- */
- }
- /**
- * @brief Transmission Mailbox 0 Cancellation callback.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_TxMailbox0AbortCallback could be implemented in the
- user file
- */
- }
- /**
- * @brief Transmission Mailbox 1 Cancellation callback.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_TxMailbox1AbortCallback could be implemented in the
- user file
- */
- }
- /**
- * @brief Transmission Mailbox 2 Cancellation callback.
- * @param hcan pointer to an CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_TxMailbox2AbortCallback could be implemented in the
- user file
- */
- }
- /**
- * @brief Rx FIFO 0 message pending callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_RxFifo0MsgPendingCallback could be implemented in the
- user file
- */
- }
- /**
- * @brief Rx FIFO 0 full callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_RxFifo0FullCallback could be implemented in the user
- file
- */
- }
- /**
- * @brief Rx FIFO 1 message pending callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_RxFifo1MsgPendingCallback could be implemented in the
- user file
- */
- }
- /**
- * @brief Rx FIFO 1 full callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_RxFifo1FullCallback could be implemented in the user
- file
- */
- }
- /**
- * @brief Sleep callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_SleepCallback could be implemented in the user file
- */
- }
- /**
- * @brief WakeUp from Rx message callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_WakeUpFromRxMsgCallback could be implemented in the
- user file
- */
- }
- /**
- * @brief Error CAN callback.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval None
- */
- __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hcan);
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_CAN_ErrorCallback could be implemented in the user file
- */
- }
- /**
- * @}
- */
- /** @defgroup CAN_Exported_Functions_Group6 Peripheral State and Error functions
- * @brief CAN Peripheral State functions
- *
- @verbatim
- ==============================================================================
- ##### Peripheral State and Error functions #####
- ==============================================================================
- [..]
- This subsection provides functions allowing to :
- (+) HAL_CAN_GetState() : Return the CAN state.
- (+) HAL_CAN_GetError() : Return the CAN error codes if any.
- (+) HAL_CAN_ResetError(): Reset the CAN error codes if any.
- @endverbatim
- * @{
- */
- /**
- * @brief Return the CAN state.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval HAL state
- */
- HAL_CAN_StateTypeDef HAL_CAN_GetState(const CAN_HandleTypeDef *hcan)
- {
- HAL_CAN_StateTypeDef state = hcan->State;
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Check sleep mode acknowledge flag */
- if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
- {
- /* Sleep mode is active */
- state = HAL_CAN_STATE_SLEEP_ACTIVE;
- }
- /* Check sleep mode request flag */
- else if ((hcan->Instance->MCR & CAN_MCR_SLEEP) != 0U)
- {
- /* Sleep mode request is pending */
- state = HAL_CAN_STATE_SLEEP_PENDING;
- }
- else
- {
- /* Neither sleep mode request nor sleep mode acknowledge */
- }
- }
- /* Return CAN state */
- return state;
- }
- /**
- * @brief Return the CAN error code.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval CAN Error Code
- */
- uint32_t HAL_CAN_GetError(const CAN_HandleTypeDef *hcan)
- {
- /* Return CAN error code */
- return hcan->ErrorCode;
- }
- /**
- * @brief Reset the CAN error code.
- * @param hcan pointer to a CAN_HandleTypeDef structure that contains
- * the configuration information for the specified CAN.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_CAN_ResetError(CAN_HandleTypeDef *hcan)
- {
- HAL_StatusTypeDef status = HAL_OK;
- HAL_CAN_StateTypeDef state = hcan->State;
- if ((state == HAL_CAN_STATE_READY) ||
- (state == HAL_CAN_STATE_LISTENING))
- {
- /* Reset CAN error code */
- hcan->ErrorCode = 0U;
- }
- else
- {
- /* Update error code */
- hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
- status = HAL_ERROR;
- }
- /* Return the status */
- return status;
- }
- /**
- * @}
- */
- /**
- * @}
- */
- #endif /* HAL_CAN_MODULE_ENABLED */
- /**
- * @}
- */
- #endif /* CAN1 */
- /**
- * @}
- */
|