microbit: added accessor/mutator for systemTick

This commit adds a mutator that dynamically reconfigures the
systemTicker to call systemTick at a different period to the default.

The accessor returns the current tick speed in milliseconds.

The accessor is now also used in scheduler_tick to keep timing as
accurate as possible.

MICROBIT_DISPLAY_REFRESH_PERIOD has now been removed, and replaced with
MICROBIT_DEFAULT_TICK_PERIOD.
master
James Devine 2016-01-29 14:07:36 +00:00
parent 6f812aa474
commit 6e316dc741
5 changed files with 58 additions and 14 deletions

View File

@ -46,12 +46,14 @@
#define MICROBIT_NAME_CODE_LETTERS 5
// Random number generator
#define NRF51822_RNG_ADDRESS 0x4000D000
#define NRF51822_RNG_ADDRESS 0x4000D000
// mbed pin assignments of core components.
#define MICROBIT_PIN_SDA P0_30
#define MICROBIT_PIN_SCL P0_0
#define MICROBIT_PIN_SDA P0_30
#define MICROBIT_PIN_SCL P0_0
#define MICROBIT_DEFAULT_TICK_PERIOD FIBER_TICK_PERIOD_MS
/**
* Class definition for a MicroBit device.
@ -74,6 +76,8 @@ class MicroBit
// Periodic callback
Ticker systemTicker;
int tickPeriod;
// I2C Interface
MicroBitI2C i2c;
@ -259,6 +263,21 @@ class MicroBit
*/
int removeIdleComponent(MicroBitComponent *component);
/*
* Reconfigures the ticker to the given speed in milliseconds.
* @param speedMs the speed in milliseconds
* @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if speedUs < 1
*
* @note this will also modify the value that is added to ticks in MiroBitFiber:scheduler_tick()
*/
int setTickPeriod(int speedMs);
/*
* Returns the currently used tick speed in milliseconds
*/
int getTickPeriod();
/**
* Determine the time since this MicroBit was last reset.
*

View File

@ -1,12 +1,6 @@
#ifndef MICROBIT_DISPLAY_H
#define MICROBIT_DISPLAY_H
/**
* Core Configuration settings.
*/
#define MICROBIT_DISPLAY_REFRESH_PERIOD ((float)FIBER_TICK_PERIOD_MS / (float)1000)
/**
* MessageBus Event Codes
*/

View File

@ -1,4 +1,4 @@
/*
/*
* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ
* If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
* The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
@ -133,8 +133,10 @@ void MicroBit::init()
// Seed our random number generator
seedRandom();
tickPeriod = MICROBIT_DEFAULT_TICK_PERIOD;
// Start refreshing the Matrix Display
systemTicker.attach(this, &MicroBit::systemTick, MICROBIT_DISPLAY_REFRESH_PERIOD);
systemTicker.attach_us(this, &MicroBit::systemTick, tickPeriod * 1000);
// Register our compass calibration algorithm.
MessageBus.listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CALIBRATE, this, &MicroBit::compassCalibrator, MESSAGE_BUS_LISTENER_IMMEDIATE);
@ -607,6 +609,35 @@ int MicroBit::removeIdleComponent(MicroBitComponent *component)
return MICROBIT_OK;
}
/*
* Reconfigures the ticker to the given speed in milliseconds.
* @param speedMs the speed in milliseconds
* @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if speedUs < 1
*
* @note this will also modify the value that is added to ticks in MiroBitFiber:scheduler_tick()
*/
int MicroBit::setTickPeriod(int speedMs)
{
if(speedMs < 1)
return MICROBIT_INVALID_PARAMETER;
uBit.systemTicker.detach();
uBit.systemTicker.attach_us(this, &MicroBit::systemTick, speedMs * 1000);
tickPeriod = speedMs;
return MICROBIT_OK;
}
/*
* Returns the currently used tick speed in milliseconds
*/
int MicroBit::getTickPeriod()
{
return tickPeriod;
}
/**
* Determine the time since this MicroBit was last reset.
*

View File

@ -145,7 +145,7 @@ void MicroBitDisplay::render()
//timer does not have enough resolution for brightness of 1. 23.53 us
if(brightness != MICROBIT_DISPLAY_MAXIMUM_BRIGHTNESS && brightness > MICROBIT_DISPLAY_MINIMUM_BRIGHTNESS)
renderTimer.attach(this, &MicroBitDisplay::renderFinish, (((float)brightness) / ((float)MICROBIT_DISPLAY_MAXIMUM_BRIGHTNESS)) * (float)MICROBIT_DISPLAY_REFRESH_PERIOD);
renderTimer.attach_us(this, &MicroBitDisplay::renderFinish, (((brightness * 100) / (MICROBIT_DISPLAY_MAXIMUM_BRIGHTNESS)) * uBit.getTickPeriod()));
//this will take around 23us to execute
if(brightness <= MICROBIT_DISPLAY_MINIMUM_BRIGHTNESS)

View File

@ -181,8 +181,8 @@ void scheduler_tick()
Fiber *t;
// increment our real-time counter.
ticks += FIBER_TICK_PERIOD_MS;
ticks += uBit.getTickPeriod();
// Check the sleep queue, and wake up any fibers as necessary.
while (f != NULL)
{