Subversion Repositories LedShow

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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