Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HardwareTimersLib by
Diff: HardwareTimer.h
- Revision:
- 7:78f6ee57d324
- Parent:
- 2:5056ec8c52e8
- Child:
- 12:cb395e4be69e
--- a/HardwareTimer.h Mon Mar 10 20:35:21 2014 +0000
+++ b/HardwareTimer.h Tue Mar 11 00:52:13 2014 +0000
@@ -16,38 +16,89 @@
*/
class HardwareTimer {
public:
+ typedef enum {
+ ns,
+ us,
+ ms,
+ s,
+ m,
+ h
+ } tick_units_t;
+
/**
* Constructs a new HardwareTimer.
- * @param tickValue the amount of time corresponding to each timer tick. Units are arbitrary. Depends on specific timer clock rate.
+ * @param valid if false, none of the timer functions can be used. This is intended to enforce only one object
+ * managing a unique hardware timer.
+ * @param maxRolloverTick maximum number of ticks in the hardware unit before it rolls over.
+ * @param tickValue the amount of time corresponding to each timer tick, in units given by tickUnits.
+ * @param tickUnits units for tickValue
*/
- HardwareTimer(float tickValue);
-
- ~HardwareTimer();
+ HardwareTimer(uint32_t maxRolloverTick, float tickValue, tick_units_t tickUnits);
/**
- * @returns true if this timer is valid. If false, no timing mechanisms are functional.
+ * Destructs the HardwareTimer.
+ */
+ virtual ~HardwareTimer();
+
+ /**
+ * @returns true if this timer is valid. If false, the timer cannot be used.
*/
bool valid();
/**
+ * @returns true if the timer is ready to start.
+ */
+ bool enabled();
+
+ /**
* @returns true if the timer is running.
*/
- bool isEnabled();
+ bool running();
/**
- * @returns the amount of time corresponding to each timer tick. Units are arbitrary. Depends on specific timer clock rate.
+ * @returns the amount of time corresponding to each timer tick in units given by tickUnits().
*/
float tickValue();
/**
- * Enables the timer.
+ * @returns time in seconds corresponding to each tick
+ */
+ float tickUnits();
+
+ /**
+ * Enables the timer with a user-specified callback function that is called each time the timer expires.
+ * @param fptr the user callback function
*/
- virtual void enableTimer() = 0;
+ void enable(void (*fptr)(void));
+
+ /**
+ * Enables the timer with a user-specified callback function that is called each time the timer expires.
+ * @param tptr the object
+ * @param mptr method to call on the object
+ */
+ template <typename T> void enable(T *tptr, void (T::*mptr)(void));
/**
- * Disables the timer.
+ * Stops and disables the timer. No user function callbacks will be made, and the tick value stops increasing.
*/
- virtual void disableTimer() = 0;
+ void disable();
+
+ /**
+ * Starts the timer. If valid() or enabled() are false, then this method does nothing. Otherwise, the timer
+ * begins ticking. The user callback function specified in enableTimer() is called each time the timer rolls over.
+ * @param callback_tick_count the modulo tick value for when the timer calls the user callback function.
+ * Note that the timer counts up. Note that some timers may not support the full 32-bit range. Use getMaxCallbackTickCount()
+ * To check the maximum allowed value. If callback_tick_count is greater than that value, this method will have no effect.
+ * @param periodic if true, the timer will call the user function every time the internal tick modulo callback_tick_count is reached.
+ * If false, the user callback function is only called the first time.
+ */
+ void start(uint32_t callback_tick_count, bool periodic);
+
+ /**
+ * @returns the maximum value of the user-settable callback tick count (via startTimer()).
+ * Some timers may support full 32-bit tick counts, while others may be less.
+ */
+ uint32_t getMaxCallbackTickCount();
/**
* Gets the timer value in a nice form.
@@ -56,17 +107,51 @@
* getTick() for most purposes. getTime() is mostly for convenience.
* @returns the current tick converted into a PreciseTime representation.
*/
- virtual PreciseTime getTime() = 0;
+ PreciseTime getTime();
/**
- * @returns the current tick number. Convert to real time units by multiplying by tickValue(), which may have arbitrary units.
+ * @returns the current tick number. Convert to seconds by multiplying the return value with tickValue().
+ * Note that getTick() * tickValue() can easily overflow on faster timers due to the 32-bit upper bound
+ * on arithmetic.
*/
virtual uint32_t getTick() = 0;
- protected:
- bool __valid;
- bool __enabled;
- float __tickValue; //how many <units> per tick
+ /**
+ * Interrupt service routine for the timer. This should do timer hardware-specific chores before calling the user
+ * callback function.
+ */
+ virtual void __timer_isr() = 0;
+
+ protected:
+ /**
+ * Initializes the particular hardware timer.
+ */
+ virtual void __init_timer() = 0;
+
+ /**
+ * Starts the particular hardware timer.
+ */
+ virtual void __start_timer() = 0;
+
+ /**
+ * Stop and disable the particular hardware timer.
+ */
+ virtual void __stop_timer() = 0;
+
+ bool __valid; //timer can be used
+ uint32_t __count; //number of rollovers
+ uint32_t __rolloverValue; //ticks per rollover
+ bool __periodic; //periodic callbacks
+ bool __call_user_function; //flag
+
+ FunctionPointer *__user_fptr; //User callback function
+
+ private:
+ bool __enabled; //timer is configured
+ bool __running; //timer is running
+ uint32_t __maxRolloverTick; //maximum number of ticks before timer hardware rolls over
+ float __tickValue; //how many units per tick
+ tick_units_t __tickUnits; //tick units
};
#endif
\ No newline at end of file
