forked from pedvide/ADC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalogBufferDMA.cpp
284 lines (253 loc) · 10.6 KB
/
AnalogBufferDMA.cpp
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
/* Teensy 3.x, LC, 4.0 ADC library
https://github.com/pedvide/ADC
Copyright (c) 2020 Pedro Villanueva
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.
*/
#include "AnalogBufferDMA.h"
#ifdef ADC_USE_DMA
//#define DEBUG_DUMP_DATA
// Global objects
AnalogBufferDMA *AnalogBufferDMA::_activeObjectPerADC[2] = {nullptr, nullptr};
#if defined(__IMXRT1062__) // Teensy 4.0
#define SOURCE_ADC_0 ADC1_R0
#define DMAMUX_ADC_0 DMAMUX_SOURCE_ADC1
#define SOURCE_ADC_1 ADC2_R0
#define DMAMUX_ADC_1 DMAMUX_SOURCE_ADC2
#elif defined(KINETISK)
#define SOURCE_ADC_0 ADC0_RA
#define DMAMUX_ADC_0 DMAMUX_SOURCE_ADC0
#ifdef ADC_DUAL_ADCS
#define SOURCE_ADC_1 ADC1_RA
#define DMAMUX_ADC_1 DMAMUX_SOURCE_ADC1
#endif
#elif defined(KINETISL)
#define SOURCE_ADC_0 ADC0_RA
#define DMAMUX_ADC_0 DMAMUX_SOURCE_ADC0
#endif
//=============================================================================
// Debug support
//=============================================================================
#ifdef DEBUG_DUMP_DATA
static void dumpDMA_TCD(DMABaseClass *dmabc)
{
#ifndef KINETISL
Serial.printf("%x %x:", (uint32_t)dmabc, (uint32_t)dmabc->TCD);
Serial.printf("SA:%x SO:%d AT:%x NB:%x SL:%d DA:%x DO: %d CI:%x DL:%x CS:%x BI:%x\n", (uint32_t)dmabc->TCD->SADDR,
dmabc->TCD->SOFF, dmabc->TCD->ATTR, dmabc->TCD->NBYTES, dmabc->TCD->SLAST, (uint32_t)dmabc->TCD->DADDR,
dmabc->TCD->DOFF, dmabc->TCD->CITER, dmabc->TCD->DLASTSGA, dmabc->TCD->CSR, dmabc->TCD->BITER);
#else
Serial.printf("%x %x:", (uint32_t)dmabc, (uint32_t)dmabc->CFG);
Serial.printf("SAR:%x DAR:%x DSR_BCR:%x DCR:%x\n", (uint32_t)dmabc->CFG->SAR, dmabc->CFG->DAR, dmabc->CFG->DSR_BCR,
dmabc->CFG->DCR);
#endif
}
#endif
//=============================================================================
// Init - Initialize the object including setup DMA structures
//=============================================================================
void AnalogBufferDMA::init(ADC *adc, int8_t adc_num)
{
_since_init_micros = 0; //start tracking time
// enable DMA and interrupts
#ifdef DEBUG_DUMP_DATA
Serial.println("AnalogBufferDMA::init");
Serial.flush();
#endif
#ifndef KINETISL
// setup a DMA Channel.
// Now lets see the different things that RingbufferDMA setup for us before
// See if we were created with one or two buffers. If one assume we stop on completion, else assume continuous.
if (_buffer2 && _buffer2_count)
{
#ifdef ADC_DUAL_ADCS
_dmasettings_adc[0].source((volatile uint16_t &)((adc_num == 1) ? SOURCE_ADC_1 : SOURCE_ADC_0));
#else
_dmasettings_adc[0].source((volatile uint16_t &)(SOURCE_ADC_0));
#endif
_dmasettings_adc[0].destinationBuffer((uint16_t *)_buffer1, _buffer1_count * 2); // 2*b_size is necessary for some reason
_dmasettings_adc[0].replaceSettingsOnCompletion(_dmasettings_adc[1]); // go off and use second one...
_dmasettings_adc[0].interruptAtCompletion(); //interruptAtHalf or interruptAtCompletion
#ifdef ADC_DUAL_ADCS
_dmasettings_adc[1].source((volatile uint16_t &)((adc_num == 1) ? SOURCE_ADC_1 : SOURCE_ADC_0));
_dmasettings_adc[1].destinationBuffer((uint16_t *)_buffer2, _buffer2_count * 2); // 2*b_size is necessary for some reason
_dmasettings_adc[1].replaceSettingsOnCompletion(_dmasettings_adc[0]); // Cycle back to the first one
_dmasettings_adc[1].interruptAtCompletion(); //interruptAtHalf or interruptAtCompletion
#endif
_dmachannel_adc = _dmasettings_adc[0];
_stop_on_completion = false;
}
else
{
// Only one buffer so lets just setup the dmachannel ...
// Serial.printf("AnalogBufferDMA::init Single buffer %d\n", adc_num);
#ifdef ADC_DUAL_ADCS
_dmachannel_adc.source((volatile uint16_t &)((adc_num == 1) ? SOURCE_ADC_1 : SOURCE_ADC_0));
#else
_dmachannel_adc.source((volatile uint16_t &)(SOURCE_ADC_0));
#endif
_dmachannel_adc.destinationBuffer((uint16_t *)_buffer1, _buffer1_count * 2); // 2*b_size is necessary for some reason
_dmachannel_adc.interruptAtCompletion(); //interruptAtHalf or interruptAtCompletion
_dmachannel_adc.disableOnCompletion(); // we will disable on completion.
_stop_on_completion = true;
}
if (adc_num == 1)
{
#ifdef ADC_DUAL_ADCS
_activeObjectPerADC[1] = this;
_dmachannel_adc.attachInterrupt(&adc_1_dmaISR);
_dmachannel_adc.triggerAtHardwareEvent(DMAMUX_ADC_1); // start DMA channel when ADC finishes a conversion
#endif
}
else
{
_activeObjectPerADC[0] = this;
_dmachannel_adc.attachInterrupt(&adc_0_dmaISR);
_dmachannel_adc.triggerAtHardwareEvent(DMAMUX_ADC_0); // start DMA channel when ADC finishes a conversion
}
//arm_dcache_flush((void*)dmaChannel, sizeof(dmaChannel));
_dmachannel_adc.enable();
adc->adc[adc_num]->continuousMode();
adc->adc[adc_num]->enableDMA();
#ifdef DEBUG_DUMP_DATA
dumpDMA_TCD(&_dmachannel_adc);
dumpDMA_TCD(&_dmasettings_adc[0]);
dumpDMA_TCD(&_dmasettings_adc[1]);
#if defined(__IMXRT1062__) // Teensy 4.0
if (adc_num == 1)
{
Serial.printf("ADC2: HC0:%x HS:%x CFG:%x GC:%x GS:%x\n", ADC2_HC0, ADC2_HS, ADC2_CFG, ADC2_GC, ADC2_GS);
}
else
{
Serial.printf("ADC1: HC0:%x HS:%x CFG:%x GC:%x GS:%x\n", ADC1_HC0, ADC1_HS, ADC1_CFG, ADC1_GC, ADC1_GS);
}
#endif
#endif
#else
// Kinetisl (TLC)
// setup a DMA Channel.
// Now lets see the different things that RingbufferDMA setup for us before
_dmachannel_adc.source((volatile uint16_t &)(SOURCE_ADC_0));
;
_dmachannel_adc.destinationBuffer((uint16_t *)_buffer1, _buffer1_count * 2); // 2*b_size is necessary for some reason
_dmachannel_adc.disableOnCompletion(); // ISR will hae to restart with other buffer
_dmachannel_adc.interruptAtCompletion(); //interruptAtHalf or interruptAtCompletion
_activeObjectPerADC[0] = this;
_dmachannel_adc.attachInterrupt(&adc_0_dmaISR);
_dmachannel_adc.triggerAtHardwareEvent(DMAMUX_ADC_0); // start DMA channel when ADC finishes a conversion
_dmachannel_adc.enable();
adc->startContinuous(adc_num);
adc->enableDMA(adc_num);
#ifdef DEBUG_DUMP_DATA
dumpDMA_TCD(&_dmachannel_adc);
#endif
#endif
_last_isr_time = millis();
_last_isr_time_micros = _since_init_micros;
}
//=============================================================================
// stopOnCompletion: allows you to turn on or off stopping when a DMA buffer
// has completed filling. Default is on when only one buffer passed in to the
// constructor and off if two buffers passed in.
//=============================================================================
void AnalogBufferDMA::stopOnCompletion(bool stop_on_complete)
{
#ifndef KINETISL
if (stop_on_complete)
_dmachannel_adc.TCD->CSR |= DMA_TCD_CSR_DREQ;
else
_dmachannel_adc.TCD->CSR &= ~DMA_TCD_CSR_DREQ;
#else
if (stop_on_complete)
_dmachannel_adc.CFG->DCR |= DMA_DCR_D_REQ;
else
_dmachannel_adc.CFG->DCR &= ~DMA_DCR_D_REQ;
#endif
_stop_on_completion = stop_on_complete;
}
//=============================================================================
// ClearCompletion: if we have stop on completion, then clear the completion state
// i.e. reenable the dma operation. Note only valid if we are
// in the stopOnCompletion state.
//=============================================================================
bool AnalogBufferDMA::clearCompletion()
{
if (!_stop_on_completion)
return false;
// should probably check to see if we are dsiable or not...
_dmachannel_adc.enable();
return true;
}
//=============================================================================
// processADC_DMAISR: Process the DMA completion ISR
// common for both ISRs on those processors who have more than one ADC
//=============================================================================
void AnalogBufferDMA::processADC_DMAISR()
{
_last_isr_time_micros = _since_init_micros;
//digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
uint32_t cur_time = millis();
_interrupt_count++;
_interrupt_delta_time = cur_time - _last_isr_time;
_last_isr_time = cur_time;
// update the internal buffer positions
_dmachannel_adc.clearInterrupt();
#ifdef KINETISL
// Lets try to clear the previous interrupt, change buffers
// and restart
if (_buffer2 && (_interrupt_count & 1))
{
_dmachannel_adc.destinationBuffer((uint16_t *)_buffer2, _buffer2_count * 2); // 2*b_size is necessary for some reason
}
else
{
_dmachannel_adc.destinationBuffer((uint16_t *)_buffer1, _buffer1_count * 2); // 2*b_size is necessary for some reason
}
// If we are not stopping on completion, then reenable...
if (!_stop_on_completion)
_dmachannel_adc.enable();
#endif
}
//=============================================================================
// adc_0_dmaISR: called for first ADC when DMA has completed filling a buffer.
//=============================================================================
void AnalogBufferDMA::adc_0_dmaISR()
{
if (_activeObjectPerADC[0])
{
_activeObjectPerADC[0]->processADC_DMAISR();
}
#if defined(__IMXRT1062__) // Teensy 4.0
asm("DSB");
#endif
}
//=============================================================================
// adc_1_dmaISR - Used for processors that have a second ADC object
//=============================================================================
void AnalogBufferDMA::adc_1_dmaISR()
{
if (_activeObjectPerADC[1])
{
_activeObjectPerADC[1]->processADC_DMAISR();
}
#if defined(__IMXRT1062__) // Teensy 4.0
asm("DSB");
#endif
}
#endif // ADC_USE_DMA