diff --git a/examples/stm32/nucleo-f746zg-freertos-mip/Makefile b/examples/stm32/nucleo-f746zg-freertos-mip/Makefile index 8d9bc388..535597d1 100644 --- a/examples/stm32/nucleo-f746zg-freertos-mip/Makefile +++ b/examples/stm32/nucleo-f746zg-freertos-mip/Makefile @@ -14,8 +14,8 @@ SOURCES += FreeRTOS-Kernel/portable/GCC/ARM_CM7/r0p1/port.c CFLAGS += -IFreeRTOS-Kernel/include CFLAGS += -IFreeRTOS-Kernel/portable/GCC/ARM_CM7/r0p1 -Wno-conversion -SOURCES += ../../../mongoose.c # Mongoose options are defined in mongoose_custom.h -CFLAGS += -I../../.. $(CFLAGS_EXTRA) +SOURCES += mongoose.c net.c packed_fs.c +CFLAGS += $(CFLAGS_EXTRA) # Mongoose options are defined in mongoose_custom.h ifeq ($(OS),Windows_NT) RM = cmd /C del /Q /F /S @@ -50,6 +50,7 @@ test: CFLAGS_EXTRA += -DUART_DEBUG=USART1 test: update curl --fail-with-body -su :$(VCON_API_KEY) $(DEVICE_URL)/tx?t=5 | tee /tmp/output.txt grep 'READY, IP:' /tmp/output.txt # Check for network init + grep 'MQTT connected' /tmp/output.txt # Check for MQTT connection success clean: $(RM) firmware.* *.su cmsis_core cmsis_f7 FreeRTOS-Kernel diff --git a/examples/stm32/nucleo-f746zg-freertos-mip/hal.h b/examples/stm32/nucleo-f746zg-freertos-mip/hal.h index 4aac3b3f..a29353d7 100644 --- a/examples/stm32/nucleo-f746zg-freertos-mip/hal.h +++ b/examples/stm32/nucleo-f746zg-freertos-mip/hal.h @@ -75,7 +75,6 @@ static inline void gpio_output(uint16_t pin) { static inline void irq_exti_attach(uint16_t pin) { uint8_t bank = (uint8_t) (PINBANK(pin)), n = (uint8_t) (PINNO(pin)); - RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // Enable SYSCFG SYSCFG->EXTICR[n / 4] &= ~(15UL << ((n % 4) * 4)); SYSCFG->EXTICR[n / 4] |= (uint32_t) (bank << ((n % 4) * 4)); EXTI->IMR |= BIT(n); @@ -132,3 +131,12 @@ static inline uint32_t rng_read(void) { while ((RNG->SR & RNG_SR_DRDY) == 0) (void) 0; return RNG->DR; } + +#define UUID ((uint8_t *) UID_BASE) // Unique 96-bit chip ID. TRM 41.1 + +// Helper macro for MAC generation +#define GENERATE_LOCALLY_ADMINISTERED_MAC() \ + { \ + 2, UUID[0] ^ UUID[1], UUID[2] ^ UUID[3], UUID[4] ^ UUID[5], \ + UUID[6] ^ UUID[7] ^ UUID[8], UUID[9] ^ UUID[10] ^ UUID[11] \ + } diff --git a/examples/stm32/nucleo-f746zg-freertos-mip/main.c b/examples/stm32/nucleo-f746zg-freertos-mip/main.c index ec6d0c49..3e156311 100644 --- a/examples/stm32/nucleo-f746zg-freertos-mip/main.c +++ b/examples/stm32/nucleo-f746zg-freertos-mip/main.c @@ -7,7 +7,6 @@ #define LED1 PIN('B', 0) // On-board LED pin (green) #define LED2 PIN('B', 7) // On-board LED pin (blue) #define LED3 PIN('B', 14) // On-board LED pin (red) -#define BTN1 PIN('C', 13) // On-board user button #define LED LED2 // Use blue LED for blinking #define BLINK_PERIOD_MS 1000 // LED blinking period in millis @@ -19,19 +18,12 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG } } -// HTTP server event handler function -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_HTTP_MSG) { - struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (mg_http_match_uri(hm, "/api/debug")) { - int level = mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG); - mg_log_set(level); - mg_http_reply(c, 200, "", "Debug level set to %d\n", level); - } else { - mg_http_reply(c, 200, "", "%s\n", "hi"); - } - } - (void) fn_data; +static void timer_fn(void *arg) { + struct mg_tcpip_if *ifp = arg; // And show + const char *names[] = {"down", "up", "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)); } static void ethernet_init(void) { @@ -50,13 +42,6 @@ static void ethernet_init(void) { RCC_AHB1ENR_ETHMACEN | RCC_AHB1ENR_ETHMACTXEN | RCC_AHB1ENR_ETHMACRXEN; } -static void timer_fn(void *arg) { - struct mg_tcpip_if *ifp = arg; // And show - const char *names[] = {"down", "up", "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)); -} static void server(void *args) { struct mg_mgr mgr; // Initialise Mongoose event manager @@ -66,17 +51,24 @@ static void server(void *args) { // Initialise Mongoose network stack // Specify MAC address, and IP/mask/GW in network byte order for static // IP configuration. If IP/mask/GW are unset, DHCP is going to be used - MG_INFO(("Initializing Ethernet driver")); ethernet_init(); struct mg_tcpip_driver_stm32_data driver_data = {.mdc_cr = 4}; - struct mg_tcpip_if mif = {.driver = &mg_tcpip_driver_stm32, + struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(), + .driver = &mg_tcpip_driver_stm32, .driver_data = &driver_data}; mg_tcpip_init(&mgr, &mif); - - MG_INFO(("Starting Mongoose v%s", MG_VERSION)); // Tell the world - mg_http_listen(&mgr, "http://0.0.0.0", fn, &mgr); // Web listener 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 != MIP_STATE_READY) { + mg_mgr_poll(&mgr, 0); + } + + MG_INFO(("Initialising application...")); + extern void device_dashboard_fn(struct mg_connection *, int, void *, void *); + mg_http_listen(&mgr, "http://0.0.0.0", device_dashboard_fn, NULL); + + MG_INFO(("Starting event loop")); for (;;) mg_mgr_poll(&mgr, 1); // Infinite event loop (void) args; } diff --git a/examples/stm32/nucleo-f746zg-freertos-mip/mongoose.c b/examples/stm32/nucleo-f746zg-freertos-mip/mongoose.c new file mode 120000 index 00000000..5e522bbc --- /dev/null +++ b/examples/stm32/nucleo-f746zg-freertos-mip/mongoose.c @@ -0,0 +1 @@ +../../../mongoose.c \ No newline at end of file diff --git a/examples/stm32/nucleo-f746zg-freertos-mip/mongoose.h b/examples/stm32/nucleo-f746zg-freertos-mip/mongoose.h new file mode 120000 index 00000000..ee4ac823 --- /dev/null +++ b/examples/stm32/nucleo-f746zg-freertos-mip/mongoose.h @@ -0,0 +1 @@ +../../../mongoose.h \ No newline at end of file diff --git a/examples/stm32/nucleo-f746zg-freertos-mip/mongoose_custom.h b/examples/stm32/nucleo-f746zg-freertos-mip/mongoose_custom.h index 2e67119d..53162794 100644 --- a/examples/stm32/nucleo-f746zg-freertos-mip/mongoose_custom.h +++ b/examples/stm32/nucleo-f746zg-freertos-mip/mongoose_custom.h @@ -7,3 +7,5 @@ #define MG_ENABLE_TCPIP 1 #define MG_IO_SIZE 256 #define MG_ENABLE_CUSTOM_RANDOM 1 +#define MG_ENABLE_PACKED_FS 1 + diff --git a/examples/stm32/nucleo-f746zg-freertos-mip/net.c b/examples/stm32/nucleo-f746zg-freertos-mip/net.c new file mode 120000 index 00000000..fe0e6f06 --- /dev/null +++ b/examples/stm32/nucleo-f746zg-freertos-mip/net.c @@ -0,0 +1 @@ +../../device-dashboard/net.c \ No newline at end of file diff --git a/examples/stm32/nucleo-f746zg-freertos-mip/packed_fs.c b/examples/stm32/nucleo-f746zg-freertos-mip/packed_fs.c new file mode 120000 index 00000000..e06bf092 --- /dev/null +++ b/examples/stm32/nucleo-f746zg-freertos-mip/packed_fs.c @@ -0,0 +1 @@ +../../device-dashboard/packed_fs.c \ No newline at end of file