stm32f1xx_hal_smartcard.c 66 KB

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