-
Notifications
You must be signed in to change notification settings - Fork 27
/
OpenEIT.c
1467 lines (1245 loc) · 50.7 KB
/
OpenEIT.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
/**********************************
Author: Jean Rintoul , Mindseye Biomedical LLC Copyright 2018
This software is modified from an original Analog Devices example,
It's original copyright notice is included below.
All code additions to this project are under a
Creative Commons Non-Commercial License((CC BY-NC-SA 4.0)).
If you are a person who wants to experiment/hack or publish etc, please go ahead and do it!
We'd love an acknowledgement.
If you are planning a commercial enterprise and are making a derivative work,
please get in contact with Mindseye Biomedical LLC ([email protected]).
We'd love a payment for use of the OpenEIT suite of work which would in turn help
us afford the time to maintain this project and make new projects, while giving
your company commerical use license.
Please support Open Source Hardware! Imagine a world where we shared knowledge,
to enable each other to reach greater heights.
All contributions are much appreciated! Donations, Code Contributions are all welcome.
Copyright (c) 2014 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.
*********************************************************************************/
#ifdef _MISRA_RULES
#pragma diag(push)
#pragma diag(suppress:misra_rules_all:"suppress all MISRA rules for test")
#endif /* _MISRA_RULES */
#include <stdio.h>
#include <stddef.h> // for 'NULL'
#include <stdio.h> // for scanf
#include <string.h> // for strncmp
#include <stdint.h>
#include "arm_math.h"
#include "test_common.h"
#include "afe.h"
#include "afe_lib.h"
#include "uart.h"
#include "sequences.h"
#include "lookup.h"
#include "gpio.h"
#include "modes.h"
#include <ADuCM350_device.h>
#if defined ( __ICCARM__ ) // IAR compiler...
/* Apply ADI MISRA Suppressions */
#define ASSERT_ADI_MISRA_SUPPRESSIONS
#include "misra.h"
#endif
/* Macro to enable the returning of AFE data using the UART */
/* 1 = return AFE data on UART */
/* 0 = return AFE data on SW (Std Output) */
#define USE_UART_FOR_DATA (1)
/* Helper macro for printing strings to UART or Std. Output */
#define PRINT(s) test_print(s)
/* Size of Tx and Rx buffers */
#define RX_BUFFER_SIZE 2
#define TX_BUFFER_SIZE 100
/* Rx and Tx buffers */
static uint8_t RxBuffer[RX_BUFFER_SIZE];
static uint8_t TxBuffer[TX_BUFFER_SIZE];
/* UART Handle */
ADI_UART_HANDLE hUartDevice;
ADI_AFE_DEV_HANDLE hDevice;
/* Custom fixed-point type used for final results, */
/* to keep track of the decimal point position. */
/* Signed number with 28 integer bits and 4 fractional bits. */
typedef union {
int32_t full;
struct {
uint8_t fpart:4;
int32_t ipart:28;
} parts;
} fixed32_t;
/* Function prototypes */
q15_t arctan (q15_t imag, q15_t real);
fixed32_t calculate_magnitude(q31_t magnitude_1, q31_t magnitude_2, uint32_t res);
fixed32_t calculate_phase (q15_t phase_rcal, q15_t phase_z);
void convert_dft_results (int16_t *dft_results, q15_t *dft_results_q15, q31_t *dft_results_q31);
void sprintf_fixed32 (char *out, fixed32_t in);
void print_MagnitudePhase (char *text, fixed32_t magnitude, fixed32_t phase);
void test_print (char *pBuffer);
ADI_UART_RESULT_TYPE uart_Init (void);
ADI_UART_RESULT_TYPE uart_Init_Simple (void);
ADI_UART_RESULT_TYPE uart_UnInit (void);
void delay (uint32_t counts);
extern int32_t adi_initpinmux (void);
void multiplex_adg732 (ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq,uint32_t n_el);
void time_series (ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq);
void bioimpedance_spectroscopy (ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq);
void init_GPIO_ports (void);
fixed32_t calculate_bipolar_magnitude (q31_t magnitude_rcal, q31_t magnitude_z);
void init_mode_tetramux (void);
void init_mode_bis (void);
void init_mode_bipolar (void);
void time_series_bipolar(ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq);
fixed32_t calculate_bipolar_magnitude (q31_t magnitude_rcal, q31_t magnitude_z);
void bipolar_adg732(ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq,uint32_t n_el);
int main(void)
{
ADI_UART_RESULT_TYPE uartResult;
int16_t rxSize;
int16_t txSize;
int16_t mode = 0;
/* Flag which indicates whether to stop the program */
_Bool bStopFlag = false;
/* Clock initialization */
SystemInit();
/* NVIC initialization */
NVIC_SetPriorityGrouping(12);
/* Change the system clock source to HFXTAL and change clock frequency to 16MHz */
/* Requirement for AFE (ACLK) */
SystemTransitionClocks(ADI_SYS_CLOCK_TRIGGER_MEASUREMENT_ON);
/* SPLL with 32MHz used, need to divide by 2 */
SetSystemClockDivider(ADI_SYS_CLOCK_UART, 2);
/* Test initialization */
test_Init();
/* Use static pinmuxing */
adi_initpinmux();
/* Initialize the UART */
// if (ADI_UART_SUCCESS != uart_Init_Simple())
// {
// FAIL("uart_Init");
// }
//
if (ADI_UART_SUCCESS != uart_Init())
{
FAIL("uart_Init");
}
//PRINT("OpenEIT\n");
char msg1[300] = {0};
strcat(msg1, "OpenEIT\n");
PRINT(msg1);
uint16_t numbytes = 2;
bStopFlag = true;
rxSize = 2;
mode = 4;
init_mode_tetramux();
/* main processing loop */
while (bStopFlag == true) // running
{
// only execute a blocking read buffer if something has been sent.
if (adi_UART_GetNumRxBytes(hUartDevice) > numbytes)
{
/* Read a character */
uartResult = adi_UART_BufRx(hUartDevice, RxBuffer, &rxSize);
}
/* Select 1,2,3 to enter into a different mode. */
if(RxBuffer[0] == 'a' && RxBuffer[1] == '\n' && mode != 1) // Time-Series
{
mode = 1;
// Reset everything.
//re-initialize uart...
uart_UnInit();
adi_GPIO_ResetToPowerUp();
adi_initpinmux();
uart_Init();
adi_GPIO_UnInit();
adi_AFE_UnInit(hDevice);
PRINT("mode 1: time series\n");
init_mode_tetramux();
adi_UART_BufFlush(hUartDevice);
}
else if (RxBuffer[0] == 'b' && RxBuffer[1] == '\n' && mode != 2) // Bioimpedance Spectroscopy
{
// Reset everything.
adi_GPIO_UnInit();
adi_AFE_UnInit(hDevice);
PRINT("mode 2: bioimpedance spectroscopy\n");
mode = 2;
init_mode_bis();
PRINT("end initialize\n");
adi_UART_BufFlush(hUartDevice);
}
else if (RxBuffer[0] == 'c' && RxBuffer[1] == '\n' && mode !=3) // 8 electrode Tetrapolar Imaging
{
mode = 3;
// Reset everything.
adi_GPIO_UnInit();
adi_AFE_UnInit(hDevice);
PRINT("mode 3: 8 electrode imaging\n");
init_mode_tetramux();
adi_UART_BufFlush(hUartDevice);
}
else if (RxBuffer[0] == 'd' && RxBuffer[1] == '\n' && mode !=4) // 16 electrode Tetrapolar Imaging
{
mode = 4;
// Reset everything.
adi_GPIO_UnInit();
adi_AFE_UnInit(hDevice);
PRINT("mode 4: 16 electrode imaging\n");
init_mode_tetramux();
adi_UART_BufFlush(hUartDevice);
}
else if (RxBuffer[0] == 'e' && RxBuffer[1] == '\n' && mode !=5) // 32 electrode Tetrapolar Imaging
{
mode = 5;
// Reset everything.
adi_GPIO_UnInit();
adi_AFE_UnInit(hDevice);
PRINT("mode 3: 32 electrode imaging\n");
init_mode_tetramux();
adi_UART_BufFlush(hUartDevice);
}
else if (RxBuffer[0] == 'f' && RxBuffer[1] == '\n' ) // Bipolar Imaging
{
mode = 6;
init_mode_bipolar();
adi_UART_BufFlush(hUartDevice);
}
else if (RxBuffer[0] == 'g' && RxBuffer[1] == '\n' ) // Bipolar Time series Imaging
{
mode = 7;
init_mode_bipolar();
adi_UART_BufFlush(hUartDevice);
}
else {
// clears out UART buffer in case user presses random stuff a few times.
adi_UART_BufFlush(hUartDevice);
}
if (mode == 1) { // time series
time_series(hDevice, seq_afe_fast_meas_4wire);
}
else if (mode == 2) { // bioimpedance spectroscopy
bioimpedance_spectroscopy(hDevice, seq_afe_fast_acmeasBioZ_4wire);
}
else if (mode == 3) { // 8 electrode imaging
uint32_t n_el = 8;
/* Perform the multiplex adg732 Tetrapolar Impedance measurements */
multiplex_adg732(hDevice, seq_afe_fast_meas_4wire, n_el);
}
else if (mode == 4) { // 16 electrode imaging
uint32_t n_el = 16;
/* Perform the multiplex adg732 Tetrapolar Impedance measurements */
multiplex_adg732(hDevice, seq_afe_fast_meas_4wire, n_el);
}
else if (mode == 5) { // 32 electrode imaging
uint32_t n_el = 32;
/* Perform the multiplex adg732 Tetrapolar Impedance measurements */
multiplex_adg732(hDevice, seq_afe_fast_meas_4wire, n_el);
}
else if (mode == 6) {
uint32_t n_el = 16;
bipolar_adg732(hDevice, seq_fast_2wire_bipolar, n_el);
}
else if (mode == 7) {
time_series_bipolar(hDevice, seq_fast_2wire_bipolar);
}
else {
PRINT("no mode chosen\n");
}
} // END OF WHILE LOOP
/* AFE Power Down */
if (ADI_AFE_SUCCESS != adi_AFE_PowerDown(hDevice))
{
FAIL("PowerDown");
}
/* Uninitialize the AFE API */
if (ADI_AFE_SUCCESS != adi_AFE_UnInit(hDevice))
{
FAIL("Uninit");
}
/* Close the UART */
uartResult = adi_UART_UnInit(hUartDevice);
if (ADI_UART_SUCCESS != uartResult)
{
FAIL("adi_UART_UnInit");
}
}
/* Arctan Implementation */
/* ===================== */
/* Arctan is calculated using the formula: */
/* */
/* y = arctan(x) = 0.318253 * x + 0.003314 * x^2 - 0.130908 * x^3 + 0.068542 * x^4 - 0.009159 * x^5 */
/* */
/* The angle in radians is given by (y * pi) */
/* */
/* For the fixed-point implementation below, the coefficients are quantized to 16-bit and */
/* represented as 1.15 */
/* The input vector is rotated until positioned between 0 and pi/4. After the arctan */
/* is calculated for the rotated vector, the initial angle is restored. */
/* The format of the output is 1.15 and scaled by PI. To find the angle value in radians from the output */
/* of this function, a multiplication by PI is needed. */
const q15_t coeff[5] = {
(q15_t)0x28BD, /* 0.318253 */
(q15_t)0x006D, /* 0.003314 */
(q15_t)0xEF3E, /* -0.130908 */
(q15_t)0x08C6, /* 0.068542 */
(q15_t)0xFED4, /* -0.009159 */
};
q15_t arctan(q15_t imag, q15_t real) {
q15_t t;
q15_t out;
uint8_t rotation; /* Clockwise, multiples of PI/4 */
int8_t i;
if ((q15_t)0 == imag) {
/* Check the sign*/
if (real & (q15_t)0x8000) {
/* Negative, return -PI */
return (q15_t)0x8000;
}
else {
return (q15_t)0;
}
}
else {
rotation = 0;
/* Rotate the vector until it's placed in the first octant (0..PI/4) */
if (imag < 0) {
imag = -imag;
real = -real;
rotation += 4;
}
if (real <= 0) {
/* Using 't' as temporary storage before its normal usage */
t = real;
real = imag;
imag = -t;
rotation += 2;
}
if (real <= imag) {
/* The addition below may overflow, drop 1 LSB precision if needed. */
/* The subtraction cannot underflow. */
t = real + imag;
if (t < 0) {
/* Overflow */
t = imag - real;
real = (q15_t)(((q31_t)real + (q31_t)imag) >> 1);
imag = t >> 1;
}
else {
t = imag - real;
real = (real + imag);
imag = t;
}
rotation += 1;
}
/* Calculate tangent value */
t = (q15_t)((q31_t)(imag << 15) / real);
out = (q15_t)0;
for (i = 4; i >=0; i--) {
out += coeff[i];
arm_mult_q15(&out, &t, &out, 1);
}
/* Rotate back to original position, in multiples of pi/4 */
/* We're using 1.15 representation, scaled by pi, so pi/4 = 0x2000 */
out += (rotation << 13);
return out;
}
}
/* This function performs dual functionality: */
/* - open circuit check: the real and imaginary parts can be non-zero but very small */
/* due to noise. If they are within the defined thresholds, overwrite them with 0s, */
/* this will indicate an open. */
/* - convert the int16_t to q15_t and q31_t formats, needed for the magnitude and phase */
/* calculations. */
void convert_dft_results(int16_t *dft_results, q15_t *dft_results_q15, q31_t *dft_results_q31) {
int8_t i;
for (i = 0; i < (DFT_RESULTS_COUNT / 2); i++) {
if ((dft_results[i] < DFT_RESULTS_OPEN_MAX_THR) &&
(dft_results[i] > DFT_RESULTS_OPEN_MIN_THR) && /* real part */
(dft_results[2 * i + 1] < DFT_RESULTS_OPEN_MAX_THR) &&
(dft_results[2 * i + 1] > DFT_RESULTS_OPEN_MIN_THR)) { /* imaginary part */
/* Open circuit, force both real and imaginary parts to 0 */
dft_results[i] = 0;
dft_results[2 * i + 1] = 0;
}
}
/* Convert to 1.15 format */
for (i = 0; i < DFT_RESULTS_COUNT; i++) {
dft_results_q15[i] = (q15_t)dft_results[i];
}
/* Convert to 1.31 format */
arm_q15_to_q31(dft_results_q15, dft_results_q31, DFT_RESULTS_COUNT);
}
/* Calculates magnitude. */
/* performs the calculation: */
/* magnitude = magnitude_1 / magnitude_2 * res */
/* Output in custom fixed-point format (28.4) */
fixed32_t calculate_magnitude(q31_t magnitude_1, q31_t magnitude_2, uint32_t res) {
q63_t magnitude;
fixed32_t out;
magnitude = (q63_t)0;
if ((q63_t)0 != magnitude_2) {
magnitude = (q63_t)magnitude_1 * (q63_t)res;
/* Shift up for additional precision and rounding */
magnitude = (magnitude << 5) / (q63_t)magnitude_2;
/* Rounding */
magnitude = (magnitude + 1) >> 1;
}
/* Saturate if needed */
if (magnitude & 0xFFFFFFFF00000000) {
/* Cannot be negative */
out.full = 0x7FFFFFFF;
}
else {
out.full = magnitude & 0xFFFFFFFF;
}
return out;
}
/* Calculates calibrated magnitude. */
/* The input values are the measured RCAL magnitude (magnitude_rcal) */
/* and the measured magnitude of the unknown impedance (magnitude_z). */
/* Performs the calculation: */
/* magnitude = magnitude_rcal / magnitude_z * RCAL */
/* Output in custom fixed-point format (28.4). */
fixed32_t calculate_bipolar_magnitude(q31_t magnitude_rcal, q31_t magnitude_z) {
q63_t magnitude;
fixed32_t out;
magnitude = (q63_t)0;
if ((q63_t)0 != magnitude_z) {
magnitude = (q63_t)magnitude_rcal * (q63_t)RCAL;
/* Shift up for additional precision and rounding */
magnitude = (magnitude << 5) / (q63_t)magnitude_z;
/* Rounding */
magnitude = (magnitude + 1) >> 1;
}
/* Saturate if needed */
if (magnitude & 0xFFFFFFFF00000000) {
/* Cannot be negative */
out.full = 0x7FFFFFFF;
}
else {
out.full = magnitude & 0xFFFFFFFF;
}
return out;
}
/* Calculates phase. */
/* performs the calculation: */
/* phase = (phase_2 - phase_1) * PI / (2 * PI) * 180 */
/* = (phase_2 - phase_1) * 180 */
/* Output in custom fixed-point format (28.4). */
fixed32_t calculate_phase(q15_t phase_1, q15_t phase_2) {
q63_t phase;
fixed32_t out;
/* Multiply by 180 to convert to degrees */
phase = ((q63_t)(phase_2 - phase_1) * (q63_t)180);
/* Round and convert to fixed32_t */
out.full = ((phase + (q63_t)0x400) >> 11) & 0xFFFFFFFF;
return out;
}
/* Simple conversion of a fixed32_t variable to string format. */
void sprintf_fixed32(char *out, fixed32_t in) {
fixed32_t tmp;
if (in.full < 0) {
tmp.parts.fpart = (16 - in.parts.fpart) & 0x0F;
tmp.parts.ipart = in.parts.ipart;
if (0 != in.parts.fpart) {
tmp.parts.ipart++;
}
if (0 == tmp.parts.ipart) {
sprintf(out, " -0.%04d", tmp.parts.fpart * FIXED32_LSB_SIZE);
}
else {
sprintf(out, "%8d.%04d", tmp.parts.ipart, tmp.parts.fpart * FIXED32_LSB_SIZE);
}
}
else {
sprintf(out, "%8d.%04d", in.parts.ipart, in.parts.fpart * FIXED32_LSB_SIZE);
}
}
/* Helper function for printing fixed32_t (magnitude & phase) results */
void print_MagnitudePhase(char *text, fixed32_t magnitude, fixed32_t phase) {
char msg[MSG_MAXLEN_M3];
char tmp[MSG_MAXLEN_M3];
sprintf(msg, " %s = (", text);
/* Magnitude */
sprintf_fixed32(tmp, magnitude);
strcat(msg, tmp);
strcat(msg, ", ");
/* Phase */
sprintf_fixed32(tmp, phase);
strcat(msg, tmp);
strcat(msg, ")\r\n");
PRINT(msg);
}
/* Helper function for printing a string to UART or Std. Output */
void test_print (char *pBuffer) {
#if (1 == USE_UART_FOR_DATA)
int16_t size;
/* Print to UART */
size = strlen(pBuffer);
adi_UART_BufTx(hUartDevice, pBuffer, &size);
#elif (0 == USE_UART_FOR_DATA)
/* Print to console */
printf(pBuffer);
#endif /* USE_UART_FOR_DATA */
}
/* Initialize the UART, set the baud rate and enable */
ADI_UART_RESULT_TYPE uart_Init_Simple (void) {
ADI_UART_RESULT_TYPE result = ADI_UART_SUCCESS;
/* Open UART in blocking, non-intrrpt mode by supplying no internal buffs */
if (ADI_UART_SUCCESS != (result = adi_UART_Init(ADI_UART_DEVID_0, &hUartDevice, NULL)))
{
return result;
}
/* Set UART baud rate to 115200 */
if (ADI_UART_SUCCESS != (result = adi_UART_SetBaudRate(hUartDevice, ADI_UART_BAUD_115200)))
{
return result;
}
/* Enable UART */
if (ADI_UART_SUCCESS != (result = adi_UART_Enable(hUartDevice,true)))
{
return result;
}
return result;
}
/* Initialize the UART, set the baud rate and enable */
ADI_UART_RESULT_TYPE uart_Init (void) {
/* UART return code */
ADI_UART_RESULT_TYPE uartResult;
ADI_UART_INIT_DATA initData;
ADI_UART_GENERIC_SETTINGS_TYPE Settings;
/*
* Initialize UART
*/
initData.pRxBufferData = RxBuffer;
initData.RxBufferSize = RX_BUFFER_SIZE;
initData.pTxBufferData = TxBuffer;
initData.TxBufferSize = TX_BUFFER_SIZE;
/* Open UART driver */
uartResult = adi_UART_Init(ADI_UART_DEVID_0, &hUartDevice, &initData);
if (ADI_UART_SUCCESS != uartResult)
{
test_Fail("adi_UART_Init() failed");
}
Settings.BaudRate = ADI_UART_BAUD_115200; //ADI_UART_BAUD_115200;
Settings.bBlockingMode = true;
Settings.bInterruptMode = true;
Settings.Parity = ADI_UART_PARITY_NONE;
Settings.WordLength = ADI_UART_WLS_8;
Settings.bDmaMode = false;
/* config UART */
uartResult = adi_UART_SetGenericSettings(hUartDevice, &Settings);
if (ADI_UART_SUCCESS != uartResult)
{
test_Fail("adi_UART_SetGenericSettings() failed");
}
/* enable UART */
uartResult = adi_UART_Enable(hUartDevice, true);
if (ADI_UART_SUCCESS != uartResult)
{
test_Fail("adi_UART_Enable(true) failed");
}
return uartResult;
}
/* Uninitialize the UART */
ADI_UART_RESULT_TYPE uart_UnInit (void) {
ADI_UART_RESULT_TYPE result = ADI_UART_SUCCESS;
/* Uninitialize the UART API */
if (ADI_UART_SUCCESS != (result = adi_UART_UnInit(hUartDevice)))
{
return result;
}
return result;
}
void delay(uint32_t count)
{
while(count>0)
{
count--;
}
}
/******************************************************************************
Main loop for tetrapolar bioimpedance spectroscopy
*****************************************************************************/
void bioimpedance_spectroscopy(ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq) {
int16_t dft_results[DFT_RESULTS_COUNT];
q15_t dft_results_q15[DFT_RESULTS_COUNT];
q31_t dft_results_q31[DFT_RESULTS_COUNT];
q31_t magnitude[DFT_RESULTS_COUNT / 2];
int8_t j,k;
uint32_t rtiaAndGain;
for (j = 0; j < MULTIFREQUENCY_ARRAY_SIZE; j++) /* Here we start an outer frequency loop. */
{
// seq_afe_fast_acmeasBioZ_4wire
/* recalculate FCW here, based on number in array. */
uint64_t FREQ_MOD = multifrequency[j];
uint32_t FCW_MOD = ((uint32_t)(((uint64_t)FREQ_MOD << 26) / 16000000 + 0.5));
char stringfrequency[MSG_MAXLEN_M2];
sprintf(stringfrequency, "%s;", stringfreqs[j]);
char msg[MSG_MAXLEN_M2] = {0};
if (j >= MULTIFREQUENCY_ARRAY_SIZE) { // reset J
j = 0;
}
/* Update FCW in the sequence */
seq_afe_fast_acmeasBioZ_4wire[3] = SEQ_MMR_WRITE(REG_AFE_AFE_WG_FCW, FCW_MOD);
/* Update sine amplitude in the sequence */
seq_afe_fast_acmeasBioZ_4wire[4] = SEQ_MMR_WRITE(REG_AFE_AFE_WG_AMPLITUDE, SINE_AMPLITUDE);
/* Recalculate CRC in software for the AC measurement, because we changed */
/* FCW and sine amplitude settings */
adi_AFE_EnableSoftwareCRC(hDevice, true);
int max = 1; // do only 5 runs of each frequency. I am not successfully changing frequency.
for (k = 0; k < max ; k++)
{
fixed32_t magnitude_result[DFT_RESULTS_COUNT / 2 - 1]={0};
if (ADI_AFE_SUCCESS != adi_AFE_RunSequence(hDevice, seq, (uint16_t *)dft_results, DFT_RESULTS_COUNT))
{
PRINT("Impedance Measurement FAILED");
}
/* Convert DFT results to 1.15 and 1.31 formats. */
convert_dft_results(dft_results, dft_results_q15, dft_results_q31);
/* Magnitude calculation */
/* Use CMSIS function */
arm_cmplx_mag_q31(dft_results_q31, magnitude, DFT_RESULTS_COUNT / 2);
/* Calculate final magnitude value, calibrated with RTIA the gain of the instrumenation amplifier */
rtiaAndGain = (uint32_t)((RTIA * 1.5) / INST_AMP_GAIN);
magnitude_result[0] = calculate_magnitude(magnitude[1], magnitude[0], rtiaAndGain);
char tmp[300] = {0};
sprintf(msg, "%s:", "magnitudes");
strcat(msg,stringfrequency);
sprintf_fixed32(tmp, magnitude_result[0]);
strcat(msg,tmp);
}
strcat(msg," \r\n");
PRINT(msg);
}
}
/******************************************************************************
Main loop for bipolar time series measurements.
*****************************************************************************/
void time_series_bipolar(ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq) {
int16_t dft_results[DFT_RESULTS_COUNT];
q15_t dft_results_q15[DFT_RESULTS_COUNT];
q31_t dft_results_q31[DFT_RESULTS_COUNT];
q31_t magnitude[DFT_RESULTS_COUNT / 2];
fixed32_t magnitude_result[DFT_RESULTS_COUNT / 2 - 1]={0};
char msg[MSG_MAXLEN_M1] = {0};
char tmp[300] = {0};
int8_t i;
/* Perform the Impedance measurement */
if (adi_AFE_RunSequence(hDevice, seq_fast_2wire_bipolar, (uint16_t *)dft_results, DFT_RESULTS_COUNT))
{
PRINT("Impedance Measurement");
}
/* Convert DFT results to 1.15 and 1.31 formats. */
convert_dft_results(dft_results, dft_results_q15, dft_results_q31);
/* Magnitude calculation */
/* Use CMSIS function */
arm_cmplx_mag_q31(dft_results_q31, magnitude, DFT_RESULTS_COUNT / 2);
/* Calculate final magnitude values, calibrated with RCAL. */
for (i = 0; i < DFT_RESULTS_COUNT / 2 - 1; i++)
{
magnitude_result[i] = calculate_bipolar_magnitude(magnitude[0], magnitude[i + 1]);
}
sprintf_fixed32(tmp, magnitude_result[0]);
strcat(msg,tmp);
strcat(msg," \r\n");
PRINT(msg);
}
/******************************************************************************
Main loop for tetrapolar time series measurements.
*****************************************************************************/
void time_series(ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq) {
int16_t dft_results[DFT_RESULTS_COUNT];
q15_t dft_results_q15[DFT_RESULTS_COUNT];
q31_t dft_results_q31[DFT_RESULTS_COUNT];
q31_t magnitude[DFT_RESULTS_COUNT / 2];
fixed32_t magnitude_result[DFT_RESULTS_COUNT / 2 - 1]={0};
uint32_t rtiaAndGain;
/* Calculate final magnitude value, calibrated with RTIA the gain of the instrumenation amplifier */
rtiaAndGain = (uint32_t)((RTIA * 1.5) / INST_AMP_GAIN);
char msg[MSG_MAXLEN_M1] = {0};
char tmp[300] = {0};
if (ADI_AFE_SUCCESS != adi_AFE_RunSequence(hDevice, seq, (uint16_t *)dft_results, DFT_RESULTS_COUNT))
{
PRINT("Impedance Measurement FAILED");
}
/* Convert DFT results to 1.15 and 1.31 formats. */
convert_dft_results(dft_results, dft_results_q15, dft_results_q31);
/* Magnitude calculation */
/* Use CMSIS function */
arm_cmplx_mag_q31(dft_results_q31, magnitude, DFT_RESULTS_COUNT / 2);
/* Calculate final magnitude value, calibrated with RTIA the gain of the instrumenation amplifier */
rtiaAndGain = (uint32_t)((RTIA * 1.5) / INST_AMP_GAIN);
magnitude_result[0] = calculate_magnitude(magnitude[1], magnitude[0], rtiaAndGain);
sprintf_fixed32(tmp, magnitude_result[0]);
strcat(msg,tmp);
strcat(msg," \r\n");
PRINT(msg);
}
/******************************************************************************
Main code for the imaging function with 32 electrodes.
*****************************************************************************/
void multiplex_adg732(ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq,uint32_t n_el) {
// int 32 of the sequence, no of measures based on the sequence entered.
// i.e. n_el if 8, 16, 32, we can pick which sequence.
// 32, 192, 896
uint32_t numberofmeasures;
if (n_el == 8) {
numberofmeasures = 32;
}
else if (n_el == 16) {
numberofmeasures = 192;
}
else if (n_el == 32) {
numberofmeasures = 896;
}
else {
numberofmeasures = 928;
PRINT("number of measures is 0\n");
}
uint32_t rtiaAndGain;
/* Calculate final magnitude value, calibrated with RTIA the gain of the instrumenation amplifier */
rtiaAndGain = (uint32_t)((RTIA * 1.5) / INST_AMP_GAIN);
char msg[MSG_MAXLEN_M3] = {0};
//sprintf(msg, "GAIN: %u Magnitudes:", rtiaAndGain); // Now gain is 33132?
sprintf(msg,"magnitudes: ");
PRINT(msg);
//
// NUMBEROFMEASURES is determined by which electrode configuration: 8,16 or 32.
for (uint32_t econf = 0;econf<numberofmeasures;econf++) {
char tmp[300] = {0};
q31_t dft_results_q31[DFT_RESULTS_COUNT] = {0};
q15_t dft_results_q15[DFT_RESULTS_COUNT] = {0};
q31_t temp_magnitude[DFT_RESULTS_COUNT/2] = {0};
int16_t temp_dft_results[DFT_RESULTS_COUNT] = {0};
fixed32_t magnitude_result[DFT_RESULTS_COUNT/2-1] = {0};
// This is where we select the electrode sequence. i.e. 8,16 or 32 adjacent or opposition.
int16_t* e;
if (n_el == 8) {
e = (int16_t *)electrode_configuration_8_opposition[econf];
}
else if (n_el == 16) {
e = (int16_t *)electrode_configuration_16_opposition[econf];
}
else if (n_el == 32) {
e = (int16_t *)electrode_configuration_32_opposition[econf];
}
else {
e = (int16_t *)electrode_configuration_32_adjacent[econf];
}
// M1,M2,M3,M4 = 1,2,4,5
// U4, A-, m1, position 3
// U2, V-, m2, position 2
// U1, A+. m3, position 1
// U5, V+, m4, position 4
//
// e_conf file is written as A+,A-,V+,V-
// I've mapped them like this so the e_conf file matches the
// multiplexer assignement i.e. A+ -> A+ etc.
int16_t* mx1_assignment = (int16_t *)truth_table[e[1]]; // A- ->
int16_t* mx2_assignment = (int16_t *)truth_table[e[3]]; // V- ->
int16_t* mx3_assignment = (int16_t *)truth_table[e[0]]; // A+ ->
int16_t* mx4_assignment = (int16_t *)truth_table[e[2]]; // V+ ->
PinMap m1_portpin;
PinMap m2_portpin;
PinMap m3_portpin;
PinMap m4_portpin;
// A4 A3 A2 A1 A0
for (int i=0;i<5;i++) {
m1_portpin = m1_configuration[i]; // first one is a4,a3,a2,a1,a0.
m2_portpin = m2_configuration[i]; // second one is
m3_portpin = m3_configuration[i]; // third one is
m4_portpin = m4_configuration[i]; // fourth one is
// set the port pins on each multiplexer.
if (mx1_assignment[i] > 0) {
adi_GPIO_SetHigh(m1_portpin.Port, m1_portpin.Pins);
}
else {
adi_GPIO_SetLow(m1_portpin.Port, m1_portpin.Pins);
}
if (mx2_assignment[i] > 0) {
adi_GPIO_SetHigh(m2_portpin.Port,m2_portpin.Pins);
}
else {
adi_GPIO_SetLow(m2_portpin.Port,m2_portpin.Pins);
}
if (mx3_assignment[i] > 0) {
adi_GPIO_SetHigh(m3_portpin.Port,m3_portpin.Pins);
}
else {
adi_GPIO_SetLow(m3_portpin.Port,m3_portpin.Pins);
}
if (mx4_assignment[i] > 0) {
adi_GPIO_SetHigh(m4_portpin.Port,m4_portpin.Pins);
}
else {
adi_GPIO_SetLow(m4_portpin.Port,m4_portpin.Pins);
}
} // end of for loop for setting multiplexers.
// Now the multiplexers are set, take a measurement.
// Get a measurement:
// 1. calibrate (only the first time)
// 2. sequence enable
// 3. take measurement.
// 4. sequence disable
// 5. put result into new results table
// adi_AFE_EnableSoftwareCRC(hDevice, true);
/* Perform the Impedance measurement */
if (ADI_AFE_SUCCESS != adi_AFE_RunSequence(hDevice, seq, (uint16_t *)temp_dft_results, DFT_RESULTS_COUNT))
{
PRINT("FAILED Impedance Measurement");
}
convert_dft_results(temp_dft_results, dft_results_q15, dft_results_q31);
/* Magnitude calculation */
//arm_cmplx_mag_q31(dft_results_q31, temp_magnitude, 2);
/* Magnitude calculation */
/* Use CMSIS function */
arm_cmplx_mag_q31(dft_results_q31, temp_magnitude, DFT_RESULTS_COUNT / 2);
// magnitude = magnitude_1 / magnitude_2 * res ,
magnitude_result[0] = calculate_magnitude(temp_magnitude[1], temp_magnitude[0], rtiaAndGain);
/* Print DFT complex results to console 983039, 0 on my board, and when it works magnitude is 74333772, 274209844) */
//sprintf(tmp, " magnitudes = (%u, %u)\r\n", temp_magnitude[0], temp_magnitude[1]);
//strcat(msg,tmp);
sprintf_fixed32(tmp, magnitude_result[0]);
strcat(tmp,",");
//strcat(msg," ,");
PRINT(tmp);
} // END e_config for loop.
//strcat(msg," \r\n");
PRINT("\r\n");
adi_UART_BufFlush(hUartDevice);
}
/******************************************************************************
Main code for the imaging function bipolar measurements(not tetrapolar).
*****************************************************************************/
void bipolar_adg732(ADI_AFE_DEV_HANDLE hDevice, const uint32_t *const seq,uint32_t n_el) {
// int 32 of the sequence, no of measures based on the sequence entered.
// i.e. n_el if 8, 16, 32, we can pick which sequence.
// 32, 192, 896
uint32_t numberofmeasures;
if (n_el == 8) {
numberofmeasures = 32;
}
else if (n_el == 16) {
numberofmeasures = 192;
}
else if (n_el == 32) {
numberofmeasures = 896;
}
else {
numberofmeasures = 0;
PRINT("number of measures is 0\n");
}
char msg[MSG_MAXLEN_M3] = {0};
sprintf(msg,"magnitudes: ");
PRINT(msg);
// NUMBEROFMEASURES is determined by which electrode configuration: 8,16 or 32.
for (uint32_t econf = 0;econf<numberofmeasures;econf++) {
char tmp[300] = {0};
q31_t dft_results_q31[DFT_RESULTS_COUNT] = {0};
q15_t dft_results_q15[DFT_RESULTS_COUNT] = {0};
q31_t temp_magnitude[DFT_RESULTS_COUNT/2] = {0};
int16_t temp_dft_results[DFT_RESULTS_COUNT] = {0};
fixed32_t magnitude_result[DFT_RESULTS_COUNT/2-1] = {0};
int8_t i = 0;
// This is where we select the electrode sequence. i.e. 8,16 or 32 adjacent or opposition.