#include "project.h" static volatile uint32_t ticks; static volatile uint32_t delay_ms_count; static uint32_t scale = 7; void delay_us (uint32_t d) { d *= scale; while (d--) __asm__ ("nop"); } void sys_tick_handler (void) { if (delay_ms_count) delay_ms_count--; led_tick(); oled1_ticker(); oled2_ticker(); gauge_ticker(); ticks++; } void delay_ms (uint32_t d) { delay_ms_count = d; while (delay_ms_count); } void ticker_init (void) { uint32_t v, w; /*Start periodic timer */ systick_set_clocksource (STK_CSR_CLKSOURCE_AHB_DIV8); /* 72MHz / 8 = > 9Mhz */ systick_set_reload (9000); /* 9MHz / 9000 => 1kHz */ systick_interrupt_enable(); systick_counter_enable(); /*Calibrate the delay loop */ do { scale--; v = ticks; while (v == ticks); delay_us (1000); w = ticks; v++; w -= v; } while (w); }