cores: Now duplicate the global structure
[simavr] / simavr / cores / sim_megax.h
1 /*
2         sim_mega128.c
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_MEGAX_H__
23 #define __SIM_MEGAX_H__
24
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"
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 mx_init(struct avr_t * avr);
38 void mx_reset(struct avr_t * avr);
39
40 /*
41  * This is a template for all of the 8/32/64 devices, hopefuly
42  */
43 struct mcu_t {
44         avr_t          core;
45         avr_eeprom_t    eeprom;
46         avr_flash_t     selfprog;
47         avr_watchdog_t  watchdog;
48         avr_extint_t    extint;
49         avr_ioport_t    portb, portc, portd;
50         avr_uart_t              uart;
51         avr_adc_t               adc;
52         avr_timer_t             timer0,timer1,timer2;
53         avr_spi_t               spi;
54         avr_twi_t               twi;
55 };
56
57 #ifdef SIM_CORENAME
58
59 #ifndef SIM_VECTOR_SIZE
60 #error SIM_VECTOR_SIZE is not declared
61 #endif
62 #ifndef SIM_MMCU
63 #error SIM_MMCU is not declared
64 #endif
65
66 #ifndef EFUSE_DEFAULT
67 #define EFUSE_DEFAULT 0xff
68 #endif
69 #define EICRA MCUCR
70 #define EIMSK GICR
71 #define EIFR GIFR
72
73 const struct mcu_t SIM_CORENAME = {
74         .core = {
75                 .mmcu = SIM_MMCU,
76                 DEFAULT_CORE(SIM_VECTOR_SIZE),
77
78                 .init = mx_init,
79                 .reset = mx_reset,
80         },
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)),
84         .extint = {
85                 AVR_EXTINT_DECLARE(0, 'D', PD2),
86                 AVR_EXTINT_DECLARE(1, 'D', PD3),
87         },
88         .portb = {
89                 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
90         },
91         .portc = {
92                 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
93         },
94         .portd = {
95                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
96         },
97
98         .uart = {
99            // no PRUSART .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
100                 .name = '0',
101                 .r_udr = UDR,
102
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
107
108                 .r_ucsra = UCSRA,
109                 .r_ucsrb = UCSRB,
110                 .r_ucsrc = UCSRC,
111                 .r_ubrrl = UBRRL,
112                 .r_ubrrh = UBRRH,
113                 .rxc = {
114                         .enable = AVR_IO_REGBIT(UCSRB, RXCIE),
115                         .raised = AVR_IO_REGBIT(UCSRA, RXC),
116                         .vector = USART_RXC_vect,
117                 },
118                 .txc = {
119                         .enable = AVR_IO_REGBIT(UCSRB, TXCIE),
120                         .raised = AVR_IO_REGBIT(UCSRA, TXC),
121                         .vector = USART_TXC_vect,
122                 },
123                 .udrc = {
124                         .enable = AVR_IO_REGBIT(UCSRB, UDRIE),
125                         .raised = AVR_IO_REGBIT(UCSRA, UDRE),
126                         .vector = USART_UDRE_vect,
127                 },
128         },
129         .adc = {
130                 .r_admux = ADMUX,
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 },
135
136                 .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
137                 .r_adcsra = ADCSRA,
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),},
142
143                 .r_adch = ADCH,
144                 .r_adcl = ADCL,
145
146                 //.r_adcsrb = ADCSRB,
147                 // .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2),},
148
149                 .muxmode = {
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),
154
155                         [14] = AVR_ADC_REF(1300),       // 1.30V
156                         [15] = AVR_ADC_REF(0),          // GND
157                 },
158
159                 .adc = {
160                         .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
161                         .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
162                         .vector = ADC_vect,
163                 },
164         },
165         .timer0 = {
166                 .name = '0',
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 */},
170
171                 .r_tcnt = TCNT0,
172
173                 .overflow = {
174                         .enable = AVR_IO_REGBIT(TIMSK, TOIE0),
175                         .raised = AVR_IO_REGBIT(TIFR, TOV0),
176                         .vector = TIMER0_OVF_vect,
177                 },
178         },
179         .timer1 = {
180                 .name = '1',
181                 .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
182                                         AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
183                 .wgm_op = {
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(),
196                 },
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 */},
199
200                 .r_tcnt = TCNT1L,
201                 .r_icr = ICR1L,
202                 .r_icrh = ICR1H,
203                 .r_tcnth = TCNT1H,
204
205                 .ices = AVR_IO_REGBIT(TCCR1B, ICES1),
206                 .icp = AVR_IO_REGBIT(PORTD, 4),
207
208                 .overflow = {
209                         .enable = AVR_IO_REGBIT(TIMSK, TOIE1),
210                         .raised = AVR_IO_REGBIT(TIFR, TOV1),
211                         .vector = TIMER1_OVF_vect,
212                 },
213                 .icr = {
214                         .enable = AVR_IO_REGBIT(TIMSK, TICIE1),
215                         .raised = AVR_IO_REGBIT(TIFR, ICF1),
216                         .vector = TIMER1_CAPT_vect,
217                 },
218                 .comp = {
219                         [AVR_TIMER_COMPA] = {
220                                 .r_ocr = OCR1AL,
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),
224                                 .interrupt = {
225                                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1A),
226                                         .raised = AVR_IO_REGBIT(TIFR, OCF1A),
227                                         .vector = TIMER1_COMPA_vect,
228                                 },
229                         },
230                         [AVR_TIMER_COMPB] = {
231                                 .r_ocr = OCR1BL,
232                                 .r_ocrh = OCR1BH,
233                                 .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
234                                 .com_pin = AVR_IO_REGBIT(PORTB, PB6),
235                                 .interrupt = {
236                                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1B),
237                                         .raised = AVR_IO_REGBIT(TIFR, OCF1B),
238                                         .vector = TIMER1_COMPB_vect,
239                                 },
240                         },
241                 },
242
243         },
244         .timer2 = {
245                 .name = '2',
246                 .wgm = { AVR_IO_REGBIT(TCCR2, WGM20), AVR_IO_REGBIT(TCCR2, WGM21) },
247                 .wgm_op = {
248                         [0] = AVR_TIMER_WGM_NORMAL8(),
249                         // TODO 1 pwm phase correct 
250                         [2] = AVR_TIMER_WGM_CTC(),
251                         [3] = AVR_TIMER_WGM_FASTPWM8(),
252                 },
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 */ },
255
256                 .r_tcnt = TCNT2,
257
258                 // asynchronous timer source bit.. if set, use 32khz frequency
259                 .as2 = AVR_IO_REGBIT(ASSR, AS2),
260
261                 .overflow = {
262                         .enable = AVR_IO_REGBIT(TIMSK, TOIE2),
263                         .raised = AVR_IO_REGBIT(TIFR, TOV2),
264                         .vector = TIMER2_OVF_vect,
265                 },
266                 .comp = {
267                         [AVR_TIMER_COMPA] = {
268                                 .r_ocr = OCR2,
269                                 .com = AVR_IO_REGBITS(TCCR2, COM20, 0x3),
270                                 .com_pin = AVR_IO_REGBIT(PORTB, PB7), // same as timer1C
271                                 .interrupt = {
272                                         .enable = AVR_IO_REGBIT(TIMSK, OCIE2),
273                                         .raised = AVR_IO_REGBIT(TIFR, OCF2),
274                                         .vector = TIMER2_COMP_vect,
275                                 },
276                         },
277                 },
278         },
279         .spi = {
280
281                 .r_spdr = SPDR,
282                 .r_spcr = SPCR,
283                 .r_spsr = SPSR,
284
285                 .spe = AVR_IO_REGBIT(SPCR, SPE),
286                 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
287
288                 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
289                 .spi = {
290                         .enable = AVR_IO_REGBIT(SPCR, SPIE),
291                         .raised = AVR_IO_REGBIT(SPSR, SPIF),
292                         .vector = SPI_STC_vect,
293                 },
294         },
295         
296         .twi = {
297
298                 .r_twcr = TWCR,
299                 .r_twsr = TWSR,
300                 .r_twbr = TWBR,
301                 .r_twdr = TWDR,
302                 .r_twar = TWAR,
303                 // no .r_twamr = TWAMR,
304
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),
310
311                 .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f),       // 5 bits
312                 .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3),       // 2 bits
313
314                 .twi = {
315                         .enable = AVR_IO_REGBIT(TWCR, TWIE),
316                         .raised = AVR_IO_REGBIT(TWCR, TWINT),
317                         .vector = TWI_vect,
318                 },
319         },
320
321 };
322
323 #endif /* SIM_CORENAME */
324
325 #endif /* __SIM_MEGAX_H__ */