Forked to avoid changing the publioc repo

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Lancaster University

Committer:
bluetooth_mdw
Date:
Thu Apr 20 13:42:44 2017 +0000
Revision:
74:4c7db2d5ca9a
Parent:
49:88f03f3feff1
Example app showing how to include the Bluetooth accelerometer service in your micro:bit application code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 1:8aa5cdb4ab67 1 /*
Jonathan Austin 1:8aa5cdb4ab67 2 The MIT License (MIT)
Jonathan Austin 1:8aa5cdb4ab67 3
Jonathan Austin 1:8aa5cdb4ab67 4 Copyright (c) 2016 British Broadcasting Corporation.
Jonathan Austin 1:8aa5cdb4ab67 5 This software is provided by Lancaster University by arrangement with the BBC.
Jonathan Austin 1:8aa5cdb4ab67 6
Jonathan Austin 1:8aa5cdb4ab67 7 Permission is hereby granted, free of charge, to any person obtaining a
Jonathan Austin 1:8aa5cdb4ab67 8 copy of this software and associated documentation files (the "Software"),
Jonathan Austin 1:8aa5cdb4ab67 9 to deal in the Software without restriction, including without limitation
Jonathan Austin 1:8aa5cdb4ab67 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
Jonathan Austin 1:8aa5cdb4ab67 11 and/or sell copies of the Software, and to permit persons to whom the
Jonathan Austin 1:8aa5cdb4ab67 12 Software is furnished to do so, subject to the following conditions:
Jonathan Austin 1:8aa5cdb4ab67 13
Jonathan Austin 1:8aa5cdb4ab67 14 The above copyright notice and this permission notice shall be included in
Jonathan Austin 1:8aa5cdb4ab67 15 all copies or substantial portions of the Software.
Jonathan Austin 1:8aa5cdb4ab67 16
Jonathan Austin 1:8aa5cdb4ab67 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jonathan Austin 1:8aa5cdb4ab67 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jonathan Austin 1:8aa5cdb4ab67 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Jonathan Austin 1:8aa5cdb4ab67 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Jonathan Austin 1:8aa5cdb4ab67 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 23 DEALINGS IN THE SOFTWARE.
Jonathan Austin 1:8aa5cdb4ab67 24 */
Jonathan Austin 1:8aa5cdb4ab67 25
Jonathan Austin 1:8aa5cdb4ab67 26 /**
Jonathan Austin 1:8aa5cdb4ab67 27 * Definitions for the MicroBit system timer.
Jonathan Austin 1:8aa5cdb4ab67 28 *
Jonathan Austin 1:8aa5cdb4ab67 29 * This module provides:
Jonathan Austin 1:8aa5cdb4ab67 30 *
Jonathan Austin 1:8aa5cdb4ab67 31 * 1) a concept of global system time since power up
Jonathan Austin 1:8aa5cdb4ab67 32 * 2) a simple periodic multiplexing API for the underlying mbed implementation.
Jonathan Austin 1:8aa5cdb4ab67 33 *
Jonathan Austin 1:8aa5cdb4ab67 34 * The latter is useful to avoid costs associated with multiple mbed Ticker instances
Jonathan Austin 1:8aa5cdb4ab67 35 * in microbit-dal components, as each incurs a significant additional RAM overhead (circa 80 bytes).
Jonathan Austin 1:8aa5cdb4ab67 36 */
Jonathan Austin 1:8aa5cdb4ab67 37 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 38 #include "MicroBitSystemTimer.h"
Jonathan Austin 1:8aa5cdb4ab67 39 #include "ErrorNo.h"
Jonathan Austin 1:8aa5cdb4ab67 40
Jonathan Austin 1:8aa5cdb4ab67 41 /*
Jonathan Austin 1:8aa5cdb4ab67 42 * Time since power on. Measured in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 43 * When stored as an unsigned long, this gives us approx 50 days between rollover, which is ample. :-)
Jonathan Austin 1:8aa5cdb4ab67 44 */
LancasterUniversity 49:88f03f3feff1 45 static uint64_t time_us = 0;
Jonathan Austin 1:8aa5cdb4ab67 46 static unsigned int tick_period = 0;
Jonathan Austin 1:8aa5cdb4ab67 47
Jonathan Austin 1:8aa5cdb4ab67 48 // Array of components which are iterated during a system tick
Jonathan Austin 1:8aa5cdb4ab67 49 static MicroBitComponent* systemTickComponents[MICROBIT_SYSTEM_COMPONENTS];
Jonathan Austin 1:8aa5cdb4ab67 50
Jonathan Austin 1:8aa5cdb4ab67 51 // Periodic callback interrupt
LancasterUniversity 49:88f03f3feff1 52 static Ticker *ticker = NULL;
LancasterUniversity 49:88f03f3feff1 53
LancasterUniversity 49:88f03f3feff1 54 // System timer.
LancasterUniversity 49:88f03f3feff1 55 static Timer *timer = NULL;
Jonathan Austin 1:8aa5cdb4ab67 56
Jonathan Austin 1:8aa5cdb4ab67 57
Jonathan Austin 1:8aa5cdb4ab67 58 /**
LancasterUniversity 49:88f03f3feff1 59 * Initialises a system wide timer, used to drive the various components used in the runtime.
Jonathan Austin 1:8aa5cdb4ab67 60 *
Jonathan Austin 1:8aa5cdb4ab67 61 * This must be called before any components register to receive periodic periodic callbacks.
Jonathan Austin 1:8aa5cdb4ab67 62 *
Jonathan Austin 1:8aa5cdb4ab67 63 * @param timer_period The initial period between interrupts, in millseconds.
Jonathan Austin 1:8aa5cdb4ab67 64 *
Jonathan Austin 1:8aa5cdb4ab67 65 * @return MICROBIT_OK on success.
Jonathan Austin 1:8aa5cdb4ab67 66 */
Jonathan Austin 1:8aa5cdb4ab67 67 int system_timer_init(int period)
Jonathan Austin 1:8aa5cdb4ab67 68 {
LancasterUniversity 49:88f03f3feff1 69 if (ticker == NULL)
LancasterUniversity 49:88f03f3feff1 70 ticker = new Ticker();
LancasterUniversity 49:88f03f3feff1 71
Jonathan Austin 1:8aa5cdb4ab67 72 if (timer == NULL)
LancasterUniversity 49:88f03f3feff1 73 {
LancasterUniversity 49:88f03f3feff1 74 timer = new Timer();
LancasterUniversity 49:88f03f3feff1 75 timer->start();
LancasterUniversity 49:88f03f3feff1 76 }
Jonathan Austin 1:8aa5cdb4ab67 77
Jonathan Austin 1:8aa5cdb4ab67 78 return system_timer_set_period(period);
Jonathan Austin 1:8aa5cdb4ab67 79 }
Jonathan Austin 1:8aa5cdb4ab67 80
Jonathan Austin 1:8aa5cdb4ab67 81 /**
Jonathan Austin 1:8aa5cdb4ab67 82 * Reconfigures the system wide timer to the given period in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 83 *
Jonathan Austin 1:8aa5cdb4ab67 84 * @param period the new period of the timer in milliseconds
Jonathan Austin 1:8aa5cdb4ab67 85 *
Jonathan Austin 1:8aa5cdb4ab67 86 * @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if period < 1
Jonathan Austin 1:8aa5cdb4ab67 87 */
Jonathan Austin 1:8aa5cdb4ab67 88 int system_timer_set_period(int period)
Jonathan Austin 1:8aa5cdb4ab67 89 {
Jonathan Austin 1:8aa5cdb4ab67 90 if (period < 1)
Jonathan Austin 1:8aa5cdb4ab67 91 return MICROBIT_INVALID_PARAMETER;
Jonathan Austin 1:8aa5cdb4ab67 92
Jonathan Austin 1:8aa5cdb4ab67 93 // If a timer is already running, ensure it is disabled before reconfiguring.
Jonathan Austin 1:8aa5cdb4ab67 94 if (tick_period)
LancasterUniversity 49:88f03f3feff1 95 ticker->detach();
Jonathan Austin 1:8aa5cdb4ab67 96
Jonathan Austin 1:8aa5cdb4ab67 97 // register a period callback to drive the scheduler and any other registered components.
Jonathan Austin 1:8aa5cdb4ab67 98 tick_period = period;
LancasterUniversity 49:88f03f3feff1 99 ticker->attach_us(system_timer_tick, period * 1000);
Jonathan Austin 1:8aa5cdb4ab67 100
Jonathan Austin 1:8aa5cdb4ab67 101 return MICROBIT_OK;
Jonathan Austin 1:8aa5cdb4ab67 102 }
Jonathan Austin 1:8aa5cdb4ab67 103
Jonathan Austin 1:8aa5cdb4ab67 104 /**
Jonathan Austin 1:8aa5cdb4ab67 105 * Accessor to obtain the current tick period in milliseconds
Jonathan Austin 1:8aa5cdb4ab67 106 *
Jonathan Austin 1:8aa5cdb4ab67 107 * @return the current tick period in milliseconds
Jonathan Austin 1:8aa5cdb4ab67 108 */
Jonathan Austin 1:8aa5cdb4ab67 109 int system_timer_get_period()
Jonathan Austin 1:8aa5cdb4ab67 110 {
Jonathan Austin 1:8aa5cdb4ab67 111 return tick_period;
Jonathan Austin 1:8aa5cdb4ab67 112 }
Jonathan Austin 1:8aa5cdb4ab67 113
Jonathan Austin 1:8aa5cdb4ab67 114 /**
LancasterUniversity 49:88f03f3feff1 115 * Updates the current time in microseconds, since power on.
LancasterUniversity 49:88f03f3feff1 116 *
LancasterUniversity 49:88f03f3feff1 117 * If the mbed Timer hasn't been initialised, it will be initialised
LancasterUniversity 49:88f03f3feff1 118 * on the first call to this function.
LancasterUniversity 49:88f03f3feff1 119 */
LancasterUniversity 49:88f03f3feff1 120 void update_time()
LancasterUniversity 49:88f03f3feff1 121 {
LancasterUniversity 49:88f03f3feff1 122 // If we haven't been initialized, bring up the timer with the default period.
LancasterUniversity 49:88f03f3feff1 123 if (timer == NULL || ticker == NULL)
LancasterUniversity 49:88f03f3feff1 124 system_timer_init(SYSTEM_TICK_PERIOD_MS);
LancasterUniversity 49:88f03f3feff1 125
LancasterUniversity 49:88f03f3feff1 126 time_us += timer->read_us();
LancasterUniversity 49:88f03f3feff1 127 timer->reset();
LancasterUniversity 49:88f03f3feff1 128 }
LancasterUniversity 49:88f03f3feff1 129
LancasterUniversity 49:88f03f3feff1 130 /**
Jonathan Austin 1:8aa5cdb4ab67 131 * Determines the time since the device was powered on.
Jonathan Austin 1:8aa5cdb4ab67 132 *
Jonathan Austin 1:8aa5cdb4ab67 133 * @return the current time since power on in milliseconds
Jonathan Austin 1:8aa5cdb4ab67 134 */
LancasterUniversity 49:88f03f3feff1 135 uint64_t system_timer_current_time()
Jonathan Austin 1:8aa5cdb4ab67 136 {
LancasterUniversity 49:88f03f3feff1 137 return system_timer_current_time_us() / 1000;
LancasterUniversity 49:88f03f3feff1 138 }
LancasterUniversity 49:88f03f3feff1 139
LancasterUniversity 49:88f03f3feff1 140 /**
LancasterUniversity 49:88f03f3feff1 141 * Determines the time since the device was powered on.
LancasterUniversity 49:88f03f3feff1 142 *
LancasterUniversity 49:88f03f3feff1 143 * @return the current time since power on in microseconds
LancasterUniversity 49:88f03f3feff1 144 */
LancasterUniversity 49:88f03f3feff1 145 uint64_t system_timer_current_time_us()
LancasterUniversity 49:88f03f3feff1 146 {
LancasterUniversity 49:88f03f3feff1 147 update_time();
LancasterUniversity 49:88f03f3feff1 148 return time_us;
Jonathan Austin 1:8aa5cdb4ab67 149 }
Jonathan Austin 1:8aa5cdb4ab67 150
Jonathan Austin 1:8aa5cdb4ab67 151 /**
Jonathan Austin 1:8aa5cdb4ab67 152 * Timer callback. Called from interrupt context, once per period.
Jonathan Austin 1:8aa5cdb4ab67 153 *
Jonathan Austin 1:8aa5cdb4ab67 154 * Simply checks to determine if any fibers blocked on the sleep queue need to be woken up
Jonathan Austin 1:8aa5cdb4ab67 155 * and made runnable.
Jonathan Austin 1:8aa5cdb4ab67 156 */
Jonathan Austin 1:8aa5cdb4ab67 157 void system_timer_tick()
Jonathan Austin 1:8aa5cdb4ab67 158 {
LancasterUniversity 49:88f03f3feff1 159 update_time();
Jonathan Austin 1:8aa5cdb4ab67 160
Jonathan Austin 1:8aa5cdb4ab67 161 // Update any components registered for a callback
Jonathan Austin 1:8aa5cdb4ab67 162 for(int i = 0; i < MICROBIT_SYSTEM_COMPONENTS; i++)
Jonathan Austin 1:8aa5cdb4ab67 163 if(systemTickComponents[i] != NULL)
Jonathan Austin 1:8aa5cdb4ab67 164 systemTickComponents[i]->systemTick();
Jonathan Austin 1:8aa5cdb4ab67 165 }
Jonathan Austin 1:8aa5cdb4ab67 166
Jonathan Austin 1:8aa5cdb4ab67 167 /**
Jonathan Austin 1:8aa5cdb4ab67 168 * Add a component to the array of system components. This component will then receive
Jonathan Austin 1:8aa5cdb4ab67 169 * periodic callbacks, once every tick period.
Jonathan Austin 1:8aa5cdb4ab67 170 *
Jonathan Austin 1:8aa5cdb4ab67 171 * @param component The component to add.
Jonathan Austin 1:8aa5cdb4ab67 172 *
Jonathan Austin 1:8aa5cdb4ab67 173 * @return MICROBIT_OK on success. MICROBIT_NO_RESOURCES is returned if the component array is full.
Jonathan Austin 1:8aa5cdb4ab67 174 *
Jonathan Austin 1:8aa5cdb4ab67 175 * @note The callback will be in interrupt context.
Jonathan Austin 1:8aa5cdb4ab67 176 */
Jonathan Austin 1:8aa5cdb4ab67 177 int system_timer_add_component(MicroBitComponent *component)
Jonathan Austin 1:8aa5cdb4ab67 178 {
Jonathan Austin 1:8aa5cdb4ab67 179 int i = 0;
Jonathan Austin 1:8aa5cdb4ab67 180
Jonathan Austin 1:8aa5cdb4ab67 181 // If we haven't been initialized, bring up the timer with the default period.
LancasterUniversity 49:88f03f3feff1 182 if (timer == NULL || ticker == NULL)
Jonathan Austin 1:8aa5cdb4ab67 183 system_timer_init(SYSTEM_TICK_PERIOD_MS);
Jonathan Austin 1:8aa5cdb4ab67 184
Jonathan Austin 1:8aa5cdb4ab67 185 while(systemTickComponents[i] != NULL && i < MICROBIT_SYSTEM_COMPONENTS)
Jonathan Austin 1:8aa5cdb4ab67 186 i++;
Jonathan Austin 1:8aa5cdb4ab67 187
Jonathan Austin 1:8aa5cdb4ab67 188 if(i == MICROBIT_SYSTEM_COMPONENTS)
Jonathan Austin 1:8aa5cdb4ab67 189 return MICROBIT_NO_RESOURCES;
Jonathan Austin 1:8aa5cdb4ab67 190
Jonathan Austin 1:8aa5cdb4ab67 191 systemTickComponents[i] = component;
Jonathan Austin 1:8aa5cdb4ab67 192 return MICROBIT_OK;
Jonathan Austin 1:8aa5cdb4ab67 193 }
Jonathan Austin 1:8aa5cdb4ab67 194
Jonathan Austin 1:8aa5cdb4ab67 195 /**
Jonathan Austin 1:8aa5cdb4ab67 196 * Remove a component from the array of system components. This component will no longer receive
Jonathan Austin 1:8aa5cdb4ab67 197 * periodic callbacks.
Jonathan Austin 1:8aa5cdb4ab67 198 *
Jonathan Austin 1:8aa5cdb4ab67 199 * @param component The component to remove.
Jonathan Austin 1:8aa5cdb4ab67 200 *
Jonathan Austin 1:8aa5cdb4ab67 201 * @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if the given component has not been previously added.
Jonathan Austin 1:8aa5cdb4ab67 202 */
Jonathan Austin 1:8aa5cdb4ab67 203 int system_timer_remove_component(MicroBitComponent *component)
Jonathan Austin 1:8aa5cdb4ab67 204 {
Jonathan Austin 1:8aa5cdb4ab67 205 int i = 0;
Jonathan Austin 1:8aa5cdb4ab67 206
Jonathan Austin 1:8aa5cdb4ab67 207 while(systemTickComponents[i] != component && i < MICROBIT_SYSTEM_COMPONENTS)
Jonathan Austin 1:8aa5cdb4ab67 208 i++;
Jonathan Austin 1:8aa5cdb4ab67 209
Jonathan Austin 1:8aa5cdb4ab67 210 if(i == MICROBIT_SYSTEM_COMPONENTS)
Jonathan Austin 1:8aa5cdb4ab67 211 return MICROBIT_INVALID_PARAMETER;
Jonathan Austin 1:8aa5cdb4ab67 212
Jonathan Austin 1:8aa5cdb4ab67 213 systemTickComponents[i] = NULL;
Jonathan Austin 1:8aa5cdb4ab67 214
Jonathan Austin 1:8aa5cdb4ab67 215 return MICROBIT_OK;
LancasterUniversity 31:87789e55bac7 216 }