-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc_dma.ino
208 lines (168 loc) · 6.28 KB
/
adc_dma.ino
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
/* Example for using DMA with ADC
This example uses DMA object to do the sampling. It does not use a timer so
it runs at whatever speed the ADC will run at with current settings.
It should work for Teensy LC, 3.x and T4
DMA: using AnalogBufferDMA with two buffers, this runs in continuous mode and
when one buffer fills an interrupt is signaled, which sets flag saying it has
data, which this test application scans the data, and computes things like a
minimum, maximum, average values and an RMS value. For the RMS it keeps the
average from the previous set of data.
*/
#include <ADC.h>
#include <AnalogBufferDMA.h>
#ifdef ADC_USE_DMA
// Define the button pin
const int buttonPin = 2; // Change this to the pin number where your button is connected
int previous_time = 0;
const int sample_time = 10;
// This version uses both ADC1 and ADC2
#if defined(KINETISL)
const int readPin_adc_0 = A0;
#elif defined(KINETISK)
const int readPin_adc_0 = A0;
const int readPin_adc_1 = A2;
#else
const int readPin_adc_0 = A0;
const int readPin_adc_1 = 26;
#endif
ADC *adc = new ADC(); // adc object
const uint32_t initial_average_value = 2048;
// Going to try two buffers here using 2 dmaSettings and a DMAChannel
#ifdef KINETISL
const uint32_t buffer_size = 5000;
#else
const uint32_t buffer_size = 16000;
#endif
DMAMEM static volatile uint16_t __attribute__((aligned(32)))
dma_adc_buff1[buffer_size];
DMAMEM static volatile uint16_t __attribute__((aligned(32)))
dma_adc_buff2[buffer_size];
AnalogBufferDMA abdma1(dma_adc_buff1, buffer_size, dma_adc_buff2, buffer_size);
#ifdef ADC_DUAL_ADCS
DMAMEM static volatile uint16_t __attribute__((aligned(32)))
dma_adc2_buff1[buffer_size];
DMAMEM static volatile uint16_t __attribute__((aligned(32)))
dma_adc2_buff2[buffer_size];
AnalogBufferDMA abdma2(dma_adc2_buff1, buffer_size, dma_adc2_buff2,
buffer_size);
#endif
void setup() {
while (!Serial && millis() < 5000)
;
pinMode(LED_BUILTIN, OUTPUT);
pinMode(readPin_adc_0, INPUT_DISABLE); // pin 23 single ended
#ifdef ADC_DUAL_ADCS
pinMode(readPin_adc_1, INPUT_DISABLE);
#endif
Serial.begin(115200);
Serial.println("Setup ADC_0");
pinMode(buttonPin, INPUT_PULLUP);
// reference can be ADC_REFERENCE::REF_3V3, ADC_REFERENCE::REF_1V2 (not for
// Teensy LC) or ADC_REF_EXT.
// adc->setReference(ADC_REFERENCE::REF_1V2, ADC_0); // change all 3.3 to 1.2
// if you change the reference to 1V2
adc->adc0->setAveraging(1); // set number of averages
adc->adc0->setResolution(10); // set bits of resolution
adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // Set high-speed sampling
adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // Set high-speed conversion
// always call the compare functions after changing the resolution!
// adc->enableCompare(1.0/3.3*adc->getMaxValue(ADC_0), 0, ADC_0); //
// measurement will be ready if value < 1.0V
// adc->enableCompareRange(1.0*adc->getMaxValue(ADC_1)/3.3, 2.0*adc->getMaxValue(ADC_1)/3.3,
// 0, 1, ADC_1); // ready if value lies out of [1.0,2.0] V
// enable DMA and interrupts
Serial.println("before enableDMA");
Serial.flush();
// setup a DMA Channel.
// Now lets see the different things that RingbufferDMA setup for us before
abdma1.init(adc, ADC_0);
abdma1.userData(initial_average_value); // save away initial starting average
#ifdef ADC_DUAL_ADCS
Serial.println("Setup ADC_1");
adc->adc1->setAveraging(1); // set number of averages
adc->adc1->setResolution(12); // set bits of resolution
abdma2.init(adc, ADC_1);
abdma2.userData(initial_average_value); // save away initial starting average
adc->adc1->startContinuous(readPin_adc_1);
#endif
// Start the dma operation..
adc->adc0->startContinuous(readPin_adc_0);
Serial.println("End Setup");
}
char c = 0;
void loop() {
// Maybe only when both have triggered?
#ifdef ADC_DUAL_ADCS
if (abdma1.interrupted() && abdma2.interrupted()) {
if((micros()-previous_time) >= sample_time){
Serial.print("\nsampling");
if (abdma1.interrupted())
ProcessAnalogData(&abdma1, 0);
if (abdma2.interrupted())
ProcessAnalogData(&abdma2, 1);
// Serial.println();
// Serial.println(dma_adc_buff2[0]);
previous_time = micros();
}
}
#else
if (abdma1.interrupted()) {
ProcessAnalogData(&abdma1, 0);
for (uint32_t i = 0; i < buffer_size; i++) {
Serial.println(dma_adc_buff1[i]);
}
//Serial.println();
}
#endif
//Serial.println(dma_adc_buff2[0]);
// Serial.println("Buffer contents:");
// for (uint32_t i = 0; i < buffer_size; i++) {
// Serial.println(dma_adc_buff1[i]);
// }
Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
// Print buffer contents
Serial.println("Buffer contents:");
for (uint32_t i = 0; i < buffer_size; i++) {
Serial.println(dma_adc_buff2[i]);
}
Wait for button release to avoid continuous printing while the button is held down
while (digitalRead(buttonPin) == LOW) {
delay(10);
}
delay(200); // Debouncing delay
}
}
void ProcessAnalogData(AnalogBufferDMA *pabdma, int8_t adc_num) {
uint32_t sum_values = 0;
uint16_t min_val = 0xffff;
uint16_t max_val = 0;
uint32_t average_value = pabdma->userData();
volatile uint16_t *pbuffer = pabdma->bufferLastISRFilled();
volatile uint16_t *end_pbuffer = pbuffer + pabdma->bufferCountLastISRFilled();
float sum_delta_sq = 0.0;
if ((uint32_t)pbuffer >= 0x20200000u)
arm_dcache_delete((void *)pbuffer, sizeof(dma_adc_buff1));
while (pbuffer < end_pbuffer) {
if (*pbuffer < min_val)
min_val = *pbuffer;
if (*pbuffer > max_val)
max_val = *pbuffer;
sum_values += *pbuffer;
int delta_from_center = (int)*pbuffer - average_value;
sum_delta_sq += delta_from_center * delta_from_center;
pbuffer++;
}
//int rms = sqrt(sum_delta_sq / buffer_size);
average_value = sum_values / buffer_size;
// Serial.printf(" %d - %u(%u): %u <= %u <= %u %d ", adc_num,
// pabdma->interruptCount(), pabdma->interruptDeltaTime(), min_val,
// average_value, max_val, rms);
//Serial.println(average_value);
pabdma->clearInterrupt();
pabdma->userData(average_value);
}
#else // make sure the example can run for any boards (automated testing)
void setup() { }
void loop() {}
#endif // ADC_USE_DMA