microbit: Updated MicroBitSystemTimer to use dynamically allocated Ticker

MicroBitSystemTimer uses a statically allocated Ticker object as a timer
source. This both incurs a static memory overhead when the timer is never
initialised, and causes temporal dependencies for applicaitons that
initialise the timer from within a static context.

This patch simply changes the implementaiton of MicroBitSystemTimer to
dynamically allocate the Ticker object on the heap when it is first used to
address both of these issues.

The patch also cleans up an unecessary call to system_timer_init() in
MicroBit::init().
This commit is contained in:
Joe Finney 2016-03-19 22:46:09 +00:00
parent 65affa35a8
commit b9e8dc979b
2 changed files with 7 additions and 7 deletions

View File

@ -137,9 +137,6 @@ void MicroBit::init()
microbit_create_nested_heap(MICROBIT_NESTED_HEAP_SIZE);
#endif
// Bring up the system timer.
system_timer_init(SYSTEM_TICK_PERIOD_MS);
// Bring up fiber scheduler.
scheduler_init(&messageBus);

View File

@ -24,7 +24,7 @@ static unsigned int tick_period = 0;
static MicroBitComponent* systemTickComponents[MICROBIT_SYSTEM_COMPONENTS];
// Periodic callback interrupt
static Ticker timer;
static Ticker *timer = NULL;
/**
@ -36,6 +36,9 @@ static Ticker timer;
*/
int system_timer_init(int period)
{
if (timer == NULL)
timer = new Ticker();
return system_timer_set_period(period);
}
@ -52,11 +55,11 @@ int system_timer_set_period(int period)
// If a timer is already running, ensure it is disabled before reconfiguring.
if (tick_period)
timer.detach();
timer->detach();
// register a period callback to drive the scheduler and any other registered components.
tick_period = period;
timer.attach_us(system_timer_tick, period * 1000);
timer->attach_us(system_timer_tick, period * 1000);
return MICROBIT_OK;
}
@ -107,7 +110,7 @@ int system_timer_add_component(MicroBitComponent *component)
int i = 0;
// If we haven't been initialized, bring up the timer with the default period.
if (tick_period == 0)
if (timer == NULL)
system_timer_init(SYSTEM_TICK_PERIOD_MS);
while(systemTickComponents[i] != NULL && i < MICROBIT_SYSTEM_COMPONENTS)