-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADE90xx.c
More file actions
4088 lines (4006 loc) · 167 KB
/
ADE90xx.c
File metadata and controls
4088 lines (4006 loc) · 167 KB
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 lib.c
* @author Ali Moallem (https://github.com/AliMoal)
* @brief
**********************************************************************************
*
*! Copyright (c) 2021 Mahda Embedded System (MIT License)
*!
*! Permission is hereby granted, free of charge, to any person obtaining a copy
*! of this software and associated documentation files (the "Software"), to deal
*! in the Software without restriction, including without limitation the rights
*! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*! copies of the Software, and to permit persons to whom the Software is
*! furnished to do so, subject to the following conditions:
*!
*! The above copyright notice and this permission notice shall be included in all
*! copies or substantial portions of the Software.
*!
*! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*! SOFTWARE.
*!
**********************************************************************************
**/
//* Private Includes -------------------------------------------------------------- //
#include "ADE90xx.h"
// #include "driver/uart.h"
//* Private Defines and Macros ---------------------------------------------------- //
#define CHECK_REG_ADD_VALIDATION 1 // 0: Disable | 1: Enable
#define MAX_MASK 0xFFFFFFFF
//#define MAX_MASK_32 0xFFFFFFFF
//#define MAX_MASK_16 0xFFFF
//#define ReadReg(r, m, d) ((r >= 0x480 && r <= 0x4FE) ? ADE90xx_ReadReg16(ADE_Handler, r, m, d) : ADE90xx_ReadReg32(ADE_Handler, r, m, d))
//#define WriteReg(r, m, d) ((r >= 0x480 && r <= 0x4FE) ? ADE90xx_WriteReg16(ADE_Handler, r, m, d) : ADE90xx_WriteReg32(ADE_Handler, r, m, d))
//#define ReadReg16Log(r) ADE90xx_ReadReg16(ADE_Handler, r, MAX_MASK_16, NULL)
//#define ReadReg32Log(r) ADE90xx_ReadReg32(ADE_Handler, r, MAX_MASK_32, NULL)
//* Others ------------------------------------------------------------------------ //
#ifdef ADE90xx_Debug_Enable
#if ADE90xx_Debug_Enable == 1
#define PROGRAMLOG(arg...) printf(arg)
#define WRITE_REG_LOG(n, m, cv, i) if (ADE_ADD_##n >= 0x480 && ADE_ADD_##n <= 0x4FE) \
{PROGRAMLOG("%-12s | 0x%03X | 0x%04X | 0x%04X | 0x%04X | %-50s \r\n", #n, ADE_ADD_##n, m & 0xFFFF, ADE90xx_ReadRegS(ADE_Handler, ADE_ADD_##n, m, NULL), cv, i ? i : (""));} else \
{PROGRAMLOG("%-12s | 0x%03X | 0x%08X | 0x%08X | 0x%08X | %-50s \r\n" , #n, ADE_ADD_##n, m, ADE90xx_ReadRegS(ADE_Handler, ADE_ADD_##n, m, NULL), cv, i ? i : (""));}
#define READ_REG_LOG(n, m , i) if (ADE_ADD_##n >= 0x480 && ADE_ADD_##n <= 0x4FE) \
{PROGRAMLOG("%-12s | 0x%03X | 0x%04X | 0x%04X | ------ | %-50s \r\n", #n, ADE_ADD_##n, m & 0xFFFF, ADE90xx_ReadRegS(ADE_Handler, ADE_ADD_##n, m, NULL), i ? i : (""));} else \
{PROGRAMLOG("%-12s | 0x%03X | 0x%08X | 0x%08X | ---------- | %-50s \r\n" , #n, ADE_ADD_##n, m, ADE90xx_ReadRegS(ADE_Handler, ADE_ADD_##n, m, NULL), i ? i : ("")i);}
#define VALUE_LOG(s, n, func, i) PROGRAMLOG("%-12s | 0x%03X | ---------- | %-10f | ---------- | %-50s \r\n", s, ADE_ADD_##n, func, i);
#define WRITE_VAL_LOG(s, n, d, cv, i) PROGRAMLOG("%-12s | 0x%03X | ---------- | %-10f | %-10f | %-50s \r\n", s ? s : "------------", ADE_ADD_##n, d, cv, i ? i : (""));
#define REAL_VAL_LOG(s, n, d, i) PROGRAMLOG("%-12s | 0x%03X | ---------- | %-10f | ---------- | %-50s \r\n", s ? s : "------------", ADE_ADD_##n, d, i ? i : (""));
#else
#define PROGRAMLOG(arg...)
#define WRITE_REG_LOG(n, m, cv, i)
#define READ_REG_LOG(n, m, i)
#define VALUE_LOG(s, n, func, i)
#define WRITE_VAL_LOG(s, n, d, cv, i)
#define REAL_VAL_LOG(s, n, d, i)
#endif
#else
#define PROGRAMLOG(arg...)
#define WRITE_REG_LOG(n, m, cv, i)
#define READ_REG_LOG(n, m, i)
#define VALUE_LOG(s, n, func, i)
#define WRITE_VAL_LOG(s, n, d, cv, i)
#define REAL_VAL_LOG(s, n, d, i)
#endif
#ifdef ADE90xx_USE_MACRO_DELAY
#if ADE90xx_USE_MACRO_DELAY == 1
#define Delay_US(x) ADE90xx_MACRO_DELAY_US(x)
#ifndef ADE90xx_MACRO_DELAY_US
#error "ADE90xx_MACRO_DELAY_US is not defined. Please Use handler delay or config ADE90xx_MACRO_DELAY_US macro, You can choose it on ADE90xx_USE_MACRO_DELAY define"
#endif
#define Delay_MS(x) ADE90xx_MACRO_DELAY_MS(x)
#ifndef ADE90xx_MACRO_DELAY_MS
#error "ADE90xx_MACRO_DELAY_MS is not defined. Please Use handler delay or config ADE90xx_MACRO_DELAY_MS macro, You can choose it on ADE90xx_USE_MACRO_DELAY define"
#endif
#else
#define Delay_US(x) ADE_Handler->ADE_Delay_US(x)
#define Delay_MS(x) ADE_Handler->ADE_Delay_MS(x)
#endif
#else
#define Delay_US(x)
#define Delay_MS(x)
#endif
#ifdef ADE90xx_ChooseFullScale
#if ADE90xx_ChooseFullScale == 1
#define FSSinc4 67107786.0f
#define FSDec 74518668.0f
#define FSxPCF 74532013.0f
#define FSResample 18196.0f
#define FSPower 20694066.0f
#else
#define FSSinc4 67110000.0f
#define FSDec 74520000.0f
#define FSxPCF 74770000.0f
#define FSResample 18100.0f
#define FSPower 20823646.0f
#endif
#else
#define FSSinc4 67110000.0f
#define FSDec 74520000.0f
#define FSxPCF 74770000.0f
#define FSResample 18100.0f
#define FSPower 20823646.0f
#endif
/**
** ==================================================================================
** ##### Private Enums #####
** ==================================================================================
**/
/**
** ==================================================================================
** ##### Private Typedef #####
** ==================================================================================
**/
/**
** ==================================================================================
** ##### Private Struct #####
** ==================================================================================
**/
/**
** ==================================================================================
** ##### Private Variables #####
** ==================================================================================
**/
/**
** ==================================================================================
** ##### Private Union #####
** ==================================================================================
**/
/**
*! ==================================================================================
*! ##### Private Functions #####
*! ==================================================================================
**/
//static uint8_t
//ADE90xx_ReadRegUINT8(ADE90xx_Handler_t *ADE_Handler, uint16_t Reg)
//{
// ADE_Handler->ADE_SPI_CS_LOW();
// ADE_Handler->ADE_SPI_WriteByte((Reg >> 4) & 0xFF);
// ADE_Handler->ADE_SPI_WriteByte((Reg << 4) & 0xF8);
// uint8_t retval = ADE_Handler->ADE_SPI_ReadByte();
// retval = ADE_Handler->ADE_SPI_ReadByte();
// ADE_Handler->ADE_SPI_CS_HIGH();
// return retval;
//}
//static void
//ADE90xx_WriteRegUINT8(ADE90xx_Handler_t *ADE_Handler, uint16_t Reg, uint8_t Data)
//{
// ADE_Handler->ADE_SPI_CS_LOW();
// ADE_Handler->ADE_SPI_WriteByte((Reg >> 4) & 0xFF);
// ADE_Handler->ADE_SPI_WriteByte((Reg << 4) & 0xF0);
// ADE_Handler->ADE_SPI_WriteByte(0x00);
// ADE_Handler->ADE_SPI_WriteByte(Data);
// ADE_Handler->ADE_SPI_CS_HIGH();
//}
static uint16_t
ADE90xx_ReadReg16(ADE90xx_Handler_t *ADE_Handler, uint16_t Reg, uint16_t MASK, uint16_t *Data)
{
#ifdef CHECK_REG_ADD_VALIDATION
#if CHECK_REG_ADD_VALIDATION == 1
if (Reg < 0x480 || Reg > 0x4FE) {PROGRAMLOG("!!! Register 0x%X is 32bit, Use ReadReg32 function to read it.\r\n", Reg); return 0;}
#else
#endif
#else
#endif
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( (Reg >> 4) & 0xFF);
ADE_Handler->ADE_SPI_WriteByte(((Reg << 4) | 0x08) & 0xF8);
uint16_t retval = ADE_Handler->ADE_SPI_ReadByte() << 8;
retval |= ADE_Handler->ADE_SPI_ReadByte();
ADE_Handler->ADE_SPI_CS_HIGH();
#ifdef ADE90xx_SAVE_DATA_VALUE
#if ADE90xx_SAVE_DATA_VALUE == 1
if (Data) *Data = retval & MASK; else return retval & MASK;
#else
if (Data) *Data = (retval & MASK) | ((~MASK) & (*Data)); else return (retval & MASK);
#endif
#else
if (Data) *Data = (retval & MASK) | ((~MASK) & (*Data)); else return (retval & MASK);
#endif
return *Data;
}
static void
ADE90xx_WriteReg16(ADE90xx_Handler_t *ADE_Handler, uint16_t Reg, uint16_t MASK, uint16_t Data)
{
#ifdef CHECK_REG_ADD_VALIDATION
#if CHECK_REG_ADD_VALIDATION == 1
if (Reg < 0x480 || Reg > 0x4FE) {PROGRAMLOG("!!! Register 0x%X is 32bit, Use WriteReg32 function to write it.\r\n", Reg); return;}
#else
#endif
#else
#endif
// PROGRAMLOG("O");
// ADE_Handler->ADE_Delay_MS(1000);
// PROGRAMLOG("K\r\n");
#ifdef ADE90xx_SAVE_DATA_VALUE
#if ADE90xx_SAVE_DATA_VALUE == 1
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte(Reg >> 4);
ADE_Handler->ADE_SPI_WriteByte((Reg << 4) & 0xF0);
ADE_Handler->ADE_SPI_WriteByte((MASK & Data) >> 8);
ADE_Handler->ADE_SPI_WriteByte(MASK & Data);
ADE_Handler->ADE_SPI_CS_HIGH();
#else
uint16_t ReadData = (ADE90xx_ReadReg16(ADE_Handler, Reg, 0xFFFF, NULL) & (~MASK)) | (MASK & Data);
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte(Reg >> 4);
ADE_Handler->ADE_SPI_WriteByte((Reg << 4) & 0xF0);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 8);
ADE_Handler->ADE_SPI_WriteByte(ReadData);
ADE_Handler->ADE_SPI_CS_HIGH();
#endif
#else
uint16_t ReadData = (ADE90xx_ReadReg16(ADE_Handler, Reg, MASK, NULL) & (~MASK)) | (MASK & Data);
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte(Reg >> 4);
ADE_Handler->ADE_SPI_WriteByte((Reg << 4) & 0xF0);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 8);
ADE_Handler->ADE_SPI_WriteByte(ReadData);
ADE_Handler->ADE_SPI_CS_HIGH();
#endif
}
static uint32_t
ADE90xx_ReadReg32(ADE90xx_Handler_t *ADE_Handler, uint16_t Reg, uint32_t MASK, uint32_t *Data)
{
#ifdef CHECK_REG_ADD_VALIDATION
#if CHECK_REG_ADD_VALIDATION == 1
if (Reg >= 0x480 && Reg <= 0x4FE) {PROGRAMLOG("!!! Register 0x%X is 16bit, Use ReadReg16 function to Read it.\r\n", Reg); return 0;}
#else
#endif
#else
#endif
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte(Reg >> 4);
ADE_Handler->ADE_SPI_WriteByte(((Reg << 4) | 0x08) & 0xF8);
uint32_t retval = ADE_Handler->ADE_SPI_ReadByte() << 24;
retval |= ADE_Handler->ADE_SPI_ReadByte() << 16;
retval |= ADE_Handler->ADE_SPI_ReadByte() << 8;
retval |= ADE_Handler->ADE_SPI_ReadByte();
ADE_Handler->ADE_SPI_CS_HIGH();
#ifdef ADE90xx_SAVE_DATA_VALUE
#if ADE90xx_SAVE_DATA_VALUE == 1
if (Data) *Data = retval & MASK; else return retval & MASK;
#else
if (Data) *Data = (retval & MASK) | ((~MASK) & (*Data)); else return (retval & MASK);
#endif
#else
if (Data) *Data = (retval & MASK) | ((~MASK) & (*Data)); else return (retval & MASK);
#endif
return *Data;
}
static void
ADE90xx_WriteReg32(ADE90xx_Handler_t *ADE_Handler, uint16_t Reg, uint32_t MASK, uint32_t Data)
{
#ifdef CHECK_REG_ADD_VALIDATION
#if CHECK_REG_ADD_VALIDATION == 1
if (Reg >= 0x480 && Reg <= 0x4FE) {PROGRAMLOG("!!! Register 0x%X is 16bit, Use WriteReg16 function to write it.\r\n", Reg); return;}
#else
#endif
#else
#endif
#ifdef ADE90xx_SAVE_DATA_VALUE
#if ADE90xx_SAVE_DATA_VALUE == 1
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte(Reg >> 4);
ADE_Handler->ADE_SPI_WriteByte((Reg << 4) & 0xF0);
ADE_Handler->ADE_SPI_WriteByte((MASK & Data) >> 24);
ADE_Handler->ADE_SPI_WriteByte((MASK & Data) >> 16);
ADE_Handler->ADE_SPI_WriteByte((MASK & Data) >> 8);
ADE_Handler->ADE_SPI_WriteByte(MASK & Data);
ADE_Handler->ADE_SPI_CS_HIGH();
#else
uint32_t ReadData = (ADE90xx_ReadReg32(ADE_Handler, Reg, 0xFFFFFFFF, NULL) & (~MASK)) | (MASK & Data);
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte(Reg >> 4);
ADE_Handler->ADE_SPI_WriteByte((Reg << 4) & 0xF0);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 24);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 16);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 8);
ADE_Handler->ADE_SPI_WriteByte(ReadData);
ADE_Handler->ADE_SPI_CS_HIGH();
#endif
#else
uint32_t ReadData = (ADE90xx_ReadReg32(ADE_Handler, Reg, MASK, NULL) & (~MASK)) | (MASK & Data);
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte(Reg >> 4);
ADE_Handler->ADE_SPI_WriteByte((Reg << 4) & 0xF0);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 24);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 16);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 8);
ADE_Handler->ADE_SPI_WriteByte(ReadData);
ADE_Handler->ADE_SPI_CS_HIGH();
#endif
}
static uint32_t
ADE90xx_ReadRegS(ADE90xx_Handler_t *ADE_Handler, uint16_t Reg, uint32_t MASK, uint32_t *Data)
{
if (Reg >= 0x480 && Reg <= 0x4FE) // 16bit Register
return ADE90xx_ReadReg16(ADE_Handler, Reg, MASK, (uint16_t *)Data);
else // 32bit Register
return ADE90xx_ReadReg32(ADE_Handler, Reg, MASK, Data);
}
static void
ADE90xx_WriteRegS(ADE90xx_Handler_t *ADE_Handler, uint16_t Reg, uint32_t MASK, uint32_t Data)
{
if (Reg >= 0x480 && Reg <= 0x4FE) // 16bit Register
ADE90xx_WriteReg16(ADE_Handler, Reg, MASK, Data);
else // 32bit Register
ADE90xx_WriteReg32(ADE_Handler, Reg, MASK, Data);
}
#define ADE90xx_WriteRegLog(n, m, d, i) ADE90xx_WriteRegS(ADE_Handler, ADE_ADD_##n, m, d); WRITE_REG_LOG(n, m, d, i ? i : (""))
#define ADE90xx_ReadRegLog(n, m, i) ADE90xx_ReadRegS(ADE_Handler, ADE_ADD_##n, m, NULL); READ_REG_LOG(n, m, i ? i : (""))
#define ADE90xx_ValLogFunc(n, m) ((FPType)ADE90xx_ReadRegS(ADE_Handler, ADE_ADD_##n, m, NULL))
#define ADE90xx_WriteValLog(s, n, m, d, func, i) ADE90xx_WriteRegS(ADE_Handler, ADE_ADD_##n, m, d); VALUE_LOG(s ? s : "------------", n, func, i ? i : (""))
#define ADE90xx_ReadValLog(s, n, m, func, i) ADE90xx_ReadRegS(ADE_Handler, ADE_ADD_##n, m, NULL); VALUE_LOG(s ? s : "------------", n, func, i ? i : (""))
#define ADE90xx_Write(n, m, d) ADE90xx_WriteRegS(ADE_Handler, ADE_ADD_##n, m, d)
#define ADE90xx_WriteRLog(n, m, d, i) WRITE_REG_LOG(n, m, d, i)
#define ADE90xx_WriteVLog(s, n, d, cv, i) WRITE_VAL_LOG(s, n, d, cv, i)
#define ADE90xx_Read(n, m) ADE90xx_ReadRegS(ADE_Handler, ADE_ADD_##n, m, NULL)
#define ADE90xx_ReadRLog(n, m, i) READ_REG_LOG(n, m, i)
#define ADE90xx_ReadVLog(s, n, d, i) REAL_VAL_LOG(s, n, d, i)
#ifdef ADE90xx_BLOCKING_MODE
#if ADE90xx_BLOCKING_MODE == 2
static ADE90xx_INLINE_ATTRIBUTE bool
ADE90xx_CheckIRQ0(ADE90xx_Handler_t *ADE_Handler)
{
if (ADE_Handler->ADE_IRQ0_Read)
return ADE_Handler->ADE_IRQ0_Read();
return 1;
}
static ADE90xx_INLINE_ATTRIBUTE bool
ADE90xx_CheckIRQ1(ADE90xx_Handler_t *ADE_Handler)
{
if (ADE_Handler->ADE_IRQ1_Read)
return ADE_Handler->ADE_IRQ1_Read();
return 1;
}
#elif ADE90xx_BLOCKING_MODE == 1
static ADE90xx_INLINE_ATTRIBUTE bool
ADE90xx_CheckIRQ0(ADE90xx_Handler_t *ADE_Handler)
{
if (ADE_Handler->IsActiveIRQ0)
{
ADE_Handler->IsActiveIRQ0 = false;
return 0; // It means IRQ0 pin is low
}
return 1;
}
static ADE90xx_INLINE_ATTRIBUTE bool
ADE90xx_CheckIRQ1(ADE90xx_Handler_t *ADE_Handler)
{
if (ADE_Handler->IsActiveIRQ1)
{
ADE_Handler->IsActiveIRQ1 = false;
return 0; // It means IRQ1 pin is low
}
return 1;
}
#endif
#if ADE90xx_BLOCKING_MODE == 1 || ADE90xx_BLOCKING_MODE == 2
static bool
ADE90xx_CheckInt(ADE90xx_Handler_t *ADE_Handler, bool Statusx, uint8_t Bitx)
{
bool flag = false;
if (Statusx)
{
// #ifdef ADE90xx_BLOCKING_MODE
// #if ADE90xx_BLOCKING_MODE == 1 || ADE90xx_BLOCKING_MODE == 2
// while(ADE90xx_CheckIRQ1(ADE_Handler)){}
// #endif
// #endif
flag = ADE90xx_ReadRegLL(ADE_Handler, ADE_ADD_STATUS1, ADE_SIZE_STATUS1, Bitx, 1, NULL);
if (flag && (Bitx != ADE_MB_ERROR1 && Bitx != ADE_MB_ERROR0))
ADE90xx_WriteRegLL(ADE_Handler, ADE_ADD_STATUS1, ADE_SIZE_STATUS1, Bitx, 1, 1);
}
else
{
// #ifdef ADE90xx_BLOCKING_MODE
// #if ADE90xx_BLOCKING_MODE == 1 || ADE90xx_BLOCKING_MODE == 2
// while(ADE90xx_CheckIRQ0(ADE_Handler)){}
// #endif
// #endif
flag = ADE90xx_ReadRegLL(ADE_Handler, ADE_ADD_STATUS1, ADE_SIZE_STATUS0, Bitx, 1, NULL);
if (flag)
ADE90xx_WriteRegLL(ADE_Handler, ADE_ADD_STATUS1, ADE_SIZE_STATUS0, Bitx, 1, 1);
}
return flag;
}
static void
ADE90xx_IntHandler(ADE90xx_Handler_t *ADE_Handler, bool Statusx, uint8_t Bitx)
{
bool NotPassInt = true;
bool IntPresence = false;
uint16_t CounterSafe = 0;
while(NotPassInt)
{
IntPresence = false;
if(Statusx)
{
#ifdef ADE90xx_BLOCKING_MODE
#if ADE90xx_BLOCKING_MODE == 1
if (ADE_Handler->IsActiveIRQ1)
{
ADE_Handler->IsActiveIRQ1 = false;
IntPresence = true; // It means IRQ1 pin is low
}
#elif ADE90xx_BLOCKING_MODE == 2
if (ADE_Handler->ADE_IRQ1_Read)
IntPresence = !ADE_Handler->ADE_IRQ1_Read();
#endif
#endif
}
else
{
#ifdef ADE90xx_BLOCKING_MODE
#if ADE90xx_BLOCKING_MODE == 1
if (ADE_Handler->IsActiveIRQ0)
{
ADE_Handler->IsActiveIRQ0 = false;
IntPresence = true; // It means IRQ0 pin is low
}
#elif ADE90xx_BLOCKING_MODE == 2
if (ADE_Handler->ADE_IRQ0_Read)
IntPresence = !ADE_Handler->ADE_IRQ0_Read();
#endif
#endif
}
if(IntPresence)
{
if (Statusx)
{
NotPassInt = ADE90xx_ReadRegLL(ADE_Handler, ADE_ADD_STATUS1, ADE_SIZE_STATUS1, Bitx, 1, NULL);
if (NotPassInt && (Bitx != ADE_MB_ERROR1 && Bitx != ADE_MB_ERROR0))
ADE90xx_WriteRegLL(ADE_Handler, ADE_ADD_STATUS1, ADE_SIZE_STATUS1, Bitx, 1, 1);
}
else
{
NotPassInt = ADE90xx_ReadRegLL(ADE_Handler, ADE_ADD_STATUS0, ADE_SIZE_STATUS0, Bitx, 1, NULL);
if (NotPassInt)
ADE90xx_WriteRegLL(ADE_Handler, ADE_ADD_STATUS0, ADE_SIZE_STATUS0, Bitx, 1, 1);
}
NotPassInt = NotPassInt ? false : true;
}
if (CounterSafe == 0xFFFF)
{
PROGRAMLOG("Error To Find Interrupt, Status:%d Bit:%d\r\n", Statusx, Bitx);
return;
}
else
CounterSafe++;
}
}
#endif
#endif
// //uint16_t DataCo[512] = {0};
// static void
// ADE90xx_ReadWFBBurst(ADE90xx_Handler_t *ADE_Handler, uint32_t MASK, uint16_t *Data, uint16_t NumOfData)
// {
// ADE_Handler->ADE_SPI_CS_LOW();
// ADE_Handler->ADE_SPI_WriteByte(0x80);
// ADE_Handler->ADE_SPI_WriteByte(0x08);
// for(uint16_t DateBrustCounter = 0; DateBrustCounter < NumOfData; DateBrustCounter++)
// Data[DateBrustCounter] = (ADE_Handler->ADE_SPI_ReadByte() << 8) | ADE_Handler->ADE_SPI_ReadByte();
// ADE_Handler->ADE_SPI_CS_HIGH();
// #ifdef ADE90xx_SAVE_DATA_VALUE
// #if ADE90xx_SAVE_DATA_VALUE == 1
// #else
// for(uint16_t MASKCounter = 0; MASKCounter < NumOfData; MASKCounter++)
// Data[MASKCounter] &= MASK;
// #endif
// #else
// for(uint16_t MASKCounter = 0; MASKCounter < NumOfData; MASKCounter++)
// Data[MASKCounter] &= MASK;
// #endif
// }
/**
** ==================================================================================
** ##### Public Functions #####
** ==================================================================================
**/
//---------------------------- LOW LEVEL ----------------------------//
void
ADE90xx_InitLL(void)
{
PROGRAMLOG("\r\n");
PROGRAMLOG(" Name | Reg Add | Mask | Value | Correct Value | Information \r\n");
PROGRAMLOG("-------------|---------|------------|------------|---------------|-------------\r\n");
}
uint32_t
ADE90xx_ReadRegLL(ADE90xx_Handler_t *ADE_Handler, uint16_t RegAdd, uint8_t RegSize, uint8_t StartBit, uint32_t MaskBit, uint32_t *Data)
{
#ifdef CHECK_REG_ADD_VALIDATION
#if CHECK_REG_ADD_VALIDATION == 1
if ((RegAdd < 0x480 || RegAdd > 0x4FE ) && (RegSize == 16)) {PROGRAMLOG("!!! Register 0x%X is 32bit, RegSize must be 32. make it correct and then recall the function\r\n", RegAdd); return 0;}
if ((RegAdd >= 0x480 && RegAdd <= 0x4FE) && (RegSize == 32)) {PROGRAMLOG("!!! Register 0x%X is 16bit, RegSize must be 16. make it correct and then recall the function\r\n", RegAdd); return 0;}
#else
#endif
#else
#endif
uint32_t retval = 0;
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( RegAdd >> 4);
ADE_Handler->ADE_SPI_WriteByte(((RegAdd << 4) | 0x08) & 0xF8);
if (RegSize == 16) // 16bit Register
{
retval = ADE_Handler->ADE_SPI_ReadByte() << 8;
retval |= ADE_Handler->ADE_SPI_ReadByte();
}
else // 32bit Register
{
retval = ADE_Handler->ADE_SPI_ReadByte() << 24;
retval |= ADE_Handler->ADE_SPI_ReadByte() << 16;
retval |= ADE_Handler->ADE_SPI_ReadByte() << 8;
retval |= ADE_Handler->ADE_SPI_ReadByte();
}
ADE_Handler->ADE_SPI_CS_HIGH();
// #ifdef ADE90xx_SAVE_DATA_VALUE
// #if ADE90xx_SAVE_DATA_VALUE == 1
// if (Data) *Data = ((retval >> StartBit) & MaskBit); else return ((retval >> StartBit) & MaskBit);
// #else
// if (Data) *Data = ((retval >> StartBit) & MaskBit) | ((~(MaskBit << StartBit)) & (*Data)); else return ((retval >> StartBit) & MaskBit);
// #endif
// #else
// if (Data) *Data = ((retval >> StartBit) & MaskBit) | ((~(MaskBit << StartBit)) & (*Data)); else return ((retval >> StartBit) & MaskBit);
// #endif
#ifdef ADE90xx_Debug_Enable
#if ADE90xx_Debug_Enable == 1
if (RegSize == 16)
PROGRAMLOG("------------ | 0x%03X | 0x%04X | 0x%04X | ------ | ----------- \r\n", RegAdd, (MaskBit << StartBit) & 0xFFFF, retval & (MaskBit << StartBit));
else
PROGRAMLOG("------------ | 0x%03X | 0x%08X | 0x%08X | ---------- | ----------- \r\n" , RegAdd, (MaskBit << StartBit), retval & (MaskBit << StartBit));
#else
#endif
#else
#endif
if (Data)
{
*Data = ((retval >> StartBit) & MaskBit);
return *Data;
}
else
return ((retval >> StartBit) & MaskBit);
}
void
ADE90xx_WriteRegLL(ADE90xx_Handler_t *ADE_Handler, uint16_t RegAdd, uint8_t RegSize, uint8_t StartBit, uint32_t MaskBit, uint32_t Data)
{
#ifdef CHECK_REG_ADD_VALIDATION
#if CHECK_REG_ADD_VALIDATION == 1
if ((RegAdd < 0x480 || RegAdd > 0x4FE ) && (RegSize == 16)) {PROGRAMLOG("!!! Register 0x%X is 32bit, RegSize must be 32. make it correct and then recall the function\r\n", RegAdd); return;}
if ((RegAdd >= 0x480 && RegAdd <= 0x4FE) && (RegSize == 32)) {PROGRAMLOG("!!! Register 0x%X is 16bit, RegSize must be 16. make it correct and then recall the function\r\n", RegAdd); return;}
#else
#endif
#else
#endif
if (RegSize == 16)
{
#ifdef ADE90xx_SAVE_DATA_VALUE
#if ADE90xx_SAVE_DATA_VALUE == 1
uint16_t ReadData = (MaskBit & Data) << StartBit;
#else
uint16_t ReadData = 0;
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( RegAdd >> 4);
ADE_Handler->ADE_SPI_WriteByte(((RegAdd << 4) | 0x08) & 0xF8);
ReadData = ADE_Handler->ADE_SPI_ReadByte() << 8;
ReadData |= ADE_Handler->ADE_SPI_ReadByte();
ADE_Handler->ADE_SPI_CS_HIGH();
ReadData = (ReadData & (~(MaskBit << StartBit))) | ((MaskBit & Data) << StartBit);
#endif
#else
uint16_t ReadData = 0;
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( RegAdd >> 4);
ADE_Handler->ADE_SPI_WriteByte(((RegAdd << 4) | 0x08) & 0xF8);
ReadData = ADE_Handler->ADE_SPI_ReadByte() << 8;
ReadData |= ADE_Handler->ADE_SPI_ReadByte();
ADE_Handler->ADE_SPI_CS_HIGH();
ReadData = (ReadData & (~(MaskBit << StartBit))) | ((MaskBit & Data) << StartBit);
#endif
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( RegAdd >> 4);
ADE_Handler->ADE_SPI_WriteByte((RegAdd << 4) & 0xF0);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 8);
ADE_Handler->ADE_SPI_WriteByte(ReadData);
ADE_Handler->ADE_SPI_CS_HIGH();
#ifdef ADE90xx_Debug_Enable
#if ADE90xx_Debug_Enable == 1
uint16_t ReadDebug = 0;
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( RegAdd >> 4);
ADE_Handler->ADE_SPI_WriteByte(((RegAdd << 4) | 0x08) & 0xF8);
ReadDebug = ADE_Handler->ADE_SPI_ReadByte() << 8;
ReadDebug |= ADE_Handler->ADE_SPI_ReadByte();
ADE_Handler->ADE_SPI_CS_HIGH();
PROGRAMLOG("------------ | 0x%03X | 0x%04X | 0x%04X | 0x%04X | ------ | ----------- \r\n", RegAdd, (MaskBit << StartBit) & 0xFFFF, ReadDebug & (MaskBit << StartBit), (MaskBit & Data) << StartBit);
#else
#endif
#else
#endif
}
else
{
#ifdef ADE90xx_SAVE_DATA_VALUE
#if ADE90xx_SAVE_DATA_VALUE == 1
uint32_t ReadData = (MaskBit & Data) << StartBit;
#else
uint32_t ReadData = 0;
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( RegAdd >> 4);
ADE_Handler->ADE_SPI_WriteByte(((RegAdd << 4) | 0x08) & 0xF8);
ReadData = ADE_Handler->ADE_SPI_ReadByte() << 24;
ReadData |= ADE_Handler->ADE_SPI_ReadByte() << 16;
ReadData |= ADE_Handler->ADE_SPI_ReadByte() << 8;
ReadData |= ADE_Handler->ADE_SPI_ReadByte();
ADE_Handler->ADE_SPI_CS_HIGH();
ReadData = (ReadData & (~(MaskBit << StartBit))) | ((MaskBit & Data) << StartBit);
#endif
#else
uint32_t ReadData = 0;
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( RegAdd >> 4);
ADE_Handler->ADE_SPI_WriteByte(((RegAdd << 4) | 0x08) & 0xF8);
ReadData = ADE_Handler->ADE_SPI_ReadByte() << 24;
ReadData |= ADE_Handler->ADE_SPI_ReadByte() << 16;
ReadData |= ADE_Handler->ADE_SPI_ReadByte() << 8;
ReadData |= ADE_Handler->ADE_SPI_ReadByte();
ADE_Handler->ADE_SPI_CS_HIGH();
ReadData = (ReadData & (~(MaskBit << StartBit))) | ((MaskBit & Data) << StartBit);
#endif
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( RegAdd >> 4);
ADE_Handler->ADE_SPI_WriteByte((RegAdd << 4) & 0xF0);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 24);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 16);
ADE_Handler->ADE_SPI_WriteByte(ReadData >> 8);
ADE_Handler->ADE_SPI_WriteByte(ReadData);
ADE_Handler->ADE_SPI_CS_HIGH();
#ifdef ADE90xx_Debug_Enable
#if ADE90xx_Debug_Enable == 1
uint32_t ReadDebug = 0;
ADE_Handler->ADE_SPI_CS_LOW();
ADE_Handler->ADE_SPI_WriteByte( RegAdd >> 4);
ADE_Handler->ADE_SPI_WriteByte(((RegAdd << 4) | 0x08) & 0xF8);
ReadDebug = ADE_Handler->ADE_SPI_ReadByte() << 24;
ReadDebug |= ADE_Handler->ADE_SPI_ReadByte() << 16;
ReadDebug |= ADE_Handler->ADE_SPI_ReadByte() << 8;
ReadDebug |= ADE_Handler->ADE_SPI_ReadByte();
ADE_Handler->ADE_SPI_CS_HIGH();
PROGRAMLOG("------------ | 0x%03X | 0x%08X | 0x%08X | 0x%08X | ---------- | ----------- \r\n", RegAdd, (MaskBit << StartBit), ReadDebug & (MaskBit << StartBit), (MaskBit & Data) << StartBit);
#else
#endif
#else
#endif
}
}
//------------------------------ BASIC ------------------------------//
void
ADE90xx_Init(ADE90xx_Handler_t *ADE_Handler, bool UseRogowskiCoil, bool UseINChannel, bool DisableVAR)
{
if (!ADE_Handler->ADE_SPI_CS_HIGH) return;
if (!ADE_Handler->ADE_SPI_CS_LOW) return;
if (!ADE_Handler->ADE_SPI_WriteByte) return;
if (!ADE_Handler->ADE_SPI_ReadByte) return;
#ifdef ADE90xx_BLOCKING_MODE
#if ADE90xx_BLOCKING_MODE == 2
if (!ADE_Handler->ADE_IRQ0_Read) return;
if (!ADE_Handler->ADE_IRQ1_Read) return;
#endif
#endif
PROGRAMLOG("---------------\r\n");
PROGRAMLOG("ADE90xx Init...\r\n");
#ifdef ADE90xx_BLOCKING_MODE
#if ADE90xx_BLOCKING_MODE == 1
ADE_Handler->IsActiveIRQ0 = false;
ADE_Handler->IsActiveIRQ1 = false;
#endif
#endif
if (ADE_Handler->ADE_PM0_LOW) ADE_Handler->ADE_PM0_LOW(); // For normal operation
if (ADE_Handler->ADE_PM1_LOW) ADE_Handler->ADE_PM1_LOW(); // For normal operation
/*
// Quick Start:
// 1. Wait for the RSTDONE interrupt, indicated by the IRQ pin going low
// 2. Configure PSM0 normal power mode by setting the PM1 pin and the PM0 pin to low
// 3. Configure PGA gain on current and voltage channels using the PGA_GAIN gain register. The default gain on all channels is 1
// 4. Configure the HPFDIS bits in the CONFIG0 register to enable/disable the high-pass filter. The high-pass filter is
// enabled by default. It is recommended to keep the highpass filter enabled. Set the desired corner frequency for
// HPF using the HPF_CRN bits in the CONFIG2 register. The default value for HPF_CRN is 6 (1.25 Hz).
// 5. If Rogowski coils are used as current sensors, enable the digital integrator using the INTEN and INITEN bits in the
// CONFIG0 register. Disable the integrator when using current transformers. The digital integrators are disabled by default
// a If an integrator is enabled, set the DICOEFF register to 0xFFFFE000.
// 6. Configure the expected fundamental frequency using the SELFREQ bit (50 Hz: SELFREQ = 0, 60 Hz: SELFREQ = 1)
// in the ACCMODE register, and program the nominal voltage in the VLEVEL register for fundamental calculations.
// VLEVEL = X * 1,144,084, where X is the dynamic range that the nominal signal is at with respect to full scale.
// 7. Configure the zero-crossing source for ZX detection. If ZX_SRC_SEL = 1 in the CONFIG0 register, data before the
// HPF, integrator, and phase compensation is used. If ZX_SRC_SEL = 0, data after the HPF, integrator, and phase
// compensation is used. It is recommended to have ZX_SRC_SEL = 0
// 8. Set VCONSEL = 000 in the ACCMODE register for a 3-phase, 4-wire wye configuration.
// 9. If energy is monitored using the CF outputs, configure the following registers. Skip this section if the CF outputs are not used.
// a Configure the CFxSEL bits in the CFMODE register to select the energy type to monitor.
// b Configure the TERMSELx bits in the COMPMODE register to select the phases to include in the CF calculation.
// c Program xTHR to 0x00100000.
// d Compute and program the corresponding CFxDEN register based on the desired impulses per kilowatt-hour.
// e Configure the CF pulse width using the CF_LCFG register.
// 10. If energy is monitored using energy registers, configure the following registers:
// a Configure the WATTACC and VARACC bits in the ACCMODE register to select amongst available accumulation modes (for example: signed, absolute,
// positive, or negative accumulation mode). The default accumulation mode is signed
// b Configure the NOLOAD_TMR bits in the EP_CFG register and set the ACT_NL_LVL, REACT_NL_LVL,
// and APP_NL_LVL level registers to detect no load and prevent energy accumulation of noise.
// c Configure the EGY_TMR_MODE bit in the EP_CFG register to select sample (EGY_TMR_MODE = 0) or
// line cycle (EGY_TMR_MODE = 1) accumulation. Set the desired samples or half line cycles in the EGY_TIME register.
// d Configure the EGY_LD_ACCUM bit in the EP_CFG register to add the internal energy register to user energy
// register on EGYRDY (EGY_LD_ACCUM = 0), or to overwrite the user energy register with the internal energy register value (EGY_LD_ACCUM = 1).
// e Configure the RD_RST_EN bit in the EP_CFG register to enable reset of user energy registers on read
// (RD_RST_EN = 1), or to disable reset of user energy registers on read (RD_RST_EN = 0).
// 11. The ADE90xx can provide interrupts for a variety of events on the IRQ0 and IRQ1 pins. The MASK0 or MASK1 and
// STATUS0 or STATUS1 registers manage the respective interrupt pins.
// 12. See the Power Quality Measurements section to configure the power quality parameters.
// 13. See the Waveform Buffer section to configure and use the waveform buffer.
// 14. Enable the DSP by setting the RUN register = 1, and enable energy accumulation by setting the EGY_PWR_EN bit in the EP_CFG register = 1.
// WriteReg(ADE_ADD_RUN, 1, 1);
// 15. Note that calibration is performed once at typical operating conditions. When the calibration values are computed,
// write the constants to registers before enabling the DSP
// 16. To prevent any changes to the ADE90xx configuration, enable write protection by writing 0x3C64 to the WR_LOCK register
*/
PROGRAMLOG("ADE90xx Software Reset...\r\n");
ADE90xx_WriteRegS(ADE_Handler, ADE_ADD_CONFIG1, 1, 1); // Software Reset
// Quick Start: (ADE9078)
#ifdef ADE90xx_BLOCKING_MODE
#if ADE90xx_BLOCKING_MODE == 1 || ADE90xx_BLOCKING_MODE == 2
// 1. Wait for the RSTDONE interrupt, indicated by the IRQ1 pin going low
while(ADE90xx_CheckIRQ1(ADE_Handler));
PROGRAMLOG("RSTDONE interrupt detected...\r\n");
#else
Delay_MS(100); // To wait for interrupt
#endif
#else
Delay_MS(100); // To wait for interrupt
#endif
PROGRAMLOG("\r\n");
PROGRAMLOG(" Name | Reg Add | Mask | Value | Correct Value | Information \r\n");
PROGRAMLOG("-------------|---------|------------|------------|---------------|-------------\r\n");
// 1.1. Tie IRQ1 to High
// ADE90xx_WriteRegS(ADE_Handler, ADE_ADD_STATUS1, 1 << 16, 1 << 16); // Turn Off RSTDONE status
ADE90xx_Write(STATUS1, 1 << 16 , 1 << 16); // Turn Off RSTDONE status
// // ADE90xx_WriteRegLog(STATUS1, 1 << 16, 1 << 16, NULL);
// 2. Configure the xIGAIN, xVGAIN, and xPGAIN registers via the SPI to calibrate the measurements
// USING DEFAULT: xIGAIN, xVGAIN, xPGAIN
// 3. If other calibration values are required, for example, to improve rms performance at low input signal levels, write these registers.
// USING DEFAULT
// 4. If the CFx pulse output is used, configure the CFxDEN and xTHR registers.
// USING DEFAULT: CFxDEN, xTHR
// 5. Configure the expected fundamental frequency (50 Hz or 60 Hz network) in the SELFREQ bit and write VLEVEL = 0x117514.
// USING DEFAULT: SELFREQ
ADE90xx_WriteRegLog(VLEVEL, MAX_MASK, 0x117514, NULL);
// ADE90xx_Write(VLEVEL, MAX_MASK, 0x117514);
// ADE90xx_WriteRLog(VLEVEL, MAX_MASK, 0x117514, NULL);
// 6. If a Rogowski coil sensor is used, write the INTEN bit in the CONFIG0 register to enable the digital integrator on
// the IA, IB, and IC channels. To enable the digital integrator on the neutral current, IN, channel, set the ININTEN bit.
// Additionally, write DICOEF = 0xFFFFE000 to configure the digital integrator. If current transformers are used,
// INTEN and ININTEN in the CONFIG0 register must = 0.
if (UseRogowskiCoil)
{
if (UseINChannel)
{
ADE90xx_WriteRegLog(CONFIG0, MAX_MASK, (1 << 5 /*INTEN*/) | (1 << 11 /*ININTEN*/) /*| (1 << 3 HPFDIS)*/, "Enable INTEN & ININTEN");
// ADE90xx_Write(CONFIG0, MAX_MASK, (1 << 5 /*INTEN*/) | (1 << 11 /*ININTEN*/) /*| (1 << 3 HPFDIS)*/);
// ADE90xx_WriteRLog(CONFIG0, MAX_MASK, (1 << 5 /*INTEN*/) | (1 << 11 /*ININTEN*/) /*| (1 << 3 HPFDIS)*/, "Enable INTEN & ININTEN");
}
else
{
ADE90xx_WriteRegLog(CONFIG0, MAX_MASK, 1 << 5 /*INTEN*/, "Enable INTEN");
// ADE90xx_Write(CONFIG0, MAX_MASK, 1 << 5 /*INTEN*/);
// ADE90xx_WriteRLog(CONFIG0, MAX_MASK, 1 << 5 /*INTEN*/, "Enable INTEN");
}
ADE90xx_WriteRegLog(DICOEFF, MAX_MASK, 0xFFFFE000, NULL);
// ADE90xx_Write(DICOEFF, MAX_MASK, 0xFFFFE000);
// ADE90xx_WriteRLog(DICOEFF, MAX_MASK, 0xFFFFE000, NULL);
}
// 7. If the service bring measured is something other than 4-wire wye, see Table 24 to determine how to configure
// ICONSEL and VCONSEL in the ACCMODE register.
//TODO: NEED TO IMPLEMENT
// USING DEFAULT: VCONSEL, ICONSEL
// 7.1. Disable calculating VAR
if (DisableVAR)
{
ADE90xx_WriteRegLog(VAR_DIS, MAX_MASK, 1, "Disable VAR calculation");
// ADE90xx_Write(VAR_DIS, MAX_MASK, 1);
// ADE90xx_WriteRLog(VAR_DIS, MAX_MASK, 1, "Disable VAR calculation");
}
// 8. Write a 1 to the run register.
ADE90xx_WriteRegLog(RUN, MAX_MASK, 1, "Run Program");
// ADE90xx_Write(RUN, MAX_MASK, 1);
// ADE90xx_WriteRLog(RUN, MAX_MASK, 1, "Run Program");
// 9. Write a 1 to the EP_CFG register
ADE90xx_WriteRegLog(EP_CFG, MAX_MASK, 1, "Enable Accumulated Power & Energy");
// ADE90xx_Write(EP_CFG, MAX_MASK, 1);
// ADE90xx_WriteRLog(EP_CFG, MAX_MASK, 1, "Enable Accumulated Power & Energy");
ADE_Handler->PWR_TIME_VAL = 0xFF;
ADE_Handler->EGY_TIME_VAL = 0xFF;
// // ADE90xx_ReadRegLog(PART_ID, MAX_MASK);
// // ADE90xx_ReadRegLog(VERSION, MAX_MASK);
// // ADE90xx_ReadRegLog(LAST_DATA_32, MAX_MASK);
// // ADE90xx_ReadRegLog(LAST_DATA_16, MAX_MASK);
// // ADE90xx_ReadRegLog(LAST_CMD, MAX_MASK);
}
void
ADE90xx_Reset(ADE90xx_Handler_t *ADE_Handler)
{
PROGRAMLOG("ADE90xx Software Reset...\r\n\r\n");
// ADE90xx_WriteReg16(ADE_Handler, ADE_ADD_CONFIG1, 1, 1);
ADE90xx_Write(CONFIG1, 1, 1);
}
//------------------------- CONFIGURATIONS --------------------------//
void
ADE90xx_SetMainConfig(ADE90xx_Handler_t *ADE_Handler, uint16_t Command, uint16_t Data)
{
switch(Command)
{
// CONFIG0:
case ADE_DISRPLPF:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 13, Data << 13, "ADE_DISRPLPF");
break;
}
case ADE_DISAPLPF:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 12, Data << 12, "ADE_DISAPLPF");
break;
}
case ADE_ININTEN:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 11, Data << 11, "ADE_ININTEN");
break;
}
case ADE_VNOMC_EN:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 10, Data << 10, "ADE_VNOMC_EN");
break;
}
case ADE_VNOMB_EN:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 9, Data << 9, "ADE_VNOMB_EN");
break;
}
case ADE_VNOMA_EN:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 8, Data << 8, "ADE_VNOMA_EN");
break;
}
case ADE_ZX_SRC_SEL:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 6, Data << 6, "ADE_ZX_SRC_SEL");
break;
}
case ADE_INTEN:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 5, Data << 5, "ADE_INTEN");
break;
}
case ADE_MTEN:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 4, Data << 4, "ADE_MTEN");
break;
}
case ADE_HPFDIS:
{
ADE90xx_WriteRegLog(CONFIG0, 1 << 3, Data << 3, "ADE_HPFDIS");
break;
}
case ADE_ISUM_CFG:
{
ADE90xx_WriteRegLog(CONFIG0, 3, Data, "ADE_ISUM_CFG");
break;
}
// CONFIG1:
case ADE_EXT_REF:
{
ADE90xx_WriteRegLog(CONFIG1, 1 << 15, Data << 15, "ADE_EXT_REF");
break;
}
case ADE_IRQ0_ON_IRQ1:
{
ADE90xx_WriteRegLog(CONFIG1, 1 << 12, Data << 12, "ADE_IRQ0_ON_IRQ1");
break;
}
case ADE_BURST_EN:
{
ADE90xx_WriteRegLog(CONFIG1, 1 << 11, Data << 11, "ADE_BURST_EN");
break;
}
case ADE_PWR_SETTLE:
{
ADE90xx_WriteRegLog(CONFIG1, 3 << 8, Data << 8, "ADE_PWR_SETTLE");
break;
}
case ADE_CF_ACC_CLR:
{
ADE90xx_WriteRegLog(CONFIG1, 1 << 5, Data << 5, "ADE_CF_ACC_CLR");
break;
}
case ADE_CF4_CFG:
{
ADE90xx_WriteRegLog(CONFIG1, 3 << 2, Data << 2, "ADE_CF4_CFG");
break;
}
case ADE_CF3_CFG:
{
ADE90xx_WriteRegLog(CONFIG1, 1 << 1, Data << 1, "ADE_CF3_CFG");
break;
}
case ADE_SWRST:
{
ADE90xx_WriteRegLog(CONFIG1, 1, Data, "ADE_SWRST");
break;
}
// CONFIG2:
case ADE_UPERIOD_SEL:
{
ADE90xx_WriteRegLog(CONFIG2, 1 << 12, Data << 12, "ADE_UPERIOD_SEL");
break;
}
case ADE_HPF_CRN:
{
ADE90xx_WriteRegLog(CONFIG2, 7 << 9, Data << 9, "ADE_UPERIOD_SEL");
break;
}
// CONFIG3:
case ADE_PEAKSEL: