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