/* * ESP-NOW radio for the ESP32-P4-Nano's controller. The C6 runs esp-hosted (the P4's Wi-Fi over SDIO), and * this file adds what esp-hosted does not forward: ESP-NOW. Frames arrive from the P4 in peer-data batches * (bridge/main/radio_proto.h), go out one at a time, and every frame heard goes back up in batches. * Same job as the USB bridge in bridge/, over SDIO instead of USB. */ #include #include "eh_cp_feat_peer_data.h" #include "esp_app_desc.h" #include "esp_event.h" #include "esp_log.h" #include "esp_mac.h" #include "esp_now.h" #include "esp_wifi.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/semphr.h" #include "freertos/task.h" #include "nvs_flash.h" #include "bridge_proto.h" #include "radio_proto.h" static const char *TAG = "radio_now"; #define PEERS_MAX 19 // ESP-NOW allows 20 peers: 19 unicast and the broadcast one #define TX_QUEUE_LEN 64 #define RX_QUEUE_LEN 64 #define SEND_WAIT_MS 100 typedef struct { uint8_t mac[6]; uint8_t len; uint8_t data[BRIDGE_PKT_MAX]; } pkt_t; static const uint8_t BROADCAST[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; static QueueHandle_t s_txq; static QueueHandle_t s_rxq; static SemaphoreHandle_t s_sent; static volatile bool s_now_up; static uint8_t s_peers[PEERS_MAX][6]; // most recently used first static int s_npeers; static uint32_t s_tx, s_tx_fail, s_rx, s_drop; /* ---- ESP-NOW ----------------------------------------------------------------------------------- */ static void on_sent(const esp_now_send_info_t *info, esp_now_send_status_t status) { (void)info; if (status != ESP_NOW_SEND_SUCCESS) { s_tx_fail++; } xSemaphoreGive(s_sent); } static void on_recv(const esp_now_recv_info_t *info, const uint8_t *data, int len) { if (len <= 0 || len > BRIDGE_PKT_MAX) { return; } pkt_t p; memcpy(p.mac, info->src_addr, 6); p.len = (uint8_t)len; memcpy(p.data, data, len); if (xQueueSend(s_rxq, &p, 0) == pdTRUE) { s_rx++; } else { s_drop++; } } // Keep the peer table within the ESP-NOW limit, least recently used dropped first (as bridge/ does) static void ensure_peer(const uint8_t *mac) { if (memcmp(mac, BROADCAST, 6) == 0) { if (!esp_now_is_peer_exist(BROADCAST)) { esp_now_peer_info_t p = { .channel = 0, .ifidx = WIFI_IF_STA, .encrypt = false }; memcpy(p.peer_addr, BROADCAST, 6); esp_now_add_peer(&p); } return; } for (int i = 0; i < s_npeers; i++) { if (memcmp(s_peers[i], mac, 6) == 0) { uint8_t hit[6]; memcpy(hit, mac, 6); memmove(s_peers[1], s_peers[0], i * 6); memcpy(s_peers[0], hit, 6); return; } } if (s_npeers == PEERS_MAX) { esp_now_del_peer(s_peers[PEERS_MAX - 1]); s_npeers--; } esp_now_peer_info_t p = { .channel = 0, .ifidx = WIFI_IF_STA, .encrypt = false }; memcpy(p.peer_addr, mac, 6); if (esp_now_add_peer(&p) != ESP_OK) { return; } memmove(s_peers[1], s_peers[0], s_npeers * 6); memcpy(s_peers[0], mac, 6); s_npeers++; } // The P4 starts and stops Wi-Fi over esp-hosted; ESP-NOW follows it static void on_wifi(void *arg, esp_event_base_t base, int32_t id, void *data) { (void)arg; (void)base; (void)data; if ((id == WIFI_EVENT_STA_START || id == WIFI_EVENT_AP_START) && !s_now_up) { if (esp_now_init() == ESP_OK) { esp_now_register_send_cb(on_sent); esp_now_register_recv_cb(on_recv); s_npeers = 0; s_now_up = true; uint8_t ch; wifi_second_chan_t sc; esp_wifi_get_channel(&ch, &sc); ESP_LOGI(TAG, "ESP-NOW up on channel %u", ch); } else { ESP_LOGE(TAG, "esp_now_init failed"); } } else if (id == WIFI_EVENT_STA_STOP && s_now_up) { s_now_up = false; esp_now_deinit(); ESP_LOGI(TAG, "ESP-NOW down (Wi-Fi stopped)"); } } static void tx_task(void *arg) { (void)arg; pkt_t p; for (;;) { xQueueReceive(s_txq, &p, portMAX_DELAY); if (!s_now_up) { s_tx_fail++; continue; } ensure_peer(p.mac); xSemaphoreTake(s_sent, 0); if (esp_now_send(p.mac, p.data, p.len) != ESP_OK) { s_tx_fail++; continue; } s_tx++; xSemaphoreTake(s_sent, pdMS_TO_TICKS(SEND_WAIT_MS)); } } // Everything heard, packed into as few peer-data messages as the queue allows static void rx_task(void *arg) { (void)arg; static uint8_t batch[RADIO_BATCH_MAX]; pkt_t p; for (;;) { xQueueReceive(s_rxq, &p, portMAX_DELAY); size_t n = 0; do { memcpy(batch + n, p.mac, 6); batch[n + 6] = p.len; memcpy(batch + n + RADIO_ENTRY_HDR, p.data, p.len); n += RADIO_ENTRY_HDR + p.len; } while (n + RADIO_ENTRY_HDR + BRIDGE_PKT_MAX <= sizeof(batch) && xQueueReceive(s_rxq, &p, 0) == pdTRUE); if (eh_cp_feat_peer_data_send(RADIO_MSG_RX, batch, n) != ESP_OK) { s_drop++; } } } /* ---- the P4's messages -------------------------------------------------------------------------- */ static void put_u32(uint8_t *p, uint32_t v) { p[0] = v & 0xFF; p[1] = (v >> 8) & 0xFF; p[2] = (v >> 16) & 0xFF; p[3] = (v >> 24) & 0xFF; } static void send_info(void) { uint8_t b[BRIDGE_INFO_LEN] = { 0 }; uint8_t ch = 0; wifi_second_chan_t sc; b[0] = BRIDGE_PROTO_VERSION; esp_read_mac(b + 1, ESP_MAC_WIFI_STA); if (esp_wifi_get_channel(&ch, &sc) == ESP_OK) { b[7] = ch; } put_u32(b + 8, s_tx); put_u32(b + 12, s_tx_fail); put_u32(b + 16, s_rx); put_u32(b + 20, s_drop); eh_cp_feat_peer_data_send(RADIO_MSG_INFO, b, sizeof(b)); } // Runs in the RPC receive context: only queue, no waiting static void on_tx(uint32_t id, const uint8_t *data, size_t len, void *ctx) { (void)id; (void)ctx; size_t off = 0; while (off + RADIO_ENTRY_HDR <= len) { pkt_t p; memcpy(p.mac, data + off, 6); p.len = data[off + 6]; if (p.len == 0 || p.len > BRIDGE_PKT_MAX || off + RADIO_ENTRY_HDR + p.len > len) { break; } memcpy(p.data, data + off + RADIO_ENTRY_HDR, p.len); off += RADIO_ENTRY_HDR + p.len; if (xQueueSend(s_txq, &p, 0) != pdTRUE) { s_tx_fail++; } } } static void on_ctl(uint32_t id, const uint8_t *data, size_t len, void *ctx) { (void)id; (void)ctx; if (len < 1) { return; } if (data[0] == BRIDGE_MSG_SET_CHANNEL && len >= 2 && data[1] >= 1 && data[1] <= 13) { esp_wifi_set_channel(data[1], WIFI_SECOND_CHAN_NONE); ESP_LOGI(TAG, "channel %u", data[1]); } if (data[0] == BRIDGE_MSG_SET_CHANNEL || data[0] == BRIDGE_MSG_HELLO) { send_info(); } else if (data[0] == RADIO_CTL_VERSION) { const char *v = esp_app_get_description()->version; eh_cp_feat_peer_data_send(RADIO_MSG_VERSION, (const uint8_t *)v, strlen(v)); } } void app_main(void) { esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(esp_event_loop_create_default()); s_txq = xQueueCreate(TX_QUEUE_LEN, sizeof(pkt_t)); s_rxq = xQueueCreate(RX_QUEUE_LEN, sizeof(pkt_t)); s_sent = xSemaphoreCreateBinary(); ESP_ERROR_CHECK(s_txq && s_rxq && s_sent ? ESP_OK : ESP_ERR_NO_MEM); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, on_wifi, NULL)); ESP_ERROR_CHECK(eh_cp_feat_peer_data_register_callback(RADIO_MSG_TX, on_tx, NULL)); ESP_ERROR_CHECK(eh_cp_feat_peer_data_register_callback(RADIO_MSG_CTL, on_ctl, NULL)); xTaskCreate(tx_task, "now_tx", 3072, NULL, 6, NULL); xTaskCreate(rx_task, "now_rx", 3072, NULL, 5, NULL); ESP_LOGI(TAG, "radio-c6 %s ready", esp_app_get_description()->version); }