Initial Commit
[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 "simavr.h"
24 #include "sim_core_declare.h"
25 #include "avr_eeprom.h"
26 #include "avr_ioport.h"
27 #include "avr_uart.h"
28 #include "avr_timer8.h"
29
30 #define _AVR_IO_H_
31 #define __ASSEMBLER__
32 #include "avr/iom644.h"
33
34 static void init(struct avr_t * avr);
35 static void reset(struct avr_t * avr);
36
37
38 static struct mcu_t {
39         avr_t core;
40         avr_eeprom_t    eeprom;
41         avr_ioport_t    porta, portb, portc, portd;
42         avr_uart_t              uart0,uart1;
43         avr_timer8_t    timer0,timer2;
44 } mcu = {
45         .core = {
46                 .mmcu = "atmega644",
47                 DEFAULT_CORE(4),
48
49                 .init = init,
50                 .reset = reset,
51         },
52         .eeprom = {
53                 .size = E2END+1,
54                 .r_eearh = EEARH,
55                 .r_eearl = EEARL,
56                 .r_eedr = EEDR,
57                 .r_eecr = EECR,
58                 .eepm = { AVR_IO_REGBIT(EECR, EEPM0), AVR_IO_REGBIT(EECR, EEPM1) },
59                 .eempe = AVR_IO_REGBIT(EECR, EEMPE),
60                 .eepe = AVR_IO_REGBIT(EECR, EEPE),
61                 .eere = AVR_IO_REGBIT(EECR, EERE),
62                 .ready = {
63                         .enable = AVR_IO_REGBIT(EECR, EERIE),
64                         .vector = EE_READY_vect,
65                 },
66         },
67         .porta = {
68                 .name = 'A', .r_port = PORTA, .r_ddr = DDRA, .r_pin = PINA,
69                 .pcint = {
70                         .enable = AVR_IO_REGBIT(PCICR, PCIE0),
71                         .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
72                         .vector = PCINT0_vect,
73                 },
74                 .r_pcint = PCMSK0,
75         },
76         .portb = {
77                 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
78                 .pcint = {
79                         .enable = AVR_IO_REGBIT(PCICR, PCIE1),
80                         .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
81                         .vector = PCINT1_vect,
82                 },
83                 .r_pcint = PCMSK1,
84         },
85         .portc = {
86                 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
87                 .pcint = {
88                         .enable = AVR_IO_REGBIT(PCICR, PCIE2),
89                         .raised = AVR_IO_REGBIT(PCIFR, PCIF2),
90                         .vector = PCINT2_vect,
91                 },
92                 .r_pcint = PCMSK2,
93         },
94         .portd = {
95                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
96                 .pcint = {
97                         .enable = AVR_IO_REGBIT(PCICR, PCIE3),
98                         .raised = AVR_IO_REGBIT(PCIFR, PCIF3),
99                         .vector = PCINT3_vect,
100                 },
101                 .r_pcint = PCMSK3,
102         },
103
104         .uart0 = {
105                 .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
106                 .name = '0',
107                 .r_udr = UDR0,
108                 .udre = AVR_IO_REGBIT(UCSR0A, UDRE0),
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                         .vector = USART0_RX_vect,
118                 },
119                 .txc = {
120                         .enable = AVR_IO_REGBIT(UCSR0B, TXCIE0),
121                         .vector = USART0_TX_vect,
122                 },
123                 .udrc = {
124                         .enable = AVR_IO_REGBIT(UCSR0B, UDRIE0),
125                         .vector = USART0_UDRE_vect,
126                 },
127         },
128         .uart1 = {
129                 .disabled = AVR_IO_REGBIT(PRR,PRUSART1),
130                 .name = '1',
131                 .r_udr = UDR1,
132                 .udre = AVR_IO_REGBIT(UCSR1A, UDRE1),
133
134                 .r_ucsra = UCSR1A,
135                 .r_ucsrb = UCSR1B,
136                 .r_ucsrc = UCSR1C,
137                 .r_ubrrl = UBRR1L,
138                 .r_ubrrh = UBRR1H,
139                 .rxc = {
140                         .enable = AVR_IO_REGBIT(UCSR1B, RXCIE1),
141                         .vector = USART1_RX_vect,
142                 },
143                 .txc = {
144                         .enable = AVR_IO_REGBIT(UCSR1B, TXCIE1),
145                         .vector = USART1_TX_vect,
146                 },
147                 .udrc = {
148                         .enable = AVR_IO_REGBIT(UCSR1B, UDRIE1),
149                         .vector = USART1_UDRE_vect,
150                 },
151         },
152
153         .timer0 = {
154                 .name = '0',
155                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
156                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
157                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
158
159                 .r_ocra = OCR0A,
160                 .r_ocrb = OCR0B,
161                 .r_tcnt = TCNT0,
162
163                 .overflow = {
164                         .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
165                         .raised = AVR_IO_REGBIT(TIFR0, TOV0),
166                         .vector = TIMER0_OVF_vect,
167                 },
168                 .compa = {
169                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
170                         .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
171                         .vector = TIMER0_COMPA_vect,
172                 },
173                 .compb = {
174                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
175                         .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
176                         .vector = TIMER0_COMPB_vect,
177                 },
178         },
179         .timer2 = {
180                 .name = '2',
181                 .wgm = { AVR_IO_REGBIT(TCCR2A, WGM20), AVR_IO_REGBIT(TCCR2A, WGM21), AVR_IO_REGBIT(TCCR2B, WGM22) },
182                 .cs = { AVR_IO_REGBIT(TCCR2B, CS20), AVR_IO_REGBIT(TCCR2B, CS21), AVR_IO_REGBIT(TCCR2B, CS22) },
183                 .cs_div = { 0, 0, 3 /* 8 */, 5 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ },
184
185                 .r_ocra = OCR2A,
186                 .r_ocrb = OCR2B,
187                 .r_tcnt = TCNT2,
188                 
189                 // asynchronous timer source bit.. if set, use 32khz frequency
190                 .as2 = AVR_IO_REGBIT(ASSR, AS2),
191                 
192                 .overflow = {
193                         .enable = AVR_IO_REGBIT(TIMSK2, TOIE2),
194                         .raised = AVR_IO_REGBIT(TIFR2, TOV2),
195                         .vector = TIMER2_OVF_vect,
196                 },
197                 .compa = {
198                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2A),
199                         .raised = AVR_IO_REGBIT(TIFR2, OCF2A),
200                         .vector = TIMER2_COMPA_vect,
201                 },
202                 .compb = {
203                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2B),
204                         .raised = AVR_IO_REGBIT(TIFR2, OCF2B),
205                         .vector = TIMER2_COMPB_vect,
206                 },
207         },
208 };
209
210 static avr_t * make()
211 {
212         return &mcu.core;
213 }
214
215 avr_kind_t mega644 = {
216         .names = { "atmega644", "atmega644p" },
217         .make = make
218 };
219
220 static void init(struct avr_t * avr)
221 {
222         struct mcu_t * mcu = (struct mcu_t*)avr;
223
224         printf("%s init\n", avr->mmcu);
225         
226         avr_eeprom_init(avr, &mcu->eeprom);
227         avr_ioport_init(avr, &mcu->porta);
228         avr_ioport_init(avr, &mcu->portb);
229         avr_ioport_init(avr, &mcu->portc);
230         avr_ioport_init(avr, &mcu->portd);
231         avr_uart_init(avr, &mcu->uart0);
232         avr_uart_init(avr, &mcu->uart1);
233         avr_timer8_init(avr, &mcu->timer0);
234         avr_timer8_init(avr, &mcu->timer2);
235 }
236
237 static void reset(struct avr_t * avr)
238 {
239 //      struct mcu_t * mcu = (struct mcu_t*)avr;
240 }