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/>.
22 #ifndef __SIM_MEGAX_H__
23 #define __SIM_MEGAX_H__
25 #include "sim_core_declare.h"
26 #include "avr_eeprom.h"
27 #include "avr_flash.h"
28 #include "avr_watchdog.h"
29 #include "avr_extint.h"
30 #include "avr_ioport.h"
33 #include "avr_timer.h"
37 void mx_init(struct avr_t * avr);
38 void mx_reset(struct avr_t * avr);
41 * This is a template for all of the 8/32/64 devices, hopefuly
47 avr_watchdog_t watchdog;
49 avr_ioport_t portb, portc, portd;
52 avr_timer_t timer0,timer1,timer2;
59 #ifndef SIM_VECTOR_SIZE
60 #error SIM_VECTOR_SIZE is not declared
63 #error SIM_MMCU is not declared
67 #define EFUSE_DEFAULT 0xff
73 const struct mcu_t SIM_CORENAME = {
76 DEFAULT_CORE(SIM_VECTOR_SIZE),
81 AVR_EEPROM_DECLARE_NOEEPM(EE_RDY_vect),
82 AVR_SELFPROG_DECLARE(SPMCR, SPMEN, SPM_RDY_vect),
83 AVR_WATCHDOG_DECLARE_128(WDTCR, _VECTOR(0)),
85 AVR_EXTINT_DECLARE(0, 'D', PD2),
86 AVR_EXTINT_DECLARE(1, 'D', PD3),
89 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
92 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
95 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
99 // no PRUSART .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
103 .txen = AVR_IO_REGBIT(UCSRB, TXEN),
104 .rxen = AVR_IO_REGBIT(UCSRB, RXEN),
105 .ucsz = AVR_IO_REGBITS(UCSRC, UCSZ0, 0x3), // 2 bits
106 .ucsz2 = AVR_IO_REGBIT(UCSRB, UCSZ2), // 1 bits
114 .enable = AVR_IO_REGBIT(UCSRB, RXCIE),
115 .raised = AVR_IO_REGBIT(UCSRA, RXC),
116 .vector = USART_RXC_vect,
119 .enable = AVR_IO_REGBIT(UCSRB, TXCIE),
120 .raised = AVR_IO_REGBIT(UCSRA, TXC),
121 .vector = USART_TXC_vect,
124 .enable = AVR_IO_REGBIT(UCSRB, UDRIE),
125 .raised = AVR_IO_REGBIT(UCSRA, UDRE),
126 .vector = USART_UDRE_vect,
131 .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1),
132 AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3),},
133 .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1)},
134 .ref_values = { [1] = ADC_VREF_AVCC, [3] = ADC_VREF_V256 },
136 .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
138 .aden = AVR_IO_REGBIT(ADCSRA, ADEN),
139 .adsc = AVR_IO_REGBIT(ADCSRA, ADSC),
140 // no ADATE .adate = AVR_IO_REGBIT(ADCSRA, ADATE),
141 .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),},
146 //.r_adcsrb = ADCSRB,
147 // .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2),},
150 [0] = AVR_ADC_SINGLE(0), [1] = AVR_ADC_SINGLE(1),
151 [2] = AVR_ADC_SINGLE(2), [3] = AVR_ADC_SINGLE(3),
152 [4] = AVR_ADC_SINGLE(4), [5] = AVR_ADC_SINGLE(5),
153 [6] = AVR_ADC_SINGLE(6), [7] = AVR_ADC_SINGLE(7),
155 [14] = AVR_ADC_REF(1300), // 1.30V
156 [15] = AVR_ADC_REF(0), // GND
160 .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
161 .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
167 .cs = { AVR_IO_REGBIT(TCCR0, CS00), AVR_IO_REGBIT(TCCR0, CS01), AVR_IO_REGBIT(TCCR0, CS02) },
168 // .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
169 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */},
174 .enable = AVR_IO_REGBIT(TIMSK, TOIE0),
175 .raised = AVR_IO_REGBIT(TIFR, TOV0),
176 .vector = TIMER0_OVF_vect,
181 .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
182 AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
184 [0] = AVR_TIMER_WGM_NORMAL16(),
185 // TODO: 1 PWM phase correct 8bit
186 // 2 PWM phase correct 9bit
187 // 3 PWM phase correct 10bit
188 [4] = AVR_TIMER_WGM_CTC(),
189 [5] = AVR_TIMER_WGM_FASTPWM8(),
190 [6] = AVR_TIMER_WGM_FASTPWM9(),
191 [7] = AVR_TIMER_WGM_FASTPWM10(),
192 // TODO: 8, 9 PWM phase and freq correct ICR & 10, 11
193 [12] = AVR_TIMER_WGM_ICCTC(),
194 [14] = AVR_TIMER_WGM_ICPWM(),
195 [15] = AVR_TIMER_WGM_OCPWM(),
197 .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
198 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* TODO: 2 External clocks */},
205 .ices = AVR_IO_REGBIT(TCCR1B, ICES1),
206 .icp = AVR_IO_REGBIT(PORTD, 4),
209 .enable = AVR_IO_REGBIT(TIMSK, TOIE1),
210 .raised = AVR_IO_REGBIT(TIFR, TOV1),
211 .vector = TIMER1_OVF_vect,
214 .enable = AVR_IO_REGBIT(TIMSK, TICIE1),
215 .raised = AVR_IO_REGBIT(TIFR, ICF1),
216 .vector = TIMER1_CAPT_vect,
219 [AVR_TIMER_COMPA] = {
221 .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it
222 .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3),
223 .com_pin = AVR_IO_REGBIT(PORTB, PB5),
225 .enable = AVR_IO_REGBIT(TIMSK, OCIE1A),
226 .raised = AVR_IO_REGBIT(TIFR, OCF1A),
227 .vector = TIMER1_COMPA_vect,
230 [AVR_TIMER_COMPB] = {
233 .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
234 .com_pin = AVR_IO_REGBIT(PORTB, PB6),
236 .enable = AVR_IO_REGBIT(TIMSK, OCIE1B),
237 .raised = AVR_IO_REGBIT(TIFR, OCF1B),
238 .vector = TIMER1_COMPB_vect,
246 .wgm = { AVR_IO_REGBIT(TCCR2, WGM20), AVR_IO_REGBIT(TCCR2, WGM21) },
248 [0] = AVR_TIMER_WGM_NORMAL8(),
249 // TODO 1 pwm phase correct
250 [2] = AVR_TIMER_WGM_CTC(),
251 [3] = AVR_TIMER_WGM_FASTPWM8(),
253 .cs = { AVR_IO_REGBIT(TCCR2, CS20), AVR_IO_REGBIT(TCCR2, CS21), AVR_IO_REGBIT(TCCR2, CS22) },
254 .cs_div = { 0, 0, 3 /* 8 */, 4 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ /* TODO external clock */ },
258 // asynchronous timer source bit.. if set, use 32khz frequency
259 .as2 = AVR_IO_REGBIT(ASSR, AS2),
262 .enable = AVR_IO_REGBIT(TIMSK, TOIE2),
263 .raised = AVR_IO_REGBIT(TIFR, TOV2),
264 .vector = TIMER2_OVF_vect,
267 [AVR_TIMER_COMPA] = {
269 .com = AVR_IO_REGBITS(TCCR2, COM20, 0x3),
270 .com_pin = AVR_IO_REGBIT(PORTB, PB7), // same as timer1C
272 .enable = AVR_IO_REGBIT(TIMSK, OCIE2),
273 .raised = AVR_IO_REGBIT(TIFR, OCF2),
274 .vector = TIMER2_COMP_vect,
285 .spe = AVR_IO_REGBIT(SPCR, SPE),
286 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
288 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
290 .enable = AVR_IO_REGBIT(SPCR, SPIE),
291 .raised = AVR_IO_REGBIT(SPSR, SPIF),
292 .vector = SPI_STC_vect,
303 // no .r_twamr = TWAMR,
305 .twen = AVR_IO_REGBIT(TWCR, TWEN),
306 .twea = AVR_IO_REGBIT(TWCR, TWEA),
307 .twsta = AVR_IO_REGBIT(TWCR, TWSTA),
308 .twsto = AVR_IO_REGBIT(TWCR, TWSTO),
309 .twwc = AVR_IO_REGBIT(TWCR, TWWC),
311 .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f), // 5 bits
312 .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3), // 2 bits
315 .enable = AVR_IO_REGBIT(TWCR, TWIE),
316 .raised = AVR_IO_REGBIT(TWCR, TWINT),
323 #endif /* SIM_CORENAME */
325 #endif /* __SIM_MEGAX_H__ */