From 39687aa7c7b138d2a1ce2551f2400bade3b1a6fb Mon Sep 17 00:00:00 2001 From: James McKenzie Date: Sun, 2 Aug 2015 23:30:06 +0100 Subject: fis --- app/ticker.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 app/ticker.c (limited to 'app/ticker.c') diff --git a/app/ticker.c b/app/ticker.c new file mode 100644 index 0000000..dcba0d1 --- /dev/null +++ b/app/ticker.c @@ -0,0 +1,82 @@ +#include "project.h" + + +static volatile uint32_t delay_ms_count; +volatile uint32_t ticks; +static uint32_t scale = 7; + + +void +delay_us (uint32_t d) +{ + d *= scale; + while (d--) + { + __asm__ ("nop"); + } +} + + +void +sys_tick_handler (void) +{ + //uint8_t d; + + if (delay_ms_count) + delay_ms_count--; + + ticks++; + + led_tick (); + lcd_tick (); +} + + +void +delay_ms (uint32_t d) +{ + delay_ms_count = d; + while (delay_ms_count); +} + +int +timed_out (uint32_t then, unsigned int ms) +{ + then = ticks - then; + + if (then > ms) + return 1; + return 0; +} + + +void +ticker_init (void) +{ + uint32_t v, w; + + /*Start periodic timer */ + + systick_set_clocksource (STK_CSR_CLKSOURCE_AHB_DIV8); + /* 48MHz / 8 = > 6Mhz */ + systick_set_reload (6000); + /* 6MHz / 6000 => 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); + + +} -- cgit v1.2.3