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