stm32l1xx_hal_smartcard.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_smartcard.c
  4. * @author MCD Application Team
  5. * @brief SMARTCARD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the SMARTCARD peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral State and Errors functions
  11. * + Peripheral Control functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. The SMARTCARD HAL driver can be used as follows:
  19. (#) Declare a SMARTCARD_HandleTypeDef handle structure.
  20. (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  21. (##) Enable the interface clock of the USARTx associated to the SMARTCARD.
  22. (##) SMARTCARD pins configuration:
  23. (+++) Enable the clock for the SMARTCARD GPIOs.
  24. (+++) Configure the SMARTCARD pins as alternate function pull-up.
  25. (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  26. and HAL_SMARTCARD_Receive_IT() APIs):
  27. (+++) Configure the USARTx interrupt priority.
  28. (+++) Enable the NVIC USART IRQ handle.
  29. (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  30. and HAL_SMARTCARD_Receive_DMA() APIs):
  31. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  32. (+++) Enable the DMAx interface clock.
  33. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  34. (+++) Configure the DMA Tx/Rx channel.
  35. (+++) Associate the initilalized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  36. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  37. (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
  38. (used for last byte sending completion detection in DMA non circular mode)
  39. (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
  40. flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
  41. (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
  42. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  43. by calling the customed HAL_SMARTCARD_MspInit(&hsc) API.
  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. (#) Three operation modes are available within this driver :
  48. *** Polling mode IO operation ***
  49. =================================
  50. [..]
  51. (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  52. (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  53. *** Interrupt mode IO operation ***
  54. ===================================
  55. [..]
  56. (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
  57. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  58. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  59. (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
  60. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  61. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  62. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  63. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  64. *** DMA mode IO operation ***
  65. ==============================
  66. [..]
  67. (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  68. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  69. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  70. (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  71. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  72. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  73. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  74. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  75. *** SMARTCARD HAL driver macros list ***
  76. ========================================
  77. [..]
  78. Below the list of most used macros in SMARTCARD HAL driver.
  79. (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
  80. (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
  81. (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
  82. (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
  83. (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
  84. (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
  85. (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether the specified SMARTCARD interrupt has occurred or not
  86. [..]
  87. (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
  88. @endverbatim
  89. ******************************************************************************
  90. * @attention
  91. *
  92. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  93. *
  94. * Redistribution and use in source and binary forms, with or without modification,
  95. * are permitted provided that the following conditions are met:
  96. * 1. Redistributions of source code must retain the above copyright notice,
  97. * this list of conditions and the following disclaimer.
  98. * 2. Redistributions in binary form must reproduce the above copyright notice,
  99. * this list of conditions and the following disclaimer in the documentation
  100. * and/or other materials provided with the distribution.
  101. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  102. * may be used to endorse or promote products derived from this software
  103. * without specific prior written permission.
  104. *
  105. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  106. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  107. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  108. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  109. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  110. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  111. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  112. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  113. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  114. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  115. *
  116. ******************************************************************************
  117. */
  118. /* Includes ------------------------------------------------------------------*/
  119. #include "stm32l1xx_hal.h"
  120. /** @addtogroup STM32L1xx_HAL_Driver
  121. * @{
  122. */
  123. /** @defgroup SMARTCARD SMARTCARD
  124. * @brief HAL SMARTCARD module driver
  125. * @{
  126. */
  127. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  128. /* Private typedef -----------------------------------------------------------*/
  129. /* Private define ------------------------------------------------------------*/
  130. /* Private macros --------------------------------------------------------*/
  131. /* Private variables ---------------------------------------------------------*/
  132. /* Private function prototypes -----------------------------------------------*/
  133. /** @addtogroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  134. * @{
  135. */
  136. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
  137. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  138. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
  139. static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
  140. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  141. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  142. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  143. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  144. /**
  145. * @}
  146. */
  147. /* Exported functions ---------------------------------------------------------*/
  148. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  149. * @{
  150. */
  151. /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  152. * @brief Initialization and Configuration functions
  153. *
  154. @verbatim
  155. ==============================================================================
  156. ##### Initialization and Configuration functions #####
  157. ==============================================================================
  158. [..]
  159. This subsection provides a set of functions allowing to initialize the USART
  160. in Smartcard mode.
  161. [..]
  162. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  163. defined in the ISO 7816-3 standard.
  164. [..]
  165. The USART can provide a clock to the smartcard through the SCLK output.
  166. In smartcard mode, SCLK is not associated to the communication but is simply derived
  167. from the internal peripheral input clock through a 5-bit prescaler.
  168. [..]
  169. (+) For the Smartcard mode only these parameters can be configured:
  170. (++) Baud Rate
  171. (++) Word Length => Should be 9 bits (8 bits + parity)
  172. (++) Stop Bit
  173. (++) Parity: => Should be enabled
  174. (++) USART polarity
  175. (++) USART phase
  176. (++) USART LastBit
  177. (++) Receiver/transmitter modes
  178. (++) Prescaler
  179. (++) GuardTime
  180. (++) NACKState: The Smartcard NACK state
  181. (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
  182. (++) Word Length = 9 Bits
  183. (++) 1.5 Stop Bit
  184. (++) Even parity
  185. (++) BaudRate = 12096 baud
  186. (++) Tx and Rx enabled
  187. [..]
  188. Please refer to the ISO 7816-3 specification for more details.
  189. (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
  190. to use 1.5 stop bits for both transmitting and receiving to avoid switching
  191. between the two configurations.
  192. [..]
  193. The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
  194. procedure (details for the procedure are available in reference manual (RM0038)).
  195. @endverbatim
  196. * @{
  197. */
  198. /*
  199. Additionnal remark on the smartcard frame:
  200. +-------------------------------------------------------------+
  201. | M bit | PCE bit | SMARTCARD frame |
  202. |---------------------|---------------------------------------|
  203. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  204. +-------------------------------------------------------------+
  205. */
  206. /**
  207. * @brief Initializes the SmartCard mode according to the specified
  208. * parameters in the SMARTCARD_HandleTypeDef and create the associated handle.
  209. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  210. * the configuration information for the specified SMARTCARD module.
  211. * @retval HAL status
  212. */
  213. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
  214. {
  215. /* Check the SMARTCARD handle allocation */
  216. if(hsc == NULL)
  217. {
  218. return HAL_ERROR;
  219. }
  220. /* Check Wordlength, Parity and Stop bits parameters */
  221. if ( (!(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength)))
  222. ||(!(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits)))
  223. ||(!(IS_SMARTCARD_PARITY(hsc->Init.Parity))) )
  224. {
  225. return HAL_ERROR;
  226. }
  227. /* Check the parameters */
  228. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  229. assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  230. assert_param(IS_SMARTCARD_PRESCALER(hsc->Init.Prescaler));
  231. if(hsc->State == HAL_SMARTCARD_STATE_RESET)
  232. {
  233. /* Allocate lock resource and initialize it */
  234. hsc->Lock = HAL_UNLOCKED;
  235. /* Init the low level hardware */
  236. HAL_SMARTCARD_MspInit(hsc);
  237. }
  238. hsc->State = HAL_SMARTCARD_STATE_BUSY;
  239. /* Disable the Peripheral */
  240. __HAL_SMARTCARD_DISABLE(hsc);
  241. /* Set the Prescaler */
  242. MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
  243. /* Set the Guard Time */
  244. MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8));
  245. /* Set the Smartcard Communication parameters */
  246. SMARTCARD_SetConfig(hsc);
  247. /* In SmartCard mode, the following bits must be kept cleared:
  248. - LINEN bit in the USART_CR2 register
  249. - HDSEL and IREN bits in the USART_CR3 register.*/
  250. CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
  251. CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
  252. /* Enable the Peripharal */
  253. __HAL_SMARTCARD_ENABLE(hsc);
  254. /* Configure the Smartcard NACK state */
  255. MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
  256. /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  257. SET_BIT(hsc->Instance->CR3, USART_CR3_SCEN);
  258. /* Initialize the SMARTCARD state*/
  259. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  260. hsc->State= HAL_SMARTCARD_STATE_READY;
  261. return HAL_OK;
  262. }
  263. /**
  264. * @brief DeInitializes the SMARTCARD peripheral
  265. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  266. * the configuration information for the specified SMARTCARD module.
  267. * @retval HAL status
  268. */
  269. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
  270. {
  271. /* Check the SMARTCARD handle allocation */
  272. if(hsc == NULL)
  273. {
  274. return HAL_ERROR;
  275. }
  276. /* Check the parameters */
  277. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  278. hsc->State = HAL_SMARTCARD_STATE_BUSY;
  279. /* Disable the Peripheral */
  280. __HAL_SMARTCARD_DISABLE(hsc);
  281. hsc->Instance->CR1 = 0x0;
  282. hsc->Instance->CR2 = 0x0;
  283. hsc->Instance->CR3 = 0x0;
  284. hsc->Instance->BRR = 0x0;
  285. hsc->Instance->GTPR = 0x0;
  286. /* DeInit the low level hardware */
  287. HAL_SMARTCARD_MspDeInit(hsc);
  288. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  289. hsc->State = HAL_SMARTCARD_STATE_RESET;
  290. /* Release Lock */
  291. __HAL_UNLOCK(hsc);
  292. return HAL_OK;
  293. }
  294. /**
  295. * @brief SMARTCARD MSP Init.
  296. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  297. * the configuration information for the specified SMARTCARD module.
  298. * @retval None
  299. */
  300. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
  301. {
  302. /* Prevent unused argument(s) compilation warning */
  303. UNUSED(hsc);
  304. /* NOTE: This function should not be modified, when the callback is needed,
  305. the HAL_SMARTCARD_MspInit can be implemented in the user file
  306. */
  307. }
  308. /**
  309. * @brief SMARTCARD MSP DeInit.
  310. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  311. * the configuration information for the specified SMARTCARD module.
  312. * @retval None
  313. */
  314. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
  315. {
  316. /* Prevent unused argument(s) compilation warning */
  317. UNUSED(hsc);
  318. /* NOTE: This function should not be modified, when the callback is needed,
  319. the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  320. */
  321. }
  322. /**
  323. * @}
  324. */
  325. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  326. * @brief SMARTCARD Transmit and Receive functions
  327. *
  328. @verbatim
  329. ==============================================================================
  330. ##### IO operation functions #####
  331. ==============================================================================
  332. [..]
  333. This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  334. [..]
  335. (#) Smartcard is a single wire half duplex communication protocol.
  336. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  337. defined in the ISO 7816-3 standard.
  338. (#) The USART should be configured as:
  339. (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  340. (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  341. (#) There are two modes of transfer:
  342. (++) Blocking mode: The communication is performed in polling mode.
  343. The HAL status of all data processing is returned by the same function
  344. after finishing transfer.
  345. (++) No-Blocking mode: The communication is performed using Interrupts
  346. or DMA, the relevant API's return the HAL status.
  347. The end of the data processing will be indicated through the
  348. dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  349. using DMA mode.
  350. The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  351. will be executed respectively at the end of the Transmit or Receive process
  352. The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
  353. error is detected.
  354. (#) Blocking mode APIs are :
  355. (++) HAL_SMARTCARD_Transmit()
  356. (++) HAL_SMARTCARD_Receive()
  357. (#) Non Blocking mode APIs with Interrupt are :
  358. (++) HAL_SMARTCARD_Transmit_IT()
  359. (++) HAL_SMARTCARD_Receive_IT()
  360. (++) HAL_SMARTCARD_IRQHandler()
  361. (#) Non Blocking mode functions with DMA are :
  362. (++) HAL_SMARTCARD_Transmit_DMA()
  363. (++) HAL_SMARTCARD_Receive_DMA()
  364. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  365. (++) HAL_SMARTCARD_TxCpltCallback()
  366. (++) HAL_SMARTCARD_RxCpltCallback()
  367. (++) HAL_SMARTCARD_ErrorCallback()
  368. @endverbatim
  369. * @{
  370. */
  371. /**
  372. * @brief Sends an amount of data in blocking mode.
  373. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  374. * the configuration information for the specified SMARTCARD module.
  375. * @param pData: Pointer to data buffer
  376. * @param Size: Amount of data to be sent
  377. * @param Timeout: Specify timeout value
  378. * @retval HAL status
  379. */
  380. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  381. {
  382. uint32_t tmp_state = 0;
  383. tmp_state = hsc->State;
  384. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_RX))
  385. {
  386. if((pData == NULL) || (Size == 0))
  387. {
  388. return HAL_ERROR;
  389. }
  390. /* Process Locked */
  391. __HAL_LOCK(hsc);
  392. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  393. /* Check if a non-blocking receive process is ongoing or not */
  394. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
  395. {
  396. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  397. }
  398. else
  399. {
  400. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  401. }
  402. hsc->TxXferSize = Size;
  403. hsc->TxXferCount = Size;
  404. while(hsc->TxXferCount > 0)
  405. {
  406. if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
  407. {
  408. return HAL_TIMEOUT;
  409. }
  410. WRITE_REG(hsc->Instance->DR, (*pData++ & (uint8_t)0xFF));
  411. hsc->TxXferCount--;
  412. }
  413. if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)
  414. {
  415. return HAL_TIMEOUT;
  416. }
  417. /* Check if a non-blocking receive process is ongoing or not */
  418. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  419. {
  420. hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  421. }
  422. else
  423. {
  424. hsc->State = HAL_SMARTCARD_STATE_READY;
  425. }
  426. /* Process Unlocked */
  427. __HAL_UNLOCK(hsc);
  428. return HAL_OK;
  429. }
  430. else
  431. {
  432. return HAL_BUSY;
  433. }
  434. }
  435. /**
  436. * @brief Receive an amount of data in blocking mode.
  437. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  438. * the configuration information for the specified SMARTCARD module.
  439. * @param pData: Pointer to data buffer
  440. * @param Size: Amount of data to be received
  441. * @param Timeout: Specify timeout value
  442. * @retval HAL status
  443. */
  444. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  445. {
  446. uint32_t tmp_state = 0;
  447. tmp_state = hsc->State;
  448. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX))
  449. {
  450. if((pData == NULL) || (Size == 0))
  451. {
  452. return HAL_ERROR;
  453. }
  454. /* Process Locked */
  455. __HAL_LOCK(hsc);
  456. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  457. /* Check if a non-blocking transmit process is ongoing or not */
  458. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
  459. {
  460. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  461. }
  462. else
  463. {
  464. hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  465. }
  466. hsc->RxXferSize = Size;
  467. hsc->RxXferCount = Size;
  468. /* Check the remain data to be received */
  469. while(hsc->RxXferCount > 0)
  470. {
  471. if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  472. {
  473. return HAL_TIMEOUT;
  474. }
  475. *pData++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF);
  476. hsc->RxXferCount--;
  477. }
  478. /* Check if a non-blocking transmit process is ongoing or not */
  479. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  480. {
  481. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  482. }
  483. else
  484. {
  485. hsc->State = HAL_SMARTCARD_STATE_READY;
  486. }
  487. /* Process Unlocked */
  488. __HAL_UNLOCK(hsc);
  489. return HAL_OK;
  490. }
  491. else
  492. {
  493. return HAL_BUSY;
  494. }
  495. }
  496. /**
  497. * @brief Sends an amount of data in non-blocking mode.
  498. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  499. * the configuration information for the specified SMARTCARD module.
  500. * @param pData: Pointer to data buffer
  501. * @param Size: Amount of data to be sent
  502. * @retval HAL status
  503. */
  504. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  505. {
  506. uint32_t tmp_state = 0;
  507. tmp_state = hsc->State;
  508. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_RX))
  509. {
  510. if((pData == NULL) || (Size == 0))
  511. {
  512. return HAL_ERROR;
  513. }
  514. /* Process Locked */
  515. __HAL_LOCK(hsc);
  516. hsc->pTxBuffPtr = pData;
  517. hsc->TxXferSize = Size;
  518. hsc->TxXferCount = Size;
  519. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  520. /* Check if a non-blocking receive process is ongoing or not */
  521. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
  522. {
  523. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  524. }
  525. else
  526. {
  527. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  528. }
  529. /* Process Unlocked */
  530. __HAL_UNLOCK(hsc);
  531. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  532. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
  533. /* Enable the SMARTCARD Transmit data register empty Interrupt */
  534. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TXE);
  535. return HAL_OK;
  536. }
  537. else
  538. {
  539. return HAL_BUSY;
  540. }
  541. }
  542. /**
  543. * @brief Receives an amount of data in non-blocking mode.
  544. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  545. * the configuration information for the specified SMARTCARD module.
  546. * @param pData: Pointer to data buffer
  547. * @param Size: Amount of data to be received
  548. * @retval HAL status
  549. */
  550. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  551. {
  552. uint32_t tmp_state = 0;
  553. tmp_state = hsc->State;
  554. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX))
  555. {
  556. if((pData == NULL) || (Size == 0))
  557. {
  558. return HAL_ERROR;
  559. }
  560. /* Process Locked */
  561. __HAL_LOCK(hsc);
  562. hsc->pRxBuffPtr = pData;
  563. hsc->RxXferSize = Size;
  564. hsc->RxXferCount = Size;
  565. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  566. /* Check if a non-blocking transmit process is ongoing or not */
  567. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
  568. {
  569. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  570. }
  571. else
  572. {
  573. hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  574. }
  575. /* Process Unlocked */
  576. __HAL_UNLOCK(hsc);
  577. /* Enable the SMARTCARD Data Register not empty Interrupt */
  578. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE);
  579. /* Enable the SMARTCARD Parity Error Interrupt */
  580. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
  581. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  582. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
  583. return HAL_OK;
  584. }
  585. else
  586. {
  587. return HAL_BUSY;
  588. }
  589. }
  590. /**
  591. * @brief Sends an amount of data in non-blocking mode.
  592. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  593. * the configuration information for the specified SMARTCARD module.
  594. * @param pData: Pointer to data buffer
  595. * @param Size: Amount of data to be sent
  596. * @retval HAL status
  597. */
  598. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  599. {
  600. uint32_t *tmp = 0;
  601. uint32_t tmp_state = 0;
  602. tmp_state = hsc->State;
  603. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_RX))
  604. {
  605. if((pData == NULL) || (Size == 0))
  606. {
  607. return HAL_ERROR;
  608. }
  609. /* Process Locked */
  610. __HAL_LOCK(hsc);
  611. hsc->pTxBuffPtr = pData;
  612. hsc->TxXferSize = Size;
  613. hsc->TxXferCount = Size;
  614. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  615. /* Check if a non-blocking receive process is ongoing or not */
  616. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
  617. {
  618. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  619. }
  620. else
  621. {
  622. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  623. }
  624. /* Set the SMARTCARD DMA transfer complete callback */
  625. hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  626. /* Set the DMA error callback */
  627. hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  628. /* Enable the SMARTCARD transmit DMA channel */
  629. tmp = (uint32_t*)&pData;
  630. HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
  631. /* Clear the TC flag in the SR register by writing 0 to it */
  632. __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
  633. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  634. in the SMARTCARD CR3 register */
  635. SET_BIT(hsc->Instance->CR3,USART_CR3_DMAT);
  636. /* Process Unlocked */
  637. __HAL_UNLOCK(hsc);
  638. return HAL_OK;
  639. }
  640. else
  641. {
  642. return HAL_BUSY;
  643. }
  644. }
  645. /**
  646. * @brief Receive an amount of data in non-blocking mode.
  647. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  648. * the configuration information for the specified SMARTCARD module.
  649. * @param pData: Pointer to data buffer
  650. * @param Size: Amount of data to be received
  651. * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.
  652. * @retval HAL status
  653. */
  654. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  655. {
  656. uint32_t *tmp = 0;
  657. uint32_t tmp_state = 0;
  658. tmp_state = hsc->State;
  659. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX))
  660. {
  661. if((pData == NULL) || (Size == 0))
  662. {
  663. return HAL_ERROR;
  664. }
  665. /* Process Locked */
  666. __HAL_LOCK(hsc);
  667. hsc->pRxBuffPtr = pData;
  668. hsc->RxXferSize = Size;
  669. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  670. /* Check if a non-blocking transmit process is ongoing or not */
  671. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
  672. {
  673. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  674. }
  675. else
  676. {
  677. hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  678. }
  679. /* Set the SMARTCARD DMA transfer complete callback */
  680. hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  681. /* Set the DMA error callback */
  682. hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  683. /* Enable the DMA channel */
  684. tmp = (uint32_t*)&pData;
  685. HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
  686. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  687. in the SMARTCARD CR3 register */
  688. SET_BIT(hsc->Instance->CR3,USART_CR3_DMAR);
  689. /* Process Unlocked */
  690. __HAL_UNLOCK(hsc);
  691. return HAL_OK;
  692. }
  693. else
  694. {
  695. return HAL_BUSY;
  696. }
  697. }
  698. /**
  699. * @brief This function handles SMARTCARD interrupt request.
  700. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  701. * the configuration information for the specified SMARTCARD module.
  702. * @retval None
  703. */
  704. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
  705. {
  706. uint32_t tmp_flag = 0, tmp_it_source = 0;
  707. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_PE);
  708. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_PE);
  709. /* SMARTCARD parity error interrupt occurred -----------------------------------*/
  710. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  711. {
  712. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  713. }
  714. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_FE);
  715. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR);
  716. /* SMARTCARD frame error interrupt occurred ------------------------------------*/
  717. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  718. {
  719. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  720. }
  721. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_NE);
  722. /* SMARTCARD noise error interrupt occurred ------------------------------------*/
  723. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  724. {
  725. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  726. }
  727. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_ORE);
  728. /* SMARTCARD Over-Run interrupt occurred ---------------------------------------*/
  729. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  730. {
  731. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  732. }
  733. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_RXNE);
  734. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RXNE);
  735. /* SMARTCARD in mode Receiver --------------------------------------------------*/
  736. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  737. {
  738. SMARTCARD_Receive_IT(hsc);
  739. }
  740. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_TXE);
  741. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TXE);
  742. /* SMARTCARD in mode Transmitter -----------------------------------------------*/
  743. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  744. {
  745. SMARTCARD_Transmit_IT(hsc);
  746. }
  747. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_TC);
  748. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TC);
  749. /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  750. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  751. {
  752. SMARTCARD_EndTransmit_IT(hsc);
  753. }
  754. /* Call the Error call Back in case of Errors */
  755. if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  756. {
  757. /* Clear all the error flag at once */
  758. __HAL_SMARTCARD_CLEAR_PEFLAG(hsc);
  759. /* Set the SMARTCARD state ready to be able to start again the process */
  760. hsc->State= HAL_SMARTCARD_STATE_READY;
  761. HAL_SMARTCARD_ErrorCallback(hsc);
  762. }
  763. }
  764. /**
  765. * @brief Tx Transfer completed callback.
  766. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  767. * the configuration information for the specified SMARTCARD module.
  768. * @retval None
  769. */
  770. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  771. {
  772. /* Prevent unused argument(s) compilation warning */
  773. UNUSED(hsc);
  774. /* NOTE: This function should not be modified, when the callback is needed,
  775. the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file
  776. */
  777. }
  778. /**
  779. * @brief Rx Transfer completed callback.
  780. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  781. * the configuration information for the specified SMARTCARD module.
  782. * @retval None
  783. */
  784. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  785. {
  786. /* Prevent unused argument(s) compilation warning */
  787. UNUSED(hsc);
  788. /* NOTE: This function should not be modified, when the callback is needed,
  789. the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file
  790. */
  791. }
  792. /**
  793. * @brief SMARTCARD error callback.
  794. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  795. * the configuration information for the specified SMARTCARD module.
  796. * @retval None
  797. */
  798. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
  799. {
  800. /* Prevent unused argument(s) compilation warning */
  801. UNUSED(hsc);
  802. /* NOTE: This function should not be modified, when the callback is needed,
  803. the HAL_SMARTCARD_ErrorCallback can be implemented in the user file
  804. */
  805. }
  806. /**
  807. * @}
  808. */
  809. /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
  810. * @brief SMARTCARD State and Errors functions
  811. *
  812. @verbatim
  813. ==============================================================================
  814. ##### Peripheral State and Errors functions #####
  815. ==============================================================================
  816. [..]
  817. This subsection provides a set of functions allowing to return the State of SmartCard
  818. communication process and also return Peripheral Errors occurred during communication process
  819. (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
  820. of the SMARTCARD peripheral.
  821. (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during
  822. communication.
  823. @endverbatim
  824. * @{
  825. */
  826. /**
  827. * @brief Returns the SMARTCARD state.
  828. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  829. * the configuration information for the specified SMARTCARD module.
  830. * @retval HAL state
  831. */
  832. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
  833. {
  834. return hsc->State;
  835. }
  836. /**
  837. * @brief Return the SMARTCARD error code
  838. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  839. * the configuration information for the specified SMARTCARD module.
  840. * @retval SMARTCARD Error Code
  841. */
  842. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
  843. {
  844. return hsc->ErrorCode;
  845. }
  846. /**
  847. * @}
  848. */
  849. /**
  850. * @}
  851. */
  852. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  853. * @brief SMARTCARD Private functions
  854. * @{
  855. */
  856. /**
  857. * @brief DMA SMARTCARD transmit process complete callback.
  858. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  859. * the configuration information for the specified DMA module.
  860. * @retval None
  861. */
  862. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  863. {
  864. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  865. hsc->TxXferCount = 0;
  866. /* Disable the DMA transfer for transmit request by setting the DMAT bit
  867. in the SMARTCARD CR3 register */
  868. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  869. /* Enable the SMARTCARD Transmit Complete Interrupt */
  870. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
  871. }
  872. /**
  873. * @brief DMA SMARTCARD receive process complete callback.
  874. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  875. * the configuration information for the specified DMA module.
  876. * @retval None
  877. */
  878. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  879. {
  880. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  881. hsc->RxXferCount = 0;
  882. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  883. in the USART CR3 register */
  884. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  885. /* Check if a non-blocking transmit process is ongoing or not */
  886. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  887. {
  888. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  889. }
  890. else
  891. {
  892. hsc->State = HAL_SMARTCARD_STATE_READY;
  893. }
  894. HAL_SMARTCARD_RxCpltCallback(hsc);
  895. }
  896. /**
  897. * @brief DMA SMARTCARD communication error callback.
  898. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  899. * the configuration information for the specified DMA module.
  900. * @retval None
  901. */
  902. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  903. {
  904. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  905. hsc->RxXferCount = 0;
  906. hsc->TxXferCount = 0;
  907. hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  908. hsc->State= HAL_SMARTCARD_STATE_READY;
  909. HAL_SMARTCARD_ErrorCallback(hsc);
  910. }
  911. /**
  912. * @brief This function handles SMARTCARD Communication Timeout.
  913. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  914. * the configuration information for the specified SMARTCARD module.
  915. * @param Flag: specifies the SMARTCARD flag to check.
  916. * @param Status: The new Flag status (SET or RESET).
  917. * @param Timeout: Timeout duration
  918. * @retval HAL status
  919. */
  920. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  921. {
  922. uint32_t tickstart = 0;
  923. /* Get tick */
  924. tickstart = HAL_GetTick();
  925. /* Wait until flag is set */
  926. if(Status == RESET)
  927. {
  928. while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) == RESET)
  929. {
  930. /* Check for the Timeout */
  931. if(Timeout != HAL_MAX_DELAY)
  932. {
  933. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  934. {
  935. /* Disable TXE and RXNE interrupts for the interrupt process */
  936. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
  937. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
  938. hsc->State= HAL_SMARTCARD_STATE_READY;
  939. /* Process Unlocked */
  940. __HAL_UNLOCK(hsc);
  941. return HAL_TIMEOUT;
  942. }
  943. }
  944. }
  945. }
  946. else
  947. {
  948. while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) != RESET)
  949. {
  950. /* Check for the Timeout */
  951. if(Timeout != HAL_MAX_DELAY)
  952. {
  953. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  954. {
  955. /* Disable TXE and RXNE interrupts for the interrupt process */
  956. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
  957. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
  958. hsc->State= HAL_SMARTCARD_STATE_READY;
  959. /* Process Unlocked */
  960. __HAL_UNLOCK(hsc);
  961. return HAL_TIMEOUT;
  962. }
  963. }
  964. }
  965. }
  966. return HAL_OK;
  967. }
  968. /**
  969. * @brief Send an amount of data in non-blocking mode.
  970. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  971. * the configuration information for the specified SMARTCARD module.
  972. * Function called under interruption only, once
  973. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  974. * @retval HAL status
  975. */
  976. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
  977. {
  978. uint32_t tmp_state = 0;
  979. tmp_state = hsc->State;
  980. if((tmp_state == HAL_SMARTCARD_STATE_BUSY_TX) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX_RX))
  981. {
  982. WRITE_REG(hsc->Instance->DR, (*hsc->pTxBuffPtr++ & (uint8_t)0xFF));
  983. if(--hsc->TxXferCount == 0)
  984. {
  985. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  986. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
  987. /* Enable the SMARTCARD Transmit Complete Interrupt */
  988. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
  989. }
  990. return HAL_OK;
  991. }
  992. else
  993. {
  994. return HAL_BUSY;
  995. }
  996. }
  997. /**
  998. * @brief Wraps up transmission in non blocking mode.
  999. * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1000. * the configuration information for the specified SMARTCARD module.
  1001. * @retval HAL status
  1002. */
  1003. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1004. {
  1005. /* Disable the SMARTCARD Transmit Complete Interrupt */
  1006. __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TC);
  1007. /* Check if a receive process is ongoing or not */
  1008. if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  1009. {
  1010. hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
  1011. }
  1012. else
  1013. {
  1014. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1015. __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
  1016. hsmartcard->State = HAL_SMARTCARD_STATE_READY;
  1017. }
  1018. HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  1019. return HAL_OK;
  1020. }
  1021. /**
  1022. * @brief Receive an amount of data in non-blocking mode.
  1023. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1024. * the configuration information for the specified SMARTCARD module.
  1025. * @retval HAL status
  1026. */
  1027. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
  1028. {
  1029. uint32_t tmp_state = 0;
  1030. tmp_state = hsc->State;
  1031. if((tmp_state == HAL_SMARTCARD_STATE_BUSY_RX) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX_RX))
  1032. {
  1033. *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFF);
  1034. if(--hsc->RxXferCount == 0)
  1035. {
  1036. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
  1037. /* Disable the SMARTCARD Parity Error Interrupt */
  1038. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
  1039. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1040. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
  1041. /* Check if a non-blocking transmit process is ongoing or not */
  1042. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  1043. {
  1044. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  1045. }
  1046. else
  1047. {
  1048. hsc->State = HAL_SMARTCARD_STATE_READY;
  1049. }
  1050. HAL_SMARTCARD_RxCpltCallback(hsc);
  1051. return HAL_OK;
  1052. }
  1053. return HAL_OK;
  1054. }
  1055. else
  1056. {
  1057. return HAL_BUSY;
  1058. }
  1059. }
  1060. /**
  1061. * @brief Configures the SMARTCARD peripheral.
  1062. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1063. * the configuration information for the specified SMARTCARD module.
  1064. * @retval None
  1065. */
  1066. static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
  1067. {
  1068. /* Check the parameters */
  1069. assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
  1070. assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
  1071. assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
  1072. assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
  1073. assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
  1074. assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
  1075. assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
  1076. assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
  1077. assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  1078. /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
  1079. receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
  1080. CLEAR_BIT(hsc->Instance->CR1, (uint32_t)(USART_CR1_TE | USART_CR1_RE));
  1081. /*------ SMARTCARD-associated USART registers setting : CR2 Configuration ------*/
  1082. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  1083. /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
  1084. /* Set CPOL bit according to hsc->Init.CLKPolarity value */
  1085. /* Set CPHA bit according to hsc->Init.CLKPhase value */
  1086. /* Set LBCL bit according to hsc->Init.CLKLastBit value */
  1087. MODIFY_REG(hsc->Instance->CR2,
  1088. ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL)),
  1089. ((uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity | hsc->Init.CLKPhase| hsc->Init.CLKLastBit)) );
  1090. /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
  1091. MODIFY_REG(hsc->Instance->CR2, USART_CR2_STOP,(uint32_t)(hsc->Init.StopBits));
  1092. /*------ SMARTCARD-associated USART registers setting : CR1 Configuration ------*/
  1093. /* Clear M, PCE, PS, TE and RE bits */
  1094. /* Configure the SMARTCARD Word Length, Parity and mode:
  1095. Set the M according to hsc->Init.WordLength value (forced to 1 as 9B data frame should be selected)
  1096. Set PCE and PS bits according to hsc->Init.Parity value (PCE bit forced to 1 as parity control should always be enabled)
  1097. Set TE and RE bits according to hsc->Init.Mode value */
  1098. MODIFY_REG(hsc->Instance->CR1,
  1099. ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)),
  1100. ((uint32_t)(USART_CR1_M | USART_CR1_PCE | hsc->Init.Parity | hsc->Init.Mode)) );
  1101. /*------ SMARTCARD-associated USART registers setting : CR3 Configuration ------*/
  1102. /* Clear CTSE and RTSE bits */
  1103. CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
  1104. /*------ SMARTCARD-associated USART registers setting : BRR Configuration ------*/
  1105. if(hsc->Instance == USART1)
  1106. {
  1107. hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
  1108. }
  1109. else
  1110. {
  1111. hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate);
  1112. }
  1113. }
  1114. /**
  1115. * @}
  1116. */
  1117. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  1118. /**
  1119. * @}
  1120. */
  1121. /**
  1122. * @}
  1123. */
  1124. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/