You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_smartcard.c 82KB

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