4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
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.
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.
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/>.
28 TIMER_IRQ_OUT_PWM0 = 0,
33 // Get the internal IRQ corresponding to the INT
34 #define AVR_IOCTL_TIMER_GETIRQ(_name) AVR_IOCTL_DEF('t','m','r',(_name))
37 avr_timer_wgm_none = 0, // invalid mode
41 avr_timer_wgm_fast_pwm,
44 avr_timer_wgm_reg_constant = 0,
45 avr_timer_wgm_reg_ocra,
46 avr_timer_wgm_reg_icr,
48 typedef struct avr_timer_wgm_t {
49 uint32_t top: 8, bottom: 8, size : 8, kind : 8;
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 }
62 typedef struct avr_timer_t {
65 avr_regbit_t disabled; // bit in the PRR
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;
71 avr_timer_wgm_t wgm_op[16];
75 avr_regbit_t as2; // asynchronous clock 32khz
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
83 uint64_t compa_cycles;
84 uint64_t compb_cycles;
86 uint64_t tov_base; // we we last were called
87 uint16_t tov_top; // current top value to calculate tnct
90 void avr_timer_init(avr_t * avr, avr_timer_t * port);
92 #endif /* AVR_TIMER_H_ */