stm32f1xx_hal_smartcard.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_smartcard.c
  4. * @author MCD Application Team
  5. * @brief SMARTCARD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the SMARTCARD peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Error functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2016 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The SMARTCARD HAL driver can be used as follows:
  30. (#) Declare a SMARTCARD_HandleTypeDef handle structure.
  31. (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  32. (##) Enable the interface clock of the USARTx associated to the SMARTCARD.
  33. (##) SMARTCARD pins configuration:
  34. (+++) Enable the clock for the SMARTCARD GPIOs.
  35. (+++) Configure SMARTCARD pins as alternate function pull-up.
  36. (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  37. and HAL_SMARTCARD_Receive_IT() APIs):
  38. (+++) Configure the USARTx interrupt priority.
  39. (+++) Enable the NVIC USART IRQ handle.
  40. (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  41. and HAL_SMARTCARD_Receive_DMA() APIs):
  42. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  43. (+++) Enable the DMAx interface clock.
  44. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  45. (+++) Configure the DMA Tx/Rx channel.
  46. (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  47. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  48. (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
  49. (used for last byte sending completion detection in DMA non circular mode)
  50. (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
  51. flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
  52. (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
  53. (++) These APIs configure also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  54. by calling the customized HAL_SMARTCARD_MspInit() API.
  55. [..]
  56. (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
  57. RXNE interrupt and Error Interrupts) will be managed using the macros
  58. __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
  59. [..]
  60. Three operation modes are available within this driver :
  61. *** Polling mode IO operation ***
  62. =================================
  63. [..]
  64. (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  65. (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  66. *** Interrupt mode IO operation ***
  67. ===================================
  68. [..]
  69. (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
  70. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  71. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  72. (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
  73. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  74. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  75. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  76. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  77. *** DMA mode IO operation ***
  78. ==============================
  79. [..]
  80. (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  81. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  82. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  83. (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  84. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  85. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  86. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  87. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  88. *** SMARTCARD HAL driver macros list ***
  89. ========================================
  90. [..]
  91. Below the list of most used macros in SMARTCARD HAL driver.
  92. (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
  93. (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
  94. (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
  95. (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
  96. (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
  97. (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
  98. [..]
  99. (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
  100. ##### Callback registration #####
  101. ==================================
  102. [..]
  103. The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
  104. allows the user to configure dynamically the driver callbacks.
  105. [..]
  106. Use Function HAL_SMARTCARD_RegisterCallback() to register a user callback.
  107. Function HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
  108. (+) TxCpltCallback : Tx Complete Callback.
  109. (+) RxCpltCallback : Rx Complete Callback.
  110. (+) ErrorCallback : Error Callback.
  111. (+) AbortCpltCallback : Abort Complete Callback.
  112. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  113. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  114. (+) MspInitCallback : SMARTCARD MspInit.
  115. (+) MspDeInitCallback : SMARTCARD MspDeInit.
  116. This function takes as parameters the HAL peripheral handle, the Callback ID
  117. and a pointer to the user callback function.
  118. [..]
  119. Use function HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
  120. weak (surcharged) function.
  121. HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  122. and the Callback ID.
  123. This function allows to reset following callbacks:
  124. (+) TxCpltCallback : Tx Complete Callback.
  125. (+) RxCpltCallback : Rx Complete Callback.
  126. (+) ErrorCallback : Error Callback.
  127. (+) AbortCpltCallback : Abort Complete Callback.
  128. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  129. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  130. (+) MspInitCallback : SMARTCARD MspInit.
  131. (+) MspDeInitCallback : SMARTCARD MspDeInit.
  132. [..]
  133. By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
  134. all callbacks are set to the corresponding weak (surcharged) functions:
  135. examples HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback().
  136. Exception done for MspInit and MspDeInit functions that are respectively
  137. reset to the legacy weak (surcharged) functions in the HAL_SMARTCARD_Init()
  138. and HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
  139. If not, MspInit or MspDeInit are not null, the HAL_SMARTCARD_Init() and HAL_SMARTCARD_DeInit()
  140. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  141. [..]
  142. Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
  143. Exception done MspInit/MspDeInit that can be registered/unregistered
  144. in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
  145. MspInit/DeInit callbacks can be used during the Init/DeInit.
  146. In that case first register the MspInit/MspDeInit user callbacks
  147. using HAL_SMARTCARD_RegisterCallback() before calling HAL_SMARTCARD_DeInit()
  148. or HAL_SMARTCARD_Init() function.
  149. [..]
  150. When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
  151. not defined, the callback registration feature is not available
  152. and weak (surcharged) callbacks are used.
  153. @endverbatim
  154. ******************************************************************************
  155. */
  156. /* Includes ------------------------------------------------------------------*/
  157. #include "stm32f1xx_hal.h"
  158. /** @addtogroup STM32F1xx_HAL_Driver
  159. * @{
  160. */
  161. /** @defgroup SMARTCARD SMARTCARD
  162. * @brief HAL SMARTCARD module driver
  163. * @{
  164. */
  165. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  166. /* Private typedef -----------------------------------------------------------*/
  167. /* Private define ------------------------------------------------------------*/
  168. /** @addtogroup SMARTCARD_Private_Constants
  169. * @{
  170. */
  171. /**
  172. * @}
  173. */
  174. /* Private macro -------------------------------------------------------------*/
  175. /* Private variables ---------------------------------------------------------*/
  176. /* Private function prototypes -----------------------------------------------*/
  177. /** @addtogroup SMARTCARD_Private_Functions
  178. * @{
  179. */
  180. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  181. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc);
  182. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  183. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc);
  184. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc);
  185. static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
  186. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
  187. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsc);
  188. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
  189. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  190. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  191. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  192. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  193. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  194. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  195. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  196. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  197. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  198. /**
  199. * @}
  200. */
  201. /* Exported functions --------------------------------------------------------*/
  202. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  203. * @{
  204. */
  205. /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
  206. * @brief Initialization and Configuration functions
  207. *
  208. @verbatim
  209. ==============================================================================
  210. ##### Initialization and Configuration functions #####
  211. ==============================================================================
  212. [..]
  213. This subsection provides a set of functions allowing to initialize the USART
  214. in Smartcard mode.
  215. [..]
  216. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  217. defined in the ISO 7816-3 standard.
  218. [..]
  219. The USART can provide a clock to the smartcard through the SCLK output.
  220. In smartcard mode, SCLK is not associated to the communication but is simply derived
  221. from the internal peripheral input clock through a 5-bit prescaler.
  222. [..]
  223. (+) For the Smartcard mode only these parameters can be configured:
  224. (++) Baud Rate
  225. (++) Word Length => Should be 9 bits (8 bits + parity)
  226. (++) Stop Bit
  227. (++) Parity: => Should be enabled
  228. (++) USART polarity
  229. (++) USART phase
  230. (++) USART LastBit
  231. (++) Receiver/transmitter modes
  232. (++) Prescaler
  233. (++) GuardTime
  234. (++) NACKState: The Smartcard NACK state
  235. (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
  236. (++) Word Length = 9 Bits
  237. (++) 1.5 Stop Bit
  238. (++) Even parity
  239. (++) BaudRate = 12096 baud
  240. (++) Tx and Rx enabled
  241. [..]
  242. Please refer to the ISO 7816-3 specification for more details.
  243. [..]
  244. (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
  245. to use 1.5 stop bits for both transmitting and receiving to avoid switching
  246. between the two configurations.
  247. [..]
  248. The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
  249. procedures (details for the procedures are available in reference manuals
  250. (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
  251. @endverbatim
  252. The SMARTCARD frame format is given in the following table:
  253. +-------------------------------------------------------------+
  254. | M bit | PCE bit | SMARTCARD frame |
  255. |---------------------|---------------------------------------|
  256. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  257. +-------------------------------------------------------------+
  258. * @{
  259. */
  260. /**
  261. * @brief Initializes the SmartCard mode according to the specified
  262. * parameters in the SMARTCARD_InitTypeDef and create the associated handle.
  263. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  264. * the configuration information for SMARTCARD module.
  265. * @retval HAL status
  266. */
  267. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
  268. {
  269. /* Check the SMARTCARD handle allocation */
  270. if(hsc == NULL)
  271. {
  272. return HAL_ERROR;
  273. }
  274. /* Check the parameters */
  275. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  276. assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  277. if(hsc->gState == HAL_SMARTCARD_STATE_RESET)
  278. {
  279. /* Allocate lock resource and initialize it */
  280. hsc->Lock = HAL_UNLOCKED;
  281. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  282. SMARTCARD_InitCallbacksToDefault(hsc);
  283. if (hsc->MspInitCallback == NULL)
  284. {
  285. hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
  286. }
  287. /* Init the low level hardware */
  288. hsc->MspInitCallback(hsc);
  289. #else
  290. /* Init the low level hardware : GPIO, CLOCK */
  291. HAL_SMARTCARD_MspInit(hsc);
  292. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  293. }
  294. hsc->gState = HAL_SMARTCARD_STATE_BUSY;
  295. /* Set the Prescaler */
  296. MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
  297. /* Set the Guard Time */
  298. MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8U));
  299. /* Set the Smartcard Communication parameters */
  300. SMARTCARD_SetConfig(hsc);
  301. /* In SmartCard mode, the following bits must be kept cleared:
  302. - LINEN bit in the USART_CR2 register
  303. - HDSEL and IREN bits in the USART_CR3 register.*/
  304. CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
  305. CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
  306. /* Enable the SMARTCARD Parity Error Interrupt */
  307. SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  308. /* Enable the SMARTCARD Framing Error Interrupt */
  309. SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  310. /* Enable the Peripheral */
  311. __HAL_SMARTCARD_ENABLE(hsc);
  312. /* Configure the Smartcard NACK state */
  313. MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
  314. /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  315. hsc->Instance->CR3 |= (USART_CR3_SCEN);
  316. /* Initialize the SMARTCARD state*/
  317. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  318. hsc->gState= HAL_SMARTCARD_STATE_READY;
  319. hsc->RxState= HAL_SMARTCARD_STATE_READY;
  320. return HAL_OK;
  321. }
  322. /**
  323. * @brief DeInitializes the USART SmartCard peripheral
  324. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  325. * the configuration information for SMARTCARD module.
  326. * @retval HAL status
  327. */
  328. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
  329. {
  330. /* Check the SMARTCARD handle allocation */
  331. if(hsc == NULL)
  332. {
  333. return HAL_ERROR;
  334. }
  335. /* Check the parameters */
  336. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  337. hsc->gState = HAL_SMARTCARD_STATE_BUSY;
  338. /* Disable the Peripheral */
  339. __HAL_SMARTCARD_DISABLE(hsc);
  340. /* DeInit the low level hardware */
  341. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  342. if (hsc->MspDeInitCallback == NULL)
  343. {
  344. hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  345. }
  346. /* DeInit the low level hardware */
  347. hsc->MspDeInitCallback(hsc);
  348. #else
  349. HAL_SMARTCARD_MspDeInit(hsc);
  350. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  351. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  352. hsc->gState = HAL_SMARTCARD_STATE_RESET;
  353. hsc->RxState = HAL_SMARTCARD_STATE_RESET;
  354. /* Release Lock */
  355. __HAL_UNLOCK(hsc);
  356. return HAL_OK;
  357. }
  358. /**
  359. * @brief SMARTCARD MSP Init
  360. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  361. * the configuration information for SMARTCARD module.
  362. * @retval None
  363. */
  364. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
  365. {
  366. /* Prevent unused argument(s) compilation warning */
  367. UNUSED(hsc);
  368. /* NOTE : This function should not be modified, when the callback is needed,
  369. the HAL_SMARTCARD_MspInit can be implemented in the user file
  370. */
  371. }
  372. /**
  373. * @brief SMARTCARD MSP DeInit
  374. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  375. * the configuration information for SMARTCARD module.
  376. * @retval None
  377. */
  378. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
  379. {
  380. /* Prevent unused argument(s) compilation warning */
  381. UNUSED(hsc);
  382. /* NOTE : This function should not be modified, when the callback is needed,
  383. the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  384. */
  385. }
  386. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  387. /**
  388. * @brief Register a User SMARTCARD Callback
  389. * To be used instead of the weak predefined callback
  390. * @note The HAL_SMARTCARD_RegisterCallback() may be called before HAL_SMARTCARD_Init()
  391. * in HAL_SMARTCARD_STATE_RESET to register callbacks for HAL_SMARTCARD_MSPINIT_CB_ID
  392. * and HAL_SMARTCARD_MSPDEINIT_CB_ID
  393. * @param hsc smartcard handle
  394. * @param CallbackID ID of the callback to be registered
  395. * This parameter can be one of the following values:
  396. * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  397. * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  398. * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  399. * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  400. * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  401. * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  402. * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  403. * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  404. * @param pCallback pointer to the Callback function
  405. * @retval HAL status
  406. */
  407. HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback)
  408. {
  409. HAL_StatusTypeDef status = HAL_OK;
  410. if (pCallback == NULL)
  411. {
  412. /* Update the error code */
  413. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  414. return HAL_ERROR;
  415. }
  416. if (hsc->gState == HAL_SMARTCARD_STATE_READY)
  417. {
  418. switch (CallbackID)
  419. {
  420. case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  421. hsc->TxCpltCallback = pCallback;
  422. break;
  423. case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  424. hsc->RxCpltCallback = pCallback;
  425. break;
  426. case HAL_SMARTCARD_ERROR_CB_ID :
  427. hsc->ErrorCallback = pCallback;
  428. break;
  429. case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  430. hsc->AbortCpltCallback = pCallback;
  431. break;
  432. case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  433. hsc->AbortTransmitCpltCallback = pCallback;
  434. break;
  435. case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  436. hsc->AbortReceiveCpltCallback = pCallback;
  437. break;
  438. case HAL_SMARTCARD_MSPINIT_CB_ID :
  439. hsc->MspInitCallback = pCallback;
  440. break;
  441. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  442. hsc->MspDeInitCallback = pCallback;
  443. break;
  444. default :
  445. /* Update the error code */
  446. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  447. /* Return error status */
  448. status = HAL_ERROR;
  449. break;
  450. }
  451. }
  452. else if (hsc->gState == HAL_SMARTCARD_STATE_RESET)
  453. {
  454. switch (CallbackID)
  455. {
  456. case HAL_SMARTCARD_MSPINIT_CB_ID :
  457. hsc->MspInitCallback = pCallback;
  458. break;
  459. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  460. hsc->MspDeInitCallback = pCallback;
  461. break;
  462. default :
  463. /* Update the error code */
  464. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  465. /* Return error status */
  466. status = HAL_ERROR;
  467. break;
  468. }
  469. }
  470. else
  471. {
  472. /* Update the error code */
  473. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  474. /* Return error status */
  475. status = HAL_ERROR;
  476. }
  477. return status;
  478. }
  479. /**
  480. * @brief Unregister an SMARTCARD callback
  481. * SMARTCARD callback is redirected to the weak predefined callback
  482. * @note The HAL_SMARTCARD_UnRegisterCallback() may be called before HAL_SMARTCARD_Init()
  483. * in HAL_SMARTCARD_STATE_RESET to un-register callbacks for HAL_SMARTCARD_MSPINIT_CB_ID
  484. * and HAL_SMARTCARD_MSPDEINIT_CB_ID
  485. * @param hsc smartcard handle
  486. * @param CallbackID ID of the callback to be unregistered
  487. * This parameter can be one of the following values:
  488. * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  489. * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  490. * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  491. * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  492. * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  493. * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  494. * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  495. * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  496. * @retval HAL status
  497. */
  498. HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
  499. {
  500. HAL_StatusTypeDef status = HAL_OK;
  501. if (HAL_SMARTCARD_STATE_READY == hsc->gState)
  502. {
  503. switch (CallbackID)
  504. {
  505. case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  506. hsc->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
  507. break;
  508. case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  509. hsc->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
  510. break;
  511. case HAL_SMARTCARD_ERROR_CB_ID :
  512. hsc->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
  513. break;
  514. case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  515. hsc->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  516. break;
  517. case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  518. hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  519. break;
  520. case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  521. hsc->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
  522. break;
  523. case HAL_SMARTCARD_MSPINIT_CB_ID :
  524. hsc->MspInitCallback = HAL_SMARTCARD_MspInit; /* Legacy weak MspInitCallback */
  525. break;
  526. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  527. hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; /* Legacy weak MspDeInitCallback */
  528. break;
  529. default :
  530. /* Update the error code */
  531. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  532. /* Return error status */
  533. status = HAL_ERROR;
  534. break;
  535. }
  536. }
  537. else if (HAL_SMARTCARD_STATE_RESET == hsc->gState)
  538. {
  539. switch (CallbackID)
  540. {
  541. case HAL_SMARTCARD_MSPINIT_CB_ID :
  542. hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
  543. break;
  544. case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  545. hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  546. break;
  547. default :
  548. /* Update the error code */
  549. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  550. /* Return error status */
  551. status = HAL_ERROR;
  552. break;
  553. }
  554. }
  555. else
  556. {
  557. /* Update the error code */
  558. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  559. /* Return error status */
  560. status = HAL_ERROR;
  561. }
  562. return status;
  563. }
  564. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  565. /**
  566. * @}
  567. */
  568. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  569. * @brief SMARTCARD Transmit and Receive functions
  570. *
  571. @verbatim
  572. ===============================================================================
  573. ##### IO operation functions #####
  574. ===============================================================================
  575. [..]
  576. This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  577. [..]
  578. (#) Smartcard is a single wire half duplex communication protocol.
  579. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  580. defined in the ISO 7816-3 standard.
  581. (#) The USART should be configured as:
  582. (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  583. (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  584. (#) There are two modes of transfer:
  585. (++) Blocking mode: The communication is performed in polling mode.
  586. The HAL status of all data processing is returned by the same function
  587. after finishing transfer.
  588. (++) Non Blocking mode: The communication is performed using Interrupts
  589. or DMA, These APIs return the HAL status.
  590. The end of the data processing will be indicated through the
  591. dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  592. using DMA mode.
  593. The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  594. will be executed respectively at the end of the Transmit or Receive process
  595. The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
  596. (#) Blocking mode APIs are :
  597. (++) HAL_SMARTCARD_Transmit()
  598. (++) HAL_SMARTCARD_Receive()
  599. (#) Non Blocking mode APIs with Interrupt are :
  600. (++) HAL_SMARTCARD_Transmit_IT()
  601. (++) HAL_SMARTCARD_Receive_IT()
  602. (++) HAL_SMARTCARD_IRQHandler()
  603. (#) Non Blocking mode functions with DMA are :
  604. (++) HAL_SMARTCARD_Transmit_DMA()
  605. (++) HAL_SMARTCARD_Receive_DMA()
  606. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  607. (++) HAL_SMARTCARD_TxCpltCallback()
  608. (++) HAL_SMARTCARD_RxCpltCallback()
  609. (++) HAL_SMARTCARD_ErrorCallback()
  610. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  611. (+) HAL_SMARTCARD_Abort()
  612. (+) HAL_SMARTCARD_AbortTransmit()
  613. (+) HAL_SMARTCARD_AbortReceive()
  614. (+) HAL_SMARTCARD_Abort_IT()
  615. (+) HAL_SMARTCARD_AbortTransmit_IT()
  616. (+) HAL_SMARTCARD_AbortReceive_IT()
  617. (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  618. (+) HAL_SMARTCARD_AbortCpltCallback()
  619. (+) HAL_SMARTCARD_AbortTransmitCpltCallback()
  620. (+) HAL_SMARTCARD_AbortReceiveCpltCallback()
  621. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  622. Errors are handled as follows :
  623. (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  624. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  625. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  626. and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
  627. If user wants to abort it, Abort services should be called by user.
  628. (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  629. This concerns Frame Error in Interrupt mode transmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
  630. Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
  631. @endverbatim
  632. * @{
  633. */
  634. /**
  635. * @brief Send an amount of data in blocking mode
  636. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  637. * the configuration information for SMARTCARD module.
  638. * @param pData Pointer to data buffer
  639. * @param Size Amount of data to be sent
  640. * @param Timeout Timeout duration
  641. * @retval HAL status
  642. */
  643. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
  644. {
  645. const uint8_t *tmp = pData;
  646. uint32_t tickstart = 0U;
  647. if(hsc->gState == HAL_SMARTCARD_STATE_READY)
  648. {
  649. if((pData == NULL) || (Size == 0U))
  650. {
  651. return HAL_ERROR;
  652. }
  653. /* Process Locked */
  654. __HAL_LOCK(hsc);
  655. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  656. hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  657. /* Init tickstart for timeout management */
  658. tickstart = HAL_GetTick();
  659. hsc->TxXferSize = Size;
  660. hsc->TxXferCount = Size;
  661. while(hsc->TxXferCount > 0U)
  662. {
  663. hsc->TxXferCount--;
  664. if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  665. {
  666. return HAL_TIMEOUT;
  667. }
  668. hsc->Instance->DR = (uint8_t)(*tmp & 0xFFU);
  669. tmp++;
  670. }
  671. if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  672. {
  673. return HAL_TIMEOUT;
  674. }
  675. /* At end of Tx process, restore hsc->gState to Ready */
  676. hsc->gState = HAL_SMARTCARD_STATE_READY;
  677. /* Process Unlocked */
  678. __HAL_UNLOCK(hsc);
  679. return HAL_OK;
  680. }
  681. else
  682. {
  683. return HAL_BUSY;
  684. }
  685. }
  686. /**
  687. * @brief Receive an amount of data in blocking mode
  688. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  689. * the configuration information for SMARTCARD module.
  690. * @param pData Pointer to data buffer
  691. * @param Size Amount of data to be received
  692. * @param Timeout Timeout duration
  693. * @retval HAL status
  694. */
  695. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  696. {
  697. uint8_t *tmp = pData;
  698. uint32_t tickstart = 0U;
  699. if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
  700. {
  701. if((pData == NULL) || (Size == 0U))
  702. {
  703. return HAL_ERROR;
  704. }
  705. /* Process Locked */
  706. __HAL_LOCK(hsc);
  707. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  708. hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  709. /* Init tickstart for timeout management */
  710. tickstart = HAL_GetTick();
  711. hsc->RxXferSize = Size;
  712. hsc->RxXferCount = Size;
  713. /* Check the remain data to be received */
  714. while(hsc->RxXferCount > 0U)
  715. {
  716. hsc->RxXferCount--;
  717. if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  718. {
  719. return HAL_TIMEOUT;
  720. }
  721. *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
  722. tmp++;
  723. }
  724. /* At end of Rx process, restore hsc->RxState to Ready */
  725. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  726. /* Process Unlocked */
  727. __HAL_UNLOCK(hsc);
  728. return HAL_OK;
  729. }
  730. else
  731. {
  732. return HAL_BUSY;
  733. }
  734. }
  735. /**
  736. * @brief Send an amount of data in non blocking mode
  737. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  738. * the configuration information for SMARTCARD module.
  739. * @param pData Pointer to data buffer
  740. * @param Size Amount of data to be sent
  741. * @retval HAL status
  742. */
  743. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size)
  744. {
  745. /* Check that a Tx process is not already ongoing */
  746. if(hsc->gState == HAL_SMARTCARD_STATE_READY)
  747. {
  748. if((pData == NULL) || (Size == 0U))
  749. {
  750. return HAL_ERROR;
  751. }
  752. /* Process Locked */
  753. __HAL_LOCK(hsc);
  754. hsc->pTxBuffPtr = pData;
  755. hsc->TxXferSize = Size;
  756. hsc->TxXferCount = Size;
  757. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  758. hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  759. /* Process Unlocked */
  760. __HAL_UNLOCK(hsc);
  761. /* Enable the SMARTCARD Parity Error Interrupt */
  762. SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  763. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  764. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  765. /* Enable the SMARTCARD Transmit data register empty Interrupt */
  766. SET_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
  767. return HAL_OK;
  768. }
  769. else
  770. {
  771. return HAL_BUSY;
  772. }
  773. }
  774. /**
  775. * @brief Receive an amount of data in non blocking mode
  776. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  777. * the configuration information for SMARTCARD module.
  778. * @param pData Pointer to data buffer
  779. * @param Size Amount of data to be received
  780. * @retval HAL status
  781. */
  782. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  783. {
  784. /* Check that a Rx process is not already ongoing */
  785. if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
  786. {
  787. if((pData == NULL) || (Size == 0U))
  788. {
  789. return HAL_ERROR;
  790. }
  791. /* Process Locked */
  792. __HAL_LOCK(hsc);
  793. hsc->pRxBuffPtr = pData;
  794. hsc->RxXferSize = Size;
  795. hsc->RxXferCount = Size;
  796. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  797. hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  798. /* Process Unlocked */
  799. __HAL_UNLOCK(hsc);
  800. /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
  801. SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
  802. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  803. SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  804. return HAL_OK;
  805. }
  806. else
  807. {
  808. return HAL_BUSY;
  809. }
  810. }
  811. /**
  812. * @brief Send an amount of data in non blocking mode
  813. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  814. * the configuration information for SMARTCARD module.
  815. * @param pData Pointer to data buffer
  816. * @param Size Amount of data to be sent
  817. * @retval HAL status
  818. */
  819. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size)
  820. {
  821. const uint32_t *tmp;
  822. /* Check that a Tx process is not already ongoing */
  823. if(hsc->gState == HAL_SMARTCARD_STATE_READY)
  824. {
  825. if((pData == NULL) || (Size == 0U))
  826. {
  827. return HAL_ERROR;
  828. }
  829. /* Process Locked */
  830. __HAL_LOCK(hsc);
  831. hsc->pTxBuffPtr = pData;
  832. hsc->TxXferSize = Size;
  833. hsc->TxXferCount = Size;
  834. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  835. hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  836. /* Set the SMARTCARD DMA transfer complete callback */
  837. hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  838. /* Set the DMA error callback */
  839. hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  840. /* Set the DMA abort callback */
  841. hsc->hdmatx->XferAbortCallback = NULL;
  842. /* Enable the SMARTCARD transmit DMA channel */
  843. tmp = (const uint32_t*)&pData;
  844. HAL_DMA_Start_IT(hsc->hdmatx, *(const uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
  845. /* Clear the TC flag in the SR register by writing 0 to it */
  846. __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
  847. /* Process Unlocked */
  848. __HAL_UNLOCK(hsc);
  849. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  850. in the SMARTCARD CR3 register */
  851. SET_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  852. return HAL_OK;
  853. }
  854. else
  855. {
  856. return HAL_BUSY;
  857. }
  858. }
  859. /**
  860. * @brief Receive an amount of data in non blocking mode
  861. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  862. * the configuration information for SMARTCARD module.
  863. * @param pData Pointer to data buffer
  864. * @param Size Amount of data to be received
  865. * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
  866. * @retval HAL status
  867. */
  868. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  869. {
  870. uint32_t *tmp;
  871. /* Check that a Rx process is not already ongoing */
  872. if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
  873. {
  874. if((pData == NULL) || (Size == 0U))
  875. {
  876. return HAL_ERROR;
  877. }
  878. /* Process Locked */
  879. __HAL_LOCK(hsc);
  880. hsc->pRxBuffPtr = pData;
  881. hsc->RxXferSize = Size;
  882. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  883. hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  884. /* Set the SMARTCARD DMA transfer complete callback */
  885. hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  886. /* Set the DMA error callback */
  887. hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  888. /* Set the DMA abort callback */
  889. hsc->hdmatx->XferAbortCallback = NULL;
  890. /* Enable the DMA channel */
  891. tmp = (uint32_t*)&pData;
  892. HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
  893. /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
  894. __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
  895. /* Process Unlocked */
  896. __HAL_UNLOCK(hsc);
  897. /* Enable the SMARTCARD Parity Error Interrupt */
  898. SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  899. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  900. SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  901. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  902. in the SMARTCARD CR3 register */
  903. SET_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  904. return HAL_OK;
  905. }
  906. else
  907. {
  908. return HAL_BUSY;
  909. }
  910. }
  911. /**
  912. * @brief Abort ongoing transfers (blocking mode).
  913. * @param hsc SMARTCARD handle.
  914. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  915. * This procedure performs following operations :
  916. * - Disable PPP Interrupts
  917. * - Disable the DMA transfer in the peripheral register (if enabled)
  918. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  919. * - Set handle State to READY
  920. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  921. * @retval HAL status
  922. */
  923. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc)
  924. {
  925. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  926. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  927. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  928. /* Disable the SMARTCARD DMA Tx request if enabled */
  929. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  930. {
  931. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  932. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  933. if(hsc->hdmatx != NULL)
  934. {
  935. /* Set the SMARTCARD DMA Abort callback to Null.
  936. No call back execution at end of DMA abort procedure */
  937. hsc->hdmatx->XferAbortCallback = NULL;
  938. HAL_DMA_Abort(hsc->hdmatx);
  939. }
  940. }
  941. /* Disable the SMARTCARD DMA Rx request if enabled */
  942. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  943. {
  944. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  945. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  946. if(hsc->hdmarx != NULL)
  947. {
  948. /* Set the SMARTCARD DMA Abort callback to Null.
  949. No call back execution at end of DMA abort procedure */
  950. hsc->hdmarx->XferAbortCallback = NULL;
  951. HAL_DMA_Abort(hsc->hdmarx);
  952. }
  953. }
  954. /* Reset Tx and Rx transfer counters */
  955. hsc->TxXferCount = 0x00U;
  956. hsc->RxXferCount = 0x00U;
  957. /* Reset ErrorCode */
  958. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  959. /* Restore hsc->RxState and hsc->gState to Ready */
  960. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  961. hsc->gState = HAL_SMARTCARD_STATE_READY;
  962. return HAL_OK;
  963. }
  964. /**
  965. * @brief Abort ongoing Transmit transfer (blocking mode).
  966. * @param hsc SMARTCARD handle.
  967. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  968. * This procedure performs following operations :
  969. * - Disable SMARTCARD Interrupts (Tx)
  970. * - Disable the DMA transfer in the peripheral register (if enabled)
  971. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  972. * - Set handle State to READY
  973. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  974. * @retval HAL status
  975. */
  976. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc)
  977. {
  978. /* Disable TXEIE and TCIE interrupts */
  979. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  980. /* Disable the SMARTCARD DMA Tx request if enabled */
  981. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  982. {
  983. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  984. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  985. if(hsc->hdmatx != NULL)
  986. {
  987. /* Set the SMARTCARD DMA Abort callback to Null.
  988. No call back execution at end of DMA abort procedure */
  989. hsc->hdmatx->XferAbortCallback = NULL;
  990. HAL_DMA_Abort(hsc->hdmatx);
  991. }
  992. }
  993. /* Reset Tx transfer counter */
  994. hsc->TxXferCount = 0x00U;
  995. /* Restore hsc->gState to Ready */
  996. hsc->gState = HAL_SMARTCARD_STATE_READY;
  997. return HAL_OK;
  998. }
  999. /**
  1000. * @brief Abort ongoing Receive transfer (blocking mode).
  1001. * @param hsc SMARTCARD handle.
  1002. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1003. * This procedure performs following operations :
  1004. * - Disable PPP Interrupts
  1005. * - Disable the DMA transfer in the peripheral register (if enabled)
  1006. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1007. * - Set handle State to READY
  1008. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1009. * @retval HAL status
  1010. */
  1011. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc)
  1012. {
  1013. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1014. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1015. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1016. /* Disable the SMARTCARD DMA Rx request if enabled */
  1017. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1018. {
  1019. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1020. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1021. if(hsc->hdmarx != NULL)
  1022. {
  1023. /* Set the SMARTCARD DMA Abort callback to Null.
  1024. No call back execution at end of DMA abort procedure */
  1025. hsc->hdmarx->XferAbortCallback = NULL;
  1026. HAL_DMA_Abort(hsc->hdmarx);
  1027. }
  1028. }
  1029. /* Reset Rx transfer counter */
  1030. hsc->RxXferCount = 0x00U;
  1031. /* Restore hsc->RxState to Ready */
  1032. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1033. return HAL_OK;
  1034. }
  1035. /**
  1036. * @brief Abort ongoing transfers (Interrupt mode).
  1037. * @param hsc SMARTCARD handle.
  1038. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1039. * This procedure performs following operations :
  1040. * - Disable PPP Interrupts
  1041. * - Disable the DMA transfer in the peripheral register (if enabled)
  1042. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1043. * - Set handle State to READY
  1044. * - At abort completion, call user abort complete callback
  1045. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1046. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1047. * @retval HAL status
  1048. */
  1049. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc)
  1050. {
  1051. uint32_t AbortCplt = 0x01U;
  1052. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1053. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1054. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1055. /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
  1056. before any call to DMA Abort functions */
  1057. /* DMA Tx Handle is valid */
  1058. if(hsc->hdmatx != NULL)
  1059. {
  1060. /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
  1061. Otherwise, set it to NULL */
  1062. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  1063. {
  1064. hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
  1065. }
  1066. else
  1067. {
  1068. hsc->hdmatx->XferAbortCallback = NULL;
  1069. }
  1070. }
  1071. /* DMA Rx Handle is valid */
  1072. if(hsc->hdmarx != NULL)
  1073. {
  1074. /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
  1075. Otherwise, set it to NULL */
  1076. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1077. {
  1078. hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
  1079. }
  1080. else
  1081. {
  1082. hsc->hdmarx->XferAbortCallback = NULL;
  1083. }
  1084. }
  1085. /* Disable the SMARTCARD DMA Tx request if enabled */
  1086. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  1087. {
  1088. /* Disable DMA Tx at SMARTCARD level */
  1089. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1090. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1091. if(hsc->hdmatx != NULL)
  1092. {
  1093. /* SMARTCARD Tx DMA Abort callback has already been initialised :
  1094. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1095. /* Abort DMA TX */
  1096. if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
  1097. {
  1098. hsc->hdmatx->XferAbortCallback = NULL;
  1099. }
  1100. else
  1101. {
  1102. AbortCplt = 0x00U;
  1103. }
  1104. }
  1105. }
  1106. /* Disable the SMARTCARD DMA Rx request if enabled */
  1107. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1108. {
  1109. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1110. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1111. if(hsc->hdmarx != NULL)
  1112. {
  1113. /* SMARTCARD Rx DMA Abort callback has already been initialised :
  1114. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1115. /* Abort DMA RX */
  1116. if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
  1117. {
  1118. hsc->hdmarx->XferAbortCallback = NULL;
  1119. AbortCplt = 0x01U;
  1120. }
  1121. else
  1122. {
  1123. AbortCplt = 0x00U;
  1124. }
  1125. }
  1126. }
  1127. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1128. if(AbortCplt == 0x01U)
  1129. {
  1130. /* Reset Tx and Rx transfer counters */
  1131. hsc->TxXferCount = 0x00U;
  1132. hsc->RxXferCount = 0x00U;
  1133. /* Reset ErrorCode */
  1134. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1135. /* Restore hsc->gState and hsc->RxState to Ready */
  1136. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1137. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1138. /* As no DMA to be aborted, call directly user Abort complete callback */
  1139. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1140. /* Call registered Abort complete callback */
  1141. hsc->AbortCpltCallback(hsc);
  1142. #else
  1143. /* Call legacy weak Abort complete callback */
  1144. HAL_SMARTCARD_AbortCpltCallback(hsc);
  1145. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1146. }
  1147. return HAL_OK;
  1148. }
  1149. /**
  1150. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1151. * @param hsc SMARTCARD handle.
  1152. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1153. * This procedure performs following operations :
  1154. * - Disable SMARTCARD Interrupts (Tx)
  1155. * - Disable the DMA transfer in the peripheral register (if enabled)
  1156. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1157. * - Set handle State to READY
  1158. * - At abort completion, call user abort complete callback
  1159. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1160. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1161. * @retval HAL status
  1162. */
  1163. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
  1164. {
  1165. /* Disable TXEIE and TCIE interrupts */
  1166. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1167. /* Disable the SMARTCARD DMA Tx request if enabled */
  1168. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  1169. {
  1170. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1171. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1172. if(hsc->hdmatx != NULL)
  1173. {
  1174. /* Set the SMARTCARD DMA Abort callback :
  1175. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1176. hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
  1177. /* Abort DMA TX */
  1178. if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
  1179. {
  1180. /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
  1181. hsc->hdmatx->XferAbortCallback(hsc->hdmatx);
  1182. }
  1183. }
  1184. else
  1185. {
  1186. /* Reset Tx transfer counter */
  1187. hsc->TxXferCount = 0x00U;
  1188. /* Restore hsc->gState to Ready */
  1189. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1190. /* As no DMA to be aborted, call directly user Abort complete callback */
  1191. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1192. /* Call registered Abort Transmit Complete Callback */
  1193. hsc->AbortTransmitCpltCallback(hsc);
  1194. #else
  1195. /* Call legacy weak Abort Transmit Complete Callback */
  1196. HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
  1197. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1198. }
  1199. }
  1200. else
  1201. {
  1202. /* Reset Tx transfer counter */
  1203. hsc->TxXferCount = 0x00U;
  1204. /* Restore hsc->gState to Ready */
  1205. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1206. /* As no DMA to be aborted, call directly user Abort complete callback */
  1207. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1208. /* Call registered Abort Transmit Complete Callback */
  1209. hsc->AbortTransmitCpltCallback(hsc);
  1210. #else
  1211. /* Call legacy weak Abort Transmit Complete Callback */
  1212. HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
  1213. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1214. }
  1215. return HAL_OK;
  1216. }
  1217. /**
  1218. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1219. * @param hsc SMARTCARD handle.
  1220. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1221. * This procedure performs following operations :
  1222. * - Disable SMARTCARD Interrupts (Rx)
  1223. * - Disable the DMA transfer in the peripheral register (if enabled)
  1224. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1225. * - Set handle State to READY
  1226. * - At abort completion, call user abort complete callback
  1227. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1228. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1229. * @retval HAL status
  1230. */
  1231. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc)
  1232. {
  1233. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1234. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1235. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1236. /* Disable the SMARTCARD DMA Rx request if enabled */
  1237. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1238. {
  1239. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1240. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1241. if(hsc->hdmarx != NULL)
  1242. {
  1243. /* Set the SMARTCARD DMA Abort callback :
  1244. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1245. hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
  1246. /* Abort DMA RX */
  1247. if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
  1248. {
  1249. /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
  1250. hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
  1251. }
  1252. }
  1253. else
  1254. {
  1255. /* Reset Rx transfer counter */
  1256. hsc->RxXferCount = 0x00U;
  1257. /* Restore hsc->RxState to Ready */
  1258. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1259. /* As no DMA to be aborted, call directly user Abort complete callback */
  1260. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1261. /* Call registered Abort Receive Complete Callback */
  1262. hsc->AbortReceiveCpltCallback(hsc);
  1263. #else
  1264. /* Call legacy weak Abort Receive Complete Callback */
  1265. HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
  1266. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1267. }
  1268. }
  1269. else
  1270. {
  1271. /* Reset Rx transfer counter */
  1272. hsc->RxXferCount = 0x00U;
  1273. /* Restore hsc->RxState to Ready */
  1274. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1275. /* As no DMA to be aborted, call directly user Abort complete callback */
  1276. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1277. /* Call registered Abort Receive Complete Callback */
  1278. hsc->AbortReceiveCpltCallback(hsc);
  1279. #else
  1280. /* Call legacy weak Abort Receive Complete Callback */
  1281. HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
  1282. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1283. }
  1284. return HAL_OK;
  1285. }
  1286. /**
  1287. * @brief This function handles SMARTCARD interrupt request.
  1288. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1289. * the configuration information for SMARTCARD module.
  1290. * @retval None
  1291. */
  1292. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
  1293. {
  1294. uint32_t isrflags = READ_REG(hsc->Instance->SR);
  1295. uint32_t cr1its = READ_REG(hsc->Instance->CR1);
  1296. uint32_t cr3its = READ_REG(hsc->Instance->CR3);
  1297. uint32_t dmarequest = 0x00U;
  1298. uint32_t errorflags = 0x00U;
  1299. /* If no error occurs */
  1300. errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
  1301. if(errorflags == RESET)
  1302. {
  1303. /* SMARTCARD in mode Receiver -------------------------------------------------*/
  1304. if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1305. {
  1306. SMARTCARD_Receive_IT(hsc);
  1307. return;
  1308. }
  1309. }
  1310. /* If some errors occur */
  1311. if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
  1312. {
  1313. /* SMARTCARD parity error interrupt occurred ---------------------------*/
  1314. if(((isrflags & SMARTCARD_FLAG_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1315. {
  1316. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  1317. }
  1318. /* SMARTCARD frame error interrupt occurred ----------------------------*/
  1319. if(((isrflags & SMARTCARD_FLAG_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1320. {
  1321. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  1322. }
  1323. /* SMARTCARD noise error interrupt occurred ----------------------------*/
  1324. if(((isrflags & SMARTCARD_FLAG_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1325. {
  1326. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  1327. }
  1328. /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
  1329. if(((isrflags & SMARTCARD_FLAG_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
  1330. {
  1331. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  1332. }
  1333. /* Call the Error call Back in case of Errors --------------------------*/
  1334. if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  1335. {
  1336. /* SMARTCARD in mode Receiver ----------------------------------------*/
  1337. if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1338. {
  1339. SMARTCARD_Receive_IT(hsc);
  1340. }
  1341. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1342. consider error as blocking */
  1343. dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
  1344. if(((hsc->ErrorCode & HAL_SMARTCARD_ERROR_ORE) != RESET) || dmarequest)
  1345. {
  1346. /* Blocking error : transfer is aborted
  1347. Set the SMARTCARD state ready to be able to start again the process,
  1348. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1349. SMARTCARD_EndRxTransfer(hsc);
  1350. /* Disable the SMARTCARD DMA Rx request if enabled */
  1351. if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1352. {
  1353. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1354. /* Abort the SMARTCARD DMA Rx channel */
  1355. if(hsc->hdmarx != NULL)
  1356. {
  1357. /* Set the SMARTCARD DMA Abort callback :
  1358. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1359. hsc->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1360. if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
  1361. {
  1362. /* Call Directly XferAbortCallback function in case of error */
  1363. hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
  1364. }
  1365. }
  1366. else
  1367. {
  1368. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1369. /* Call registered user error callback */
  1370. hsc->ErrorCallback(hsc);
  1371. #else
  1372. /* Call legacy weak user error callback */
  1373. HAL_SMARTCARD_ErrorCallback(hsc);
  1374. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1375. }
  1376. }
  1377. else
  1378. {
  1379. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1380. /* Call registered user error callback */
  1381. hsc->ErrorCallback(hsc);
  1382. #else
  1383. /* Call legacy weak user error callback */
  1384. HAL_SMARTCARD_ErrorCallback(hsc);
  1385. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1386. }
  1387. }
  1388. else
  1389. {
  1390. /* Non Blocking error : transfer could go on.
  1391. Error is notified to user through user error callback */
  1392. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1393. /* Call registered user error callback */
  1394. hsc->ErrorCallback(hsc);
  1395. #else
  1396. /* Call legacy weak user error callback */
  1397. HAL_SMARTCARD_ErrorCallback(hsc);
  1398. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1399. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1400. }
  1401. }
  1402. return;
  1403. } /* End if some error occurs */
  1404. /* SMARTCARD in mode Transmitter ------------------------------------------*/
  1405. if(((isrflags & SMARTCARD_FLAG_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1406. {
  1407. SMARTCARD_Transmit_IT(hsc);
  1408. return;
  1409. }
  1410. /* SMARTCARD in mode Transmitter (transmission end) -----------------------*/
  1411. if(((isrflags & SMARTCARD_FLAG_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1412. {
  1413. SMARTCARD_EndTransmit_IT(hsc);
  1414. return;
  1415. }
  1416. }
  1417. /**
  1418. * @brief Tx Transfer completed callbacks
  1419. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1420. * the configuration information for SMARTCARD module.
  1421. * @retval None
  1422. */
  1423. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  1424. {
  1425. /* Prevent unused argument(s) compilation warning */
  1426. UNUSED(hsc);
  1427. /* NOTE : This function should not be modified, when the callback is needed,
  1428. the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
  1429. */
  1430. }
  1431. /**
  1432. * @brief Rx Transfer completed callback
  1433. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1434. * the configuration information for SMARTCARD module.
  1435. * @retval None
  1436. */
  1437. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  1438. {
  1439. /* Prevent unused argument(s) compilation warning */
  1440. UNUSED(hsc);
  1441. /* NOTE : This function should not be modified, when the callback is needed,
  1442. the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
  1443. */
  1444. }
  1445. /**
  1446. * @brief SMARTCARD error callback
  1447. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1448. * the configuration information for SMARTCARD module.
  1449. * @retval None
  1450. */
  1451. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
  1452. {
  1453. /* Prevent unused argument(s) compilation warning */
  1454. UNUSED(hsc);
  1455. /* NOTE : This function should not be modified, when the callback is needed,
  1456. the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
  1457. */
  1458. }
  1459. /**
  1460. * @brief SMARTCARD Abort Complete callback.
  1461. * @param hsc SMARTCARD handle.
  1462. * @retval None
  1463. */
  1464. __weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsc)
  1465. {
  1466. /* Prevent unused argument(s) compilation warning */
  1467. UNUSED(hsc);
  1468. /* NOTE : This function should not be modified, when the callback is needed,
  1469. the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
  1470. */
  1471. }
  1472. /**
  1473. * @brief SMARTCARD Abort Transmit Complete callback.
  1474. * @param hsc SMARTCARD handle.
  1475. * @retval None
  1476. */
  1477. __weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsc)
  1478. {
  1479. /* Prevent unused argument(s) compilation warning */
  1480. UNUSED(hsc);
  1481. /* NOTE : This function should not be modified, when the callback is needed,
  1482. the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
  1483. */
  1484. }
  1485. /**
  1486. * @brief SMARTCARD Abort Receive Complete callback.
  1487. * @param hsc SMARTCARD handle.
  1488. * @retval None
  1489. */
  1490. __weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsc)
  1491. {
  1492. /* Prevent unused argument(s) compilation warning */
  1493. UNUSED(hsc);
  1494. /* NOTE : This function should not be modified, when the callback is needed,
  1495. the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
  1496. */
  1497. }
  1498. /**
  1499. * @}
  1500. */
  1501. /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
  1502. * @brief SMARTCARD State and Errors functions
  1503. *
  1504. @verbatim
  1505. ===============================================================================
  1506. ##### Peripheral State and Errors functions #####
  1507. ===============================================================================
  1508. [..]
  1509. This subsection provides a set of functions allowing to control the SmartCard.
  1510. (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
  1511. (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
  1512. @endverbatim
  1513. * @{
  1514. */
  1515. /**
  1516. * @brief Return the SMARTCARD handle state
  1517. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1518. * the configuration information for SMARTCARD module.
  1519. * @retval HAL state
  1520. */
  1521. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(const SMARTCARD_HandleTypeDef *hsc)
  1522. {
  1523. uint32_t temp1= 0x00U, temp2 = 0x00U;
  1524. temp1 = hsc->gState;
  1525. temp2 = hsc->RxState;
  1526. return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
  1527. }
  1528. /**
  1529. * @brief Return the SMARTCARD error code
  1530. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1531. * the configuration information for the specified SMARTCARD.
  1532. * @retval SMARTCARD Error Code
  1533. */
  1534. uint32_t HAL_SMARTCARD_GetError(const SMARTCARD_HandleTypeDef *hsc)
  1535. {
  1536. return hsc->ErrorCode;
  1537. }
  1538. /**
  1539. * @}
  1540. */
  1541. /**
  1542. * @}
  1543. */
  1544. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  1545. * @{
  1546. */
  1547. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1548. /**
  1549. * @brief Initialize the callbacks to their default values.
  1550. * @param hsc SMARTCARD handle.
  1551. * @retval none
  1552. */
  1553. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc)
  1554. {
  1555. /* Init the SMARTCARD Callback settings */
  1556. hsc->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
  1557. hsc->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
  1558. hsc->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
  1559. hsc->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  1560. hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  1561. hsc->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
  1562. }
  1563. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  1564. /**
  1565. * @brief DMA SMARTCARD transmit process complete callback
  1566. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1567. * the configuration information for the specified DMA module.
  1568. * @retval None
  1569. */
  1570. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1571. {
  1572. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1573. hsc->TxXferCount = 0U;
  1574. /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1575. in the USART CR3 register */
  1576. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1577. /* Enable the SMARTCARD Transmit Complete Interrupt */
  1578. SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
  1579. }
  1580. /**
  1581. * @brief DMA SMARTCARD receive process complete callback
  1582. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1583. * the configuration information for the specified DMA module.
  1584. * @retval None
  1585. */
  1586. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1587. {
  1588. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1589. hsc->RxXferCount = 0U;
  1590. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1591. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1592. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1593. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1594. in the USART CR3 register */
  1595. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1596. /* At end of Rx process, restore hsc->RxState to Ready */
  1597. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1598. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1599. /* Call registered Rx complete callback */
  1600. hsc->RxCpltCallback(hsc);
  1601. #else
  1602. /* Call legacy weak Rx complete callback */
  1603. HAL_SMARTCARD_RxCpltCallback(hsc);
  1604. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1605. }
  1606. /**
  1607. * @brief DMA SMARTCARD communication error callback
  1608. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1609. * the configuration information for the specified DMA module.
  1610. * @retval None
  1611. */
  1612. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  1613. {
  1614. uint32_t dmarequest = 0x00U;
  1615. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1616. hsc->RxXferCount = 0U;
  1617. hsc->TxXferCount = 0U;
  1618. hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1619. /* Stop SMARTCARD DMA Tx request if ongoing */
  1620. dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT);
  1621. if((hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX) && dmarequest)
  1622. {
  1623. SMARTCARD_EndTxTransfer(hsc);
  1624. }
  1625. /* Stop SMARTCARD DMA Rx request if ongoing */
  1626. dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
  1627. if((hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX) && dmarequest)
  1628. {
  1629. SMARTCARD_EndRxTransfer(hsc);
  1630. }
  1631. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1632. /* Call registered user error callback */
  1633. hsc->ErrorCallback(hsc);
  1634. #else
  1635. /* Call legacy weak user error callback */
  1636. HAL_SMARTCARD_ErrorCallback(hsc);
  1637. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1638. }
  1639. /**
  1640. * @brief This function handles SMARTCARD Communication Timeout. It waits
  1641. * until a flag is no longer in the specified status.
  1642. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1643. * the configuration information for SMARTCARD module.
  1644. * @param Flag Specifies the SMARTCARD flag to check.
  1645. * @param Status The actual Flag status (SET or RESET).
  1646. * @param Timeout Timeout duration
  1647. * @param Tickstart Tick start value
  1648. * @retval HAL status
  1649. */
  1650. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1651. {
  1652. /* Wait until flag is set */
  1653. while((__HAL_SMARTCARD_GET_FLAG(hsc, Flag) ? SET : RESET) == Status)
  1654. {
  1655. /* Check for the Timeout */
  1656. if(Timeout != HAL_MAX_DELAY)
  1657. {
  1658. if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
  1659. {
  1660. /* Disable TXE and RXNE interrupts for the interrupt process */
  1661. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
  1662. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
  1663. hsc->gState= HAL_SMARTCARD_STATE_READY;
  1664. hsc->RxState= HAL_SMARTCARD_STATE_READY;
  1665. /* Process Unlocked */
  1666. __HAL_UNLOCK(hsc);
  1667. return HAL_TIMEOUT;
  1668. }
  1669. }
  1670. }
  1671. return HAL_OK;
  1672. }
  1673. /**
  1674. * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
  1675. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1676. * the configuration information for SMARTCARD module.
  1677. * @retval None
  1678. */
  1679. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc)
  1680. {
  1681. /* At end of Tx process, restore hsc->gState to Ready */
  1682. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1683. /* Disable TXEIE and TCIE interrupts */
  1684. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1685. }
  1686. /**
  1687. * @brief End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
  1688. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1689. * the configuration information for SMARTCARD module.
  1690. * @retval None
  1691. */
  1692. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc)
  1693. {
  1694. /* At end of Rx process, restore hsc->RxState to Ready */
  1695. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1696. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1697. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1698. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1699. }
  1700. /**
  1701. * @brief Send an amount of data in non blocking mode
  1702. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1703. * the configuration information for SMARTCARD module.
  1704. * @retval HAL status
  1705. */
  1706. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
  1707. {
  1708. /* Check that a Tx process is ongoing */
  1709. if(hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1710. {
  1711. hsc->Instance->DR = (uint8_t)(*hsc->pTxBuffPtr & 0xFFU);
  1712. hsc->pTxBuffPtr++;
  1713. if(--hsc->TxXferCount == 0U)
  1714. {
  1715. /* Disable the SMARTCARD Transmit data register empty Interrupt */
  1716. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
  1717. /* Enable the SMARTCARD Transmit Complete Interrupt */
  1718. SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
  1719. }
  1720. return HAL_OK;
  1721. }
  1722. else
  1723. {
  1724. return HAL_BUSY;
  1725. }
  1726. }
  1727. /**
  1728. * @brief Wraps up transmission in non blocking mode.
  1729. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1730. * the configuration information for the specified SMARTCARD module.
  1731. * @retval HAL status
  1732. */
  1733. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
  1734. {
  1735. /* Disable the SMARTCARD Transmit Complete Interrupt */
  1736. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
  1737. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1738. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1739. /* Tx process is ended, restore hsc->gState to Ready */
  1740. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1741. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1742. /* Call registered Tx complete callback */
  1743. hsc->TxCpltCallback(hsc);
  1744. #else
  1745. /* Call legacy weak Tx complete callback */
  1746. HAL_SMARTCARD_TxCpltCallback(hsc);
  1747. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1748. return HAL_OK;
  1749. }
  1750. /**
  1751. * @brief Receive an amount of data in non blocking mode
  1752. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1753. * the configuration information for SMARTCARD module.
  1754. * @retval HAL status
  1755. */
  1756. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
  1757. {
  1758. /* Check that a Rx process is ongoing */
  1759. if(hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  1760. {
  1761. *hsc->pRxBuffPtr = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
  1762. hsc->pRxBuffPtr++;
  1763. if(--hsc->RxXferCount == 0U)
  1764. {
  1765. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
  1766. /* Disable the SMARTCARD Parity Error Interrupt */
  1767. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  1768. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1769. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1770. /* Rx process is completed, restore hsc->RxState to Ready */
  1771. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1772. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1773. /* Call registered Rx complete callback */
  1774. hsc->RxCpltCallback(hsc);
  1775. #else
  1776. /* Call legacy weak Rx complete callback */
  1777. HAL_SMARTCARD_RxCpltCallback(hsc);
  1778. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1779. return HAL_OK;
  1780. }
  1781. return HAL_OK;
  1782. }
  1783. else
  1784. {
  1785. return HAL_BUSY;
  1786. }
  1787. }
  1788. /**
  1789. * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
  1790. * (To be called at end of DMA Abort procedure following error occurrence).
  1791. * @param hdma DMA handle.
  1792. * @retval None
  1793. */
  1794. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1795. {
  1796. SMARTCARD_HandleTypeDef* hsc = (SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1797. hsc->RxXferCount = 0x00U;
  1798. hsc->TxXferCount = 0x00U;
  1799. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1800. /* Call registered user error callback */
  1801. hsc->ErrorCallback(hsc);
  1802. #else
  1803. /* Call legacy weak user error callback */
  1804. HAL_SMARTCARD_ErrorCallback(hsc);
  1805. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1806. }
  1807. /**
  1808. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
  1809. * (To be called at end of DMA Tx Abort procedure following user abort request).
  1810. * @note When this callback is executed, User Abort complete call back is called only if no
  1811. * Abort still ongoing for Rx DMA Handle.
  1812. * @param hdma DMA handle.
  1813. * @retval None
  1814. */
  1815. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  1816. {
  1817. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1818. hsc->hdmatx->XferAbortCallback = NULL;
  1819. /* Check if an Abort process is still ongoing */
  1820. if(hsc->hdmarx != NULL)
  1821. {
  1822. if(hsc->hdmarx->XferAbortCallback != NULL)
  1823. {
  1824. return;
  1825. }
  1826. }
  1827. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1828. hsc->TxXferCount = 0x00U;
  1829. hsc->RxXferCount = 0x00U;
  1830. /* Reset ErrorCode */
  1831. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1832. /* Restore hsc->gState and hsc->RxState to Ready */
  1833. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1834. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1835. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1836. /* Call registered Abort complete callback */
  1837. hsc->AbortCpltCallback(hsc);
  1838. #else
  1839. /* Call legacy weak Abort complete callback */
  1840. HAL_SMARTCARD_AbortCpltCallback(hsc);
  1841. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1842. }
  1843. /**
  1844. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
  1845. * (To be called at end of DMA Rx Abort procedure following user abort request).
  1846. * @note When this callback is executed, User Abort complete call back is called only if no
  1847. * Abort still ongoing for Tx DMA Handle.
  1848. * @param hdma DMA handle.
  1849. * @retval None
  1850. */
  1851. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  1852. {
  1853. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1854. hsc->hdmarx->XferAbortCallback = NULL;
  1855. /* Check if an Abort process is still ongoing */
  1856. if(hsc->hdmatx != NULL)
  1857. {
  1858. if(hsc->hdmatx->XferAbortCallback != NULL)
  1859. {
  1860. return;
  1861. }
  1862. }
  1863. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1864. hsc->TxXferCount = 0x00U;
  1865. hsc->RxXferCount = 0x00U;
  1866. /* Reset ErrorCode */
  1867. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1868. /* Restore hsc->gState and hsc->RxState to Ready */
  1869. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1870. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1871. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1872. /* Call registered Abort complete callback */
  1873. hsc->AbortCpltCallback(hsc);
  1874. #else
  1875. /* Call legacy weak Abort complete callback */
  1876. HAL_SMARTCARD_AbortCpltCallback(hsc);
  1877. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1878. }
  1879. /**
  1880. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
  1881. * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
  1882. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  1883. * and leads to user Tx Abort Complete callback execution).
  1884. * @param hdma DMA handle.
  1885. * @retval None
  1886. */
  1887. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1888. {
  1889. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1890. hsc->TxXferCount = 0x00U;
  1891. /* Restore hsc->gState to Ready */
  1892. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1893. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1894. /* Call registered Abort Transmit Complete Callback */
  1895. hsc->AbortTransmitCpltCallback(hsc);
  1896. #else
  1897. /* Call legacy weak Abort Transmit Complete Callback */
  1898. HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
  1899. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1900. }
  1901. /**
  1902. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
  1903. * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
  1904. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  1905. * and leads to user Rx Abort Complete callback execution).
  1906. * @param hdma DMA handle.
  1907. * @retval None
  1908. */
  1909. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1910. {
  1911. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1912. hsc->RxXferCount = 0x00U;
  1913. /* Restore hsc->RxState to Ready */
  1914. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1915. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1916. /* Call registered Abort Receive Complete Callback */
  1917. hsc->AbortReceiveCpltCallback(hsc);
  1918. #else
  1919. /* Call legacy weak Abort Receive Complete Callback */
  1920. HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
  1921. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1922. }
  1923. /**
  1924. * @brief Configure the SMARTCARD peripheral
  1925. * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1926. * the configuration information for SMARTCARD module.
  1927. * @retval None
  1928. */
  1929. static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
  1930. {
  1931. uint32_t tmpreg = 0x00U;
  1932. uint32_t pclk;
  1933. /* Check the parameters */
  1934. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  1935. assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
  1936. assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
  1937. assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
  1938. assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
  1939. assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
  1940. assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
  1941. assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
  1942. assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
  1943. assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  1944. /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
  1945. receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
  1946. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1947. /*---------------------------- USART CR2 Configuration ---------------------*/
  1948. tmpreg = hsc->Instance->CR2;
  1949. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  1950. tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
  1951. /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
  1952. /* Set CPOL bit according to hsc->Init.CLKPolarity value */
  1953. /* Set CPHA bit according to hsc->Init.CLKPhase value */
  1954. /* Set LBCL bit according to hsc->Init.CLKLastBit value */
  1955. /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
  1956. tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
  1957. hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits);
  1958. /* Write to USART CR2 */
  1959. WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
  1960. tmpreg = hsc->Instance->CR2;
  1961. /* Clear STOP[13:12] bits */
  1962. tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  1963. /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
  1964. tmpreg |= (uint32_t)(hsc->Init.StopBits);
  1965. /* Write to USART CR2 */
  1966. WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
  1967. /*-------------------------- USART CR1 Configuration -----------------------*/
  1968. tmpreg = hsc->Instance->CR1;
  1969. /* Clear M, PCE, PS, TE and RE bits */
  1970. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  1971. USART_CR1_RE));
  1972. /* Configure the SMARTCARD Word Length, Parity and mode:
  1973. Set the M bits according to hsc->Init.WordLength value
  1974. Set PCE and PS bits according to hsc->Init.Parity value
  1975. Set TE and RE bits according to hsc->Init.Mode value */
  1976. tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
  1977. /* Write to USART CR1 */
  1978. WRITE_REG(hsc->Instance->CR1, (uint32_t)tmpreg);
  1979. /*-------------------------- USART CR3 Configuration -----------------------*/
  1980. /* Clear CTSE and RTSE bits */
  1981. CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
  1982. /*-------------------------- USART BRR Configuration -----------------------*/
  1983. if(hsc->Instance == USART1)
  1984. {
  1985. pclk = HAL_RCC_GetPCLK2Freq();
  1986. hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
  1987. }
  1988. else
  1989. {
  1990. pclk = HAL_RCC_GetPCLK1Freq();
  1991. hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
  1992. }
  1993. }
  1994. /**
  1995. * @}
  1996. */
  1997. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  1998. /**
  1999. * @}
  2000. */
  2001. /**
  2002. * @}
  2003. */