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