timer: Masssive timer update. 8 & 16 bits
[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 #include "sim_avr.h"
26
27 enum {
28         TIMER_IRQ_OUT_PWM0 = 0,
29         TIMER_IRQ_OUT_PWM1,
30         TIMER_IRQ_COUNT
31 };
32
33 // Get the internal IRQ corresponding to the INT
34 #define AVR_IOCTL_TIMER_GETIRQ(_name) AVR_IOCTL_DEF('t','m','r',(_name))
35
36 enum {
37         avr_timer_wgm_none = 0, // invalid mode
38         avr_timer_wgm_normal,
39         avr_timer_wgm_ctc,
40         avr_timer_wgm_fast_pwm,
41 };
42 typedef struct avr_timer_wgm_t {
43         uint32_t top: 8, bottom: 8, size : 8, kind : 8;
44 } avr_timer_wgm_t;
45
46 #define AVR_TIMER_WGM_NORMAL8() { .kind = avr_timer_wgm_normal, .size=8 }
47 #define AVR_TIMER_WGM_NORMAL16() { .kind = avr_timer_wgm_normal, .size=16 }
48 #define AVR_TIMER_WGM_CTC() { .kind = avr_timer_wgm_ctc }
49 #define AVR_TIMER_WGM_FASTPWM() { .kind = avr_timer_wgm_fast_pwm }
50
51 typedef struct avr_timer_t {
52         avr_io_t        io;
53         char name;
54         avr_regbit_t    disabled;       // bit in the PRR
55
56         avr_io_addr_t   r_ocra, r_ocrb, r_ocrc, r_tcnt, r_icr;
57         avr_io_addr_t   r_ocrah, r_ocrbh, r_tcnth, r_icrh;
58         
59         avr_regbit_t    wgm[4];
60         avr_timer_wgm_t wgm_op[16];
61
62         avr_regbit_t    cs[4];
63         uint8_t                 cs_div[16];
64         avr_regbit_t    as2;            // asynchronous clock 32khz
65
66         avr_int_vector_t compa; // comparator A
67         avr_int_vector_t compb; // comparator A
68         avr_int_vector_t overflow;      // overflow
69         avr_int_vector_t icr;   // input capture
70
71         uint64_t                compa_cycles;
72         uint64_t                compb_cycles;
73         uint64_t                tov_cycles;
74 } avr_timer_t;
75
76 void avr_timer_init(avr_t * avr, avr_timer_t * port);
77
78 #endif /* AVR_TIMER_H_ */