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