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

@@ -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 pico2)
include(pico_sdk_import.cmake)
project(firmware)
pico_sdk_init()
add_executable(firmware main.c mongoose/mongoose.c)
target_include_directories(firmware PRIVATE mongoose)
target_link_libraries(firmware pico_stdlib hardware_spi pico_rand hardware_flash pico_bootrom)
pico_add_extra_outputs(firmware) # create map/bin/hex file etc.
pico_enable_stdio_usb(firmware 1) # Route stdio
# Prevent extreme optimization from breaking OTA
target_compile_options(firmware PRIVATE -fno-tree-loop-distribute-patterns)
+50
View File
@@ -0,0 +1,50 @@
# 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
# Automated remote test. Requires env variable VCON_API_KEY set. See https://vcon.io/automated-firmware-tests/
DEVICE_URL ?= https://dash.vcon.io/api/v3/devices/3
# stdio has to be changed from USB to UART in CMakeLists.txt
update:
perl -pi -e 's/pico_enable_stdio_usb(firmware 1)/pico_enable_stdio_usb(firmware 0)/' CMakeLists.txt
$(MAKE) build
perl -pi -e 's/pico_enable_stdio_usb(firmware 0)/pico_enable_stdio_usb(firmware 1)/' CMakeLists.txt
curl --fail-with-body -su :$(VCON_API_KEY) $(DEVICE_URL)/ota?uf2=1 --data-binary @build/firmware.uf2
test: update
curl --fail-with-body -su :$(VCON_API_KEY) $(DEVICE_URL)/tx?t=15 | tee /tmp/output.txt
grep 'READY, IP:' /tmp/output.txt # Check for network 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/
+100
View File
@@ -0,0 +1,100 @@
// SPDX-FileCopyrightText: 2026 Cesanta Software Limited
// SPDX-License-Identifier: GPL-2.0-only or commercial
#include "hardware/spi.h"
#include "pico/stdlib.h"
#include "pico/unique_id.h"
#include "mongoose.h"
enum { SPI_CS = 17, SPI_CLK = 18, SPI_TX = 19, SPI_RX = 16 }; // Pins
// enum { SPI_CS = 5, SPI_CLK = 2, SPI_TX = 3, SPI_RX = 4 }; // Pins
static void spi_begin(void *spi) {
gpio_put(SPI_CS, 0);
}
static void spi_end(void *spi) {
gpio_put(SPI_CS, 1);
}
static void spi_txn(void *spi, uint8_t *tx, uint8_t *rx, size_t len) {
if (tx != NULL && rx != NULL) {
spi_write_read_blocking(spi0, tx, rx, len);
} else if (tx != NULL) {
spi_write_blocking(spi0, tx, len);
} else {
spi_read_blocking(spi0, 0xFF, rx, len);
}
}
static void ethernet_init(void) {
// Init SPI pins
spi_init(spi0, 500 * 1000);
gpio_set_function(SPI_RX, GPIO_FUNC_SPI); // MISO
gpio_set_function(SPI_TX, GPIO_FUNC_SPI); // MOSI
gpio_set_function(SPI_CLK, GPIO_FUNC_SPI); // CLK
gpio_init(SPI_CS); // CS
gpio_set_dir(SPI_CS, GPIO_OUT); // Set CS to output
gpio_put(SPI_CS, 1); // And drive CS high (inactive)
}
// Construct MAC address from the unique board ID
static void generate_mac_from_board_id(unsigned char *mac) {
pico_unique_board_id_t board_id;
pico_get_unique_board_id(&board_id);
mac[0] = 2;
memcpy(&mac[1], &board_id.id[3], 5);
}
static void blink_task(void) {
static uint64_t timer;
if (mg_timer_expired(&timer, 500, mg_now())) {
gpio_put(PICO_DEFAULT_LED_PIN, !gpio_get(PICO_DEFAULT_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;
struct mg_tcpip_spi spi = {NULL, spi_begin, spi_end, spi_txn};
struct mg_tcpip_if mif = {
.driver = &mg_tcpip_driver_w5500,
.driver_data = &spi,
};
generate_mac_from_board_id(mif.mac);
mg_mgr_init(&mgr);
mg_tcpip_init(&mgr, &mif);
MG_INFO(("Driver: W5500, MAC: %M", mg_print_mac, mif.mac));
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();
gpio_init(PICO_DEFAULT_LED_PIN); // Initialise GPIO
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); // Set as output
gpio_put(PICO_DEFAULT_LED_PIN, 1);
ethernet_init();
MG_INFO(("Hardware initialised, starting firmware..."));
run_mongoose();
return 0;
}
@@ -0,0 +1,10 @@
#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_W5500 1
#define MG_ENABLE_TCPIP_DRIVER_INIT 0