56 lines
1.9 KiB
C
Raw Normal View History

2024-01-27 17:06:12 +00:00
// Copyright (c) 2023 Cesanta Software Limited
// All rights reserved
#include "hal.h"
#include "mongoose.h"
#include "net.h"
#define BLINK_PERIOD_MS 1000 // LED blinking period in millis
static void timer_fn(void *arg) {
2024-02-01 10:15:32 -03:00
gpio_toggle(LED); // Blink LED
2024-01-27 17:06:12 +00:00
struct mg_tcpip_if *ifp = arg; // And show
const char *names[] = {"down", "up", "req", "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));
}
int main(void) {
hal_init();
struct mg_mgr mgr; // Initialise
mg_mgr_init(&mgr); // Mongoose event manager
mg_log_set(MG_LL_DEBUG); // Set log level
MG_INFO(("Starting, CPU freq %g MHz", (double) SystemCoreClock / 1000000));
2024-02-01 10:15:32 -03:00
2024-01-27 17:06:12 +00:00
// Initialise Mongoose network stack
2024-02-01 10:15:32 -03:00
struct mg_tcpip_driver_ra_data driver_data = {
.clock = SystemCoreClock, .irqno = 0, .phy_addr = 1};
struct mg_tcpip_if mif = {// .mac = {...}, generate random mac address
2024-01-27 17:06:12 +00: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)),
2024-02-01 10:15:32 -03:00
.driver = &mg_tcpip_driver_ra,
2024-01-27 17:06:12 +00:00
.driver_data = &driver_data};
mg_tcpip_init(&mgr, &mif);
2024-02-01 10:15:32 -03:00
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &mif);
2024-01-27 17:06:12 +00:00
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..."));
web_init(&mgr);
MG_INFO(("Starting event loop"));
for (;;) {
mg_mgr_poll(&mgr, 0);
}
return 0;
}