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