stm32l4xx_hal_smartcard.c 128 KB

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