Revamp rp examples

This commit is contained in:
cpq committed 2026-05-22 08:40:58 +01:00
1 parent 948b56a3ec
commit 83ba170b45
142 files changed
+769 -3721

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.13)
# If you will set board type here, do it BEFORE including the SDK
set(PICO_BOARD pico_w)
include(pico_sdk_import.cmake)
project(firmware)
pico_sdk_init()
add_executable(firmware main.c wifi.c mongoose/mongoose.c)
target_include_directories(firmware PRIVATE mongoose)
target_link_libraries(firmware pico_stdlib hardware_spi pico_rand pico_cyw43_driver pico_cyw43_arch_none hardware_flash pico_bootrom)
pico_add_extra_outputs(firmware) # create map/bin/hex file etc.
pico_enable_stdio_usb(firmware 1) # Route stdio to USB
# Prevent extreme optimization from breaking OTA
target_compile_options(firmware PRIVATE -fno-tree-loop-distribute-patterns)
+37
View File
@@ -0,0 +1,37 @@
# Environment setup: https://mongoose.ws/docs/getting-started/build-environment/
CWD ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
SDK_PATH ?= $(CWD)/pico-sdk
RM = rm -rf
MKBUILD = test -d build || mkdir build
ifeq ($(OS),Windows_NT)
RM = cmd /C del /Q /F /S
MKBUILD = if not exist build mkdir build
endif
.PHONY: build
all build example: build/firmware.uf2
build/firmware.uf2: pico_sdk_import.cmake mongoose/mongoose.c mongoose/mongoose.h
pico_sdk_import.cmake: $(SDK_PATH)
cp $(SDK_PATH)/external/$@ $@
mongoose/mongoose.c mongoose/mongoose.h:
cp ../../../$(notdir $@) $@
build/firmware.uf2: $(SDK_PATH) $(wildcard *.[ch]) CMakeLists.txt Makefile
$(MKBUILD)
cd build && PICO_SDK_PATH=$(SDK_PATH) cmake -G "Unix Makefiles" .. && make
flash: build/firmware.uf2
picotool load $< -f
clean:
$(RM) build pico_sdk_import.cmake mongoose/mongoose.c mongoose/mongoose.h $(SDK_PATH)
$(SDK_PATH):
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.1.1 https://github.com/raspberrypi/pico-sdk $@
cd $@ && git submodule update --quiet --init
+28
View File
@@ -0,0 +1,28 @@
# RP2040 / RP2350 Mongoose example
This example can be built for both RP2040 and RP2350 targets.
## Set target board
By default, it builds for RP2040 target. If you want to change the target
board, edit `CMakeKists.txt` and set the board, for example:
```cmake
set(PICO_BOARD pico2_w)
```
## Edit WiFi credentials
For WiFi examples, edit `wifi.c` and set WiFi network name and password.
## Build and run
Make sure you have `picotool` installed. Then run:
```sh
make build flash
```
Start serial console to see the debug output.
See detailed documentation at https://mongoose.ws/docs/guides/rp-tcpip-stack/
+44
View File
@@ -0,0 +1,44 @@
// Copyright (c) 2026 Cesanta Software Limited
// All rights reserved
#include "mongoose.h"
static void blink_task(void) {
static uint64_t timer;
if (mg_timer_expired(&timer, 500, mg_now())) {
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN,
!cyw43_arch_gpio_get(CYW43_WL_GPIO_LED_PIN));
}
}
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) { // New HTTP request received
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
uint64_t tick = to_ms_since_boot(get_absolute_time());
if (mg_match(hm->uri, mg_str("/api/tick"), NULL)) {
mg_http_reply(c, 200, "", "{%m:%llu}\n", MG_ESC("tick"), tick);
} else {
mg_http_reply(c, 200, "", "Hi from Mongoose, tick %llu\n", tick);
}
}
}
// In an RTOS environment, give this task 8 KB of stack space.
static void run_mongoose(void) {
struct mg_mgr mgr;
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "http://0.0.0.0", http_ev_handler, NULL);
for (;;) {
mg_mgr_poll(&mgr, 1);
blink_task();
}
}
int main(void) {
stdio_init_all();
// do not access the CYW43 LED before Mongoose initializes !
MG_INFO(("Hardware initialised, starting firmware..."));
run_mongoose();
return 0;
}
@@ -0,0 +1,15 @@
#pragma once
// Build options: https://mongoose.ws/docs/getting-started/build-options/
#define MG_ARCH MG_ARCH_PICOSDK
#define MG_OTA MG_OTA_PICOSDK
#define MG_IO_SIZE 2048
#define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_PICO_W 1
#define MG_ENABLE_TCPIP_DRIVER_INIT 0
// MAC address is read from OTP by the driver
// Set your Wi-Fi configuration using a custom function:
extern void wifi_setconfig(void *, void *);
#define MG_SET_WIFI_CONFIG(ifp, data) wifi_setconfig(ifp, data)
+120
View File
@@ -0,0 +1,120 @@
// Copyright (c) 2026 Cesanta Software Limited
// All rights reserved
#include "mongoose.h"
#define WIFI_NAME "YOUR_WIFI_NETWORK" // SET THIS!
#define WIFI_PASS "YOUR_WIFI_PASSWORD" // SET THIS!
// Access point setup
#define ENABLE_AP false // Change to true to enable
#define WIFI_AP_NAME "YOUR_AP_NETWORK" // SET THIS!
#define WIFI_AP_PASS "YOUR_AP_PASSWORD" // SET THIS!
// mif user states
enum { AP, SCANNING, STOPPING_AP, CONNECTING, READY };
static unsigned int state;
static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
// TODO(): should we include this inside ifp ? add an fn_data ?
if (ev == MG_TCPIP_EV_ST_CHG) {
MG_INFO(("State change: %u", *(uint8_t *) ev_data));
}
switch (state) {
case AP: // we are in AP mode, wait for a user connection to trigger a scan
// or a connection to a network
if (ev == MG_TCPIP_EV_ST_CHG &&
*(uint8_t *) ev_data == MG_TCPIP_STATE_READY) {
MG_INFO(("Access Point READY !"));
// simulate user request to scan for networks
bool res = mg_wifi_scan();
MG_INFO(("Starting scan: %s", res ? "OK" : "FAIL"));
if (res) state = SCANNING;
}
break;
case SCANNING:
if (ev == MG_TCPIP_EV_WIFI_SCAN_RESULT) {
struct mg_wifi_scan_bss_data *bss =
(struct mg_wifi_scan_bss_data *) ev_data;
MG_INFO(("BSS: %.*s (%u) (%M) %d dBm %u", bss->SSID.len, bss->SSID.buf,
bss->channel, mg_print_mac, bss->BSSID, (int) bss->RSSI,
bss->security));
} else if (ev == MG_TCPIP_EV_WIFI_SCAN_END) {
struct mg_tcpip_driver_pico_w_data *d =
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;
MG_INFO(("Wi-Fi scan finished"));
// simulate user selection of a network (1/2: stop AP)
bool res = mg_wifi_ap_stop();
MG_INFO(("Manually stopping AP: %s", res ? "OK" : "FAIL"));
if (res) state = STOPPING_AP;
// else we have a hw/fw problem
}
break;
case STOPPING_AP:
if (ev == MG_TCPIP_EV_ST_CHG &&
*(uint8_t *) ev_data == MG_TCPIP_STATE_DOWN) {
struct mg_wifi_data *wifi =
&((struct mg_tcpip_driver_pico_w_data *) ifp->driver_data)->wifi;
wifi->apmode = false;
// simulate user selection of a network (2/2: actual connect)
bool res = mg_wifi_connect(wifi);
MG_INFO(("Manually connecting: %s", res ? "OK" : "FAIL"));
if (res) {
state = CONNECTING;
} // else manually start AP as below
}
break;
case CONNECTING:
if (ev == MG_TCPIP_EV_ST_CHG &&
*(uint8_t *) ev_data == MG_TCPIP_STATE_READY) {
MG_INFO(("READY!"));
state = READY;
// simulate user code disconnection and go back to AP mode (1/2:
// disconnect)
bool res = mg_wifi_disconnect();
MG_INFO(("Manually disconnecting: %s", res ? "OK" : "FAIL"));
} else if (ev == MG_TCPIP_EV_WIFI_CONNECT_ERR) {
MG_ERROR(("Wi-Fi connect failed"));
// manually start AP as below
}
break;
case READY:
// go back to AP mode after a disconnection (simulation 2/2), you could
// retry
if (ev == MG_TCPIP_EV_ST_CHG &&
*(uint8_t *) ev_data == MG_TCPIP_STATE_DOWN) {
struct mg_wifi_data *wifi =
&((struct mg_tcpip_driver_pico_w_data *) ifp->driver_data)->wifi;
bool res = mg_wifi_ap_start(wifi);
MG_INFO(("Disconnected"));
MG_INFO(("Manually starting AP: %s", res ? "OK" : "FAIL"));
if (res) {
state = AP;
wifi->apmode = true;
}
}
break;
}
}
void wifi_setconfig(void *ifp, void *driver_data) {
struct mg_tcpip_driver_pico_w_data *d =
(struct mg_tcpip_driver_pico_w_data *) driver_data;
struct mg_wifi_data *wifi = &d->wifi;
wifi->ssid = WIFI_NAME;
wifi->pass = WIFI_PASS;
wifi->apssid = WIFI_AP_NAME;
wifi->appass = WIFI_AP_PASS;
wifi->apip = MG_IPV4(192, 168, 111, 1);
wifi->apmask = MG_IPV4(255, 255, 255, 0);
wifi->security = 0;
wifi->apsecurity = 0;
wifi->apchannel = 10;
wifi->apmode = ENABLE_AP;
((struct mg_tcpip_if *) ifp)->fn = mif_fn;
}