stm32g0xx_hal_fdcan.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_hal_fdcan.c
  4. * @author MCD Application Team
  5. * @brief FDCAN HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Flexible DataRate Controller Area Network
  8. * (FDCAN) peripheral:
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral Configuration and Control functions
  12. * + Peripheral State and Error functions
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2018 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. (#) Initialize the FDCAN peripheral using HAL_FDCAN_Init function.
  30. (#) If needed , configure the reception filters and optional features using
  31. the following configuration functions:
  32. (++) HAL_FDCAN_ConfigFilter
  33. (++) HAL_FDCAN_ConfigGlobalFilter
  34. (++) HAL_FDCAN_ConfigExtendedIdMask
  35. (++) HAL_FDCAN_ConfigRxFifoOverwrite
  36. (++) HAL_FDCAN_ConfigRamWatchdog
  37. (++) HAL_FDCAN_ConfigTimestampCounter
  38. (++) HAL_FDCAN_EnableTimestampCounter
  39. (++) HAL_FDCAN_DisableTimestampCounter
  40. (++) HAL_FDCAN_ConfigTimeoutCounter
  41. (++) HAL_FDCAN_EnableTimeoutCounter
  42. (++) HAL_FDCAN_DisableTimeoutCounter
  43. (++) HAL_FDCAN_ConfigTxDelayCompensation
  44. (++) HAL_FDCAN_EnableTxDelayCompensation
  45. (++) HAL_FDCAN_DisableTxDelayCompensation
  46. (++) HAL_FDCAN_EnableISOMode
  47. (++) HAL_FDCAN_DisableISOMode
  48. (++) HAL_FDCAN_EnableEdgeFiltering
  49. (++) HAL_FDCAN_DisableEdgeFiltering
  50. (#) Start the FDCAN module using HAL_FDCAN_Start function. At this level
  51. the node is active on the bus: it can send and receive messages.
  52. (#) The following Tx control functions can only be called when the FDCAN
  53. module is started:
  54. (++) HAL_FDCAN_AddMessageToTxFifoQ
  55. (++) HAL_FDCAN_AbortTxRequest
  56. (#) After having submitted a Tx request in Tx Fifo or Queue, it is possible to
  57. get Tx buffer location used to place the Tx request thanks to
  58. HAL_FDCAN_GetLatestTxFifoQRequestBuffer API.
  59. It is then possible to abort later on the corresponding Tx Request using
  60. HAL_FDCAN_AbortTxRequest API.
  61. (#) When a message is received into the FDCAN message RAM, it can be
  62. retrieved using the HAL_FDCAN_GetRxMessage function.
  63. (#) Calling the HAL_FDCAN_Stop function stops the FDCAN module by entering
  64. it to initialization mode and re-enabling access to configuration
  65. registers through the configuration functions listed here above.
  66. (#) All other control functions can be called any time after initialization
  67. phase, no matter if the FDCAN module is started or stopped.
  68. *** Polling mode operation ***
  69. ==============================
  70. [..]
  71. (#) Reception and transmission states can be monitored via the following
  72. functions:
  73. (++) HAL_FDCAN_IsTxBufferMessagePending
  74. (++) HAL_FDCAN_GetRxFifoFillLevel
  75. (++) HAL_FDCAN_GetTxFifoFreeLevel
  76. *** Interrupt mode operation ***
  77. ================================
  78. [..]
  79. (#) There are two interrupt lines: line 0 and 1.
  80. By default, all interrupts are assigned to line 0. Interrupt lines
  81. can be configured using HAL_FDCAN_ConfigInterruptLines function.
  82. (#) Notifications are activated using HAL_FDCAN_ActivateNotification
  83. function. Then, the process can be controlled through one of the
  84. available user callbacks: HAL_FDCAN_xxxCallback.
  85. *** Callback registration ***
  86. =============================================
  87. The compilation define USE_HAL_FDCAN_REGISTER_CALLBACKS when set to 1
  88. allows the user to configure dynamically the driver callbacks.
  89. Use Function HAL_FDCAN_RegisterCallback() or HAL_FDCAN_RegisterXXXCallback()
  90. to register an interrupt callback.
  91. Function HAL_FDCAN_RegisterCallback() allows to register following callbacks:
  92. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  93. (+) HighPriorityMessageCallback : High Priority Message Callback.
  94. (+) TimestampWraparoundCallback : Timestamp Wraparound Callback.
  95. (+) TimeoutOccurredCallback : Timeout Occurred Callback.
  96. (+) ErrorCallback : Error Callback.
  97. (+) MspInitCallback : FDCAN MspInit.
  98. (+) MspDeInitCallback : FDCAN MspDeInit.
  99. This function takes as parameters the HAL peripheral handle, the Callback ID
  100. and a pointer to the user callback function.
  101. For specific callbacks TxEventFifoCallback, RxFifo0Callback, RxFifo1Callback,
  102. TxBufferCompleteCallback, TxBufferAbortCallback and ErrorStatusCallback use dedicated
  103. register callbacks: respectively HAL_FDCAN_RegisterTxEventFifoCallback(),
  104. HAL_FDCAN_RegisterRxFifo0Callback(), HAL_FDCAN_RegisterRxFifo1Callback(),
  105. HAL_FDCAN_RegisterTxBufferCompleteCallback(), HAL_FDCAN_RegisterTxBufferAbortCallback()
  106. and HAL_FDCAN_RegisterErrorStatusCallback().
  107. Use function HAL_FDCAN_UnRegisterCallback() to reset a callback to the default
  108. weak function.
  109. HAL_FDCAN_UnRegisterCallback takes as parameters the HAL peripheral handle,
  110. and the Callback ID.
  111. This function allows to reset following callbacks:
  112. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  113. (+) HighPriorityMessageCallback : High Priority Message Callback.
  114. (+) TimestampWraparoundCallback : Timestamp Wraparound Callback.
  115. (+) TimeoutOccurredCallback : Timeout Occurred Callback.
  116. (+) ErrorCallback : Error Callback.
  117. (+) MspInitCallback : FDCAN MspInit.
  118. (+) MspDeInitCallback : FDCAN MspDeInit.
  119. For specific callbacks TxEventFifoCallback, RxFifo0Callback, RxFifo1Callback,
  120. TxBufferCompleteCallback and TxBufferAbortCallback, use dedicated
  121. unregister callbacks: respectively HAL_FDCAN_UnRegisterTxEventFifoCallback(),
  122. HAL_FDCAN_UnRegisterRxFifo0Callback(), HAL_FDCAN_UnRegisterRxFifo1Callback(),
  123. HAL_FDCAN_UnRegisterTxBufferCompleteCallback(), HAL_FDCAN_UnRegisterTxBufferAbortCallback()
  124. and HAL_FDCAN_UnRegisterErrorStatusCallback().
  125. By default, after the HAL_FDCAN_Init() and when the state is HAL_FDCAN_STATE_RESET,
  126. all callbacks are set to the corresponding weak functions:
  127. examples HAL_FDCAN_ErrorCallback().
  128. Exception done for MspInit and MspDeInit functions that are
  129. reset to the legacy weak function in the HAL_FDCAN_Init()/ HAL_FDCAN_DeInit() only when
  130. these callbacks are null (not registered beforehand).
  131. if not, MspInit or MspDeInit are not null, the HAL_FDCAN_Init()/ HAL_FDCAN_DeInit()
  132. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  133. Callbacks can be registered/unregistered in HAL_FDCAN_STATE_READY state only.
  134. Exception done MspInit/MspDeInit that can be registered/unregistered
  135. in HAL_FDCAN_STATE_READY or HAL_FDCAN_STATE_RESET state,
  136. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  137. In that case first register the MspInit/MspDeInit user callbacks
  138. using HAL_FDCAN_RegisterCallback() before calling HAL_FDCAN_DeInit()
  139. or HAL_FDCAN_Init() function.
  140. When The compilation define USE_HAL_FDCAN_REGISTER_CALLBACKS is set to 0 or
  141. not defined, the callback registration feature is not available and all callbacks
  142. are set to the corresponding weak functions.
  143. @endverbatim
  144. ******************************************************************************
  145. */
  146. /* Includes ------------------------------------------------------------------*/
  147. #include "stm32g0xx_hal.h"
  148. #if defined(FDCAN1)
  149. /** @addtogroup STM32G0xx_HAL_Driver
  150. * @{
  151. */
  152. /** @defgroup FDCAN FDCAN
  153. * @brief FDCAN HAL module driver
  154. * @{
  155. */
  156. #ifdef HAL_FDCAN_MODULE_ENABLED
  157. /* Private typedef -----------------------------------------------------------*/
  158. /* Private define ------------------------------------------------------------*/
  159. /** @addtogroup FDCAN_Private_Constants
  160. * @{
  161. */
  162. #define FDCAN_TIMEOUT_VALUE 10U
  163. #define FDCAN_TX_EVENT_FIFO_MASK (FDCAN_IR_TEFL | FDCAN_IR_TEFF | FDCAN_IR_TEFN)
  164. #define FDCAN_RX_FIFO0_MASK (FDCAN_IR_RF0L | FDCAN_IR_RF0F | FDCAN_IR_RF0N)
  165. #define FDCAN_RX_FIFO1_MASK (FDCAN_IR_RF1L | FDCAN_IR_RF1F | FDCAN_IR_RF1N)
  166. #define FDCAN_ERROR_MASK (FDCAN_IR_ELO | FDCAN_IR_WDI | FDCAN_IR_PEA | FDCAN_IR_PED | FDCAN_IR_ARA)
  167. #define FDCAN_ERROR_STATUS_MASK (FDCAN_IR_EP | FDCAN_IR_EW | FDCAN_IR_BO)
  168. #define FDCAN_ELEMENT_MASK_STDID ((uint32_t)0x1FFC0000U) /* Standard Identifier */
  169. #define FDCAN_ELEMENT_MASK_EXTID ((uint32_t)0x1FFFFFFFU) /* Extended Identifier */
  170. #define FDCAN_ELEMENT_MASK_RTR ((uint32_t)0x20000000U) /* Remote Transmission Request */
  171. #define FDCAN_ELEMENT_MASK_XTD ((uint32_t)0x40000000U) /* Extended Identifier */
  172. #define FDCAN_ELEMENT_MASK_ESI ((uint32_t)0x80000000U) /* Error State Indicator */
  173. #define FDCAN_ELEMENT_MASK_TS ((uint32_t)0x0000FFFFU) /* Timestamp */
  174. #define FDCAN_ELEMENT_MASK_DLC ((uint32_t)0x000F0000U) /* Data Length Code */
  175. #define FDCAN_ELEMENT_MASK_BRS ((uint32_t)0x00100000U) /* Bit Rate Switch */
  176. #define FDCAN_ELEMENT_MASK_FDF ((uint32_t)0x00200000U) /* FD Format */
  177. #define FDCAN_ELEMENT_MASK_EFC ((uint32_t)0x00800000U) /* Event FIFO Control */
  178. #define FDCAN_ELEMENT_MASK_MM ((uint32_t)0xFF000000U) /* Message Marker */
  179. #define FDCAN_ELEMENT_MASK_FIDX ((uint32_t)0x7F000000U) /* Filter Index */
  180. #define FDCAN_ELEMENT_MASK_ANMF ((uint32_t)0x80000000U) /* Accepted Non-matching Frame */
  181. #define FDCAN_ELEMENT_MASK_ET ((uint32_t)0x00C00000U) /* Event type */
  182. #define SRAMCAN_FLS_NBR (28U) /* Max. Filter List Standard Number */
  183. #define SRAMCAN_FLE_NBR ( 8U) /* Max. Filter List Extended Number */
  184. #define SRAMCAN_RF0_NBR ( 3U) /* RX FIFO 0 Elements Number */
  185. #define SRAMCAN_RF1_NBR ( 3U) /* RX FIFO 1 Elements Number */
  186. #define SRAMCAN_TEF_NBR ( 3U) /* TX Event FIFO Elements Number */
  187. #define SRAMCAN_TFQ_NBR ( 3U) /* TX FIFO/Queue Elements Number */
  188. #define SRAMCAN_FLS_SIZE ( 1U * 4U) /* Filter Standard Element Size in bytes */
  189. #define SRAMCAN_FLE_SIZE ( 2U * 4U) /* Filter Extended Element Size in bytes */
  190. #define SRAMCAN_RF0_SIZE (18U * 4U) /* RX FIFO 0 Elements Size in bytes */
  191. #define SRAMCAN_RF1_SIZE (18U * 4U) /* RX FIFO 1 Elements Size in bytes */
  192. #define SRAMCAN_TEF_SIZE ( 2U * 4U) /* TX Event FIFO Elements Size in bytes */
  193. #define SRAMCAN_TFQ_SIZE (18U * 4U) /* TX FIFO/Queue Elements Size in bytes */
  194. #define SRAMCAN_FLSSA ((uint32_t)0) /* Filter List Standard Start
  195. Address */
  196. #define SRAMCAN_FLESA ((uint32_t)(SRAMCAN_FLSSA + (SRAMCAN_FLS_NBR * SRAMCAN_FLS_SIZE))) /* Filter List Extended Start
  197. Address */
  198. #define SRAMCAN_RF0SA ((uint32_t)(SRAMCAN_FLESA + (SRAMCAN_FLE_NBR * SRAMCAN_FLE_SIZE))) /* Rx FIFO 0 Start Address */
  199. #define SRAMCAN_RF1SA ((uint32_t)(SRAMCAN_RF0SA + (SRAMCAN_RF0_NBR * SRAMCAN_RF0_SIZE))) /* Rx FIFO 1 Start Address */
  200. #define SRAMCAN_TEFSA ((uint32_t)(SRAMCAN_RF1SA + (SRAMCAN_RF1_NBR * SRAMCAN_RF1_SIZE))) /* Tx Event FIFO Start
  201. Address */
  202. #define SRAMCAN_TFQSA ((uint32_t)(SRAMCAN_TEFSA + (SRAMCAN_TEF_NBR * SRAMCAN_TEF_SIZE))) /* Tx FIFO/Queue Start
  203. Address */
  204. #define SRAMCAN_SIZE ((uint32_t)(SRAMCAN_TFQSA + (SRAMCAN_TFQ_NBR * SRAMCAN_TFQ_SIZE))) /* Message RAM size */
  205. /**
  206. * @}
  207. */
  208. /* Private macro -------------------------------------------------------------*/
  209. /* Private variables ---------------------------------------------------------*/
  210. /** @addtogroup FDCAN_Private_Variables
  211. * @{
  212. */
  213. static const uint8_t DLCtoBytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};
  214. /**
  215. * @}
  216. */
  217. /* Private function prototypes -----------------------------------------------*/
  218. /** @addtogroup FDCAN_Private_Functions_Prototypes
  219. * @{
  220. */
  221. static void FDCAN_CalcultateRamBlockAddresses(FDCAN_HandleTypeDef *hfdcan);
  222. static void FDCAN_CopyMessageToRAM(const FDCAN_HandleTypeDef *hfdcan, const FDCAN_TxHeaderTypeDef *pTxHeader,
  223. const uint8_t *pTxData, uint32_t BufferIndex);
  224. /**
  225. * @}
  226. */
  227. /* Exported functions --------------------------------------------------------*/
  228. /** @defgroup FDCAN_Exported_Functions FDCAN Exported Functions
  229. * @{
  230. */
  231. /** @defgroup FDCAN_Exported_Functions_Group1 Initialization and de-initialization functions
  232. * @brief Initialization and Configuration functions
  233. *
  234. @verbatim
  235. ==============================================================================
  236. ##### Initialization and de-initialization functions #####
  237. ==============================================================================
  238. [..] This section provides functions allowing to:
  239. (+) Initialize and configure the FDCAN.
  240. (+) De-initialize the FDCAN.
  241. (+) Enter FDCAN peripheral in power down mode.
  242. (+) Exit power down mode.
  243. (+) Register callbacks.
  244. (+) Unregister callbacks.
  245. @endverbatim
  246. * @{
  247. */
  248. /**
  249. * @brief Initializes the FDCAN peripheral according to the specified
  250. * parameters in the FDCAN_InitTypeDef structure.
  251. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  252. * the configuration information for the specified FDCAN.
  253. * @retval HAL status
  254. */
  255. HAL_StatusTypeDef HAL_FDCAN_Init(FDCAN_HandleTypeDef *hfdcan)
  256. {
  257. uint32_t tickstart;
  258. /* Check FDCAN handle */
  259. if (hfdcan == NULL)
  260. {
  261. return HAL_ERROR;
  262. }
  263. /* Check function parameters */
  264. assert_param(IS_FDCAN_ALL_INSTANCE(hfdcan->Instance));
  265. if (hfdcan->Instance == FDCAN1)
  266. {
  267. assert_param(IS_FDCAN_CKDIV(hfdcan->Init.ClockDivider));
  268. }
  269. assert_param(IS_FDCAN_FRAME_FORMAT(hfdcan->Init.FrameFormat));
  270. assert_param(IS_FDCAN_MODE(hfdcan->Init.Mode));
  271. assert_param(IS_FUNCTIONAL_STATE(hfdcan->Init.AutoRetransmission));
  272. assert_param(IS_FUNCTIONAL_STATE(hfdcan->Init.TransmitPause));
  273. assert_param(IS_FUNCTIONAL_STATE(hfdcan->Init.ProtocolException));
  274. assert_param(IS_FDCAN_NOMINAL_PRESCALER(hfdcan->Init.NominalPrescaler));
  275. assert_param(IS_FDCAN_NOMINAL_SJW(hfdcan->Init.NominalSyncJumpWidth));
  276. assert_param(IS_FDCAN_NOMINAL_TSEG1(hfdcan->Init.NominalTimeSeg1));
  277. assert_param(IS_FDCAN_NOMINAL_TSEG2(hfdcan->Init.NominalTimeSeg2));
  278. if (hfdcan->Init.FrameFormat == FDCAN_FRAME_FD_BRS)
  279. {
  280. assert_param(IS_FDCAN_DATA_PRESCALER(hfdcan->Init.DataPrescaler));
  281. assert_param(IS_FDCAN_DATA_SJW(hfdcan->Init.DataSyncJumpWidth));
  282. assert_param(IS_FDCAN_DATA_TSEG1(hfdcan->Init.DataTimeSeg1));
  283. assert_param(IS_FDCAN_DATA_TSEG2(hfdcan->Init.DataTimeSeg2));
  284. }
  285. assert_param(IS_FDCAN_MAX_VALUE(hfdcan->Init.StdFiltersNbr, SRAMCAN_FLS_NBR));
  286. assert_param(IS_FDCAN_MAX_VALUE(hfdcan->Init.ExtFiltersNbr, SRAMCAN_FLE_NBR));
  287. assert_param(IS_FDCAN_TX_FIFO_QUEUE_MODE(hfdcan->Init.TxFifoQueueMode));
  288. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  289. if (hfdcan->State == HAL_FDCAN_STATE_RESET)
  290. {
  291. /* Allocate lock resource and initialize it */
  292. hfdcan->Lock = HAL_UNLOCKED;
  293. /* Reset callbacks to legacy functions */
  294. hfdcan->TxEventFifoCallback = HAL_FDCAN_TxEventFifoCallback; /* TxEventFifoCallback */
  295. hfdcan->RxFifo0Callback = HAL_FDCAN_RxFifo0Callback; /* RxFifo0Callback */
  296. hfdcan->RxFifo1Callback = HAL_FDCAN_RxFifo1Callback; /* RxFifo1Callback */
  297. hfdcan->TxFifoEmptyCallback = HAL_FDCAN_TxFifoEmptyCallback; /* TxFifoEmptyCallback */
  298. hfdcan->TxBufferCompleteCallback = HAL_FDCAN_TxBufferCompleteCallback; /* TxBufferCompleteCallback */
  299. hfdcan->TxBufferAbortCallback = HAL_FDCAN_TxBufferAbortCallback; /* TxBufferAbortCallback */
  300. hfdcan->HighPriorityMessageCallback = HAL_FDCAN_HighPriorityMessageCallback; /* HighPriorityMessageCallback */
  301. hfdcan->TimestampWraparoundCallback = HAL_FDCAN_TimestampWraparoundCallback; /* TimestampWraparoundCallback */
  302. hfdcan->TimeoutOccurredCallback = HAL_FDCAN_TimeoutOccurredCallback; /* TimeoutOccurredCallback */
  303. hfdcan->ErrorCallback = HAL_FDCAN_ErrorCallback; /* ErrorCallback */
  304. hfdcan->ErrorStatusCallback = HAL_FDCAN_ErrorStatusCallback; /* ErrorStatusCallback */
  305. if (hfdcan->MspInitCallback == NULL)
  306. {
  307. hfdcan->MspInitCallback = HAL_FDCAN_MspInit; /* Legacy weak MspInit */
  308. }
  309. /* Init the low level hardware: CLOCK, NVIC */
  310. hfdcan->MspInitCallback(hfdcan);
  311. }
  312. #else
  313. if (hfdcan->State == HAL_FDCAN_STATE_RESET)
  314. {
  315. /* Allocate lock resource and initialize it */
  316. hfdcan->Lock = HAL_UNLOCKED;
  317. /* Init the low level hardware: CLOCK, NVIC */
  318. HAL_FDCAN_MspInit(hfdcan);
  319. }
  320. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  321. /* Exit from Sleep mode */
  322. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CSR);
  323. /* Get tick */
  324. tickstart = HAL_GetTick();
  325. /* Check Sleep mode acknowledge */
  326. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_CSA) == FDCAN_CCCR_CSA)
  327. {
  328. if ((HAL_GetTick() - tickstart) > FDCAN_TIMEOUT_VALUE)
  329. {
  330. /* Update error code */
  331. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  332. /* Change FDCAN state */
  333. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  334. return HAL_ERROR;
  335. }
  336. }
  337. /* Request initialisation */
  338. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_INIT);
  339. /* Get tick */
  340. tickstart = HAL_GetTick();
  341. /* Wait until the INIT bit into CCCR register is set */
  342. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_INIT) == 0U)
  343. {
  344. /* Check for the Timeout */
  345. if ((HAL_GetTick() - tickstart) > FDCAN_TIMEOUT_VALUE)
  346. {
  347. /* Update error code */
  348. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  349. /* Change FDCAN state */
  350. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  351. return HAL_ERROR;
  352. }
  353. }
  354. /* Enable configuration change */
  355. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CCE);
  356. /* Check FDCAN instance */
  357. if (hfdcan->Instance == FDCAN1)
  358. {
  359. /* Configure Clock divider */
  360. FDCAN_CONFIG->CKDIV = hfdcan->Init.ClockDivider;
  361. }
  362. /* Set the no automatic retransmission */
  363. if (hfdcan->Init.AutoRetransmission == ENABLE)
  364. {
  365. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_DAR);
  366. }
  367. else
  368. {
  369. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_DAR);
  370. }
  371. /* Set the transmit pause feature */
  372. if (hfdcan->Init.TransmitPause == ENABLE)
  373. {
  374. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_TXP);
  375. }
  376. else
  377. {
  378. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_TXP);
  379. }
  380. /* Set the Protocol Exception Handling */
  381. if (hfdcan->Init.ProtocolException == ENABLE)
  382. {
  383. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_PXHD);
  384. }
  385. else
  386. {
  387. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_PXHD);
  388. }
  389. /* Set FDCAN Frame Format */
  390. MODIFY_REG(hfdcan->Instance->CCCR, FDCAN_FRAME_FD_BRS, hfdcan->Init.FrameFormat);
  391. /* Reset FDCAN Operation Mode */
  392. CLEAR_BIT(hfdcan->Instance->CCCR, (FDCAN_CCCR_TEST | FDCAN_CCCR_MON | FDCAN_CCCR_ASM));
  393. CLEAR_BIT(hfdcan->Instance->TEST, FDCAN_TEST_LBCK);
  394. /* Set FDCAN Operating Mode:
  395. | Normal | Restricted | Bus | Internal | External
  396. | | Operation | Monitoring | LoopBack | LoopBack
  397. CCCR.TEST | 0 | 0 | 0 | 1 | 1
  398. CCCR.MON | 0 | 0 | 1 | 1 | 0
  399. TEST.LBCK | 0 | 0 | 0 | 1 | 1
  400. CCCR.ASM | 0 | 1 | 0 | 0 | 0
  401. */
  402. if (hfdcan->Init.Mode == FDCAN_MODE_RESTRICTED_OPERATION)
  403. {
  404. /* Enable Restricted Operation mode */
  405. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_ASM);
  406. }
  407. else if (hfdcan->Init.Mode != FDCAN_MODE_NORMAL)
  408. {
  409. if (hfdcan->Init.Mode != FDCAN_MODE_BUS_MONITORING)
  410. {
  411. /* Enable write access to TEST register */
  412. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_TEST);
  413. /* Enable LoopBack mode */
  414. SET_BIT(hfdcan->Instance->TEST, FDCAN_TEST_LBCK);
  415. if (hfdcan->Init.Mode == FDCAN_MODE_INTERNAL_LOOPBACK)
  416. {
  417. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_MON);
  418. }
  419. }
  420. else
  421. {
  422. /* Enable bus monitoring mode */
  423. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_MON);
  424. }
  425. }
  426. else
  427. {
  428. /* Nothing to do: normal mode */
  429. }
  430. /* Set the nominal bit timing register */
  431. hfdcan->Instance->NBTP = ((((uint32_t)hfdcan->Init.NominalSyncJumpWidth - 1U) << FDCAN_NBTP_NSJW_Pos) | \
  432. (((uint32_t)hfdcan->Init.NominalTimeSeg1 - 1U) << FDCAN_NBTP_NTSEG1_Pos) | \
  433. (((uint32_t)hfdcan->Init.NominalTimeSeg2 - 1U) << FDCAN_NBTP_NTSEG2_Pos) | \
  434. (((uint32_t)hfdcan->Init.NominalPrescaler - 1U) << FDCAN_NBTP_NBRP_Pos));
  435. /* If FD operation with BRS is selected, set the data bit timing register */
  436. if (hfdcan->Init.FrameFormat == FDCAN_FRAME_FD_BRS)
  437. {
  438. hfdcan->Instance->DBTP = ((((uint32_t)hfdcan->Init.DataSyncJumpWidth - 1U) << FDCAN_DBTP_DSJW_Pos) | \
  439. (((uint32_t)hfdcan->Init.DataTimeSeg1 - 1U) << FDCAN_DBTP_DTSEG1_Pos) | \
  440. (((uint32_t)hfdcan->Init.DataTimeSeg2 - 1U) << FDCAN_DBTP_DTSEG2_Pos) | \
  441. (((uint32_t)hfdcan->Init.DataPrescaler - 1U) << FDCAN_DBTP_DBRP_Pos));
  442. }
  443. /* Select between Tx FIFO and Tx Queue operation modes */
  444. SET_BIT(hfdcan->Instance->TXBC, hfdcan->Init.TxFifoQueueMode);
  445. /* Calculate each RAM block address */
  446. FDCAN_CalcultateRamBlockAddresses(hfdcan);
  447. /* Initialize the Latest Tx request buffer index */
  448. hfdcan->LatestTxFifoQRequest = 0U;
  449. /* Initialize the error code */
  450. hfdcan->ErrorCode = HAL_FDCAN_ERROR_NONE;
  451. /* Initialize the FDCAN state */
  452. hfdcan->State = HAL_FDCAN_STATE_READY;
  453. /* Return function status */
  454. return HAL_OK;
  455. }
  456. /**
  457. * @brief Deinitializes the FDCAN peripheral registers to their default reset values.
  458. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  459. * the configuration information for the specified FDCAN.
  460. * @retval HAL status
  461. */
  462. HAL_StatusTypeDef HAL_FDCAN_DeInit(FDCAN_HandleTypeDef *hfdcan)
  463. {
  464. /* Check FDCAN handle */
  465. if (hfdcan == NULL)
  466. {
  467. return HAL_ERROR;
  468. }
  469. /* Check function parameters */
  470. assert_param(IS_FDCAN_ALL_INSTANCE(hfdcan->Instance));
  471. /* Stop the FDCAN module: return value is voluntary ignored */
  472. (void)HAL_FDCAN_Stop(hfdcan);
  473. /* Disable Interrupt lines */
  474. CLEAR_BIT(hfdcan->Instance->ILE, (FDCAN_INTERRUPT_LINE0 | FDCAN_INTERRUPT_LINE1));
  475. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  476. if (hfdcan->MspDeInitCallback == NULL)
  477. {
  478. hfdcan->MspDeInitCallback = HAL_FDCAN_MspDeInit; /* Legacy weak MspDeInit */
  479. }
  480. /* DeInit the low level hardware: CLOCK, NVIC */
  481. hfdcan->MspDeInitCallback(hfdcan);
  482. #else
  483. /* DeInit the low level hardware: CLOCK, NVIC */
  484. HAL_FDCAN_MspDeInit(hfdcan);
  485. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  486. /* Reset the FDCAN ErrorCode */
  487. hfdcan->ErrorCode = HAL_FDCAN_ERROR_NONE;
  488. /* Change FDCAN state */
  489. hfdcan->State = HAL_FDCAN_STATE_RESET;
  490. /* Return function status */
  491. return HAL_OK;
  492. }
  493. /**
  494. * @brief Initializes the FDCAN MSP.
  495. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  496. * the configuration information for the specified FDCAN.
  497. * @retval None
  498. */
  499. __weak void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef *hfdcan)
  500. {
  501. /* Prevent unused argument(s) compilation warning */
  502. UNUSED(hfdcan);
  503. /* NOTE: This function Should not be modified, when the callback is needed,
  504. the HAL_FDCAN_MspInit could be implemented in the user file
  505. */
  506. }
  507. /**
  508. * @brief DeInitializes the FDCAN MSP.
  509. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  510. * the configuration information for the specified FDCAN.
  511. * @retval None
  512. */
  513. __weak void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef *hfdcan)
  514. {
  515. /* Prevent unused argument(s) compilation warning */
  516. UNUSED(hfdcan);
  517. /* NOTE: This function Should not be modified, when the callback is needed,
  518. the HAL_FDCAN_MspDeInit could be implemented in the user file
  519. */
  520. }
  521. /**
  522. * @brief Enter FDCAN peripheral in sleep mode.
  523. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  524. * the configuration information for the specified FDCAN.
  525. * @retval HAL status
  526. */
  527. HAL_StatusTypeDef HAL_FDCAN_EnterPowerDownMode(FDCAN_HandleTypeDef *hfdcan)
  528. {
  529. uint32_t tickstart;
  530. /* Request clock stop */
  531. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CSR);
  532. /* Get tick */
  533. tickstart = HAL_GetTick();
  534. /* Wait until FDCAN is ready for power down */
  535. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_CSA) == 0U)
  536. {
  537. if ((HAL_GetTick() - tickstart) > FDCAN_TIMEOUT_VALUE)
  538. {
  539. /* Update error code */
  540. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  541. /* Change FDCAN state */
  542. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  543. return HAL_ERROR;
  544. }
  545. }
  546. /* Return function status */
  547. return HAL_OK;
  548. }
  549. /**
  550. * @brief Exit power down mode.
  551. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  552. * the configuration information for the specified FDCAN.
  553. * @retval HAL status
  554. */
  555. HAL_StatusTypeDef HAL_FDCAN_ExitPowerDownMode(FDCAN_HandleTypeDef *hfdcan)
  556. {
  557. uint32_t tickstart;
  558. /* Reset clock stop request */
  559. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CSR);
  560. /* Get tick */
  561. tickstart = HAL_GetTick();
  562. /* Wait until FDCAN exits sleep mode */
  563. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_CSA) == FDCAN_CCCR_CSA)
  564. {
  565. if ((HAL_GetTick() - tickstart) > FDCAN_TIMEOUT_VALUE)
  566. {
  567. /* Update error code */
  568. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  569. /* Change FDCAN state */
  570. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  571. return HAL_ERROR;
  572. }
  573. }
  574. /* Enter normal operation */
  575. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_INIT);
  576. /* Return function status */
  577. return HAL_OK;
  578. }
  579. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  580. /**
  581. * @brief Register a FDCAN CallBack.
  582. * To be used instead of the weak predefined callback
  583. * @param hfdcan pointer to a FDCAN_HandleTypeDef structure that contains
  584. * the configuration information for FDCAN module
  585. * @param CallbackID ID of the callback to be registered
  586. * This parameter can be one of the following values:
  587. * @arg @ref HAL_FDCAN_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty callback ID
  588. * @arg @ref HAL_FDCAN_HIGH_PRIO_MESSAGE_CB_ID High priority message callback ID
  589. * @arg @ref HAL_FDCAN_TIMESTAMP_WRAPAROUND_CB_ID Timestamp wraparound callback ID
  590. * @arg @ref HAL_FDCAN_TIMEOUT_OCCURRED_CB_ID Timeout occurred callback ID
  591. * @arg @ref HAL_FDCAN_ERROR_CALLBACK_CB_ID Error callback ID
  592. * @arg @ref HAL_FDCAN_MSPINIT_CB_ID MspInit callback ID
  593. * @arg @ref HAL_FDCAN_MSPDEINIT_CB_ID MspDeInit callback ID
  594. * @param pCallback pointer to the Callback function
  595. * @retval HAL status
  596. */
  597. HAL_StatusTypeDef HAL_FDCAN_RegisterCallback(FDCAN_HandleTypeDef *hfdcan, HAL_FDCAN_CallbackIDTypeDef CallbackID,
  598. void (* pCallback)(FDCAN_HandleTypeDef *_hFDCAN))
  599. {
  600. HAL_StatusTypeDef status = HAL_OK;
  601. if (pCallback == NULL)
  602. {
  603. /* Update the error code */
  604. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  605. return HAL_ERROR;
  606. }
  607. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  608. {
  609. switch (CallbackID)
  610. {
  611. case HAL_FDCAN_TX_FIFO_EMPTY_CB_ID :
  612. hfdcan->TxFifoEmptyCallback = pCallback;
  613. break;
  614. case HAL_FDCAN_HIGH_PRIO_MESSAGE_CB_ID :
  615. hfdcan->HighPriorityMessageCallback = pCallback;
  616. break;
  617. case HAL_FDCAN_TIMESTAMP_WRAPAROUND_CB_ID :
  618. hfdcan->TimestampWraparoundCallback = pCallback;
  619. break;
  620. case HAL_FDCAN_TIMEOUT_OCCURRED_CB_ID :
  621. hfdcan->TimeoutOccurredCallback = pCallback;
  622. break;
  623. case HAL_FDCAN_ERROR_CALLBACK_CB_ID :
  624. hfdcan->ErrorCallback = pCallback;
  625. break;
  626. case HAL_FDCAN_MSPINIT_CB_ID :
  627. hfdcan->MspInitCallback = pCallback;
  628. break;
  629. case HAL_FDCAN_MSPDEINIT_CB_ID :
  630. hfdcan->MspDeInitCallback = pCallback;
  631. break;
  632. default :
  633. /* Update the error code */
  634. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  635. /* Return error status */
  636. status = HAL_ERROR;
  637. break;
  638. }
  639. }
  640. else if (hfdcan->State == HAL_FDCAN_STATE_RESET)
  641. {
  642. switch (CallbackID)
  643. {
  644. case HAL_FDCAN_MSPINIT_CB_ID :
  645. hfdcan->MspInitCallback = pCallback;
  646. break;
  647. case HAL_FDCAN_MSPDEINIT_CB_ID :
  648. hfdcan->MspDeInitCallback = pCallback;
  649. break;
  650. default :
  651. /* Update the error code */
  652. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  653. /* Return error status */
  654. status = HAL_ERROR;
  655. break;
  656. }
  657. }
  658. else
  659. {
  660. /* Update the error code */
  661. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  662. /* Return error status */
  663. status = HAL_ERROR;
  664. }
  665. return status;
  666. }
  667. /**
  668. * @brief Unregister a FDCAN CallBack.
  669. * FDCAN callback is redirected to the weak predefined callback
  670. * @param hfdcan pointer to a FDCAN_HandleTypeDef structure that contains
  671. * the configuration information for FDCAN module
  672. * @param CallbackID ID of the callback to be unregistered
  673. * This parameter can be one of the following values:
  674. * @arg @ref HAL_FDCAN_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty callback ID
  675. * @arg @ref HAL_FDCAN_HIGH_PRIO_MESSAGE_CB_ID High priority message callback ID
  676. * @arg @ref HAL_FDCAN_TIMESTAMP_WRAPAROUND_CB_ID Timestamp wraparound callback ID
  677. * @arg @ref HAL_FDCAN_TIMEOUT_OCCURRED_CB_ID Timeout occurred callback ID
  678. * @arg @ref HAL_FDCAN_ERROR_CALLBACK_CB_ID Error callback ID
  679. * @arg @ref HAL_FDCAN_MSPINIT_CB_ID MspInit callback ID
  680. * @arg @ref HAL_FDCAN_MSPDEINIT_CB_ID MspDeInit callback ID
  681. * @retval HAL status
  682. */
  683. HAL_StatusTypeDef HAL_FDCAN_UnRegisterCallback(FDCAN_HandleTypeDef *hfdcan, HAL_FDCAN_CallbackIDTypeDef CallbackID)
  684. {
  685. HAL_StatusTypeDef status = HAL_OK;
  686. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  687. {
  688. switch (CallbackID)
  689. {
  690. case HAL_FDCAN_TX_FIFO_EMPTY_CB_ID :
  691. hfdcan->TxFifoEmptyCallback = HAL_FDCAN_TxFifoEmptyCallback;
  692. break;
  693. case HAL_FDCAN_HIGH_PRIO_MESSAGE_CB_ID :
  694. hfdcan->HighPriorityMessageCallback = HAL_FDCAN_HighPriorityMessageCallback;
  695. break;
  696. case HAL_FDCAN_TIMESTAMP_WRAPAROUND_CB_ID :
  697. hfdcan->TimestampWraparoundCallback = HAL_FDCAN_TimestampWraparoundCallback;
  698. break;
  699. case HAL_FDCAN_TIMEOUT_OCCURRED_CB_ID :
  700. hfdcan->TimeoutOccurredCallback = HAL_FDCAN_TimeoutOccurredCallback;
  701. break;
  702. case HAL_FDCAN_ERROR_CALLBACK_CB_ID :
  703. hfdcan->ErrorCallback = HAL_FDCAN_ErrorCallback;
  704. break;
  705. case HAL_FDCAN_MSPINIT_CB_ID :
  706. hfdcan->MspInitCallback = HAL_FDCAN_MspInit;
  707. break;
  708. case HAL_FDCAN_MSPDEINIT_CB_ID :
  709. hfdcan->MspDeInitCallback = HAL_FDCAN_MspDeInit;
  710. break;
  711. default :
  712. /* Update the error code */
  713. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  714. /* Return error status */
  715. status = HAL_ERROR;
  716. break;
  717. }
  718. }
  719. else if (hfdcan->State == HAL_FDCAN_STATE_RESET)
  720. {
  721. switch (CallbackID)
  722. {
  723. case HAL_FDCAN_MSPINIT_CB_ID :
  724. hfdcan->MspInitCallback = HAL_FDCAN_MspInit;
  725. break;
  726. case HAL_FDCAN_MSPDEINIT_CB_ID :
  727. hfdcan->MspDeInitCallback = HAL_FDCAN_MspDeInit;
  728. break;
  729. default :
  730. /* Update the error code */
  731. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  732. /* Return error status */
  733. status = HAL_ERROR;
  734. break;
  735. }
  736. }
  737. else
  738. {
  739. /* Update the error code */
  740. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  741. /* Return error status */
  742. status = HAL_ERROR;
  743. }
  744. return status;
  745. }
  746. /**
  747. * @brief Register Tx Event Fifo FDCAN Callback
  748. * To be used instead of the weak HAL_FDCAN_TxEventFifoCallback() predefined callback
  749. * @param hfdcan FDCAN handle
  750. * @param pCallback pointer to the Tx Event Fifo Callback function
  751. * @retval HAL status
  752. */
  753. HAL_StatusTypeDef HAL_FDCAN_RegisterTxEventFifoCallback(FDCAN_HandleTypeDef *hfdcan,
  754. pFDCAN_TxEventFifoCallbackTypeDef pCallback)
  755. {
  756. HAL_StatusTypeDef status = HAL_OK;
  757. if (pCallback == NULL)
  758. {
  759. /* Update the error code */
  760. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  761. return HAL_ERROR;
  762. }
  763. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  764. {
  765. hfdcan->TxEventFifoCallback = pCallback;
  766. }
  767. else
  768. {
  769. /* Update the error code */
  770. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  771. /* Return error status */
  772. status = HAL_ERROR;
  773. }
  774. return status;
  775. }
  776. /**
  777. * @brief UnRegister the Tx Event Fifo FDCAN Callback
  778. * Tx Event Fifo FDCAN Callback is redirected to the weak HAL_FDCAN_TxEventFifoCallback() predefined callback
  779. * @param hfdcan FDCAN handle
  780. * @retval HAL status
  781. */
  782. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTxEventFifoCallback(FDCAN_HandleTypeDef *hfdcan)
  783. {
  784. HAL_StatusTypeDef status = HAL_OK;
  785. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  786. {
  787. hfdcan->TxEventFifoCallback = HAL_FDCAN_TxEventFifoCallback; /* Legacy weak TxEventFifoCallback */
  788. }
  789. else
  790. {
  791. /* Update the error code */
  792. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  793. /* Return error status */
  794. status = HAL_ERROR;
  795. }
  796. return status;
  797. }
  798. /**
  799. * @brief Register Rx Fifo 0 FDCAN Callback
  800. * To be used instead of the weak HAL_FDCAN_RxFifo0Callback() predefined callback
  801. * @param hfdcan FDCAN handle
  802. * @param pCallback pointer to the Rx Fifo 0 Callback function
  803. * @retval HAL status
  804. */
  805. HAL_StatusTypeDef HAL_FDCAN_RegisterRxFifo0Callback(FDCAN_HandleTypeDef *hfdcan,
  806. pFDCAN_RxFifo0CallbackTypeDef pCallback)
  807. {
  808. HAL_StatusTypeDef status = HAL_OK;
  809. if (pCallback == NULL)
  810. {
  811. /* Update the error code */
  812. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  813. return HAL_ERROR;
  814. }
  815. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  816. {
  817. hfdcan->RxFifo0Callback = pCallback;
  818. }
  819. else
  820. {
  821. /* Update the error code */
  822. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  823. /* Return error status */
  824. status = HAL_ERROR;
  825. }
  826. return status;
  827. }
  828. /**
  829. * @brief UnRegister the Rx Fifo 0 FDCAN Callback
  830. * Rx Fifo 0 FDCAN Callback is redirected to the weak HAL_FDCAN_RxFifo0Callback() predefined callback
  831. * @param hfdcan FDCAN handle
  832. * @retval HAL status
  833. */
  834. HAL_StatusTypeDef HAL_FDCAN_UnRegisterRxFifo0Callback(FDCAN_HandleTypeDef *hfdcan)
  835. {
  836. HAL_StatusTypeDef status = HAL_OK;
  837. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  838. {
  839. hfdcan->RxFifo0Callback = HAL_FDCAN_RxFifo0Callback; /* Legacy weak RxFifo0Callback */
  840. }
  841. else
  842. {
  843. /* Update the error code */
  844. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  845. /* Return error status */
  846. status = HAL_ERROR;
  847. }
  848. return status;
  849. }
  850. /**
  851. * @brief Register Rx Fifo 1 FDCAN Callback
  852. * To be used instead of the weak HAL_FDCAN_RxFifo1Callback() predefined callback
  853. * @param hfdcan FDCAN handle
  854. * @param pCallback pointer to the Rx Fifo 1 Callback function
  855. * @retval HAL status
  856. */
  857. HAL_StatusTypeDef HAL_FDCAN_RegisterRxFifo1Callback(FDCAN_HandleTypeDef *hfdcan,
  858. pFDCAN_RxFifo1CallbackTypeDef pCallback)
  859. {
  860. HAL_StatusTypeDef status = HAL_OK;
  861. if (pCallback == NULL)
  862. {
  863. /* Update the error code */
  864. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  865. return HAL_ERROR;
  866. }
  867. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  868. {
  869. hfdcan->RxFifo1Callback = pCallback;
  870. }
  871. else
  872. {
  873. /* Update the error code */
  874. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  875. /* Return error status */
  876. status = HAL_ERROR;
  877. }
  878. return status;
  879. }
  880. /**
  881. * @brief UnRegister the Rx Fifo 1 FDCAN Callback
  882. * Rx Fifo 1 FDCAN Callback is redirected to the weak HAL_FDCAN_RxFifo1Callback() predefined callback
  883. * @param hfdcan FDCAN handle
  884. * @retval HAL status
  885. */
  886. HAL_StatusTypeDef HAL_FDCAN_UnRegisterRxFifo1Callback(FDCAN_HandleTypeDef *hfdcan)
  887. {
  888. HAL_StatusTypeDef status = HAL_OK;
  889. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  890. {
  891. hfdcan->RxFifo1Callback = HAL_FDCAN_RxFifo1Callback; /* Legacy weak RxFifo1Callback */
  892. }
  893. else
  894. {
  895. /* Update the error code */
  896. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  897. /* Return error status */
  898. status = HAL_ERROR;
  899. }
  900. return status;
  901. }
  902. /**
  903. * @brief Register Tx Buffer Complete FDCAN Callback
  904. * To be used instead of the weak HAL_FDCAN_TxBufferCompleteCallback() predefined callback
  905. * @param hfdcan FDCAN handle
  906. * @param pCallback pointer to the Tx Buffer Complete Callback function
  907. * @retval HAL status
  908. */
  909. HAL_StatusTypeDef HAL_FDCAN_RegisterTxBufferCompleteCallback(FDCAN_HandleTypeDef *hfdcan,
  910. pFDCAN_TxBufferCompleteCallbackTypeDef pCallback)
  911. {
  912. HAL_StatusTypeDef status = HAL_OK;
  913. if (pCallback == NULL)
  914. {
  915. /* Update the error code */
  916. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  917. return HAL_ERROR;
  918. }
  919. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  920. {
  921. hfdcan->TxBufferCompleteCallback = pCallback;
  922. }
  923. else
  924. {
  925. /* Update the error code */
  926. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  927. /* Return error status */
  928. status = HAL_ERROR;
  929. }
  930. return status;
  931. }
  932. /**
  933. * @brief UnRegister the Tx Buffer Complete FDCAN Callback
  934. * Tx Buffer Complete FDCAN Callback is redirected to
  935. * the weak HAL_FDCAN_TxBufferCompleteCallback() predefined callback
  936. * @param hfdcan FDCAN handle
  937. * @retval HAL status
  938. */
  939. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTxBufferCompleteCallback(FDCAN_HandleTypeDef *hfdcan)
  940. {
  941. HAL_StatusTypeDef status = HAL_OK;
  942. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  943. {
  944. hfdcan->TxBufferCompleteCallback = HAL_FDCAN_TxBufferCompleteCallback; /* Legacy weak TxBufferCompleteCallback */
  945. }
  946. else
  947. {
  948. /* Update the error code */
  949. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  950. /* Return error status */
  951. status = HAL_ERROR;
  952. }
  953. return status;
  954. }
  955. /**
  956. * @brief Register Tx Buffer Abort FDCAN Callback
  957. * To be used instead of the weak HAL_FDCAN_TxBufferAbortCallback() predefined callback
  958. * @param hfdcan FDCAN handle
  959. * @param pCallback pointer to the Tx Buffer Abort Callback function
  960. * @retval HAL status
  961. */
  962. HAL_StatusTypeDef HAL_FDCAN_RegisterTxBufferAbortCallback(FDCAN_HandleTypeDef *hfdcan,
  963. pFDCAN_TxBufferAbortCallbackTypeDef pCallback)
  964. {
  965. HAL_StatusTypeDef status = HAL_OK;
  966. if (pCallback == NULL)
  967. {
  968. /* Update the error code */
  969. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  970. return HAL_ERROR;
  971. }
  972. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  973. {
  974. hfdcan->TxBufferAbortCallback = pCallback;
  975. }
  976. else
  977. {
  978. /* Update the error code */
  979. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  980. /* Return error status */
  981. status = HAL_ERROR;
  982. }
  983. return status;
  984. }
  985. /**
  986. * @brief UnRegister the Tx Buffer Abort FDCAN Callback
  987. * Tx Buffer Abort FDCAN Callback is redirected to
  988. * the weak HAL_FDCAN_TxBufferAbortCallback() predefined callback
  989. * @param hfdcan FDCAN handle
  990. * @retval HAL status
  991. */
  992. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTxBufferAbortCallback(FDCAN_HandleTypeDef *hfdcan)
  993. {
  994. HAL_StatusTypeDef status = HAL_OK;
  995. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  996. {
  997. hfdcan->TxBufferAbortCallback = HAL_FDCAN_TxBufferAbortCallback; /* Legacy weak TxBufferAbortCallback */
  998. }
  999. else
  1000. {
  1001. /* Update the error code */
  1002. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1003. /* Return error status */
  1004. status = HAL_ERROR;
  1005. }
  1006. return status;
  1007. }
  1008. /**
  1009. * @brief Register Error Status FDCAN Callback
  1010. * To be used instead of the weak HAL_FDCAN_ErrorStatusCallback() predefined callback
  1011. * @param hfdcan FDCAN handle
  1012. * @param pCallback pointer to the Error Status Callback function
  1013. * @retval HAL status
  1014. */
  1015. HAL_StatusTypeDef HAL_FDCAN_RegisterErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan,
  1016. pFDCAN_ErrorStatusCallbackTypeDef pCallback)
  1017. {
  1018. HAL_StatusTypeDef status = HAL_OK;
  1019. if (pCallback == NULL)
  1020. {
  1021. /* Update the error code */
  1022. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1023. return HAL_ERROR;
  1024. }
  1025. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1026. {
  1027. hfdcan->ErrorStatusCallback = pCallback;
  1028. }
  1029. else
  1030. {
  1031. /* Update the error code */
  1032. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1033. /* Return error status */
  1034. status = HAL_ERROR;
  1035. }
  1036. return status;
  1037. }
  1038. /**
  1039. * @brief UnRegister the Error Status FDCAN Callback
  1040. * Error Status FDCAN Callback is redirected to the weak HAL_FDCAN_ErrorStatusCallback() predefined callback
  1041. * @param hfdcan FDCAN handle
  1042. * @retval HAL status
  1043. */
  1044. HAL_StatusTypeDef HAL_FDCAN_UnRegisterErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan)
  1045. {
  1046. HAL_StatusTypeDef status = HAL_OK;
  1047. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1048. {
  1049. hfdcan->ErrorStatusCallback = HAL_FDCAN_ErrorStatusCallback; /* Legacy weak ErrorStatusCallback */
  1050. }
  1051. else
  1052. {
  1053. /* Update the error code */
  1054. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1055. /* Return error status */
  1056. status = HAL_ERROR;
  1057. }
  1058. return status;
  1059. }
  1060. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  1061. /**
  1062. * @}
  1063. */
  1064. /** @defgroup FDCAN_Exported_Functions_Group2 Configuration functions
  1065. * @brief FDCAN Configuration functions.
  1066. *
  1067. @verbatim
  1068. ==============================================================================
  1069. ##### Configuration functions #####
  1070. ==============================================================================
  1071. [..] This section provides functions allowing to:
  1072. (+) HAL_FDCAN_ConfigFilter : Configure the FDCAN reception filters
  1073. (+) HAL_FDCAN_ConfigGlobalFilter : Configure the FDCAN global filter
  1074. (+) HAL_FDCAN_ConfigExtendedIdMask : Configure the extended ID mask
  1075. (+) HAL_FDCAN_ConfigRxFifoOverwrite : Configure the Rx FIFO operation mode
  1076. (+) HAL_FDCAN_ConfigRamWatchdog : Configure the RAM watchdog
  1077. (+) HAL_FDCAN_ConfigTimestampCounter : Configure the timestamp counter
  1078. (+) HAL_FDCAN_EnableTimestampCounter : Enable the timestamp counter
  1079. (+) HAL_FDCAN_DisableTimestampCounter : Disable the timestamp counter
  1080. (+) HAL_FDCAN_GetTimestampCounter : Get the timestamp counter value
  1081. (+) HAL_FDCAN_ResetTimestampCounter : Reset the timestamp counter to zero
  1082. (+) HAL_FDCAN_ConfigTimeoutCounter : Configure the timeout counter
  1083. (+) HAL_FDCAN_EnableTimeoutCounter : Enable the timeout counter
  1084. (+) HAL_FDCAN_DisableTimeoutCounter : Disable the timeout counter
  1085. (+) HAL_FDCAN_GetTimeoutCounter : Get the timeout counter value
  1086. (+) HAL_FDCAN_ResetTimeoutCounter : Reset the timeout counter to its start value
  1087. (+) HAL_FDCAN_ConfigTxDelayCompensation : Configure the transmitter delay compensation
  1088. (+) HAL_FDCAN_EnableTxDelayCompensation : Enable the transmitter delay compensation
  1089. (+) HAL_FDCAN_DisableTxDelayCompensation : Disable the transmitter delay compensation
  1090. (+) HAL_FDCAN_EnableISOMode : Enable ISO 11898-1 protocol mode
  1091. (+) HAL_FDCAN_DisableISOMode : Disable ISO 11898-1 protocol mode
  1092. (+) HAL_FDCAN_EnableEdgeFiltering : Enable edge filtering during bus integration
  1093. (+) HAL_FDCAN_DisableEdgeFiltering : Disable edge filtering during bus integration
  1094. @endverbatim
  1095. * @{
  1096. */
  1097. /**
  1098. * @brief Configure the FDCAN reception filter according to the specified
  1099. * parameters in the FDCAN_FilterTypeDef structure.
  1100. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1101. * the configuration information for the specified FDCAN.
  1102. * @param sFilterConfig pointer to an FDCAN_FilterTypeDef structure that
  1103. * contains the filter configuration information
  1104. * @retval HAL status
  1105. */
  1106. HAL_StatusTypeDef HAL_FDCAN_ConfigFilter(FDCAN_HandleTypeDef *hfdcan, const FDCAN_FilterTypeDef *sFilterConfig)
  1107. {
  1108. uint32_t FilterElementW1;
  1109. uint32_t FilterElementW2;
  1110. uint32_t *FilterAddress;
  1111. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  1112. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  1113. {
  1114. /* Check function parameters */
  1115. assert_param(IS_FDCAN_ID_TYPE(sFilterConfig->IdType));
  1116. assert_param(IS_FDCAN_FILTER_CFG(sFilterConfig->FilterConfig));
  1117. if (sFilterConfig->IdType == FDCAN_STANDARD_ID)
  1118. {
  1119. /* Check function parameters */
  1120. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterIndex, (hfdcan->Init.StdFiltersNbr - 1U)));
  1121. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterID1, 0x7FFU));
  1122. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterID2, 0x7FFU));
  1123. assert_param(IS_FDCAN_STD_FILTER_TYPE(sFilterConfig->FilterType));
  1124. /* Build filter element */
  1125. FilterElementW1 = ((sFilterConfig->FilterType << 30U) |
  1126. (sFilterConfig->FilterConfig << 27U) |
  1127. (sFilterConfig->FilterID1 << 16U) |
  1128. sFilterConfig->FilterID2);
  1129. /* Calculate filter address */
  1130. FilterAddress = (uint32_t *)(hfdcan->msgRam.StandardFilterSA + (sFilterConfig->FilterIndex * SRAMCAN_FLS_SIZE));
  1131. /* Write filter element to the message RAM */
  1132. *FilterAddress = FilterElementW1;
  1133. }
  1134. else /* sFilterConfig->IdType == FDCAN_EXTENDED_ID */
  1135. {
  1136. /* Check function parameters */
  1137. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterIndex, (hfdcan->Init.ExtFiltersNbr - 1U)));
  1138. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterID1, 0x1FFFFFFFU));
  1139. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterID2, 0x1FFFFFFFU));
  1140. assert_param(IS_FDCAN_EXT_FILTER_TYPE(sFilterConfig->FilterType));
  1141. /* Build first word of filter element */
  1142. FilterElementW1 = ((sFilterConfig->FilterConfig << 29U) | sFilterConfig->FilterID1);
  1143. /* Build second word of filter element */
  1144. FilterElementW2 = ((sFilterConfig->FilterType << 30U) | sFilterConfig->FilterID2);
  1145. /* Calculate filter address */
  1146. FilterAddress = (uint32_t *)(hfdcan->msgRam.ExtendedFilterSA + (sFilterConfig->FilterIndex * SRAMCAN_FLE_SIZE));
  1147. /* Write filter element to the message RAM */
  1148. *FilterAddress = FilterElementW1;
  1149. FilterAddress++;
  1150. *FilterAddress = FilterElementW2;
  1151. }
  1152. /* Return function status */
  1153. return HAL_OK;
  1154. }
  1155. else
  1156. {
  1157. /* Update error code */
  1158. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  1159. return HAL_ERROR;
  1160. }
  1161. }
  1162. /**
  1163. * @brief Configure the FDCAN global filter.
  1164. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1165. * the configuration information for the specified FDCAN.
  1166. * @param NonMatchingStd Defines how received messages with 11-bit IDs that
  1167. * do not match any element of the filter list are treated.
  1168. * This parameter can be a value of @arg FDCAN_Non_Matching_Frames.
  1169. * @param NonMatchingExt Defines how received messages with 29-bit IDs that
  1170. * do not match any element of the filter list are treated.
  1171. * This parameter can be a value of @arg FDCAN_Non_Matching_Frames.
  1172. * @param RejectRemoteStd Filter or reject all the remote 11-bit IDs frames.
  1173. * This parameter can be a value of @arg FDCAN_Reject_Remote_Frames.
  1174. * @param RejectRemoteExt Filter or reject all the remote 29-bit IDs frames.
  1175. * This parameter can be a value of @arg FDCAN_Reject_Remote_Frames.
  1176. * @retval HAL status
  1177. */
  1178. HAL_StatusTypeDef HAL_FDCAN_ConfigGlobalFilter(FDCAN_HandleTypeDef *hfdcan,
  1179. uint32_t NonMatchingStd,
  1180. uint32_t NonMatchingExt,
  1181. uint32_t RejectRemoteStd,
  1182. uint32_t RejectRemoteExt)
  1183. {
  1184. /* Check function parameters */
  1185. assert_param(IS_FDCAN_NON_MATCHING(NonMatchingStd));
  1186. assert_param(IS_FDCAN_NON_MATCHING(NonMatchingExt));
  1187. assert_param(IS_FDCAN_REJECT_REMOTE(RejectRemoteStd));
  1188. assert_param(IS_FDCAN_REJECT_REMOTE(RejectRemoteExt));
  1189. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1190. {
  1191. /* Configure global filter */
  1192. MODIFY_REG(hfdcan->Instance->RXGFC, (FDCAN_RXGFC_ANFS |
  1193. FDCAN_RXGFC_ANFE |
  1194. FDCAN_RXGFC_RRFS |
  1195. FDCAN_RXGFC_RRFE),
  1196. ((NonMatchingStd << FDCAN_RXGFC_ANFS_Pos) |
  1197. (NonMatchingExt << FDCAN_RXGFC_ANFE_Pos) |
  1198. (RejectRemoteStd << FDCAN_RXGFC_RRFS_Pos) |
  1199. (RejectRemoteExt << FDCAN_RXGFC_RRFE_Pos)));
  1200. /* Return function status */
  1201. return HAL_OK;
  1202. }
  1203. else
  1204. {
  1205. /* Update error code */
  1206. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1207. return HAL_ERROR;
  1208. }
  1209. }
  1210. /**
  1211. * @brief Configure the extended ID mask.
  1212. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1213. * the configuration information for the specified FDCAN.
  1214. * @param Mask Extended ID Mask.
  1215. * This parameter must be a number between 0 and 0x1FFFFFFF.
  1216. * @retval HAL status
  1217. */
  1218. HAL_StatusTypeDef HAL_FDCAN_ConfigExtendedIdMask(FDCAN_HandleTypeDef *hfdcan, uint32_t Mask)
  1219. {
  1220. /* Check function parameters */
  1221. assert_param(IS_FDCAN_MAX_VALUE(Mask, 0x1FFFFFFFU));
  1222. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1223. {
  1224. /* Configure the extended ID mask */
  1225. hfdcan->Instance->XIDAM = Mask;
  1226. /* Return function status */
  1227. return HAL_OK;
  1228. }
  1229. else
  1230. {
  1231. /* Update error code */
  1232. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1233. return HAL_ERROR;
  1234. }
  1235. }
  1236. /**
  1237. * @brief Configure the Rx FIFO operation mode.
  1238. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1239. * the configuration information for the specified FDCAN.
  1240. * @param RxFifo Rx FIFO.
  1241. * This parameter can be one of the following values:
  1242. * @arg FDCAN_RX_FIFO0: Rx FIFO 0
  1243. * @arg FDCAN_RX_FIFO1: Rx FIFO 1
  1244. * @param OperationMode operation mode.
  1245. * This parameter can be a value of @arg FDCAN_Rx_FIFO_operation_mode.
  1246. * @retval HAL status
  1247. */
  1248. HAL_StatusTypeDef HAL_FDCAN_ConfigRxFifoOverwrite(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo, uint32_t OperationMode)
  1249. {
  1250. /* Check function parameters */
  1251. assert_param(IS_FDCAN_RX_FIFO(RxFifo));
  1252. assert_param(IS_FDCAN_RX_FIFO_MODE(OperationMode));
  1253. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1254. {
  1255. if (RxFifo == FDCAN_RX_FIFO0)
  1256. {
  1257. /* Select FIFO 0 Operation Mode */
  1258. MODIFY_REG(hfdcan->Instance->RXGFC, FDCAN_RXGFC_F0OM, (OperationMode << FDCAN_RXGFC_F0OM_Pos));
  1259. }
  1260. else /* RxFifo == FDCAN_RX_FIFO1 */
  1261. {
  1262. /* Select FIFO 1 Operation Mode */
  1263. MODIFY_REG(hfdcan->Instance->RXGFC, FDCAN_RXGFC_F1OM, (OperationMode << FDCAN_RXGFC_F1OM_Pos));
  1264. }
  1265. /* Return function status */
  1266. return HAL_OK;
  1267. }
  1268. else
  1269. {
  1270. /* Update error code */
  1271. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1272. return HAL_ERROR;
  1273. }
  1274. }
  1275. /**
  1276. * @brief Configure the RAM watchdog.
  1277. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1278. * the configuration information for the specified FDCAN.
  1279. * @param CounterStartValue Start value of the Message RAM Watchdog Counter,
  1280. * This parameter must be a number between 0x00 and 0xFF,
  1281. * with the reset value of 0x00 the counter is disabled.
  1282. * @retval HAL status
  1283. */
  1284. HAL_StatusTypeDef HAL_FDCAN_ConfigRamWatchdog(FDCAN_HandleTypeDef *hfdcan, uint32_t CounterStartValue)
  1285. {
  1286. /* Check function parameters */
  1287. assert_param(IS_FDCAN_MAX_VALUE(CounterStartValue, 0xFFU));
  1288. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1289. {
  1290. /* Configure the RAM watchdog counter start value */
  1291. MODIFY_REG(hfdcan->Instance->RWD, FDCAN_RWD_WDC, CounterStartValue);
  1292. /* Return function status */
  1293. return HAL_OK;
  1294. }
  1295. else
  1296. {
  1297. /* Update error code */
  1298. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1299. return HAL_ERROR;
  1300. }
  1301. }
  1302. /**
  1303. * @brief Configure the timestamp counter.
  1304. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1305. * the configuration information for the specified FDCAN.
  1306. * @param TimestampPrescaler Timestamp Counter Prescaler.
  1307. * This parameter can be a value of @arg FDCAN_Timestamp_Prescaler.
  1308. * @retval HAL status
  1309. */
  1310. HAL_StatusTypeDef HAL_FDCAN_ConfigTimestampCounter(FDCAN_HandleTypeDef *hfdcan, uint32_t TimestampPrescaler)
  1311. {
  1312. /* Check function parameters */
  1313. assert_param(IS_FDCAN_TIMESTAMP_PRESCALER(TimestampPrescaler));
  1314. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1315. {
  1316. /* Configure prescaler */
  1317. MODIFY_REG(hfdcan->Instance->TSCC, FDCAN_TSCC_TCP, TimestampPrescaler);
  1318. /* Return function status */
  1319. return HAL_OK;
  1320. }
  1321. else
  1322. {
  1323. /* Update error code */
  1324. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1325. return HAL_ERROR;
  1326. }
  1327. }
  1328. /**
  1329. * @brief Enable the timestamp counter.
  1330. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1331. * the configuration information for the specified FDCAN.
  1332. * @param TimestampOperation Timestamp counter operation.
  1333. * This parameter can be a value of @arg FDCAN_Timestamp.
  1334. * @retval HAL status
  1335. */
  1336. HAL_StatusTypeDef HAL_FDCAN_EnableTimestampCounter(FDCAN_HandleTypeDef *hfdcan, uint32_t TimestampOperation)
  1337. {
  1338. /* Check function parameters */
  1339. assert_param(IS_FDCAN_TIMESTAMP(TimestampOperation));
  1340. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1341. {
  1342. /* Enable timestamp counter */
  1343. MODIFY_REG(hfdcan->Instance->TSCC, FDCAN_TSCC_TSS, TimestampOperation);
  1344. /* Return function status */
  1345. return HAL_OK;
  1346. }
  1347. else
  1348. {
  1349. /* Update error code */
  1350. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1351. return HAL_ERROR;
  1352. }
  1353. }
  1354. /**
  1355. * @brief Disable the timestamp counter.
  1356. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1357. * the configuration information for the specified FDCAN.
  1358. * @retval HAL status
  1359. */
  1360. HAL_StatusTypeDef HAL_FDCAN_DisableTimestampCounter(FDCAN_HandleTypeDef *hfdcan)
  1361. {
  1362. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1363. {
  1364. /* Disable timestamp counter */
  1365. CLEAR_BIT(hfdcan->Instance->TSCC, FDCAN_TSCC_TSS);
  1366. /* Return function status */
  1367. return HAL_OK;
  1368. }
  1369. else
  1370. {
  1371. /* Update error code */
  1372. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1373. return HAL_ERROR;
  1374. }
  1375. }
  1376. /**
  1377. * @brief Get the timestamp counter value.
  1378. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1379. * the configuration information for the specified FDCAN.
  1380. * @retval Timestamp counter value
  1381. */
  1382. uint16_t HAL_FDCAN_GetTimestampCounter(const FDCAN_HandleTypeDef *hfdcan)
  1383. {
  1384. return (uint16_t)(hfdcan->Instance->TSCV);
  1385. }
  1386. /**
  1387. * @brief Reset the timestamp counter to zero.
  1388. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1389. * the configuration information for the specified FDCAN.
  1390. * @retval HAL status
  1391. */
  1392. HAL_StatusTypeDef HAL_FDCAN_ResetTimestampCounter(FDCAN_HandleTypeDef *hfdcan)
  1393. {
  1394. if ((hfdcan->Instance->TSCC & FDCAN_TSCC_TSS) != FDCAN_TIMESTAMP_EXTERNAL)
  1395. {
  1396. /* Reset timestamp counter.
  1397. Actually any write operation to TSCV clears the counter */
  1398. CLEAR_REG(hfdcan->Instance->TSCV);
  1399. }
  1400. else
  1401. {
  1402. /* Update error code.
  1403. Unable to reset external counter */
  1404. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  1405. return HAL_ERROR;
  1406. }
  1407. /* Return function status */
  1408. return HAL_OK;
  1409. }
  1410. /**
  1411. * @brief Configure the timeout counter.
  1412. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1413. * the configuration information for the specified FDCAN.
  1414. * @param TimeoutOperation Timeout counter operation.
  1415. * This parameter can be a value of @arg FDCAN_Timeout_Operation.
  1416. * @param TimeoutPeriod Start value of the timeout down-counter.
  1417. * This parameter must be a number between 0x0000 and 0xFFFF
  1418. * @retval HAL status
  1419. */
  1420. HAL_StatusTypeDef HAL_FDCAN_ConfigTimeoutCounter(FDCAN_HandleTypeDef *hfdcan, uint32_t TimeoutOperation,
  1421. uint32_t TimeoutPeriod)
  1422. {
  1423. /* Check function parameters */
  1424. assert_param(IS_FDCAN_TIMEOUT(TimeoutOperation));
  1425. assert_param(IS_FDCAN_MAX_VALUE(TimeoutPeriod, 0xFFFFU));
  1426. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1427. {
  1428. /* Select timeout operation and configure period */
  1429. MODIFY_REG(hfdcan->Instance->TOCC,
  1430. (FDCAN_TOCC_TOS | FDCAN_TOCC_TOP), (TimeoutOperation | (TimeoutPeriod << FDCAN_TOCC_TOP_Pos)));
  1431. /* Return function status */
  1432. return HAL_OK;
  1433. }
  1434. else
  1435. {
  1436. /* Update error code */
  1437. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1438. return HAL_ERROR;
  1439. }
  1440. }
  1441. /**
  1442. * @brief Enable the timeout counter.
  1443. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1444. * the configuration information for the specified FDCAN.
  1445. * @retval HAL status
  1446. */
  1447. HAL_StatusTypeDef HAL_FDCAN_EnableTimeoutCounter(FDCAN_HandleTypeDef *hfdcan)
  1448. {
  1449. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1450. {
  1451. /* Enable timeout counter */
  1452. SET_BIT(hfdcan->Instance->TOCC, FDCAN_TOCC_ETOC);
  1453. /* Return function status */
  1454. return HAL_OK;
  1455. }
  1456. else
  1457. {
  1458. /* Update error code */
  1459. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1460. return HAL_ERROR;
  1461. }
  1462. }
  1463. /**
  1464. * @brief Disable the timeout counter.
  1465. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1466. * the configuration information for the specified FDCAN.
  1467. * @retval HAL status
  1468. */
  1469. HAL_StatusTypeDef HAL_FDCAN_DisableTimeoutCounter(FDCAN_HandleTypeDef *hfdcan)
  1470. {
  1471. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1472. {
  1473. /* Disable timeout counter */
  1474. CLEAR_BIT(hfdcan->Instance->TOCC, FDCAN_TOCC_ETOC);
  1475. /* Return function status */
  1476. return HAL_OK;
  1477. }
  1478. else
  1479. {
  1480. /* Update error code */
  1481. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1482. return HAL_ERROR;
  1483. }
  1484. }
  1485. /**
  1486. * @brief Get the timeout counter value.
  1487. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1488. * the configuration information for the specified FDCAN.
  1489. * @retval Timeout counter value
  1490. */
  1491. uint16_t HAL_FDCAN_GetTimeoutCounter(const FDCAN_HandleTypeDef *hfdcan)
  1492. {
  1493. return (uint16_t)(hfdcan->Instance->TOCV);
  1494. }
  1495. /**
  1496. * @brief Reset the timeout counter to its start value.
  1497. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1498. * the configuration information for the specified FDCAN.
  1499. * @retval HAL status
  1500. */
  1501. HAL_StatusTypeDef HAL_FDCAN_ResetTimeoutCounter(FDCAN_HandleTypeDef *hfdcan)
  1502. {
  1503. if ((hfdcan->Instance->TOCC & FDCAN_TOCC_TOS) == FDCAN_TIMEOUT_CONTINUOUS)
  1504. {
  1505. /* Reset timeout counter to start value */
  1506. CLEAR_REG(hfdcan->Instance->TOCV);
  1507. /* Return function status */
  1508. return HAL_OK;
  1509. }
  1510. else
  1511. {
  1512. /* Update error code.
  1513. Unable to reset counter: controlled only by FIFO empty state */
  1514. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  1515. return HAL_ERROR;
  1516. }
  1517. }
  1518. /**
  1519. * @brief Configure the transmitter delay compensation.
  1520. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1521. * the configuration information for the specified FDCAN.
  1522. * @param TdcOffset Transmitter Delay Compensation Offset.
  1523. * This parameter must be a number between 0x00 and 0x7F.
  1524. * @param TdcFilter Transmitter Delay Compensation Filter Window Length.
  1525. * This parameter must be a number between 0x00 and 0x7F.
  1526. * @retval HAL status
  1527. */
  1528. HAL_StatusTypeDef HAL_FDCAN_ConfigTxDelayCompensation(FDCAN_HandleTypeDef *hfdcan, uint32_t TdcOffset,
  1529. uint32_t TdcFilter)
  1530. {
  1531. /* Check function parameters */
  1532. assert_param(IS_FDCAN_MAX_VALUE(TdcOffset, 0x7FU));
  1533. assert_param(IS_FDCAN_MAX_VALUE(TdcFilter, 0x7FU));
  1534. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1535. {
  1536. /* Configure TDC offset and filter window */
  1537. hfdcan->Instance->TDCR = ((TdcFilter << FDCAN_TDCR_TDCF_Pos) | (TdcOffset << FDCAN_TDCR_TDCO_Pos));
  1538. /* Return function status */
  1539. return HAL_OK;
  1540. }
  1541. else
  1542. {
  1543. /* Update error code */
  1544. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1545. return HAL_ERROR;
  1546. }
  1547. }
  1548. /**
  1549. * @brief Enable the transmitter delay compensation.
  1550. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1551. * the configuration information for the specified FDCAN.
  1552. * @retval HAL status
  1553. */
  1554. HAL_StatusTypeDef HAL_FDCAN_EnableTxDelayCompensation(FDCAN_HandleTypeDef *hfdcan)
  1555. {
  1556. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1557. {
  1558. /* Enable transmitter delay compensation */
  1559. SET_BIT(hfdcan->Instance->DBTP, FDCAN_DBTP_TDC);
  1560. /* Return function status */
  1561. return HAL_OK;
  1562. }
  1563. else
  1564. {
  1565. /* Update error code */
  1566. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1567. return HAL_ERROR;
  1568. }
  1569. }
  1570. /**
  1571. * @brief Disable the transmitter delay compensation.
  1572. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1573. * the configuration information for the specified FDCAN.
  1574. * @retval HAL status
  1575. */
  1576. HAL_StatusTypeDef HAL_FDCAN_DisableTxDelayCompensation(FDCAN_HandleTypeDef *hfdcan)
  1577. {
  1578. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1579. {
  1580. /* Disable transmitter delay compensation */
  1581. CLEAR_BIT(hfdcan->Instance->DBTP, FDCAN_DBTP_TDC);
  1582. /* Return function status */
  1583. return HAL_OK;
  1584. }
  1585. else
  1586. {
  1587. /* Update error code */
  1588. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1589. return HAL_ERROR;
  1590. }
  1591. }
  1592. /**
  1593. * @brief Enable ISO 11898-1 protocol mode.
  1594. * CAN FD frame format is according to ISO 11898-1 standard.
  1595. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1596. * the configuration information for the specified FDCAN.
  1597. * @retval HAL status
  1598. */
  1599. HAL_StatusTypeDef HAL_FDCAN_EnableISOMode(FDCAN_HandleTypeDef *hfdcan)
  1600. {
  1601. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1602. {
  1603. /* Disable Non ISO protocol mode */
  1604. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_NISO);
  1605. /* Return function status */
  1606. return HAL_OK;
  1607. }
  1608. else
  1609. {
  1610. /* Update error code */
  1611. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1612. return HAL_ERROR;
  1613. }
  1614. }
  1615. /**
  1616. * @brief Disable ISO 11898-1 protocol mode.
  1617. * CAN FD frame format is according to Bosch CAN FD specification V1.0.
  1618. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1619. * the configuration information for the specified FDCAN.
  1620. * @retval HAL status
  1621. */
  1622. HAL_StatusTypeDef HAL_FDCAN_DisableISOMode(FDCAN_HandleTypeDef *hfdcan)
  1623. {
  1624. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1625. {
  1626. /* Enable Non ISO protocol mode */
  1627. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_NISO);
  1628. /* Return function status */
  1629. return HAL_OK;
  1630. }
  1631. else
  1632. {
  1633. /* Update error code */
  1634. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1635. return HAL_ERROR;
  1636. }
  1637. }
  1638. /**
  1639. * @brief Enable edge filtering during bus integration.
  1640. * Two consecutive dominant tq are required to detect an edge for hard synchronization.
  1641. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1642. * the configuration information for the specified FDCAN.
  1643. * @retval HAL status
  1644. */
  1645. HAL_StatusTypeDef HAL_FDCAN_EnableEdgeFiltering(FDCAN_HandleTypeDef *hfdcan)
  1646. {
  1647. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1648. {
  1649. /* Enable edge filtering */
  1650. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_EFBI);
  1651. /* Return function status */
  1652. return HAL_OK;
  1653. }
  1654. else
  1655. {
  1656. /* Update error code */
  1657. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1658. return HAL_ERROR;
  1659. }
  1660. }
  1661. /**
  1662. * @brief Disable edge filtering during bus integration.
  1663. * One dominant tq is required to detect an edge for hard synchronization.
  1664. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1665. * the configuration information for the specified FDCAN.
  1666. * @retval HAL status
  1667. */
  1668. HAL_StatusTypeDef HAL_FDCAN_DisableEdgeFiltering(FDCAN_HandleTypeDef *hfdcan)
  1669. {
  1670. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1671. {
  1672. /* Disable edge filtering */
  1673. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_EFBI);
  1674. /* Return function status */
  1675. return HAL_OK;
  1676. }
  1677. else
  1678. {
  1679. /* Update error code */
  1680. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1681. return HAL_ERROR;
  1682. }
  1683. }
  1684. /**
  1685. * @}
  1686. */
  1687. /** @defgroup FDCAN_Exported_Functions_Group3 Control functions
  1688. * @brief Control functions
  1689. *
  1690. @verbatim
  1691. ==============================================================================
  1692. ##### Control functions #####
  1693. ==============================================================================
  1694. [..] This section provides functions allowing to:
  1695. (+) HAL_FDCAN_Start : Start the FDCAN module
  1696. (+) HAL_FDCAN_Stop : Stop the FDCAN module and enable access to configuration registers
  1697. (+) HAL_FDCAN_AddMessageToTxFifoQ : Add a message to the Tx FIFO/Queue and activate the corresponding
  1698. transmission request
  1699. (+) HAL_FDCAN_GetLatestTxFifoQRequestBuffer : Get Tx buffer index of latest Tx FIFO/Queue request
  1700. (+) HAL_FDCAN_AbortTxRequest : Abort transmission request
  1701. (+) HAL_FDCAN_GetRxMessage : Get an FDCAN frame from the Rx FIFO zone into the message RAM
  1702. (+) HAL_FDCAN_GetTxEvent : Get an FDCAN Tx event from the Tx Event FIFO zone
  1703. into the message RAM
  1704. (+) HAL_FDCAN_GetHighPriorityMessageStatus : Get high priority message status
  1705. (+) HAL_FDCAN_GetProtocolStatus : Get protocol status
  1706. (+) HAL_FDCAN_GetErrorCounters : Get error counter values
  1707. (+) HAL_FDCAN_IsTxBufferMessagePending : Check if a transmission request is pending
  1708. on the selected Tx buffer
  1709. (+) HAL_FDCAN_GetRxFifoFillLevel : Return Rx FIFO fill level
  1710. (+) HAL_FDCAN_GetTxFifoFreeLevel : Return Tx FIFO free level
  1711. (+) HAL_FDCAN_IsRestrictedOperationMode : Check if the FDCAN peripheral entered Restricted Operation Mode
  1712. (+) HAL_FDCAN_ExitRestrictedOperationMode : Exit Restricted Operation Mode
  1713. @endverbatim
  1714. * @{
  1715. */
  1716. /**
  1717. * @brief Start the FDCAN module.
  1718. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1719. * the configuration information for the specified FDCAN.
  1720. * @retval HAL status
  1721. */
  1722. HAL_StatusTypeDef HAL_FDCAN_Start(FDCAN_HandleTypeDef *hfdcan)
  1723. {
  1724. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1725. {
  1726. /* Change FDCAN peripheral state */
  1727. hfdcan->State = HAL_FDCAN_STATE_BUSY;
  1728. /* Request leave initialisation */
  1729. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_INIT);
  1730. /* Reset the FDCAN ErrorCode */
  1731. hfdcan->ErrorCode = HAL_FDCAN_ERROR_NONE;
  1732. /* Return function status */
  1733. return HAL_OK;
  1734. }
  1735. else
  1736. {
  1737. /* Update error code */
  1738. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1739. return HAL_ERROR;
  1740. }
  1741. }
  1742. /**
  1743. * @brief Stop the FDCAN module and enable access to configuration registers.
  1744. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1745. * the configuration information for the specified FDCAN.
  1746. * @retval HAL status
  1747. */
  1748. HAL_StatusTypeDef HAL_FDCAN_Stop(FDCAN_HandleTypeDef *hfdcan)
  1749. {
  1750. uint32_t Counter = 0U;
  1751. if (hfdcan->State == HAL_FDCAN_STATE_BUSY)
  1752. {
  1753. /* Request initialisation */
  1754. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_INIT);
  1755. /* Wait until the INIT bit into CCCR register is set */
  1756. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_INIT) == 0U)
  1757. {
  1758. /* Check for the Timeout */
  1759. if (Counter > FDCAN_TIMEOUT_VALUE)
  1760. {
  1761. /* Update error code */
  1762. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  1763. /* Change FDCAN state */
  1764. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  1765. return HAL_ERROR;
  1766. }
  1767. /* Increment counter */
  1768. Counter++;
  1769. }
  1770. /* Reset counter */
  1771. Counter = 0U;
  1772. /* Exit from Sleep mode */
  1773. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CSR);
  1774. /* Wait until FDCAN exits sleep mode */
  1775. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_CSA) == FDCAN_CCCR_CSA)
  1776. {
  1777. /* Check for the Timeout */
  1778. if (Counter > FDCAN_TIMEOUT_VALUE)
  1779. {
  1780. /* Update error code */
  1781. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  1782. /* Change FDCAN state */
  1783. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  1784. return HAL_ERROR;
  1785. }
  1786. /* Increment counter */
  1787. Counter++;
  1788. }
  1789. /* Enable configuration change */
  1790. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CCE);
  1791. /* Reset Latest Tx FIFO/Queue Request Buffer Index */
  1792. hfdcan->LatestTxFifoQRequest = 0U;
  1793. /* Change FDCAN peripheral state */
  1794. hfdcan->State = HAL_FDCAN_STATE_READY;
  1795. /* Return function status */
  1796. return HAL_OK;
  1797. }
  1798. else
  1799. {
  1800. /* Update error code */
  1801. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  1802. return HAL_ERROR;
  1803. }
  1804. }
  1805. /**
  1806. * @brief Add a message to the Tx FIFO/Queue and activate the corresponding transmission request
  1807. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1808. * the configuration information for the specified FDCAN.
  1809. * @param pTxHeader pointer to a FDCAN_TxHeaderTypeDef structure.
  1810. * @param pTxData pointer to a buffer containing the payload of the Tx frame.
  1811. * @retval HAL status
  1812. */
  1813. HAL_StatusTypeDef HAL_FDCAN_AddMessageToTxFifoQ(FDCAN_HandleTypeDef *hfdcan, const FDCAN_TxHeaderTypeDef *pTxHeader,
  1814. const uint8_t *pTxData)
  1815. {
  1816. uint32_t PutIndex;
  1817. /* Check function parameters */
  1818. assert_param(IS_FDCAN_ID_TYPE(pTxHeader->IdType));
  1819. if (pTxHeader->IdType == FDCAN_STANDARD_ID)
  1820. {
  1821. assert_param(IS_FDCAN_MAX_VALUE(pTxHeader->Identifier, 0x7FFU));
  1822. }
  1823. else /* pTxHeader->IdType == FDCAN_EXTENDED_ID */
  1824. {
  1825. assert_param(IS_FDCAN_MAX_VALUE(pTxHeader->Identifier, 0x1FFFFFFFU));
  1826. }
  1827. assert_param(IS_FDCAN_FRAME_TYPE(pTxHeader->TxFrameType));
  1828. assert_param(IS_FDCAN_DLC(pTxHeader->DataLength));
  1829. assert_param(IS_FDCAN_ESI(pTxHeader->ErrorStateIndicator));
  1830. assert_param(IS_FDCAN_BRS(pTxHeader->BitRateSwitch));
  1831. assert_param(IS_FDCAN_FDF(pTxHeader->FDFormat));
  1832. assert_param(IS_FDCAN_EFC(pTxHeader->TxEventFifoControl));
  1833. assert_param(IS_FDCAN_MAX_VALUE(pTxHeader->MessageMarker, 0xFFU));
  1834. if (hfdcan->State == HAL_FDCAN_STATE_BUSY)
  1835. {
  1836. /* Check that the Tx FIFO/Queue is not full */
  1837. if ((hfdcan->Instance->TXFQS & FDCAN_TXFQS_TFQF) != 0U)
  1838. {
  1839. /* Update error code */
  1840. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_FIFO_FULL;
  1841. return HAL_ERROR;
  1842. }
  1843. else
  1844. {
  1845. /* Retrieve the Tx FIFO PutIndex */
  1846. PutIndex = ((hfdcan->Instance->TXFQS & FDCAN_TXFQS_TFQPI) >> FDCAN_TXFQS_TFQPI_Pos);
  1847. /* Add the message to the Tx FIFO/Queue */
  1848. FDCAN_CopyMessageToRAM(hfdcan, pTxHeader, pTxData, PutIndex);
  1849. /* Activate the corresponding transmission request */
  1850. hfdcan->Instance->TXBAR = ((uint32_t)1 << PutIndex);
  1851. /* Store the Latest Tx FIFO/Queue Request Buffer Index */
  1852. hfdcan->LatestTxFifoQRequest = ((uint32_t)1 << PutIndex);
  1853. }
  1854. /* Return function status */
  1855. return HAL_OK;
  1856. }
  1857. else
  1858. {
  1859. /* Update error code */
  1860. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  1861. return HAL_ERROR;
  1862. }
  1863. }
  1864. /**
  1865. * @brief Get Tx buffer index of latest Tx FIFO/Queue request
  1866. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1867. * the configuration information for the specified FDCAN.
  1868. * @retval Tx buffer index of last Tx FIFO/Queue request
  1869. * - Any value of @arg FDCAN_Tx_location if Tx request has been submitted.
  1870. * - 0 if no Tx FIFO/Queue request have been submitted.
  1871. */
  1872. uint32_t HAL_FDCAN_GetLatestTxFifoQRequestBuffer(const FDCAN_HandleTypeDef *hfdcan)
  1873. {
  1874. /* Return Last Tx FIFO/Queue Request Buffer */
  1875. return hfdcan->LatestTxFifoQRequest;
  1876. }
  1877. /**
  1878. * @brief Abort transmission request
  1879. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1880. * the configuration information for the specified FDCAN.
  1881. * @param BufferIndex buffer index.
  1882. * This parameter can be any combination of @arg FDCAN_Tx_location.
  1883. * @retval HAL status
  1884. */
  1885. HAL_StatusTypeDef HAL_FDCAN_AbortTxRequest(FDCAN_HandleTypeDef *hfdcan, uint32_t BufferIndex)
  1886. {
  1887. /* Check function parameters */
  1888. assert_param(IS_FDCAN_TX_LOCATION_LIST(BufferIndex));
  1889. if (hfdcan->State == HAL_FDCAN_STATE_BUSY)
  1890. {
  1891. /* Add cancellation request */
  1892. hfdcan->Instance->TXBCR = BufferIndex;
  1893. /* Return function status */
  1894. return HAL_OK;
  1895. }
  1896. else
  1897. {
  1898. /* Update error code */
  1899. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  1900. return HAL_ERROR;
  1901. }
  1902. }
  1903. /**
  1904. * @brief Get an FDCAN frame from the Rx FIFO zone into the message RAM.
  1905. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1906. * the configuration information for the specified FDCAN.
  1907. * @param RxLocation Location of the received message to be read.
  1908. * This parameter can be a value of @arg FDCAN_Rx_location.
  1909. * @param pRxHeader pointer to a FDCAN_RxHeaderTypeDef structure.
  1910. * @param pRxData pointer to a buffer where the payload of the Rx frame will be stored.
  1911. * @retval HAL status
  1912. */
  1913. HAL_StatusTypeDef HAL_FDCAN_GetRxMessage(FDCAN_HandleTypeDef *hfdcan, uint32_t RxLocation,
  1914. FDCAN_RxHeaderTypeDef *pRxHeader, uint8_t *pRxData)
  1915. {
  1916. uint32_t *RxAddress;
  1917. uint8_t *pData;
  1918. uint32_t ByteCounter;
  1919. uint32_t GetIndex = 0;
  1920. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  1921. /* Check function parameters */
  1922. assert_param(IS_FDCAN_RX_FIFO(RxLocation));
  1923. if (state == HAL_FDCAN_STATE_BUSY)
  1924. {
  1925. if (RxLocation == FDCAN_RX_FIFO0) /* Rx element is assigned to the Rx FIFO 0 */
  1926. {
  1927. /* Check that the Rx FIFO 0 is not empty */
  1928. if ((hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0FL) == 0U)
  1929. {
  1930. /* Update error code */
  1931. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_FIFO_EMPTY;
  1932. return HAL_ERROR;
  1933. }
  1934. else
  1935. {
  1936. /* Check that the Rx FIFO 0 is full & overwrite mode is on */
  1937. if (((hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0F) >> FDCAN_RXF0S_F0F_Pos) == 1U)
  1938. {
  1939. if (((hfdcan->Instance->RXGFC & FDCAN_RXGFC_F0OM) >> FDCAN_RXGFC_F0OM_Pos) == FDCAN_RX_FIFO_OVERWRITE)
  1940. {
  1941. /* When overwrite status is on discard first message in FIFO */
  1942. GetIndex = 1U;
  1943. }
  1944. }
  1945. /* Calculate Rx FIFO 0 element index */
  1946. GetIndex += ((hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0GI) >> FDCAN_RXF0S_F0GI_Pos);
  1947. /* Calculate Rx FIFO 0 element address */
  1948. RxAddress = (uint32_t *)(hfdcan->msgRam.RxFIFO0SA + (GetIndex * SRAMCAN_RF0_SIZE));
  1949. }
  1950. }
  1951. else /* Rx element is assigned to the Rx FIFO 1 */
  1952. {
  1953. /* Check that the Rx FIFO 1 is not empty */
  1954. if ((hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1FL) == 0U)
  1955. {
  1956. /* Update error code */
  1957. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_FIFO_EMPTY;
  1958. return HAL_ERROR;
  1959. }
  1960. else
  1961. {
  1962. /* Check that the Rx FIFO 1 is full & overwrite mode is on */
  1963. if (((hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1F) >> FDCAN_RXF1S_F1F_Pos) == 1U)
  1964. {
  1965. if (((hfdcan->Instance->RXGFC & FDCAN_RXGFC_F1OM) >> FDCAN_RXGFC_F1OM_Pos) == FDCAN_RX_FIFO_OVERWRITE)
  1966. {
  1967. /* When overwrite status is on discard first message in FIFO */
  1968. GetIndex = 1U;
  1969. }
  1970. }
  1971. /* Calculate Rx FIFO 1 element index */
  1972. GetIndex += ((hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1GI) >> FDCAN_RXF1S_F1GI_Pos);
  1973. /* Calculate Rx FIFO 1 element address */
  1974. RxAddress = (uint32_t *)(hfdcan->msgRam.RxFIFO1SA + (GetIndex * SRAMCAN_RF1_SIZE));
  1975. }
  1976. }
  1977. /* Retrieve IdType */
  1978. pRxHeader->IdType = *RxAddress & FDCAN_ELEMENT_MASK_XTD;
  1979. /* Retrieve Identifier */
  1980. if (pRxHeader->IdType == FDCAN_STANDARD_ID) /* Standard ID element */
  1981. {
  1982. pRxHeader->Identifier = ((*RxAddress & FDCAN_ELEMENT_MASK_STDID) >> 18U);
  1983. }
  1984. else /* Extended ID element */
  1985. {
  1986. pRxHeader->Identifier = (*RxAddress & FDCAN_ELEMENT_MASK_EXTID);
  1987. }
  1988. /* Retrieve RxFrameType */
  1989. pRxHeader->RxFrameType = (*RxAddress & FDCAN_ELEMENT_MASK_RTR);
  1990. /* Retrieve ErrorStateIndicator */
  1991. pRxHeader->ErrorStateIndicator = (*RxAddress & FDCAN_ELEMENT_MASK_ESI);
  1992. /* Increment RxAddress pointer to second word of Rx FIFO element */
  1993. RxAddress++;
  1994. /* Retrieve RxTimestamp */
  1995. pRxHeader->RxTimestamp = (*RxAddress & FDCAN_ELEMENT_MASK_TS);
  1996. /* Retrieve DataLength */
  1997. pRxHeader->DataLength = ((*RxAddress & FDCAN_ELEMENT_MASK_DLC) >> 16U);
  1998. /* Retrieve BitRateSwitch */
  1999. pRxHeader->BitRateSwitch = (*RxAddress & FDCAN_ELEMENT_MASK_BRS);
  2000. /* Retrieve FDFormat */
  2001. pRxHeader->FDFormat = (*RxAddress & FDCAN_ELEMENT_MASK_FDF);
  2002. /* Retrieve FilterIndex */
  2003. pRxHeader->FilterIndex = ((*RxAddress & FDCAN_ELEMENT_MASK_FIDX) >> 24U);
  2004. /* Retrieve NonMatchingFrame */
  2005. pRxHeader->IsFilterMatchingFrame = ((*RxAddress & FDCAN_ELEMENT_MASK_ANMF) >> 31U);
  2006. /* Increment RxAddress pointer to payload of Rx FIFO element */
  2007. RxAddress++;
  2008. /* Retrieve Rx payload */
  2009. pData = (uint8_t *)RxAddress;
  2010. for (ByteCounter = 0; ByteCounter < DLCtoBytes[pRxHeader->DataLength]; ByteCounter++)
  2011. {
  2012. pRxData[ByteCounter] = pData[ByteCounter];
  2013. }
  2014. if (RxLocation == FDCAN_RX_FIFO0) /* Rx element is assigned to the Rx FIFO 0 */
  2015. {
  2016. /* Acknowledge the Rx FIFO 0 that the oldest element is read so that it increments the GetIndex */
  2017. hfdcan->Instance->RXF0A = GetIndex;
  2018. }
  2019. else /* Rx element is assigned to the Rx FIFO 1 */
  2020. {
  2021. /* Acknowledge the Rx FIFO 1 that the oldest element is read so that it increments the GetIndex */
  2022. hfdcan->Instance->RXF1A = GetIndex;
  2023. }
  2024. /* Return function status */
  2025. return HAL_OK;
  2026. }
  2027. else
  2028. {
  2029. /* Update error code */
  2030. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  2031. return HAL_ERROR;
  2032. }
  2033. }
  2034. /**
  2035. * @brief Get an FDCAN Tx event from the Tx Event FIFO zone into the message RAM.
  2036. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2037. * the configuration information for the specified FDCAN.
  2038. * @param pTxEvent pointer to a FDCAN_TxEventFifoTypeDef structure.
  2039. * @retval HAL status
  2040. */
  2041. HAL_StatusTypeDef HAL_FDCAN_GetTxEvent(FDCAN_HandleTypeDef *hfdcan, FDCAN_TxEventFifoTypeDef *pTxEvent)
  2042. {
  2043. uint32_t *TxEventAddress;
  2044. uint32_t GetIndex;
  2045. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  2046. if (state == HAL_FDCAN_STATE_BUSY)
  2047. {
  2048. /* Check that the Tx event FIFO is not empty */
  2049. if ((hfdcan->Instance->TXEFS & FDCAN_TXEFS_EFFL) == 0U)
  2050. {
  2051. /* Update error code */
  2052. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_FIFO_EMPTY;
  2053. return HAL_ERROR;
  2054. }
  2055. /* Calculate Tx event FIFO element address */
  2056. GetIndex = ((hfdcan->Instance->TXEFS & FDCAN_TXEFS_EFGI) >> FDCAN_TXEFS_EFGI_Pos);
  2057. TxEventAddress = (uint32_t *)(hfdcan->msgRam.TxEventFIFOSA + (GetIndex * SRAMCAN_TEF_SIZE));
  2058. /* Retrieve IdType */
  2059. pTxEvent->IdType = *TxEventAddress & FDCAN_ELEMENT_MASK_XTD;
  2060. /* Retrieve Identifier */
  2061. if (pTxEvent->IdType == FDCAN_STANDARD_ID) /* Standard ID element */
  2062. {
  2063. pTxEvent->Identifier = ((*TxEventAddress & FDCAN_ELEMENT_MASK_STDID) >> 18U);
  2064. }
  2065. else /* Extended ID element */
  2066. {
  2067. pTxEvent->Identifier = (*TxEventAddress & FDCAN_ELEMENT_MASK_EXTID);
  2068. }
  2069. /* Retrieve TxFrameType */
  2070. pTxEvent->TxFrameType = (*TxEventAddress & FDCAN_ELEMENT_MASK_RTR);
  2071. /* Retrieve ErrorStateIndicator */
  2072. pTxEvent->ErrorStateIndicator = (*TxEventAddress & FDCAN_ELEMENT_MASK_ESI);
  2073. /* Increment TxEventAddress pointer to second word of Tx Event FIFO element */
  2074. TxEventAddress++;
  2075. /* Retrieve TxTimestamp */
  2076. pTxEvent->TxTimestamp = (*TxEventAddress & FDCAN_ELEMENT_MASK_TS);
  2077. /* Retrieve DataLength */
  2078. pTxEvent->DataLength = ((*TxEventAddress & FDCAN_ELEMENT_MASK_DLC) >> 16U);
  2079. /* Retrieve BitRateSwitch */
  2080. pTxEvent->BitRateSwitch = (*TxEventAddress & FDCAN_ELEMENT_MASK_BRS);
  2081. /* Retrieve FDFormat */
  2082. pTxEvent->FDFormat = (*TxEventAddress & FDCAN_ELEMENT_MASK_FDF);
  2083. /* Retrieve EventType */
  2084. pTxEvent->EventType = (*TxEventAddress & FDCAN_ELEMENT_MASK_ET);
  2085. /* Retrieve MessageMarker */
  2086. pTxEvent->MessageMarker = ((*TxEventAddress & FDCAN_ELEMENT_MASK_MM) >> 24U);
  2087. /* Acknowledge the Tx Event FIFO that the oldest element is read so that it increments the GetIndex */
  2088. hfdcan->Instance->TXEFA = GetIndex;
  2089. /* Return function status */
  2090. return HAL_OK;
  2091. }
  2092. else
  2093. {
  2094. /* Update error code */
  2095. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  2096. return HAL_ERROR;
  2097. }
  2098. }
  2099. /**
  2100. * @brief Get high priority message status.
  2101. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2102. * the configuration information for the specified FDCAN.
  2103. * @param HpMsgStatus pointer to an FDCAN_HpMsgStatusTypeDef structure.
  2104. * @retval HAL status
  2105. */
  2106. HAL_StatusTypeDef HAL_FDCAN_GetHighPriorityMessageStatus(const FDCAN_HandleTypeDef *hfdcan,
  2107. FDCAN_HpMsgStatusTypeDef *HpMsgStatus)
  2108. {
  2109. HpMsgStatus->FilterList = ((hfdcan->Instance->HPMS & FDCAN_HPMS_FLST) >> FDCAN_HPMS_FLST_Pos);
  2110. HpMsgStatus->FilterIndex = ((hfdcan->Instance->HPMS & FDCAN_HPMS_FIDX) >> FDCAN_HPMS_FIDX_Pos);
  2111. HpMsgStatus->MessageStorage = (hfdcan->Instance->HPMS & FDCAN_HPMS_MSI);
  2112. HpMsgStatus->MessageIndex = (hfdcan->Instance->HPMS & FDCAN_HPMS_BIDX);
  2113. /* Return function status */
  2114. return HAL_OK;
  2115. }
  2116. /**
  2117. * @brief Get protocol status.
  2118. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2119. * the configuration information for the specified FDCAN.
  2120. * @param ProtocolStatus pointer to an FDCAN_ProtocolStatusTypeDef structure.
  2121. * @retval HAL status
  2122. */
  2123. HAL_StatusTypeDef HAL_FDCAN_GetProtocolStatus(const FDCAN_HandleTypeDef *hfdcan,
  2124. FDCAN_ProtocolStatusTypeDef *ProtocolStatus)
  2125. {
  2126. uint32_t StatusReg;
  2127. /* Read the protocol status register */
  2128. StatusReg = READ_REG(hfdcan->Instance->PSR);
  2129. /* Fill the protocol status structure */
  2130. ProtocolStatus->LastErrorCode = (StatusReg & FDCAN_PSR_LEC);
  2131. ProtocolStatus->DataLastErrorCode = ((StatusReg & FDCAN_PSR_DLEC) >> FDCAN_PSR_DLEC_Pos);
  2132. ProtocolStatus->Activity = (StatusReg & FDCAN_PSR_ACT);
  2133. ProtocolStatus->ErrorPassive = ((StatusReg & FDCAN_PSR_EP) >> FDCAN_PSR_EP_Pos);
  2134. ProtocolStatus->Warning = ((StatusReg & FDCAN_PSR_EW) >> FDCAN_PSR_EW_Pos);
  2135. ProtocolStatus->BusOff = ((StatusReg & FDCAN_PSR_BO) >> FDCAN_PSR_BO_Pos);
  2136. ProtocolStatus->RxESIflag = ((StatusReg & FDCAN_PSR_RESI) >> FDCAN_PSR_RESI_Pos);
  2137. ProtocolStatus->RxBRSflag = ((StatusReg & FDCAN_PSR_RBRS) >> FDCAN_PSR_RBRS_Pos);
  2138. ProtocolStatus->RxFDFflag = ((StatusReg & FDCAN_PSR_REDL) >> FDCAN_PSR_REDL_Pos);
  2139. ProtocolStatus->ProtocolException = ((StatusReg & FDCAN_PSR_PXE) >> FDCAN_PSR_PXE_Pos);
  2140. ProtocolStatus->TDCvalue = ((StatusReg & FDCAN_PSR_TDCV) >> FDCAN_PSR_TDCV_Pos);
  2141. /* Return function status */
  2142. return HAL_OK;
  2143. }
  2144. /**
  2145. * @brief Get error counter values.
  2146. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2147. * the configuration information for the specified FDCAN.
  2148. * @param ErrorCounters pointer to an FDCAN_ErrorCountersTypeDef structure.
  2149. * @retval HAL status
  2150. */
  2151. HAL_StatusTypeDef HAL_FDCAN_GetErrorCounters(const FDCAN_HandleTypeDef *hfdcan,
  2152. FDCAN_ErrorCountersTypeDef *ErrorCounters)
  2153. {
  2154. uint32_t CountersReg;
  2155. /* Read the error counters register */
  2156. CountersReg = READ_REG(hfdcan->Instance->ECR);
  2157. /* Fill the error counters structure */
  2158. ErrorCounters->TxErrorCnt = ((CountersReg & FDCAN_ECR_TEC) >> FDCAN_ECR_TEC_Pos);
  2159. ErrorCounters->RxErrorCnt = ((CountersReg & FDCAN_ECR_REC) >> FDCAN_ECR_REC_Pos);
  2160. ErrorCounters->RxErrorPassive = ((CountersReg & FDCAN_ECR_RP) >> FDCAN_ECR_RP_Pos);
  2161. ErrorCounters->ErrorLogging = ((CountersReg & FDCAN_ECR_CEL) >> FDCAN_ECR_CEL_Pos);
  2162. /* Return function status */
  2163. return HAL_OK;
  2164. }
  2165. /**
  2166. * @brief Check if a transmission request is pending on the selected Tx buffer.
  2167. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2168. * the configuration information for the specified FDCAN.
  2169. * @param TxBufferIndex Tx buffer index.
  2170. * This parameter can be any combination of @arg FDCAN_Tx_location.
  2171. * @retval Status
  2172. * - 0 : No pending transmission request on TxBufferIndex list.
  2173. * - 1 : Pending transmission request on TxBufferIndex.
  2174. */
  2175. uint32_t HAL_FDCAN_IsTxBufferMessagePending(const FDCAN_HandleTypeDef *hfdcan, uint32_t TxBufferIndex)
  2176. {
  2177. /* Check function parameters */
  2178. assert_param(IS_FDCAN_TX_LOCATION_LIST(TxBufferIndex));
  2179. /* Check pending transmission request on the selected buffer */
  2180. if ((hfdcan->Instance->TXBRP & TxBufferIndex) == 0U)
  2181. {
  2182. return 0;
  2183. }
  2184. return 1;
  2185. }
  2186. /**
  2187. * @brief Return Rx FIFO fill level.
  2188. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2189. * the configuration information for the specified FDCAN.
  2190. * @param RxFifo Rx FIFO.
  2191. * This parameter can be one of the following values:
  2192. * @arg FDCAN_RX_FIFO0: Rx FIFO 0
  2193. * @arg FDCAN_RX_FIFO1: Rx FIFO 1
  2194. * @retval Rx FIFO fill level.
  2195. */
  2196. uint32_t HAL_FDCAN_GetRxFifoFillLevel(const FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo)
  2197. {
  2198. uint32_t FillLevel;
  2199. /* Check function parameters */
  2200. assert_param(IS_FDCAN_RX_FIFO(RxFifo));
  2201. if (RxFifo == FDCAN_RX_FIFO0)
  2202. {
  2203. FillLevel = hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0FL;
  2204. }
  2205. else /* RxFifo == FDCAN_RX_FIFO1 */
  2206. {
  2207. FillLevel = hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1FL;
  2208. }
  2209. /* Return Rx FIFO fill level */
  2210. return FillLevel;
  2211. }
  2212. /**
  2213. * @brief Return Tx FIFO free level: number of consecutive free Tx FIFO
  2214. * elements starting from Tx FIFO GetIndex.
  2215. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2216. * the configuration information for the specified FDCAN.
  2217. * @retval Tx FIFO free level.
  2218. */
  2219. uint32_t HAL_FDCAN_GetTxFifoFreeLevel(const FDCAN_HandleTypeDef *hfdcan)
  2220. {
  2221. uint32_t FreeLevel;
  2222. FreeLevel = hfdcan->Instance->TXFQS & FDCAN_TXFQS_TFFL;
  2223. /* Return Tx FIFO free level */
  2224. return FreeLevel;
  2225. }
  2226. /**
  2227. * @brief Check if the FDCAN peripheral entered Restricted Operation Mode.
  2228. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2229. * the configuration information for the specified FDCAN.
  2230. * @retval Status
  2231. * - 0 : Normal FDCAN operation.
  2232. * - 1 : Restricted Operation Mode active.
  2233. */
  2234. uint32_t HAL_FDCAN_IsRestrictedOperationMode(const FDCAN_HandleTypeDef *hfdcan)
  2235. {
  2236. uint32_t OperationMode;
  2237. /* Get Operation Mode */
  2238. OperationMode = ((hfdcan->Instance->CCCR & FDCAN_CCCR_ASM) >> FDCAN_CCCR_ASM_Pos);
  2239. return OperationMode;
  2240. }
  2241. /**
  2242. * @brief Exit Restricted Operation Mode.
  2243. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2244. * the configuration information for the specified FDCAN.
  2245. * @retval HAL status
  2246. */
  2247. HAL_StatusTypeDef HAL_FDCAN_ExitRestrictedOperationMode(FDCAN_HandleTypeDef *hfdcan)
  2248. {
  2249. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  2250. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  2251. {
  2252. /* Exit Restricted Operation mode */
  2253. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_ASM);
  2254. /* Return function status */
  2255. return HAL_OK;
  2256. }
  2257. else
  2258. {
  2259. /* Update error code */
  2260. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  2261. return HAL_ERROR;
  2262. }
  2263. }
  2264. /**
  2265. * @}
  2266. */
  2267. /** @defgroup FDCAN_Exported_Functions_Group4 Interrupts management
  2268. * @brief Interrupts management
  2269. *
  2270. @verbatim
  2271. ==============================================================================
  2272. ##### Interrupts management #####
  2273. ==============================================================================
  2274. [..] This section provides functions allowing to:
  2275. (+) HAL_FDCAN_ConfigInterruptLines : Assign interrupts to either Interrupt line 0 or 1
  2276. (+) HAL_FDCAN_ActivateNotification : Enable interrupts
  2277. (+) HAL_FDCAN_DeactivateNotification : Disable interrupts
  2278. (+) HAL_FDCAN_IRQHandler : Handles FDCAN interrupt request
  2279. @endverbatim
  2280. * @{
  2281. */
  2282. /**
  2283. * @brief Assign interrupts to either Interrupt line 0 or 1.
  2284. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2285. * the configuration information for the specified FDCAN.
  2286. * @param ITList indicates which interrupts group will be assigned to the selected interrupt line.
  2287. * This parameter can be any combination of @arg FDCAN_Interrupts_Group.
  2288. * @param InterruptLine Interrupt line.
  2289. * This parameter can be a value of @arg FDCAN_Interrupt_Line.
  2290. * @retval HAL status
  2291. */
  2292. HAL_StatusTypeDef HAL_FDCAN_ConfigInterruptLines(FDCAN_HandleTypeDef *hfdcan, uint32_t ITList, uint32_t InterruptLine)
  2293. {
  2294. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  2295. /* Check function parameters */
  2296. assert_param(IS_FDCAN_IT_GROUP(ITList));
  2297. assert_param(IS_FDCAN_IT_LINE(InterruptLine));
  2298. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  2299. {
  2300. /* Assign list of interrupts to the selected line */
  2301. if (InterruptLine == FDCAN_INTERRUPT_LINE0)
  2302. {
  2303. CLEAR_BIT(hfdcan->Instance->ILS, ITList);
  2304. }
  2305. else /* InterruptLine == FDCAN_INTERRUPT_LINE1 */
  2306. {
  2307. SET_BIT(hfdcan->Instance->ILS, ITList);
  2308. }
  2309. /* Return function status */
  2310. return HAL_OK;
  2311. }
  2312. else
  2313. {
  2314. /* Update error code */
  2315. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  2316. return HAL_ERROR;
  2317. }
  2318. }
  2319. /**
  2320. * @brief Enable interrupts.
  2321. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2322. * the configuration information for the specified FDCAN.
  2323. * @param ActiveITs indicates which interrupts will be enabled.
  2324. * This parameter can be any combination of @arg FDCAN_Interrupts.
  2325. * @param BufferIndexes Tx Buffer Indexes.
  2326. * This parameter can be any combination of @arg FDCAN_Tx_location.
  2327. * This parameter is ignored if ActiveITs does not include one of the following:
  2328. * - FDCAN_IT_TX_COMPLETE
  2329. * - FDCAN_IT_TX_ABORT_COMPLETE
  2330. * @retval HAL status
  2331. */
  2332. HAL_StatusTypeDef HAL_FDCAN_ActivateNotification(FDCAN_HandleTypeDef *hfdcan, uint32_t ActiveITs,
  2333. uint32_t BufferIndexes)
  2334. {
  2335. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  2336. uint32_t ITs_lines_selection;
  2337. /* Check function parameters */
  2338. assert_param(IS_FDCAN_IT(ActiveITs));
  2339. if ((ActiveITs & (FDCAN_IT_TX_COMPLETE | FDCAN_IT_TX_ABORT_COMPLETE)) != 0U)
  2340. {
  2341. assert_param(IS_FDCAN_TX_LOCATION_LIST(BufferIndexes));
  2342. }
  2343. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  2344. {
  2345. /* Get interrupts line selection */
  2346. ITs_lines_selection = hfdcan->Instance->ILS;
  2347. /* Enable Interrupt lines */
  2348. if ((((ActiveITs & FDCAN_IT_LIST_RX_FIFO0) != 0U)
  2349. && (((ITs_lines_selection) & FDCAN_IT_GROUP_RX_FIFO0) == 0U)) || \
  2350. (((ActiveITs & FDCAN_IT_LIST_RX_FIFO1) != 0U)
  2351. && (((ITs_lines_selection) & FDCAN_IT_GROUP_RX_FIFO1) == 0U)) || \
  2352. (((ActiveITs & FDCAN_IT_LIST_SMSG) != 0U)
  2353. && (((ITs_lines_selection) & FDCAN_IT_GROUP_SMSG) == 0U)) || \
  2354. (((ActiveITs & FDCAN_IT_LIST_TX_FIFO_ERROR) != 0U)
  2355. && (((ITs_lines_selection) & FDCAN_IT_GROUP_TX_FIFO_ERROR) == 0U)) || \
  2356. (((ActiveITs & FDCAN_IT_LIST_MISC) != 0U)
  2357. && (((ITs_lines_selection) & FDCAN_IT_GROUP_MISC) == 0U)) || \
  2358. (((ActiveITs & FDCAN_IT_LIST_BIT_LINE_ERROR) != 0U)
  2359. && (((ITs_lines_selection) & FDCAN_IT_GROUP_BIT_LINE_ERROR) == 0U)) || \
  2360. (((ActiveITs & FDCAN_IT_LIST_PROTOCOL_ERROR) != 0U)
  2361. && (((ITs_lines_selection) & FDCAN_IT_GROUP_PROTOCOL_ERROR) == 0U)))
  2362. {
  2363. /* Enable Interrupt line 0 */
  2364. SET_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE0);
  2365. }
  2366. if ((((ActiveITs & FDCAN_IT_LIST_RX_FIFO0) != 0U)
  2367. && (((ITs_lines_selection) & FDCAN_IT_GROUP_RX_FIFO0) != 0U)) || \
  2368. (((ActiveITs & FDCAN_IT_LIST_RX_FIFO1) != 0U)
  2369. && (((ITs_lines_selection) & FDCAN_IT_GROUP_RX_FIFO1) != 0U)) || \
  2370. (((ActiveITs & FDCAN_IT_LIST_SMSG) != 0U)
  2371. && (((ITs_lines_selection) & FDCAN_IT_GROUP_SMSG) != 0U)) || \
  2372. (((ActiveITs & FDCAN_IT_LIST_TX_FIFO_ERROR) != 0U)
  2373. && (((ITs_lines_selection) & FDCAN_IT_GROUP_TX_FIFO_ERROR) != 0U)) || \
  2374. (((ActiveITs & FDCAN_IT_LIST_MISC) != 0U)
  2375. && (((ITs_lines_selection) & FDCAN_IT_GROUP_MISC) != 0U)) || \
  2376. (((ActiveITs & FDCAN_IT_LIST_BIT_LINE_ERROR) != 0U)
  2377. && (((ITs_lines_selection) & FDCAN_IT_GROUP_BIT_LINE_ERROR) != 0U)) || \
  2378. (((ActiveITs & FDCAN_IT_LIST_PROTOCOL_ERROR) != 0U)
  2379. && (((ITs_lines_selection) & FDCAN_IT_GROUP_PROTOCOL_ERROR) != 0U)))
  2380. {
  2381. /* Enable Interrupt line 1 */
  2382. SET_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE1);
  2383. }
  2384. if ((ActiveITs & FDCAN_IT_TX_COMPLETE) != 0U)
  2385. {
  2386. /* Enable Tx Buffer Transmission Interrupt to set TC flag in IR register,
  2387. but interrupt will only occur if TC is enabled in IE register */
  2388. SET_BIT(hfdcan->Instance->TXBTIE, BufferIndexes);
  2389. }
  2390. if ((ActiveITs & FDCAN_IT_TX_ABORT_COMPLETE) != 0U)
  2391. {
  2392. /* Enable Tx Buffer Cancellation Finished Interrupt to set TCF flag in IR register,
  2393. but interrupt will only occur if TCF is enabled in IE register */
  2394. SET_BIT(hfdcan->Instance->TXBCIE, BufferIndexes);
  2395. }
  2396. /* Enable the selected interrupts */
  2397. __HAL_FDCAN_ENABLE_IT(hfdcan, ActiveITs);
  2398. /* Return function status */
  2399. return HAL_OK;
  2400. }
  2401. else
  2402. {
  2403. /* Update error code */
  2404. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  2405. return HAL_ERROR;
  2406. }
  2407. }
  2408. /**
  2409. * @brief Disable interrupts.
  2410. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2411. * the configuration information for the specified FDCAN.
  2412. * @param InactiveITs indicates which interrupts will be disabled.
  2413. * This parameter can be any combination of @arg FDCAN_Interrupts.
  2414. * @retval HAL status
  2415. */
  2416. HAL_StatusTypeDef HAL_FDCAN_DeactivateNotification(FDCAN_HandleTypeDef *hfdcan, uint32_t InactiveITs)
  2417. {
  2418. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  2419. uint32_t ITs_enabled;
  2420. uint32_t ITs_lines_selection;
  2421. /* Check function parameters */
  2422. assert_param(IS_FDCAN_IT(InactiveITs));
  2423. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  2424. {
  2425. /* Disable the selected interrupts */
  2426. __HAL_FDCAN_DISABLE_IT(hfdcan, InactiveITs);
  2427. if ((InactiveITs & FDCAN_IT_TX_COMPLETE) != 0U)
  2428. {
  2429. /* Disable Tx Buffer Transmission Interrupts */
  2430. CLEAR_REG(hfdcan->Instance->TXBTIE);
  2431. }
  2432. if ((InactiveITs & FDCAN_IT_TX_ABORT_COMPLETE) != 0U)
  2433. {
  2434. /* Disable Tx Buffer Cancellation Finished Interrupt */
  2435. CLEAR_REG(hfdcan->Instance->TXBCIE);
  2436. }
  2437. /* Get interrupts enabled and interrupts line selection */
  2438. ITs_enabled = hfdcan->Instance->IE;
  2439. ITs_lines_selection = hfdcan->Instance->ILS;
  2440. /* Check if some interrupts are still enabled on interrupt line 0 */
  2441. if ((((ITs_enabled & FDCAN_IT_LIST_RX_FIFO0) != 0U)
  2442. && (((ITs_lines_selection) & FDCAN_IT_GROUP_RX_FIFO0) == 0U)) || \
  2443. (((ITs_enabled & FDCAN_IT_LIST_RX_FIFO1) != 0U)
  2444. && (((ITs_lines_selection) & FDCAN_IT_GROUP_RX_FIFO1) == 0U)) || \
  2445. (((ITs_enabled & FDCAN_IT_LIST_SMSG) != 0U)
  2446. && (((ITs_lines_selection) & FDCAN_IT_GROUP_SMSG) == 0U)) || \
  2447. (((ITs_enabled & FDCAN_IT_LIST_TX_FIFO_ERROR) != 0U)
  2448. && (((ITs_lines_selection) & FDCAN_IT_GROUP_TX_FIFO_ERROR) == 0U)) || \
  2449. (((ITs_enabled & FDCAN_IT_LIST_MISC) != 0U)
  2450. && (((ITs_lines_selection) & FDCAN_IT_GROUP_MISC) == 0U)) || \
  2451. (((ITs_enabled & FDCAN_IT_LIST_BIT_LINE_ERROR) != 0U)
  2452. && (((ITs_lines_selection) & FDCAN_IT_GROUP_BIT_LINE_ERROR) == 0U)) || \
  2453. (((ITs_enabled & FDCAN_IT_LIST_PROTOCOL_ERROR) != 0U)
  2454. && (((ITs_lines_selection) & FDCAN_IT_GROUP_PROTOCOL_ERROR) == 0U)))
  2455. {
  2456. /* Do nothing */
  2457. }
  2458. else /* no more interrupts enabled on interrupt line 0 */
  2459. {
  2460. /* Disable interrupt line 0 */
  2461. CLEAR_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE0);
  2462. }
  2463. /* Check if some interrupts are still enabled on interrupt line 1 */
  2464. if ((((ITs_enabled & FDCAN_IT_LIST_RX_FIFO0) != 0U)
  2465. && (((ITs_lines_selection) & FDCAN_IT_GROUP_RX_FIFO0) != 0U)) || \
  2466. (((ITs_enabled & FDCAN_IT_LIST_RX_FIFO1) != 0U)
  2467. && (((ITs_lines_selection) & FDCAN_IT_GROUP_RX_FIFO1) != 0U)) || \
  2468. (((ITs_enabled & FDCAN_IT_LIST_SMSG) != 0U)
  2469. && (((ITs_lines_selection) & FDCAN_IT_GROUP_SMSG) != 0U)) || \
  2470. (((ITs_enabled & FDCAN_IT_LIST_TX_FIFO_ERROR) != 0U)
  2471. && (((ITs_lines_selection) & FDCAN_IT_GROUP_TX_FIFO_ERROR) != 0U)) || \
  2472. (((ITs_enabled & FDCAN_IT_LIST_MISC) != 0U)
  2473. && (((ITs_lines_selection) & FDCAN_IT_GROUP_MISC) != 0U)) || \
  2474. (((ITs_enabled & FDCAN_IT_LIST_BIT_LINE_ERROR) != 0U)
  2475. && (((ITs_lines_selection) & FDCAN_IT_GROUP_BIT_LINE_ERROR) != 0U)) || \
  2476. (((ITs_enabled & FDCAN_IT_LIST_PROTOCOL_ERROR) != 0U)
  2477. && (((ITs_lines_selection) & FDCAN_IT_GROUP_PROTOCOL_ERROR) != 0U)))
  2478. {
  2479. /* Do nothing */
  2480. }
  2481. else /* no more interrupts enabled on interrupt line 1 */
  2482. {
  2483. /* Disable interrupt line 1 */
  2484. CLEAR_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE1);
  2485. }
  2486. /* Return function status */
  2487. return HAL_OK;
  2488. }
  2489. else
  2490. {
  2491. /* Update error code */
  2492. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  2493. return HAL_ERROR;
  2494. }
  2495. }
  2496. /**
  2497. * @brief Handles FDCAN interrupt request.
  2498. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2499. * the configuration information for the specified FDCAN.
  2500. * @retval HAL status
  2501. */
  2502. void HAL_FDCAN_IRQHandler(FDCAN_HandleTypeDef *hfdcan)
  2503. {
  2504. uint32_t TxEventFifoITs;
  2505. uint32_t RxFifo0ITs;
  2506. uint32_t RxFifo1ITs;
  2507. uint32_t Errors;
  2508. uint32_t ErrorStatusITs;
  2509. uint32_t TransmittedBuffers;
  2510. uint32_t AbortedBuffers;
  2511. uint32_t itsource;
  2512. uint32_t itflag;
  2513. TxEventFifoITs = hfdcan->Instance->IR & FDCAN_TX_EVENT_FIFO_MASK;
  2514. TxEventFifoITs &= hfdcan->Instance->IE;
  2515. RxFifo0ITs = hfdcan->Instance->IR & FDCAN_RX_FIFO0_MASK;
  2516. RxFifo0ITs &= hfdcan->Instance->IE;
  2517. RxFifo1ITs = hfdcan->Instance->IR & FDCAN_RX_FIFO1_MASK;
  2518. RxFifo1ITs &= hfdcan->Instance->IE;
  2519. Errors = hfdcan->Instance->IR & FDCAN_ERROR_MASK;
  2520. Errors &= hfdcan->Instance->IE;
  2521. ErrorStatusITs = hfdcan->Instance->IR & FDCAN_ERROR_STATUS_MASK;
  2522. ErrorStatusITs &= hfdcan->Instance->IE;
  2523. itsource = hfdcan->Instance->IE;
  2524. itflag = hfdcan->Instance->IR;
  2525. /* High Priority Message interrupt management *******************************/
  2526. if (FDCAN_CHECK_FLAG(itflag, FDCAN_FLAG_RX_HIGH_PRIORITY_MSG) != RESET)
  2527. {
  2528. if (FDCAN_CHECK_IT_SOURCE(itsource, FDCAN_IT_RX_HIGH_PRIORITY_MSG) != RESET)
  2529. {
  2530. /* Clear the High Priority Message flag */
  2531. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_RX_HIGH_PRIORITY_MSG);
  2532. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2533. /* Call registered callback*/
  2534. hfdcan->HighPriorityMessageCallback(hfdcan);
  2535. #else
  2536. /* High Priority Message Callback */
  2537. HAL_FDCAN_HighPriorityMessageCallback(hfdcan);
  2538. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2539. }
  2540. }
  2541. /* Transmission Abort interrupt management **********************************/
  2542. if (FDCAN_CHECK_FLAG(itflag, FDCAN_FLAG_TX_ABORT_COMPLETE) != RESET)
  2543. {
  2544. if (FDCAN_CHECK_IT_SOURCE(itsource, FDCAN_IT_TX_ABORT_COMPLETE) != RESET)
  2545. {
  2546. /* List of aborted monitored buffers */
  2547. AbortedBuffers = hfdcan->Instance->TXBCF;
  2548. AbortedBuffers &= hfdcan->Instance->TXBCIE;
  2549. /* Clear the Transmission Cancellation flag */
  2550. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TX_ABORT_COMPLETE);
  2551. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2552. /* Call registered callback*/
  2553. hfdcan->TxBufferAbortCallback(hfdcan, AbortedBuffers);
  2554. #else
  2555. /* Transmission Cancellation Callback */
  2556. HAL_FDCAN_TxBufferAbortCallback(hfdcan, AbortedBuffers);
  2557. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2558. }
  2559. }
  2560. /* Tx event FIFO interrupts management **************************************/
  2561. if (TxEventFifoITs != 0U)
  2562. {
  2563. /* Clear the Tx Event FIFO flags */
  2564. __HAL_FDCAN_CLEAR_FLAG(hfdcan, TxEventFifoITs);
  2565. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2566. /* Call registered callback*/
  2567. hfdcan->TxEventFifoCallback(hfdcan, TxEventFifoITs);
  2568. #else
  2569. /* Tx Event FIFO Callback */
  2570. HAL_FDCAN_TxEventFifoCallback(hfdcan, TxEventFifoITs);
  2571. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2572. }
  2573. /* Rx FIFO 0 interrupts management ******************************************/
  2574. if (RxFifo0ITs != 0U)
  2575. {
  2576. /* Clear the Rx FIFO 0 flags */
  2577. __HAL_FDCAN_CLEAR_FLAG(hfdcan, RxFifo0ITs);
  2578. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2579. /* Call registered callback*/
  2580. hfdcan->RxFifo0Callback(hfdcan, RxFifo0ITs);
  2581. #else
  2582. /* Rx FIFO 0 Callback */
  2583. HAL_FDCAN_RxFifo0Callback(hfdcan, RxFifo0ITs);
  2584. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2585. }
  2586. /* Rx FIFO 1 interrupts management ******************************************/
  2587. if (RxFifo1ITs != 0U)
  2588. {
  2589. /* Clear the Rx FIFO 1 flags */
  2590. __HAL_FDCAN_CLEAR_FLAG(hfdcan, RxFifo1ITs);
  2591. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2592. /* Call registered callback*/
  2593. hfdcan->RxFifo1Callback(hfdcan, RxFifo1ITs);
  2594. #else
  2595. /* Rx FIFO 1 Callback */
  2596. HAL_FDCAN_RxFifo1Callback(hfdcan, RxFifo1ITs);
  2597. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2598. }
  2599. /* Tx FIFO empty interrupt management ***************************************/
  2600. if (FDCAN_CHECK_FLAG(itflag, FDCAN_FLAG_TX_FIFO_EMPTY) != RESET)
  2601. {
  2602. if (FDCAN_CHECK_IT_SOURCE(itsource, FDCAN_IT_TX_FIFO_EMPTY) != RESET)
  2603. {
  2604. /* Clear the Tx FIFO empty flag */
  2605. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TX_FIFO_EMPTY);
  2606. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2607. /* Call registered callback*/
  2608. hfdcan->TxFifoEmptyCallback(hfdcan);
  2609. #else
  2610. /* Tx FIFO empty Callback */
  2611. HAL_FDCAN_TxFifoEmptyCallback(hfdcan);
  2612. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2613. }
  2614. }
  2615. /* Transmission Complete interrupt management *******************************/
  2616. if (FDCAN_CHECK_FLAG(itflag, FDCAN_FLAG_TX_COMPLETE) != RESET)
  2617. {
  2618. if (FDCAN_CHECK_IT_SOURCE(itsource, FDCAN_IT_TX_COMPLETE) != RESET)
  2619. {
  2620. /* List of transmitted monitored buffers */
  2621. TransmittedBuffers = hfdcan->Instance->TXBTO;
  2622. TransmittedBuffers &= hfdcan->Instance->TXBTIE;
  2623. /* Clear the Transmission Complete flag */
  2624. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TX_COMPLETE);
  2625. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2626. /* Call registered callback*/
  2627. hfdcan->TxBufferCompleteCallback(hfdcan, TransmittedBuffers);
  2628. #else
  2629. /* Transmission Complete Callback */
  2630. HAL_FDCAN_TxBufferCompleteCallback(hfdcan, TransmittedBuffers);
  2631. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2632. }
  2633. }
  2634. /* Timestamp Wraparound interrupt management ********************************/
  2635. if (FDCAN_CHECK_FLAG(itflag, FDCAN_FLAG_TIMESTAMP_WRAPAROUND) != RESET)
  2636. {
  2637. if (FDCAN_CHECK_IT_SOURCE(itsource, FDCAN_IT_TIMESTAMP_WRAPAROUND) != RESET)
  2638. {
  2639. /* Clear the Timestamp Wraparound flag */
  2640. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TIMESTAMP_WRAPAROUND);
  2641. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2642. /* Call registered callback*/
  2643. hfdcan->TimestampWraparoundCallback(hfdcan);
  2644. #else
  2645. /* Timestamp Wraparound Callback */
  2646. HAL_FDCAN_TimestampWraparoundCallback(hfdcan);
  2647. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2648. }
  2649. }
  2650. /* Timeout Occurred interrupt management ************************************/
  2651. if (FDCAN_CHECK_FLAG(itflag, FDCAN_FLAG_TIMEOUT_OCCURRED) != RESET)
  2652. {
  2653. if (FDCAN_CHECK_IT_SOURCE(itsource, FDCAN_IT_TIMEOUT_OCCURRED) != RESET)
  2654. {
  2655. /* Clear the Timeout Occurred flag */
  2656. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TIMEOUT_OCCURRED);
  2657. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2658. /* Call registered callback*/
  2659. hfdcan->TimeoutOccurredCallback(hfdcan);
  2660. #else
  2661. /* Timeout Occurred Callback */
  2662. HAL_FDCAN_TimeoutOccurredCallback(hfdcan);
  2663. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2664. }
  2665. }
  2666. /* Message RAM access failure interrupt management **************************/
  2667. if (FDCAN_CHECK_FLAG(itflag, FDCAN_FLAG_RAM_ACCESS_FAILURE) != RESET)
  2668. {
  2669. if (FDCAN_CHECK_IT_SOURCE(itsource, FDCAN_IT_RAM_ACCESS_FAILURE) != RESET)
  2670. {
  2671. /* Clear the Message RAM access failure flag */
  2672. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_RAM_ACCESS_FAILURE);
  2673. /* Update error code */
  2674. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_RAM_ACCESS;
  2675. }
  2676. }
  2677. /* Error Status interrupts management ***************************************/
  2678. if (ErrorStatusITs != 0U)
  2679. {
  2680. /* Clear the Error flags */
  2681. __HAL_FDCAN_CLEAR_FLAG(hfdcan, ErrorStatusITs);
  2682. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2683. /* Call registered callback*/
  2684. hfdcan->ErrorStatusCallback(hfdcan, ErrorStatusITs);
  2685. #else
  2686. /* Error Status Callback */
  2687. HAL_FDCAN_ErrorStatusCallback(hfdcan, ErrorStatusITs);
  2688. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2689. }
  2690. /* Error interrupts management **********************************************/
  2691. if (Errors != 0U)
  2692. {
  2693. /* Clear the Error flags */
  2694. __HAL_FDCAN_CLEAR_FLAG(hfdcan, Errors);
  2695. /* Update error code */
  2696. hfdcan->ErrorCode |= Errors;
  2697. }
  2698. if (hfdcan->ErrorCode != HAL_FDCAN_ERROR_NONE)
  2699. {
  2700. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  2701. /* Call registered callback*/
  2702. hfdcan->ErrorCallback(hfdcan);
  2703. #else
  2704. /* Error Callback */
  2705. HAL_FDCAN_ErrorCallback(hfdcan);
  2706. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  2707. }
  2708. }
  2709. /**
  2710. * @}
  2711. */
  2712. /** @defgroup FDCAN_Exported_Functions_Group5 Callback functions
  2713. * @brief FDCAN Callback functions
  2714. *
  2715. @verbatim
  2716. ==============================================================================
  2717. ##### Callback functions #####
  2718. ==============================================================================
  2719. [..]
  2720. This subsection provides the following callback functions:
  2721. (+) HAL_FDCAN_TxEventFifoCallback
  2722. (+) HAL_FDCAN_RxFifo0Callback
  2723. (+) HAL_FDCAN_RxFifo1Callback
  2724. (+) HAL_FDCAN_TxFifoEmptyCallback
  2725. (+) HAL_FDCAN_TxBufferCompleteCallback
  2726. (+) HAL_FDCAN_TxBufferAbortCallback
  2727. (+) HAL_FDCAN_HighPriorityMessageCallback
  2728. (+) HAL_FDCAN_TimestampWraparoundCallback
  2729. (+) HAL_FDCAN_TimeoutOccurredCallback
  2730. (+) HAL_FDCAN_ErrorCallback
  2731. (+) HAL_FDCAN_ErrorStatusCallback
  2732. @endverbatim
  2733. * @{
  2734. */
  2735. /**
  2736. * @brief Tx Event callback.
  2737. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2738. * the configuration information for the specified FDCAN.
  2739. * @param TxEventFifoITs indicates which Tx Event FIFO interrupts are signaled.
  2740. * This parameter can be any combination of @arg FDCAN_Tx_Event_Fifo_Interrupts.
  2741. * @retval None
  2742. */
  2743. __weak void HAL_FDCAN_TxEventFifoCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t TxEventFifoITs)
  2744. {
  2745. /* Prevent unused argument(s) compilation warning */
  2746. UNUSED(hfdcan);
  2747. UNUSED(TxEventFifoITs);
  2748. /* NOTE: This function Should not be modified, when the callback is needed,
  2749. the HAL_FDCAN_TxEventFifoCallback could be implemented in the user file
  2750. */
  2751. }
  2752. /**
  2753. * @brief Rx FIFO 0 callback.
  2754. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2755. * the configuration information for the specified FDCAN.
  2756. * @param RxFifo0ITs indicates which Rx FIFO 0 interrupts are signaled.
  2757. * This parameter can be any combination of @arg FDCAN_Rx_Fifo0_Interrupts.
  2758. * @retval None
  2759. */
  2760. __weak void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
  2761. {
  2762. /* Prevent unused argument(s) compilation warning */
  2763. UNUSED(hfdcan);
  2764. UNUSED(RxFifo0ITs);
  2765. /* NOTE: This function Should not be modified, when the callback is needed,
  2766. the HAL_FDCAN_RxFifo0Callback could be implemented in the user file
  2767. */
  2768. }
  2769. /**
  2770. * @brief Rx FIFO 1 callback.
  2771. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2772. * the configuration information for the specified FDCAN.
  2773. * @param RxFifo1ITs indicates which Rx FIFO 1 interrupts are signaled.
  2774. * This parameter can be any combination of @arg FDCAN_Rx_Fifo1_Interrupts.
  2775. * @retval None
  2776. */
  2777. __weak void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo1ITs)
  2778. {
  2779. /* Prevent unused argument(s) compilation warning */
  2780. UNUSED(hfdcan);
  2781. UNUSED(RxFifo1ITs);
  2782. /* NOTE: This function Should not be modified, when the callback is needed,
  2783. the HAL_FDCAN_RxFifo1Callback could be implemented in the user file
  2784. */
  2785. }
  2786. /**
  2787. * @brief Tx FIFO Empty callback.
  2788. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2789. * the configuration information for the specified FDCAN.
  2790. * @retval None
  2791. */
  2792. __weak void HAL_FDCAN_TxFifoEmptyCallback(FDCAN_HandleTypeDef *hfdcan)
  2793. {
  2794. /* Prevent unused argument(s) compilation warning */
  2795. UNUSED(hfdcan);
  2796. /* NOTE: This function Should not be modified, when the callback is needed,
  2797. the HAL_FDCAN_TxFifoEmptyCallback could be implemented in the user file
  2798. */
  2799. }
  2800. /**
  2801. * @brief Transmission Complete callback.
  2802. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2803. * the configuration information for the specified FDCAN.
  2804. * @param BufferIndexes Indexes of the transmitted buffers.
  2805. * This parameter can be any combination of @arg FDCAN_Tx_location.
  2806. * @retval None
  2807. */
  2808. __weak void HAL_FDCAN_TxBufferCompleteCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t BufferIndexes)
  2809. {
  2810. /* Prevent unused argument(s) compilation warning */
  2811. UNUSED(hfdcan);
  2812. UNUSED(BufferIndexes);
  2813. /* NOTE: This function Should not be modified, when the callback is needed,
  2814. the HAL_FDCAN_TxBufferCompleteCallback could be implemented in the user file
  2815. */
  2816. }
  2817. /**
  2818. * @brief Transmission Cancellation callback.
  2819. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2820. * the configuration information for the specified FDCAN.
  2821. * @param BufferIndexes Indexes of the aborted buffers.
  2822. * This parameter can be any combination of @arg FDCAN_Tx_location.
  2823. * @retval None
  2824. */
  2825. __weak void HAL_FDCAN_TxBufferAbortCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t BufferIndexes)
  2826. {
  2827. /* Prevent unused argument(s) compilation warning */
  2828. UNUSED(hfdcan);
  2829. UNUSED(BufferIndexes);
  2830. /* NOTE: This function Should not be modified, when the callback is needed,
  2831. the HAL_FDCAN_TxBufferAbortCallback could be implemented in the user file
  2832. */
  2833. }
  2834. /**
  2835. * @brief Timestamp Wraparound callback.
  2836. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2837. * the configuration information for the specified FDCAN.
  2838. * @retval None
  2839. */
  2840. __weak void HAL_FDCAN_TimestampWraparoundCallback(FDCAN_HandleTypeDef *hfdcan)
  2841. {
  2842. /* Prevent unused argument(s) compilation warning */
  2843. UNUSED(hfdcan);
  2844. /* NOTE: This function Should not be modified, when the callback is needed,
  2845. the HAL_FDCAN_TimestampWraparoundCallback could be implemented in the user file
  2846. */
  2847. }
  2848. /**
  2849. * @brief Timeout Occurred callback.
  2850. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2851. * the configuration information for the specified FDCAN.
  2852. * @retval None
  2853. */
  2854. __weak void HAL_FDCAN_TimeoutOccurredCallback(FDCAN_HandleTypeDef *hfdcan)
  2855. {
  2856. /* Prevent unused argument(s) compilation warning */
  2857. UNUSED(hfdcan);
  2858. /* NOTE: This function Should not be modified, when the callback is needed,
  2859. the HAL_FDCAN_TimeoutOccurredCallback could be implemented in the user file
  2860. */
  2861. }
  2862. /**
  2863. * @brief High Priority Message callback.
  2864. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2865. * the configuration information for the specified FDCAN.
  2866. * @retval None
  2867. */
  2868. __weak void HAL_FDCAN_HighPriorityMessageCallback(FDCAN_HandleTypeDef *hfdcan)
  2869. {
  2870. /* Prevent unused argument(s) compilation warning */
  2871. UNUSED(hfdcan);
  2872. /* NOTE: This function Should not be modified, when the callback is needed,
  2873. the HAL_FDCAN_HighPriorityMessageCallback could be implemented in the user file
  2874. */
  2875. }
  2876. /**
  2877. * @brief Error callback.
  2878. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2879. * the configuration information for the specified FDCAN.
  2880. * @retval None
  2881. */
  2882. __weak void HAL_FDCAN_ErrorCallback(FDCAN_HandleTypeDef *hfdcan)
  2883. {
  2884. /* Prevent unused argument(s) compilation warning */
  2885. UNUSED(hfdcan);
  2886. /* NOTE: This function Should not be modified, when the callback is needed,
  2887. the HAL_FDCAN_ErrorCallback could be implemented in the user file
  2888. */
  2889. }
  2890. /**
  2891. * @brief Error status callback.
  2892. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2893. * the configuration information for the specified FDCAN.
  2894. * @param ErrorStatusITs indicates which Error Status interrupts are signaled.
  2895. * This parameter can be any combination of @arg FDCAN_Error_Status_Interrupts.
  2896. * @retval None
  2897. */
  2898. __weak void HAL_FDCAN_ErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t ErrorStatusITs)
  2899. {
  2900. /* Prevent unused argument(s) compilation warning */
  2901. UNUSED(hfdcan);
  2902. UNUSED(ErrorStatusITs);
  2903. /* NOTE: This function Should not be modified, when the callback is needed,
  2904. the HAL_FDCAN_ErrorStatusCallback could be implemented in the user file
  2905. */
  2906. }
  2907. /**
  2908. * @}
  2909. */
  2910. /** @defgroup FDCAN_Exported_Functions_Group6 Peripheral State functions
  2911. * @brief FDCAN Peripheral State functions
  2912. *
  2913. @verbatim
  2914. ==============================================================================
  2915. ##### Peripheral State functions #####
  2916. ==============================================================================
  2917. [..]
  2918. This subsection provides functions allowing to :
  2919. (+) HAL_FDCAN_GetState() : Return the FDCAN state.
  2920. (+) HAL_FDCAN_GetError() : Return the FDCAN error code if any.
  2921. @endverbatim
  2922. * @{
  2923. */
  2924. /**
  2925. * @brief Return the FDCAN state
  2926. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2927. * the configuration information for the specified FDCAN.
  2928. * @retval HAL state
  2929. */
  2930. HAL_FDCAN_StateTypeDef HAL_FDCAN_GetState(const FDCAN_HandleTypeDef *hfdcan)
  2931. {
  2932. /* Return FDCAN state */
  2933. return hfdcan->State;
  2934. }
  2935. /**
  2936. * @brief Return the FDCAN error code
  2937. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2938. * the configuration information for the specified FDCAN.
  2939. * @retval FDCAN Error Code
  2940. */
  2941. uint32_t HAL_FDCAN_GetError(const FDCAN_HandleTypeDef *hfdcan)
  2942. {
  2943. /* Return FDCAN error code */
  2944. return hfdcan->ErrorCode;
  2945. }
  2946. /**
  2947. * @}
  2948. */
  2949. /**
  2950. * @}
  2951. */
  2952. /** @defgroup FDCAN_Private_Functions FDCAN Private Functions
  2953. * @{
  2954. */
  2955. /**
  2956. * @brief Calculate each RAM block start address and size
  2957. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2958. * the configuration information for the specified FDCAN.
  2959. * @retval none
  2960. */
  2961. static void FDCAN_CalcultateRamBlockAddresses(FDCAN_HandleTypeDef *hfdcan)
  2962. {
  2963. uint32_t RAMcounter;
  2964. uint32_t SramCanInstanceBase = SRAMCAN_BASE;
  2965. #if defined(FDCAN2)
  2966. if (hfdcan->Instance == FDCAN2)
  2967. {
  2968. SramCanInstanceBase += SRAMCAN_SIZE;
  2969. }
  2970. #endif /* FDCAN2 */
  2971. /* Standard filter list start address */
  2972. hfdcan->msgRam.StandardFilterSA = SramCanInstanceBase + SRAMCAN_FLSSA;
  2973. /* Standard filter elements number */
  2974. MODIFY_REG(hfdcan->Instance->RXGFC, FDCAN_RXGFC_LSS, (hfdcan->Init.StdFiltersNbr << FDCAN_RXGFC_LSS_Pos));
  2975. /* Extended filter list start address */
  2976. hfdcan->msgRam.ExtendedFilterSA = SramCanInstanceBase + SRAMCAN_FLESA;
  2977. /* Extended filter elements number */
  2978. MODIFY_REG(hfdcan->Instance->RXGFC, FDCAN_RXGFC_LSE, (hfdcan->Init.ExtFiltersNbr << FDCAN_RXGFC_LSE_Pos));
  2979. /* Rx FIFO 0 start address */
  2980. hfdcan->msgRam.RxFIFO0SA = SramCanInstanceBase + SRAMCAN_RF0SA;
  2981. /* Rx FIFO 1 start address */
  2982. hfdcan->msgRam.RxFIFO1SA = SramCanInstanceBase + SRAMCAN_RF1SA;
  2983. /* Tx event FIFO start address */
  2984. hfdcan->msgRam.TxEventFIFOSA = SramCanInstanceBase + SRAMCAN_TEFSA;
  2985. /* Tx FIFO/queue start address */
  2986. hfdcan->msgRam.TxFIFOQSA = SramCanInstanceBase + SRAMCAN_TFQSA;
  2987. /* Flush the allocated Message RAM area */
  2988. for (RAMcounter = SramCanInstanceBase; RAMcounter < (SramCanInstanceBase + SRAMCAN_SIZE); RAMcounter += 4U)
  2989. {
  2990. *(uint32_t *)(RAMcounter) = 0x00000000U;
  2991. }
  2992. }
  2993. /**
  2994. * @brief Copy Tx message to the message RAM.
  2995. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2996. * the configuration information for the specified FDCAN.
  2997. * @param pTxHeader pointer to a FDCAN_TxHeaderTypeDef structure.
  2998. * @param pTxData pointer to a buffer containing the payload of the Tx frame.
  2999. * @param BufferIndex index of the buffer to be configured.
  3000. * @retval none
  3001. */
  3002. static void FDCAN_CopyMessageToRAM(const FDCAN_HandleTypeDef *hfdcan, const FDCAN_TxHeaderTypeDef *pTxHeader,
  3003. const uint8_t *pTxData, uint32_t BufferIndex)
  3004. {
  3005. uint32_t TxElementW1;
  3006. uint32_t TxElementW2;
  3007. uint32_t *TxAddress;
  3008. uint32_t ByteCounter;
  3009. /* Build first word of Tx header element */
  3010. if (pTxHeader->IdType == FDCAN_STANDARD_ID)
  3011. {
  3012. TxElementW1 = (pTxHeader->ErrorStateIndicator |
  3013. FDCAN_STANDARD_ID |
  3014. pTxHeader->TxFrameType |
  3015. (pTxHeader->Identifier << 18U));
  3016. }
  3017. else /* pTxHeader->IdType == FDCAN_EXTENDED_ID */
  3018. {
  3019. TxElementW1 = (pTxHeader->ErrorStateIndicator |
  3020. FDCAN_EXTENDED_ID |
  3021. pTxHeader->TxFrameType |
  3022. pTxHeader->Identifier);
  3023. }
  3024. /* Build second word of Tx header element */
  3025. TxElementW2 = ((pTxHeader->MessageMarker << 24U) |
  3026. pTxHeader->TxEventFifoControl |
  3027. pTxHeader->FDFormat |
  3028. pTxHeader->BitRateSwitch |
  3029. (pTxHeader->DataLength << 16U));
  3030. /* Calculate Tx element address */
  3031. TxAddress = (uint32_t *)(hfdcan->msgRam.TxFIFOQSA + (BufferIndex * SRAMCAN_TFQ_SIZE));
  3032. /* Write Tx element header to the message RAM */
  3033. *TxAddress = TxElementW1;
  3034. TxAddress++;
  3035. *TxAddress = TxElementW2;
  3036. TxAddress++;
  3037. /* Write Tx payload to the message RAM */
  3038. for (ByteCounter = 0; ByteCounter < DLCtoBytes[pTxHeader->DataLength]; ByteCounter += 4U)
  3039. {
  3040. *TxAddress = (((uint32_t)pTxData[ByteCounter + 3U] << 24U) |
  3041. ((uint32_t)pTxData[ByteCounter + 2U] << 16U) |
  3042. ((uint32_t)pTxData[ByteCounter + 1U] << 8U) |
  3043. (uint32_t)pTxData[ByteCounter]);
  3044. TxAddress++;
  3045. }
  3046. }
  3047. /**
  3048. * @}
  3049. */
  3050. #endif /* HAL_FDCAN_MODULE_ENABLED */
  3051. /**
  3052. * @}
  3053. */
  3054. /**
  3055. * @}
  3056. */
  3057. #endif /* FDCAN1 */