stm32f4xx_hal_smartcard.c 44 KB

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