Mark Gottscho / HardwareTimersLib

Fork of HardwareTimersLib by Mark Gottscho

Committer:
mgottscho
Date:
Sun Mar 16 01:08:20 2014 +0000
Revision:
14:960fbd85909f
Parent:
12:cb395e4be69e
Bug fix.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mgottscho 0:47acc8320421 1 /* HardwareTimer.h
mgottscho 0:47acc8320421 2 * Tested with mbed board: FRDM-KL46Z
mgottscho 0:47acc8320421 3 * Author: Mark Gottscho
mgottscho 0:47acc8320421 4 * mgottscho@ucla.edu
mgottscho 0:47acc8320421 5 */
mgottscho 0:47acc8320421 6
mgottscho 0:47acc8320421 7 #ifndef HARDWARETIMER_H
mgottscho 0:47acc8320421 8 #define HARDWARETIMER_H
mgottscho 0:47acc8320421 9
mgottscho 0:47acc8320421 10 #include "mbed.h"
mgottscho 0:47acc8320421 11 #include "PreciseTime.h"
mgottscho 0:47acc8320421 12
mgottscho 0:47acc8320421 13 /**
mgottscho 0:47acc8320421 14 * This provides a base class from which actual hardware timers should derive their implementations.
mgottscho 0:47acc8320421 15 * This allows for a nice software interface regardless of the particular timer used.
mgottscho 0:47acc8320421 16 */
mgottscho 0:47acc8320421 17 class HardwareTimer {
mgottscho 0:47acc8320421 18 public:
mgottscho 7:78f6ee57d324 19 typedef enum {
mgottscho 7:78f6ee57d324 20 ns,
mgottscho 7:78f6ee57d324 21 us,
mgottscho 7:78f6ee57d324 22 ms,
mgottscho 7:78f6ee57d324 23 s,
mgottscho 7:78f6ee57d324 24 m,
mgottscho 7:78f6ee57d324 25 h
mgottscho 7:78f6ee57d324 26 } tick_units_t;
mgottscho 7:78f6ee57d324 27
mgottscho 0:47acc8320421 28 /**
mgottscho 0:47acc8320421 29 * Constructs a new HardwareTimer.
mgottscho 7:78f6ee57d324 30 * @param valid if false, none of the timer functions can be used. This is intended to enforce only one object
mgottscho 7:78f6ee57d324 31 * managing a unique hardware timer.
mgottscho 7:78f6ee57d324 32 * @param maxRolloverTick maximum number of ticks in the hardware unit before it rolls over.
mgottscho 7:78f6ee57d324 33 * @param tickValue the amount of time corresponding to each timer tick, in units given by tickUnits.
mgottscho 7:78f6ee57d324 34 * @param tickUnits units for tickValue
mgottscho 0:47acc8320421 35 */
mgottscho 7:78f6ee57d324 36 HardwareTimer(uint32_t maxRolloverTick, float tickValue, tick_units_t tickUnits);
mgottscho 0:47acc8320421 37
mgottscho 0:47acc8320421 38 /**
mgottscho 7:78f6ee57d324 39 * Destructs the HardwareTimer.
mgottscho 7:78f6ee57d324 40 */
mgottscho 7:78f6ee57d324 41 virtual ~HardwareTimer();
mgottscho 7:78f6ee57d324 42
mgottscho 7:78f6ee57d324 43 /**
mgottscho 7:78f6ee57d324 44 * @returns true if this timer is valid. If false, the timer cannot be used.
mgottscho 0:47acc8320421 45 */
mgottscho 0:47acc8320421 46 bool valid();
mgottscho 0:47acc8320421 47
mgottscho 0:47acc8320421 48 /**
mgottscho 7:78f6ee57d324 49 * @returns true if the timer is ready to start.
mgottscho 7:78f6ee57d324 50 */
mgottscho 7:78f6ee57d324 51 bool enabled();
mgottscho 7:78f6ee57d324 52
mgottscho 7:78f6ee57d324 53 /**
mgottscho 0:47acc8320421 54 * @returns true if the timer is running.
mgottscho 0:47acc8320421 55 */
mgottscho 7:78f6ee57d324 56 bool running();
mgottscho 0:47acc8320421 57
mgottscho 0:47acc8320421 58 /**
mgottscho 7:78f6ee57d324 59 * @returns the amount of time corresponding to each timer tick in units given by tickUnits().
mgottscho 0:47acc8320421 60 */
mgottscho 0:47acc8320421 61 float tickValue();
mgottscho 0:47acc8320421 62
mgottscho 0:47acc8320421 63 /**
mgottscho 7:78f6ee57d324 64 * @returns time in seconds corresponding to each tick
mgottscho 7:78f6ee57d324 65 */
mgottscho 7:78f6ee57d324 66 float tickUnits();
mgottscho 7:78f6ee57d324 67
mgottscho 7:78f6ee57d324 68 /**
mgottscho 7:78f6ee57d324 69 * Enables the timer with a user-specified callback function that is called each time the timer expires.
mgottscho 7:78f6ee57d324 70 * @param fptr the user callback function
mgottscho 0:47acc8320421 71 */
mgottscho 7:78f6ee57d324 72 void enable(void (*fptr)(void));
mgottscho 7:78f6ee57d324 73
mgottscho 7:78f6ee57d324 74 /**
mgottscho 7:78f6ee57d324 75 * Enables the timer with a user-specified callback function that is called each time the timer expires.
mgottscho 7:78f6ee57d324 76 * @param tptr the object
mgottscho 7:78f6ee57d324 77 * @param mptr method to call on the object
mgottscho 7:78f6ee57d324 78 */
mgottscho 14:960fbd85909f 79 template<typename T> void enable(T *tptr, void (T::*mptr)(void));
mgottscho 0:47acc8320421 80
mgottscho 0:47acc8320421 81 /**
mgottscho 7:78f6ee57d324 82 * Stops and disables the timer. No user function callbacks will be made, and the tick value stops increasing.
mgottscho 0:47acc8320421 83 */
mgottscho 7:78f6ee57d324 84 void disable();
mgottscho 7:78f6ee57d324 85
mgottscho 7:78f6ee57d324 86 /**
mgottscho 7:78f6ee57d324 87 * Starts the timer. If valid() or enabled() are false, then this method does nothing. Otherwise, the timer
mgottscho 7:78f6ee57d324 88 * begins ticking. The user callback function specified in enableTimer() is called each time the timer rolls over.
mgottscho 7:78f6ee57d324 89 * @param callback_tick_count the modulo tick value for when the timer calls the user callback function.
mgottscho 7:78f6ee57d324 90 * Note that the timer counts up. Note that some timers may not support the full 32-bit range. Use getMaxCallbackTickCount()
mgottscho 7:78f6ee57d324 91 * To check the maximum allowed value. If callback_tick_count is greater than that value, this method will have no effect.
mgottscho 7:78f6ee57d324 92 * @param periodic if true, the timer will call the user function every time the internal tick modulo callback_tick_count is reached.
mgottscho 12:cb395e4be69e 93 * If false, the user callback function is only called the first num_callbacks times.
mgottscho 12:cb395e4be69e 94 * @param num_callbacks if periodic is set to false, this many callbacks will be made. Before the timer stops.
mgottscho 7:78f6ee57d324 95 */
mgottscho 12:cb395e4be69e 96 void start(uint32_t callback_tick_count, bool periodic, uint32_t num_callbacks);
mgottscho 7:78f6ee57d324 97
mgottscho 7:78f6ee57d324 98 /**
mgottscho 7:78f6ee57d324 99 * @returns the maximum value of the user-settable callback tick count (via startTimer()).
mgottscho 7:78f6ee57d324 100 * Some timers may support full 32-bit tick counts, while others may be less.
mgottscho 7:78f6ee57d324 101 */
mgottscho 7:78f6ee57d324 102 uint32_t getMaxCallbackTickCount();
mgottscho 0:47acc8320421 103
mgottscho 0:47acc8320421 104 /**
mgottscho 0:47acc8320421 105 * Gets the timer value in a nice form.
mgottscho 0:47acc8320421 106 * Note that in general, the timer may overflow, leading to saturated values obtained from getTime().
mgottscho 0:47acc8320421 107 * To maximize resolution, accuracy, performance, and range, it is recommended to use
mgottscho 0:47acc8320421 108 * getTick() for most purposes. getTime() is mostly for convenience.
mgottscho 0:47acc8320421 109 * @returns the current tick converted into a PreciseTime representation.
mgottscho 0:47acc8320421 110 */
mgottscho 7:78f6ee57d324 111 PreciseTime getTime();
mgottscho 0:47acc8320421 112
mgottscho 0:47acc8320421 113 /**
mgottscho 7:78f6ee57d324 114 * @returns the current tick number. Convert to seconds by multiplying the return value with tickValue().
mgottscho 7:78f6ee57d324 115 * Note that getTick() * tickValue() can easily overflow on faster timers due to the 32-bit upper bound
mgottscho 7:78f6ee57d324 116 * on arithmetic.
mgottscho 0:47acc8320421 117 */
mgottscho 0:47acc8320421 118 virtual uint32_t getTick() = 0;
mgottscho 0:47acc8320421 119
mgottscho 7:78f6ee57d324 120 /**
mgottscho 7:78f6ee57d324 121 * Interrupt service routine for the timer. This should do timer hardware-specific chores before calling the user
mgottscho 7:78f6ee57d324 122 * callback function.
mgottscho 7:78f6ee57d324 123 */
mgottscho 7:78f6ee57d324 124 virtual void __timer_isr() = 0;
mgottscho 7:78f6ee57d324 125
mgottscho 7:78f6ee57d324 126 protected:
mgottscho 7:78f6ee57d324 127 /**
mgottscho 7:78f6ee57d324 128 * Initializes the particular hardware timer.
mgottscho 7:78f6ee57d324 129 */
mgottscho 7:78f6ee57d324 130 virtual void __init_timer() = 0;
mgottscho 7:78f6ee57d324 131
mgottscho 7:78f6ee57d324 132 /**
mgottscho 7:78f6ee57d324 133 * Starts the particular hardware timer.
mgottscho 7:78f6ee57d324 134 */
mgottscho 7:78f6ee57d324 135 virtual void __start_timer() = 0;
mgottscho 7:78f6ee57d324 136
mgottscho 7:78f6ee57d324 137 /**
mgottscho 7:78f6ee57d324 138 * Stop and disable the particular hardware timer.
mgottscho 7:78f6ee57d324 139 */
mgottscho 7:78f6ee57d324 140 virtual void __stop_timer() = 0;
mgottscho 7:78f6ee57d324 141
mgottscho 7:78f6ee57d324 142 bool __valid; //timer can be used
mgottscho 12:cb395e4be69e 143 volatile uint32_t __count; //number of rollovers
mgottscho 7:78f6ee57d324 144 uint32_t __rolloverValue; //ticks per rollover
mgottscho 7:78f6ee57d324 145 bool __periodic; //periodic callbacks
mgottscho 12:cb395e4be69e 146 volatile uint32_t __num_callbacks;
mgottscho 7:78f6ee57d324 147
mgottscho 7:78f6ee57d324 148 FunctionPointer *__user_fptr; //User callback function
mgottscho 7:78f6ee57d324 149
mgottscho 7:78f6ee57d324 150 private:
mgottscho 7:78f6ee57d324 151 bool __enabled; //timer is configured
mgottscho 7:78f6ee57d324 152 bool __running; //timer is running
mgottscho 7:78f6ee57d324 153 uint32_t __maxRolloverTick; //maximum number of ticks before timer hardware rolls over
mgottscho 7:78f6ee57d324 154 float __tickValue; //how many units per tick
mgottscho 7:78f6ee57d324 155 tick_units_t __tickUnits; //tick units
mgottscho 0:47acc8320421 156 };
mgottscho 0:47acc8320421 157
mgottscho 0:47acc8320421 158 #endif