Add rp/usb-w

This commit is contained in:
cpq committed 2026-11-28 20:23:01 +00:00
1 parent c71c2d5f88
commit d275b0a460
8 files changed
+384 -8

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.13)
set(PICO_BOARD pico2_w 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 dashboard.c file_data.c ../../../mongoose.c)
target_include_directories(firmware PRIVATE . ../../..)
target_link_libraries(firmware pico_stdlib pico_unique_id pico_rand hardware_resets pico_cyw43_driver pico_cyw43_arch_none)
pico_enable_stdio_usb(firmware 0)
pico_enable_stdio_uart(firmware 0)
pico_add_extra_outputs(firmware)
+20
View File
@@ -0,0 +1,20 @@
PICO_SDK_PATH ?= $(HOME)/src/pico-sdk
PICO_BOARD ?= pico2_w
BUILD ?= build/$(PICO_BOARD)
.PHONY: all build flash clean
all: build
build: file_data.c
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
file_data.c: dashboard.html
node ../../../resources/html2c.js dashboard.html -o $@
clean:
rm -rf build file_data.c
+81
View File
@@ -0,0 +1,81 @@
// SPDX-FileCopyrightText: 2026 Cesanta Software Limited
// SPDX-License-Identifier: GPL-2.0-only or commercial
#include "mongoose.h"
static struct mg_dash s_dash;
// WiFi settings and logic are in main.c. This is the "wifi" object in
// dashboard.html
extern char g_wifi_ssid[33], g_wifi_pass[64];
extern bool g_wifi_enable, g_wifi_changed;
extern uint32_t wifi_ip(void);
static char s_wifi_status[40]; // Read-only, regenerated on every read
static bool wifi_fn(enum mg_dash_op op, struct mg_dash_user *u) {
(void) u;
if (op == MG_DASH_READ) {
uint32_t ip = wifi_ip();
if (ip != 0) {
mg_snprintf(s_wifi_status, sizeof(s_wifi_status), "enabled, IP: %M",
mg_print_ip4, &ip);
} else {
mg_snprintf(s_wifi_status, sizeof(s_wifi_status), "not connected");
}
return true;
}
// Called twice: before the fields are updated (permission check), and
// after. main() reconnects once, later
if (op == MG_DASH_WRITE) g_wifi_changed = true;
return op == MG_DASH_WRITE;
}
static struct mg_field fields_wifi[] = {
{"ssid", MG_VAL_STR, g_wifi_ssid, sizeof(g_wifi_ssid)},
{"pass", MG_VAL_STR, g_wifi_pass, sizeof(g_wifi_pass)},
{"enable", MG_VAL_BOOL, &g_wifi_enable, sizeof(g_wifi_enable)},
{"status", MG_VAL_STR, s_wifi_status, 0}, // value_size 0: read-only
{NULL, MG_VAL_INT, NULL, 0},
};
static struct mg_field_set set_wifi = {"wifi", fields_wifi, wifi_fn, NULL,
NULL};
// 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"
void mg_dash_init(struct mg_mgr *mgr) {
s_dash.tls_cert = mg_str(TLS_CERT); // HTTPS listener, see MG_HTTPS_ADDR
s_dash.tls_key = mg_str(TLS_KEY);
MG_DASH_ADD_FIELD_SET(&s_dash, &set_wifi);
mg_mem_files = mg_packed_files;
mg_http_listen(mgr, MG_HTTP_ADDR, mg_dash_ev_handler, &s_dash);
mg_http_listen(mgr, MG_HTTPS_ADDR, mg_dash_ev_handler, &s_dash);
}
void mg_dash_poll(struct mg_mgr *mgr) {
static uint32_t prev_ip;
if (wifi_ip() != prev_ip) { // Got or lost an address: update the status
prev_ip = wifi_ip();
mg_dash_send_change(mgr, &set_wifi);
}
}
+123
View File
@@ -0,0 +1,123 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>WiFi Settings</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Mongoose device dashboard -->
<script src="https://mongoose.ws/resources/dashboard.js"></script>
<style>
:root { --color-text-toolbar: rgb(71, 85, 105); --color-text-strong: #24364d; --font-size-body: 14px; --max-width: 80rem; --spacing-3: 0.75rem; --spacing-4: 1rem; --spacing-6: 1.5rem; }
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: var(--font-size-body); line-height: 1.5; min-height: 100vh; color: var(--color-text-strong); background: #fff; }
.header-container { max-width: var(--max-width); margin: 0 auto; display: flex; align-items: center; gap: var(--spacing-4); padding: var(--spacing-3) var(--spacing-6); }
.logo { height: 2.8rem; margin: 0 1rem; width: auto; }
.connection-badge { display: inline-flex; align-items: center; gap: 0.4rem; padding: 4px 14px; border-radius: 14px; font-weight: 600; }
.connection-badge-online { color: rgb(4, 120, 87); border: 1px solid rgb(167, 243, 208); background: rgb(236, 253, 245); }
.connection-badge-offline { color: rgb(120, 44, 57); border: 1px solid rgb(243, 177, 208); background: rgb(253, 246, 245); }
.connection-dot { width: 11px; height: 11px; border-radius: 50%; flex-shrink: 0; }
.connection-dot-online { background: rgb(16, 185, 129); }
.connection-dot-offline { background: rgb(185, 96, 109); }
.nav-icon { height: 1rem; width: 1rem; flex-shrink: 0; }
.stage { max-width: var(--max-width); margin: 0 auto; padding: 0.5rem var(--spacing-6) var(--spacing-4); }
.panel { max-width: 32rem; background: #ffffff; border: 1px solid rgba(203, 213, 225, 0.75); border-radius: 0.75rem; box-shadow: inset 0 1px 0 rgba(255, 255, 255, 1), 0 2px 6px rgba(148, 163, 184, 0.1), 0 8px 24px rgba(148, 163, 184, 0.08); overflow: hidden; display: flex; flex-direction: column; }
.panel-title { display: flex; align-items: center; font-size: 0.95rem; font-weight: 700; color: #334155; padding: 0 1rem; min-height: 3rem; border-bottom: 1px solid #e2e8f0; }
.panel-body { display: flex; flex-direction: column; gap: 0.5rem; padding: 1rem 1.25rem; }
.panel-footer { display: flex; align-items: center; justify-content: flex-end; gap: 0.75rem; padding: 0.75rem 1.25rem; border-top: 1px solid #e2e8f0; }
.field { display: flex; flex-direction: row; justify-content: space-between; align-items: center; gap: 0.75rem; min-height: 2.2rem; }
.field-label { font-size: 0.75rem; font-weight: 700; text-transform: uppercase; color: #94a3b8; flex-shrink: 0; white-space: nowrap; min-width: 3rem; }
.field-input { flex: 1; min-width: 0; max-width: 18rem; padding: 0.45rem 0.75rem; font-size: 0.88rem; font-family: inherit; color: #334155; border: 1px solid #e2e8f0; border-radius: 0.55rem; outline: none; transition: border-color 0.15s ease; }
.field-input.edited { border-color: #00500040 !important; background: #00ff0020; }
.field-input::placeholder { color: #cbd5e1; }
.field-input:hover { border-color: #cbd5e1; }
.field-input:focus { border-color: #7dd3fc; }
.field-value { min-width: 0; color: #334155; font-size: 0.9rem; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem; height: 2rem; padding: 0.3rem 1rem; font-size: 0.85rem; font-family: inherit; font-weight: 600; border-radius: 0.5rem; border: 1px solid transparent; cursor: pointer; transition: all 0.15s ease; white-space: nowrap; }
.btn-secondary { color: #64748b; background: #ffffff; border-color: #e2e8f0; box-shadow: 0 1px 2px rgba(148, 163, 184, 0.1); }
.btn-secondary:hover { background: #f1f5f9; border-color: #cbd5e1; color: #475569; }
.btn-primary { color: #ffffff; background: linear-gradient(160deg, #38bdf8, #0ea5e9); border-color: #0284c7; box-shadow: 0 1px 3px rgba(14, 165, 233, 0.3), 0 4px 10px rgba(14, 165, 233, 0.15); }
.btn-primary:hover { background: linear-gradient(160deg, #0ea5e9, #0284c7); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; pointer-events: none; box-shadow: none; }
.toggle { position: relative; width: 2.75rem; height: 1.6rem; margin: 0; vertical-align: top; border: 1px solid transparent; background: #d1d5db; border-radius: 0.75rem; outline: none; cursor: pointer; -webkit-appearance: none; -moz-appearance: none; appearance: none; }
.toggle::after { content: ""; position: absolute; left: 1px; top: 1px; width: 1.35rem; height: 1.35rem; background: #fff; border-radius: 50%; transition: all 0.3s; }
.toggle:checked { background: #10B981; }
.toggle:checked::after { transform: translateX(calc(100% - 3px)); }
.toggle.edited { border-color: #0fe; }
</style>
</head>
<body style="visibility: hidden;">
<div class="header-container">
<svg class="logo" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 600" width="1200" height="600">
<defs>
<linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="#2dd4bf"/>
<stop offset="100%" stop-color="#0f766e"/>
</linearGradient>
</defs>
<g fill="none" stroke="url(#g)" stroke-width="30" stroke-linecap="round" stroke-linejoin="round">
<rect x="160" y="60" width="300" height="300" rx="70"/>
<rect x="20" y="200" width="300" height="300" rx="70"/>
</g>
<text x="560" y="360" font-family="system-ui, -apple-system, Segoe UI, Roboto, sans-serif"
font-size="180" font-weight="600" fill="#6b7280">MyLogo</text>
</svg>
<div class="connection-badge connection-badge-${__status.online ? 'online' : 'offline'}">
<span>${__status.online ? 'live' : 'offline'}</span>
<span class="connection-dot connection-dot-${__status.online ? 'online' : 'offline'}"></span>
</div>
</div>
<main class="stage">
<div class="panel">
<div class="panel-title">WiFi settings</div>
<div class="panel-body">
<div class="field">
<label class="field-label">WiFi name</label>
<input class="field-input" type="text" data-bind="wifi.ssid" />
</div>
<div class="field">
<label class="field-label">WiFi password</label>
<input class="field-input" type="password" data-bind="wifi.pass" autocomplete="new-password" placeholder="" />
</div>
<div class="field">
<label class="field-label">WiFi enable</label>
<input type="checkbox" data-bind="wifi.enable" class="toggle" />
</div>
<div class="field">
<label class="field-label">WiFi status</label>
<span class="field-value" data-bind="wifi.status"></span>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-secondary" type="button" data-cancel="wifi">
<svg viewBox="0 0 24 24" class="nav-icon" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 6 6 18" />
<path d="m6 6 12 12" />
</svg>
Cancel
</button>
<button class="btn btn-primary" type="button" data-save="wifi">
<svg viewBox="0 0 24 24" class="nav-icon" fill="none" stroke="currentColor" stroke-width="2">
<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2Z" />
<path d="M17 21v-8H7v8" />
<path d="M7 3v5h8" />
</svg>
Save
</button>
</div>
</div>
</main>
<!-- At the end of body: with no login, init() loads data immediately -->
<script>
Dashboard.on('ready', () => document.body.style.visibility = 'visible');
Dashboard.init({
debug: true,
data: {
wifi: { ssid: 'MyNetwork', pass: '', enable: false, status: 'not connected' },
},
});
</script>
</body>
</html>
+96
View File
@@ -0,0 +1,96 @@
// Copyright (c) 2026 Cesanta Software Limited
// All rights reserved
#include "hardware/clocks.h"
#include "mongoose.h"
#include "pico/stdlib.h"
static void blink_task(void) {
static uint64_t blink_timer = 0;
static bool on; // Kept here: reading the CYW43 LED pin back is unreliable
if (mg_timer_expired(&blink_timer, 500, mg_millis())) {
on = !on;
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, on);
}
}
// 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));
}
// WiFi settings, edited in the dashboard, see dashboard.c
char g_wifi_ssid[33]; // Network name, up to 32 bytes
char g_wifi_pass[64]; // WPA2 passphrase, up to 63 characters
bool g_wifi_enable; // Connect, and poll s_wifi_mgr, if true
bool g_wifi_changed; // Settings saved: reconnect in main()
static struct mg_mgr s_wifi_mgr; // WiFi network, polled while enabled
// WiFi IP address, 0 if WiFi is disabled or has no address yet
uint32_t wifi_ip(void) {
struct mg_tcpip_if *ifp = s_wifi_mgr.ifp;
bool up = g_wifi_enable && ifp != NULL && ifp->state >= MG_TCPIP_STATE_IP;
return up ? ifp->ip : 0;
}
// HTTP server on the WiFi interface: one plain text page
static void wifi_http_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
(void) ev_data;
mg_http_reply(c, 200, "Content-Type: text/plain\r\n",
"Hi from Mongoose over WiFi, uptime %llu ms\n", mg_millis());
}
}
int main(void) {
struct mg_mgr mgr; // USB network, always polled
mg_mgr_init(&mgr);
MG_TCPIP_DRIVER_USB_RP_INIT(&mgr); // Static IP, DHCP server for the host
mg_dash_init(&mgr); // WiFi settings dashboard, see dashboard.c
// WiFi interface: a DHCP client. The driver starts the CYW43 chip, so the
// LED works from here on, and reads the MAC from OTP. It connects when
// WiFi is enabled in the dashboard
static struct mg_tcpip_driver_pico_w_data wifi_data;
static struct mg_tcpip_if wifi_if;
wifi_if.driver = &mg_tcpip_driver_pico_w;
wifi_if.driver_data = &wifi_data;
wifi_if.recv_queue.size = 8192;
wifi_if.mac[0] = 2; // Non-zero: keep mg_tcpip_init() from making one up
mg_mgr_init(&s_wifi_mgr);
mg_tcpip_init(&s_wifi_mgr, &wifi_if);
mg_http_listen(&s_wifi_mgr, "http://0.0.0.0:80", wifi_http_fn, 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 + WiFi"));
for (;;) {
mg_mgr_poll(&mgr, 0);
mg_dash_poll(&mgr);
if (g_wifi_changed) { // Settings saved in the dashboard: reconnect
g_wifi_changed = false;
mg_wifi_disconnect();
if (g_wifi_enable && g_wifi_ssid[0] != '\0') {
struct mg_wifi_data wifi;
memset(&wifi, 0, sizeof(wifi));
wifi.ssid = g_wifi_ssid, wifi.pass = g_wifi_pass;
mg_wifi_connect(&wifi);
}
}
if (g_wifi_enable) mg_mgr_poll(&s_wifi_mgr, 0);
blink_task();
}
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
#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
#define MG_ENABLE_DRIVER_PICO_W 1 // WiFi on the CYW43 chip
// Two interfaces, each in its own manager: main.c starts them, not
// mg_mgr_init(). The WiFi one ignores the static address below, uses DHCP
#define MG_ENABLE_TCPIP_DRIVER_INIT 0
// 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)