mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jun 24 14:45:08 2014 +0100
Revision:
237:f3da66175598
Child:
375:3d36234a1087
Synchronized with git revision 8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7

Full URL: https://github.com/mbedmicro/mbed/commit/8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7/

[NUCLEO_F334R8] Add platform files

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