stm32f1xx_hal_smartcard.c 82 KB

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