p4-probe identifies the P4 and its C6 over esp-hosted SDIO. c6-ota holds the C6 coprocessor build and the two host updaters (3.1 and the 2.x one that moved the factory C6 firmware to esp-hosted 3.1). Also the pending graphify output. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
3.5 KiB
C
88 lines
3.5 KiB
C
// One-shot C6 updater for the Waveshare ESP32-P4-Nano, built on esp-hosted 2.x so it speaks the
|
|
// RPC protocol of the C6's old factory firmware. Streams the C6 image that is already in the
|
|
// P4's "slave_fw" flash partition (written by ../mcu_host's flash step) and then reboots.
|
|
#include <string.h>
|
|
#include "esp_log.h"
|
|
#include "esp_partition.h"
|
|
#include "esp_app_desc.h"
|
|
#include "esp_image_format.h"
|
|
#include "esp_event.h"
|
|
#include "esp_netif.h"
|
|
#include "nvs_flash.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_system.h"
|
|
#include "esp_hosted.h"
|
|
#include "esp_hosted_ota.h"
|
|
|
|
static const char *TAG = "c6-ota";
|
|
#define CHUNK 1400
|
|
|
|
// Size of an ESP app image: header, segments, checksum padded to 16, optional SHA-256.
|
|
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) return 0;
|
|
off += sizeof s + s.data_len;
|
|
}
|
|
off = (off + 1 + 15) & ~15u; // checksum byte, then pad to 16
|
|
if (h.hash_appended) off += 32;
|
|
return off <= p->size ? off : 0;
|
|
}
|
|
|
|
static void halt(const char *why)
|
|
{
|
|
ESP_LOGE(TAG, "%s", why);
|
|
for (;;) { ESP_LOGE(TAG, "RESULT FAIL: %s", why); vTaskDelay(pdMS_TO_TICKS(5000)); }
|
|
}
|
|
|
|
void app_main(void)
|
|
{
|
|
nvs_flash_init();
|
|
esp_netif_init();
|
|
esp_event_loop_create_default();
|
|
|
|
if (esp_hosted_connect_to_slave() != 0) halt("cannot connect to C6");
|
|
|
|
esp_hosted_coprocessor_fwver_t v = {0};
|
|
if (esp_hosted_get_coprocessor_fwversion(&v) == ESP_OK)
|
|
ESP_LOGI(TAG, "C6 firmware before update: %lu.%lu.%lu", (unsigned long)v.major1,
|
|
(unsigned long)v.minor1, (unsigned long)v.patch1);
|
|
else
|
|
ESP_LOGW(TAG, "C6 did not report a version (old firmware)");
|
|
|
|
const esp_partition_t *p = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 0x40, "slave_fw");
|
|
if (!p) halt("no slave_fw partition");
|
|
uint32_t size = image_size(p);
|
|
if (!size) halt("slave_fw does not hold a valid image");
|
|
esp_app_desc_t d;
|
|
esp_partition_read(p, sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t), &d, sizeof d);
|
|
ESP_LOGI(TAG, "New C6 image: %u bytes, project '%s', IDF %s", (unsigned)size, d.project_name, d.idf_ver);
|
|
|
|
if (esp_hosted_slave_ota_begin() != ESP_OK) halt("OTA begin failed");
|
|
static uint8_t buf[CHUNK];
|
|
for (uint32_t off = 0; off < size;) {
|
|
uint32_t n = size - off > CHUNK ? CHUNK : size - off;
|
|
esp_partition_read(p, off, buf, n);
|
|
if (esp_hosted_slave_ota_write(buf, n) != ESP_OK) {
|
|
ESP_LOGE(TAG, "write failed at offset %u", (unsigned)off);
|
|
halt("OTA write failed");
|
|
}
|
|
off += n;
|
|
if ((off / CHUNK) % 100 == 0 || off == size)
|
|
ESP_LOGI(TAG, "sent %u / %u bytes", (unsigned)off, (unsigned)size);
|
|
}
|
|
if (esp_hosted_slave_ota_end() != ESP_OK) halt("OTA end failed (image rejected)");
|
|
ESP_LOGI(TAG, "OTA end OK");
|
|
esp_err_t a = esp_hosted_slave_ota_activate(); // only on C6 fw 2.6+; older ones switch at end
|
|
ESP_LOGI(TAG, "activate: %s", esp_err_to_name(a));
|
|
|
|
for (int i = 0; i < 3; i++) { ESP_LOGI(TAG, "RESULT OK: C6 updated, rebooting"); vTaskDelay(pdMS_TO_TICKS(1000)); }
|
|
esp_restart(); // reboots the P4, which resets the C6 into the new image
|
|
}
|