aboutsummaryrefslogtreecommitdiff
path: root/timer.h
blob: 192c15d5561206ffaecfc712e1d0386f5f1b55cf (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>

#include "mcu/mcu_def.h"

#ifndef _IOE_TIMER_H_
#define _IOE_TIMER_H_
#ifdef CONFIG_IOE_TIMERS

enum timerDivider {
    TIMER_DIVIDER_1,
    TIMER_DIVIDER_8,
    TIMER_DIVIDER_64,
    TIMER_DIVIDER_256,
    TIMER_DIVIDER_1024
};

static inline void timer_init(uint8_t timer, enum timerDivider div) {
    switch (timer) {
#ifdef COUNTER0_PWM
    case COUNTER0_PWM:
        // Null counting register
        TCNT0 = 0;
        // Set interrupt flag
        TIMSK0 = _BV(TOIE0);
        // Set division and start counting
        switch (div) {
            case TIMER_DIVIDER_1:
                TCCR0B = _BV(CS00);
                break;
            case TIMER_DIVIDER_8:
                TCCR0B = _BV(CS01);
                break;
            case TIMER_DIVIDER_64:
                TCCR0B = _BV(CS01) | _BV(CS00);
                break;
            case TIMER_DIVIDER_256:
                TCCR0B = _BV(CS02);
                break;
            case TIMER_DIVIDER_1024:
                TCCR0B = _BV(CS02) | _BV(CS01);
                break;
            default:
                return;
        }
        break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
    case COUNTER1_16PWM:
        break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
    case COUNTER2_PWM_ASYNC:
        break;
#endif /* COUNTER2_PWM_ASYNC */
    }
}

static inline void timer_disable(uint8_t timer) {
    switch (timer) {
#ifdef COUNTER0_PWM
    case COUNTER0_PWM:
        TCCR0B = 0;
        break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
    case COUNTER1_16PWM:
        break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
    case COUNTER2_PWM_ASYNC:
        break;
#endif /* COUNTER2_PWM_ASYNC */
    }
}

static inline void timer_reset(uint8_t timer) {
    switch (timer) {
#ifdef COUNTER0_PWM
    case COUNTER0_PWM:
        TCNT0 = 0;
        break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
    case COUNTER1_16PWM:
        break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
    case COUNTER2_PWM_ASYNC:
        break;
#endif /* COUNTER2_PWM_ASYNC */
    }
}

static inline uint16_t timer_value(uint8_t timer) {
    switch (timer) {
#ifdef COUNTER0_PWM
    case COUNTER0_PWM:
        return TCNT0;
        break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
    case COUNTER1_16PWM:
        break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
    case COUNTER2_PWM_ASYNC:
        break;
#endif /* COUNTER2_PWM_ASYNC */
    default:
        return 0;
    }
}

#ifdef COUNTER0_PWM
void (*timer_0_pwm_overflow) (void);
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
void (*timer_1_16pwm_overflow) (void);
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
void (*timer_2_pwm_overflow) (void);
#endif /* COUNTER2_PWM_ASYNC */

static inline void timer_sethook(uint8_t timer, void (*fnc) (void)) {
    switch (timer) {
#ifdef COUNTER0_PWM
    case COUNTER0_PWM:
        timer_0_pwm_overflow = fnc;
        break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
    case COUNTER1_16PWM:
        timer_1_16pwm_overflow = fnc;
        break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
    case COUNTER2_PWM_ASYNC:
        timer_2_pwm_overflow = fnc;
        break;
#endif /* COUNTER2_PWM_ASYNC */
    }
}

#endif /* CONFIG_IOE_TIMERS */
#endif /* _IOE_TIMER_H_ */