mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Sep 28 20:15:09 2015 +0100
Revision:
634:ac7d6880524d
Parent:
632:7687fb9c4f91
Synchronized with git revision 9b7d23d47153c298a6d24de9a415202705889d11

Full URL: https://github.com/mbedmicro/mbed/commit/9b7d23d47153c298a6d24de9a415202705889d11/

Revert "[NUCLEO_F303K8] add support of the STM32F303K8"

Who changed what in which revision?

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