stm32l1xx_hal_smartcard.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_smartcard.h
  4. * @author MCD Application Team
  5. * @brief This file contains all the functions prototypes for the SMARTCARD
  6. * firmware library.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* Define to prevent recursive inclusion -------------------------------------*/
  37. #ifndef __STM32L1xx_HAL_SMARTCARD_H
  38. #define __STM32L1xx_HAL_SMARTCARD_H
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /* Includes ------------------------------------------------------------------*/
  43. #include "stm32l1xx_hal_def.h"
  44. /** @addtogroup STM32L1xx_HAL_Driver
  45. * @{
  46. */
  47. /** @addtogroup SMARTCARD
  48. * @{
  49. */
  50. /* Exported types ------------------------------------------------------------*/
  51. /** @defgroup SMARTCARD_Exported_Types SMARTCARD Exported Types
  52. * @{
  53. */
  54. /**
  55. * @brief SMARTCARD Init Structure definition
  56. */
  57. typedef struct
  58. {
  59. uint32_t BaudRate; /*!< This member configures the SmartCard communication baud rate.
  60. The baud rate is computed using the following formula:
  61. - IntegerDivider = ((PCLKx) / (16 * (hsmartcard->Init.BaudRate)))
  62. - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 16) + 0.5 */
  63. uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  64. This parameter can be a value of @ref SMARTCARD_Word_Length */
  65. uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
  66. This parameter can be a value of @ref SMARTCARD_Stop_Bits */
  67. uint32_t Parity; /*!< Specifies the parity mode.
  68. This parameter can be a value of @ref SMARTCARD_Parity
  69. @note When parity is enabled, the computed parity is inserted
  70. at the MSB position of the transmitted data (9th bit when
  71. the word length is set to 9 data bits; 8th bit when the
  72. word length is set to 8 data bits).*/
  73. uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
  74. This parameter can be a value of @ref SMARTCARD_Mode */
  75. uint32_t CLKPolarity; /*!< Specifies the steady state of the serial clock.
  76. This parameter can be a value of @ref SMARTCARD_Clock_Polarity */
  77. uint32_t CLKPhase; /*!< Specifies the clock transition on which the bit capture is made.
  78. This parameter can be a value of @ref SMARTCARD_Clock_Phase */
  79. uint32_t CLKLastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
  80. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  81. This parameter can be a value of @ref SMARTCARD_Last_Bit */
  82. uint32_t Prescaler; /*!< Specifies the SmartCard Prescaler value used for dividing the system clock
  83. to provide the smartcard clock. The value given in the register
  84. (5 significant bits) is multiplied by 2 to give the division factor of
  85. the source clock frequency;
  86. This parameter can be a value of @ref SMARTCARD_Prescaler */
  87. uint32_t GuardTime; /*!< Specifies the SmartCard Guard Time value in terms of number
  88. of baud clocks */
  89. uint32_t NACKState; /*!< Specifies the SmartCard NACK Transmission state
  90. This parameter can be a value of @ref SMARTCARD_NACK_State */
  91. }SMARTCARD_InitTypeDef;
  92. /**
  93. * @brief HAL State structures definition
  94. */
  95. typedef enum
  96. {
  97. HAL_SMARTCARD_STATE_RESET = 0x00, /*!< Peripheral is not yet Initialized */
  98. HAL_SMARTCARD_STATE_READY = 0x01, /*!< Peripheral Initialized and ready for use */
  99. HAL_SMARTCARD_STATE_BUSY = 0x02, /*!< an internal process is ongoing */
  100. HAL_SMARTCARD_STATE_BUSY_TX = 0x12, /*!< Data Transmission process is ongoing */
  101. HAL_SMARTCARD_STATE_BUSY_RX = 0x22, /*!< Data Reception process is ongoing */
  102. HAL_SMARTCARD_STATE_BUSY_TX_RX = 0x32, /*!< Data Transmission and Reception process is ongoing */
  103. HAL_SMARTCARD_STATE_TIMEOUT = 0x03, /*!< Timeout state */
  104. HAL_SMARTCARD_STATE_ERROR = 0x04 /*!< Error */
  105. }HAL_SMARTCARD_StateTypeDef;
  106. /**
  107. * @brief SMARTCARD handle Structure definition
  108. */
  109. typedef struct
  110. {
  111. USART_TypeDef *Instance; /*!< USART registers base address */
  112. SMARTCARD_InitTypeDef Init; /*!< SmartCard communication parameters */
  113. uint8_t *pTxBuffPtr; /*!< Pointer to SmartCard Tx transfer Buffer */
  114. uint16_t TxXferSize; /*!< SmartCard Tx Transfer size */
  115. uint16_t TxXferCount; /*!< SmartCard Tx Transfer Counter */
  116. uint8_t *pRxBuffPtr; /*!< Pointer to SmartCard Rx transfer Buffer */
  117. uint16_t RxXferSize; /*!< SmartCard Rx Transfer size */
  118. uint16_t RxXferCount; /*!< SmartCard Rx Transfer Counter */
  119. DMA_HandleTypeDef *hdmatx; /*!< SmartCard Tx DMA Handle parameters */
  120. DMA_HandleTypeDef *hdmarx; /*!< SmartCard Rx DMA Handle parameters */
  121. HAL_LockTypeDef Lock; /*!< Locking object */
  122. __IO HAL_SMARTCARD_StateTypeDef State; /*!< SmartCard communication state */
  123. __IO uint32_t ErrorCode; /*!< SmartCard Error code */
  124. }SMARTCARD_HandleTypeDef;
  125. /**
  126. * @}
  127. */
  128. /* Exported constants --------------------------------------------------------*/
  129. /** @defgroup SMARTCARD_Exported_Constants SMARTCARD Exported constants
  130. * @{
  131. */
  132. /** @defgroup SMARTCARD_Error_Codes SMARTCARD Error Codes
  133. * @{
  134. */
  135. #define HAL_SMARTCARD_ERROR_NONE (0x00U) /*!< No error */
  136. #define HAL_SMARTCARD_ERROR_PE (0x01U) /*!< Parity error */
  137. #define HAL_SMARTCARD_ERROR_NE (0x02U) /*!< Noise error */
  138. #define HAL_SMARTCARD_ERROR_FE (0x04U) /*!< frame error */
  139. #define HAL_SMARTCARD_ERROR_ORE (0x08U) /*!< Overrun error */
  140. #define HAL_SMARTCARD_ERROR_DMA (0x10U) /*!< DMA transfer error */
  141. /**
  142. * @}
  143. */
  144. /** @defgroup SMARTCARD_Word_Length SMARTCARD Word Length
  145. * @{
  146. */
  147. #define SMARTCARD_WORDLENGTH_9B ((uint32_t)USART_CR1_M)
  148. /**
  149. * @}
  150. */
  151. /** @defgroup SMARTCARD_Stop_Bits SMARTCARD Number of Stop Bits
  152. * @{
  153. */
  154. #define SMARTCARD_STOPBITS_0_5 ((uint32_t)USART_CR2_STOP_0)
  155. #define SMARTCARD_STOPBITS_1_5 ((uint32_t)(USART_CR2_STOP_0 | USART_CR2_STOP_1))
  156. /**
  157. * @}
  158. */
  159. /** @defgroup SMARTCARD_Parity SMARTCARD Parity
  160. * @{
  161. */
  162. #define SMARTCARD_PARITY_EVEN ((uint32_t)USART_CR1_PCE)
  163. #define SMARTCARD_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
  164. /**
  165. * @}
  166. */
  167. /** @defgroup SMARTCARD_Mode SMARTCARD Mode
  168. * @{
  169. */
  170. #define SMARTCARD_MODE_RX ((uint32_t)USART_CR1_RE)
  171. #define SMARTCARD_MODE_TX ((uint32_t)USART_CR1_TE)
  172. #define SMARTCARD_MODE_TX_RX ((uint32_t)(USART_CR1_TE |USART_CR1_RE))
  173. /**
  174. * @}
  175. */
  176. /** @defgroup SMARTCARD_Clock_Polarity SMARTCARD Clock Polarity
  177. * @{
  178. */
  179. #define SMARTCARD_POLARITY_LOW (0x00000000U)
  180. #define SMARTCARD_POLARITY_HIGH ((uint32_t)USART_CR2_CPOL)
  181. /**
  182. * @}
  183. */
  184. /** @defgroup SMARTCARD_Clock_Phase SMARTCARD Clock Phase
  185. * @{
  186. */
  187. #define SMARTCARD_PHASE_1EDGE (0x00000000U)
  188. #define SMARTCARD_PHASE_2EDGE ((uint32_t)USART_CR2_CPHA)
  189. /**
  190. * @}
  191. */
  192. /** @defgroup SMARTCARD_Last_Bit SMARTCARD Last Bit
  193. * @{
  194. */
  195. #define SMARTCARD_LASTBIT_DISABLE (0x00000000U)
  196. #define SMARTCARD_LASTBIT_ENABLE ((uint32_t)USART_CR2_LBCL)
  197. /**
  198. * @}
  199. */
  200. /** @defgroup SMARTCARD_OneBit_Sampling SMARTCARD One Bit Sampling Method
  201. * @{
  202. */
  203. #define SMARTCARD_ONE_BIT_SAMPLE_DISABLE (0x00000000U)
  204. #define SMARTCARD_ONE_BIT_SAMPLE_ENABLE ((uint32_t)USART_CR3_ONEBIT)
  205. /**
  206. * @}
  207. */
  208. /** @defgroup SMARTCARD_NACK_State SMARTCARD NACK State
  209. * @{
  210. */
  211. #define SMARTCARD_NACK_ENABLE ((uint32_t)USART_CR3_NACK)
  212. #define SMARTCARD_NACK_DISABLE (0x00000000U)
  213. /**
  214. * @}
  215. */
  216. /** @defgroup SMARTCARD_DMA_Requests SMARTCARD DMA requests
  217. * @{
  218. */
  219. #define SMARTCARD_DMAREQ_TX ((uint32_t)USART_CR3_DMAT)
  220. #define SMARTCARD_DMAREQ_RX ((uint32_t)USART_CR3_DMAR)
  221. /**
  222. * @}
  223. */
  224. /** @defgroup SMARTCARD_Prescaler SMARTCARD Prescaler
  225. * @{
  226. */
  227. #define SMARTCARD_PRESCALER_SYSCLK_DIV2 (0x00000001U) /*!< SYSCLK divided by 2 */
  228. #define SMARTCARD_PRESCALER_SYSCLK_DIV4 (0x00000002U) /*!< SYSCLK divided by 4 */
  229. #define SMARTCARD_PRESCALER_SYSCLK_DIV6 (0x00000003U) /*!< SYSCLK divided by 6 */
  230. #define SMARTCARD_PRESCALER_SYSCLK_DIV8 (0x00000004U) /*!< SYSCLK divided by 8 */
  231. #define SMARTCARD_PRESCALER_SYSCLK_DIV10 (0x00000005U) /*!< SYSCLK divided by 10 */
  232. #define SMARTCARD_PRESCALER_SYSCLK_DIV12 (0x00000006U) /*!< SYSCLK divided by 12 */
  233. #define SMARTCARD_PRESCALER_SYSCLK_DIV14 (0x00000007U) /*!< SYSCLK divided by 14 */
  234. #define SMARTCARD_PRESCALER_SYSCLK_DIV16 (0x00000008U) /*!< SYSCLK divided by 16 */
  235. #define SMARTCARD_PRESCALER_SYSCLK_DIV18 (0x00000009U) /*!< SYSCLK divided by 18 */
  236. #define SMARTCARD_PRESCALER_SYSCLK_DIV20 (0x0000000AU) /*!< SYSCLK divided by 20 */
  237. #define SMARTCARD_PRESCALER_SYSCLK_DIV22 (0x0000000BU) /*!< SYSCLK divided by 22 */
  238. #define SMARTCARD_PRESCALER_SYSCLK_DIV24 (0x0000000CU) /*!< SYSCLK divided by 24 */
  239. #define SMARTCARD_PRESCALER_SYSCLK_DIV26 (0x0000000DU) /*!< SYSCLK divided by 26 */
  240. #define SMARTCARD_PRESCALER_SYSCLK_DIV28 (0x0000000EU) /*!< SYSCLK divided by 28 */
  241. #define SMARTCARD_PRESCALER_SYSCLK_DIV30 (0x0000000FU) /*!< SYSCLK divided by 30 */
  242. #define SMARTCARD_PRESCALER_SYSCLK_DIV32 (0x00000010U) /*!< SYSCLK divided by 32 */
  243. #define SMARTCARD_PRESCALER_SYSCLK_DIV34 (0x00000011U) /*!< SYSCLK divided by 34 */
  244. #define SMARTCARD_PRESCALER_SYSCLK_DIV36 (0x00000012U) /*!< SYSCLK divided by 36 */
  245. #define SMARTCARD_PRESCALER_SYSCLK_DIV38 (0x00000013U) /*!< SYSCLK divided by 38 */
  246. #define SMARTCARD_PRESCALER_SYSCLK_DIV40 (0x00000014U) /*!< SYSCLK divided by 40 */
  247. #define SMARTCARD_PRESCALER_SYSCLK_DIV42 (0x00000015U) /*!< SYSCLK divided by 42 */
  248. #define SMARTCARD_PRESCALER_SYSCLK_DIV44 (0x00000016U) /*!< SYSCLK divided by 44 */
  249. #define SMARTCARD_PRESCALER_SYSCLK_DIV46 (0x00000017U) /*!< SYSCLK divided by 46 */
  250. #define SMARTCARD_PRESCALER_SYSCLK_DIV48 (0x00000018U) /*!< SYSCLK divided by 48 */
  251. #define SMARTCARD_PRESCALER_SYSCLK_DIV50 (0x00000019U) /*!< SYSCLK divided by 50 */
  252. #define SMARTCARD_PRESCALER_SYSCLK_DIV52 (0x0000001AU) /*!< SYSCLK divided by 52 */
  253. #define SMARTCARD_PRESCALER_SYSCLK_DIV54 (0x0000001BU) /*!< SYSCLK divided by 54 */
  254. #define SMARTCARD_PRESCALER_SYSCLK_DIV56 (0x0000001CU) /*!< SYSCLK divided by 56 */
  255. #define SMARTCARD_PRESCALER_SYSCLK_DIV58 (0x0000001DU) /*!< SYSCLK divided by 58 */
  256. #define SMARTCARD_PRESCALER_SYSCLK_DIV60 (0x0000001EU) /*!< SYSCLK divided by 60 */
  257. #define SMARTCARD_PRESCALER_SYSCLK_DIV62 (0x0000001FU) /*!< SYSCLK divided by 62 */
  258. /**
  259. * @}
  260. */
  261. /** @defgroup SMARTCARD_Flags SMARTCARD Flags
  262. * Elements values convention: 0xXXXX
  263. * - 0xXXXX : Flag mask in the SR register
  264. * @{
  265. */
  266. #define SMARTCARD_FLAG_TXE ((uint32_t)USART_SR_TXE)
  267. #define SMARTCARD_FLAG_TC ((uint32_t)USART_SR_TC)
  268. #define SMARTCARD_FLAG_RXNE ((uint32_t)USART_SR_RXNE)
  269. #define SMARTCARD_FLAG_IDLE ((uint32_t)USART_SR_IDLE)
  270. #define SMARTCARD_FLAG_ORE ((uint32_t)USART_SR_ORE)
  271. #define SMARTCARD_FLAG_NE ((uint32_t)USART_SR_NE)
  272. #define SMARTCARD_FLAG_FE ((uint32_t)USART_SR_FE)
  273. #define SMARTCARD_FLAG_PE ((uint32_t)USART_SR_PE)
  274. /**
  275. * @}
  276. */
  277. /** @defgroup SMARTCARD_Interrupt_definition SMARTCARD Interrupts Definition
  278. * Elements values convention: 0xY000XXXX
  279. * - XXXX : Interrupt mask (16 bits) in the Y register
  280. * - Y : Interrupt source register (4 bits)
  281. * - 0001: CR1 register
  282. * - 0010: CR3 register
  283. *
  284. * @{
  285. */
  286. #define SMARTCARD_IT_PE ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_PEIE))
  287. #define SMARTCARD_IT_TXE ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_TXEIE))
  288. #define SMARTCARD_IT_TC ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_TCIE))
  289. #define SMARTCARD_IT_RXNE ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_RXNEIE))
  290. #define SMARTCARD_IT_IDLE ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28 | USART_CR1_IDLEIE))
  291. #define SMARTCARD_IT_ERR ((uint32_t)(SMARTCARD_CR3_REG_INDEX << 28 | USART_CR3_EIE))
  292. /**
  293. * @}
  294. */
  295. /**
  296. * @}
  297. */
  298. /* Exported macro ------------------------------------------------------------*/
  299. /** @defgroup SMARTCARD_Exported_Macros SMARTCARD Exported Macros
  300. * @{
  301. */
  302. /** @brief Reset SMARTCARD handle state
  303. * @param __HANDLE__: specifies the SMARTCARD Handle.
  304. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  305. * @retval None
  306. */
  307. #define __HAL_SMARTCARD_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SMARTCARD_STATE_RESET)
  308. /** @brief Flush the Smartcard DR register
  309. * @param __HANDLE__: specifies the SMARTCARD Handle.
  310. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  311. * @retval None
  312. */
  313. #define __HAL_SMARTCARD_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
  314. /** @brief Check whether the specified Smartcard flag is set or not.
  315. * @param __HANDLE__: specifies the SMARTCARD Handle.
  316. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  317. * @param __FLAG__: specifies the flag to check.
  318. * This parameter can be one of the following values:
  319. * @arg SMARTCARD_FLAG_TXE: Transmit data register empty flag
  320. * @arg SMARTCARD_FLAG_TC: Transmission Complete flag
  321. * @arg SMARTCARD_FLAG_RXNE: Receive data register not empty flag
  322. * @arg SMARTCARD_FLAG_IDLE: Idle Line detection flag
  323. * @arg SMARTCARD_FLAG_ORE: OverRun Error flag
  324. * @arg SMARTCARD_FLAG_NE: Noise Error flag
  325. * @arg SMARTCARD_FLAG_FE: Framing Error flag
  326. * @arg SMARTCARD_FLAG_PE: Parity Error flag
  327. * @retval The new state of __FLAG__ (TRUE or FALSE).
  328. */
  329. #define __HAL_SMARTCARD_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
  330. /** @brief Clear the specified Smartcard pending flags.
  331. * @param __HANDLE__: specifies the SMARTCARD Handle.
  332. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  333. * @param __FLAG__: specifies the flag to check.
  334. * This parameter can be any combination of the following values:
  335. * @arg SMARTCARD_FLAG_TC: Transmission Complete flag.
  336. * @arg SMARTCARD_FLAG_RXNE: Receive data register not empty flag.
  337. * @retval None
  338. *
  339. * @note PE (Parity error), FE (Framing error), NE (Noise error) and ORE (OverRun
  340. * error) flags are cleared by software sequence: a read operation to
  341. * USART_SR register followed by a read operation to USART_DR register.
  342. * @note RXNE flag can be also cleared by a read to the USART_DR register.
  343. * @note TC flag can be also cleared by software sequence: a read operation to
  344. * USART_SR register followed by a write operation to USART_DR register.
  345. * @note TXE flag is cleared only by a write to the USART_DR register.
  346. *
  347. * @retval None
  348. */
  349. #define __HAL_SMARTCARD_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
  350. /** @brief Clear the SMARTCARD PE pending flag.
  351. * @param __HANDLE__: specifies the USART Handle.
  352. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  353. * @retval None
  354. */
  355. #define __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__) \
  356. do{ \
  357. __IO uint32_t tmpreg; \
  358. tmpreg = (__HANDLE__)->Instance->SR; \
  359. tmpreg = (__HANDLE__)->Instance->DR; \
  360. UNUSED(tmpreg); \
  361. }while(0)
  362. /** @brief Clear the SMARTCARD FE pending flag.
  363. * @param __HANDLE__: specifies the USART Handle.
  364. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  365. * @retval None
  366. */
  367. #define __HAL_SMARTCARD_CLEAR_FEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
  368. /** @brief Clear the SMARTCARD NE pending flag.
  369. * @param __HANDLE__: specifies the USART Handle.
  370. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  371. * @retval None
  372. */
  373. #define __HAL_SMARTCARD_CLEAR_NEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
  374. /** @brief Clear the SMARTCARD ORE pending flag.
  375. * @param __HANDLE__: specifies the USART Handle.
  376. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  377. * @retval None
  378. */
  379. #define __HAL_SMARTCARD_CLEAR_OREFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
  380. /** @brief Clear the SMARTCARD IDLE pending flag.
  381. * @param __HANDLE__: specifies the USART Handle.
  382. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  383. * @retval None
  384. */
  385. #define __HAL_SMARTCARD_CLEAR_IDLEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
  386. /** @brief Enable the specified SmartCard interrupt.
  387. * @param __HANDLE__: specifies the SMARTCARD Handle.
  388. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  389. * @param __INTERRUPT__: specifies the SMARTCARD interrupt to enable.
  390. * This parameter can be one of the following values:
  391. * @arg SMARTCARD_IT_TXE: Transmit Data Register empty interrupt
  392. * @arg SMARTCARD_IT_TC: Transmission complete interrupt
  393. * @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
  394. * @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
  395. * @arg SMARTCARD_IT_PE: Parity Error interrupt
  396. * @arg SMARTCARD_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  397. * @retval None
  398. */
  399. #define __HAL_SMARTCARD_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == SMARTCARD_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & SMARTCARD_IT_MASK)): \
  400. ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & SMARTCARD_IT_MASK)))
  401. /** @brief Disable the specified SmartCard interrupts.
  402. * @param __HANDLE__: specifies the SMARTCARD Handle.
  403. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  404. * @param __INTERRUPT__: specifies the SMARTCARD interrupt to disable.
  405. * This parameter can be one of the following values:
  406. * @arg SMARTCARD_IT_TXE: Transmit Data Register empty interrupt
  407. * @arg SMARTCARD_IT_TC: Transmission complete interrupt
  408. * @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
  409. * @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
  410. * @arg SMARTCARD_IT_PE: Parity Error interrupt
  411. * @arg SMARTCARD_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  412. */
  413. #define __HAL_SMARTCARD_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == SMARTCARD_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & SMARTCARD_IT_MASK)): \
  414. ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & SMARTCARD_IT_MASK)))
  415. /** @brief Check whether the specified SmartCard interrupt has occurred or not.
  416. * @param __HANDLE__: specifies the SMARTCARD Handle.
  417. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  418. * @param __IT__: specifies the SMARTCARD interrupt source to check.
  419. * This parameter can be one of the following values:
  420. * @arg SMARTCARD_IT_TXE: Transmit Data Register empty interrupt
  421. * @arg SMARTCARD_IT_TC: Transmission complete interrupt
  422. * @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
  423. * @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
  424. * @arg SMARTCARD_IT_ERR: Error interrupt
  425. * @arg SMARTCARD_IT_PE: Parity Error interrupt
  426. * @retval The new state of __IT__ (TRUE or FALSE).
  427. */
  428. #define __HAL_SMARTCARD_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28) == SMARTCARD_CR1_REG_INDEX)? (__HANDLE__)->Instance->CR1: (__HANDLE__)->Instance->CR3) & (((uint32_t)(__IT__)) & SMARTCARD_IT_MASK))
  429. /** @brief Enables the SMARTCARD one bit sample method
  430. * @param __HANDLE__: specifies the SMARTCARD Handle.
  431. * @retval None
  432. */
  433. #define __HAL_SMARTCARD_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) (SET_BIT((__HANDLE__)->Instance->CR3, (USART_CR3_ONEBIT)))
  434. /** @brief Disables the SMARTCARD one bit sample method
  435. * @param __HANDLE__: specifies the SMARTCARD Handle.
  436. * @retval None
  437. */
  438. #define __HAL_SMARTCARD_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) (CLEAR_BIT((__HANDLE__)->Instance->CR3, (USART_CR3_ONEBIT)))
  439. /** @brief Enable the USART associated to the SMARTCARD Handle
  440. * @param __HANDLE__: specifies the SMARTCARD Handle.
  441. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  442. * @retval None
  443. */
  444. #define __HAL_SMARTCARD_ENABLE(__HANDLE__) (SET_BIT((__HANDLE__)->Instance->CR1, USART_CR1_UE))
  445. /** @brief Disable the USART associated to the SMARTCARD Handle
  446. * @param __HANDLE__: specifies the SMARTCARD Handle.
  447. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  448. * @retval None
  449. */
  450. #define __HAL_SMARTCARD_DISABLE(__HANDLE__) (CLEAR_BIT((__HANDLE__)->Instance->CR1, USART_CR1_UE))
  451. /** @brief Enable the SmartCard DMA request.
  452. * @param __HANDLE__: specifies the SmartCard Handle.
  453. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  454. * @param __REQUEST__: specifies the SmartCard DMA request.
  455. * This parameter can be one of the following values:
  456. * @arg SMARTCARD_DMAREQ_TX: SmartCard DMA transmit request
  457. * @arg SMARTCARD_DMAREQ_RX: SmartCard DMA receive request
  458. * @retval None
  459. */
  460. #define __HAL_SMARTCARD_DMA_REQUEST_ENABLE(__HANDLE__, __REQUEST__) (SET_BIT((__HANDLE__)->Instance->CR3, (__REQUEST__)))
  461. /** @brief Disable the SmartCard DMA request.
  462. * @param __HANDLE__: specifies the SmartCard Handle.
  463. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  464. * @param __REQUEST__: specifies the SmartCard DMA request.
  465. * This parameter can be one of the following values:
  466. * @arg SMARTCARD_DMAREQ_TX: SmartCard DMA transmit request
  467. * @arg SMARTCARD_DMAREQ_RX: SmartCard DMA receive request
  468. * @retval None
  469. */
  470. #define __HAL_SMARTCARD_DMA_REQUEST_DISABLE(__HANDLE__, __REQUEST__) (CLEAR_BIT((__HANDLE__)->Instance->CR3, (__REQUEST__)))
  471. /**
  472. * @}
  473. */
  474. /* Private macros --------------------------------------------------------*/
  475. /** @defgroup SMARTCARD_Private_Macros SMARTCARD Private Macros
  476. * @{
  477. */
  478. #define SMARTCARD_CR1_REG_INDEX 1
  479. #define SMARTCARD_CR3_REG_INDEX 3
  480. #define SMARTCARD_DIV(__PCLK__, __BAUD__) (((__PCLK__)*25)/(4*(__BAUD__)))
  481. #define SMARTCARD_DIVMANT(__PCLK__, __BAUD__) (SMARTCARD_DIV((__PCLK__), (__BAUD__))/100)
  482. #define SMARTCARD_DIVFRAQ(__PCLK__, __BAUD__) (((SMARTCARD_DIV((__PCLK__), (__BAUD__)) - (SMARTCARD_DIVMANT((__PCLK__), (__BAUD__)) * 100)) * 16 + 50) / 100)
  483. /* UART BRR = mantissa + overflow + fraction
  484. = (UART DIVMANT << 4) + (UART DIVFRAQ & 0xF0) + (UART DIVFRAQ & 0x0F) */
  485. #define SMARTCARD_BRR(_PCLK_, _BAUD_) (((SMARTCARD_DIVMANT((_PCLK_), (_BAUD_)) << 4) + \
  486. (SMARTCARD_DIVFRAQ((_PCLK_), (_BAUD_)) & 0xF0)) + \
  487. (SMARTCARD_DIVFRAQ((_PCLK_), (_BAUD_)) & 0x0F))
  488. /** Check the Baud rate range.
  489. * The maximum Baud Rate is derived from the maximum clock on APB (i.e. 32 MHz)
  490. * divided by the smallest oversampling used on the USART (i.e. 16)
  491. * __BAUDRATE__: Baud rate set by the configuration function.
  492. * Return : TRUE or FALSE
  493. */
  494. #define IS_SMARTCARD_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) < 2000001)
  495. #define IS_SMARTCARD_WORD_LENGTH(LENGTH) ((LENGTH) == SMARTCARD_WORDLENGTH_9B)
  496. #define IS_SMARTCARD_STOPBITS(STOPBITS) (((STOPBITS) == SMARTCARD_STOPBITS_0_5) || \
  497. ((STOPBITS) == SMARTCARD_STOPBITS_1_5))
  498. #define IS_SMARTCARD_PARITY(PARITY) (((PARITY) == SMARTCARD_PARITY_EVEN) || \
  499. ((PARITY) == SMARTCARD_PARITY_ODD))
  500. #define IS_SMARTCARD_MODE(MODE) ((((MODE) & (~((uint32_t)SMARTCARD_MODE_TX_RX))) == 0x00U) && \
  501. ((MODE) != 0x00000000U))
  502. #define IS_SMARTCARD_POLARITY(CPOL) (((CPOL) == SMARTCARD_POLARITY_LOW) || ((CPOL) == SMARTCARD_POLARITY_HIGH))
  503. #define IS_SMARTCARD_PHASE(CPHA) (((CPHA) == SMARTCARD_PHASE_1EDGE) || ((CPHA) == SMARTCARD_PHASE_2EDGE))
  504. #define IS_SMARTCARD_LASTBIT(LASTBIT) (((LASTBIT) == SMARTCARD_LASTBIT_DISABLE) || \
  505. ((LASTBIT) == SMARTCARD_LASTBIT_ENABLE))
  506. #define IS_SMARTCARD_ONE_BIT_SAMPLE(ONEBIT) (((ONEBIT) == SMARTCARD_ONE_BIT_SAMPLE_DISABLE) || \
  507. ((ONEBIT) == SMARTCARD_ONE_BIT_SAMPLE_ENABLE))
  508. #define IS_SMARTCARD_NACK_STATE(NACK) (((NACK) == SMARTCARD_NACK_ENABLE) || \
  509. ((NACK) == SMARTCARD_NACK_DISABLE))
  510. #define IS_SMARTCARD_PRESCALER(PRESCALER) (((PRESCALER) >= SMARTCARD_PRESCALER_SYSCLK_DIV2) && \
  511. ((PRESCALER) <= SMARTCARD_PRESCALER_SYSCLK_DIV62) )
  512. /** SMARTCARD interruptions flag mask
  513. *
  514. */
  515. #define SMARTCARD_IT_MASK ((uint32_t) USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RXNEIE | \
  516. USART_CR1_IDLEIE | USART_CR3_EIE )
  517. /**
  518. * @}
  519. */
  520. /* Exported functions --------------------------------------------------------*/
  521. /** @addtogroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  522. * @{
  523. */
  524. /** @addtogroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  525. * @{
  526. */
  527. /* Initialization/de-initialization functions **********************************/
  528. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc);
  529. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc);
  530. void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc);
  531. void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc);
  532. /**
  533. * @}
  534. */
  535. /** @addtogroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  536. * @{
  537. */
  538. /* IO operation functions *******************************************************/
  539. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  540. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  541. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
  542. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
  543. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
  544. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
  545. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc);
  546. void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc);
  547. void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc);
  548. void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc);
  549. /**
  550. * @}
  551. */
  552. /** @addtogroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
  553. * @{
  554. */
  555. /* Peripheral State and Errors functions functions *****************************/
  556. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc);
  557. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc);
  558. /**
  559. * @}
  560. */
  561. /**
  562. * @}
  563. */
  564. /**
  565. * @}
  566. */
  567. /**
  568. * @}
  569. */
  570. #ifdef __cplusplus
  571. }
  572. #endif
  573. #endif /* __STM32L1xx_HAL_SMARTCARD_H */
  574. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/