mongoose/examples/wch/ch32v307-make-baremetal-builtin/main.c

86 lines
2.9 KiB
C
Raw Normal View History

2023-11-22 12:23:34 +08:00
// Copyright (c) 2023 Cesanta Software Limited
#include "hal.h"
#include "mongoose.h"
2023-11-27 10:50:46 +08:00
#include "net.h"
2023-11-22 12:23:34 +08:00
#define BLINK_PERIOD_MS 1000 // LED_PIN blinking period in millis
2023-11-29 15:41:18 +08:00
// This flash space resides at after the 0-wait 320k area
static char *s_flash_space = (char *) (0x8000000 + 320 * 1024);
bool web_load_settings(void *buf, size_t len) {
if (*(uint32_t *) s_flash_space != SETTINGS_MAGIC) return false;
memcpy(buf, s_flash_space, len);
return true;
}
bool web_save_settings(void *buf, size_t len) {
return mg_flash_write(s_flash_space, buf, len);
}
2023-11-22 12:23:34 +08:00
static void timer_fn(void *arg) {
gpio_toggle(LED_PIN); // Blink LED_PIN
struct mg_tcpip_if *ifp = arg; // And show
const char *names[] = {"down", "up", "req", "ready"}; // network stats
2024-01-18 02:07:25 +08:00
MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u RAM: %lu/%lu",
2023-11-22 12:23:34 +08:00
names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent,
2024-01-18 02:07:25 +08:00
ifp->ndrop, ifp->nerr, hal_ram_used(), hal_ram_free()));
2023-11-22 12:23:34 +08:00
}
// https://mongoose.ws/documentation/#2-minute-integration-guide
static void fn(struct mg_connection *c, int ev, void *ev_data) {
2023-11-22 12:23:34 +08:00
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
2024-01-18 02:07:25 +08:00
mg_http_reply(c, 200, "", "ok %p\r\n", hm);
2023-11-22 12:23:34 +08:00
}
}
int main(void) {
2024-01-18 02:07:25 +08:00
struct mg_mgr mgr;
2023-11-22 12:23:34 +08:00
2024-01-18 02:07:25 +08:00
hal_init();
mg_mgr_init(&mgr);
mg_log_set(MG_LL_DEBUG);
2023-11-22 12:23:34 +08:00
MG_INFO(("Starting, CPU freq %g MHz", (double) SystemCoreClock / 1000000));
2023-11-29 15:41:18 +08:00
// Print chip RAM/Flash configuration, and set to 64/256
const char *sizes[] = {"128/192", "96/224", "64/256", "32/288"};
uint32_t mode = (FLASH->OBR >> 8) & 3U;
MG_INFO(("RAM/FLASH configuration: %s", sizes[mode]));
2023-12-28 06:53:48 +08:00
// if (mode != 2) set_ram_size(2);
2023-11-22 12:23:34 +08:00
2024-01-18 02:07:25 +08:00
extern char _end[], _heap_end[];
MG_INFO(("Heap size: %lu bytes. RAM: used %lu, free %lu", _heap_end - _end,
hal_ram_used(), hal_ram_free()));
2023-11-22 12:23:34 +08:00
// Initialise Mongoose network stack
struct mg_tcpip_driver_stm32f_data driver_data = {.mdc_cr = 1, .phy_addr = 1};
2023-11-22 12:23:34 +08:00
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)),
2023-12-16 05:38:02 +08:00
.driver = &mg_tcpip_driver_stm32f,
2023-11-22 12:23:34 +08:00
.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_INFO(("Initialising application..."));
2023-11-27 10:50:46 +08:00
mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, NULL);
2023-12-28 06:45:02 +08:00
web_init(&mgr);
2023-11-22 12:23:34 +08:00
MG_INFO(("Starting event loop"));
for (;;) {
mg_mgr_poll(&mgr, 0);
}
return 0;
}