stm32l5xx_hal_fdcan.c 122 KB

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