cores: Added Mega128
[simavr] / simavr / cores / sim_mega128.c
1 /*
2         sim_mega128.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 <stdio.h>
23 #include "sim_avr.h"
24 #include "sim_core_declare.h"
25 #include "avr_eeprom.h"
26 #include "avr_flash.h"
27 #include "avr_watchdog.h"
28 #include "avr_extint.h"
29 #include "avr_ioport.h"
30 #include "avr_uart.h"
31 #include "avr_adc.h"
32 #include "avr_timer.h"
33 #include "avr_spi.h"
34 #include "avr_twi.h"
35
36 void m128_init(struct avr_t * avr);
37 void m128_reset(struct avr_t * avr);
38
39 #define _AVR_IO_H_
40 #define __ASSEMBLER__
41 #include "avr/iom128.h"
42
43 /*
44  * This is a template for all of the 128 devices, hopefuly
45  */
46 struct mcu_t {
47         avr_t          core;
48         avr_eeprom_t    eeprom;
49         avr_flash_t     selfprog;
50         avr_watchdog_t  watchdog;
51         avr_extint_t    extint;
52         avr_ioport_t    porta, portb, portc, portd, porte, portf, portg;
53         avr_uart_t              uart0,uart1;
54         avr_adc_t               adc;
55         avr_timer_t             timer0,timer1,timer2,timer3;
56         avr_spi_t               spi;
57         avr_twi_t               twi;
58 } mcu_mega128 = {
59         .core = {
60                 .mmcu = "atmega128",
61                 DEFAULT_CORE(4),
62
63                 .init = m128_init,
64                 .reset = m128_reset,
65
66                 .rampz = RAMPZ, // extended program memory access
67         },
68         AVR_EEPROM_DECLARE_NOEEPM(EE_READY_vect),
69         AVR_SELFPROG_DECLARE(SPMCSR, SPMEN, SPM_READY_vect),
70         AVR_WATCHDOG_DECLARE_128(WDTCR, _VECTOR(0)),
71         .extint = {
72                 AVR_EXTINT_DECLARE(0, 'D', PD2),
73                 AVR_EXTINT_DECLARE(1, 'D', PD3),
74                 AVR_EXTINT_DECLARE(2, 'B', PB3),
75         },
76         .porta = {  // no PCINTs in atmega128
77                 .name = 'A', .r_port = PORTA, .r_ddr = DDRA, .r_pin = PINA,
78         },
79         .portb = {
80                 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
81         },
82         .portc = {
83                 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
84         },
85         .portd = {
86                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
87         },
88         .porte = {
89                 .name = 'E', .r_port = PORTE, .r_ddr = DDRE, .r_pin = PINE,
90         },
91         .portf = {
92                 .name = 'F', .r_port = PORTF, .r_ddr = DDRF, .r_pin = PINF,
93         },
94         .portg = {
95                 .name = 'G', .r_port = PORTG, .r_ddr = DDRG, .r_pin = PING,
96         },
97
98         .uart0 = {
99            // no PRUSART .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
100                 .name = '0',
101                 .r_udr = UDR0,
102
103                 .txen = AVR_IO_REGBIT(UCSR0B, TXEN0),
104                 .rxen = AVR_IO_REGBIT(UCSR0B, RXEN0),
105
106                 .r_ucsra = UCSR0A,
107                 .r_ucsrb = UCSR0B,
108                 .r_ucsrc = UCSR0C,
109                 .r_ubrrl = UBRR0L,
110                 .r_ubrrh = UBRR0H,
111                 .rxc = {
112                         .enable = AVR_IO_REGBIT(UCSR0B, RXCIE0),
113                         .raised = AVR_IO_REGBIT(UCSR0A, RXC0),
114                         .vector = USART0_RX_vect,
115                 },
116                 .txc = {
117                         .enable = AVR_IO_REGBIT(UCSR0B, TXCIE0),
118                         .raised = AVR_IO_REGBIT(UCSR0A, TXC0),
119                         .vector = USART0_TX_vect,
120                 },
121                 .udrc = {
122                         .enable = AVR_IO_REGBIT(UCSR0B, UDRIE0),
123                         .raised = AVR_IO_REGBIT(UCSR0A, UDRE0),
124                         .vector = USART0_UDRE_vect,
125                 },
126         },
127         .uart1 = {
128            // no PRUSART .disabled = AVR_IO_REGBIT(PRR,PRUSART1),
129                 .name = '1',
130                 .r_udr = UDR1,
131
132                 .txen = AVR_IO_REGBIT(UCSR1B, TXEN1),
133                 .rxen = AVR_IO_REGBIT(UCSR1B, RXEN1),
134
135                 .r_ucsra = UCSR1A,
136                 .r_ucsrb = UCSR1B,
137                 .r_ucsrc = UCSR1C,
138                 .r_ubrrl = UBRR1L,
139                 .r_ubrrh = UBRR1H,
140                 .rxc = {
141                         .enable = AVR_IO_REGBIT(UCSR1B, RXCIE1),
142                         .raised = AVR_IO_REGBIT(UCSR1A, RXC1),
143                         .vector = USART1_RX_vect,
144                 },
145                 .txc = {
146                         .enable = AVR_IO_REGBIT(UCSR1B, TXCIE1),
147                         .raised = AVR_IO_REGBIT(UCSR1A, TXC1),
148                         .vector = USART1_TX_vect,
149                 },
150                 .udrc = {
151                         .enable = AVR_IO_REGBIT(UCSR1B, UDRIE1),
152                         .raised = AVR_IO_REGBIT(UCSR1A, UDRE1),
153                         .vector = USART1_UDRE_vect,
154                 },
155         },
156         .adc = {
157                 .r_admux = ADMUX,
158                 .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1),
159                                         AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3),
160                                         AVR_IO_REGBIT(ADMUX, MUX4),},
161                 .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1)},
162                 .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
163                 .r_adcsra = ADCSRA,
164                 .aden = AVR_IO_REGBIT(ADCSRA, ADEN),
165                 .adsc = AVR_IO_REGBIT(ADCSRA, ADSC),
166                 // no ADATE .adate = AVR_IO_REGBIT(ADCSRA, ADATE),
167                 .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),},
168
169                 .r_adch = ADCH,
170                 .r_adcl = ADCL,
171
172                 //.r_adcsrb = ADCSRB,
173                 // .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2),},
174
175                 .adc = {
176                         .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
177                         .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
178                         .vector = ADC_vect,
179                 },
180         },
181         .timer0 = {
182                 .name = '0',
183                 .wgm = { AVR_IO_REGBIT(TCCR0, WGM00), AVR_IO_REGBIT(TCCR0, WGM01) },
184                 .wgm_op = {
185                         [0] = AVR_TIMER_WGM_NORMAL8(),
186                         // PHASE CORRECT 
187                         [2] = AVR_TIMER_WGM_CTC(),
188                         [3] = AVR_TIMER_WGM_FASTPWM8(),
189                 },
190                 .cs = { AVR_IO_REGBIT(TCCR0, CS00), AVR_IO_REGBIT(TCCR0, CS01), AVR_IO_REGBIT(TCCR0, CS02) },
191                 //              .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
192                 .cs_div = { 0, 0, 3 /* 8 */, 5 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */},
193
194                 // asynchronous timer source bit.. if set, use 32khz frequency
195                 .as2 = AVR_IO_REGBIT(ASSR, AS0),
196                 
197                 .r_ocra = OCR0,
198                 .r_tcnt = TCNT0,
199
200                 .overflow = {
201                         .enable = AVR_IO_REGBIT(TIMSK, TOIE0),
202                         .raised = AVR_IO_REGBIT(TIFR, TOV0),
203                         .vector = TIMER0_OVF_vect,
204                 },
205                 .compa = {
206                         .enable = AVR_IO_REGBIT(TIMSK, OCIE0),
207                         .raised = AVR_IO_REGBIT(TIFR, OCF0),
208                         .vector = TIMER0_COMP_vect,
209                 },
210         },
211         .timer1 = {
212                 .name = '1',
213                 .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
214                                         AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
215                 .wgm_op = {
216                         [0] = AVR_TIMER_WGM_NORMAL16(),
217                         // TODO: 1 PWM phase corret 8bit
218                         //               2 PWM phase corret 9bit
219                         //       3 PWM phase corret 10bit
220                         [4] = AVR_TIMER_WGM_CTC(),
221                         [5] = AVR_TIMER_WGM_FASTPWM8(),
222                         [6] = AVR_TIMER_WGM_FASTPWM9(),
223                         [7] = AVR_TIMER_WGM_FASTPWM10(),
224                         // TODO: 8, 9 PWM phase and freq correct ICR & 10, 11
225                         [12] = AVR_TIMER_WGM_ICCTC(),
226                         [14] = AVR_TIMER_WGM_ICPWM(),
227                         [15] = AVR_TIMER_WGM_OCPWM(),
228                 },
229                 .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
230                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */  /* TODO: 2 External clocks */},
231
232                 .r_ocra = OCR1AL,
233                 .r_ocrb = OCR1BL,
234                 .r_ocrc = OCR1CL,
235                 .r_tcnt = TCNT1L,
236                 .r_icr = ICR1L,
237                 .r_icrh = ICR1H,
238                 .r_ocrah = OCR1AH,      // 16 bits timers have two bytes of it
239                 .r_ocrbh = OCR1BH,
240                 .r_ocrch = OCR1CH,
241                 .r_tcnth = TCNT1H,
242
243                 .overflow = {
244                         .enable = AVR_IO_REGBIT(TIMSK, TOIE1),
245                         .raised = AVR_IO_REGBIT(TIFR, TOV1),
246                         .vector = TIMER1_OVF_vect,
247                 },
248                 .compa = {
249                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1A),
250                         .raised = AVR_IO_REGBIT(TIFR, OCF1A),
251                         .vector = TIMER1_COMPA_vect,
252                 },
253                 .compb = {
254                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1B),
255                         .raised = AVR_IO_REGBIT(TIFR, OCF1B),
256                         .vector = TIMER1_COMPB_vect,
257                 },
258                 .compc = {
259                         .enable = AVR_IO_REGBIT(ETIMSK, OCIE1C),
260                         .raised = AVR_IO_REGBIT(ETIFR, OCF1C),
261                         .vector = TIMER1_COMPC_vect,
262                 },
263                 .icr = {
264                         .enable = AVR_IO_REGBIT(TIMSK, TICIE1),
265                         .raised = AVR_IO_REGBIT(TIFR, ICF1),
266                         .vector = TIMER1_CAPT_vect,
267                 },
268         },
269         .timer2 = {
270                 .name = '2',
271                 .wgm = { AVR_IO_REGBIT(TCCR2, WGM20), AVR_IO_REGBIT(TCCR2, WGM21) },
272                 .wgm_op = {
273                         [0] = AVR_TIMER_WGM_NORMAL8(),
274                         // TODO 1 pwm phase correct 
275                         [2] = AVR_TIMER_WGM_CTC(),
276                         [3] = AVR_TIMER_WGM_FASTPWM8(),
277                 },
278                 .cs = { AVR_IO_REGBIT(TCCR2, CS20), AVR_IO_REGBIT(TCCR2, CS21), AVR_IO_REGBIT(TCCR2, CS22) },
279                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* TODO external clock */ },
280
281                 .r_ocra = OCR2,
282                 .r_tcnt = TCNT2,
283                 
284                 .overflow = {
285                         .enable = AVR_IO_REGBIT(TIMSK, TOIE2),
286                         .raised = AVR_IO_REGBIT(TIFR, TOV2),
287                         .vector = TIMER2_OVF_vect,
288                 },
289                 .compa = {  // compa is just COMP
290                         .enable = AVR_IO_REGBIT(TIMSK, OCIE2),
291                         .raised = AVR_IO_REGBIT(TIFR, OCF2),
292                         .vector = TIMER2_COMP_vect,
293                 },
294         },
295         .timer3 = {
296                 .name = '3',
297                 .wgm = { AVR_IO_REGBIT(TCCR3A, WGM30), AVR_IO_REGBIT(TCCR3A, WGM31),
298                                         AVR_IO_REGBIT(TCCR3B, WGM32), AVR_IO_REGBIT(TCCR3B, WGM33) },
299                 .wgm_op = {
300                         [0] = AVR_TIMER_WGM_NORMAL16(),
301                         // TODO: 1 PWM phase corret 8bit
302                         //       2 PWM phase corret 9bit
303                         //       3 PWM phase corret 10bit
304                         [4] = AVR_TIMER_WGM_CTC(),
305                         [5] = AVR_TIMER_WGM_FASTPWM8(),
306                         [6] = AVR_TIMER_WGM_FASTPWM9(),
307                         [7] = AVR_TIMER_WGM_FASTPWM10(),
308                         // TODO: 8 PWM phase and freq corret ICR
309                         //       9 PWM phase and freq corret OCR
310                         //       10
311                         //       11
312                         [12] = AVR_TIMER_WGM_ICCTC(),
313                         [14] = AVR_TIMER_WGM_ICPWM(),
314                         [15] = AVR_TIMER_WGM_OCPWM(),
315                 },
316                 .cs = { AVR_IO_REGBIT(TCCR3B, CS30), AVR_IO_REGBIT(TCCR3B, CS31), AVR_IO_REGBIT(TCCR3B, CS32) },
317                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */  /* TODO: 2 External clocks */},
318
319                 .r_ocra = OCR3AL,
320                 .r_ocrb = OCR3BL,
321                 .r_ocrc = OCR3CL,
322                 .r_tcnt = TCNT3L,
323                 .r_icr = ICR3L,
324                 .r_icrh = ICR3H,
325                 .r_ocrah = OCR3AH,      // 16 bits timers have two bytes of it
326                 .r_ocrbh = OCR3BH,
327                 .r_ocrch = OCR3CH,
328                 .r_tcnth = TCNT3H,
329
330                 .overflow = {
331                         .enable = AVR_IO_REGBIT(ETIMSK, TOIE3),
332                         .raised = AVR_IO_REGBIT(ETIFR, TOV3),
333                         .vector = TIMER3_OVF_vect,
334                 },
335                 .compa = {
336                         .enable = AVR_IO_REGBIT(ETIMSK, OCIE3A),
337                         .raised = AVR_IO_REGBIT(ETIFR, OCF3A),
338                         .vector = TIMER3_COMPA_vect,
339                 },
340                 .compb = {
341                         .enable = AVR_IO_REGBIT(ETIMSK, OCIE3B),
342                         .raised = AVR_IO_REGBIT(ETIFR, OCF3B),
343                         .vector = TIMER3_COMPB_vect,
344                 },
345                 .compc = {
346                         .enable = AVR_IO_REGBIT(ETIMSK, OCIE3C),
347                         .raised = AVR_IO_REGBIT(ETIFR, OCF3C),
348                         .vector = TIMER3_COMPC_vect,
349                 },
350                 .icr = {
351                         .enable = AVR_IO_REGBIT(ETIMSK, TICIE3),
352                         .raised = AVR_IO_REGBIT(ETIFR, ICF3),
353                         .vector = TIMER3_CAPT_vect,
354                 },
355         },
356         .spi = {
357
358                 .r_spdr = SPDR,
359                 .r_spcr = SPCR,
360                 .r_spsr = SPSR,
361
362                 .spe = AVR_IO_REGBIT(SPCR, SPE),
363                 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
364
365                 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
366                 .spi = {
367                         .enable = AVR_IO_REGBIT(SPCR, SPIE),
368                         .raised = AVR_IO_REGBIT(SPSR, SPIF),
369                         .vector = SPI_STC_vect,
370                 },
371         },
372         
373         .twi = {
374
375                 .r_twcr = TWCR,
376                 .r_twsr = TWSR,
377                 .r_twbr = TWBR,
378                 .r_twdr = TWDR,
379                 .r_twar = TWAR,
380                 // no .r_twamr = TWAMR,
381
382                 .twen = AVR_IO_REGBIT(TWCR, TWEN),
383                 .twea = AVR_IO_REGBIT(TWCR, TWEA),
384                 .twsta = AVR_IO_REGBIT(TWCR, TWSTA),
385                 .twsto = AVR_IO_REGBIT(TWCR, TWSTO),
386                 .twwc = AVR_IO_REGBIT(TWCR, TWWC),
387
388                 .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f),       // 5 bits
389                 .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3),       // 2 bits
390
391                 .twi = {
392                         .enable = AVR_IO_REGBIT(TWCR, TWIE),
393                         .raised = AVR_IO_REGBIT(TWSR, TWINT),
394                         .vector = TWI_vect,
395                 },
396         },
397
398 };
399
400 static avr_t * make()
401 {
402         return &mcu_mega128.core;
403 }
404
405 avr_kind_t mega128 = {
406         .names = { "mega128", "mega128L" },
407         .make = make
408 };
409
410 void m128_init(struct avr_t * avr)
411 {
412         struct mcu_t * mcu = (struct mcu_t*)avr;
413
414         printf("%s init\n", avr->mmcu);
415         
416         avr_eeprom_init(avr, &mcu->eeprom);
417         avr_flash_init(avr, &mcu->selfprog);
418         avr_extint_init(avr, &mcu->extint);
419         avr_watchdog_init(avr, &mcu->watchdog);
420         avr_ioport_init(avr, &mcu->porta);
421         avr_ioport_init(avr, &mcu->portb);
422         avr_ioport_init(avr, &mcu->portc);
423         avr_ioport_init(avr, &mcu->portd);
424         avr_ioport_init(avr, &mcu->porte);
425         avr_ioport_init(avr, &mcu->portf);
426         avr_ioport_init(avr, &mcu->portg);
427         avr_uart_init(avr, &mcu->uart0);
428         avr_uart_init(avr, &mcu->uart1);
429         avr_adc_init(avr, &mcu->adc);
430         avr_timer_init(avr, &mcu->timer0);
431         avr_timer_init(avr, &mcu->timer1);
432         avr_timer_init(avr, &mcu->timer2);
433         avr_timer_init(avr, &mcu->timer3);
434         avr_spi_init(avr, &mcu->spi);
435         avr_twi_init(avr, &mcu->twi);
436 }
437
438 void m128_reset(struct avr_t * avr)
439 {
440 //      struct mcu_t * mcu = (struct mcu_t*)avr;
441 }