cores: Updated for 16 bits timers and ADCs
[simavr] / simavr / cores / sim_tinyx5.h
1 /*
2         sim_tinyx5.h
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5                              Jon Escombe <lists@dresco.co.uk>
6
7         This file is part of simavr.
8
9         simavr is free software: you can redistribute it and/or modify
10         it under the terms of the GNU General Public License as published by
11         the Free Software Foundation, either version 3 of the License, or
12         (at your option) any later version.
13
14         simavr is distributed in the hope that it will be useful,
15         but WITHOUT ANY WARRANTY; without even the implied warranty of
16         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17         GNU General Public License for more details.
18
19         You should have received a copy of the GNU General Public License
20         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #ifndef __SIM_TINYX5_H__
25 #define __SIM_TINYX5_H__
26
27 #include "sim_core_declare.h"
28 #include "avr_eeprom.h"
29 #include "avr_extint.h"
30 #include "avr_ioport.h"
31 #include "avr_adc.h"
32 #include "avr_timer.h"
33
34 void tx5_init(struct avr_t * avr);
35 void tx5_reset(struct avr_t * avr);
36
37 /*
38  * This is a template for all of the tinyx5 devices, hopefully
39  */
40 struct mcu_t {
41         avr_t core;
42         avr_eeprom_t    eeprom;
43         avr_extint_t    extint;
44         avr_ioport_t    portb;
45         avr_adc_t               adc;
46         avr_timer_t     timer0, timer1;
47 };
48
49 #ifdef SIM_CORENAME
50
51 #ifndef SIM_VECTOR_SIZE
52 #error SIM_VECTOR_SIZE is not declared
53 #endif
54 #ifndef SIM_MMCU
55 #error SIM_MMCU is not declared
56 #endif
57
58 struct mcu_t SIM_CORENAME = {
59         .core = {
60                 .mmcu = SIM_MMCU,
61                 DEFAULT_CORE(SIM_VECTOR_SIZE),
62
63                 .init = tx5_init,
64                 .reset = tx5_reset,
65         },
66         AVR_EEPROM_DECLARE(EE_RDY_vect),
67         .extint = {
68                 AVR_EXTINT_TINY_DECLARE(0, 'B', PB2, GIFR),
69         },
70         .portb = {
71                 .name = 'B',  .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
72                 .pcint = {
73                         .enable = AVR_IO_REGBIT(GIMSK, PCIE),
74                         .raised = AVR_IO_REGBIT(GIFR, PCIF),
75                         .vector = PCINT0_vect,
76                 },
77                 .r_pcint = PCMSK,
78         },
79         .adc = {
80                 .r_admux = ADMUX,
81                 .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1),
82                                         AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3),},
83                 .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1), AVR_IO_REGBIT(ADMUX, REFS2), },
84                 .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
85                 .r_adcsra = ADCSRA,
86                 .aden = AVR_IO_REGBIT(ADCSRA, ADEN),
87                 .adsc = AVR_IO_REGBIT(ADCSRA, ADSC),
88                 .adate = AVR_IO_REGBIT(ADCSRA, ADATE),
89                 .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),},
90
91                 .r_adch = ADCH,
92                 .r_adcl = ADCL,
93
94                 .r_adcsrb = ADCSRB,
95                 .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2),},
96                 .bin = AVR_IO_REGBIT(ADCSRB, BIN),
97                 .ipr = AVR_IO_REGBIT(ADCSRA, IPR),
98
99                 .adc = {
100                         .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
101                         .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
102                         .vector = ADC_vect,
103                 },
104         },
105         .timer0 = {
106                 .name = '0',
107                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
108                 .wgm_op = {
109                         [0] = AVR_TIMER_WGM_NORMAL8(),
110                         [2] = AVR_TIMER_WGM_CTC(),
111                         [3] = AVR_TIMER_WGM_FASTPWM(),
112                         [7] = AVR_TIMER_WGM_FASTPWM(),
113                 },
114                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
115                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
116
117                 .r_ocra = OCR0A,
118                 .r_ocrb = OCR0B,
119                 .r_tcnt = TCNT0,
120
121                 .overflow = {
122                         .enable = AVR_IO_REGBIT(TIMSK, TOIE0),
123                         .raised = AVR_IO_REGBIT(TIFR, TOV0),
124                         .vector = TIMER0_OVF_vect,
125                 },
126                 .compa = {
127                         .enable = AVR_IO_REGBIT(TIMSK, OCIE0A),
128                         .raised = AVR_IO_REGBIT(TIFR, OCF0A),
129                         .vector = TIMER0_COMPA_vect,
130                 },
131                 .compb = {
132                         .enable = AVR_IO_REGBIT(TIMSK, OCIE0B),
133                         .raised = AVR_IO_REGBIT(TIFR, OCF0B),
134                         .vector = TIMER0_COMPB_vect,
135                 },
136         },
137         .timer1 = {
138                 .name = '1',
139                 // no wgm bits
140                 .cs = { AVR_IO_REGBIT(TCCR1, CS10), AVR_IO_REGBIT(TCCR1, CS11), AVR_IO_REGBIT(TCCR1, CS12), AVR_IO_REGBIT(TCCR1, CS13) },
141                 .cs_div = { 0, 0, 1 /* 2 */, 2 /* 4 */, 3 /* 8 */, 4 /* 16 */ },
142
143                 .r_ocra = OCR1A,
144                 .r_ocrb = OCR1B,
145                 .r_ocrc = OCR1C,
146                 .r_tcnt = TCNT1,
147
148                 .overflow = {
149                         .enable = AVR_IO_REGBIT(TIMSK, TOIE1),
150                         .raised = AVR_IO_REGBIT(TIFR, TOV1),
151                         .vector = TIMER1_OVF_vect,
152                 },
153                 .compa = {
154                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1A),
155                         .raised = AVR_IO_REGBIT(TIFR, OCF1A),
156                         .vector = TIMER1_COMPA_vect,
157                 },
158                 .compb = {
159                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1B),
160                         .raised = AVR_IO_REGBIT(TIFR, OCF1B),
161                         .vector = TIMER1_COMPB_vect,
162                 },
163         },
164
165
166 };
167 #endif /* SIM_CORENAME */
168
169 #endif /* __SIM_TINYX5_H__ */