inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1 /**
NYX 0:85b3fd62ea1a 2 ******************************************************************************
NYX 0:85b3fd62ea1a 3 * @file stm32f4xx_hal_smartcard.c
NYX 0:85b3fd62ea1a 4 * @author MCD Application Team
NYX 0:85b3fd62ea1a 5 * @version V1.7.1
NYX 0:85b3fd62ea1a 6 * @date 14-April-2017
NYX 0:85b3fd62ea1a 7 * @brief SMARTCARD HAL module driver.
NYX 0:85b3fd62ea1a 8 * This file provides firmware functions to manage the following
NYX 0:85b3fd62ea1a 9 * functionalities of the SMARTCARD peripheral:
NYX 0:85b3fd62ea1a 10 * + Initialization and de-initialization functions
NYX 0:85b3fd62ea1a 11 * + IO operation functions
NYX 0:85b3fd62ea1a 12 * + Peripheral State and Errors functions
NYX 0:85b3fd62ea1a 13 *
NYX 0:85b3fd62ea1a 14 @verbatim
NYX 0:85b3fd62ea1a 15 ==============================================================================
NYX 0:85b3fd62ea1a 16 ##### How to use this driver #####
NYX 0:85b3fd62ea1a 17 ==============================================================================
NYX 0:85b3fd62ea1a 18 [..]
NYX 0:85b3fd62ea1a 19 The SMARTCARD HAL driver can be used as follows:
NYX 0:85b3fd62ea1a 20
NYX 0:85b3fd62ea1a 21 (#) Declare a SMARTCARD_HandleTypeDef handle structure.
NYX 0:85b3fd62ea1a 22 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
NYX 0:85b3fd62ea1a 23 (##) Enable the USARTx interface clock.
NYX 0:85b3fd62ea1a 24 (##) SMARTCARD pins configuration:
NYX 0:85b3fd62ea1a 25 (+++) Enable the clock for the SMARTCARD GPIOs.
NYX 0:85b3fd62ea1a 26 (+++) Configure these SMARTCARD pins as alternate function pull-up.
NYX 0:85b3fd62ea1a 27 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
NYX 0:85b3fd62ea1a 28 and HAL_SMARTCARD_Receive_IT() APIs):
NYX 0:85b3fd62ea1a 29 (+++) Configure the USARTx interrupt priority.
NYX 0:85b3fd62ea1a 30 (+++) Enable the NVIC USART IRQ handle.
NYX 0:85b3fd62ea1a 31 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
NYX 0:85b3fd62ea1a 32 and HAL_SMARTCARD_Receive_DMA() APIs):
NYX 0:85b3fd62ea1a 33 (+++) Declare a DMA handle structure for the Tx/Rx stream.
NYX 0:85b3fd62ea1a 34 (+++) Enable the DMAx interface clock.
NYX 0:85b3fd62ea1a 35 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
NYX 0:85b3fd62ea1a 36 (+++) Configure the DMA Tx/Rx Stream.
NYX 0:85b3fd62ea1a 37 (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
NYX 0:85b3fd62ea1a 38 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
NYX 0:85b3fd62ea1a 39
NYX 0:85b3fd62ea1a 40 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
NYX 0:85b3fd62ea1a 41 flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
NYX 0:85b3fd62ea1a 42
NYX 0:85b3fd62ea1a 43 (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
NYX 0:85b3fd62ea1a 44 (++) These APIs configure also the low level Hardware GPIO, CLOCK, CORTEX...etc)
NYX 0:85b3fd62ea1a 45 by calling the customized HAL_SMARTCARD_MspInit() API.
NYX 0:85b3fd62ea1a 46 [..]
NYX 0:85b3fd62ea1a 47 (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
NYX 0:85b3fd62ea1a 48 RXNE interrupt and Error Interrupts) will be managed using the macros
NYX 0:85b3fd62ea1a 49 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
NYX 0:85b3fd62ea1a 50
NYX 0:85b3fd62ea1a 51 [..]
NYX 0:85b3fd62ea1a 52 Three operation modes are available within this driver :
NYX 0:85b3fd62ea1a 53
NYX 0:85b3fd62ea1a 54 *** Polling mode IO operation ***
NYX 0:85b3fd62ea1a 55 =================================
NYX 0:85b3fd62ea1a 56 [..]
NYX 0:85b3fd62ea1a 57 (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
NYX 0:85b3fd62ea1a 58 (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
NYX 0:85b3fd62ea1a 59
NYX 0:85b3fd62ea1a 60 *** Interrupt mode IO operation ***
NYX 0:85b3fd62ea1a 61 ===================================
NYX 0:85b3fd62ea1a 62 [..]
NYX 0:85b3fd62ea1a 63 (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
NYX 0:85b3fd62ea1a 64 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
NYX 0:85b3fd62ea1a 65 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
NYX 0:85b3fd62ea1a 66 (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
NYX 0:85b3fd62ea1a 67 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
NYX 0:85b3fd62ea1a 68 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
NYX 0:85b3fd62ea1a 69 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
NYX 0:85b3fd62ea1a 70 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
NYX 0:85b3fd62ea1a 71
NYX 0:85b3fd62ea1a 72 *** DMA mode IO operation ***
NYX 0:85b3fd62ea1a 73 ==============================
NYX 0:85b3fd62ea1a 74 [..]
NYX 0:85b3fd62ea1a 75 (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
NYX 0:85b3fd62ea1a 76 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
NYX 0:85b3fd62ea1a 77 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
NYX 0:85b3fd62ea1a 78 (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
NYX 0:85b3fd62ea1a 79 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
NYX 0:85b3fd62ea1a 80 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
NYX 0:85b3fd62ea1a 81 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
NYX 0:85b3fd62ea1a 82 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
NYX 0:85b3fd62ea1a 83
NYX 0:85b3fd62ea1a 84 *** SMARTCARD HAL driver macros list ***
NYX 0:85b3fd62ea1a 85 =============================================
NYX 0:85b3fd62ea1a 86 [..]
NYX 0:85b3fd62ea1a 87 Below the list of most used macros in SMARTCARD HAL driver.
NYX 0:85b3fd62ea1a 88
NYX 0:85b3fd62ea1a 89 (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
NYX 0:85b3fd62ea1a 90 (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
NYX 0:85b3fd62ea1a 91 (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
NYX 0:85b3fd62ea1a 92 (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
NYX 0:85b3fd62ea1a 93 (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
NYX 0:85b3fd62ea1a 94 (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
NYX 0:85b3fd62ea1a 95
NYX 0:85b3fd62ea1a 96 [..]
NYX 0:85b3fd62ea1a 97 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
NYX 0:85b3fd62ea1a 98
NYX 0:85b3fd62ea1a 99 @endverbatim
NYX 0:85b3fd62ea1a 100 ******************************************************************************
NYX 0:85b3fd62ea1a 101 * @attention
NYX 0:85b3fd62ea1a 102 *
NYX 0:85b3fd62ea1a 103 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
NYX 0:85b3fd62ea1a 104 *
NYX 0:85b3fd62ea1a 105 * Redistribution and use in source and binary forms, with or without modification,
NYX 0:85b3fd62ea1a 106 * are permitted provided that the following conditions are met:
NYX 0:85b3fd62ea1a 107 * 1. Redistributions of source code must retain the above copyright notice,
NYX 0:85b3fd62ea1a 108 * this list of conditions and the following disclaimer.
NYX 0:85b3fd62ea1a 109 * 2. Redistributions in binary form must reproduce the above copyright notice,
NYX 0:85b3fd62ea1a 110 * this list of conditions and the following disclaimer in the documentation
NYX 0:85b3fd62ea1a 111 * and/or other materials provided with the distribution.
NYX 0:85b3fd62ea1a 112 * 3. Neither the name of STMicroelectronics nor the names of its contributors
NYX 0:85b3fd62ea1a 113 * may be used to endorse or promote products derived from this software
NYX 0:85b3fd62ea1a 114 * without specific prior written permission.
NYX 0:85b3fd62ea1a 115 *
NYX 0:85b3fd62ea1a 116 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
NYX 0:85b3fd62ea1a 117 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
NYX 0:85b3fd62ea1a 118 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NYX 0:85b3fd62ea1a 119 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
NYX 0:85b3fd62ea1a 120 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
NYX 0:85b3fd62ea1a 121 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
NYX 0:85b3fd62ea1a 122 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
NYX 0:85b3fd62ea1a 123 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
NYX 0:85b3fd62ea1a 124 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
NYX 0:85b3fd62ea1a 125 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NYX 0:85b3fd62ea1a 126 *
NYX 0:85b3fd62ea1a 127 ******************************************************************************
NYX 0:85b3fd62ea1a 128 */
NYX 0:85b3fd62ea1a 129
NYX 0:85b3fd62ea1a 130 /* Includes ------------------------------------------------------------------*/
NYX 0:85b3fd62ea1a 131 #include "stm32f4xx_hal.h"
NYX 0:85b3fd62ea1a 132
NYX 0:85b3fd62ea1a 133 /** @addtogroup STM32F4xx_HAL_Driver
NYX 0:85b3fd62ea1a 134 * @{
NYX 0:85b3fd62ea1a 135 */
NYX 0:85b3fd62ea1a 136
NYX 0:85b3fd62ea1a 137 /** @defgroup SMARTCARD SMARTCARD
NYX 0:85b3fd62ea1a 138 * @brief HAL USART SMARTCARD module driver
NYX 0:85b3fd62ea1a 139 * @{
NYX 0:85b3fd62ea1a 140 */
NYX 0:85b3fd62ea1a 141 #ifdef HAL_SMARTCARD_MODULE_ENABLED
NYX 0:85b3fd62ea1a 142 /* Private typedef -----------------------------------------------------------*/
NYX 0:85b3fd62ea1a 143 /* Private define ------------------------------------------------------------*/
NYX 0:85b3fd62ea1a 144 /** @addtogroup SMARTCARD_Private_Constants
NYX 0:85b3fd62ea1a 145 * @{
NYX 0:85b3fd62ea1a 146 */
NYX 0:85b3fd62ea1a 147 /**
NYX 0:85b3fd62ea1a 148 * @}
NYX 0:85b3fd62ea1a 149 */
NYX 0:85b3fd62ea1a 150 /* Private macro -------------------------------------------------------------*/
NYX 0:85b3fd62ea1a 151 /* Private variables ---------------------------------------------------------*/
NYX 0:85b3fd62ea1a 152 /* Private function prototypes -----------------------------------------------*/
NYX 0:85b3fd62ea1a 153 /** @addtogroup SMARTCARD_Private_Functions
NYX 0:85b3fd62ea1a 154 * @{
NYX 0:85b3fd62ea1a 155 */
NYX 0:85b3fd62ea1a 156 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc);
NYX 0:85b3fd62ea1a 157 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc);
NYX 0:85b3fd62ea1a 158 static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
NYX 0:85b3fd62ea1a 159 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
NYX 0:85b3fd62ea1a 160 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
NYX 0:85b3fd62ea1a 161 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
NYX 0:85b3fd62ea1a 162 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
NYX 0:85b3fd62ea1a 163 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
NYX 0:85b3fd62ea1a 164 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
NYX 0:85b3fd62ea1a 165 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
NYX 0:85b3fd62ea1a 166 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
NYX 0:85b3fd62ea1a 167 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
NYX 0:85b3fd62ea1a 168 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
NYX 0:85b3fd62ea1a 169 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
NYX 0:85b3fd62ea1a 170 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
NYX 0:85b3fd62ea1a 171 /**
NYX 0:85b3fd62ea1a 172 * @}
NYX 0:85b3fd62ea1a 173 */
NYX 0:85b3fd62ea1a 174 /* Exported functions --------------------------------------------------------*/
NYX 0:85b3fd62ea1a 175 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
NYX 0:85b3fd62ea1a 176 * @{
NYX 0:85b3fd62ea1a 177 */
NYX 0:85b3fd62ea1a 178
NYX 0:85b3fd62ea1a 179 /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
NYX 0:85b3fd62ea1a 180 * @brief Initialization and Configuration functions
NYX 0:85b3fd62ea1a 181 *
NYX 0:85b3fd62ea1a 182 @verbatim
NYX 0:85b3fd62ea1a 183 ==============================================================================
NYX 0:85b3fd62ea1a 184 ##### Initialization and Configuration functions #####
NYX 0:85b3fd62ea1a 185 ==============================================================================
NYX 0:85b3fd62ea1a 186 [..]
NYX 0:85b3fd62ea1a 187 This subsection provides a set of functions allowing to initialize the USART
NYX 0:85b3fd62ea1a 188 in Smartcard mode.
NYX 0:85b3fd62ea1a 189 [..]
NYX 0:85b3fd62ea1a 190 The Smartcard interface is designed to support asynchronous protocol Smartcards as
NYX 0:85b3fd62ea1a 191 defined in the ISO 7816-3 standard.
NYX 0:85b3fd62ea1a 192 [..]
NYX 0:85b3fd62ea1a 193 The USART can provide a clock to the smartcard through the SCLK output.
NYX 0:85b3fd62ea1a 194 In smartcard mode, SCLK is not associated to the communication but is simply derived
NYX 0:85b3fd62ea1a 195 from the internal peripheral input clock through a 5-bit prescaler.
NYX 0:85b3fd62ea1a 196 [..]
NYX 0:85b3fd62ea1a 197 (+) For the Smartcard mode only these parameters can be configured:
NYX 0:85b3fd62ea1a 198 (++) Baud Rate
NYX 0:85b3fd62ea1a 199 (++) Word Length => Should be 9 bits (8 bits + parity)
NYX 0:85b3fd62ea1a 200 (++) Stop Bit
NYX 0:85b3fd62ea1a 201 (++) Parity: => Should be enabled
NYX 0:85b3fd62ea1a 202 (++) USART polarity
NYX 0:85b3fd62ea1a 203 (++) USART phase
NYX 0:85b3fd62ea1a 204 (++) USART LastBit
NYX 0:85b3fd62ea1a 205 (++) Receiver/transmitter modes
NYX 0:85b3fd62ea1a 206 (++) Prescaler
NYX 0:85b3fd62ea1a 207 (++) GuardTime
NYX 0:85b3fd62ea1a 208 (++) NACKState: The Smartcard NACK state
NYX 0:85b3fd62ea1a 209
NYX 0:85b3fd62ea1a 210 (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
NYX 0:85b3fd62ea1a 211 (++) Word Length = 9 Bits
NYX 0:85b3fd62ea1a 212 (++) 1.5 Stop Bit
NYX 0:85b3fd62ea1a 213 (++) Even parity
NYX 0:85b3fd62ea1a 214 (++) BaudRate = 12096 baud
NYX 0:85b3fd62ea1a 215 (++) Tx and Rx enabled
NYX 0:85b3fd62ea1a 216 [..]
NYX 0:85b3fd62ea1a 217 Please refer to the ISO 7816-3 specification for more details.
NYX 0:85b3fd62ea1a 218
NYX 0:85b3fd62ea1a 219 [..]
NYX 0:85b3fd62ea1a 220 (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
NYX 0:85b3fd62ea1a 221 to use 1.5 stop bits for both transmitting and receiving to avoid switching
NYX 0:85b3fd62ea1a 222 between the two configurations.
NYX 0:85b3fd62ea1a 223 [..]
NYX 0:85b3fd62ea1a 224 The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
NYX 0:85b3fd62ea1a 225 procedure (details for the procedure are available in reference manual (RM0329)).
NYX 0:85b3fd62ea1a 226
NYX 0:85b3fd62ea1a 227 @endverbatim
NYX 0:85b3fd62ea1a 228
NYX 0:85b3fd62ea1a 229 The SMARTCARD frame format is given in the following table:
NYX 0:85b3fd62ea1a 230 +-------------------------------------------------------------+
NYX 0:85b3fd62ea1a 231 | M bit | PCE bit | SMARTCARD frame |
NYX 0:85b3fd62ea1a 232 |---------------------|---------------------------------------|
NYX 0:85b3fd62ea1a 233 | 1 | 1 | | SB | 8 bit data | PB | STB | |
NYX 0:85b3fd62ea1a 234 +-------------------------------------------------------------+
NYX 0:85b3fd62ea1a 235 * @{
NYX 0:85b3fd62ea1a 236 */
NYX 0:85b3fd62ea1a 237
NYX 0:85b3fd62ea1a 238 /**
NYX 0:85b3fd62ea1a 239 * @brief Initializes the SmartCard mode according to the specified
NYX 0:85b3fd62ea1a 240 * parameters in the SMARTCARD_InitTypeDef and create the associated handle .
NYX 0:85b3fd62ea1a 241 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 242 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 243 * @retval HAL status
NYX 0:85b3fd62ea1a 244 */
NYX 0:85b3fd62ea1a 245 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 246 {
NYX 0:85b3fd62ea1a 247 /* Check the SMARTCARD handle allocation */
NYX 0:85b3fd62ea1a 248 if(hsc == NULL)
NYX 0:85b3fd62ea1a 249 {
NYX 0:85b3fd62ea1a 250 return HAL_ERROR;
NYX 0:85b3fd62ea1a 251 }
NYX 0:85b3fd62ea1a 252
NYX 0:85b3fd62ea1a 253 /* Check the parameters */
NYX 0:85b3fd62ea1a 254 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
NYX 0:85b3fd62ea1a 255 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
NYX 0:85b3fd62ea1a 256
NYX 0:85b3fd62ea1a 257 if(hsc->gState == HAL_SMARTCARD_STATE_RESET)
NYX 0:85b3fd62ea1a 258 {
NYX 0:85b3fd62ea1a 259 /* Allocate lock resource and initialize it */
NYX 0:85b3fd62ea1a 260 hsc->Lock = HAL_UNLOCKED;
NYX 0:85b3fd62ea1a 261 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
NYX 0:85b3fd62ea1a 262 HAL_SMARTCARD_MspInit(hsc);
NYX 0:85b3fd62ea1a 263 }
NYX 0:85b3fd62ea1a 264
NYX 0:85b3fd62ea1a 265 hsc->gState = HAL_SMARTCARD_STATE_BUSY;
NYX 0:85b3fd62ea1a 266
NYX 0:85b3fd62ea1a 267 /* Set the Prescaler */
NYX 0:85b3fd62ea1a 268 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
NYX 0:85b3fd62ea1a 269
NYX 0:85b3fd62ea1a 270 /* Set the Guard Time */
NYX 0:85b3fd62ea1a 271 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8U));
NYX 0:85b3fd62ea1a 272
NYX 0:85b3fd62ea1a 273 /* Set the Smartcard Communication parameters */
NYX 0:85b3fd62ea1a 274 SMARTCARD_SetConfig(hsc);
NYX 0:85b3fd62ea1a 275
NYX 0:85b3fd62ea1a 276 /* In SmartCard mode, the following bits must be kept cleared:
NYX 0:85b3fd62ea1a 277 - LINEN bit in the USART_CR2 register
NYX 0:85b3fd62ea1a 278 - HDSEL and IREN bits in the USART_CR3 register.*/
NYX 0:85b3fd62ea1a 279 CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
NYX 0:85b3fd62ea1a 280 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
NYX 0:85b3fd62ea1a 281
NYX 0:85b3fd62ea1a 282 /* Enable the SMARTCARD Parity Error Interrupt */
NYX 0:85b3fd62ea1a 283 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
NYX 0:85b3fd62ea1a 284
NYX 0:85b3fd62ea1a 285 /* Enable the SMARTCARD Framing Error Interrupt */
NYX 0:85b3fd62ea1a 286 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 287
NYX 0:85b3fd62ea1a 288 /* Enable the Peripheral */
NYX 0:85b3fd62ea1a 289 __HAL_SMARTCARD_ENABLE(hsc);
NYX 0:85b3fd62ea1a 290
NYX 0:85b3fd62ea1a 291 /* Configure the Smartcard NACK state */
NYX 0:85b3fd62ea1a 292 MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
NYX 0:85b3fd62ea1a 293
NYX 0:85b3fd62ea1a 294 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
NYX 0:85b3fd62ea1a 295 hsc->Instance->CR3 |= (USART_CR3_SCEN);
NYX 0:85b3fd62ea1a 296
NYX 0:85b3fd62ea1a 297 /* Initialize the SMARTCARD state*/
NYX 0:85b3fd62ea1a 298 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 299 hsc->gState= HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 300 hsc->RxState= HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 301
NYX 0:85b3fd62ea1a 302 return HAL_OK;
NYX 0:85b3fd62ea1a 303 }
NYX 0:85b3fd62ea1a 304
NYX 0:85b3fd62ea1a 305 /**
NYX 0:85b3fd62ea1a 306 * @brief DeInitializes the USART SmartCard peripheral
NYX 0:85b3fd62ea1a 307 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 308 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 309 * @retval HAL status
NYX 0:85b3fd62ea1a 310 */
NYX 0:85b3fd62ea1a 311 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 312 {
NYX 0:85b3fd62ea1a 313 /* Check the SMARTCARD handle allocation */
NYX 0:85b3fd62ea1a 314 if(hsc == NULL)
NYX 0:85b3fd62ea1a 315 {
NYX 0:85b3fd62ea1a 316 return HAL_ERROR;
NYX 0:85b3fd62ea1a 317 }
NYX 0:85b3fd62ea1a 318
NYX 0:85b3fd62ea1a 319 /* Check the parameters */
NYX 0:85b3fd62ea1a 320 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
NYX 0:85b3fd62ea1a 321
NYX 0:85b3fd62ea1a 322 hsc->gState = HAL_SMARTCARD_STATE_BUSY;
NYX 0:85b3fd62ea1a 323
NYX 0:85b3fd62ea1a 324 /* Disable the Peripheral */
NYX 0:85b3fd62ea1a 325 __HAL_SMARTCARD_DISABLE(hsc);
NYX 0:85b3fd62ea1a 326
NYX 0:85b3fd62ea1a 327 /* DeInit the low level hardware */
NYX 0:85b3fd62ea1a 328 HAL_SMARTCARD_MspDeInit(hsc);
NYX 0:85b3fd62ea1a 329
NYX 0:85b3fd62ea1a 330 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 331 hsc->gState = HAL_SMARTCARD_STATE_RESET;
NYX 0:85b3fd62ea1a 332 hsc->RxState = HAL_SMARTCARD_STATE_RESET;
NYX 0:85b3fd62ea1a 333
NYX 0:85b3fd62ea1a 334 /* Release Lock */
NYX 0:85b3fd62ea1a 335 __HAL_UNLOCK(hsc);
NYX 0:85b3fd62ea1a 336
NYX 0:85b3fd62ea1a 337 return HAL_OK;
NYX 0:85b3fd62ea1a 338 }
NYX 0:85b3fd62ea1a 339
NYX 0:85b3fd62ea1a 340 /**
NYX 0:85b3fd62ea1a 341 * @brief SMARTCARD MSP Init
NYX 0:85b3fd62ea1a 342 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 343 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 344 * @retval None
NYX 0:85b3fd62ea1a 345 */
NYX 0:85b3fd62ea1a 346 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 347 {
NYX 0:85b3fd62ea1a 348 /* Prevent unused argument(s) compilation warning */
NYX 0:85b3fd62ea1a 349 UNUSED(hsc);
NYX 0:85b3fd62ea1a 350 /* NOTE : This function Should not be modified, when the callback is needed,
NYX 0:85b3fd62ea1a 351 the HAL_SMARTCARD_MspInit could be implemented in the user file
NYX 0:85b3fd62ea1a 352 */
NYX 0:85b3fd62ea1a 353 }
NYX 0:85b3fd62ea1a 354
NYX 0:85b3fd62ea1a 355 /**
NYX 0:85b3fd62ea1a 356 * @brief SMARTCARD MSP DeInit
NYX 0:85b3fd62ea1a 357 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 358 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 359 * @retval None
NYX 0:85b3fd62ea1a 360 */
NYX 0:85b3fd62ea1a 361 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 362 {
NYX 0:85b3fd62ea1a 363 /* Prevent unused argument(s) compilation warning */
NYX 0:85b3fd62ea1a 364 UNUSED(hsc);
NYX 0:85b3fd62ea1a 365 /* NOTE : This function Should not be modified, when the callback is needed,
NYX 0:85b3fd62ea1a 366 the HAL_SMARTCARD_MspDeInit could be implemented in the user file
NYX 0:85b3fd62ea1a 367 */
NYX 0:85b3fd62ea1a 368 }
NYX 0:85b3fd62ea1a 369
NYX 0:85b3fd62ea1a 370 /**
NYX 0:85b3fd62ea1a 371 * @}
NYX 0:85b3fd62ea1a 372 */
NYX 0:85b3fd62ea1a 373
NYX 0:85b3fd62ea1a 374 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
NYX 0:85b3fd62ea1a 375 * @brief SMARTCARD Transmit and Receive functions
NYX 0:85b3fd62ea1a 376 *
NYX 0:85b3fd62ea1a 377 @verbatim
NYX 0:85b3fd62ea1a 378 ===============================================================================
NYX 0:85b3fd62ea1a 379 ##### IO operation functions #####
NYX 0:85b3fd62ea1a 380 ===============================================================================
NYX 0:85b3fd62ea1a 381 [..] This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
NYX 0:85b3fd62ea1a 382
NYX 0:85b3fd62ea1a 383 [..]
NYX 0:85b3fd62ea1a 384 (#) Smartcard is a single wire half duplex communication protocol.
NYX 0:85b3fd62ea1a 385 The Smartcard interface is designed to support asynchronous protocol Smartcards as
NYX 0:85b3fd62ea1a 386 defined in the ISO 7816-3 standard.
NYX 0:85b3fd62ea1a 387 (#) The USART should be configured as:
NYX 0:85b3fd62ea1a 388 (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
NYX 0:85b3fd62ea1a 389 (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
NYX 0:85b3fd62ea1a 390
NYX 0:85b3fd62ea1a 391 (#) There are two modes of transfer:
NYX 0:85b3fd62ea1a 392 (++) Blocking mode: The communication is performed in polling mode.
NYX 0:85b3fd62ea1a 393 The HAL status of all data processing is returned by the same function
NYX 0:85b3fd62ea1a 394 after finishing transfer.
NYX 0:85b3fd62ea1a 395 (++) Non Blocking mode: The communication is performed using Interrupts
NYX 0:85b3fd62ea1a 396 or DMA, These APIs return the HAL status.
NYX 0:85b3fd62ea1a 397 The end of the data processing will be indicated through the
NYX 0:85b3fd62ea1a 398 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
NYX 0:85b3fd62ea1a 399 using DMA mode.
NYX 0:85b3fd62ea1a 400 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
NYX 0:85b3fd62ea1a 401 will be executed respectively at the end of the Transmit or Receive process
NYX 0:85b3fd62ea1a 402 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
NYX 0:85b3fd62ea1a 403
NYX 0:85b3fd62ea1a 404 (#) Blocking mode APIs are :
NYX 0:85b3fd62ea1a 405 (++) HAL_SMARTCARD_Transmit()
NYX 0:85b3fd62ea1a 406 (++) HAL_SMARTCARD_Receive()
NYX 0:85b3fd62ea1a 407
NYX 0:85b3fd62ea1a 408 (#) Non Blocking mode APIs with Interrupt are :
NYX 0:85b3fd62ea1a 409 (++) HAL_SMARTCARD_Transmit_IT()
NYX 0:85b3fd62ea1a 410 (++) HAL_SMARTCARD_Receive_IT()
NYX 0:85b3fd62ea1a 411 (++) HAL_SMARTCARD_IRQHandler()
NYX 0:85b3fd62ea1a 412
NYX 0:85b3fd62ea1a 413 (#) Non Blocking mode functions with DMA are :
NYX 0:85b3fd62ea1a 414 (++) HAL_SMARTCARD_Transmit_DMA()
NYX 0:85b3fd62ea1a 415 (++) HAL_SMARTCARD_Receive_DMA()
NYX 0:85b3fd62ea1a 416
NYX 0:85b3fd62ea1a 417 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
NYX 0:85b3fd62ea1a 418 (++) HAL_SMARTCARD_TxCpltCallback()
NYX 0:85b3fd62ea1a 419 (++) HAL_SMARTCARD_RxCpltCallback()
NYX 0:85b3fd62ea1a 420 (++) HAL_SMARTCARD_ErrorCallback()
NYX 0:85b3fd62ea1a 421
NYX 0:85b3fd62ea1a 422 @endverbatim
NYX 0:85b3fd62ea1a 423 * @{
NYX 0:85b3fd62ea1a 424 */
NYX 0:85b3fd62ea1a 425
NYX 0:85b3fd62ea1a 426 /**
NYX 0:85b3fd62ea1a 427 * @brief Send an amount of data in blocking mode
NYX 0:85b3fd62ea1a 428 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 429 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 430 * @param pData: pointer to data buffer
NYX 0:85b3fd62ea1a 431 * @param Size: amount of data to be sent
NYX 0:85b3fd62ea1a 432 * @param Timeout: Timeout duration
NYX 0:85b3fd62ea1a 433 * @retval HAL status
NYX 0:85b3fd62ea1a 434 */
NYX 0:85b3fd62ea1a 435 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
NYX 0:85b3fd62ea1a 436 {
NYX 0:85b3fd62ea1a 437 uint16_t* tmp;
NYX 0:85b3fd62ea1a 438 uint32_t tickstart = 0U;
NYX 0:85b3fd62ea1a 439
NYX 0:85b3fd62ea1a 440 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
NYX 0:85b3fd62ea1a 441 {
NYX 0:85b3fd62ea1a 442 if((pData == NULL) || (Size == 0))
NYX 0:85b3fd62ea1a 443 {
NYX 0:85b3fd62ea1a 444 return HAL_ERROR;
NYX 0:85b3fd62ea1a 445 }
NYX 0:85b3fd62ea1a 446
NYX 0:85b3fd62ea1a 447 /* Process Locked */
NYX 0:85b3fd62ea1a 448 __HAL_LOCK(hsc);
NYX 0:85b3fd62ea1a 449
NYX 0:85b3fd62ea1a 450 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 451 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
NYX 0:85b3fd62ea1a 452
NYX 0:85b3fd62ea1a 453 /* Init tickstart for timeout managment */
NYX 0:85b3fd62ea1a 454 tickstart = HAL_GetTick();
NYX 0:85b3fd62ea1a 455
NYX 0:85b3fd62ea1a 456 hsc->TxXferSize = Size;
NYX 0:85b3fd62ea1a 457 hsc->TxXferCount = Size;
NYX 0:85b3fd62ea1a 458 while(hsc->TxXferCount > 0U)
NYX 0:85b3fd62ea1a 459 {
NYX 0:85b3fd62ea1a 460 hsc->TxXferCount--;
NYX 0:85b3fd62ea1a 461 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
NYX 0:85b3fd62ea1a 462 {
NYX 0:85b3fd62ea1a 463 return HAL_TIMEOUT;
NYX 0:85b3fd62ea1a 464 }
NYX 0:85b3fd62ea1a 465 tmp = (uint16_t*) pData;
NYX 0:85b3fd62ea1a 466 hsc->Instance->DR = (*tmp & (uint16_t)0x01FF);
NYX 0:85b3fd62ea1a 467 pData +=1U;
NYX 0:85b3fd62ea1a 468 }
NYX 0:85b3fd62ea1a 469
NYX 0:85b3fd62ea1a 470 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
NYX 0:85b3fd62ea1a 471 {
NYX 0:85b3fd62ea1a 472 return HAL_TIMEOUT;
NYX 0:85b3fd62ea1a 473 }
NYX 0:85b3fd62ea1a 474
NYX 0:85b3fd62ea1a 475 /* At end of Tx process, restore hsc->gState to Ready */
NYX 0:85b3fd62ea1a 476 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 477
NYX 0:85b3fd62ea1a 478 /* Process Unlocked */
NYX 0:85b3fd62ea1a 479 __HAL_UNLOCK(hsc);
NYX 0:85b3fd62ea1a 480
NYX 0:85b3fd62ea1a 481 return HAL_OK;
NYX 0:85b3fd62ea1a 482 }
NYX 0:85b3fd62ea1a 483 else
NYX 0:85b3fd62ea1a 484 {
NYX 0:85b3fd62ea1a 485 return HAL_BUSY;
NYX 0:85b3fd62ea1a 486 }
NYX 0:85b3fd62ea1a 487 }
NYX 0:85b3fd62ea1a 488
NYX 0:85b3fd62ea1a 489 /**
NYX 0:85b3fd62ea1a 490 * @brief Receive an amount of data in blocking mode
NYX 0:85b3fd62ea1a 491 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 492 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 493 * @param pData: pointer to data buffer
NYX 0:85b3fd62ea1a 494 * @param Size: amount of data to be received
NYX 0:85b3fd62ea1a 495 * @param Timeout: Timeout duration
NYX 0:85b3fd62ea1a 496 * @retval HAL status
NYX 0:85b3fd62ea1a 497 */
NYX 0:85b3fd62ea1a 498 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
NYX 0:85b3fd62ea1a 499 {
NYX 0:85b3fd62ea1a 500 uint16_t* tmp;
NYX 0:85b3fd62ea1a 501 uint32_t tickstart = 0U;
NYX 0:85b3fd62ea1a 502
NYX 0:85b3fd62ea1a 503 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
NYX 0:85b3fd62ea1a 504 {
NYX 0:85b3fd62ea1a 505 if((pData == NULL) || (Size == 0))
NYX 0:85b3fd62ea1a 506 {
NYX 0:85b3fd62ea1a 507 return HAL_ERROR;
NYX 0:85b3fd62ea1a 508 }
NYX 0:85b3fd62ea1a 509
NYX 0:85b3fd62ea1a 510 /* Process Locked */
NYX 0:85b3fd62ea1a 511 __HAL_LOCK(hsc);
NYX 0:85b3fd62ea1a 512
NYX 0:85b3fd62ea1a 513 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 514 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
NYX 0:85b3fd62ea1a 515
NYX 0:85b3fd62ea1a 516 /* Init tickstart for timeout managment */
NYX 0:85b3fd62ea1a 517 tickstart = HAL_GetTick();
NYX 0:85b3fd62ea1a 518
NYX 0:85b3fd62ea1a 519 hsc->RxXferSize = Size;
NYX 0:85b3fd62ea1a 520 hsc->RxXferCount = Size;
NYX 0:85b3fd62ea1a 521
NYX 0:85b3fd62ea1a 522 /* Check the remain data to be received */
NYX 0:85b3fd62ea1a 523 while(hsc->RxXferCount > 0U)
NYX 0:85b3fd62ea1a 524 {
NYX 0:85b3fd62ea1a 525 hsc->RxXferCount--;
NYX 0:85b3fd62ea1a 526 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
NYX 0:85b3fd62ea1a 527 {
NYX 0:85b3fd62ea1a 528 return HAL_TIMEOUT;
NYX 0:85b3fd62ea1a 529 }
NYX 0:85b3fd62ea1a 530 tmp = (uint16_t*) pData;
NYX 0:85b3fd62ea1a 531 *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFF);
NYX 0:85b3fd62ea1a 532 pData +=1U;
NYX 0:85b3fd62ea1a 533 }
NYX 0:85b3fd62ea1a 534
NYX 0:85b3fd62ea1a 535 /* At end of Rx process, restore hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 536 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 537
NYX 0:85b3fd62ea1a 538 /* Process Unlocked */
NYX 0:85b3fd62ea1a 539 __HAL_UNLOCK(hsc);
NYX 0:85b3fd62ea1a 540
NYX 0:85b3fd62ea1a 541 return HAL_OK;
NYX 0:85b3fd62ea1a 542 }
NYX 0:85b3fd62ea1a 543 else
NYX 0:85b3fd62ea1a 544 {
NYX 0:85b3fd62ea1a 545 return HAL_BUSY;
NYX 0:85b3fd62ea1a 546 }
NYX 0:85b3fd62ea1a 547 }
NYX 0:85b3fd62ea1a 548
NYX 0:85b3fd62ea1a 549 /**
NYX 0:85b3fd62ea1a 550 * @brief Send an amount of data in non blocking mode
NYX 0:85b3fd62ea1a 551 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 552 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 553 * @param pData: pointer to data buffer
NYX 0:85b3fd62ea1a 554 * @param Size: amount of data to be sent
NYX 0:85b3fd62ea1a 555 * @retval HAL status
NYX 0:85b3fd62ea1a 556 */
NYX 0:85b3fd62ea1a 557 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
NYX 0:85b3fd62ea1a 558 {
NYX 0:85b3fd62ea1a 559 /* Check that a Tx process is not already ongoing */
NYX 0:85b3fd62ea1a 560 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
NYX 0:85b3fd62ea1a 561 {
NYX 0:85b3fd62ea1a 562 if((pData == NULL) || (Size == 0))
NYX 0:85b3fd62ea1a 563 {
NYX 0:85b3fd62ea1a 564 return HAL_ERROR;
NYX 0:85b3fd62ea1a 565 }
NYX 0:85b3fd62ea1a 566
NYX 0:85b3fd62ea1a 567 /* Process Locked */
NYX 0:85b3fd62ea1a 568 __HAL_LOCK(hsc);
NYX 0:85b3fd62ea1a 569
NYX 0:85b3fd62ea1a 570 hsc->pTxBuffPtr = pData;
NYX 0:85b3fd62ea1a 571 hsc->TxXferSize = Size;
NYX 0:85b3fd62ea1a 572 hsc->TxXferCount = Size;
NYX 0:85b3fd62ea1a 573
NYX 0:85b3fd62ea1a 574 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 575 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
NYX 0:85b3fd62ea1a 576
NYX 0:85b3fd62ea1a 577 /* Process Unlocked */
NYX 0:85b3fd62ea1a 578 __HAL_UNLOCK(hsc);
NYX 0:85b3fd62ea1a 579
NYX 0:85b3fd62ea1a 580 /* Enable the SMARTCARD Parity Error Interrupt */
NYX 0:85b3fd62ea1a 581 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
NYX 0:85b3fd62ea1a 582
NYX 0:85b3fd62ea1a 583 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
NYX 0:85b3fd62ea1a 584 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 585
NYX 0:85b3fd62ea1a 586 /* Enable the SMARTCARD Transmit data register empty Interrupt */
NYX 0:85b3fd62ea1a 587 SET_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
NYX 0:85b3fd62ea1a 588
NYX 0:85b3fd62ea1a 589 return HAL_OK;
NYX 0:85b3fd62ea1a 590 }
NYX 0:85b3fd62ea1a 591 else
NYX 0:85b3fd62ea1a 592 {
NYX 0:85b3fd62ea1a 593 return HAL_BUSY;
NYX 0:85b3fd62ea1a 594 }
NYX 0:85b3fd62ea1a 595 }
NYX 0:85b3fd62ea1a 596
NYX 0:85b3fd62ea1a 597 /**
NYX 0:85b3fd62ea1a 598 * @brief Receive an amount of data in non blocking mode
NYX 0:85b3fd62ea1a 599 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 600 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 601 * @param pData: pointer to data buffer
NYX 0:85b3fd62ea1a 602 * @param Size: amount of data to be received
NYX 0:85b3fd62ea1a 603 * @retval HAL status
NYX 0:85b3fd62ea1a 604 */
NYX 0:85b3fd62ea1a 605 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
NYX 0:85b3fd62ea1a 606 {
NYX 0:85b3fd62ea1a 607 /* Check that a Rx process is not already ongoing */
NYX 0:85b3fd62ea1a 608 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
NYX 0:85b3fd62ea1a 609 {
NYX 0:85b3fd62ea1a 610 if((pData == NULL) || (Size == 0))
NYX 0:85b3fd62ea1a 611 {
NYX 0:85b3fd62ea1a 612 return HAL_ERROR;
NYX 0:85b3fd62ea1a 613 }
NYX 0:85b3fd62ea1a 614
NYX 0:85b3fd62ea1a 615 /* Process Locked */
NYX 0:85b3fd62ea1a 616 __HAL_LOCK(hsc);
NYX 0:85b3fd62ea1a 617
NYX 0:85b3fd62ea1a 618 hsc->pRxBuffPtr = pData;
NYX 0:85b3fd62ea1a 619 hsc->RxXferSize = Size;
NYX 0:85b3fd62ea1a 620 hsc->RxXferCount = Size;
NYX 0:85b3fd62ea1a 621
NYX 0:85b3fd62ea1a 622 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 623 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
NYX 0:85b3fd62ea1a 624
NYX 0:85b3fd62ea1a 625 /* Process Unlocked */
NYX 0:85b3fd62ea1a 626 __HAL_UNLOCK(hsc);
NYX 0:85b3fd62ea1a 627
NYX 0:85b3fd62ea1a 628 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
NYX 0:85b3fd62ea1a 629 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
NYX 0:85b3fd62ea1a 630
NYX 0:85b3fd62ea1a 631 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
NYX 0:85b3fd62ea1a 632 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 633
NYX 0:85b3fd62ea1a 634 return HAL_OK;
NYX 0:85b3fd62ea1a 635 }
NYX 0:85b3fd62ea1a 636 else
NYX 0:85b3fd62ea1a 637 {
NYX 0:85b3fd62ea1a 638 return HAL_BUSY;
NYX 0:85b3fd62ea1a 639 }
NYX 0:85b3fd62ea1a 640 }
NYX 0:85b3fd62ea1a 641
NYX 0:85b3fd62ea1a 642 /**
NYX 0:85b3fd62ea1a 643 * @brief Send an amount of data in non blocking mode
NYX 0:85b3fd62ea1a 644 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 645 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 646 * @param pData: pointer to data buffer
NYX 0:85b3fd62ea1a 647 * @param Size: amount of data to be sent
NYX 0:85b3fd62ea1a 648 * @retval HAL status
NYX 0:85b3fd62ea1a 649 */
NYX 0:85b3fd62ea1a 650 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
NYX 0:85b3fd62ea1a 651 {
NYX 0:85b3fd62ea1a 652 uint32_t *tmp;
NYX 0:85b3fd62ea1a 653
NYX 0:85b3fd62ea1a 654 /* Check that a Tx process is not already ongoing */
NYX 0:85b3fd62ea1a 655 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
NYX 0:85b3fd62ea1a 656 {
NYX 0:85b3fd62ea1a 657 if((pData == NULL) || (Size == 0))
NYX 0:85b3fd62ea1a 658 {
NYX 0:85b3fd62ea1a 659 return HAL_ERROR;
NYX 0:85b3fd62ea1a 660 }
NYX 0:85b3fd62ea1a 661
NYX 0:85b3fd62ea1a 662 /* Process Locked */
NYX 0:85b3fd62ea1a 663 __HAL_LOCK(hsc);
NYX 0:85b3fd62ea1a 664
NYX 0:85b3fd62ea1a 665 hsc->pTxBuffPtr = pData;
NYX 0:85b3fd62ea1a 666 hsc->TxXferSize = Size;
NYX 0:85b3fd62ea1a 667 hsc->TxXferCount = Size;
NYX 0:85b3fd62ea1a 668
NYX 0:85b3fd62ea1a 669 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 670 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
NYX 0:85b3fd62ea1a 671
NYX 0:85b3fd62ea1a 672 /* Set the SMARTCARD DMA transfer complete callback */
NYX 0:85b3fd62ea1a 673 hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
NYX 0:85b3fd62ea1a 674
NYX 0:85b3fd62ea1a 675 /* Set the DMA error callback */
NYX 0:85b3fd62ea1a 676 hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
NYX 0:85b3fd62ea1a 677
NYX 0:85b3fd62ea1a 678 /* Set the DMA abort callback */
NYX 0:85b3fd62ea1a 679 hsc->hdmatx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 680
NYX 0:85b3fd62ea1a 681 /* Enable the SMARTCARD transmit DMA Stream */
NYX 0:85b3fd62ea1a 682 tmp = (uint32_t*)&pData;
NYX 0:85b3fd62ea1a 683 HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
NYX 0:85b3fd62ea1a 684
NYX 0:85b3fd62ea1a 685 /* Clear the TC flag in the SR register by writing 0 to it */
NYX 0:85b3fd62ea1a 686 __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
NYX 0:85b3fd62ea1a 687
NYX 0:85b3fd62ea1a 688 /* Process Unlocked */
NYX 0:85b3fd62ea1a 689 __HAL_UNLOCK(hsc);
NYX 0:85b3fd62ea1a 690
NYX 0:85b3fd62ea1a 691 /* Enable the DMA transfer for transmit request by setting the DMAT bit
NYX 0:85b3fd62ea1a 692 in the SMARTCARD CR3 register */
NYX 0:85b3fd62ea1a 693 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
NYX 0:85b3fd62ea1a 694
NYX 0:85b3fd62ea1a 695 return HAL_OK;
NYX 0:85b3fd62ea1a 696 }
NYX 0:85b3fd62ea1a 697 else
NYX 0:85b3fd62ea1a 698 {
NYX 0:85b3fd62ea1a 699 return HAL_BUSY;
NYX 0:85b3fd62ea1a 700 }
NYX 0:85b3fd62ea1a 701 }
NYX 0:85b3fd62ea1a 702
NYX 0:85b3fd62ea1a 703 /**
NYX 0:85b3fd62ea1a 704 * @brief Receive an amount of data in non blocking mode
NYX 0:85b3fd62ea1a 705 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 706 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 707 * @param pData: pointer to data buffer
NYX 0:85b3fd62ea1a 708 * @param Size: amount of data to be received
NYX 0:85b3fd62ea1a 709 * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
NYX 0:85b3fd62ea1a 710 * @retval HAL status
NYX 0:85b3fd62ea1a 711 */
NYX 0:85b3fd62ea1a 712 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
NYX 0:85b3fd62ea1a 713 {
NYX 0:85b3fd62ea1a 714 uint32_t *tmp;
NYX 0:85b3fd62ea1a 715
NYX 0:85b3fd62ea1a 716 /* Check that a Rx process is not already ongoing */
NYX 0:85b3fd62ea1a 717 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
NYX 0:85b3fd62ea1a 718 {
NYX 0:85b3fd62ea1a 719 if((pData == NULL) || (Size == 0))
NYX 0:85b3fd62ea1a 720 {
NYX 0:85b3fd62ea1a 721 return HAL_ERROR;
NYX 0:85b3fd62ea1a 722 }
NYX 0:85b3fd62ea1a 723
NYX 0:85b3fd62ea1a 724 /* Process Locked */
NYX 0:85b3fd62ea1a 725 __HAL_LOCK(hsc);
NYX 0:85b3fd62ea1a 726
NYX 0:85b3fd62ea1a 727 hsc->pRxBuffPtr = pData;
NYX 0:85b3fd62ea1a 728 hsc->RxXferSize = Size;
NYX 0:85b3fd62ea1a 729
NYX 0:85b3fd62ea1a 730 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 731 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
NYX 0:85b3fd62ea1a 732
NYX 0:85b3fd62ea1a 733 /* Set the SMARTCARD DMA transfer complete callback */
NYX 0:85b3fd62ea1a 734 hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
NYX 0:85b3fd62ea1a 735
NYX 0:85b3fd62ea1a 736 /* Set the DMA error callback */
NYX 0:85b3fd62ea1a 737 hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
NYX 0:85b3fd62ea1a 738
NYX 0:85b3fd62ea1a 739 /* Set the DMA abort callback */
NYX 0:85b3fd62ea1a 740 hsc->hdmatx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 741
NYX 0:85b3fd62ea1a 742 /* Enable the DMA Stream */
NYX 0:85b3fd62ea1a 743 tmp = (uint32_t*)&pData;
NYX 0:85b3fd62ea1a 744 HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
NYX 0:85b3fd62ea1a 745
NYX 0:85b3fd62ea1a 746 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
NYX 0:85b3fd62ea1a 747 __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
NYX 0:85b3fd62ea1a 748
NYX 0:85b3fd62ea1a 749 /* Process Unlocked */
NYX 0:85b3fd62ea1a 750 __HAL_UNLOCK(hsc);
NYX 0:85b3fd62ea1a 751
NYX 0:85b3fd62ea1a 752 /* Enable the SMARTCARD Parity Error Interrupt */
NYX 0:85b3fd62ea1a 753 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
NYX 0:85b3fd62ea1a 754
NYX 0:85b3fd62ea1a 755 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
NYX 0:85b3fd62ea1a 756 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 757
NYX 0:85b3fd62ea1a 758 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
NYX 0:85b3fd62ea1a 759 in the SMARTCARD CR3 register */
NYX 0:85b3fd62ea1a 760 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
NYX 0:85b3fd62ea1a 761
NYX 0:85b3fd62ea1a 762 return HAL_OK;
NYX 0:85b3fd62ea1a 763 }
NYX 0:85b3fd62ea1a 764 else
NYX 0:85b3fd62ea1a 765 {
NYX 0:85b3fd62ea1a 766 return HAL_BUSY;
NYX 0:85b3fd62ea1a 767 }
NYX 0:85b3fd62ea1a 768 }
NYX 0:85b3fd62ea1a 769
NYX 0:85b3fd62ea1a 770 /**
NYX 0:85b3fd62ea1a 771 * @brief Abort ongoing transfers (blocking mode).
NYX 0:85b3fd62ea1a 772 * @param hsc SMARTCARD handle.
NYX 0:85b3fd62ea1a 773 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
NYX 0:85b3fd62ea1a 774 * This procedure performs following operations :
NYX 0:85b3fd62ea1a 775 * - Disable PPP Interrupts
NYX 0:85b3fd62ea1a 776 * - Disable the DMA transfer in the peripheral register (if enabled)
NYX 0:85b3fd62ea1a 777 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
NYX 0:85b3fd62ea1a 778 * - Set handle State to READY
NYX 0:85b3fd62ea1a 779 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
NYX 0:85b3fd62ea1a 780 * @retval HAL status
NYX 0:85b3fd62ea1a 781 */
NYX 0:85b3fd62ea1a 782 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 783 {
NYX 0:85b3fd62ea1a 784 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
NYX 0:85b3fd62ea1a 785 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
NYX 0:85b3fd62ea1a 786 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 787
NYX 0:85b3fd62ea1a 788 /* Disable the SMARTCARD DMA Tx request if enabled */
NYX 0:85b3fd62ea1a 789 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
NYX 0:85b3fd62ea1a 790 {
NYX 0:85b3fd62ea1a 791 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
NYX 0:85b3fd62ea1a 792
NYX 0:85b3fd62ea1a 793 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
NYX 0:85b3fd62ea1a 794 if(hsc->hdmatx != NULL)
NYX 0:85b3fd62ea1a 795 {
NYX 0:85b3fd62ea1a 796 /* Set the SMARTCARD DMA Abort callback to Null.
NYX 0:85b3fd62ea1a 797 No call back execution at end of DMA abort procedure */
NYX 0:85b3fd62ea1a 798 hsc->hdmatx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 799
NYX 0:85b3fd62ea1a 800 HAL_DMA_Abort(hsc->hdmatx);
NYX 0:85b3fd62ea1a 801 }
NYX 0:85b3fd62ea1a 802 }
NYX 0:85b3fd62ea1a 803
NYX 0:85b3fd62ea1a 804 /* Disable the SMARTCARD DMA Rx request if enabled */
NYX 0:85b3fd62ea1a 805 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
NYX 0:85b3fd62ea1a 806 {
NYX 0:85b3fd62ea1a 807 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
NYX 0:85b3fd62ea1a 808
NYX 0:85b3fd62ea1a 809 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
NYX 0:85b3fd62ea1a 810 if(hsc->hdmarx != NULL)
NYX 0:85b3fd62ea1a 811 {
NYX 0:85b3fd62ea1a 812 /* Set the SMARTCARD DMA Abort callback to Null.
NYX 0:85b3fd62ea1a 813 No call back execution at end of DMA abort procedure */
NYX 0:85b3fd62ea1a 814 hsc->hdmarx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 815
NYX 0:85b3fd62ea1a 816 HAL_DMA_Abort(hsc->hdmarx);
NYX 0:85b3fd62ea1a 817 }
NYX 0:85b3fd62ea1a 818 }
NYX 0:85b3fd62ea1a 819
NYX 0:85b3fd62ea1a 820 /* Reset Tx and Rx transfer counters */
NYX 0:85b3fd62ea1a 821 hsc->TxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 822 hsc->RxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 823
NYX 0:85b3fd62ea1a 824 /* Reset ErrorCode */
NYX 0:85b3fd62ea1a 825 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 826
NYX 0:85b3fd62ea1a 827 /* Restore hsc->RxState and hsc->gState to Ready */
NYX 0:85b3fd62ea1a 828 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 829 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 830
NYX 0:85b3fd62ea1a 831 return HAL_OK;
NYX 0:85b3fd62ea1a 832 }
NYX 0:85b3fd62ea1a 833
NYX 0:85b3fd62ea1a 834 /**
NYX 0:85b3fd62ea1a 835 * @brief Abort ongoing Transmit transfer (blocking mode).
NYX 0:85b3fd62ea1a 836 * @param hsc SMARTCARD handle.
NYX 0:85b3fd62ea1a 837 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
NYX 0:85b3fd62ea1a 838 * This procedure performs following operations :
NYX 0:85b3fd62ea1a 839 * - Disable PPP Interrupts
NYX 0:85b3fd62ea1a 840 * - Disable the DMA transfer in the peripheral register (if enabled)
NYX 0:85b3fd62ea1a 841 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
NYX 0:85b3fd62ea1a 842 * - Set handle State to READY
NYX 0:85b3fd62ea1a 843 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
NYX 0:85b3fd62ea1a 844 * @retval HAL status
NYX 0:85b3fd62ea1a 845 */
NYX 0:85b3fd62ea1a 846 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 847 {
NYX 0:85b3fd62ea1a 848 /* Disable TXEIE and TCIE interrupts */
NYX 0:85b3fd62ea1a 849 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
NYX 0:85b3fd62ea1a 850
NYX 0:85b3fd62ea1a 851 /* Disable the SMARTCARD DMA Tx request if enabled */
NYX 0:85b3fd62ea1a 852 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
NYX 0:85b3fd62ea1a 853 {
NYX 0:85b3fd62ea1a 854 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
NYX 0:85b3fd62ea1a 855
NYX 0:85b3fd62ea1a 856 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
NYX 0:85b3fd62ea1a 857 if(hsc->hdmatx != NULL)
NYX 0:85b3fd62ea1a 858 {
NYX 0:85b3fd62ea1a 859 /* Set the SMARTCARD DMA Abort callback to Null.
NYX 0:85b3fd62ea1a 860 No call back execution at end of DMA abort procedure */
NYX 0:85b3fd62ea1a 861 hsc->hdmatx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 862
NYX 0:85b3fd62ea1a 863 HAL_DMA_Abort(hsc->hdmatx);
NYX 0:85b3fd62ea1a 864 }
NYX 0:85b3fd62ea1a 865 }
NYX 0:85b3fd62ea1a 866
NYX 0:85b3fd62ea1a 867 /* Reset Tx transfer counter */
NYX 0:85b3fd62ea1a 868 hsc->TxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 869
NYX 0:85b3fd62ea1a 870 /* Restore hsc->gState to Ready */
NYX 0:85b3fd62ea1a 871 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 872
NYX 0:85b3fd62ea1a 873 return HAL_OK;
NYX 0:85b3fd62ea1a 874 }
NYX 0:85b3fd62ea1a 875
NYX 0:85b3fd62ea1a 876 /**
NYX 0:85b3fd62ea1a 877 * @brief Abort ongoing Receive transfer (blocking mode).
NYX 0:85b3fd62ea1a 878 * @param hsc SMARTCARD handle.
NYX 0:85b3fd62ea1a 879 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
NYX 0:85b3fd62ea1a 880 * This procedure performs following operations :
NYX 0:85b3fd62ea1a 881 * - Disable PPP Interrupts
NYX 0:85b3fd62ea1a 882 * - Disable the DMA transfer in the peripheral register (if enabled)
NYX 0:85b3fd62ea1a 883 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
NYX 0:85b3fd62ea1a 884 * - Set handle State to READY
NYX 0:85b3fd62ea1a 885 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
NYX 0:85b3fd62ea1a 886 * @retval HAL status
NYX 0:85b3fd62ea1a 887 */
NYX 0:85b3fd62ea1a 888 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 889 {
NYX 0:85b3fd62ea1a 890 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
NYX 0:85b3fd62ea1a 891 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
NYX 0:85b3fd62ea1a 892 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 893
NYX 0:85b3fd62ea1a 894 /* Disable the SMARTCARD DMA Rx request if enabled */
NYX 0:85b3fd62ea1a 895 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
NYX 0:85b3fd62ea1a 896 {
NYX 0:85b3fd62ea1a 897 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
NYX 0:85b3fd62ea1a 898
NYX 0:85b3fd62ea1a 899 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
NYX 0:85b3fd62ea1a 900 if(hsc->hdmarx != NULL)
NYX 0:85b3fd62ea1a 901 {
NYX 0:85b3fd62ea1a 902 /* Set the SMARTCARD DMA Abort callback to Null.
NYX 0:85b3fd62ea1a 903 No call back execution at end of DMA abort procedure */
NYX 0:85b3fd62ea1a 904 hsc->hdmarx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 905
NYX 0:85b3fd62ea1a 906 HAL_DMA_Abort(hsc->hdmarx);
NYX 0:85b3fd62ea1a 907 }
NYX 0:85b3fd62ea1a 908 }
NYX 0:85b3fd62ea1a 909
NYX 0:85b3fd62ea1a 910 /* Reset Rx transfer counter */
NYX 0:85b3fd62ea1a 911 hsc->RxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 912
NYX 0:85b3fd62ea1a 913 /* Restore hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 914 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 915
NYX 0:85b3fd62ea1a 916 return HAL_OK;
NYX 0:85b3fd62ea1a 917 }
NYX 0:85b3fd62ea1a 918
NYX 0:85b3fd62ea1a 919 /**
NYX 0:85b3fd62ea1a 920 * @brief Abort ongoing transfers (Interrupt mode).
NYX 0:85b3fd62ea1a 921 * @param hsc SMARTCARD handle.
NYX 0:85b3fd62ea1a 922 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
NYX 0:85b3fd62ea1a 923 * This procedure performs following operations :
NYX 0:85b3fd62ea1a 924 * - Disable PPP Interrupts
NYX 0:85b3fd62ea1a 925 * - Disable the DMA transfer in the peripheral register (if enabled)
NYX 0:85b3fd62ea1a 926 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
NYX 0:85b3fd62ea1a 927 * - Set handle State to READY
NYX 0:85b3fd62ea1a 928 * - At abort completion, call user abort complete callback
NYX 0:85b3fd62ea1a 929 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
NYX 0:85b3fd62ea1a 930 * considered as completed only when user abort complete callback is executed (not when exiting function).
NYX 0:85b3fd62ea1a 931 * @retval HAL status
NYX 0:85b3fd62ea1a 932 */
NYX 0:85b3fd62ea1a 933 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 934 {
NYX 0:85b3fd62ea1a 935 uint32_t AbortCplt = 0x01U;
NYX 0:85b3fd62ea1a 936
NYX 0:85b3fd62ea1a 937 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
NYX 0:85b3fd62ea1a 938 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
NYX 0:85b3fd62ea1a 939 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 940
NYX 0:85b3fd62ea1a 941 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
NYX 0:85b3fd62ea1a 942 before any call to DMA Abort functions */
NYX 0:85b3fd62ea1a 943 /* DMA Tx Handle is valid */
NYX 0:85b3fd62ea1a 944 if(hsc->hdmatx != NULL)
NYX 0:85b3fd62ea1a 945 {
NYX 0:85b3fd62ea1a 946 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
NYX 0:85b3fd62ea1a 947 Otherwise, set it to NULL */
NYX 0:85b3fd62ea1a 948 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
NYX 0:85b3fd62ea1a 949 {
NYX 0:85b3fd62ea1a 950 hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
NYX 0:85b3fd62ea1a 951 }
NYX 0:85b3fd62ea1a 952 else
NYX 0:85b3fd62ea1a 953 {
NYX 0:85b3fd62ea1a 954 hsc->hdmatx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 955 }
NYX 0:85b3fd62ea1a 956 }
NYX 0:85b3fd62ea1a 957 /* DMA Rx Handle is valid */
NYX 0:85b3fd62ea1a 958 if(hsc->hdmarx != NULL)
NYX 0:85b3fd62ea1a 959 {
NYX 0:85b3fd62ea1a 960 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
NYX 0:85b3fd62ea1a 961 Otherwise, set it to NULL */
NYX 0:85b3fd62ea1a 962 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
NYX 0:85b3fd62ea1a 963 {
NYX 0:85b3fd62ea1a 964 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
NYX 0:85b3fd62ea1a 965 }
NYX 0:85b3fd62ea1a 966 else
NYX 0:85b3fd62ea1a 967 {
NYX 0:85b3fd62ea1a 968 hsc->hdmarx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 969 }
NYX 0:85b3fd62ea1a 970 }
NYX 0:85b3fd62ea1a 971
NYX 0:85b3fd62ea1a 972 /* Disable the SMARTCARD DMA Tx request if enabled */
NYX 0:85b3fd62ea1a 973 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
NYX 0:85b3fd62ea1a 974 {
NYX 0:85b3fd62ea1a 975 /* Disable DMA Tx at SMARTCARD level */
NYX 0:85b3fd62ea1a 976 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
NYX 0:85b3fd62ea1a 977
NYX 0:85b3fd62ea1a 978 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
NYX 0:85b3fd62ea1a 979 if(hsc->hdmatx != NULL)
NYX 0:85b3fd62ea1a 980 {
NYX 0:85b3fd62ea1a 981 /* SMARTCARD Tx DMA Abort callback has already been initialised :
NYX 0:85b3fd62ea1a 982 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
NYX 0:85b3fd62ea1a 983
NYX 0:85b3fd62ea1a 984 /* Abort DMA TX */
NYX 0:85b3fd62ea1a 985 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
NYX 0:85b3fd62ea1a 986 {
NYX 0:85b3fd62ea1a 987 hsc->hdmatx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 988 }
NYX 0:85b3fd62ea1a 989 else
NYX 0:85b3fd62ea1a 990 {
NYX 0:85b3fd62ea1a 991 AbortCplt = 0x00U;
NYX 0:85b3fd62ea1a 992 }
NYX 0:85b3fd62ea1a 993 }
NYX 0:85b3fd62ea1a 994 }
NYX 0:85b3fd62ea1a 995
NYX 0:85b3fd62ea1a 996 /* Disable the SMARTCARD DMA Rx request if enabled */
NYX 0:85b3fd62ea1a 997 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
NYX 0:85b3fd62ea1a 998 {
NYX 0:85b3fd62ea1a 999 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
NYX 0:85b3fd62ea1a 1000
NYX 0:85b3fd62ea1a 1001 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
NYX 0:85b3fd62ea1a 1002 if(hsc->hdmarx != NULL)
NYX 0:85b3fd62ea1a 1003 {
NYX 0:85b3fd62ea1a 1004 /* SMARTCARD Rx DMA Abort callback has already been initialised :
NYX 0:85b3fd62ea1a 1005 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
NYX 0:85b3fd62ea1a 1006
NYX 0:85b3fd62ea1a 1007 /* Abort DMA RX */
NYX 0:85b3fd62ea1a 1008 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
NYX 0:85b3fd62ea1a 1009 {
NYX 0:85b3fd62ea1a 1010 hsc->hdmarx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 1011 AbortCplt = 0x01U;
NYX 0:85b3fd62ea1a 1012 }
NYX 0:85b3fd62ea1a 1013 else
NYX 0:85b3fd62ea1a 1014 {
NYX 0:85b3fd62ea1a 1015 AbortCplt = 0x00U;
NYX 0:85b3fd62ea1a 1016 }
NYX 0:85b3fd62ea1a 1017 }
NYX 0:85b3fd62ea1a 1018 }
NYX 0:85b3fd62ea1a 1019
NYX 0:85b3fd62ea1a 1020 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
NYX 0:85b3fd62ea1a 1021 if(AbortCplt == 0x01U)
NYX 0:85b3fd62ea1a 1022 {
NYX 0:85b3fd62ea1a 1023 /* Reset Tx and Rx transfer counters */
NYX 0:85b3fd62ea1a 1024 hsc->TxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1025 hsc->RxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1026
NYX 0:85b3fd62ea1a 1027 /* Reset ErrorCode */
NYX 0:85b3fd62ea1a 1028 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 1029
NYX 0:85b3fd62ea1a 1030 /* Restore hsc->gState and hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 1031 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1032 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1033
NYX 0:85b3fd62ea1a 1034 /* As no DMA to be aborted, call directly user Abort complete callback */
NYX 0:85b3fd62ea1a 1035 HAL_SMARTCARD_AbortCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1036 }
NYX 0:85b3fd62ea1a 1037 return HAL_OK;
NYX 0:85b3fd62ea1a 1038 }
NYX 0:85b3fd62ea1a 1039
NYX 0:85b3fd62ea1a 1040 /**
NYX 0:85b3fd62ea1a 1041 * @brief Abort ongoing Transmit transfer (Interrupt mode).
NYX 0:85b3fd62ea1a 1042 * @param hsc SMARTCARD handle.
NYX 0:85b3fd62ea1a 1043 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
NYX 0:85b3fd62ea1a 1044 * This procedure performs following operations :
NYX 0:85b3fd62ea1a 1045 * - Disable PPP Interrupts
NYX 0:85b3fd62ea1a 1046 * - Disable the DMA transfer in the peripheral register (if enabled)
NYX 0:85b3fd62ea1a 1047 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
NYX 0:85b3fd62ea1a 1048 * - Set handle State to READY
NYX 0:85b3fd62ea1a 1049 * - At abort completion, call user abort complete callback
NYX 0:85b3fd62ea1a 1050 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
NYX 0:85b3fd62ea1a 1051 * considered as completed only when user abort complete callback is executed (not when exiting function).
NYX 0:85b3fd62ea1a 1052 * @retval HAL status
NYX 0:85b3fd62ea1a 1053 */
NYX 0:85b3fd62ea1a 1054 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1055 {
NYX 0:85b3fd62ea1a 1056 /* Disable TXEIE and TCIE interrupts */
NYX 0:85b3fd62ea1a 1057 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
NYX 0:85b3fd62ea1a 1058
NYX 0:85b3fd62ea1a 1059 /* Disable the SMARTCARD DMA Tx request if enabled */
NYX 0:85b3fd62ea1a 1060 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
NYX 0:85b3fd62ea1a 1061 {
NYX 0:85b3fd62ea1a 1062 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
NYX 0:85b3fd62ea1a 1063
NYX 0:85b3fd62ea1a 1064 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
NYX 0:85b3fd62ea1a 1065 if(hsc->hdmatx != NULL)
NYX 0:85b3fd62ea1a 1066 {
NYX 0:85b3fd62ea1a 1067 /* Set the SMARTCARD DMA Abort callback :
NYX 0:85b3fd62ea1a 1068 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
NYX 0:85b3fd62ea1a 1069 hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
NYX 0:85b3fd62ea1a 1070
NYX 0:85b3fd62ea1a 1071 /* Abort DMA TX */
NYX 0:85b3fd62ea1a 1072 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
NYX 0:85b3fd62ea1a 1073 {
NYX 0:85b3fd62ea1a 1074 /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
NYX 0:85b3fd62ea1a 1075 hsc->hdmatx->XferAbortCallback(hsc->hdmatx);
NYX 0:85b3fd62ea1a 1076 }
NYX 0:85b3fd62ea1a 1077 }
NYX 0:85b3fd62ea1a 1078 else
NYX 0:85b3fd62ea1a 1079 {
NYX 0:85b3fd62ea1a 1080 /* Reset Tx transfer counter */
NYX 0:85b3fd62ea1a 1081 hsc->TxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1082
NYX 0:85b3fd62ea1a 1083 /* Restore hsc->gState to Ready */
NYX 0:85b3fd62ea1a 1084 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1085
NYX 0:85b3fd62ea1a 1086 /* As no DMA to be aborted, call directly user Abort complete callback */
NYX 0:85b3fd62ea1a 1087 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1088 }
NYX 0:85b3fd62ea1a 1089 }
NYX 0:85b3fd62ea1a 1090 else
NYX 0:85b3fd62ea1a 1091 {
NYX 0:85b3fd62ea1a 1092 /* Reset Tx transfer counter */
NYX 0:85b3fd62ea1a 1093 hsc->TxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1094
NYX 0:85b3fd62ea1a 1095 /* Restore hsc->gState to Ready */
NYX 0:85b3fd62ea1a 1096 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1097
NYX 0:85b3fd62ea1a 1098 /* As no DMA to be aborted, call directly user Abort complete callback */
NYX 0:85b3fd62ea1a 1099 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1100 }
NYX 0:85b3fd62ea1a 1101
NYX 0:85b3fd62ea1a 1102 return HAL_OK;
NYX 0:85b3fd62ea1a 1103 }
NYX 0:85b3fd62ea1a 1104
NYX 0:85b3fd62ea1a 1105 /**
NYX 0:85b3fd62ea1a 1106 * @brief Abort ongoing Receive transfer (Interrupt mode).
NYX 0:85b3fd62ea1a 1107 * @param hsc SMARTCARD handle.
NYX 0:85b3fd62ea1a 1108 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
NYX 0:85b3fd62ea1a 1109 * This procedure performs following operations :
NYX 0:85b3fd62ea1a 1110 * - Disable PPP Interrupts
NYX 0:85b3fd62ea1a 1111 * - Disable the DMA transfer in the peripheral register (if enabled)
NYX 0:85b3fd62ea1a 1112 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
NYX 0:85b3fd62ea1a 1113 * - Set handle State to READY
NYX 0:85b3fd62ea1a 1114 * - At abort completion, call user abort complete callback
NYX 0:85b3fd62ea1a 1115 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
NYX 0:85b3fd62ea1a 1116 * considered as completed only when user abort complete callback is executed (not when exiting function).
NYX 0:85b3fd62ea1a 1117 * @retval HAL status
NYX 0:85b3fd62ea1a 1118 */
NYX 0:85b3fd62ea1a 1119 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1120 {
NYX 0:85b3fd62ea1a 1121 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
NYX 0:85b3fd62ea1a 1122 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
NYX 0:85b3fd62ea1a 1123 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 1124
NYX 0:85b3fd62ea1a 1125 /* Disable the SMARTCARD DMA Rx request if enabled */
NYX 0:85b3fd62ea1a 1126 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
NYX 0:85b3fd62ea1a 1127 {
NYX 0:85b3fd62ea1a 1128 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
NYX 0:85b3fd62ea1a 1129
NYX 0:85b3fd62ea1a 1130 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
NYX 0:85b3fd62ea1a 1131 if(hsc->hdmarx != NULL)
NYX 0:85b3fd62ea1a 1132 {
NYX 0:85b3fd62ea1a 1133 /* Set the SMARTCARD DMA Abort callback :
NYX 0:85b3fd62ea1a 1134 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
NYX 0:85b3fd62ea1a 1135 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
NYX 0:85b3fd62ea1a 1136
NYX 0:85b3fd62ea1a 1137 /* Abort DMA RX */
NYX 0:85b3fd62ea1a 1138 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
NYX 0:85b3fd62ea1a 1139 {
NYX 0:85b3fd62ea1a 1140 /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
NYX 0:85b3fd62ea1a 1141 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
NYX 0:85b3fd62ea1a 1142 }
NYX 0:85b3fd62ea1a 1143 }
NYX 0:85b3fd62ea1a 1144 else
NYX 0:85b3fd62ea1a 1145 {
NYX 0:85b3fd62ea1a 1146 /* Reset Rx transfer counter */
NYX 0:85b3fd62ea1a 1147 hsc->RxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1148
NYX 0:85b3fd62ea1a 1149 /* Restore hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 1150 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1151
NYX 0:85b3fd62ea1a 1152 /* As no DMA to be aborted, call directly user Abort complete callback */
NYX 0:85b3fd62ea1a 1153 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1154 }
NYX 0:85b3fd62ea1a 1155 }
NYX 0:85b3fd62ea1a 1156 else
NYX 0:85b3fd62ea1a 1157 {
NYX 0:85b3fd62ea1a 1158 /* Reset Rx transfer counter */
NYX 0:85b3fd62ea1a 1159 hsc->RxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1160
NYX 0:85b3fd62ea1a 1161 /* Restore hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 1162 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1163
NYX 0:85b3fd62ea1a 1164 /* As no DMA to be aborted, call directly user Abort complete callback */
NYX 0:85b3fd62ea1a 1165 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1166 }
NYX 0:85b3fd62ea1a 1167
NYX 0:85b3fd62ea1a 1168 return HAL_OK;
NYX 0:85b3fd62ea1a 1169 }
NYX 0:85b3fd62ea1a 1170
NYX 0:85b3fd62ea1a 1171 /**
NYX 0:85b3fd62ea1a 1172 * @brief This function handles SMARTCARD interrupt request.
NYX 0:85b3fd62ea1a 1173 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1174 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 1175 * @retval None
NYX 0:85b3fd62ea1a 1176 */
NYX 0:85b3fd62ea1a 1177 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1178 {
NYX 0:85b3fd62ea1a 1179 uint32_t isrflags = READ_REG(hsc->Instance->SR);
NYX 0:85b3fd62ea1a 1180 uint32_t cr1its = READ_REG(hsc->Instance->CR1);
NYX 0:85b3fd62ea1a 1181 uint32_t cr3its = READ_REG(hsc->Instance->CR3);
NYX 0:85b3fd62ea1a 1182 uint32_t dmarequest = 0x00U;
NYX 0:85b3fd62ea1a 1183 uint32_t errorflags = 0x00U;
NYX 0:85b3fd62ea1a 1184
NYX 0:85b3fd62ea1a 1185 /* If no error occurs */
NYX 0:85b3fd62ea1a 1186 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
NYX 0:85b3fd62ea1a 1187 if(errorflags == RESET)
NYX 0:85b3fd62ea1a 1188 {
NYX 0:85b3fd62ea1a 1189 /* SMARTCARD in mode Receiver -------------------------------------------------*/
NYX 0:85b3fd62ea1a 1190 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
NYX 0:85b3fd62ea1a 1191 {
NYX 0:85b3fd62ea1a 1192 SMARTCARD_Receive_IT(hsc);
NYX 0:85b3fd62ea1a 1193 return;
NYX 0:85b3fd62ea1a 1194 }
NYX 0:85b3fd62ea1a 1195 }
NYX 0:85b3fd62ea1a 1196
NYX 0:85b3fd62ea1a 1197 /* If some errors occur */
NYX 0:85b3fd62ea1a 1198 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
NYX 0:85b3fd62ea1a 1199 {
NYX 0:85b3fd62ea1a 1200 /* SMARTCARD parity error interrupt occurred ---------------------------*/
NYX 0:85b3fd62ea1a 1201 if(((isrflags & SMARTCARD_FLAG_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
NYX 0:85b3fd62ea1a 1202 {
NYX 0:85b3fd62ea1a 1203 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
NYX 0:85b3fd62ea1a 1204 }
NYX 0:85b3fd62ea1a 1205
NYX 0:85b3fd62ea1a 1206 /* SMARTCARD frame error interrupt occurred ----------------------------*/
NYX 0:85b3fd62ea1a 1207 if(((isrflags & SMARTCARD_FLAG_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
NYX 0:85b3fd62ea1a 1208 {
NYX 0:85b3fd62ea1a 1209 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
NYX 0:85b3fd62ea1a 1210 }
NYX 0:85b3fd62ea1a 1211
NYX 0:85b3fd62ea1a 1212 /* SMARTCARD noise error interrupt occurred ----------------------------*/
NYX 0:85b3fd62ea1a 1213 if(((isrflags & SMARTCARD_FLAG_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
NYX 0:85b3fd62ea1a 1214 {
NYX 0:85b3fd62ea1a 1215 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
NYX 0:85b3fd62ea1a 1216 }
NYX 0:85b3fd62ea1a 1217
NYX 0:85b3fd62ea1a 1218 /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
NYX 0:85b3fd62ea1a 1219 if(((isrflags & SMARTCARD_FLAG_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
NYX 0:85b3fd62ea1a 1220 {
NYX 0:85b3fd62ea1a 1221 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
NYX 0:85b3fd62ea1a 1222 }
NYX 0:85b3fd62ea1a 1223 /* Call the Error call Back in case of Errors */
NYX 0:85b3fd62ea1a 1224 if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
NYX 0:85b3fd62ea1a 1225 {
NYX 0:85b3fd62ea1a 1226 /* SMARTCARD in mode Receiver -----------------------------------------------*/
NYX 0:85b3fd62ea1a 1227 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
NYX 0:85b3fd62ea1a 1228 {
NYX 0:85b3fd62ea1a 1229 SMARTCARD_Receive_IT(hsc);
NYX 0:85b3fd62ea1a 1230 }
NYX 0:85b3fd62ea1a 1231
NYX 0:85b3fd62ea1a 1232 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
NYX 0:85b3fd62ea1a 1233 consider error as blocking */
NYX 0:85b3fd62ea1a 1234 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
NYX 0:85b3fd62ea1a 1235 if(((hsc->ErrorCode & HAL_SMARTCARD_ERROR_ORE) != RESET) || dmarequest)
NYX 0:85b3fd62ea1a 1236 {
NYX 0:85b3fd62ea1a 1237 /* Blocking error : transfer is aborted
NYX 0:85b3fd62ea1a 1238 Set the SMARTCARD state ready to be able to start again the process,
NYX 0:85b3fd62ea1a 1239 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
NYX 0:85b3fd62ea1a 1240 SMARTCARD_EndRxTransfer(hsc);
NYX 0:85b3fd62ea1a 1241 /* Disable the SMARTCARD DMA Rx request if enabled */
NYX 0:85b3fd62ea1a 1242 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
NYX 0:85b3fd62ea1a 1243 {
NYX 0:85b3fd62ea1a 1244 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
NYX 0:85b3fd62ea1a 1245
NYX 0:85b3fd62ea1a 1246 /* Abort the SMARTCARD DMA Rx channel */
NYX 0:85b3fd62ea1a 1247 if(hsc->hdmarx != NULL)
NYX 0:85b3fd62ea1a 1248 {
NYX 0:85b3fd62ea1a 1249 /* Set the SMARTCARD DMA Abort callback :
NYX 0:85b3fd62ea1a 1250 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
NYX 0:85b3fd62ea1a 1251 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
NYX 0:85b3fd62ea1a 1252
NYX 0:85b3fd62ea1a 1253 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
NYX 0:85b3fd62ea1a 1254 {
NYX 0:85b3fd62ea1a 1255 /* Call Directly XferAbortCallback function in case of error */
NYX 0:85b3fd62ea1a 1256 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
NYX 0:85b3fd62ea1a 1257 }
NYX 0:85b3fd62ea1a 1258 }
NYX 0:85b3fd62ea1a 1259 else
NYX 0:85b3fd62ea1a 1260 {
NYX 0:85b3fd62ea1a 1261 /* Call user error callback */
NYX 0:85b3fd62ea1a 1262 HAL_SMARTCARD_ErrorCallback(hsc);
NYX 0:85b3fd62ea1a 1263 }
NYX 0:85b3fd62ea1a 1264 }
NYX 0:85b3fd62ea1a 1265 else
NYX 0:85b3fd62ea1a 1266 {
NYX 0:85b3fd62ea1a 1267 /* Call user error callback */
NYX 0:85b3fd62ea1a 1268 HAL_SMARTCARD_ErrorCallback(hsc);
NYX 0:85b3fd62ea1a 1269 }
NYX 0:85b3fd62ea1a 1270 }
NYX 0:85b3fd62ea1a 1271 else
NYX 0:85b3fd62ea1a 1272 {
NYX 0:85b3fd62ea1a 1273 /* Call user error callback */
NYX 0:85b3fd62ea1a 1274 HAL_SMARTCARD_ErrorCallback(hsc);
NYX 0:85b3fd62ea1a 1275 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 1276 }
NYX 0:85b3fd62ea1a 1277 }
NYX 0:85b3fd62ea1a 1278 return;
NYX 0:85b3fd62ea1a 1279 } /* End if some error occurs */
NYX 0:85b3fd62ea1a 1280
NYX 0:85b3fd62ea1a 1281 /* SMARTCARD in mode Transmitter -------------------------------------------*/
NYX 0:85b3fd62ea1a 1282 if(((isrflags & SMARTCARD_FLAG_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
NYX 0:85b3fd62ea1a 1283 {
NYX 0:85b3fd62ea1a 1284 SMARTCARD_Transmit_IT(hsc);
NYX 0:85b3fd62ea1a 1285 return;
NYX 0:85b3fd62ea1a 1286 }
NYX 0:85b3fd62ea1a 1287
NYX 0:85b3fd62ea1a 1288 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
NYX 0:85b3fd62ea1a 1289 if(((isrflags & SMARTCARD_FLAG_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
NYX 0:85b3fd62ea1a 1290 {
NYX 0:85b3fd62ea1a 1291 SMARTCARD_EndTransmit_IT(hsc);
NYX 0:85b3fd62ea1a 1292 return;
NYX 0:85b3fd62ea1a 1293 }
NYX 0:85b3fd62ea1a 1294 }
NYX 0:85b3fd62ea1a 1295
NYX 0:85b3fd62ea1a 1296 /**
NYX 0:85b3fd62ea1a 1297 * @brief Tx Transfer completed callbacks
NYX 0:85b3fd62ea1a 1298 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1299 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 1300 * @retval None
NYX 0:85b3fd62ea1a 1301 */
NYX 0:85b3fd62ea1a 1302 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1303 {
NYX 0:85b3fd62ea1a 1304 /* Prevent unused argument(s) compilation warning */
NYX 0:85b3fd62ea1a 1305 UNUSED(hsc);
NYX 0:85b3fd62ea1a 1306 /* NOTE : This function Should not be modified, when the callback is needed,
NYX 0:85b3fd62ea1a 1307 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
NYX 0:85b3fd62ea1a 1308 */
NYX 0:85b3fd62ea1a 1309 }
NYX 0:85b3fd62ea1a 1310
NYX 0:85b3fd62ea1a 1311 /**
NYX 0:85b3fd62ea1a 1312 * @brief Rx Transfer completed callbacks
NYX 0:85b3fd62ea1a 1313 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1314 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 1315 * @retval None
NYX 0:85b3fd62ea1a 1316 */
NYX 0:85b3fd62ea1a 1317 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1318 {
NYX 0:85b3fd62ea1a 1319 /* Prevent unused argument(s) compilation warning */
NYX 0:85b3fd62ea1a 1320 UNUSED(hsc);
NYX 0:85b3fd62ea1a 1321 /* NOTE : This function Should not be modified, when the callback is needed,
NYX 0:85b3fd62ea1a 1322 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
NYX 0:85b3fd62ea1a 1323 */
NYX 0:85b3fd62ea1a 1324 }
NYX 0:85b3fd62ea1a 1325
NYX 0:85b3fd62ea1a 1326 /**
NYX 0:85b3fd62ea1a 1327 * @brief SMARTCARD error callbacks
NYX 0:85b3fd62ea1a 1328 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1329 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 1330 * @retval None
NYX 0:85b3fd62ea1a 1331 */
NYX 0:85b3fd62ea1a 1332 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1333 {
NYX 0:85b3fd62ea1a 1334 /* Prevent unused argument(s) compilation warning */
NYX 0:85b3fd62ea1a 1335 UNUSED(hsc);
NYX 0:85b3fd62ea1a 1336 /* NOTE : This function Should not be modified, when the callback is needed,
NYX 0:85b3fd62ea1a 1337 the HAL_SMARTCARD_ErrorCallback could be implemented in the user file
NYX 0:85b3fd62ea1a 1338 */
NYX 0:85b3fd62ea1a 1339 }
NYX 0:85b3fd62ea1a 1340
NYX 0:85b3fd62ea1a 1341 /**
NYX 0:85b3fd62ea1a 1342 * @brief SMARTCARD Abort Complete callback.
NYX 0:85b3fd62ea1a 1343 * @param hsc SMARTCARD handle.
NYX 0:85b3fd62ea1a 1344 * @retval None
NYX 0:85b3fd62ea1a 1345 */
NYX 0:85b3fd62ea1a 1346 __weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1347 {
NYX 0:85b3fd62ea1a 1348 /* Prevent unused argument(s) compilation warning */
NYX 0:85b3fd62ea1a 1349 UNUSED(hsc);
NYX 0:85b3fd62ea1a 1350
NYX 0:85b3fd62ea1a 1351 /* NOTE : This function should not be modified, when the callback is needed,
NYX 0:85b3fd62ea1a 1352 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
NYX 0:85b3fd62ea1a 1353 */
NYX 0:85b3fd62ea1a 1354 }
NYX 0:85b3fd62ea1a 1355
NYX 0:85b3fd62ea1a 1356 /**
NYX 0:85b3fd62ea1a 1357 * @brief SMARTCARD Abort Transmit Complete callback.
NYX 0:85b3fd62ea1a 1358 * @param hsc SMARTCARD handle.
NYX 0:85b3fd62ea1a 1359 * @retval None
NYX 0:85b3fd62ea1a 1360 */
NYX 0:85b3fd62ea1a 1361 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1362 {
NYX 0:85b3fd62ea1a 1363 /* Prevent unused argument(s) compilation warning */
NYX 0:85b3fd62ea1a 1364 UNUSED(hsc);
NYX 0:85b3fd62ea1a 1365
NYX 0:85b3fd62ea1a 1366 /* NOTE : This function should not be modified, when the callback is needed,
NYX 0:85b3fd62ea1a 1367 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
NYX 0:85b3fd62ea1a 1368 */
NYX 0:85b3fd62ea1a 1369 }
NYX 0:85b3fd62ea1a 1370
NYX 0:85b3fd62ea1a 1371 /**
NYX 0:85b3fd62ea1a 1372 * @brief SMARTCARD Abort ReceiveComplete callback.
NYX 0:85b3fd62ea1a 1373 * @param hsc SMARTCARD handle.
NYX 0:85b3fd62ea1a 1374 * @retval None
NYX 0:85b3fd62ea1a 1375 */
NYX 0:85b3fd62ea1a 1376 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1377 {
NYX 0:85b3fd62ea1a 1378 /* Prevent unused argument(s) compilation warning */
NYX 0:85b3fd62ea1a 1379 UNUSED(hsc);
NYX 0:85b3fd62ea1a 1380
NYX 0:85b3fd62ea1a 1381 /* NOTE : This function should not be modified, when the callback is needed,
NYX 0:85b3fd62ea1a 1382 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
NYX 0:85b3fd62ea1a 1383 */
NYX 0:85b3fd62ea1a 1384 }
NYX 0:85b3fd62ea1a 1385
NYX 0:85b3fd62ea1a 1386 /**
NYX 0:85b3fd62ea1a 1387 * @}
NYX 0:85b3fd62ea1a 1388 */
NYX 0:85b3fd62ea1a 1389
NYX 0:85b3fd62ea1a 1390 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
NYX 0:85b3fd62ea1a 1391 * @brief SMARTCARD State and Errors functions
NYX 0:85b3fd62ea1a 1392 *
NYX 0:85b3fd62ea1a 1393 @verbatim
NYX 0:85b3fd62ea1a 1394 ===============================================================================
NYX 0:85b3fd62ea1a 1395 ##### Peripheral State and Errors functions #####
NYX 0:85b3fd62ea1a 1396 ===============================================================================
NYX 0:85b3fd62ea1a 1397 [..]
NYX 0:85b3fd62ea1a 1398 This subsection provides a set of functions allowing to control the SmartCard.
NYX 0:85b3fd62ea1a 1399 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
NYX 0:85b3fd62ea1a 1400 (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
NYX 0:85b3fd62ea1a 1401 @endverbatim
NYX 0:85b3fd62ea1a 1402 * @{
NYX 0:85b3fd62ea1a 1403 */
NYX 0:85b3fd62ea1a 1404
NYX 0:85b3fd62ea1a 1405 /**
NYX 0:85b3fd62ea1a 1406 * @brief return the SMARTCARD state
NYX 0:85b3fd62ea1a 1407 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1408 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 1409 * @retval HAL state
NYX 0:85b3fd62ea1a 1410 */
NYX 0:85b3fd62ea1a 1411 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1412 {
NYX 0:85b3fd62ea1a 1413 uint32_t temp1= 0x00U, temp2 = 0x00U;
NYX 0:85b3fd62ea1a 1414 temp1 = hsc->gState;
NYX 0:85b3fd62ea1a 1415 temp2 = hsc->RxState;
NYX 0:85b3fd62ea1a 1416
NYX 0:85b3fd62ea1a 1417 return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
NYX 0:85b3fd62ea1a 1418 }
NYX 0:85b3fd62ea1a 1419
NYX 0:85b3fd62ea1a 1420 /**
NYX 0:85b3fd62ea1a 1421 * @brief Return the SMARTCARD error code
NYX 0:85b3fd62ea1a 1422 * @param hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1423 * the configuration information for the specified SMARTCARD.
NYX 0:85b3fd62ea1a 1424 * @retval SMARTCARD Error Code
NYX 0:85b3fd62ea1a 1425 */
NYX 0:85b3fd62ea1a 1426 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1427 {
NYX 0:85b3fd62ea1a 1428 return hsc->ErrorCode;
NYX 0:85b3fd62ea1a 1429 }
NYX 0:85b3fd62ea1a 1430
NYX 0:85b3fd62ea1a 1431 /**
NYX 0:85b3fd62ea1a 1432 * @}
NYX 0:85b3fd62ea1a 1433 */
NYX 0:85b3fd62ea1a 1434
NYX 0:85b3fd62ea1a 1435 /**
NYX 0:85b3fd62ea1a 1436 * @brief DMA SMARTCARD transmit process complete callback
NYX 0:85b3fd62ea1a 1437 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1438 * the configuration information for the specified DMA module.
NYX 0:85b3fd62ea1a 1439 * @retval None
NYX 0:85b3fd62ea1a 1440 */
NYX 0:85b3fd62ea1a 1441 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
NYX 0:85b3fd62ea1a 1442 {
NYX 0:85b3fd62ea1a 1443 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
NYX 0:85b3fd62ea1a 1444
NYX 0:85b3fd62ea1a 1445 hsc->TxXferCount = 0U;
NYX 0:85b3fd62ea1a 1446
NYX 0:85b3fd62ea1a 1447 /* Disable the DMA transfer for transmit request by setting the DMAT bit
NYX 0:85b3fd62ea1a 1448 in the USART CR3 register */
NYX 0:85b3fd62ea1a 1449 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
NYX 0:85b3fd62ea1a 1450
NYX 0:85b3fd62ea1a 1451 /* Enable the SMARTCARD Transmit Complete Interrupt */
NYX 0:85b3fd62ea1a 1452 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
NYX 0:85b3fd62ea1a 1453 }
NYX 0:85b3fd62ea1a 1454
NYX 0:85b3fd62ea1a 1455 /**
NYX 0:85b3fd62ea1a 1456 * @brief DMA SMARTCARD receive process complete callback
NYX 0:85b3fd62ea1a 1457 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1458 * the configuration information for the specified DMA module.
NYX 0:85b3fd62ea1a 1459 * @retval None
NYX 0:85b3fd62ea1a 1460 */
NYX 0:85b3fd62ea1a 1461 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
NYX 0:85b3fd62ea1a 1462 {
NYX 0:85b3fd62ea1a 1463 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
NYX 0:85b3fd62ea1a 1464
NYX 0:85b3fd62ea1a 1465 hsc->RxXferCount = 0U;
NYX 0:85b3fd62ea1a 1466
NYX 0:85b3fd62ea1a 1467 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
NYX 0:85b3fd62ea1a 1468 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
NYX 0:85b3fd62ea1a 1469 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 1470
NYX 0:85b3fd62ea1a 1471 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
NYX 0:85b3fd62ea1a 1472 in the USART CR3 register */
NYX 0:85b3fd62ea1a 1473 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
NYX 0:85b3fd62ea1a 1474
NYX 0:85b3fd62ea1a 1475 /* At end of Rx process, restore hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 1476 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1477
NYX 0:85b3fd62ea1a 1478 HAL_SMARTCARD_RxCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1479 }
NYX 0:85b3fd62ea1a 1480
NYX 0:85b3fd62ea1a 1481 /**
NYX 0:85b3fd62ea1a 1482 * @brief DMA SMARTCARD communication error callback
NYX 0:85b3fd62ea1a 1483 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1484 * the configuration information for the specified DMA module.
NYX 0:85b3fd62ea1a 1485 * @retval None
NYX 0:85b3fd62ea1a 1486 */
NYX 0:85b3fd62ea1a 1487 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
NYX 0:85b3fd62ea1a 1488 {
NYX 0:85b3fd62ea1a 1489 uint32_t dmarequest = 0x00U;
NYX 0:85b3fd62ea1a 1490 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
NYX 0:85b3fd62ea1a 1491 hsc->RxXferCount = 0U;
NYX 0:85b3fd62ea1a 1492 hsc->TxXferCount = 0U;
NYX 0:85b3fd62ea1a 1493 hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
NYX 0:85b3fd62ea1a 1494
NYX 0:85b3fd62ea1a 1495 /* Stop SMARTCARD DMA Tx request if ongoing */
NYX 0:85b3fd62ea1a 1496 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT);
NYX 0:85b3fd62ea1a 1497 if((hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX) && dmarequest)
NYX 0:85b3fd62ea1a 1498 {
NYX 0:85b3fd62ea1a 1499 SMARTCARD_EndTxTransfer(hsc);
NYX 0:85b3fd62ea1a 1500 }
NYX 0:85b3fd62ea1a 1501
NYX 0:85b3fd62ea1a 1502 /* Stop SMARTCARD DMA Rx request if ongoing */
NYX 0:85b3fd62ea1a 1503 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
NYX 0:85b3fd62ea1a 1504 if((hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX) && dmarequest)
NYX 0:85b3fd62ea1a 1505 {
NYX 0:85b3fd62ea1a 1506 SMARTCARD_EndRxTransfer(hsc);
NYX 0:85b3fd62ea1a 1507 }
NYX 0:85b3fd62ea1a 1508
NYX 0:85b3fd62ea1a 1509 HAL_SMARTCARD_ErrorCallback(hsc);
NYX 0:85b3fd62ea1a 1510 }
NYX 0:85b3fd62ea1a 1511
NYX 0:85b3fd62ea1a 1512 /**
NYX 0:85b3fd62ea1a 1513 * @brief This function handles SMARTCARD Communication Timeout.
NYX 0:85b3fd62ea1a 1514 * @param hsc: SMARTCARD handle
NYX 0:85b3fd62ea1a 1515 * @param Flag: specifies the SMARTCARD flag to check.
NYX 0:85b3fd62ea1a 1516 * @param Status: The new Flag status (SET or RESET).
NYX 0:85b3fd62ea1a 1517 * @param Timeout: Timeout duration
NYX 0:85b3fd62ea1a 1518 * @param Tickstart: tick start value
NYX 0:85b3fd62ea1a 1519 * @retval HAL status
NYX 0:85b3fd62ea1a 1520 */
NYX 0:85b3fd62ea1a 1521 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
NYX 0:85b3fd62ea1a 1522 {
NYX 0:85b3fd62ea1a 1523 /* Wait until flag is set */
NYX 0:85b3fd62ea1a 1524 while((__HAL_SMARTCARD_GET_FLAG(hsc, Flag) ? SET : RESET) == Status)
NYX 0:85b3fd62ea1a 1525 {
NYX 0:85b3fd62ea1a 1526 /* Check for the Timeout */
NYX 0:85b3fd62ea1a 1527 if(Timeout != HAL_MAX_DELAY)
NYX 0:85b3fd62ea1a 1528 {
NYX 0:85b3fd62ea1a 1529 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
NYX 0:85b3fd62ea1a 1530 {
NYX 0:85b3fd62ea1a 1531 /* Disable TXE and RXNE interrupts for the interrupt process */
NYX 0:85b3fd62ea1a 1532 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
NYX 0:85b3fd62ea1a 1533 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
NYX 0:85b3fd62ea1a 1534
NYX 0:85b3fd62ea1a 1535 hsc->gState= HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1536 hsc->RxState= HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1537
NYX 0:85b3fd62ea1a 1538 /* Process Unlocked */
NYX 0:85b3fd62ea1a 1539 __HAL_UNLOCK(hsc);
NYX 0:85b3fd62ea1a 1540
NYX 0:85b3fd62ea1a 1541 return HAL_TIMEOUT;
NYX 0:85b3fd62ea1a 1542 }
NYX 0:85b3fd62ea1a 1543 }
NYX 0:85b3fd62ea1a 1544 }
NYX 0:85b3fd62ea1a 1545 return HAL_OK;
NYX 0:85b3fd62ea1a 1546 }
NYX 0:85b3fd62ea1a 1547
NYX 0:85b3fd62ea1a 1548 /**
NYX 0:85b3fd62ea1a 1549 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
NYX 0:85b3fd62ea1a 1550 * @param hsc: SMARTCARD handle.
NYX 0:85b3fd62ea1a 1551 * @retval None
NYX 0:85b3fd62ea1a 1552 */
NYX 0:85b3fd62ea1a 1553 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1554 {
NYX 0:85b3fd62ea1a 1555 /* At end of Tx process, restore hsc->gState to Ready */
NYX 0:85b3fd62ea1a 1556 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1557
NYX 0:85b3fd62ea1a 1558 /* Disable TXEIE and TCIE interrupts */
NYX 0:85b3fd62ea1a 1559 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
NYX 0:85b3fd62ea1a 1560 }
NYX 0:85b3fd62ea1a 1561
NYX 0:85b3fd62ea1a 1562
NYX 0:85b3fd62ea1a 1563 /**
NYX 0:85b3fd62ea1a 1564 * @brief End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
NYX 0:85b3fd62ea1a 1565 * @param hsc: SMARTCARD handle.
NYX 0:85b3fd62ea1a 1566 * @retval None
NYX 0:85b3fd62ea1a 1567 */
NYX 0:85b3fd62ea1a 1568 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1569 {
NYX 0:85b3fd62ea1a 1570 /* At end of Rx process, restore hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 1571 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1572
NYX 0:85b3fd62ea1a 1573 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
NYX 0:85b3fd62ea1a 1574 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
NYX 0:85b3fd62ea1a 1575 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 1576 }
NYX 0:85b3fd62ea1a 1577
NYX 0:85b3fd62ea1a 1578 /**
NYX 0:85b3fd62ea1a 1579 * @brief Send an amount of data in non blocking mode
NYX 0:85b3fd62ea1a 1580 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1581 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 1582 * @retval HAL status
NYX 0:85b3fd62ea1a 1583 */
NYX 0:85b3fd62ea1a 1584 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1585 {
NYX 0:85b3fd62ea1a 1586 uint16_t* tmp;
NYX 0:85b3fd62ea1a 1587
NYX 0:85b3fd62ea1a 1588 /* Check that a Tx process is ongoing */
NYX 0:85b3fd62ea1a 1589 if(hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX)
NYX 0:85b3fd62ea1a 1590 {
NYX 0:85b3fd62ea1a 1591 tmp = (uint16_t*) hsc->pTxBuffPtr;
NYX 0:85b3fd62ea1a 1592 hsc->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
NYX 0:85b3fd62ea1a 1593 hsc->pTxBuffPtr += 1U;
NYX 0:85b3fd62ea1a 1594
NYX 0:85b3fd62ea1a 1595 if(--hsc->TxXferCount == 0U)
NYX 0:85b3fd62ea1a 1596 {
NYX 0:85b3fd62ea1a 1597 /* Disable the SMARTCARD Transmit data register empty Interrupt */
NYX 0:85b3fd62ea1a 1598 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
NYX 0:85b3fd62ea1a 1599
NYX 0:85b3fd62ea1a 1600 /* Enable the SMARTCARD Transmit Complete Interrupt */
NYX 0:85b3fd62ea1a 1601 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
NYX 0:85b3fd62ea1a 1602 }
NYX 0:85b3fd62ea1a 1603
NYX 0:85b3fd62ea1a 1604 return HAL_OK;
NYX 0:85b3fd62ea1a 1605 }
NYX 0:85b3fd62ea1a 1606 else
NYX 0:85b3fd62ea1a 1607 {
NYX 0:85b3fd62ea1a 1608 return HAL_BUSY;
NYX 0:85b3fd62ea1a 1609 }
NYX 0:85b3fd62ea1a 1610 }
NYX 0:85b3fd62ea1a 1611
NYX 0:85b3fd62ea1a 1612 /**
NYX 0:85b3fd62ea1a 1613 * @brief Wraps up transmission in non blocking mode.
NYX 0:85b3fd62ea1a 1614 * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1615 * the configuration information for the specified SMARTCARD module.
NYX 0:85b3fd62ea1a 1616 * @retval HAL status
NYX 0:85b3fd62ea1a 1617 */
NYX 0:85b3fd62ea1a 1618 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
NYX 0:85b3fd62ea1a 1619 {
NYX 0:85b3fd62ea1a 1620 /* Disable the SMARTCARD Transmit Complete Interrupt */
NYX 0:85b3fd62ea1a 1621 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TCIE);
NYX 0:85b3fd62ea1a 1622
NYX 0:85b3fd62ea1a 1623 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
NYX 0:85b3fd62ea1a 1624 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 1625
NYX 0:85b3fd62ea1a 1626 /* Tx process is ended, restore hsmartcard->gState to Ready */
NYX 0:85b3fd62ea1a 1627 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1628
NYX 0:85b3fd62ea1a 1629 HAL_SMARTCARD_TxCpltCallback(hsmartcard);
NYX 0:85b3fd62ea1a 1630
NYX 0:85b3fd62ea1a 1631 return HAL_OK;
NYX 0:85b3fd62ea1a 1632 }
NYX 0:85b3fd62ea1a 1633
NYX 0:85b3fd62ea1a 1634 /**
NYX 0:85b3fd62ea1a 1635 * @brief Receive an amount of data in non blocking mode
NYX 0:85b3fd62ea1a 1636 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1637 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 1638 * @retval HAL status
NYX 0:85b3fd62ea1a 1639 */
NYX 0:85b3fd62ea1a 1640 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1641 {
NYX 0:85b3fd62ea1a 1642 uint16_t* tmp;
NYX 0:85b3fd62ea1a 1643
NYX 0:85b3fd62ea1a 1644 /* Check that a Rx process is ongoing */
NYX 0:85b3fd62ea1a 1645 if(hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
NYX 0:85b3fd62ea1a 1646 {
NYX 0:85b3fd62ea1a 1647 tmp = (uint16_t*) hsc->pRxBuffPtr;
NYX 0:85b3fd62ea1a 1648 *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF);
NYX 0:85b3fd62ea1a 1649 hsc->pRxBuffPtr += 1U;
NYX 0:85b3fd62ea1a 1650
NYX 0:85b3fd62ea1a 1651 if(--hsc->RxXferCount == 0U)
NYX 0:85b3fd62ea1a 1652 {
NYX 0:85b3fd62ea1a 1653 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
NYX 0:85b3fd62ea1a 1654
NYX 0:85b3fd62ea1a 1655 /* Disable the SMARTCARD Parity Error Interrupt */
NYX 0:85b3fd62ea1a 1656 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
NYX 0:85b3fd62ea1a 1657
NYX 0:85b3fd62ea1a 1658 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
NYX 0:85b3fd62ea1a 1659 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
NYX 0:85b3fd62ea1a 1660
NYX 0:85b3fd62ea1a 1661 /* Rx process is completed, restore hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 1662 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1663
NYX 0:85b3fd62ea1a 1664 HAL_SMARTCARD_RxCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1665
NYX 0:85b3fd62ea1a 1666 return HAL_OK;
NYX 0:85b3fd62ea1a 1667 }
NYX 0:85b3fd62ea1a 1668 return HAL_OK;
NYX 0:85b3fd62ea1a 1669 }
NYX 0:85b3fd62ea1a 1670 else
NYX 0:85b3fd62ea1a 1671 {
NYX 0:85b3fd62ea1a 1672 return HAL_BUSY;
NYX 0:85b3fd62ea1a 1673 }
NYX 0:85b3fd62ea1a 1674 }
NYX 0:85b3fd62ea1a 1675
NYX 0:85b3fd62ea1a 1676 /**
NYX 0:85b3fd62ea1a 1677 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
NYX 0:85b3fd62ea1a 1678 * (To be called at end of DMA Abort procedure following error occurrence).
NYX 0:85b3fd62ea1a 1679 * @param hdma DMA handle.
NYX 0:85b3fd62ea1a 1680 * @retval None
NYX 0:85b3fd62ea1a 1681 */
NYX 0:85b3fd62ea1a 1682 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
NYX 0:85b3fd62ea1a 1683 {
NYX 0:85b3fd62ea1a 1684 SMARTCARD_HandleTypeDef* hsc = (SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
NYX 0:85b3fd62ea1a 1685 hsc->RxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1686 hsc->TxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1687
NYX 0:85b3fd62ea1a 1688 HAL_SMARTCARD_ErrorCallback(hsc);
NYX 0:85b3fd62ea1a 1689 }
NYX 0:85b3fd62ea1a 1690
NYX 0:85b3fd62ea1a 1691 /**
NYX 0:85b3fd62ea1a 1692 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
NYX 0:85b3fd62ea1a 1693 * (To be called at end of DMA Tx Abort procedure following user abort request).
NYX 0:85b3fd62ea1a 1694 * @note When this callback is executed, User Abort complete call back is called only if no
NYX 0:85b3fd62ea1a 1695 * Abort still ongoing for Rx DMA Handle.
NYX 0:85b3fd62ea1a 1696 * @param hdma DMA handle.
NYX 0:85b3fd62ea1a 1697 * @retval None
NYX 0:85b3fd62ea1a 1698 */
NYX 0:85b3fd62ea1a 1699 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
NYX 0:85b3fd62ea1a 1700 {
NYX 0:85b3fd62ea1a 1701 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
NYX 0:85b3fd62ea1a 1702
NYX 0:85b3fd62ea1a 1703 hsc->hdmatx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 1704
NYX 0:85b3fd62ea1a 1705 /* Check if an Abort process is still ongoing */
NYX 0:85b3fd62ea1a 1706 if(hsc->hdmarx != NULL)
NYX 0:85b3fd62ea1a 1707 {
NYX 0:85b3fd62ea1a 1708 if(hsc->hdmarx->XferAbortCallback != NULL)
NYX 0:85b3fd62ea1a 1709 {
NYX 0:85b3fd62ea1a 1710 return;
NYX 0:85b3fd62ea1a 1711 }
NYX 0:85b3fd62ea1a 1712 }
NYX 0:85b3fd62ea1a 1713
NYX 0:85b3fd62ea1a 1714 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
NYX 0:85b3fd62ea1a 1715 hsc->TxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1716 hsc->RxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1717
NYX 0:85b3fd62ea1a 1718 /* Reset ErrorCode */
NYX 0:85b3fd62ea1a 1719 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 1720
NYX 0:85b3fd62ea1a 1721 /* Restore hsc->gState and hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 1722 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1723 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1724
NYX 0:85b3fd62ea1a 1725 /* Call user Abort complete callback */
NYX 0:85b3fd62ea1a 1726 HAL_SMARTCARD_AbortCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1727 }
NYX 0:85b3fd62ea1a 1728
NYX 0:85b3fd62ea1a 1729 /**
NYX 0:85b3fd62ea1a 1730 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
NYX 0:85b3fd62ea1a 1731 * (To be called at end of DMA Rx Abort procedure following user abort request).
NYX 0:85b3fd62ea1a 1732 * @note When this callback is executed, User Abort complete call back is called only if no
NYX 0:85b3fd62ea1a 1733 * Abort still ongoing for Tx DMA Handle.
NYX 0:85b3fd62ea1a 1734 * @param hdma DMA handle.
NYX 0:85b3fd62ea1a 1735 * @retval None
NYX 0:85b3fd62ea1a 1736 */
NYX 0:85b3fd62ea1a 1737 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
NYX 0:85b3fd62ea1a 1738 {
NYX 0:85b3fd62ea1a 1739 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
NYX 0:85b3fd62ea1a 1740
NYX 0:85b3fd62ea1a 1741 hsc->hdmarx->XferAbortCallback = NULL;
NYX 0:85b3fd62ea1a 1742
NYX 0:85b3fd62ea1a 1743 /* Check if an Abort process is still ongoing */
NYX 0:85b3fd62ea1a 1744 if(hsc->hdmatx != NULL)
NYX 0:85b3fd62ea1a 1745 {
NYX 0:85b3fd62ea1a 1746 if(hsc->hdmatx->XferAbortCallback != NULL)
NYX 0:85b3fd62ea1a 1747 {
NYX 0:85b3fd62ea1a 1748 return;
NYX 0:85b3fd62ea1a 1749 }
NYX 0:85b3fd62ea1a 1750 }
NYX 0:85b3fd62ea1a 1751
NYX 0:85b3fd62ea1a 1752 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
NYX 0:85b3fd62ea1a 1753 hsc->TxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1754 hsc->RxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1755
NYX 0:85b3fd62ea1a 1756 /* Reset ErrorCode */
NYX 0:85b3fd62ea1a 1757 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
NYX 0:85b3fd62ea1a 1758
NYX 0:85b3fd62ea1a 1759 /* Restore hsc->gState and hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 1760 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1761 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1762
NYX 0:85b3fd62ea1a 1763 /* Call user Abort complete callback */
NYX 0:85b3fd62ea1a 1764 HAL_SMARTCARD_AbortCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1765 }
NYX 0:85b3fd62ea1a 1766
NYX 0:85b3fd62ea1a 1767 /**
NYX 0:85b3fd62ea1a 1768 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
NYX 0:85b3fd62ea1a 1769 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
NYX 0:85b3fd62ea1a 1770 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
NYX 0:85b3fd62ea1a 1771 * and leads to user Tx Abort Complete callback execution).
NYX 0:85b3fd62ea1a 1772 * @param hdma DMA handle.
NYX 0:85b3fd62ea1a 1773 * @retval None
NYX 0:85b3fd62ea1a 1774 */
NYX 0:85b3fd62ea1a 1775 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
NYX 0:85b3fd62ea1a 1776 {
NYX 0:85b3fd62ea1a 1777 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
NYX 0:85b3fd62ea1a 1778
NYX 0:85b3fd62ea1a 1779 hsc->TxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1780
NYX 0:85b3fd62ea1a 1781 /* Restore hsc->gState to Ready */
NYX 0:85b3fd62ea1a 1782 hsc->gState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1783
NYX 0:85b3fd62ea1a 1784 /* Call user Abort complete callback */
NYX 0:85b3fd62ea1a 1785 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1786 }
NYX 0:85b3fd62ea1a 1787
NYX 0:85b3fd62ea1a 1788 /**
NYX 0:85b3fd62ea1a 1789 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
NYX 0:85b3fd62ea1a 1790 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
NYX 0:85b3fd62ea1a 1791 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
NYX 0:85b3fd62ea1a 1792 * and leads to user Rx Abort Complete callback execution).
NYX 0:85b3fd62ea1a 1793 * @param hdma DMA handle.
NYX 0:85b3fd62ea1a 1794 * @retval None
NYX 0:85b3fd62ea1a 1795 */
NYX 0:85b3fd62ea1a 1796 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
NYX 0:85b3fd62ea1a 1797 {
NYX 0:85b3fd62ea1a 1798 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
NYX 0:85b3fd62ea1a 1799
NYX 0:85b3fd62ea1a 1800 hsc->RxXferCount = 0x00U;
NYX 0:85b3fd62ea1a 1801
NYX 0:85b3fd62ea1a 1802 /* Restore hsc->RxState to Ready */
NYX 0:85b3fd62ea1a 1803 hsc->RxState = HAL_SMARTCARD_STATE_READY;
NYX 0:85b3fd62ea1a 1804
NYX 0:85b3fd62ea1a 1805 /* Call user Abort complete callback */
NYX 0:85b3fd62ea1a 1806 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
NYX 0:85b3fd62ea1a 1807 }
NYX 0:85b3fd62ea1a 1808
NYX 0:85b3fd62ea1a 1809 /**
NYX 0:85b3fd62ea1a 1810 * @brief Configure the SMARTCARD peripheral
NYX 0:85b3fd62ea1a 1811 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
NYX 0:85b3fd62ea1a 1812 * the configuration information for SMARTCARD module.
NYX 0:85b3fd62ea1a 1813 * @retval None
NYX 0:85b3fd62ea1a 1814 */
NYX 0:85b3fd62ea1a 1815 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
NYX 0:85b3fd62ea1a 1816 {
NYX 0:85b3fd62ea1a 1817 uint32_t tmpreg = 0x00U;
NYX 0:85b3fd62ea1a 1818
NYX 0:85b3fd62ea1a 1819 /* Check the parameters */
NYX 0:85b3fd62ea1a 1820 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
NYX 0:85b3fd62ea1a 1821 assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
NYX 0:85b3fd62ea1a 1822 assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
NYX 0:85b3fd62ea1a 1823 assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
NYX 0:85b3fd62ea1a 1824 assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
NYX 0:85b3fd62ea1a 1825 assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
NYX 0:85b3fd62ea1a 1826 assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
NYX 0:85b3fd62ea1a 1827 assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
NYX 0:85b3fd62ea1a 1828 assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
NYX 0:85b3fd62ea1a 1829 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
NYX 0:85b3fd62ea1a 1830
NYX 0:85b3fd62ea1a 1831 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
NYX 0:85b3fd62ea1a 1832 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
NYX 0:85b3fd62ea1a 1833 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
NYX 0:85b3fd62ea1a 1834
NYX 0:85b3fd62ea1a 1835 /*---------------------------- USART CR2 Configuration ---------------------*/
NYX 0:85b3fd62ea1a 1836 tmpreg = hsc->Instance->CR2;
NYX 0:85b3fd62ea1a 1837 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
NYX 0:85b3fd62ea1a 1838 tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
NYX 0:85b3fd62ea1a 1839 /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
NYX 0:85b3fd62ea1a 1840 /* Set CPOL bit according to hsc->Init.CLKPolarity value */
NYX 0:85b3fd62ea1a 1841 /* Set CPHA bit according to hsc->Init.CLKPhase value */
NYX 0:85b3fd62ea1a 1842 /* Set LBCL bit according to hsc->Init.CLKLastBit value */
NYX 0:85b3fd62ea1a 1843 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
NYX 0:85b3fd62ea1a 1844 tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
NYX 0:85b3fd62ea1a 1845 hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits);
NYX 0:85b3fd62ea1a 1846 /* Write to USART CR2 */
NYX 0:85b3fd62ea1a 1847 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
NYX 0:85b3fd62ea1a 1848
NYX 0:85b3fd62ea1a 1849 tmpreg = hsc->Instance->CR2;
NYX 0:85b3fd62ea1a 1850
NYX 0:85b3fd62ea1a 1851 /* Clear STOP[13:12] bits */
NYX 0:85b3fd62ea1a 1852 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
NYX 0:85b3fd62ea1a 1853
NYX 0:85b3fd62ea1a 1854 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
NYX 0:85b3fd62ea1a 1855 tmpreg |= (uint32_t)(hsc->Init.StopBits);
NYX 0:85b3fd62ea1a 1856
NYX 0:85b3fd62ea1a 1857 /* Write to USART CR2 */
NYX 0:85b3fd62ea1a 1858 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
NYX 0:85b3fd62ea1a 1859
NYX 0:85b3fd62ea1a 1860 /*-------------------------- USART CR1 Configuration -----------------------*/
NYX 0:85b3fd62ea1a 1861 tmpreg = hsc->Instance->CR1;
NYX 0:85b3fd62ea1a 1862
NYX 0:85b3fd62ea1a 1863 /* Clear M, PCE, PS, TE and RE bits */
NYX 0:85b3fd62ea1a 1864 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
NYX 0:85b3fd62ea1a 1865 USART_CR1_RE));
NYX 0:85b3fd62ea1a 1866
NYX 0:85b3fd62ea1a 1867 /* Configure the SMARTCARD Word Length, Parity and mode:
NYX 0:85b3fd62ea1a 1868 Set the M bits according to hsc->Init.WordLength value
NYX 0:85b3fd62ea1a 1869 Set PCE and PS bits according to hsc->Init.Parity value
NYX 0:85b3fd62ea1a 1870 Set TE and RE bits according to hsc->Init.Mode value */
NYX 0:85b3fd62ea1a 1871 tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
NYX 0:85b3fd62ea1a 1872
NYX 0:85b3fd62ea1a 1873 /* Write to USART CR1 */
NYX 0:85b3fd62ea1a 1874 WRITE_REG(hsc->Instance->CR1, (uint32_t)tmpreg);
NYX 0:85b3fd62ea1a 1875
NYX 0:85b3fd62ea1a 1876 /*-------------------------- USART CR3 Configuration -----------------------*/
NYX 0:85b3fd62ea1a 1877 /* Clear CTSE and RTSE bits */
NYX 0:85b3fd62ea1a 1878 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
NYX 0:85b3fd62ea1a 1879
NYX 0:85b3fd62ea1a 1880 /*-------------------------- USART BRR Configuration -----------------------*/
NYX 0:85b3fd62ea1a 1881 #if defined(USART6)
NYX 0:85b3fd62ea1a 1882 if((hsc->Instance == USART1) || (hsc->Instance == USART6))
NYX 0:85b3fd62ea1a 1883 {
NYX 0:85b3fd62ea1a 1884 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
NYX 0:85b3fd62ea1a 1885 }
NYX 0:85b3fd62ea1a 1886 #else
NYX 0:85b3fd62ea1a 1887 if(hsc->Instance == USART1)
NYX 0:85b3fd62ea1a 1888 {
NYX 0:85b3fd62ea1a 1889 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
NYX 0:85b3fd62ea1a 1890 }
NYX 0:85b3fd62ea1a 1891 #endif /* USART6 */
NYX 0:85b3fd62ea1a 1892 else
NYX 0:85b3fd62ea1a 1893 {
NYX 0:85b3fd62ea1a 1894 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate);
NYX 0:85b3fd62ea1a 1895 }
NYX 0:85b3fd62ea1a 1896 }
NYX 0:85b3fd62ea1a 1897
NYX 0:85b3fd62ea1a 1898 /**
NYX 0:85b3fd62ea1a 1899 * @}
NYX 0:85b3fd62ea1a 1900 */
NYX 0:85b3fd62ea1a 1901
NYX 0:85b3fd62ea1a 1902 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
NYX 0:85b3fd62ea1a 1903 /**
NYX 0:85b3fd62ea1a 1904 * @}
NYX 0:85b3fd62ea1a 1905 */
NYX 0:85b3fd62ea1a 1906
NYX 0:85b3fd62ea1a 1907 /**
NYX 0:85b3fd62ea1a 1908 * @}
NYX 0:85b3fd62ea1a 1909 */
NYX 0:85b3fd62ea1a 1910
NYX 0:85b3fd62ea1a 1911 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/