From 06dd8efa89afa6bfba8467f8700cf4760d4021e5 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Sun, 10 Mar 2024 17:02:52 +0000 Subject: [PATCH] Add driver init to mg_mgr_init() --- mongoose.c | 4 +++ mongoose.h | 60 ++++++++++++++++++++++++++++++++++++++++++++ src/config.h | 30 ++++++++++++++++++++++ src/drivers/stm32f.h | 30 ++++++++++++++++++++++ src/net.c | 4 +++ 5 files changed, 128 insertions(+) diff --git a/mongoose.c b/mongoose.c index 74f1a7252e..4064293415 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4875,6 +4875,10 @@ void mg_mgr_init(struct mg_mgr *mgr) { mgr->dns4.url = "udp://8.8.8.8:53"; mgr->dns6.url = "udp://[2001:4860:4860::8888]:53"; mg_tls_ctx_init(mgr); + +#if (MG_ENABLE_DRIVER_INIT == 1) && defined(MG_TCPIP_DRIVER_INIT) + MG_TCPIP_DRIVER_INIT(mgr); +#endif } #ifdef MG_ENABLE_LINES diff --git a/mongoose.h b/mongoose.h index 0519865975..b7ef4d9162 100644 --- a/mongoose.h +++ b/mongoose.h @@ -833,6 +833,36 @@ struct timeval { #define MG_ENABLE_PROFILE 0 #endif +#ifndef MG_ENABLE_DRIVER_INIT +#define MG_ENABLE_DRIVER_INIT 1 +#endif + +#ifndef MG_TCPIP_IP +#define MG_TCPIP_IP 0 +#endif + +#ifndef MG_TCPIP_MASK +#define MG_TCPIP_MASK 0 +#endif + +#ifndef MG_TCPIP_GW +#define MG_TCPIP_GW 0 +#endif + +#define MG_MAC_ADDRESS_RANDOM \ + { 0, 0, 0, 0, 0, 0 } + +#define MG_STM32_UID_TO_MAC_ADDRESS(BASE) \ + { \ + 2, ((uint8_t *) (BASE))[0] ^ ((uint8_t *) (BASE))[1], \ + ((uint8_t *) (BASE))[2] ^ ((uint8_t *) (BASE))[3], \ + ((uint8_t *) (BASE))[4] ^ ((uint8_t *) (BASE))[5], \ + ((uint8_t *) (BASE))[6] ^ ((uint8_t *) (BASE))[7] ^ \ + ((uint8_t *) (BASE))[8], \ + ((uint8_t *) (BASE))[9] ^ ((uint8_t *) (BASE))[10] ^ \ + ((uint8_t *) (BASE))[11] \ + } + @@ -2909,6 +2939,36 @@ 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 + +#ifndef MG_TCPIP_MDC_CR +#define MG_TCPIP_MDC_CR 4 +#endif + +#define MG_TCPIP_DRIVER_INIT(mgr) \ + do { \ + static struct mg_tcpip_driver_stm32f_data driver_data = { \ + .mdc_cr = MG_TCPIP_MDC_CR, \ + .phy_addr = MG_TCPIP_PHY_ADDR, \ + }; \ + static struct mg_tcpip_if mif = { \ + .mac = MG_MAC_ADDRESS, \ + .ip = MG_TCPIP_IP, \ + .mask = MG_TCPIP_MASK, \ + .gw = MG_TCPIP_GW, \ + .driver = &mg_tcpip_driver_stm32f, \ + .driver_data = &driver_data, \ + }; \ + mg_tcpip_init((mgr), &mif); \ + MG_INFO(("Driver: stm32fxx, MAC: %M", mg_print_mac, mif.mac)); \ + } while (0) + struct mg_tcpip_driver_stm32h_data { // MDC clock divider. MDC clock is derived from HCLK, must not exceed 2.5MHz diff --git a/src/config.h b/src/config.h index 50161eeb5b..21de0d365e 100644 --- a/src/config.h +++ b/src/config.h @@ -157,3 +157,33 @@ #ifndef MG_ENABLE_PROFILE #define MG_ENABLE_PROFILE 0 #endif + +#ifndef MG_ENABLE_DRIVER_INIT +#define MG_ENABLE_DRIVER_INIT 1 +#endif + +#ifndef MG_TCPIP_IP +#define MG_TCPIP_IP 0 +#endif + +#ifndef MG_TCPIP_MASK +#define MG_TCPIP_MASK 0 +#endif + +#ifndef MG_TCPIP_GW +#define MG_TCPIP_GW 0 +#endif + +#define MG_MAC_ADDRESS_RANDOM \ + { 0, 0, 0, 0, 0, 0 } + +#define MG_STM32_UID_TO_MAC_ADDRESS(BASE) \ + { \ + 2, ((uint8_t *) (BASE))[0] ^ ((uint8_t *) (BASE))[1], \ + ((uint8_t *) (BASE))[2] ^ ((uint8_t *) (BASE))[3], \ + ((uint8_t *) (BASE))[4] ^ ((uint8_t *) (BASE))[5], \ + ((uint8_t *) (BASE))[6] ^ ((uint8_t *) (BASE))[7] ^ \ + ((uint8_t *) (BASE))[8], \ + ((uint8_t *) (BASE))[9] ^ ((uint8_t *) (BASE))[10] ^ \ + ((uint8_t *) (BASE))[11] \ + } diff --git a/src/drivers/stm32f.h b/src/drivers/stm32f.h index bec133524b..c55399e043 100644 --- a/src/drivers/stm32f.h +++ b/src/drivers/stm32f.h @@ -16,3 +16,33 @@ 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 + +#ifndef MG_TCPIP_MDC_CR +#define MG_TCPIP_MDC_CR 4 +#endif + +#define MG_TCPIP_DRIVER_INIT(mgr) \ + do { \ + static struct mg_tcpip_driver_stm32f_data driver_data = { \ + .mdc_cr = MG_TCPIP_MDC_CR, \ + .phy_addr = MG_TCPIP_PHY_ADDR, \ + }; \ + static struct mg_tcpip_if mif = { \ + .mac = MG_MAC_ADDRESS, \ + .ip = MG_TCPIP_IP, \ + .mask = MG_TCPIP_MASK, \ + .gw = MG_TCPIP_GW, \ + .driver = &mg_tcpip_driver_stm32f, \ + .driver_data = &driver_data, \ + }; \ + mg_tcpip_init((mgr), &mif); \ + MG_INFO(("Driver: stm32fxx, MAC: %M", mg_print_mac, mif.mac)); \ + } while (0) diff --git a/src/net.c b/src/net.c index f291bd30ad..078fa28d44 100644 --- a/src/net.c +++ b/src/net.c @@ -276,4 +276,8 @@ void mg_mgr_init(struct mg_mgr *mgr) { mgr->dns4.url = "udp://8.8.8.8:53"; mgr->dns6.url = "udp://[2001:4860:4860::8888]:53"; mg_tls_ctx_init(mgr); + +#if (MG_ENABLE_DRIVER_INIT == 1) && defined(MG_TCPIP_DRIVER_INIT) + MG_TCPIP_DRIVER_INIT(mgr); +#endif }