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