BLE radio support

This commit is contained in:
cpq committed 2026-11-26 11:01:25 +00:00
1 parent a8d6573ccc
commit 0582cd6724
81 files changed
+7053 -4906

No files matched your search

+162 -100
View File
@@ -1,11 +1,131 @@
#include "l2_mesh.h"
#include "log.h"
#include "util.h"
#include "net_builtin.h"
#include "radio.h"
#include "util.h"
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MCXW71) && \
MG_ENABLE_DRIVER_MCXW71
// TrustZone: code built for the secure state (-mcmse) uses the secure alias
// of the radio registers, 0x5xxxxxxx. Otherwise, the non-secure alias
#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE & 2)
#define MGI_RF_BASE 0x58A00000U
#define MGI_PKT_RAM ((volatile uint8_t *) 0x58A08000)
#else
#define MGI_RF_BASE 0x48A00000U
#define MGI_PKT_RAM ((volatile uint8_t *) 0x48A08000)
#endif
#define MGI_NVIC_ISER ((volatile uint32_t *) 0xE000E100) // Enable IRQ
#define MGI_NVIC_ICER ((volatile uint32_t *) 0xE000E180) // Disable IRQ
#if MG_RADIO == MG_RADIO_BLE
#define MGI_IRQN 54 // RF_Generic_IRQn
#else
#define MGI_IRQN 53 // RF_802_15_4_IRQn
#endif
// Keep the IRQ handler out while we change the radio. The NVIC disable is
// atomic, so register updates here and there do not race
static void mg_mcxw71_lock(void) {
MGI_NVIC_ICER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
__asm volatile("dsb; isb" ::: "memory");
}
static struct mg_tcpip_if *s_mcxw71_ifp;
static volatile bool s_mcxw71_txing, s_mcxw71_done;
static uint64_t s_mcxw71_sendstart;
#if MG_RADIO == MG_RADIO_BLE
// GENFSK Bluetooth LE uncoded mode, RM Table 489. S0 + 8-bit length, then
// our frame. The PHY supplies the preamble, whitening and 24-bit CRC.
struct mg_genfsk {
volatile uint32_t IRQ_CTRL, EVENT_TMR, T1_CMP, T2_CMP, TIMESTAMP, XCVR_CTRL,
XCVR_STS, XCVR_CFG, CHANNEL_NUM0, TX_POWER, NTW_ADR_CTRL, NTW_ADR[4],
RX_WATERMARK, DSM_CTRL, PART_ID, SLOT_PRELOAD, SLOT_TIME, TURNAROUND_TIME,
ACKDELAY, RXDELAY, TXDELAY, PACKET_CFG, H0_CFG, H1_CFG, CRC_CFG,
LENGTH_ADJ, TIMESTAMP_RX_DONE, TIMESTAMP_TX_DONE, MULT_PKT_CTRL,
RPA_WL_STATUS, LENGTH_MAX, EVENT_TMR_LD, EVENT_TMR_ADD, ENH_FEATURE;
uint32_t reserved[21];
volatile uint32_t SEQ_STS;
};
#define MGI_GFSK ((struct mg_genfsk *) (MGI_RF_BASE + 0x2000))
#define MGI_TX_PB MGI_PKT_RAM // Access address, S0, length, frame; no CRC
#define MGI_RX_PB (MGI_PKT_RAM + 0x1000) // S0, length, frame, CRC; no AA
#define MGI_RADIO_MAX 251
#define MGI_GFSK_IRQ_EN (MG_BIT(26) | MG_BIT(16)) // Master + sequence end
// BLE preamble starts with the access address's first bit; transmit LSBit
// first.
#define MGI_GFSK_CFG \
(MG_BIT(24) | (((MG_RADIO_NETWORK & 1U) ? 0x55U : 0xaaU) << 16) | (1U << 4))
static void mg_mcxw71_abort(void) {
// NXP CONNRF-767: do not abort during TX warmup.
while (MGI_GFSK->SEQ_STS & MG_BIT(2)) (void) 0;
MGI_GFSK->XCVR_CTRL = 11; // ABORT_ALL
while (MGI_GFSK->XCVR_CTRL & MG_BIT(31)) (void) 0;
MGI_GFSK->IRQ_CTRL = MGI_GFSK_IRQ_EN | 0xffffU; // W1C status
}
static void mg_mcxw71_listen(void) {
MGI_GFSK->IRQ_CTRL = MGI_GFSK_IRQ_EN | 0xffffU;
MGI_GFSK->XCVR_CFG = MGI_GFSK_CFG;
MGI_GFSK->XCVR_CTRL = 5; // RX_START_NOW
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
}
static void mg_mcxw71_go(void) {
mg_mcxw71_abort();
MGI_GFSK->XCVR_CFG = MGI_GFSK_CFG; // ABORT disables packet processing
MGI_GFSK->XCVR_CTRL = 1; // TX_START_NOW
}
void RF_Generic_IRQHandler(void);
void RF_Generic_IRQHandler(void) {
uint32_t status = MGI_GFSK->IRQ_CTRL;
if (!(status & MG_BIT(0))) return;
mg_mcxw71_abort();
if (s_mcxw71_txing) {
s_mcxw71_txing = false;
s_mcxw71_done = true;
} else if ((status & (MG_BIT(2) | MG_BIT(10))) ==
(MG_BIT(2) | MG_BIT(10))) { // RX and CRC valid
uint8_t frame[MGI_RADIO_MAX];
size_t i, n = MGI_RX_PB[1];
if (n > 0 && n <= sizeof(frame)) {
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i + 2];
mg_radio_rx_queue(s_mcxw71_ifp, frame, n);
}
}
mg_mcxw71_listen();
}
static bool mgi_mcxw71_radio_init(struct mg_tcpip_if *ifp, uint16_t addr) {
s_mcxw71_ifp = ifp;
(void) addr; // Frames are filtered by mg_radio_rx_queue()
MGI_GFSK->IRQ_CTRL = 0xffffU;
mg_mcxw71_abort();
// GENFSK channels are MHz above 2360; MG_RADIO_CHANNEL is a BLE data index.
MGI_GFSK->CHANNEL_NUM0 = MG_RADIO_CHANNEL < 11 ? 44 + 2 * MG_RADIO_CHANNEL
: 46 + 2 * MG_RADIO_CHANNEL;
MGI_GFSK->TX_POWER = 0x3f;
MGI_GFSK->NTW_ADR_CTRL = 0x301; // 32-bit access address, match address 0
MGI_GFSK->NTW_ADR[0] = MG_RADIO_NETWORK;
MGI_GFSK->PACKET_CFG = 0x000800c8; // H0=8 bits, length=8, AA=4 bytes
MGI_GFSK->H0_CFG = MGI_GFSK->H1_CFG = 0; // No header filtering
MGI_GFSK->LENGTH_ADJ = 3; // Length excludes the three CRC bytes
MGI_GFSK->MULT_PKT_CTRL = 0;
MGI_GFSK->LENGTH_MAX = 16U << 16; // 256 bytes; software caps at 251
MGI_GFSK->ENH_FEATURE = 9 | MG_BIT(6); // BLE uncoded, alternate rate: 2M
MGI_GFSK->XCVR_CFG = MGI_GFSK_CFG;
mg_mcxw71_listen();
return true;
}
#else
#define MGI_ZLL ((struct mg_zll *) (MGI_RF_BASE + 0x1000))
#define MGI_RADIO_MAX 125
// ZLL, 802.15.4 link layer. See MCXW71 reference manual and NXP
// ieee_802_15_4 PHY sources: PhyPacketProcessor.c, PhyPlmeData.c, PhyISR.c
struct mg_zll {
@@ -20,16 +140,6 @@ struct mg_zll {
COEX_PRIORITY, ENHACK_CTRL0;
};
// TrustZone: code built for the secure state (-mcmse) uses the secure alias
// of the radio registers, 0x5xxxxxxx. Otherwise, the non-secure alias
#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE & 2)
#define MGI_ZLL ((struct mg_zll *) 0x58A01000)
#define MGI_PKT_RAM ((volatile uint8_t *) 0x58A08000)
#else
#define MGI_ZLL ((struct mg_zll *) 0x48A01000)
#define MGI_PKT_RAM ((volatile uint8_t *) 0x48A08000)
#endif
#define MGI_TX_PB MGI_PKT_RAM // Length byte (incl. FCS), then frame
#define MGI_RX_PB (MGI_PKT_RAM + 0x80) // Received frame, no length byte
@@ -42,9 +152,6 @@ struct mg_zll {
#define MGI_PHY_CTRL_ALL_IRQ_MASKS 0x8007ff00U // Bits 8..18, 31: mask all IRQs
#define MGI_PHY_CTRL_SEQMSK MG_BIT(8) // Mask the sequence end IRQ
#define MGI_PHY_CTRL_TRCV_MSK MG_BIT(31) // Mask all transceiver IRQs
#define MGI_IRQN 53 // RF_802_15_4_IRQn
#define MGI_NVIC_ISER ((volatile uint32_t *) 0xE000E100) // Enable IRQ
#define MGI_NVIC_ICER ((volatile uint32_t *) 0xE000E180) // Disable IRQ
#define MGI_SEQ_STATE_MASK 0x1fU
#define MGI_SEQ_STATE_RX_WU 0x10U // RX warm up
#define MGI_SEQ_CTRL_NO_RX_RECYCLE MG_BIT(5)
@@ -53,12 +160,6 @@ struct mg_zll {
// PHY_CTRL[XCVSEQ]
enum { MGI_SEQ_IDLE = 0, MGI_SEQ_RX = 1, MGI_SEQ_TX = 2 };
static struct mg_tcpip_if *s_mcxw71_ifp;
static struct mg_queue s_mcxw71_rx;
static char s_mcxw71_queue[2048];
static volatile bool s_mcxw71_txing, s_mcxw71_done;
static uint64_t s_mcxw71_sendstart;
// Stop the current sequence, wait for the sequencer to go idle. See PhyAbort()
static void mg_mcxw71_abort(void) {
if (MGI_ZLL->PHY_CTRL & MGI_PHY_CTRL_XCVSEQ) {
@@ -78,13 +179,6 @@ static void mg_mcxw71_listen(void) {
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
}
// Keep the IRQ handler out while we change the radio. The NVIC disable is
// atomic, so PHY_CTRL updates here and there do not race
static void mg_mcxw71_lock(void) {
MGI_NVIC_ICER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
__asm volatile("dsb; isb" ::: "memory");
}
// Start the TX sequence for the loaded frame. Its end: see the IRQ handler
static void mg_mcxw71_go(void) {
MGI_ZLL->PHY_CTRL |= MGI_PHY_CTRL_TRCV_MSK | MGI_PHY_CTRL_SEQMSK;
@@ -110,22 +204,49 @@ void RF_802_15_4_IRQHandler(void) {
n = n >= 2 ? n - 2 : 0;
if (n > 0 && n <= sizeof(frame)) {
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i];
mg_mesh_rx_queue(&s_mcxw71_rx, s_mcxw71_ifp, frame, n);
mg_radio_rx_queue(s_mcxw71_ifp, frame, n);
}
}
mg_mcxw71_listen();
}
}
static bool mgi_mcxw71_radio_init(struct mg_tcpip_if *ifp, uint16_t addr) {
s_mcxw71_ifp = ifp;
MGI_ZLL->SEQ_CTRL_STS |= MGI_SEQ_CTRL_NO_RX_RECYCLE;
MGI_ZLL->PHY_CTRL = MGI_PHY_CTRL_ALL_IRQ_MASKS;
MGI_ZLL->IRQSTS = MGI_ZLL->IRQSTS;
MGI_ZLL->SAM_CTRL = 0;
MGI_ZLL->RX_FRAME_FILTER = MGI_FRAME_FILTER;
MGI_ZLL->CHANNEL_NUM0 = MG_RADIO_CHANNEL;
MGI_ZLL->MACSHORTADDRS0 = ((uint32_t) addr << 16) | MG_RADIO_NETWORK;
MGI_ZLL->PA_PWR = 0x3f;
mg_mcxw71_listen();
return true;
}
#endif
static size_t mgi_mcxw71_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
size_t len) {
size_t i;
(void) ifp;
if (len > 125 || s_mcxw71_txing) return 0;
if (len > MGI_RADIO_MAX || s_mcxw71_txing) return 0;
mg_mcxw71_lock();
#if MG_RADIO == MG_RADIO_BLE
if (MGI_GFSK->SEQ_STS & MG_BIT(11)) { // Do not interrupt a received frame
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
return 0;
}
for (i = 0; i < 4; i++)
MGI_TX_PB[i] = (uint8_t) (MG_RADIO_NETWORK >> (8 * i));
MGI_TX_PB[4] = 0, MGI_TX_PB[5] = (uint8_t) len;
for (i = 0; i < len; i++) MGI_TX_PB[i + 6] = ((const uint8_t *) buf)[i];
#else
MGI_TX_PB[0] = (uint8_t) (len + 2); // FCS is added by hardware
for (i = 0; i < len; i++)
MGI_TX_PB[i + 1] = ((const uint8_t *) buf)[i]; // 8-bit access only
#endif
s_mcxw71_done = false;
s_mcxw71_txing = true;
s_mcxw71_sendstart = mg_millis();
@@ -134,46 +255,11 @@ static size_t mgi_mcxw71_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
return len;
}
static bool mgi_mcxw71_radio_init(struct mg_tcpip_if *ifp) {
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh;
s_mcxw71_ifp = ifp;
MGI_ZLL->SEQ_CTRL_STS |= MGI_SEQ_CTRL_NO_RX_RECYCLE;
MGI_ZLL->PHY_CTRL = MGI_PHY_CTRL_ALL_IRQ_MASKS;
MGI_ZLL->IRQSTS = MGI_ZLL->IRQSTS;
MGI_ZLL->SAM_CTRL = 0;
MGI_ZLL->RX_FRAME_FILTER = MGI_FRAME_FILTER;
MGI_ZLL->CHANNEL_NUM0 = MG_802154_CHANNEL;
MGI_ZLL->MACSHORTADDRS0 =
((uint32_t) node->radio_address << 16) | MG_802154_PAN_ID;
MGI_ZLL->PA_PWR = 0x3f;
mg_mcxw71_listen();
return true;
}
static size_t mgi_mcxw71_tx(const void *buf, size_t len,
struct mg_tcpip_if *ifp) {
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
return data->mesh->strategy->tx(data->mesh, buf, len);
}
static size_t mgi_mcxw71_rx(void *buf, size_t cap, struct mg_tcpip_if *ifp) {
struct mg_tcpip_driver_mcxw71_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_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
struct mg_mesh_node *node = data->mesh;
uint64_t now = mg_micros();
(void) s1;
static bool mgi_mcxw71_radio_poll(struct mg_tcpip_if *ifp) {
bool done;
(void) ifp;
if (s_mcxw71_txing &&
mg_millis() > s_mcxw71_sendstart + MG_MESH_TX_TIMEOUT_US / 1000) {
mg_millis() > s_mcxw71_sendstart + MG_RADIO_TX_TIMEOUT_US / 1000) {
mg_mcxw71_lock();
if (s_mcxw71_txing) {
mg_mcxw71_abort();
@@ -184,40 +270,16 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
}
}
if (s_mcxw71_done) {
s_mcxw71_done = false;
node->tx_at = now + node->strategy->radio->ifs_us;
}
node->now = now;
mg_mesh_rx_drain(node, &s_mcxw71_rx);
node->strategy->tick(node, now);
return node->joined;
done = s_mcxw71_done;
s_mcxw71_done = false;
return done;
}
static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
const struct mg_mesh *strategy = data->strategy;
uint16_t address;
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_mcxw71_radio_tx;
mg_queue_init(&s_mcxw71_rx, s_mcxw71_queue, sizeof(s_mcxw71_queue));
if (mgi_mcxw71_radio_init(ifp)) return true;
strategy->free(data->mesh);
mg_free(data->mesh);
data->mesh = NULL;
return false;
return mg_radio_init(ifp, mgi_mcxw71_radio_init, mgi_mcxw71_radio_poll,
mgi_mcxw71_radio_tx);
}
struct mg_tcpip_driver mg_tcpip_driver_mcxw71 = {
mgi_mcxw71_init, mgi_mcxw71_tx, mgi_mcxw71_rx, mgi_mcxw71_poll};
struct mg_tcpip_driver mg_tcpip_driver_mcxw71 = {mgi_mcxw71_init, mg_radio_tx,
mg_radio_rx, mg_radio_poll};
#endif