From f7f7319698e961f9a40ce3f075046697c0b3ba5d Mon Sep 17 00:00:00 2001 From: cpq Date: Fri, 2 Sep 2022 12:58:35 +0100 Subject: [PATCH] Refactor W5500 driver, add arduino w5500 example --- examples/arduino/w5500/mongoose.c | 1 + examples/arduino/w5500/mongoose.h | 1 + examples/arduino/w5500/mongoose_custom.h | 12 ++++++ examples/arduino/w5500/w5500.ino | 48 ++++++++++++++++++++++++ mip/{driver_w5500.h => driver_w5500.c} | 0 5 files changed, 62 insertions(+) create mode 120000 examples/arduino/w5500/mongoose.c create mode 120000 examples/arduino/w5500/mongoose.h create mode 100644 examples/arduino/w5500/mongoose_custom.h create mode 100644 examples/arduino/w5500/w5500.ino rename mip/{driver_w5500.h => driver_w5500.c} (100%) diff --git a/examples/arduino/w5500/mongoose.c b/examples/arduino/w5500/mongoose.c new file mode 120000 index 00000000..5e522bbc --- /dev/null +++ b/examples/arduino/w5500/mongoose.c @@ -0,0 +1 @@ +../../../mongoose.c \ No newline at end of file diff --git a/examples/arduino/w5500/mongoose.h b/examples/arduino/w5500/mongoose.h new file mode 120000 index 00000000..ee4ac823 --- /dev/null +++ b/examples/arduino/w5500/mongoose.h @@ -0,0 +1 @@ +../../../mongoose.h \ No newline at end of file diff --git a/examples/arduino/w5500/mongoose_custom.h b/examples/arduino/w5500/mongoose_custom.h new file mode 100644 index 00000000..9d10e69a --- /dev/null +++ b/examples/arduino/w5500/mongoose_custom.h @@ -0,0 +1,12 @@ +#pragma once + +#include "Arduino.h" + +#include +#include +#include + +#define MG_ARCH MG_ARCH_CUSTOM +#define MG_ENABLE_SOCKET 0 +#define MG_ENABLE_MIP 1 +//#define MG_ENABLE_LOG 0 diff --git a/examples/arduino/w5500/w5500.ino b/examples/arduino/w5500/w5500.ino new file mode 100644 index 00000000..8231c0c0 --- /dev/null +++ b/examples/arduino/w5500/w5500.ino @@ -0,0 +1,48 @@ +#include +#include "mongoose.h" + +#define SS_PIN 3 // Slave select pin +struct mg_mgr mgr; // Mongoose event manager +struct mip_spi spi = { + NULL, // SPI data + [](void *) { digitalWrite(SS_PIN, LOW); }, // begin transation + [](void *) { digitalWrite(SS_PIN, HIGH); }, // end transaction + [](void *, uint8_t c) { return SPI.transfer(c); }, // execute transaction +}; + +void setup() { + Serial.begin(115200); + pinMode(SS_PIN, OUTPUT); + SPI.begin(); + + // Set logging function to a serial print + mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); + mg_mgr_init(&mgr); + + delay(3000); + MG_INFO(("Starting TCP/IP stack...")); + + // Init TCP/IP stack. Set MAC address. Set IP to 0, to enable DHCP + struct mip_cfg c = {.mac = {0, 0, 1, 2, 3, 4}, .ip = 0, .mask = 0, .gw = 0}; + mip_init(&mgr, &c, &mip_driver_w5500, &spi); + + // Start a 5 sec timer, print status message periodically + mg_timer_add( + &mgr, 5000, MG_TIMER_REPEAT, + [](void *) { + MG_INFO(("ethernet: %s", mip_driver_w5500.up(&spi) ? "up" : "down")); + }, + NULL); + + // Setup HTTP listener. Respond "ok" on any HTTP request + mg_http_listen( + &mgr, "http://0.0.0.0", + [](struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "ok\n"); + }, + &mgr); +} + +void loop() { + mg_mgr_poll(&mgr, 1); +} diff --git a/mip/driver_w5500.h b/mip/driver_w5500.c similarity index 100% rename from mip/driver_w5500.h rename to mip/driver_w5500.c