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