3124df9e86284802ed184fe0f9a240de0435d20b
[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_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 mx8_init(struct avr_t * avr);
36 void mx8_reset(struct avr_t * avr);
37
38 /*
39  * This is a template for all of the x8 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    portb,portc,portd;
46         avr_uart_t              uart;
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_VECTOR_SIZE
55 #error SIM_VECTOR_SIZE is not declared
56 #endif
57 #ifndef SIM_MMCU
58 #error SIM_MMCU is not declared
59 #endif
60
61 struct mcu_t SIM_CORENAME = {
62         .core = {
63                 .mmcu = SIM_MMCU,
64                 DEFAULT_CORE(SIM_VECTOR_SIZE),
65
66                 .init = mx8_init,
67                 .reset = mx8_reset,
68         },
69         AVR_EEPROM_DECLARE(EE_READY_vect),
70         .extint = {
71                 AVR_EXTINT_DECLARE(0, 'D', 2),
72                 AVR_EXTINT_DECLARE(1, 'D', 3),
73         },
74         .portb = {
75                 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
76                 .pcint = {
77                         .enable = AVR_IO_REGBIT(PCICR, PCIE0),
78                         .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
79                         .vector = PCINT0_vect,
80                 },
81                 .r_pcint = PCMSK0,
82         },
83         .portc = {
84                 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
85                 .pcint = {
86                         .enable = AVR_IO_REGBIT(PCICR, PCIE1),
87                         .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
88                         .vector = PCINT1_vect,
89                 },
90                 .r_pcint = PCMSK1,
91         },
92         .portd = {
93                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
94                 .pcint = {
95                         .enable = AVR_IO_REGBIT(PCICR, PCIE2),
96                         .raised = AVR_IO_REGBIT(PCIFR, PCIF2),
97                         .vector = PCINT2_vect,
98                 },
99                 .r_pcint = PCMSK2,
100         },
101
102         .uart = {
103                 .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
104                 .name = '0',
105                 .r_udr = UDR0,
106
107                 .txen = AVR_IO_REGBIT(UCSR0B, TXEN0),
108                 .rxen = AVR_IO_REGBIT(UCSR0B, RXEN0),
109
110                 .r_ucsra = UCSR0A,
111                 .r_ucsrb = UCSR0B,
112                 .r_ucsrc = UCSR0C,
113                 .r_ubrrl = UBRR0L,
114                 .r_ubrrh = UBRR0H,
115                 .rxc = {
116                         .enable = AVR_IO_REGBIT(UCSR0B, RXCIE0),
117                         .raised = AVR_IO_REGBIT(UCSR0A, RXC0),
118                         .vector = USART_RX_vect,
119                 },
120                 .txc = {
121                         .enable = AVR_IO_REGBIT(UCSR0B, TXCIE0),
122                         .raised = AVR_IO_REGBIT(UCSR0A, TXC0),
123                         .vector = USART_TX_vect,
124                 },
125                 .udrc = {
126                         .enable = AVR_IO_REGBIT(UCSR0B, UDRIE0),
127                         .raised = AVR_IO_REGBIT(UCSR0A, UDRE0),
128                         .vector = USART_UDRE_vect,
129                 },
130         },
131
132         .timer0 = {
133                 .name = '0',
134                 .disabled = AVR_IO_REGBIT(PRR,PRTIM0),
135                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
136                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
137                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
138
139                 .r_ocra = OCR0A,
140                 .r_ocrb = OCR0B,
141                 .r_tcnt = TCNT0,
142
143                 .overflow = {
144                         .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
145                         .raised = AVR_IO_REGBIT(TIFR0, TOV0),
146                         .vector = TIMER0_OVF_vect,
147                 },
148                 .compa = {
149                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
150                         .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
151                         .vector = TIMER0_COMPA_vect,
152                 },
153                 .compb = {
154                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
155                         .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
156                         .vector = TIMER0_COMPB_vect,
157                 },
158         },
159         .timer2 = {
160                 .name = '2',
161                 .disabled = AVR_IO_REGBIT(PRR,PRTIM2),
162                 .wgm = { AVR_IO_REGBIT(TCCR2A, WGM20), AVR_IO_REGBIT(TCCR2A, WGM21), AVR_IO_REGBIT(TCCR2B, WGM22) },
163                 .cs = { AVR_IO_REGBIT(TCCR2B, CS20), AVR_IO_REGBIT(TCCR2B, CS21), AVR_IO_REGBIT(TCCR2B, CS22) },
164                 .cs_div = { 0, 0, 3 /* 8 */, 5 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ },
165
166                 .r_ocra = OCR2A,
167                 .r_ocrb = OCR2B,
168                 .r_tcnt = TCNT2,
169                 
170                 // asynchronous timer source bit.. if set, use 32khz frequency
171                 .as2 = AVR_IO_REGBIT(ASSR, AS2),
172                 
173                 .overflow = {
174                         .enable = AVR_IO_REGBIT(TIMSK2, TOIE2),
175                         .raised = AVR_IO_REGBIT(TIFR2, TOV2),
176                         .vector = TIMER2_OVF_vect,
177                 },
178                 .compa = {
179                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2A),
180                         .raised = AVR_IO_REGBIT(TIFR2, OCF2A),
181                         .vector = TIMER2_COMPA_vect,
182                 },
183                 .compb = {
184                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2B),
185                         .raised = AVR_IO_REGBIT(TIFR2, OCF2B),
186                         .vector = TIMER2_COMPB_vect,
187                 },
188         },
189         
190         .spi = {
191                 .disabled = AVR_IO_REGBIT(PRR,PRSPI),
192
193                 .r_spdr = SPDR,
194                 .r_spcr = SPCR,
195                 .r_spsr = SPSR,
196
197                 .spe = AVR_IO_REGBIT(SPCR, SPE),
198                 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
199
200                 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
201                 .spi = {
202                         .enable = AVR_IO_REGBIT(SPCR, SPIE),
203                         .raised = AVR_IO_REGBIT(SPSR, SPIF),
204                         .vector = SPI_STC_vect,
205                 },
206         },
207
208         .twi = {
209                 .disabled = AVR_IO_REGBIT(PRR,PRTWI),
210
211                 .r_twcr = TWCR,
212                 .r_twsr = TWSR,
213                 .r_twbr = TWBR,
214                 .r_twdr = TWDR,
215                 .r_twar = TWAR,
216                 .r_twamr = TWAMR,
217
218                 .twen = AVR_IO_REGBIT(TWCR, TWEN),
219                 .twea = AVR_IO_REGBIT(TWCR, TWEA),
220                 .twsta = AVR_IO_REGBIT(TWCR, TWSTA),
221                 .twsto = AVR_IO_REGBIT(TWCR, TWSTO),
222                 .twwc = AVR_IO_REGBIT(TWCR, TWWC),
223
224                 .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f),       // 5 bits
225                 .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3),       // 2 bits
226
227                 .twi = {
228                         .enable = AVR_IO_REGBIT(TWCR, TWIE),
229                         .raised = AVR_IO_REGBIT(TWSR, TWINT),
230                         .vector = TWI_vect,
231                 },
232         },
233         
234 };
235 #endif /* SIM_CORENAME */
236
237 #endif /* __SIM_MEGAX8_H__ */