stm32f1xx_hal_smartcard.c 83 KB

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