From: Nathael Pajani Date: Sat, 4 Jan 2014 12:52:13 +0000 (+0100) Subject: System tick callback : the basics of OS implementation :) X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=b6ce7b65e020c6b3bd6c57dc587a40264367c9f6;p=soft%2Flpc122x%2Fcore System tick callback : the basics of OS implementation :) --- diff --git a/core/systick.c b/core/systick.c index cd6f2e2..82672cb 100644 --- a/core/systick.c +++ b/core/systick.c @@ -38,16 +38,55 @@ static volatile uint32_t systick_running = 0; /* Wraps every 50 days or so with a 1ms tick */ static volatile uint32_t global_wrapping_system_ticks = 0; + +struct systick_callback { + void (*callback) (uint32_t); + uint16_t period; + uint16_t countdown; +}; +static volatile struct systick_callback cbs[MAX_SYSTICK_CALLBACKS] = {}; + /* System Tick Timer Interrupt Handler */ void SysTick_Handler(void) { + int i = 0; global_wrapping_system_ticks++; if (sleep_count != 0) { sleep_count--; } + for (i = 0; i < MAX_SYSTICK_CALLBACKS; i++) { + if (cbs[i].callback != NULL) { + cbs[i].countdown--; + if (cbs[i].countdown == 0) { + cbs[i].countdown = cbs[i].period; + cbs[i].callback(global_wrapping_system_ticks); + } + } + } } +/* Register a callback to be called every 'period' system ticks with 'param' parameter. + * returns the callback number, to be used to remove the callback from the table of callbacks. + * returns negative value on error. + */ +int add_systick_callback(void (*callback) (uint32_t), uint16_t period) +{ + int i = 0; + if (period == 0) { + return -EINVAL; + } + for (i = 0; i < MAX_SYSTICK_CALLBACKS; i++) { + if (cbs[i].callback == NULL) { + cbs[i].callback = callback; + cbs[i].period = period; + cbs[i].countdown = period; + return i; + } + } + return -EBUSY; +} + /***************************************************************************** */ /* systick timer control function */ diff --git a/include/core/system.h b/include/core/system.h index ea037ca..9f791db 100644 --- a/include/core/system.h +++ b/include/core/system.h @@ -135,6 +135,12 @@ void systick_timer_on(uint32_t ms); /* Removes the main clock from the selected timer block */ void systick_timer_off(void); +/* Register a callback to be called every 'period' system ticks with 'param' parameter. + * returns the callback number, to be used to remove the callback from the table of callbacks. + * returns negative value on error. + */ +#define MAX_SYSTICK_CALLBACKS 4 +int add_systick_callback(void (*callback) (uint32_t), uint16_t period); /***************************************************************************** */ /* Sleeping functions : these use systick */