stm32f3xx_hal_smartcard.c 90 KB

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