No Changes
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
Diff: source/core/MicroBitSystemTimer.cpp
- Revision:
- 37:b624ae5e94a5
- Parent:
- 35:8ce23bc1af38
- Child:
- 49:88f03f3feff1
--- a/source/core/MicroBitSystemTimer.cpp Wed Jul 13 12:18:15 2016 +0100 +++ b/source/core/MicroBitSystemTimer.cpp Wed Jul 13 12:18:16 2016 +0100 @@ -42,21 +42,18 @@ * Time since power on. Measured in milliseconds. * When stored as an unsigned long, this gives us approx 50 days between rollover, which is ample. :-) */ -static uint64_t time_us = 0; +static unsigned long ticks = 0; static unsigned int tick_period = 0; // Array of components which are iterated during a system tick static MicroBitComponent* systemTickComponents[MICROBIT_SYSTEM_COMPONENTS]; // Periodic callback interrupt -static Ticker *ticker = NULL; - -// System timer. -static Timer *timer = NULL; +static Ticker *timer = NULL; /** - * Initialises a system wide timer, used to drive the various components used in the runtime. + * Initialises the system wide timer. * * This must be called before any components register to receive periodic periodic callbacks. * @@ -66,14 +63,8 @@ */ int system_timer_init(int period) { - if (ticker == NULL) - ticker = new Ticker(); - if (timer == NULL) - { - timer = new Timer(); - timer->start(); - } + timer = new Ticker(); return system_timer_set_period(period); } @@ -92,11 +83,11 @@ // If a timer is already running, ensure it is disabled before reconfiguring. if (tick_period) - ticker->detach(); + timer->detach(); // register a period callback to drive the scheduler and any other registered components. tick_period = period; - ticker->attach_us(system_timer_tick, period * 1000); + timer->attach_us(system_timer_tick, period * 1000); return MICROBIT_OK; } @@ -112,40 +103,13 @@ } /** - * Updates the current time in microseconds, since power on. - * - * If the mbed Timer hasn't been initialised, it will be initialised - * on the first call to this function. - */ -void update_time() -{ - // If we haven't been initialized, bring up the timer with the default period. - if (timer == NULL || ticker == NULL) - system_timer_init(SYSTEM_TICK_PERIOD_MS); - - time_us += timer->read_us(); - timer->reset(); -} - -/** * Determines the time since the device was powered on. * * @return the current time since power on in milliseconds */ -uint64_t system_timer_current_time() +unsigned long system_timer_current_time() { - return system_timer_current_time_us() / 1000; -} - -/** - * Determines the time since the device was powered on. - * - * @return the current time since power on in microseconds - */ -uint64_t system_timer_current_time_us() -{ - update_time(); - return time_us; + return ticks; } /** @@ -156,7 +120,8 @@ */ void system_timer_tick() { - update_time(); + // increment our real-time counter. + ticks += system_timer_get_period(); // Update any components registered for a callback for(int i = 0; i < MICROBIT_SYSTEM_COMPONENTS; i++) @@ -179,7 +144,7 @@ int i = 0; // If we haven't been initialized, bring up the timer with the default period. - if (timer == NULL || ticker == NULL) + if (timer == NULL) system_timer_init(SYSTEM_TICK_PERIOD_MS); while(systemTickComponents[i] != NULL && i < MICROBIT_SYSTEM_COMPONENTS)