L4 HAL Drivers

Committer:
EricLew
Date:
Mon Nov 02 19:37:23 2015 +0000
Revision:
0:80ee8f3b695e
Errors are with definitions of LCD and QSPI functions. I believe all .h and .c files are  uploaded, but there may need to be certain functions called.

Who changed what in which revision?

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