Struggles with INMP441 -- i2s connects; zeroed data!

CascadingStatic
Posts: 5
Joined: Sat Feb 17, 2024 6:13 pm

Struggles with INMP441 -- i2s connects; zeroed data!

Postby CascadingStatic » Sat Feb 17, 2024 7:01 pm

Hello all.
I apologise for anything lacking in my explanation/request for help; I have tried to be thorough; I'm a junior and have no recourse to assistance/advice in my company.

I finally got my setup working after adapting some I2S code -- reading the mic, FFTing it etc accurately to a pure tone played on a speaker.
Then... my mic started returning the same value e.g. `1070517748` for every sample (which when FFT'd, results in a 0 for every frequency's corresponding amplitude bucket). No crashes, no errors be it ESP_ERR or memory allocation issues.

My confusion is that, as far as I can tell, I haven't actually changed anything between when I got it working and when it broke... but I am extremely limited in the equipment and testing resources available to me. I would be enormously grateful for any pointers as to where this is going wrong, because previously I got this code giving me working audio!

Setup: INMP441 with an ESP32S3 dev kit.

https://imgur.com/a/5m75PuA

Things I've done:
  • checked pins
  • changed mic
  • changed ESP32S3
  • changed pin positions
  • error checked the bytes-read (4096 being read)
  • error checked the `esp_err_t` returned by `i2s_read()` (ESP_OK returned)
  • Reverted to older, working commit (it has stopped working!)
For the sake of brevity etc I am only showing relevant parts of the codebase:
  1. // ___________BUFFER HANDLING___________
  2.  
  3. #define I2S_DMA_BUFFER_COUNT 4
  4. #define I2S_DMA_BUFFER_LENGTH 1024
  5.  
  6. // ___________SAMPLE RATE HANDLING___________
  7. #define I2S_SAMPLE_RATE 3000
  8.  
  9. // ___________INMP441 I2S microphone connections___________
  10. // These are defined in config.cpp
  11.  
  12. extern i2s_pin_config_t I2S_MIC_PINS;
  13. extern i2s_config_t I2S_MIC_CONFIG;
  14. extern i2s_port_t I2S_MIC_PORT;
  15.  
  16. #define I2S_MIC_WS 4
  17. #define I2S_MIC_SD 6
  18. #define I2S_MIC_SCK 5
  1. #include "config.h"
  2.  
  3. i2s_port_t I2S_MIC_PORT = I2S_NUM_0;
  4.  
  5. i2s_pin_config_t I2S_MIC_PINS = {
  6.   .bck_io_num = I2S_MIC_SCK,
  7.   .ws_io_num = I2S_MIC_WS,
  8.   .data_out_num = I2S_PIN_NO_CHANGE,
  9.   .data_in_num = I2S_MIC_SD,
  10. };
  11.  
  12. i2s_config_t I2S_MIC_CONFIG = {
  13.   .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
  14.   .sample_rate = I2S_SAMPLE_RATE,
  15.   .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
  16.   .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  17.   .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  18.   .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  19.   .dma_buf_count = I2S_DMA_BUFFER_COUNT,
  20.   .dma_buf_len = I2S_DMA_BUFFER_LENGTH,
  21.   .use_apll = false, /* "Audio Phase-Locked Loop" */
  22.   .tx_desc_auto_clear = false, /* helps in avoiding noise in case of underflow/data unavailability */
  23.   .fixed_mclk = 0,
  24. };
  1. static const char* TAG = "I2S MIC";
  2. static i2s_port_t m_micPort;
  3.  
  4. esp_err_t mic_init(i2s_port_t port, i2s_pin_config_t pins, i2s_config_t config)
  5. {
  6.   esp_err_t ret = i2s_driver_install(port, &config, 0, NULL);
  7.   if (ret != ESP_OK) {
  8.     err_log_allOutputs(TAG, "i2s_driver_install", &ret, NULL);
  9.     return ret;
  10.   }
  11.   sleep(0.05);
  12.  
  13.   ret = i2s_set_pin(port, &pins);
  14.   if (ret != ESP_OK) {
  15.     err_log_allOutputs(TAG, "i2s_set_pin", &ret, NULL);
  16.     return ret;
  17.   }
  18.   sleep(0.05);
  19.  
  20.   ret = i2s_start(port);
  21.   if (ret != ESP_OK) {
  22.     err_log_allOutputs(TAG, "i2s_start", &ret, NULL);
  23.     return ret;
  24.   }
  25.  
  26.   m_micPort = port;
  27.   return ret;
  28. }
  29.  
  30.  
  31. esp_err_t mic_deinit(void)
  32. {
  33.   esp_err_t ret = i2s_driver_uninstall(m_micPort);
  34.   if (ret != ESP_OK) {
  35.     err_log_allOutputs(TAG, "mic_deinit", &ret, NULL);
  36.     ESP_ERROR_CHECK_WITHOUT_ABORT(ret);
  37.   }
  38.   return ret;
  39. }
  40.  
  41.  
  42. int mic_read(i2s_port_t port, int16_t *samples, int count)
  43. {
  44.   int32_t *raw_samples = (int32_t *)malloc(sizeof(int32_t) * count);
  45.   size_t bytes_read = 0;
  46.   esp_err_t result = i2s_read(port, raw_samples, sizeof(int32_t) * count, &bytes_read, portMAX_DELAY);
  47.   // here some checking prints to serial
  48.   int samples_read = bytes_read / sizeof(int32_t);
  49.   for (int i = 0; i < samples_read; i++) {
  50.     samples[i] = (raw_samples[i] & 0xFFFFFFF0) >> 11;
  51.   }
  52.   free(raw_samples);
  53.   return samples_read;
  54. }
  55.  
