refactor mstimer callback handling (#400)

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2023-02-18 10:02:13 -06:00
committed by GitHub
parent 343d9f893d
commit 261a506eb4
5 changed files with 112 additions and 220 deletions
+39 -64
View File
@@ -35,32 +35,31 @@
#include "tc.h"
#include "bacnet/basic/sys/mstimer.h"
/* define which timer counter we are using */
#define MY_TIMER TCE0
/* define which timer for the counter we are using */
#define MS_TIMER_COUNTER TCE0
/* define which timer for the callback we are using */
#define MS_TIMER_CALLBACK TCE1
/* counter for the base timer */
static volatile uint32_t Millisecond_Counter;
/* callback data head of list */
static volatile struct mstimer_callback_data_t *Callback_Head;
/**
* Handles the interrupt from the timer
* Handles an interrupt from a hardware millisecond timer
*/
static void my_callback(void)
static void my_counter_handler(void)
{
struct mstimer_callback_data_t *cb;
Millisecond_Counter++;
cb = (struct mstimer_callback_data_t *)Callback_Head;
while (cb) {
if (mstimer_expired(&cb->timer)) {
cb->callback();
if (mstimer_interval(&cb->timer) > 0) {
mstimer_reset(&cb->timer);
}
}
cb = cb->next;
}
}
/**
* Handles an interrupt from a hardware millisecond timer
*/
static void my_callback_handler(void)
{
/* callback might go too long; prevent re-entrency by disable IRQ source */
tc_set_overflow_interrupt_level(&MS_TIMER_CALLBACK, TC_INT_LVL_OFF);
mstimer_callback_handler();
tc_set_overflow_interrupt_level(&MS_TIMER_CALLBACK, TC_INT_LVL_LO);
}
/**
@@ -72,49 +71,13 @@ unsigned long mstimer_now(void)
{
uint32_t timer_value; /* return value */
tc_set_overflow_interrupt_level(&MY_TIMER, TC_INT_LVL_OFF);
tc_set_overflow_interrupt_level(&MS_TIMER_COUNTER, TC_INT_LVL_OFF);
timer_value = Millisecond_Counter;
tc_set_overflow_interrupt_level(&MY_TIMER, TC_INT_LVL_LO);
tc_set_overflow_interrupt_level(&MS_TIMER_COUNTER, TC_INT_LVL_LO);
return timer_value;
}
/**
* Configures and enables a repeating callback function
*
* @param new_cb - pointer to #mstimer_callback_data_t
* @param callback - pointer to a #timer_callback_function function
* @param milliseconds - how often to call the function
*
* @return true if successfully added and enabled
*/
void mstimer_callback(struct mstimer_callback_data_t *new_cb,
mstimer_callback_function callback,
unsigned long milliseconds)
{
struct mstimer_callback_data_t *cb;
tc_set_overflow_interrupt_level(&MY_TIMER, TC_INT_LVL_OFF);
if (new_cb) {
new_cb->callback = callback;
mstimer_set(&new_cb->timer, milliseconds);
}
if (Callback_Head) {
cb = (struct mstimer_callback_data_t *)Callback_Head;
while (cb) {
if (!cb->next) {
cb->next = new_cb;
break;
} else {
cb = cb->next;
}
}
} else {
Callback_Head = new_cb;
}
tc_set_overflow_interrupt_level(&MY_TIMER, TC_INT_LVL_LO);
}
/**
* Timer setup for 1 millisecond timer
*/
@@ -122,13 +85,25 @@ void mstimer_init(void)
{
unsigned long period;
tc_enable(&MY_TIMER);
tc_set_overflow_interrupt_callback(&MY_TIMER, my_callback);
tc_set_wgm(&MY_TIMER, TC_WG_NORMAL);
tc_write_count(&MY_TIMER, 1);
period = sysclk_get_peripheral_bus_hz(&MY_TIMER);
tc_enable(&MS_TIMER_COUNTER);
tc_set_overflow_interrupt_callback(&MS_TIMER_COUNTER,
mstimer_counter_handler);
tc_set_wgm(&MS_TIMER_COUNTER, TC_WG_NORMAL);
tc_write_count(&MS_TIMER_COUNTER, 1);
period = sysclk_get_peripheral_bus_hz(&MS_TIMER_COUNTER);
period /= 1000;
tc_write_period(&MY_TIMER, period);
tc_set_overflow_interrupt_level(&MY_TIMER, TC_INT_LVL_LO);
tc_write_clock_source(&MY_TIMER, TC_CLKSEL_DIV1_gc);
tc_write_period(&MS_TIMER_COUNTER, period);
tc_set_overflow_interrupt_level(&MS_TIMER_COUNTER, TC_INT_LVL_LO);
tc_write_clock_source(&MS_TIMER_COUNTER, TC_CLKSEL_DIV1_gc);
tc_enable(&MS_TIMER_CALLBACK);
tc_set_overflow_interrupt_callback(&MS_TIMER_CALLBACK,
my_callback_handler);
tc_set_wgm(&MS_TIMER_CALLBACK, TC_WG_NORMAL);
tc_write_count(&MS_TIMER_CALLBACK, 1);
period = sysclk_get_peripheral_bus_hz(&MS_TIMER_CALLBACK);
period /= 1000;
tc_write_period(&MS_TIMER_CALLBACK, period);
tc_set_overflow_interrupt_level(&MS_TIMER_CALLBACK, TC_INT_LVL_LO);
tc_write_clock_source(&MS_TIMER_CALLBACK, TC_CLKSEL_DIV1_gc);
}