from bbc microbit library

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit

Fork of microbit-dal by Lancaster University

Committer:
Jonathan Austin
Date:
Thu Apr 07 01:33:22 2016 +0100
Revision:
1:8aa5cdb4ab67
Child:
31:87789e55bac7
Synchronized with git rev 55cb9199

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
Jonathan Austin 1:8aa5cdb4ab67 38 #ifndef MICROBIT_SYSTEM_TIMER_H
Jonathan Austin 1:8aa5cdb4ab67 39 #define MICROBIT_SYSTEM_TIMER_H
Jonathan Austin 1:8aa5cdb4ab67 40
Jonathan Austin 1:8aa5cdb4ab67 41 #include "mbed.h"
Jonathan Austin 1:8aa5cdb4ab67 42 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 43 #include "MicroBitComponent.h"
Jonathan Austin 1:8aa5cdb4ab67 44
Jonathan Austin 1:8aa5cdb4ab67 45 /**
Jonathan Austin 1:8aa5cdb4ab67 46 * Initialises the system wide timer.
Jonathan Austin 1:8aa5cdb4ab67 47 *
Jonathan Austin 1:8aa5cdb4ab67 48 * This must be called before any components register to receive periodic periodic callbacks.
Jonathan Austin 1:8aa5cdb4ab67 49 *
Jonathan Austin 1:8aa5cdb4ab67 50 * @param timer_period The initial period between interrupts, in millseconds.
Jonathan Austin 1:8aa5cdb4ab67 51 *
Jonathan Austin 1:8aa5cdb4ab67 52 * @return MICROBIT_OK on success.
Jonathan Austin 1:8aa5cdb4ab67 53 */
Jonathan Austin 1:8aa5cdb4ab67 54 int system_timer_init(int period);
Jonathan Austin 1:8aa5cdb4ab67 55
Jonathan Austin 1:8aa5cdb4ab67 56 /**
Jonathan Austin 1:8aa5cdb4ab67 57 * Reconfigures the system wide timer to the given period in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 58 *
Jonathan Austin 1:8aa5cdb4ab67 59 * @param period the new period of the timer in milliseconds
Jonathan Austin 1:8aa5cdb4ab67 60 *
Jonathan Austin 1:8aa5cdb4ab67 61 * @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if period < 1
Jonathan Austin 1:8aa5cdb4ab67 62 */
Jonathan Austin 1:8aa5cdb4ab67 63 int system_timer_set_period(int period);
Jonathan Austin 1:8aa5cdb4ab67 64
Jonathan Austin 1:8aa5cdb4ab67 65 /**
Jonathan Austin 1:8aa5cdb4ab67 66 * Accessor to obtain the current tick period in milliseconds
Jonathan Austin 1:8aa5cdb4ab67 67 *
Jonathan Austin 1:8aa5cdb4ab67 68 * @return the current tick period in milliseconds
Jonathan Austin 1:8aa5cdb4ab67 69 */
Jonathan Austin 1:8aa5cdb4ab67 70 int system_timer_get_period();
Jonathan Austin 1:8aa5cdb4ab67 71
Jonathan Austin 1:8aa5cdb4ab67 72 /**
Jonathan Austin 1:8aa5cdb4ab67 73 * Determines the time since the device was powered on.
Jonathan Austin 1:8aa5cdb4ab67 74 *
Jonathan Austin 1:8aa5cdb4ab67 75 * @return the current time since power on in milliseconds
Jonathan Austin 1:8aa5cdb4ab67 76 */
Jonathan Austin 1:8aa5cdb4ab67 77 unsigned long system_timer_current_time();
Jonathan Austin 1:8aa5cdb4ab67 78
Jonathan Austin 1:8aa5cdb4ab67 79 /**
Jonathan Austin 1:8aa5cdb4ab67 80 * Timer callback. Called from interrupt context, once per period.
Jonathan Austin 1:8aa5cdb4ab67 81 *
Jonathan Austin 1:8aa5cdb4ab67 82 * Simply checks to determine if any fibers blocked on the sleep queue need to be woken up
Jonathan Austin 1:8aa5cdb4ab67 83 * and made runnable.
Jonathan Austin 1:8aa5cdb4ab67 84 */
Jonathan Austin 1:8aa5cdb4ab67 85 void system_timer_tick();
Jonathan Austin 1:8aa5cdb4ab67 86
Jonathan Austin 1:8aa5cdb4ab67 87 /**
Jonathan Austin 1:8aa5cdb4ab67 88 * Add a component to the array of system components. This component will then receive
Jonathan Austin 1:8aa5cdb4ab67 89 * periodic callbacks, once every tick period in interrupt context.
Jonathan Austin 1:8aa5cdb4ab67 90 *
Jonathan Austin 1:8aa5cdb4ab67 91 * @param component The component to add.
Jonathan Austin 1:8aa5cdb4ab67 92 *
Jonathan Austin 1:8aa5cdb4ab67 93 * @return MICROBIT_OK on success or MICROBIT_NO_RESOURCES if the component array is full.
Jonathan Austin 1:8aa5cdb4ab67 94 *
Jonathan Austin 1:8aa5cdb4ab67 95 * @code
Jonathan Austin 1:8aa5cdb4ab67 96 * // heap allocated - otherwise it will be paged out!
Jonathan Austin 1:8aa5cdb4ab67 97 * MicroBitDisplay* display = new MicroBitDisplay();
Jonathan Austin 1:8aa5cdb4ab67 98 *
Jonathan Austin 1:8aa5cdb4ab67 99 * system_timer_add_component(display);
Jonathan Austin 1:8aa5cdb4ab67 100 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 101 */
Jonathan Austin 1:8aa5cdb4ab67 102 int system_timer_add_component(MicroBitComponent *component);
Jonathan Austin 1:8aa5cdb4ab67 103
Jonathan Austin 1:8aa5cdb4ab67 104 /**
Jonathan Austin 1:8aa5cdb4ab67 105 * Remove a component from the array of system components. This component will no longer receive
Jonathan Austin 1:8aa5cdb4ab67 106 * periodic callbacks.
Jonathan Austin 1:8aa5cdb4ab67 107 *
Jonathan Austin 1:8aa5cdb4ab67 108 * @param component The component to remove.
Jonathan Austin 1:8aa5cdb4ab67 109 *
Jonathan Austin 1:8aa5cdb4ab67 110 * @return MICROBIT_OK on success or MICROBIT_INVALID_PARAMETER is returned if the given component has not been previously added.
Jonathan Austin 1:8aa5cdb4ab67 111 *
Jonathan Austin 1:8aa5cdb4ab67 112 * @code
Jonathan Austin 1:8aa5cdb4ab67 113 * // heap allocated - otherwise it will be paged out!
Jonathan Austin 1:8aa5cdb4ab67 114 * MicroBitDisplay* display = new MicroBitDisplay();
Jonathan Austin 1:8aa5cdb4ab67 115 *
Jonathan Austin 1:8aa5cdb4ab67 116 * system_timer_add_component(display);
Jonathan Austin 1:8aa5cdb4ab67 117 *
Jonathan Austin 1:8aa5cdb4ab67 118 * system_timer_remove_component(display);
Jonathan Austin 1:8aa5cdb4ab67 119 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 120 */
Jonathan Austin 1:8aa5cdb4ab67 121 int system_timer_remove_component(MicroBitComponent *component);
Jonathan Austin 1:8aa5cdb4ab67 122
Jonathan Austin 1:8aa5cdb4ab67 123 /**
Jonathan Austin 1:8aa5cdb4ab67 124 * A simple C/C++ wrapper to allow periodic callbacks to standard C functions transparently.
Jonathan Austin 1:8aa5cdb4ab67 125 */
Jonathan Austin 1:8aa5cdb4ab67 126 class MicroBitSystemTimerCallback : MicroBitComponent
Jonathan Austin 1:8aa5cdb4ab67 127 {
Jonathan Austin 1:8aa5cdb4ab67 128 void (*fn)(void);
Jonathan Austin 1:8aa5cdb4ab67 129
Jonathan Austin 1:8aa5cdb4ab67 130 /**
Jonathan Austin 1:8aa5cdb4ab67 131 * Creates an object that receives periodic callbacks from the system timer,
Jonathan Austin 1:8aa5cdb4ab67 132 * and, in turn, calls a plain C function as provided as a parameter.
Jonathan Austin 1:8aa5cdb4ab67 133 *
Jonathan Austin 1:8aa5cdb4ab67 134 * @param function the function to invoke upon a systemTick.
Jonathan Austin 1:8aa5cdb4ab67 135 */
Jonathan Austin 1:8aa5cdb4ab67 136 public:
Jonathan Austin 1:8aa5cdb4ab67 137 MicroBitSystemTimerCallback(void (*function)(void))
Jonathan Austin 1:8aa5cdb4ab67 138 {
Jonathan Austin 1:8aa5cdb4ab67 139 fn = function;
Jonathan Austin 1:8aa5cdb4ab67 140 system_timer_add_component(this);
Jonathan Austin 1:8aa5cdb4ab67 141 }
Jonathan Austin 1:8aa5cdb4ab67 142
Jonathan Austin 1:8aa5cdb4ab67 143 void systemTick()
Jonathan Austin 1:8aa5cdb4ab67 144 {
Jonathan Austin 1:8aa5cdb4ab67 145 fn();
Jonathan Austin 1:8aa5cdb4ab67 146 }
Jonathan Austin 1:8aa5cdb4ab67 147 };
Jonathan Austin 1:8aa5cdb4ab67 148
Jonathan Austin 1:8aa5cdb4ab67 149 #endif