When I run mic_read in the immediately above function, I allocate the buffer of `int16_t* samples` -- this is not mallocing incorrectly and memsetting or not memsetting appears to make no difference.

I am setting up and running like this (error checks et all stripped out):
  1. mic_init(I2S_MIC_PORT, I2S_MIC_PINS, I2S_MIC_CONFIG);
  2.  
  3. ...
  4. int16_t* audioSamples = mic_allocateBuffer(samplesLen);
  5. int samplesRead = mic_read(I2S_MIC_PORT, audioSamples, I2S_DMA_BUFFER_LENGTH);
  6. ...
  7.  
  8. mic_deinit();
Thank you sincerely to anybody who took the time to read this. I'm sure I'm being an idiot and missing something obvious/lacking key knowledge somewhere, but it's getting a little disheartening now...
Last edited by CascadingStatic on Sun Feb 18, 2024 6:17 pm, edited 1 time in total.

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Struggles with INMP441 -- i2s connects; zeroed data!

Postby ESP_Sprite » Sun Feb 18, 2024 3:26 am

Okay, the fact that you 1. previously had the code working, and 2. rolling back to a known-working earlier version also results in breakage indicate you have a hardware issue. However, you replacing everything already means you tried the easier bits of troubleshooting.

Is there any way you can buy/borrow/acquire something that can show you more on the electric level? Ideally an oscilloscope, but a logic analyzer would already get you halfway there as well. If that is really hard, you may be able to build one using e.g. https://github.com/EUA/ESP32_LogicAnalyzer and a spare ESP32 board.

User avatar
ok-home
Posts: 78
Joined: Sun May 02, 2021 7:23 pm
Location: Russia Novosibirsk
Contact:

Re: Struggles with INMP441 -- i2s connects; zeroed data!

Postby ok-home » Sun Feb 18, 2024 4:27 am

hi
try increasing
#define I2S_SAMPLE_RATE 3000

CascadingStatic
Posts: 5
Joined: Sat Feb 17, 2024 6:13 pm

Re: Struggles with INMP441 -- i2s connects; zeroed data!

Postby CascadingStatic » Sun Feb 18, 2024 3:30 pm

ESP_Sprite wrote:
Sun Feb 18, 2024 3:26 am
Okay, the fact that you 1. previously had the code working, and 2. rolling back to a known-working earlier version also results in breakage indicate you have a hardware issue. However, you replacing everything already means you tried the easier bits of troubleshooting.

Is there any way you can buy/borrow/acquire something that can show you more on the electric level? Ideally an oscilloscope, but a logic analyzer would already get you halfway there as well. If that is really hard, you may be able to build one using e.g. https://github.com/EUA/ESP32_LogicAnalyzer and a spare ESP32 board.
I also just changed out the breadboard with just F:F connectors -- not that either!

I will ask management and try and get one sent to me. In the meantime, I will look at this library -- thank you for pointing me to this as there's still much I'm unaware of/wouldn't think to look for.
ok-home wrote:
Sun Feb 18, 2024 4:27 am
hi
try increasing
#define I2S_SAMPLE_RATE 3000
Thanks for replying -- I tried altering this but had no observable changes to amplitudes changing this. Testing at e.g. 8k and 44100 all the same, just changes the number of samples per read. 3k is well above my Nyquist and worked well previously.

CascadingStatic
Posts: 5
Joined: Sat Feb 17, 2024 6:13 pm

Re: Struggles with INMP441 -- i2s connects; zeroed data!

Postby CascadingStatic » Sun Feb 18, 2024 6:21 pm

I am extremely stupid and had integrated problems with my samples handling when porting my code that I mistook for hardware out of lack of thoroughness. I didn't spot it because I am a fool and was likely lucky before getting something working and you both have my sincere apologies for wasting your time.

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Struggles with INMP441 -- i2s connects; zeroed data!

Postby ESP_Sprite » Mon Feb 19, 2024 3:15 am

CascadingStatic wrote:
Sun Feb 18, 2024 6:21 pm
I am extremely stupid and had integrated problems with my samples handling when porting my code that I mistook for hardware out of lack of thoroughness. I didn't spot it because I am a fool and was likely lucky before getting something working and you both have my sincere apologies for wasting your time.
Don't worry about it - I doubt there's any experienced engineer who hasn't banged their head against some problem for a few days until figuring out it was something wrong with their preconceptions. Glad you found it :)

Who is online

Users browsing this forum: No registered users and 135 guests