stm32l1xx_hal_smartcard.c 47 KB

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