2014-12-02 17:10:06 -07:00
|
|
|
#ifndef UTIL_TIME_H
|
|
|
|
#define UTIL_TIME_H
|
|
|
|
|
2014-12-18 15:45:36 -07:00
|
|
|
#include <util/atomic.h>
|
2019-01-08 14:24:29 -07:00
|
|
|
#include "hardware/AFSK.h"
|
2019-01-27 12:25:11 -07:00
|
|
|
#include "hardware/LED.h"
|
|
|
|
#include "hardware/sdcard/diskio.h"
|
2014-12-02 17:10:06 -07:00
|
|
|
|
|
|
|
#define DIV_ROUND(dividend, divisor) (((dividend) + (divisor) / 2) / (divisor))
|
|
|
|
|
2019-01-12 08:30:26 -07:00
|
|
|
typedef int32_t ticks_t;
|
|
|
|
typedef int32_t mtime_t;
|
|
|
|
volatile ticks_t _clock;
|
2014-12-02 17:10:06 -07:00
|
|
|
|
2019-01-12 08:30:26 -07:00
|
|
|
static inline ticks_t timer_clock(void) {
|
|
|
|
ticks_t result;
|
2014-12-02 17:10:06 -07:00
|
|
|
|
|
|
|
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
|
|
|
result = _clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-12 08:30:26 -07:00
|
|
|
inline ticks_t ms_to_ticks(mtime_t ms) {
|
2014-12-02 17:10:06 -07:00
|
|
|
return ms * DIV_ROUND(CLOCK_TICKS_PER_SEC, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void cpu_relax(void) {
|
|
|
|
// Do nothing!
|
|
|
|
}
|
|
|
|
|
2015-10-02 15:44:24 -06:00
|
|
|
static inline void delay_ms(unsigned long ms) {
|
2014-12-18 15:45:36 -07:00
|
|
|
ticks_t start = timer_clock();
|
|
|
|
unsigned long n_ticks = ms_to_ticks(ms);
|
|
|
|
while (timer_clock() - start < n_ticks) {
|
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 08:30:26 -07:00
|
|
|
#endif
|