mongoose/examples/stm32/nucleo-f746zg-make-freertos-builtin/main.c

76 lines
2.5 KiB
C
Raw Normal View History

2023-08-02 01:19:51 +08:00
// Copyright (c) 2022-2023 Cesanta Software Limited
2022-11-09 17:38:30 +08:00
// All rights reserved
2023-02-11 08:04:34 +08:00
#include "hal.h"
2023-05-31 03:17:46 +08:00
#include "mongoose.h"
2023-05-26 22:48:20 +08:00
#include "net.h"
2022-11-09 17:38:30 +08:00
2023-02-11 08:04:34 +08:00
#define BLINK_PERIOD_MS 1000 // LED blinking period in millis
void mg_random(void *buf, size_t len) { // Use on-board RNG
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
uint32_t r = rng_read();
memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r));
2022-11-09 17:38:30 +08:00
}
2023-02-11 08:04:34 +08:00
}
static void timer_fn(void *arg) {
struct mg_tcpip_if *ifp = arg; // And show
const char *names[] = {"down", "up", "req", "ready"}; // network stats
2023-02-11 08:04:34 +08:00
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));
2022-11-09 17:38:30 +08:00
}
static void server(void *args) {
2022-11-09 17:38:30 +08:00
struct mg_mgr mgr; // Initialise Mongoose event manager
mg_mgr_init(&mgr); // and attach it to the interface
2022-11-09 17:38:30 +08:00
mg_log_set(MG_LL_DEBUG); // Set log level
// Initialise Mongoose network stack
ethernet_init();
2023-12-16 05:38:02 +08:00
struct mg_tcpip_driver_stm32f_data driver_data = {.mdc_cr = 4};
2023-02-11 08:04:34 +08:00
struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(),
2023-07-19 04:10:05 +08:00
// 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-02-11 08:04:34 +08:00
.driver_data = &driver_data};
mg_tcpip_init(&mgr, &mif);
2023-02-11 08:04:34 +08:00
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &mif);
2022-11-09 17:38:30 +08:00
2023-02-11 08:04:34 +08:00
MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
2023-03-09 22:25:02 +08:00
while (mif.state != MG_TCPIP_STATE_READY) {
2023-02-11 08:04:34 +08:00
mg_mgr_poll(&mgr, 0);
}
MG_INFO(("Initialising application..."));
2023-05-26 22:48:20 +08:00
web_init(&mgr);
2023-02-11 08:04:34 +08:00
MG_INFO(("Starting event loop"));
for (;;) mg_mgr_poll(&mgr, 1); // Infinite event loop
(void) args;
2022-11-09 17:38:30 +08:00
}
static void blinker(void *args) {
2023-02-11 08:04:34 +08:00
gpio_init(LED, GPIO_MODE_OUTPUT, GPIO_OTYPE_PUSH_PULL, GPIO_SPEED_MEDIUM,
2022-11-09 17:38:30 +08:00
GPIO_PULL_NONE, 0);
for (;;) {
2023-02-11 08:04:34 +08:00
gpio_toggle(LED);
vTaskDelay(pdMS_TO_TICKS(BLINK_PERIOD_MS));
2022-11-09 17:38:30 +08:00
}
2023-02-11 08:04:34 +08:00
(void) args;
2022-11-09 17:38:30 +08:00
}
int main(void) {
2023-02-11 08:04:34 +08:00
uart_init(UART_DEBUG, 115200); // Initialise UART
// Start tasks. NOTE: stack sizes are in 32-bit words
2022-11-09 17:38:30 +08:00
xTaskCreate(blinker, "blinker", 128, ":)", configMAX_PRIORITIES - 1, NULL);
xTaskCreate(server, "server", 2048, 0, configMAX_PRIORITIES - 1, NULL);
2023-02-11 08:04:34 +08:00
2022-11-09 17:38:30 +08:00
vTaskStartScheduler(); // This blocks
return 0;
}