-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst25dv.c
2063 lines (1825 loc) · 59.5 KB
/
st25dv.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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
* @file st25dv.c
* @author MMY Application Team
* @brief This file provides set of driver functions to manage communication
* between BSP and ST25DV chip.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "st25dv.h"
/** @addtogroup BSP
* @{
*/
/** @defgroup ST25DV ST25DV driver
* @brief This module implements the functions to drive the ST25DV NFC dynamic tag.
* @details As recommended by the STM32 Cube methodology, this driver provides a standard structure to expose the NFC tag standard API.\n
* It also provides an extended API through its extended driver structure.\n
* To be usable on any MCU, this driver calls several IOBus functions.
* The IOBus functions are implemented outside this driver, and are in charge of accessing the MCU peripherals used for the communication with the tag.
* @{
*/
/* External variables --------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private defines -----------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static int32_t ReadRegWrap(void *Handle, uint16_t Reg, uint8_t *pData, uint16_t Length);
static int32_t WriteRegWrap(void *Handle, uint16_t Reg, const uint8_t *pData, uint16_t Length);
static int32_t ST25DV_Init( ST25DV_Object_t* );
static int32_t ST25DV_ReadID(ST25DV_Object_t* pObj, uint8_t * const pICRef );
static int32_t ST25DV_IsDeviceReady(ST25DV_Object_t* pObj, const uint32_t Trials );
static int32_t ST25DV_GetGPOStatus(ST25DV_Object_t* pObj, uint16_t * const pGPOStatus );
static int32_t ST25DV_ConfigureGPO(ST25DV_Object_t* pObj, const uint16_t ITConf );
static int32_t ST25DV_ReadData(ST25DV_Object_t* pObj, uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte );
static int32_t ST25DV_WriteData(ST25DV_Object_t* pObj, const uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte );
/* Global variables ---------------------------------------------------------*/
/**
* @brief Standard NFC tag driver API for the ST25DV.
* @details Provides a generic way to access the ST25DV implementation of the NFC tag standard driver functions.
*/
ST25DV_Drv_t St25Dv_Drv =
{
ST25DV_Init,
ST25DV_ReadID,
ST25DV_IsDeviceReady,
ST25DV_GetGPOStatus,
ST25DV_ConfigureGPO,
ST25DV_ReadData,
ST25DV_WriteData,
};
/* Public functions ---------------------------------------------------------*/
/**
* @brief Register Component Bus IO operations
* @param pObj the device pObj
* @retval 0 in case of success, an error code otherwise
*/
int32_t ST25DV_RegisterBusIO (ST25DV_Object_t* pObj, ST25DV_IO_t *pIO)
{
int32_t ret = ST25DV_OK;
if (pObj == NULL)
{
ret = ST25DV_ERROR;
}
else
{
pObj->IO.Init = pIO->Init;
pObj->IO.DeInit = pIO->DeInit;
pObj->IO.Write = pIO->Write;
pObj->IO.Read = pIO->Read;
pObj->IO.IsReady = pIO->IsReady;
pObj->IO.GetTick = pIO->GetTick;
pObj->Ctx.ReadReg = ReadRegWrap;
pObj->Ctx.WriteReg = WriteRegWrap;
pObj->Ctx.handle = pObj;
if (pObj->IO.Init == NULL)
{
ret = ST25DV_ERROR;
}
else
{
if (pObj->IO.Init() != 0)
{
ret = ST25DV_ERROR;
}
}
}
return ret;
}
/**
* @brief ST25DV nfctag Initialization.
* @param pObj the device pObj
* @retval Component error status.
*/
static int32_t ST25DV_Init( ST25DV_Object_t *pObj )
{
int32_t status = ST25DV_OK;
if (pObj->IsInitialized == 0U)
{
uint8_t nfctag_id;
ST25DV_ReadID(pObj,&nfctag_id);
if( (nfctag_id != I_AM_ST25DV04) && (nfctag_id != I_AM_ST25DV64) )
{
status = ST25DV_ERROR;
} else {
pObj->IsInitialized = 1U;
}
}
return status;
}
/**
* @brief Reads the ST25DV ID.
* @param pObj the device pObj
* @param pICRef Pointeron a uint8_t used to return the ST25DV ID.
* @retval Component error status.
*/
static int32_t ST25DV_ReadID(ST25DV_Object_t* pObj, uint8_t * const pICRef )
{
/* Read ICRef on device */
return st25dv_get_icref(&(pObj->Ctx), pICRef);
}
/**
* @brief Checks the ST25DV availability.
* @param pObj the device pObj
* @details The ST25DV I2C is NACKed when a RF communication is on-going.
* This function determines if the ST25DV is ready to answer an I2C request.
* @param Trials Max number of tentative.
* @retval Component error status.
*/
static int32_t ST25DV_IsDeviceReady(ST25DV_Object_t* pObj, const uint32_t Trials )
{
/* Test communication with device */
return pObj->IO.IsReady(ST25DV_ADDR_DATA_I2C, Trials );
}
/**
* @brief Reads the ST25DV GPO configuration.
* @param pObj the device pObj
* @param pGPOStatus Pointer on a uint16_t used to return the current GPO consiguration, as:
* - RFUSERSTATE = 0x01
* - RFBUSY = 0x02
* - RFINTERRUPT = 0x04
* - FIELDFALLING = 0x08
* - FIELDRISING = 0x10
* - RFPUTMSG = 0x20
* - RFGETMSG = 0x40
* - RFWRITE = 0x80
*
* @retval Component error status.
*/
static int32_t ST25DV_GetGPOStatus(ST25DV_Object_t* pObj, uint16_t * const pGPOStatus )
{
uint8_t reg_value;
int32_t status;
/* Read value of GPO register */
status = st25dv_get_gpo_all(&(pObj->Ctx), ®_value);
if( status == ST25DV_OK )
{
/* Extract GPO configuration */
*pGPOStatus = (uint16_t)reg_value;
}
return status;
}
/**
* @brief Configures the ST25DV GPO.
* @details Needs the I2C Password presentation to be effective.
* @param pObj the device pObj
* @param ITConf Provides the GPO configuration to apply:
* - RFUSERSTATE = 0x01
* - RFBUSY = 0x02
* - RFINTERRUPT = 0x04
* - FIELDFALLING = 0x08
* - FIELDRISING = 0x10
* - RFPUTMSG = 0x20
* - RFGETMSG = 0x40
* - RFWRITE = 0x80
*
* @retval Component error status.
*/
static int32_t ST25DV_ConfigureGPO(ST25DV_Object_t* pObj, const uint16_t ITConf )
{
/* Write GPO configuration to register */
return st25dv_set_gpo_all( &(pObj->Ctx), (uint8_t *)&ITConf);
}
/**
* @brief Reads N bytes of Data, starting from the specified I2C address.
* @param pObj the device pObj
* @param pData Pointer used to return the read data.
* @param TarAddr I2C data memory address to read.
* @param NbByte Number of bytes to be read.
* @retval Component error status.
*/
static int32_t ST25DV_ReadData(ST25DV_Object_t* pObj, uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte )
{
/* Read Data in user memory */
return pObj->IO.Read(ST25DV_ADDR_DATA_I2C, TarAddr, pData, NbByte );
}
/**
* @brief Writes N bytes of Data starting from the specified I2C Address.
* @param pObj the device pObj
* @param pData Pointer on the data to be written.
* @param TarAddr I2C data memory address to be written.
* @param NbByte Number of bytes to be written.
* @retval Component error status.
*/
static int32_t ST25DV_WriteData(ST25DV_Object_t* pObj, const uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte )
{
int32_t ret;
uint16_t split_data_nb;
const uint8_t *pdata_index = (const uint8_t *)pData;
uint16_t bytes_to_write = NbByte;
uint16_t mem_addr = TarAddr;
/* ST25DV can write a maximum of 256 bytes in EEPROM per i2c communication */
do
{
/* Split write if data to write is superior of max write bytes for ST25DV */
if( bytes_to_write > ST25DV_MAX_WRITE_BYTE )
{
/* DataSize higher than max page write, copy data by page */
split_data_nb = (uint16_t)ST25DV_MAX_WRITE_BYTE;
}
else
{
/* DataSize lower or equal to max page write, copy only last bytes */
split_data_nb = bytes_to_write;
}
/* Write split_data_nb bytes in memory */
ret = pObj->IO.Write( ST25DV_ADDR_DATA_I2C, mem_addr, pdata_index, split_data_nb);
if( ret == ST25DV_OK )
{
int32_t pollstatus;
/* Poll until EEPROM is available */
uint32_t tickstart = pObj->IO.GetTick();
/* Wait until ST25DV is ready or timeout occurs */
do
{
pollstatus = pObj->IO.IsReady( ST25DV_ADDR_DATA_I2C, 1 );
} while( ( (uint32_t)((int32_t)pObj->IO.GetTick() - (int32_t)tickstart) < ST25DV_WRITE_TIMEOUT) && (pollstatus != ST25DV_OK) );
if( pollstatus != ST25DV_OK )
{
ret = ST25DV_TIMEOUT;
}
}
/* update index, dest address, size for next write */
pdata_index += split_data_nb;
mem_addr += split_data_nb;
bytes_to_write -= split_data_nb;
}
while( ( bytes_to_write > 0 ) && ( ret == ST25DV_OK ) );
return ret;
}
/**
* @brief Reads the ST25DV IC Revision.
* @param pObj the device pObj
* @param pICRev Pointer on the uint8_t used to return the ST25DV IC Revision number.
* @retval Component error status.
*/
int32_t ST25DV_ReadICRev(ST25DV_Object_t* pObj, uint8_t * const pICRev )
{
/* Read ICRev on device */
return st25dv_get_icrev(&(pObj->Ctx), pICRev);
}
/**
* @brief Reads the ST25DV ITtime duration for the GPO pulses.
* @param pObj the device pObj
* @param pITtime Pointer used to return the coefficient for the GPO Pulse duration (Pulse duration = 302,06 us - ITtime * 512 / fc).
* @retval Component error status.
*/
int32_t ST25DV_ReadITPulse(ST25DV_Object_t* pObj, ST25DV_PULSE_DURATION * const pITtime )
{
uint8_t reg_value;
int32_t status;
/* Read ITtime register value */
status = st25dv_get_ittime_delay( &(pObj->Ctx), ®_value);
if( status == ST25DV_OK )
{
/* Extract delay coefficient value */
*pITtime = (ST25DV_PULSE_DURATION)reg_value;
}
return status;
}
/**
* @brief Configures the ST25DV ITtime duration for the GPO pulse.
* @details Needs the I2C Password presentation to be effective.
* @param pObj the device pObj
* @param ITtime Coefficient for the Pulse duration to be written (Pulse duration = 302,06 us - ITtime * 512 / fc)
* @retval Component error status.
*/
int32_t ST25DV_WriteITPulse( ST25DV_Object_t* pObj, const ST25DV_PULSE_DURATION ITtime )
{
uint8_t reg_value;
/* prepare data to write */
reg_value = (uint8_t)ITtime;
/* Write value for ITtime register */
return st25dv_set_ittime_delay( &(pObj->Ctx), ®_value );
}
/**
* @brief Reads N bytes from Registers, starting at the specified I2C address.
* @param pObj the device pObj
* @param pData Pointer used to return the read data.
* @param TarAddr I2C memory address to be read.
* @param NbByte Number of bytes to be read.
* @retval Component error status.
*/
int32_t ST25DV_ReadRegister(ST25DV_Object_t* pObj, uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte )
{
/* Read Data in system memory */
return pObj->IO.Read( ST25DV_ADDR_SYST_I2C, TarAddr, pData, NbByte );
}
/**
* @brief Writes N bytes to the specified register.
* @param pObj the device pObj
* @details Needs the I2C Password presentation to be effective.
* @param pData Pointer on the data to be written.
* @param TarAddr I2C register address to written.
* @param NbByte Number of bytes to be written.
* @retval Component error status.
*/
int32_t ST25DV_WriteRegister(ST25DV_Object_t* pObj, const uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte )
{
int32_t ret;
uint8_t split_data_nb;
uint16_t bytes_to_write = NbByte;
uint16_t mem_addr = TarAddr;
const uint8_t *pdata_index = (const uint8_t *)pData;
/* ST25DV can write a maximum of 256 bytes in EEPROM per i2c communication */
do
{
/* Split write if data to write is superior of max write bytes for ST25DV */
if( bytes_to_write > ST25DV_MAX_WRITE_BYTE )
{
/* DataSize higher than max page write, copy data by page */
split_data_nb = (uint8_t)ST25DV_MAX_WRITE_BYTE;
}
else
{
/* DataSize lower or equal to max page write, copy only last bytes */
split_data_nb = bytes_to_write;
}
/* Write split_data_nb bytes in register */
ret = pObj->IO.Write( ST25DV_ADDR_SYST_I2C, mem_addr, pdata_index, split_data_nb);
if( ret == ST25DV_OK )
{
int32_t pollstatus;
/* Poll until EEPROM is available */
uint32_t tickstart = pObj->IO.GetTick();
/* Wait until ST25DV is ready or timeout occurs */
do
{
pollstatus = pObj->IO.IsReady( ST25DV_ADDR_DATA_I2C, 1 );
} while( ( (uint32_t)((int32_t)pObj->IO.GetTick() - (int32_t)tickstart) < ST25DV_WRITE_TIMEOUT) && (pollstatus != ST25DV_OK) );
if( pollstatus != ST25DV_OK )
{
ret = ST25DV_TIMEOUT;
}
}
/* update index, dest address, size for next write */
pdata_index += split_data_nb;
mem_addr += split_data_nb;
bytes_to_write -= split_data_nb;
}
while( ( bytes_to_write > 0 ) && ( ret == ST25DV_OK ) );
return ret;
}
/**
* @brief Reads the ST25DV UID.
* @param pObj the device pObj
* @param pUid Pointer used to return the ST25DV UID value.
* @retval Component error status.
*/
int32_t ST25DV_ReadUID(ST25DV_Object_t* pObj, ST25DV_UID * const pUid )
{
uint8_t reg_value[8];
uint8_t i;
int32_t status;
/* Read value of UID registers */
status = st25dv_get_uid( &(pObj->Ctx), reg_value);
if( status == ST25DV_OK )
{
/* Store information in 2 WORD */
pUid->MsbUid = 0;
for( i = 0; i < 4; i++ )
{
pUid->MsbUid = (pUid->MsbUid << 8) | reg_value[7 - i];
}
pUid->LsbUid = 0;
for( i = 0; i < 4; i++ )
{
pUid->LsbUid = (pUid->LsbUid << 8) | reg_value[3 - i];
}
}
return status;
}
/**
* @brief Reads the ST25DV DSFID.
* @param pObj the device pObj
* @param pDsfid Pointer used to return the ST25DV DSFID value.
* @retval Component error status.
*/
int32_t ST25DV_ReadDSFID(ST25DV_Object_t* pObj, uint8_t * const pDsfid )
{
/* Read DSFID register */
return st25dv_get_dsfid(&(pObj->Ctx), pDsfid);
}
/**
* @brief Reads the ST25DV DSFID RF Lock state.
* @param pObj the device pObj
* @param pLockDsfid Pointer on a ST25DV_LOCK_STATUS used to return the DSFID lock state.
* @retval Component error status.
*/
int32_t ST25DV_ReadDsfidRFProtection(ST25DV_Object_t* pObj, ST25DV_LOCK_STATUS * const pLockDsfid )
{
uint8_t reg_value;
int32_t status;
/* Read register */
status = st25dv_get_lockdsfid(&(pObj->Ctx), ®_value );
if( status == ST25DV_OK )
{
/* Extract Lock Status */
if( reg_value == 0 )
{
*pLockDsfid = ST25DV_UNLOCKED;
}
else
{
*pLockDsfid = ST25DV_LOCKED;
}
}
return status;
}
/**
* @brief Reads the ST25DV AFI.
* @param pObj the device pObj
* @param pAfi Pointer used to return the ST25DV AFI value.
* @retval Component error status.
*/
int32_t ST25DV_ReadAFI(ST25DV_Object_t* pObj, uint8_t * const pAfi )
{
/* Read AFI register */
return st25dv_get_afi(&(pObj->Ctx), pAfi);
}
/**
* @brief Reads the AFI RF Lock state.
* @param pObj the device pObj
* @param pLockAfi Pointer on a ST25DV_LOCK_STATUS used to return the ASFID lock state.
* @retval Component error status.
*/
int32_t ST25DV_ReadAfiRFProtection(ST25DV_Object_t* pObj, ST25DV_LOCK_STATUS * const pLockAfi )
{
uint8_t reg_value;
int32_t status;
/* Read register */
status = st25dv_get_lockafi( &(pObj->Ctx), ®_value);
if( status == ST25DV_OK )
{
/* Extract Lock Status */
if( reg_value == 0 )
{
*pLockAfi = ST25DV_UNLOCKED;
}
else
{
*pLockAfi = ST25DV_LOCKED;
}
}
return status;
}
/**
* @brief Reads the I2C Protected Area state.
* @param pObj the device pObj
* @param pProtZone Pointer on a ST25DV_I2C_PROT_ZONE structure used to return the Protected Area state.
* @retval Component error status.
*/
int32_t ST25DV_ReadI2CProtectZone(ST25DV_Object_t* pObj, ST25DV_I2C_PROT_ZONE * const pProtZone )
{
uint8_t reg_value;
int32_t status;
/* Read value of I2c Protected Zone register */
status = st25dv_get_i2css_all( &(pObj->Ctx), ®_value);
if( status == ST25DV_OK )
{
/* Dispatch information to corresponding struct member */
pProtZone->ProtectZone1 = (ST25DV_PROTECTION_CONF)( (reg_value & ST25DV_I2CSS_PZ1_MASK) >> ST25DV_I2CSS_PZ1_SHIFT );
pProtZone->ProtectZone2 = (ST25DV_PROTECTION_CONF)( (reg_value & ST25DV_I2CSS_PZ2_MASK) >> ST25DV_I2CSS_PZ2_SHIFT );
pProtZone->ProtectZone3 = (ST25DV_PROTECTION_CONF)( (reg_value & ST25DV_I2CSS_PZ3_MASK) >> ST25DV_I2CSS_PZ3_SHIFT );
pProtZone->ProtectZone4 = (ST25DV_PROTECTION_CONF)( (reg_value & ST25DV_I2CSS_PZ4_MASK) >> ST25DV_I2CSS_PZ4_SHIFT );
}
return status;
}
/**
* @brief Sets the I2C write-protected state to an EEPROM Area.
* @details Needs the I2C Password presentation to be effective.
* @param pObj the device pObj
* @param Zone ST25DV_PROTECTION_ZONE value coresponding to the area to protect.
* @param ReadWriteProtection ST25DV_PROTECTION_CONF value corresponding to the protection to be set.
* @retval Component error status.
*/
int32_t ST25DV_WriteI2CProtectZonex(ST25DV_Object_t* pObj, const ST25DV_PROTECTION_ZONE Zone, const ST25DV_PROTECTION_CONF ReadWriteProtection )
{
int32_t status;
uint8_t reg_value = 0;
/* Compute and update new i2c Zone Security Status */
switch( Zone )
{
case ST25DV_PROT_ZONE1:
/* Read protection is not allowed for Zone 1 */
reg_value = (ReadWriteProtection & 0x01);
status = st25dv_set_i2css_pz1( &(pObj->Ctx), ®_value);
break;
case ST25DV_PROT_ZONE2:
reg_value = ReadWriteProtection;
status = st25dv_set_i2css_pz2( &(pObj->Ctx), ®_value);
break;
case ST25DV_PROT_ZONE3:
reg_value = ReadWriteProtection;
status = st25dv_set_i2css_pz3( &(pObj->Ctx), ®_value);
break;
case ST25DV_PROT_ZONE4:
reg_value = ReadWriteProtection;
status = st25dv_set_i2css_pz4( &(pObj->Ctx), ®_value);
break;
default:
return ST25DV_ERROR;
}
/* Write I2CZSS register */
return status;
}
/**
* @brief Reads the CCile protection state.
* @param pObj the device pObj
* @param pLockCCFile Pointer on a ST25DV_LOCK_CCFILE value corresponding to the lock state of the CCFile.
* @retval Component error status.
*/
int32_t ST25DV_ReadLockCCFile(ST25DV_Object_t* pObj, ST25DV_LOCK_CCFILE * const pLockCCFile )
{
uint8_t reg_value;
int32_t status;
/* Get actual LOCKCCFILE register value */
status = st25dv_get_lockccfile_all( &(pObj->Ctx), ®_value);
if( status == ST25DV_OK )
{
/* Extract CCFile block information */
if( reg_value & ST25DV_LOCKCCFILE_BLCK0_MASK )
{
pLockCCFile->LckBck0 = ST25DV_LOCKED;
}
else
{
pLockCCFile->LckBck0 = ST25DV_UNLOCKED;
}
if( reg_value & ST25DV_LOCKCCFILE_BLCK1_MASK )
{
pLockCCFile->LckBck1 = ST25DV_LOCKED;
}
else
{
pLockCCFile->LckBck1 = ST25DV_UNLOCKED;
}
}
return status;
}
/**
* @brief Locks the CCile to prevent any RF write access.
* @details Needs the I2C Password presentation to be effective.
* @param pObj the device pObj
* @param NbBlockCCFile ST25DV_CCFILE_BLOCK value corresponding to the number of blocks to be locked.
* @param LockCCFile ST25DV_LOCK_CCFILE value corresponding to the lock state to apply on the CCFile.
* @retval Component error status.
*/
int32_t ST25DV_WriteLockCCFile(ST25DV_Object_t* pObj, const ST25DV_CCFILE_BLOCK NbBlockCCFile, const ST25DV_LOCK_STATUS LockCCFile )
{
uint8_t reg_value;
/* Configure value to write on register */
if( NbBlockCCFile == ST25DV_CCFILE_1BLCK )
{
if( LockCCFile == ST25DV_LOCKED )
{
reg_value = ST25DV_LOCKCCFILE_BLCK0_MASK;
}
else
{
reg_value = 0x00;
}
}
else
{
if( LockCCFile == ST25DV_LOCKED )
{
reg_value = ST25DV_LOCKCCFILE_BLCK0_MASK | ST25DV_LOCKCCFILE_BLCK1_MASK;
}
else
{
reg_value = 0x00;
}
}
/* Write LOCKCCFILE register */
return st25dv_set_lockccfile_all( &(pObj->Ctx), ®_value);
}
/**
* @brief Reads the Cfg registers protection.
* @param pObj the device pObj
* @param pLockCfg Pointer on a ST25DV_LOCK_STATUS value corresponding to the Cfg registers lock state.
* @retval Component error status.
*/
int32_t ST25DV_ReadLockCFG(ST25DV_Object_t* pObj, ST25DV_LOCK_STATUS * const pLockCfg )
{
uint8_t reg_value;
int32_t status;
/* Get actual LOCKCCFILE register value */
status = st25dv_get_lockcfg_b0(&(pObj->Ctx), ®_value);
if( status == ST25DV_OK )
{
/* Extract LOCKCFG block information */
if( reg_value )
{
*pLockCfg = ST25DV_LOCKED;
}
else
{
*pLockCfg = ST25DV_UNLOCKED;
}
}
return status;
}
/**
* @brief Lock/Unlock the Cfg registers, to prevent any RF write access.
* @details Needs the I2C Password presentation to be effective.
* @param pObj the device pObj
* @param LockCfg ST25DV_LOCK_STATUS value corresponding to the lock state to be written.
* @retval Component error status.
*/
int32_t ST25DV_WriteLockCFG(ST25DV_Object_t* pObj, const ST25DV_LOCK_STATUS LockCfg )
{
uint8_t reg_value;
/* Configure value to write on register */
reg_value = (uint8_t)LockCfg;
/* Write LOCKCFG register */
return st25dv_set_lockcfg_b0(&(pObj->Ctx), ®_value );
}
/**
* @brief Presents I2C password, to authorize the I2C writes to protected areas.
* @param pObj the device pObj
* @param PassWord Password value on 32bits
* @retval Component error status.
*/
int32_t ST25DV_PresentI2CPassword(ST25DV_Object_t* pObj, const ST25DV_PASSWD PassWord )
{
uint8_t ai2c_message[17] = {0};
uint8_t i;
/* Build I2C Message with Password + Validation code 0x09 + Password */
ai2c_message[8] = 0x09;
for( i = 0; i < 4; i++ )
{
ai2c_message[i] = ( PassWord.MsbPasswd >> ( (3 - i) * 8) ) & 0xFF;
ai2c_message[i + 4] = ( PassWord.LsbPasswd >> ( (3 - i) * 8) ) & 0xFF;
ai2c_message[i + 9] = ai2c_message[i];
ai2c_message[i + 13] = ai2c_message[i + 4];
};
/* Present password to ST25DV */
return ST25DV_WriteRegister(pObj, ai2c_message, ST25DV_I2CPASSWD_REG, 17 );
}
/**
* @brief Writes a new I2C password.
* @details Needs the I2C Password presentation to be effective.
* @param pObj the device pObj
* @param PassWord New I2C PassWord value on 32bits.
* @retval Component error status.
*/
int32_t ST25DV_WriteI2CPassword( ST25DV_Object_t* pObj, const ST25DV_PASSWD PassWord )
{
uint8_t ai2c_message[17] = {0};
uint8_t i;
/* Build I2C Message with Password + Validation code 0x07 + Password */
ai2c_message[8] = 0x07;
for( i = 0; i < 4; i++ )
{
ai2c_message[i] = ( PassWord.MsbPasswd >> ( (3 - i) * 8) ) & 0xFF;
ai2c_message[i + 4] = ( PassWord.LsbPasswd >> ( (3 - i) * 8) ) & 0xFF;
ai2c_message[i + 9] = ai2c_message[i];
ai2c_message[i + 13] = ai2c_message[i + 4];
};
/* Write new password in I2CPASSWD register */
return ST25DV_WriteRegister(pObj, ai2c_message, ST25DV_I2CPASSWD_REG, 17 );
}
/**
* @brief Reads the RF Zone Security Status (defining the allowed RF accesses).
* @param pObj the device pObj
* @param Zone ST25DV_PROTECTION_ZONE value coresponding to the protected area.
* @param pRfprotZone Pointer on a ST25DV_RF_PROT_ZONE value corresponding to the area protection state.
* @retval Component error status.
*/
int32_t ST25DV_ReadRFZxSS( ST25DV_Object_t* pObj, const ST25DV_PROTECTION_ZONE Zone, ST25DV_RF_PROT_ZONE * const pRfprotZone )
{
uint8_t reg_value;
int32_t status;
/* Read actual value of Sector Security Status register */
switch( Zone )
{
case ST25DV_PROT_ZONE1:
status = st25dv_get_rfa1ss_all(&(pObj->Ctx), ®_value);
break;
case ST25DV_PROT_ZONE2:
status = st25dv_get_rfa2ss_all(&(pObj->Ctx), ®_value);
break;
case ST25DV_PROT_ZONE3:
status = st25dv_get_rfa3ss_all(&(pObj->Ctx), ®_value);
break;
case ST25DV_PROT_ZONE4:
status = st25dv_get_rfa4ss_all(&(pObj->Ctx), ®_value);
break;
default:
status = ST25DV_ERROR;
}
if( status == ST25DV_OK )
{
/* Extract Sector Security Status configuration */
pRfprotZone->PasswdCtrl = (ST25DV_PASSWD_PROT_STATUS)((reg_value & ST25DV_RFA1SS_PWDCTRL_MASK) >> ST25DV_RFA1SS_PWDCTRL_SHIFT);
pRfprotZone->RWprotection = (ST25DV_PROTECTION_CONF)((reg_value & ST25DV_RFA1SS_RWPROT_MASK) >> ST25DV_RFA1SS_RWPROT_SHIFT);
}
return status;
}
/**
* @brief Writes the RF Zone Security Status (defining the allowed RF accesses)
* @details Needs the I2C Password presentation to be effective.
* @param pObj the device pObj
* @param Zone ST25DV_PROTECTION_ZONE value corresponding to the area on which to set the RF protection.
* @param RfProtZone Pointer on a ST25DV_RF_PROT_ZONE value defininf the protection to be set on the area.
* @retval Component error status.
*/
int32_t ST25DV_WriteRFZxSS( ST25DV_Object_t* pObj, const ST25DV_PROTECTION_ZONE Zone, const ST25DV_RF_PROT_ZONE RfProtZone )
{
uint8_t reg_value;
int32_t status;
/* Update Sector Security Status */
reg_value = (RfProtZone.RWprotection << ST25DV_RFA1SS_RWPROT_SHIFT) & ST25DV_RFA1SS_RWPROT_MASK;
reg_value |= ((RfProtZone.PasswdCtrl << ST25DV_RFA1SS_PWDCTRL_SHIFT) & ST25DV_RFA1SS_PWDCTRL_MASK);
/* Write Sector Security register */
switch( Zone )
{
case ST25DV_PROT_ZONE1:
status = st25dv_set_rfa1ss_all(&(pObj->Ctx), ®_value);
break;
case ST25DV_PROT_ZONE2:
status = st25dv_set_rfa2ss_all(&(pObj->Ctx), ®_value);
break;
case ST25DV_PROT_ZONE3:
status = st25dv_set_rfa3ss_all(&(pObj->Ctx), ®_value);
break;
case ST25DV_PROT_ZONE4:
status = st25dv_set_rfa4ss_all(&(pObj->Ctx), ®_value);
break;
default:
status = ST25DV_ERROR;
}
return status;
}
/**
* @brief Reads the value of the an area end address.
* @param pObj the device pObj
* @param EndZone ST25DV_END_ZONE value corresponding to an area end address.
* @param pEndZ Pointer used to return the end address of the area.
* @retval Component error status.
*/
int32_t ST25DV_ReadEndZonex( ST25DV_Object_t* pObj, const ST25DV_END_ZONE EndZone, uint8_t * pEndZ )
{
int32_t status;
/* Read the corresponding End zone */
switch( EndZone )
{
case ST25DV_ZONE_END1:
status = st25dv_get_enda1(&(pObj->Ctx),pEndZ);
break;
case ST25DV_ZONE_END2:
status = st25dv_get_enda2(&(pObj->Ctx),pEndZ);
break;
case ST25DV_ZONE_END3:
status = st25dv_get_enda3(&(pObj->Ctx),pEndZ);
break;
default:
status = ST25DV_ERROR;
}
return status;
}
/**
* @brief Sets the end address of an area.
* @details Needs the I2C Password presentation to be effective.
* @note The ST25DV answers a NACK when setting the EndZone2 & EndZone3 to same value than repectively EndZone1 & EndZone2.\n
* These NACKs are ok.
* @param pObj the device pObj
* @param EndZone ST25DV_END_ZONE value corresponding to an area.
* @param EndZ End zone value to be written.
* @retval Component error status.
*/
int32_t ST25DV_WriteEndZonex( ST25DV_Object_t* pObj, const ST25DV_END_ZONE EndZone, const uint8_t EndZ )
{
int32_t status;
/* Write the corresponding End zone value in register */
switch( EndZone )
{
case ST25DV_ZONE_END1:
status = st25dv_set_enda1(&(pObj->Ctx),&EndZ);
break;
case ST25DV_ZONE_END2:
status = st25dv_set_enda2(&(pObj->Ctx),&EndZ);
break;
case ST25DV_ZONE_END3:
status = st25dv_set_enda3(&(pObj->Ctx),&EndZ);
break;
default:
status = ST25DV_ERROR;
}
return status;
}
/**
* @brief Initializes the end address of the ST25DV areas with their default values (end of memory).
* @details Needs the I2C Password presentation to be effective..
* The ST25DV answers a NACK when setting the EndZone2 & EndZone3 to same value than repectively EndZone1 & EndZone2.
* These NACKs are ok.
* @param pObj the device pObj
* @retval Component error status.
*/
int32_t ST25DV_InitEndZone( ST25DV_Object_t* pObj )
{
uint8_t endval = 0xFF;
uint32_t maxmemlength;
ST25DV_MEM_SIZE memsize;
int32_t ret;
memsize.Mem_Size = 0;
memsize.BlockSize = 0;
/* Get EEPROM mem size */
ST25DV_ReadMemSize(pObj, &memsize );
maxmemlength = (memsize.Mem_Size + 1) * (memsize.BlockSize + 1);
/* Compute Max value for endzone register */
endval = (maxmemlength / 32) - 1;
/* Write EndZone value to ST25DV registers */
ret = ST25DV_WriteEndZonex(pObj, ST25DV_ZONE_END3, endval );
if( (ret == ST25DV_OK) || (ret == ST25DV_NACK) )
{
ret = ST25DV_WriteEndZonex(pObj, ST25DV_ZONE_END2, endval );
if( (ret == ST25DV_OK) || (ret == ST25DV_NACK) )
{
ret = ST25DV_WriteEndZonex(pObj, ST25DV_ZONE_END1, endval );
}
}
return ret;
}
/**
* @brief Creates user areas with defined lengths.
* @details Needs the I2C Password presentation to be effective.
* @param pObj the device pObj
* @param Zone1Length Length of area1 in bytes (32 to 8192, 0x20 to 0x2000)
* @param Zone2Length Length of area2 in bytes (0 to 8128, 0x00 to 0x1FC0)
* @param Zone3Length Length of area3 in bytes (0 to 8064, 0x00 to 0x1F80)
* @param Zone4Length Length of area4 in bytes (0 to 8000, 0x00 to 0x1F40)
* @retval Component error status.
*/
int32_t ST25DV_CreateUserZone( ST25DV_Object_t* pObj, uint16_t Zone1Length, uint16_t Zone2Length, uint16_t Zone3Length, uint16_t Zone4Length )
{
uint8_t EndVal;
ST25DV_MEM_SIZE memsize;
uint16_t maxmemlength = 0;
int32_t ret = ST25DV_OK;
memsize.Mem_Size = 0;
memsize.BlockSize = 0;
ST25DV_ReadMemSize(pObj, &memsize );
maxmemlength = (memsize.Mem_Size + 1) * (memsize.BlockSize + 1);
/* Checks that values of different zones are in bounds */
if( ( Zone1Length < 32 ) || ( Zone1Length > maxmemlength ) || ( Zone2Length > (maxmemlength - 32) )
|| ( Zone3Length > (maxmemlength - 64) ) || ( Zone4Length > (maxmemlength - 96) ) )
{
ret = ST25DV_ERROR;
}
/* Checks that the total is less than the authorised maximum */
if( ( Zone1Length + Zone2Length + Zone3Length + Zone4Length ) > maxmemlength )
{
ret = ST25DV_ERROR;
}
if ( ret == ST25DV_OK)
{
/* if The value for each Length is not a multiple of 64 correct it. */