forked from cpq/bare-metal-programming-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
44 lines (35 loc) · 1.14 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) 2022-2023 Cesanta Software Limited
// SPDX-License-Identifier: MIT
#include "hal.h"
#define BLINK_PERIOD_MS 500 // LED blinking period in millis
#define LOG_PERIOD_MS 1000 // Info log period in millis
uint32_t SystemCoreClock; // Required by CMSIS. Holds system core cock value
void SystemInit(void) { // Called automatically by startup code
clock_init(); // Sets SystemCoreClock
}
static volatile uint64_t s_ticks; // Milliseconds since boot
void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms
s_ticks++;
}
static void led_task(void) { // Blink LED every BLINK_PERIOD_MS
static uint64_t timer = 0;
if (timer_expired(&timer, BLINK_PERIOD_MS, s_ticks)) {
gpio_toggle(LED_PIN);
}
}
static void log_task(void) { // Print a log every LOG_PERIOD_MS
static uint64_t timer = 0;
if (timer_expired(&timer, LOG_PERIOD_MS, s_ticks)) {
printf("tick: %5lu, CPU %lu MHz\n", (unsigned long) s_ticks,
SystemCoreClock / 1000000);
}
}
int main(void) {
gpio_output(LED_PIN);
uart_init(UART_DEBUG, 115200);
for (;;) {
led_task();
log_task();
}
return 0;
}