2023-02-04 03:32:38 +08:00
|
|
|
// Copyright (c) 2023 Cesanta Software Limited
|
2023-01-22 19:31:06 +08:00
|
|
|
// All rights reserved
|
|
|
|
|
2023-02-09 04:14:39 +08:00
|
|
|
#include "hal.h"
|
2023-01-22 19:31:06 +08:00
|
|
|
#include "mongoose.h"
|
|
|
|
|
2023-02-09 04:14:39 +08:00
|
|
|
#define LED1 PIN('B', 0) // On-board LED pin (green)
|
|
|
|
#define LED2 PIN('E', 1) // On-board LED pin (yellow)
|
|
|
|
#define LED3 PIN('B', 14) // On-board LED pin (red)
|
2023-01-22 19:31:06 +08:00
|
|
|
|
2023-02-09 04:14:39 +08:00
|
|
|
#define LED LED2 // Use blue LED for blinking
|
|
|
|
#define BLINK_PERIOD_MS 1000 // LED blinking period in millis
|
2023-01-22 19:31:06 +08:00
|
|
|
|
2023-02-09 04:14:39 +08:00
|
|
|
static volatile uint64_t s_ticks; // Milliseconds since boot
|
|
|
|
void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms
|
|
|
|
s_ticks++;
|
2023-01-22 19:31:06 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 04:14:39 +08:00
|
|
|
uint64_t mg_millis(void) { // Let Mongoose use our uptime function
|
|
|
|
return s_ticks; // Return number of milliseconds since boot
|
|
|
|
}
|
2023-02-04 03:32:38 +08:00
|
|
|
|
2023-02-09 04:14:39 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2023-02-04 03:32:38 +08:00
|
|
|
|
2023-02-09 04:14:39 +08:00
|
|
|
static void timer_fn(void *arg) {
|
|
|
|
gpio_toggle(LED); // Blink LED
|
|
|
|
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));
|
|
|
|
}
|
2023-02-04 03:32:38 +08:00
|
|
|
|
2023-02-09 04:14:39 +08:00
|
|
|
static void ethernet_init(void) {
|
|
|
|
// Initialise Ethernet. Enable MAC GPIO pins, see
|
2023-02-04 03:32:38 +08:00
|
|
|
// https://www.st.com/resource/en/user_manual/um2407-stm32h7-nucleo144-boards-mb1364-stmicroelectronics.pdf
|
|
|
|
uint16_t pins[] = {PIN('A', 1), PIN('A', 2), PIN('A', 7),
|
|
|
|
PIN('B', 13), PIN('C', 1), PIN('C', 4),
|
|
|
|
PIN('C', 5), PIN('G', 11), PIN('G', 13)};
|
|
|
|
for (size_t i = 0; i < sizeof(pins) / sizeof(pins[0]); i++) {
|
|
|
|
gpio_init(pins[i], GPIO_MODE_AF, GPIO_OTYPE_PUSH_PULL, GPIO_SPEED_INSANE,
|
2023-02-09 04:14:39 +08:00
|
|
|
GPIO_PULL_NONE, 11); // 11 is the Ethernet function
|
2023-02-04 03:32:38 +08:00
|
|
|
}
|
2023-02-09 04:14:39 +08:00
|
|
|
NVIC_EnableIRQ(ETH_IRQn); // Setup Ethernet IRQ handler
|
2023-02-04 03:32:38 +08:00
|
|
|
SETBITS(SYSCFG->PMCR, 7 << 21, 4 << 21); // Use RMII (12.3.1)
|
|
|
|
RCC->AHB1ENR |= BIT(15) | BIT(16) | BIT(17); // Enable Ethernet clocks
|
2023-02-09 04:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
gpio_output(LED); // Setup green LED
|
|
|
|
uart_init(UART_DEBUG, 115200); // Initialise debug printf
|
|
|
|
ethernet_init(); // Initialise ethernet pins
|
2023-01-22 19:31:06 +08:00
|
|
|
|
2023-02-09 04:14:39 +08:00
|
|
|
MG_INFO(("Chip revision: %c, max cpu clock: %u MHz", chiprev(),
|
|
|
|
(chiprev() == 'V') ? 480 : 400));
|
|
|
|
MG_INFO(("Starting, CPU freq %g MHz", (double) SystemCoreClock / 1000000));
|
|
|
|
|
|
|
|
struct mg_mgr mgr; // Initialise
|
|
|
|
mg_mgr_init(&mgr); // Mongoose event manager
|
2023-01-22 19:31:06 +08:00
|
|
|
mg_log_set(MG_LL_DEBUG); // Set log level
|
|
|
|
|
2023-02-04 03:32:38 +08:00
|
|
|
// 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
|
2023-02-08 05:16:42 +08:00
|
|
|
struct mg_tcpip_driver_stm32h_data driver_data = {.mdc_cr = 4};
|
2023-02-09 04:14:39 +08:00
|
|
|
struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(),
|
|
|
|
.driver = &mg_tcpip_driver_stm32h,
|
|
|
|
.driver_data = &driver_data};
|
2023-02-08 05:16:42 +08:00
|
|
|
mg_tcpip_init(&mgr, &mif);
|
2023-02-09 04:14:39 +08:00
|
|
|
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &mif);
|
2023-02-04 03:32:38 +08:00
|
|
|
|
2023-02-09 04:14:39 +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-01-22 19:31:06 +08:00
|
|
|
mg_mgr_poll(&mgr, 0);
|
|
|
|
}
|
|
|
|
|
2023-02-04 03:32:38 +08:00
|
|
|
MG_INFO(("Initialising application..."));
|
|
|
|
extern void device_dashboard_fn(struct mg_connection *, int, void *, void *);
|
2023-02-09 04:14:39 +08:00
|
|
|
mg_http_listen(&mgr, "http://0.0.0.0", device_dashboard_fn, NULL);
|
2023-02-04 03:32:38 +08:00
|
|
|
|
|
|
|
MG_INFO(("Starting event loop"));
|
2023-02-09 04:14:39 +08:00
|
|
|
for (;;) {
|
|
|
|
mg_mgr_poll(&mgr, 0);
|
|
|
|
}
|
2023-02-04 03:32:38 +08:00
|
|
|
|
2023-01-22 19:31:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|