BLE radio support

This commit is contained in:
cpq committed 2026-11-25 13:58:07 +00:00
1 parent a8d6573ccc
commit 76fbf3c713
82 files changed
+7047 -4893

No files matched your search

+132 -217
View File
@@ -1,46 +1,36 @@
#include "net_builtin.h"
#include "drivers/usb.h"
#include "drivers/usb_rp.h"
#include "net_builtin.h"
#include "util.h"
#if MG_ENABLE_USB && MG_ENABLE_DRIVER_USB_RP
#if (MG_ENABLE_TCPIP || !MG_ENABLE_USB_ETHERNET) && MG_ENABLE_DRIVER_USB_RP && \
(MG_ENABLE_USB_ETHERNET || MG_ENABLE_USB_SERIAL)
// RP2040 / RP2350 USB device controller: single core, polled, full speed,
// 64-byte single-buffered endpoints in USB DPRAM. Longer IN transfers go
// out one packet at a time, the next one when the previous completes
// 64-byte single-buffered endpoints in USB DPRAM
struct rp_ep {
uint8_t *buf; // Endpoint buffer in USB DPRAM
io_rw_32 *ctrl; // Buffer control register
const uint8_t *data; // IN transfer: bytes still to send
size_t left; // IN transfer: how many
unsigned packets; // IN transfer: packets still to send
uint8_t pid; // Next DATA PID, 0 or 1
bool busy; // Buffer handed to the controller
// usb.c's event handler, see mg_usb_hw::poll()
#define MGI_USB_RP_FN int (*fn)(enum mg_usb_ev, enum mg_usb_ep, void *, size_t)
struct mgi_usb_rp_ep {
uint8_t *buf; // Endpoint buffer in USB DPRAM
io_rw_32 *ctrl; // Buffer control register
uint8_t pid; // Next DATA PID, 0 or 1
};
static struct rp_ep s_ep[5][2]; // Index: endpoint number, OUT=0 IN=1
static struct mg_usb s_usb;
static uint8_t s_req[16]; // SETUP packet, then its OUT data stage
static bool s_req_out; // Waiting for that OUT data stage
static uint8_t s_address; // Address to apply after the status stage
static char s_serial[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
#if MG_ENABLE_USB_ETHERNET
static uint8_t s_rx[MG_USB_NTB_SIZE], s_tx[MG_USB_NTB_SIZE];
static size_t s_rxlen; // Received part of the OUT transfer block
static bool s_discard; // OUT transfer block too long: drop it
#endif
#if MG_ENABLE_USB_SERIAL
static char s_log[64]; // Log output in flight
#endif
static struct mgi_usb_rp_ep s_mgi_usb_rp_ep[5][2]; // Endpoint, OUT=0 IN=1
static uint8_t s_mgi_usb_rp_req[16]; // SETUP packet, then its OUT data
static bool s_mgi_usb_rp_req_out; // Waiting for that OUT data stage
static uint8_t s_mgi_usb_rp_address; // Address to set after the status stage
// Hand one packet buffer to the controller. OUT: accept up to len bytes
static void packet(unsigned ep, bool in, const void *data, unsigned len) {
struct rp_ep *e = &s_ep[ep][in];
static void mgi_usb_rp_packet(unsigned ep, bool in, const void *data,
unsigned len) {
struct mgi_usb_rp_ep *e = &s_mgi_usb_rp_ep[ep][in];
uint32_t v = len | (e->pid ? USB_BUF_CTRL_DATA1_PID : 0) |
(in ? USB_BUF_CTRL_FULL : 0);
if (in && len) memcpy(e->buf, data, len);
e->pid ^= 1;
e->busy = true;
*e->ctrl = v;
// RP2040 datasheet: allow at least 12 cycles between writing buffer
// metadata and handing ownership to the controller through AVAIL
@@ -48,36 +38,23 @@ static void packet(unsigned ep, bool in, const void *data, unsigned len) {
*e->ctrl = v | USB_BUF_CTRL_AVAIL;
}
// Send the next packet of the IN transfer on ep. False if it is over
static bool in_next(unsigned ep) {
struct rp_ep *e = &s_ep[ep][1];
unsigned n = e->left > 64 ? 64 : (unsigned) e->left;
if (e->packets == 0) return false;
packet(ep, true, e->data, n);
e->data += n, e->left -= n, e->packets--;
return true;
}
// Start an IN transfer. zlp: end a multiple of 64 bytes with an empty packet
static void in_start(unsigned ep, const void *data, size_t len, bool zlp) {
struct rp_ep *e = &s_ep[ep][1];
e->data = (const uint8_t *) data, e->left = len;
e->packets = (unsigned) (len / 64 + (len % 64 || len == 0 || zlp));
in_next(ep);
// mg_usb_hw::in()
static void mgi_usb_rp_in(enum mg_usb_ep ep, const void *buf, size_t len) {
mgi_usb_rp_packet(ep, true, buf, (unsigned) len);
}
// Set up endpoint ep, direction in, idle. Endpoints 1 and 3 are interrupt,
// 2 and 4 bulk, each with a 64-byte DPRAM buffer of its own
static void ep_init(unsigned ep, bool in) {
struct rp_ep *e = &s_ep[ep][in];
e->ctrl = in ? &usb_dpram->ep_buf_ctrl[ep].in
: &usb_dpram->ep_buf_ctrl[ep].out;
static void mgi_usb_rp_ep_init(unsigned ep, bool in) {
struct mgi_usb_rp_ep *e = &s_mgi_usb_rp_ep[ep][in];
e->ctrl =
in ? &usb_dpram->ep_buf_ctrl[ep].in : &usb_dpram->ep_buf_ctrl[ep].out;
e->buf = ep ? &usb_dpram->epx_data[(ep * 2 + in) * 64] : usb_dpram->ep0_buf_a;
e->pid = 0, e->busy = false, e->packets = 0;
e->pid = 0;
*e->ctrl = 0;
if (ep) {
io_rw_32 *ctrl = in ? &usb_dpram->ep_ctrl[ep - 1].in
: &usb_dpram->ep_ctrl[ep - 1].out;
io_rw_32 *ctrl =
in ? &usb_dpram->ep_ctrl[ep - 1].in : &usb_dpram->ep_ctrl[ep - 1].out;
*ctrl = EP_CTRL_ENABLE_BITS | EP_CTRL_INTERRUPT_PER_BUFFER |
((ep & 1 ? 3U : 2U) << EP_CTRL_BUFFER_TYPE_LSB) |
(uint32_t) ((uintptr_t) e->buf - (uintptr_t) usb_dpram);
@@ -85,152 +62,150 @@ static void ep_init(unsigned ep, bool in) {
}
#if MG_ENABLE_USB_ETHERNET
// Ethernet data endpoints idle, then armed to receive if Ethernet is open
static void data_reset(void) {
ep_init(2, false), ep_init(2, true);
s_rxlen = 0, s_discard = false;
if (s_usb.alt) packet(2, false, NULL, 64);
}
// A packet on the Ethernet OUT endpoint: add it to the transfer block
static void rx(const uint8_t *buf, unsigned n) {
if (s_rxlen + n <= sizeof(s_rx) && !s_discard) {
memcpy(s_rx + s_rxlen, buf, n);
s_rxlen += n;
} else {
s_discard = true;
}
// At the advertised maximum OUT size, the host need not send a ZLP.
// Other transfers end with a short packet (including a ZLP)
if (n < 64 || s_rxlen == sizeof(s_rx)) {
if (!s_discard) mg_usb_decode(&s_usb, s_rx, s_rxlen);
s_rxlen = 0;
s_discard = false;
}
if (s_usb.alt) packet(2, false, NULL, 64);
// Ethernet data endpoints idle, OUT armed to receive if Ethernet is open
static void mgi_usb_rp_data_reset(bool open) {
mgi_usb_rp_ep_init(2, false), mgi_usb_rp_ep_init(2, true);
if (open) mgi_usb_rp_packet(2, false, NULL, 64);
}
#endif
// All endpoints idle, then the serial port armed to receive if configured
static void endpoints_reset(void) {
static const uint8_t eps[] = {
0x00, 0x80,
// Endpoints 1-4 idle. If configured, the serial port armed to receive
static void mgi_usb_rp_endpoints(bool on) {
#if MG_ENABLE_USB_ETHERNET
0x81,
mgi_usb_rp_ep_init(1, true);
mgi_usb_rp_data_reset(false);
#endif
#if MG_ENABLE_USB_SERIAL
0x83, 0x04, 0x84,
#endif
};
size_t i;
for (i = 0; i < sizeof(eps); i++) ep_init(eps[i] & 15U, eps[i] >> 7);
#if MG_ENABLE_USB_ETHERNET
data_reset();
#endif
#if MG_ENABLE_USB_SERIAL
if (s_usb.configured) packet(4, false, NULL, 64);
mgi_usb_rp_ep_init(3, true);
mgi_usb_rp_ep_init(4, false), mgi_usb_rp_ep_init(4, true);
if (on) mgi_usb_rp_packet(4, false, NULL, 64);
#endif
(void) on;
}
// Stall endpoint 0 until the next SETUP
static void stall(void) {
static void mgi_usb_rp_stall(void) {
usb_hw->ep_stall_arm =
USB_EP_STALL_ARM_EP0_IN_BITS | USB_EP_STALL_ARM_EP0_OUT_BITS;
*s_ep[0][1].ctrl = USB_BUF_CTRL_STALL;
*s_ep[0][0].ctrl = USB_BUF_CTRL_STALL;
*s_mgi_usb_rp_ep[0][1].ctrl = USB_BUF_CTRL_STALL;
*s_mgi_usb_rp_ep[0][0].ctrl = USB_BUF_CTRL_STALL;
}
// Run a complete control request: SETUP in s_req, then any OUT data
static void request(void) {
const void *reply;
int n = mg_usb_setup(&s_usb, s_req, &reply);
if (n < 0) {
stall();
// A complete control request: usb.c answers it, then the controller follows
static void mgi_usb_rp_request(MGI_USB_RP_FN) {
uint8_t *r = s_mgi_usb_rp_req;
size_t out = (r[0] & 0x80) ? 0 : MG_LOAD_LE16(r + 6); // OUT data stage
int res = fn(MG_USB_EV_SETUP, MG_USB_EP_CTL, r, 8 + out);
if (res < 0) {
mgi_usb_rp_stall();
return;
}
switch ((s_req[0] << 8) | s_req[1]) {
case 0x0005: // SET_ADDRESS, applied after the status stage
s_address = s_req[2];
switch ((r[0] << 8) | r[1]) {
case 0x0005: // SET_ADDRESS, set after the status stage
s_mgi_usb_rp_address = r[2];
break;
case 0x0009: // SET_CONFIGURATION
endpoints_reset();
mgi_usb_rp_endpoints(r[2] == 1);
break;
#if MG_ENABLE_USB_ETHERNET
case 0x010b: // SET_INTERFACE
if (s_req[4] == 1) data_reset();
if (r[4] == MG_USB_IF_ETH_DATA) mgi_usb_rp_data_reset(r[2] == 1);
break;
#endif
case 0x0201: { // CLEAR_FEATURE(ENDPOINT_HALT)
struct rp_ep *e = &s_ep[s_req[4] & 15][s_req[4] >> 7];
struct mgi_usb_rp_ep *e = &s_mgi_usb_rp_ep[r[4] & 15][r[4] >> 7];
*e->ctrl &= ~USB_BUF_CTRL_STALL, e->pid = 0;
break;
}
}
// IN data stage, or the status stage, both starting with DATA1
s_ep[0][1].pid = 1;
in_start(0, reply, (size_t) n, n < (int) MG_LOAD_LE16(s_req + 6));
if (res == 0) mgi_usb_rp_packet(0, true, NULL, 0); // Status stage, DATA1
}
// A SETUP packet: run it, or first receive its OUT data stage
static void setup(void) {
static void mgi_usb_rp_setup(MGI_USB_RP_FN) {
uint8_t *r = s_mgi_usb_rp_req;
unsigned wlen;
memcpy(s_req, (const void *) usb_dpram->setup_packet, 8);
wlen = MG_LOAD_LE16(s_req + 6);
// A SETUP cancels the preceding control transfer. OUT stages use DATA1
ep_init(0, false), ep_init(0, true);
s_ep[0][0].pid = 1;
s_address = 0, s_req_out = false;
if ((s_req[0] & 0x80) || wlen == 0) {
request();
} else if (wlen > sizeof(s_req) - 8) {
stall();
memcpy(r, (const void *) usb_dpram->setup_packet, 8);
wlen = MG_LOAD_LE16(r + 6);
// A SETUP cancels the preceding control transfer. Both its data and
// status stages start with DATA1
mgi_usb_rp_ep_init(0, false), mgi_usb_rp_ep_init(0, true);
s_mgi_usb_rp_ep[0][0].pid = s_mgi_usb_rp_ep[0][1].pid = 1;
s_mgi_usb_rp_address = 0, s_mgi_usb_rp_req_out = false;
if ((r[0] & 0x80) || wlen == 0) {
mgi_usb_rp_request(fn);
} else if (wlen > sizeof(s_mgi_usb_rp_req) - 8) {
mgi_usb_rp_stall();
} else {
s_req_out = true;
packet(0, false, NULL, 64);
s_mgi_usb_rp_req_out = true;
mgi_usb_rp_packet(0, false, NULL, 64);
}
}
// A buffer the controller has finished with
static void complete(unsigned ep, bool in) {
struct rp_ep *e = &s_ep[ep][in];
static void mgi_usb_rp_complete(unsigned ep, bool in, MGI_USB_RP_FN) {
struct mgi_usb_rp_ep *e = &s_mgi_usb_rp_ep[ep][in];
uint8_t *r = s_mgi_usb_rp_req;
unsigned n = *e->ctrl & USB_BUF_CTRL_LEN_MASK;
e->busy = false;
if (in && in_next(ep)) return;
if (in && fn(MG_USB_EV_IN, (enum mg_usb_ep) ep, NULL, 0)) return;
if (ep == 0 && in) { // Control IN data or status stage sent
if (s_address) {
usb_hw->dev_addr_ctrl = s_address, s_address = 0;
} else if ((s_req[0] & 0x80) && MG_LOAD_LE16(s_req + 6)) {
packet(0, false, NULL, 64); // Host's OUT status stage
if (s_mgi_usb_rp_address) {
usb_hw->dev_addr_ctrl = s_mgi_usb_rp_address, s_mgi_usb_rp_address = 0;
} else if ((r[0] & 0x80) && MG_LOAD_LE16(r + 6)) {
mgi_usb_rp_packet(0, false, NULL, 64); // Host's OUT status stage
}
} else if (ep == 0 && !in && s_req_out) { // Control OUT data stage
s_req_out = false;
if (n != MG_LOAD_LE16(s_req + 6)) {
stall();
} else if (ep == 0 && !in && s_mgi_usb_rp_req_out) { // OUT data stage
s_mgi_usb_rp_req_out = false;
if (n != MG_LOAD_LE16(r + 6)) {
mgi_usb_rp_stall();
} else {
memcpy(s_req + 8, e->buf, n);
request();
memcpy(r + 8, e->buf, n);
mgi_usb_rp_request(fn);
}
#if MG_ENABLE_USB_ETHERNET
} else if (ep == 2 && !in) {
rx(e->buf, n);
} else if (ep == MG_USB_EP_ETH_DATA && !in) {
fn(MG_USB_EV_OUT, MG_USB_EP_ETH_DATA, e->buf, n);
mgi_usb_rp_packet(ep, false, NULL, 64);
#endif
#if MG_ENABLE_USB_SERIAL
} else if (ep == 4 && !in) {
packet(4, false, NULL, 64); // Serial input is discarded: log console
} else if (ep == MG_USB_EP_SER_DATA && !in) {
mgi_usb_rp_packet(ep, false, NULL, 64); // Serial input: dropped
#endif
}
}
void mg_usb_init(void) {
static bool started;
if (started) return;
started = true;
pico_get_unique_board_id_string(s_serial, sizeof(s_serial));
s_usb.serial = s_serial;
mg_usb_reset(&s_usb);
#if MG_ENABLE_USB_SERIAL
mg_log_set_fn(mg_usb_log, &s_usb); // Log to the USB serial port
#endif
// mg_usb_hw::poll()
static void mgi_usb_rp_poll(MGI_USB_RP_FN) {
uint32_t status = usb_hw->intr, done;
unsigned i;
if (status & USB_INTR_BUS_RESET_BITS) {
usb_hw->sie_status = USB_SIE_STATUS_BUS_RESET_BITS;
usb_hw->dev_addr_ctrl = 0;
usb_hw->buf_status = 0xffffffff;
fn(MG_USB_EV_RESET, MG_USB_EP_CTL, NULL, 0);
mgi_usb_rp_ep_init(0, false), mgi_usb_rp_ep_init(0, true);
mgi_usb_rp_endpoints(false);
return;
}
if (status & USB_INTR_SETUP_REQ_BITS) {
// Discard stale EP0 completions from an interrupted control transfer
usb_hw->buf_status = 3;
usb_hw->sie_status = USB_SIE_STATUS_SETUP_REC_BITS;
mgi_usb_rp_setup(fn);
}
done = usb_hw->buf_status;
for (i = 0; i < 10; i++) { // Bits: EP0 IN, EP0 OUT, EP1 IN, ...
if (!(done & (1U << i))) continue;
usb_hw->buf_status = 1U << i;
if (s_mgi_usb_rp_ep[i / 2][!(i & 1)].ctrl) {
mgi_usb_rp_complete(i / 2, !(i & 1), fn);
}
}
}
// mg_usb_hw::init(): the serial number, then the controller, attached
static void mgi_usb_rp_start(char serial[17]) {
pico_get_unique_board_id_string(serial, 17);
reset_block(RESETS_RESET_USBCTRL_BITS);
unreset_block_wait(RESETS_RESET_USBCTRL_BITS);
memset(usb_dpram, 0, sizeof(*usb_dpram));
@@ -240,81 +215,21 @@ void mg_usb_init(void) {
usb_hw->main_ctrl = USB_MAIN_CTRL_CONTROLLER_EN_BITS;
usb_hw->sie_ctrl = USB_SIE_CTRL_EP0_INT_1BUF_BITS;
usb_hw->inte = 0; // Poll raw status instead of an interrupt handler
endpoints_reset();
mgi_usb_rp_ep_init(0, false), mgi_usb_rp_ep_init(0, true);
mgi_usb_rp_endpoints(false);
hw_set_bits(&usb_hw->sie_ctrl, USB_SIE_CTRL_PULLUP_EN_BITS);
}
void mg_usb_poll(void) {
uint32_t status, done;
unsigned i;
mg_usb_init();
status = usb_hw->intr;
#if MG_ENABLE_USB_SERIAL
if (s_usb.reboot && mg_millis() > s_usb.reboot) reset_usb_boot(0, 0);
#endif
if (status & USB_INTR_BUS_RESET_BITS) {
usb_hw->sie_status = USB_SIE_STATUS_BUS_RESET_BITS;
usb_hw->dev_addr_ctrl = 0;
usb_hw->buf_status = 0xffffffff;
mg_usb_reset(&s_usb);
endpoints_reset();
return;
}
if (status & USB_INTR_SETUP_REQ_BITS) {
// Discard stale EP0 completions from an interrupted control transfer
usb_hw->buf_status = 3;
usb_hw->sie_status = USB_SIE_STATUS_SETUP_REC_BITS;
setup();
}
done = usb_hw->buf_status;
for (i = 0; i < 10; i++) { // Bits: EP0 IN, EP0 OUT, EP1 IN, ...
if (!(done & (1U << i))) continue;
usb_hw->buf_status = 1U << i;
if (s_ep[i / 2][!(i & 1)].ctrl) complete(i / 2, !(i & 1));
}
#if MG_ENABLE_USB_ETHERNET
if (!s_ep[1][1].busy) {
const void *msg;
size_t n = mg_usb_notify(&s_usb, &msg);
if (n) in_start(1, msg, n, false);
}
#endif
#if MG_ENABLE_USB_SERIAL
if (s_usb.configured && s_usb.dtr && !s_ep[4][1].busy) {
size_t n = mg_usb_log_read(&s_usb, s_log, sizeof(s_log));
if (n) in_start(4, s_log, n, true);
}
#endif
}
const struct mg_usb_hw mg_usb_hw_rp = {mgi_usb_rp_start, mgi_usb_rp_poll,
mgi_usb_rp_in};
#if MG_ENABLE_USB_ETHERNET
static bool mg_tcpip_driver_usb_rp_init(struct mg_tcpip_if *ifp) {
s_usb.ifp = ifp; // mg_tcpip_init() has started USB already
return true;
static bool mgi_usb_rp_init(struct mg_tcpip_if *ifp) {
return mg_usb_init(ifp, &mg_usb_hw_rp);
}
static size_t mg_tcpip_driver_usb_rp_tx(const void *buf, size_t len,
struct mg_tcpip_if *ifp) {
// One frame can be in flight. Keep USB moving while waiting, but bound
// the wait if the host stops reading or the cable is gone
uint64_t until = mg_millis() + 20;
size_t n = 0;
while (s_ep[2][1].busy && mg_millis() < until) mg_usb_poll();
if (!s_ep[2][1].busy) n = mg_usb_encode(&s_usb, s_tx, buf, len);
if (n) in_start(2, s_tx, n, n < s_usb.input_size);
(void) ifp;
return n ? len : 0;
}
// USB itself runs from mg_mgr_poll(). Report the link: Ethernet open
static bool mg_tcpip_driver_usb_rp_poll(struct mg_tcpip_if *ifp, bool s1) {
(void) ifp;
return s1 && s_usb.alt && s_usb.filter;
}
struct mg_tcpip_driver mg_tcpip_driver_usb_rp = {
mg_tcpip_driver_usb_rp_init, mg_tcpip_driver_usb_rp_tx, NULL,
mg_tcpip_driver_usb_rp_poll};
struct mg_tcpip_driver mg_tcpip_driver_usb_rp = {mgi_usb_rp_init, mg_usb_tx,
NULL, mg_usb_poll};
#endif
#endif