Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "smartcard.h"
Global variables definition and initialization
SC_A2R
SC_ATR_Table
SCData
F_Table
D_Table
SCHandle
--------------APDU
------------ ATR
Physical Port Layer
SC_Handler(SC_State *, SC_ADPU_Commands *, SC_ADPU_Response *)
SC_PowerCmd(FunctionalState)
SC_Reset(GPIO_PinState)
SC_ParityErrorHandler()
SC_PTSConfig()
SC_SendData(SC_ADPU_Commands *, SC_ADPU_Response *)
SC_AnswerReq(SC_State *, uint8_t *, uint8_t)
SC_decode_Answer2reset(uint8_t *)
SC_Init()
SC_DeInit()
SC_VoltageConfig(uint32_t)
SC_IOConfig()
SC_Detect()
Files
loading...
CodeScopeSTM32 Libraries and SamplesSMARTCARD_T0Src/smartcard.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file SMARTCARD/SMARTCARD_T0/Src/smartcard.c * @author MCD Application Team * @brief This file provides all the Smartcard firmware functions. ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /** @addtogroup STM32F4xx_HAL_Examples * @{ *//* ... */ /** @addtogroup SMARTCARD_T0 * @{ *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "smartcard.h" Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Global variables definition and initialization ----------------------------*/ SC_ATR SC_A2R; uint8_t SC_ATR_Table[40] = {0}; static __IO uint8_t SCData = 0; static uint32_t F_Table[16] = {372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0...}; static uint32_t D_Table[16] = {0, 1, 2, 4, 8, 16, 32, 0, 12, 20, 0, 0, 0, 0, 0, 0}; SMARTCARD_HandleTypeDef SCHandle; Global variables definition and initialization /* Private function prototypes -----------------------------------------------*/ /* Transport Layer -----------------------------------------------------------*/ /*--------------APDU-----------*/ static void SC_SendData(SC_ADPU_Commands *SC_ADPU, SC_ADPU_Response *SC_ResponseStatus); --------------APDU /*------------ ATR ------------*/ static void SC_AnswerReq(SC_State *SCState, uint8_t *card, uint8_t length); /* Ask ATR */ static uint8_t SC_decode_Answer2reset(uint8_t *card); /* Decode ATR */ ------------ ATR /* Physical Port Layer -------------------------------------------------------*/ static void SC_Init(void); static void SC_DeInit(void); static void SC_VoltageConfig(uint32_t SC_Voltage); static uint8_t SC_Detect(void); Physical Port Layer /* Private functions ---------------------------------------------------------*/ /** * @brief Handles all Smartcard states and serves to send and receive all * communication data between Smartcard and reader. * @param SCState: pointer to an SC_State enumeration that will contain the * Smartcard state. * @param SC_ADPU: pointer to an SC_ADPU_Commands structure that will be initialized. * @param SC_Response: pointer to a SC_ADPU_Response structure which will be initialized. * @retval None *//* ... */ void SC_Handler(SC_State *SCState, SC_ADPU_Commands *SC_ADPU, SC_ADPU_Response *SC_Response) { uint32_t i = 0; switch(*SCState) { case SC_POWER_ON: if (SC_ADPU->Header.INS == SC_GET_A2R) { /* Smartcard initialization --------------------------------------------*/ SC_Init(); Smartcard initialization /* Reset Data from SC buffer -------------------------------------------*/ for (i = 0; i < 40; i++) { SC_ATR_Table[i] = 0; }for (i = 0; i < 40; i++) { ... } Reset Data from SC buffer /* Reset SC_A2R Structure ----------------------------------------------*/ SC_A2R.TS = 0; SC_A2R.T0 = 0; for (i = 0; i < SETUP_LENGTH; i++) { SC_A2R.T[i] = 0; }for (i = 0; i < SETUP_LENGTH; i++) { ... } for (i = 0; i < HIST_LENGTH; i++) { SC_A2R.H[i] = 0; }for (i = 0; i < HIST_LENGTH; i++) { ... } SC_A2R.Tlength = 0; SC_A2R.Hlength = 0; Reset SC_A2R Structure /* Next State ----------------------------------------------------------*/ *SCState = SC_RESET_LOW; }if (SC_ADPU->Header.INS == SC_GET_A2R) { ... } break; case SC_POWER_ON: case SC_RESET_LOW: if(SC_ADPU->Header.INS == SC_GET_A2R) { /* If card is detected then Power ON, Card Reset and wait for an answer) */ if (SC_Detect()) { while(((*SCState) != SC_POWER_OFF) && ((*SCState) != SC_ACTIVE)) { SC_AnswerReq(SCState, (uint8_t *)&SC_ATR_Table, 40); /* Check for answer to reset */ }while (((*SCState) != SC_POWER_OFF) && ((*SCState) != SC_ACTIVE)) { ... } }if (SC_Detect()) { ... } else { (*SCState) = SC_POWER_OFF; }else { ... } }if (SC_ADPU->Header.INS == SC_GET_A2R) { ... } break; case SC_RESET_LOW: case SC_ACTIVE: if (SC_ADPU->Header.INS == SC_GET_A2R) { if(SC_decode_Answer2reset(&SC_ATR_Table[0]) == T0_PROTOCOL) { (*SCState) = SC_ACTIVE_ON_T0; }if (SC_decode_Answer2reset(&SC_ATR_Table[0]) == T0_PROTOCOL) { ... } else { (*SCState) = SC_POWER_OFF; }else { ... } }if (SC_ADPU->Header.INS == SC_GET_A2R) { ... } break; case SC_ACTIVE: case SC_ACTIVE_ON_T0: SC_SendData(SC_ADPU, SC_Response); break; case SC_ACTIVE_ON_T0: case SC_POWER_OFF: SC_DeInit(); /* Disable Smartcard interface */ break; case SC_POWER_OFF: default: (*SCState) = SC_POWER_OFF; }switch (*SCState) { ... } }{ ... } /** * @brief Enables or disables the power to the Smartcard. * @param NewState: new state of the Smartcard power supply. * This parameter can be: ENABLE or DISABLE. * @retval None *//* ... */ void SC_PowerCmd(FunctionalState NewState) { if(NewState != DISABLE) { HAL_GPIO_WritePin(SC_CMDVCC_GPIO_PORT, SC_CMDVCC_PIN, GPIO_PIN_RESET); }if (NewState != DISABLE) { ... } else { HAL_GPIO_WritePin(SC_CMDVCC_GPIO_PORT, SC_CMDVCC_PIN, GPIO_PIN_SET); }else { ... } }{ ... } /** * @brief Sets or clears the Smartcard reset pin. * @param ResetState: this parameter specifies the state of the Smartcard * reset pin. BitVal must be one of the GPIO_PinState enum values: * @arg GPIO_PIN_RESET: to clear the port pin. * @arg GPIO_PIN_SET: to set the port pin. * @retval None *//* ... */ void SC_Reset(GPIO_PinState ResetState) { HAL_GPIO_WritePin(SC_RESET_GPIO_PORT, SC_RESET_PIN, ResetState); }{ ... } /** * @brief Resends the byte that failed to be received (by the Smartcard) correctly. * @param None * @retval None *//* ... */ void SC_ParityErrorHandler(void) { HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)&SCData, 1, SC_TRANSMIT_TIMEOUT); }{ ... } /** * @brief Configures the IO speed (BaudRate) communication. * @param None * @retval None *//* ... */ void SC_PTSConfig(void) { uint32_t workingbaudrate = 0, apbclock = 0; uint8_t PPSConfirmStatus = 1; uint8_t ptscmd[4] = {0x00}; uint8_t ptsanswer[4] = {0x00}; /* Reconfigure the USART Baud Rate -----------------------------------------*/ apbclock = HAL_RCC_GetPCLK2Freq(); apbclock /= ((SC_USART->GTPR & (uint16_t)0x00FF) * 2); /* Enable the DMA Receive (Set DMAR bit only) to enable interrupt generation in case of a framing error FE *//* ... */ __HAL_SMARTCARD_DMA_REQUEST_ENABLE(&SCHandle, SMARTCARD_DMAREQ_RX); if((SC_A2R.T0 & (uint8_t)0x10) == 0x10) { if(SC_A2R.T[0] != 0x11) { /* PPSS identifies the PPS request or response and is equal to 0xFF */ ptscmd[0] = 0xFF; /* PPS0 indicates by the bits b5, b6, b7 equal to 1 the presence of the optional bytes PPSI1, PPS2, PPS3 respectively *//* ... */ ptscmd[1] = 0x10; /* PPS1 allows the interface device to propose value of F and D to the card */ ptscmd[2] = SC_A2R.T[0]; /* PCK check character */ ptscmd[3] = (uint8_t)0xFF^(uint8_t)0x10^(uint8_t)SC_A2R.T[0]; /* Start the receive IT process: to receive the command answer from the card */ HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)ptsanswer, 4); /* Send command */ HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)ptscmd, 4, SC_TRANSMIT_TIMEOUT); /* Disable the DMA Receive Request */ __HAL_SMARTCARD_DMA_REQUEST_DISABLE(&SCHandle, SMARTCARD_DMAREQ_RX); /* Wait until receiving the answer from the card */ while(HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) {}while (HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) { ... } /* Check the received command answer */ if((ptsanswer[0] != 0xFF) || (ptsanswer[1] != 0x10) || (ptsanswer[2] != SC_A2R.T[0]) || (ptsanswer[3] != ((uint8_t)0xFF^(uint8_t)0x10^(uint8_t)SC_A2R.T[0]))) { /* PPSS, PPS0, PPS1 and PCK exchange unsuccessful */ PPSConfirmStatus = 0x00; }if ((ptsanswer[0] != 0xFF) || (ptsanswer[1] != 0x10) || (ptsanswer[2] != SC_A2R.T[0]) || (ptsanswer[3] != ((uint8_t)0xFF^(uint8_t)0x10^(uint8_t)SC_A2R.T[0]))) { ... } /* PPS exchange successful */ if(PPSConfirmStatus == 0x01) { workingbaudrate = apbclock * D_Table[(SC_A2R.T[0] & (uint8_t)0x0F)]; workingbaudrate /= F_Table[((SC_A2R.T[0] >> 4) & (uint8_t)0x0F)]; SCHandle.Init.BaudRate = workingbaudrate; SCHandle.Init.WordLength = SMARTCARD_WORDLENGTH_9B; SCHandle.Init.StopBits = SMARTCARD_STOPBITS_1_5; SCHandle.Init.Parity = SMARTCARD_PARITY_EVEN; SCHandle.Init.Mode = SMARTCARD_MODE_TX_RX; SCHandle.Init.CLKPolarity = SMARTCARD_POLARITY_LOW; SCHandle.Init.CLKPhase = SMARTCARD_PHASE_1EDGE; SCHandle.Init.CLKLastBit = SMARTCARD_LASTBIT_ENABLE; SCHandle.Init.Prescaler = 12; SCHandle.Init.GuardTime = 16; SCHandle.Init.NACKState = SMARTCARD_NACK_ENABLE; SCHandle.Instance = SC_USART; if(HAL_SMARTCARD_Init(&SCHandle) != HAL_OK) { while (1); }if (HAL_SMARTCARD_Init(&SCHandle) != HAL_OK) { ... } }if (PPSConfirmStatus == 0x01) { ... } }if (SC_A2R.T[0] != 0x11) { ... } }if ((SC_A2R.T0 & (uint8_t)0x10) == 0x10) { ... } }{ ... } /** * @brief Manages the Smartcard transport layer: send APDU commands and receives * the APDU response. * @param SC_ADPU: pointer to a SC_ADPU_Commands structure which will be initialized. * @param SC_ADPU_Response: pointer to a SC_ADPU_Response structure which will be initialized. * @retval None *//* ... */ static void SC_SendData(SC_ADPU_Commands *SC_ADPU, SC_ADPU_Response *SC_ResponseStatus) { uint32_t i = 0; uint8_t command[5] = {0x00}; uint8_t answer[40] = {0x00}; /* Reset response buffer ---------------------------------------------------*/ for(i = 0; i < LC_MAX; i++) { SC_ResponseStatus->Data[i] = 0; }for (i = 0; i < LC_MAX; i++) { ... } SC_ResponseStatus->SW1 = 0; SC_ResponseStatus->SW2 = 0; /* Enable the DMA Receive (Set DMAR bit only) to enable interrupt generation in case of a framing error FE *//* ... */ __HAL_SMARTCARD_DMA_REQUEST_ENABLE(&SCHandle, SMARTCARD_DMAREQ_RX); Reset response buffer /* Send header -------------------------------------------------------------*/ command[0] = SC_ADPU->Header.CLA; command[1] = SC_ADPU->Header.INS; command[2] = SC_ADPU->Header.P1; command[3] = SC_ADPU->Header.P2; Send header /* Send body length to/from SC ---------------------------------------------*/ if(SC_ADPU->Body.LC) { command[4] = SC_ADPU->Body.LC; HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)command, 5, SC_TRANSMIT_TIMEOUT); }if (SC_ADPU->Body.LC) { ... } else if(SC_ADPU->Body.LE) { command[4] = SC_ADPU->Body.LE; HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)command, 5, SC_TRANSMIT_TIMEOUT); }else if (SC_ADPU->Body.LE) { ... } /* Flush the SC_USART DR */ __HAL_SMARTCARD_FLUSH_DRREGISTER(&SCHandle); /* Start the receive IT process: to receive the command answer from the card */ HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[0], 1); /* Wait until receiving the answer from the card */ while(HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) {}while (HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) { ... } /* -------------------------------------------------------- Wait Procedure byte from card: 1 - ACK 2 - NULL 3 - SW1; SW2 -------------------------------------------------------- *//* ... */ if(((answer[0] & (uint8_t)0xF0) == 0x60) || ((answer[0] & (uint8_t)0xF0) == 0x90)) { /* SW1 received */ SC_ResponseStatus->SW1 = answer[0]; if(HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[1], 1) == HAL_OK) { /* SW2 received */ SC_ResponseStatus->SW2 = answer[1]; }if (HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[1], 1) == HAL_OK) { ... } }if (((answer[0] & (uint8_t)0xF0) == 0x60) || ((answer[0] & (uint8_t)0xF0) == 0x90)) { ... } else if(((answer[0] & (uint8_t)0xFE) == (((uint8_t)~(SC_ADPU->Header.INS)) & (uint8_t)0xFE)) || \ ((answer[0] & (uint8_t)0xFE) == (SC_ADPU->Header.INS & (uint8_t)0xFE))) { SC_ResponseStatus->Data[0] = answer[0];/* ACK received */ }else if (((answer[0] & (uint8_t)0xFE) == (((uint8_t)~(SC_ADPU->Header.INS)) & (uint8_t)0xFE)) || \ ((answer[0] & (uint8_t)0xFE) == (SC_ADPU->Header.INS & (uint8_t)0xFE))) { ... } Send body length to/from SC /* If no status bytes received ---------------------------------------------*/ if(SC_ResponseStatus->SW1 == 0x00) { /* Send body data to SC --------------------------------------------------*/ if(SC_ADPU->Body.LC) { /* Send body data */ HAL_SMARTCARD_Transmit(&SCHandle, (uint8_t *)&SC_ADPU->Body.Data[0], SC_ADPU->Body.LC, SC_TRANSMIT_TIMEOUT); /* Flush the SC_USART DR */ __HAL_SMARTCARD_FLUSH_DRREGISTER(&SCHandle); /* Disable the DMA Receive (Reset DMAR bit only) */ __HAL_SMARTCARD_DMA_REQUEST_DISABLE(&SCHandle, SMARTCARD_DMAREQ_RX); /* Start the receive IT process: to receive the command answer from the card */ HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[0], 2); /* Wait until receiving the answer from the card */ while(HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) {}while (HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) { ... } /* Decode the SW1 */ SC_ResponseStatus->SW1 = answer[0]; /* Decode the SW2 */ SC_ResponseStatus->SW2 = answer[1]; }if (SC_ADPU->Body.LC) { ... } Send body data to SC /* Or receive body data from SC ------------------------------------------*/ else if(SC_ADPU->Body.LE) { /* Start the receive IT process: to receive the command answer from the card */ HAL_SMARTCARD_Receive_IT(&SCHandle, (uint8_t *)&answer[0], (SC_ADPU->Body.LE +2)); /* Wait until receiving the answer from the card */ while(HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) {}while (HAL_SMARTCARD_GetState(&SCHandle) != HAL_SMARTCARD_STATE_READY) { ... } /* Decode the body data */ for(i = 0; i < SC_ADPU->Body.LE; i++) { SC_ResponseStatus->Data[i] = answer[i]; }for (i = 0; i < SC_ADPU->Body.LE; i++) { ... } /* Decode the SW1 */ SC_ResponseStatus->SW1 = answer[SC_ADPU->Body.LE]; /* Decode the SW2 */ SC_ResponseStatus->SW2 = answer[SC_ADPU->Body.LE +1]; }else if (SC_ADPU->Body.LE) { ... } }if (SC_ResponseStatus->SW1 == 0x00) { ... } }{ ... } /** * @brief Requests the reset answer from card. * @param SCState: pointer to an SC_State enumeration that will contain the Smartcard state. * @param card: pointer to a buffer which will contain the card ATR. * @param length: maximum ATR length * @retval None *//* ... */ static void SC_AnswerReq(SC_State *SCState, uint8_t *card, uint8_t length) { switch(*SCState) { case SC_RESET_LOW: /* Check response with reset low -----------------------------------------*/ HAL_SMARTCARD_Receive(&SCHandle, card, length, SC_RECEIVE_TIMEOUT); if(card[0] != 0x00) { (*SCState) = SC_ACTIVE; SC_Reset(GPIO_PIN_SET); }if (card[0] != 0x00) { ... } else { (*SCState) = SC_RESET_HIGH; }else { ... } break; case SC_RESET_LOW: case SC_RESET_HIGH: /* Check response with reset high ----------------------------------------*/ SC_Reset(GPIO_PIN_SET); /* Reset High */ HAL_SMARTCARD_Receive(&SCHandle, card, length, SC_RECEIVE_TIMEOUT); if(card[0]) { (*SCState) = SC_ACTIVE; }if (card[0]) { ... } else { (*SCState) = SC_POWER_OFF; }else { ... } break; case SC_RESET_HIGH: case SC_ACTIVE: break; case SC_ACTIVE: case SC_POWER_OFF: /* Close Connection if no answer received --------------------------------*/ SC_Reset(GPIO_PIN_SET); /* Reset high - a bit is used as level shifter from 3.3 to 5 V */ SC_PowerCmd(DISABLE); break; case SC_POWER_OFF: default: (*SCState) = SC_RESET_LOW; break;default }switch (*SCState) { ... } }{ ... } /** * @brief Decodes the Answer to reset received from card. * @param card: pointer to the buffer containing the card ATR. * @retval Smartcard communication Protocol *//* ... */ static uint8_t SC_decode_Answer2reset(uint8_t *card) { uint32_t i = 0, flag = 0, buf = 0, protocol = 0; SC_A2R.TS = card[0]; /* Initial character */ SC_A2R.T0 = card[1]; /* Format character */ SC_A2R.Hlength = SC_A2R.T0 & (uint8_t)0x0F; if ((SC_A2R.T0 & (uint8_t)0x80) == 0x80) { flag = 1; }if ((SC_A2R.T0 & (uint8_t)0x80) == 0x80) { ... } for (i = 0; i < 4; i++) { SC_A2R.Tlength = SC_A2R.Tlength + (((SC_A2R.T0 & (uint8_t)0xF0) >> (4 + i)) & (uint8_t)0x1); }for (i = 0; i < 4; i++) { ... } for (i = 0; i < SC_A2R.Tlength; i++) { SC_A2R.T[i] = card[i + 2]; }for (i = 0; i < SC_A2R.Tlength; i++) { ... } if ((SC_A2R.T0 & (uint8_t)0x80) == 0x00) { protocol = 0; }if ((SC_A2R.T0 & (uint8_t)0x80) == 0x00) { ... } else { protocol = SC_A2R.T[SC_A2R.Tlength - 1] & (uint8_t)0x0F; }else { ... } while (flag) { if ((SC_A2R.T[SC_A2R.Tlength - 1] & (uint8_t)0x80) == 0x80) { flag = 1; }if ((SC_A2R.T[SC_A2R.Tlength - 1] & (uint8_t)0x80) == 0x80) { ... } else { flag = 0; }else { ... } buf = SC_A2R.Tlength; SC_A2R.Tlength = 0; for (i = 0; i < 4; i++) { SC_A2R.Tlength = SC_A2R.Tlength + (((SC_A2R.T[buf - 1] & (uint8_t)0xF0) >> (4 + i)) & (uint8_t)0x1); }for (i = 0; i < 4; i++) { ... } for (i = 0;i < SC_A2R.Tlength; i++) { SC_A2R.T[buf + i] = card[i + 2 + buf]; }for (i = 0;i < SC_A2R.Tlength; i++) { ... } SC_A2R.Tlength += (uint8_t)buf; }while (flag) { ... } for (i = 0; i < SC_A2R.Hlength; i++) { SC_A2R.H[i] = card[i + 2 + SC_A2R.Tlength]; }for (i = 0; i < SC_A2R.Hlength; i++) { ... } return (uint8_t)protocol; }{ ... } /** * @brief Initializes all peripheral used for Smartcard interface. * @param None * @retval None *//* ... */ static void SC_Init(void) { /* SC_USART configuration --------------------------------------------------*/ /* SC_USART configured as follows: - Word Length = 9 Bits - 1.5 Stop Bit - Even parity - BaudRate = 8064 baud - Hardware flow control disabled (RTS and CTS signals) - Tx and Rx enabled - USART Clock enabled *//* ... */ /* USART Clock set to 3,5 MHz (PCLK2 (84 MHz) / 24) */ SCHandle.Instance = SC_USART; SCHandle.Init.BaudRate = 9408; /* Starting baudrate = 3,5MHz / 372etu */ SCHandle.Init.WordLength = SMARTCARD_WORDLENGTH_9B; SCHandle.Init.StopBits = SMARTCARD_STOPBITS_1_5; SCHandle.Init.Parity = SMARTCARD_PARITY_EVEN; SCHandle.Init.Mode = SMARTCARD_MODE_TX_RX; SCHandle.Init.CLKPolarity = SMARTCARD_POLARITY_LOW; SCHandle.Init.CLKPhase = SMARTCARD_PHASE_1EDGE; SCHandle.Init.CLKLastBit = SMARTCARD_LASTBIT_ENABLE; SCHandle.Init.Prescaler = 12; SCHandle.Init.GuardTime = 16; SCHandle.Init.NACKState = SMARTCARD_NACK_ENABLE; HAL_SMARTCARD_Init(&SCHandle); /* Set RSTIN HIGH */ SC_Reset(GPIO_PIN_SET); }{ ... } /** * @brief Deinitializes all resources used by the Smartcard interface. * @param None * @retval None *//* ... */ static void SC_DeInit(void) { /* Disable CMDVCC */ SC_PowerCmd(DISABLE); /* Deinitializes the SCHandle */ HAL_SMARTCARD_DeInit(&SCHandle); }{ ... } /** * @brief Configures the card power voltage. * @param SC_Voltage: specifies the card power voltage. * This parameter can be one of the following values: * @arg SC_VOLTAGE_5V: 5V cards. * @arg SC_VOLTAGE_3V: 3V cards. * @retval None *//* ... */ static void SC_VoltageConfig(uint32_t SC_Voltage) { if(SC_Voltage == SC_VOLTAGE_5V) { /* Select Smartcard 5V */ HAL_GPIO_WritePin(SC_3_5V_GPIO_PORT, SC_3_5V_PIN, GPIO_PIN_SET); }if (SC_Voltage == SC_VOLTAGE_5V) { ... } else { /* Select Smartcard 3V */ HAL_GPIO_WritePin(SC_3_5V_GPIO_PORT, SC_3_5V_PIN, GPIO_PIN_RESET); }else { ... } }{ ... } /** * @brief Configures GPIO hardware resources used for Samrtcard. * @param None * @retval None *//* ... */ void SC_IOConfig(void) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable GPIO clocks */ SC_3_5V_CLK_ENABLE(); SC_RESET_CLK_ENABLE(); SC_CMDVCC_CLK_ENABLE(); SC_OFF_CLK_ENABLE(); /* Configure Smartcard CMDVCC pin */ GPIO_InitStruct.Pin = SC_CMDVCC_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(SC_CMDVCC_GPIO_PORT, &GPIO_InitStruct); /* Configure Smartcard Reset pin */ GPIO_InitStruct.Pin = SC_RESET_PIN; HAL_GPIO_Init(SC_RESET_GPIO_PORT, &GPIO_InitStruct); /* Configure Smartcard 3/5V pin */ GPIO_InitStruct.Pin = SC_3_5V_PIN; HAL_GPIO_Init(SC_3_5V_GPIO_PORT, &GPIO_InitStruct); /* Select 5V */ SC_VoltageConfig(SC_VOLTAGE_5V); /* Disable CMDVCC */ SC_PowerCmd(DISABLE); /* Set RSTIN HIGH */ SC_Reset(GPIO_PIN_SET); /* Configure Smartcard OFF pin */ GPIO_InitStruct.Pin = SC_OFF_PIN; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(SC_OFF_GPIO_PORT, &GPIO_InitStruct); /* Configure the NVIC for Smartcard OFF pin */ HAL_NVIC_SetPriority(SC_OFF_EXTI_IRQn, 0, 0); HAL_NVIC_EnableIRQ(SC_OFF_EXTI_IRQn); }{ ... } /** * @brief Detects whether the Smartcard is present or not. * @param None. * @retval 0 - Smartcard inserted * 1 - Smartcard not inserted *//* ... */ static uint8_t SC_Detect(void) { return HAL_GPIO_ReadPin(SC_OFF_GPIO_PORT, SC_OFF_PIN); }{ ... } /** * @} *//* ... */ /** * @} *//* ... */