comparison Common/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_smartcard.c @ 160:e3ca52b8e7fa

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