mirror of
https://github.com/cesanta/mongoose.git
synced 2026-12-04 21:58:22 +00:00
65 lines
2.0 KiB
C
65 lines
2.0 KiB
C
// Copyright (c) 2026 Cesanta Software Limited
|
|
// All rights reserved
|
|
|
|
#include "hal.h"
|
|
#include "mongoose.h"
|
|
|
|
static void blink_task(void) {
|
|
static uint64_t blink_timer = 0;
|
|
if (mg_timer_expired(&blink_timer, 500, mg_millis())) {
|
|
hal_gpio_toggle(LED_RED);
|
|
}
|
|
}
|
|
|
|
// Mongoose time source, see MG_ENABLE_CUSTOM_MILLIS
|
|
uint64_t mg_millis(void) {
|
|
return hal_get_tick();
|
|
}
|
|
|
|
// Mongoose random source, see MG_ENABLE_CUSTOM_RANDOM
|
|
bool mg_random(void *buf, size_t len) {
|
|
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
|
|
uint32_t r = hal_rng_read();
|
|
memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// The on-board P25Q16H 2 MB flash for littlefs, see MG_LFS_FLASH in
|
|
// mongoose_config.h. Mongoose's SPI flash API drives it over hal.h's SPI.
|
|
// Early boards (V1.1) have the chip not fitted. Then init fails and the
|
|
// filesystem is not available
|
|
static struct mg_spi s_spi = {NULL, hal_spi_begin, hal_spi_end, hal_spi_txn};
|
|
MG_SPI_FLASH(mg_flash_spi, &s_spi);
|
|
|
|
int main(void) {
|
|
hal_system_init();
|
|
hal_rng_init();
|
|
hal_gpio_write(LED_RED, true); // RGB LED off, it is active low
|
|
hal_gpio_write(LED_GREEN, true);
|
|
hal_gpio_write(LED_BLUE, true);
|
|
hal_gpio_output(LED_RED); // Dashboard LED1, LED2, LED3
|
|
hal_gpio_output(LED_GREEN);
|
|
hal_gpio_output(LED_BLUE);
|
|
int imu = hal_imu_init(); // For the dashboard's real-time graph
|
|
hal_spi_init(); // On-board flash, see MG_SPI_FLASH() above
|
|
|
|
struct mg_mgr mgr;
|
|
mg_mgr_init(&mgr); // Also starts the USB driver, see mongoose_config.h
|
|
mg_dash_init(&mgr); // Use tutorials/device-dashboard/full/, see Makefile
|
|
|
|
MG_INFO(("Initialised. CPU clock: %lu MHz", SystemCoreClock / 1000000));
|
|
MG_INFO(("RAM used: %zu, free: %zu", hal_ram_used(), hal_ram_free()));
|
|
MG_INFO(("USB Ethernet + serial"));
|
|
if (imu) MG_INFO(("IMU at I2C address 0x%x", imu));
|
|
if (!imu) MG_ERROR(("IMU not found, is this a XIAO nRF52840 Sense?"));
|
|
|
|
for (;;) {
|
|
mg_mgr_poll(&mgr, 0);
|
|
mg_dash_poll(&mgr);
|
|
blink_task();
|
|
}
|
|
|
|
return 0;
|
|
}
|