-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparkfun_edge_audio_provider.cpp
377 lines (316 loc) · 13.5 KB
/
sparkfun_edge_audio_provider.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
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
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#if defined(ARDUINO) && !defined(ARDUINO_SFE_EDGE)
#define ARDUINO_EXCLUDE_CODE
#endif // defined(ARDUINO) && !defined(ARDUINO_SFE_EDGE)
#ifndef ARDUINO_EXCLUDE_CODE
#include "audio_provider.h"
#include <limits>
// These are headers from Ambiq's Apollo3 SDK.
#include "am_bsp.h"
#include "am_mcu_apollo.h"
#include "am_util.h"
#include "micro_features_micro_model_settings.h"
namespace {
// These are the raw buffers that are filled by the ADC during DMA
constexpr int kAdcNumSlots = 2;
constexpr int kAdcSamplesPerSlot = 1024;
constexpr int kAdcSampleBufferSize = (kAdcNumSlots * kAdcSamplesPerSlot);
uint32_t g_ui32ADCSampleBuffer0[kAdcSampleBufferSize];
uint32_t g_ui32ADCSampleBuffer1[kAdcSampleBufferSize];
// Controls the double buffering between the two DMA buffers.
int g_dma_destination_index = 0;
// ADC Device Handle.
static void* g_adc_handle;
// ADC DMA error flag.
volatile bool g_adc_dma_error;
// So the interrupt can use the passed-in error handler to report issues.
tflite::ErrorReporter* g_adc_dma_error_reporter = nullptr;
// Holds a longer history of audio samples in a ring buffer.
constexpr int kAudioCaptureBufferSize = 16000;
int16_t g_audio_capture_buffer[kAudioCaptureBufferSize] = {};
int g_audio_capture_buffer_start = 0;
int64_t g_total_samples_captured = 0;
int32_t g_latest_audio_timestamp = 0;
// Copy of audio samples returned to the caller.
int16_t g_audio_output_buffer[kMaxAudioSampleSize];
bool g_is_audio_initialized = false;
// Start the DMA fetch of ADC samples.
void adc_start_dma(tflite::ErrorReporter* error_reporter) {
am_hal_adc_dma_config_t ADCDMAConfig;
// Configure the ADC to use DMA for the sample transfer.
ADCDMAConfig.bDynamicPriority = true;
ADCDMAConfig.ePriority = AM_HAL_ADC_PRIOR_SERVICE_IMMED;
ADCDMAConfig.bDMAEnable = true;
ADCDMAConfig.ui32SampleCount = kAdcSampleBufferSize;
if (g_dma_destination_index == 0) {
ADCDMAConfig.ui32TargetAddress = (uint32_t)g_ui32ADCSampleBuffer0;
} else {
ADCDMAConfig.ui32TargetAddress = (uint32_t)g_ui32ADCSampleBuffer1;
}
if (AM_HAL_STATUS_SUCCESS !=
am_hal_adc_configure_dma(g_adc_handle, &ADCDMAConfig)) {
TF_LITE_REPORT_ERROR(error_reporter, "Error - configuring ADC DMA failed.");
}
// Reset the ADC DMA flags.
g_adc_dma_error = false;
g_adc_dma_error_reporter = error_reporter;
}
// Configure the ADC.
void adc_config0(tflite::ErrorReporter* error_reporter) {
am_hal_adc_config_t ADCConfig;
am_hal_adc_slot_config_t ADCSlotConfig;
// Initialize the ADC and get the handle.
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_initialize(0, &g_adc_handle)) {
TF_LITE_REPORT_ERROR(error_reporter,
"Error - reservation of the ADC0 instance failed.");
}
// Power on the ADC.
if (AM_HAL_STATUS_SUCCESS !=
am_hal_adc_power_control(g_adc_handle, AM_HAL_SYSCTRL_WAKE, false)) {
TF_LITE_REPORT_ERROR(error_reporter, "Error - ADC0 power on failed.");
}
// Set up the ADC configuration parameters. These settings are reasonable
// for accurate measurements at a low sample rate.
ADCConfig.eClock = AM_HAL_ADC_CLKSEL_HFRC_DIV2;
ADCConfig.ePolarity = AM_HAL_ADC_TRIGPOL_RISING;
ADCConfig.eTrigger = AM_HAL_ADC_TRIGSEL_SOFTWARE;
ADCConfig.eReference =
AM_HAL_ADC_REFSEL_INT_2P0; // AM_HAL_ADC_REFSEL_INT_1P5;
ADCConfig.eClockMode = AM_HAL_ADC_CLKMODE_LOW_LATENCY;
ADCConfig.ePowerMode = AM_HAL_ADC_LPMODE0;
ADCConfig.eRepeat = AM_HAL_ADC_REPEATING_SCAN;
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_configure(g_adc_handle, &ADCConfig)) {
TF_LITE_REPORT_ERROR(error_reporter, "Error - configuring ADC0 failed.");
}
// Set up an ADC slot (2)
ADCSlotConfig.eMeasToAvg = AM_HAL_ADC_SLOT_AVG_1;
ADCSlotConfig.ePrecisionMode = AM_HAL_ADC_SLOT_14BIT;
ADCSlotConfig.eChannel = AM_HAL_ADC_SLOT_CHSEL_SE2;
ADCSlotConfig.bWindowCompare = false;
ADCSlotConfig.bEnabled = true;
if (AM_HAL_STATUS_SUCCESS !=
am_hal_adc_configure_slot(g_adc_handle, 2, &ADCSlotConfig)) {
TF_LITE_REPORT_ERROR(error_reporter,
"Error - configuring ADC Slot 2 failed.");
}
// Set up an ADC slot (1)
ADCSlotConfig.eMeasToAvg = AM_HAL_ADC_SLOT_AVG_1;
ADCSlotConfig.ePrecisionMode = AM_HAL_ADC_SLOT_14BIT;
ADCSlotConfig.eChannel = AM_HAL_ADC_SLOT_CHSEL_SE1;
ADCSlotConfig.bWindowCompare = false;
ADCSlotConfig.bEnabled = true;
if (AM_HAL_STATUS_SUCCESS !=
am_hal_adc_configure_slot(g_adc_handle, 1, &ADCSlotConfig)) {
TF_LITE_REPORT_ERROR(error_reporter,
"Error - configuring ADC Slot 1 failed.");
}
// Configure the ADC to use DMA for the sample transfer.
adc_start_dma(error_reporter);
// For this example, the samples will be coming in slowly. This means we
// can afford to wake up for every conversion.
am_hal_adc_interrupt_enable(g_adc_handle,
AM_HAL_ADC_INT_DERR | AM_HAL_ADC_INT_DCMP);
// Enable the ADC.
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_enable(g_adc_handle)) {
TF_LITE_REPORT_ERROR(error_reporter, "Error - enabling ADC0 failed.");
}
}
// Initialize the ADC repetitive sample timer A3.
void init_timerA3_for_ADC() {
// Start a timer to trigger the ADC periodically (1 second).
am_hal_ctimer_config_single(3, AM_HAL_CTIMER_TIMERA,
AM_HAL_CTIMER_HFRC_12MHZ |
AM_HAL_CTIMER_FN_REPEAT |
AM_HAL_CTIMER_INT_ENABLE);
am_hal_ctimer_int_enable(AM_HAL_CTIMER_INT_TIMERA3);
// 750 = 12,000,000 (clock rate) / 16,000 (desired sample rate).
am_hal_ctimer_period_set(3, AM_HAL_CTIMER_TIMERA, 750, 374);
// Enable the timer A3 to trigger the ADC directly
am_hal_ctimer_adc_trigger_enable();
// Start the timer.
am_hal_ctimer_start(3, AM_HAL_CTIMER_TIMERA);
}
// Make sure the CPU is running as fast as possible.
void enable_burst_mode(tflite::ErrorReporter* error_reporter) {
am_hal_burst_avail_e eBurstModeAvailable;
am_hal_burst_mode_e eBurstMode;
// Check that the Burst Feature is available.
if (AM_HAL_STATUS_SUCCESS ==
am_hal_burst_mode_initialize(&eBurstModeAvailable)) {
if (AM_HAL_BURST_AVAIL == eBurstModeAvailable) {
TF_LITE_REPORT_ERROR(error_reporter, "Apollo3 Burst Mode is Available\n");
} else {
TF_LITE_REPORT_ERROR(error_reporter,
"Apollo3 Burst Mode is Not Available\n");
}
} else {
TF_LITE_REPORT_ERROR(error_reporter,
"Failed to Initialize for Burst Mode operation\n");
}
// Put the MCU into "Burst" mode.
if (AM_HAL_STATUS_SUCCESS == am_hal_burst_mode_enable(&eBurstMode)) {
if (AM_HAL_BURST_MODE == eBurstMode) {
TF_LITE_REPORT_ERROR(error_reporter,
"Apollo3 operating in Burst Mode (96MHz)\n");
}
} else {
TF_LITE_REPORT_ERROR(error_reporter,
"Failed to Enable Burst Mode operation\n");
}
}
} // namespace
// Interrupt handler for the ADC.
extern "C" void am_adc_isr(void) {
uint32_t ui32IntMask;
// Read the interrupt status.
if (AM_HAL_STATUS_SUCCESS !=
am_hal_adc_interrupt_status(g_adc_handle, &ui32IntMask, false)) {
TF_LITE_REPORT_ERROR(g_adc_dma_error_reporter,
"Error reading ADC0 interrupt status.");
}
// Clear the ADC interrupt.
if (AM_HAL_STATUS_SUCCESS !=
am_hal_adc_interrupt_clear(g_adc_handle, ui32IntMask)) {
TF_LITE_REPORT_ERROR(g_adc_dma_error_reporter,
"Error clearing ADC0 interrupt status.");
}
// If we got a DMA complete, set the flag.
if (ui32IntMask & AM_HAL_ADC_INT_DCMP) {
uint32_t* source_buffer;
if (g_dma_destination_index == 0) {
source_buffer = g_ui32ADCSampleBuffer0;
g_dma_destination_index = 1;
} else {
source_buffer = g_ui32ADCSampleBuffer1;
g_dma_destination_index = 0;
}
adc_start_dma(g_adc_dma_error_reporter);
// For slot 1:
uint32_t slotCount = 0;
for (uint32_t indi = 0; indi < kAdcSampleBufferSize; indi++) {
am_hal_adc_sample_t temp;
temp.ui32Slot = AM_HAL_ADC_FIFO_SLOT(source_buffer[indi]);
temp.ui32Sample = AM_HAL_ADC_FIFO_SAMPLE(source_buffer[indi]);
if (temp.ui32Slot == 1) {
g_audio_capture_buffer[g_audio_capture_buffer_start] = temp.ui32Sample;
g_audio_capture_buffer_start =
(g_audio_capture_buffer_start + 1) % kAudioCaptureBufferSize;
slotCount++;
}
}
g_total_samples_captured += slotCount;
g_latest_audio_timestamp =
(g_total_samples_captured / (kAudioSampleFrequency / 1000));
}
// If we got a DMA error, set the flag.
if (ui32IntMask & AM_HAL_ADC_INT_DERR) {
g_adc_dma_error = true;
}
}
TfLiteStatus InitAudioRecording(tflite::ErrorReporter* error_reporter) {
// Set the clock frequency.
if (AM_HAL_STATUS_SUCCESS !=
am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_SYSCLK_MAX, 0)) {
TF_LITE_REPORT_ERROR(error_reporter,
"Error - configuring the system clock failed.");
return kTfLiteError;
}
// Set the default cache configuration and enable it.
if (AM_HAL_STATUS_SUCCESS !=
am_hal_cachectrl_config(&am_hal_cachectrl_defaults)) {
TF_LITE_REPORT_ERROR(error_reporter,
"Error - configuring the system cache failed.");
return kTfLiteError;
}
if (AM_HAL_STATUS_SUCCESS != am_hal_cachectrl_enable()) {
TF_LITE_REPORT_ERROR(error_reporter,
"Error - enabling the system cache failed.");
return kTfLiteError;
}
// Ensure the CPU is running as fast as possible.
enable_burst_mode(error_reporter);
// Start the CTIMER A3 for timer-based ADC measurements.
init_timerA3_for_ADC();
// Enable interrupts.
NVIC_EnableIRQ(ADC_IRQn);
am_hal_interrupt_master_enable();
// Edge Board Pin Definitions
constexpr int kSfEdgePinMic0 = 11;
const am_hal_gpio_pincfg_t g_sf_edge_pin_mic0 = {
.uFuncSel = AM_HAL_PIN_11_ADCSE2,
};
constexpr int kSfEdgePinMic1 = 29;
const am_hal_gpio_pincfg_t g_sf_edge_pin_mic1 = {
.uFuncSel = AM_HAL_PIN_29_ADCSE1,
};
// Set pins to act as our ADC input
am_hal_gpio_pinconfig(kSfEdgePinMic0, g_sf_edge_pin_mic0);
am_hal_gpio_pinconfig(kSfEdgePinMic1, g_sf_edge_pin_mic1);
// Configure the ADC
adc_config0(error_reporter);
// Trigger the ADC sampling for the first time manually.
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_sw_trigger(g_adc_handle)) {
TF_LITE_REPORT_ERROR(error_reporter, "Error - triggering the ADC0 failed.");
return kTfLiteError;
}
// Enable the LED outputs.
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED_RED, g_AM_HAL_GPIO_OUTPUT_12);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED_YELLOW, g_AM_HAL_GPIO_OUTPUT_12);
am_hal_gpio_output_set(AM_BSP_GPIO_LED_RED);
am_hal_gpio_output_set(AM_BSP_GPIO_LED_YELLOW);
return kTfLiteOk;
}
TfLiteStatus GetAudioSamples(tflite::ErrorReporter* error_reporter,
int start_ms, int duration_ms,
int* audio_samples_size, int16_t** audio_samples) {
if (!g_is_audio_initialized) {
TfLiteStatus init_status = InitAudioRecording(error_reporter);
if (init_status != kTfLiteOk) {
return init_status;
}
g_is_audio_initialized = true;
}
// This is the 'zero' level of the microphone when no audio is present, and
// should be recalibrated if the hardware configuration ever changes. It was
// generated experimentally by averaging some samples captured on a board.
const int16_t kAdcSampleDC = 6003;
// Temporary gain emulation to deal with too-quiet audio on prototype boards.
const int16_t kAdcSampleGain = 10;
// This should only be called when the main thread notices that the latest
// audio sample data timestamp has changed, so that there's new data in the
// capture ring buffer. The ring buffer will eventually wrap around and
// overwrite the data, but the assumption is that the main thread is checking
// often enough and the buffer is large enough that this call will be made
// before that happens.
const int start_offset = start_ms * (kAudioSampleFrequency / 1000);
const int duration_sample_count =
duration_ms * (kAudioSampleFrequency / 1000);
for (int i = 0; i < duration_sample_count; ++i) {
const int capture_index = (start_offset + i) % kAudioCaptureBufferSize;
const int32_t capture_value = g_audio_capture_buffer[capture_index];
int32_t output_value = capture_value - kAdcSampleDC;
output_value *= kAdcSampleGain;
if (output_value < std::numeric_limits<int16_t>::min()) {
output_value = std::numeric_limits<int16_t>::min();
}
if (output_value > std::numeric_limits<int16_t>::max()) {
output_value = std::numeric_limits<int16_t>::max();
}
g_audio_output_buffer[i] = output_value;
}
*audio_samples_size = kMaxAudioSampleSize;
*audio_samples = g_audio_output_buffer;
return kTfLiteOk;
}
int32_t LatestAudioTimestamp() { return g_latest_audio_timestamp; }
#endif // ARDUINO_EXCLUDE_CODE