Compare commits

..
1 Commits
Author SHA1 Message Date
cpq 5aa320658a STM32F driver: per-driver INIT macro, like the radio drivers 2026-11-20 17:17:50 +00:00
47 changed files with 275 additions and 1001 deletions

No files matched your search

+96 -313
View File
@@ -6587,8 +6587,6 @@ static size_t mgi_mesh_b_queue(struct mg_mesh_node *node, struct mg_queue *q,
#define MGI_JOIN_JITTER_US 640 #define MGI_JOIN_JITTER_US 640
#define MGI_DATA_QUEUE_SIZE 2048 #define MGI_DATA_QUEUE_SIZE 2048
#define MGI_TX_RETRY_US 32 #define MGI_TX_RETRY_US 32
#define MGI_ANSWER_MARGIN_US 500 // Node: finish the first answer frame this
// much before the bridge stops waiting
// Our frames: the radio header, with the radio addresses, then the mesh // Our frames: the radio header, with the radio addresses, then the mesh
// header MG_MESH_HDR_LEN, then the payload // header MG_MESH_HDR_LEN, then the payload
#define MGI_JOIN_UP_LEN \ #define MGI_JOIN_UP_LEN \
@@ -6615,7 +6613,6 @@ struct mgi_mesh_b_data {
int8_t missed_rounds; // -1 after a grant; otherwise completed rounds missed int8_t missed_rounds; // -1 after a grant; otherwise completed rounds missed
uint16_t coordinator; // Coordinator radio address uint16_t coordinator; // Coordinator radio address
uint8_t phase; // MGI_TURN_IDLE, MGI_TURN_JOIN, or MGI_TURN_DATA uint8_t phase; // MGI_TURN_IDLE, MGI_TURN_JOIN, or MGI_TURN_DATA
uint64_t answer_by; // Node: first answer frame must end on air by then
}; };
struct mgi_mesh_b_peer { struct mgi_mesh_b_peer {
@@ -6728,24 +6725,11 @@ static void mgi_mesh_b_tx_step(struct mgi_mesh_b_data *data, uint64_t now) {
} else { } else {
mgi_mesh_b_frame_header(frame, dst, node->radio_address, command, 0); mgi_mesh_b_frame_header(frame, dst, node->radio_address, command, 0);
} }
// A node's first answer frame: if it cannot end on air before the bridge
// stops waiting, stay silent. The queued frame waits for the next turn
if (node->lan == NULL &&
(data->tx_offset == 0 || data->tx_offset == MGI_TX_DST_LEN) &&
now + mgi_mesh_b_airtime(len) > data->answer_by) {
data->tx_offset = 0;
data->tx_at = (uint64_t) -1;
return;
}
if (now < data->node->tx_at || data->node->tx(radio, frame, len) != len) { if (now < data->node->tx_at || data->node->tx(radio, frame, len) != len) {
data->tx_at = now + MGI_TX_RETRY_US; data->tx_at = now + MGI_TX_RETRY_US;
return; return;
} }
if (data->tx_offset == 0 || (data->tx_offset += len + 1) == record_len) { if (data->tx_offset == 0 || (data->tx_offset += len + 1) == record_len) {
// Bridge: the turn's last frame is out, wait for the answer
if (node->lan != NULL)
data->deadline =
now + mgi_mesh_b_airtime(len) + MG_MESH_ANSWER_TIMEOUT_US;
mgi_mesh_b_tx_finish(data); mgi_mesh_b_tx_finish(data);
} else { } else {
data->tx_at = now + mgi_mesh_b_airtime(len) + s_mesh_default_radio->ifs_us; data->tx_at = now + mgi_mesh_b_airtime(len) + s_mesh_default_radio->ifs_us;
@@ -6755,7 +6739,6 @@ static void mgi_mesh_b_tx_step(struct mgi_mesh_b_data *data, uint64_t now) {
static void mgi_mesh_b_tx_begin(struct mgi_mesh_b_data *data, bool burst, static void mgi_mesh_b_tx_begin(struct mgi_mesh_b_data *data, bool burst,
uint64_t now, uint64_t deadline) { uint64_t now, uint64_t deadline) {
data->phase = MGI_TURN_DATA; data->phase = MGI_TURN_DATA;
data->answer_by = (uint64_t) -1; // Node sets it after a grant
data->tx_offset = burst ? MGI_TX_DST_LEN : 0; data->tx_offset = burst ? MGI_TX_DST_LEN : 0;
data->tx_at = now; data->tx_at = now;
data->deadline = deadline; data->deadline = deadline;
@@ -6786,11 +6769,6 @@ static int mgi_mesh_b_data_receive(struct mgi_mesh_b_data *data,
memcpy(data->rx_buf + offset, frame + MG_MESH_FRAME_HDR_LEN, chunk); memcpy(data->rx_buf + offset, frame + MG_MESH_FRAME_HDR_LEN, chunk);
if (index < last) { if (index < last) {
data->rx_expected = (uint8_t) (burst + 0x10); data->rx_expected = (uint8_t) (burst + 0x10);
// Bridge: an answer is under way, wait for its next fragment
if (data->node->lan != NULL)
data->deadline =
now + mgi_mesh_b_airtime(s_mesh_default_radio->max_frame_size) +
s_mesh_default_radio->ifs_us + MG_MESH_ANSWER_TIMEOUT_US;
return -1; return -1;
} }
data->rx_expected = 0xff; data->rx_expected = 0xff;
@@ -6800,6 +6778,7 @@ static int mgi_mesh_b_data_receive(struct mgi_mesh_b_data *data,
// Bridge scheduling and Ethernet forwarding. // Bridge scheduling and Ethernet forwarding.
#define MGI_MAX_MISSES 3 // Turns in a row without an answer: remove the member
static uint16_t mgi_mesh_b_join_field(const uint8_t *p) { static uint16_t mgi_mesh_b_join_field(const uint8_t *p) {
return (uint16_t) ((unsigned) p[0] * 256 + p[1]); return (uint16_t) ((unsigned) p[0] * 256 + p[1]);
@@ -6900,13 +6879,13 @@ static void mgi_mesh_b_bridge_rx(uint64_t now, struct mg_mesh_node *b,
memcpy(peer->mac, frame + MG_MESH_FRAME_HDR_LEN, sizeof(peer->mac)); memcpy(peer->mac, frame + MG_MESH_FRAME_HDR_LEN, sizeof(peer->mac));
} }
// Member peer did not answer its turn. After MG_MESH_MAX_MISSES in a row, it // Member peer did not answer its turn. After MGI_MAX_MISSES in a row, it is
// is gone: remove it, and its queued downlink frames // gone: remove it, and its queued downlink frames
static void mgi_mesh_b_member_missed(struct mgi_mesh_b_data *data, static void mgi_mesh_b_member_missed(struct mgi_mesh_b_data *data,
struct mgi_mesh_b_peer *peer) { struct mgi_mesh_b_peer *peer) {
if (++peer->misses < MG_MESH_MAX_MISSES) return; if (++peer->misses < MGI_MAX_MISSES) return;
MG_INFO(("Member %M removed: no answer %d times", mg_print_mac, peer->mac, MG_INFO(("Member %M removed: no answer %d times", mg_print_mac, peer->mac,
MG_MESH_MAX_MISSES)); MGI_MAX_MISSES));
if (data->fanout == peer) { if (data->fanout == peer) {
data->fanout = peer->next; data->fanout = peer->next;
if (data->fanout == NULL) { if (data->fanout == NULL) {
@@ -6999,7 +6978,7 @@ static void mgi_mesh_b_node_rx(struct mg_mesh_node *n, uint64_t now,
if (command == MGI_JOIN_DOWN && len == MG_MESH_FRAME_HDR_LEN && if (command == MGI_JOIN_DOWN && len == MG_MESH_FRAME_HDR_LEN &&
dst == 0xffffu) { dst == 0xffffu) {
if (n->joined && src == data->coordinator && if (n->joined && src == data->coordinator &&
++data->missed_rounds >= MG_MESH_MAX_MISSES) { ++data->missed_rounds >= MGI_MAX_MISSES) {
n->joined = false; n->joined = false;
data->phase = MGI_TURN_IDLE; data->phase = MGI_TURN_IDLE;
data->tx_at = (uint64_t) -1; data->tx_at = (uint64_t) -1;
@@ -7034,22 +7013,17 @@ static void mgi_mesh_b_node_rx(struct mg_mesh_node *n, uint64_t now,
now + mgi_mesh_b_burst_time(MGI_ETH_MAX) + now + mgi_mesh_b_burst_time(MGI_ETH_MAX) +
2 * s_mesh_default_radio->ifs_us + 2 * s_mesh_default_radio->ifs_us +
MGI_JOIN_TURN_US); MGI_JOIN_TURN_US);
data->answer_by = now + MG_MESH_ANSWER_TIMEOUT_US - MGI_ANSWER_MARGIN_US;
} }
} }
// Receive timing counts from the frame's arrival, see mg_mesh_rx_drain().
// Without an arrival time, e.g. when called directly, use the poll time
static void mgi_mesh_b_input(struct mg_mesh_node *node, void *buf, size_t len) { static void mgi_mesh_b_input(struct mg_mesh_node *node, void *buf, size_t len) {
uint64_t ready = node->now + node->strategy->radio->turnaround_us; uint64_t ready = node->now + node->strategy->radio->turnaround_us;
uint64_t arrived = node->rx_at != 0 ? node->rx_at : node->now;
if (node->tx_at < ready) node->tx_at = ready; if (node->tx_at < ready) node->tx_at = ready;
node->next_tick = node->now; node->next_tick = node->now;
if (node->lan != NULL) { if (node->lan != NULL)
mgi_mesh_b_bridge_rx(arrived, node, (const uint8_t *) buf, len); mgi_mesh_b_bridge_rx(node->now, node, (const uint8_t *) buf, len);
} else { else
mgi_mesh_b_node_rx(node, arrived, (uint8_t *) buf, len); mgi_mesh_b_node_rx(node, node->now, (uint8_t *) buf, len);
}
} }
static void mgi_mesh_b_tick(struct mg_mesh_node *node, uint64_t now) { static void mgi_mesh_b_tick(struct mg_mesh_node *node, uint64_t now) {
@@ -7106,41 +7080,6 @@ static void mgi_mesh_b_free(struct mg_mesh_node *node) {
node->data = NULL; node->data = NULL;
} }
// Receive queue record: arrival time, then the radio frame
#define MGI_RX_STAMP_LEN sizeof(uint64_t)
void mg_mesh_rx_queue(struct mg_queue *q, struct mg_tcpip_if *ifp,
const void *frame, size_t len) {
uint64_t arrived = mg_micros();
size_t need = MGI_RX_STAMP_LEN + len;
char *dst;
if (mg_queue_book(q, &dst, need) < need) {
if (ifp != NULL) ifp->ndrop++;
return;
}
memcpy(dst, &arrived, MGI_RX_STAMP_LEN);
memcpy(dst + MGI_RX_STAMP_LEN, frame, len);
mg_queue_add(q, need);
}
void mg_mesh_rx_drain(struct mg_mesh_node *node, struct mg_queue *q) {
char *record;
size_t len;
while (node->rx_len == 0) {
len = mg_queue_next(q, &record);
if (len == 0) break;
if (len > MGI_RX_STAMP_LEN) {
memcpy(&node->rx_at, record, MGI_RX_STAMP_LEN);
node->strategy->rx(node, record + MGI_RX_STAMP_LEN,
len - MGI_RX_STAMP_LEN);
node->rx_at = 0;
}
mg_queue_del(q, len);
}
}
const struct mg_mesh mg_mesh_default = { const struct mg_mesh mg_mesh_default = {
"default", "default",
mgi_mesh_b_init, mgi_mesh_b_init,
@@ -30526,38 +30465,33 @@ bool mg_path_is_sane(const struct mg_str path) {
return true; return true;
} }
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one #if MG_ENABLE_CUSTOM_MILLIS
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS #else
#if MG_ENABLE_CUSTOM_MICROS == 0 uint64_t mg_millis(void) {
uint64_t mg_micros(void) { #if MG_ARCH == MG_ARCH_ESP32 || MG_ENABLE_FREERTOS
#if MG_ARCH == MG_ARCH_ESP32 return xTaskGetTickCount() * portTICK_PERIOD_MS;
return (uint64_t) esp_timer_get_time();
#elif MG_ENABLE_FREERTOS
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
#elif MG_ARCH == MG_ARCH_THREADX #elif MG_ARCH == MG_ARCH_THREADX
return (uint64_t) tx_time_get() * return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND);
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_TIRTOS #elif MG_ARCH == MG_ARCH_TIRTOS
return (uint64_t) Clock_getTicks() * 1000; return (uint64_t) Clock_getTicks();
#elif MG_ARCH == MG_ARCH_ZEPHYR #elif MG_ARCH == MG_ARCH_ZEPHYR
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks()); return (uint64_t) k_uptime_get();
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1 #elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
return (uint64_t) rt_time_get() * 1000; return (uint64_t) rt_time_get();
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2 #elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) * return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq());
1000;
#elif MG_ARCH == MG_ARCH_RTTHREAD #elif MG_ARCH == MG_ARCH_RTTHREAD
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000; return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND);
#elif MG_ARCH == MG_ARCH_WIN32 #elif MG_ARCH == MG_ARCH_WIN32
return (uint64_t) GetTickCount() * 1000; return GetTickCount();
#elif MG_ARCH == MG_ARCH_PICOSDK #elif MG_ARCH == MG_ARCH_PICOSDK
return time_us_64(); return time_us_64() / 1000;
#elif MG_ARCH == MG_ARCH_CUBE #elif MG_ARCH == MG_ARCH_CUBE
return (uint64_t) HAL_GetTick() * 1000; return (uint64_t) HAL_GetTick();
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__) #elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux // Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux // Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000; return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000;
#elif MG_ARCH == MG_ARCH_UNIX #elif MG_ARCH == MG_ARCH_UNIX
struct timespec ts = {0, 0}; struct timespec ts = {0, 0};
// See #1615 - prefer monotonic clock // See #1615 - prefer monotonic clock
@@ -30572,21 +30506,15 @@ uint64_t mg_micros(void) {
// adjustments performed by adjtime and NTP // adjustments performed by adjtime and NTP
clock_gettime(CLOCK_REALTIME, &ts); clock_gettime(CLOCK_REALTIME, &ts);
#endif #endif
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000); return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000);
#elif defined(ARDUINO) #elif defined(ARDUINO)
return (uint64_t) micros(); return (uint64_t) millis();
#else #else
return (uint64_t) time(NULL) * 1000000; return (uint64_t) (time(NULL) * 1000);
#endif #endif
} }
#endif #endif
#if MG_ENABLE_CUSTOM_MILLIS == 0
uint64_t mg_millis(void) {
return mg_micros() / 1000;
}
#endif
// network format equates big endian order // network format equates big endian order
uint16_t mg_ntohs(uint16_t net) { uint16_t mg_ntohs(uint16_t net) {
return MG_LOAD_BE16(&net); return MG_LOAD_BE16(&net);
@@ -32685,199 +32613,6 @@ bool mg_wifi_ap_stop(void) {
#endif #endif
#ifdef MG_ENABLE_LINES
#line 1 "src/drivers/esp32_802154.c"
#endif
#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>
#define MG_ESP32_802154_TX_TIMEOUT_US 60000
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);
}
// A transmission refused by Wi-Fi coexistence, a busy channel or an abort is
// sent again after this pause, until MG_ESP32_802154_TX_TIMEOUT_US. Without
// it, the mesh would count the refused frame as sent
#define MG_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
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() + MG_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_ESP32_802154_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
#ifdef MG_ENABLE_LINES #ifdef MG_ENABLE_LINES
#line 1 "src/drivers/imxrt.c" #line 1 "src/drivers/imxrt.c"
#endif #endif
@@ -33220,7 +32955,15 @@ void RF_802_15_4_IRQHandler(void) {
n = n >= 2 ? n - 2 : 0; n = n >= 2 ? n - 2 : 0;
if (n > 0 && n <= sizeof(frame)) { if (n > 0 && n <= sizeof(frame)) {
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i]; for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i];
mg_mesh_rx_queue(&s_mcxw71_rx, s_mcxw71_ifp, frame, n); {
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_mcxw71_listen(); mg_mcxw71_listen();
@@ -33281,6 +33024,8 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data; struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh; struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros(); uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1; (void) s1;
if (s_mcxw71_txing && if (s_mcxw71_txing &&
mg_millis() > s_mcxw71_sendstart + MGI_SEND_TIMEOUT_MS) { mg_millis() > s_mcxw71_sendstart + MGI_SEND_TIMEOUT_MS) {
@@ -33299,7 +33044,10 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
node->tx_at = now + node->strategy->radio->ifs_us; node->tx_at = now + node->strategy->radio->ifs_us;
} }
node->now = now; node->now = now;
mg_mesh_rx_drain(node, &s_mcxw71_rx); 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);
}
node->strategy->tick(node, now); node->strategy->tick(node, now);
return node->joined; return node->joined;
} }
@@ -33311,10 +33059,10 @@ static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
address = address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u); (uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default; if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = mg_calloc(1, sizeof(*data->mesh)); data->mesh = calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false; if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) { if (!strategy->init(data->mesh, ifp, NULL, address)) {
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -33323,7 +33071,7 @@ static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_mcxw71_rx, s_mcxw71_queue, sizeof(s_mcxw71_queue)); mg_queue_init(&s_mcxw71_rx, s_mcxw71_queue, sizeof(s_mcxw71_queue));
if (mgi_mcxw71_radio_init(ifp)) return true; if (mgi_mcxw71_radio_init(ifp)) return true;
strategy->free(data->mesh); strategy->free(data->mesh);
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -33360,6 +33108,7 @@ static void mg_mg24_rx_frame(RAIL_Handle_t h) {
RAIL_RxPacketInfo_t info; RAIL_RxPacketInfo_t info;
uint8_t f[128]; uint8_t f[128];
size_t n; size_t n;
char *dst;
if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) == if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) ==
RAIL_RX_PACKET_HANDLE_INVALID || RAIL_RX_PACKET_HANDLE_INVALID ||
info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS || info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS ||
@@ -33369,7 +33118,12 @@ static void mg_mg24_rx_frame(RAIL_Handle_t h) {
RAIL_CopyRxPacket(f, &info); RAIL_CopyRxPacket(f, &info);
n = info.packetBytes - 1U; // Strip PHR n = info.packetBytes - 1U; // Strip PHR
if (n == f[0]) n -= 2; // FCS stored, strip it if (n == f[0]) n -= 2; // FCS stored, strip it
mg_mesh_rx_queue(&s_mg24_rx, s_mg24_ifp, f + 1, n); 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++;
}
} }
// RAIL event callback, runs in interrupt context // RAIL event callback, runs in interrupt context
@@ -33467,6 +33221,8 @@ static bool mgi_mg24_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data; struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh; struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros(); uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1; (void) s1;
node->now = now; node->now = now;
if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart > if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart >
@@ -33478,7 +33234,10 @@ static bool mgi_mg24_poll(struct mg_tcpip_if *ifp, bool s1) {
s_mg24_txing = false; s_mg24_txing = false;
node->tx_at = now + node->strategy->radio->ifs_us; node->tx_at = now + node->strategy->radio->ifs_us;
} }
mg_mesh_rx_drain(node, &s_mg24_rx); 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);
}
node->strategy->tick(node, now); node->strategy->tick(node, now);
return node->joined; return node->joined;
} }
@@ -33488,10 +33247,10 @@ static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
uint16_t address = uint16_t address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u); (uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default; if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = mg_calloc(1, sizeof(*data->mesh)); data->mesh = calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false; if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) { if (!strategy->init(data->mesh, ifp, NULL, address)) {
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -33500,7 +33259,7 @@ static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_mg24_rx, s_mg24_rxbuf, sizeof(s_mg24_rxbuf)); mg_queue_init(&s_mg24_rx, s_mg24_rxbuf, sizeof(s_mg24_rxbuf));
if (mgi_mg24_radio_init(ifp)) return true; if (mgi_mg24_radio_init(ifp)) return true;
data->mesh->strategy->free(data->mesh); data->mesh->strategy->free(data->mesh);
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -34030,7 +33789,13 @@ void RADIO_IRQHandler(void) {
mg_radio_802154.parse_hdr(&mg_radio_802154, frame, len, &dst, &src) != mg_radio_802154.parse_hdr(&mg_radio_802154, frame, len, &dst, &src) !=
0 && 0 &&
(dst == 0xffffu || dst == node->radio_address)) { (dst == 0xffffu || dst == node->radio_address)) {
mg_mesh_rx_queue(&s_nrf52_rx, s_nrf52_ifp, frame, len); 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++;
}
} }
} }
mgi_nrf52_listen(); mgi_nrf52_listen();
@@ -34105,6 +33870,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_tcpip_driver_nrf52_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh; struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros(); uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1; (void) s1;
if (s_nrf52_txing && mg_micros() - s_nrf52_started > 60000) { if (s_nrf52_txing && mg_micros() - s_nrf52_started > 60000) {
NVIC_DisableIRQ(RADIO_IRQn); NVIC_DisableIRQ(RADIO_IRQn);
@@ -34119,7 +33886,10 @@ static bool mgi_nrf52_poll(struct mg_tcpip_if *ifp, bool s1) {
node->tx_at = now + node->strategy->radio->ifs_us; node->tx_at = now + node->strategy->radio->ifs_us;
} }
node->now = now; node->now = now;
mg_mesh_rx_drain(node, &s_nrf52_rx); 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);
}
node->strategy->tick(node, now); node->strategy->tick(node, now);
return node->joined; return node->joined;
} }
@@ -34131,10 +33901,10 @@ static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
address = address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u); (uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default; if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = mg_calloc(1, sizeof(*data->mesh)); data->mesh = calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false; if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) { if (!strategy->init(data->mesh, ifp, NULL, address)) {
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -34143,7 +33913,7 @@ static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_nrf52_rx, s_nrf52_queue, sizeof(s_nrf52_queue)); mg_queue_init(&s_nrf52_rx, s_nrf52_queue, sizeof(s_nrf52_queue));
if (mgi_nrf52_radio_init(ifp)) return true; if (mgi_nrf52_radio_init(ifp)) return true;
strategy->free(data->mesh); strategy->free(data->mesh);
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -35254,7 +35024,15 @@ 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) { static void mg_rw612_queue_rx(const uint8_t *frame, size_t len) {
mg_mesh_rx_queue(&s_rw612w_rx, s_rw612.ifp, frame, 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++;
}
}
} }
// Spinel packed unsigned integer: 7 bits per byte, LSB first // Spinel packed unsigned integer: 7 bits per byte, LSB first
@@ -35565,6 +35343,8 @@ static bool mgi_rw612w_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data; struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh; struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros(); uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1; (void) s1;
bool was_sending = s_rw612.txing; bool was_sending = s_rw612.txing;
mg_rw612_poll_imu(); mg_rw612_poll_imu();
@@ -35576,7 +35356,10 @@ static bool mgi_rw612w_poll(struct mg_tcpip_if *ifp, bool s1) {
if (was_sending && !s_rw612.txing) if (was_sending && !s_rw612.txing)
node->tx_at = now + node->strategy->radio->ifs_us; node->tx_at = now + node->strategy->radio->ifs_us;
node->now = now; node->now = now;
mg_mesh_rx_drain(node, &s_rw612w_rx); 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);
}
node->strategy->tick(node, now); node->strategy->tick(node, now);
return node->joined; return node->joined;
} }
@@ -35594,10 +35377,10 @@ static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
address = address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u); (uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default; if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = mg_calloc(1, sizeof(*data->mesh)); data->mesh = calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false; if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) { if (!strategy->init(data->mesh, ifp, NULL, address)) {
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -35606,7 +35389,7 @@ static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_rw612w_rx, s_rw612w_queue, sizeof(s_rw612w_queue)); mg_queue_init(&s_rw612w_rx, s_rw612w_queue, sizeof(s_rw612w_queue));
if (mgi_rw612w_radio_init(ifp)) return true; if (mgi_rw612w_radio_init(ifp)) return true;
strategy->free(data->mesh); strategy->free(data->mesh);
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
+29 -103
View File
@@ -113,9 +113,7 @@ extern "C" {
#if MG_ARCH == MG_ARCH_ARMGCC #if MG_ARCH == MG_ARCH_ARMGCC
#ifndef _POSIX_TIMERS // Some newlib builds, e.g. Espressif's, define it
#define _POSIX_TIMERS #define _POSIX_TIMERS
#endif
#include <ctype.h> #include <ctype.h>
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP #if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
@@ -191,18 +189,16 @@ extern "C" {
#define MG_TLS MG_TLS_BUILTIN #define MG_TLS MG_TLS_BUILTIN
#endif #endif
#ifndef MG_OTA // Pick OTA for the STM32 family, unless set by the user #if !defined(MG_OTA) && defined(STM32F1) || defined(STM32F2) || \
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || \ defined(STM32F4) || defined(STM32F7)
defined(STM32F7)
#define MG_OTA MG_OTA_STM32F #define MG_OTA MG_OTA_STM32F
#elif defined(STM32H5) #elif !defined(MG_OTA) && defined(STM32H5)
#define MG_OTA MG_OTA_STM32H5 #define MG_OTA MG_OTA_STM32H5
#elif defined(STM32H7) #elif !defined(MG_OTA) && defined(STM32H7)
#define MG_OTA MG_OTA_STM32H7 #define MG_OTA MG_OTA_STM32H7
#elif defined(STM32C5) #elif !defined(MG_OTA) && defined(STM32C5)
#define MG_OTA MG_OTA_STM32C5 #define MG_OTA MG_OTA_STM32C5
#endif #endif
#endif
// use HAL-defined execute-in-ram section // use HAL-defined execute-in-ram section
#ifndef MG_IRAM #ifndef MG_IRAM
@@ -1228,10 +1224,6 @@ struct timeval {
#define MG_ENABLE_CUSTOM_MILLIS 0 #define MG_ENABLE_CUSTOM_MILLIS 0
#endif #endif
#ifndef MG_ENABLE_CUSTOM_MICROS
#define MG_ENABLE_CUSTOM_MICROS 0
#endif
#ifndef MG_ENABLE_ASSERT #ifndef MG_ENABLE_ASSERT
#define MG_ENABLE_ASSERT 0 #define MG_ENABLE_ASSERT 0
#endif #endif
@@ -3438,13 +3430,11 @@ size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt,
// Return microseconds since boot. Never goes backwards. // Return milliseconds since boot (uptime). Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1 // On RTOS targets (FreeRTOS, ThreadX, Zephyr, etc.) uses the RTOS tick count.
uint64_t mg_micros(void); // On STM32/Cube uses HAL_GetTick(). On Arduino uses millis().
// On Linux/macOS uses CLOCK_MONOTONIC_RAW or CLOCK_MONOTONIC; falls back to
// Return milliseconds since boot. Never goes backwards. Calls mg_micros(). // CLOCK_REALTIME if neither is available.
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void); uint64_t mg_millis(void);
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync; // Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
@@ -4852,7 +4842,6 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac);
// Built-in hardware drivers. Assign one to mg_tcpip_if.driver before calling // Built-in hardware drivers. Assign one to mg_tcpip_if.driver before calling
// mg_tcpip_init(). Each driver also requires a corresponding driver_data // mg_tcpip_init(). Each driver also requires a corresponding driver_data
// struct; see the matching driver header for details. // struct; see the matching driver header for details.
extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
extern struct mg_tcpip_driver mg_tcpip_driver_w5500; extern struct mg_tcpip_driver mg_tcpip_driver_w5500;
extern struct mg_tcpip_driver mg_tcpip_driver_w5100; extern struct mg_tcpip_driver mg_tcpip_driver_w5100;
extern struct mg_tcpip_driver mg_tcpip_driver_tm4c; extern struct mg_tcpip_driver mg_tcpip_driver_tm4c;
@@ -4944,6 +4933,9 @@ struct mg_tcpip_spi {
#if MG_ENABLE_TCPIP #if MG_ENABLE_TCPIP
// Monotonic time in microseconds.
uint64_t mg_micros(void);
// A radio standard // A radio standard
struct mg_radio { struct mg_radio {
const char *name; // For logs const char *name; // For logs
@@ -4973,21 +4965,6 @@ struct mg_radio {
#define MG_802154_CHANNEL 15 #define MG_802154_CHANNEL 15
#endif #endif
// A turn's answer must arrive this soon after the turn's last frame ends on
// air, or the turn ends. A node that would answer later stays silent and keeps
// its frame for the next turn. Covers the node's poll latency, the radio
// turnaround and one full fragment's airtime, 4.3 ms on 802.15.4
#ifndef MG_MESH_ANSWER_TIMEOUT_US
#define MG_MESH_ANSWER_TIMEOUT_US 8000
#endif
// Misses in a row before giving up, 1 to 127. The bridge removes a member
// that did not answer this many turns. A node leaves the mesh after this
// many rounds without a turn
#ifndef MG_MESH_MAX_MISSES
#define MG_MESH_MAX_MISSES 10
#endif
extern const struct mg_radio mg_radio_802154; extern const struct mg_radio mg_radio_802154;
struct mg_mesh_node { struct mg_mesh_node {
@@ -4997,7 +4974,6 @@ struct mg_mesh_node {
size_t (*tx)(struct mg_tcpip_if *, const void *, size_t); // Radio frame TX size_t (*tx)(struct mg_tcpip_if *, const void *, size_t); // Radio frame TX
size_t rx_len; // Complete Ethernet frame in the interface RX buffer size_t rx_len; // Complete Ethernet frame in the interface RX buffer
uint64_t now; // Current driver poll time, us uint64_t now; // Current driver poll time, us
uint64_t rx_at; // Arrival time of the radio frame being handled, us
uint64_t tx_at; // Earliest next transmission, us uint64_t tx_at; // Earliest next transmission, us
void *data; // Private state owned by the strategy void *data; // Private state owned by the strategy
size_t ram_used; // Live mesh state bytes size_t ram_used; // Live mesh state bytes
@@ -5022,18 +4998,6 @@ struct mg_mesh {
extern const struct mg_mesh mg_mesh_default; extern const struct mg_mesh mg_mesh_default;
// Put a received radio frame into the driver's receive queue, together with
// its arrival time from mg_micros(). Call it when the frame arrives, from the
// radio interrupt if the driver has one. If the queue is full, the frame is
// dropped and counted in ifp->ndrop
void mg_mesh_rx_queue(struct mg_queue *q, struct mg_tcpip_if *ifp,
const void *frame, size_t len);
// Pass queued radio frames to the mesh, oldest first. Before each frame,
// node->rx_at is set to its arrival time. Stops while the node holds a
// received Ethernet frame the TCP/IP stack has not taken yet
void mg_mesh_rx_drain(struct mg_mesh_node *node, struct mg_queue *q);
#endif #endif
@@ -5341,46 +5305,6 @@ struct mg_tcpip_driver_cyw_data {
} while (0) } while (0)
#endif #endif
// Copyright (c) 2026 Cesanta Software Limited
// All rights reserved
#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: it uses ESP-IDF's
// esp_ieee802154 driver, and defines its esp_ieee802154_receive_done(),
// esp_ieee802154_transmit_done() and esp_ieee802154_transmit_failed()
// callbacks. 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
#if MG_ENABLE_TCPIP && \ #if MG_ENABLE_TCPIP && \
@@ -5445,7 +5369,7 @@ struct mg_tcpip_driver_mcxw71_data {
extern struct mg_tcpip_driver mg_tcpip_driver_mcxw71; extern struct mg_tcpip_driver mg_tcpip_driver_mcxw71;
#define MG_TCPIP_DRIVER_MCXW71_INIT(manager) \ #define MG_TCPIP_DRIVER_MCXW71_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_mcxw71_data driver_data_; \ static struct mg_tcpip_driver_mcxw71_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -5453,10 +5377,9 @@ extern struct mg_tcpip_driver mg_tcpip_driver_mcxw71;
mif_.ip = MG_TCPIP_IP; \ mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \ mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \ mif_.gw = MG_TCPIP_GW; \
mif_.mgr = (manager); \
mif_.driver = &mg_tcpip_driver_mcxw71; \ mif_.driver = &mg_tcpip_driver_mcxw71; \
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
mg_tcpip_init((manager), &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: mcxw71, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: mcxw71, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
@@ -5479,7 +5402,7 @@ struct mg_tcpip_driver_mg24_data {
extern struct mg_tcpip_driver mg_tcpip_driver_mg24; extern struct mg_tcpip_driver mg_tcpip_driver_mg24;
#define MG_TCPIP_DRIVER_MG24_INIT(manager) \ #define MG_TCPIP_DRIVER_MG24_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_mg24_data driver_data_; \ static struct mg_tcpip_driver_mg24_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -5487,10 +5410,9 @@ extern struct mg_tcpip_driver mg_tcpip_driver_mg24;
mif_.ip = MG_TCPIP_IP; \ mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \ mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \ mif_.gw = MG_TCPIP_GW; \
mif_.mgr = (manager); \
mif_.driver = &mg_tcpip_driver_mg24; \ mif_.driver = &mg_tcpip_driver_mg24; \
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
mg_tcpip_init((manager), &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: mg24, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: mg24, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
@@ -5562,7 +5484,7 @@ struct mg_tcpip_driver_nrf52_data {
extern struct mg_tcpip_driver mg_tcpip_driver_nrf52; extern struct mg_tcpip_driver mg_tcpip_driver_nrf52;
#define MG_TCPIP_DRIVER_NRF52_INIT(manager) \ #define MG_TCPIP_DRIVER_NRF52_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_nrf52_data driver_data_; \ static struct mg_tcpip_driver_nrf52_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -5570,10 +5492,9 @@ extern struct mg_tcpip_driver mg_tcpip_driver_nrf52;
mif_.ip = MG_TCPIP_IP; \ mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \ mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \ mif_.gw = MG_TCPIP_GW; \
mif_.mgr = (manager); \
mif_.driver = &mg_tcpip_driver_nrf52; \ mif_.driver = &mg_tcpip_driver_nrf52; \
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
mg_tcpip_init((manager), &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: nrf52, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: nrf52, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
@@ -5768,7 +5689,7 @@ struct mg_tcpip_driver_rw612w_data {
extern struct mg_tcpip_driver mg_tcpip_driver_rw612w; extern struct mg_tcpip_driver mg_tcpip_driver_rw612w;
#define MG_TCPIP_DRIVER_RW612W_INIT(manager) \ #define MG_TCPIP_DRIVER_RW612W_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_rw612w_data driver_data_; \ static struct mg_tcpip_driver_rw612w_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -5776,10 +5697,9 @@ extern struct mg_tcpip_driver mg_tcpip_driver_rw612w;
mif_.ip = MG_TCPIP_IP; \ mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \ mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \ mif_.gw = MG_TCPIP_GW; \
mif_.mgr = (manager); \
mif_.driver = &mg_tcpip_driver_rw612w; \ mif_.driver = &mg_tcpip_driver_rw612w; \
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
mg_tcpip_init((manager), &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: rw612w, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: rw612w, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
@@ -5927,7 +5847,9 @@ struct mg_tcpip_driver_stm32f_data {
#define MG_ENABLE_ETH_IRQ() #define MG_ENABLE_ETH_IRQ()
#endif #endif
#define MG_TCPIP_DRIVER_INIT(mgr) \ extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
#define MG_TCPIP_DRIVER_STM32F_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_stm32f_data driver_data_; \ static struct mg_tcpip_driver_stm32f_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -5940,10 +5862,14 @@ struct mg_tcpip_driver_stm32f_data {
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
MG_SET_MAC_ADDRESS(mif_.mac); \ MG_SET_MAC_ADDRESS(mif_.mac); \
MG_ENABLE_ETH_IRQ(); \ MG_ENABLE_ETH_IRQ(); \
mg_tcpip_init(mgr, &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
#ifndef MG_TCPIP_DRIVER_INIT
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_STM32F_INIT(x)
#endif
#endif #endif
-2
View File
@@ -1,9 +1,7 @@
#pragma once #pragma once
#if MG_ARCH == MG_ARCH_ARMGCC #if MG_ARCH == MG_ARCH_ARMGCC
#ifndef _POSIX_TIMERS // Some newlib builds, e.g. Espressif's, define it
#define _POSIX_TIMERS #define _POSIX_TIMERS
#endif
#include <ctype.h> #include <ctype.h>
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP #if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
+5 -7
View File
@@ -52,18 +52,16 @@
#define MG_TLS MG_TLS_BUILTIN #define MG_TLS MG_TLS_BUILTIN
#endif #endif
#ifndef MG_OTA // Pick OTA for the STM32 family, unless set by the user #if !defined(MG_OTA) && defined(STM32F1) || defined(STM32F2) || \
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || \ defined(STM32F4) || defined(STM32F7)
defined(STM32F7)
#define MG_OTA MG_OTA_STM32F #define MG_OTA MG_OTA_STM32F
#elif defined(STM32H5) #elif !defined(MG_OTA) && defined(STM32H5)
#define MG_OTA MG_OTA_STM32H5 #define MG_OTA MG_OTA_STM32H5
#elif defined(STM32H7) #elif !defined(MG_OTA) && defined(STM32H7)
#define MG_OTA MG_OTA_STM32H7 #define MG_OTA MG_OTA_STM32H7
#elif defined(STM32C5) #elif !defined(MG_OTA) && defined(STM32C5)
#define MG_OTA MG_OTA_STM32C5 #define MG_OTA MG_OTA_STM32C5
#endif #endif
#endif
// use HAL-defined execute-in-ram section // use HAL-defined execute-in-ram section
#ifndef MG_IRAM #ifndef MG_IRAM
-4
View File
@@ -94,10 +94,6 @@
#define MG_ENABLE_CUSTOM_MILLIS 0 #define MG_ENABLE_CUSTOM_MILLIS 0
#endif #endif
#ifndef MG_ENABLE_CUSTOM_MICROS
#define MG_ENABLE_CUSTOM_MICROS 0
#endif
#ifndef MG_ENABLE_ASSERT #ifndef MG_ENABLE_ASSERT
#define MG_ENABLE_ASSERT 0 #define MG_ENABLE_ASSERT 0
#endif #endif
-189
View File
@@ -1,189 +0,0 @@
#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>
#define MG_ESP32_802154_TX_TIMEOUT_US 60000
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);
}
// A transmission refused by Wi-Fi coexistence, a busy channel or an abort is
// sent again after this pause, until MG_ESP32_802154_TX_TIMEOUT_US. Without
// it, the mesh would count the refused frame as sent
#define MG_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
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() + MG_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_ESP32_802154_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
-40
View File
@@ -1,40 +0,0 @@
// 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: it uses ESP-IDF's
// esp_ieee802154 driver, and defines its esp_ieee802154_receive_done(),
// esp_ieee802154_transmit_done() and esp_ieee802154_transmit_failed()
// callbacks. 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
+18 -5
View File
@@ -110,7 +110,15 @@ void RF_802_15_4_IRQHandler(void) {
n = n >= 2 ? n - 2 : 0; n = n >= 2 ? n - 2 : 0;
if (n > 0 && n <= sizeof(frame)) { if (n > 0 && n <= sizeof(frame)) {
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i]; for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i];
mg_mesh_rx_queue(&s_mcxw71_rx, s_mcxw71_ifp, frame, n); {
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_mcxw71_listen(); mg_mcxw71_listen();
@@ -171,6 +179,8 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data; struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh; struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros(); uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1; (void) s1;
if (s_mcxw71_txing && if (s_mcxw71_txing &&
mg_millis() > s_mcxw71_sendstart + MGI_SEND_TIMEOUT_MS) { mg_millis() > s_mcxw71_sendstart + MGI_SEND_TIMEOUT_MS) {
@@ -189,7 +199,10 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
node->tx_at = now + node->strategy->radio->ifs_us; node->tx_at = now + node->strategy->radio->ifs_us;
} }
node->now = now; node->now = now;
mg_mesh_rx_drain(node, &s_mcxw71_rx); 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);
}
node->strategy->tick(node, now); node->strategy->tick(node, now);
return node->joined; return node->joined;
} }
@@ -201,10 +214,10 @@ static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
address = address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u); (uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default; if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = mg_calloc(1, sizeof(*data->mesh)); data->mesh = calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false; if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) { if (!strategy->init(data->mesh, ifp, NULL, address)) {
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -213,7 +226,7 @@ static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_mcxw71_rx, s_mcxw71_queue, sizeof(s_mcxw71_queue)); mg_queue_init(&s_mcxw71_rx, s_mcxw71_queue, sizeof(s_mcxw71_queue));
if (mgi_mcxw71_radio_init(ifp)) return true; if (mgi_mcxw71_radio_init(ifp)) return true;
strategy->free(data->mesh); strategy->free(data->mesh);
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
+2 -3
View File
@@ -13,7 +13,7 @@ struct mg_tcpip_driver_mcxw71_data {
extern struct mg_tcpip_driver mg_tcpip_driver_mcxw71; extern struct mg_tcpip_driver mg_tcpip_driver_mcxw71;
#define MG_TCPIP_DRIVER_MCXW71_INIT(manager) \ #define MG_TCPIP_DRIVER_MCXW71_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_mcxw71_data driver_data_; \ static struct mg_tcpip_driver_mcxw71_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -21,10 +21,9 @@ extern struct mg_tcpip_driver mg_tcpip_driver_mcxw71;
mif_.ip = MG_TCPIP_IP; \ mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \ mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \ mif_.gw = MG_TCPIP_GW; \
mif_.mgr = (manager); \
mif_.driver = &mg_tcpip_driver_mcxw71; \ mif_.driver = &mg_tcpip_driver_mcxw71; \
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
mg_tcpip_init((manager), &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: mcxw71, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: mcxw71, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
+16 -5
View File
@@ -23,6 +23,7 @@ static void mg_mg24_rx_frame(RAIL_Handle_t h) {
RAIL_RxPacketInfo_t info; RAIL_RxPacketInfo_t info;
uint8_t f[128]; uint8_t f[128];
size_t n; size_t n;
char *dst;
if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) == if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) ==
RAIL_RX_PACKET_HANDLE_INVALID || RAIL_RX_PACKET_HANDLE_INVALID ||
info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS || info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS ||
@@ -32,7 +33,12 @@ static void mg_mg24_rx_frame(RAIL_Handle_t h) {
RAIL_CopyRxPacket(f, &info); RAIL_CopyRxPacket(f, &info);
n = info.packetBytes - 1U; // Strip PHR n = info.packetBytes - 1U; // Strip PHR
if (n == f[0]) n -= 2; // FCS stored, strip it if (n == f[0]) n -= 2; // FCS stored, strip it
mg_mesh_rx_queue(&s_mg24_rx, s_mg24_ifp, f + 1, n); 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++;
}
} }
// RAIL event callback, runs in interrupt context // RAIL event callback, runs in interrupt context
@@ -130,6 +136,8 @@ static bool mgi_mg24_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data; struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh; struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros(); uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1; (void) s1;
node->now = now; node->now = now;
if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart > if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart >
@@ -141,7 +149,10 @@ static bool mgi_mg24_poll(struct mg_tcpip_if *ifp, bool s1) {
s_mg24_txing = false; s_mg24_txing = false;
node->tx_at = now + node->strategy->radio->ifs_us; node->tx_at = now + node->strategy->radio->ifs_us;
} }
mg_mesh_rx_drain(node, &s_mg24_rx); 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);
}
node->strategy->tick(node, now); node->strategy->tick(node, now);
return node->joined; return node->joined;
} }
@@ -151,10 +162,10 @@ static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
uint16_t address = uint16_t address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u); (uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default; if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = mg_calloc(1, sizeof(*data->mesh)); data->mesh = calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false; if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) { if (!strategy->init(data->mesh, ifp, NULL, address)) {
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -163,7 +174,7 @@ static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_mg24_rx, s_mg24_rxbuf, sizeof(s_mg24_rxbuf)); mg_queue_init(&s_mg24_rx, s_mg24_rxbuf, sizeof(s_mg24_rxbuf));
if (mgi_mg24_radio_init(ifp)) return true; if (mgi_mg24_radio_init(ifp)) return true;
data->mesh->strategy->free(data->mesh); data->mesh->strategy->free(data->mesh);
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
+2 -3
View File
@@ -12,7 +12,7 @@ struct mg_tcpip_driver_mg24_data {
extern struct mg_tcpip_driver mg_tcpip_driver_mg24; extern struct mg_tcpip_driver mg_tcpip_driver_mg24;
#define MG_TCPIP_DRIVER_MG24_INIT(manager) \ #define MG_TCPIP_DRIVER_MG24_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_mg24_data driver_data_; \ static struct mg_tcpip_driver_mg24_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -20,10 +20,9 @@ extern struct mg_tcpip_driver mg_tcpip_driver_mg24;
mif_.ip = MG_TCPIP_IP; \ mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \ mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \ mif_.gw = MG_TCPIP_GW; \
mif_.mgr = (manager); \
mif_.driver = &mg_tcpip_driver_mg24; \ mif_.driver = &mg_tcpip_driver_mg24; \
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
mg_tcpip_init((manager), &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: mg24, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: mg24, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
+16 -5
View File
@@ -45,7 +45,13 @@ void RADIO_IRQHandler(void) {
mg_radio_802154.parse_hdr(&mg_radio_802154, frame, len, &dst, &src) != mg_radio_802154.parse_hdr(&mg_radio_802154, frame, len, &dst, &src) !=
0 && 0 &&
(dst == 0xffffu || dst == node->radio_address)) { (dst == 0xffffu || dst == node->radio_address)) {
mg_mesh_rx_queue(&s_nrf52_rx, s_nrf52_ifp, frame, len); 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++;
}
} }
} }
mgi_nrf52_listen(); mgi_nrf52_listen();
@@ -120,6 +126,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_tcpip_driver_nrf52_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh; struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros(); uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1; (void) s1;
if (s_nrf52_txing && mg_micros() - s_nrf52_started > 60000) { if (s_nrf52_txing && mg_micros() - s_nrf52_started > 60000) {
NVIC_DisableIRQ(RADIO_IRQn); NVIC_DisableIRQ(RADIO_IRQn);
@@ -134,7 +142,10 @@ static bool mgi_nrf52_poll(struct mg_tcpip_if *ifp, bool s1) {
node->tx_at = now + node->strategy->radio->ifs_us; node->tx_at = now + node->strategy->radio->ifs_us;
} }
node->now = now; node->now = now;
mg_mesh_rx_drain(node, &s_nrf52_rx); 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);
}
node->strategy->tick(node, now); node->strategy->tick(node, now);
return node->joined; return node->joined;
} }
@@ -146,10 +157,10 @@ static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
address = address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u); (uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default; if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = mg_calloc(1, sizeof(*data->mesh)); data->mesh = calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false; if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) { if (!strategy->init(data->mesh, ifp, NULL, address)) {
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -158,7 +169,7 @@ static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_nrf52_rx, s_nrf52_queue, sizeof(s_nrf52_queue)); mg_queue_init(&s_nrf52_rx, s_nrf52_queue, sizeof(s_nrf52_queue));
if (mgi_nrf52_radio_init(ifp)) return true; if (mgi_nrf52_radio_init(ifp)) return true;
strategy->free(data->mesh); strategy->free(data->mesh);
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
+2 -3
View File
@@ -12,7 +12,7 @@ struct mg_tcpip_driver_nrf52_data {
extern struct mg_tcpip_driver mg_tcpip_driver_nrf52; extern struct mg_tcpip_driver mg_tcpip_driver_nrf52;
#define MG_TCPIP_DRIVER_NRF52_INIT(manager) \ #define MG_TCPIP_DRIVER_NRF52_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_nrf52_data driver_data_; \ static struct mg_tcpip_driver_nrf52_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -20,10 +20,9 @@ extern struct mg_tcpip_driver mg_tcpip_driver_nrf52;
mif_.ip = MG_TCPIP_IP; \ mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \ mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \ mif_.gw = MG_TCPIP_GW; \
mif_.mgr = (manager); \
mif_.driver = &mg_tcpip_driver_nrf52; \ mif_.driver = &mg_tcpip_driver_nrf52; \
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
mg_tcpip_init((manager), &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: nrf52, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: nrf52, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
+18 -5
View File
@@ -138,7 +138,15 @@ 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) { static void mg_rw612_queue_rx(const uint8_t *frame, size_t len) {
mg_mesh_rx_queue(&s_rw612w_rx, s_rw612.ifp, frame, 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++;
}
}
} }
// Spinel packed unsigned integer: 7 bits per byte, LSB first // Spinel packed unsigned integer: 7 bits per byte, LSB first
@@ -449,6 +457,8 @@ static bool mgi_rw612w_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data; struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh; struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros(); uint64_t now = mg_micros();
char *buf;
size_t len;
(void) s1; (void) s1;
bool was_sending = s_rw612.txing; bool was_sending = s_rw612.txing;
mg_rw612_poll_imu(); mg_rw612_poll_imu();
@@ -460,7 +470,10 @@ static bool mgi_rw612w_poll(struct mg_tcpip_if *ifp, bool s1) {
if (was_sending && !s_rw612.txing) if (was_sending && !s_rw612.txing)
node->tx_at = now + node->strategy->radio->ifs_us; node->tx_at = now + node->strategy->radio->ifs_us;
node->now = now; node->now = now;
mg_mesh_rx_drain(node, &s_rw612w_rx); 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);
}
node->strategy->tick(node, now); node->strategy->tick(node, now);
return node->joined; return node->joined;
} }
@@ -478,10 +491,10 @@ static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
address = address =
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u); (uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
if (strategy == NULL) strategy = &mg_mesh_default; if (strategy == NULL) strategy = &mg_mesh_default;
data->mesh = mg_calloc(1, sizeof(*data->mesh)); data->mesh = calloc(1, sizeof(*data->mesh));
if (data->mesh == NULL) return false; if (data->mesh == NULL) return false;
if (!strategy->init(data->mesh, ifp, NULL, address)) { if (!strategy->init(data->mesh, ifp, NULL, address)) {
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
@@ -490,7 +503,7 @@ static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
mg_queue_init(&s_rw612w_rx, s_rw612w_queue, sizeof(s_rw612w_queue)); mg_queue_init(&s_rw612w_rx, s_rw612w_queue, sizeof(s_rw612w_queue));
if (mgi_rw612w_radio_init(ifp)) return true; if (mgi_rw612w_radio_init(ifp)) return true;
strategy->free(data->mesh); strategy->free(data->mesh);
mg_free(data->mesh); free(data->mesh);
data->mesh = NULL; data->mesh = NULL;
return false; return false;
} }
+2 -3
View File
@@ -13,7 +13,7 @@ struct mg_tcpip_driver_rw612w_data {
extern struct mg_tcpip_driver mg_tcpip_driver_rw612w; extern struct mg_tcpip_driver mg_tcpip_driver_rw612w;
#define MG_TCPIP_DRIVER_RW612W_INIT(manager) \ #define MG_TCPIP_DRIVER_RW612W_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_rw612w_data driver_data_; \ static struct mg_tcpip_driver_rw612w_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -21,10 +21,9 @@ extern struct mg_tcpip_driver mg_tcpip_driver_rw612w;
mif_.ip = MG_TCPIP_IP; \ mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \ mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \ mif_.gw = MG_TCPIP_GW; \
mif_.mgr = (manager); \
mif_.driver = &mg_tcpip_driver_rw612w; \ mif_.driver = &mg_tcpip_driver_rw612w; \
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
mg_tcpip_init((manager), &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: rw612w, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: rw612w, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
+8 -2
View File
@@ -34,7 +34,9 @@ struct mg_tcpip_driver_stm32f_data {
#define MG_ENABLE_ETH_IRQ() #define MG_ENABLE_ETH_IRQ()
#endif #endif
#define MG_TCPIP_DRIVER_INIT(mgr) \ extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
#define MG_TCPIP_DRIVER_STM32F_INIT(mgr) \
do { \ do { \
static struct mg_tcpip_driver_stm32f_data driver_data_; \ static struct mg_tcpip_driver_stm32f_data driver_data_; \
static struct mg_tcpip_if mif_; \ static struct mg_tcpip_if mif_; \
@@ -47,8 +49,12 @@ struct mg_tcpip_driver_stm32f_data {
mif_.driver_data = &driver_data_; \ mif_.driver_data = &driver_data_; \
MG_SET_MAC_ADDRESS(mif_.mac); \ MG_SET_MAC_ADDRESS(mif_.mac); \
MG_ENABLE_ETH_IRQ(); \ MG_ENABLE_ETH_IRQ(); \
mg_tcpip_init(mgr, &mif_); \ mg_tcpip_init((mgr), &mif_); \
MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \ MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0) } while (0)
#ifndef MG_TCPIP_DRIVER_INIT
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_STM32F_INIT(x)
#endif
#endif #endif
+10 -71
View File
@@ -108,8 +108,6 @@ static size_t mgi_mesh_b_queue(struct mg_mesh_node *node, struct mg_queue *q,
#define MGI_JOIN_JITTER_US 640 #define MGI_JOIN_JITTER_US 640
#define MGI_DATA_QUEUE_SIZE 2048 #define MGI_DATA_QUEUE_SIZE 2048
#define MGI_TX_RETRY_US 32 #define MGI_TX_RETRY_US 32
#define MGI_ANSWER_MARGIN_US 500 // Node: finish the first answer frame this
// much before the bridge stops waiting
// Our frames: the radio header, with the radio addresses, then the mesh // Our frames: the radio header, with the radio addresses, then the mesh
// header MG_MESH_HDR_LEN, then the payload // header MG_MESH_HDR_LEN, then the payload
#define MGI_JOIN_UP_LEN \ #define MGI_JOIN_UP_LEN \
@@ -136,7 +134,6 @@ struct mgi_mesh_b_data {
int8_t missed_rounds; // -1 after a grant; otherwise completed rounds missed int8_t missed_rounds; // -1 after a grant; otherwise completed rounds missed
uint16_t coordinator; // Coordinator radio address uint16_t coordinator; // Coordinator radio address
uint8_t phase; // MGI_TURN_IDLE, MGI_TURN_JOIN, or MGI_TURN_DATA uint8_t phase; // MGI_TURN_IDLE, MGI_TURN_JOIN, or MGI_TURN_DATA
uint64_t answer_by; // Node: first answer frame must end on air by then
}; };
struct mgi_mesh_b_peer { struct mgi_mesh_b_peer {
@@ -249,24 +246,11 @@ static void mgi_mesh_b_tx_step(struct mgi_mesh_b_data *data, uint64_t now) {
} else { } else {
mgi_mesh_b_frame_header(frame, dst, node->radio_address, command, 0); mgi_mesh_b_frame_header(frame, dst, node->radio_address, command, 0);
} }
// A node's first answer frame: if it cannot end on air before the bridge
// stops waiting, stay silent. The queued frame waits for the next turn
if (node->lan == NULL &&
(data->tx_offset == 0 || data->tx_offset == MGI_TX_DST_LEN) &&
now + mgi_mesh_b_airtime(len) > data->answer_by) {
data->tx_offset = 0;
data->tx_at = (uint64_t) -1;
return;
}
if (now < data->node->tx_at || data->node->tx(radio, frame, len) != len) { if (now < data->node->tx_at || data->node->tx(radio, frame, len) != len) {
data->tx_at = now + MGI_TX_RETRY_US; data->tx_at = now + MGI_TX_RETRY_US;
return; return;
} }
if (data->tx_offset == 0 || (data->tx_offset += len + 1) == record_len) { if (data->tx_offset == 0 || (data->tx_offset += len + 1) == record_len) {
// Bridge: the turn's last frame is out, wait for the answer
if (node->lan != NULL)
data->deadline =
now + mgi_mesh_b_airtime(len) + MG_MESH_ANSWER_TIMEOUT_US;
mgi_mesh_b_tx_finish(data); mgi_mesh_b_tx_finish(data);
} else { } else {
data->tx_at = now + mgi_mesh_b_airtime(len) + s_mesh_default_radio->ifs_us; data->tx_at = now + mgi_mesh_b_airtime(len) + s_mesh_default_radio->ifs_us;
@@ -276,7 +260,6 @@ static void mgi_mesh_b_tx_step(struct mgi_mesh_b_data *data, uint64_t now) {
static void mgi_mesh_b_tx_begin(struct mgi_mesh_b_data *data, bool burst, static void mgi_mesh_b_tx_begin(struct mgi_mesh_b_data *data, bool burst,
uint64_t now, uint64_t deadline) { uint64_t now, uint64_t deadline) {
data->phase = MGI_TURN_DATA; data->phase = MGI_TURN_DATA;
data->answer_by = (uint64_t) -1; // Node sets it after a grant
data->tx_offset = burst ? MGI_TX_DST_LEN : 0; data->tx_offset = burst ? MGI_TX_DST_LEN : 0;
data->tx_at = now; data->tx_at = now;
data->deadline = deadline; data->deadline = deadline;
@@ -307,11 +290,6 @@ static int mgi_mesh_b_data_receive(struct mgi_mesh_b_data *data,
memcpy(data->rx_buf + offset, frame + MG_MESH_FRAME_HDR_LEN, chunk); memcpy(data->rx_buf + offset, frame + MG_MESH_FRAME_HDR_LEN, chunk);
if (index < last) { if (index < last) {
data->rx_expected = (uint8_t) (burst + 0x10); data->rx_expected = (uint8_t) (burst + 0x10);
// Bridge: an answer is under way, wait for its next fragment
if (data->node->lan != NULL)
data->deadline =
now + mgi_mesh_b_airtime(s_mesh_default_radio->max_frame_size) +
s_mesh_default_radio->ifs_us + MG_MESH_ANSWER_TIMEOUT_US;
return -1; return -1;
} }
data->rx_expected = 0xff; data->rx_expected = 0xff;
@@ -321,6 +299,7 @@ static int mgi_mesh_b_data_receive(struct mgi_mesh_b_data *data,
// Bridge scheduling and Ethernet forwarding. // Bridge scheduling and Ethernet forwarding.
#define MGI_MAX_MISSES 3 // Turns in a row without an answer: remove the member
static uint16_t mgi_mesh_b_join_field(const uint8_t *p) { static uint16_t mgi_mesh_b_join_field(const uint8_t *p) {
return (uint16_t) ((unsigned) p[0] * 256 + p[1]); return (uint16_t) ((unsigned) p[0] * 256 + p[1]);
@@ -421,13 +400,13 @@ static void mgi_mesh_b_bridge_rx(uint64_t now, struct mg_mesh_node *b,
memcpy(peer->mac, frame + MG_MESH_FRAME_HDR_LEN, sizeof(peer->mac)); memcpy(peer->mac, frame + MG_MESH_FRAME_HDR_LEN, sizeof(peer->mac));
} }
// Member peer did not answer its turn. After MG_MESH_MAX_MISSES in a row, it // Member peer did not answer its turn. After MGI_MAX_MISSES in a row, it is
// is gone: remove it, and its queued downlink frames // gone: remove it, and its queued downlink frames
static void mgi_mesh_b_member_missed(struct mgi_mesh_b_data *data, static void mgi_mesh_b_member_missed(struct mgi_mesh_b_data *data,
struct mgi_mesh_b_peer *peer) { struct mgi_mesh_b_peer *peer) {
if (++peer->misses < MG_MESH_MAX_MISSES) return; if (++peer->misses < MGI_MAX_MISSES) return;
MG_INFO(("Member %M removed: no answer %d times", mg_print_mac, peer->mac, MG_INFO(("Member %M removed: no answer %d times", mg_print_mac, peer->mac,
MG_MESH_MAX_MISSES)); MGI_MAX_MISSES));
if (data->fanout == peer) { if (data->fanout == peer) {
data->fanout = peer->next; data->fanout = peer->next;
if (data->fanout == NULL) { if (data->fanout == NULL) {
@@ -520,7 +499,7 @@ static void mgi_mesh_b_node_rx(struct mg_mesh_node *n, uint64_t now,
if (command == MGI_JOIN_DOWN && len == MG_MESH_FRAME_HDR_LEN && if (command == MGI_JOIN_DOWN && len == MG_MESH_FRAME_HDR_LEN &&
dst == 0xffffu) { dst == 0xffffu) {
if (n->joined && src == data->coordinator && if (n->joined && src == data->coordinator &&
++data->missed_rounds >= MG_MESH_MAX_MISSES) { ++data->missed_rounds >= MGI_MAX_MISSES) {
n->joined = false; n->joined = false;
data->phase = MGI_TURN_IDLE; data->phase = MGI_TURN_IDLE;
data->tx_at = (uint64_t) -1; data->tx_at = (uint64_t) -1;
@@ -555,22 +534,17 @@ static void mgi_mesh_b_node_rx(struct mg_mesh_node *n, uint64_t now,
now + mgi_mesh_b_burst_time(MGI_ETH_MAX) + now + mgi_mesh_b_burst_time(MGI_ETH_MAX) +
2 * s_mesh_default_radio->ifs_us + 2 * s_mesh_default_radio->ifs_us +
MGI_JOIN_TURN_US); MGI_JOIN_TURN_US);
data->answer_by = now + MG_MESH_ANSWER_TIMEOUT_US - MGI_ANSWER_MARGIN_US;
} }
} }
// Receive timing counts from the frame's arrival, see mg_mesh_rx_drain().
// Without an arrival time, e.g. when called directly, use the poll time
static void mgi_mesh_b_input(struct mg_mesh_node *node, void *buf, size_t len) { static void mgi_mesh_b_input(struct mg_mesh_node *node, void *buf, size_t len) {
uint64_t ready = node->now + node->strategy->radio->turnaround_us; uint64_t ready = node->now + node->strategy->radio->turnaround_us;
uint64_t arrived = node->rx_at != 0 ? node->rx_at : node->now;
if (node->tx_at < ready) node->tx_at = ready; if (node->tx_at < ready) node->tx_at = ready;
node->next_tick = node->now; node->next_tick = node->now;
if (node->lan != NULL) { if (node->lan != NULL)
mgi_mesh_b_bridge_rx(arrived, node, (const uint8_t *) buf, len); mgi_mesh_b_bridge_rx(node->now, node, (const uint8_t *) buf, len);
} else { else
mgi_mesh_b_node_rx(node, arrived, (uint8_t *) buf, len); mgi_mesh_b_node_rx(node, node->now, (uint8_t *) buf, len);
}
} }
static void mgi_mesh_b_tick(struct mg_mesh_node *node, uint64_t now) { static void mgi_mesh_b_tick(struct mg_mesh_node *node, uint64_t now) {
@@ -627,41 +601,6 @@ static void mgi_mesh_b_free(struct mg_mesh_node *node) {
node->data = NULL; node->data = NULL;
} }
// Receive queue record: arrival time, then the radio frame
#define MGI_RX_STAMP_LEN sizeof(uint64_t)
void mg_mesh_rx_queue(struct mg_queue *q, struct mg_tcpip_if *ifp,
const void *frame, size_t len) {
uint64_t arrived = mg_micros();
size_t need = MGI_RX_STAMP_LEN + len;
char *dst;
if (mg_queue_book(q, &dst, need) < need) {
if (ifp != NULL) ifp->ndrop++;
return;
}
memcpy(dst, &arrived, MGI_RX_STAMP_LEN);
memcpy(dst + MGI_RX_STAMP_LEN, frame, len);
mg_queue_add(q, need);
}
void mg_mesh_rx_drain(struct mg_mesh_node *node, struct mg_queue *q) {
char *record;
size_t len;
while (node->rx_len == 0) {
len = mg_queue_next(q, &record);
if (len == 0) break;
if (len > MGI_RX_STAMP_LEN) {
memcpy(&node->rx_at, record, MGI_RX_STAMP_LEN);
node->strategy->rx(node, record + MGI_RX_STAMP_LEN,
len - MGI_RX_STAMP_LEN);
node->rx_at = 0;
}
mg_queue_del(q, len);
}
}
const struct mg_mesh mg_mesh_default = { const struct mg_mesh mg_mesh_default = {
"default", "default",
mgi_mesh_b_init, mgi_mesh_b_init,
+3 -28
View File
@@ -12,6 +12,9 @@
#if MG_ENABLE_TCPIP #if MG_ENABLE_TCPIP
// Monotonic time in microseconds.
uint64_t mg_micros(void);
// A radio standard // A radio standard
struct mg_radio { struct mg_radio {
const char *name; // For logs const char *name; // For logs
@@ -41,21 +44,6 @@ struct mg_radio {
#define MG_802154_CHANNEL 15 #define MG_802154_CHANNEL 15
#endif #endif
// A turn's answer must arrive this soon after the turn's last frame ends on
// air, or the turn ends. A node that would answer later stays silent and keeps
// its frame for the next turn. Covers the node's poll latency, the radio
// turnaround and one full fragment's airtime, 4.3 ms on 802.15.4
#ifndef MG_MESH_ANSWER_TIMEOUT_US
#define MG_MESH_ANSWER_TIMEOUT_US 8000
#endif
// Misses in a row before giving up, 1 to 127. The bridge removes a member
// that did not answer this many turns. A node leaves the mesh after this
// many rounds without a turn
#ifndef MG_MESH_MAX_MISSES
#define MG_MESH_MAX_MISSES 10
#endif
extern const struct mg_radio mg_radio_802154; extern const struct mg_radio mg_radio_802154;
struct mg_mesh_node { struct mg_mesh_node {
@@ -65,7 +53,6 @@ struct mg_mesh_node {
size_t (*tx)(struct mg_tcpip_if *, const void *, size_t); // Radio frame TX size_t (*tx)(struct mg_tcpip_if *, const void *, size_t); // Radio frame TX
size_t rx_len; // Complete Ethernet frame in the interface RX buffer size_t rx_len; // Complete Ethernet frame in the interface RX buffer
uint64_t now; // Current driver poll time, us uint64_t now; // Current driver poll time, us
uint64_t rx_at; // Arrival time of the radio frame being handled, us
uint64_t tx_at; // Earliest next transmission, us uint64_t tx_at; // Earliest next transmission, us
void *data; // Private state owned by the strategy void *data; // Private state owned by the strategy
size_t ram_used; // Live mesh state bytes size_t ram_used; // Live mesh state bytes
@@ -90,16 +77,4 @@ struct mg_mesh {
extern const struct mg_mesh mg_mesh_default; extern const struct mg_mesh mg_mesh_default;
// Put a received radio frame into the driver's receive queue, together with
// its arrival time from mg_micros(). Call it when the frame arrives, from the
// radio interrupt if the driver has one. If the queue is full, the frame is
// dropped and counted in ifp->ndrop
void mg_mesh_rx_queue(struct mg_queue *q, struct mg_tcpip_if *ifp,
const void *frame, size_t len);
// Pass queued radio frames to the mesh, oldest first. Before each frame,
// node->rx_at is set to its arrival time. Stops while the node holds a
// received Ethernet frame the TCP/IP stack has not taken yet
void mg_mesh_rx_drain(struct mg_mesh_node *node, struct mg_queue *q);
#endif #endif
+1 -2
View File
@@ -135,7 +135,6 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac);
// Built-in hardware drivers. Assign one to mg_tcpip_if.driver before calling // Built-in hardware drivers. Assign one to mg_tcpip_if.driver before calling
// mg_tcpip_init(). Each driver also requires a corresponding driver_data // mg_tcpip_init(). Each driver also requires a corresponding driver_data
// struct; see the matching driver header for details. // struct; see the matching driver header for details.
extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
extern struct mg_tcpip_driver mg_tcpip_driver_w5500; extern struct mg_tcpip_driver mg_tcpip_driver_w5500;
extern struct mg_tcpip_driver mg_tcpip_driver_w5100; extern struct mg_tcpip_driver mg_tcpip_driver_w5100;
extern struct mg_tcpip_driver mg_tcpip_driver_tm4c; extern struct mg_tcpip_driver mg_tcpip_driver_tm4c;
@@ -159,7 +158,7 @@ extern struct mg_tcpip_driver mg_tcpip_driver_netc;
#include "drivers/mcxw71.h" #include "drivers/mcxw71.h"
#include "drivers/nrf52.h" #include "drivers/nrf52.h"
#include "drivers/rw612w.h" #include "drivers/rw612w.h"
#include "drivers/esp32_802154.h" #include "drivers/stm32f.h"
// SPI bus abstraction for SPI-attached network chips (e.g. W5500). // SPI bus abstraction for SPI-attached network chips (e.g. W5500).
// Populate and assign to the driver's driver_data field. // Populate and assign to the driver's driver_data field.
+5 -7
View File
@@ -4,13 +4,11 @@
#include "net.h" #include "net.h"
// Return microseconds since boot. Never goes backwards. // Return milliseconds since boot (uptime). Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1 // On RTOS targets (FreeRTOS, ThreadX, Zephyr, etc.) uses the RTOS tick count.
uint64_t mg_micros(void); // On STM32/Cube uses HAL_GetTick(). On Arduino uses millis().
// On Linux/macOS uses CLOCK_MONOTONIC_RAW or CLOCK_MONOTONIC; falls back to
// Return milliseconds since boot. Never goes backwards. Calls mg_micros(). // CLOCK_REALTIME if neither is available.
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void); uint64_t mg_millis(void);
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync; // Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
+18 -29
View File
@@ -197,38 +197,33 @@ bool mg_path_is_sane(const struct mg_str path) {
return true; return true;
} }
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one #if MG_ENABLE_CUSTOM_MILLIS
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS #else
#if MG_ENABLE_CUSTOM_MICROS == 0 uint64_t mg_millis(void) {
uint64_t mg_micros(void) { #if MG_ARCH == MG_ARCH_ESP32 || MG_ENABLE_FREERTOS
#if MG_ARCH == MG_ARCH_ESP32 return xTaskGetTickCount() * portTICK_PERIOD_MS;
return (uint64_t) esp_timer_get_time();
#elif MG_ENABLE_FREERTOS
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
#elif MG_ARCH == MG_ARCH_THREADX #elif MG_ARCH == MG_ARCH_THREADX
return (uint64_t) tx_time_get() * return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND);
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_TIRTOS #elif MG_ARCH == MG_ARCH_TIRTOS
return (uint64_t) Clock_getTicks() * 1000; return (uint64_t) Clock_getTicks();
#elif MG_ARCH == MG_ARCH_ZEPHYR #elif MG_ARCH == MG_ARCH_ZEPHYR
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks()); return (uint64_t) k_uptime_get();
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1 #elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
return (uint64_t) rt_time_get() * 1000; return (uint64_t) rt_time_get();
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2 #elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) * return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq());
1000;
#elif MG_ARCH == MG_ARCH_RTTHREAD #elif MG_ARCH == MG_ARCH_RTTHREAD
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000; return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND);
#elif MG_ARCH == MG_ARCH_WIN32 #elif MG_ARCH == MG_ARCH_WIN32
return (uint64_t) GetTickCount() * 1000; return GetTickCount();
#elif MG_ARCH == MG_ARCH_PICOSDK #elif MG_ARCH == MG_ARCH_PICOSDK
return time_us_64(); return time_us_64() / 1000;
#elif MG_ARCH == MG_ARCH_CUBE #elif MG_ARCH == MG_ARCH_CUBE
return (uint64_t) HAL_GetTick() * 1000; return (uint64_t) HAL_GetTick();
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__) #elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux // Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux // Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000; return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000;
#elif MG_ARCH == MG_ARCH_UNIX #elif MG_ARCH == MG_ARCH_UNIX
struct timespec ts = {0, 0}; struct timespec ts = {0, 0};
// See #1615 - prefer monotonic clock // See #1615 - prefer monotonic clock
@@ -243,21 +238,15 @@ uint64_t mg_micros(void) {
// adjustments performed by adjtime and NTP // adjustments performed by adjtime and NTP
clock_gettime(CLOCK_REALTIME, &ts); clock_gettime(CLOCK_REALTIME, &ts);
#endif #endif
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000); return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000);
#elif defined(ARDUINO) #elif defined(ARDUINO)
return (uint64_t) micros(); return (uint64_t) millis();
#else #else
return (uint64_t) time(NULL) * 1000000; return (uint64_t) (time(NULL) * 1000);
#endif #endif
} }
#endif #endif
#if MG_ENABLE_CUSTOM_MILLIS == 0
uint64_t mg_millis(void) {
return mg_micros() / 1000;
}
#endif
// network format equates big endian order // network format equates big endian order
uint16_t mg_ntohs(uint16_t net) { uint16_t mg_ntohs(uint16_t net) {
return MG_LOAD_BE16(&net); return MG_LOAD_BE16(&net);
+1 -1
View File
@@ -6,7 +6,7 @@ HDRS = simulator.h $(ROOT)/mongoose.h
WARN = -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion -fno-common \ WARN = -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion -fno-common \
-Wconversion -Wundef -Wmissing-prototypes -Wstrict-prototypes -Wconversion -Wundef -Wmissing-prototypes -Wstrict-prototypes
DEFS = -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ENABLE_IPV6=0 \ DEFS = -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ENABLE_IPV6=0 \
-DMG_ENABLE_CUSTOM_MICROS=1 -DMG_ENABLE_TCPIP_TCPRTX=1 \ -DMG_ENABLE_CUSTOM_MILLIS=1 -DMG_ENABLE_TCPIP_TCPRTX=1 \
-DMG_IO_SIZE=512 \ -DMG_IO_SIZE=512 \
-DMG_TCPIP_ARP_MS=1000 -DMG_TCPIP_ACK_MS=500 # Slow radio: wait longer -DMG_TCPIP_ARP_MS=1000 -DMG_TCPIP_ACK_MS=500 # Slow radio: wait longer
CFLAGS ?= -O2 -g0 $(WARN) CFLAGS ?= -O2 -g0 $(WARN)
+3 -3
View File
@@ -277,7 +277,7 @@ The FRDM-RW612 example is the Ethernet bridge. The XIAO MG24 and
FRDM-MCXW71 examples are radio clients. All three use the slotted mesh. FRDM-MCXW71 examples are radio clients. All three use the slotted mesh.
```sh ```sh
make -C tutorials/nxp/frdm-rw612/ethernet-to-802.15.4-bridge make -C tutorials/nxp/frdm-rw612/bridge
make -C tutorials/silabs/xiao-mg24/minimal make -C tutorials/silabs/xiao-mg24/minimal
make -C tutorials/nxp/frdm-mcxw71/minimal make -C tutorials/nxp/frdm-mcxw71/minimal
``` ```
@@ -292,7 +292,7 @@ MG24 is a node only; its radio address is derived from its MAC.
The drivers in `src/drivers` send and receive raw 802.15.4 frames. The drivers in `src/drivers` send and receive raw 802.15.4 frames.
They use the same slotted mesh implementation as the simulator. They use the same slotted mesh implementation as the simulator.
The board driver is initialized by `mg_mgr_init()`. The RW612 bridge The board driver is initialized by `mg_mgr_init()`. The RW612 bridge
has its own `ethernet-to-802.15.4-bridge/main.c`, which initializes and polls the two drivers has its own `bridge/main.c`, which initializes and polls the two drivers
directly, without an event manager or local IP stack. directly, without an event manager or local IP stack.
## Hardware integration ## Hardware integration
@@ -306,7 +306,7 @@ A NULL strategy selects `mg_mesh_default`, named `"default"`; assigning another
raw radio frames; the selected strategy owns joining, scheduling and Ethernet raw radio frames; the selected strategy owns joining, scheduling and Ethernet
fragmentation. Driver initialization goes through `mg_tcpip_init()`. fragmentation. Driver initialization goes through `mg_tcpip_init()`.
Board examples are under `tutorials/nxp/frdm-rw612/ethernet-to-802.15.4-bridge`, Board examples are under `tutorials/nxp/frdm-rw612/bridge`,
`tutorials/nxp/frdm-mcxw71/minimal`, `tutorials/silabs/xiao-mg24/minimal`, and `tutorials/nxp/frdm-mcxw71/minimal`, `tutorials/silabs/xiao-mg24/minimal`, and
`tutorials/nordic/xiao-52840/minimal`. Their HALs supply `mg_micros()`. `tutorials/nordic/xiao-52840/minimal`. Their HALs supply `mg_micros()`.
+4
View File
@@ -18,6 +18,10 @@ uint64_t mg_micros(void) {
return s_sim.now; return s_sim.now;
} }
uint64_t mg_millis(void) {
return mg_micros() / 1000;
}
// Parse "X,Y,Z", or "X,Y" with Z 0. Return false if it is not that // Parse "X,Y,Z", or "X,Y" with Z 0. Return false if it is not that
static bool parse_pos(const char *s, struct pos *p) { static bool parse_pos(const char *s, struct pos *p) {
int n = 0; // Characters parsed int n = 0; // Characters parsed
@@ -1,4 +0,0 @@
cmake_minimum_required(VERSION 3.16)
set(IDF_TARGET esp32c6)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(mongoose-minimal-802154)
-23
View File
@@ -1,23 +0,0 @@
# Environment setup: https://mongoose.ws/documentation/tutorials/tools/
# Build: ESP-IDF from the espressif/idf Docker image. Flash: esptool.
# Monitor: esputil, https://github.com/cpq/esputil
THISDIR = $(realpath $(CURDIR))
ROOTDIR = $(realpath $(CURDIR)/../../..)
DOCKER ?= docker run --rm -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) espressif/idf
PORT ?= /dev/cu.usbmodem2101
all build:
$(DOCKER) idf.py build
flash: build
cd build && esptool --chip esp32c6 -p $(PORT) -b 460800 write-flash @flash_args
monitor:
esputil -p $(PORT) monitor
menuconfig:
docker run --rm -it -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) espressif/idf idf.py menuconfig
clean:
test -d build && $(DOCKER) rm -rf build sdkconfig || true
@@ -1,5 +0,0 @@
idf_component_register(SRCS "main.c" "../../../../mongoose.c"
"../../../mqtt/mqtt-client/mongoose_mqtt.c"
INCLUDE_DIRS "." "../../../.."
REQUIRES ieee802154 esp_timer esp_hw_support
esp_driver_gpio app_update)
@@ -1,99 +0,0 @@
// Copyright (c) 2026 Cesanta Software Limited
// All rights reserved
#include <driver/gpio.h>
#include <esp_heap_caps.h>
#include "mongoose.h"
void mg_mqtt_init(struct mg_mgr *mgr); // From tutorials/mqtt/mqtt-client
void mg_mqtt_poll(struct mg_mgr *mgr);
// Seeed XIAO ESP32-C6
#define LED1 GPIO_NUM_15 // User LED, active low
#define RF_SW_PWR GPIO_NUM_3 // RF switch power, active low
#define RF_SW_SEL GPIO_NUM_14 // Antenna select: low built-in, high U.FL
// Self-signed P-256 certificate and key for the HTTPS listener. Created by:
// openssl ecparam -genkey -name prime256v1 -noout -out key.pem
// openssl req -x509 -key key.pem -out cert.pem -days 3650 -subj /CN=mg
#define TLS_CERT \
"-----BEGIN CERTIFICATE-----\n" \
"MIIBgjCCASegAwIBAgIUIJOq77Qn7lHbH6WseMemeqorR50wCgYIKoZIzj0EAwIw\n" \
"FjEUMBIGA1UEAwwLbnVjbGVvLWg3MjMwHhcNMjYxMDEyMTUwMjE5WhcNMzYxMDA5\n" \
"MTUwMjE5WjAWMRQwEgYDVQQDDAtudWNsZW8taDcyMzBZMBMGByqGSM49AgEGCCqG\n" \
"SM49AwEHA0IABB6IQJNmzrsdQL+2BnZYS7GYOaj7sz8f6cjBmz3n8D9Em19XodlU\n" \
"uy4ybIvyhr766lL0ERoTmMUivb/lGmzx9C+jUzBRMB0GA1UdDgQWBBQqxMuc4gj7\n" \
"MTMzreWlRMNI3QMqTzAfBgNVHSMEGDAWgBQqxMuc4gj7MTMzreWlRMNI3QMqTzAP\n" \
"BgNVHRMBAf8EBTADAQH/MAoGCCqGSM49BAMCA0kAMEYCIQDY4xaJpuxq6b3rHK3b\n" \
"lOIQyfkQX+orCRqQUkseplqpqwIhAKswZFNn+2zUNRRNY8+cHmdIwmdpsmxtoWYP\n" \
"BCjX++LI\n" \
"-----END CERTIFICATE-----\n"
#define TLS_KEY \
"-----BEGIN EC PRIVATE KEY-----\n" \
"MHcCAQEEIJ/GBl0TlD52q9PMg1KWaNIdXqp8b/HyIsABmBpqC+O1oAoGCCqGSM49\n" \
"AwEHoUQDQgAEHohAk2bOux1Av7YGdlhLsZg5qPuzPx/pyMGbPefwP0SbX1eh2VS7\n" \
"LjJsi/KGvvrqUvQRGhOYxSK9v+UabPH0Lw==\n" \
"-----END EC PRIVATE KEY-----\n"
// Power the XIAO RF switch and select the built-in antenna
static void antenna_init(void) {
gpio_set_direction(RF_SW_PWR, GPIO_MODE_OUTPUT);
gpio_set_level(RF_SW_PWR, 0);
gpio_set_direction(RF_SW_SEL, GPIO_MODE_OUTPUT);
gpio_set_level(RF_SW_SEL, 0);
}
// Toggle the user LED twice a second
static void blink_task(void) {
static uint64_t blink_timer = 0;
static int on = 0;
if (mg_timer_expired(&blink_timer, 500, mg_millis())) {
gpio_set_level(LED1, on ^= 1);
}
}
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_ACCEPT && c->is_tls) { // Accepted on the https:// listener
struct mg_tls_opts opts = {.cert = mg_str(TLS_CERT),
.key = mg_str(TLS_KEY)};
mg_tls_init(c, &opts);
} else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_match(hm->uri, mg_str("/api/tick"), NULL)) {
mg_http_reply(c, 200, "", "{%m:%llu}\n", MG_ESC("tick"), mg_millis());
} else if (mg_match(hm->uri, mg_str("/api/crash"), NULL)) {
__asm volatile("unimp"); // Illegal instruction: ESP-IDF prints a panic
} else {
mg_http_reply(c, 200, "",
"Hi from Mongoose!\n"
"tick: %llu\n"
"ram free: %zu\n"
"ram min: %zu\n",
mg_millis(), heap_caps_get_free_size(MALLOC_CAP_DEFAULT),
heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT));
}
}
}
void app_main(void) {
struct mg_mgr mgr;
antenna_init();
gpio_set_direction(LED1, GPIO_MODE_OUTPUT);
mg_mgr_init(&mgr); // Also starts the 802.15.4 driver
mg_http_listen(&mgr, "http://0.0.0.0", http_ev_handler, NULL);
mg_http_listen(&mgr, "https://0.0.0.0", http_ev_handler, NULL);
mg_mqtt_init(&mgr);
MG_INFO(("Initialised. RAM free: %zu",
heap_caps_get_free_size(MALLOC_CAP_DEFAULT)));
for (;;) {
mg_mgr_poll(&mgr, 0);
mg_mqtt_poll(&mgr);
blink_task();
}
}
@@ -1,25 +0,0 @@
#pragma once
#include <esp_mac.h>
// See https://mongoose.ws/docs/getting-started/build-options/
#define MG_ARCH MG_ARCH_ESP32
#define MG_TLS MG_TLS_BUILTIN
#define MQTT_CLIENT_ID "esp32c6" // For tutorials/mqtt/mqtt-client
#define MG_ENABLE_TCPIP 1 // Built-in TCP/IP stack, not lwIP
#define MG_ENABLE_DRIVER_ESP32_802154 1 // ESP32-C6 802.15.4 radio
#define MG_ENABLE_TCPIP_TCPRTX 1 // Link can be lossy, enable retransmit
// Stable MAC, so the DHCP address stays the same across reboots: the factory
// MAC, locally administered
#define MG_SET_MAC_ADDRESS(mac) \
do { \
esp_read_mac(mac, ESP_MAC_BASE); \
(mac)[0] |= 2; \
} while (0)
// mdash.net device management service support
#define MG_ENABLE_MDASH 0
#define MG_MDASH_KEY "..." // Set this - mDash device token
#define MG_FIRMWARE_VERSION "esp32c6.1.0.0"
@@ -1,8 +0,0 @@
CONFIG_IDF_TARGET="esp32c6"
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
CONFIG_IEEE802154_ENABLED=y
# Built-in TLS handshakes need a larger stack
CONFIG_ESP_MAIN_TASK_STACK_SIZE=12288
# The main task busy-loops the mesh, so the idle task does not run
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n
@@ -17,6 +17,9 @@ static void blink_task(void) {
} }
} }
uint64_t mg_millis(void) {
return hal_get_tick();
}
bool mg_random(void *buf, size_t len) { bool mg_random(void *buf, size_t len) {
for (size_t n = 0; n < len; n += sizeof(uint32_t)) { for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
@@ -4,7 +4,7 @@
#define MG_ENABLE_TCPIP 1 #define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_NRF52 1 #define MG_ENABLE_DRIVER_NRF52 1
#define MG_ENABLE_TCPIP_TCPRTX 1 #define MG_ENABLE_TCPIP_TCPRTX 1
#define MG_ENABLE_CUSTOM_MICROS 1 #define MG_ENABLE_CUSTOM_MILLIS 1
#define MG_ENABLE_CUSTOM_RANDOM 1 #define MG_ENABLE_CUSTOM_RANDOM 1
#define MG_TLS MG_TLS_BUILTIN #define MG_TLS MG_TLS_BUILTIN
#define MQTT_CLIENT_ID "xiao-nrf52840" #define MQTT_CLIENT_ID "xiao-nrf52840"
+3
View File
@@ -25,6 +25,9 @@ static void blink_task(void) {
} }
} }
uint64_t mg_millis(void) {
return hal_get_tick();
}
bool mg_random(void *buf, size_t len) { bool mg_random(void *buf, size_t len) {
for (size_t n = 0; n < len; n += sizeof(uint32_t)) { for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
@@ -9,7 +9,7 @@
#define MG_ENABLE_TCPIP 1 #define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_MCXW71 1 #define MG_ENABLE_DRIVER_MCXW71 1
#define MG_ENABLE_CUSTOM_MICROS 1 #define MG_ENABLE_CUSTOM_MILLIS 1
#define MG_ENABLE_CUSTOM_RANDOM 1 #define MG_ENABLE_CUSTOM_RANDOM 1
#define MG_ENABLE_TCPIP_TCPRTX 1 // Link can be lossy, enable retransmit #define MG_ENABLE_TCPIP_TCPRTX 1 // Link can be lossy, enable retransmit
@@ -43,6 +43,9 @@ static void log_fn(char ch, void *arg) {
hal_uart_write_buf(arg, &ch, 1); hal_uart_write_buf(arg, &ch, 1);
} }
uint64_t mg_millis(void) {
return hal_get_tick();
}
int main(void) { int main(void) {
static struct mg_tcpip_driver_rw612_data eth_data; static struct mg_tcpip_driver_rw612_data eth_data;
@@ -6,7 +6,7 @@
#define MG_ENABLE_TCPIP 1 #define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_RW612 1 // Ethernet #define MG_ENABLE_DRIVER_RW612 1 // Ethernet
#define MG_ENABLE_DRIVER_RW612W 1 // Mesh bridge over raw 802.15.4 #define MG_ENABLE_DRIVER_RW612W 1 // Mesh bridge over raw 802.15.4
#define MG_ENABLE_CUSTOM_MICROS 1 #define MG_ENABLE_CUSTOM_MILLIS 1
// The bridge initializes and polls its two L2 interfaces directly. // The bridge initializes and polls its two L2 interfaces directly.
@@ -26,6 +26,9 @@ static void blink_task(void) {
} }
} }
uint64_t mg_millis(void) {
return hal_get_tick();
}
bool mg_random(void *buf, size_t len) { bool mg_random(void *buf, size_t len) {
for (size_t n = 0; n < len; n += sizeof(uint32_t)) { for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
@@ -8,7 +8,7 @@
#define MG_ENABLE_TCPIP 1 #define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_MG24 1 #define MG_ENABLE_DRIVER_MG24 1
#define MG_ENABLE_CUSTOM_MICROS 1 #define MG_ENABLE_CUSTOM_MILLIS 1
#define MG_ENABLE_CUSTOM_RANDOM 1 // Secure Engine TRNG, see hal.c #define MG_ENABLE_CUSTOM_RANDOM 1 // Secure Engine TRNG, see hal.c
#define MG_ENABLE_TCPIP_TCPRTX 1 // Link can be lossy, enable retransmit #define MG_ENABLE_TCPIP_TCPRTX 1 // Link can be lossy, enable retransmit