cores: Add a few more mega cores
[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_extint.h"
29 #include "avr_ioport.h"
30 #include "avr_uart.h"
31 #include "avr_timer8.h"
32 #include "avr_spi.h"
33 #include "avr_twi.h"
34
35 void mx4_init(struct avr_t * avr);
36 void mx4_reset(struct avr_t * avr);
37
38 /*
39  * This is a template for all of the x4 devices, hopefuly
40  */
41 struct mcu_t {
42         avr_t core;
43         avr_eeprom_t    eeprom;
44         avr_extint_t    extint;
45         avr_ioport_t    porta, portb, portc, portd;
46         avr_uart_t              uart0,uart1;
47         avr_timer8_t    timer0,timer2;
48         avr_spi_t               spi;
49         avr_twi_t               twi;
50 };
51
52 #ifdef SIM_CORENAME
53
54 #ifndef SIM_MMCU
55 #error SIM_MMCU is not declared
56 #endif
57
58 struct mcu_t SIM_CORENAME = {
59         .core = {
60                 .mmcu = SIM_MMCU,
61                 DEFAULT_CORE(4),
62
63                 .init = mx4_init,
64                 .reset = mx4_reset,
65         },
66         AVR_EEPROM_DECLARE(EE_READY_vect),
67         .extint = {
68                 AVR_EXTINT_DECLARE(0, 'D', PD2),
69                 AVR_EXTINT_DECLARE(1, 'D', PD3),
70                 AVR_EXTINT_DECLARE(2, 'B', PB3),
71         },
72         .porta = {
73                 .name = 'A', .r_port = PORTA, .r_ddr = DDRA, .r_pin = PINA,
74                 .pcint = {
75                         .enable = AVR_IO_REGBIT(PCICR, PCIE0),
76                         .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
77                         .vector = PCINT0_vect,
78                 },
79                 .r_pcint = PCMSK0,
80         },
81         .portb = {
82                 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
83                 .pcint = {
84                         .enable = AVR_IO_REGBIT(PCICR, PCIE1),
85                         .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
86                         .vector = PCINT1_vect,
87                 },
88                 .r_pcint = PCMSK1,
89         },
90         .portc = {
91                 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
92                 .pcint = {
93                         .enable = AVR_IO_REGBIT(PCICR, PCIE2),
94                         .raised = AVR_IO_REGBIT(PCIFR, PCIF2),
95                         .vector = PCINT2_vect,
96                 },
97                 .r_pcint = PCMSK2,
98         },
99         .portd = {
100                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
101                 .pcint = {
102                         .enable = AVR_IO_REGBIT(PCICR, PCIE3),
103                         .raised = AVR_IO_REGBIT(PCIFR, PCIF3),
104                         .vector = PCINT3_vect,
105                 },
106                 .r_pcint = PCMSK3,
107         },
108
109         .uart0 = {
110                 .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
111                 .name = '0',
112                 .r_udr = UDR0,
113
114                 .txen = AVR_IO_REGBIT(UCSR0B, TXEN0),
115                 .rxen = AVR_IO_REGBIT(UCSR0B, RXEN0),
116
117                 .r_ucsra = UCSR0A,
118                 .r_ucsrb = UCSR0B,
119                 .r_ucsrc = UCSR0C,
120                 .r_ubrrl = UBRR0L,
121                 .r_ubrrh = UBRR0H,
122                 .rxc = {
123                         .enable = AVR_IO_REGBIT(UCSR0B, RXCIE0),
124                         .raised = AVR_IO_REGBIT(UCSR0A, RXC0),
125                         .vector = USART0_RX_vect,
126                 },
127                 .txc = {
128                         .enable = AVR_IO_REGBIT(UCSR0B, TXCIE0),
129                         .raised = AVR_IO_REGBIT(UCSR0A, TXC0),
130                         .vector = USART0_TX_vect,
131                 },
132                 .udrc = {
133                         .enable = AVR_IO_REGBIT(UCSR0B, UDRIE0),
134                         .raised = AVR_IO_REGBIT(UCSR0A, UDRE0),
135                         .vector = USART0_UDRE_vect,
136                 },
137         },
138         .uart1 = {
139                 .disabled = AVR_IO_REGBIT(PRR,PRUSART1),
140                 .name = '1',
141                 .r_udr = UDR1,
142
143                 .txen = AVR_IO_REGBIT(UCSR1B, TXEN1),
144                 .rxen = AVR_IO_REGBIT(UCSR1B, RXEN1),
145
146                 .r_ucsra = UCSR1A,
147                 .r_ucsrb = UCSR1B,
148                 .r_ucsrc = UCSR1C,
149                 .r_ubrrl = UBRR1L,
150                 .r_ubrrh = UBRR1H,
151                 .rxc = {
152                         .enable = AVR_IO_REGBIT(UCSR1B, RXCIE1),
153                         .raised = AVR_IO_REGBIT(UCSR1A, RXC1),
154                         .vector = USART1_RX_vect,
155                 },
156                 .txc = {
157                         .enable = AVR_IO_REGBIT(UCSR1B, TXCIE1),
158                         .raised = AVR_IO_REGBIT(UCSR1A, TXC1),
159                         .vector = USART1_TX_vect,
160                 },
161                 .udrc = {
162                         .enable = AVR_IO_REGBIT(UCSR1B, UDRIE1),
163                         .raised = AVR_IO_REGBIT(UCSR1A, UDRE1),
164                         .vector = USART1_UDRE_vect,
165                 },
166         },
167
168         .timer0 = {
169                 .name = '0',
170                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
171                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
172                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
173
174                 .r_ocra = OCR0A,
175                 .r_ocrb = OCR0B,
176                 .r_tcnt = TCNT0,
177
178                 .overflow = {
179                         .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
180                         .raised = AVR_IO_REGBIT(TIFR0, TOV0),
181                         .vector = TIMER0_OVF_vect,
182                 },
183                 .compa = {
184                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
185                         .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
186                         .vector = TIMER0_COMPA_vect,
187                 },
188                 .compb = {
189                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
190                         .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
191                         .vector = TIMER0_COMPB_vect,
192                 },
193         },
194         .timer2 = {
195                 .name = '2',
196                 .wgm = { AVR_IO_REGBIT(TCCR2A, WGM20), AVR_IO_REGBIT(TCCR2A, WGM21), AVR_IO_REGBIT(TCCR2B, WGM22) },
197                 .cs = { AVR_IO_REGBIT(TCCR2B, CS20), AVR_IO_REGBIT(TCCR2B, CS21), AVR_IO_REGBIT(TCCR2B, CS22) },
198                 .cs_div = { 0, 0, 3 /* 8 */, 5 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ },
199
200                 .r_ocra = OCR2A,
201                 .r_ocrb = OCR2B,
202                 .r_tcnt = TCNT2,
203                 
204                 // asynchronous timer source bit.. if set, use 32khz frequency
205                 .as2 = AVR_IO_REGBIT(ASSR, AS2),
206                 
207                 .overflow = {
208                         .enable = AVR_IO_REGBIT(TIMSK2, TOIE2),
209                         .raised = AVR_IO_REGBIT(TIFR2, TOV2),
210                         .vector = TIMER2_OVF_vect,
211                 },
212                 .compa = {
213                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2A),
214                         .raised = AVR_IO_REGBIT(TIFR2, OCF2A),
215                         .vector = TIMER2_COMPA_vect,
216                 },
217                 .compb = {
218                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2B),
219                         .raised = AVR_IO_REGBIT(TIFR2, OCF2B),
220                         .vector = TIMER2_COMPB_vect,
221                 },
222         },
223         .spi = {
224                 .disabled = AVR_IO_REGBIT(PRR,PRSPI),
225
226                 .r_spdr = SPDR,
227                 .r_spcr = SPCR,
228                 .r_spsr = SPSR,
229
230                 .spe = AVR_IO_REGBIT(SPCR, SPE),
231                 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
232
233                 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
234                 .spi = {
235                         .enable = AVR_IO_REGBIT(SPCR, SPIE),
236                         .raised = AVR_IO_REGBIT(SPSR, SPIF),
237                         .vector = SPI_STC_vect,
238                 },
239         },
240         
241         .twi = {
242                 .disabled = AVR_IO_REGBIT(PRR,PRTWI),
243
244                 .r_twcr = TWCR,
245                 .r_twsr = TWSR,
246                 .r_twbr = TWBR,
247                 .r_twdr = TWDR,
248                 .r_twar = TWAR,
249                 .r_twamr = TWAMR,
250
251                 .twen = AVR_IO_REGBIT(TWCR, TWEN),
252                 .twea = AVR_IO_REGBIT(TWCR, TWEA),
253                 .twsta = AVR_IO_REGBIT(TWCR, TWSTA),
254                 .twsto = AVR_IO_REGBIT(TWCR, TWSTO),
255                 .twwc = AVR_IO_REGBIT(TWCR, TWWC),
256
257                 .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f),       // 5 bits
258                 .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3),       // 2 bits
259
260                 .twi = {
261                         .enable = AVR_IO_REGBIT(TWCR, TWIE),
262                         .raised = AVR_IO_REGBIT(TWSR, TWINT),
263                         .vector = TWI_vect,
264                 },
265         },
266
267 };
268
269 #endif /* SIM_CORENAME */
270
271 #endif /* __SIM_MEGAX4_H__ */