mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
296:ec1b66a3d094
test with CLOCK_SETUP = 0

Who changed what in which revision?

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