-
Notifications
You must be signed in to change notification settings - Fork 0
/
_timers.h
66 lines (55 loc) · 2.18 KB
/
_timers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef _COMMON__TIMERS_H
#define _COMMON__TIMERS_H
#include "utils.h"
/* Bits of timers: value is power of 2
*/
typedef enum timer_bits_t {
TBITS8 = 256,
TBITS16 = 65536,
} timer_bits_t;
struct timerc_t;
typedef void (*timer_handler_t)(struct timerc_t *tc);
typedef struct timerc_t {
volatile timer_handler_t alarm_handler;
volatile unsigned long nover; // number of overflow
unsigned long bits; // number of bits
volatile unsigned long cn; // start from this count value
volatile unsigned long cycles; // cycles number
volatile int prescaler; // prescaler (1, 8, 32... - depends on arch. and timer)
volatile int cycles_prescaler; // prescaler (usually 1024) for total counter cycles
int *prescalers; // array of timer-counter prescalers
int nprescalers; // length of prescalers' array
} timerc_t;
#define __TIMERC_INITIALIZER(BITS, PRESCALERS, NPRESCALERS) \
{NULL, 0, BITS, 0, 0, 0, 0, PRESCALERS, NPRESCALERS}
#define __TIMER_IRQ(T) do { \
if (_timers[T].cycles_prescaler) { \
/* needs total cycles */ \
if (_timers[T].cycles) { \
_timers[T].cycles--; \
} \
else { \
_timers[T].cycles_prescaler = 0; \
/* switch to new prescaler */ \
_alarm_regsetup(T, _timers[T].cn, _timers[T].prescaler); \
} \
goto __anyway; \
} \
if (_timers[T].alarm_handler) { \
alarm(T, 0); \
_timers[T].alarm_handler(_timers); \
} \
__anyway: \
_timers[T].nover++; } while (0)
#ifdef CLOCKS_PER_SEC
# error Another clock is used
#endif
#define MSECS2CLOCKS(ms) (((CLOCKS_PER_SEC)*(ms))/1000)
#define USECS2CLOCKS(ms) (((CLOCKS_PER_SEC)*(ms))/1000000)
int alarm_action(int timer, timer_handler_t alarm_handler);
int _alarm(int timer, unsigned long usec);
int alarm(int timer, unsigned long usec); // the same but with cli()/sei() wrap
unsigned long tclock(int timer);
unsigned long millis(void); // msec from last call
unsigned long micros(void); // usec from last call
#endif