Initial Commit
[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
33 void mx8_init(struct avr_t * avr);
34 void mx8_reset(struct avr_t * avr);
35
36 /*
37  * This is a template for all of the x8 devices, hopefuly
38  */
39 struct mcu_t {
40         avr_t core;
41         avr_eeprom_t    eeprom;
42         avr_ioport_t    portb,portc,portd;
43         avr_uart_t              uart;
44         avr_timer8_t    timer0,timer2;
45         avr_spi_t               spi;
46 };
47
48 #ifdef SIM_CORENAME
49
50 #ifndef SIM_VECTOR_SIZE
51 #error SIM_VECTOR_SIZE is not declared
52 #endif
53 #ifndef SIM_MMCU
54 #error SIM_MMCU is not declared
55 #endif
56
57 struct mcu_t SIM_CORENAME = {
58         .core = {
59                 .mmcu = SIM_MMCU,
60                 DEFAULT_CORE(SIM_VECTOR_SIZE),
61
62                 .init = mx8_init,
63                 .reset = mx8_reset,
64         },
65         .eeprom = {
66                 .size = E2END+1,
67                 .r_eearh = EEARH,
68                 .r_eearl = EEARL,
69                 .r_eedr = EEDR,
70                 .r_eecr = EECR,
71                 .eepm = { AVR_IO_REGBIT(EECR, EEPM0), AVR_IO_REGBIT(EECR, EEPM1) },
72                 .eempe = AVR_IO_REGBIT(EECR, EEMPE),
73                 .eepe = AVR_IO_REGBIT(EECR, EEPE),
74                 .eere = AVR_IO_REGBIT(EECR, EERE),
75                 .ready = {
76                         .enable = AVR_IO_REGBIT(EECR, EERIE),
77                         .vector = EE_READY_vect,
78                 },
79         },
80         .portb = {
81                 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
82                 .pcint = {
83                         .enable = AVR_IO_REGBIT(PCICR, PCIE0),
84                         .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
85                         .vector = PCINT0_vect,
86                 },
87                 .r_pcint = PCMSK0,
88         },
89         .portc = {
90                 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
91                 .pcint = {
92                         .enable = AVR_IO_REGBIT(PCICR, PCIE1),
93                         .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
94                         .vector = PCINT1_vect,
95                 },
96                 .r_pcint = PCMSK1,
97         },
98         .portd = {
99                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
100                 .pcint = {
101                         .enable = AVR_IO_REGBIT(PCICR, PCIE2),
102                         .raised = AVR_IO_REGBIT(PCIFR, PCIF2),
103                         .vector = PCINT2_vect,
104                 },
105                 .r_pcint = PCMSK2,
106         },
107
108         .uart = {
109                 .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
110                 .name = '0',
111                 .r_udr = UDR0,
112                 .udre = AVR_IO_REGBIT(UCSR0A, UDRE0),
113
114                 .r_ucsra = UCSR0A,
115                 .r_ucsrb = UCSR0B,
116                 .r_ucsrc = UCSR0C,
117                 .r_ubrrl = UBRR0L,
118                 .r_ubrrh = UBRR0H,
119                 .rxc = {
120                         .enable = AVR_IO_REGBIT(UCSR0B, RXCIE0),
121                         .vector = USART_RX_vect,
122                 },
123                 .txc = {
124                         .enable = AVR_IO_REGBIT(UCSR0B, TXCIE0),
125                         .vector = USART_TX_vect,
126                 },
127                 .udrc = {
128                         .enable = AVR_IO_REGBIT(UCSR0B, UDRIE0),
129                         .vector = USART_UDRE_vect,
130                 },
131         },
132
133         .timer0 = {
134                 .name = '0',
135                 .disabled = AVR_IO_REGBIT(PRR,PRTIM0),
136                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
137                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
138                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
139
140                 .r_ocra = OCR0A,
141                 .r_ocrb = OCR0B,
142                 .r_tcnt = TCNT0,
143
144                 .overflow = {
145                         .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
146                         .raised = AVR_IO_REGBIT(TIFR0, TOV0),
147                         .vector = TIMER0_OVF_vect,
148                 },
149                 .compa = {
150                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
151                         .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
152                         .vector = TIMER0_COMPA_vect,
153                 },
154                 .compb = {
155                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
156                         .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
157                         .vector = TIMER0_COMPB_vect,
158                 },
159         },
160         .timer2 = {
161                 .name = '2',
162                 .disabled = AVR_IO_REGBIT(PRR,PRTIM2),
163                 .wgm = { AVR_IO_REGBIT(TCCR2A, WGM20), AVR_IO_REGBIT(TCCR2A, WGM21), AVR_IO_REGBIT(TCCR2B, WGM22) },
164                 .cs = { AVR_IO_REGBIT(TCCR2B, CS20), AVR_IO_REGBIT(TCCR2B, CS21), AVR_IO_REGBIT(TCCR2B, CS22) },
165                 .cs_div = { 0, 0, 3 /* 8 */, 5 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ },
166
167                 .r_ocra = OCR2A,
168                 .r_ocrb = OCR2B,
169                 .r_tcnt = TCNT2,
170                 
171                 // asynchronous timer source bit.. if set, use 32khz frequency
172                 .as2 = AVR_IO_REGBIT(ASSR, AS2),
173                 
174                 .overflow = {
175                         .enable = AVR_IO_REGBIT(TIMSK2, TOIE2),
176                         .raised = AVR_IO_REGBIT(TIFR2, TOV2),
177                         .vector = TIMER2_OVF_vect,
178                 },
179                 .compa = {
180                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2A),
181                         .raised = AVR_IO_REGBIT(TIFR2, OCF2A),
182                         .vector = TIMER2_COMPA_vect,
183                 },
184                 .compb = {
185                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2B),
186                         .raised = AVR_IO_REGBIT(TIFR2, OCF2B),
187                         .vector = TIMER2_COMPB_vect,
188                 },
189         },
190         
191         .spi = {
192                 .disabled = AVR_IO_REGBIT(PRR,PRSPI),
193                 .spe = AVR_IO_REGBIT(SPCR, SPE),
194                 .dord = AVR_IO_REGBIT(SPCR, DORD),
195                 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
196                 .cpol = AVR_IO_REGBIT(SPCR, CPOL),
197                 .cpha = AVR_IO_REGBIT(SPCR, CPHA),
198
199                 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
200                 .spi = {
201                         .enable = AVR_IO_REGBIT(SPCR, SPIE),
202                         .raised = AVR_IO_REGBIT(SPSR, SPIF),
203                         .vector = SPI_STC_vect,
204                 },
205         },
206 };
207 #endif /* SIM_CORENAME */
208
209 #endif /* __SIM_MEGAX8_H__ */