Add esp32c6 802.15.4

This commit is contained in:
cpq committed 2026-11-23 14:56:21 +00:00
1 parent 2cf96bcb19
commit ecedc31e34
40 files changed
+981 -247

No files matched your search

+191
View File
@@ -0,0 +1,191 @@
#include "l2_mesh.h"
#include "log.h"
#include "net_builtin.h"
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ESP32_802154) && \
MG_ENABLE_DRIVER_ESP32_802154
#if MG_ARCH != MG_ARCH_ESP32
#error "MG_ENABLE_DRIVER_ESP32_802154 needs MG_ARCH_ESP32 (ESP-IDF)"
#endif
// ESP-IDF owns the PHY, coexistence and interrupts. Its callbacks run in
// interrupt context and pass radio frames through a lock-free queue, read by
// mgi_esp32_802154_poll(). In both directions frame[0] is the PHR, the length
// including FCS
#include <esp_attr.h>
#include <esp_ieee802154.h>
#include <esp_timer.h>
static struct mg_tcpip_if *s_esp32_802154_ifp;
static struct mg_queue s_esp32_802154_rx;
static char s_esp32_802154_rxbuf[2048];
static volatile bool s_esp32_802154_txing, s_esp32_802154_tx_done;
static uint64_t s_esp32_802154_txstart; // Send start, us
// Frame for the radio: PHR, the length including FCS, then the PSDU
static uint8_t s_esp32_802154_txframe[128] __attribute__((aligned(4)));
// Queue a received PSDU without its FCS
static void mg_esp32_802154_rx_frame(const uint8_t *psdu, size_t len) {
mg_mesh_rx_queue(&s_esp32_802154_rx, s_esp32_802154_ifp, psdu, len);
}
// ESP-IDF only: it accepts a frame and may refuse it later, because of Wi-Fi
// coexistence, a busy channel or an abort. Such a frame is sent again after
// this pause, until MG_MESH_TX_TIMEOUT_US. Without it, the mesh would count
// the refused frame as sent
#define MGI_ESP32_802154_RETRY_US 200
static volatile bool s_esp32_802154_retry; // A resend is pending
static volatile uint32_t s_esp32_802154_retry_at; // When to resend, us
// ESP-IDF's esp_ieee802154 driver calls the three functions below by name,
// from interrupt context. They are global only for that reason, and IRAM_ATTR
// keeps them in IRAM
void IRAM_ATTR esp_ieee802154_receive_done(uint8_t *frame,
esp_ieee802154_frame_info_t *info) {
(void) info;
if (frame[0] > 2) mg_esp32_802154_rx_frame(frame + 1, frame[0] - 2U);
esp_ieee802154_receive_handle_done(frame);
}
void IRAM_ATTR esp_ieee802154_transmit_done(const uint8_t *frame,
const uint8_t *ack,
esp_ieee802154_frame_info_t *info) {
(void) frame, (void) info;
if (ack != NULL) esp_ieee802154_receive_handle_done(ack);
s_esp32_802154_tx_done = true;
}
void IRAM_ATTR esp_ieee802154_transmit_failed(const uint8_t *frame,
esp_ieee802154_tx_error_t err) {
(void) frame;
if (err == ESP_IEEE802154_TX_ERR_COEXIST ||
err == ESP_IEEE802154_TX_ERR_CCA_BUSY ||
err == ESP_IEEE802154_TX_ERR_ABORT) {
s_esp32_802154_retry_at =
(uint32_t) esp_timer_get_time() + MGI_ESP32_802154_RETRY_US;
s_esp32_802154_retry = true; // Set last: the poll reads it first
} else {
s_esp32_802154_tx_done = true;
}
}
static bool mg_esp32_802154_hw_init(uint16_t address) {
// rx_when_idle: the driver returns to receive after each transmission
return esp_ieee802154_enable() == ESP_OK &&
esp_ieee802154_set_promiscuous(false) == ESP_OK &&
esp_ieee802154_set_coordinator(false) == ESP_OK &&
esp_ieee802154_set_rx_when_idle(true) == ESP_OK &&
esp_ieee802154_set_panid(MG_802154_PAN_ID) == ESP_OK &&
esp_ieee802154_set_short_address(address) == ESP_OK &&
esp_ieee802154_set_channel(MG_802154_CHANNEL) == ESP_OK &&
esp_ieee802154_set_txpower(0) == ESP_OK &&
esp_ieee802154_receive() == ESP_OK;
}
static bool mg_esp32_802154_hw_tx(void) {
s_esp32_802154_retry = false;
return esp_ieee802154_transmit(s_esp32_802154_txframe, false) == ESP_OK;
}
static void mg_esp32_802154_hw_abort(void) {
s_esp32_802154_retry = false;
esp_ieee802154_receive(); // Stops the current operation
}
// Resend a refused frame when its pause is over. If the radio does not take
// it, give up: the frame counts as done
static void mg_esp32_802154_hw_poll(void) {
uint32_t now;
if (!s_esp32_802154_retry) return;
now = (uint32_t) esp_timer_get_time();
if ((int32_t) (now - s_esp32_802154_retry_at) < 0) return;
s_esp32_802154_retry = false;
if (esp_ieee802154_transmit(s_esp32_802154_txframe, false) != ESP_OK) {
s_esp32_802154_tx_done = true;
}
}
// Mesh radio transmit. The hardware adds the FCS
static size_t mgi_esp32_802154_radio_tx(struct mg_tcpip_if *ifp,
const void *buf, size_t len) {
(void) ifp;
if (len > 125 || s_esp32_802154_txing) return 0;
s_esp32_802154_txframe[0] = (uint8_t) (len + 2);
memcpy(s_esp32_802154_txframe + 1, buf, len);
s_esp32_802154_tx_done = false;
s_esp32_802154_txing = true, s_esp32_802154_txstart = mg_micros();
if (!mg_esp32_802154_hw_tx()) {
s_esp32_802154_txing = false;
return 0;
}
return len;
}
static size_t mgi_esp32_802154_tx(const void *buf, size_t len,
struct mg_tcpip_if *ifp) {
struct mg_tcpip_driver_esp32_802154_data *data = ifp->driver_data;
return data->mesh->strategy->tx(data->mesh, buf, len);
}
static size_t mgi_esp32_802154_rx(void *buf, size_t cap,
struct mg_tcpip_if *ifp) {
struct mg_tcpip_driver_esp32_802154_data *data = ifp->driver_data;
size_t len = data->mesh->rx_len;
if (len > cap) return 0;
if (len != 0 && buf != ifp->recv_queue.buf)
memcpy(buf, ifp->recv_queue.buf, len);
data->mesh->rx_len = 0;
return len;
}
static bool mgi_esp32_802154_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_esp32_802154_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros();
(void) s1;
node->now = now;
mg_esp32_802154_hw_poll();
if (s_esp32_802154_txing &&
(s_esp32_802154_tx_done ||
now - s_esp32_802154_txstart > MG_MESH_TX_TIMEOUT_US)) {
if (!s_esp32_802154_tx_done) mg_esp32_802154_hw_abort();
s_esp32_802154_txing = false;
node->tx_at = now + node->strategy->radio->ifs_us;
}
mg_mesh_rx_drain(node, &s_esp32_802154_rx);
node->strategy->tick(node, now);
return node->joined;
}
static bool mgi_esp32_802154_init(struct mg_tcpip_if *ifp) {
struct mg_tcpip_driver_esp32_802154_data *data = ifp->driver_data;
const struct mg_mesh *strategy = data->strategy;
uint16_t address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = mg_calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) {
mg_free(data->mesh);
data->mesh = NULL;
return false;
}
data->mesh->strategy = strategy;
data->mesh->tx = mgi_esp32_802154_radio_tx;
mg_queue_init(&s_esp32_802154_rx, s_esp32_802154_rxbuf,
sizeof(s_esp32_802154_rxbuf));
s_esp32_802154_ifp = ifp;
if (mg_esp32_802154_hw_init(address)) return true;
MG_ERROR(("802.15.4 radio init failed"));
data->mesh->strategy->free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
}
struct mg_tcpip_driver mg_tcpip_driver_esp32_802154 = {
mgi_esp32_802154_init, mgi_esp32_802154_tx, mgi_esp32_802154_rx,
mgi_esp32_802154_poll};
#endif
+38
View File
@@ -0,0 +1,38 @@
// Copyright (c) 2026 Cesanta Software Limited
// All rights reserved
#pragma once
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ESP32_802154) && \
MG_ENABLE_DRIVER_ESP32_802154
// ESP32-C6 IEEE 802.15.4 radio, as an Ethernet interface through the mesh.
// Needs MG_ARCH_ESP32 and CONFIG_IEEE802154_ENABLED.
// See tutorials/esp32/minimal-802.15.4
struct mg_tcpip_driver_esp32_802154_data {
struct mg_mesh_node *mesh; // Driver-owned runtime state
const struct mg_mesh *strategy; // NULL selects mg_mesh_default
};
extern struct mg_tcpip_driver mg_tcpip_driver_esp32_802154;
#define MG_TCPIP_DRIVER_ESP32_802154_INIT(manager) \
do { \
static struct mg_tcpip_driver_esp32_802154_data driver_data_; \
static struct mg_tcpip_if mif_; \
MG_SET_MAC_ADDRESS(mif_.mac); \
mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \
mif_.mgr = (manager); \
mif_.driver = &mg_tcpip_driver_esp32_802154; \
mif_.driver_data = &driver_data_; \
mg_tcpip_init((manager), &mif_); \
MG_INFO(("Driver: esp32_802154, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0)
#ifndef MG_TCPIP_DRIVER_INIT
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ESP32_802154_INIT(x)
#endif
#endif
+6 -20
View File
@@ -51,7 +51,6 @@ struct mg_zll {
// PHY_CTRL[XCVSEQ]
enum { MGI_SEQ_IDLE = 0, MGI_SEQ_RX = 1, MGI_SEQ_TX = 2 };
#define MGI_SEND_TIMEOUT_MS 60
static struct mg_tcpip_if *s_mcxw71_ifp;
static struct mg_queue s_mcxw71_rx;
@@ -110,15 +109,7 @@ void RF_802_15_4_IRQHandler(void) {
n = n >= 2 ? n - 2 : 0;
if (n > 0 && n <= sizeof(frame)) {
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i];
{
char *dst;
if (mg_queue_book(&s_mcxw71_rx, &dst, n) >= n) {
memcpy(dst, frame, n);
mg_queue_add(&s_mcxw71_rx, n);
} else {
s_mcxw71_ifp->ndrop++;
}
}
mg_mesh_rx_queue(&s_mcxw71_rx, s_mcxw71_ifp, frame, n);
}
}
mg_mcxw71_listen();
@@ -179,11 +170,9 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1;
if (s_mcxw71_txing &&
mg_millis() > s_mcxw71_sendstart + MGI_SEND_TIMEOUT_MS) {
mg_millis() > s_mcxw71_sendstart + MG_MESH_TX_TIMEOUT_US / 1000) {
mg_mcxw71_lock();
if (s_mcxw71_txing) {
mg_mcxw71_abort();
@@ -199,10 +188,7 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
node->tx_at = now + node->strategy->radio->ifs_us;
}
node->now = now;
while (node->rx_len == 0 && (len = mg_queue_next(&s_mcxw71_rx, &buf)) != 0) {
node->strategy->rx(node, buf, len);
mg_queue_del(&s_mcxw71_rx, len);
}
mg_mesh_rx_drain(node, &s_mcxw71_rx);
node->strategy->tick(node, now);
return node->joined;
}
@@ -214,10 +200,10 @@ static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = calloc(1, sizeof(*data->mesh));
data->mesh = mg_calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) {
free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
}
@@ -226,7 +212,7 @@ static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_mcxw71_rx, s_mcxw71_queue, sizeof(s_mcxw71_queue));
if (mgi_mcxw71_radio_init(ifp)) return true;
strategy->free(data->mesh);
free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
}
+6 -18
View File
@@ -8,7 +8,6 @@
#include <rail_ieee802154.h>
// RAIL delivers raw frames and transmit completion from interrupt context.
#define MG_MG24_TX_TIMEOUT_US 60000
static struct mg_tcpip_if *s_mg24_ifp;
static RAIL_Handle_t s_mg24_rail;
@@ -23,7 +22,6 @@ static void mg_mg24_rx_frame(RAIL_Handle_t h) {
RAIL_RxPacketInfo_t info;
uint8_t f[128];
size_t n;
char *dst;
if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) ==
RAIL_RX_PACKET_HANDLE_INVALID ||
info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS ||
@@ -33,12 +31,7 @@ static void mg_mg24_rx_frame(RAIL_Handle_t h) {
RAIL_CopyRxPacket(f, &info);
n = info.packetBytes - 1U; // Strip PHR
if (n == f[0]) n -= 2; // FCS stored, strip it
if (mg_queue_book(&s_mg24_rx, &dst, n) >= n) {
memcpy(dst, f + 1, n);
mg_queue_add(&s_mg24_rx, n);
} else {
s_mg24_ifp->ndrop++;
}
mg_mesh_rx_queue(&s_mg24_rx, s_mg24_ifp, f + 1, n);
}
// RAIL event callback, runs in interrupt context
@@ -136,12 +129,10 @@ static bool mgi_mg24_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1;
node->now = now;
if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart >
MG_MG24_TX_TIMEOUT_US)) {
MG_MESH_TX_TIMEOUT_US)) {
if (!s_mg24_tx_done) {
RAIL_Idle(s_mg24_rail, RAIL_IDLE_ABORT, true);
RAIL_StartRx(s_mg24_rail, MG_802154_CHANNEL, NULL);
@@ -149,10 +140,7 @@ static bool mgi_mg24_poll(struct mg_tcpip_if *ifp, bool s1) {
s_mg24_txing = false;
node->tx_at = now + node->strategy->radio->ifs_us;
}
while (node->rx_len == 0 && (len = mg_queue_next(&s_mg24_rx, &buf)) != 0) {
node->strategy->rx(node, buf, len);
mg_queue_del(&s_mg24_rx, len);
}
mg_mesh_rx_drain(node, &s_mg24_rx);
node->strategy->tick(node, now);
return node->joined;
}
@@ -162,10 +150,10 @@ static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
uint16_t address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = calloc(1, sizeof(*data->mesh));
data->mesh = mg_calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) {
free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
}
@@ -174,7 +162,7 @@ static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_mg24_rx, s_mg24_rxbuf, sizeof(s_mg24_rxbuf));
if (mgi_mg24_radio_init(ifp)) return true;
data->mesh->strategy->free(data->mesh);
free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
}
+6 -17
View File
@@ -45,13 +45,7 @@ void RADIO_IRQHandler(void) {
mg_radio_802154.parse_hdr(&mg_radio_802154, frame, len, &dst, &src) !=
0 &&
(dst == 0xffffu || dst == node->radio_address)) {
char *dst;
if (mg_queue_book(&s_nrf52_rx, &dst, len) >= len) {
memcpy(dst, frame, len);
mg_queue_add(&s_nrf52_rx, len);
} else {
s_nrf52_ifp->ndrop++;
}
mg_mesh_rx_queue(&s_nrf52_rx, s_nrf52_ifp, frame, len);
}
}
mgi_nrf52_listen();
@@ -126,10 +120,8 @@ static bool mgi_nrf52_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_nrf52_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1;
if (s_nrf52_txing && mg_micros() - s_nrf52_started > 60000) {
if (s_nrf52_txing && mg_micros() - s_nrf52_started > MG_MESH_TX_TIMEOUT_US) {
NVIC_DisableIRQ(RADIO_IRQn);
mgi_nrf52_disable();
s_nrf52_txing = false;
@@ -142,10 +134,7 @@ static bool mgi_nrf52_poll(struct mg_tcpip_if *ifp, bool s1) {
node->tx_at = now + node->strategy->radio->ifs_us;
}
node->now = now;
while (node->rx_len == 0 && (len = mg_queue_next(&s_nrf52_rx, &buf)) != 0) {
node->strategy->rx(node, buf, len);
mg_queue_del(&s_nrf52_rx, len);
}
mg_mesh_rx_drain(node, &s_nrf52_rx);
node->strategy->tick(node, now);
return node->joined;
}
@@ -157,10 +146,10 @@ static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = calloc(1, sizeof(*data->mesh));
data->mesh = mg_calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) {
free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
}
@@ -169,7 +158,7 @@ static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_nrf52_rx, s_nrf52_queue, sizeof(s_nrf52_queue));
if (mgi_nrf52_radio_init(ifp)) return true;
strategy->free(data->mesh);
free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
}
+5 -18
View File
@@ -138,15 +138,7 @@ static void mg_rw612_control(uint8_t subtype, uint32_t a, uint32_t b,
}
static void mg_rw612_queue_rx(const uint8_t *frame, size_t len) {
{
char *dst;
if (mg_queue_book(&s_rw612w_rx, &dst, len) >= len) {
memcpy(dst, frame, len);
mg_queue_add(&s_rw612w_rx, len);
} else {
s_rw612.ifp->ndrop++;
}
}
mg_mesh_rx_queue(&s_rw612w_rx, s_rw612.ifp, frame, len);
}
// Spinel packed unsigned integer: 7 bits per byte, LSB first
@@ -457,8 +449,6 @@ static bool mgi_rw612w_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1;
bool was_sending = s_rw612.txing;
mg_rw612_poll_imu();
@@ -470,10 +460,7 @@ static bool mgi_rw612w_poll(struct mg_tcpip_if *ifp, bool s1) {
if (was_sending && !s_rw612.txing)
node->tx_at = now + node->strategy->radio->ifs_us;
node->now = now;
while (node->rx_len == 0 && (len = mg_queue_next(&s_rw612w_rx, &buf)) != 0) {
node->strategy->rx(node, buf, len);
mg_queue_del(&s_rw612w_rx, len);
}
mg_mesh_rx_drain(node, &s_rw612w_rx);
node->strategy->tick(node, now);
return node->joined;
}
@@ -491,10 +478,10 @@ static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = calloc(1, sizeof(*data->mesh));
data->mesh = mg_calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) {
free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
}
@@ -503,7 +490,7 @@ static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_rw612w_rx, s_rw612w_queue, sizeof(s_rw612w_queue));
if (mgi_rw612w_radio_init(ifp)) return true;
strategy->free(data->mesh);
free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
}