Skip to content

Commit

Permalink
Add driver init to mg_mgr_init()
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Mar 11, 2024
1 parent f72b0b5 commit 844372f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_MAC_ADDRESS_STM32_UID(ADDR) \
{ \
2, ((uint8_t *) (ADDR))[0] ^ ((uint8_t *) (ADDR))[1], \
((uint8_t *) (ADDR))[2] ^ ((uint8_t *) (ADDR))[3], \
((uint8_t *) (ADDR))[4] ^ ((uint8_t *) (ADDR))[5], \
((uint8_t *) (ADDR))[6] ^ ((uint8_t *) (ADDR))[7] ^ \
((uint8_t *) (ADDR))[8], \
((uint8_t *) (ADDR))[9] ^ ((uint8_t *) (ADDR))[10] ^ \
((uint8_t *) (ADDR))[11] \
}




Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_MAC_ADDRESS_STM32_UID(ADDR) \
{ \
2, ((uint8_t *) (ADDR))[0] ^ ((uint8_t *) (ADDR))[1], \
((uint8_t *) (ADDR))[2] ^ ((uint8_t *) (ADDR))[3], \
((uint8_t *) (ADDR))[4] ^ ((uint8_t *) (ADDR))[5], \
((uint8_t *) (ADDR))[6] ^ ((uint8_t *) (ADDR))[7] ^ \
((uint8_t *) (ADDR))[8], \
((uint8_t *) (ADDR))[9] ^ ((uint8_t *) (ADDR))[10] ^ \
((uint8_t *) (ADDR))[11] \
}
30 changes: 30 additions & 0 deletions src/drivers/stm32f.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 4 additions & 0 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 844372f

Please sign in to comment.