comments: What don't you typo the comments, too ?
[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_pwm,
41         avr_timer_wgm_fast_pwm,
42 };
43 enum {
44         avr_timer_wgm_reg_constant = 0,
45         avr_timer_wgm_reg_ocra,
46         avr_timer_wgm_reg_icr,
47 };
48 typedef struct avr_timer_wgm_t {
49         uint32_t top: 8, bottom: 8, size : 8, kind : 8;
50 } avr_timer_wgm_t;
51
52 #define AVR_TIMER_WGM_NORMAL8() { .kind = avr_timer_wgm_normal, .size=8 }
53 #define AVR_TIMER_WGM_NORMAL16() { .kind = avr_timer_wgm_normal, .size=16 }
54 #define AVR_TIMER_WGM_CTC() { .kind = avr_timer_wgm_ctc, .top = avr_timer_wgm_reg_ocra }
55 #define AVR_TIMER_WGM_ICCTC() { .kind = avr_timer_wgm_ctc, .top = avr_timer_wgm_reg_icr }
56 #define AVR_TIMER_WGM_FASTPWM8() { .kind = avr_timer_wgm_fast_pwm, .size=8 }
57 #define AVR_TIMER_WGM_FASTPWM9() { .kind = avr_timer_wgm_fast_pwm, .size=9 }
58 #define AVR_TIMER_WGM_FASTPWM10() { .kind = avr_timer_wgm_fast_pwm, .size=10 }
59 #define AVR_TIMER_WGM_OCPWM() { .kind = avr_timer_wgm_pwm, .top = avr_timer_wgm_reg_ocra }
60 #define AVR_TIMER_WGM_ICPWM() { .kind = avr_timer_wgm_pwm, .top = avr_timer_wgm_reg_icr }
61
62 typedef struct avr_timer_t {
63         avr_io_t        io;
64         char name;
65         avr_regbit_t    disabled;       // bit in the PRR
66
67         avr_io_addr_t   r_ocra, r_ocrb, r_ocrc, r_tcnt, r_icr;
68         avr_io_addr_t   r_ocrah, r_ocrbh, r_tcnth, r_icrh;
69         
70         avr_regbit_t    wgm[4];
71         avr_timer_wgm_t wgm_op[16];
72
73         avr_regbit_t    cs[4];
74         uint8_t                 cs_div[16];
75         avr_regbit_t    as2;            // asynchronous clock 32khz
76
77         avr_int_vector_t compa; // comparator A
78         avr_int_vector_t compb; // comparator A
79         avr_int_vector_t overflow;      // overflow
80         avr_int_vector_t icr;   // input capture
81
82         avr_timer_wgm_t mode;
83         uint64_t                compa_cycles;
84         uint64_t                compb_cycles;
85         uint64_t                tov_cycles;
86         uint64_t                tov_base;       // we we last were called
87         uint16_t                tov_top;        // current top value to calculate tnct
88 } avr_timer_t;
89
90 void avr_timer_init(avr_t * avr, avr_timer_t * port);
91
92 #endif /* AVR_TIMER_H_ */