From bf013fcdaeead67083d7f63e3f8c3ce4eba1c78b Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Thu, 5 Dec 2024 11:33:17 -0300 Subject: [PATCH] Add Pico-W driver in src/drivedrivers --- .../CMakeLists.txt | 1 - .../driver_pico-w.h | 8 --- .../pico-w-picosdk-baremetal-builtin/main.c | 3 - .../mongoose_config.h | 1 + mongoose.c | 65 +++++++++++++++++++ mongoose.h | 14 ++++ .../driver_pico-w.c => src/drivers/pico-w.c | 52 +++++++-------- src/drivers/pico-w.h | 15 +++++ src/net_builtin.h | 1 + 9 files changed, 122 insertions(+), 38 deletions(-) delete mode 100644 examples/pico-sdk/pico-w-picosdk-baremetal-builtin/driver_pico-w.h rename examples/pico-sdk/pico-w-picosdk-baremetal-builtin/driver_pico-w.c => src/drivers/pico-w.c (57%) create mode 100644 src/drivers/pico-w.h diff --git a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/CMakeLists.txt b/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/CMakeLists.txt index bfbb283b07..5cde665983 100644 --- a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/CMakeLists.txt +++ b/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/CMakeLists.txt @@ -5,7 +5,6 @@ project(firmware) pico_sdk_init() add_executable(firmware - driver_pico-w.c main.c mongoose.c net.c diff --git a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/driver_pico-w.h b/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/driver_pico-w.h deleted file mode 100644 index 6e9fba587b..0000000000 --- a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/driver_pico-w.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -struct mg_tcpip_driver_pico_w_data { - char *ssid; - char *pass; -}; - -extern struct mg_tcpip_driver mg_tcpip_driver_pico_w; diff --git a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/main.c b/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/main.c index eef698d4ce..3bb398591a 100644 --- a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/main.c +++ b/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/main.c @@ -1,12 +1,9 @@ // Copyright (c) 2024 Cesanta Software Limited // All rights reserved -#include "pico/stdlib.h" - #include "mongoose.h" #include "net.h" -#include "driver_pico-w.h" #define WIFI_SSID "yourWiFiSSID" #define WIFI_PASS "yourWiFiPassword" diff --git a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/mongoose_config.h b/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/mongoose_config.h index 2c88e5c62e..1959cce8d4 100644 --- a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/mongoose_config.h +++ b/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/mongoose_config.h @@ -1,5 +1,6 @@ #define MG_ARCH MG_ARCH_PICOSDK #define MG_ENABLE_TCPIP 1 +#define MG_ENABLE_DRIVER_PICO_W 1 #define MG_ENABLE_TCPIP_DRIVER_INIT 0 #define MG_ENABLE_PACKED_FS 1 diff --git a/mongoose.c b/mongoose.c index 2a7f65f494..09ca219a49 100644 --- a/mongoose.c +++ b/mongoose.c @@ -17801,6 +17801,71 @@ bool mg_phy_up(struct mg_phy *phy, uint8_t phy_addr, bool *full_duplex, return up; } +#ifdef MG_ENABLE_LINES +#line 1 "src/drivers/pico-w.c" +#endif +#if MG_ENABLE_TCPIP && MG_ARCH == MG_ARCH_PICOSDK && \ + defined(MG_ENABLE_DRIVER_PICO_W) && MG_ENABLE_DRIVER_PICO_W + + + + + +static struct mg_tcpip_if *s_ifp; + +static bool mg_tcpip_driver_pico_w_init(struct mg_tcpip_if *ifp) { + struct mg_tcpip_driver_pico_w_data *d = + (struct mg_tcpip_driver_pico_w_data *) ifp->driver_data; + s_ifp = ifp; + if (cyw43_arch_init() != 0) + return false; // initialize async_context and WiFi chip + cyw43_arch_enable_sta_mode(); + // start connecting to network + if (cyw43_arch_wifi_connect_bssid_async(d->ssid, NULL, d->pass, + CYW43_AUTH_WPA2_AES_PSK) != 0) + return false; + cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac); + return true; +} + +static size_t mg_tcpip_driver_pico_w_tx(const void *buf, size_t len, + struct mg_tcpip_if *ifp) { + (void) ifp; + return cyw43_send_ethernet(&cyw43_state, CYW43_ITF_STA, len, buf, false) + ? 0 + : len; +} + +static bool mg_tcpip_driver_pico_w_up(struct mg_tcpip_if *ifp) { + (void) ifp; + return (cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA) == + CYW43_LINK_JOIN); +} + +struct mg_tcpip_driver mg_tcpip_driver_pico_w = { + mg_tcpip_driver_pico_w_init, + mg_tcpip_driver_pico_w_tx, + NULL, + mg_tcpip_driver_pico_w_up, +}; + +// Called once per outstanding frame by async_context +void cyw43_cb_process_ethernet(void *cb_data, int itf, size_t len, + const uint8_t *buf) { + if (itf != CYW43_ITF_STA) return; + mg_tcpip_qwrite((void *) buf, len, s_ifp); + (void) cb_data; +} + +// Called by async_context +void cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf) {} +void cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf) {} + +// there's life beyond lwIP +void pbuf_copy_partial(void) {(void) 0;} + +#endif + #ifdef MG_ENABLE_LINES #line 1 "src/drivers/ppp.c" #endif diff --git a/mongoose.h b/mongoose.h index 2b23e4cf89..63f83b6009 100644 --- a/mongoose.h +++ b/mongoose.h @@ -2785,6 +2785,7 @@ extern struct mg_tcpip_driver mg_tcpip_driver_ra; extern struct mg_tcpip_driver mg_tcpip_driver_xmc; extern struct mg_tcpip_driver mg_tcpip_driver_xmc7; extern struct mg_tcpip_driver mg_tcpip_driver_ppp; +extern struct mg_tcpip_driver mg_tcpip_driver_pico_w; // Drivers that require SPI, can use this SPI abstraction struct mg_tcpip_spi { @@ -2946,6 +2947,19 @@ void mg_phy_init(struct mg_phy *, uint8_t addr, uint8_t config); bool mg_phy_up(struct mg_phy *, uint8_t addr, bool *full_duplex, uint8_t *speed); +#if MG_ENABLE_TCPIP && MG_ARCH == MG_ARCH_PICOSDK && \ + defined(MG_ENABLE_DRIVER_PICO_W) && MG_ENABLE_DRIVER_PICO_W + +#include "cyw43.h" // keep this include +#include "pico/cyw43_arch.h" // keep this include +#include "pico/unique_id.h" // keep this include + +struct mg_tcpip_driver_pico_w_data { + char *ssid; + char *pass; +}; + +#endif struct mg_tcpip_driver_ppp_data { void *uart; // Opaque UART bus descriptor diff --git a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/driver_pico-w.c b/src/drivers/pico-w.c similarity index 57% rename from examples/pico-sdk/pico-w-picosdk-baremetal-builtin/driver_pico-w.c rename to src/drivers/pico-w.c index f8beb2b9af..fdcca98c7f 100644 --- a/examples/pico-sdk/pico-w-picosdk-baremetal-builtin/driver_pico-w.c +++ b/src/drivers/pico-w.c @@ -1,52 +1,51 @@ -// Copyright (c) 2024 Cesanta Software Limited -// All rights reserved - -#include -#include -#include +#if MG_ENABLE_TCPIP && MG_ARCH == MG_ARCH_PICOSDK && \ + defined(MG_ENABLE_DRIVER_PICO_W) && MG_ENABLE_DRIVER_PICO_W +#include "net_builtin.h" +#include "drivers/pico-w.h" #include "pico/stdlib.h" -#include "pico/unique_id.h" -#include "pico/cyw43_arch.h" -#include "cyw43.h" - -#include "mongoose.h" -#include "driver_pico-w.h" - static struct mg_tcpip_if *s_ifp; static bool mg_tcpip_driver_pico_w_init(struct mg_tcpip_if *ifp) { - struct mg_tcpip_driver_pico_w_data *d = (struct mg_tcpip_driver_pico_w_data *) ifp->driver_data; + struct mg_tcpip_driver_pico_w_data *d = + (struct mg_tcpip_driver_pico_w_data *) ifp->driver_data; s_ifp = ifp; - if (cyw43_arch_init() != 0) return false; // initialize async_context and WiFi chip + if (cyw43_arch_init() != 0) + return false; // initialize async_context and WiFi chip cyw43_arch_enable_sta_mode(); // start connecting to network - if (cyw43_arch_wifi_connect_bssid_async(d->ssid, NULL, d->pass, CYW43_AUTH_WPA2_AES_PSK) != 0) + if (cyw43_arch_wifi_connect_bssid_async(d->ssid, NULL, d->pass, + CYW43_AUTH_WPA2_AES_PSK) != 0) return false; cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac); return true; } static size_t mg_tcpip_driver_pico_w_tx(const void *buf, size_t len, - struct mg_tcpip_if *ifp) { - return cyw43_send_ethernet(&cyw43_state, CYW43_ITF_STA, len, buf, false) ? 0 : len; + struct mg_tcpip_if *ifp) { (void) ifp; + return cyw43_send_ethernet(&cyw43_state, CYW43_ITF_STA, len, buf, false) + ? 0 + : len; } static bool mg_tcpip_driver_pico_w_up(struct mg_tcpip_if *ifp) { - return (cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA) == CYW43_LINK_JOIN); + (void) ifp; + return (cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA) == + CYW43_LINK_JOIN); } struct mg_tcpip_driver mg_tcpip_driver_pico_w = { - mg_tcpip_driver_pico_w_init, - mg_tcpip_driver_pico_w_tx, - NULL, - mg_tcpip_driver_pico_w_up, + mg_tcpip_driver_pico_w_init, + mg_tcpip_driver_pico_w_tx, + NULL, + mg_tcpip_driver_pico_w_up, }; // Called once per outstanding frame by async_context -void cyw43_cb_process_ethernet(void *cb_data, int itf, size_t len, const uint8_t *buf) { +void cyw43_cb_process_ethernet(void *cb_data, int itf, size_t len, + const uint8_t *buf) { if (itf != CYW43_ITF_STA) return; mg_tcpip_qwrite((void *) buf, len, s_ifp); (void) cb_data; @@ -56,6 +55,7 @@ void cyw43_cb_process_ethernet(void *cb_data, int itf, size_t len, const uint8_t void cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf) {} void cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf) {} - // there's life beyond lwIP -void pbuf_copy_partial(void){(void)0;} +void pbuf_copy_partial(void) {(void) 0;} + +#endif diff --git a/src/drivers/pico-w.h b/src/drivers/pico-w.h new file mode 100644 index 0000000000..fd6837c9e2 --- /dev/null +++ b/src/drivers/pico-w.h @@ -0,0 +1,15 @@ +#pragma once + +#if MG_ENABLE_TCPIP && MG_ARCH == MG_ARCH_PICOSDK && \ + defined(MG_ENABLE_DRIVER_PICO_W) && MG_ENABLE_DRIVER_PICO_W + +#include "cyw43.h" // keep this include +#include "pico/cyw43_arch.h" // keep this include +#include "pico/unique_id.h" // keep this include + +struct mg_tcpip_driver_pico_w_data { + char *ssid; + char *pass; +}; + +#endif diff --git a/src/net_builtin.h b/src/net_builtin.h index 2f1c899c76..155365362f 100644 --- a/src/net_builtin.h +++ b/src/net_builtin.h @@ -82,6 +82,7 @@ extern struct mg_tcpip_driver mg_tcpip_driver_ra; extern struct mg_tcpip_driver mg_tcpip_driver_xmc; extern struct mg_tcpip_driver mg_tcpip_driver_xmc7; extern struct mg_tcpip_driver mg_tcpip_driver_ppp; +extern struct mg_tcpip_driver mg_tcpip_driver_pico_w; // Drivers that require SPI, can use this SPI abstraction struct mg_tcpip_spi {