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

+150
View File
@@ -0,0 +1,150 @@
#include "sdkconfig.h"
#if CONFIG_IDF_TARGET_ESP32P4
#include "c6_update.h"
#include <string.h>
#include "esp_app_desc.h"
#include "esp_hosted.h"
#include "esp_hosted_ota.h"
#include "esp_image_format.h"
#include "esp_log.h"
#include "esp_partition.h"
#include "esp_system.h"
#include "nvs.h"
#include "eh_host_feat_peer_data.h"
#include "freertos/semphr.h"
#include "radio_proto.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
static const char *TAG = "c6_update";
#define SLAVE_FW_SUBTYPE 0x40
#define CHUNK 1400
// Size of an app image: header, segments, checksum padded to 16, optional SHA-256. 0 if it is not one.
static uint32_t image_size(const esp_partition_t *p)
{
esp_image_header_t h;
if (esp_partition_read(p, 0, &h, sizeof h) != ESP_OK || h.magic != ESP_IMAGE_HEADER_MAGIC) {
return 0;
}
uint32_t off = sizeof h;
for (int i = 0; i < h.segment_count; i++) {
esp_image_segment_header_t s;
if (esp_partition_read(p, off, &s, sizeof s) != ESP_OK || s.data_len > p->size) {
return 0;
}
off += sizeof s + s.data_len;
}
off = (off + 1 + 15) & ~15u; // the checksum byte, padded to 16
if (h.hash_appended) {
off += 32;
}
return off <= p->size ? off : 0;
}
static char s_cur[32];
static SemaphoreHandle_t s_got;
static void on_version(uint32_t id, const uint8_t *data, size_t len, void *ctx)
{
(void)id;
(void)ctx;
size_t n = len < sizeof(s_cur) - 1 ? len : sizeof(s_cur) - 1;
memcpy(s_cur, data, n);
s_cur[n] = 0;
xSemaphoreGive(s_got);
}
// The version radio-c6 reports over peer data; false if the C6 does not answer (it runs something else)
static bool c6_version(char *out, size_t n)
{
s_got = xSemaphoreCreateBinary();
if (!s_got || eh_host_peer_data_register(RADIO_MSG_VERSION, on_version, NULL) != ESP_OK) {
return false;
}
uint8_t ask = RADIO_CTL_VERSION;
bool ok = false;
for (int i = 0; i < 3 && !ok; i++) { // an unregistered msg id on the C6 is simply dropped
eh_host_peer_data_send(RADIO_MSG_CTL, &ask, 1);
ok = xSemaphoreTake(s_got, pdMS_TO_TICKS(500)) == pdTRUE;
}
if (ok) {
strlcpy(out, s_cur, n);
}
return ok;
}
void c6_update_if_needed(void)
{
const esp_partition_t *p = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, SLAVE_FW_SUBTYPE, "slave_fw");
if (!p) {
ESP_LOGW(TAG, "no slave_fw partition: the C6 keeps its firmware");
return;
}
uint32_t size = image_size(p);
esp_app_desc_t img;
if (!size || esp_partition_read(p, sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t), &img,
sizeof img) != ESP_OK || img.magic_word != ESP_APP_DESC_MAGIC_WORD) {
ESP_LOGW(TAG, "slave_fw holds no C6 image (make flash writes it): the C6 keeps its firmware");
return;
}
char cur[32] = "";
bool known = c6_version(cur, sizeof(cur));
if (known && strncmp(cur, img.version, sizeof(cur)) == 0) {
ESP_LOGI(TAG, "C6 runs %s, as in slave_fw", cur);
return;
}
// One try per image: if the C6 still runs something else after it, say so and carry on rather than loop
nvs_handle_t nv;
char tried[32] = "";
size_t tl = sizeof(tried);
if (nvs_open("c6_update", NVS_READWRITE, &nv) != ESP_OK) {
return;
}
if (nvs_get_str(nv, "tried", tried, &tl) == ESP_OK && strncmp(tried, img.version, sizeof(tried)) == 0) {
ESP_LOGE(TAG, "C6 runs '%.32s' though '%.32s' was sent to it already: not trying again (erase NVS key "
"c6_update/tried to retry)", known ? cur : "not radio-c6", img.version);
nvs_close(nv);
return;
}
nvs_set_str(nv, "tried", img.version);
nvs_commit(nv);
nvs_close(nv);
ESP_LOGW(TAG, "C6 runs '%.32s', slave_fw has '%.32s' (%u bytes): updating the C6",
known ? cur : "not radio-c6", img.version, (unsigned)size);
esp_err_t r = esp_hosted_cp_ota_begin();
if (r != ESP_OK) {
ESP_LOGE(TAG, "C6 OTA begin failed (%s): it keeps its firmware", esp_err_to_name(r));
return;
}
static uint8_t buf[CHUNK];
for (uint32_t off = 0; off < size;) {
uint32_t n = size - off > CHUNK ? CHUNK : size - off;
if (esp_partition_read(p, off, buf, n) != ESP_OK || esp_hosted_cp_ota_write(buf, n) != ESP_OK) {
ESP_LOGE(TAG, "C6 OTA write failed at %u: it keeps its firmware", (unsigned)off);
esp_hosted_cp_ota_end();
return;
}
off += n;
if ((off / CHUNK) % 200 == 0 || off == size) {
ESP_LOGI(TAG, "sent %u / %u bytes", (unsigned)off, (unsigned)size);
}
}
r = esp_hosted_cp_ota_end();
if (r != ESP_OK) {
ESP_LOGE(TAG, "C6 OTA end failed (%s, image rejected?): it keeps its firmware", esp_err_to_name(r));
return;
}
r = esp_hosted_cp_ota_activate();
if (r != ESP_OK) {
ESP_LOGE(TAG, "C6 OTA activate failed (%s): it keeps its firmware", esp_err_to_name(r));
return;
}
ESP_LOGW(TAG, "C6 updated: restarting the board");
vTaskDelay(pdMS_TO_TICKS(2000)); // the C6 restarts itself into the new image too
esp_restart();
}
#endif // CONFIG_IDF_TARGET_ESP32P4