From 93f2939655d42f16d93243120c103fa8e77605ad Mon Sep 17 00:00:00 2001 From: cpq Date: Sat, 21 Nov 2026 11:18:25 +0000 Subject: [PATCH] Make w5500 driver like the rest --- mongoose.h | 47 ++++++++++++++++--- src/drivers/w5500.h | 27 +++++++++++ src/net_builtin.h | 6 ++- .../arduino/nano-w5500-mqtt/mongoose_config.h | 7 ++- .../arduino/nano-w5500-mqtt/w5500-mqtt.ino | 13 ++--- .../arduino/w5500-http/mongoose_config.h | 7 ++- tutorials/arduino/w5500-http/w5500-http.ino | 11 ++--- .../arduino/w5500-mqtt/mongoose_config.h | 7 ++- tutorials/arduino/w5500-mqtt/w5500-mqtt.ino | 13 ++--- tutorials/rp/w5500-evb-pico/main.c | 25 +++------- tutorials/rp/w5500-evb-pico/mongoose_config.h | 11 ++++- tutorials/rp/w55rp20-evb-pico/main.c | 18 +++---- .../rp/w55rp20-evb-pico/mongoose_config.h | 37 ++++----------- .../nucleo-g031k8/minimal-w5500/Makefile | 7 ++- .../stm32/nucleo-g031k8/minimal-w5500/main.c | 36 ++++++-------- .../minimal-w5500/mongoose/mongoose_config.h | 11 ----- .../minimal-w5500/mongoose_config.h | 29 ++++++++++++ 17 files changed, 182 insertions(+), 130 deletions(-) create mode 100644 src/drivers/w5500.h delete mode 100644 tutorials/stm32/nucleo-g031k8/minimal-w5500/mongoose/mongoose_config.h create mode 100644 tutorials/stm32/nucleo-g031k8/minimal-w5500/mongoose_config.h diff --git a/mongoose.h b/mongoose.h index c2a30096..6569d697 100644 --- a/mongoose.h +++ b/mongoose.h @@ -113,7 +113,9 @@ extern "C" { #if MG_ARCH == MG_ARCH_ARMGCC +#ifndef _POSIX_TIMERS // Some newlib builds, e.g. Espressif's, define it #define _POSIX_TIMERS +#endif #include #if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP @@ -189,16 +191,18 @@ extern "C" { #define MG_TLS MG_TLS_BUILTIN #endif -#if !defined(MG_OTA) && defined(STM32F1) || defined(STM32F2) || \ - defined(STM32F4) || defined(STM32F7) +#ifndef MG_OTA // Pick OTA for the STM32 family, unless set by the user +#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || \ + defined(STM32F7) #define MG_OTA MG_OTA_STM32F -#elif !defined(MG_OTA) && defined(STM32H5) +#elif defined(STM32H5) #define MG_OTA MG_OTA_STM32H5 -#elif !defined(MG_OTA) && defined(STM32H7) +#elif defined(STM32H7) #define MG_OTA MG_OTA_STM32H7 -#elif !defined(MG_OTA) && defined(STM32C5) +#elif defined(STM32C5) #define MG_OTA MG_OTA_STM32C5 #endif +#endif // use HAL-defined execute-in-ram section #ifndef MG_IRAM @@ -4842,7 +4846,6 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac); // Built-in hardware drivers. Assign one to mg_tcpip_if.driver before calling // mg_tcpip_init(). Each driver also requires a corresponding driver_data // struct; see the matching driver header for details. -extern struct mg_tcpip_driver mg_tcpip_driver_w5500; extern struct mg_tcpip_driver mg_tcpip_driver_w5100; extern struct mg_tcpip_driver mg_tcpip_driver_tm4c; extern struct mg_tcpip_driver mg_tcpip_driver_tms570; @@ -4867,6 +4870,7 @@ extern struct mg_tcpip_driver mg_tcpip_driver_netc; + // SPI bus abstraction for SPI-attached network chips (e.g. W5500). // Populate and assign to the driver's driver_data field. struct mg_tcpip_spi { @@ -4876,6 +4880,10 @@ struct mg_tcpip_spi { void (*txn)(void *, uint8_t *write, uint8_t *read, size_t); // Full-duplex transfer: write and read len bytes simultaneously }; +// SPI bus for SPI-attached network chips, defined by the application. +// Used by the drivers' INIT macros, e.g. MG_TCPIP_DRIVER_W5500_INIT +extern struct mg_tcpip_spi mg_spi; + // Alignment attributes for DMA-capable Ethernet descriptor buffers. // Use these on driver RX/TX descriptor arrays where the hardware requires // specific alignment. @@ -6038,6 +6046,33 @@ struct mg_tcpip_driver_tms570_data { +#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && \ + MG_ENABLE_DRIVER_W5500 + +extern struct mg_tcpip_driver mg_tcpip_driver_w5500; + +// Initialises W5500 on mgr. The application defines +// struct mg_tcpip_spi mg_spi. Example: tutorials/rp/w5500-evb-pico +#define MG_TCPIP_DRIVER_W5500_INIT(mgr) \ + do { \ + 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 = &mg_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) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_W5500_INIT(x) +#endif + +#endif + + #if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC struct mg_tcpip_driver_xmc_data { diff --git a/src/drivers/w5500.h b/src/drivers/w5500.h new file mode 100644 index 00000000..7b7ca37f --- /dev/null +++ b/src/drivers/w5500.h @@ -0,0 +1,27 @@ +#pragma once + +#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && \ + MG_ENABLE_DRIVER_W5500 + +extern struct mg_tcpip_driver mg_tcpip_driver_w5500; + +// Initialises W5500 on mgr. The application defines +// struct mg_tcpip_spi mg_spi. Example: tutorials/rp/w5500-evb-pico +#define MG_TCPIP_DRIVER_W5500_INIT(mgr) \ + do { \ + 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 = &mg_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) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_W5500_INIT(x) +#endif + +#endif diff --git a/src/net_builtin.h b/src/net_builtin.h index 40ce1243..2ca8c379 100644 --- a/src/net_builtin.h +++ b/src/net_builtin.h @@ -135,7 +135,6 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac); // Built-in hardware drivers. Assign one to mg_tcpip_if.driver before calling // mg_tcpip_init(). Each driver also requires a corresponding driver_data // struct; see the matching driver header for details. -extern struct mg_tcpip_driver mg_tcpip_driver_w5500; extern struct mg_tcpip_driver mg_tcpip_driver_w5100; extern struct mg_tcpip_driver mg_tcpip_driver_tm4c; extern struct mg_tcpip_driver mg_tcpip_driver_tms570; @@ -159,6 +158,7 @@ extern struct mg_tcpip_driver mg_tcpip_driver_netc; #include "drivers/nrf52.h" #include "drivers/rw612w.h" #include "drivers/stm32f.h" +#include "drivers/w5500.h" // SPI bus abstraction for SPI-attached network chips (e.g. W5500). // Populate and assign to the driver's driver_data field. @@ -169,6 +169,10 @@ struct mg_tcpip_spi { void (*txn)(void *, uint8_t *write, uint8_t *read, size_t); // Full-duplex transfer: write and read len bytes simultaneously }; +// SPI bus for SPI-attached network chips, defined by the application. +// Used by the drivers' INIT macros, e.g. MG_TCPIP_DRIVER_W5500_INIT +extern struct mg_tcpip_spi mg_spi; + // Alignment attributes for DMA-capable Ethernet descriptor buffers. // Use these on driver RX/TX descriptor arrays where the hardware requires // specific alignment. diff --git a/tutorials/arduino/nano-w5500-mqtt/mongoose_config.h b/tutorials/arduino/nano-w5500-mqtt/mongoose_config.h index e58a6157..84710470 100644 --- a/tutorials/arduino/nano-w5500-mqtt/mongoose_config.h +++ b/tutorials/arduino/nano-w5500-mqtt/mongoose_config.h @@ -10,7 +10,12 @@ #define MG_ARCH MG_ARCH_CUSTOM #define MG_ENABLE_TCPIP 1 #define MG_ENABLE_DRIVER_W5500 1 -#define MG_ENABLE_TCPIP_DRIVER_INIT 0 +// Fixed MAC address for the W5500 module +#define MG_SET_MAC_ADDRESS(mac) \ + do { \ + mac[0] = 2, mac[1] = 0, mac[2] = 1, mac[3] = 2; \ + mac[4] = 3, mac[5] = 5; \ + } while (0) #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 #define MG_ENABLE_CUSTOM_MILLIS 1 #define MG_ENABLE_CUSTOM_RANDOM 1 diff --git a/tutorials/arduino/nano-w5500-mqtt/w5500-mqtt.ino b/tutorials/arduino/nano-w5500-mqtt/w5500-mqtt.ino index 200ec5aa..5fb1153f 100644 --- a/tutorials/arduino/nano-w5500-mqtt/w5500-mqtt.ino +++ b/tutorials/arduino/nano-w5500-mqtt/w5500-mqtt.ino @@ -8,7 +8,8 @@ #define LED_PIN 21 // LED pin struct mg_connection *mqtt_connection; -struct mg_tcpip_spi spi = { +// SPI bus used by the W5500 driver, see MG_TCPIP_DRIVER_W5500_INIT +struct mg_tcpip_spi mg_spi = { NULL, // SPI metadata [](void *) { digitalWrite(SS_PIN, LOW); SPI.beginTransaction(SPISettings()); }, [](void *) { digitalWrite(SS_PIN, HIGH); SPI.endTransaction(); }, @@ -20,8 +21,7 @@ struct mg_tcpip_spi spi = { } }, }; -struct mg_mgr mgr; // Mongoose event manager -struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface +struct mg_mgr mgr; // Mongoose event manager uint64_t mg_millis(void) { return millis(); @@ -79,7 +79,7 @@ static void mqtt_ev_handler(struct mg_connection *c, int ev, void *ev_data) { } void reconnect_if_not_connected(void) { - if (mif.state == MG_TCPIP_STATE_READY && mqtt_connection == NULL) { + if (mgr.ifp->state == MG_TCPIP_STATE_READY && mqtt_connection == NULL) { struct mg_mqtt_opts opts = {}; opts.clean = true; mqtt_connection = @@ -95,12 +95,9 @@ void setup() { pinMode(SS_PIN, OUTPUT); // to communicate with W5500 Ethernet module pinMode(LED_PIN, OUTPUT); // Initialise LED - mg_mgr_init(&mgr); // Initialise Mongoose event manager mg_log_set(MG_LL_DEBUG); // Set debug log level mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); // Log serial - mif.driver = &mg_tcpip_driver_w5500; // Use W5500 built-in driver - mif.driver_data = &spi; // Pass SPI interface to W5500 driver - mg_tcpip_init(&mgr, &mif); // Initialise built-in TCP/IP stack + mg_mgr_init(&mgr); // Initialise Mongoose event manager and W5500 driver } void loop() { diff --git a/tutorials/arduino/w5500-http/mongoose_config.h b/tutorials/arduino/w5500-http/mongoose_config.h index 940edf63..03c192f5 100644 --- a/tutorials/arduino/w5500-http/mongoose_config.h +++ b/tutorials/arduino/w5500-http/mongoose_config.h @@ -10,7 +10,12 @@ #define MG_ARCH MG_ARCH_CUSTOM #define MG_ENABLE_TCPIP 1 #define MG_ENABLE_DRIVER_W5500 1 -#define MG_ENABLE_TCPIP_DRIVER_INIT 0 +// Fixed MAC address for the W5500 module +#define MG_SET_MAC_ADDRESS(mac) \ + do { \ + mac[0] = 2, mac[1] = 0, mac[2] = 1, mac[3] = 2; \ + mac[4] = 3, mac[5] = 5; \ + } while (0) #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 #define MG_ENABLE_CUSTOM_MILLIS 1 #define MG_ENABLE_CUSTOM_RANDOM 1 diff --git a/tutorials/arduino/w5500-http/w5500-http.ino b/tutorials/arduino/w5500-http/w5500-http.ino index 8890a1a0..49484424 100644 --- a/tutorials/arduino/w5500-http/w5500-http.ino +++ b/tutorials/arduino/w5500-http/w5500-http.ino @@ -4,7 +4,8 @@ #define LED_PIN LED_BUILTIN // LED pin #define SS_PIN 17 // Slave select pin for the W5500 module -struct mg_tcpip_spi spi = { +// SPI bus used by the W5500 driver, see MG_TCPIP_DRIVER_W5500_INIT +struct mg_tcpip_spi mg_spi = { NULL, // SPI metadata [](void *) { digitalWrite(SS_PIN, LOW); SPI.beginTransaction(SPISettings()); }, [](void *) { digitalWrite(SS_PIN, HIGH); SPI.endTransaction(); }, @@ -16,8 +17,7 @@ struct mg_tcpip_spi spi = { } }, }; -struct mg_mgr mgr; // Mongoose event manager -struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface +struct mg_mgr mgr; // Mongoose event manager // Used by Mongoose for time tracking uint64_t mg_millis(void) { @@ -99,12 +99,9 @@ void setup() { pinMode(SS_PIN, OUTPUT); // to communicate with W5500 Ethernet module pinMode(LED_PIN, OUTPUT); // Initialise LED - mg_mgr_init(&mgr); // Initialise Mongoose event manager mg_log_set(MG_LL_DEBUG); // Set debug log level mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); // Log serial - mif.driver = &mg_tcpip_driver_w5500; // Use W5500 built-in driver - mif.driver_data = &spi; // Pass SPI interface to W5500 driver - mg_tcpip_init(&mgr, &mif); // Initialise built-in TCP/IP stack + mg_mgr_init(&mgr); // Initialise Mongoose event manager and W5500 driver // Setup HTTP & HTTPS listeners. Respond "ok" on any HTTP request mg_http_listen(&mgr, "http://0.0.0.0:80", http_ev_handler, NULL); diff --git a/tutorials/arduino/w5500-mqtt/mongoose_config.h b/tutorials/arduino/w5500-mqtt/mongoose_config.h index 093742f7..afa62eb6 100644 --- a/tutorials/arduino/w5500-mqtt/mongoose_config.h +++ b/tutorials/arduino/w5500-mqtt/mongoose_config.h @@ -11,7 +11,12 @@ #define MG_ENABLE_SOCKET 0 #define MG_ENABLE_TCPIP 1 #define MG_ENABLE_DRIVER_W5500 1 -#define MG_ENABLE_TCPIP_DRIVER_INIT 0 +// Fixed MAC address for the W5500 module +#define MG_SET_MAC_ADDRESS(mac) \ + do { \ + mac[0] = 2, mac[1] = 0, mac[2] = 1, mac[3] = 2; \ + mac[4] = 3, mac[5] = 5; \ + } while (0) #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 #define MG_ENABLE_CUSTOM_MILLIS 1 #define MG_ENABLE_CUSTOM_RANDOM 1 diff --git a/tutorials/arduino/w5500-mqtt/w5500-mqtt.ino b/tutorials/arduino/w5500-mqtt/w5500-mqtt.ino index 5c3e79bb..1227584f 100644 --- a/tutorials/arduino/w5500-mqtt/w5500-mqtt.ino +++ b/tutorials/arduino/w5500-mqtt/w5500-mqtt.ino @@ -8,7 +8,8 @@ #define LED_PIN 21 // LED pin struct mg_connection *mqtt_connection; -struct mg_tcpip_spi spi = { +// SPI bus used by the W5500 driver, see MG_TCPIP_DRIVER_W5500_INIT +struct mg_tcpip_spi mg_spi = { NULL, // SPI metadata [](void *) { digitalWrite(SS_PIN, LOW); SPI.beginTransaction(SPISettings()); }, [](void *) { digitalWrite(SS_PIN, HIGH); SPI.endTransaction(); }, @@ -20,8 +21,7 @@ struct mg_tcpip_spi spi = { } }, }; -struct mg_mgr mgr; // Mongoose event manager -struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface +struct mg_mgr mgr; // Mongoose event manager uint64_t mg_millis(void) { return millis(); @@ -79,7 +79,7 @@ static void mqtt_ev_handler(struct mg_connection *c, int ev, void *ev_data) { } void reconnect_if_not_connected(void) { - if (mif.state == MG_TCPIP_STATE_READY && mqtt_connection == NULL) { + if (mgr.ifp->state == MG_TCPIP_STATE_READY && mqtt_connection == NULL) { struct mg_mqtt_opts opts = {}; opts.clean = true; mqtt_connection = @@ -95,12 +95,9 @@ void setup() { pinMode(SS_PIN, OUTPUT); // to communicate with W5500 Ethernet module pinMode(LED_PIN, OUTPUT); // Initialise LED - mg_mgr_init(&mgr); // Initialise Mongoose event manager mg_log_set(MG_LL_DEBUG); // Set debug log level mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); // Log serial - mif.driver = &mg_tcpip_driver_w5500; // Use W5500 built-in driver - mif.driver_data = &spi; // Pass SPI interface to W5500 driver - mg_tcpip_init(&mgr, &mif); // Initialise built-in TCP/IP stack + mg_mgr_init(&mgr); // Initialise Mongoose event manager and W5500 driver } void loop() { diff --git a/tutorials/rp/w5500-evb-pico/main.c b/tutorials/rp/w5500-evb-pico/main.c index 3f1ab143..9eaa04c2 100644 --- a/tutorials/rp/w5500-evb-pico/main.c +++ b/tutorials/rp/w5500-evb-pico/main.c @@ -3,7 +3,6 @@ #include "hardware/spi.h" #include "pico/stdlib.h" -#include "pico/unique_id.h" #include "mongoose.h" @@ -11,14 +10,17 @@ 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) { + (void) spi; gpio_put(SPI_CS, 0); } static void spi_end(void *spi) { + (void) spi; gpio_put(SPI_CS, 1); } static void spi_txn(void *spi, uint8_t *tx, uint8_t *rx, size_t len) { + (void) spi; if (tx != NULL && rx != NULL) { spi_write_read_blocking(spi0, tx, rx, len); } else if (tx != NULL) { @@ -28,6 +30,9 @@ static void spi_txn(void *spi, uint8_t *tx, uint8_t *rx, size_t len) { } } +// SPI bus used by the W5500 driver, see MG_TCPIP_DRIVER_W5500_INIT +struct mg_tcpip_spi mg_spi = {NULL, spi_begin, spi_end, spi_txn}; + static void ethernet_init(void) { // Init SPI pins spi_init(spi0, 500 * 1000); @@ -39,14 +44,6 @@ static void ethernet_init(void) { 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())) { @@ -106,17 +103,9 @@ static void log_fn(char ch, void *param) { // 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_log_set_fn(log_fn, NULL); 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); mg_http_listen(&mgr, "https://0.0.0.0", http_ev_handler, NULL); diff --git a/tutorials/rp/w5500-evb-pico/mongoose_config.h b/tutorials/rp/w5500-evb-pico/mongoose_config.h index d134f6b9..9c99db25 100644 --- a/tutorials/rp/w5500-evb-pico/mongoose_config.h +++ b/tutorials/rp/w5500-evb-pico/mongoose_config.h @@ -8,4 +8,13 @@ #define MG_ENABLE_TCPIP 1 #define MG_ENABLE_DRIVER_W5500 1 -#define MG_ENABLE_TCPIP_DRIVER_INIT 0 + +// Construct MAC address from the unique board ID +#define MG_SET_MAC_ADDRESS(mac) \ + do { \ + pico_unique_board_id_t id_; \ + pico_get_unique_board_id(&id_); \ + mac[0] = 2, mac[1] = id_.id[3]; \ + mac[2] = id_.id[4], mac[3] = id_.id[5]; \ + mac[4] = id_.id[6], mac[5] = id_.id[7]; \ + } while (0) diff --git a/tutorials/rp/w55rp20-evb-pico/main.c b/tutorials/rp/w55rp20-evb-pico/main.c index 55b52438..8e6545f6 100644 --- a/tutorials/rp/w55rp20-evb-pico/main.c +++ b/tutorials/rp/w55rp20-evb-pico/main.c @@ -2,7 +2,6 @@ // SPDX-License-Identifier: GPL-2.0-only or commercial #include "pico/stdlib.h" -#include "pico/unique_id.h" #include "hardware/pio.h" #include "mongoose.h" @@ -97,14 +96,6 @@ static void piospi_done(PIO pio, uint sm) { pio_interrupt_clear(pio, 0); } -// 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 pio_sm_config s_piospi_sm_config; static uint s_piospi_sm_addr; @@ -142,23 +133,26 @@ static void piospi_read(uint8_t *data, size_t len) { piospi_done(SPIPIO, SPISM); } -void spi_begin(void *spi) { +static void spi_begin(void *spi) { (void) spi; gpio_put(CSPIN, 0); } -void spi_end(void *spi) { +static void spi_end(void *spi) { (void) spi; gpio_put(CSPIN, 1); } // either write or read, not both -void spi_txn(void *arg, uint8_t *txdata, uint8_t *rxdata, size_t len) { +static void spi_txn(void *arg, uint8_t *txdata, uint8_t *rxdata, size_t len) { if (txdata != NULL) piospi_write(txdata, len); if (rxdata != NULL) piospi_read(rxdata, len); (void) arg; } +// SPI bus used by the W5500 driver, see MG_TCPIP_DRIVER_W5500_INIT +struct mg_tcpip_spi mg_spi = {NULL, spi_begin, spi_end, spi_txn}; + static void blink_task(void) { static uint64_t timer; diff --git a/tutorials/rp/w55rp20-evb-pico/mongoose_config.h b/tutorials/rp/w55rp20-evb-pico/mongoose_config.h index 078452cd..1ab9b080 100644 --- a/tutorials/rp/w55rp20-evb-pico/mongoose_config.h +++ b/tutorials/rp/w55rp20-evb-pico/mongoose_config.h @@ -16,33 +16,12 @@ // #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 -#include - -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)); \ +// Construct MAC address from the unique board ID +#define MG_SET_MAC_ADDRESS(mac) \ + do { \ + pico_unique_board_id_t id_; \ + pico_get_unique_board_id(&id_); \ + mac[0] = 2, mac[1] = id_.id[3]; \ + mac[2] = id_.id[4], mac[3] = id_.id[5]; \ + mac[4] = id_.id[6], mac[5] = id_.id[7]; \ } while (0) diff --git a/tutorials/stm32/nucleo-g031k8/minimal-w5500/Makefile b/tutorials/stm32/nucleo-g031k8/minimal-w5500/Makefile index 08d14472..da2c6d19 100644 --- a/tutorials/stm32/nucleo-g031k8/minimal-w5500/Makefile +++ b/tutorials/stm32/nucleo-g031k8/minimal-w5500/Makefile @@ -4,13 +4,12 @@ CFLAGS = -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion CFLAGS += -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion CFLAGS += -g3 -Os -ffunction-sections -fdata-sections -CFLAGS += -I. -Icmsis_core/CMSIS/Core/Include -Icmsis_g0/Include -Imongoose +CFLAGS += -I. -I../../../.. -Icmsis_core/CMSIS/Core/Include -Icmsis_g0/Include CFLAGS += -mcpu=cortex-m0 -mthumb -mfloat-abi=soft $(CFLAGS_EXTRA) LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs nano.specs -lc -lgcc -Wl,--gc-sections -SOURCES = main.c hal.c +SOURCES = main.c hal.c ../../../../mongoose.c SOURCES += cmsis_g0/Source/Templates/gcc/startup_stm32g031xx.s -SOURCES += mongoose/mongoose.c CFLAGS += -DHTTP_URL=\"http://0.0.0.0/\" @@ -19,7 +18,7 @@ all build example: firmware.bin mongoose/mongoose.c mongoose/mongoose.h: cp ../../../../mongoose.[ch] mongoose/ -firmware.elf: cmsis_core cmsis_g0 hal.h link.ld Makefile $(SOURCES) mongoose/mongoose.h +firmware.elf: cmsis_core cmsis_g0 hal.h link.ld Makefile $(SOURCES) ../../../../mongoose.h arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(CFLAGS_EXTRA) $(LDFLAGS) -o $@ firmware.bin: firmware.elf diff --git a/tutorials/stm32/nucleo-g031k8/minimal-w5500/main.c b/tutorials/stm32/nucleo-g031k8/minimal-w5500/main.c index 06d6eeb3..5d225935 100644 --- a/tutorials/stm32/nucleo-g031k8/minimal-w5500/main.c +++ b/tutorials/stm32/nucleo-g031k8/minimal-w5500/main.c @@ -21,6 +21,14 @@ static struct spi spi_pins = { .cs = PIN('A', 4), }; +// SPI bus used by the W5500 driver, see MG_TCPIP_DRIVER_W5500_INIT +struct mg_tcpip_spi mg_spi = { + .begin = (void (*)(void *)) spi_begin, + .end = (void (*)(void *)) spi_end, + .txn = (void (*)(void *, uint8_t *, uint8_t *, size_t)) spi_txn, + .spi = &spi_pins, +}; + static void log_fn(char ch, void *param) { uart_write_buf(param, &ch, 1); } @@ -82,35 +90,19 @@ int main(void) { // ethernet_init(); // Initialise ethernet pins MG_INFO(("Starting, CPU freq %g MHz", (double) SystemCoreClock / 1000000)); + spi_init(&spi_pins); // SPI must be ready before mg_mgr_init() struct mg_mgr mgr; // Initialise - mg_mgr_init(&mgr); // Mongoose event manager mg_log_set(MG_LL_DEBUG); // Set log level + mg_mgr_init(&mgr); // Mongoose event manager and W5500 driver - // Initialise Mongoose network stack - spi_init(&spi_pins); - struct mg_tcpip_spi spi = { - .begin = (void (*)(void *)) spi_begin, - .end = (void (*)(void *)) spi_end, - .txn = (void (*)(void *, uint8_t *, uint8_t *, size_t)) spi_txn, - .spi = &spi_pins, - }; - struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(), - // Uncomment below for static configuration: - // .ip = mg_htonl(MG_U32(192, 168, 0, 223)), - // .mask = mg_htonl(MG_U32(255, 255, 255, 0)), - // .gw = mg_htonl(MG_U32(192, 168, 0, 1)), - .driver = &mg_tcpip_driver_w5500, - .driver_data = &spi}; - mg_tcpip_init(&mgr, &mif); - - MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac)); - while (mif.state != MG_TCPIP_STATE_READY) { + MG_INFO(("Waiting for IP...")); + while (mgr.ifp->state != MG_TCPIP_STATE_READY) { mg_mgr_poll(&mgr, 0); } MG_INFO(("Initialising application...")); - mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &mif); - mg_http_listen(&mgr, "http://0.0.0.0:80", fn, &mif); + mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, mgr.ifp); + mg_http_listen(&mgr, "http://0.0.0.0:80", fn, mgr.ifp); MG_INFO(("Starting event loop")); for (;;) { diff --git a/tutorials/stm32/nucleo-g031k8/minimal-w5500/mongoose/mongoose_config.h b/tutorials/stm32/nucleo-g031k8/minimal-w5500/mongoose/mongoose_config.h deleted file mode 100644 index 43830256..00000000 --- a/tutorials/stm32/nucleo-g031k8/minimal-w5500/mongoose/mongoose_config.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -// See https://mongoose.ws/docs/getting-started/build-environment/ -#define MG_ARCH MG_ARCH_ARMGCC - -#define MG_ENABLE_TCPIP 1 -#define MG_ENABLE_DRIVER_W5500 1 -#define MG_ENABLE_CUSTOM_MILLIS 1 -#define MG_ENABLE_CUSTOM_RANDOM 1 -#define MG_IO_SIZE 128 -#define MG_ENABLE_LINES 1 diff --git a/tutorials/stm32/nucleo-g031k8/minimal-w5500/mongoose_config.h b/tutorials/stm32/nucleo-g031k8/minimal-w5500/mongoose_config.h new file mode 100644 index 00000000..6b609a6b --- /dev/null +++ b/tutorials/stm32/nucleo-g031k8/minimal-w5500/mongoose_config.h @@ -0,0 +1,29 @@ +#pragma once + +// See https://mongoose.ws/docs/getting-started/build-environment/ +#define MG_ARCH MG_ARCH_ARMGCC + +#define MG_ENABLE_TCPIP 1 +#define MG_ENABLE_DRIVER_W5500 1 +#define MG_ENABLE_CUSTOM_MILLIS 1 +#define MG_ENABLE_CUSTOM_RANDOM 1 +#define MG_IO_SIZE 128 +#define MG_ENABLE_LINES 1 + +// For static IP configuration, define MG_TCPIP_{IP,MASK,GW} +// #define MG_TCPIP_IP MG_IPV4(192, 168, 0, 223) +// #define MG_TCPIP_GW MG_IPV4(192, 168, 0, 1) +// #define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0) + +// Construct MAC address from the MCU unique ID. It is defined in the +// ST CMSIS header as UID_BASE +#define MGUID ((uint32_t *) 0x1FFF7590) // Unique 96-bit chip ID +#define MG_SET_MAC_ADDRESS(mac) \ + do { \ + mac[0] = 2; \ + mac[1] = MGUID[0] & 255; \ + mac[2] = (MGUID[0] >> 10) & 255; \ + mac[3] = (MGUID[0] >> 19) & 255; \ + mac[4] = MGUID[1] & 255; \ + mac[5] = MGUID[2] & 255; \ + } while (0)