misc: Point to correct simavr include dirs
[simavr] / simavr / sim / avr_timer.h
1 /*
2         avr_timer.h
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #ifndef __AVR_TIMER_H__
23 #define __AVR_TIMER_H__
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #include "sim_avr.h"
30
31 enum {
32         AVR_TIMER_COMPA = 0,
33         AVR_TIMER_COMPB,
34         AVR_TIMER_COMPC,
35
36         AVR_TIMER_COMP_COUNT
37 };
38
39 enum {
40         TIMER_IRQ_OUT_PWM0 = 0,
41         TIMER_IRQ_OUT_PWM1,
42         TIMER_IRQ_OUT_COMP,     // comparator pins output IRQ
43
44         TIMER_IRQ_COUNT = TIMER_IRQ_OUT_COMP + AVR_TIMER_COMP_COUNT
45 };
46
47 // Get the internal IRQ corresponding to the INT
48 #define AVR_IOCTL_TIMER_GETIRQ(_name) AVR_IOCTL_DEF('t','m','r',(_name))
49
50 // Waveform generation modes
51 enum {
52         avr_timer_wgm_none = 0, // invalid mode
53         avr_timer_wgm_normal,
54         avr_timer_wgm_ctc,
55         avr_timer_wgm_pwm,
56         avr_timer_wgm_fast_pwm,
57         avr_timer_wgm_fc_pwm,
58 };
59
60 // Compare output modes
61 enum {
62         avr_timer_com_normal = 0,// Normal mode, OCnx disconnected
63         avr_timer_com_toggle,   // Toggle OCnx on compare match
64         avr_timer_com_clear,    // clear OCnx on compare match
65         avr_timer_com_set,      // set OCnx on compare match
66         
67 };
68
69 enum {
70         avr_timer_wgm_reg_constant = 0,
71         avr_timer_wgm_reg_ocra,
72         avr_timer_wgm_reg_icr,
73 };
74 typedef struct avr_timer_wgm_t {
75         uint32_t top: 8, bottom: 8, size : 8, kind : 8;
76 } avr_timer_wgm_t;
77
78 #define AVR_TIMER_WGM_NORMAL8() { .kind = avr_timer_wgm_normal, .size=8 }
79 #define AVR_TIMER_WGM_NORMAL16() { .kind = avr_timer_wgm_normal, .size=16 }
80 #define AVR_TIMER_WGM_CTC() { .kind = avr_timer_wgm_ctc, .top = avr_timer_wgm_reg_ocra }
81 #define AVR_TIMER_WGM_ICCTC() { .kind = avr_timer_wgm_ctc, .top = avr_timer_wgm_reg_icr }
82 #define AVR_TIMER_WGM_FASTPWM8() { .kind = avr_timer_wgm_fast_pwm, .size=8 }
83 #define AVR_TIMER_WGM_FASTPWM9() { .kind = avr_timer_wgm_fast_pwm, .size=9 }
84 #define AVR_TIMER_WGM_FASTPWM10() { .kind = avr_timer_wgm_fast_pwm, .size=10 }
85 #define AVR_TIMER_WGM_FCPWM8() { .kind = avr_timer_wgm_fc_pwm, .size=8 }
86 #define AVR_TIMER_WGM_FCPWM9() { .kind = avr_timer_wgm_fc_pwm, .size=9 }
87 #define AVR_TIMER_WGM_FCPWM10() { .kind = avr_timer_wgm_fc_pwm, .size=10 }
88 #define AVR_TIMER_WGM_OCPWM() { .kind = avr_timer_wgm_pwm, .top = avr_timer_wgm_reg_ocra }
89 #define AVR_TIMER_WGM_ICPWM() { .kind = avr_timer_wgm_pwm, .top = avr_timer_wgm_reg_icr }
90
91
92 typedef struct avr_timer_t {
93         avr_io_t        io;
94         char name;
95
96         avr_regbit_t    disabled;       // bit in the PRR
97
98         avr_io_addr_t   r_tcnt, r_icr;
99         avr_io_addr_t   r_tcnth, r_icrh;
100
101         avr_regbit_t    wgm[4];
102         avr_timer_wgm_t wgm_op[16];
103
104         avr_regbit_t    cs[4];
105         uint8_t                 cs_div[16];
106         avr_regbit_t    as2;            // asynchronous clock 32khz
107         avr_regbit_t    icp;            // input capture pin, to link IRQs
108         avr_regbit_t    ices;           // input capture edge select
109
110         struct {
111                 avr_int_vector_t        interrupt;              // interrupt vector
112                 avr_io_addr_t           r_ocr;                  // comparator register low byte
113                 avr_io_addr_t           r_ocrh;                 // comparator register hi byte
114                 avr_regbit_t            com;                    // comparator output mode registers
115                 avr_regbit_t            com_pin;                // where comparator output is connected
116                 uint64_t                        comp_cycles;
117         } comp[AVR_TIMER_COMP_COUNT];
118
119         avr_int_vector_t overflow;      // overflow
120         avr_int_vector_t icr;   // input capture
121
122         avr_timer_wgm_t mode;
123         uint64_t                tov_cycles;
124         uint64_t                tov_base;       // when we last were called
125         uint16_t                tov_top;        // current top value to calculate tnct
126 } avr_timer_t;
127
128 void avr_timer_init(avr_t * avr, avr_timer_t * port);
129
130 #ifdef __cplusplus
131 };
132 #endif
133
134 #endif /*__AVR_TIMER_H__*/