Files
mongoose/tutorials/nordic/xiao-nrf52840/usb/main.c
T

57 lines
1.6 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;
}
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
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;
}