stm32f4xx_hal_smartcard.c 83 KB

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