Attempting to publish a tree
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
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 the system wide timer. 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 * Determines the time since the device was powered on. 00074 * 00075 * @return the current time since power on in milliseconds 00076 */ 00077 unsigned long system_timer_current_time(); 00078 00079 /** 00080 * Timer callback. Called from interrupt context, once per period. 00081 * 00082 * Simply checks to determine if any fibers blocked on the sleep queue need to be woken up 00083 * and made runnable. 00084 */ 00085 void system_timer_tick(); 00086 00087 /** 00088 * Add a component to the array of system components. This component will then receive 00089 * periodic callbacks, once every tick period in interrupt context. 00090 * 00091 * @param component The component to add. 00092 * 00093 * @return MICROBIT_OK on success or MICROBIT_NO_RESOURCES if the component array is full. 00094 * 00095 * @code 00096 * // heap allocated - otherwise it will be paged out! 00097 * MicroBitDisplay* display = new MicroBitDisplay(); 00098 * 00099 * system_timer_add_component(display); 00100 * @endcode 00101 */ 00102 int system_timer_add_component(MicroBitComponent *component); 00103 00104 /** 00105 * Remove a component from the array of system components. This component will no longer receive 00106 * periodic callbacks. 00107 * 00108 * @param component The component to remove. 00109 * 00110 * @return MICROBIT_OK on success or MICROBIT_INVALID_PARAMETER is returned if the given component has not been previously added. 00111 * 00112 * @code 00113 * // heap allocated - otherwise it will be paged out! 00114 * MicroBitDisplay* display = new MicroBitDisplay(); 00115 * 00116 * system_timer_add_component(display); 00117 * 00118 * system_timer_remove_component(display); 00119 * @endcode 00120 */ 00121 int system_timer_remove_component(MicroBitComponent *component); 00122 00123 /** 00124 * A simple C/C++ wrapper to allow periodic callbacks to standard C functions transparently. 00125 */ 00126 class MicroBitSystemTimerCallback : MicroBitComponent 00127 { 00128 void (*fn)(void); 00129 00130 /** 00131 * Creates an object that receives periodic callbacks from the system timer, 00132 * and, in turn, calls a plain C function as provided as a parameter. 00133 * 00134 * @param function the function to invoke upon a systemTick. 00135 */ 00136 public: 00137 MicroBitSystemTimerCallback(void (*function)(void)) 00138 { 00139 fn = function; 00140 system_timer_add_component(this); 00141 } 00142 00143 void systemTick() 00144 { 00145 fn(); 00146 } 00147 }; 00148 00149 #endif
Generated on Tue Jul 12 2022 19:58:09 by 1.7.2