mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-06 15:51:25 +00:00
RP boards
This commit is contained in:
1 parent
b773e33f4e
commit
ccd3514da3
13 files changed
+618
-10
No files matched your search
@@ -0,0 +1,16 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
# If you will set board type here, do it BEFORE including the SDK
|
||||
# set(PICO_BOARD pico2_w)
|
||||
include(pico_sdk_import.cmake)
|
||||
|
||||
project(firmware)
|
||||
pico_sdk_init()
|
||||
add_executable(firmware main.c mongoose.c)
|
||||
|
||||
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)
|
||||
@@ -0,0 +1,53 @@
|
||||
# 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: build/firmware.uf2
|
||||
|
||||
build/firmware.uf2: pico_sdk_import.cmake mongoose.c mongoose.h
|
||||
|
||||
pico_sdk_import.cmake: $(SDK_PATH)
|
||||
cp $(SDK_PATH)/external/$@ $@
|
||||
|
||||
mongoose.c mongoose.h:
|
||||
cp ../../../$@ .
|
||||
|
||||
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.c mongoose.h
|
||||
|
||||
fullclean: clean
|
||||
$(RM) $(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
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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
|
||||
|
||||
void spi_begin(void *spi) {
|
||||
gpio_put(SPI_CS, 0);
|
||||
}
|
||||
|
||||
void spi_end(void *spi) {
|
||||
gpio_put(SPI_CS, 1);
|
||||
}
|
||||
|
||||
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
|
||||
void genmac(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;
|
||||
|
||||
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();
|
||||
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,48 @@
|
||||
#pragma once
|
||||
|
||||
// See https://mongoose.ws/documentation/#build-options
|
||||
#define MG_ARCH MG_ARCH_PICOSDK
|
||||
#define MG_OTA MG_OTA_PICOSDK
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
#define MG_IO_SIZE 2048
|
||||
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_W5500 1
|
||||
|
||||
// For static IP configuration, define MG_TCPIP_{IP,MASK,GW}
|
||||
// By default, those are set to zero, meaning that DHCP is used
|
||||
//
|
||||
// #define MG_TCPIP_IP MG_IPV4(192, 168, 1, 10)
|
||||
// #define MG_TCPIP_GW MG_IPV4(192, 168, 1, 1)
|
||||
// #define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0)
|
||||
|
||||
// Set custom MAC address. By default, it is randomly generated
|
||||
// Using a build-time constant:
|
||||
// #define MG_SET_MAC_ADDRESS(mac) do { uint8_t buf_[6] = {2,3,4,5,6,7};
|
||||
// memmove(mac, buf_, sizeof(buf_)); } while (0)
|
||||
//
|
||||
// Using custom function:
|
||||
extern void genmac(unsigned char *mac);
|
||||
#define MG_SET_MAC_ADDRESS(mac) genmac(mac)
|
||||
|
||||
// Hardware specific SPI support
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
extern void spi_begin(void *);
|
||||
extern void spi_end(void *);
|
||||
extern void spi_txn(void *spi, uint8_t *tx, uint8_t *rx, size_t len);
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_spi spi_ = {NULL, spi_begin, spi_end, spi_txn}; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
mif_.ip = MG_TCPIP_IP; \
|
||||
mif_.mask = MG_TCPIP_MASK; \
|
||||
mif_.gw = MG_TCPIP_GW; \
|
||||
mif_.driver = &mg_tcpip_driver_w5500; \
|
||||
mif_.driver_data = &spi_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
MG_INFO(("Driver: W5500, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
Reference in new issue
Block a user