-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathad5940.c
4422 lines (4104 loc) · 155 KB
/
ad5940.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 ad5940.c
* @brief AD5940 library. This file contains all AD5940 library functions.
* @author ADI
* @date March 2019
* @par Revision History:
*
* Copyright (c) 2017-2019 Analog Devices, Inc. All Rights Reserved.
*
* This software is proprietary to Analog Devices, Inc. and its licensors.
* By using this software you agree to the terms of the associated
* Analog Devices Software License Agreement.
**/
#include "ad5940.h"
/*! \mainpage AD5940 Library Introduction
*
* ![AD5940 EVAL Board](https://www.analog.com/-/media/analog/en/evaluation-board-images/images/eval-ad5940elcztop-web.gif?h=500&thn=1&hash=1F38F7CC1002894616F74D316365C0A2631C432B "ADI logo")
*
* # Introduction
*
* The documentation is for AD594x library and examples.
*
* # Manual Structure
*
* @ref AD5940_Library
* - @ref AD5940_Functions
* - @ref TypeDefinitions
* @ref AD5940_Standard_Examples
* @ref AD5940_System_Examples
*
* # How to Use It
* We provide examples that can directly run out of box.
* The files can generally be separated to three parts:
* - AD5940 Library files. ad5940.c and ad5940.h specifically. These two files are shared among all examples.
* - AD5940 System Examples. The system examples mean system level application like measuring impedance.
* - Standard examples. These include basic block level examples like ADC. It shows how to setup and use one specific block.
*
* ## Requirements to run these examples
* ### Hardware
* - Use EVAL_AD5940 or EVAL_AD5941. The default MCU board we used is ADICUP3029. We also provide project for ST NUCLEO board.
* - Or use EVAL_ADuCM355
* ### Software
* - Pull all the source file from [GitHub](https://github.com/analogdevicesinc/ad5940-examples.git)
* - CMSIS pack that related to specific MCU. This normally is done by IDE you use.
*
* ## Materials
* Please use this library together with following materials.
* - [AD5940 Data Sheet](https://www.analog.com/media/en/technical-documentation/data-sheets/AD5940.pdf)
* - [AD5940 Eval Board](https://www.analog.com/en/design-center/evaluation-hardware-and-software/evaluation-boards-kits/EVAL-AD5940.html)
*
*/
/* Remove below variables after AD594x is released. */
static BoolFlag bIsS2silicon = bFALSE;
/* Declare of SPI functions used to read/write registers */
#ifndef CHIPSEL_M355
static uint32_t AD5940_SPIReadReg(uint16_t RegAddr);
static void AD5940_SPIWriteReg(uint16_t RegAddr, uint32_t RegData);
#else
static uint32_t AD5940_D2DReadReg(uint16_t RegAddr);
static void AD5940_D2DWriteReg(uint16_t RegAddr, uint32_t RegData);
#endif
/**
* @addtogroup AD5940_Library
* The library functions, structures and constants.
* @{
* @defgroup AD5940_Functions
* @{
* @defgroup Function_Helpers
* @brief The functions with no hardware access. They are helpers.
* @{
* @defgroup Sequencer_Generator_Functions
* @brief The set of function used to track all register read and write once it's enabled. It can translate register write operation to sequencer commands.
* @{
*/
#define SEQUENCE_GENERATOR /*!< Build sequence generator part in to lib. Comment this line to remove this feature */
#ifdef SEQUENCE_GENERATOR
/**
* Structure used to store register information(address and its data)
* */
typedef struct
{
uint32_t RegAddr :8; /**< 8bit address is enough for sequencer */
uint32_t RegValue :24; /**< Reg data is limited to 24bit by sequencer */
}SEQGenRegInfo_Type;
/**
* Sequencer generator data base.
*/
struct
{
BoolFlag EngineStart; /**< Flag to mark start of the generator */
uint32_t BufferSize; /**< Total buffer size */
uint32_t *pSeqBuff; /**< The buffer for sequence generator(both sequences and RegInfo) */
uint32_t SeqLen; /**< Generated sequence length till now */
SEQGenRegInfo_Type *pRegInfo; /**< Pointer to buffer where stores register info */
uint32_t RegCount; /**< The count of register info available in buffer *pRegInfo. */
AD5940Err LastError; /**< The last error message. */
}SeqGenDB; /* Data base of Seq Generator */
/**
* @brief Manually input a command to sequencer generator.
* @param CmdWord: The 32-bit width sequencer command word. @ref Sequencer_Helper can be used to generate commands.
* @return None;
*/
void AD5940_SEQGenInsert(uint32_t CmdWord)
{
uint32_t temp;
temp = SeqGenDB.RegCount + SeqGenDB.SeqLen;
/* Generate Sequence command */
if(temp < SeqGenDB.BufferSize)
{
SeqGenDB.pSeqBuff[SeqGenDB.SeqLen] = CmdWord;
SeqGenDB.SeqLen ++;
}
else /* There is no buffer */
SeqGenDB.LastError = AD5940ERR_BUFF;
}
/**
* @brief Search data-base to get current register value.
* @param RegAddr: The register address.
* @param pIndex: Pointer to a variable that used to store index of found register-info.
* @return Return AD5940ERR_OK if register found in data-base. Otherwise return AD5940ERR_SEQREG.
*/
static AD5940Err AD5940_SEQGenSearchReg(uint32_t RegAddr, uint32_t *pIndex)
{
uint32_t i;
RegAddr = (RegAddr>>2)&0xff;
for(i=0;i<SeqGenDB.SeqLen;i++)
{
if(RegAddr == SeqGenDB.pRegInfo[i].RegAddr)
{
*pIndex = i;
return AD5940ERR_OK;
}
}
return AD5940ERR_SEQREG;
}
/**
* @brief Get the register default value by SPI read. This function requires AD5940 is in active state, otherwise we cannot get the default register value.
* @param RegAddr: The register address.
* @param pRegData: Pointer to a variable to store register default value.
* @return Return AD5940ERR_OK.
*/
static AD5940Err AD5940_SEQGenGetRegDefault(uint32_t RegAddr, uint32_t *pRegData)
{
#ifdef CHIPSEL_M355
*pRegData = AD5940_D2DReadReg(RegAddr);
#else
*pRegData = AD5940_SPIReadReg(RegAddr);
#endif
return AD5940ERR_OK;
}
/**
* @brief Record the current register info to data-base. Update LastError if there is error.
* @param RegAddr: The register address.
* @param RegData: The register data
* @return Return None.
*/
static void AD5940_SEQRegInfoInsert(uint16_t RegAddr, uint32_t RegData)
{
uint32_t temp;
temp = SeqGenDB.RegCount + SeqGenDB.SeqLen;
if(temp < SeqGenDB.BufferSize)
{
SeqGenDB.pRegInfo --; /* Move back */
SeqGenDB.pRegInfo[0].RegAddr = (RegAddr>>2)&0xff;
SeqGenDB.pRegInfo[0].RegValue = RegData&0x00ffffff;
SeqGenDB.RegCount ++;
}
else /* There is no more buffer */
{
SeqGenDB.LastError = AD5940ERR_BUFF;
}
}
/**
* @brief Get current register value. If we have record in data-base, read it. Otherwise, return the register default value.
* @param RegAddr: The register address.
* @return Return register value.
*/
static uint32_t AD5940_SEQReadReg(uint16_t RegAddr)
{
uint32_t RegIndex, RegData;
if(AD5940_SEQGenSearchReg(RegAddr, &RegIndex) != AD5940ERR_OK)
{
/* There is no record in data-base, read the default value. */
AD5940_SEQGenGetRegDefault(RegAddr, &RegData);
AD5940_SEQRegInfoInsert(RegAddr, RegData);
}
else
{
/* return the current register value stored in data-base */
RegData = SeqGenDB.pRegInfo[RegIndex].RegValue;
}
return RegData;
}
/**
* @brief Generate a sequencer command to write register. If the register address is out of range, it won't generate a command.
* This function will also update the register-info in data-base to record current register value.
* @param RegAddr: The register address.
* @param RegData: The register value.
* @return Return None.
*/
static void AD5940_SEQWriteReg(uint16_t RegAddr, uint32_t RegData)
{
uint32_t RegIndex;
if(RegAddr > 0x21ff)
{
SeqGenDB.LastError = AD5940ERR_ADDROR; /* address out of range */
return;
}
if(AD5940_SEQGenSearchReg(RegAddr, &RegIndex) == AD5940ERR_OK)
{
/* Store register value */
SeqGenDB.pRegInfo[RegIndex].RegValue = RegData;
/* Generate Sequence command */
AD5940_SEQGenInsert(SEQ_WR(RegAddr, RegData));
}
else
{
AD5940_SEQRegInfoInsert(RegAddr, RegData);
/* Generate Sequence command */
AD5940_SEQGenInsert(SEQ_WR(RegAddr, RegData));
}
}
/**
* @brief Initialize sequencer generator with specified buffer.
* The buffer is used to store sequencer generated and record register value changes.
* The command is stored from start address of buffer while register value is stored from end of buffer.
* Buffer[0] : First sequencer command;
* Buffer[1] : Second Sequencer command;
* ...
* Buffer[Last-1]: The second register value record.
* Buffer[Last]: The first register value record.
* @param pBuffer: Pointer to the buffer.
* @param BufferSize: The buffer length.
* @return Return None.
*/
void AD5940_SEQGenInit(uint32_t *pBuffer, uint32_t BufferSize)
{
if(BufferSize < 2) return;
SeqGenDB.BufferSize = BufferSize;
SeqGenDB.pSeqBuff = pBuffer;
SeqGenDB.pRegInfo = (SEQGenRegInfo_Type*)pBuffer + BufferSize - 1; /* Point to the last element in buffer */
SeqGenDB.SeqLen = 0;
SeqGenDB.RegCount = 0;
SeqGenDB.LastError = AD5940ERR_OK;
SeqGenDB.EngineStart = bFALSE;
}
/**
* @brief Get sequencer command generated.
* @param ppSeqCmd: Pointer to a variable(pointer) used to store the pointer to generated sequencer command.
* @param pSeqLen: Pointer to a variable that used to store how many commands available in buffer.
* @return Return lasterror.
*/
AD5940Err AD5940_SEQGenFetchSeq(const uint32_t **ppSeqCmd, uint32_t *pSeqLen)
{
AD5940Err lasterror;
if(ppSeqCmd)
*ppSeqCmd = SeqGenDB.pSeqBuff;
if(pSeqLen)
*pSeqLen = SeqGenDB.SeqLen;
//SeqGenDB.SeqLen = 0; /* Start a new sequence */
lasterror = SeqGenDB.LastError;
//SeqGenDB.LastError = AD5940ERR_OK; /* Clear error message */
return lasterror;
}
/**
* @brief Start or stop the sequencer generator. Once started, the register write will be recorded to sequencer generator.
* Once it's disabled, the register write is written to AD5940 directly by SPI bus.
* @param bFlag: Enable or disable sequencer generator.
* @return Return None.
*/
void AD5940_SEQGenCtrl(BoolFlag bFlag)
{
if(bFlag == bFALSE) /* Disable sequence generator */
{
SeqGenDB.EngineStart = bFALSE;
}
else
{
SeqGenDB.SeqLen = 0;
SeqGenDB.LastError = AD5940ERR_OK; /* Clear error message */
SeqGenDB.EngineStart = bTRUE;
}
}
/**
* @brief Calculate the number of cycles in the sequence
* @return Return Number of ACLK Cycles that a generated sequence will take.
*/
uint32_t AD5940_SEQCycleTime(void)
{
uint32_t i, Cycles, Cmd;
Cycles = 0;
for(i=0;i<SeqGenDB.RegCount;i++)
{
Cmd = (SeqGenDB.pSeqBuff[i] >> 30) & 0x3;
if (Cmd & 0x2)
{
/* A write command */
Cycles += 1;
}
else
{
if (Cmd & 0x1)
{
/* Timeout Command */
Cycles += 1;
}
else
{
/* Wait command */
Cycles += SeqGenDB.pSeqBuff[i] & 0x3FFFFFFF;
}
}
}
return Cycles;
}
#endif
/**
* @} Sequencer_Generator_Functions
*/
/**
* Check if an uint8_t value exist in table.
*/
static int32_t _is_value_in_table(uint8_t value, const uint8_t *table, uint8_t len, uint8_t *index)
{
for(int i=0; i<len; i++)
{
if(value == table[i])
{
*index = i;
return bTRUE;
}
}
return bFALSE;
}
/**
* @brief return if the SINC3/SINC2 combination is available for notch 50Hz filter.
* If it's not availabe, hardware automatically bypass Notch even if it's enabled.
* @param pFilterInfo the filter configuration, only need sinc2/sinc3 osr and adc data rate information.
* @return return bTRUE if notch 50Hz filter is available.
*/
BoolFlag AD5940_Notch50HzAvailable(ADCFilterCfg_Type *pFilterInfo, uint8_t *dl)
{
if((pFilterInfo->ADCRate == ADCRATE_800KHZ && pFilterInfo->ADCSinc3Osr == ADCSINC3OSR_2)||\
(pFilterInfo->ADCRate == ADCRATE_1P6MHZ && pFilterInfo->ADCSinc3Osr != ADCSINC3OSR_2))
{
//this combination suits for filter:
//SINC3 OSR2, for 800kSPS
//and SINC3 OSR4 and OSR5 for 1.6MSPS,
const uint8_t available_sinc2_osr[] = {ADCSINC2OSR_533, ADCSINC2OSR_667,ADCSINC2OSR_800, ADCSINC2OSR_889, ADCSINC2OSR_1333};
const uint8_t dl_50Hz[] = {15,12,10,9,6};
uint8_t index;
if(_is_value_in_table(pFilterInfo->ADCSinc2Osr, available_sinc2_osr, sizeof(available_sinc2_osr), &index))
{
*dl = dl_50Hz[index];
return bTRUE;
}
}
else if(pFilterInfo->ADCRate == ADCRATE_1P6MHZ && pFilterInfo->ADCSinc3Osr == ADCSINC3OSR_2)
{
//this combination suits for filter:
//SINC3 OSR2 for 1.6MSPS
const uint8_t available_sinc2_osr[] = {ADCSINC2OSR_889, ADCSINC2OSR_1067, ADCSINC2OSR_1333};
const uint8_t dl_50Hz[] = {18,15,12};
uint8_t index;
if(_is_value_in_table(pFilterInfo->ADCSinc2Osr, available_sinc2_osr, sizeof(available_sinc2_osr), &index))
{
*dl = dl_50Hz[index];
return bTRUE;
}
}
else if(pFilterInfo->ADCRate == ADCRATE_800KHZ && pFilterInfo->ADCSinc3Osr != ADCSINC3OSR_2)
{
//this combination suits for filter:
//SINC3 OSR4 and OSR5 for 800kSPS,
const uint8_t available_sinc2_osr[] = {ADCSINC2OSR_178, ADCSINC2OSR_267, ADCSINC2OSR_533, ADCSINC2OSR_640,\
ADCSINC2OSR_800, ADCSINC2OSR_1067};
const uint8_t dl_50Hz[] = {18,12,6,5,4,3};
uint8_t index;
if(_is_value_in_table(pFilterInfo->ADCSinc2Osr, available_sinc2_osr, sizeof(available_sinc2_osr), &index))
{
*dl = dl_50Hz[index];
return bTRUE;
}
}
*dl = 0;
return bFALSE;
}
/**
* @brief return if the SINC3/SINC2 combination is available for notch 60Hz filter.
* If it's not availabe, hardware automatically bypass Notch even if it's enabled.
* @param pFilterInfo the filter configuration, need sinc2/sinc3 osr and adc data rate information.
* @return return bTRUE if notch 60Hz filter is available.
*/
BoolFlag AD5940_Notch60HzAvailable(ADCFilterCfg_Type *pFilterInfo, uint8_t *dl)
{
if((pFilterInfo->ADCRate == ADCRATE_800KHZ && pFilterInfo->ADCSinc3Osr == ADCSINC3OSR_2)||\
(pFilterInfo->ADCRate == ADCRATE_1P6MHZ && pFilterInfo->ADCSinc3Osr != ADCSINC3OSR_2))
{
//this combination suits for filter:
//SINC3 OSR2, for 800kSPS
//and SINC3 OSR4 and OSR5 for 1.6MSPS,
const uint8_t available_sinc2_osr[] = {ADCSINC2OSR_667, ADCSINC2OSR_1333};
const uint8_t dl_60Hz[] = {10,5};
uint8_t index;
if(_is_value_in_table(pFilterInfo->ADCSinc2Osr, available_sinc2_osr, sizeof(available_sinc2_osr), &index))
{
*dl = dl_60Hz[index];
return bTRUE;
}
}
else if(pFilterInfo->ADCRate == ADCRATE_1P6MHZ && pFilterInfo->ADCSinc3Osr == ADCSINC3OSR_2)
{
//this combination suits for filter:
//SINC3 OSR2 for 1.6MSPS
const uint8_t available_sinc2_osr[] = {ADCSINC2OSR_889, ADCSINC2OSR_1333};
const uint8_t dl_60Hz[] = {15,10};
uint8_t index;
if(_is_value_in_table(pFilterInfo->ADCSinc2Osr, available_sinc2_osr, sizeof(available_sinc2_osr), &index))
{
*dl = dl_60Hz[index];
return bTRUE;
}
}
else if(pFilterInfo->ADCRate == ADCRATE_800KHZ && pFilterInfo->ADCSinc3Osr != ADCSINC3OSR_2)
{
//this combination suits for filter:
//SINC3 OSR4 and OSR5 for 800kSPS,
const uint8_t available_sinc2_osr[] = {ADCSINC2OSR_178, ADCSINC2OSR_267, ADCSINC2OSR_533, ADCSINC2OSR_667,\
ADCSINC2OSR_889, ADCSINC2OSR_1333};
const uint8_t dl_60Hz[] = {15,10,5,4,3,2};
uint8_t index;
if(_is_value_in_table(pFilterInfo->ADCSinc2Osr, available_sinc2_osr, sizeof(available_sinc2_osr), &index))
{
*dl = dl_60Hz[index];
return bTRUE;
}
}
*dl = 0;
return bFALSE;
}
/**
* @brief Calculate how many clocks are needed in sequencer wait command to generate required number of data from filter output.
* @note When measurement is done, it's recommend to disable blocks like ADCPWR, ADCCNV, SINC2, DFT etc. If blocks remain powered up,
* they may need less clocks to generate required number of output. Use function @ref AD5940_AFECtrlS to control these blocks.
* @param pFilterInfo: Pointer to configuration structure.
* @param pClocks: pointer used to store results.
* @return return none.
*/
void AD5940_ClksCalculate(ClksCalInfo_Type *pFilterInfo, uint32_t *pClocks)
{
uint32_t temp = 0;
const uint32_t sinc2osr_table[] = {22,44,89,178,267,533,640,667,800,889,1067,1333,0};
const uint32_t sinc3osr_table[] = {5,4,2,0};
*pClocks = 0;
if(pFilterInfo == NULL) return;
if(pClocks == NULL) return;
if(pFilterInfo->ADCSinc2Osr > ADCSINC2OSR_1333) return;
if(pFilterInfo->ADCSinc3Osr > 2) return; /* 0: OSR5, 1:OSR4, 2:OSR2 */
if(pFilterInfo->ADCAvgNum > ADCAVGNUM_16) return; /* Average number index:0,1,2,3 */
switch(pFilterInfo->DataType)
{
case DATATYPE_ADCRAW:
temp = (uint32_t)(20*pFilterInfo->DataCount*pFilterInfo->RatioSys2AdcClk);
break;
case DATATYPE_SINC3:
temp = (uint32_t)(((pFilterInfo->DataCount+2)*sinc3osr_table[pFilterInfo->ADCSinc3Osr]+1)*20*pFilterInfo->RatioSys2AdcClk + 0.5f);
break;
case DATATYPE_SINC2:
temp = (pFilterInfo->DataCount+1)*sinc2osr_table[pFilterInfo->ADCSinc2Osr] + 1;
pFilterInfo->DataType = DATATYPE_SINC3;
pFilterInfo->DataCount = temp;
AD5940_ClksCalculate(pFilterInfo, &temp);
pFilterInfo->DataType = DATATYPE_SINC2;
temp += 15; /* Need extra 15 clocks for FIFO etc. Just to be safe. */
break;
case DATATYPE_NOTCH:
{
ADCFilterCfg_Type filter;
filter.ADCRate = pFilterInfo->ADCRate;
filter.ADCSinc3Osr = pFilterInfo->ADCSinc3Osr;
filter.ADCSinc2Osr = pFilterInfo->ADCSinc2Osr;
uint8_t dl=0, dl_50, dl_60;
if(AD5940_Notch50HzAvailable(&filter, &dl_50)){
dl += dl_50 - 1;
}
if(AD5940_Notch60HzAvailable(&filter, &dl_60)){
dl += dl_60 - 1;
}
pFilterInfo->DataType = DATATYPE_SINC2;
pFilterInfo->DataCount += dl; //DL is the extra data input needed for filter to output first data.
AD5940_ClksCalculate(pFilterInfo,&temp);
//restore the filter info.
pFilterInfo->DataType = DATATYPE_NOTCH;
pFilterInfo->DataCount -= dl;
break;
}
case DATATYPE_DFT:
switch(pFilterInfo->DftSrc)
{
case DFTSRC_ADCRAW:
pFilterInfo->DataType = DATATYPE_ADCRAW;
AD5940_ClksCalculate(pFilterInfo, &temp);
break;
case DFTSRC_SINC3:
pFilterInfo->DataType = DATATYPE_SINC3;
AD5940_ClksCalculate(pFilterInfo, &temp);
break;
case DFTSRC_SINC2NOTCH:
if(pFilterInfo->BpNotch)
pFilterInfo->DataType = DATATYPE_SINC2;
else
pFilterInfo->DataType = DATATYPE_NOTCH;
AD5940_ClksCalculate(pFilterInfo, &temp);
break;
case DFTSRC_AVG:
pFilterInfo->DataType = DATATYPE_SINC3;
pFilterInfo->DataCount *= 1L<<(pFilterInfo->ADCAvgNum+1); /* 0: average2, 1: average4, 2: average8, 3: average16 */
AD5940_ClksCalculate(pFilterInfo, &temp);
break;
default:
break;
}
pFilterInfo->DataType = DATATYPE_DFT;
temp += 25; /* add margin */
break;
default:
break;
}
*pClocks = temp;
}
/**
@brief void AD5940_SweepNext(SoftSweepCfg_Type *pSweepCfg, float *pNextFreq)
For sweep function, calculate next frequency point according to pSweepCfg info.
@return Return next frequency point in Hz.
*/
void AD5940_SweepNext(SoftSweepCfg_Type *pSweepCfg, float *pNextFreq)
{
float frequency;
if(pSweepCfg->SweepLog)/* Log step */
{
if(pSweepCfg->SweepStart<pSweepCfg->SweepStop) /* Normal */
{
if(++pSweepCfg->SweepIndex == pSweepCfg->SweepPoints)
pSweepCfg->SweepIndex = 0;
frequency = pSweepCfg->SweepStart*pow(10,pSweepCfg->SweepIndex*log10(pSweepCfg->SweepStop/pSweepCfg->SweepStart)/(pSweepCfg->SweepPoints-1));
}
else
{
pSweepCfg->SweepIndex --;
if(pSweepCfg->SweepIndex >= pSweepCfg->SweepPoints)
pSweepCfg->SweepIndex = pSweepCfg->SweepPoints-1;
frequency = pSweepCfg->SweepStop*pow(10,pSweepCfg->SweepIndex*
(log10(pSweepCfg->SweepStart/pSweepCfg->SweepStop)/(pSweepCfg->SweepPoints-1)));
}
}
else/* Linear step */
{
if(pSweepCfg->SweepStart<pSweepCfg->SweepStop) /* Normal */
{
if(++pSweepCfg->SweepIndex == pSweepCfg->SweepPoints)
pSweepCfg->SweepIndex = 0;
frequency = pSweepCfg->SweepStart + pSweepCfg->SweepIndex*(double)(pSweepCfg->SweepStop-pSweepCfg->SweepStart)/(pSweepCfg->SweepPoints-1);
}
else
{
pSweepCfg->SweepIndex --;
if(pSweepCfg->SweepIndex >= pSweepCfg->SweepPoints)
pSweepCfg->SweepIndex = pSweepCfg->SweepPoints-1;
frequency = pSweepCfg->SweepStop + pSweepCfg->SweepIndex*(double)(pSweepCfg->SweepStart - pSweepCfg->SweepStop)/(pSweepCfg->SweepPoints-1);
}
}
*pNextFreq = frequency;
}
/**
@brief Initialize Structure members to zero
@param pStruct: Pointer to the structure.
@param StructSize: The structure size in Byte.
@return Return None.
**/
void AD5940_StructInit(void *pStruct, uint32_t StructSize)
{
memset(pStruct, 0, StructSize);
}
/**
@brief Convert ADC Code to voltage.
@param ADCPga: The ADC PGA used for this result.
@param code: ADC code.
@param VRef1p82: the actual 1.82V reference voltage.
@return Voltage in volt.
**/
float AD5940_ADCCode2Volt(uint32_t code, uint32_t ADCPga, float VRef1p82)
{
float kFactor = 1.835/1.82;
float fVolt = 0.0;
float tmp = 0;
tmp = (int32_t)code - 32768;
switch(ADCPga)
{
case ADCPGA_1:
break;
case ADCPGA_1P5:
tmp /= 1.5f;
break;
case ADCPGA_2:
tmp /= 2.0f;
break;
case ADCPGA_4:
tmp /= 4.0f;
break;
case ADCPGA_9:
tmp /= 9.0f;
break;
default:break;
}
fVolt = tmp*VRef1p82/32768*kFactor;
return fVolt;
}
/**
* @brief Do complex number division.
* @param a: The dividend.
* @param b: The divisor.
* @return Return result.
**/
fImpCar_Type AD5940_ComplexDivFloat(fImpCar_Type *a, fImpCar_Type *b)
{
fImpCar_Type res;
float temp;
temp = b->Real*b->Real + b->Image*b->Image;
res.Real = a->Real*b->Real + a->Image*b->Image;
res.Real /= temp;
res.Image = a->Image*b->Real - a->Real*b->Image;
res.Image /= temp;
return res;
}
/**
* @brief Do complex number multiplication.
* @param a: The multiplicand.
* @param b: The multiplier .
* @return Return result.
**/
fImpCar_Type AD5940_ComplexMulFloat(fImpCar_Type *a, fImpCar_Type *b)
{
fImpCar_Type res;
res.Real = a->Real*b->Real - a->Image*b->Image;
res.Image = a->Image*b->Real + a->Real*b->Image;
return res;
}
/**
* @brief Do complex number addition.
* @param a: The addend.
* @param b: The addend .
* @return Return result.
**/
fImpCar_Type AD5940_ComplexAddFloat(fImpCar_Type *a, fImpCar_Type *b)
{
fImpCar_Type res;
res.Real = a->Real + b->Real;
res.Image = a->Image + b->Image;
return res;
}
/**
* @brief Do complex number subtraction.
* @param a: The minuend.
* @param b: The subtrahend .
* @return Return result.
**/
fImpCar_Type AD5940_ComplexSubFloat(fImpCar_Type *a, fImpCar_Type *b)
{
fImpCar_Type res;
res.Real = a->Real - b->Real;
res.Image = a->Image - b->Image;
return res;
}
/**
* @brief Do complex number division.
* @param a: The dividend.
* @param b: The divisor.
* @return Return result.
**/
fImpCar_Type AD5940_ComplexDivInt(iImpCar_Type *a, iImpCar_Type *b)
{
fImpCar_Type res;
float temp;
temp = (float)b->Real*b->Real + (float)b->Image*b->Image;
res.Real = (float)a->Real*b->Real + (float)a->Image*b->Image;
res.Real /= temp;
res.Image = (float)a->Image*b->Real - (float)a->Real*b->Image;
res.Image /= temp;
return res;
}
/**
* @brief Do complex number multiplication.
* @param a: The multiplicand.
* @param b: The multiplier .
* @return Return result.
**/
fImpCar_Type AD5940_ComplexMulInt(iImpCar_Type *a, iImpCar_Type *b)
{
fImpCar_Type res;
res.Real = (float)a->Real*b->Real - (float)a->Image*b->Image;
res.Image = (float)a->Image*b->Real + (float)a->Real*b->Image;
return res;
}
/**
* @brief Calculate the complex number magnitude.
* @param a: The complex number.
* @return Return magnitude.
**/
float AD5940_ComplexMag(fImpCar_Type *a)
{
return sqrt(a->Real*a->Real + a->Image*a->Image);
}
/**
* @brief Calculate the complex number phase.
* @param a: The complex number.
* @return Return phase.
**/
float AD5940_ComplexPhase(fImpCar_Type *a)
{
return atan2(a->Image, a->Real);
}
/**
* @brief Calculate the optimum filter settings based on signal frequency.
* @param freq: Frequency of signalr.
* @return Return FreqParams.
**/
FreqParams_Type AD5940_GetFreqParameters(float freq)
{
const uint32_t dft_table[] = {4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384};
const uint32_t sinc2osr_table[] = {1, 22,44,89,178,267,533,640,667,800,889,1067,1333};
const uint32_t sinc3osr_table[] = {2, 4, 5};
float AdcRate = 800000;
uint32_t n1 = 0; // Sample rate after ADC filters
uint32_t n2 = 0; // Sample rate after DFT block
uint32_t iCycle = 0;
FreqParams_Type freq_params;
/* High power mode */
if(freq >= 20000)
{
freq_params. DftSrc = DFTSRC_SINC3;
freq_params.ADCSinc2Osr = 0;
freq_params.ADCSinc3Osr = 2;
freq_params.DftNum = DFTNUM_8192;
freq_params.NumClks = 0;
freq_params.HighPwrMode = bTRUE;
return freq_params;
}
if(freq < 0.51)
{
freq_params. DftSrc = DFTSRC_SINC2NOTCH;
freq_params.ADCSinc2Osr = 6;
freq_params.ADCSinc3Osr = 1;
freq_params.DftNum = DFTNUM_8192;
freq_params.NumClks = 0;
freq_params.HighPwrMode = bTRUE;
return freq_params;
}
/* Start with SINC2 setting */
for(uint8_t i = 0; i<sizeof(sinc2osr_table) / sizeof(uint32_t); i++)
{
n1 = sinc2osr_table[i] * sinc3osr_table[1];
if(((AdcRate/n1) < freq * 10) && (freq<20e3))
continue;
/* Try DFT number */
for(uint32_t j = 8; j<sizeof(dft_table) / sizeof(uint32_t); j++)
{
n2 = dft_table[j];
iCycle = (uint32_t)(n1 * n2 * freq)/AdcRate;
if(iCycle < 8)
continue;
freq_params. DftSrc = DFTSRC_SINC2NOTCH;
freq_params.ADCSinc2Osr = i-1;
freq_params.ADCSinc3Osr = 1;
freq_params.DftNum = j;
freq_params.NumClks = 0;
freq_params.HighPwrMode = bFALSE;
if(n1 == 4)
{
freq_params. DftSrc = DFTSRC_SINC3;
freq_params.ADCSinc2Osr = 0;
}
return freq_params;
}
}
return freq_params;
}
/**
* @} Function_Helpers
*/
#ifdef CHIPSEL_M355
static void AD5940_D2DWriteReg(uint16_t RegAddr, uint32_t RegData)
{
if(((RegAddr>=0x1000)&&(RegAddr<=0x3014))) /* 32bit register */
*(volatile uint32_t *)(RegAddr+0x400c0000) = RegData;
else /* 16bit register */
*(volatile uint16_t *)(RegAddr+0x400c0000) = RegData;
}
static uint32_t AD5940_D2DReadReg(uint16_t RegAddr)
{
if(((RegAddr>=0x1000)&&(RegAddr<=0x3014))) /* 32bit register */
return *(volatile uint32_t *)(RegAddr+0x400c0000);
else /* 16bit register */
return *(volatile uint16_t *)(RegAddr+0x400c0000);
}
void AD5940_FIFORd(uint32_t *pBuffer, uint32_t uiReadCount)
{
while(uiReadCount--)
*pBuffer++ = *(volatile uint32_t *)(0x400c206C);
}
#else
/**
* @defgroup SPI_Block
* @brief Functions to communicate with AD5940 registers following AD5940 SPI protocols
* @{
*
* @defgroup SPI_Block_Functions
* @brief The basic SPI protocols. All functions are basic on AD5940_ReadWriteNBytes which
* provided by user.
*
* ##SPI basic protocol
* All SPI protocol starts with one-byte command word. Following are data(16B or 32B)
* There are four SPI commands available @ref SPI_Block_Const.
* @{
*/
/**
@brief Using SPI to transmit one byte and return the received byte.
@param data: The 8-bit data SPI will transmit.
@return received data.
**/
static unsigned char AD5940_ReadWrite8B(unsigned char data)
{
uint8_t tx[1], rx[1];
tx[0] = data;
AD5940_ReadWriteNBytes(tx,rx,1);
return rx[0];
}
/**
@brief Using SPI to transmit two bytes and return the received bytes.
@param data: The 16-bit data SPI will transmit.
@return received data.
**/
static uint16_t AD5940_ReadWrite16B(uint16_t data)
{
uint8_t SendBuffer[2];
uint8_t RecvBuffer[2];
SendBuffer[0] = data>>8;
SendBuffer[1] = data&0xff;
AD5940_ReadWriteNBytes(SendBuffer,RecvBuffer,2);
return (((uint16_t)RecvBuffer[0])<<8)|RecvBuffer[1];
}
/**
* @brief Using SPI to transmit four bytes and return the received bytes.
* @param data: The 32-bit data SPI will transmit.
* @return received data.
**/
static uint32_t AD5940_ReadWrite32B(uint32_t data)
{
uint8_t SendBuffer[4];
uint8_t RecvBuffer[4];
SendBuffer[0] = (data>>24)&0xff;
SendBuffer[1] = (data>>16)&0xff;
SendBuffer[2] = (data>> 8)&0xff;
SendBuffer[3] = (data )&0xff;
AD5940_ReadWriteNBytes(SendBuffer,RecvBuffer,4);
return (((uint32_t)RecvBuffer[0])<<24)|(((uint32_t)RecvBuffer[1])<<16)|(((uint32_t)RecvBuffer[2])<<8)|RecvBuffer[3];
}
/**
* @brief Write register through SPI.
* @param RegAddr: The register address.
* @param RegData: The register data.
* @return Return None.
**/
static void AD5940_SPIWriteReg(uint16_t RegAddr, uint32_t RegData)
{
/* Set register address */
AD5940_CsClr();
AD5940_ReadWrite8B(SPICMD_SETADDR);
AD5940_ReadWrite16B(RegAddr);
AD5940_CsSet();
/* Add delay here to meet the SPI timing. */
AD5940_CsClr();
AD5940_ReadWrite8B(SPICMD_WRITEREG);
if(((RegAddr>=0x1000)&&(RegAddr<=0x3014)))
AD5940_ReadWrite32B(RegData);
else
AD5940_ReadWrite16B(RegData);
AD5940_CsSet();
}
/**
* @brief Read register through SPI.
* @param RegAddr: The register address.
* @return Return register data.
**/
static uint32_t AD5940_SPIReadReg(uint16_t RegAddr)
{
uint32_t Data = 0;
/* Set register address that we want to read */
AD5940_CsClr();
AD5940_ReadWrite8B(SPICMD_SETADDR);
AD5940_ReadWrite16B(RegAddr);
AD5940_CsSet();
/* Read it */
AD5940_CsClr();
AD5940_ReadWrite8B(SPICMD_READREG);
AD5940_ReadWrite8B(0); //Dummy read
/* The real data is coming */
if((RegAddr>=0x1000)&&(RegAddr<=0x3014))
Data = AD5940_ReadWrite32B(0);
else
Data = AD5940_ReadWrite16B(0);
AD5940_CsSet();
return Data;
}
/**
@brief Read specific number of data from FIFO with optimized SPI access.
@param pBuffer: Pointer to a buffer that used to store data read back.
@param uiReadCount: How much data to be read.
@return none.
**/
void AD5940_FIFORd(uint32_t *pBuffer, uint32_t uiReadCount)
{
/* Use function AD5940_SPIReadReg to read REG_AFE_DATAFIFORD is also one method. */
uint32_t i;
if(uiReadCount < 3)
{
/* This method is more efficient when readcount < 3 */
uint32_t i;
AD5940_CsClr();
AD5940_ReadWrite8B(SPICMD_SETADDR);
AD5940_ReadWrite16B(REG_AFE_DATAFIFORD);
AD5940_CsSet();