stm32f7xx_hal_smartcard.c 44 KB

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