9b1cb1fb81b62e29488e7eb8846d56d6e0a059b0
[simavr] / simavr / cores / sim_megax4.h
1 /*
2         sim_megax4.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 __SIM_MEGAX4_H__
23 #define __SIM_MEGAX4_H__
24
25 #include "sim_avr.h"
26 #include "sim_core_declare.h"
27 #include "avr_eeprom.h"
28 #include "avr_flash.h"
29 #include "avr_extint.h"
30 #include "avr_ioport.h"
31 #include "avr_uart.h"
32 #include "avr_adc.h"
33 #include "avr_timer.h"
34 #include "avr_spi.h"
35 #include "avr_twi.h"
36
37 void mx4_init(struct avr_t * avr);
38 void mx4_reset(struct avr_t * avr);
39
40 /*
41  * This is a template for all of the x4 devices, hopefuly
42  */
43 struct mcu_t {
44         avr_t core;
45         avr_eeprom_t    eeprom;
46         avr_flash_t     selfprog;
47         avr_extint_t    extint;
48         avr_ioport_t    porta, portb, portc, portd;
49         avr_uart_t              uart0,uart1;
50         avr_adc_t               adc;
51         avr_timer_t             timer0,timer1,timer2;
52         avr_spi_t               spi;
53         avr_twi_t               twi;
54 };
55
56 #ifdef SIM_CORENAME
57
58 #ifndef SIM_MMCU
59 #error SIM_MMCU is not declared
60 #endif
61
62 struct mcu_t SIM_CORENAME = {
63         .core = {
64                 .mmcu = SIM_MMCU,
65                 DEFAULT_CORE(4),
66
67                 .init = mx4_init,
68                 .reset = mx4_reset,
69         },
70         AVR_EEPROM_DECLARE(EE_READY_vect),
71         AVR_SELFPROG_DECLARE(SPMCSR, SPMEN, SPM_READY_vect),
72         .extint = {
73                 AVR_EXTINT_DECLARE(0, 'D', PD2),
74                 AVR_EXTINT_DECLARE(1, 'D', PD3),
75                 AVR_EXTINT_DECLARE(2, 'B', PB3),
76         },
77         .porta = {
78                 .name = 'A', .r_port = PORTA, .r_ddr = DDRA, .r_pin = PINA,
79                 .pcint = {
80                         .enable = AVR_IO_REGBIT(PCICR, PCIE0),
81                         .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
82                         .vector = PCINT0_vect,
83                 },
84                 .r_pcint = PCMSK0,
85         },
86         .portb = {
87                 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
88                 .pcint = {
89                         .enable = AVR_IO_REGBIT(PCICR, PCIE1),
90                         .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
91                         .vector = PCINT1_vect,
92                 },
93                 .r_pcint = PCMSK1,
94         },
95         .portc = {
96                 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
97                 .pcint = {
98                         .enable = AVR_IO_REGBIT(PCICR, PCIE2),
99                         .raised = AVR_IO_REGBIT(PCIFR, PCIF2),
100                         .vector = PCINT2_vect,
101                 },
102                 .r_pcint = PCMSK2,
103         },
104         .portd = {
105                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
106                 .pcint = {
107                         .enable = AVR_IO_REGBIT(PCICR, PCIE3),
108                         .raised = AVR_IO_REGBIT(PCIFR, PCIF3),
109                         .vector = PCINT3_vect,
110                 },
111                 .r_pcint = PCMSK3,
112         },
113
114         .uart0 = {
115                 .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
116                 .name = '0',
117                 .r_udr = UDR0,
118
119                 .txen = AVR_IO_REGBIT(UCSR0B, TXEN0),
120                 .rxen = AVR_IO_REGBIT(UCSR0B, RXEN0),
121
122                 .r_ucsra = UCSR0A,
123                 .r_ucsrb = UCSR0B,
124                 .r_ucsrc = UCSR0C,
125                 .r_ubrrl = UBRR0L,
126                 .r_ubrrh = UBRR0H,
127                 .rxc = {
128                         .enable = AVR_IO_REGBIT(UCSR0B, RXCIE0),
129                         .raised = AVR_IO_REGBIT(UCSR0A, RXC0),
130                         .vector = USART0_RX_vect,
131                 },
132                 .txc = {
133                         .enable = AVR_IO_REGBIT(UCSR0B, TXCIE0),
134                         .raised = AVR_IO_REGBIT(UCSR0A, TXC0),
135                         .vector = USART0_TX_vect,
136                 },
137                 .udrc = {
138                         .enable = AVR_IO_REGBIT(UCSR0B, UDRIE0),
139                         .raised = AVR_IO_REGBIT(UCSR0A, UDRE0),
140                         .vector = USART0_UDRE_vect,
141                 },
142         },
143         .uart1 = {
144                 .disabled = AVR_IO_REGBIT(PRR,PRUSART1),
145                 .name = '1',
146                 .r_udr = UDR1,
147
148                 .txen = AVR_IO_REGBIT(UCSR1B, TXEN1),
149                 .rxen = AVR_IO_REGBIT(UCSR1B, RXEN1),
150
151                 .r_ucsra = UCSR1A,
152                 .r_ucsrb = UCSR1B,
153                 .r_ucsrc = UCSR1C,
154                 .r_ubrrl = UBRR1L,
155                 .r_ubrrh = UBRR1H,
156                 .rxc = {
157                         .enable = AVR_IO_REGBIT(UCSR1B, RXCIE1),
158                         .raised = AVR_IO_REGBIT(UCSR1A, RXC1),
159                         .vector = USART1_RX_vect,
160                 },
161                 .txc = {
162                         .enable = AVR_IO_REGBIT(UCSR1B, TXCIE1),
163                         .raised = AVR_IO_REGBIT(UCSR1A, TXC1),
164                         .vector = USART1_TX_vect,
165                 },
166                 .udrc = {
167                         .enable = AVR_IO_REGBIT(UCSR1B, UDRIE1),
168                         .raised = AVR_IO_REGBIT(UCSR1A, UDRE1),
169                         .vector = USART1_UDRE_vect,
170                 },
171         },
172         .adc = {
173                 .r_admux = ADMUX,
174                 .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1),
175                                         AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3),
176                                         AVR_IO_REGBIT(ADMUX, MUX4),},
177                 .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1)},
178                 .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
179                 .r_adcsra = ADCSRA,
180                 .aden = AVR_IO_REGBIT(ADCSRA, ADEN),
181                 .adsc = AVR_IO_REGBIT(ADCSRA, ADSC),
182                 .adate = AVR_IO_REGBIT(ADCSRA, ADATE),
183                 .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),},
184
185                 .r_adch = ADCH,
186                 .r_adcl = ADCL,
187
188                 .r_adcsrb = ADCSRB,
189                 .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2),},
190
191                 .adc = {
192                         .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
193                         .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
194                         .vector = ADC_vect,
195                 },
196         },
197         .timer0 = {
198                 .name = '0',
199                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
200                 .wgm_op = {
201                         [0] = AVR_TIMER_WGM_NORMAL8(),
202                         [2] = AVR_TIMER_WGM_CTC(),
203                         [3] = AVR_TIMER_WGM_FASTPWM8(),
204                         [7] = AVR_TIMER_WGM_OCPWM(),
205                 },
206                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
207                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
208
209                 .r_ocra = OCR0A,
210                 .r_ocrb = OCR0B,
211                 .r_tcnt = TCNT0,
212
213                 .overflow = {
214                         .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
215                         .raised = AVR_IO_REGBIT(TIFR0, TOV0),
216                         .vector = TIMER0_OVF_vect,
217                 },
218                 .compa = {
219                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
220                         .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
221                         .vector = TIMER0_COMPA_vect,
222                 },
223                 .compb = {
224                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
225                         .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
226                         .vector = TIMER0_COMPB_vect,
227                 },
228         },
229         .timer1 = {
230                 .name = '1',
231                 .disabled = AVR_IO_REGBIT(PRR,PRTIM1),
232                 .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
233                                         AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
234                 .wgm_op = {
235                         [0] = AVR_TIMER_WGM_NORMAL16(),
236                         [4] = AVR_TIMER_WGM_CTC(),
237                         [5] = AVR_TIMER_WGM_FASTPWM8(),
238                         [6] = AVR_TIMER_WGM_FASTPWM9(),
239                         [7] = AVR_TIMER_WGM_FASTPWM10(),
240                         [12] = AVR_TIMER_WGM_ICCTC(),
241                         [14] = AVR_TIMER_WGM_ICPWM(),
242                         [15] = AVR_TIMER_WGM_OCPWM(),
243                 },
244                 .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
245                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */  /* External clock T1 is not handled */},
246
247                 .r_ocra = OCR1AL,
248                 .r_ocrb = OCR1BL,
249                 .r_tcnt = TCNT1L,
250                 .r_icr = ICR1L,
251                 .r_icrh = ICR1H,
252                 .r_ocrah = OCR1AH,      // 16 bits timers have two bytes of it
253                 .r_ocrbh = OCR1BH,
254                 .r_tcnth = TCNT1H,
255
256                 .overflow = {
257                         .enable = AVR_IO_REGBIT(TIMSK1, TOIE1),
258                         .raised = AVR_IO_REGBIT(TIFR1, TOV1),
259                         .vector = TIMER1_OVF_vect,
260                 },
261                 .compa = {
262                         .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A),
263                         .raised = AVR_IO_REGBIT(TIFR1, OCF1A),
264                         .vector = TIMER1_COMPA_vect,
265                 },
266                 .compb = {
267                         .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B),
268                         .raised = AVR_IO_REGBIT(TIFR1, OCF1B),
269                         .vector = TIMER1_COMPB_vect,
270                 },
271                 .icr = {
272                         .enable = AVR_IO_REGBIT(TIMSK1, ICIE1),
273                         .raised = AVR_IO_REGBIT(TIFR1, ICF1),
274                         .vector = TIMER1_CAPT_vect,
275                 },
276         },
277         .timer2 = {
278                 .name = '2',
279                 .wgm = { AVR_IO_REGBIT(TCCR2A, WGM20), AVR_IO_REGBIT(TCCR2A, WGM21), AVR_IO_REGBIT(TCCR2B, WGM22) },
280                 .wgm_op = {
281                         [0] = AVR_TIMER_WGM_NORMAL8(),
282                         [2] = AVR_TIMER_WGM_CTC(),
283                         [3] = AVR_TIMER_WGM_FASTPWM8(),
284                         [7] = AVR_TIMER_WGM_OCPWM(),
285                 },
286                 .cs = { AVR_IO_REGBIT(TCCR2B, CS20), AVR_IO_REGBIT(TCCR2B, CS21), AVR_IO_REGBIT(TCCR2B, CS22) },
287                 .cs_div = { 0, 0, 3 /* 8 */, 5 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ },
288
289                 .r_ocra = OCR2A,
290                 .r_ocrb = OCR2B,
291                 .r_tcnt = TCNT2,
292                 
293                 // asynchronous timer source bit.. if set, use 32khz frequency
294                 .as2 = AVR_IO_REGBIT(ASSR, AS2),
295                 
296                 .overflow = {
297                         .enable = AVR_IO_REGBIT(TIMSK2, TOIE2),
298                         .raised = AVR_IO_REGBIT(TIFR2, TOV2),
299                         .vector = TIMER2_OVF_vect,
300                 },
301                 .compa = {
302                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2A),
303                         .raised = AVR_IO_REGBIT(TIFR2, OCF2A),
304                         .vector = TIMER2_COMPA_vect,
305                 },
306                 .compb = {
307                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2B),
308                         .raised = AVR_IO_REGBIT(TIFR2, OCF2B),
309                         .vector = TIMER2_COMPB_vect,
310                 },
311         },
312         .spi = {
313                 .disabled = AVR_IO_REGBIT(PRR,PRSPI),
314
315                 .r_spdr = SPDR,
316                 .r_spcr = SPCR,
317                 .r_spsr = SPSR,
318
319                 .spe = AVR_IO_REGBIT(SPCR, SPE),
320                 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
321
322                 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
323                 .spi = {
324                         .enable = AVR_IO_REGBIT(SPCR, SPIE),
325                         .raised = AVR_IO_REGBIT(SPSR, SPIF),
326                         .vector = SPI_STC_vect,
327                 },
328         },
329         
330         .twi = {
331                 .disabled = AVR_IO_REGBIT(PRR,PRTWI),
332
333                 .r_twcr = TWCR,
334                 .r_twsr = TWSR,
335                 .r_twbr = TWBR,
336                 .r_twdr = TWDR,
337                 .r_twar = TWAR,
338                 .r_twamr = TWAMR,
339
340                 .twen = AVR_IO_REGBIT(TWCR, TWEN),
341                 .twea = AVR_IO_REGBIT(TWCR, TWEA),
342                 .twsta = AVR_IO_REGBIT(TWCR, TWSTA),
343                 .twsto = AVR_IO_REGBIT(TWCR, TWSTO),
344                 .twwc = AVR_IO_REGBIT(TWCR, TWWC),
345
346                 .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f),       // 5 bits
347                 .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3),       // 2 bits
348
349                 .twi = {
350                         .enable = AVR_IO_REGBIT(TWCR, TWIE),
351                         .raised = AVR_IO_REGBIT(TWSR, TWINT),
352                         .vector = TWI_vect,
353                 },
354         },
355
356 };
357
358 #endif /* SIM_CORENAME */
359
360 #endif /* __SIM_MEGAX4_H__ */