mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal
Dependencies: BLE_API nRF51822 mbed-dev-bin
Dependents: microbit Microbit IoTChallenge1 microbit ... more
MicroBitSystemTimer.h
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 00038 #ifndef MICROBIT_SYSTEM_TIMER_H 00039 #define MICROBIT_SYSTEM_TIMER_H 00040 00041 #include "mbed.h" 00042 #include "MicroBitConfig.h" 00043 #include "MicroBitComponent.h" 00044 00045 /** 00046 * Initialises a system wide timer, used to drive the various components used in the runtime. 00047 * 00048 * This must be called before any components register to receive periodic periodic callbacks. 00049 * 00050 * @param timer_period The initial period between interrupts, in millseconds. 00051 * 00052 * @return MICROBIT_OK on success. 00053 */ 00054 int system_timer_init(int period); 00055 00056 /** 00057 * Reconfigures the system wide timer to the given period in milliseconds. 00058 * 00059 * @param period the new period of the timer in milliseconds 00060 * 00061 * @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if period < 1 00062 */ 00063 int system_timer_set_period(int period); 00064 00065 /** 00066 * Accessor to obtain the current tick period in milliseconds 00067 * 00068 * @return the current tick period in milliseconds 00069 */ 00070 int system_timer_get_period(); 00071 00072 /** 00073 * Updates the current time in microseconds, since power on. 00074 * 00075 * If the mbed Timer hasn't been initialised, it will be initialised 00076 * on the first call to this function. 00077 */ 00078 inline void update_time(); 00079 00080 /** 00081 * Determines the time since the device was powered on. 00082 * 00083 * @return the current time since power on in milliseconds 00084 */ 00085 uint64_t system_timer_current_time(); 00086 00087 /** 00088 * Determines the time since the device was powered on. 00089 * 00090 * @return the current time since power on in microseconds 00091 */ 00092 uint64_t system_timer_current_time_us(); 00093 00094 /** 00095 * Timer callback. Called from interrupt context, once per period. 00096 * 00097 * Simply checks to determine if any fibers blocked on the sleep queue need to be woken up 00098 * and made runnable. 00099 */ 00100 void system_timer_tick(); 00101 00102 /** 00103 * Add a component to the array of system components. This component will then receive 00104 * periodic callbacks, once every tick period in interrupt context. 00105 * 00106 * @param component The component to add. 00107 * 00108 * @return MICROBIT_OK on success or MICROBIT_NO_RESOURCES if the component array is full. 00109 * 00110 * @code 00111 * // heap allocated - otherwise it will be paged out! 00112 * MicroBitDisplay* display = new MicroBitDisplay(); 00113 * 00114 * system_timer_add_component(display); 00115 * @endcode 00116 */ 00117 int system_timer_add_component(MicroBitComponent *component); 00118 00119 /** 00120 * Remove a component from the array of system components. This component will no longer receive 00121 * periodic callbacks. 00122 * 00123 * @param component The component to remove. 00124 * 00125 * @return MICROBIT_OK on success or MICROBIT_INVALID_PARAMETER is returned if the given component has not been previously added. 00126 * 00127 * @code 00128 * // heap allocated - otherwise it will be paged out! 00129 * MicroBitDisplay* display = new MicroBitDisplay(); 00130 * 00131 * system_timer_add_component(display); 00132 * 00133 * system_timer_remove_component(display); 00134 * @endcode 00135 */ 00136 int system_timer_remove_component(MicroBitComponent *component); 00137 00138 /** 00139 * A simple C/C++ wrapper to allow periodic callbacks to standard C functions transparently. 00140 */ 00141 class MicroBitSystemTimerCallback : MicroBitComponent 00142 { 00143 void (*fn)(void); 00144 00145 /** 00146 * Creates an object that receives periodic callbacks from the system timer, 00147 * and, in turn, calls a plain C function as provided as a parameter. 00148 * 00149 * @param function the function to invoke upon a systemTick. 00150 */ 00151 public: 00152 MicroBitSystemTimerCallback(void (*function)(void)) 00153 { 00154 fn = function; 00155 system_timer_add_component(this); 00156 } 00157 00158 void systemTick() 00159 { 00160 fn(); 00161 } 00162 }; 00163 00164 #endif
Generated on Tue Jul 12 2022 15:22:56 by 1.7.2