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