Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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