stm32f1xx_hal_smartcard.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_smartcard.c
  4. * @author MCD Application Team
  5. * @brief SMARTCARD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the SMARTCARD peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Errors functions
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The SMARTCARD HAL driver can be used as follows:
  18. (#) Declare a SMARTCARD_HandleTypeDef handle structure.
  19. (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  20. (##) Enable the USARTx interface clock.
  21. (##) SMARTCARD pins configuration:
  22. (+++) Enable the clock for the SMARTCARD GPIOs.
  23. (+++) Configure the SMARTCARD pins as alternate function pull-up.
  24. (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  25. and HAL_SMARTCARD_Receive_IT() APIs):
  26. (+++) Configure the USARTx interrupt priority.
  27. (+++) Enable the NVIC USART IRQ handle.
  28. (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  29. and HAL_SMARTCARD_Receive_DMA() APIs):
  30. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  31. (+++) Enable the DMAx interface clock.
  32. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  33. (+++) Configure the DMA Tx/Rx channel.
  34. (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  35. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  36. (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
  37. (used for last byte sending completion detection in DMA non circular mode)
  38. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  39. flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
  40. (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
  41. (++) These APIs configure also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  42. by calling the customized HAL_SMARTCARD_MspInit() API.
  43. [..]
  44. (@)The specific SMARTCARD interrupts (Transmission complete interrupt,
  45. RXNE interrupt and Error Interrupts) will be managed using the macros
  46. __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
  47. [..]
  48. Three operation modes are available within this driver:
  49. *** Polling mode IO operation ***
  50. =================================
  51. [..]
  52. (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  53. (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  54. *** Interrupt mode IO operation ***
  55. ===================================
  56. [..]
  57. (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
  58. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  59. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  60. (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
  61. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  62. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  63. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  64. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  65. *** DMA mode IO operation ***
  66. ==============================
  67. [..]
  68. (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  69. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  70. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  71. (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  72. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  73. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  74. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  75. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  76. *** SMARTCARD HAL driver macros list ***
  77. =============================================
  78. [..]
  79. Below the list of most used macros in SMARTCARD HAL driver.
  80. (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
  81. (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
  82. (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
  83. (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
  84. (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
  85. (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
  86. [..]
  87. (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
  88. @endverbatim
  89. [..]
  90. (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
  91. in the data register is transmitted but is changed by the parity bit.
  92. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  93. the possible SMARTCARD frame formats are as listed in the following table:
  94. +-------------------------------------------------------------+
  95. | M bit | PCE bit | SMARTCARD frame |
  96. |---------------------|---------------------------------------|
  97. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  98. +-------------------------------------------------------------+
  99. ******************************************************************************
  100. * @attention
  101. *
  102. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  103. *
  104. * Redistribution and use in source and binary forms, with or without modification,
  105. * are permitted provided that the following conditions are met:
  106. * 1. Redistributions of source code must retain the above copyright notice,
  107. * this list of conditions and the following disclaimer.
  108. * 2. Redistributions in binary form must reproduce the above copyright notice,
  109. * this list of conditions and the following disclaimer in the documentation
  110. * and/or other materials provided with the distribution.
  111. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  112. * may be used to endorse or promote products derived from this software
  113. * without specific prior written permission.
  114. *
  115. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  116. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  117. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  118. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  119. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  120. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  121. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  122. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  123. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  124. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  125. *
  126. ******************************************************************************
  127. */
  128. /* Includes ------------------------------------------------------------------*/
  129. #include "stm32f1xx_hal.h"
  130. /** @addtogroup STM32F1xx_HAL_Driver
  131. * @{
  132. */
  133. /** @defgroup SMARTCARD SMARTCARD
  134. * @brief HAL SMARTCARD module driver
  135. * @{
  136. */
  137. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  138. /* Private typedef -----------------------------------------------------------*/
  139. /* Private define ------------------------------------------------------------*/
  140. /** @addtogroup SMARTCARD_Private_Constants
  141. * @{
  142. */
  143. /**
  144. * @}
  145. */
  146. /* Private macro -------------------------------------------------------------*/
  147. /* Private variables ---------------------------------------------------------*/
  148. /* Private function prototypes -----------------------------------------------*/
  149. /** @addtogroup SMARTCARD_Private_Functions
  150. * @{
  151. */
  152. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc);
  153. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc);
  154. static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc);
  155. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
  156. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  157. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
  158. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  159. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  160. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  161. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  162. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  163. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  164. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  165. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  166. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  167. /**
  168. * @}
  169. */
  170. /* Exported functions --------------------------------------------------------*/
  171. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  172. * @{
  173. */
  174. /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard 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 USART
  183. in Smartcard mode.
  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. (+) For the Smartcard mode only these parameters can be configured:
  193. (++) Baud Rate
  194. (++) Word Length => Should be 9 bits (8 bits + parity)
  195. (++) Stop Bit
  196. (++) Parity: => Should be enabled
  197. (++) USART polarity
  198. (++) USART phase
  199. (++) USART LastBit
  200. (++) Receiver/transmitter modes
  201. (++) Prescaler
  202. (++) GuardTime
  203. (++) NACKState: The Smartcard NACK state
  204. (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
  205. (++) Word Length = 9 Bits
  206. (++) 1.5 Stop Bit
  207. (++) Even parity
  208. (++) BaudRate = 12096 baud
  209. (++) Tx and Rx enabled
  210. [..]
  211. Please refer to the ISO 7816-3 specification for more details.
  212. [..]
  213. (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
  214. to use 1.5 stop bits for both transmitting and receiving to avoid switching
  215. between the two configurations.
  216. [..]
  217. The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
  218. procedure (details for the procedure are available in reference manual (RM0329)).
  219. @endverbatim
  220. * @{
  221. */
  222. /**
  223. * @brief Initializes the SmartCard mode according to the specified
  224. * parameters in the SMARTCARD_InitTypeDef and create the associated handle.
  225. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  226. * the configuration information for SMARTCARD module.
  227. * @retval HAL status
  228. */
  229. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
  230. {
  231. /* Check the SMARTCARD handle allocation */
  232. if (hsc == NULL)
  233. {
  234. return HAL_ERROR;
  235. }
  236. /* Check the parameters */
  237. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  238. if (hsc->gState == HAL_SMARTCARD_STATE_RESET)
  239. {
  240. /* Allocate lock resource and initialize it */
  241. hsc->Lock = HAL_UNLOCKED;
  242. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  243. HAL_SMARTCARD_MspInit(hsc);
  244. }
  245. hsc->gState = HAL_SMARTCARD_STATE_BUSY;
  246. /* Set the Prescaler */
  247. MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
  248. /* Set the Guard Time */
  249. MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime) << 8U));
  250. /* Set the Smartcard Communication parameters */
  251. SMARTCARD_SetConfig(hsc);
  252. /* In SmartCard mode, the following bits must be kept cleared:
  253. - LINEN bit in the USART_CR2 register
  254. - HDSEL and IREN bits in the USART_CR3 register.*/
  255. CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
  256. CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
  257. /* Enable the SMARTCARD Parity Error Interrupt */
  258. SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  259. /* Enable the SMARTCARD Framing Error Interrupt */
  260. SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  261. /* Enable the Peripheral */
  262. __HAL_SMARTCARD_ENABLE(hsc);
  263. /* Configure the Smartcard NACK state */
  264. MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
  265. /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  266. hsc->Instance->CR3 |= (USART_CR3_SCEN);
  267. /* Initialize the SMARTCARD state*/
  268. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  269. hsc->gState = HAL_SMARTCARD_STATE_READY;
  270. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  271. return HAL_OK;
  272. }
  273. /**
  274. * @brief DeInitializes the USART SmartCard peripheral
  275. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  276. * the configuration information for SMARTCARD module.
  277. * @retval HAL status
  278. */
  279. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
  280. {
  281. /* Check the SMARTCARD handle allocation */
  282. if (hsc == NULL)
  283. {
  284. return HAL_ERROR;
  285. }
  286. /* Check the parameters */
  287. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  288. hsc->gState = HAL_SMARTCARD_STATE_BUSY;
  289. /* DeInit the low level hardware */
  290. HAL_SMARTCARD_MspDeInit(hsc);
  291. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  292. hsc->gState = HAL_SMARTCARD_STATE_RESET;
  293. hsc->RxState = HAL_SMARTCARD_STATE_RESET;
  294. /* Release Lock */
  295. __HAL_UNLOCK(hsc);
  296. return HAL_OK;
  297. }
  298. /**
  299. * @brief SMARTCARD MSP Init.
  300. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  301. * the configuration information for SMARTCARD module.
  302. * @retval None
  303. */
  304. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
  305. {
  306. /* Prevent unused argument(s) compilation warning */
  307. UNUSED(hsc);
  308. /* NOTE: This function should not be modified, when the callback is needed,
  309. the HAL_SMARTCARD_MspInit could be implemented in the user file
  310. */
  311. }
  312. /**
  313. * @brief SMARTCARD MSP DeInit
  314. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  315. * the configuration information for SMARTCARD module.
  316. * @retval None
  317. */
  318. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
  319. {
  320. /* Prevent unused argument(s) compilation warning */
  321. UNUSED(hsc);
  322. /* NOTE: This function should not be modified, when the callback is needed,
  323. the HAL_SMARTCARD_MspDeInit could be implemented in the user file
  324. */
  325. }
  326. /**
  327. * @}
  328. */
  329. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  330. * @brief SMARTCARD Transmit and Receive functions
  331. *
  332. @verbatim
  333. ===============================================================================
  334. ##### IO operation functions #####
  335. ===============================================================================
  336. [..]
  337. This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  338. [..]
  339. (#) Smartcard is a single wire half duplex communication protocol.
  340. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  341. defined in the ISO 7816-3 standard.
  342. (#) The USART should be configured as:
  343. (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  344. (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  345. (#) There are two modes of transfer:
  346. (++) Blocking mode: The communication is performed in polling mode.
  347. The HAL status of all data processing is returned by the same function
  348. after finishing transfer.
  349. (++) Non Blocking mode: The communication is performed using Interrupts
  350. or DMA, These APIs return the HAL status.
  351. The end of the data processing will be indicated through the
  352. dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  353. using DMA mode.
  354. The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  355. will be executed respectively at the end of the Transmit or Receive process
  356. The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
  357. (#) Blocking mode APIs are :
  358. (++) HAL_SMARTCARD_Transmit()
  359. (++) HAL_SMARTCARD_Receive()
  360. (#) Non Blocking mode APIs with Interrupt are :
  361. (++) HAL_SMARTCARD_Transmit_IT()
  362. (++) HAL_SMARTCARD_Receive_IT()
  363. (++) HAL_SMARTCARD_IRQHandler()
  364. (#) Non Blocking mode functions with DMA are :
  365. (++) HAL_SMARTCARD_Transmit_DMA()
  366. (++) HAL_SMARTCARD_Receive_DMA()
  367. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  368. (++) HAL_SMARTCARD_TxCpltCallback()
  369. (++) HAL_SMARTCARD_RxCpltCallback()
  370. (++) HAL_SMARTCARD_ErrorCallback()
  371. @endverbatim
  372. * @{
  373. */
  374. /**
  375. * @brief Send an amount of data in blocking mode
  376. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  377. * the configuration information for SMARTCARD module.
  378. * @param pData: pointer to data buffer
  379. * @param Size: amount of data to be sent
  380. * @param Timeout: Timeout duration
  381. * @retval HAL status
  382. */
  383. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  384. {
  385. uint32_t tickstart = 0U;
  386. if (hsc->gState == HAL_SMARTCARD_STATE_READY)
  387. {
  388. if ((pData == NULL) || (Size == 0U))
  389. {
  390. return HAL_ERROR;
  391. }
  392. /* Process Locked */
  393. __HAL_LOCK(hsc);
  394. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  395. hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  396. /* Init tickstart for timeout managment */
  397. tickstart = HAL_GetTick();
  398. hsc->TxXferSize = Size;
  399. hsc->TxXferCount = Size;
  400. while (hsc->TxXferCount > 0U)
  401. {
  402. hsc->TxXferCount--;
  403. if (SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  404. {
  405. return HAL_TIMEOUT;
  406. }
  407. hsc->Instance->DR = *(uint8_t *) pData;
  408. pData += 1U;
  409. }
  410. if (SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  411. {
  412. return HAL_TIMEOUT;
  413. }
  414. /* At end of Tx process, restore hsc->gState to Ready */
  415. hsc->gState = HAL_SMARTCARD_STATE_READY;
  416. /* Process Unlocked */
  417. __HAL_UNLOCK(hsc);
  418. return HAL_OK;
  419. }
  420. else
  421. {
  422. return HAL_BUSY;
  423. }
  424. }
  425. /**
  426. * @brief Receive an amount of data in blocking mode
  427. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  428. * the configuration information for SMARTCARD module.
  429. * @param pData: pointer to data buffer
  430. * @param Size: amount of data to be received
  431. * @param Timeout: Timeout duration
  432. * @retval HAL status
  433. */
  434. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  435. {
  436. uint32_t tickstart = 0U;
  437. if (hsc->RxState == HAL_SMARTCARD_STATE_READY)
  438. {
  439. if ((pData == NULL) || (Size == 0U))
  440. {
  441. return HAL_ERROR;
  442. }
  443. /* Process Locked */
  444. __HAL_LOCK(hsc);
  445. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  446. hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  447. /* Init tickstart for timeout managment */
  448. tickstart = HAL_GetTick();
  449. hsc->RxXferSize = Size;
  450. hsc->RxXferCount = Size;
  451. /* Check the remain data to be received */
  452. while (hsc->RxXferCount > 0U)
  453. {
  454. hsc->RxXferCount--;
  455. if (SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  456. {
  457. return HAL_TIMEOUT;
  458. }
  459. *(uint8_t *) pData = (uint8_t)hsc->Instance->DR;
  460. pData += 1U;
  461. }
  462. /* At end of Rx process, restore hsc->RxState to Ready */
  463. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  464. /* Process Unlocked */
  465. __HAL_UNLOCK(hsc);
  466. return HAL_OK;
  467. }
  468. else
  469. {
  470. return HAL_BUSY;
  471. }
  472. }
  473. /**
  474. * @brief Send an amount of data in non blocking mode
  475. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  476. * the configuration information for SMARTCARD module.
  477. * @param pData: pointer to data buffer
  478. * @param Size: amount of data to be sent
  479. * @retval HAL status
  480. */
  481. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  482. {
  483. /* Check that a Tx process is not already ongoing */
  484. if (hsc->gState == HAL_SMARTCARD_STATE_READY)
  485. {
  486. if ((pData == NULL) || (Size == 0U))
  487. {
  488. return HAL_ERROR;
  489. }
  490. /* Process Locked */
  491. __HAL_LOCK(hsc);
  492. hsc->pTxBuffPtr = pData;
  493. hsc->TxXferSize = Size;
  494. hsc->TxXferCount = Size;
  495. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  496. hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  497. /* Process Unlocked */
  498. __HAL_UNLOCK(hsc);
  499. /* Enable the SMARTCARD Parity Error Interrupt */
  500. SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  501. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  502. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  503. /* Enable the SMARTCARD Transmit data register empty Interrupt */
  504. SET_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
  505. return HAL_OK;
  506. }
  507. else
  508. {
  509. return HAL_BUSY;
  510. }
  511. }
  512. /**
  513. * @brief Receive an amount of data in non blocking mode
  514. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  515. * the configuration information for SMARTCARD module.
  516. * @param pData: pointer to data buffer
  517. * @param Size: amount of data to be received
  518. * @retval HAL status
  519. */
  520. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  521. {
  522. /* Check that a Rx process is not already ongoing */
  523. if (hsc->RxState == HAL_SMARTCARD_STATE_READY)
  524. {
  525. if ((pData == NULL) || (Size == 0U))
  526. {
  527. return HAL_ERROR;
  528. }
  529. /* Process Locked */
  530. __HAL_LOCK(hsc);
  531. hsc->pRxBuffPtr = pData;
  532. hsc->RxXferSize = Size;
  533. hsc->RxXferCount = Size;
  534. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  535. hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  536. /* Process Unlocked */
  537. __HAL_UNLOCK(hsc);
  538. /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
  539. SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  540. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  541. SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  542. return HAL_OK;
  543. }
  544. else
  545. {
  546. return HAL_BUSY;
  547. }
  548. }
  549. /**
  550. * @brief Send an amount of data in non blocking mode
  551. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  552. * the configuration information for SMARTCARD module.
  553. * @param pData: pointer to data buffer
  554. * @param Size: amount of data to be sent
  555. * @retval HAL status
  556. */
  557. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  558. {
  559. uint32_t *tmp;
  560. /* Check that a Tx process is not already ongoing */
  561. if (hsc->gState == HAL_SMARTCARD_STATE_READY)
  562. {
  563. if ((pData == NULL) || (Size == 0U))
  564. {
  565. return HAL_ERROR;
  566. }
  567. /* Process Locked */
  568. __HAL_LOCK(hsc);
  569. hsc->pTxBuffPtr = pData;
  570. hsc->TxXferSize = Size;
  571. hsc->TxXferCount = Size;
  572. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  573. hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  574. /* Set the SMARTCARD DMA transfer complete callback */
  575. hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  576. /* Set the DMA error callback */
  577. hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  578. /* Set the DMA abort callback */
  579. hsc->hdmatx->XferAbortCallback = NULL;
  580. /* Enable the SMARTCARD transmit DMA Channel */
  581. tmp = (uint32_t *)&pData;
  582. HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t *)tmp, (uint32_t)&hsc->Instance->DR, Size);
  583. /* Clear the TC flag in the SR register by writing 0 to it */
  584. __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
  585. /* Process Unlocked */
  586. __HAL_UNLOCK(hsc);
  587. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  588. in the SMARTCARD CR3 register */
  589. SET_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  590. return HAL_OK;
  591. }
  592. else
  593. {
  594. return HAL_BUSY;
  595. }
  596. }
  597. /**
  598. * @brief Receive an amount of data in non blocking mode
  599. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  600. * the configuration information for SMARTCARD module.
  601. * @param pData: pointer to data buffer
  602. * @param Size: amount of data to be received
  603. * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
  604. * @retval HAL status
  605. */
  606. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  607. {
  608. uint32_t *tmp;
  609. /* Check that a Rx process is not already ongoing */
  610. if (hsc->RxState == HAL_SMARTCARD_STATE_READY)
  611. {
  612. if ((pData == NULL) || (Size == 0U))
  613. {
  614. return HAL_ERROR;
  615. }
  616. /* Process Locked */
  617. __HAL_LOCK(hsc);
  618. hsc->pRxBuffPtr = pData;
  619. hsc->RxXferSize = Size;
  620. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  621. hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  622. /* Set the SMARTCARD DMA transfer complete callback */
  623. hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  624. /* Set the DMA error callback */
  625. hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  626. /* Set the DMA abort callback */
  627. hsc->hdmatx->XferAbortCallback = NULL;
  628. /* Enable the DMA Channel */
  629. tmp = (uint32_t *)&pData;
  630. HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t *)tmp, Size);
  631. /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
  632. __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
  633. /* Process Unlocked */
  634. __HAL_UNLOCK(hsc);
  635. /* Enable the SMARTCARD Parity Error Interrupt */
  636. SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  637. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  638. SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  639. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  640. in the SMARTCARD CR3 register */
  641. SET_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  642. return HAL_OK;
  643. }
  644. else
  645. {
  646. return HAL_BUSY;
  647. }
  648. }
  649. /**
  650. * @brief Abort ongoing transfers (blocking mode).
  651. * @param hsc SMARTCARD handle.
  652. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  653. * This procedure performs following operations :
  654. * - Disable PPP Interrupts
  655. * - Disable the DMA transfer in the peripheral register (if enabled)
  656. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  657. * - Set handle State to READY
  658. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  659. * @retval HAL status
  660. */
  661. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc)
  662. {
  663. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  664. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  665. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  666. /* Disable the SMARTCARD DMA Tx request if enabled */
  667. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  668. {
  669. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  670. /* Abort the SMARTCARD DMA Tx channel: use blocking DMA Abort API (no callback) */
  671. if (hsc->hdmatx != NULL)
  672. {
  673. /* Set the SMARTCARD DMA Abort callback to Null.
  674. No call back execution at end of DMA abort procedure */
  675. hsc->hdmatx->XferAbortCallback = NULL;
  676. HAL_DMA_Abort(hsc->hdmatx);
  677. }
  678. }
  679. /* Disable the SMARTCARD DMA Rx request if enabled */
  680. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  681. {
  682. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  683. /* Abort the SMARTCARD DMA Rx channel: use blocking DMA Abort API (no callback) */
  684. if (hsc->hdmarx != NULL)
  685. {
  686. /* Set the SMARTCARD DMA Abort callback to Null.
  687. No call back execution at end of DMA abort procedure */
  688. hsc->hdmarx->XferAbortCallback = NULL;
  689. HAL_DMA_Abort(hsc->hdmarx);
  690. }
  691. }
  692. /* Reset Tx and Rx transfer counters */
  693. hsc->TxXferCount = 0x00U;
  694. hsc->RxXferCount = 0x00U;
  695. /* Reset ErrorCode */
  696. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  697. /* Restore hsc->RxState and hsc->gState to Ready */
  698. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  699. hsc->gState = HAL_SMARTCARD_STATE_READY;
  700. return HAL_OK;
  701. }
  702. /**
  703. * @brief Abort ongoing Transmit transfer (blocking mode).
  704. * @param hsc SMARTCARD handle.
  705. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  706. * This procedure performs following operations :
  707. * - Disable PPP Interrupts
  708. * - Disable the DMA transfer in the peripheral register (if enabled)
  709. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  710. * - Set handle State to READY
  711. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  712. * @retval HAL status
  713. */
  714. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc)
  715. {
  716. /* Disable TXEIE and TCIE interrupts */
  717. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  718. /* Disable the SMARTCARD DMA Tx request if enabled */
  719. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  720. {
  721. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  722. /* Abort the SMARTCARD DMA Tx channel: use blocking DMA Abort API (no callback) */
  723. if (hsc->hdmatx != NULL)
  724. {
  725. /* Set the SMARTCARD DMA Abort callback to Null.
  726. No call back execution at end of DMA abort procedure */
  727. hsc->hdmatx->XferAbortCallback = NULL;
  728. HAL_DMA_Abort(hsc->hdmatx);
  729. }
  730. }
  731. /* Reset Tx transfer counter */
  732. hsc->TxXferCount = 0x00U;
  733. /* Restore hsc->gState to Ready */
  734. hsc->gState = HAL_SMARTCARD_STATE_READY;
  735. return HAL_OK;
  736. }
  737. /**
  738. * @brief Abort ongoing Receive transfer (blocking mode).
  739. * @param hsc SMARTCARD handle.
  740. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  741. * This procedure performs following operations :
  742. * - Disable PPP Interrupts
  743. * - Disable the DMA transfer in the peripheral register (if enabled)
  744. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  745. * - Set handle State to READY
  746. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  747. * @retval HAL status
  748. */
  749. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc)
  750. {
  751. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  752. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  753. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  754. /* Disable the SMARTCARD DMA Rx request if enabled */
  755. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  756. {
  757. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  758. /* Abort the SMARTCARD DMA Rx channel: use blocking DMA Abort API (no callback) */
  759. if (hsc->hdmarx != NULL)
  760. {
  761. /* Set the SMARTCARD DMA Abort callback to Null.
  762. No call back execution at end of DMA abort procedure */
  763. hsc->hdmarx->XferAbortCallback = NULL;
  764. HAL_DMA_Abort(hsc->hdmarx);
  765. }
  766. }
  767. /* Reset Rx transfer counter */
  768. hsc->RxXferCount = 0x00U;
  769. /* Restore hsc->RxState to Ready */
  770. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  771. return HAL_OK;
  772. }
  773. /**
  774. * @brief Abort ongoing transfers (Interrupt mode).
  775. * @param hsc SMARTCARD handle.
  776. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  777. * This procedure performs following operations :
  778. * - Disable PPP Interrupts
  779. * - Disable the DMA transfer in the peripheral register (if enabled)
  780. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  781. * - Set handle State to READY
  782. * - At abort completion, call user abort complete callback
  783. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  784. * considered as completed only when user abort complete callback is executed (not when exiting function).
  785. * @retval HAL status
  786. */
  787. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc)
  788. {
  789. uint32_t AbortCplt = 0x01U;
  790. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  791. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  792. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  793. /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
  794. before any call to DMA Abort functions */
  795. /* DMA Tx Handle is valid */
  796. if (hsc->hdmatx != NULL)
  797. {
  798. /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
  799. Otherwise, set it to NULL */
  800. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  801. {
  802. hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
  803. }
  804. else
  805. {
  806. hsc->hdmatx->XferAbortCallback = NULL;
  807. }
  808. }
  809. /* DMA Rx Handle is valid */
  810. if (hsc->hdmarx != NULL)
  811. {
  812. /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
  813. Otherwise, set it to NULL */
  814. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  815. {
  816. hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
  817. }
  818. else
  819. {
  820. hsc->hdmarx->XferAbortCallback = NULL;
  821. }
  822. }
  823. /* Disable the SMARTCARD DMA Tx request if enabled */
  824. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  825. {
  826. /* Disable DMA Tx at SMARTCARD level */
  827. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  828. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  829. if (hsc->hdmatx != NULL)
  830. {
  831. /* SMARTCARD Tx DMA Abort callback has already been initialised :
  832. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  833. /* Abort DMA TX */
  834. if (HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
  835. {
  836. hsc->hdmatx->XferAbortCallback = NULL;
  837. }
  838. else
  839. {
  840. AbortCplt = 0x00U;
  841. }
  842. }
  843. }
  844. /* Disable the SMARTCARD DMA Rx request if enabled */
  845. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  846. {
  847. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  848. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  849. if (hsc->hdmarx != NULL)
  850. {
  851. /* SMARTCARD Rx DMA Abort callback has already been initialised :
  852. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  853. /* Abort DMA RX */
  854. if (HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
  855. {
  856. hsc->hdmarx->XferAbortCallback = NULL;
  857. AbortCplt = 0x01U;
  858. }
  859. else
  860. {
  861. AbortCplt = 0x00U;
  862. }
  863. }
  864. }
  865. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  866. if (AbortCplt == 0x01U)
  867. {
  868. /* Reset Tx and Rx transfer counters */
  869. hsc->TxXferCount = 0x00U;
  870. hsc->RxXferCount = 0x00U;
  871. /* Reset ErrorCode */
  872. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  873. /* Restore hsc->gState and hsc->RxState to Ready */
  874. hsc->gState = HAL_SMARTCARD_STATE_READY;
  875. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  876. /* As no DMA to be aborted, call directly user Abort complete callback */
  877. HAL_SMARTCARD_AbortCpltCallback(hsc);
  878. }
  879. return HAL_OK;
  880. }
  881. /**
  882. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  883. * @param hsc SMARTCARD handle.
  884. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  885. * This procedure performs following operations :
  886. * - Disable PPP Interrupts
  887. * - Disable the DMA transfer in the peripheral register (if enabled)
  888. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  889. * - Set handle State to READY
  890. * - At abort completion, call user abort complete callback
  891. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  892. * considered as completed only when user abort complete callback is executed (not when exiting function).
  893. * @retval HAL status
  894. */
  895. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
  896. {
  897. /* Disable TXEIE and TCIE interrupts */
  898. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  899. /* Disable the SMARTCARD DMA Tx request if enabled */
  900. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  901. {
  902. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  903. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  904. if (hsc->hdmatx != NULL)
  905. {
  906. /* Set the SMARTCARD DMA Abort callback :
  907. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  908. hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
  909. /* Abort DMA TX */
  910. if (HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
  911. {
  912. /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
  913. hsc->hdmatx->XferAbortCallback(hsc->hdmatx);
  914. }
  915. }
  916. else
  917. {
  918. /* Reset Tx transfer counter */
  919. hsc->TxXferCount = 0x00U;
  920. /* Restore hsc->gState to Ready */
  921. hsc->gState = HAL_SMARTCARD_STATE_READY;
  922. /* As no DMA to be aborted, call directly user Abort complete callback */
  923. HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
  924. }
  925. }
  926. else
  927. {
  928. /* Reset Tx transfer counter */
  929. hsc->TxXferCount = 0x00U;
  930. /* Restore hsc->gState to Ready */
  931. hsc->gState = HAL_SMARTCARD_STATE_READY;
  932. /* As no DMA to be aborted, call directly user Abort complete callback */
  933. HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
  934. }
  935. return HAL_OK;
  936. }
  937. /**
  938. * @brief Abort ongoing Receive transfer (Interrupt mode).
  939. * @param hsc SMARTCARD handle.
  940. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  941. * This procedure performs following operations :
  942. * - Disable PPP Interrupts
  943. * - Disable the DMA transfer in the peripheral register (if enabled)
  944. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  945. * - Set handle State to READY
  946. * - At abort completion, call user abort complete callback
  947. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  948. * considered as completed only when user abort complete callback is executed (not when exiting function).
  949. * @retval HAL status
  950. */
  951. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc)
  952. {
  953. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  954. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  955. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  956. /* Disable the SMARTCARD DMA Rx request if enabled */
  957. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  958. {
  959. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  960. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  961. if (hsc->hdmarx != NULL)
  962. {
  963. /* Set the SMARTCARD DMA Abort callback :
  964. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  965. hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
  966. /* Abort DMA RX */
  967. if (HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
  968. {
  969. /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
  970. hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
  971. }
  972. }
  973. else
  974. {
  975. /* Reset Rx transfer counter */
  976. hsc->RxXferCount = 0x00U;
  977. /* Restore hsc->RxState to Ready */
  978. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  979. /* As no DMA to be aborted, call directly user Abort complete callback */
  980. HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
  981. }
  982. }
  983. else
  984. {
  985. /* Reset Rx transfer counter */
  986. hsc->RxXferCount = 0x00U;
  987. /* Restore hsc->RxState to Ready */
  988. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  989. /* As no DMA to be aborted, call directly user Abort complete callback */
  990. HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
  991. }
  992. return HAL_OK;
  993. }
  994. /**
  995. * @brief This function handles SMARTCARD interrupt request.
  996. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  997. * the configuration information for SMARTCARD module.
  998. * @retval None
  999. */
  1000. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
  1001. {
  1002. uint32_t isrflags = READ_REG(hsc->Instance->SR);
  1003. uint32_t cr1its = READ_REG(hsc->Instance->CR1);
  1004. uint32_t cr3its = READ_REG(hsc->Instance->CR3);
  1005. uint32_t dmarequest = 0x00U;
  1006. uint32_t errorflags = 0x00U;
  1007. /* If no error occurs */
  1008. errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
  1009. if (errorflags == RESET)
  1010. {
  1011. /* SMARTCARD in mode Receiver -------------------------------------------------*/
  1012. if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1013. {
  1014. SMARTCARD_Receive_IT(hsc);
  1015. return;
  1016. }
  1017. }
  1018. /* If some errors occur */
  1019. if ((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
  1020. {
  1021. /* SMARTCARD parity error interrupt occurred ---------------------------*/
  1022. if (((isrflags & SMARTCARD_FLAG_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1023. {
  1024. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  1025. }
  1026. /* SMARTCARD noise error interrupt occurred ----------------------------*/
  1027. if (((isrflags & SMARTCARD_FLAG_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1028. {
  1029. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  1030. }
  1031. /* SMARTCARD frame error interrupt occurred ----------------------------*/
  1032. if (((isrflags & SMARTCARD_FLAG_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1033. {
  1034. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  1035. }
  1036. /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
  1037. if (((isrflags & SMARTCARD_FLAG_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1038. {
  1039. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  1040. }
  1041. /* Call SMARTCARD Error Call back function if need be ------------------*/
  1042. if (hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  1043. {
  1044. /* SMARTCARD in mode Receiver ----------------------------------------*/
  1045. if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1046. {
  1047. SMARTCARD_Receive_IT(hsc);
  1048. }
  1049. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1050. consider error as blocking */
  1051. dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
  1052. if (((hsc->ErrorCode & HAL_SMARTCARD_ERROR_ORE) != RESET) || dmarequest)
  1053. {
  1054. /* Blocking error : transfer is aborted
  1055. Set the SMARTCARD state ready to be able to start again the process,
  1056. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1057. SMARTCARD_EndRxTransfer(hsc);
  1058. /* Disable the SMARTCARD DMA Rx request if enabled */
  1059. if (HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1060. {
  1061. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1062. /* Abort the SMARTCARD DMA Rx channel */
  1063. if (hsc->hdmarx != NULL)
  1064. {
  1065. /* Set the SMARTCARD DMA Abort callback :
  1066. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1067. hsc->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1068. if (HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
  1069. {
  1070. /* Call Directly XferAbortCallback function in case of error */
  1071. hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
  1072. }
  1073. }
  1074. else
  1075. {
  1076. /* Call user error callback */
  1077. HAL_SMARTCARD_ErrorCallback(hsc);
  1078. }
  1079. }
  1080. else
  1081. {
  1082. /* Call user error callback */
  1083. HAL_SMARTCARD_ErrorCallback(hsc);
  1084. }
  1085. }
  1086. else
  1087. {
  1088. /* Non Blocking error : transfer could go on.
  1089. Error is notified to user through user error callback */
  1090. HAL_SMARTCARD_ErrorCallback(hsc);
  1091. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1092. }
  1093. }
  1094. return;
  1095. } /* End if some error occurs */
  1096. /* SMARTCARD in mode Transmitter -------------------------------------------*/
  1097. if (((isrflags & SMARTCARD_FLAG_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1098. {
  1099. SMARTCARD_Transmit_IT(hsc);
  1100. return;
  1101. }
  1102. /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  1103. if (((isrflags & SMARTCARD_FLAG_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1104. {
  1105. SMARTCARD_EndTransmit_IT(hsc);
  1106. return;
  1107. }
  1108. }
  1109. /**
  1110. * @brief Tx Transfer completed callbacks
  1111. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1112. * the configuration information for SMARTCARD module.
  1113. * @retval None
  1114. */
  1115. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  1116. {
  1117. /* Prevent unused argument(s) compilation warning */
  1118. UNUSED(hsc);
  1119. /* NOTE : This function Should not be modified, when the callback is needed,
  1120. the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
  1121. */
  1122. }
  1123. /**
  1124. * @brief Rx Transfer completed callbacks
  1125. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1126. * the configuration information for SMARTCARD module.
  1127. * @retval None
  1128. */
  1129. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  1130. {
  1131. /* Prevent unused argument(s) compilation warning */
  1132. UNUSED(hsc);
  1133. /* NOTE : This function Should not be modified, when the callback is needed,
  1134. the HAL_SMARTCARD_RxCpltCallback could be implemented in the user file
  1135. */
  1136. }
  1137. /**
  1138. * @brief SMARTCARD error callbacks
  1139. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1140. * the configuration information for SMARTCARD module.
  1141. * @retval None
  1142. */
  1143. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
  1144. {
  1145. /* Prevent unused argument(s) compilation warning */
  1146. UNUSED(hsc);
  1147. /* NOTE : This function Should not be modified, when the callback is needed,
  1148. the HAL_SMARTCARD_ErrorCallback could be implemented in the user file
  1149. */
  1150. }
  1151. /**
  1152. * @brief SMARTCARD Abort Complete callback.
  1153. * @param hsc SMARTCARD handle.
  1154. * @retval None
  1155. */
  1156. __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  1157. {
  1158. /* Prevent unused argument(s) compilation warning */
  1159. UNUSED(hsc);
  1160. /* NOTE : This function should not be modified, when the callback is needed,
  1161. the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
  1162. */
  1163. }
  1164. /**
  1165. * @brief SMARTCARD Abort Transmit Complete callback.
  1166. * @param hsc SMARTCARD handle.
  1167. * @retval None
  1168. */
  1169. __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  1170. {
  1171. /* Prevent unused argument(s) compilation warning */
  1172. UNUSED(hsc);
  1173. /* NOTE : This function should not be modified, when the callback is needed,
  1174. the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
  1175. */
  1176. }
  1177. /**
  1178. * @brief SMARTCARD Abort ReceiveComplete callback.
  1179. * @param hsc SMARTCARD handle.
  1180. * @retval None
  1181. */
  1182. __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  1183. {
  1184. /* Prevent unused argument(s) compilation warning */
  1185. UNUSED(hsc);
  1186. /* NOTE : This function should not be modified, when the callback is needed,
  1187. the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
  1188. */
  1189. }
  1190. /**
  1191. * @}
  1192. */
  1193. /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
  1194. * @brief SMARTCARD State and Errors functions
  1195. *
  1196. @verbatim
  1197. ===============================================================================
  1198. ##### Peripheral State and Errors functions #####
  1199. ===============================================================================
  1200. [..]
  1201. This subsection provides a set of functions allowing to control the SmartCard.
  1202. (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
  1203. (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
  1204. @endverbatim
  1205. * @{
  1206. */
  1207. /**
  1208. * @brief return the SMARTCARD state
  1209. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1210. * the configuration information for SMARTCARD module.
  1211. * @retval HAL state
  1212. */
  1213. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
  1214. {
  1215. uint32_t temp1 = 0x00U, temp2 = 0x00U;
  1216. temp1 = hsc->gState;
  1217. temp2 = hsc->RxState;
  1218. return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
  1219. }
  1220. /**
  1221. * @brief Return the SMARTCARD error code
  1222. * @param hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains
  1223. * the configuration information for the specified SMARTCARD.
  1224. * @retval SMARTCARD Error Code
  1225. */
  1226. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
  1227. {
  1228. return hsc->ErrorCode;
  1229. }
  1230. /**
  1231. * @}
  1232. */
  1233. /**
  1234. * @brief DMA SMARTCARD transmit process complete callback
  1235. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1236. * the configuration information for the specified DMA module.
  1237. * @retval None
  1238. */
  1239. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1240. {
  1241. SMARTCARD_HandleTypeDef *hsc = (SMARTCARD_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1242. hsc->TxXferCount = 0U;
  1243. /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1244. in the USART CR3 register */
  1245. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1246. /* Enable the SMARTCARD Transmit Complete Interrupt */
  1247. SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
  1248. }
  1249. /**
  1250. * @brief DMA SMARTCARD receive process complete callback
  1251. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1252. * the configuration information for the specified DMA module.
  1253. * @retval None
  1254. */
  1255. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1256. {
  1257. SMARTCARD_HandleTypeDef *hsc = (SMARTCARD_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1258. hsc->RxXferCount = 0U;
  1259. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1260. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1261. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1262. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1263. in the USART CR3 register */
  1264. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1265. /* At end of Rx process, restore hsc->RxState to Ready */
  1266. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1267. HAL_SMARTCARD_RxCpltCallback(hsc);
  1268. }
  1269. /**
  1270. * @brief DMA SMARTCARD communication error callback
  1271. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1272. * the configuration information for the specified DMA module.
  1273. * @retval None
  1274. */
  1275. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  1276. {
  1277. uint32_t dmarequest = 0x00U;
  1278. SMARTCARD_HandleTypeDef *hsc = (SMARTCARD_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1279. hsc->RxXferCount = 0U;
  1280. hsc->TxXferCount = 0U;
  1281. hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1282. /* Stop SMARTCARD DMA Tx request if ongoing */
  1283. dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT);
  1284. if ((hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX) && dmarequest)
  1285. {
  1286. SMARTCARD_EndTxTransfer(hsc);
  1287. }
  1288. /* Stop SMARTCARD DMA Rx request if ongoing */
  1289. dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
  1290. if ((hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX) && dmarequest)
  1291. {
  1292. SMARTCARD_EndRxTransfer(hsc);
  1293. }
  1294. HAL_SMARTCARD_ErrorCallback(hsc);
  1295. }
  1296. /**
  1297. * @brief This function handles SMARTCARD Communication Timeout.
  1298. * @param hsc: SMARTCARD handle
  1299. * @param Flag: specifies the SMARTCARD flag to check.
  1300. * @param Status: The new Flag status (SET or RESET).
  1301. * @param Timeout: Timeout duration
  1302. * @param Tickstart: tick start value
  1303. * @retval HAL status
  1304. */
  1305. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1306. {
  1307. /* Wait until flag is set */
  1308. while ((__HAL_SMARTCARD_GET_FLAG(hsc, Flag) ? SET : RESET) == Status)
  1309. {
  1310. /* Check for the Timeout */
  1311. if (Timeout != HAL_MAX_DELAY)
  1312. {
  1313. if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
  1314. {
  1315. /* Disable TXE and RXNE interrupts for the interrupt process */
  1316. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
  1317. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
  1318. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1319. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1320. /* Process Unlocked */
  1321. __HAL_UNLOCK(hsc);
  1322. return HAL_TIMEOUT;
  1323. }
  1324. }
  1325. }
  1326. return HAL_OK;
  1327. }
  1328. /**
  1329. * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
  1330. * @param hsc: SMARTCARD handle.
  1331. * @retval None
  1332. */
  1333. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc)
  1334. {
  1335. /* At end of Tx process, restore hsc->gState to Ready */
  1336. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1337. /* Disable TXEIE and TCIE interrupts */
  1338. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1339. }
  1340. /**
  1341. * @brief End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
  1342. * @param hsc: SMARTCARD handle.
  1343. * @retval None
  1344. */
  1345. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc)
  1346. {
  1347. /* At end of Rx process, restore hsc->RxState to Ready */
  1348. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1349. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1350. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1351. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1352. }
  1353. /**
  1354. * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
  1355. * (To be called at end of DMA Abort procedure following error occurrence).
  1356. * @param hdma DMA handle.
  1357. * @retval None
  1358. */
  1359. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1360. {
  1361. SMARTCARD_HandleTypeDef *hsc = (SMARTCARD_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1362. hsc->RxXferCount = 0x00U;
  1363. hsc->TxXferCount = 0x00U;
  1364. HAL_SMARTCARD_ErrorCallback(hsc);
  1365. }
  1366. /**
  1367. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
  1368. * (To be called at end of DMA Tx Abort procedure following user abort request).
  1369. * @note When this callback is executed, User Abort complete call back is called only if no
  1370. * Abort still ongoing for Rx DMA Handle.
  1371. * @param hdma DMA handle.
  1372. * @retval None
  1373. */
  1374. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  1375. {
  1376. SMARTCARD_HandleTypeDef *hsc = (SMARTCARD_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1377. hsc->hdmatx->XferAbortCallback = NULL;
  1378. /* Check if an Abort process is still ongoing */
  1379. if (hsc->hdmarx != NULL)
  1380. {
  1381. if (hsc->hdmarx->XferAbortCallback != NULL)
  1382. {
  1383. return;
  1384. }
  1385. }
  1386. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1387. hsc->TxXferCount = 0x00U;
  1388. hsc->RxXferCount = 0x00U;
  1389. /* Reset ErrorCode */
  1390. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1391. /* Restore hsc->gState and hsc->RxState to Ready */
  1392. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1393. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1394. /* Call user Abort complete callback */
  1395. HAL_SMARTCARD_AbortCpltCallback(hsc);
  1396. }
  1397. /**
  1398. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
  1399. * (To be called at end of DMA Rx Abort procedure following user abort request).
  1400. * @note When this callback is executed, User Abort complete call back is called only if no
  1401. * Abort still ongoing for Tx DMA Handle.
  1402. * @param hdma DMA handle.
  1403. * @retval None
  1404. */
  1405. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  1406. {
  1407. SMARTCARD_HandleTypeDef *hsc = (SMARTCARD_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1408. hsc->hdmarx->XferAbortCallback = NULL;
  1409. /* Check if an Abort process is still ongoing */
  1410. if (hsc->hdmatx != NULL)
  1411. {
  1412. if (hsc->hdmatx->XferAbortCallback != NULL)
  1413. {
  1414. return;
  1415. }
  1416. }
  1417. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1418. hsc->TxXferCount = 0x00U;
  1419. hsc->RxXferCount = 0x00U;
  1420. /* Reset ErrorCode */
  1421. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1422. /* Restore hsc->gState and hsc->RxState to Ready */
  1423. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1424. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1425. /* Call user Abort complete callback */
  1426. HAL_SMARTCARD_AbortCpltCallback(hsc);
  1427. }
  1428. /**
  1429. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
  1430. * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
  1431. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  1432. * and leads to user Tx Abort Complete callback execution).
  1433. * @param hdma DMA handle.
  1434. * @retval None
  1435. */
  1436. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1437. {
  1438. SMARTCARD_HandleTypeDef *hsc = (SMARTCARD_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1439. hsc->TxXferCount = 0x00U;
  1440. /* Restore hsc->gState to Ready */
  1441. hsc->gState = HAL_SMARTCARD_STATE_READY;
  1442. /* Call user Abort complete callback */
  1443. HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
  1444. }
  1445. /**
  1446. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
  1447. * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
  1448. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  1449. * and leads to user Rx Abort Complete callback execution).
  1450. * @param hdma DMA handle.
  1451. * @retval None
  1452. */
  1453. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1454. {
  1455. SMARTCARD_HandleTypeDef *hsc = (SMARTCARD_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1456. hsc->RxXferCount = 0x00U;
  1457. /* Restore hsc->RxState to Ready */
  1458. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1459. /* Call user Abort complete callback */
  1460. HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
  1461. }
  1462. /**
  1463. * @brief Send an amount of data in non blocking mode
  1464. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1465. * the configuration information for SMARTCARD module.
  1466. * @retval HAL status
  1467. */
  1468. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
  1469. {
  1470. /* Check that a Tx process is ongoing */
  1471. if (hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1472. {
  1473. hsc->Instance->DR = *(uint8_t *) hsc->pTxBuffPtr;
  1474. hsc->pTxBuffPtr += 1U;
  1475. if (--hsc->TxXferCount == 0U)
  1476. {
  1477. /* Disable the SMARTCARD Transmit data register empty Interrupt */
  1478. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
  1479. /* Enable the SMARTCARD Transmit Complete Interrupt */
  1480. SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
  1481. }
  1482. return HAL_OK;
  1483. }
  1484. else
  1485. {
  1486. return HAL_BUSY;
  1487. }
  1488. }
  1489. /**
  1490. * @brief Wraps up transmission in non blocking mode.
  1491. * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1492. * the configuration information for the specified SMARTCARD module.
  1493. * @retval HAL status
  1494. */
  1495. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1496. {
  1497. /* Disable the SMARTCARD Transmit Complete Interrupt */
  1498. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TCIE);
  1499. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1500. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1501. /* Tx process is ended, restore hsmartcard->gState to Ready */
  1502. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1503. HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  1504. return HAL_OK;
  1505. }
  1506. /**
  1507. * @brief Receive an amount of data in non blocking mode
  1508. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1509. * the configuration information for SMARTCARD module.
  1510. * @retval HAL status
  1511. */
  1512. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
  1513. {
  1514. /* Check that a Rx process is ongoing */
  1515. if (hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  1516. {
  1517. *(uint8_t *) hsc->pRxBuffPtr = (uint8_t)hsc->Instance->DR;
  1518. hsc->pRxBuffPtr += 1U;
  1519. if (--hsc->RxXferCount == 0U)
  1520. {
  1521. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
  1522. /* Disable the SMARTCARD Parity Error Interrupt */
  1523. CLEAR_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  1524. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1525. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1526. /* Rx process is completed, restore hsc->RxState to Ready */
  1527. hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1528. HAL_SMARTCARD_RxCpltCallback(hsc);
  1529. return HAL_OK;
  1530. }
  1531. return HAL_OK;
  1532. }
  1533. else
  1534. {
  1535. return HAL_BUSY;
  1536. }
  1537. }
  1538. /**
  1539. * @brief Configure the SMARTCARD peripheral
  1540. * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1541. * the configuration information for SMARTCARD module.
  1542. * @retval None
  1543. */
  1544. static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
  1545. {
  1546. uint32_t tmpreg = 0x00U;
  1547. /* Check the parameters */
  1548. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  1549. assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
  1550. assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
  1551. assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
  1552. assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
  1553. assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
  1554. assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
  1555. assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
  1556. assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
  1557. assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  1558. /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
  1559. receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
  1560. CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1561. /*---------------------------- USART CR2 Configuration ---------------------*/
  1562. tmpreg = hsc->Instance->CR2;
  1563. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  1564. tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
  1565. /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
  1566. /* Set CPOL bit according to hsc->Init.CLKPolarity value */
  1567. /* Set CPHA bit according to hsc->Init.CLKPhase value */
  1568. /* Set LBCL bit according to hsc->Init.CLKLastBit value */
  1569. /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
  1570. tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
  1571. hsc->Init.CLKPhase | hsc->Init.CLKLastBit | hsc->Init.StopBits);
  1572. /* Write to USART CR2 */
  1573. WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
  1574. tmpreg = hsc->Instance->CR2;
  1575. /* Clear STOP[13:12] bits */
  1576. tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  1577. /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
  1578. tmpreg |= (uint32_t)(hsc->Init.StopBits);
  1579. /* Write to USART CR2 */
  1580. WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
  1581. /*-------------------------- USART CR1 Configuration -----------------------*/
  1582. tmpreg = hsc->Instance->CR1;
  1583. /* Clear M, PCE, PS, TE and RE bits */
  1584. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  1585. USART_CR1_RE));
  1586. /* Configure the SMARTCARD Word Length, Parity and mode:
  1587. Set the M bits according to hsc->Init.WordLength value
  1588. Set PCE and PS bits according to hsc->Init.Parity value
  1589. Set TE and RE bits according to hsc->Init.Mode value */
  1590. tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
  1591. /* Write to USART CR1 */
  1592. WRITE_REG(hsc->Instance->CR1, (uint32_t)tmpreg);
  1593. /*-------------------------- USART CR3 Configuration -----------------------*/
  1594. /* Clear CTSE and RTSE bits */
  1595. CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
  1596. /*-------------------------- USART BRR Configuration -----------------------*/
  1597. if (hsc->Instance == USART1)
  1598. {
  1599. hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
  1600. }
  1601. else
  1602. {
  1603. hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate);
  1604. }
  1605. }
  1606. /**
  1607. * @}
  1608. */
  1609. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  1610. /**
  1611. * @}
  1612. */
  1613. /**
  1614. * @}
  1615. */
  1616. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/