stm32f1xx_hal_smartcard.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_smartcard.h
  4. * @author MCD Application Team
  5. * @brief Header file of SMARTCARD HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* Define to prevent recursive inclusion -------------------------------------*/
  19. #ifndef __STM32F1xx_HAL_SMARTCARD_H
  20. #define __STM32F1xx_HAL_SMARTCARD_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32f1xx_hal_def.h"
  26. /** @addtogroup STM32F1xx_HAL_Driver
  27. * @{
  28. */
  29. /** @addtogroup SMARTCARD
  30. * @{
  31. */
  32. /* Exported types ------------------------------------------------------------*/
  33. /** @defgroup SMARTCARD_Exported_Types SMARTCARD Exported Types
  34. * @{
  35. */
  36. /**
  37. * @brief SMARTCARD Init Structure definition
  38. */
  39. typedef struct
  40. {
  41. uint32_t BaudRate; /*!< This member configures the SmartCard communication baud rate.
  42. The baud rate is computed using the following formula:
  43. - IntegerDivider = ((PCLKx) / (16 * (hsc->Init.BaudRate)))
  44. - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 16) + 0.5 */
  45. uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  46. This parameter can be a value of @ref SMARTCARD_Word_Length */
  47. uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
  48. This parameter can be a value of @ref SMARTCARD_Stop_Bits */
  49. uint32_t Parity; /*!< Specifies the parity mode.
  50. This parameter can be a value of @ref SMARTCARD_Parity
  51. @note When parity is enabled, the computed parity is inserted
  52. at the MSB position of the transmitted data (9th bit when
  53. the word length is set to 9 data bits; 8th bit when the
  54. word length is set to 8 data bits).*/
  55. uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
  56. This parameter can be a value of @ref SMARTCARD_Mode */
  57. uint32_t CLKPolarity; /*!< Specifies the steady state of the serial clock.
  58. This parameter can be a value of @ref SMARTCARD_Clock_Polarity */
  59. uint32_t CLKPhase; /*!< Specifies the clock transition on which the bit capture is made.
  60. This parameter can be a value of @ref SMARTCARD_Clock_Phase */
  61. uint32_t CLKLastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
  62. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  63. This parameter can be a value of @ref SMARTCARD_Last_Bit */
  64. uint32_t Prescaler; /*!< Specifies the SmartCard Prescaler value used for dividing the system clock
  65. to provide the smartcard clock. The value given in the register (5 significant bits)
  66. is multiplied by 2 to give the division factor of the source clock frequency.
  67. This parameter can be a value of @ref SMARTCARD_Prescaler */
  68. uint32_t GuardTime; /*!< Specifies the SmartCard Guard Time value in terms of number of baud clocks */
  69. uint32_t NACKState; /*!< Specifies the SmartCard NACK Transmission state.
  70. This parameter can be a value of @ref SMARTCARD_NACK_State */
  71. }SMARTCARD_InitTypeDef;
  72. /**
  73. * @brief HAL SMARTCARD State structures definition
  74. * @note HAL SMARTCARD State value is a combination of 2 different substates: gState and RxState.
  75. * - gState contains SMARTCARD state information related to global Handle management
  76. * and also information related to Tx operations.
  77. * gState value coding follow below described bitmap :
  78. * b7-b6 Error information
  79. * 00 : No Error
  80. * 01 : (Not Used)
  81. * 10 : Timeout
  82. * 11 : Error
  83. * b5 IP initialization status
  84. * 0 : Reset (IP not initialized)
  85. * 1 : Init done (IP initialized. HAL SMARTCARD Init function already called)
  86. * b4-b3 (not used)
  87. * xx : Should be set to 00
  88. * b2 Intrinsic process state
  89. * 0 : Ready
  90. * 1 : Busy (IP busy with some configuration or internal operations)
  91. * b1 (not used)
  92. * x : Should be set to 0
  93. * b0 Tx state
  94. * 0 : Ready (no Tx operation ongoing)
  95. * 1 : Busy (Tx operation ongoing)
  96. * - RxState contains information related to Rx operations.
  97. * RxState value coding follow below described bitmap :
  98. * b7-b6 (not used)
  99. * xx : Should be set to 00
  100. * b5 IP initialization status
  101. * 0 : Reset (IP not initialized)
  102. * 1 : Init done (IP initialized)
  103. * b4-b2 (not used)
  104. * xxx : Should be set to 000
  105. * b1 Rx state
  106. * 0 : Ready (no Rx operation ongoing)
  107. * 1 : Busy (Rx operation ongoing)
  108. * b0 (not used)
  109. * x : Should be set to 0.
  110. */
  111. typedef enum
  112. {
  113. HAL_SMARTCARD_STATE_RESET = 0x00U, /*!< Peripheral is not yet Initialized
  114. Value is allowed for gState and RxState */
  115. HAL_SMARTCARD_STATE_READY = 0x20U, /*!< Peripheral Initialized and ready for use
  116. Value is allowed for gState and RxState */
  117. HAL_SMARTCARD_STATE_BUSY = 0x24U, /*!< an internal process is ongoing
  118. Value is allowed for gState only */
  119. HAL_SMARTCARD_STATE_BUSY_TX = 0x21U, /*!< Data Transmission process is ongoing
  120. Value is allowed for gState only */
  121. HAL_SMARTCARD_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing
  122. Value is allowed for RxState only */
  123. HAL_SMARTCARD_STATE_BUSY_TX_RX = 0x23U, /*!< Data Transmission and Reception process is ongoing
  124. Not to be used for neither gState nor RxState.
  125. Value is result of combination (Or) between gState and RxState values */
  126. HAL_SMARTCARD_STATE_TIMEOUT = 0xA0U, /*!< Timeout state
  127. Value is allowed for gState only */
  128. HAL_SMARTCARD_STATE_ERROR = 0xE0U /*!< Error
  129. Value is allowed for gState only */
  130. }HAL_SMARTCARD_StateTypeDef;
  131. /**
  132. * @brief SMARTCARD handle Structure definition
  133. */
  134. typedef struct __SMARTCARD_HandleTypeDef
  135. {
  136. USART_TypeDef *Instance; /*!< USART registers base address */
  137. SMARTCARD_InitTypeDef Init; /*!< SmartCard communication parameters */
  138. const uint8_t *pTxBuffPtr; /*!< Pointer to SmartCard Tx transfer Buffer */
  139. uint16_t TxXferSize; /*!< SmartCard Tx Transfer size */
  140. __IO uint16_t TxXferCount; /*!< SmartCard Tx Transfer Counter */
  141. uint8_t *pRxBuffPtr; /*!< Pointer to SmartCard Rx transfer Buffer */
  142. uint16_t RxXferSize; /*!< SmartCard Rx Transfer size */
  143. __IO uint16_t RxXferCount; /*!< SmartCard Rx Transfer Counter */
  144. DMA_HandleTypeDef *hdmatx; /*!< SmartCard Tx DMA Handle parameters */
  145. DMA_HandleTypeDef *hdmarx; /*!< SmartCard Rx DMA Handle parameters */
  146. HAL_LockTypeDef Lock; /*!< Locking object */
  147. __IO HAL_SMARTCARD_StateTypeDef gState; /*!< SmartCard state information related to global Handle management
  148. and also related to Tx operations.
  149. This parameter can be a value of @ref HAL_SMARTCARD_StateTypeDef */
  150. __IO HAL_SMARTCARD_StateTypeDef RxState; /*!< SmartCard state information related to Rx operations.
  151. This parameter can be a value of @ref HAL_SMARTCARD_StateTypeDef */
  152. __IO uint32_t ErrorCode; /*!< SmartCard Error code */
  153. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  154. void (* TxCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc); /*!< SMARTCARD Tx Complete Callback */
  155. void (* RxCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc); /*!< SMARTCARD Rx Complete Callback */
  156. void (* ErrorCallback)(struct __SMARTCARD_HandleTypeDef *hsc); /*!< SMARTCARD Error Callback */
  157. void (* AbortCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc); /*!< SMARTCARD Abort Complete Callback */
  158. void (* AbortTransmitCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc); /*!< SMARTCARD Abort Transmit Complete Callback */
  159. void (* AbortReceiveCpltCallback)(struct __SMARTCARD_HandleTypeDef *hsc); /*!< SMARTCARD Abort Receive Complete Callback */
  160. void (* MspInitCallback)(struct __SMARTCARD_HandleTypeDef *hsc); /*!< SMARTCARD Msp Init callback */
  161. void (* MspDeInitCallback)(struct __SMARTCARD_HandleTypeDef *hsc); /*!< SMARTCARD Msp DeInit callback */
  162. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  163. } SMARTCARD_HandleTypeDef;
  164. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  165. /**
  166. * @brief HAL SMARTCARD Callback ID enumeration definition
  167. */
  168. typedef enum
  169. {
  170. HAL_SMARTCARD_TX_COMPLETE_CB_ID = 0x00U, /*!< SMARTCARD Tx Complete Callback ID */
  171. HAL_SMARTCARD_RX_COMPLETE_CB_ID = 0x01U, /*!< SMARTCARD Rx Complete Callback ID */
  172. HAL_SMARTCARD_ERROR_CB_ID = 0x02U, /*!< SMARTCARD Error Callback ID */
  173. HAL_SMARTCARD_ABORT_COMPLETE_CB_ID = 0x03U, /*!< SMARTCARD Abort Complete Callback ID */
  174. HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID = 0x04U, /*!< SMARTCARD Abort Transmit Complete Callback ID */
  175. HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID = 0x05U, /*!< SMARTCARD Abort Receive Complete Callback ID */
  176. HAL_SMARTCARD_MSPINIT_CB_ID = 0x08U, /*!< SMARTCARD MspInit callback ID */
  177. HAL_SMARTCARD_MSPDEINIT_CB_ID = 0x09U /*!< SMARTCARD MspDeInit callback ID */
  178. } HAL_SMARTCARD_CallbackIDTypeDef;
  179. /**
  180. * @brief HAL SMARTCARD Callback pointer definition
  181. */
  182. typedef void (*pSMARTCARD_CallbackTypeDef)(SMARTCARD_HandleTypeDef *hsc); /*!< pointer to an SMARTCARD callback function */
  183. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  184. /**
  185. * @}
  186. */
  187. /* Exported constants --------------------------------------------------------*/
  188. /** @defgroup SMARTCARD_Exported_Constants SMARTCARD Exported constants
  189. * @{
  190. */
  191. /** @defgroup SMARTCARD_Error_Code SMARTCARD Error Code
  192. * @{
  193. */
  194. #define HAL_SMARTCARD_ERROR_NONE 0x00000000U /*!< No error */
  195. #define HAL_SMARTCARD_ERROR_PE 0x00000001U /*!< Parity error */
  196. #define HAL_SMARTCARD_ERROR_NE 0x00000002U /*!< Noise error */
  197. #define HAL_SMARTCARD_ERROR_FE 0x00000004U /*!< Frame error */
  198. #define HAL_SMARTCARD_ERROR_ORE 0x00000008U /*!< Overrun error */
  199. #define HAL_SMARTCARD_ERROR_DMA 0x00000010U /*!< DMA transfer error */
  200. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  201. #define HAL_SMARTCARD_ERROR_INVALID_CALLBACK 0x00000020U /*!< Invalid Callback error */
  202. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  203. /**
  204. * @}
  205. */
  206. /** @defgroup SMARTCARD_Word_Length SMARTCARD Word Length
  207. * @{
  208. */
  209. #define SMARTCARD_WORDLENGTH_9B ((uint32_t)USART_CR1_M)
  210. /**
  211. * @}
  212. */
  213. /** @defgroup SMARTCARD_Stop_Bits SMARTCARD Number of Stop Bits
  214. * @{
  215. */
  216. #define SMARTCARD_STOPBITS_0_5 ((uint32_t)USART_CR2_STOP_0)
  217. #define SMARTCARD_STOPBITS_1_5 ((uint32_t)(USART_CR2_STOP_0 | USART_CR2_STOP_1))
  218. /**
  219. * @}
  220. */
  221. /** @defgroup SMARTCARD_Parity SMARTCARD Parity
  222. * @{
  223. */
  224. #define SMARTCARD_PARITY_EVEN ((uint32_t)USART_CR1_PCE)
  225. #define SMARTCARD_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
  226. /**
  227. * @}
  228. */
  229. /** @defgroup SMARTCARD_Mode SMARTCARD Mode
  230. * @{
  231. */
  232. #define SMARTCARD_MODE_RX ((uint32_t)USART_CR1_RE)
  233. #define SMARTCARD_MODE_TX ((uint32_t)USART_CR1_TE)
  234. #define SMARTCARD_MODE_TX_RX ((uint32_t)(USART_CR1_TE |USART_CR1_RE))
  235. /**
  236. * @}
  237. */
  238. /** @defgroup SMARTCARD_Clock_Polarity SMARTCARD Clock Polarity
  239. * @{
  240. */
  241. #define SMARTCARD_POLARITY_LOW 0x00000000U
  242. #define SMARTCARD_POLARITY_HIGH ((uint32_t)USART_CR2_CPOL)
  243. /**
  244. * @}
  245. */
  246. /** @defgroup SMARTCARD_Clock_Phase SMARTCARD Clock Phase
  247. * @{
  248. */
  249. #define SMARTCARD_PHASE_1EDGE 0x00000000U
  250. #define SMARTCARD_PHASE_2EDGE ((uint32_t)USART_CR2_CPHA)
  251. /**
  252. * @}
  253. */
  254. /** @defgroup SMARTCARD_Last_Bit SMARTCARD Last Bit
  255. * @{
  256. */
  257. #define SMARTCARD_LASTBIT_DISABLE 0x00000000U
  258. #define SMARTCARD_LASTBIT_ENABLE ((uint32_t)USART_CR2_LBCL)
  259. /**
  260. * @}
  261. */
  262. /** @defgroup SMARTCARD_NACK_State SMARTCARD NACK State
  263. * @{
  264. */
  265. #define SMARTCARD_NACK_ENABLE ((uint32_t)USART_CR3_NACK)
  266. #define SMARTCARD_NACK_DISABLE 0x00000000U
  267. /**
  268. * @}
  269. */
  270. /** @defgroup SMARTCARD_DMA_Requests SMARTCARD DMA requests
  271. * @{
  272. */
  273. #define SMARTCARD_DMAREQ_TX ((uint32_t)USART_CR3_DMAT)
  274. #define SMARTCARD_DMAREQ_RX ((uint32_t)USART_CR3_DMAR)
  275. /**
  276. * @}
  277. */
  278. /** @defgroup SMARTCARD_Prescaler SMARTCARD Prescaler
  279. * @{
  280. */
  281. #define SMARTCARD_PRESCALER_SYSCLK_DIV2 0x00000001U /*!< SYSCLK divided by 2 */
  282. #define SMARTCARD_PRESCALER_SYSCLK_DIV4 0x00000002U /*!< SYSCLK divided by 4 */
  283. #define SMARTCARD_PRESCALER_SYSCLK_DIV6 0x00000003U /*!< SYSCLK divided by 6 */
  284. #define SMARTCARD_PRESCALER_SYSCLK_DIV8 0x00000004U /*!< SYSCLK divided by 8 */
  285. #define SMARTCARD_PRESCALER_SYSCLK_DIV10 0x00000005U /*!< SYSCLK divided by 10 */
  286. #define SMARTCARD_PRESCALER_SYSCLK_DIV12 0x00000006U /*!< SYSCLK divided by 12 */
  287. #define SMARTCARD_PRESCALER_SYSCLK_DIV14 0x00000007U /*!< SYSCLK divided by 14 */
  288. #define SMARTCARD_PRESCALER_SYSCLK_DIV16 0x00000008U /*!< SYSCLK divided by 16 */
  289. #define SMARTCARD_PRESCALER_SYSCLK_DIV18 0x00000009U /*!< SYSCLK divided by 18 */
  290. #define SMARTCARD_PRESCALER_SYSCLK_DIV20 0x0000000AU /*!< SYSCLK divided by 20 */
  291. #define SMARTCARD_PRESCALER_SYSCLK_DIV22 0x0000000BU /*!< SYSCLK divided by 22 */
  292. #define SMARTCARD_PRESCALER_SYSCLK_DIV24 0x0000000CU /*!< SYSCLK divided by 24 */
  293. #define SMARTCARD_PRESCALER_SYSCLK_DIV26 0x0000000DU /*!< SYSCLK divided by 26 */
  294. #define SMARTCARD_PRESCALER_SYSCLK_DIV28 0x0000000EU /*!< SYSCLK divided by 28 */
  295. #define SMARTCARD_PRESCALER_SYSCLK_DIV30 0x0000000FU /*!< SYSCLK divided by 30 */
  296. #define SMARTCARD_PRESCALER_SYSCLK_DIV32 0x00000010U /*!< SYSCLK divided by 32 */
  297. #define SMARTCARD_PRESCALER_SYSCLK_DIV34 0x00000011U /*!< SYSCLK divided by 34 */
  298. #define SMARTCARD_PRESCALER_SYSCLK_DIV36 0x00000012U /*!< SYSCLK divided by 36 */
  299. #define SMARTCARD_PRESCALER_SYSCLK_DIV38 0x00000013U /*!< SYSCLK divided by 38 */
  300. #define SMARTCARD_PRESCALER_SYSCLK_DIV40 0x00000014U /*!< SYSCLK divided by 40 */
  301. #define SMARTCARD_PRESCALER_SYSCLK_DIV42 0x00000015U /*!< SYSCLK divided by 42 */
  302. #define SMARTCARD_PRESCALER_SYSCLK_DIV44 0x00000016U /*!< SYSCLK divided by 44 */
  303. #define SMARTCARD_PRESCALER_SYSCLK_DIV46 0x00000017U /*!< SYSCLK divided by 46 */
  304. #define SMARTCARD_PRESCALER_SYSCLK_DIV48 0x00000018U /*!< SYSCLK divided by 48 */
  305. #define SMARTCARD_PRESCALER_SYSCLK_DIV50 0x00000019U /*!< SYSCLK divided by 50 */
  306. #define SMARTCARD_PRESCALER_SYSCLK_DIV52 0x0000001AU /*!< SYSCLK divided by 52 */
  307. #define SMARTCARD_PRESCALER_SYSCLK_DIV54 0x0000001BU /*!< SYSCLK divided by 54 */
  308. #define SMARTCARD_PRESCALER_SYSCLK_DIV56 0x0000001CU /*!< SYSCLK divided by 56 */
  309. #define SMARTCARD_PRESCALER_SYSCLK_DIV58 0x0000001DU /*!< SYSCLK divided by 58 */
  310. #define SMARTCARD_PRESCALER_SYSCLK_DIV60 0x0000001EU /*!< SYSCLK divided by 60 */
  311. #define SMARTCARD_PRESCALER_SYSCLK_DIV62 0x0000001FU /*!< SYSCLK divided by 62 */
  312. /**
  313. * @}
  314. */
  315. /** @defgroup SmartCard_Flags SMARTCARD Flags
  316. * Elements values convention: 0xXXXX
  317. * - 0xXXXX : Flag mask in the SR register
  318. * @{
  319. */
  320. #define SMARTCARD_FLAG_TXE ((uint32_t)USART_SR_TXE)
  321. #define SMARTCARD_FLAG_TC ((uint32_t)USART_SR_TC)
  322. #define SMARTCARD_FLAG_RXNE ((uint32_t)USART_SR_RXNE)
  323. #define SMARTCARD_FLAG_IDLE ((uint32_t)USART_SR_IDLE)
  324. #define SMARTCARD_FLAG_ORE ((uint32_t)USART_SR_ORE)
  325. #define SMARTCARD_FLAG_NE ((uint32_t)USART_SR_NE)
  326. #define SMARTCARD_FLAG_FE ((uint32_t)USART_SR_FE)
  327. #define SMARTCARD_FLAG_PE ((uint32_t)USART_SR_PE)
  328. /**
  329. * @}
  330. */
  331. /** @defgroup SmartCard_Interrupt_definition SMARTCARD Interrupts Definition
  332. * Elements values convention: 0xY000XXXX
  333. * - XXXX : Interrupt mask in the Y register
  334. * - Y : Interrupt source register (2bits)
  335. * - 01: CR1 register
  336. * - 11: CR3 register
  337. * @{
  338. */
  339. #define SMARTCARD_IT_PE ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_PEIE))
  340. #define SMARTCARD_IT_TXE ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_TXEIE))
  341. #define SMARTCARD_IT_TC ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_TCIE))
  342. #define SMARTCARD_IT_RXNE ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_RXNEIE))
  343. #define SMARTCARD_IT_IDLE ((uint32_t)(SMARTCARD_CR1_REG_INDEX << 28U | USART_CR1_IDLEIE))
  344. #define SMARTCARD_IT_ERR ((uint32_t)(SMARTCARD_CR3_REG_INDEX << 28U | USART_CR3_EIE))
  345. /**
  346. * @}
  347. */
  348. /**
  349. * @}
  350. */
  351. /* Exported macro ------------------------------------------------------------*/
  352. /** @defgroup SMARTCARD_Exported_Macros SMARTCARD Exported Macros
  353. * @{
  354. */
  355. /** @brief Reset SMARTCARD handle gstate & RxState
  356. * @param __HANDLE__ specifies the SMARTCARD Handle.
  357. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  358. * @retval None
  359. */
  360. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  361. #define __HAL_SMARTCARD_RESET_HANDLE_STATE(__HANDLE__) do{ \
  362. (__HANDLE__)->gState = HAL_SMARTCARD_STATE_RESET; \
  363. (__HANDLE__)->RxState = HAL_SMARTCARD_STATE_RESET; \
  364. (__HANDLE__)->MspInitCallback = NULL; \
  365. (__HANDLE__)->MspDeInitCallback = NULL; \
  366. } while(0U)
  367. #else
  368. #define __HAL_SMARTCARD_RESET_HANDLE_STATE(__HANDLE__) do{ \
  369. (__HANDLE__)->gState = HAL_SMARTCARD_STATE_RESET; \
  370. (__HANDLE__)->RxState = HAL_SMARTCARD_STATE_RESET; \
  371. } while(0U)
  372. #endif /*USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  373. /** @brief Flush the Smartcard DR register
  374. * @param __HANDLE__ specifies the SMARTCARD Handle.
  375. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  376. * @retval None
  377. */
  378. #define __HAL_SMARTCARD_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
  379. /** @brief Check whether the specified Smartcard flag is set or not.
  380. * @param __HANDLE__ specifies the SMARTCARD Handle.
  381. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  382. * @param __FLAG__ specifies the flag to check.
  383. * This parameter can be one of the following values:
  384. * @arg SMARTCARD_FLAG_TXE: Transmit data register empty flag
  385. * @arg SMARTCARD_FLAG_TC: Transmission Complete flag
  386. * @arg SMARTCARD_FLAG_RXNE: Receive data register not empty flag
  387. * @arg SMARTCARD_FLAG_IDLE: Idle Line detection flag
  388. * @arg SMARTCARD_FLAG_ORE: Overrun Error flag
  389. * @arg SMARTCARD_FLAG_NE: Noise Error flag
  390. * @arg SMARTCARD_FLAG_FE: Framing Error flag
  391. * @arg SMARTCARD_FLAG_PE: Parity Error flag
  392. * @retval The new state of __FLAG__ (TRUE or FALSE).
  393. */
  394. #define __HAL_SMARTCARD_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
  395. /** @brief Clear the specified Smartcard pending flags.
  396. * @param __HANDLE__ specifies the SMARTCARD Handle.
  397. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  398. * @param __FLAG__ specifies the flag to check.
  399. * This parameter can be any combination of the following values:
  400. * @arg SMARTCARD_FLAG_TC: Transmission Complete flag.
  401. * @arg SMARTCARD_FLAG_RXNE: Receive data register not empty flag.
  402. *
  403. * @note PE (Parity error), FE (Framing error), NE (Noise error) and ORE (Overrun
  404. * error) flags are cleared by software sequence: a read operation to
  405. * USART_SR register followed by a read operation to USART_DR register.
  406. * @note RXNE flag can be also cleared by a read to the USART_DR register.
  407. * @note TC flag can be also cleared by software sequence: a read operation to
  408. * USART_SR register followed by a write operation to USART_DR register.
  409. * @note TXE flag is cleared only by a write to the USART_DR register.
  410. * @retval None
  411. */
  412. #define __HAL_SMARTCARD_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
  413. /** @brief Clear the SMARTCARD PE pending flag.
  414. * @param __HANDLE__ specifies the USART Handle.
  415. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  416. * @retval None
  417. */
  418. #define __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__) \
  419. do{ \
  420. __IO uint32_t tmpreg = 0x00U; \
  421. tmpreg = (__HANDLE__)->Instance->SR; \
  422. tmpreg = (__HANDLE__)->Instance->DR; \
  423. UNUSED(tmpreg); \
  424. } while(0U)
  425. /** @brief Clear the SMARTCARD FE pending flag.
  426. * @param __HANDLE__ specifies the USART Handle.
  427. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  428. * @retval None
  429. */
  430. #define __HAL_SMARTCARD_CLEAR_FEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
  431. /** @brief Clear the SMARTCARD NE pending flag.
  432. * @param __HANDLE__ specifies the USART Handle.
  433. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  434. * @retval None
  435. */
  436. #define __HAL_SMARTCARD_CLEAR_NEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
  437. /** @brief Clear the SMARTCARD ORE pending flag.
  438. * @param __HANDLE__ specifies the USART Handle.
  439. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  440. * @retval None
  441. */
  442. #define __HAL_SMARTCARD_CLEAR_OREFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
  443. /** @brief Clear the SMARTCARD IDLE pending flag.
  444. * @param __HANDLE__ specifies the USART Handle.
  445. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  446. * @retval None
  447. */
  448. #define __HAL_SMARTCARD_CLEAR_IDLEFLAG(__HANDLE__) __HAL_SMARTCARD_CLEAR_PEFLAG(__HANDLE__)
  449. /** @brief Enable the specified SmartCard interrupt.
  450. * @param __HANDLE__ specifies the SMARTCARD Handle.
  451. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  452. * @param __INTERRUPT__ specifies the SMARTCARD interrupt to enable.
  453. * This parameter can be one of the following values:
  454. * @arg SMARTCARD_IT_TXE: Transmit Data Register empty interrupt
  455. * @arg SMARTCARD_IT_TC: Transmission complete interrupt
  456. * @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
  457. * @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
  458. * @arg SMARTCARD_IT_PE: Parity Error interrupt
  459. * @arg SMARTCARD_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  460. * @retval None
  461. */
  462. #define __HAL_SMARTCARD_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == SMARTCARD_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & SMARTCARD_IT_MASK)): \
  463. ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & SMARTCARD_IT_MASK)))
  464. /** @brief Disable the specified SmartCard interrupt.
  465. * @param __HANDLE__ specifies the SMARTCARD Handle.
  466. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  467. * @param __INTERRUPT__ specifies the SMARTCARD interrupt to disable.
  468. * This parameter can be one of the following values:
  469. * @arg SMARTCARD_IT_TXE: Transmit Data Register empty interrupt
  470. * @arg SMARTCARD_IT_TC: Transmission complete interrupt
  471. * @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
  472. * @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
  473. * @arg SMARTCARD_IT_PE: Parity Error interrupt
  474. * @arg SMARTCARD_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  475. * @retval None
  476. */
  477. #define __HAL_SMARTCARD_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == SMARTCARD_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & SMARTCARD_IT_MASK)): \
  478. ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & SMARTCARD_IT_MASK)))
  479. /** @brief Checks whether the specified SmartCard interrupt has occurred or not.
  480. * @param __HANDLE__ specifies the SmartCard Handle.
  481. * @param __IT__ specifies the SMARTCARD interrupt source to check.
  482. * This parameter can be one of the following values:
  483. * @arg SMARTCARD_IT_TXE: Transmit Data Register empty interrupt
  484. * @arg SMARTCARD_IT_TC: Transmission complete interrupt
  485. * @arg SMARTCARD_IT_RXNE: Receive Data register not empty interrupt
  486. * @arg SMARTCARD_IT_IDLE: Idle line detection interrupt
  487. * @arg SMARTCARD_IT_ERR: Error interrupt
  488. * @arg SMARTCARD_IT_PE: Parity Error interrupt
  489. * @retval The new state of __IT__ (TRUE or FALSE).
  490. */
  491. #define __HAL_SMARTCARD_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28U) == SMARTCARD_CR1_REG_INDEX)? (__HANDLE__)->Instance->CR1: (__HANDLE__)->Instance->CR3) & (((uint32_t)(__IT__)) & SMARTCARD_IT_MASK))
  492. /** @brief Enable the USART associated to the SMARTCARD Handle
  493. * @param __HANDLE__ specifies the SMARTCARD Handle.
  494. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  495. * @retval None
  496. */
  497. #define __HAL_SMARTCARD_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
  498. /** @brief Disable the USART associated to the SMARTCARD Handle
  499. * @param __HANDLE__ specifies the SMARTCARD Handle.
  500. * SMARTCARD Handle selects the USARTx peripheral (USART availability and x value depending on device).
  501. * @retval None
  502. */
  503. #define __HAL_SMARTCARD_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
  504. /** @brief Macros to enable the SmartCard DMA request.
  505. * @param __HANDLE__ specifies the SmartCard Handle.
  506. * @param __REQUEST__ specifies the SmartCard DMA request.
  507. * This parameter can be one of the following values:
  508. * @arg SMARTCARD_DMAREQ_TX: SmartCard DMA transmit request
  509. * @arg SMARTCARD_DMAREQ_RX: SmartCard DMA receive request
  510. * @retval None
  511. */
  512. #define __HAL_SMARTCARD_DMA_REQUEST_ENABLE(__HANDLE__, __REQUEST__) ((__HANDLE__)->Instance->CR3 |= (__REQUEST__))
  513. /** @brief Macros to disable the SmartCard DMA request.
  514. * @param __HANDLE__ specifies the SmartCard Handle.
  515. * @param __REQUEST__ specifies the SmartCard DMA request.
  516. * This parameter can be one of the following values:
  517. * @arg SMARTCARD_DMAREQ_TX: SmartCard DMA transmit request
  518. * @arg SMARTCARD_DMAREQ_RX: SmartCard DMA receive request
  519. * @retval None
  520. */
  521. #define __HAL_SMARTCARD_DMA_REQUEST_DISABLE(__HANDLE__, __REQUEST__) ((__HANDLE__)->Instance->CR3 &= ~(__REQUEST__))
  522. /**
  523. * @}
  524. */
  525. /* Exported functions --------------------------------------------------------*/
  526. /** @addtogroup SMARTCARD_Exported_Functions
  527. * @{
  528. */
  529. /** @addtogroup SMARTCARD_Exported_Functions_Group1
  530. * @{
  531. */
  532. /* Initialization/de-initialization functions **********************************/
  533. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc);
  534. HAL_StatusTypeDef HAL_SMARTCARD_ReInit(SMARTCARD_HandleTypeDef *hsc);
  535. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc);
  536. void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc);
  537. void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc);
  538. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  539. /* Callbacks Register/UnRegister functions ***********************************/
  540. HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback);
  541. HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID);
  542. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  543. /**
  544. * @}
  545. */
  546. /** @addtogroup SMARTCARD_Exported_Functions_Group2
  547. * @{
  548. */
  549. /* IO operation functions *******************************************************/
  550. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size, uint32_t Timeout);
  551. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  552. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size);
  553. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
  554. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, const uint8_t *pData, uint16_t Size);
  555. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size);
  556. /* Transfer Abort functions */
  557. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc);
  558. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc);
  559. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc);
  560. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc);
  561. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc);
  562. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc);
  563. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc);
  564. void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc);
  565. void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc);
  566. void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc);
  567. void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsc);
  568. void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsc);
  569. void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsc);
  570. /**
  571. * @}
  572. */
  573. /** @addtogroup SMARTCARD_Exported_Functions_Group3
  574. * @{
  575. */
  576. /* Peripheral State functions **************************************************/
  577. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(const SMARTCARD_HandleTypeDef *hsc);
  578. uint32_t HAL_SMARTCARD_GetError(const SMARTCARD_HandleTypeDef *hsc);
  579. /**
  580. * @}
  581. */
  582. /**
  583. * @}
  584. */
  585. /* Private types -------------------------------------------------------------*/
  586. /* Private variables ---------------------------------------------------------*/
  587. /* Private constants ---------------------------------------------------------*/
  588. /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
  589. * @{
  590. */
  591. /** @brief SMARTCARD interruptions flag mask
  592. *
  593. */
  594. #define SMARTCARD_IT_MASK ((uint32_t) USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RXNEIE | \
  595. USART_CR1_IDLEIE | USART_CR3_EIE )
  596. #define SMARTCARD_CR1_REG_INDEX 1U
  597. #define SMARTCARD_CR3_REG_INDEX 3U
  598. /**
  599. * @}
  600. */
  601. /* Private macros --------------------------------------------------------*/
  602. /** @defgroup SMARTCARD_Private_Macros SMARTCARD Private Macros
  603. * @{
  604. */
  605. #define IS_SMARTCARD_WORD_LENGTH(LENGTH) ((LENGTH) == SMARTCARD_WORDLENGTH_9B)
  606. #define IS_SMARTCARD_STOPBITS(STOPBITS) (((STOPBITS) == SMARTCARD_STOPBITS_0_5) || \
  607. ((STOPBITS) == SMARTCARD_STOPBITS_1_5))
  608. #define IS_SMARTCARD_PARITY(PARITY) (((PARITY) == SMARTCARD_PARITY_EVEN) || \
  609. ((PARITY) == SMARTCARD_PARITY_ODD))
  610. #define IS_SMARTCARD_MODE(MODE) ((((MODE) & 0x0000FFF3U) == 0x00U) && ((MODE) != 0x000000U))
  611. #define IS_SMARTCARD_POLARITY(CPOL) (((CPOL) == SMARTCARD_POLARITY_LOW) || ((CPOL) == SMARTCARD_POLARITY_HIGH))
  612. #define IS_SMARTCARD_PHASE(CPHA) (((CPHA) == SMARTCARD_PHASE_1EDGE) || ((CPHA) == SMARTCARD_PHASE_2EDGE))
  613. #define IS_SMARTCARD_LASTBIT(LASTBIT) (((LASTBIT) == SMARTCARD_LASTBIT_DISABLE) || \
  614. ((LASTBIT) == SMARTCARD_LASTBIT_ENABLE))
  615. #define IS_SMARTCARD_NACK_STATE(NACK) (((NACK) == SMARTCARD_NACK_ENABLE) || \
  616. ((NACK) == SMARTCARD_NACK_DISABLE))
  617. #define IS_SMARTCARD_BAUDRATE(BAUDRATE) ((BAUDRATE) < 4500001U)
  618. #define SMARTCARD_DIV(__PCLK__, __BAUD__) (((__PCLK__)*25U)/(4U*(__BAUD__)))
  619. #define SMARTCARD_DIVMANT(__PCLK__, __BAUD__) (SMARTCARD_DIV((__PCLK__), (__BAUD__))/100U)
  620. #define SMARTCARD_DIVFRAQ(__PCLK__, __BAUD__) ((((SMARTCARD_DIV((__PCLK__), (__BAUD__)) - (SMARTCARD_DIVMANT((__PCLK__), (__BAUD__)) * 100U)) * 16U) + 50U) / 100U)
  621. /* SMARTCARD BRR = mantissa + overflow + fraction
  622. = (SMARTCARD DIVMANT << 4) + (SMARTCARD DIVFRAQ & 0xF0) + (SMARTCARD DIVFRAQ & 0x0FU) */
  623. #define SMARTCARD_BRR(__PCLK__, __BAUD__) (((SMARTCARD_DIVMANT((__PCLK__), (__BAUD__)) << 4U) + \
  624. (SMARTCARD_DIVFRAQ((__PCLK__), (__BAUD__)) & 0xF0U)) + \
  625. (SMARTCARD_DIVFRAQ((__PCLK__), (__BAUD__)) & 0x0FU))
  626. /**
  627. * @}
  628. */
  629. /* Private functions ---------------------------------------------------------*/
  630. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  631. * @{
  632. */
  633. /**
  634. * @}
  635. */
  636. /**
  637. * @}
  638. */
  639. /**
  640. * @}
  641. */
  642. #ifdef __cplusplus
  643. }
  644. #endif
  645. #endif /* __STM32F1xx_HAL_SMARTCARD_H */