https://github.com/WebBluetoothCG/demos/pull/42
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
inc/core/MicroBitSystemTimer.h@31:87789e55bac7, 2016-07-13 (annotated)
- Committer:
- LancasterUniversity
- Date:
- Wed Jul 13 12:18:10 2016 +0100
- Revision:
- 31:87789e55bac7
- Parent:
- 1:8aa5cdb4ab67
- Child:
- 32:ece16b5987dd
Synchronized with git rev 3923dfff
Author: James Devine
microbit-dal: increase granularity of the system wide timer
Added an mbed Timer instance to maintain a microsecond level timestamp.
Added a function system_timer_current_time_us() which now returns the
time since power on in microseconds.
MicroBitEvents now call system_timer_current_time_us() giving
microsecond level timestamps.
Who changed what in which revision?
User | Revision | Line number | New 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 | /** |
LancasterUniversity | 31:87789e55bac7 | 46 | * Initialises a system wide timer, used to drive the various components used in the runtime. |
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 | /** |
LancasterUniversity | 31:87789e55bac7 | 73 | * Updates the current time in microseconds, since power on. |
LancasterUniversity | 31:87789e55bac7 | 74 | * |
LancasterUniversity | 31:87789e55bac7 | 75 | * If the mbed Timer hasn't been initialised, it will be initialised |
LancasterUniversity | 31:87789e55bac7 | 76 | * on the first call to this function. |
LancasterUniversity | 31:87789e55bac7 | 77 | */ |
LancasterUniversity | 31:87789e55bac7 | 78 | inline void update_time(); |
LancasterUniversity | 31:87789e55bac7 | 79 | |
LancasterUniversity | 31:87789e55bac7 | 80 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | * Determines the time since the device was powered on. |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | * @return the current time since power on in milliseconds |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | */ |
LancasterUniversity | 31:87789e55bac7 | 85 | uint64_t system_timer_current_time(); |
LancasterUniversity | 31:87789e55bac7 | 86 | |
LancasterUniversity | 31:87789e55bac7 | 87 | /** |
LancasterUniversity | 31:87789e55bac7 | 88 | * Determines the time since the device was powered on. |
LancasterUniversity | 31:87789e55bac7 | 89 | * |
LancasterUniversity | 31:87789e55bac7 | 90 | * @return the current time since power on in microseconds |
LancasterUniversity | 31:87789e55bac7 | 91 | */ |
LancasterUniversity | 31:87789e55bac7 | 92 | uint64_t system_timer_current_time_us(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | * Timer callback. Called from interrupt context, once per period. |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | * Simply checks to determine if any fibers blocked on the sleep queue need to be woken up |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | * and made runnable. |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | void system_timer_tick(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | * Add a component to the array of system components. This component will then receive |
Jonathan Austin |
1:8aa5cdb4ab67 | 104 | * periodic callbacks, once every tick period in interrupt context. |
Jonathan Austin |
1:8aa5cdb4ab67 | 105 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 106 | * @param component The component to add. |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | * @return MICROBIT_OK on success or MICROBIT_NO_RESOURCES if the component array is full. |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | * // heap allocated - otherwise it will be paged out! |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | * MicroBitDisplay* display = new MicroBitDisplay(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 114 | * system_timer_add_component(display); |
Jonathan Austin |
1:8aa5cdb4ab67 | 115 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 116 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 117 | int system_timer_add_component(MicroBitComponent *component); |
Jonathan Austin |
1:8aa5cdb4ab67 | 118 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 119 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 120 | * Remove a component from the array of system components. This component will no longer receive |
Jonathan Austin |
1:8aa5cdb4ab67 | 121 | * periodic callbacks. |
Jonathan Austin |
1:8aa5cdb4ab67 | 122 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 123 | * @param component The component to remove. |
Jonathan Austin |
1:8aa5cdb4ab67 | 124 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 125 | * @return MICROBIT_OK on success or MICROBIT_INVALID_PARAMETER is returned if the given component has not been previously added. |
Jonathan Austin |
1:8aa5cdb4ab67 | 126 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 127 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 128 | * // heap allocated - otherwise it will be paged out! |
Jonathan Austin |
1:8aa5cdb4ab67 | 129 | * MicroBitDisplay* display = new MicroBitDisplay(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 130 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 131 | * system_timer_add_component(display); |
Jonathan Austin |
1:8aa5cdb4ab67 | 132 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 133 | * system_timer_remove_component(display); |
Jonathan Austin |
1:8aa5cdb4ab67 | 134 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 135 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 136 | int system_timer_remove_component(MicroBitComponent *component); |
Jonathan Austin |
1:8aa5cdb4ab67 | 137 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 138 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 139 | * A simple C/C++ wrapper to allow periodic callbacks to standard C functions transparently. |
Jonathan Austin |
1:8aa5cdb4ab67 | 140 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 141 | class MicroBitSystemTimerCallback : MicroBitComponent |
Jonathan Austin |
1:8aa5cdb4ab67 | 142 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 143 | void (*fn)(void); |
Jonathan Austin |
1:8aa5cdb4ab67 | 144 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 145 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 146 | * Creates an object that receives periodic callbacks from the system timer, |
Jonathan Austin |
1:8aa5cdb4ab67 | 147 | * and, in turn, calls a plain C function as provided as a parameter. |
Jonathan Austin |
1:8aa5cdb4ab67 | 148 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 149 | * @param function the function to invoke upon a systemTick. |
Jonathan Austin |
1:8aa5cdb4ab67 | 150 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 151 | public: |
Jonathan Austin |
1:8aa5cdb4ab67 | 152 | MicroBitSystemTimerCallback(void (*function)(void)) |
Jonathan Austin |
1:8aa5cdb4ab67 | 153 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 154 | fn = function; |
Jonathan Austin |
1:8aa5cdb4ab67 | 155 | system_timer_add_component(this); |
Jonathan Austin |
1:8aa5cdb4ab67 | 156 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 157 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 158 | void systemTick() |
Jonathan Austin |
1:8aa5cdb4ab67 | 159 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 160 | fn(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 161 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 162 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 163 | |
LancasterUniversity | 31:87789e55bac7 | 164 | #endif |