From: Nathael Pajani Date: Mon, 2 Feb 2015 01:38:26 +0000 (+0100) Subject: System tick timer additions and improvements Implement the remove callback functionna... X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=6bd46978557e9e8d2d10213bd36a7b3bf2469727;p=soft%2Flpc122x%2Fcore System tick timer additions and improvements Implement the remove callback functionnality Reset wrapping counters when systick is stopped or reset. Add is_systick_running() and systick_get_tick_ms_period() helper functions Add or improve comments. --- diff --git a/core/system.c b/core/system.c index 5a9744a..7a966e9 100644 --- a/core/system.c +++ b/core/system.c @@ -122,6 +122,12 @@ void system_set_default_power_state(void) sys_ctrl->sys_AHB_clk_ctrl = LPC_SYS_ABH_CLK_CTRL_MEM_ALL; } +/* Enter deep sleep. + * NOTE : entering deep sleep implies a lot of side effects. I'll try to lost them all here + * so this can be done right. + * + * Note : see remark about RTC and deep sleep in section 5.3.3 of UM10441 + */ void enter_deep_sleep(void) { struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; diff --git a/core/systick.c b/core/systick.c index a157ea5..6e289bc 100644 --- a/core/systick.c +++ b/core/systick.c @@ -70,8 +70,8 @@ void SysTick_Handler(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. +/* Register a callback to be called every 'period' system ticks. + * returns the callback number if registration was OK. * returns negative value on error. * The callback will get the "global_wrapping_system_ticks" as argument, which wraps every 50 days * or so with a 1ms tick @@ -93,16 +93,34 @@ int add_systick_callback(void (*callback) (uint32_t), uint16_t period) return -EBUSY; } +int remove_systick_callback(void (*callback) (uint32_t)) +{ + int i = 0; + for (i = 0; i < MAX_SYSTICK_CALLBACKS; i++) { + if (cbs[i].callback == callback) { + cbs[i].callback = NULL; + return 0; + } + } + return -EINVAL; +} /***************************************************************************** */ /* systick timer control function */ -/* Start the system tick timer */ +/* Start the system tick timer + * Starting the systick timer also resets the internal tick counters. + * If you need a value that goes beyond one start/stop cycle and accross resets, + * then it's up to you to keep track of this using systick_get_tick_count() and/or + * systick_get_clock_cycles(). + */ void systick_start(void) { struct lpc_system_tick* systick = LPC_SYSTICK; systick->value = 0; systick_running = 1; + global_wrapping_system_ticks = 0; + global_wrapping_system_clock_cycles = tick_reload; systick->control |= LPC_SYSTICK_CTRL_ENABLE; } /* Stop the system tick timer */ @@ -113,18 +131,43 @@ void systick_stop(void) systick_running = 0; systick->value = 0; } -/* Reset the system tick timer, making it count down from the reload value again */ +/* Reset the system tick timer, making it count down from the reload value again + * Reseting the systick timer also resets the internal tick counters. + * If you need a value that goes beyond one start/stop cycle and accross resets, + * then it's up to you to keep track of this using systick_get_tick_count() and/or + * systick_get_clock_cycles(). + */ void systick_reset(void) { struct lpc_system_tick* systick = LPC_SYSTICK; systick->value = 0; + global_wrapping_system_ticks = 0; + global_wrapping_system_clock_cycles = tick_reload; } +/* Get system tick timer current value (counts at get_main_clock() !) */ uint32_t systick_get_timer_val(void) { struct lpc_system_tick* systick = LPC_SYSTICK; return systick->value; } + +/* Check if systick is running (return 1) or not (return 0) */ +uint32_t is_systick_running(void) +{ + return systick_running; +} + +/* Get the system tick period in ms + * A vaue of 0 means the system tick timer has not been configured. + * Note : calls to msleep() or usleep() will configure the system tick timer + * with a value of 1ms if it was not configured yet. + */ +uint32_t systick_get_tick_ms_period(void) +{ + return tick_ms; +} + /* Get the "timer wrapped" indicator. * Used in usleep() function. * Note : the first to call this function will get the right information. diff --git a/include/core/system.h b/include/core/system.h index 2cea777..b4340aa 100644 --- a/include/core/system.h +++ b/include/core/system.h @@ -118,16 +118,38 @@ void clkout_off(void); /* System Tick Timer */ /***************************************************************************** */ -/* Start the system tick timer */ +/* Start the system tick timer + * Starting the systick timer also resets the internal tick counters. + * If you need a value that goes beyond one start/stop cycle and accross resets, + * then it's up to you to keep track of this using systick_get_tick_count() and/or + * systick_get_clock_cycles(). + */ void systick_start(void); + /* Stop the system tick timer */ void systick_stop(void); -/* Reset the system tick timer, making it count down from the reload value again */ + +/* Reset the system tick timer, making it count down from the reload value again + * Reseting the systick timer also resets the internal tick counters. + * If you need a value that goes beyond one start/stop cycle and accross resets, + * then it's up to you to keep track of this using systick_get_tick_count() and/or + * systick_get_clock_cycles(). + */ void systick_reset(void); -/* Get system tick timer current value */ +/* Get system tick timer current value (counts at get_main_clock() !) */ uint32_t systick_get_timer_val(void); +/* Check if systick is running (return 1) or not (return 0) */ +uint32_t is_systick_running(void); + +/* Get the system tick period in ms + * A vaue of 0 means the system tick timer has not been configured. + * Note : calls to msleep() or usleep() will configure the system tick timer + * with a value of 1ms if it was not configured yet. + */ +uint32_t systick_get_tick_ms_period(void); + /* Get the number of system ticks ... since last wrapping of the counter, which * is about 50 days with a 1ms system tick. */ uint32_t systick_get_tick_count(void); @@ -145,13 +167,15 @@ void systick_timer_on(uint32_t ms); void systick_timer_off(void); /* Register a callback to be called every 'period' system ticks. - * returns the callback number, to be used to remove the callback from the table of callbacks. + * returns the callback number if registration was OK. * returns negative value on error. * The callback will get the "global_wrapping_system_ticks" as argument, which wraps every 50 days * or so with a 1ms tick */ #define MAX_SYSTICK_CALLBACKS 4 int add_systick_callback(void (*callback) (uint32_t), uint16_t period); +/* Remove a registered callback, given the callback address used to register it. */ +int remove_systick_callback(void (*callback) (uint32_t)); /***************************************************************************** */ /* Sleeping functions : these use systick */