diff --git a/mongoose.h b/mongoose.h index f38dfe6b..10040b81 100644 --- a/mongoose.h +++ b/mongoose.h @@ -4847,26 +4847,25 @@ void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp); 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; -extern struct mg_tcpip_driver mg_tcpip_driver_stm32h; -extern struct mg_tcpip_driver mg_tcpip_driver_imxrt; -extern struct mg_tcpip_driver mg_tcpip_driver_same54; -extern struct mg_tcpip_driver mg_tcpip_driver_cmsis; -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_pico_w; -extern struct mg_tcpip_driver mg_tcpip_driver_rw612; -extern struct mg_tcpip_driver mg_tcpip_driver_cyw; -extern struct mg_tcpip_driver mg_tcpip_driver_nxp_wifi; -extern struct mg_tcpip_driver mg_tcpip_driver_st67w6; -extern struct mg_tcpip_driver mg_tcpip_driver_atcmd; -extern struct mg_tcpip_driver mg_tcpip_driver_netc; +// mg_tcpip_init(). See the matching driver header for configuration and +// initialisation helpers. + + + + + + + + + + + + + + + + + @@ -4884,6 +4883,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. @@ -5298,12 +5301,58 @@ struct mg_tcpip_driver_atcmd_data { bool unstuffing; // internal use: unstuffing state }; +#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD + +extern struct mg_tcpip_driver mg_tcpip_driver_atcmd; + +// Modem callbacks and AT script, defined by the application. +// See tutorials/arduino/sim800-mqtt for modem configuration. +extern struct mg_tcpip_driver_atcmd_data mg_atcmd; + +#define MG_TCPIP_DRIVER_ATCMD_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_atcmd; \ + mif_.driver_data = &mg_atcmd; \ + mif_.l2type = MG_TCPIP_L2_PPP; \ + MG_SET_MAC_ADDRESS(mif_.mac); \ + mg_tcpip_init((mgr), &mif_); \ + MG_INFO(("Driver: atcmd, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ATCMD_INIT(x) +#endif + +#endif + #if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_CMSIS) && MG_ENABLE_DRIVER_CMSIS #include "Driver_ETH_MAC.h" // keep this include #include "Driver_ETH_PHY.h" // keep this include +extern struct mg_tcpip_driver mg_tcpip_driver_cmsis; + +#define MG_TCPIP_DRIVER_CMSIS_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_cmsis; \ + MG_SET_MAC_ADDRESS(mif_.mac); \ + mg_tcpip_init((mgr), &mif_); \ + MG_INFO(("Driver: cmsis, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_CMSIS_INIT(x) +#endif + #endif @@ -5327,7 +5376,9 @@ struct mg_tcpip_driver_cyw_data { bool hs; // use chip "high-speed" mode; otherwise SPI CPOL0 CPHA0 (DS 4.2.3 Table 6) }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_cyw; + +#define MG_TCPIP_DRIVER_CYW_INIT(mgr) \ do { \ static struct mg_tcpip_driver_cyw_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -5339,10 +5390,14 @@ struct mg_tcpip_driver_cyw_data { mif_.driver_data = &driver_data_; \ mif_.recv_queue.size = 8192; \ mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: cyw, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_CYW_INIT(x) +#endif + #endif // Copyright (c) 2026 Cesanta Software Limited // All rights reserved @@ -5414,7 +5469,9 @@ struct mg_tcpip_driver_imxrt_data { #define MG_DRIVER_MDC_CR 24 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_imxrt; + +#define MG_TCPIP_DRIVER_IMXRT_INIT(mgr) \ do { \ static struct mg_tcpip_driver_imxrt_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -5426,10 +5483,14 @@ struct mg_tcpip_driver_imxrt_data { mif_.driver = &mg_tcpip_driver_imxrt; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: imxrt, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_IMXRT_INIT(x) +#endif + #endif // Copyright (c) 2026 Cesanta Software Limited // All rights reserved @@ -5526,10 +5587,9 @@ struct mg_tcpip_driver_netc_data { #define MG_TCPIP_NETC_LINK 0 #endif -struct mg_tcpip_driver; extern struct mg_tcpip_driver mg_tcpip_driver_netc; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +#define MG_TCPIP_DRIVER_NETC_INIT(mgr) \ do { \ static struct mg_tcpip_driver_netc_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -5542,10 +5602,14 @@ extern struct mg_tcpip_driver mg_tcpip_driver_netc; mif_.driver = &mg_tcpip_driver_netc; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: netc, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_NETC_INIT(x) +#endif + #endif // Copyright (c) 2026 Cesanta Software Limited // All rights reserved @@ -5591,7 +5655,9 @@ struct mg_tcpip_driver_nxp_wifi_data { }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_nxp_wifi; + +#define MG_TCPIP_DRIVER_NXP_WIFI_INIT(mgr) \ do { \ static struct mg_tcpip_driver_nxp_wifi_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -5603,10 +5669,14 @@ struct mg_tcpip_driver_nxp_wifi_data { mif_.driver_data = &driver_data_; \ mif_.recv_queue.size = 8192; \ mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: nxp wifi, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_NXP_WIFI_INIT(x) +#endif + #endif @@ -5643,7 +5713,9 @@ struct mg_tcpip_driver_pico_w_data { struct mg_wifi_data wifi; }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_pico_w; + +#define MG_TCPIP_DRIVER_PICO_W_INIT(mgr) \ do { \ static struct mg_tcpip_driver_pico_w_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -5655,10 +5727,14 @@ struct mg_tcpip_driver_pico_w_data { mif_.driver_data = &driver_data_; \ mif_.recv_queue.size = 8192; \ mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: pico-w, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_PICO_W_INIT(x) +#endif + #endif @@ -5685,7 +5761,9 @@ struct mg_tcpip_driver_ra_data { #define MG_TCPIP_PHY_ADDR 0 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_ra; + +#define MG_TCPIP_DRIVER_RA_INIT(mgr) \ do { \ static struct mg_tcpip_driver_ra_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -5698,10 +5776,14 @@ struct mg_tcpip_driver_ra_data { mif_.driver = &mg_tcpip_driver_ra; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: ra, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_RA_INIT(x) +#endif + #endif @@ -5732,8 +5814,9 @@ struct mg_tcpip_driver_rw612_data { #define MG_DRIVER_MDC_HOLDTIME 3 #endif -#ifndef MG_TCPIP_DRIVER_INIT -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_rw612; + +#define MG_TCPIP_DRIVER_RW612_INIT(mgr) \ do { \ static struct mg_tcpip_driver_rw612_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -5745,9 +5828,12 @@ struct mg_tcpip_driver_rw612_data { mif_.driver = &mg_tcpip_driver_rw612; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: rw612, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_RW612_INIT(x) #endif #endif @@ -5803,7 +5889,9 @@ struct mg_tcpip_driver_same54_data { uint8_t phy_addr; }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_same54; + +#define MG_TCPIP_DRIVER_SAME54_INIT(mgr) \ do { \ static struct mg_tcpip_driver_same54_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -5815,10 +5903,14 @@ struct mg_tcpip_driver_same54_data { mif_.driver = &mg_tcpip_driver_same54; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: same54, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_SAME54_INIT(x) +#endif + #endif @@ -5871,7 +5963,9 @@ struct mg_tcpip_driver_st67w6_data { struct mg_queue send_queue; // decouple tx calls from module polls }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_st67w6; + +#define MG_TCPIP_DRIVER_ST67W6_INIT(mgr) \ do { \ static struct mg_tcpip_driver_st67w6_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -5883,10 +5977,14 @@ struct mg_tcpip_driver_st67w6_data { mif_.driver_data = &driver_data_; \ mif_.recv_queue.size = 8192; \ mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: st67w6, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ST67W6_INIT(x) +#endif + #endif @@ -6022,7 +6120,9 @@ struct mg_tcpip_driver_stm32h_data { #define MG_IPV6_INIT(mif) #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_stm32h; + +#define MG_TCPIP_DRIVER_STM32H_INIT(mgr) \ do { \ static struct mg_tcpip_driver_stm32h_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -6036,11 +6136,15 @@ struct mg_tcpip_driver_stm32h_data { mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ MG_IPV6_INIT(mif_); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_ENABLE_ETH_IRQ(); \ MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_STM32H_INIT(x) +#endif + #endif #endif @@ -6064,7 +6168,9 @@ struct mg_tcpip_driver_tm4c_data { #define MG_DRIVER_MDC_CR 1 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_tm4c; + +#define MG_TCPIP_DRIVER_TM4C_INIT(mgr) \ do { \ static struct mg_tcpip_driver_tm4c_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -6075,10 +6181,14 @@ struct mg_tcpip_driver_tm4c_data { mif_.driver = &mg_tcpip_driver_tm4c; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: tm4c, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_TM4C_INIT(x) +#endif + #endif @@ -6096,7 +6206,9 @@ struct mg_tcpip_driver_tms570_data { #define MG_DRIVER_MDC_CR 1 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_tms570; + +#define MG_TCPIP_DRIVER_TMS570_INIT(mgr) \ do { \ static struct mg_tcpip_driver_tms570_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -6108,11 +6220,70 @@ struct mg_tcpip_driver_tms570_data { mif_.driver = &mg_tcpip_driver_tms570; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: tms570, MAC: %M", mg_print_mac, mif_.mac));\ } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_TMS570_INIT(x) #endif +#endif + + + +#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5100) && \ + MG_ENABLE_DRIVER_W5100 + +extern struct mg_tcpip_driver mg_tcpip_driver_w5100; + +// Initialises W5100 on mgr. The application defines +// struct mg_tcpip_spi mg_spi +#define MG_TCPIP_DRIVER_W5100_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_w5100; \ + mif_.driver_data = &mg_spi; \ + MG_SET_MAC_ADDRESS(mif_.mac); \ + mg_tcpip_init((mgr), &mif_); \ + MG_INFO(("Driver: w5100, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_W5100_INIT(x) +#endif + +#endif + + +#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 @@ -6143,7 +6314,9 @@ struct mg_tcpip_driver_xmc_data { #define MG_DRIVER_MDC_CR 4 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_xmc; + +#define MG_TCPIP_DRIVER_XMC_INIT(mgr) \ do { \ static struct mg_tcpip_driver_xmc_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -6155,10 +6328,14 @@ struct mg_tcpip_driver_xmc_data { mif_.driver = &mg_tcpip_driver_xmc; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_XMC_INIT(x) +#endif + #endif @@ -6177,7 +6354,9 @@ struct mg_tcpip_driver_xmc7_data { #define MG_DRIVER_MDC_CR 3 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_xmc7; + +#define MG_TCPIP_DRIVER_XMC7_INIT(mgr) \ do { \ static struct mg_tcpip_driver_xmc7_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -6189,10 +6368,14 @@ struct mg_tcpip_driver_xmc7_data { mif_.driver = &mg_tcpip_driver_xmc7; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_XMC7_INIT(x) +#endif + #endif diff --git a/src/drivers/at_cmd.h b/src/drivers/at_cmd.h index ea49cf8e..87b8373e 100644 --- a/src/drivers/at_cmd.h +++ b/src/drivers/at_cmd.h @@ -12,3 +12,31 @@ struct mg_tcpip_driver_atcmd_data { bool link; // internal use: done with AT processing bool unstuffing; // internal use: unstuffing state }; + +#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD + +extern struct mg_tcpip_driver mg_tcpip_driver_atcmd; + +// Modem callbacks and AT script, defined by the application. +// See tutorials/arduino/sim800-mqtt for modem configuration. +extern struct mg_tcpip_driver_atcmd_data mg_atcmd; + +#define MG_TCPIP_DRIVER_ATCMD_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_atcmd; \ + mif_.driver_data = &mg_atcmd; \ + mif_.l2type = MG_TCPIP_L2_PPP; \ + MG_SET_MAC_ADDRESS(mif_.mac); \ + mg_tcpip_init((mgr), &mif_); \ + MG_INFO(("Driver: atcmd, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ATCMD_INIT(x) +#endif + +#endif diff --git a/src/drivers/cmsis.h b/src/drivers/cmsis.h index f8b4dadd..c7a4e861 100644 --- a/src/drivers/cmsis.h +++ b/src/drivers/cmsis.h @@ -5,4 +5,22 @@ #include "Driver_ETH_MAC.h" // keep this include #include "Driver_ETH_PHY.h" // keep this include +extern struct mg_tcpip_driver mg_tcpip_driver_cmsis; + +#define MG_TCPIP_DRIVER_CMSIS_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_cmsis; \ + MG_SET_MAC_ADDRESS(mif_.mac); \ + mg_tcpip_init((mgr), &mif_); \ + MG_INFO(("Driver: cmsis, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_CMSIS_INIT(x) +#endif + #endif diff --git a/src/drivers/cyw.h b/src/drivers/cyw.h index 0c342867..c0f3b71c 100644 --- a/src/drivers/cyw.h +++ b/src/drivers/cyw.h @@ -20,7 +20,9 @@ struct mg_tcpip_driver_cyw_data { bool hs; // use chip "high-speed" mode; otherwise SPI CPOL0 CPHA0 (DS 4.2.3 Table 6) }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_cyw; + +#define MG_TCPIP_DRIVER_CYW_INIT(mgr) \ do { \ static struct mg_tcpip_driver_cyw_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -32,8 +34,12 @@ struct mg_tcpip_driver_cyw_data { mif_.driver_data = &driver_data_; \ mif_.recv_queue.size = 8192; \ mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: cyw, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_CYW_INIT(x) +#endif + #endif diff --git a/src/drivers/imxrt.h b/src/drivers/imxrt.h index 84690b8c..97790988 100644 --- a/src/drivers/imxrt.h +++ b/src/drivers/imxrt.h @@ -30,7 +30,9 @@ struct mg_tcpip_driver_imxrt_data { #define MG_DRIVER_MDC_CR 24 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_imxrt; + +#define MG_TCPIP_DRIVER_IMXRT_INIT(mgr) \ do { \ static struct mg_tcpip_driver_imxrt_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -42,8 +44,12 @@ struct mg_tcpip_driver_imxrt_data { mif_.driver = &mg_tcpip_driver_imxrt; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: imxrt, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_IMXRT_INIT(x) +#endif + #endif diff --git a/src/drivers/netc.h b/src/drivers/netc.h index 995651e5..4a1eb36d 100644 --- a/src/drivers/netc.h +++ b/src/drivers/netc.h @@ -26,10 +26,9 @@ struct mg_tcpip_driver_netc_data { #define MG_TCPIP_NETC_LINK 0 #endif -struct mg_tcpip_driver; extern struct mg_tcpip_driver mg_tcpip_driver_netc; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +#define MG_TCPIP_DRIVER_NETC_INIT(mgr) \ do { \ static struct mg_tcpip_driver_netc_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -42,8 +41,12 @@ extern struct mg_tcpip_driver mg_tcpip_driver_netc; mif_.driver = &mg_tcpip_driver_netc; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: netc, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_NETC_INIT(x) +#endif + #endif diff --git a/src/drivers/nxp_wifi.h b/src/drivers/nxp_wifi.h index 2f8fe385..d043d637 100644 --- a/src/drivers/nxp_wifi.h +++ b/src/drivers/nxp_wifi.h @@ -9,7 +9,9 @@ struct mg_tcpip_driver_nxp_wifi_data { }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_nxp_wifi; + +#define MG_TCPIP_DRIVER_NXP_WIFI_INIT(mgr) \ do { \ static struct mg_tcpip_driver_nxp_wifi_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -21,8 +23,12 @@ struct mg_tcpip_driver_nxp_wifi_data { mif_.driver_data = &driver_data_; \ mif_.recv_queue.size = 8192; \ mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: nxp wifi, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_NXP_WIFI_INIT(x) +#endif + #endif diff --git a/src/drivers/pico-w.h b/src/drivers/pico-w.h index ffafe6f3..3a32112e 100644 --- a/src/drivers/pico-w.h +++ b/src/drivers/pico-w.h @@ -11,7 +11,9 @@ struct mg_tcpip_driver_pico_w_data { struct mg_wifi_data wifi; }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_pico_w; + +#define MG_TCPIP_DRIVER_PICO_W_INIT(mgr) \ do { \ static struct mg_tcpip_driver_pico_w_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -23,8 +25,12 @@ struct mg_tcpip_driver_pico_w_data { mif_.driver_data = &driver_data_; \ mif_.recv_queue.size = 8192; \ mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: pico-w, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_PICO_W_INIT(x) +#endif + #endif diff --git a/src/drivers/ra.h b/src/drivers/ra.h index 13a54944..dde093f8 100644 --- a/src/drivers/ra.h +++ b/src/drivers/ra.h @@ -23,7 +23,9 @@ struct mg_tcpip_driver_ra_data { #define MG_TCPIP_PHY_ADDR 0 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_ra; + +#define MG_TCPIP_DRIVER_RA_INIT(mgr) \ do { \ static struct mg_tcpip_driver_ra_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -36,8 +38,12 @@ struct mg_tcpip_driver_ra_data { mif_.driver = &mg_tcpip_driver_ra; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: ra, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_RA_INIT(x) +#endif + #endif diff --git a/src/drivers/rw612.h b/src/drivers/rw612.h index 0d2d2e3f..aa22e213 100644 --- a/src/drivers/rw612.h +++ b/src/drivers/rw612.h @@ -27,8 +27,9 @@ struct mg_tcpip_driver_rw612_data { #define MG_DRIVER_MDC_HOLDTIME 3 #endif -#ifndef MG_TCPIP_DRIVER_INIT -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_rw612; + +#define MG_TCPIP_DRIVER_RW612_INIT(mgr) \ do { \ static struct mg_tcpip_driver_rw612_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -40,9 +41,12 @@ struct mg_tcpip_driver_rw612_data { mif_.driver = &mg_tcpip_driver_rw612; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: rw612, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_RW612_INIT(x) #endif #endif diff --git a/src/drivers/same54.h b/src/drivers/same54.h index ca6e5e5b..910eb02e 100644 --- a/src/drivers/same54.h +++ b/src/drivers/same54.h @@ -16,7 +16,9 @@ struct mg_tcpip_driver_same54_data { uint8_t phy_addr; }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_same54; + +#define MG_TCPIP_DRIVER_SAME54_INIT(mgr) \ do { \ static struct mg_tcpip_driver_same54_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -28,8 +30,12 @@ struct mg_tcpip_driver_same54_data { mif_.driver = &mg_tcpip_driver_same54; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: same54, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_SAME54_INIT(x) +#endif + #endif diff --git a/src/drivers/st67w6.h b/src/drivers/st67w6.h index b930b8bb..8869a915 100644 --- a/src/drivers/st67w6.h +++ b/src/drivers/st67w6.h @@ -10,7 +10,9 @@ struct mg_tcpip_driver_st67w6_data { struct mg_queue send_queue; // decouple tx calls from module polls }; -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_st67w6; + +#define MG_TCPIP_DRIVER_ST67W6_INIT(mgr) \ do { \ static struct mg_tcpip_driver_st67w6_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -22,8 +24,12 @@ struct mg_tcpip_driver_st67w6_data { mif_.driver_data = &driver_data_; \ mif_.recv_queue.size = 8192; \ mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: st67w6, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ST67W6_INIT(x) +#endif + #endif diff --git a/src/drivers/stm32h.h b/src/drivers/stm32h.h index 834d5377..c25e3cae 100644 --- a/src/drivers/stm32h.h +++ b/src/drivers/stm32h.h @@ -72,7 +72,9 @@ struct mg_tcpip_driver_stm32h_data { #define MG_IPV6_INIT(mif) #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_stm32h; + +#define MG_TCPIP_DRIVER_STM32H_INIT(mgr) \ do { \ static struct mg_tcpip_driver_stm32h_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -86,10 +88,14 @@ struct mg_tcpip_driver_stm32h_data { mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ MG_IPV6_INIT(mif_); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_ENABLE_ETH_IRQ(); \ MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_STM32H_INIT(x) +#endif + #endif #endif diff --git a/src/drivers/tm4c.h b/src/drivers/tm4c.h index acd11d03..3bb4eb0a 100644 --- a/src/drivers/tm4c.h +++ b/src/drivers/tm4c.h @@ -19,7 +19,9 @@ struct mg_tcpip_driver_tm4c_data { #define MG_DRIVER_MDC_CR 1 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_tm4c; + +#define MG_TCPIP_DRIVER_TM4C_INIT(mgr) \ do { \ static struct mg_tcpip_driver_tm4c_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -30,8 +32,12 @@ struct mg_tcpip_driver_tm4c_data { mif_.driver = &mg_tcpip_driver_tm4c; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: tm4c, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_TM4C_INIT(x) +#endif + #endif diff --git a/src/drivers/tms570.h b/src/drivers/tms570.h index 1d48e6e3..fdcdd422 100644 --- a/src/drivers/tms570.h +++ b/src/drivers/tms570.h @@ -14,7 +14,9 @@ struct mg_tcpip_driver_tms570_data { #define MG_DRIVER_MDC_CR 1 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_tms570; + +#define MG_TCPIP_DRIVER_TMS570_INIT(mgr) \ do { \ static struct mg_tcpip_driver_tms570_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -26,8 +28,13 @@ struct mg_tcpip_driver_tms570_data { mif_.driver = &mg_tcpip_driver_tms570; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: tms570, MAC: %M", mg_print_mac, mif_.mac));\ } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_TMS570_INIT(x) +#endif + #endif diff --git a/src/drivers/w5100.h b/src/drivers/w5100.h new file mode 100644 index 00000000..ead9b6ef --- /dev/null +++ b/src/drivers/w5100.h @@ -0,0 +1,27 @@ +#pragma once + +#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5100) && \ + MG_ENABLE_DRIVER_W5100 + +extern struct mg_tcpip_driver mg_tcpip_driver_w5100; + +// Initialises W5100 on mgr. The application defines +// struct mg_tcpip_spi mg_spi +#define MG_TCPIP_DRIVER_W5100_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_w5100; \ + mif_.driver_data = &mg_spi; \ + MG_SET_MAC_ADDRESS(mif_.mac); \ + mg_tcpip_init((mgr), &mif_); \ + MG_INFO(("Driver: w5100, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) + +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_W5100_INIT(x) +#endif + +#endif 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/drivers/xmc.h b/src/drivers/xmc.h index 4dda11c3..db4421d3 100644 --- a/src/drivers/xmc.h +++ b/src/drivers/xmc.h @@ -28,7 +28,9 @@ struct mg_tcpip_driver_xmc_data { #define MG_DRIVER_MDC_CR 4 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_xmc; + +#define MG_TCPIP_DRIVER_XMC_INIT(mgr) \ do { \ static struct mg_tcpip_driver_xmc_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -40,8 +42,12 @@ struct mg_tcpip_driver_xmc_data { mif_.driver = &mg_tcpip_driver_xmc; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_XMC_INIT(x) +#endif + #endif diff --git a/src/drivers/xmc7.h b/src/drivers/xmc7.h index c59fc203..ed0f4417 100644 --- a/src/drivers/xmc7.h +++ b/src/drivers/xmc7.h @@ -15,7 +15,9 @@ struct mg_tcpip_driver_xmc7_data { #define MG_DRIVER_MDC_CR 3 #endif -#define MG_TCPIP_DRIVER_INIT(mgr) \ +extern struct mg_tcpip_driver mg_tcpip_driver_xmc7; + +#define MG_TCPIP_DRIVER_XMC7_INIT(mgr) \ do { \ static struct mg_tcpip_driver_xmc7_data driver_data_; \ static struct mg_tcpip_if mif_; \ @@ -27,9 +29,13 @@ struct mg_tcpip_driver_xmc7_data { mif_.driver = &mg_tcpip_driver_xmc7; \ mif_.driver_data = &driver_data_; \ MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ + mg_tcpip_init((mgr), &mif_); \ MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \ } while (0) +#ifndef MG_TCPIP_DRIVER_INIT +#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_XMC7_INIT(x) +#endif + #endif diff --git a/src/net_builtin.h b/src/net_builtin.h index 76141a96..43d3f7b3 100644 --- a/src/net_builtin.h +++ b/src/net_builtin.h @@ -133,32 +133,31 @@ void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp); 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; -extern struct mg_tcpip_driver mg_tcpip_driver_stm32h; -extern struct mg_tcpip_driver mg_tcpip_driver_imxrt; -extern struct mg_tcpip_driver mg_tcpip_driver_same54; -extern struct mg_tcpip_driver mg_tcpip_driver_cmsis; -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_pico_w; -extern struct mg_tcpip_driver mg_tcpip_driver_rw612; -extern struct mg_tcpip_driver mg_tcpip_driver_cyw; -extern struct mg_tcpip_driver mg_tcpip_driver_nxp_wifi; -extern struct mg_tcpip_driver mg_tcpip_driver_st67w6; -extern struct mg_tcpip_driver mg_tcpip_driver_atcmd; -extern struct mg_tcpip_driver mg_tcpip_driver_netc; - -#include "drivers/mg24.h" +// mg_tcpip_init(). See the matching driver header for configuration and +// initialisation helpers. +#include "drivers/at_cmd.h" +#include "drivers/cmsis.h" +#include "drivers/cyw.h" +#include "drivers/imxrt.h" #include "drivers/mcxw71.h" +#include "drivers/mg24.h" +#include "drivers/netc.h" #include "drivers/nrf52.h" +#include "drivers/nxp_wifi.h" +#include "drivers/pico-w.h" +#include "drivers/ra.h" +#include "drivers/rw612.h" #include "drivers/rw612w.h" +#include "drivers/same54.h" +#include "drivers/st67w6.h" #include "drivers/stm32f.h" +#include "drivers/stm32h.h" +#include "drivers/tm4c.h" +#include "drivers/tms570.h" +#include "drivers/w5100.h" +#include "drivers/w5500.h" +#include "drivers/xmc.h" +#include "drivers/xmc7.h" #include "drivers/esp32_802154.h" // SPI bus abstraction for SPI-attached network chips (e.g. W5500). @@ -170,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)