diff options
author | William Chang <william@factual.com> | 2019-11-20 22:17:07 -0800 |
---|---|---|
committer | William Chang <william@factual.com> | 2019-11-20 22:17:07 -0800 |
commit | e7f4d56592b3975c38af329e77b4efd9108495e8 (patch) | |
tree | 0a416bccbf70bfdbdb9ffcdb3bf136b47378c014 /tmk_core/common/timer.h | |
parent | 71493b2f9bbd5f3d18373c518fa14ccafcbf48fc (diff) | |
parent | 8416a94ad27b3ff058576f09f35f0704a8b39ff3 (diff) | |
download | firmware-e7f4d56592b3975c38af329e77b4efd9108495e8.tar.gz firmware-e7f4d56592b3975c38af329e77b4efd9108495e8.tar.bz2 firmware-e7f4d56592b3975c38af329e77b4efd9108495e8.zip |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'tmk_core/common/timer.h')
-rw-r--r-- | tmk_core/common/timer.h | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/tmk_core/common/timer.h b/tmk_core/common/timer.h index fe23f87ae..378cf7892 100644 --- a/tmk_core/common/timer.h +++ b/tmk_core/common/timer.h @@ -19,18 +19,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define TIMER_H 1 #include <stdint.h> +#include <stdbool.h> #if defined(__AVR__) -#include "avr/timer_avr.h" +# include "avr/timer_avr.h" #endif - -#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a)) -#define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX) -#define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX) -#define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX) -#define TIMER_DIFF_RAW(a, b) TIMER_DIFF_8(a, b) - +#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a)) +#define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX) +#define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX) +#define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX) +#define TIMER_DIFF_RAW(a, b) TIMER_DIFF_8(a, b) #ifdef __cplusplus extern "C" { @@ -38,14 +37,18 @@ extern "C" { extern volatile uint32_t timer_count; - -void timer_init(void); -void timer_clear(void); +void timer_init(void); +void timer_clear(void); uint16_t timer_read(void); uint32_t timer_read32(void); uint16_t timer_elapsed(uint16_t last); uint32_t timer_elapsed32(uint32_t last); +// Utility functions to check if a future time has expired & autmatically handle time wrapping if checked / reset frequently (half of max value) +inline bool timer_expired(uint16_t current, uint16_t future) { return (uint16_t)(current - future) < 0x8000; } + +inline bool timer_expired32(uint32_t current, uint32_t future) { return (uint32_t)(current - future) < 0x80000000; } + #ifdef __cplusplus } #endif |