Mercurial > public > ostc4
comparison Common/Drivers/STM32F4xx_HAL_DRIVER_v120/Src/stm32f4xx_hal_smartcard.c @ 38:5f11787b4f42
include in ostc4 repository
author | heinrichsweikamp |
---|---|
date | Sat, 28 Apr 2018 11:52:34 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
37:ccc45c0e1ea2 | 38:5f11787b4f42 |
---|---|
1 /** | |
2 ****************************************************************************** | |
3 * @file stm32f4xx_hal_smartcard.c | |
4 * @author MCD Application Team | |
5 * @version V1.2.0 | |
6 * @date 26-December-2014 | |
7 * @brief SMARTCARD HAL module driver. | |
8 * This file provides firmware functions to manage the following | |
9 * functionalities of the SMARTCARD peripheral: | |
10 * + Initialization and de-initialization functions | |
11 * + IO operation functions | |
12 * + Peripheral State and Errors functions | |
13 * | |
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 initialized 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 customized 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 __HAL_SMARTCARD_ENABLE_IT() and __HAL_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>© 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 SMARTCARD | |
138 * @brief HAL USART SMARTCARD module driver | |
139 * @{ | |
140 */ | |
141 #ifdef HAL_SMARTCARD_MODULE_ENABLED | |
142 /* Private typedef -----------------------------------------------------------*/ | |
143 /* Private define ------------------------------------------------------------*/ | |
144 /** @addtogroup SMARTCARD_Private_Constants | |
145 * @{ | |
146 */ | |
147 #define SMARTCARD_TIMEOUT_VALUE 22000 | |
148 /** | |
149 * @} | |
150 */ | |
151 /* Private macro -------------------------------------------------------------*/ | |
152 /* Private variables ---------------------------------------------------------*/ | |
153 /* Private function prototypes -----------------------------------------------*/ | |
154 /** @addtogroup SMARTCARD_Private_Functions | |
155 * @{ | |
156 */ | |
157 static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc); | |
158 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc); | |
159 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard); | |
160 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc); | |
161 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma); | |
162 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma); | |
163 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma); | |
164 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout); | |
165 /** | |
166 * @} | |
167 */ | |
168 /* Exported functions --------------------------------------------------------*/ | |
169 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions | |
170 * @{ | |
171 */ | |
172 | |
173 /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions | |
174 * @brief Initialization and Configuration functions | |
175 * | |
176 @verbatim | |
177 ============================================================================== | |
178 ##### Initialization and Configuration functions ##### | |
179 ============================================================================== | |
180 [..] | |
181 This subsection provides a set of functions allowing to initialize the USART | |
182 in Smartcard mode. | |
183 [..] | |
184 The Smartcard interface is designed to support asynchronous protocol Smartcards as | |
185 defined in the ISO 7816-3 standard. | |
186 [..] | |
187 The USART can provide a clock to the smartcard through the SCLK output. | |
188 In smartcard mode, SCLK is not associated to the communication but is simply derived | |
189 from the internal peripheral input clock through a 5-bit prescaler. | |
190 [..] | |
191 (+) For the Smartcard mode only these parameters can be configured: | |
192 (++) Baud Rate | |
193 (++) Word Length => Should be 9 bits (8 bits + parity) | |
194 (++) Stop Bit | |
195 (++) Parity: => Should be enabled | |
196 +-------------------------------------------------------------+ | |
197 | M bit | PCE bit | SMARTCARD frame | | |
198 |---------------------|---------------------------------------| | |
199 | 1 | 1 | | SB | 8 bit data | PB | STB | | | |
200 +-------------------------------------------------------------+ | |
201 (++) USART polarity | |
202 (++) USART phase | |
203 (++) USART LastBit | |
204 (++) Receiver/transmitter modes | |
205 (++) Prescaler | |
206 (++) GuardTime | |
207 (++) NACKState: The Smartcard NACK state | |
208 | |
209 (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card: | |
210 (++) Word Length = 9 Bits | |
211 (++) 1.5 Stop Bit | |
212 (++) Even parity | |
213 (++) BaudRate = 12096 baud | |
214 (++) Tx and Rx enabled | |
215 [..] | |
216 Please refer to the ISO 7816-3 specification for more details. | |
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 */ | |
228 | |
229 /** | |
230 * @brief Initializes the SmartCard mode according to the specified | |
231 * parameters in the SMARTCARD_InitTypeDef and create the associated handle . | |
232 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
233 * the configuration information for SMARTCARD module. | |
234 * @retval HAL status | |
235 */ | |
236 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc) | |
237 { | |
238 /* Check the SMARTCARD handle allocation */ | |
239 if(hsc == NULL) | |
240 { | |
241 return HAL_ERROR; | |
242 } | |
243 | |
244 /* Check the parameters */ | |
245 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance)); | |
246 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState)); | |
247 | |
248 if(hsc->State == HAL_SMARTCARD_STATE_RESET) | |
249 { | |
250 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ | |
251 HAL_SMARTCARD_MspInit(hsc); | |
252 } | |
253 | |
254 hsc->State = HAL_SMARTCARD_STATE_BUSY; | |
255 | |
256 /* Set the Prescaler */ | |
257 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler); | |
258 | |
259 /* Set the Guard Time */ | |
260 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8)); | |
261 | |
262 /* Set the Smartcard Communication parameters */ | |
263 SMARTCARD_SetConfig(hsc); | |
264 | |
265 /* In SmartCard mode, the following bits must be kept cleared: | |
266 - LINEN bit in the USART_CR2 register | |
267 - HDSEL and IREN bits in the USART_CR3 register.*/ | |
268 hsc->Instance->CR2 &= ~USART_CR2_LINEN; | |
269 hsc->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_HDSEL); | |
270 | |
271 /* Enable the SMARTCARD Parity Error Interrupt */ | |
272 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE); | |
273 | |
274 /* Enable the SMARTCARD Framing Error Interrupt */ | |
275 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR); | |
276 | |
277 /* Enable the Peripheral */ | |
278 __HAL_SMARTCARD_ENABLE(hsc); | |
279 | |
280 /* Configure the Smartcard NACK state */ | |
281 MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState); | |
282 | |
283 /* Enable the SC mode by setting the SCEN bit in the CR3 register */ | |
284 hsc->Instance->CR3 |= (USART_CR3_SCEN); | |
285 | |
286 /* Initialize the SMARTCARD state*/ | |
287 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; | |
288 hsc->State= HAL_SMARTCARD_STATE_READY; | |
289 | |
290 return HAL_OK; | |
291 } | |
292 | |
293 /** | |
294 * @brief DeInitializes the USART SmartCard peripheral | |
295 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
296 * the configuration information for SMARTCARD module. | |
297 * @retval HAL status | |
298 */ | |
299 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc) | |
300 { | |
301 /* Check the SMARTCARD handle allocation */ | |
302 if(hsc == NULL) | |
303 { | |
304 return HAL_ERROR; | |
305 } | |
306 | |
307 /* Check the parameters */ | |
308 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance)); | |
309 | |
310 hsc->State = HAL_SMARTCARD_STATE_BUSY; | |
311 | |
312 /* Disable the Peripheral */ | |
313 __HAL_SMARTCARD_DISABLE(hsc); | |
314 | |
315 /* DeInit the low level hardware */ | |
316 HAL_SMARTCARD_MspDeInit(hsc); | |
317 | |
318 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; | |
319 hsc->State = HAL_SMARTCARD_STATE_RESET; | |
320 | |
321 /* Release Lock */ | |
322 __HAL_UNLOCK(hsc); | |
323 | |
324 return HAL_OK; | |
325 } | |
326 | |
327 /** | |
328 * @brief SMARTCARD MSP Init | |
329 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
330 * the configuration information for SMARTCARD module. | |
331 * @retval None | |
332 */ | |
333 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc) | |
334 { | |
335 /* NOTE : This function Should not be modified, when the callback is needed, | |
336 the HAL_SMARTCARD_MspInit could be implemented in the user file | |
337 */ | |
338 } | |
339 | |
340 /** | |
341 * @brief SMARTCARD MSP DeInit | |
342 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
343 * the configuration information for SMARTCARD module. | |
344 * @retval None | |
345 */ | |
346 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc) | |
347 { | |
348 /* NOTE : This function Should not be modified, when the callback is needed, | |
349 the HAL_SMARTCARD_MspDeInit could be implemented in the user file | |
350 */ | |
351 } | |
352 | |
353 /** | |
354 * @} | |
355 */ | |
356 | |
357 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions | |
358 * @brief SMARTCARD Transmit and Receive functions | |
359 * | |
360 @verbatim | |
361 =============================================================================== | |
362 ##### IO operation functions ##### | |
363 =============================================================================== | |
364 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers. | |
365 [..] | |
366 Smartcard is a single wire half duplex communication protocol. | |
367 The Smartcard interface is designed to support asynchronous protocol Smartcards as | |
368 defined in the ISO 7816-3 standard. The USART should be configured as: | |
369 (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register | |
370 (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register. | |
371 | |
372 (#) There are two modes of transfer: | |
373 (++) Blocking mode: The communication is performed in polling mode. | |
374 The HAL status of all data processing is returned by the same function | |
375 after finishing transfer. | |
376 (++) Non Blocking mode: The communication is performed using Interrupts | |
377 or DMA, These APIs return the HAL status. | |
378 The end of the data processing will be indicated through the | |
379 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when | |
380 using DMA mode. | |
381 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks | |
382 will be executed respectively at the end of the Transmit or Receive process | |
383 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected | |
384 | |
385 (#) Blocking mode APIs are : | |
386 (++) HAL_SMARTCARD_Transmit() | |
387 (++) HAL_SMARTCARD_Receive() | |
388 | |
389 (#) Non Blocking mode APIs with Interrupt are : | |
390 (++) HAL_SMARTCARD_Transmit_IT() | |
391 (++) HAL_SMARTCARD_Receive_IT() | |
392 (++) HAL_SMARTCARD_IRQHandler() | |
393 | |
394 (#) Non Blocking mode functions with DMA are : | |
395 (++) HAL_SMARTCARD_Transmit_DMA() | |
396 (++) HAL_SMARTCARD_Receive_DMA() | |
397 | |
398 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode: | |
399 (++) HAL_SMARTCARD_TxCpltCallback() | |
400 (++) HAL_SMARTCARD_RxCpltCallback() | |
401 (++) HAL_SMARTCARD_ErrorCallback() | |
402 | |
403 @endverbatim | |
404 * @{ | |
405 */ | |
406 | |
407 /** | |
408 * @brief Send an amount of data in blocking mode | |
409 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
410 * the configuration information for SMARTCARD module. | |
411 * @param pData: pointer to data buffer | |
412 * @param Size: amount of data to be sent | |
413 * @param Timeout: Timeout duration | |
414 * @retval HAL status | |
415 */ | |
416 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout) | |
417 { | |
418 uint16_t* tmp; | |
419 uint32_t tmp1 = 0; | |
420 | |
421 tmp1 = hsc->State; | |
422 if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_RX)) | |
423 { | |
424 if((pData == NULL) || (Size == 0)) | |
425 { | |
426 return HAL_ERROR; | |
427 } | |
428 | |
429 /* Process Locked */ | |
430 __HAL_LOCK(hsc); | |
431 | |
432 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; | |
433 /* Check if a non-blocking receive process is ongoing or not */ | |
434 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) | |
435 { | |
436 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX; | |
437 } | |
438 else | |
439 { | |
440 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX; | |
441 } | |
442 | |
443 hsc->TxXferSize = Size; | |
444 hsc->TxXferCount = Size; | |
445 while(hsc->TxXferCount > 0) | |
446 { | |
447 hsc->TxXferCount--; | |
448 if(hsc->Init.WordLength == SMARTCARD_WORDLENGTH_9B) | |
449 { | |
450 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK) | |
451 { | |
452 return HAL_TIMEOUT; | |
453 } | |
454 tmp = (uint16_t*) pData; | |
455 hsc->Instance->DR = (*tmp & (uint16_t)0x01FF); | |
456 if(hsc->Init.Parity == SMARTCARD_PARITY_NONE) | |
457 { | |
458 pData +=2; | |
459 } | |
460 else | |
461 { | |
462 pData +=1; | |
463 } | |
464 } | |
465 else | |
466 { | |
467 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK) | |
468 { | |
469 return HAL_TIMEOUT; | |
470 } | |
471 hsc->Instance->DR = (*pData++ & (uint8_t)0xFF); | |
472 } | |
473 } | |
474 | |
475 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK) | |
476 { | |
477 return HAL_TIMEOUT; | |
478 } | |
479 | |
480 /* Check if a non-blocking receive process is ongoing or not */ | |
481 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) | |
482 { | |
483 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX; | |
484 } | |
485 else | |
486 { | |
487 hsc->State = HAL_SMARTCARD_STATE_READY; | |
488 } | |
489 /* Process Unlocked */ | |
490 __HAL_UNLOCK(hsc); | |
491 | |
492 return HAL_OK; | |
493 } | |
494 else | |
495 { | |
496 return HAL_BUSY; | |
497 } | |
498 } | |
499 | |
500 /** | |
501 * @brief Receive an amount of data in blocking mode | |
502 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
503 * the configuration information for SMARTCARD module. | |
504 * @param pData: pointer to data buffer | |
505 * @param Size: amount of data to be received | |
506 * @param Timeout: Timeout duration | |
507 * @retval HAL status | |
508 */ | |
509 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout) | |
510 { | |
511 uint16_t* tmp; | |
512 uint32_t tmp1 = 0; | |
513 | |
514 tmp1 = hsc->State; | |
515 if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX)) | |
516 { | |
517 if((pData == NULL) || (Size == 0)) | |
518 { | |
519 return HAL_ERROR; | |
520 } | |
521 | |
522 /* Process Locked */ | |
523 __HAL_LOCK(hsc); | |
524 | |
525 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; | |
526 | |
527 /* Check if a non-blocking transmit process is ongoing or not */ | |
528 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) | |
529 { | |
530 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX; | |
531 } | |
532 else | |
533 { | |
534 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX; | |
535 } | |
536 | |
537 hsc->RxXferSize = Size; | |
538 hsc->RxXferCount = Size; | |
539 /* Check the remain data to be received */ | |
540 while(hsc->RxXferCount > 0) | |
541 { | |
542 hsc->RxXferCount--; | |
543 if(hsc->Init.WordLength == SMARTCARD_WORDLENGTH_9B) | |
544 { | |
545 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK) | |
546 { | |
547 return HAL_TIMEOUT; | |
548 } | |
549 tmp = (uint16_t*) pData; | |
550 if(hsc->Init.Parity == SMARTCARD_PARITY_NONE) | |
551 { | |
552 *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x01FF); | |
553 pData +=2; | |
554 } | |
555 else | |
556 { | |
557 *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x00FF); | |
558 pData +=1; | |
559 } | |
560 } | |
561 else | |
562 { | |
563 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK) | |
564 { | |
565 return HAL_TIMEOUT; | |
566 } | |
567 if(hsc->Init.Parity == SMARTCARD_PARITY_NONE) | |
568 { | |
569 *pData++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF); | |
570 } | |
571 else | |
572 { | |
573 *pData++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x007F); | |
574 } | |
575 } | |
576 } | |
577 | |
578 /* Check if a non-blocking transmit process is ongoing or not */ | |
579 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) | |
580 { | |
581 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX; | |
582 } | |
583 else | |
584 { | |
585 hsc->State = HAL_SMARTCARD_STATE_READY; | |
586 } | |
587 | |
588 /* Process Unlocked */ | |
589 __HAL_UNLOCK(hsc); | |
590 | |
591 return HAL_OK; | |
592 } | |
593 else | |
594 { | |
595 return HAL_BUSY; | |
596 } | |
597 } | |
598 | |
599 /** | |
600 * @brief Send an amount of data in non blocking mode | |
601 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
602 * the configuration information for SMARTCARD module. | |
603 * @param pData: pointer to data buffer | |
604 * @param Size: amount of data to be sent | |
605 * @retval HAL status | |
606 */ | |
607 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size) | |
608 { | |
609 uint32_t tmp1 = 0; | |
610 | |
611 tmp1 = hsc->State; | |
612 if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_RX)) | |
613 { | |
614 if((pData == NULL) || (Size == 0)) | |
615 { | |
616 return HAL_ERROR; | |
617 } | |
618 | |
619 /* Process Locked */ | |
620 __HAL_LOCK(hsc); | |
621 | |
622 hsc->pTxBuffPtr = pData; | |
623 hsc->TxXferSize = Size; | |
624 hsc->TxXferCount = Size; | |
625 | |
626 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; | |
627 /* Check if a non-blocking receive process is ongoing or not */ | |
628 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) | |
629 { | |
630 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX; | |
631 } | |
632 else | |
633 { | |
634 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX; | |
635 } | |
636 | |
637 /* Process Unlocked */ | |
638 __HAL_UNLOCK(hsc); | |
639 | |
640 /* Enable the SMARTCARD Parity Error Interrupt */ | |
641 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE); | |
642 | |
643 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ | |
644 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR); | |
645 | |
646 /* Enable the SMARTCARD Transmit data register empty Interrupt */ | |
647 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TXE); | |
648 | |
649 return HAL_OK; | |
650 } | |
651 else | |
652 { | |
653 return HAL_BUSY; | |
654 } | |
655 } | |
656 | |
657 /** | |
658 * @brief Receive an amount of data in non blocking mode | |
659 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
660 * the configuration information for SMARTCARD module. | |
661 * @param pData: pointer to data buffer | |
662 * @param Size: amount of data to be received | |
663 * @retval HAL status | |
664 */ | |
665 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size) | |
666 { | |
667 uint32_t tmp1 = 0; | |
668 | |
669 tmp1 = hsc->State; | |
670 if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX)) | |
671 { | |
672 if((pData == NULL) || (Size == 0)) | |
673 { | |
674 return HAL_ERROR; | |
675 } | |
676 | |
677 /* Process Locked */ | |
678 __HAL_LOCK(hsc); | |
679 | |
680 hsc->pRxBuffPtr = pData; | |
681 hsc->RxXferSize = Size; | |
682 hsc->RxXferCount = Size; | |
683 | |
684 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; | |
685 /* Check if a non-blocking transmit process is ongoing or not */ | |
686 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) | |
687 { | |
688 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX; | |
689 } | |
690 else | |
691 { | |
692 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX; | |
693 } | |
694 /* Process Unlocked */ | |
695 __HAL_UNLOCK(hsc); | |
696 | |
697 /* Enable the SMARTCARD Data Register not empty Interrupt */ | |
698 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE); | |
699 | |
700 /* Enable the SMARTCARD Parity Error Interrupt */ | |
701 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE); | |
702 | |
703 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ | |
704 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR); | |
705 | |
706 return HAL_OK; | |
707 } | |
708 else | |
709 { | |
710 return HAL_BUSY; | |
711 } | |
712 } | |
713 | |
714 /** | |
715 * @brief Send an amount of data in non blocking mode | |
716 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
717 * the configuration information for SMARTCARD module. | |
718 * @param pData: pointer to data buffer | |
719 * @param Size: amount of data to be sent | |
720 * @retval HAL status | |
721 */ | |
722 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size) | |
723 { | |
724 uint32_t *tmp; | |
725 uint32_t tmp1 = 0; | |
726 | |
727 tmp1 = hsc->State; | |
728 if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_RX)) | |
729 { | |
730 if((pData == NULL) || (Size == 0)) | |
731 { | |
732 return HAL_ERROR; | |
733 } | |
734 | |
735 /* Process Locked */ | |
736 __HAL_LOCK(hsc); | |
737 | |
738 hsc->pTxBuffPtr = pData; | |
739 hsc->TxXferSize = Size; | |
740 hsc->TxXferCount = Size; | |
741 | |
742 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; | |
743 /* Check if a non-blocking receive process is ongoing or not */ | |
744 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) | |
745 { | |
746 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX; | |
747 } | |
748 else | |
749 { | |
750 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX; | |
751 } | |
752 | |
753 /* Set the SMARTCARD DMA transfer complete callback */ | |
754 hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt; | |
755 | |
756 /* Set the DMA error callback */ | |
757 hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError; | |
758 | |
759 /* Enable the SMARTCARD transmit DMA Stream */ | |
760 tmp = (uint32_t*)&pData; | |
761 HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size); | |
762 | |
763 /* Clear the TC flag in the SR register by writing 0 to it */ | |
764 __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC); | |
765 | |
766 /* Enable the DMA transfer for transmit request by setting the DMAT bit | |
767 in the SMARTCARD CR3 register */ | |
768 hsc->Instance->CR3 |= USART_CR3_DMAT; | |
769 | |
770 /* Process Unlocked */ | |
771 __HAL_UNLOCK(hsc); | |
772 | |
773 return HAL_OK; | |
774 } | |
775 else | |
776 { | |
777 return HAL_BUSY; | |
778 } | |
779 } | |
780 | |
781 /** | |
782 * @brief Receive an amount of data in non blocking mode | |
783 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
784 * the configuration information for SMARTCARD module. | |
785 * @param pData: pointer to data buffer | |
786 * @param Size: amount of data to be received | |
787 * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s | |
788 * @retval HAL status | |
789 */ | |
790 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size) | |
791 { | |
792 uint32_t *tmp; | |
793 uint32_t tmp1 = 0; | |
794 | |
795 tmp1 = hsc->State; | |
796 if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX)) | |
797 { | |
798 if((pData == NULL) || (Size == 0)) | |
799 { | |
800 return HAL_ERROR; | |
801 } | |
802 | |
803 /* Process Locked */ | |
804 __HAL_LOCK(hsc); | |
805 | |
806 hsc->pRxBuffPtr = pData; | |
807 hsc->RxXferSize = Size; | |
808 | |
809 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE; | |
810 /* Check if a non-blocking transmit process is ongoing or not */ | |
811 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) | |
812 { | |
813 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX; | |
814 } | |
815 else | |
816 { | |
817 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX; | |
818 } | |
819 | |
820 /* Set the SMARTCARD DMA transfer complete callback */ | |
821 hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt; | |
822 | |
823 /* Set the DMA error callback */ | |
824 hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError; | |
825 | |
826 /* Enable the DMA Stream */ | |
827 tmp = (uint32_t*)&pData; | |
828 HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size); | |
829 | |
830 /* Enable the DMA transfer for the receiver request by setting the DMAR bit | |
831 in the SMARTCARD CR3 register */ | |
832 hsc->Instance->CR3 |= USART_CR3_DMAR; | |
833 | |
834 /* Process Unlocked */ | |
835 __HAL_UNLOCK(hsc); | |
836 | |
837 return HAL_OK; | |
838 } | |
839 else | |
840 { | |
841 return HAL_BUSY; | |
842 } | |
843 } | |
844 | |
845 /** | |
846 * @brief This function handles SMARTCARD interrupt request. | |
847 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
848 * the configuration information for SMARTCARD module. | |
849 * @retval None | |
850 */ | |
851 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc) | |
852 { | |
853 uint32_t tmp1 = 0, tmp2 = 0; | |
854 | |
855 tmp1 = hsc->Instance->SR; | |
856 tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_PE); | |
857 | |
858 /* SMARTCARD parity error interrupt occurred --------------------------------*/ | |
859 if(((tmp1 & SMARTCARD_FLAG_PE) != RESET) && (tmp2 != RESET)) | |
860 { | |
861 __HAL_SMARTCARD_CLEAR_PEFLAG(hsc); | |
862 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE; | |
863 } | |
864 | |
865 tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR); | |
866 /* SMARTCARD frame error interrupt occurred ---------------------------------*/ | |
867 if(((tmp1 & SMARTCARD_FLAG_FE) != RESET) && (tmp2 != RESET)) | |
868 { | |
869 __HAL_SMARTCARD_CLEAR_FEFLAG(hsc); | |
870 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE; | |
871 } | |
872 | |
873 tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR); | |
874 /* SMARTCARD noise error interrupt occurred ---------------------------------*/ | |
875 if(((tmp1 & SMARTCARD_FLAG_NE) != RESET) && (tmp2 != RESET)) | |
876 { | |
877 __HAL_SMARTCARD_CLEAR_NEFLAG(hsc); | |
878 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE; | |
879 } | |
880 | |
881 tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR); | |
882 /* SMARTCARD Over-Run interrupt occurred ------------------------------------*/ | |
883 if(((tmp1 & SMARTCARD_FLAG_ORE) != RESET) && (tmp2 != RESET)) | |
884 { | |
885 __HAL_SMARTCARD_CLEAR_OREFLAG(hsc); | |
886 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE; | |
887 } | |
888 | |
889 tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RXNE); | |
890 /* SMARTCARD in mode Receiver ----------------------------------------------*/ | |
891 if(((tmp1 & SMARTCARD_FLAG_RXNE) != RESET) && (tmp2 != RESET)) | |
892 { | |
893 SMARTCARD_Receive_IT(hsc); | |
894 } | |
895 | |
896 tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TXE); | |
897 /* SMARTCARD in mode Transmitter -------------------------------------------*/ | |
898 if(((tmp1 & SMARTCARD_FLAG_TXE) != RESET) && (tmp2 != RESET)) | |
899 { | |
900 SMARTCARD_Transmit_IT(hsc); | |
901 } | |
902 | |
903 tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TC); | |
904 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/ | |
905 if((tmp1 != RESET) && (tmp2 != RESET)) | |
906 { | |
907 SMARTCARD_EndTransmit_IT(hsc); | |
908 } | |
909 | |
910 /* Call the Error call Back in case of Errors */ | |
911 if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE) | |
912 { | |
913 /* Set the SMARTCARD state ready to be able to start again the process */ | |
914 hsc->State= HAL_SMARTCARD_STATE_READY; | |
915 HAL_SMARTCARD_ErrorCallback(hsc); | |
916 } | |
917 } | |
918 | |
919 /** | |
920 * @brief Tx Transfer completed 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_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc) | |
926 { | |
927 /* NOTE : This function Should not be modified, when the callback is needed, | |
928 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file | |
929 */ | |
930 } | |
931 | |
932 /** | |
933 * @brief Rx Transfer completed callbacks | |
934 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
935 * the configuration information for SMARTCARD module. | |
936 * @retval None | |
937 */ | |
938 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc) | |
939 { | |
940 /* NOTE : This function Should not be modified, when the callback is needed, | |
941 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file | |
942 */ | |
943 } | |
944 | |
945 /** | |
946 * @brief SMARTCARD error callbacks | |
947 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
948 * the configuration information for SMARTCARD module. | |
949 * @retval None | |
950 */ | |
951 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc) | |
952 { | |
953 /* NOTE : This function Should not be modified, when the callback is needed, | |
954 the HAL_SMARTCARD_ErrorCallback could be implemented in the user file | |
955 */ | |
956 } | |
957 | |
958 /** | |
959 * @} | |
960 */ | |
961 | |
962 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions | |
963 * @brief SMARTCARD State and Errors functions | |
964 * | |
965 @verbatim | |
966 =============================================================================== | |
967 ##### Peripheral State and Errors functions ##### | |
968 =============================================================================== | |
969 [..] | |
970 This subsection provides a set of functions allowing to control the SmartCard. | |
971 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral. | |
972 (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication. | |
973 @endverbatim | |
974 * @{ | |
975 */ | |
976 | |
977 /** | |
978 * @brief return the SMARTCARD state | |
979 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
980 * the configuration information for SMARTCARD module. | |
981 * @retval HAL state | |
982 */ | |
983 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc) | |
984 { | |
985 return hsc->State; | |
986 } | |
987 | |
988 /** | |
989 * @brief Return the SMARTCARD error code | |
990 * @param hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains | |
991 * the configuration information for the specified SMARTCARD. | |
992 * @retval SMARTCARD Error Code | |
993 */ | |
994 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc) | |
995 { | |
996 return hsc->ErrorCode; | |
997 } | |
998 | |
999 /** | |
1000 * @} | |
1001 */ | |
1002 | |
1003 /** | |
1004 * @brief DMA SMARTCARD transmit process complete callback | |
1005 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains | |
1006 * the configuration information for the specified DMA module. | |
1007 * @retval None | |
1008 */ | |
1009 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma) | |
1010 { | |
1011 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; | |
1012 | |
1013 hsc->TxXferCount = 0; | |
1014 | |
1015 /* Disable the DMA transfer for transmit request by setting the DMAT bit | |
1016 in the USART CR3 register */ | |
1017 hsc->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT); | |
1018 | |
1019 /* Enable the SMARTCARD Transmit Complete Interrupt */ | |
1020 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC); | |
1021 } | |
1022 | |
1023 /** | |
1024 * @brief DMA SMARTCARD receive process complete callback | |
1025 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains | |
1026 * the configuration information for the specified DMA module. | |
1027 * @retval None | |
1028 */ | |
1029 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma) | |
1030 { | |
1031 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; | |
1032 | |
1033 hsc->RxXferCount = 0; | |
1034 | |
1035 /* Disable the DMA transfer for the receiver request by setting the DMAR bit | |
1036 in the USART CR3 register */ | |
1037 hsc->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR); | |
1038 | |
1039 /* Check if a non-blocking transmit process is ongoing or not */ | |
1040 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) | |
1041 { | |
1042 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX; | |
1043 } | |
1044 else | |
1045 { | |
1046 hsc->State = HAL_SMARTCARD_STATE_READY; | |
1047 } | |
1048 | |
1049 HAL_SMARTCARD_RxCpltCallback(hsc); | |
1050 } | |
1051 | |
1052 /** | |
1053 * @brief DMA SMARTCARD communication error callback | |
1054 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains | |
1055 * the configuration information for the specified DMA module. | |
1056 * @retval None | |
1057 */ | |
1058 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma) | |
1059 { | |
1060 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; | |
1061 | |
1062 hsc->RxXferCount = 0; | |
1063 hsc->TxXferCount = 0; | |
1064 hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA; | |
1065 hsc->State= HAL_SMARTCARD_STATE_READY; | |
1066 | |
1067 HAL_SMARTCARD_ErrorCallback(hsc); | |
1068 } | |
1069 | |
1070 /** | |
1071 * @brief This function handles SMARTCARD Communication Timeout. | |
1072 * @param hsc: SMARTCARD handle | |
1073 * @param Flag: specifies the SMARTCARD flag to check. | |
1074 * @param Status: The new Flag status (SET or RESET). | |
1075 * @param Timeout: Timeout duration | |
1076 * @retval HAL status | |
1077 */ | |
1078 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout) | |
1079 { | |
1080 uint32_t tickstart = 0; | |
1081 | |
1082 /* Get tick */ | |
1083 tickstart = HAL_GetTick(); | |
1084 | |
1085 /* Wait until flag is set */ | |
1086 if(Status == RESET) | |
1087 { | |
1088 while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) == RESET) | |
1089 { | |
1090 /* Check for the Timeout */ | |
1091 if(Timeout != HAL_MAX_DELAY) | |
1092 { | |
1093 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout)) | |
1094 { | |
1095 /* Disable TXE and RXNE interrupts for the interrupt process */ | |
1096 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE); | |
1097 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE); | |
1098 | |
1099 hsc->State= HAL_SMARTCARD_STATE_READY; | |
1100 | |
1101 /* Process Unlocked */ | |
1102 __HAL_UNLOCK(hsc); | |
1103 | |
1104 return HAL_TIMEOUT; | |
1105 } | |
1106 } | |
1107 } | |
1108 } | |
1109 else | |
1110 { | |
1111 while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) != RESET) | |
1112 { | |
1113 /* Check for the Timeout */ | |
1114 if(Timeout != HAL_MAX_DELAY) | |
1115 { | |
1116 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout)) | |
1117 { | |
1118 /* Disable TXE and RXNE interrupts for the interrupt process */ | |
1119 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE); | |
1120 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE); | |
1121 | |
1122 hsc->State= HAL_SMARTCARD_STATE_READY; | |
1123 | |
1124 /* Process Unlocked */ | |
1125 __HAL_UNLOCK(hsc); | |
1126 | |
1127 return HAL_TIMEOUT; | |
1128 } | |
1129 } | |
1130 } | |
1131 } | |
1132 return HAL_OK; | |
1133 } | |
1134 | |
1135 /** | |
1136 * @brief Send an amount of data in non blocking mode | |
1137 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
1138 * the configuration information for SMARTCARD module. | |
1139 * @retval HAL status | |
1140 */ | |
1141 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc) | |
1142 { | |
1143 uint16_t* tmp; | |
1144 uint32_t tmp1 = 0; | |
1145 | |
1146 tmp1 = hsc->State; | |
1147 if((tmp1 == HAL_SMARTCARD_STATE_BUSY_TX) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX_RX)) | |
1148 { | |
1149 if(hsc->Init.WordLength == SMARTCARD_WORDLENGTH_9B) | |
1150 { | |
1151 tmp = (uint16_t*) hsc->pTxBuffPtr; | |
1152 hsc->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); | |
1153 if(hsc->Init.Parity == SMARTCARD_PARITY_NONE) | |
1154 { | |
1155 hsc->pTxBuffPtr += 2; | |
1156 } | |
1157 else | |
1158 { | |
1159 hsc->pTxBuffPtr += 1; | |
1160 } | |
1161 } | |
1162 else | |
1163 { | |
1164 hsc->Instance->DR = (uint8_t)(*hsc->pTxBuffPtr++ & (uint8_t)0x00FF); | |
1165 } | |
1166 | |
1167 if(--hsc->TxXferCount == 0) | |
1168 { | |
1169 /* Disable the SMARTCARD Transmit data register empty Interrupt */ | |
1170 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE); | |
1171 | |
1172 /* Enable the SMARTCARD Transmit Complete Interrupt */ | |
1173 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC); | |
1174 } | |
1175 | |
1176 return HAL_OK; | |
1177 } | |
1178 else | |
1179 { | |
1180 return HAL_BUSY; | |
1181 } | |
1182 } | |
1183 | |
1184 /** | |
1185 * @brief Wraps up transmission in non blocking mode. | |
1186 * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
1187 * the configuration information for the specified SMARTCARD module. | |
1188 * @retval HAL status | |
1189 */ | |
1190 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard) | |
1191 { | |
1192 /* Disable the SMARTCARD Transmit Complete Interrupt */ | |
1193 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TC); | |
1194 | |
1195 /* Check if a receive process is ongoing or not */ | |
1196 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) | |
1197 { | |
1198 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX; | |
1199 } | |
1200 else | |
1201 { | |
1202 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ | |
1203 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR); | |
1204 | |
1205 hsmartcard->State = HAL_SMARTCARD_STATE_READY; | |
1206 } | |
1207 | |
1208 HAL_SMARTCARD_TxCpltCallback(hsmartcard); | |
1209 | |
1210 return HAL_OK; | |
1211 } | |
1212 | |
1213 /** | |
1214 * @brief Receive an amount of data in non blocking mode | |
1215 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
1216 * the configuration information for SMARTCARD module. | |
1217 * @retval HAL status | |
1218 */ | |
1219 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc) | |
1220 { | |
1221 uint16_t* tmp; | |
1222 uint32_t tmp1 = 0; | |
1223 | |
1224 tmp1 = hsc->State; | |
1225 if((tmp1 == HAL_SMARTCARD_STATE_BUSY_RX) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX_RX)) | |
1226 { | |
1227 if(hsc->Init.WordLength == SMARTCARD_WORDLENGTH_9B) | |
1228 { | |
1229 tmp = (uint16_t*) hsc->pRxBuffPtr; | |
1230 if(hsc->Init.Parity == SMARTCARD_PARITY_NONE) | |
1231 { | |
1232 *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x01FF); | |
1233 hsc->pRxBuffPtr += 2; | |
1234 } | |
1235 else | |
1236 { | |
1237 *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x00FF); | |
1238 hsc->pRxBuffPtr += 1; | |
1239 } | |
1240 } | |
1241 else | |
1242 { | |
1243 if(hsc->Init.Parity == SMARTCARD_PARITY_NONE) | |
1244 { | |
1245 *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF); | |
1246 } | |
1247 else | |
1248 { | |
1249 *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x007F); | |
1250 } | |
1251 } | |
1252 | |
1253 if(--hsc->RxXferCount == 0) | |
1254 { | |
1255 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE); | |
1256 | |
1257 /* Disable the SMARTCARD Parity Error Interrupt */ | |
1258 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE); | |
1259 | |
1260 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ | |
1261 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR); | |
1262 | |
1263 /* Check if a non-blocking transmit process is ongoing or not */ | |
1264 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX) | |
1265 { | |
1266 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX; | |
1267 } | |
1268 else | |
1269 { | |
1270 hsc->State = HAL_SMARTCARD_STATE_READY; | |
1271 } | |
1272 | |
1273 HAL_SMARTCARD_RxCpltCallback(hsc); | |
1274 | |
1275 return HAL_OK; | |
1276 } | |
1277 return HAL_OK; | |
1278 } | |
1279 else | |
1280 { | |
1281 return HAL_BUSY; | |
1282 } | |
1283 } | |
1284 | |
1285 /** | |
1286 * @brief Configure the SMARTCARD peripheral | |
1287 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains | |
1288 * the configuration information for SMARTCARD module. | |
1289 * @retval None | |
1290 */ | |
1291 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc) | |
1292 { | |
1293 uint32_t tmpreg = 0x00; | |
1294 | |
1295 /* Check the parameters */ | |
1296 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance)); | |
1297 assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity)); | |
1298 assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase)); | |
1299 assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit)); | |
1300 assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate)); | |
1301 assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength)); | |
1302 assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits)); | |
1303 assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity)); | |
1304 assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode)); | |
1305 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState)); | |
1306 | |
1307 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the | |
1308 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */ | |
1309 hsc->Instance->CR1 &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE)); | |
1310 | |
1311 /*---------------------------- USART CR2 Configuration ---------------------*/ | |
1312 tmpreg = hsc->Instance->CR2; | |
1313 /* Clear CLKEN, CPOL, CPHA and LBCL bits */ | |
1314 tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL)); | |
1315 /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/ | |
1316 /* Set CPOL bit according to hsc->Init.CLKPolarity value */ | |
1317 /* Set CPHA bit according to hsc->Init.CLKPhase value */ | |
1318 /* Set LBCL bit according to hsc->Init.CLKLastBit value */ | |
1319 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */ | |
1320 tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity | | |
1321 hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits); | |
1322 /* Write to USART CR2 */ | |
1323 hsc->Instance->CR2 = (uint32_t)tmpreg; | |
1324 | |
1325 tmpreg = hsc->Instance->CR2; | |
1326 | |
1327 /* Clear STOP[13:12] bits */ | |
1328 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP); | |
1329 | |
1330 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */ | |
1331 tmpreg |= (uint32_t)(hsc->Init.StopBits); | |
1332 | |
1333 /* Write to USART CR2 */ | |
1334 hsc->Instance->CR2 = (uint32_t)tmpreg; | |
1335 | |
1336 /*-------------------------- USART CR1 Configuration -----------------------*/ | |
1337 tmpreg = hsc->Instance->CR1; | |
1338 | |
1339 /* Clear M, PCE, PS, TE and RE bits */ | |
1340 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \ | |
1341 USART_CR1_RE)); | |
1342 | |
1343 /* Configure the SMARTCARD Word Length, Parity and mode: | |
1344 Set the M bits according to hsc->Init.WordLength value | |
1345 Set PCE and PS bits according to hsc->Init.Parity value | |
1346 Set TE and RE bits according to hsc->Init.Mode value */ | |
1347 tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode; | |
1348 | |
1349 /* Write to USART CR1 */ | |
1350 hsc->Instance->CR1 = (uint32_t)tmpreg; | |
1351 | |
1352 /*-------------------------- USART CR3 Configuration -----------------------*/ | |
1353 /* Clear CTSE and RTSE bits */ | |
1354 hsc->Instance->CR3 &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE)); | |
1355 | |
1356 /*-------------------------- USART BRR Configuration -----------------------*/ | |
1357 if((hsc->Instance == USART1) || (hsc->Instance == USART6)) | |
1358 { | |
1359 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate); | |
1360 } | |
1361 else | |
1362 { | |
1363 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate); | |
1364 } | |
1365 } | |
1366 | |
1367 /** | |
1368 * @} | |
1369 */ | |
1370 | |
1371 #endif /* HAL_SMARTCARD_MODULE_ENABLED */ | |
1372 /** | |
1373 * @} | |
1374 */ | |
1375 | |
1376 /** | |
1377 * @} | |
1378 */ | |
1379 | |
1380 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |