Mark Gottscho / HardwareTimersLib

Fork of HardwareTimersLib by Mark Gottscho

Committer:
mgottscho
Date:
Tue Mar 11 00:52:13 2014 +0000
Revision:
7:78f6ee57d324
Parent:
2:5056ec8c52e8
Child:
12:cb395e4be69e
MAJOR updates and refactoring of code to improve code reuse and hardware encapsulation. Now all three LPTMR, TPM, and PIT timers inherit from the same HardwareTimer base abstract class, which provides as much functionality as possible.

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 7:78f6ee57d324 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 7:78f6ee57d324 93 * If false, the user callback function is only called the first time.
mgottscho 7:78f6ee57d324 94 */
mgottscho 7:78f6ee57d324 95 void start(uint32_t callback_tick_count, bool periodic);
mgottscho 7:78f6ee57d324 96
mgottscho 7:78f6ee57d324 97 /**
mgottscho 7:78f6ee57d324 98 * @returns the maximum value of the user-settable callback tick count (via startTimer()).
mgottscho 7:78f6ee57d324 99 * Some timers may support full 32-bit tick counts, while others may be less.
mgottscho 7:78f6ee57d324 100 */
mgottscho 7:78f6ee57d324 101 uint32_t getMaxCallbackTickCount();
mgottscho 0:47acc8320421 102
mgottscho 0:47acc8320421 103 /**
mgottscho 0:47acc8320421 104 * Gets the timer value in a nice form.
mgottscho 0:47acc8320421 105 * Note that in general, the timer may overflow, leading to saturated values obtained from getTime().
mgottscho 0:47acc8320421 106 * To maximize resolution, accuracy, performance, and range, it is recommended to use
mgottscho 0:47acc8320421 107 * getTick() for most purposes. getTime() is mostly for convenience.
mgottscho 0:47acc8320421 108 * @returns the current tick converted into a PreciseTime representation.
mgottscho 0:47acc8320421 109 */
mgottscho 7:78f6ee57d324 110 PreciseTime getTime();
mgottscho 0:47acc8320421 111
mgottscho 0:47acc8320421 112 /**
mgottscho 7:78f6ee57d324 113 * @returns the current tick number. Convert to seconds by multiplying the return value with tickValue().
mgottscho 7:78f6ee57d324 114 * Note that getTick() * tickValue() can easily overflow on faster timers due to the 32-bit upper bound
mgottscho 7:78f6ee57d324 115 * on arithmetic.
mgottscho 0:47acc8320421 116 */
mgottscho 0:47acc8320421 117 virtual uint32_t getTick() = 0;
mgottscho 0:47acc8320421 118
mgottscho 7:78f6ee57d324 119 /**
mgottscho 7:78f6ee57d324 120 * Interrupt service routine for the timer. This should do timer hardware-specific chores before calling the user
mgottscho 7:78f6ee57d324 121 * callback function.
mgottscho 7:78f6ee57d324 122 */
mgottscho 7:78f6ee57d324 123 virtual void __timer_isr() = 0;
mgottscho 7:78f6ee57d324 124
mgottscho 7:78f6ee57d324 125 protected:
mgottscho 7:78f6ee57d324 126 /**
mgottscho 7:78f6ee57d324 127 * Initializes the particular hardware timer.
mgottscho 7:78f6ee57d324 128 */
mgottscho 7:78f6ee57d324 129 virtual void __init_timer() = 0;
mgottscho 7:78f6ee57d324 130
mgottscho 7:78f6ee57d324 131 /**
mgottscho 7:78f6ee57d324 132 * Starts the particular hardware timer.
mgottscho 7:78f6ee57d324 133 */
mgottscho 7:78f6ee57d324 134 virtual void __start_timer() = 0;
mgottscho 7:78f6ee57d324 135
mgottscho 7:78f6ee57d324 136 /**
mgottscho 7:78f6ee57d324 137 * Stop and disable the particular hardware timer.
mgottscho 7:78f6ee57d324 138 */
mgottscho 7:78f6ee57d324 139 virtual void __stop_timer() = 0;
mgottscho 7:78f6ee57d324 140
mgottscho 7:78f6ee57d324 141 bool __valid; //timer can be used
mgottscho 7:78f6ee57d324 142 uint32_t __count; //number of rollovers
mgottscho 7:78f6ee57d324 143 uint32_t __rolloverValue; //ticks per rollover
mgottscho 7:78f6ee57d324 144 bool __periodic; //periodic callbacks
mgottscho 7:78f6ee57d324 145 bool __call_user_function; //flag
mgottscho 7:78f6ee57d324 146
mgottscho 7:78f6ee57d324 147 FunctionPointer *__user_fptr; //User callback function
mgottscho 7:78f6ee57d324 148
mgottscho 7:78f6ee57d324 149 private:
mgottscho 7:78f6ee57d324 150 bool __enabled; //timer is configured
mgottscho 7:78f6ee57d324 151 bool __running; //timer is running
mgottscho 7:78f6ee57d324 152 uint32_t __maxRolloverTick; //maximum number of ticks before timer hardware rolls over
mgottscho 7:78f6ee57d324 153 float __tickValue; //how many units per tick
mgottscho 7:78f6ee57d324 154 tick_units_t __tickUnits; //tick units
mgottscho 0:47acc8320421 155 };
mgottscho 0:47acc8320421 156
mgottscho 0:47acc8320421 157 #endif