cores: Updated comparators
[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                                 .com = AVR_IO_REGBITS(TCCR0, COM00, 0x3),
213                                 .com_pin = AVR_IO_REGBIT(PORTB, PB4),
214                                 .interrupt = {
215                                         .enable = AVR_IO_REGBIT(TIMSK, OCIE0),
216                                         .raised = AVR_IO_REGBIT(TIFR, OCF0),
217                                         .vector = TIMER0_COMP_vect,
218                                 },
219                         },
220                 },
221         },
222         .timer1 = {
223                 .name = '1',
224                 .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
225                                         AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
226                 .wgm_op = {
227                         [0] = AVR_TIMER_WGM_NORMAL16(),
228                         // TODO: 1 PWM phase corret 8bit
229                         //               2 PWM phase corret 9bit
230                         //       3 PWM phase corret 10bit
231                         [4] = AVR_TIMER_WGM_CTC(),
232                         [5] = AVR_TIMER_WGM_FASTPWM8(),
233                         [6] = AVR_TIMER_WGM_FASTPWM9(),
234                         [7] = AVR_TIMER_WGM_FASTPWM10(),
235                         // TODO: 8, 9 PWM phase and freq correct ICR & 10, 11
236                         [12] = AVR_TIMER_WGM_ICCTC(),
237                         [14] = AVR_TIMER_WGM_ICPWM(),
238                         [15] = AVR_TIMER_WGM_OCPWM(),
239                 },
240                 .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
241                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */  /* TODO: 2 External clocks */},
242
243                 .r_tcnt = TCNT1L,
244                 .r_icr = ICR1L,
245                 .r_icrh = ICR1H,
246                 .r_tcnth = TCNT1H,
247
248                 .overflow = {
249                         .enable = AVR_IO_REGBIT(TIMSK, TOIE1),
250                         .raised = AVR_IO_REGBIT(TIFR, TOV1),
251                         .vector = TIMER1_OVF_vect,
252                 },
253                 .icr = {
254                         .enable = AVR_IO_REGBIT(TIMSK, TICIE1),
255                         .raised = AVR_IO_REGBIT(TIFR, ICF1),
256                         .vector = TIMER1_CAPT_vect,
257                 },
258                 .comp = {
259                         [AVR_TIMER_COMPA] = {
260                                 .r_ocr = OCR1AL,
261                                 .r_ocrh = OCR1AH,       // 16 bits timers have two bytes of it
262                                 .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3),
263                                 .com_pin = AVR_IO_REGBIT(PORTB, PB5),
264                                 .interrupt = {
265                                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1A),
266                                         .raised = AVR_IO_REGBIT(TIFR, OCF1A),
267                                         .vector = TIMER1_COMPA_vect,
268                                 },
269                         },
270                         [AVR_TIMER_COMPB] = {
271                                 .r_ocr = OCR1BL,
272                                 .r_ocrh = OCR1BH,
273                                 .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
274                                 .com_pin = AVR_IO_REGBIT(PORTB, PB6),
275                                 .interrupt = {
276                                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1B),
277                                         .raised = AVR_IO_REGBIT(TIFR, OCF1B),
278                                         .vector = TIMER1_COMPB_vect,
279                                 },
280                         },
281                         [AVR_TIMER_COMPC] = {
282                                 .r_ocr = OCR1CL,
283                                 .r_ocrh = OCR1CH,
284                                 .com = AVR_IO_REGBITS(TCCR1A, COM1C0, 0x3),
285                                 .com_pin = AVR_IO_REGBIT(PORTB, PB7), // same as timer2
286                                 .interrupt = {
287                                         .enable = AVR_IO_REGBIT(ETIMSK, OCIE1C),
288                                         .raised = AVR_IO_REGBIT(ETIFR, OCF1C),
289                                         .vector = TIMER1_COMPC_vect,
290                                 },
291                         },
292                 },
293
294         },
295         .timer2 = {
296                 .name = '2',
297                 .wgm = { AVR_IO_REGBIT(TCCR2, WGM20), AVR_IO_REGBIT(TCCR2, WGM21) },
298                 .wgm_op = {
299                         [0] = AVR_TIMER_WGM_NORMAL8(),
300                         // TODO 1 pwm phase correct 
301                         [2] = AVR_TIMER_WGM_CTC(),
302                         [3] = AVR_TIMER_WGM_FASTPWM8(),
303                 },
304                 .cs = { AVR_IO_REGBIT(TCCR2, CS20), AVR_IO_REGBIT(TCCR2, CS21), AVR_IO_REGBIT(TCCR2, CS22) },
305                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* TODO external clock */ },
306
307                 .r_tcnt = TCNT2,
308                 
309                 .overflow = {
310                         .enable = AVR_IO_REGBIT(TIMSK, TOIE2),
311                         .raised = AVR_IO_REGBIT(TIFR, TOV2),
312                         .vector = TIMER2_OVF_vect,
313                 },
314                 .comp = {
315                         [AVR_TIMER_COMPA] = {
316                                 .r_ocr = OCR2,
317                                 .com = AVR_IO_REGBITS(TCCR2, COM20, 0x3),
318                                 .com_pin = AVR_IO_REGBIT(PORTB, PB7), // same as timer1C
319                                 .interrupt = {
320                                         .enable = AVR_IO_REGBIT(TIMSK, OCIE2),
321                                         .raised = AVR_IO_REGBIT(TIFR, OCF2),
322                                         .vector = TIMER2_COMP_vect,
323                                 },
324                         },
325                 },
326         },
327         .timer3 = {
328                 .name = '3',
329                 .wgm = { AVR_IO_REGBIT(TCCR3A, WGM30), AVR_IO_REGBIT(TCCR3A, WGM31),
330                                         AVR_IO_REGBIT(TCCR3B, WGM32), AVR_IO_REGBIT(TCCR3B, WGM33) },
331                 .wgm_op = {
332                         [0] = AVR_TIMER_WGM_NORMAL16(),
333                         // TODO: 1 PWM phase corret 8bit
334                         //       2 PWM phase corret 9bit
335                         //       3 PWM phase corret 10bit
336                         [4] = AVR_TIMER_WGM_CTC(),
337                         [5] = AVR_TIMER_WGM_FASTPWM8(),
338                         [6] = AVR_TIMER_WGM_FASTPWM9(),
339                         [7] = AVR_TIMER_WGM_FASTPWM10(),
340                         // TODO: 8 PWM phase and freq corret ICR
341                         //       9 PWM phase and freq corret OCR
342                         //       10
343                         //       11
344                         [12] = AVR_TIMER_WGM_ICCTC(),
345                         [14] = AVR_TIMER_WGM_ICPWM(),
346                         [15] = AVR_TIMER_WGM_OCPWM(),
347                 },
348                 .cs = { AVR_IO_REGBIT(TCCR3B, CS30), AVR_IO_REGBIT(TCCR3B, CS31), AVR_IO_REGBIT(TCCR3B, CS32) },
349                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */  /* TODO: 2 External clocks */},
350
351                 .r_tcnt = TCNT3L,
352                 .r_icr = ICR3L,
353                 .r_icrh = ICR3H,
354                 .r_tcnth = TCNT3H,
355
356                 .overflow = {
357                         .enable = AVR_IO_REGBIT(ETIMSK, TOIE3),
358                         .raised = AVR_IO_REGBIT(ETIFR, TOV3),
359                         .vector = TIMER3_OVF_vect,
360                 },
361                 .comp = {
362                         [AVR_TIMER_COMPA] = {
363                                 .r_ocr = OCR3AL,
364                                 .r_ocrh = OCR3AH,       // 16 bits timers have two bytes of it
365                                 .com = AVR_IO_REGBITS(TCCR3A, COM3A0, 0x3),
366                                 .com_pin = AVR_IO_REGBIT(PORTE, PE3),
367                                 .interrupt = {
368                                         .enable = AVR_IO_REGBIT(ETIMSK, OCIE3A),
369                                         .raised = AVR_IO_REGBIT(ETIFR, OCF3A),
370                                         .vector = TIMER3_COMPA_vect,
371                                 }
372                         },
373                         [AVR_TIMER_COMPB] = {
374                                 .r_ocr = OCR3BL,
375                                 .r_ocrh = OCR3BH,
376                                 .com = AVR_IO_REGBITS(TCCR3A, COM3B0, 0x3),
377                                 .com_pin = AVR_IO_REGBIT(PORTE, PE4),
378                                 .interrupt = {
379                                         .enable = AVR_IO_REGBIT(ETIMSK, OCIE3B),
380                                         .raised = AVR_IO_REGBIT(ETIFR, OCF3B),
381                                         .vector = TIMER3_COMPB_vect,
382                                 }
383                         },
384                         [AVR_TIMER_COMPC] = {
385                                 .r_ocr = OCR3CL,
386                                 .r_ocrh = OCR3CH,
387                                 .com = AVR_IO_REGBITS(TCCR3A, COM3C0, 0x3),
388                                 .com_pin = AVR_IO_REGBIT(PORTE, PE5),
389                                 .interrupt = {
390                                         .enable = AVR_IO_REGBIT(ETIMSK, OCIE3C),
391                                         .raised = AVR_IO_REGBIT(ETIFR, OCF3C),
392                                         .vector = TIMER3_COMPC_vect,
393                                 }
394                         }
395                 },
396                 .icr = {
397                         .enable = AVR_IO_REGBIT(ETIMSK, TICIE3),
398                         .raised = AVR_IO_REGBIT(ETIFR, ICF3),
399                         .vector = TIMER3_CAPT_vect,
400                 },
401         },
402         .spi = {
403
404                 .r_spdr = SPDR,
405                 .r_spcr = SPCR,
406                 .r_spsr = SPSR,
407
408                 .spe = AVR_IO_REGBIT(SPCR, SPE),
409                 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
410
411                 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
412                 .spi = {
413                         .enable = AVR_IO_REGBIT(SPCR, SPIE),
414                         .raised = AVR_IO_REGBIT(SPSR, SPIF),
415                         .vector = SPI_STC_vect,
416                 },
417         },
418         
419         .twi = {
420
421                 .r_twcr = TWCR,
422                 .r_twsr = TWSR,
423                 .r_twbr = TWBR,
424                 .r_twdr = TWDR,
425                 .r_twar = TWAR,
426                 // no .r_twamr = TWAMR,
427
428                 .twen = AVR_IO_REGBIT(TWCR, TWEN),
429                 .twea = AVR_IO_REGBIT(TWCR, TWEA),
430                 .twsta = AVR_IO_REGBIT(TWCR, TWSTA),
431                 .twsto = AVR_IO_REGBIT(TWCR, TWSTO),
432                 .twwc = AVR_IO_REGBIT(TWCR, TWWC),
433
434                 .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f),       // 5 bits
435                 .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3),       // 2 bits
436
437                 .twi = {
438                         .enable = AVR_IO_REGBIT(TWCR, TWIE),
439                         .raised = AVR_IO_REGBIT(TWSR, TWINT),
440                         .vector = TWI_vect,
441                 },
442         },
443
444 };
445
446 static avr_t * make()
447 {
448         return &mcu_mega128.core;
449 }
450
451 avr_kind_t mega128 = {
452         .names = { "atmega128", "atmega128L" },
453         .make = make
454 };
455
456 void m128_init(struct avr_t * avr)
457 {
458         struct mcu_t * mcu = (struct mcu_t*)avr;
459
460         printf("%s init\n", avr->mmcu);
461         
462         avr_eeprom_init(avr, &mcu->eeprom);
463         avr_flash_init(avr, &mcu->selfprog);
464         avr_extint_init(avr, &mcu->extint);
465         avr_watchdog_init(avr, &mcu->watchdog);
466         avr_ioport_init(avr, &mcu->porta);
467         avr_ioport_init(avr, &mcu->portb);
468         avr_ioport_init(avr, &mcu->portc);
469         avr_ioport_init(avr, &mcu->portd);
470         avr_ioport_init(avr, &mcu->porte);
471         avr_ioport_init(avr, &mcu->portf);
472         avr_ioport_init(avr, &mcu->portg);
473         avr_uart_init(avr, &mcu->uart0);
474         avr_uart_init(avr, &mcu->uart1);
475         avr_adc_init(avr, &mcu->adc);
476         avr_timer_init(avr, &mcu->timer0);
477         avr_timer_init(avr, &mcu->timer1);
478         avr_timer_init(avr, &mcu->timer2);
479         avr_timer_init(avr, &mcu->timer3);
480         avr_spi_init(avr, &mcu->spi);
481         avr_twi_init(avr, &mcu->twi);
482 }
483
484 void m128_reset(struct avr_t * avr)
485 {
486 //      struct mcu_t * mcu = (struct mcu_t*)avr;
487 }