-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadc-mic.diff
283 lines (263 loc) · 9.87 KB
/
adc-mic.diff
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
diff --git a/build/firefly.uf2 b/build/firefly.uf2
index 08783e0..d363e65 100644
Binary files a/build/firefly.uf2 and b/build/firefly.uf2 differ
diff --git a/source/config.h b/source/config.h
index 18d37c4..e3f5d99 100644
--- a/source/config.h
+++ b/source/config.h
@@ -26,7 +26,7 @@
* MQTT Enable: Turn on Wifi in Core1, but for MQTT + Home Assistant in Station mode
* WiFi Enable: Turn on Wifi in Core1, but host a network + website in Access Point mode
*/
-#define MICROPHONE_ENABLE 0
+#define MICROPHONE_ENABLE 1
#define MQTT_ENABLE 0 // picow only.
#define WIFI_ENABLE 0 // picow only.
@@ -70,8 +70,8 @@
////////////////////////////////////////////////////////////
#define DEBUG_DELAY_MAIN 0 // Used to give time to start Serial Monitoring
#define DEBUG_PRINT_MAIN 0
-#define DEBUG_PRINT_MIC 0
-#define DEBUG_PRINT_MIC_TIMING 0
+#define DEBUG_PRINT_MIC 1
+#define DEBUG_PRINT_MIC_TIMING 1
#define DEBUG_PRINT_FLASH 0
#define DEBUG_PRINT_OTHER 0
diff --git a/source/kylarLEDs/Controllers/FireFlyV1/FireFlyV1Controller.cpp b/source/kylarLEDs/Controllers/FireFlyV1/FireFlyV1Controller.cpp
index 6b82601..85fc6b2 100644
--- a/source/kylarLEDs/Controllers/FireFlyV1/FireFlyV1Controller.cpp
+++ b/source/kylarLEDs/Controllers/FireFlyV1/FireFlyV1Controller.cpp
@@ -14,7 +14,7 @@ FireFlyV1Controller::FireFlyV1Controller()
setStatusLED(255);
initCommunication();
- initBrightness();
+ //initBrightness();
initHue();
initPatternButton();
if(MICROPHONE_ENABLE){
@@ -27,7 +27,12 @@ FireFlyV1Controller::FireFlyV1Controller()
setStatusLED(25);
}
+
+/**
+ * Brightness 0 to 255
+ */
void FireFlyV1Controller::setStatusLED(uint8_t brightness){
+ // Brightness is 0 to 255
static uint8_t initted = 0;
if(initted == 0){
// Set GPIO 10 to PWM function
@@ -201,7 +206,7 @@ double FireFlyV1Controller::getBrightness()
{
static double brightness = 0;
static double lastPot = 0;
- //return 0.7; // Remove this, hardcode high while testing
+ return 0.7; // Remove this, hardcode high while testing
if (timing->takeMsEvery(10))
{
double newPot = analogPot->getValue();
diff --git a/source/kylarLEDs/Controllers/FireFlyV1/FireFlyV1Controller.h b/source/kylarLEDs/Controllers/FireFlyV1/FireFlyV1Controller.h
index 5d3235f..d29fd42 100644
--- a/source/kylarLEDs/Controllers/FireFlyV1/FireFlyV1Controller.h
+++ b/source/kylarLEDs/Controllers/FireFlyV1/FireFlyV1Controller.h
@@ -39,7 +39,8 @@ class FireFlyV1Controller : public Controller{
void setStatusLED(uint8_t brightness);
static strip_t strips[NUM_STRIPS];
- uint8_t PX_pins[PX_PINS] = {14, 15, 16, 9};
+ // uint8_t PX_pins[PX_PINS] = {14, 15, 16, 9};
+ uint8_t PX_pins[PX_PINS] = {16, 17, 8, 9};
uint8_t PX_sms[PX_PINS] = {0, 1, 2, 3};
uint8_t bitflipLUT[256];
uint8_t status_led = 10; // GPIO 10 for status LED
diff --git a/source/kylarLEDs/Controllers/Sensors/Microphone/pdm_mic/audio_spectrogram.c b/source/kylarLEDs/Controllers/Sensors/Microphone/pdm_mic/audio_spectrogram.c
index d443872..5356c68 100644
--- a/source/kylarLEDs/Controllers/Sensors/Microphone/pdm_mic/audio_spectrogram.c
+++ b/source/kylarLEDs/Controllers/Sensors/Microphone/pdm_mic/audio_spectrogram.c
@@ -2,6 +2,18 @@
#include "pico/time.h"
#include "../../../FireFlyW/WiFi/wifi.h"
#include "../../../../../config.h"
+#include "hardware/adc.h"
+#include "hardware/dma.h"
+
+ // adc_pin = 26;
+ // adc = 0;
+ // adc_init();
+ // adc_gpio_init(pin);
+ // adc_select_input(adc);
+
+ //adc_select_input(adc);
+ //adc_read(); // returns 0 to 4095
+
// microphone configuration
const struct pdm_microphone_config pdm_config = {
@@ -40,27 +52,10 @@ const int exec_timing = 1;
absolute_time_t new_time; //Microseconds
absolute_time_t cur_time;
absolute_time_t start_time;
-void pdm_core1_entry(){
- //Init:
- // initialize the hanning window and RFFT instance
- sleep_ms(10); //delay here so that the dma channel doesn't get claimed..
-
- // Unfortunately, this is where I will initialize wifi
- // I want it to be on core1
- // Note: This is just kept as an idea now, WIFI_ENABLE is not active currently.
- // if(WIFI_ENABLE){
- // wifi_init();
- // while(1){
- // cyw43_arch_poll();
- // }
- // }
-
- hanning_window_init_q15(window_q15, FFT_SIZE);
- arm_rfft_init_q15(&S_q15, FFT_SIZE, 0, 1);
- multicore_lockout_victim_init();// NEEDED FOR MULTICORE LOCKOUT
+int dma_chan;
- //Loop:
+void pdm_init(){
// initialize the PDM microphone
if (pdm_microphone_init(&pdm_config) < 0) {
printf("PDM microphone initialization failed!\n");
@@ -76,6 +71,68 @@ void pdm_core1_entry(){
printf("PDM microphone start failed!\n");
while (pdm_microphone_start() < 0) { tight_loop_contents(); }
}
+}
+
+void analog_init(){
+ // Set the ADC pin (e.g., GPIO26)
+ const uint adc_pin = 26;
+ adc_init();
+ adc_gpio_init(adc_pin);
+ adc_select_input(0); // Select ADC input channel 0 (corresponding to GPIO26)
+
+
+ // Configure the ADC to free-run mode
+ adc_fifo_setup(
+ true, // Write each completed conversion to the FIFO
+ true, // Enable DMA data request (DREQ)
+ 1, // DREQ (data request) when at least 1 sample is available
+ false, // Disable error bit in the FIFO
+ true // Enable byte packing (converts 12-bit values to 16-bit values)
+ );
+
+ adc_set_clkdiv(0); // Set clock divider to 0 for maximum sampling rate
+ adc_run(true); // Start ADC in free-running mode
+
+ // Allocate a DMA channel
+ dma_chan = dma_claim_unused_channel(true); // true means we wait for a channel if none are available
+
+
+ dma_channel_config c = dma_channel_get_default_config(dma_chan);
+ channel_config_set_transfer_data_size(&c, DMA_SIZE_16); // 16-bit transfer
+ channel_config_set_read_increment(&c, false); // No read increment (fixed ADC FIFO address)
+ channel_config_set_write_increment(&c, true); // Increment write pointer (buffer address)
+ channel_config_set_dreq(&c, DREQ_ADC); // Set data request to ADC
+
+ dma_channel_configure(
+ dma_chan,
+ &c,
+ capture_buffer_q15, // Destination buffer
+ &adc_hw->fifo, // ADC FIFO as source
+ INPUT_BUFFER_SIZE, // Number of transfers
+ false // Start the DMA immediately?
+ );
+
+}
+
+void pdm_core1_entry(){
+ //Init:
+ // initialize the hanning window and RFFT instance
+ sleep_ms(1000); //delay here so that the dma channel doesn't get claimed..
+
+
+ multicore_lockout_victim_init();// NEEDED FOR MULTICORE LOCKOUT
+ printf("pdm core1 entry");
+
+
+ hanning_window_init_q15(window_q15, FFT_SIZE);
+ arm_rfft_init_q15(&S_q15, FFT_SIZE, 0, 1);
+
+ // Initialize the Microphone hardware
+ analog_init();
+ //Loop:
+
+
+
int starting_bin = 2;
float low_bins = LOW_BINS; // 14
@@ -84,8 +141,18 @@ void pdm_core1_entry(){
uint32_t irq_status = 0;
uint32_t loops_count = 0;
+
+ // Start the DMA transfer
+ dma_channel_start(dma_chan);
while(1) {
+
+ // Wait for DMA transfer to complete
+ if(dma_channel_is_busy(dma_chan)){
+ printf("chan busy");
+ continue;
+ }
+
//irq_status = save_and_disable_interrupts();
//cyw43_arch_poll();
@@ -98,13 +165,13 @@ void pdm_core1_entry(){
loops_count = 0;
// Waiting for new samples
- while (new_samples_captured == 0) {
+ //while (new_samples_captured == 0) {
//printf("-> ");
//cyw43_arch_poll();
//printf("%d| ", ++loops_count);
//tight_loop_contents();
- }
+ //}
if(DEBUG_PRINT_MIC_TIMING){
cur_time = get_absolute_time();
@@ -114,16 +181,24 @@ void pdm_core1_entry(){
new_samples_captured = 0;
// move input buffer values over by INPUT_BUFFER_SIZE samples
+ printf("copy:");
arm_copy_q15(input_q15 + INPUT_BUFFER_SIZE, input_q15, (FFT_SIZE - INPUT_BUFFER_SIZE));
-
+
// copy new samples to end of the input buffer with a bit shift of INPUT_SHIFT
+ printf("shift:");
arm_shift_q15(capture_buffer_q15, INPUT_SHIFT, input_q15 + (FFT_SIZE - INPUT_BUFFER_SIZE), INPUT_BUFFER_SIZE);
// apply the DSP pipeline: Hanning Window + FFT
+ printf("mult:");
arm_mult_q15(window_q15, input_q15, windowed_input_q15, FFT_SIZE);
+ printf("rfft:");
arm_rfft_q15(&S_q15, windowed_input_q15, fft_q15);
+ printf("complx:");
arm_cmplx_mag_q15(fft_q15, fft_mag_q15, FFT_MAG_SIZE);
+ printf("dma:");
+ dma_channel_start(dma_chan);
+
if(DEBUG_PRINT_MIC_TIMING){
cur_time = get_absolute_time();
printf("arm stuff = %.1f us\n", (double)(to_us_since_boot(cur_time)-to_us_since_boot(start_time)));
diff --git a/source/kylarLEDs/Controllers/Sensors/Microphone/pdm_mic/audio_spectrogram.h b/source/kylarLEDs/Controllers/Sensors/Microphone/pdm_mic/audio_spectrogram.h
index 9a0a039..fb9bb87 100644
--- a/source/kylarLEDs/Controllers/Sensors/Microphone/pdm_mic/audio_spectrogram.h
+++ b/source/kylarLEDs/Controllers/Sensors/Microphone/pdm_mic/audio_spectrogram.h
@@ -9,10 +9,10 @@
// constants
-#define SAMPLE_RATE 4000
-#define FFT_SIZE 512
+#define SAMPLE_RATE 32000
+#define FFT_SIZE 2048
#define FFT_MAG_SIZE FFT_SIZE/2
-#define INPUT_BUFFER_SIZE 128
+#define INPUT_BUFFER_SIZE 512
#define INPUT_SHIFT 2 // 2
#define FFT_BINS_SKIP 0
#define FFT_MAG_MAX 2000.0
diff --git a/utils/picotool b/utils/picotool
--- a/utils/picotool
+++ b/utils/picotool
@@ -1 +1 @@
-Subproject commit f6fe6b7c321a2def8950d2a440335dfba19e2eab
+Subproject commit f6fe6b7c321a2def8950d2a440335dfba19e2eab-dirty