P4-Nano: controller port with the C6 as ESP-NOW radio, and the simulator

The controller now builds for the Waveshare ESP32-P4-Nano only (plus the linux
sim and the desktop); the S3 stays for the USB/network bridge in bridge/.

- radio-c6/: C6 firmware, esp-hosted plus ESP-NOW over peer data. The P4 loads
  it from the slave_fw partition when the C6 runs another version (c6_update.c)
- bridge_hosted.c: the C6 as the always-on "c6" bridge, and the P4 as a network
  bridge on UDP 8766 for the desktop
- Ethernet on the P4's EMAC, ES8311 mic; default address 192.168.50.20. The
  W5500, INMP441 and direct ESP-NOW code is gone
- The simulated rig at /sim/ is built into the board image, with an always-on
  "browser" bridge. Its frames go through a queue so a slow page cannot hold up
  the radio, and frames for simulated bars are not sent over the C6. Receiver
  buffers are on the heap; a bar's flash file is only written when it changes
- HTTP server: more handlers, sockets and stack for the rig
- docs/P4_NANO.md, README and Makefile updated; graphify output

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-24 21:45:25 +13:00
1 parent ba8e160aee
commit 4ab0c7c6d7
101 files changed
+3284 -971

No files matched your search

+71 -26
View File
@@ -1,7 +1,7 @@
/*
* INMP441 I2S microphone: level meter plus a 16-bit PCM feed for streaming.
* Reads both I2S slots and uses whichever one the mic is driving (L/R pin to
* GND = left, to 3V3 = right), keeping a running RMS/peak for the last block.
* The ESP32-P4-Nano's microphone (ES8311 codec over I2S): level meter plus a 16-bit PCM feed for streaming.
* Reads both I2S slots and uses whichever one carries sound (the ES8311 puts it in the left one),
* keeping a running RMS/peak for the last block.
* Every block also goes to the beat detector.
*/
#include <math.h>
@@ -12,6 +12,9 @@
#include "esp_check.h"
#include "driver/i2s_std.h"
#include "driver/gpio.h"
#include "driver/i2c_master.h"
#include "esp_codec_dev.h"
#include "esp_codec_dev_defaults.h"
#include "mic.h"
#include "beat.h"
@@ -19,7 +22,7 @@
#define LEVEL_BLOCKS 5 // 100 ms per level reading
#define FULL_SCALE_24BIT 8388608.0f
#define DBFS_FLOOR -120.0f
#define INMP441_SPL_OFFSET 120.0f // 94 dB SPL - (-26 dBFS)
#define SPL_OFFSET 120.0f // 94 dB SPL - (-26 dBFS), an INMP441 figure: rough for the ES8311
static const char *TAG = "mic";
@@ -30,7 +33,7 @@ static portMUX_TYPE s_level_lock = portMUX_INITIALIZER_UNLOCKED;
static StreamBufferHandle_t s_stream_buf;
static volatile bool s_stream_active;
static mic_level_t s_level = { DBFS_FLOOR, DBFS_FLOOR, DBFS_FLOOR + INMP441_SPL_OFFSET };
static mic_level_t s_level = { DBFS_FLOOR, DBFS_FLOOR, DBFS_FLOOR + SPL_OFFSET };
static float to_dbfs(float x)
{
@@ -42,7 +45,7 @@ static void mic_task(void *arg)
static int32_t buf[MIC_BLOCK_SAMPLES * 2]; // interleaved L/R
static int16_t pcm[MIC_BLOCK_SAMPLES];
static float filtered[MIC_BLOCK_SAMPLES];
// One-pole high-pass to strip the INMP441's DC offset
// One-pole high-pass to strip the mic's DC offset
float hp_prev_in = 0.0f, hp_prev_out = 0.0f;
// Level is accumulated over several short blocks so the meter stays steady
double level_sum_sq = 0.0;
@@ -99,7 +102,7 @@ static void mic_task(void *arg)
mic_level_t level;
level.rms_dbfs = to_dbfs(sqrtf((float)(level_sum_sq / level_frames)));
level.peak_dbfs = to_dbfs(level_peak);
level.spl_db = level.rms_dbfs + INMP441_SPL_OFFSET;
level.spl_db = level.rms_dbfs + SPL_OFFSET;
level_sum_sq = 0.0;
level_peak = 0.0f;
level_frames = level_blocks = 0;
@@ -117,6 +120,65 @@ static void mic_task(void *arg)
}
}
// The Waveshare ESP32-P4-Nano's microphone goes through the ES8311 codec's ADC. The codec needs MCLK and its
// registers set over I2C; esp_codec_dev does both and drives the I2S channel (s_rx_chan) as its data path, in the
// 32-bit stereo frames mic_task reads.
static esp_err_t codec_mic_start(void)
{
i2c_master_bus_handle_t bus;
i2c_master_bus_config_t bc = {
.i2c_port = I2C_NUM_0,
.sda_io_num = CONFIG_P4_CODEC_I2C_SDA_GPIO,
.scl_io_num = CONFIG_P4_CODEC_I2C_SCL_GPIO,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
ESP_RETURN_ON_ERROR(i2c_new_master_bus(&bc, &bus), TAG, "I2C bus");
i2s_std_config_t std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(MIC_SAMPLE_RATE), // MCLK = 256 x fs
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO),
.gpio_cfg = {
.mclk = CONFIG_P4_I2S_MCLK_GPIO,
.bclk = CONFIG_P4_I2S_BCLK_GPIO,
.ws = CONFIG_P4_I2S_WS_GPIO,
.dout = I2S_GPIO_UNUSED,
.din = CONFIG_P4_I2S_DIN_GPIO,
},
};
ESP_RETURN_ON_ERROR(i2s_channel_init_std_mode(s_rx_chan, &std_cfg), TAG, "I2S std mode init failed");
ESP_RETURN_ON_ERROR(i2s_channel_enable(s_rx_chan), TAG, "I2S enable failed"); // MCLK runs before the codec
audio_codec_i2s_cfg_t ic = { .port = I2S_NUM_0, .rx_handle = s_rx_chan, .tx_handle = NULL };
const audio_codec_data_if_t *data_if = audio_codec_new_i2s_data(&ic);
audio_codec_i2c_cfg_t cc = { .port = I2C_NUM_0, .addr = CONFIG_P4_CODEC_I2C_ADDR << 1, .bus_handle = bus };
const audio_codec_ctrl_if_t *ctrl_if = audio_codec_new_i2c_ctrl(&cc);
const audio_codec_gpio_if_t *gpio_if = audio_codec_new_gpio();
ESP_RETURN_ON_FALSE(data_if && ctrl_if && gpio_if, ESP_ERR_NO_MEM, TAG, "codec interfaces");
es8311_codec_cfg_t ec = {
.ctrl_if = ctrl_if,
.gpio_if = gpio_if,
.codec_mode = ESP_CODEC_DEV_WORK_MODE_ADC,
.pa_pin = -1,
.use_mclk = true,
};
const audio_codec_if_t *codec_if = es8311_codec_new(&ec);
ESP_RETURN_ON_FALSE(codec_if, ESP_FAIL, TAG, "ES8311 not found on I2C 0x%02x", CONFIG_P4_CODEC_I2C_ADDR);
esp_codec_dev_cfg_t dc = { .dev_type = ESP_CODEC_DEV_TYPE_IN, .codec_if = codec_if, .data_if = data_if };
esp_codec_dev_handle_t dev = esp_codec_dev_new(&dc);
ESP_RETURN_ON_FALSE(dev, ESP_ERR_NO_MEM, TAG, "codec device");
esp_codec_dev_sample_info_t fs = {
.bits_per_sample = 32,
.channel = 2,
.sample_rate = MIC_SAMPLE_RATE,
.mclk_multiple = 256,
};
ESP_RETURN_ON_FALSE(esp_codec_dev_open(dev, &fs) == ESP_CODEC_DEV_OK, ESP_FAIL, TAG, "codec open");
esp_codec_dev_set_in_gain(dev, CONFIG_P4_MIC_PGA_DB);
return ESP_OK;
}
esp_err_t mic_start(void)
{
s_stream_buf = xStreamBufferCreate(STREAM_BUF_BYTES, 1);
@@ -124,30 +186,13 @@ esp_err_t mic_start(void)
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER);
ESP_RETURN_ON_ERROR(i2s_new_channel(&chan_cfg, NULL, &s_rx_chan), TAG, "I2S channel create failed");
i2s_std_config_t std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(MIC_SAMPLE_RATE),
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO),
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = CONFIG_MIC_SCK_GPIO,
.ws = CONFIG_MIC_WS_GPIO,
.dout = I2S_GPIO_UNUSED,
.din = CONFIG_MIC_SD_GPIO,
},
};
ESP_RETURN_ON_ERROR(i2s_channel_init_std_mode(s_rx_chan, &std_cfg), TAG, "I2S std mode init failed");
// SD tri-states outside the mic's slot; the datasheet asks for a pull-down
gpio_pullup_dis(CONFIG_MIC_SD_GPIO);
gpio_pulldown_en(CONFIG_MIC_SD_GPIO);
ESP_RETURN_ON_ERROR(i2s_channel_enable(s_rx_chan), TAG, "I2S enable failed");
ESP_RETURN_ON_ERROR(codec_mic_start(), TAG, "ES8311 start failed");
ESP_RETURN_ON_ERROR(beat_start(MIC_SAMPLE_RATE), TAG, "Beat detector start failed");
if (xTaskCreate(mic_task, "mic", 6144, NULL, 5, NULL) != pdPASS) {
return ESP_ERR_NO_MEM;
}
ESP_LOGI(TAG, "INMP441 started at %d Hz", MIC_SAMPLE_RATE);
ESP_LOGI(TAG, "ES8311 microphone started at %d Hz", MIC_SAMPLE_RATE);
return ESP_OK;
}