Make w5500 driver like the rest

This commit is contained in:
cpq committed 2026-11-21 11:39:54 +00:00
1 parent 277741ca53
commit 93f2939655
17 files changed
+182 -130

No files matched your search

+7 -18
View File
@@ -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);
+10 -1
View File
@@ -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)
+6 -12
View File
@@ -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;
@@ -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 <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)); \
// 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)