Attempting to publish a tree

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitSystemTimer.cpp Source File

MicroBitSystemTimer.cpp

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 /**
00027   * Definitions for the MicroBit system timer.
00028   *
00029   * This module provides:
00030   *
00031   * 1) a concept of global system time since power up
00032   * 2) a simple periodic multiplexing API for the underlying mbed implementation.
00033   *
00034   * The latter is useful to avoid costs associated with multiple mbed Ticker instances
00035   * in microbit-dal components, as each incurs a significant additional RAM overhead (circa 80 bytes).
00036   */
00037 #include "MicroBitConfig.h"
00038 #include "MicroBitSystemTimer.h"
00039 #include "ErrorNo.h"
00040 
00041 /*
00042  * Time since power on. Measured in milliseconds.
00043  * When stored as an unsigned long, this gives us approx 50 days between rollover, which is ample. :-)
00044  */
00045 static unsigned long ticks = 0;
00046 static unsigned int tick_period = 0;
00047 
00048 // Array of components which are iterated during a system tick
00049 static MicroBitComponent* systemTickComponents[MICROBIT_SYSTEM_COMPONENTS];
00050 
00051 // Periodic callback interrupt
00052 static Ticker *timer = NULL;
00053 
00054 
00055 /**
00056   * Initialises the system wide timer.
00057   *
00058   * This must be called before any components register to receive periodic periodic callbacks.
00059   *
00060   * @param timer_period The initial period between interrupts, in millseconds.
00061   *
00062   * @return MICROBIT_OK on success.
00063   */
00064 int system_timer_init(int period)
00065 {
00066     if (timer == NULL)
00067         timer = new Ticker();
00068 
00069     return system_timer_set_period(period);
00070 }
00071 
00072 /**
00073   * Reconfigures the system wide timer to the given period in milliseconds.
00074   *
00075   * @param period the new period of the timer in milliseconds
00076   *
00077   * @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if period < 1
00078   */
00079 int system_timer_set_period(int period)
00080 {
00081     if (period < 1)
00082         return MICROBIT_INVALID_PARAMETER;
00083 
00084     // If a timer is already running, ensure it is disabled before reconfiguring.
00085     if (tick_period)
00086         timer->detach();
00087 
00088     // register a period callback to drive the scheduler and any other registered components.
00089     tick_period = period;
00090     timer->attach_us(system_timer_tick, period * 1000);
00091 
00092     return MICROBIT_OK;
00093 }
00094 
00095 /**
00096   * Accessor to obtain the current tick period in milliseconds
00097   *
00098   * @return the current tick period in milliseconds
00099   */
00100 int system_timer_get_period()
00101 {
00102     return tick_period;
00103 }
00104 
00105 /**
00106   * Determines the time since the device was powered on.
00107   *
00108   * @return the current time since power on in milliseconds
00109   */
00110 unsigned long system_timer_current_time()
00111 {
00112     return ticks;
00113 }
00114 
00115 /**
00116   * Timer callback. Called from interrupt context, once per period.
00117   *
00118   * Simply checks to determine if any fibers blocked on the sleep queue need to be woken up
00119   * and made runnable.
00120   */
00121 void system_timer_tick()
00122 {
00123     // increment our real-time counter.
00124     ticks += system_timer_get_period();
00125 
00126     // Update any components registered for a callback
00127     for(int i = 0; i < MICROBIT_SYSTEM_COMPONENTS; i++)
00128         if(systemTickComponents[i] != NULL)
00129             systemTickComponents[i]->systemTick();
00130 }
00131 
00132 /**
00133   * Add a component to the array of system components. This component will then receive
00134   * periodic callbacks, once every tick period.
00135   *
00136   * @param component The component to add.
00137   *
00138   * @return MICROBIT_OK on success. MICROBIT_NO_RESOURCES is returned if the component array is full.
00139   *
00140   * @note The callback will be in interrupt context.
00141   */
00142 int system_timer_add_component(MicroBitComponent *component)
00143 {
00144     int i = 0;
00145 
00146     // If we haven't been initialized, bring up the timer with the default period.
00147     if (timer == NULL)
00148         system_timer_init(SYSTEM_TICK_PERIOD_MS);
00149 
00150     while(systemTickComponents[i] != NULL && i < MICROBIT_SYSTEM_COMPONENTS)
00151         i++;
00152 
00153     if(i == MICROBIT_SYSTEM_COMPONENTS)
00154         return MICROBIT_NO_RESOURCES;
00155 
00156     systemTickComponents[i] = component;
00157     return MICROBIT_OK;
00158 }
00159 
00160 /**
00161   * Remove a component from the array of system components. This component will no longer receive
00162   * periodic callbacks.
00163   *
00164   * @param component The component to remove.
00165   *
00166   * @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if the given component has not been previously added.
00167   */
00168 int system_timer_remove_component(MicroBitComponent *component)
00169 {
00170     int i = 0;
00171 
00172     while(systemTickComponents[i] != component && i < MICROBIT_SYSTEM_COMPONENTS)
00173         i++;
00174 
00175     if(i == MICROBIT_SYSTEM_COMPONENTS)
00176         return MICROBIT_INVALID_PARAMETER;
00177 
00178     systemTickComponents[i] = NULL;
00179 
00180     return MICROBIT_OK;
00181 }