Skip to content

Commit 07f7c43

Browse files
Work on adc_mic.c
I get values, but they are all 16380 (0x3FFC).
1 parent e8a4682 commit 07f7c43

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

c_mpos/src/adc_mic.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@
44
#include "esp_heap_caps.h"
55
#include "esp_codec_dev.h" // Include for esp_codec_dev_*
66
#include "adc_mic.h" // Include for audio_codec_adc_cfg_t, audio_codec_new_adc_data, etc.
7+
#include "sdkconfig.h" // for CONFIG_ADC_MIC_TASK_CORE
78
#include <errno.h> // For ENOMEM
89

910
#define ADC_MIC_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__)
1011

1112
static mp_obj_t adc_mic_read(void) {
1213
ADC_MIC_DEBUG_PRINT("Starting adc_mic_read...\n");
14+
ADC_MIC_DEBUG_PRINT("CONFIG_ADC_MIC_TASK_CORE: %d\n", CONFIG_ADC_MIC_TASK_CORE);
1315

1416
// Configure for mono ADC on GPIO1 (ADC1_CHANNEL_0) at 16kHz
15-
audio_codec_adc_cfg_t cfg = DEFAULT_AUDIO_CODEC_ADC_MONO_CFG(ADC_CHANNEL_0, 16000);
17+
//audio_codec_adc_cfg_t cfg = DEFAULT_AUDIO_CODEC_ADC_MONO_CFG(ADC_CHANNEL_0, 16000);
18+
audio_codec_adc_cfg_t cfg = {
19+
.handle = NULL,
20+
.max_store_buf_size = 1024 * 2,
21+
.conv_frame_size = 1024,
22+
.unit_id = ADC_UNIT_1,
23+
.adc_channel_list = ((uint8_t[]){ADC_CHANNEL_0}),
24+
.adc_channel_num = 1,
25+
.sample_rate_hz = 16000,
26+
//.atten = ADC_ATTEN_DB_0, // ← try 0 dB first (0–1.1 V range, higher gain)
27+
.atten = ADC_ATTEN_DB_2_5, // ← try 0 dB first (0–1.1 V range, higher gain)
28+
// or ADC_ATTEN_DB_2_5 for ~0–1.5 V
29+
// keep other fields as default or explicit
30+
};
1631
ADC_MIC_DEBUG_PRINT("Config created for channel %d, sample rate %d\n", ADC_CHANNEL_0, 16000);
1732

1833
ADC_MIC_DEBUG_PRINT("Creating ADC data interface...\n");
@@ -54,7 +69,8 @@ static mp_obj_t adc_mic_read(void) {
5469
ADC_MIC_DEBUG_PRINT("Codec device opened successfully\n");
5570

5671
// Allocate buffer for 16000 samples (16-bit, so 32000 bytes)
57-
const size_t buf_size = 16000 * sizeof(int16_t);
72+
//const size_t buf_size = 16000 * sizeof(int16_t);
73+
const size_t buf_size = 64 * sizeof(int16_t);
5874
ADC_MIC_DEBUG_PRINT("Allocating buffer of size %zu bytes...\n", buf_size);
5975
int16_t *audio_buffer = (int16_t *)heap_caps_malloc(buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
6076
if (audio_buffer == NULL) {
@@ -81,7 +97,24 @@ static mp_obj_t adc_mic_read(void) {
8197

8298
// Create MicroPython bytes object from the buffer
8399
ADC_MIC_DEBUG_PRINT("Creating bytes object from buffer...\n");
84-
mp_obj_t buf_obj = mp_obj_new_bytes((const byte *)audio_buffer, ret);
100+
mp_obj_t buf_obj;
101+
if (ret >= 0) {
102+
ADC_MIC_DEBUG_PRINT("Creating full bytes object from buffer...\n");
103+
buf_obj = mp_obj_new_bytes((const byte *)audio_buffer, buf_size);
104+
105+
ADC_MIC_DEBUG_PRINT("First 16 samples:\n");
106+
size_t samples_to_print = 16;
107+
for (size_t i = 0; i < samples_to_print; i++) {
108+
int16_t sample = audio_buffer[i];
109+
ADC_MIC_DEBUG_PRINT("%4d (0x%04X) ", sample, (uint16_t)sample);
110+
if ((i + 1) % 4 == 0) ADC_MIC_DEBUG_PRINT("\n");
111+
}
112+
ADC_MIC_DEBUG_PRINT("\n");
113+
114+
} else {
115+
ADC_MIC_DEBUG_PRINT("Creating empty bytes object from buffer...\n");
116+
buf_obj = mp_obj_new_bytes((const byte *)audio_buffer, 0);
117+
}
85118

86119
// Cleanup
87120
ADC_MIC_DEBUG_PRINT("Cleaning up...\n");

0 commit comments

Comments
 (0)