diff --git a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c index 19ad4a75abf..25a2a76e94d 100644 --- a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c +++ b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c @@ -24,12 +24,8 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG } static void timer_fn(void *arg) { - gpio_toggle(LED); // Blink LED - struct mg_tcpip_if *ifp = arg; // And show - const char *names[] = {"down", "up", "req", "ready"}; // network stats - MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u", - names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent, - ifp->ndrop, ifp->nerr)); + gpio_toggle(LED); // Blink LED + (void) arg; } int main(void) { @@ -41,23 +37,7 @@ int main(void) { struct mg_mgr mgr; // Initialise mg_mgr_init(&mgr); // Mongoose event manager mg_log_set(MG_LL_DEBUG); // Set log level - - // Initialise Mongoose network stack - struct mg_tcpip_driver_stm32f_data driver_data = {.mdc_cr = 4}; - 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_stm32f, - .driver_data = &driver_data}; - mg_tcpip_init(&mgr, &mif); - mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &mif); - - MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac)); - while (mif.state != MG_TCPIP_STATE_READY) { - mg_mgr_poll(&mgr, 0); - } + mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, NULL); MG_INFO(("Initialising application...")); web_init(&mgr); diff --git a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/mongoose_custom.h b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/mongoose_custom.h index 4ce3a48f4d4..415a61070e9 100644 --- a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/mongoose_custom.h +++ b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/mongoose_custom.h @@ -9,3 +9,19 @@ #define MG_ENABLE_PACKED_FS 1 #define MG_ENABLE_DRIVER_STM32F 1 #define MG_ENABLE_LINES 1 +#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 1 + +// For static IP configuration, define MG_TCPIP_{IP,MASK,GW} +// By default, those are set to zero, meaning that DHCP is used +// +// #define MG_TCPIP_IP MG_IPV4(192, 168, 0, 10) +// #define MG_TCPIP_GW MG_IPV4(192, 168, 0, 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 my_function(unsigned char *mac); +// #define MG_SET_MAC_ADDRESS(mac) my_function(mac) diff --git a/mongoose.c b/mongoose.c index 4554f182c13..e766ada35eb 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4851,11 +4851,6 @@ void mg_mgr_free(struct mg_mgr *mgr) { mg_tls_ctx_free(mgr); } - -#if MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT -void mg_tcpip_auto_init(struct mg_mgr *); -#endif - void mg_mgr_init(struct mg_mgr *mgr) { memset(mgr, 0, sizeof(*mgr)); #if MG_ENABLE_EPOLL @@ -4874,8 +4869,8 @@ void mg_mgr_init(struct mg_mgr *mgr) { // Ignore SIGPIPE signal, so if client cancels the request, it // won't kill the whole process. signal(SIGPIPE, SIG_IGN); -#elif MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT - mg_tcpip_auto_init(mgr); +#elif MG_ENABLE_TCPIP_DRIVER_INIT && defined(MG_TCPIP_DRIVER_INIT) + MG_TCPIP_DRIVER_INIT(mgr); #endif mgr->pipe = MG_INVALID_SOCKET; mgr->dnstimeout = 3000; @@ -5757,7 +5752,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) { #if MG_ENABLE_TCPIP_PRINT_DEBUG_STATS if (expired_1000ms) { const char *names[] = {"down", "up", "req", "ready"}; - MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u", + MG_INFO(("Status: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u", names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent, ifp->ndrop, ifp->nerr)); } diff --git a/mongoose.h b/mongoose.h index 908611a2543..2fbccb3487e 100644 --- a/mongoose.h +++ b/mongoose.h @@ -837,19 +837,21 @@ struct timeval { #define MG_ENABLE_TCPIP_DRIVER_INIT 1 // enabled built-in driver for #endif // Mongoose built-in network stack -#ifndef MG_TCPIP_IP // e.g. MG_IPV4(192, 168, 0, 223) -#define MG_TCPIP_IP MG_IPV4(0, 0, 0, 0) // or leave as 0 for DHCP +#ifndef MG_TCPIP_IP // e.g. MG_IPV4(192, 168, 0, 223) +#define MG_TCPIP_IP MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP) #endif #ifndef MG_TCPIP_MASK -#define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0) +#define MG_TCPIP_MASK MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP) #endif #ifndef MG_TCPIP_GW -#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 1) +#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP) #endif -#define MG_MAC_ADDRESS_RANDOM { 0, 0, 0, 0, 0, 0 } +#ifndef MG_SET_MAC_ADDRESS +#define MG_SET_MAC_ADDRESS(mac) +#endif #ifndef MG_ENABLE_TCPIP_PRINT_DEBUG_STATS #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 @@ -2882,15 +2884,6 @@ struct mg_profitem { #include "Driver_ETH_MAC.h" // keep this include #include "Driver_ETH_PHY.h" // keep this include -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - -#define MG_TCPIP_DRIVER_DATA int driver_data; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_cmsis -#define MG_TCPIP_DRIVER_NAME "cmsis" - #endif @@ -2913,10 +2906,6 @@ struct mg_tcpip_driver_imxrt_data { uint8_t phy_addr; // PHY address }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_TCPIP_PHY_ADDR #define MG_TCPIP_PHY_ADDR 2 #endif @@ -2925,15 +2914,6 @@ struct mg_tcpip_driver_imxrt_data { #define MG_DRIVER_MDC_CR 24 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_imxrt_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - .phy_addr = MG_TCPIP_PHY_ADDR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_imxrt -#define MG_TCPIP_DRIVER_NAME "imxrt" - #endif @@ -2946,35 +2926,6 @@ struct mg_tcpip_driver_ra_data { uint8_t phy_addr; // PHY address }; -#undef MG_ENABLE_TCPIP_DRIVER_INIT -#define MG_ENABLE_TCPIP_DRIVER_INIT 0 // TODO(): needs SystemCoreClock -#if 0 -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - -#ifndef MG_TCPIP_PHY_ADDR -#define MG_TCPIP_PHY_ADDR 1 -#endif - -#ifndef MG_DRIVER_RA_CLOCK -#define MG_DRIVER_RA_CLOCK -#endif - -#ifndef MG_DRIVER_RA_IRQNO -#define MG_DRIVER_RA_IRQNO 0 -#endif - -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_ra_data driver_data = { \ - .clock = MG_DRIVER_RA_CLOCK, \ - .irqno = MG_DRIVER_RA_CLOCK, \ - .phy_addr = MG_TCPIP_PHY_ADDR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_ra -#define MG_TCPIP_DRIVER_NAME "ra" -#endif #endif @@ -2984,22 +2935,10 @@ struct mg_tcpip_driver_same54_data { int mdc_cr; }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_DRIVER_MDC_CR #define MG_DRIVER_MDC_CR 5 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_same54_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_same54 -#define MG_TCPIP_DRIVER_NAME "same54" - #endif @@ -3023,10 +2962,6 @@ struct mg_tcpip_driver_stm32f_data { uint8_t phy_addr; // PHY address }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_TCPIP_PHY_ADDR #define MG_TCPIP_PHY_ADDR 0 #endif @@ -3035,14 +2970,21 @@ struct mg_tcpip_driver_stm32f_data { #define MG_DRIVER_MDC_CR 4 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_stm32f_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - .phy_addr = MG_TCPIP_PHY_ADDR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32f -#define MG_TCPIP_DRIVER_NAME "stm32f" +#define MG_TCPIP_DRIVER_INIT(mgr) \ + do { \ + static struct mg_tcpip_driver_stm32f_data driver_data_; \ + static struct mg_tcpip_if mif_; \ + driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \ + driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \ + mif_.ip = MG_TCPIP_IP; \ + mif_.mask = MG_TCPIP_MASK; \ + mif_.gw = MG_TCPIP_GW; \ + mif_.driver = &mg_tcpip_driver_stm32f; \ + mif_.driver_data = &driver_data_; \ + MG_SET_MAC_ADDRESS(mif_.mac); \ + mg_tcpip_init(mgr, &mif_); \ + MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) #endif @@ -3059,27 +3001,30 @@ struct mg_tcpip_driver_stm32h_data { // 100-150 MHz HCLK/62 1 // 20-35 MHz HCLK/16 2 // 35-60 MHz HCLK/26 3 - // 150-250 MHz HCLK/102 4 <-- value for Nucleo-H* on max speed driven by HSI - // 250-300 MHz HCLK/124 5 <-- value for Nucleo-H* on max speed driven by CSI + // 150-250 MHz HCLK/102 4 <-- value for max speed HSI + // 250-300 MHz HCLK/124 5 <-- value for Nucleo-H* on CSI // 110, 111 Reserved int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5 }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_DRIVER_MDC_CR #define MG_DRIVER_MDC_CR 4 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_stm32h_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32h -#define MG_TCPIP_DRIVER_NAME "stm32h" +#define MG_TCPIP_DRIVER_INIT(mgr) \ + do { \ + static struct mg_tcpip_driver_stm32h_data driver_data_; \ + static struct mg_tcpip_if mif_ = {.mac = MG_MAC_ADDRESS}; \ + driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \ + mif_.ip = MG_TCPIP_IP; \ + mif_.mask = MG_TCPIP_MASK; \ + mif_.gw = MG_TCPIP_GW; \ + mif_.driver = &mg_tcpip_driver_stm32h; \ + mif_.driver_data = &driver_data_; \ + memmove(mif_.mac, mac_, sizeof(mac_)); \ + mg_tcpip_init(mgr, &mif_); \ + MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) #endif @@ -3099,31 +3044,10 @@ struct mg_tcpip_driver_tm4c_data { int mdc_cr; // Valid values: -1, 0, 1, 2, 3 }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_DRIVER_MDC_CR #define MG_DRIVER_MDC_CR 1 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_tm4c_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_tm4c -#define MG_TCPIP_DRIVER_NAME "tm4c" - - -#endif - - -#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && MG_ENABLE_DRIVER_W5500 - -#undef MG_ENABLE_TCPIP_DRIVER_INIT -#define MG_ENABLE_TCPIP_DRIVER_INIT 0 - #endif #ifdef __cplusplus diff --git a/src/config.h b/src/config.h index b96efe5d40e..d1acf6b56a7 100644 --- a/src/config.h +++ b/src/config.h @@ -162,19 +162,21 @@ #define MG_ENABLE_TCPIP_DRIVER_INIT 1 // enabled built-in driver for #endif // Mongoose built-in network stack -#ifndef MG_TCPIP_IP // e.g. MG_IPV4(192, 168, 0, 223) -#define MG_TCPIP_IP MG_IPV4(0, 0, 0, 0) // or leave as 0 for DHCP +#ifndef MG_TCPIP_IP // e.g. MG_IPV4(192, 168, 0, 223) +#define MG_TCPIP_IP MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP) #endif #ifndef MG_TCPIP_MASK -#define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0) +#define MG_TCPIP_MASK MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP) #endif #ifndef MG_TCPIP_GW -#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 1) +#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP) #endif -#define MG_MAC_ADDRESS_RANDOM { 0, 0, 0, 0, 0, 0 } +#ifndef MG_SET_MAC_ADDRESS +#define MG_SET_MAC_ADDRESS(mac) +#endif #ifndef MG_ENABLE_TCPIP_PRINT_DEBUG_STATS #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 diff --git a/src/drivers/cmsis.h b/src/drivers/cmsis.h index a264fc4645c..f8b4dadd9fc 100644 --- a/src/drivers/cmsis.h +++ b/src/drivers/cmsis.h @@ -5,13 +5,4 @@ #include "Driver_ETH_MAC.h" // keep this include #include "Driver_ETH_PHY.h" // keep this include -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - -#define MG_TCPIP_DRIVER_DATA int driver_data; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_cmsis -#define MG_TCPIP_DRIVER_NAME "cmsis" - #endif diff --git a/src/drivers/imxrt.h b/src/drivers/imxrt.h index 31ebbe57f88..dd5d8bc4676 100644 --- a/src/drivers/imxrt.h +++ b/src/drivers/imxrt.h @@ -19,10 +19,6 @@ struct mg_tcpip_driver_imxrt_data { uint8_t phy_addr; // PHY address }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_TCPIP_PHY_ADDR #define MG_TCPIP_PHY_ADDR 2 #endif @@ -31,13 +27,4 @@ struct mg_tcpip_driver_imxrt_data { #define MG_DRIVER_MDC_CR 24 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_imxrt_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - .phy_addr = MG_TCPIP_PHY_ADDR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_imxrt -#define MG_TCPIP_DRIVER_NAME "imxrt" - #endif diff --git a/src/drivers/ra.h b/src/drivers/ra.h index 1b105b14b38..790594828f4 100644 --- a/src/drivers/ra.h +++ b/src/drivers/ra.h @@ -9,33 +9,4 @@ struct mg_tcpip_driver_ra_data { uint8_t phy_addr; // PHY address }; -#undef MG_ENABLE_TCPIP_DRIVER_INIT -#define MG_ENABLE_TCPIP_DRIVER_INIT 0 // TODO(): needs SystemCoreClock -#if 0 -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - -#ifndef MG_TCPIP_PHY_ADDR -#define MG_TCPIP_PHY_ADDR 1 -#endif - -#ifndef MG_DRIVER_RA_CLOCK -#define MG_DRIVER_RA_CLOCK -#endif - -#ifndef MG_DRIVER_RA_IRQNO -#define MG_DRIVER_RA_IRQNO 0 -#endif - -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_ra_data driver_data = { \ - .clock = MG_DRIVER_RA_CLOCK, \ - .irqno = MG_DRIVER_RA_CLOCK, \ - .phy_addr = MG_TCPIP_PHY_ADDR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_ra -#define MG_TCPIP_DRIVER_NAME "ra" -#endif #endif diff --git a/src/drivers/same54.h b/src/drivers/same54.h index ec4a0f3a7b5..2198ff423d8 100644 --- a/src/drivers/same54.h +++ b/src/drivers/same54.h @@ -6,20 +6,8 @@ struct mg_tcpip_driver_same54_data { int mdc_cr; }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_DRIVER_MDC_CR #define MG_DRIVER_MDC_CR 5 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_same54_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_same54 -#define MG_TCPIP_DRIVER_NAME "same54" - #endif diff --git a/src/drivers/stm32f.h b/src/drivers/stm32f.h index 1d2b1c69fb4..d4a5c520f40 100644 --- a/src/drivers/stm32f.h +++ b/src/drivers/stm32f.h @@ -20,10 +20,6 @@ struct mg_tcpip_driver_stm32f_data { uint8_t phy_addr; // PHY address }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_TCPIP_PHY_ADDR #define MG_TCPIP_PHY_ADDR 0 #endif @@ -32,13 +28,20 @@ struct mg_tcpip_driver_stm32f_data { #define MG_DRIVER_MDC_CR 4 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_stm32f_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - .phy_addr = MG_TCPIP_PHY_ADDR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32f -#define MG_TCPIP_DRIVER_NAME "stm32f" +#define MG_TCPIP_DRIVER_INIT(mgr) \ + do { \ + static struct mg_tcpip_driver_stm32f_data driver_data_; \ + static struct mg_tcpip_if mif_; \ + driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \ + driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \ + mif_.ip = MG_TCPIP_IP; \ + mif_.mask = MG_TCPIP_MASK; \ + mif_.gw = MG_TCPIP_GW; \ + mif_.driver = &mg_tcpip_driver_stm32f; \ + mif_.driver_data = &driver_data_; \ + MG_SET_MAC_ADDRESS(mif_.mac); \ + mg_tcpip_init(mgr, &mif_); \ + MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) #endif diff --git a/src/drivers/stm32h.h b/src/drivers/stm32h.h index 80ea6c3a03a..d41d61ab08b 100644 --- a/src/drivers/stm32h.h +++ b/src/drivers/stm32h.h @@ -12,26 +12,29 @@ struct mg_tcpip_driver_stm32h_data { // 100-150 MHz HCLK/62 1 // 20-35 MHz HCLK/16 2 // 35-60 MHz HCLK/26 3 - // 150-250 MHz HCLK/102 4 <-- value for Nucleo-H* on max speed driven by HSI - // 250-300 MHz HCLK/124 5 <-- value for Nucleo-H* on max speed driven by CSI + // 150-250 MHz HCLK/102 4 <-- value for max speed HSI + // 250-300 MHz HCLK/124 5 <-- value for Nucleo-H* on CSI // 110, 111 Reserved int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5 }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_DRIVER_MDC_CR #define MG_DRIVER_MDC_CR 4 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_stm32h_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32h -#define MG_TCPIP_DRIVER_NAME "stm32h" +#define MG_TCPIP_DRIVER_INIT(mgr) \ + do { \ + static struct mg_tcpip_driver_stm32h_data driver_data_; \ + static struct mg_tcpip_if mif_ = {.mac = MG_MAC_ADDRESS}; \ + driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \ + mif_.ip = MG_TCPIP_IP; \ + mif_.mask = MG_TCPIP_MASK; \ + mif_.gw = MG_TCPIP_GW; \ + mif_.driver = &mg_tcpip_driver_stm32h; \ + mif_.driver_data = &driver_data_; \ + memmove(mif_.mac, mac_, sizeof(mac_)); \ + mg_tcpip_init(mgr, &mif_); \ + MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) #endif diff --git a/src/drivers/tm4c.h b/src/drivers/tm4c.h index 89862e5a27c..4b5244991df 100644 --- a/src/drivers/tm4c.h +++ b/src/drivers/tm4c.h @@ -15,21 +15,8 @@ struct mg_tcpip_driver_tm4c_data { int mdc_cr; // Valid values: -1, 0, 1, 2, 3 }; -#ifndef MG_MAC_ADDRESS -#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM -#endif - #ifndef MG_DRIVER_MDC_CR #define MG_DRIVER_MDC_CR 1 #endif -#define MG_TCPIP_DRIVER_DATA \ - static struct mg_tcpip_driver_tm4c_data driver_data = { \ - .mdc_cr = MG_DRIVER_MDC_CR, \ - }; - -#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_tm4c -#define MG_TCPIP_DRIVER_NAME "tm4c" - - #endif diff --git a/src/net.c b/src/net.c index 00662c2d431..6bb6295bd42 100644 --- a/src/net.c +++ b/src/net.c @@ -252,11 +252,6 @@ void mg_mgr_free(struct mg_mgr *mgr) { mg_tls_ctx_free(mgr); } - -#if MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT -void mg_tcpip_auto_init(struct mg_mgr *); -#endif - void mg_mgr_init(struct mg_mgr *mgr) { memset(mgr, 0, sizeof(*mgr)); #if MG_ENABLE_EPOLL @@ -275,8 +270,8 @@ void mg_mgr_init(struct mg_mgr *mgr) { // Ignore SIGPIPE signal, so if client cancels the request, it // won't kill the whole process. signal(SIGPIPE, SIG_IGN); -#elif MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT - mg_tcpip_auto_init(mgr); +#elif MG_ENABLE_TCPIP_DRIVER_INIT && defined(MG_TCPIP_DRIVER_INIT) + MG_TCPIP_DRIVER_INIT(mgr); #endif mgr->pipe = MG_INVALID_SOCKET; mgr->dnstimeout = 3000; diff --git a/src/net_builtin.c b/src/net_builtin.c index 3205c4123fe..ea6fcf17dce 100644 --- a/src/net_builtin.c +++ b/src/net_builtin.c @@ -868,7 +868,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) { #if MG_ENABLE_TCPIP_PRINT_DEBUG_STATS if (expired_1000ms) { const char *names[] = {"down", "up", "req", "ready"}; - MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u", + MG_INFO(("Status: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u", names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent, ifp->ndrop, ifp->nerr)); }