mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-25 20:28:27 +00:00
Add USB NCM drivers
This commit is contained in:
1 parent
bae9e53f00
commit
a8d6573ccc
31 files changed
+3057
-175
No files matched your search
@@ -3,7 +3,8 @@
|
||||
MGDIR = ../../../..
|
||||
|
||||
# Flash over USB: the board's Adafruit UF2 bootloader does serial DFU. The
|
||||
# firmware resets into it on a 1200 baud touch, see usb.c. PORT: the board's
|
||||
# firmware resets into it on a 1200 baud touch of its USB serial port, see
|
||||
# Mongoose's src/drivers/usb_nrf52.c. PORT: the board's
|
||||
# USB serial port. adafruit-nrfutil: pip install adafruit-nrfutil
|
||||
PORT ?= /dev/cu.usbmodem11301
|
||||
NRFUTIL ?= adafruit-nrfutil
|
||||
@@ -12,31 +13,25 @@ CFLAGS = -W -Wall -Wextra -Werror -Wshadow -Wdouble-promotion
|
||||
CFLAGS += -Wformat-truncation -fno-common -Wno-conversion -Wno-sign-conversion
|
||||
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections
|
||||
CFLAGS += -I. -I$(MGDIR) -Icmsis_core/CMSIS/Core/Include
|
||||
CFLAGS += -Inrfx/mdk -Inrfx -Inrfx/hal -Inrfx/templates -Inrfx/drivers/src -Itinyusb/src
|
||||
CFLAGS += -Wno-cast-align -Wno-shadow -Wno-unused-parameter
|
||||
CFLAGS += -Inrfx/mdk
|
||||
CFLAGS += -Wno-shadow # src/drivers/nrf52.c, RADIO_IRQHandler(): dst
|
||||
CFLAGS += -DNRF52840_XXAA -D__STARTUP_CLEAR_BSS -D__START=main -D__HEAP_SIZE=0
|
||||
CFLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
|
||||
CFLAGS += $(CFLAGS_EXTRA)
|
||||
|
||||
LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs=nano.specs -lc -lm -lgcc -Wl,--gc-sections
|
||||
|
||||
SOURCES = main.c hal.c usb.c $(MGDIR)/mongoose.c
|
||||
SOURCES = main.c hal.c $(MGDIR)/mongoose.c
|
||||
SOURCES += $(MGDIR)/tutorials/mqtt/mqtt-client/mongoose_mqtt.c
|
||||
SOURCES += nrfx/mdk/system_nrf52840.c nrfx/mdk/gcc_startup_nrf52840.S
|
||||
|
||||
# TinyUSB, for the USB serial console: the board has no USB-UART chip
|
||||
SOURCES += tinyusb/src/tusb.c tinyusb/src/common/tusb_fifo.c
|
||||
SOURCES += tinyusb/src/device/usbd.c tinyusb/src/device/usbd_control.c
|
||||
SOURCES += tinyusb/src/class/cdc/cdc_device.c
|
||||
SOURCES += tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c
|
||||
|
||||
all build: firmware.hex
|
||||
|
||||
firmware.hex: firmware.elf
|
||||
arm-none-eabi-objcopy -O ihex $< $@
|
||||
arm-none-eabi-size --format=berkeley $<
|
||||
|
||||
firmware.elf: cmsis_core nrfx tinyusb $(SOURCES) $(wildcard *.h) link.ld Makefile
|
||||
firmware.elf: cmsis_core nrfx $(SOURCES) $(wildcard *.h) link.ld Makefile
|
||||
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
firmware.zip: firmware.hex
|
||||
@@ -48,14 +43,12 @@ flash: firmware.zip
|
||||
cmsis_core:
|
||||
git clone -q -c advice.detachedHead=false --depth 1 -b 5.9.0 https://github.com/ARM-software/CMSIS_5 $@
|
||||
|
||||
# nRF52840 device headers and SystemInit(), nrf_* register helpers for TinyUSB
|
||||
# nRF52840 device headers, SystemInit() and the startup code
|
||||
nrfx:
|
||||
git clone -q -c advice.detachedHead=false --depth 1 -b v2.11.0 https://github.com/NordicSemiconductor/nrfx $@
|
||||
|
||||
tinyusb:
|
||||
git clone -q -c advice.detachedHead=false --depth 1 -b 0.14.0 https://github.com/hathach/tinyusb $@
|
||||
|
||||
clean:
|
||||
rm -rf firmware.* *.dSYM tinyusb nrfx cmsis_core
|
||||
rm -rf firmware.* *.dSYM nrfx cmsis_core
|
||||
|
||||
.PHONY: all build flash clean
|
||||
@@ -7,9 +7,6 @@ uint64_t hal_get_tick(void);
|
||||
bool hal_timer_expired(volatile uint64_t *t, uint64_t period, uint64_t now);
|
||||
size_t hal_ram_used(void);
|
||||
size_t hal_ram_free(void);
|
||||
void usb_init(void);
|
||||
void usb_poll(void);
|
||||
void usb_log(char ch, void *param);
|
||||
|
||||
#define BIT(x) (1UL << (x))
|
||||
#define PIN(port, num) (((port) << 5) | (num))
|
||||
|
||||
@@ -6,10 +6,6 @@
|
||||
|
||||
#define LED1 PIN(0, 26) // P0.26, user LED, active low
|
||||
|
||||
static void log_fn(char ch, void *param) {
|
||||
usb_log(ch, param);
|
||||
}
|
||||
|
||||
static void blink_task(void) {
|
||||
static uint64_t blink_timer = 0;
|
||||
if (hal_timer_expired(&blink_timer, 500, hal_get_tick())) {
|
||||
@@ -75,14 +71,12 @@ int main(void) {
|
||||
MG_HEALTH_INIT();
|
||||
|
||||
hal_system_init();
|
||||
usb_init();
|
||||
mg_log_set_fn(log_fn, NULL);
|
||||
hal_rng_init();
|
||||
hal_gpio_write(LED1, true);
|
||||
hal_gpio_output(LED1);
|
||||
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
mg_mgr_init(&mgr); // Also starts the radio and the USB log console
|
||||
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);
|
||||
@@ -92,7 +86,6 @@ int main(void) {
|
||||
MG_INFO(("Crash record: %M", mg_print_crash_record));
|
||||
|
||||
for (;;) {
|
||||
usb_poll();
|
||||
mg_mgr_poll(&mgr, 0);
|
||||
mg_mqtt_poll(&mgr);
|
||||
blink_task();
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
#include "nrf.h"
|
||||
#define MG_ARCH MG_ARCH_ARMGCC
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_NRF52 1
|
||||
#define MG_ENABLE_DRIVER_NRF52 1 // 802.15.4 radio: the network interface
|
||||
#define MG_ENABLE_DRIVER_USB_NRF52 1 // nRF52840 USB controller
|
||||
#define MG_ENABLE_USB_SERIAL 1 // USB serial log console
|
||||
#define MG_ENABLE_TCPIP_TCPRTX 1
|
||||
#define MG_ENABLE_CUSTOM_MICROS 1
|
||||
#define MG_ENABLE_CUSTOM_RANDOM 1
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#pragma once
|
||||
#define CFG_TUSB_MCU OPT_MCU_NRF5X
|
||||
#define CFG_TUSB_OS OPT_OS_NONE
|
||||
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
|
||||
#define CFG_TUD_ENDPOINT0_SIZE 64
|
||||
#define CFG_TUD_CDC 1
|
||||
#define CFG_TUD_CDC_RX_BUFSIZE 256
|
||||
#define CFG_TUD_CDC_TX_BUFSIZE 4096
|
||||
#define CFG_TUD_CDC_EP_BUFSIZE 64
|
||||
@@ -1,99 +0,0 @@
|
||||
#include "hal.h"
|
||||
#include "tusb.h"
|
||||
extern void tusb_hal_nrf_power_event(uint32_t event);
|
||||
void USBD_IRQHandler(void) {
|
||||
tud_int_handler(0);
|
||||
}
|
||||
void POWER_CLOCK_IRQHandler(void) {
|
||||
if (NRF_POWER->EVENTS_USBDETECTED) {
|
||||
NRF_POWER->EVENTS_USBDETECTED = 0;
|
||||
tusb_hal_nrf_power_event(0);
|
||||
}
|
||||
if (NRF_POWER->EVENTS_USBREMOVED) {
|
||||
NRF_POWER->EVENTS_USBREMOVED = 0;
|
||||
tusb_hal_nrf_power_event(1);
|
||||
}
|
||||
if (NRF_POWER->EVENTS_USBPWRRDY) {
|
||||
NRF_POWER->EVENTS_USBPWRRDY = 0;
|
||||
tusb_hal_nrf_power_event(2);
|
||||
}
|
||||
}
|
||||
void usb_init(void) {
|
||||
NRF_POWER->INTENSET = POWER_INTENSET_USBDETECTED_Msk |
|
||||
POWER_INTENSET_USBREMOVED_Msk |
|
||||
POWER_INTENSET_USBPWRRDY_Msk;
|
||||
NVIC_SetPriority(POWER_CLOCK_IRQn, 3);
|
||||
NVIC_SetPriority(USBD_IRQn, 3);
|
||||
if (NRF_POWER->USBREGSTATUS & POWER_USBREGSTATUS_VBUSDETECT_Msk)
|
||||
tusb_hal_nrf_power_event(0);
|
||||
if (NRF_POWER->USBREGSTATUS & POWER_USBREGSTATUS_OUTPUTRDY_Msk)
|
||||
tusb_hal_nrf_power_event(2);
|
||||
NRF_POWER->EVENTS_USBDETECTED = 0;
|
||||
NRF_POWER->EVENTS_USBPWRRDY = 0;
|
||||
NRF_POWER->EVENTS_USBREMOVED = 0;
|
||||
NVIC_ClearPendingIRQ(POWER_CLOCK_IRQn);
|
||||
tusb_init();
|
||||
NVIC_EnableIRQ(POWER_CLOCK_IRQn);
|
||||
}
|
||||
void usb_poll(void) {
|
||||
tud_task();
|
||||
tud_cdc_write_flush();
|
||||
}
|
||||
void usb_log(char ch, void *param) {
|
||||
(void) param;
|
||||
if (tud_cdc_connected()) tud_cdc_write_char(ch);
|
||||
}
|
||||
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
|
||||
cdc_line_coding_t lc;
|
||||
(void) rts;
|
||||
tud_cdc_n_get_line_coding(itf, &lc);
|
||||
if (!dtr && lc.bit_rate == 1200) {
|
||||
NRF_POWER->GPREGRET = 0x57;
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
}
|
||||
static const tusb_desc_device_t s_dev = {
|
||||
.bLength = sizeof(tusb_desc_device_t),
|
||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||
.bcdUSB = 0x0200,
|
||||
.bDeviceClass = TUSB_CLASS_MISC,
|
||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
|
||||
.bDeviceProtocol = MISC_PROTOCOL_IAD,
|
||||
.bMaxPacketSize0 = 64,
|
||||
.idVendor = 0xcafe,
|
||||
.idProduct = 0x4001,
|
||||
.bcdDevice = 0x0100,
|
||||
.iManufacturer = 1,
|
||||
.iProduct = 2,
|
||||
.iSerialNumber = 3,
|
||||
.bNumConfigurations = 1};
|
||||
uint8_t const *tud_descriptor_device_cb(void) {
|
||||
return (const uint8_t *) &s_dev;
|
||||
}
|
||||
static const uint8_t s_cfg[] = {
|
||||
TUD_CONFIG_DESCRIPTOR(1, 2, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN, 0,
|
||||
100),
|
||||
TUD_CDC_DESCRIPTOR(0, 0, 0x81, 8, 0x02, 0x82, 64)};
|
||||
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
|
||||
(void) index;
|
||||
return s_cfg;
|
||||
}
|
||||
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||
static uint16_t buf[32];
|
||||
const char *strings[] = {"", "Cesanta", "XIAO nRF52840", "mongoose-nrf52840"};
|
||||
size_t n = 0;
|
||||
(void) langid;
|
||||
if (index == 0) {
|
||||
buf[1] = 0x0409;
|
||||
n = 1;
|
||||
} else if (index < 4) {
|
||||
const char *s = strings[index];
|
||||
while (s[n] && n < 31) {
|
||||
buf[n + 1] = s[n];
|
||||
n++;
|
||||
}
|
||||
} else
|
||||
return NULL;
|
||||
buf[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * n + 2));
|
||||
return buf;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
# Seeed XIAO nRF52840: Mongoose over USB CDC-NCM, no TinyUSB.
|
||||
# Environment setup: https://mongoose.ws/documentation/tutorials/tools/
|
||||
|
||||
MGDIR = ../../../..
|
||||
|
||||
# Flash over USB: the board's Adafruit bootloader does serial DFU. The
|
||||
# firmware reboots into it on a 1200 baud touch. PORT: the board's USB serial
|
||||
# port. adafruit-nrfutil: pip install adafruit-nrfutil
|
||||
PORT ?= /dev/cu.usbmodem11301
|
||||
NRFUTIL ?= adafruit-nrfutil
|
||||
|
||||
CFLAGS = -W -Wall -Wextra -Werror -Wshadow -Wdouble-promotion
|
||||
CFLAGS += -Wformat-truncation -fno-common -Wno-conversion -Wno-sign-conversion
|
||||
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections
|
||||
CFLAGS += -I. -I$(MGDIR) -Icmsis_core/CMSIS/Core/Include -Inrfx/mdk
|
||||
CFLAGS += -DNRF52840_XXAA -D__STARTUP_CLEAR_BSS -D__START=main -D__HEAP_SIZE=0
|
||||
CFLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
|
||||
CFLAGS += $(CFLAGS_EXTRA)
|
||||
|
||||
LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs=nano.specs -lc -lm -lgcc
|
||||
LDFLAGS += -Wl,--gc-sections -Wl,-Map=$@.map
|
||||
|
||||
SOURCES = hal.c main.c $(MGDIR)/mongoose.c
|
||||
SOURCES += nrfx/mdk/system_nrf52840.c nrfx/mdk/gcc_startup_nrf52840.S
|
||||
|
||||
all build: firmware.hex
|
||||
|
||||
firmware.elf: cmsis_core nrfx $(SOURCES) $(wildcard *.h) link.ld Makefile
|
||||
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@
|
||||
arm-none-eabi-size --format=berkeley $@
|
||||
|
||||
firmware.hex: firmware.elf
|
||||
arm-none-eabi-objcopy -O ihex $< $@
|
||||
|
||||
firmware.zip: firmware.hex
|
||||
$(NRFUTIL) dfu genpkg --dev-type 0x0052 --sd-req 0xFFFE --application $< $@
|
||||
|
||||
flash: firmware.zip
|
||||
$(NRFUTIL) dfu serial --package $< -p $(PORT) -b 115200 --singlebank --touch 1200
|
||||
|
||||
cmsis_core:
|
||||
git clone -q -c advice.detachedHead=false --depth 1 -b 5.9.0 https://github.com/ARM-software/CMSIS_5 $@
|
||||
|
||||
# nRF52840 device headers, SystemInit() and the startup code
|
||||
nrfx:
|
||||
git clone -q -c advice.detachedHead=false --depth 1 -b v2.11.0 https://github.com/NordicSemiconductor/nrfx $@
|
||||
|
||||
clean:
|
||||
rm -rf firmware.* cmsis_core nrfx
|
||||
|
||||
.PHONY: all build flash clean
|
||||
@@ -0,0 +1,67 @@
|
||||
# XIAO nRF52840: USB Ethernet and serial with Mongoose
|
||||
|
||||
One USB cable gives the board a CDC-NCM Ethernet interface and a CDC-ACM
|
||||
serial port that carries the Mongoose log. The nRF52840 USB device controller
|
||||
is driven directly: no TinyUSB, SoftDevice, Ethernet PHY, or lwIP. Mongoose
|
||||
provides TCP/IP, a DHCP server, and HTTP/HTTPS servers.
|
||||
|
||||
The USB code is a Mongoose driver, `src/drivers/usb_nrf52.c`. In
|
||||
`mongoose_config.h`, `MG_ENABLE_DRIVER_USB_NRF52` selects it, and
|
||||
`MG_ENABLE_USB_ETHERNET` and `MG_ENABLE_USB_SERIAL` turn on its two
|
||||
functions. Mongoose runs it from `mg_mgr_init()` and `mg_mgr_poll()`, so
|
||||
`main.c` only sets up the clock, the LED, and the HTTP listeners.
|
||||
|
||||
## Build and flash
|
||||
|
||||
Install the Arm GNU toolchain and `adafruit-nrfutil`
|
||||
(`pip install adafruit-nrfutil`). The first build fetches CMSIS and Nordic's
|
||||
nrfx, for the device headers and the startup code.
|
||||
|
||||
```sh
|
||||
make
|
||||
make flash PORT=/dev/cu.usbmodemXXXX
|
||||
```
|
||||
|
||||
The board keeps its Adafruit bootloader. The application starts at 0x27000,
|
||||
above the area reserved for the SoftDevice, which is not used. `make flash`
|
||||
opens the serial port at 1200 baud, which reboots the firmware into the
|
||||
bootloader, then uploads over serial DFU. If no firmware with a USB serial
|
||||
port is running, double-tap RESET to enter the bootloader first.
|
||||
|
||||
## Use on macOS
|
||||
|
||||
Connect with a USB data cable. macOS creates an Ethernet service and a
|
||||
`/dev/cu.usbmodem*` serial port with built-in drivers. Allow the USB accessory
|
||||
if prompted, and leave the network service set to DHCP.
|
||||
|
||||
`mongoose_config.h` gives the device a static address, and the driver serves
|
||||
DHCP to the host:
|
||||
|
||||
* Device: `192.168.7.1/24`
|
||||
* HTTP: <http://192.168.7.1/>
|
||||
* HTTPS: <https://192.168.7.1/> (self-signed demonstration certificate)
|
||||
* JSON tick: <http://192.168.7.1/api/tick>
|
||||
|
||||
Change `MG_TCPIP_IP` and `MG_TCPIP_MASK` if the subnet conflicts with another
|
||||
network. Remove them to make the board a DHCP client instead, for example
|
||||
behind macOS Internet Sharing.
|
||||
|
||||
```sh
|
||||
curl --noproxy '*' http://192.168.7.1/api/tick
|
||||
curl --noproxy '*' -k https://192.168.7.1/api/tick
|
||||
screen /dev/cu.usbmodemXXXX 115200
|
||||
```
|
||||
|
||||
The baud rate is ignored, except that 1200 baud followed by DTR dropping
|
||||
reboots into the bootloader. The serial port is an output console; received
|
||||
bytes are discarded.
|
||||
|
||||
## Implementation
|
||||
|
||||
* `main.c`: LED blink task and the HTTP/HTTPS handler.
|
||||
* `hal.h`, `hal.c`: clock, SysTick, RNG, LED, newlib stubs.
|
||||
* `../../../../src/drivers/usb_nrf52.c`: nRF52840 USB device controller,
|
||||
polled. EasyDMA moves each 64-byte packet, one transfer at a time.
|
||||
Applies Nordic's errata 171, 187 and 199 workarounds.
|
||||
* `../../../../src/drivers/usb.c`: controller independent NCM + ACM, shared
|
||||
with the RP2040/RP2350 and SAMD21 drivers.
|
||||
@@ -0,0 +1,109 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "hal.h"
|
||||
#include <string.h>
|
||||
|
||||
static volatile uint64_t s_ticks; // Milliseconds since boot
|
||||
void SysTick_Handler(void) { // SysTick IRQ handler, triggered every 1ms
|
||||
s_ticks++;
|
||||
}
|
||||
|
||||
uint64_t hal_get_tick(void) {
|
||||
return s_ticks;
|
||||
}
|
||||
|
||||
// Mongoose time source, see MG_ENABLE_CUSTOM_MILLIS
|
||||
uint64_t mg_millis(void) {
|
||||
return s_ticks;
|
||||
}
|
||||
|
||||
// Mongoose random source, see MG_ENABLE_CUSTOM_RANDOM
|
||||
bool mg_random(void *buf, size_t len) {
|
||||
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
|
||||
uint32_t r = hal_rng_read();
|
||||
memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Avoid pulling printf and friends in through assert()
|
||||
void __assert_func(const char *s, int line, const char *fn, const char *expr) {
|
||||
(void) s, (void) line, (void) fn, (void) expr;
|
||||
for (;;) (void) 0;
|
||||
}
|
||||
|
||||
extern unsigned char _end[]; // End of data section, start of heap. See link.ld
|
||||
static unsigned char *s_current_heap_end = _end;
|
||||
|
||||
size_t hal_ram_used(void) {
|
||||
return (size_t) (s_current_heap_end - _end);
|
||||
}
|
||||
|
||||
size_t hal_ram_free(void) {
|
||||
unsigned char endofstack;
|
||||
return (size_t) (&endofstack - s_current_heap_end);
|
||||
}
|
||||
|
||||
void *_sbrk(int incr) {
|
||||
unsigned char *prev_heap;
|
||||
unsigned char *heap_end = (unsigned char *) ((size_t) &heap_end - 256);
|
||||
prev_heap = s_current_heap_end;
|
||||
// Check how much space we got from the heap end to the stack end
|
||||
if (s_current_heap_end + incr > heap_end) return (void *) -1;
|
||||
s_current_heap_end += incr;
|
||||
return prev_heap;
|
||||
}
|
||||
|
||||
// Newlib stubs
|
||||
struct stat;
|
||||
int _fstat(int fd, struct stat *st) {
|
||||
(void) fd, (void) st;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _open(const char *path) {
|
||||
(void) path;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _close(int fd) {
|
||||
(void) fd;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _isatty(int fd) {
|
||||
(void) fd;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _lseek(int fd, int ptr, int dir) {
|
||||
(void) fd, (void) ptr, (void) dir;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void _exit(int status) {
|
||||
(void) status;
|
||||
for (;;) asm volatile("BKPT #0");
|
||||
}
|
||||
|
||||
void _kill(int pid, int sig) {
|
||||
(void) pid, (void) sig;
|
||||
}
|
||||
|
||||
int _getpid(void) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _write(int fd, char *ptr, int len) {
|
||||
(void) fd, (void) ptr, (void) len;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _read(int fd, char *ptr, int len) {
|
||||
(void) fd, (void) ptr, (void) len;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void _init(void) {
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "nrf.h"
|
||||
|
||||
#define BIT(x) (1UL << (x))
|
||||
#define PIN(port, num) (((port) << 5) | (num))
|
||||
#define PINNO(pin) ((pin) & 31)
|
||||
#define PINPORT(pin) ((pin) >> 5)
|
||||
#define LED PIN(0, 26) // P0.26, red user LED, active low
|
||||
|
||||
uint64_t hal_get_tick(void);
|
||||
size_t hal_ram_used(void);
|
||||
size_t hal_ram_free(void);
|
||||
|
||||
static inline NRF_GPIO_Type *hal_gpio_bank(uint16_t pin) {
|
||||
return PINPORT(pin) == 0 ? NRF_P0 : NRF_P1;
|
||||
}
|
||||
|
||||
static inline void hal_gpio_write(uint16_t pin, bool val) {
|
||||
NRF_GPIO_Type *gpio = hal_gpio_bank(pin);
|
||||
if (val) {
|
||||
gpio->OUTSET = BIT(PINNO(pin));
|
||||
} else {
|
||||
gpio->OUTCLR = BIT(PINNO(pin));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hal_gpio_toggle(uint16_t pin) {
|
||||
hal_gpio_write(pin, (hal_gpio_bank(pin)->OUT & BIT(PINNO(pin))) == 0);
|
||||
}
|
||||
|
||||
static inline void hal_gpio_output(uint16_t pin) {
|
||||
hal_gpio_bank(pin)->PIN_CNF[PINNO(pin)] =
|
||||
(GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) |
|
||||
(GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos);
|
||||
}
|
||||
|
||||
// Vectors above the bootloader, 64 MHz crystal clock, 1 ms SysTick
|
||||
static inline void hal_system_init(void) {
|
||||
extern uint32_t __isr_vector[];
|
||||
SCB->VTOR = (uint32_t) __isr_vector;
|
||||
__DSB();
|
||||
__ISB();
|
||||
__enable_irq();
|
||||
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
|
||||
NRF_CLOCK->TASKS_HFCLKSTART = 1;
|
||||
while (!NRF_CLOCK->EVENTS_HFCLKSTARTED) (void) 0;
|
||||
SystemCoreClockUpdate();
|
||||
SysTick_Config(SystemCoreClock / 1000);
|
||||
}
|
||||
|
||||
static inline void hal_rng_init(void) {
|
||||
NRF_RNG->CONFIG = 1; // Bias correction
|
||||
NRF_RNG->TASKS_START = 1;
|
||||
}
|
||||
|
||||
static inline uint32_t hal_rng_read(void) {
|
||||
uint32_t value = 0;
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
while (!NRF_RNG->EVENTS_VALRDY) (void) 0;
|
||||
value |= (uint32_t) NRF_RNG->VALUE << (i * 8);
|
||||
NRF_RNG->EVENTS_VALRDY = 0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/* nRF52840 on the XIAO nRF52840, with the Adafruit UF2 bootloader. Flash:
|
||||
* MBR and SoftDevice S140 below 0x27000, the bootloader from 0xED000 up: the
|
||||
* application in between. The SoftDevice is not enabled, so all RAM is ours,
|
||||
* except the first 24k it would take: kept free, as the Arduino core does */
|
||||
ENTRY(Reset_Handler);
|
||||
MEMORY {
|
||||
flash(rx) : ORIGIN = 0x00027000, LENGTH = 0xC6000
|
||||
sram(rwx) : ORIGIN = 0x20006000, LENGTH = 0x3A000
|
||||
}
|
||||
|
||||
SECTIONS {
|
||||
.vectors : { KEEP(*(.isr_vector)) . = ALIGN(4); } > flash
|
||||
.text : { *(.text* .text.*) } > flash
|
||||
.rodata : { *(.rodata*) . = ALIGN(4); } > flash
|
||||
|
||||
.data : {
|
||||
_sdata = .;
|
||||
*(.first_data)
|
||||
*(.iram .iram.*) /* Mongoose MG_IRAM functions */
|
||||
*(.data SORT(.data.*))
|
||||
. = ALIGN(4);
|
||||
_edata = .;
|
||||
} > sram AT > flash
|
||||
__data_start = _sdata;
|
||||
__data_end = _edata;
|
||||
__data_load_start = LOADADDR(.data);
|
||||
/* Unused data regions expected by Nordic's standard startup. */
|
||||
__sdata_start = _edata;
|
||||
__sdata_end = _edata;
|
||||
__sdata_load_start = _edata;
|
||||
__tdata_start = _edata;
|
||||
__tdata_end = _edata;
|
||||
__tdata_load_start = _edata;
|
||||
__fast_start = _edata;
|
||||
__fast_end = _edata;
|
||||
__fast_load_start = _edata;
|
||||
|
||||
.bss : {
|
||||
_sbss = .;
|
||||
*(.bss SORT(.bss.*) COMMON)
|
||||
. = ALIGN(4);
|
||||
_ebss = .;
|
||||
} > sram
|
||||
__bss_start__ = _sbss;
|
||||
__bss_end__ = _ebss;
|
||||
__tbss_start__ = _ebss;
|
||||
__tbss_end__ = _ebss;
|
||||
__sbss_start__ = _ebss;
|
||||
__sbss_end__ = _ebss;
|
||||
|
||||
/* Health record, see MG_HEALTH_RAM: not cleared, survives a warm reset */
|
||||
.mg_health (NOLOAD) : { KEEP(*(.mg_health .mg_health*)) } > sram
|
||||
|
||||
. = ALIGN(8);
|
||||
_end = .; /* Start of heap, see _sbrk() in hal.c */
|
||||
|
||||
/* Nordic startup reserves its default 16 KiB stack at the top of RAM. */
|
||||
.stack ORIGIN(sram) + LENGTH(sram) - 0x4000 (NOLOAD) : {
|
||||
KEEP(*(.stack))
|
||||
} > sram
|
||||
ASSERT(_end <= __StackLimit, "RAM sections overlap the stack")
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "hal.h"
|
||||
#include "mongoose.h"
|
||||
|
||||
static void blink_task(void) {
|
||||
static uint64_t blink_timer = 0;
|
||||
if (mg_timer_expired(&blink_timer, 500, mg_millis())) {
|
||||
hal_gpio_toggle(LED);
|
||||
}
|
||||
}
|
||||
|
||||
// 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"
|
||||
|
||||
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 {
|
||||
mg_http_reply(c, 200, "",
|
||||
"Hi from Mongoose!\n"
|
||||
"tick: %llu\n"
|
||||
"ram used: %zu\n"
|
||||
"ram free: %zu\n",
|
||||
mg_millis(), hal_ram_used(), hal_ram_free());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
hal_system_init();
|
||||
hal_rng_init();
|
||||
hal_gpio_write(LED, true); // Off
|
||||
hal_gpio_output(LED);
|
||||
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr); // Also starts the USB driver, see mongoose_config.h
|
||||
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_INFO(("Initialised. CPU clock: %lu MHz", SystemCoreClock / 1000000));
|
||||
MG_INFO(("RAM used: %zu, free: %zu", hal_ram_used(), hal_ram_free()));
|
||||
MG_INFO(("USB Ethernet + serial"));
|
||||
|
||||
for (;;) {
|
||||
mg_mgr_poll(&mgr, 0);
|
||||
blink_task();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#define MG_ARCH MG_ARCH_ARMGCC
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_IPV6 0
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
#define MG_ENABLE_CUSTOM_RANDOM 1
|
||||
#define MG_ENABLE_DRIVER_USB_NRF52 1 // nRF52840 USB controller
|
||||
#define MG_ENABLE_USB_ETHERNET 1 // USB NCM Ethernet interface
|
||||
#define MG_ENABLE_USB_SERIAL 1 // USB serial log console
|
||||
// Static device address; the driver serves DHCP to the USB host. Remove
|
||||
// these to get an address over DHCP instead, e.g. from Internet Sharing
|
||||
#define MG_TCPIP_IP MG_IPV4(192, 168, 7, 1)
|
||||
#define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0)
|
||||
#define MG_SET_MAC_ADDRESS(mac) \
|
||||
do { mac[0] = 2; mac[1] = 3; mac[2] = 4; mac[3] = 5; mac[4] = 6; mac[5] = 7; } while (0)
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
set(PICO_BOARD pico2 CACHE STRING "Target board")
|
||||
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
|
||||
project(usb_ncm C CXX ASM)
|
||||
pico_sdk_init()
|
||||
add_executable(firmware main.c ../../../mongoose.c)
|
||||
target_include_directories(firmware PRIVATE . ../../..)
|
||||
target_link_libraries(firmware pico_stdlib pico_unique_id pico_rand hardware_resets)
|
||||
pico_enable_stdio_usb(firmware 0)
|
||||
pico_enable_stdio_uart(firmware 0)
|
||||
pico_add_extra_outputs(firmware)
|
||||
@@ -0,0 +1,13 @@
|
||||
PICO_SDK_PATH ?= $(HOME)/src/pico-sdk
|
||||
PICO_BOARD ?= pico2
|
||||
BUILD ?= build/$(PICO_BOARD)
|
||||
.PHONY: all build flash clean
|
||||
all: build
|
||||
build:
|
||||
PICO_SDK_PATH=$(PICO_SDK_PATH) cmake -S . -B $(BUILD) -DPICO_BOARD=$(PICO_BOARD) -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build $(BUILD) -j 4
|
||||
flash: build
|
||||
picotool load -f $(BUILD)/firmware.uf2
|
||||
picotool reboot
|
||||
clean:
|
||||
rm -rf build
|
||||
@@ -0,0 +1,99 @@
|
||||
# Pico 2 / XIAO RP2040: USB Ethernet and serial with Mongoose
|
||||
|
||||
One USB cable gives the board a CDC-NCM Ethernet interface and a CDC-ACM
|
||||
serial port that carries the Mongoose log. The native RP2040/RP2350 USB
|
||||
controller is driven directly: no PIO, Ethernet PHY, TinyUSB, or lwIP.
|
||||
Mongoose provides TCP/IP, a DHCP server, and HTTP/HTTPS servers.
|
||||
|
||||
The USB code is a Mongoose driver, `src/drivers/usb_rp.c`, enabled by
|
||||
`MG_ENABLE_DRIVER_USB_RP` in `mongoose_config.h`. `mg_mgr_init()` starts it,
|
||||
so `main.c` only sets up the LED and the HTTP listeners.
|
||||
|
||||
## Build and flash
|
||||
|
||||
Install the Pico SDK, CMake, Arm GNU toolchain, and picotool. The Makefile
|
||||
uses `~/src/pico-sdk`; override `PICO_SDK_PATH` if necessary.
|
||||
The default target is `PICO_BOARD=pico2` (RP2350, Arm Cortex-M33).
|
||||
|
||||
```sh
|
||||
make # Pico 2
|
||||
make PICO_BOARD=seeed_xiao_rp2040 # XIAO RP2040
|
||||
```
|
||||
|
||||
Each board has its own build directory, `build/pico2/firmware.uf2` or
|
||||
`build/seeed_xiao_rp2040/firmware.uf2`, so RP2040 and RP2350 CMake settings
|
||||
never mix.
|
||||
|
||||
On Pico 2, hold BOOTSEL while connecting the USB cable. On XIAO, hold BOOT
|
||||
while pressing and releasing RESET, then release BOOT. Flash:
|
||||
|
||||
```sh
|
||||
make flash
|
||||
```
|
||||
|
||||
The firmware also enters BOOTSEL when its serial port is opened at 1200 baud
|
||||
and DTR drops. `picotool -f` alone cannot reboot it: the driver does not
|
||||
implement the Pico SDK vendor reset interface.
|
||||
|
||||
## Use on macOS
|
||||
|
||||
Connect with a USB data cable. macOS creates an Ethernet service and a
|
||||
`/dev/cu.usbmodem*` serial port with built-in drivers. Allow the USB accessory
|
||||
if prompted, and leave the network service set to DHCP.
|
||||
|
||||
`mongoose_config.h` gives the device a static address, and the driver serves
|
||||
DHCP to the host:
|
||||
|
||||
* Device: `192.168.7.1/24`
|
||||
* HTTP: <http://192.168.7.1/>
|
||||
* HTTPS: <https://192.168.7.1/> (self-signed demonstration certificate)
|
||||
* JSON tick: <http://192.168.7.1/api/tick>
|
||||
|
||||
Change `MG_TCPIP_IP` and `MG_TCPIP_MASK` if the subnet conflicts with another
|
||||
network. Remove them to make the board a DHCP client instead, for example
|
||||
behind macOS Internet Sharing.
|
||||
|
||||
```sh
|
||||
curl --noproxy '*' http://192.168.7.1/api/tick
|
||||
curl --noproxy '*' -k https://192.168.7.1/api/tick
|
||||
screen /dev/cu.usbmodemXXXX 115200
|
||||
```
|
||||
|
||||
Use the actual serial device path. The baud rate is ignored, except that
|
||||
1200 baud followed by DTR dropping requests BOOTSEL. To exit screen, press
|
||||
Ctrl-A, then backslash, and confirm. The serial port is an output console;
|
||||
received bytes are discarded.
|
||||
|
||||
Linux normally provides NCM and ACM drivers. Windows 11 provides NCM and ACM;
|
||||
the driver includes Microsoft OS 2.0 compatible ID descriptors for driver
|
||||
binding. Linux and Windows have not been tested here. Windows 10 has no
|
||||
built-in NCM driver.
|
||||
|
||||
## Implementation
|
||||
|
||||
* `main.c`: LED blink task and the HTTP/HTTPS handler.
|
||||
* `../../../src/drivers/usb_rp.c`: RP2040/RP2350 USB device controller.
|
||||
* `../../../src/drivers/usb.c`: controller independent NCM + ACM:
|
||||
descriptors, control requests, NTB16 negotiation, framing, parser, and the
|
||||
serial log buffer. Shared with the SAMD21 driver, `usb_samd21.c`.
|
||||
* Network control/data interfaces 0/1; ACM control/data interfaces 2/3.
|
||||
* Network endpoints: interrupt IN 0x81, bulk OUT/IN 0x02/0x82.
|
||||
* Serial endpoints: interrupt IN 0x83, bulk OUT/IN 0x04/0x84.
|
||||
* Full-speed USB, 64-byte packets. Transfers span multiple packets and end
|
||||
with short packets or ZLPs. Ethernet FCS is not sent on USB.
|
||||
* NTB16 with no CRC, 4 KiB maximum IN/OUT blocks, and up to 16 incoming
|
||||
datagrams per block, including chained NDPs. The whole block is validated
|
||||
before any frame is delivered. NTB32 and CRC mode are not advertised.
|
||||
* One Ethernet frame per outgoing block, within the host's negotiated input
|
||||
size. Outgoing frames are not aggregated.
|
||||
* Host and device MAC addresses and the USB serial number come from the
|
||||
board's flash ID. One outgoing block can be in flight; incoming frames go
|
||||
to Mongoose's 8 KiB queue. The serial log is a nonblocking 2 KiB ring; old
|
||||
logs are overwritten when the host is not reading.
|
||||
* Buffer sizes can be changed with `MG_USB_NCM_NTB_SIZE`,
|
||||
`MG_USB_NCM_LOG_SIZE` and `MG_USB_NCM_QUEUE_SIZE`.
|
||||
* Single core, polled from `mg_mgr_poll()`. No host mode, RNDIS, suspend,
|
||||
remote wake, or Ethernet bridging.
|
||||
|
||||
The development VID/PID is `CAFE:40E4`, not an allocation for shipped
|
||||
products. Get a legitimate VID/PID before distributing hardware.
|
||||
@@ -0,0 +1,96 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "hardware/clocks.h"
|
||||
#include "mongoose.h"
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
#define LED1 PICO_DEFAULT_LED_PIN // User LED, active low
|
||||
//#define LED1 16 // User LED, active low
|
||||
|
||||
static void blink_task(void) {
|
||||
static uint64_t blink_timer = 0;
|
||||
if (mg_timer_expired(&blink_timer, 500, mg_millis())) {
|
||||
gpio_xor_mask(1u << LED1);
|
||||
}
|
||||
}
|
||||
|
||||
// Match MG24's heap-extent measurements. The Pico SDK's newlib _sbrk(0)
|
||||
// returns the current heap break without allocating. Its linker reserves
|
||||
// stacks separately, so free space ends at __HeapLimit, not the current SP.
|
||||
// These figures exclude globals/stacks and reusable blocks inside malloc.
|
||||
extern void *_sbrk(int incr);
|
||||
extern char __end__, __HeapLimit;
|
||||
|
||||
static size_t hal_ram_used(void) {
|
||||
return (size_t) ((uintptr_t) _sbrk(0) - (uintptr_t) &__end__);
|
||||
}
|
||||
|
||||
static size_t hal_ram_free(void) {
|
||||
return (size_t) ((uintptr_t) &__HeapLimit - (uintptr_t) _sbrk(0));
|
||||
}
|
||||
|
||||
// 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"
|
||||
|
||||
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 {
|
||||
mg_http_reply(c, 200, "",
|
||||
"Hi from Mongoose!\n"
|
||||
"tick: %llu\n"
|
||||
"ram used: %zu\n"
|
||||
"ram free: %zu\n",
|
||||
mg_millis(), hal_ram_used(), hal_ram_free());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
gpio_init(LED1);
|
||||
gpio_put(LED1, 1);
|
||||
gpio_set_dir(LED1, GPIO_OUT);
|
||||
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr); // Also starts the USB driver, see mongoose_config.h
|
||||
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_INFO(("Initialised. CPU clock: %lu MHz", clock_get_hz(clk_sys) / 1000000));
|
||||
MG_INFO(("RAM used: %zu, free: %zu", hal_ram_used(), hal_ram_free()));
|
||||
MG_INFO(("USB Ethernet + serial"));
|
||||
|
||||
for (;;) {
|
||||
mg_mgr_poll(&mgr, 0);
|
||||
blink_task();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#define MG_ARCH MG_ARCH_PICOSDK
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_USB_RP 1 // RP2040 / RP2350 USB controller
|
||||
#define MG_ENABLE_USB_ETHERNET 1 // USB NCM Ethernet interface
|
||||
#define MG_ENABLE_USB_SERIAL 1 // USB serial log console
|
||||
|
||||
// Static device address; the driver serves DHCP to the USB host. Remove
|
||||
// these to get an address over DHCP instead, e.g. from Internet Sharing
|
||||
#define MG_TCPIP_IP MG_IPV4(192, 168, 7, 1)
|
||||
#define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0)
|
||||
|
||||
#define MG_SET_MAC_ADDRESS(mac) \
|
||||
do { \
|
||||
mac[0] = 2; \
|
||||
mac[1] = 3; \
|
||||
mac[2] = 4; \
|
||||
mac[3] = 5; \
|
||||
mac[4] = 6; \
|
||||
mac[5] = 7; \
|
||||
} while (0)
|
||||
Reference in new issue
Block a user