new global_logger, used in AVR_LOG(), default is stdout/stderr
[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, hopefully
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 const 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                 .usbs = AVR_IO_REGBIT(UCSR0C, USBS0),
118                 .ucsz = AVR_IO_REGBITS(UCSR0C, UCSZ00, 0x3), // 2 bits
119                 .ucsz2 = AVR_IO_REGBIT(UCSR0B, UCSZ02),         // 1 bits
120
121                 .r_ucsra = UCSR0A,
122                 .r_ucsrb = UCSR0B,
123                 .r_ucsrc = UCSR0C,
124                 .r_ubrrl = UBRR0L,
125                 .r_ubrrh = UBRR0H,
126                 .rxc = {
127                         .enable = AVR_IO_REGBIT(UCSR0B, RXCIE0),
128                         .raised = AVR_IO_REGBIT(UCSR0A, RXC0),
129                         .vector = USART_RX_vect,
130                 },
131                 .txc = {
132                         .enable = AVR_IO_REGBIT(UCSR0B, TXCIE0),
133                         .raised = AVR_IO_REGBIT(UCSR0A, TXC0),
134                         .vector = USART_TX_vect,
135                 },
136                 .udrc = {
137                         .enable = AVR_IO_REGBIT(UCSR0B, UDRIE0),
138                         .raised = AVR_IO_REGBIT(UCSR0A, UDRE0),
139                         .vector = USART_UDRE_vect,
140                 },
141         },
142         .adc = {
143                 .r_admux = ADMUX,
144                 .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1),
145                                         AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3),},
146                 .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1)},
147                 .ref_values = { [1] = ADC_VREF_AVCC, [3] = ADC_VREF_V110, },
148
149                 .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
150                 .r_adcsra = ADCSRA,
151                 .aden = AVR_IO_REGBIT(ADCSRA, ADEN),
152                 .adsc = AVR_IO_REGBIT(ADCSRA, ADSC),
153                 .adate = AVR_IO_REGBIT(ADCSRA, ADATE),
154                 .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),},
155
156                 .r_adch = ADCH,
157                 .r_adcl = ADCL,
158
159                 .r_adcsrb = ADCSRB,
160                 .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2),},
161
162                 .muxmode = {
163                         [0] = AVR_ADC_SINGLE(0), [1] = AVR_ADC_SINGLE(1),
164                         [2] = AVR_ADC_SINGLE(2), [3] = AVR_ADC_SINGLE(3),
165                         [4] = AVR_ADC_SINGLE(4), [5] = AVR_ADC_SINGLE(5),
166                         [6] = AVR_ADC_SINGLE(6), [7] = AVR_ADC_TEMP(),
167                         [14] = AVR_ADC_REF(1100),       // 1.1V
168                         [15] = AVR_ADC_REF(0),          // GND
169                 },
170                 .adc = {
171                         .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
172                         .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
173                         .vector = ADC_vect,
174                 },
175         },
176         .timer0 = {
177                 .name = '0',
178                 .disabled = AVR_IO_REGBIT(PRR,PRTIM0),
179                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
180                 .wgm_op = {
181                         [0] = AVR_TIMER_WGM_NORMAL8(),
182                         [2] = AVR_TIMER_WGM_CTC(),
183                         [3] = AVR_TIMER_WGM_FASTPWM8(),
184                         [7] = AVR_TIMER_WGM_OCPWM(),
185                 },
186                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
187                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
188
189                 .r_tcnt = TCNT0,
190
191                 .overflow = {
192                         .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
193                         .raised = AVR_IO_REGBIT(TIFR0, TOV0),
194                         .vector = TIMER0_OVF_vect,
195                 },
196                 .comp = {
197                         [AVR_TIMER_COMPA] = {
198                                 .r_ocr = OCR0A,
199                                 .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3),
200                                 .com_pin = AVR_IO_REGBIT(PORTD, 6),
201                                 .interrupt = {
202                                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
203                                         .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
204                                         .vector = TIMER0_COMPA_vect,
205                                 },
206                         },
207                         [AVR_TIMER_COMPB] = {
208                                 .r_ocr = OCR0B,
209                                 .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3),
210                                 .com_pin = AVR_IO_REGBIT(PORTD, 5),
211                                 .interrupt = {
212                                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
213                                         .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
214                                         .vector = TIMER0_COMPB_vect,
215                                 }
216                         }
217                 }
218         },
219         .timer1 = {
220                 .name = '1',
221                 .disabled = AVR_IO_REGBIT(PRR,PRTIM1),
222                 .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
223                                         AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
224                 .wgm_op = {
225                         [0] = AVR_TIMER_WGM_NORMAL16(),
226                         [4] = AVR_TIMER_WGM_CTC(),
227                         [5] = AVR_TIMER_WGM_FASTPWM8(),
228                         [6] = AVR_TIMER_WGM_FASTPWM9(),
229                         [7] = AVR_TIMER_WGM_FASTPWM10(),
230                         [12] = AVR_TIMER_WGM_ICCTC(),
231                         [14] = AVR_TIMER_WGM_ICPWM(),
232                         [15] = AVR_TIMER_WGM_OCPWM(),
233                 },
234                 .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
235                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */  /* External clock T1 is not handled */},
236
237                 .r_tcnt = TCNT1L,
238                 .r_tcnth = TCNT1H,
239                 .r_icr = ICR1L,
240                 .r_icrh = ICR1H,
241
242                 .ices = AVR_IO_REGBIT(TCCR1B, ICES1),
243                 .icp = AVR_IO_REGBIT(PORTB, 0),
244
245                 .overflow = {
246                         .enable = AVR_IO_REGBIT(TIMSK1, TOIE1),
247                         .raised = AVR_IO_REGBIT(TIFR1, TOV1),
248                         .vector = TIMER1_OVF_vect,
249                 },
250                 .icr = {
251                         .enable = AVR_IO_REGBIT(TIMSK1, ICIE1),
252                         .raised = AVR_IO_REGBIT(TIFR1, ICF1),
253                         .vector = TIMER1_CAPT_vect,
254                 },
255                 .comp = {
256                         [AVR_TIMER_COMPA] = {
257                                 .r_ocr = OCR1AL,
258                                 .r_ocrh = OCR1AH,       // 16 bits timers have two bytes of it
259                                 .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3),
260                                 .com_pin = AVR_IO_REGBIT(PORTB, 1),
261                                 .interrupt = {
262                                         .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A),
263                                         .raised = AVR_IO_REGBIT(TIFR1, OCF1A),
264                                         .vector = TIMER1_COMPA_vect,
265                                 },
266                         },
267                         [AVR_TIMER_COMPB] = {
268                                 .r_ocr = OCR1BL,
269                                 .r_ocrh = OCR1BH,
270                                 .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
271                                 .com_pin = AVR_IO_REGBIT(PORTB, 2),
272                                 .interrupt = {
273                                         .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B),
274                                         .raised = AVR_IO_REGBIT(TIFR1, OCF1B),
275                                         .vector = TIMER1_COMPB_vect,
276                                 },
277                         },
278                 },
279         },
280         .timer2 = {
281                 .name = '2',
282                 .disabled = AVR_IO_REGBIT(PRR,PRTIM2),
283                 .wgm = { AVR_IO_REGBIT(TCCR2A, WGM20), AVR_IO_REGBIT(TCCR2A, WGM21), AVR_IO_REGBIT(TCCR2B, WGM22) },
284                 .wgm_op = {
285                         [0] = AVR_TIMER_WGM_NORMAL8(),
286                         [2] = AVR_TIMER_WGM_CTC(),
287                         [3] = AVR_TIMER_WGM_FASTPWM8(),
288                         [7] = AVR_TIMER_WGM_OCPWM(),
289                 },
290
291                 .cs = { AVR_IO_REGBIT(TCCR2B, CS20), AVR_IO_REGBIT(TCCR2B, CS21), AVR_IO_REGBIT(TCCR2B, CS22) },
292                 .cs_div = { 0, 0, 3 /* 8 */, 5 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ },
293
294                 .r_tcnt = TCNT2,
295                 
296                 // asynchronous timer source bit.. if set, use 32khz frequency
297                 .as2 = AVR_IO_REGBIT(ASSR, AS2),
298                 
299                 .overflow = {
300                         .enable = AVR_IO_REGBIT(TIMSK2, TOIE2),
301                         .raised = AVR_IO_REGBIT(TIFR2, TOV2),
302                         .vector = TIMER2_OVF_vect,
303                 },
304                 .comp = {
305                         [AVR_TIMER_COMPA] = {
306                                 .r_ocr = OCR2A,
307                                 .com = AVR_IO_REGBITS(TCCR2A, COM2A0, 0x3),
308                                 .com_pin = AVR_IO_REGBIT(PORTB, 3),
309                                 .interrupt = {
310                                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2A),
311                                         .raised = AVR_IO_REGBIT(TIFR2, OCF2A),
312                                         .vector = TIMER2_COMPA_vect,
313                                 }
314                         },
315                         [AVR_TIMER_COMPB] = {
316                                 .r_ocr = OCR2B,
317                                 .com = AVR_IO_REGBITS(TCCR2A, COM2B0, 0x3),
318                                 .com_pin = AVR_IO_REGBIT(PORTD, 3),
319                                 .interrupt = {
320                                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2B),
321                                         .raised = AVR_IO_REGBIT(TIFR2, OCF2B),
322                                         .vector = TIMER2_COMPB_vect,
323                                 }
324                         }
325                 }
326         },
327         .spi = {
328                 .disabled = AVR_IO_REGBIT(PRR,PRSPI),
329
330                 .r_spdr = SPDR,
331                 .r_spcr = SPCR,
332                 .r_spsr = SPSR,
333
334                 .spe = AVR_IO_REGBIT(SPCR, SPE),
335                 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
336
337                 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
338                 .spi = {
339                         .enable = AVR_IO_REGBIT(SPCR, SPIE),
340                         .raised = AVR_IO_REGBIT(SPSR, SPIF),
341                         .vector = SPI_STC_vect,
342                 },
343         },
344
345         .twi = {
346                 .disabled = AVR_IO_REGBIT(PRR,PRTWI),
347
348                 .r_twcr = TWCR,
349                 .r_twsr = TWSR,
350                 .r_twbr = TWBR,
351                 .r_twdr = TWDR,
352                 .r_twar = TWAR,
353                 .r_twamr = TWAMR,
354
355                 .twen = AVR_IO_REGBIT(TWCR, TWEN),
356                 .twea = AVR_IO_REGBIT(TWCR, TWEA),
357                 .twsta = AVR_IO_REGBIT(TWCR, TWSTA),
358                 .twsto = AVR_IO_REGBIT(TWCR, TWSTO),
359                 .twwc = AVR_IO_REGBIT(TWCR, TWWC),
360
361                 .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f),       // 5 bits
362                 .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3),       // 2 bits
363
364                 .twi = {
365                         .enable = AVR_IO_REGBIT(TWCR, TWIE),
366                         .raised = AVR_IO_REGBIT(TWCR, TWINT),
367                         .raise_sticky = 1,
368                         .vector = TWI_vect,
369                 },
370         },
371         
372 };
373 #endif /* SIM_CORENAME */
374
375 #endif /* __SIM_MEGAX8_H__ */