stm32g0xx_hal_smartcard.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_hal_smartcard.c
  4. * @author MCD Application Team
  5. * @brief SMARTCARD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the SMARTCARD peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Error functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2018 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The SMARTCARD HAL driver can be used as follows:
  30. (#) Declare a SMARTCARD_HandleTypeDef handle structure (eg. SMARTCARD_HandleTypeDef hsmartcard).
  31. (#) Associate a USART to the SMARTCARD handle hsmartcard.
  32. (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  33. (++) Enable the USARTx interface clock.
  34. (++) USART pins configuration:
  35. (+++) Enable the clock for the USART GPIOs.
  36. (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
  37. (++) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  38. and HAL_SMARTCARD_Receive_IT() APIs):
  39. (+++) Configure the USARTx interrupt priority.
  40. (+++) Enable the NVIC USART IRQ handle.
  41. (++) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  42. and HAL_SMARTCARD_Receive_DMA() APIs):
  43. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  44. (+++) Enable the DMAx interface clock.
  45. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  46. (+++) Configure the DMA Tx/Rx channel.
  47. (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  48. (+++) Configure the priority and enable the NVIC for the transfer complete
  49. interrupt on the DMA Tx/Rx channel.
  50. (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
  51. the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
  52. error enabling or disabling in the hsmartcard handle Init structure.
  53. (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
  54. in the hsmartcard handle AdvancedInit structure.
  55. (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
  56. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  57. by calling the customized HAL_SMARTCARD_MspInit() API.
  58. [..]
  59. (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
  60. RXNE interrupt and Error Interrupts) will be managed using the macros
  61. __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
  62. [..]
  63. [..] Three operation modes are available within this driver :
  64. *** Polling mode IO operation ***
  65. =================================
  66. [..]
  67. (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  68. (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  69. *** Interrupt mode IO operation ***
  70. ===================================
  71. [..]
  72. (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT()
  73. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
  74. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
  75. (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT()
  76. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
  77. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
  78. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  79. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
  80. *** DMA mode IO operation ***
  81. ==============================
  82. [..]
  83. (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  84. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
  85. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
  86. (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  87. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
  88. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
  89. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  90. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
  91. *** SMARTCARD HAL driver macros list ***
  92. ========================================
  93. [..]
  94. Below the list of most used macros in SMARTCARD HAL driver.
  95. (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set
  96. (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
  97. (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
  98. (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
  99. (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled
  100. [..]
  101. (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
  102. ##### Callback registration #####
  103. ==================================
  104. [..]
  105. The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
  106. allows the user to configure dynamically the driver callbacks.
  107. [..]
  108. Use Function HAL_SMARTCARD_RegisterCallback() to register a user callback.
  109. Function HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
  110. (+) TxCpltCallback : Tx Complete Callback.
  111. (+) RxCpltCallback : Rx Complete Callback.
  112. (+) ErrorCallback : Error Callback.
  113. (+) AbortCpltCallback : Abort Complete Callback.
  114. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  115. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  116. (+) RxFifoFullCallback : Rx Fifo Full Callback.
  117. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  118. (+) MspInitCallback : SMARTCARD MspInit.
  119. (+) MspDeInitCallback : SMARTCARD MspDeInit.
  120. This function takes as parameters the HAL peripheral handle, the Callback ID
  121. and a pointer to the user callback function.
  122. [..]
  123. Use function HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
  124. weak function.
  125. HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  126. and the Callback ID.
  127. This function allows to reset following callbacks:
  128. (+) TxCpltCallback : Tx Complete Callback.
  129. (+) RxCpltCallback : Rx Complete Callback.
  130. (+) ErrorCallback : Error Callback.
  131. (+) AbortCpltCallback : Abort Complete Callback.
  132. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  133. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  134. (+) RxFifoFullCallback : Rx Fifo Full Callback.
  135. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  136. (+) MspInitCallback : SMARTCARD MspInit.
  137. (+) MspDeInitCallback : SMARTCARD MspDeInit.
  138. [..]
  139. By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
  140. all callbacks are set to the corresponding weak functions:
  141. examples HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback().
  142. Exception done for MspInit and MspDeInit functions that are respectively
  143. reset to the legacy weak functions in the HAL_SMARTCARD_Init()
  144. and HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
  145. If not, MspInit or MspDeInit are not null, the HAL_SMARTCARD_Init() and HAL_SMARTCARD_DeInit()
  146. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  147. [..]
  148. Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
  149. Exception done MspInit/MspDeInit that can be registered/unregistered
  150. in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
  151. MspInit/DeInit callbacks can be used during the Init/DeInit.
  152. In that case first register the MspInit/MspDeInit user callbacks
  153. using HAL_SMARTCARD_RegisterCallback() before calling HAL_SMARTCARD_DeInit()
  154. or HAL_SMARTCARD_Init() function.
  155. [..]
  156. When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
  157. not defined, the callback registration feature is not available
  158. and weak callbacks are used.
  159. @endverbatim
  160. ******************************************************************************
  161. */
  162. /* Includes ------------------------------------------------------------------*/
  163. #include "stm32g0xx_hal.h"
  164. /** @addtogroup STM32G0xx_HAL_Driver
  165. * @{
  166. */
  167. /** @defgroup SMARTCARD SMARTCARD
  168. * @brief HAL SMARTCARD module driver
  169. * @{
  170. */
  171. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  172. /* Private typedef -----------------------------------------------------------*/
  173. /* Private define ------------------------------------------------------------*/
  174. /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
  175. * @{
  176. */
  177. #define SMARTCARD_TEACK_REACK_TIMEOUT 1000U /*!< SMARTCARD TX or RX enable acknowledge time-out value */
  178. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  179. USART_CR1_RE | USART_CR1_OVER8| \
  180. USART_CR1_FIFOEN)) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
  181. #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
  182. USART_CR2_CPHA | USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
  183. #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_RTOEN | USART_CR2_CLK_FIELDS | \
  184. USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by SMARTCARD_SetConfig API */
  185. #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | USART_CR3_SCARCNT | \
  186. USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
  187. #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */
  188. #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */
  189. /**
  190. * @}
  191. */
  192. /* Private macros ------------------------------------------------------------*/
  193. /* Private variables ---------------------------------------------------------*/
  194. /* Private function prototypes -----------------------------------------------*/
  195. /** @addtogroup SMARTCARD_Private_Functions
  196. * @{
  197. */
  198. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  199. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard);
  200. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  201. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  202. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  203. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard);
  204. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
  205. FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  206. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  207. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  208. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  209. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  210. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  211. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  212. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  213. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  214. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  215. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  216. static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard);
  217. static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard);
  218. static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  219. static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard);
  220. static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard);
  221. /**
  222. * @}
  223. */
  224. /* Exported functions --------------------------------------------------------*/
  225. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  226. * @{
  227. */
  228. /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  229. * @brief Initialization and Configuration functions
  230. *
  231. @verbatim
  232. ==============================================================================
  233. ##### Initialization and Configuration functions #####
  234. ==============================================================================
  235. [..]
  236. This subsection provides a set of functions allowing to initialize the USARTx
  237. associated to the SmartCard.
  238. (+) These parameters can be configured:
  239. (++) Baud Rate
  240. (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity
  241. (++) Receiver/transmitter modes
  242. (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
  243. (++) Prescaler value
  244. (++) Guard bit time
  245. (++) NACK enabling or disabling on transmission error
  246. (+) The following advanced features can be configured as well:
  247. (++) TX and/or RX pin level inversion
  248. (++) data logical level inversion
  249. (++) RX and TX pins swap
  250. (++) RX overrun detection disabling
  251. (++) DMA disabling on RX error
  252. (++) MSB first on communication line
  253. (++) Time out enabling (and if activated, timeout value)
  254. (++) Block length
  255. (++) Auto-retry counter
  256. [..]
  257. The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
  258. (details for the procedures are available in reference manual).
  259. @endverbatim
  260. The USART frame format is given in the following table:
  261. Table 1. USART frame format.
  262. +---------------------------------------------------------------+
  263. | M1M0 bits | PCE bit | USART frame |
  264. |-----------------------|---------------------------------------|
  265. | 01 | 1 | | SB | 8 bit data | PB | STB | |
  266. +---------------------------------------------------------------+
  267. * @{
  268. */
  269. /**
  270. * @brief Initialize the SMARTCARD mode according to the specified
  271. * parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
  272. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  273. * the configuration information for the specified SMARTCARD module.
  274. * @retval HAL status
  275. */
  276. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
  277. {
  278. /* Check the SMARTCARD handle allocation */
  279. if (hsmartcard == NULL)
  280. {
  281. return HAL_ERROR;
  282. }
  283. /* Check the USART associated to the SMARTCARD handle */
  284. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  285. if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
  286. {
  287. /* Allocate lock resource and initialize it */
  288. hsmartcard->Lock = HAL_UNLOCKED;
  289. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  290. SMARTCARD_InitCallbacksToDefault(hsmartcard);
  291. if (hsmartcard->MspInitCallback == NULL)
  292. {
  293. hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
  294. }
  295. /* Init the low level hardware */
  296. hsmartcard->MspInitCallback(hsmartcard);
  297. #else
  298. /* Init the low level hardware : GPIO, CLOCK */
  299. HAL_SMARTCARD_MspInit(hsmartcard);
  300. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  301. }
  302. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  303. /* Disable the Peripheral to set smartcard mode */
  304. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  305. /* In SmartCard mode, the following bits must be kept cleared:
  306. - LINEN in the USART_CR2 register,
  307. - HDSEL and IREN bits in the USART_CR3 register.*/
  308. CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN);
  309. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  310. /* set the USART in SMARTCARD mode */
  311. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN);
  312. /* Set the SMARTCARD Communication parameters */
  313. if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
  314. {
  315. return HAL_ERROR;
  316. }
  317. /* Set the SMARTCARD transmission completion indication */
  318. SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard);
  319. if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
  320. {
  321. SMARTCARD_AdvFeatureConfig(hsmartcard);
  322. }
  323. /* Enable the Peripheral */
  324. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  325. /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
  326. return (SMARTCARD_CheckIdleState(hsmartcard));
  327. }
  328. /**
  329. * @brief DeInitialize the SMARTCARD peripheral.
  330. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  331. * the configuration information for the specified SMARTCARD module.
  332. * @retval HAL status
  333. */
  334. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  335. {
  336. /* Check the SMARTCARD handle allocation */
  337. if (hsmartcard == NULL)
  338. {
  339. return HAL_ERROR;
  340. }
  341. /* Check the USART/UART associated to the SMARTCARD handle */
  342. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  343. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  344. /* Disable the Peripheral */
  345. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  346. WRITE_REG(hsmartcard->Instance->CR1, 0x0U);
  347. WRITE_REG(hsmartcard->Instance->CR2, 0x0U);
  348. WRITE_REG(hsmartcard->Instance->CR3, 0x0U);
  349. WRITE_REG(hsmartcard->Instance->RTOR, 0x0U);
  350. WRITE_REG(hsmartcard->Instance->GTPR, 0x0U);
  351. /* DeInit the low level hardware */
  352. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  353. if (hsmartcard->MspDeInitCallback == NULL)
  354. {
  355. hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  356. }
  357. /* DeInit the low level hardware */
  358. hsmartcard->MspDeInitCallback(hsmartcard);
  359. #else
  360. HAL_SMARTCARD_MspDeInit(hsmartcard);
  361. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  362. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  363. hsmartcard->gState = HAL_SMARTCARD_STATE_RESET;
  364. hsmartcard->RxState = HAL_SMARTCARD_STATE_RESET;
  365. /* Process Unlock */
  366. __HAL_UNLOCK(hsmartcard);
  367. return HAL_OK;
  368. }
  369. /**
  370. * @brief Initialize the SMARTCARD MSP.
  371. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  372. * the configuration information for the specified SMARTCARD module.
  373. * @retval None
  374. */
  375. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
  376. {
  377. /* Prevent unused argument(s) compilation warning */
  378. UNUSED(hsmartcard);
  379. /* NOTE : This function should not be modified, when the callback is needed,
  380. the HAL_SMARTCARD_MspInit can be implemented in the user file
  381. */
  382. }
  383. /**
  384. * @brief DeInitialize the SMARTCARD MSP.
  385. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  386. * the configuration information for the specified SMARTCARD module.
  387. * @retval None
  388. */
  389. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  390. {
  391. /* Prevent unused argument(s) compilation warning */
  392. UNUSED(hsmartcard);
  393. /* NOTE : This function should not be modified, when the callback is needed,
  394. the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  395. */
  396. }
  397. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  398. /**
  399. * @brief Register a User SMARTCARD Callback
  400. * To be used to override the weak predefined callback
  401. * @note The HAL_SMARTCARD_RegisterCallback() may be called before HAL_SMARTCARD_Init()
  402. * in HAL_SMARTCARD_STATE_RESET to register callbacks for HAL_SMARTCARD_MSPINIT_CB_ID
  403. * and HAL_SMARTCARD_MSPDEINIT_CB_ID
  404. * @param hsmartcard smartcard handle
  405. * @param CallbackID ID of the callback to be registered
  406. * This parameter can be one of the following values:
  407. * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  408. * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  409. * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  410. * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  411. * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  412. * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  413. * @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
  414. * @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
  415. * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  416. * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  417. * @param pCallback pointer to the Callback function
  418. * @retval HAL status
  419. */
  420. HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
  421. HAL_SMARTCARD_CallbackIDTypeDef CallbackID,
  422. pSMARTCARD_CallbackTypeDef pCallback)
  423. {
  424. HAL_StatusTypeDef status = HAL_OK;
  425. if (pCallback == NULL)
  426. {
  427. /* Update the error code */
  428. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  429. return HAL_ERROR;
  430. }
  431. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  432. {
  433. switch (CallbackID)
  434. {
  435. case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  436. hsmartcard->TxCpltCallback = pCallback;
  437. break;
  438. case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  439. hsmartcard->RxCpltCallback = pCallback;
  440. break;
  441. case HAL_SMARTCARD_ERROR_CB_ID :
  442. hsmartcard->ErrorCallback = pCallback;
  443. break;
  444. case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  445. hsmartcard->AbortCpltCallback = pCallback;
  446. break;
  447. case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  448. hsmartcard->AbortTransmitCpltCallback = pCallback;
  449. break;
  450. case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  451. hsmartcard->AbortReceiveCpltCallback = pCallback;
  452. break;
  453. case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID :
  454. hsmartcard->RxFifoFullCallback = pCallback;
  455. break;
  456. case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID :
  457. hsmartcard->TxFifoEmptyCallback = pCallback;
  458. break;
  459. case HAL_SMARTCARD_MSPINIT_CB_ID :
  460. hsmartcard->MspInitCallback = pCallback;
  461. break;
  462. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  463. hsmartcard->MspDeInitCallback = pCallback;
  464. break;
  465. default :
  466. /* Update the error code */
  467. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  468. /* Return error status */
  469. status = HAL_ERROR;
  470. break;
  471. }
  472. }
  473. else if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
  474. {
  475. switch (CallbackID)
  476. {
  477. case HAL_SMARTCARD_MSPINIT_CB_ID :
  478. hsmartcard->MspInitCallback = pCallback;
  479. break;
  480. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  481. hsmartcard->MspDeInitCallback = pCallback;
  482. break;
  483. default :
  484. /* Update the error code */
  485. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  486. /* Return error status */
  487. status = HAL_ERROR;
  488. break;
  489. }
  490. }
  491. else
  492. {
  493. /* Update the error code */
  494. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  495. /* Return error status */
  496. status = HAL_ERROR;
  497. }
  498. return status;
  499. }
  500. /**
  501. * @brief Unregister an SMARTCARD callback
  502. * SMARTCARD callback is redirected to the weak predefined callback
  503. * @note The HAL_SMARTCARD_UnRegisterCallback() may be called before HAL_SMARTCARD_Init()
  504. * in HAL_SMARTCARD_STATE_RESET to un-register callbacks for HAL_SMARTCARD_MSPINIT_CB_ID
  505. * and HAL_SMARTCARD_MSPDEINIT_CB_ID
  506. * @param hsmartcard smartcard handle
  507. * @param CallbackID ID of the callback to be unregistered
  508. * This parameter can be one of the following values:
  509. * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  510. * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  511. * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  512. * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  513. * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  514. * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  515. * @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
  516. * @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
  517. * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  518. * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  519. * @retval HAL status
  520. */
  521. HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
  522. HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
  523. {
  524. HAL_StatusTypeDef status = HAL_OK;
  525. if (HAL_SMARTCARD_STATE_READY == hsmartcard->gState)
  526. {
  527. switch (CallbackID)
  528. {
  529. case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  530. hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
  531. break;
  532. case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  533. hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
  534. break;
  535. case HAL_SMARTCARD_ERROR_CB_ID :
  536. hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
  537. break;
  538. case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  539. hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  540. break;
  541. case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  542. hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak
  543. AbortTransmitCpltCallback*/
  544. break;
  545. case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  546. hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak
  547. AbortReceiveCpltCallback */
  548. break;
  549. case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID :
  550. hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
  551. break;
  552. case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID :
  553. hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
  554. break;
  555. case HAL_SMARTCARD_MSPINIT_CB_ID :
  556. hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit; /* Legacy weak MspInitCallback */
  557. break;
  558. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  559. hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; /* Legacy weak MspDeInitCallback */
  560. break;
  561. default :
  562. /* Update the error code */
  563. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  564. /* Return error status */
  565. status = HAL_ERROR;
  566. break;
  567. }
  568. }
  569. else if (HAL_SMARTCARD_STATE_RESET == hsmartcard->gState)
  570. {
  571. switch (CallbackID)
  572. {
  573. case HAL_SMARTCARD_MSPINIT_CB_ID :
  574. hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
  575. break;
  576. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  577. hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  578. break;
  579. default :
  580. /* Update the error code */
  581. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  582. /* Return error status */
  583. status = HAL_ERROR;
  584. break;
  585. }
  586. }
  587. else
  588. {
  589. /* Update the error code */
  590. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  591. /* Return error status */
  592. status = HAL_ERROR;
  593. }
  594. return status;
  595. }
  596. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  597. /**
  598. * @}
  599. */
  600. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  601. * @brief SMARTCARD Transmit and Receive functions
  602. *
  603. @verbatim
  604. ==============================================================================
  605. ##### IO operation functions #####
  606. ==============================================================================
  607. [..]
  608. This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  609. [..]
  610. Smartcard is a single wire half duplex communication protocol.
  611. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  612. defined in the ISO 7816-3 standard. The USART should be configured as:
  613. (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  614. (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  615. [..]
  616. (#) There are two modes of transfer:
  617. (##) Blocking mode: The communication is performed in polling mode.
  618. The HAL status of all data processing is returned by the same function
  619. after finishing transfer.
  620. (##) Non-Blocking mode: The communication is performed using Interrupts
  621. or DMA, the relevant API's return the HAL status.
  622. The end of the data processing will be indicated through the
  623. dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  624. using DMA mode.
  625. (##) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  626. will be executed respectively at the end of the Transmit or Receive process
  627. The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
  628. error is detected.
  629. (#) Blocking mode APIs are :
  630. (##) HAL_SMARTCARD_Transmit()
  631. (##) HAL_SMARTCARD_Receive()
  632. (#) Non Blocking mode APIs with Interrupt are :
  633. (##) HAL_SMARTCARD_Transmit_IT()
  634. (##) HAL_SMARTCARD_Receive_IT()
  635. (##) HAL_SMARTCARD_IRQHandler()
  636. (#) Non Blocking mode functions with DMA are :
  637. (##) HAL_SMARTCARD_Transmit_DMA()
  638. (##) HAL_SMARTCARD_Receive_DMA()
  639. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  640. (##) HAL_SMARTCARD_TxCpltCallback()
  641. (##) HAL_SMARTCARD_RxCpltCallback()
  642. (##) HAL_SMARTCARD_ErrorCallback()
  643. [..]
  644. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  645. (##) HAL_SMARTCARD_Abort()
  646. (##) HAL_SMARTCARD_AbortTransmit()
  647. (##) HAL_SMARTCARD_AbortReceive()
  648. (##) HAL_SMARTCARD_Abort_IT()
  649. (##) HAL_SMARTCARD_AbortTransmit_IT()
  650. (##) HAL_SMARTCARD_AbortReceive_IT()
  651. (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT),
  652. a set of Abort Complete Callbacks are provided:
  653. (##) HAL_SMARTCARD_AbortCpltCallback()
  654. (##) HAL_SMARTCARD_AbortTransmitCpltCallback()
  655. (##) HAL_SMARTCARD_AbortReceiveCpltCallback()
  656. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  657. Errors are handled as follows :
  658. (##) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  659. to be evaluated by user : this concerns Frame Error,
  660. Parity Error or Noise Error in Interrupt mode reception .
  661. Received character is then retrieved and stored in Rx buffer,
  662. Error code is set to allow user to identify error type,
  663. and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
  664. If user wants to abort it, Abort services should be called by user.
  665. (##) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  666. This concerns Frame Error in Interrupt mode transmission, Overrun Error in Interrupt
  667. mode reception and all errors in DMA mode.
  668. Error code is set to allow user to identify error type,
  669. and HAL_SMARTCARD_ErrorCallback() user callback is executed.
  670. @endverbatim
  671. * @{
  672. */
  673. /**
  674. * @brief Send an amount of data in blocking mode.
  675. * @note When FIFO mode is enabled, writing a data in the TDR register adds one
  676. * data to the TXFIFO. Write operations to the TDR register are performed
  677. * when TXFNF flag is set. From hardware perspective, TXFNF flag and
  678. * TXE are mapped on the same bit-field.
  679. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  680. * the configuration information for the specified SMARTCARD module.
  681. * @param pData pointer to data buffer.
  682. * @param Size amount of data to be sent.
  683. * @param Timeout Timeout duration.
  684. * @retval HAL status
  685. */
  686. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size,
  687. uint32_t Timeout)
  688. {
  689. uint32_t tickstart;
  690. const uint8_t *ptmpdata = pData;
  691. /* Check that a Tx process is not already ongoing */
  692. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  693. {
  694. if ((ptmpdata == NULL) || (Size == 0U))
  695. {
  696. return HAL_ERROR;
  697. }
  698. /* Process Locked */
  699. __HAL_LOCK(hsmartcard);
  700. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  701. /* Init tickstart for timeout management */
  702. tickstart = HAL_GetTick();
  703. /* Disable the Peripheral first to update mode for TX master */
  704. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  705. /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
  706. the bidirectional line to detect a NACK signal in case of parity error.
  707. Therefore, the receiver block must be enabled as well (RE bit must be set). */
  708. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  709. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  710. {
  711. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  712. }
  713. /* Enable Tx */
  714. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  715. /* Enable the Peripheral */
  716. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  717. /* Perform a TX/RX FIFO Flush */
  718. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  719. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  720. hsmartcard->TxXferSize = Size;
  721. hsmartcard->TxXferCount = Size;
  722. while (hsmartcard->TxXferCount > 0U)
  723. {
  724. hsmartcard->TxXferCount--;
  725. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  726. {
  727. return HAL_TIMEOUT;
  728. }
  729. hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU);
  730. ptmpdata++;
  731. }
  732. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET,
  733. tickstart, Timeout) != HAL_OK)
  734. {
  735. return HAL_TIMEOUT;
  736. }
  737. /* Disable the Peripheral first to update mode */
  738. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  739. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  740. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  741. {
  742. /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
  743. for Transmit phase. Disable this receiver block. */
  744. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  745. }
  746. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  747. || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  748. {
  749. /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
  750. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  751. }
  752. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  753. /* At end of Tx process, restore hsmartcard->gState to Ready */
  754. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  755. /* Process Unlocked */
  756. __HAL_UNLOCK(hsmartcard);
  757. return HAL_OK;
  758. }
  759. else
  760. {
  761. return HAL_BUSY;
  762. }
  763. }
  764. /**
  765. * @brief Receive an amount of data in blocking mode.
  766. * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
  767. * is not empty. Read operations from the RDR register are performed when
  768. * RXFNE flag is set. From hardware perspective, RXFNE flag and
  769. * RXNE are mapped on the same bit-field.
  770. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  771. * the configuration information for the specified SMARTCARD module.
  772. * @param pData pointer to data buffer.
  773. * @param Size amount of data to be received.
  774. * @param Timeout Timeout duration.
  775. * @retval HAL status
  776. */
  777. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
  778. uint32_t Timeout)
  779. {
  780. uint32_t tickstart;
  781. uint8_t *ptmpdata = pData;
  782. /* Check that a Rx process is not already ongoing */
  783. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  784. {
  785. if ((ptmpdata == NULL) || (Size == 0U))
  786. {
  787. return HAL_ERROR;
  788. }
  789. /* Process Locked */
  790. __HAL_LOCK(hsmartcard);
  791. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  792. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  793. /* Init tickstart for timeout management */
  794. tickstart = HAL_GetTick();
  795. hsmartcard->RxXferSize = Size;
  796. hsmartcard->RxXferCount = Size;
  797. /* Check the remain data to be received */
  798. while (hsmartcard->RxXferCount > 0U)
  799. {
  800. hsmartcard->RxXferCount--;
  801. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  802. {
  803. return HAL_TIMEOUT;
  804. }
  805. *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
  806. ptmpdata++;
  807. }
  808. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  809. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  810. /* Process Unlocked */
  811. __HAL_UNLOCK(hsmartcard);
  812. return HAL_OK;
  813. }
  814. else
  815. {
  816. return HAL_BUSY;
  817. }
  818. }
  819. /**
  820. * @brief Send an amount of data in interrupt mode.
  821. * @note When FIFO mode is disabled, USART interrupt is generated whenever
  822. * USART_TDR register is empty, i.e one interrupt per data to transmit.
  823. * @note When FIFO mode is enabled, USART interrupt is generated whenever
  824. * TXFIFO threshold reached. In that case the interrupt rate depends on
  825. * TXFIFO threshold configuration.
  826. * @note This function sets the hsmartcard->TxIsr function pointer according to
  827. * the FIFO mode (data transmission processing depends on FIFO mode).
  828. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  829. * the configuration information for the specified SMARTCARD module.
  830. * @param pData pointer to data buffer.
  831. * @param Size amount of data to be sent.
  832. * @retval HAL status
  833. */
  834. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size)
  835. {
  836. /* Check that a Tx process is not already ongoing */
  837. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  838. {
  839. if ((pData == NULL) || (Size == 0U))
  840. {
  841. return HAL_ERROR;
  842. }
  843. /* Process Locked */
  844. __HAL_LOCK(hsmartcard);
  845. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  846. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  847. hsmartcard->pTxBuffPtr = pData;
  848. hsmartcard->TxXferSize = Size;
  849. hsmartcard->TxXferCount = Size;
  850. hsmartcard->TxISR = NULL;
  851. /* Disable the Peripheral first to update mode for TX master */
  852. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  853. /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
  854. the bidirectional line to detect a NACK signal in case of parity error.
  855. Therefore, the receiver block must be enabled as well (RE bit must be set). */
  856. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  857. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  858. {
  859. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  860. }
  861. /* Enable Tx */
  862. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  863. /* Enable the Peripheral */
  864. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  865. /* Perform a TX/RX FIFO Flush */
  866. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  867. /* Configure Tx interrupt processing */
  868. if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE)
  869. {
  870. /* Set the Tx ISR function pointer */
  871. hsmartcard->TxISR = SMARTCARD_TxISR_FIFOEN;
  872. /* Process Unlocked */
  873. __HAL_UNLOCK(hsmartcard);
  874. /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  875. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  876. /* Enable the TX FIFO threshold interrupt */
  877. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  878. }
  879. else
  880. {
  881. /* Set the Tx ISR function pointer */
  882. hsmartcard->TxISR = SMARTCARD_TxISR;
  883. /* Process Unlocked */
  884. __HAL_UNLOCK(hsmartcard);
  885. /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  886. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  887. /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
  888. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  889. }
  890. return HAL_OK;
  891. }
  892. else
  893. {
  894. return HAL_BUSY;
  895. }
  896. }
  897. /**
  898. * @brief Receive an amount of data in interrupt mode.
  899. * @note When FIFO mode is disabled, USART interrupt is generated whenever
  900. * USART_RDR register can be read, i.e one interrupt per data to receive.
  901. * @note When FIFO mode is enabled, USART interrupt is generated whenever
  902. * RXFIFO threshold reached. In that case the interrupt rate depends on
  903. * RXFIFO threshold configuration.
  904. * @note This function sets the hsmartcard->RxIsr function pointer according to
  905. * the FIFO mode (data reception processing depends on FIFO mode).
  906. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  907. * the configuration information for the specified SMARTCARD module.
  908. * @param pData pointer to data buffer.
  909. * @param Size amount of data to be received.
  910. * @retval HAL status
  911. */
  912. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  913. {
  914. /* Check that a Rx process is not already ongoing */
  915. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  916. {
  917. if ((pData == NULL) || (Size == 0U))
  918. {
  919. return HAL_ERROR;
  920. }
  921. /* Process Locked */
  922. __HAL_LOCK(hsmartcard);
  923. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  924. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  925. hsmartcard->pRxBuffPtr = pData;
  926. hsmartcard->RxXferSize = Size;
  927. hsmartcard->RxXferCount = Size;
  928. /* Configure Rx interrupt processing */
  929. if ((hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) && (Size >= hsmartcard->NbRxDataToProcess))
  930. {
  931. /* Set the Rx ISR function pointer */
  932. hsmartcard->RxISR = SMARTCARD_RxISR_FIFOEN;
  933. /* Process Unlocked */
  934. __HAL_UNLOCK(hsmartcard);
  935. /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */
  936. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  937. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
  938. }
  939. else
  940. {
  941. /* Set the Rx ISR function pointer */
  942. hsmartcard->RxISR = SMARTCARD_RxISR;
  943. /* Process Unlocked */
  944. __HAL_UNLOCK(hsmartcard);
  945. /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
  946. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
  947. }
  948. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  949. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  950. return HAL_OK;
  951. }
  952. else
  953. {
  954. return HAL_BUSY;
  955. }
  956. }
  957. /**
  958. * @brief Send an amount of data in DMA mode.
  959. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  960. * the configuration information for the specified SMARTCARD module.
  961. * @param pData pointer to data buffer.
  962. * @param Size amount of data to be sent.
  963. * @retval HAL status
  964. */
  965. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size)
  966. {
  967. /* Check that a Tx process is not already ongoing */
  968. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  969. {
  970. if ((pData == NULL) || (Size == 0U))
  971. {
  972. return HAL_ERROR;
  973. }
  974. /* Process Locked */
  975. __HAL_LOCK(hsmartcard);
  976. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  977. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  978. hsmartcard->pTxBuffPtr = pData;
  979. hsmartcard->TxXferSize = Size;
  980. hsmartcard->TxXferCount = Size;
  981. /* Disable the Peripheral first to update mode for TX master */
  982. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  983. /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
  984. the bidirectional line to detect a NACK signal in case of parity error.
  985. Therefore, the receiver block must be enabled as well (RE bit must be set). */
  986. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  987. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  988. {
  989. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  990. }
  991. /* Enable Tx */
  992. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  993. /* Enable the Peripheral */
  994. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  995. /* Perform a TX/RX FIFO Flush */
  996. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  997. /* Set the SMARTCARD DMA transfer complete callback */
  998. hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  999. /* Set the SMARTCARD error callback */
  1000. hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  1001. /* Set the DMA abort callback */
  1002. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1003. /* Enable the SMARTCARD transmit DMA channel */
  1004. if (HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR,
  1005. Size) == HAL_OK)
  1006. {
  1007. /* Clear the TC flag in the ICR register */
  1008. CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
  1009. /* Process Unlocked */
  1010. __HAL_UNLOCK(hsmartcard);
  1011. /* Enable the UART Error Interrupt: (Frame error) */
  1012. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1013. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1014. in the SMARTCARD associated USART CR3 register */
  1015. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1016. return HAL_OK;
  1017. }
  1018. else
  1019. {
  1020. /* Set error code to DMA */
  1021. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1022. /* Process Unlocked */
  1023. __HAL_UNLOCK(hsmartcard);
  1024. /* Restore hsmartcard->State to ready */
  1025. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1026. return HAL_ERROR;
  1027. }
  1028. }
  1029. else
  1030. {
  1031. return HAL_BUSY;
  1032. }
  1033. }
  1034. /**
  1035. * @brief Receive an amount of data in DMA mode.
  1036. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1037. * the configuration information for the specified SMARTCARD module.
  1038. * @param pData pointer to data buffer.
  1039. * @param Size amount of data to be received.
  1040. * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
  1041. * the received data contain the parity bit (MSB position).
  1042. * @retval HAL status
  1043. */
  1044. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  1045. {
  1046. /* Check that a Rx process is not already ongoing */
  1047. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1048. {
  1049. if ((pData == NULL) || (Size == 0U))
  1050. {
  1051. return HAL_ERROR;
  1052. }
  1053. /* Process Locked */
  1054. __HAL_LOCK(hsmartcard);
  1055. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1056. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  1057. hsmartcard->pRxBuffPtr = pData;
  1058. hsmartcard->RxXferSize = Size;
  1059. /* Set the SMARTCARD DMA transfer complete callback */
  1060. hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  1061. /* Set the SMARTCARD DMA error callback */
  1062. hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  1063. /* Set the DMA abort callback */
  1064. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1065. /* Enable the DMA channel */
  1066. if (HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr,
  1067. Size) == HAL_OK)
  1068. {
  1069. /* Process Unlocked */
  1070. __HAL_UNLOCK(hsmartcard);
  1071. /* Enable the SMARTCARD Parity Error Interrupt */
  1072. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  1073. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1074. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1075. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1076. in the SMARTCARD associated USART CR3 register */
  1077. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1078. return HAL_OK;
  1079. }
  1080. else
  1081. {
  1082. /* Set error code to DMA */
  1083. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1084. /* Process Unlocked */
  1085. __HAL_UNLOCK(hsmartcard);
  1086. /* Restore hsmartcard->State to ready */
  1087. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1088. return HAL_ERROR;
  1089. }
  1090. }
  1091. else
  1092. {
  1093. return HAL_BUSY;
  1094. }
  1095. }
  1096. /**
  1097. * @brief Abort ongoing transfers (blocking mode).
  1098. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1099. * the configuration information for the specified SMARTCARD module.
  1100. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1101. * This procedure performs following operations :
  1102. * - Disable SMARTCARD Interrupts (Tx and Rx)
  1103. * - Disable the DMA transfer in the peripheral register (if enabled)
  1104. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1105. * - Set handle State to READY
  1106. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1107. * @retval HAL status
  1108. */
  1109. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
  1110. {
  1111. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and
  1112. ERR (Frame error, noise error, overrun error) interrupts */
  1113. CLEAR_BIT(hsmartcard->Instance->CR1,
  1114. (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
  1115. USART_CR1_EOBIE));
  1116. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1117. /* Disable the SMARTCARD DMA Tx request if enabled */
  1118. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1119. {
  1120. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1121. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1122. if (hsmartcard->hdmatx != NULL)
  1123. {
  1124. /* Set the SMARTCARD DMA Abort callback to Null.
  1125. No call back execution at end of DMA abort procedure */
  1126. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1127. if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
  1128. {
  1129. if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1130. {
  1131. /* Set error code to DMA */
  1132. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1133. return HAL_TIMEOUT;
  1134. }
  1135. }
  1136. }
  1137. }
  1138. /* Disable the SMARTCARD DMA Rx request if enabled */
  1139. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1140. {
  1141. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1142. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1143. if (hsmartcard->hdmarx != NULL)
  1144. {
  1145. /* Set the SMARTCARD DMA Abort callback to Null.
  1146. No call back execution at end of DMA abort procedure */
  1147. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1148. if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
  1149. {
  1150. if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1151. {
  1152. /* Set error code to DMA */
  1153. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1154. return HAL_TIMEOUT;
  1155. }
  1156. }
  1157. }
  1158. }
  1159. /* Reset Tx and Rx transfer counters */
  1160. hsmartcard->TxXferCount = 0U;
  1161. hsmartcard->RxXferCount = 0U;
  1162. /* Clear the Error flags in the ICR register */
  1163. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1164. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  1165. SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1166. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1167. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1168. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1169. /* Reset Handle ErrorCode to No Error */
  1170. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1171. return HAL_OK;
  1172. }
  1173. /**
  1174. * @brief Abort ongoing Transmit transfer (blocking mode).
  1175. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1176. * the configuration information for the specified SMARTCARD module.
  1177. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1178. * This procedure performs following operations :
  1179. * - Disable SMARTCARD Interrupts (Tx)
  1180. * - Disable the DMA transfer in the peripheral register (if enabled)
  1181. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1182. * - Set handle State to READY
  1183. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1184. * @retval HAL status
  1185. */
  1186. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
  1187. {
  1188. /* Disable TCIE, TXEIE and TXFTIE interrupts */
  1189. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1190. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  1191. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1192. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1193. {
  1194. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1195. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1196. }
  1197. /* Disable the SMARTCARD DMA Tx request if enabled */
  1198. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1199. {
  1200. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1201. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1202. if (hsmartcard->hdmatx != NULL)
  1203. {
  1204. /* Set the SMARTCARD DMA Abort callback to Null.
  1205. No call back execution at end of DMA abort procedure */
  1206. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1207. if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
  1208. {
  1209. if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1210. {
  1211. /* Set error code to DMA */
  1212. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1213. return HAL_TIMEOUT;
  1214. }
  1215. }
  1216. }
  1217. }
  1218. /* Reset Tx transfer counter */
  1219. hsmartcard->TxXferCount = 0U;
  1220. /* Clear the Error flags in the ICR register */
  1221. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1222. /* Restore hsmartcard->gState to Ready */
  1223. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1224. return HAL_OK;
  1225. }
  1226. /**
  1227. * @brief Abort ongoing Receive transfer (blocking mode).
  1228. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1229. * the configuration information for the specified SMARTCARD module.
  1230. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1231. * This procedure performs following operations :
  1232. * - Disable SMARTCARD Interrupts (Rx)
  1233. * - Disable the DMA transfer in the peripheral register (if enabled)
  1234. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1235. * - Set handle State to READY
  1236. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1237. * @retval HAL status
  1238. */
  1239. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
  1240. {
  1241. /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1242. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE |
  1243. USART_CR1_EOBIE));
  1244. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  1245. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1246. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1247. {
  1248. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1249. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1250. }
  1251. /* Disable the SMARTCARD DMA Rx request if enabled */
  1252. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1253. {
  1254. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1255. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1256. if (hsmartcard->hdmarx != NULL)
  1257. {
  1258. /* Set the SMARTCARD DMA Abort callback to Null.
  1259. No call back execution at end of DMA abort procedure */
  1260. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1261. if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
  1262. {
  1263. if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1264. {
  1265. /* Set error code to DMA */
  1266. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1267. return HAL_TIMEOUT;
  1268. }
  1269. }
  1270. }
  1271. }
  1272. /* Reset Rx transfer counter */
  1273. hsmartcard->RxXferCount = 0U;
  1274. /* Clear the Error flags in the ICR register */
  1275. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1276. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  1277. SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1278. /* Restore hsmartcard->RxState to Ready */
  1279. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1280. return HAL_OK;
  1281. }
  1282. /**
  1283. * @brief Abort ongoing transfers (Interrupt mode).
  1284. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1285. * the configuration information for the specified SMARTCARD module.
  1286. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1287. * This procedure performs following operations :
  1288. * - Disable SMARTCARD Interrupts (Tx and Rx)
  1289. * - Disable the DMA transfer in the peripheral register (if enabled)
  1290. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1291. * - Set handle State to READY
  1292. * - At abort completion, call user abort complete callback
  1293. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1294. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1295. * @retval HAL status
  1296. */
  1297. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1298. {
  1299. uint32_t abortcplt = 1U;
  1300. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and
  1301. ERR (Frame error, noise error, overrun error) interrupts */
  1302. CLEAR_BIT(hsmartcard->Instance->CR1,
  1303. (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
  1304. USART_CR1_EOBIE));
  1305. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1306. /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle,
  1307. DMA Abort complete callbacks should be initialised before any call
  1308. to DMA Abort functions */
  1309. /* DMA Tx Handle is valid */
  1310. if (hsmartcard->hdmatx != NULL)
  1311. {
  1312. /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
  1313. Otherwise, set it to NULL */
  1314. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1315. {
  1316. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
  1317. }
  1318. else
  1319. {
  1320. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1321. }
  1322. }
  1323. /* DMA Rx Handle is valid */
  1324. if (hsmartcard->hdmarx != NULL)
  1325. {
  1326. /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
  1327. Otherwise, set it to NULL */
  1328. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1329. {
  1330. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
  1331. }
  1332. else
  1333. {
  1334. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1335. }
  1336. }
  1337. /* Disable the SMARTCARD DMA Tx request if enabled */
  1338. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1339. {
  1340. /* Disable DMA Tx at UART level */
  1341. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1342. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1343. if (hsmartcard->hdmatx != NULL)
  1344. {
  1345. /* SMARTCARD Tx DMA Abort callback has already been initialised :
  1346. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1347. /* Abort DMA TX */
  1348. if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1349. {
  1350. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1351. }
  1352. else
  1353. {
  1354. abortcplt = 0U;
  1355. }
  1356. }
  1357. }
  1358. /* Disable the SMARTCARD DMA Rx request if enabled */
  1359. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1360. {
  1361. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1362. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1363. if (hsmartcard->hdmarx != NULL)
  1364. {
  1365. /* SMARTCARD Rx DMA Abort callback has already been initialised :
  1366. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1367. /* Abort DMA RX */
  1368. if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1369. {
  1370. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1371. abortcplt = 1U;
  1372. }
  1373. else
  1374. {
  1375. abortcplt = 0U;
  1376. }
  1377. }
  1378. }
  1379. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1380. if (abortcplt == 1U)
  1381. {
  1382. /* Reset Tx and Rx transfer counters */
  1383. hsmartcard->TxXferCount = 0U;
  1384. hsmartcard->RxXferCount = 0U;
  1385. /* Clear ISR function pointers */
  1386. hsmartcard->RxISR = NULL;
  1387. hsmartcard->TxISR = NULL;
  1388. /* Reset errorCode */
  1389. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1390. /* Clear the Error flags in the ICR register */
  1391. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1392. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
  1393. SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1394. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1395. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1396. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1397. /* As no DMA to be aborted, call directly user Abort complete callback */
  1398. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1399. /* Call registered Abort complete callback */
  1400. hsmartcard->AbortCpltCallback(hsmartcard);
  1401. #else
  1402. /* Call legacy weak Abort complete callback */
  1403. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  1404. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1405. }
  1406. return HAL_OK;
  1407. }
  1408. /**
  1409. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1410. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1411. * the configuration information for the specified SMARTCARD module.
  1412. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1413. * This procedure performs following operations :
  1414. * - Disable SMARTCARD Interrupts (Tx)
  1415. * - Disable the DMA transfer in the peripheral register (if enabled)
  1416. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1417. * - Set handle State to READY
  1418. * - At abort completion, call user abort complete callback
  1419. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1420. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1421. * @retval HAL status
  1422. */
  1423. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1424. {
  1425. /* Disable TCIE, TXEIE and TXFTIE interrupts */
  1426. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1427. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  1428. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1429. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1430. {
  1431. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1432. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1433. }
  1434. /* Disable the SMARTCARD DMA Tx request if enabled */
  1435. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1436. {
  1437. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1438. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1439. if (hsmartcard->hdmatx != NULL)
  1440. {
  1441. /* Set the SMARTCARD DMA Abort callback :
  1442. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1443. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
  1444. /* Abort DMA TX */
  1445. if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1446. {
  1447. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1448. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1449. }
  1450. }
  1451. else
  1452. {
  1453. /* Reset Tx transfer counter */
  1454. hsmartcard->TxXferCount = 0U;
  1455. /* Clear TxISR function pointers */
  1456. hsmartcard->TxISR = NULL;
  1457. /* Restore hsmartcard->gState to Ready */
  1458. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1459. /* As no DMA to be aborted, call directly user Abort complete callback */
  1460. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1461. /* Call registered Abort Transmit Complete Callback */
  1462. hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  1463. #else
  1464. /* Call legacy weak Abort Transmit Complete Callback */
  1465. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1466. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1467. }
  1468. }
  1469. else
  1470. {
  1471. /* Reset Tx transfer counter */
  1472. hsmartcard->TxXferCount = 0U;
  1473. /* Clear TxISR function pointers */
  1474. hsmartcard->TxISR = NULL;
  1475. /* Clear the Error flags in the ICR register */
  1476. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1477. /* Restore hsmartcard->gState to Ready */
  1478. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1479. /* As no DMA to be aborted, call directly user Abort complete callback */
  1480. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1481. /* Call registered Abort Transmit Complete Callback */
  1482. hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  1483. #else
  1484. /* Call legacy weak Abort Transmit Complete Callback */
  1485. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1486. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1487. }
  1488. return HAL_OK;
  1489. }
  1490. /**
  1491. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1492. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1493. * the configuration information for the specified SMARTCARD module.
  1494. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1495. * This procedure performs following operations :
  1496. * - Disable SMARTCARD Interrupts (Rx)
  1497. * - Disable the DMA transfer in the peripheral register (if enabled)
  1498. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1499. * - Set handle State to READY
  1500. * - At abort completion, call user abort complete callback
  1501. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1502. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1503. * @retval HAL status
  1504. */
  1505. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1506. {
  1507. /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1508. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE |
  1509. USART_CR1_EOBIE));
  1510. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  1511. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1512. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1513. {
  1514. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1515. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1516. }
  1517. /* Disable the SMARTCARD DMA Rx request if enabled */
  1518. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1519. {
  1520. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1521. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1522. if (hsmartcard->hdmarx != NULL)
  1523. {
  1524. /* Set the SMARTCARD DMA Abort callback :
  1525. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1526. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
  1527. /* Abort DMA RX */
  1528. if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1529. {
  1530. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1531. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1532. }
  1533. }
  1534. else
  1535. {
  1536. /* Reset Rx transfer counter */
  1537. hsmartcard->RxXferCount = 0U;
  1538. /* Clear RxISR function pointer */
  1539. hsmartcard->RxISR = NULL;
  1540. /* Clear the Error flags in the ICR register */
  1541. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1542. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
  1543. SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1544. /* Restore hsmartcard->RxState to Ready */
  1545. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1546. /* As no DMA to be aborted, call directly user Abort complete callback */
  1547. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1548. /* Call registered Abort Receive Complete Callback */
  1549. hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  1550. #else
  1551. /* Call legacy weak Abort Receive Complete Callback */
  1552. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1553. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1554. }
  1555. }
  1556. else
  1557. {
  1558. /* Reset Rx transfer counter */
  1559. hsmartcard->RxXferCount = 0U;
  1560. /* Clear RxISR function pointer */
  1561. hsmartcard->RxISR = NULL;
  1562. /* Clear the Error flags in the ICR register */
  1563. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1564. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
  1565. SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1566. /* Restore hsmartcard->RxState to Ready */
  1567. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1568. /* As no DMA to be aborted, call directly user Abort complete callback */
  1569. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1570. /* Call registered Abort Receive Complete Callback */
  1571. hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  1572. #else
  1573. /* Call legacy weak Abort Receive Complete Callback */
  1574. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1575. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1576. }
  1577. return HAL_OK;
  1578. }
  1579. /**
  1580. * @brief Handle SMARTCARD interrupt requests.
  1581. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1582. * the configuration information for the specified SMARTCARD module.
  1583. * @retval None
  1584. */
  1585. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
  1586. {
  1587. uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR);
  1588. uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1);
  1589. uint32_t cr3its = READ_REG(hsmartcard->Instance->CR3);
  1590. uint32_t errorflags;
  1591. uint32_t errorcode;
  1592. /* If no error occurs */
  1593. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  1594. if (errorflags == 0U)
  1595. {
  1596. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1597. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
  1598. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1599. || ((cr3its & USART_CR3_RXFTIE) != 0U)))
  1600. {
  1601. if (hsmartcard->RxISR != NULL)
  1602. {
  1603. hsmartcard->RxISR(hsmartcard);
  1604. }
  1605. return;
  1606. }
  1607. }
  1608. /* If some errors occur */
  1609. if ((errorflags != 0U)
  1610. && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
  1611. || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
  1612. {
  1613. /* SMARTCARD parity error interrupt occurred -------------------------------------*/
  1614. if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
  1615. {
  1616. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
  1617. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  1618. }
  1619. /* SMARTCARD frame error interrupt occurred --------------------------------------*/
  1620. if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1621. {
  1622. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
  1623. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  1624. }
  1625. /* SMARTCARD noise error interrupt occurred --------------------------------------*/
  1626. if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1627. {
  1628. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
  1629. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  1630. }
  1631. /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
  1632. if (((isrflags & USART_ISR_ORE) != 0U)
  1633. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1634. || ((cr3its & USART_CR3_RXFTIE) != 0U)
  1635. || ((cr3its & USART_CR3_EIE) != 0U)))
  1636. {
  1637. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
  1638. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  1639. }
  1640. /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
  1641. if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
  1642. {
  1643. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
  1644. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
  1645. }
  1646. /* Call SMARTCARD Error Call back function if need be --------------------------*/
  1647. if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  1648. {
  1649. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1650. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
  1651. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1652. || ((cr3its & USART_CR3_RXFTIE) != 0U)))
  1653. {
  1654. if (hsmartcard->RxISR != NULL)
  1655. {
  1656. hsmartcard->RxISR(hsmartcard);
  1657. }
  1658. }
  1659. /* If Error is to be considered as blocking :
  1660. - Receiver Timeout error in Reception
  1661. - Overrun error in Reception
  1662. - any error occurs in DMA mode reception
  1663. */
  1664. errorcode = hsmartcard->ErrorCode;
  1665. if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1666. || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U))
  1667. {
  1668. /* Blocking error : transfer is aborted
  1669. Set the SMARTCARD state ready to be able to start again the process,
  1670. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1671. SMARTCARD_EndRxTransfer(hsmartcard);
  1672. /* Disable the SMARTCARD DMA Rx request if enabled */
  1673. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1674. {
  1675. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1676. /* Abort the SMARTCARD DMA Rx channel */
  1677. if (hsmartcard->hdmarx != NULL)
  1678. {
  1679. /* Set the SMARTCARD DMA Abort callback :
  1680. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1681. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1682. /* Abort DMA RX */
  1683. if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1684. {
  1685. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1686. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1687. }
  1688. }
  1689. else
  1690. {
  1691. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1692. /* Call registered user error callback */
  1693. hsmartcard->ErrorCallback(hsmartcard);
  1694. #else
  1695. /* Call legacy weak user error callback */
  1696. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1697. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1698. }
  1699. }
  1700. else
  1701. {
  1702. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1703. /* Call registered user error callback */
  1704. hsmartcard->ErrorCallback(hsmartcard);
  1705. #else
  1706. /* Call legacy weak user error callback */
  1707. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1708. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1709. }
  1710. }
  1711. /* other error type to be considered as blocking :
  1712. - Frame error in Transmission
  1713. */
  1714. else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1715. && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U))
  1716. {
  1717. /* Blocking error : transfer is aborted
  1718. Set the SMARTCARD state ready to be able to start again the process,
  1719. Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
  1720. SMARTCARD_EndTxTransfer(hsmartcard);
  1721. /* Disable the SMARTCARD DMA Tx request if enabled */
  1722. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1723. {
  1724. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1725. /* Abort the SMARTCARD DMA Tx channel */
  1726. if (hsmartcard->hdmatx != NULL)
  1727. {
  1728. /* Set the SMARTCARD DMA Abort callback :
  1729. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1730. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1731. /* Abort DMA TX */
  1732. if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1733. {
  1734. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1735. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1736. }
  1737. }
  1738. else
  1739. {
  1740. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1741. /* Call registered user error callback */
  1742. hsmartcard->ErrorCallback(hsmartcard);
  1743. #else
  1744. /* Call legacy weak user error callback */
  1745. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1746. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1747. }
  1748. }
  1749. else
  1750. {
  1751. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1752. /* Call registered user error callback */
  1753. hsmartcard->ErrorCallback(hsmartcard);
  1754. #else
  1755. /* Call legacy weak user error callback */
  1756. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1757. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1758. }
  1759. }
  1760. else
  1761. {
  1762. /* Non Blocking error : transfer could go on.
  1763. Error is notified to user through user error callback */
  1764. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1765. /* Call registered user error callback */
  1766. hsmartcard->ErrorCallback(hsmartcard);
  1767. #else
  1768. /* Call legacy weak user error callback */
  1769. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1770. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1771. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1772. }
  1773. }
  1774. return;
  1775. } /* End if some error occurs */
  1776. /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
  1777. if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U))
  1778. {
  1779. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1780. __HAL_UNLOCK(hsmartcard);
  1781. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1782. /* Call registered Rx complete callback */
  1783. hsmartcard->RxCpltCallback(hsmartcard);
  1784. #else
  1785. /* Call legacy weak Rx complete callback */
  1786. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  1787. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1788. /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
  1789. to be available during HAL_SMARTCARD_RxCpltCallback() processing */
  1790. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
  1791. return;
  1792. }
  1793. /* SMARTCARD in mode Transmitter ------------------------------------------------*/
  1794. if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
  1795. && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
  1796. || ((cr3its & USART_CR3_TXFTIE) != 0U)))
  1797. {
  1798. if (hsmartcard->TxISR != NULL)
  1799. {
  1800. hsmartcard->TxISR(hsmartcard);
  1801. }
  1802. return;
  1803. }
  1804. /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  1805. if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
  1806. {
  1807. if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
  1808. {
  1809. SMARTCARD_EndTransmit_IT(hsmartcard);
  1810. return;
  1811. }
  1812. }
  1813. /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/
  1814. if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
  1815. {
  1816. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1817. /* Call registered Tx Fifo Empty Callback */
  1818. hsmartcard->TxFifoEmptyCallback(hsmartcard);
  1819. #else
  1820. /* Call legacy weak Tx Fifo Empty Callback */
  1821. HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard);
  1822. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1823. return;
  1824. }
  1825. /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/
  1826. if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
  1827. {
  1828. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1829. /* Call registered Rx Fifo Full Callback */
  1830. hsmartcard->RxFifoFullCallback(hsmartcard);
  1831. #else
  1832. /* Call legacy weak Rx Fifo Full Callback */
  1833. HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard);
  1834. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1835. return;
  1836. }
  1837. }
  1838. /**
  1839. * @brief Tx Transfer completed callback.
  1840. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1841. * the configuration information for the specified SMARTCARD module.
  1842. * @retval None
  1843. */
  1844. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1845. {
  1846. /* Prevent unused argument(s) compilation warning */
  1847. UNUSED(hsmartcard);
  1848. /* NOTE : This function should not be modified, when the callback is needed,
  1849. the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
  1850. */
  1851. }
  1852. /**
  1853. * @brief Rx Transfer completed callback.
  1854. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1855. * the configuration information for the specified SMARTCARD module.
  1856. * @retval None
  1857. */
  1858. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1859. {
  1860. /* Prevent unused argument(s) compilation warning */
  1861. UNUSED(hsmartcard);
  1862. /* NOTE : This function should not be modified, when the callback is needed,
  1863. the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
  1864. */
  1865. }
  1866. /**
  1867. * @brief SMARTCARD error callback.
  1868. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1869. * the configuration information for the specified SMARTCARD module.
  1870. * @retval None
  1871. */
  1872. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1873. {
  1874. /* Prevent unused argument(s) compilation warning */
  1875. UNUSED(hsmartcard);
  1876. /* NOTE : This function should not be modified, when the callback is needed,
  1877. the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
  1878. */
  1879. }
  1880. /**
  1881. * @brief SMARTCARD Abort Complete callback.
  1882. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1883. * the configuration information for the specified SMARTCARD module.
  1884. * @retval None
  1885. */
  1886. __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1887. {
  1888. /* Prevent unused argument(s) compilation warning */
  1889. UNUSED(hsmartcard);
  1890. /* NOTE : This function should not be modified, when the callback is needed,
  1891. the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
  1892. */
  1893. }
  1894. /**
  1895. * @brief SMARTCARD Abort Complete callback.
  1896. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1897. * the configuration information for the specified SMARTCARD module.
  1898. * @retval None
  1899. */
  1900. __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1901. {
  1902. /* Prevent unused argument(s) compilation warning */
  1903. UNUSED(hsmartcard);
  1904. /* NOTE : This function should not be modified, when the callback is needed,
  1905. the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
  1906. */
  1907. }
  1908. /**
  1909. * @brief SMARTCARD Abort Receive Complete callback.
  1910. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1911. * the configuration information for the specified SMARTCARD module.
  1912. * @retval None
  1913. */
  1914. __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1915. {
  1916. /* Prevent unused argument(s) compilation warning */
  1917. UNUSED(hsmartcard);
  1918. /* NOTE : This function should not be modified, when the callback is needed,
  1919. the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
  1920. */
  1921. }
  1922. /**
  1923. * @}
  1924. */
  1925. /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
  1926. * @brief SMARTCARD State and Errors functions
  1927. *
  1928. @verbatim
  1929. ==============================================================================
  1930. ##### Peripheral State and Errors functions #####
  1931. ==============================================================================
  1932. [..]
  1933. This subsection provides a set of functions allowing to return the State of SmartCard
  1934. handle and also return Peripheral Errors occurred during communication process
  1935. (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
  1936. of the SMARTCARD peripheral.
  1937. (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
  1938. communication.
  1939. @endverbatim
  1940. * @{
  1941. */
  1942. /**
  1943. * @brief Return the SMARTCARD handle state.
  1944. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1945. * the configuration information for the specified SMARTCARD module.
  1946. * @retval SMARTCARD handle state
  1947. */
  1948. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(const SMARTCARD_HandleTypeDef *hsmartcard)
  1949. {
  1950. /* Return SMARTCARD handle state */
  1951. uint32_t temp1;
  1952. uint32_t temp2;
  1953. temp1 = (uint32_t)hsmartcard->gState;
  1954. temp2 = (uint32_t)hsmartcard->RxState;
  1955. return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
  1956. }
  1957. /**
  1958. * @brief Return the SMARTCARD handle error code.
  1959. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1960. * the configuration information for the specified SMARTCARD module.
  1961. * @retval SMARTCARD handle Error Code
  1962. */
  1963. uint32_t HAL_SMARTCARD_GetError(const SMARTCARD_HandleTypeDef *hsmartcard)
  1964. {
  1965. return hsmartcard->ErrorCode;
  1966. }
  1967. /**
  1968. * @}
  1969. */
  1970. /**
  1971. * @}
  1972. */
  1973. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  1974. * @{
  1975. */
  1976. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1977. /**
  1978. * @brief Initialize the callbacks to their default values.
  1979. * @param hsmartcard SMARTCARD handle.
  1980. * @retval none
  1981. */
  1982. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard)
  1983. {
  1984. /* Init the SMARTCARD Callback settings */
  1985. hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
  1986. hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
  1987. hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
  1988. hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  1989. hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak
  1990. AbortTransmitCpltCallback */
  1991. hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak
  1992. AbortReceiveCpltCallback */
  1993. hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak
  1994. RxFifoFullCallback */
  1995. hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak
  1996. TxFifoEmptyCallback */
  1997. }
  1998. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  1999. /**
  2000. * @brief Configure the SMARTCARD associated USART peripheral.
  2001. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2002. * the configuration information for the specified SMARTCARD module.
  2003. * @retval HAL status
  2004. */
  2005. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  2006. {
  2007. uint32_t tmpreg;
  2008. SMARTCARD_ClockSourceTypeDef clocksource;
  2009. HAL_StatusTypeDef ret = HAL_OK;
  2010. static const uint16_t SMARTCARDPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
  2011. uint32_t pclk;
  2012. /* Check the parameters */
  2013. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  2014. assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
  2015. assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
  2016. assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
  2017. assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
  2018. assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
  2019. assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
  2020. assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
  2021. assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
  2022. assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
  2023. assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
  2024. assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
  2025. assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
  2026. assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler));
  2027. /*-------------------------- USART CR1 Configuration -----------------------*/
  2028. /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
  2029. * Oversampling is forced to 16 (OVER8 = 0).
  2030. * Configure the Parity and Mode:
  2031. * set PS bit according to hsmartcard->Init.Parity value
  2032. * set TE and RE bits according to hsmartcard->Init.Mode value */
  2033. tmpreg = (((uint32_t)hsmartcard->Init.Parity) | ((uint32_t)hsmartcard->Init.Mode) |
  2034. ((uint32_t)hsmartcard->Init.WordLength));
  2035. MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  2036. /*-------------------------- USART CR2 Configuration -----------------------*/
  2037. tmpreg = hsmartcard->Init.StopBits;
  2038. /* Synchronous mode is activated by default */
  2039. tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
  2040. tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
  2041. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
  2042. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  2043. /*-------------------------- USART CR3 Configuration -----------------------*/
  2044. /* Configure
  2045. * - one-bit sampling method versus three samples' majority rule
  2046. * according to hsmartcard->Init.OneBitSampling
  2047. * - NACK transmission in case of parity error according
  2048. * to hsmartcard->Init.NACKEnable
  2049. * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
  2050. tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
  2051. tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
  2052. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
  2053. /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
  2054. /* Configure
  2055. * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
  2056. MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler);
  2057. /*-------------------------- USART GTPR Configuration ----------------------*/
  2058. tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
  2059. MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg);
  2060. /*-------------------------- USART RTOR Configuration ----------------------*/
  2061. tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
  2062. if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
  2063. {
  2064. assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
  2065. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
  2066. }
  2067. MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO | USART_RTOR_BLEN), tmpreg);
  2068. /*-------------------------- USART BRR Configuration -----------------------*/
  2069. SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
  2070. tmpreg = 0U;
  2071. switch (clocksource)
  2072. {
  2073. case SMARTCARD_CLOCKSOURCE_PCLK1:
  2074. pclk = HAL_RCC_GetPCLK1Freq();
  2075. tmpreg = (uint32_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
  2076. (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2077. break;
  2078. case SMARTCARD_CLOCKSOURCE_HSI:
  2079. tmpreg = (uint32_t)(((HSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
  2080. (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2081. break;
  2082. case SMARTCARD_CLOCKSOURCE_SYSCLK:
  2083. pclk = HAL_RCC_GetSysClockFreq();
  2084. tmpreg = (uint32_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
  2085. (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2086. break;
  2087. case SMARTCARD_CLOCKSOURCE_LSE:
  2088. tmpreg = (uint32_t)(((uint16_t)(LSE_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
  2089. (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2090. break;
  2091. default:
  2092. ret = HAL_ERROR;
  2093. break;
  2094. }
  2095. /* USARTDIV must be greater than or equal to 0d16 */
  2096. if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
  2097. {
  2098. hsmartcard->Instance->BRR = (uint16_t)tmpreg;
  2099. }
  2100. else
  2101. {
  2102. ret = HAL_ERROR;
  2103. }
  2104. /* Initialize the number of data to process during RX/TX ISR execution */
  2105. hsmartcard->NbTxDataToProcess = 1U;
  2106. hsmartcard->NbRxDataToProcess = 1U;
  2107. /* Clear ISR function pointers */
  2108. hsmartcard->RxISR = NULL;
  2109. hsmartcard->TxISR = NULL;
  2110. return ret;
  2111. }
  2112. /**
  2113. * @brief Configure the SMARTCARD associated USART peripheral advanced features.
  2114. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2115. * the configuration information for the specified SMARTCARD module.
  2116. * @retval None
  2117. */
  2118. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  2119. {
  2120. /* Check whether the set of advanced features to configure is properly set */
  2121. assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
  2122. /* if required, configure TX pin active level inversion */
  2123. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
  2124. {
  2125. assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
  2126. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
  2127. }
  2128. /* if required, configure RX pin active level inversion */
  2129. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
  2130. {
  2131. assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
  2132. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
  2133. }
  2134. /* if required, configure data inversion */
  2135. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
  2136. {
  2137. assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
  2138. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
  2139. }
  2140. /* if required, configure RX/TX pins swap */
  2141. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
  2142. {
  2143. assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
  2144. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
  2145. }
  2146. /* if required, configure RX overrun detection disabling */
  2147. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  2148. {
  2149. assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
  2150. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
  2151. }
  2152. /* if required, configure DMA disabling on reception error */
  2153. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
  2154. {
  2155. assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
  2156. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
  2157. }
  2158. /* if required, configure MSB first on communication line */
  2159. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
  2160. {
  2161. assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
  2162. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
  2163. }
  2164. }
  2165. /**
  2166. * @brief Check the SMARTCARD Idle State.
  2167. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2168. * the configuration information for the specified SMARTCARD module.
  2169. * @retval HAL status
  2170. */
  2171. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
  2172. {
  2173. uint32_t tickstart;
  2174. /* Initialize the SMARTCARD ErrorCode */
  2175. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2176. /* Init tickstart for timeout management */
  2177. tickstart = HAL_GetTick();
  2178. /* Check if the Transmitter is enabled */
  2179. if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  2180. {
  2181. /* Wait until TEACK flag is set */
  2182. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart,
  2183. SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  2184. {
  2185. /* Timeout occurred */
  2186. return HAL_TIMEOUT;
  2187. }
  2188. }
  2189. /* Check if the Receiver is enabled */
  2190. if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  2191. {
  2192. /* Wait until REACK flag is set */
  2193. if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
  2194. SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  2195. {
  2196. /* Timeout occurred */
  2197. return HAL_TIMEOUT;
  2198. }
  2199. }
  2200. /* Initialize the SMARTCARD states */
  2201. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2202. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2203. /* Process Unlocked */
  2204. __HAL_UNLOCK(hsmartcard);
  2205. return HAL_OK;
  2206. }
  2207. /**
  2208. * @brief Handle SMARTCARD Communication Timeout. It waits
  2209. * until a flag is no longer in the specified status.
  2210. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2211. * the configuration information for the specified SMARTCARD module.
  2212. * @param Flag Specifies the SMARTCARD flag to check.
  2213. * @param Status The actual Flag status (SET or RESET).
  2214. * @param Tickstart Tick start value
  2215. * @param Timeout Timeout duration.
  2216. * @retval HAL status
  2217. */
  2218. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
  2219. FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  2220. {
  2221. /* Wait until flag is set */
  2222. while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
  2223. {
  2224. /* Check for the Timeout */
  2225. if (Timeout != HAL_MAX_DELAY)
  2226. {
  2227. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  2228. {
  2229. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
  2230. interrupts for the interrupt process */
  2231. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
  2232. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2233. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2234. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2235. /* Process Unlocked */
  2236. __HAL_UNLOCK(hsmartcard);
  2237. return HAL_TIMEOUT;
  2238. }
  2239. }
  2240. }
  2241. return HAL_OK;
  2242. }
  2243. /**
  2244. * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
  2245. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2246. * the configuration information for the specified SMARTCARD module.
  2247. * @retval None
  2248. */
  2249. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  2250. {
  2251. /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
  2252. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  2253. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2254. /* At end of Tx process, restore hsmartcard->gState to Ready */
  2255. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2256. }
  2257. /**
  2258. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  2259. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2260. * the configuration information for the specified SMARTCARD module.
  2261. * @retval None
  2262. */
  2263. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  2264. {
  2265. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2266. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  2267. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2268. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  2269. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2270. }
  2271. /**
  2272. * @brief DMA SMARTCARD transmit process complete callback.
  2273. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2274. * the configuration information for the specified DMA module.
  2275. * @retval None
  2276. */
  2277. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2278. {
  2279. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2280. hsmartcard->TxXferCount = 0U;
  2281. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  2282. in the SMARTCARD associated USART CR3 register */
  2283. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  2284. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2285. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2286. }
  2287. /**
  2288. * @brief DMA SMARTCARD receive process complete callback.
  2289. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2290. * the configuration information for the specified DMA module.
  2291. * @retval None
  2292. */
  2293. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2294. {
  2295. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2296. hsmartcard->RxXferCount = 0U;
  2297. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  2298. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2299. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2300. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  2301. in the SMARTCARD associated USART CR3 register */
  2302. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  2303. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  2304. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2305. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2306. /* Call registered Rx complete callback */
  2307. hsmartcard->RxCpltCallback(hsmartcard);
  2308. #else
  2309. /* Call legacy weak Rx complete callback */
  2310. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2311. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2312. }
  2313. /**
  2314. * @brief DMA SMARTCARD communication error callback.
  2315. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2316. * the configuration information for the specified DMA module.
  2317. * @retval None
  2318. */
  2319. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  2320. {
  2321. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2322. /* Stop SMARTCARD DMA Tx request if ongoing */
  2323. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2324. {
  2325. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  2326. {
  2327. hsmartcard->TxXferCount = 0U;
  2328. SMARTCARD_EndTxTransfer(hsmartcard);
  2329. }
  2330. }
  2331. /* Stop SMARTCARD DMA Rx request if ongoing */
  2332. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2333. {
  2334. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  2335. {
  2336. hsmartcard->RxXferCount = 0U;
  2337. SMARTCARD_EndRxTransfer(hsmartcard);
  2338. }
  2339. }
  2340. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
  2341. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2342. /* Call registered user error callback */
  2343. hsmartcard->ErrorCallback(hsmartcard);
  2344. #else
  2345. /* Call legacy weak user error callback */
  2346. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  2347. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2348. }
  2349. /**
  2350. * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
  2351. * (To be called at end of DMA Abort procedure following error occurrence).
  2352. * @param hdma DMA handle.
  2353. * @retval None
  2354. */
  2355. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2356. {
  2357. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2358. hsmartcard->RxXferCount = 0U;
  2359. hsmartcard->TxXferCount = 0U;
  2360. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2361. /* Call registered user error callback */
  2362. hsmartcard->ErrorCallback(hsmartcard);
  2363. #else
  2364. /* Call legacy weak user error callback */
  2365. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  2366. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2367. }
  2368. /**
  2369. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
  2370. * (To be called at end of DMA Tx Abort procedure following user abort request).
  2371. * @note When this callback is executed, User Abort complete call back is called only if no
  2372. * Abort still ongoing for Rx DMA Handle.
  2373. * @param hdma DMA handle.
  2374. * @retval None
  2375. */
  2376. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2377. {
  2378. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2379. hsmartcard->hdmatx->XferAbortCallback = NULL;
  2380. /* Check if an Abort process is still ongoing */
  2381. if (hsmartcard->hdmarx != NULL)
  2382. {
  2383. if (hsmartcard->hdmarx->XferAbortCallback != NULL)
  2384. {
  2385. return;
  2386. }
  2387. }
  2388. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2389. hsmartcard->TxXferCount = 0U;
  2390. hsmartcard->RxXferCount = 0U;
  2391. /* Reset errorCode */
  2392. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2393. /* Clear the Error flags in the ICR register */
  2394. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2395. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  2396. SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  2397. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2398. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2399. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2400. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2401. /* Call registered Abort complete callback */
  2402. hsmartcard->AbortCpltCallback(hsmartcard);
  2403. #else
  2404. /* Call legacy weak Abort complete callback */
  2405. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2406. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2407. }
  2408. /**
  2409. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
  2410. * (To be called at end of DMA Rx Abort procedure following user abort request).
  2411. * @note When this callback is executed, User Abort complete call back is called only if no
  2412. * Abort still ongoing for Tx DMA Handle.
  2413. * @param hdma DMA handle.
  2414. * @retval None
  2415. */
  2416. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2417. {
  2418. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2419. hsmartcard->hdmarx->XferAbortCallback = NULL;
  2420. /* Check if an Abort process is still ongoing */
  2421. if (hsmartcard->hdmatx != NULL)
  2422. {
  2423. if (hsmartcard->hdmatx->XferAbortCallback != NULL)
  2424. {
  2425. return;
  2426. }
  2427. }
  2428. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2429. hsmartcard->TxXferCount = 0U;
  2430. hsmartcard->RxXferCount = 0U;
  2431. /* Reset errorCode */
  2432. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2433. /* Clear the Error flags in the ICR register */
  2434. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2435. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  2436. SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  2437. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2438. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2439. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2440. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2441. /* Call registered Abort complete callback */
  2442. hsmartcard->AbortCpltCallback(hsmartcard);
  2443. #else
  2444. /* Call legacy weak Abort complete callback */
  2445. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2446. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2447. }
  2448. /**
  2449. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
  2450. * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
  2451. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  2452. * and leads to user Tx Abort Complete callback execution).
  2453. * @param hdma DMA handle.
  2454. * @retval None
  2455. */
  2456. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2457. {
  2458. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2459. hsmartcard->TxXferCount = 0U;
  2460. /* Clear the Error flags in the ICR register */
  2461. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  2462. /* Restore hsmartcard->gState to Ready */
  2463. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2464. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2465. /* Call registered Abort Transmit Complete Callback */
  2466. hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  2467. #else
  2468. /* Call legacy weak Abort Transmit Complete Callback */
  2469. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  2470. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2471. }
  2472. /**
  2473. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
  2474. * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
  2475. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  2476. * and leads to user Rx Abort Complete callback execution).
  2477. * @param hdma DMA handle.
  2478. * @retval None
  2479. */
  2480. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2481. {
  2482. SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2483. hsmartcard->RxXferCount = 0U;
  2484. /* Clear the Error flags in the ICR register */
  2485. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2486. SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  2487. SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  2488. /* Restore hsmartcard->RxState to Ready */
  2489. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2490. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2491. /* Call registered Abort Receive Complete Callback */
  2492. hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  2493. #else
  2494. /* Call legacy weak Abort Receive Complete Callback */
  2495. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  2496. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2497. }
  2498. /**
  2499. * @brief Send an amount of data in non-blocking mode.
  2500. * @note Function called under interruption only, once
  2501. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  2502. * and when the FIFO mode is disabled.
  2503. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2504. * the configuration information for the specified SMARTCARD module.
  2505. * @retval None
  2506. */
  2507. static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2508. {
  2509. /* Check that a Tx process is ongoing */
  2510. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2511. {
  2512. if (hsmartcard->TxXferCount == 0U)
  2513. {
  2514. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  2515. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  2516. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2517. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2518. }
  2519. else
  2520. {
  2521. hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
  2522. hsmartcard->pTxBuffPtr++;
  2523. hsmartcard->TxXferCount--;
  2524. }
  2525. }
  2526. }
  2527. /**
  2528. * @brief Send an amount of data in non-blocking mode.
  2529. * @note Function called under interruption only, once
  2530. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  2531. * and when the FIFO mode is enabled.
  2532. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2533. * the configuration information for the specified SMARTCARD module.
  2534. * @retval None
  2535. */
  2536. static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
  2537. {
  2538. uint16_t nb_tx_data;
  2539. /* Check that a Tx process is ongoing */
  2540. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2541. {
  2542. for (nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
  2543. {
  2544. if (hsmartcard->TxXferCount == 0U)
  2545. {
  2546. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  2547. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  2548. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2549. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2550. }
  2551. else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
  2552. {
  2553. hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
  2554. hsmartcard->pTxBuffPtr++;
  2555. hsmartcard->TxXferCount--;
  2556. }
  2557. else
  2558. {
  2559. /* Nothing to do */
  2560. }
  2561. }
  2562. }
  2563. }
  2564. /**
  2565. * @brief Wrap up transmission in non-blocking mode.
  2566. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2567. * the configuration information for the specified SMARTCARD module.
  2568. * @retval None
  2569. */
  2570. static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  2571. {
  2572. /* Disable the SMARTCARD Transmit Complete Interrupt */
  2573. __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2574. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  2575. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  2576. {
  2577. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  2578. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2579. }
  2580. /* Disable the Peripheral first to update mode */
  2581. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2582. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  2583. && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  2584. {
  2585. /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
  2586. for Transmit phase. Disable this receiver block. */
  2587. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  2588. }
  2589. if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  2590. || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  2591. {
  2592. /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
  2593. __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  2594. }
  2595. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2596. /* Tx process is ended, restore hsmartcard->gState to Ready */
  2597. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2598. /* Clear TxISR function pointer */
  2599. hsmartcard->TxISR = NULL;
  2600. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2601. /* Call registered Tx complete callback */
  2602. hsmartcard->TxCpltCallback(hsmartcard);
  2603. #else
  2604. /* Call legacy weak Tx complete callback */
  2605. HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  2606. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2607. }
  2608. /**
  2609. * @brief Receive an amount of data in non-blocking mode.
  2610. * @note Function called under interruption only, once
  2611. * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
  2612. * and when the FIFO mode is disabled.
  2613. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2614. * the configuration information for the specified SMARTCARD module.
  2615. * @retval None
  2616. */
  2617. static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2618. {
  2619. /* Check that a Rx process is ongoing */
  2620. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2621. {
  2622. *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
  2623. hsmartcard->pRxBuffPtr++;
  2624. hsmartcard->RxXferCount--;
  2625. if (hsmartcard->RxXferCount == 0U)
  2626. {
  2627. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2628. /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  2629. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  2630. {
  2631. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2632. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2633. }
  2634. /* Disable the SMARTCARD Parity Error Interrupt */
  2635. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2636. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2637. /* Clear RxISR function pointer */
  2638. hsmartcard->RxISR = NULL;
  2639. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2640. /* Call registered Rx complete callback */
  2641. hsmartcard->RxCpltCallback(hsmartcard);
  2642. #else
  2643. /* Call legacy weak Rx complete callback */
  2644. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2645. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2646. }
  2647. }
  2648. else
  2649. {
  2650. /* Clear RXNE interrupt flag */
  2651. __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  2652. }
  2653. }
  2654. /**
  2655. * @brief Receive an amount of data in non-blocking mode.
  2656. * @note Function called under interruption only, once
  2657. * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
  2658. * and when the FIFO mode is enabled.
  2659. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2660. * the configuration information for the specified SMARTCARD module.
  2661. * @retval None
  2662. */
  2663. static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
  2664. {
  2665. uint16_t nb_rx_data;
  2666. uint16_t rxdatacount;
  2667. /* Check that a Rx process is ongoing */
  2668. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2669. {
  2670. for (nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
  2671. {
  2672. *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
  2673. hsmartcard->pRxBuffPtr++;
  2674. hsmartcard->RxXferCount--;
  2675. if (hsmartcard->RxXferCount == 0U)
  2676. {
  2677. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2678. /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  2679. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  2680. {
  2681. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2682. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2683. }
  2684. /* Disable the SMARTCARD Parity Error Interrupt */
  2685. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2686. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2687. /* Clear RxISR function pointer */
  2688. hsmartcard->RxISR = NULL;
  2689. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2690. /* Call registered Rx complete callback */
  2691. hsmartcard->RxCpltCallback(hsmartcard);
  2692. #else
  2693. /* Call legacy weak Rx complete callback */
  2694. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2695. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2696. }
  2697. }
  2698. /* When remaining number of bytes to receive is less than the RX FIFO
  2699. threshold, next incoming frames are processed as if FIFO mode was
  2700. disabled (i.e. one interrupt per received frame).
  2701. */
  2702. rxdatacount = hsmartcard->RxXferCount;
  2703. if (((rxdatacount != 0U)) && (rxdatacount < hsmartcard->NbRxDataToProcess))
  2704. {
  2705. /* Disable the UART RXFT interrupt*/
  2706. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
  2707. /* Update the RxISR function pointer */
  2708. hsmartcard->RxISR = SMARTCARD_RxISR;
  2709. /* Enable the UART Data Register Not Empty interrupt */
  2710. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2711. }
  2712. }
  2713. else
  2714. {
  2715. /* Clear RXNE interrupt flag */
  2716. __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  2717. }
  2718. }
  2719. /**
  2720. * @}
  2721. */
  2722. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  2723. /**
  2724. * @}
  2725. */
  2726. /**
  2727. * @}
  2728. */