stm32l0xx_hal_smartcard.c 113 KB

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