SimAVR
AVR Simulator
sim_megax8.h
Go to the documentation of this file.
1 /*
2  sim_megax8.h
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 
23 #ifndef __SIM_MEGAX8_H__
24 #define __SIM_MEGAX8_H__
25 
26 #include "sim_core_declare.h"
27 #include "avr_eeprom.h"
28 #include "avr_flash.h"
29 #include "avr_watchdog.h"
30 #include "avr_extint.h"
31 #include "avr_ioport.h"
32 #include "avr_uart.h"
33 #include "avr_adc.h"
34 #include "avr_timer.h"
35 #include "avr_spi.h"
36 #include "avr_twi.h"
37 
38 void mx8_init(struct avr_t * avr);
39 void mx8_reset(struct avr_t * avr);
40 
41 /*
42  * This is a template for all of the x8 devices, hopefully
43  */
44 struct mcu_t {
45  avr_t core;
52  avr_adc_t adc;
54  avr_spi_t spi;
55  avr_twi_t twi;
56 };
57 
58 #ifdef SIM_CORENAME
59 
60 #ifndef SIM_VECTOR_SIZE
61 #error SIM_VECTOR_SIZE is not declared
62 #endif
63 #ifndef SIM_MMCU
64 #error SIM_MMCU is not declared
65 #endif
66 
67 /* Termporary hack for mega 324 due to mangled headers */
68 #ifdef _AVR_IOM328P_H_
69 #undef EFUSE_DEFAULT
70 #define EFUSE_DEFAULT 0
71 #endif
72 
73 const struct mcu_t SIM_CORENAME = {
74  .core = {
75  .mmcu = SIM_MMCU,
77 
78  .init = mx8_init,
79  .reset = mx8_reset,
80  },
81  AVR_EEPROM_DECLARE(EE_READY_vect),
82 #ifdef RWWSRE
83  AVR_SELFPROG_DECLARE(SPMCSR, SELFPRGEN, SPM_READY_vect),
84 #else
85  AVR_SELFPROG_DECLARE_NORWW(SPMCSR, SELFPRGEN, SPM_READY_vect),
86 #endif
87  AVR_WATCHDOG_DECLARE(WDTCSR, WDT_vect),
88  .extint = {
89  AVR_EXTINT_DECLARE(0, 'D', 2),
90  AVR_EXTINT_DECLARE(1, 'D', 3),
91  },
92  .portb = {
93  .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
94  .pcint = {
95  .enable = AVR_IO_REGBIT(PCICR, PCIE0),
96  .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
97  .vector = PCINT0_vect,
98  },
99  .r_pcint = PCMSK0,
100  },
101  .portc = {
102  .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
103  .pcint = {
104  .enable = AVR_IO_REGBIT(PCICR, PCIE1),
105  .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
106  .vector = PCINT1_vect,
107  },
108  .r_pcint = PCMSK1,
109  },
110  .portd = {
111  .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
112  .pcint = {
113  .enable = AVR_IO_REGBIT(PCICR, PCIE2),
114  .raised = AVR_IO_REGBIT(PCIFR, PCIF2),
115  .vector = PCINT2_vect,
116  },
117  .r_pcint = PCMSK2,
118  },
119 
120  .uart = {
121  .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
122  .name = '0',
123  .r_udr = UDR0,
124 
125  .txen = AVR_IO_REGBIT(UCSR0B, TXEN0),
126  .rxen = AVR_IO_REGBIT(UCSR0B, RXEN0),
127  .usbs = AVR_IO_REGBIT(UCSR0C, USBS0),
128  .ucsz = AVR_IO_REGBITS(UCSR0C, UCSZ00, 0x3), // 2 bits
129  .ucsz2 = AVR_IO_REGBIT(UCSR0B, UCSZ02), // 1 bits
130 
131  .r_ucsra = UCSR0A,
132  .r_ucsrb = UCSR0B,
133  .r_ucsrc = UCSR0C,
134  .r_ubrrl = UBRR0L,
135  .r_ubrrh = UBRR0H,
136  .rxc = {
137  .enable = AVR_IO_REGBIT(UCSR0B, RXCIE0),
138  .raised = AVR_IO_REGBIT(UCSR0A, RXC0),
139  .vector = USART_RX_vect,
140  },
141  .txc = {
142  .enable = AVR_IO_REGBIT(UCSR0B, TXCIE0),
143  .raised = AVR_IO_REGBIT(UCSR0A, TXC0),
144  .vector = USART_TX_vect,
145  },
146  .udrc = {
147  .enable = AVR_IO_REGBIT(UCSR0B, UDRIE0),
148  .raised = AVR_IO_REGBIT(UCSR0A, UDRE0),
149  .vector = USART_UDRE_vect,
150  },
151  },
152  .adc = {
153  .r_admux = ADMUX,
154  .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1),
155  AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3),},
156  .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1)},
157  .ref_values = { [1] = ADC_VREF_AVCC, [3] = ADC_VREF_V110, },
158 
159  .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
160  .r_adcsra = ADCSRA,
161  .aden = AVR_IO_REGBIT(ADCSRA, ADEN),
162  .adsc = AVR_IO_REGBIT(ADCSRA, ADSC),
163  .adate = AVR_IO_REGBIT(ADCSRA, ADATE),
164  .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),},
165 
166  .r_adch = ADCH,
167  .r_adcl = ADCL,
168 
169  .r_adcsrb = ADCSRB,
170  .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2),},
171  .adts_op = {
172  [0] = avr_adts_free_running,
180  },
181 
182  .muxmode = {
183  [0] = AVR_ADC_SINGLE(0), [1] = AVR_ADC_SINGLE(1),
184  [2] = AVR_ADC_SINGLE(2), [3] = AVR_ADC_SINGLE(3),
185  [4] = AVR_ADC_SINGLE(4), [5] = AVR_ADC_SINGLE(5),
186  [6] = AVR_ADC_SINGLE(6), [7] = AVR_ADC_TEMP(),
187  [14] = AVR_ADC_REF(1100), // 1.1V
188  [15] = AVR_ADC_REF(0), // GND
189  },
190  .adc = {
191  .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
192  .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
193  .vector = ADC_vect,
194  },
195  },
196  .timer0 = {
197  .name = '0',
198  .disabled = AVR_IO_REGBIT(PRR,PRTIM0),
199  .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
200  .wgm_op = {
201  [0] = AVR_TIMER_WGM_NORMAL8(),
202  [2] = AVR_TIMER_WGM_CTC(),
203  [3] = AVR_TIMER_WGM_FASTPWM8(),
204  [7] = AVR_TIMER_WGM_OCPWM(),
205  },
206  .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
207  .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
208 
209  .r_tcnt = TCNT0,
210 
211  .overflow = {
212  .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
213  .raised = AVR_IO_REGBIT(TIFR0, TOV0),
214  .vector = TIMER0_OVF_vect,
215  },
216  .comp = {
217  [AVR_TIMER_COMPA] = {
218  .r_ocr = OCR0A,
219  .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3),
220  .com_pin = AVR_IO_REGBIT(PORTD, 6),
221  .interrupt = {
222  .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
223  .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
224  .vector = TIMER0_COMPA_vect,
225  },
226  },
227  [AVR_TIMER_COMPB] = {
228  .r_ocr = OCR0B,
229  .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3),
230  .com_pin = AVR_IO_REGBIT(PORTD, 5),
231  .interrupt = {
232  .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
233  .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
234  .vector = TIMER0_COMPB_vect,
235  }
236  }
237  }
238  },
239  .timer1 = {
240  .name = '1',
241  .disabled = AVR_IO_REGBIT(PRR,PRTIM1),
242  .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
243  AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
244  .wgm_op = {
245  [0] = AVR_TIMER_WGM_NORMAL16(),
246  [4] = AVR_TIMER_WGM_CTC(),
247  [5] = AVR_TIMER_WGM_FASTPWM8(),
248  [6] = AVR_TIMER_WGM_FASTPWM9(),
249  [7] = AVR_TIMER_WGM_FASTPWM10(),
250  [8] = AVR_TIMER_WGM_ICPWM(),
251  [9] = AVR_TIMER_WGM_OCPWM(),
252  [12] = AVR_TIMER_WGM_ICCTC(),
253  [14] = AVR_TIMER_WGM_ICPWM(),
254  [15] = AVR_TIMER_WGM_OCPWM(),
255  },
256  .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
257  .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* External clock T1 is not handled */},
258 
259  .r_tcnt = TCNT1L,
260  .r_tcnth = TCNT1H,
261  .r_icr = ICR1L,
262  .r_icrh = ICR1H,
263 
264  .ices = AVR_IO_REGBIT(TCCR1B, ICES1),
265  .icp = AVR_IO_REGBIT(PORTB, 0),
266 
267  .overflow = {
268  .enable = AVR_IO_REGBIT(TIMSK1, TOIE1),
269  .raised = AVR_IO_REGBIT(TIFR1, TOV1),
270  .vector = TIMER1_OVF_vect,
271  },
272  .icr = {
273  .enable = AVR_IO_REGBIT(TIMSK1, ICIE1),
274  .raised = AVR_IO_REGBIT(TIFR1, ICF1),
275  .vector = TIMER1_CAPT_vect,
276  },
277  .comp = {
278  [AVR_TIMER_COMPA] = {
279  .r_ocr = OCR1AL,
280  .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it
281  .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3),
282  .com_pin = AVR_IO_REGBIT(PORTB, 1),
283  .interrupt = {
284  .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A),
285  .raised = AVR_IO_REGBIT(TIFR1, OCF1A),
286  .vector = TIMER1_COMPA_vect,
287  },
288  },
289  [AVR_TIMER_COMPB] = {
290  .r_ocr = OCR1BL,
291  .r_ocrh = OCR1BH,
292  .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
293  .com_pin = AVR_IO_REGBIT(PORTB, 2),
294  .interrupt = {
295  .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B),
296  .raised = AVR_IO_REGBIT(TIFR1, OCF1B),
297  .vector = TIMER1_COMPB_vect,
298  },
299  },
300  },
301  },
302  .timer2 = {
303  .name = '2',
304  .disabled = AVR_IO_REGBIT(PRR,PRTIM2),
305  .wgm = { AVR_IO_REGBIT(TCCR2A, WGM20), AVR_IO_REGBIT(TCCR2A, WGM21), AVR_IO_REGBIT(TCCR2B, WGM22) },
306  .wgm_op = {
307  [0] = AVR_TIMER_WGM_NORMAL8(),
308  [2] = AVR_TIMER_WGM_CTC(),
309  [3] = AVR_TIMER_WGM_FASTPWM8(),
310  [7] = AVR_TIMER_WGM_OCPWM(),
311  },
312 
313  .cs = { AVR_IO_REGBIT(TCCR2B, CS20), AVR_IO_REGBIT(TCCR2B, CS21), AVR_IO_REGBIT(TCCR2B, CS22) },
314  .cs_div = { 0, 0, 3 /* 8 */, 5 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ },
315 
316  .r_tcnt = TCNT2,
317 
318  // asynchronous timer source bit.. if set, use 32khz frequency
319  .as2 = AVR_IO_REGBIT(ASSR, AS2),
320 
321  .overflow = {
322  .enable = AVR_IO_REGBIT(TIMSK2, TOIE2),
323  .raised = AVR_IO_REGBIT(TIFR2, TOV2),
324  .vector = TIMER2_OVF_vect,
325  },
326  .comp = {
327  [AVR_TIMER_COMPA] = {
328  .r_ocr = OCR2A,
329  .com = AVR_IO_REGBITS(TCCR2A, COM2A0, 0x3),
330  .com_pin = AVR_IO_REGBIT(PORTB, 3),
331  .interrupt = {
332  .enable = AVR_IO_REGBIT(TIMSK2, OCIE2A),
333  .raised = AVR_IO_REGBIT(TIFR2, OCF2A),
334  .vector = TIMER2_COMPA_vect,
335  }
336  },
337  [AVR_TIMER_COMPB] = {
338  .r_ocr = OCR2B,
339  .com = AVR_IO_REGBITS(TCCR2A, COM2B0, 0x3),
340  .com_pin = AVR_IO_REGBIT(PORTD, 3),
341  .interrupt = {
342  .enable = AVR_IO_REGBIT(TIMSK2, OCIE2B),
343  .raised = AVR_IO_REGBIT(TIFR2, OCF2B),
344  .vector = TIMER2_COMPB_vect,
345  }
346  }
347  }
348  },
349  AVR_SPI_DECLARE(PRR, PRSPI),
350  .twi = {
351  .disabled = AVR_IO_REGBIT(PRR,PRTWI),
352 
353  .r_twcr = TWCR,
354  .r_twsr = TWSR,
355  .r_twbr = TWBR,
356  .r_twdr = TWDR,
357  .r_twar = TWAR,
358  .r_twamr = TWAMR,
359 
360  .twen = AVR_IO_REGBIT(TWCR, TWEN),
361  .twea = AVR_IO_REGBIT(TWCR, TWEA),
362  .twsta = AVR_IO_REGBIT(TWCR, TWSTA),
363  .twsto = AVR_IO_REGBIT(TWCR, TWSTO),
364  .twwc = AVR_IO_REGBIT(TWCR, TWWC),
365 
366  .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f), // 5 bits
367  .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3), // 2 bits
368 
369  .twi = {
370  .enable = AVR_IO_REGBIT(TWCR, TWIE),
371  .raised = AVR_IO_REGBIT(TWCR, TWINT),
372  .raise_sticky = 1,
373  .vector = TWI_vect,
374  },
375  },
376 
377 };
378 #endif /* SIM_CORENAME */
379 
380 #endif /* __SIM_MEGAX8_H__ */
Definition: avr_adc.h:81
#define AVR_TIMER_WGM_NORMAL8()
Definition: avr_timer.h:87
Definition: avr_watchdog.h:35
avr_eeprom_t eeprom
Definition: sim_90usb162.c:42
void mx8_init(struct avr_t *avr)
Definition: sim_megax8.c:26
Handles self-programming subsystem if the core supports it.
Definition: avr_flash.h:38
#define SIM_MMCU
Definition: sim_mega324.c:24
#define SIM_CORENAME
Definition: sim_mega324.c:25
avr_ioport_t portb
Definition: sim_90usb162.c:46
#define AVR_ADC_REF(_t)
Definition: avr_adc.h:166
Definition: avr_adc.h:90
avr_t * avr
Definition: run_avr.c:54
#define AVR_ADC_SINGLE(_chan)
Definition: avr_adc.h:154
Definition: avr_adc.h:91
#define AVR_TIMER_WGM_FASTPWM8()
Definition: avr_timer.h:91
#define AVR_TIMER_WGM_FASTPWM10()
Definition: avr_timer.h:93
avr_uart_t uart
Definition: sim_megax.h:50
#define AVR_SELFPROG_DECLARE_NORWW(_spmr, _spen, _vector)
Definition: avr_flash.h:76
Definition: avr_adc.h:95
#define AVR_ADC_TEMP()
Definition: avr_adc.h:171
#define AVR_TIMER_WGM_ICCTC()
Definition: avr_timer.h:90
avr_timer_t timer2
Definition: sim_mega128.c:54
#define AVR_SELFPROG_DECLARE(_spmr, _spen, _vector)
Definition: avr_flash.h:82
#define AVR_TIMER_WGM_FASTPWM9()
Definition: avr_timer.h:92
This module is just a "relay" for the pin change IRQ in the IO port module.
Definition: avr_extint.h:56
avr_adc_t adc
Definition: sim_mega128.c:53
#define AVR_SPI_DECLARE(_prr, _prspi)
Definition: avr_spi.h:66
#define AVR_TIMER_WGM_CTC()
Definition: avr_timer.h:89
avr_watchdog_t watchdog
Definition: sim_90usb162.c:44
Definition: sim_90usb162.c:40
Definition: avr_adc.h:99
#define AVR_EXTINT_DECLARE(_index, _portname, _portpin)
Declares a typical INT into a avr_extint_t in a core.
Definition: avr_extint.h:77
Definition: avr_eeprom.h:35
Definition: avr_spi.h:45
void mx8_reset(struct avr_t *avr)
Definition: sim_megax8.c:46
avr_t core
Definition: sim_90usb162.c:41
#define DEFAULT_CORE(_vector_size)
Definition: sim_core_declare.h:52
Definition: avr_twi.h:74
Definition: avr_timer.h:37
Definition: avr_timer.h:111
avr_timer_t timer0
Definition: sim_90usb162.c:48
avr_ioport_t portd
Definition: sim_90usb162.c:46
Definition: avr_uart.h:91
#define AVR_IO_REGBITS(_io, _bit, _mask)
Definition: sim_regbit.h:153
#define AVR_EEPROM_DECLARE(_vector)
The eeprom block seems to be very similar across AVRs, so here is a macro to declare a "typical" one ...
Definition: avr_eeprom.h:70
avr_timer_t timer1
Definition: sim_90usb162.c:48
Definition: avr_adc.h:108
#define AVR_TIMER_WGM_ICPWM()
Definition: avr_timer.h:98
#define AVR_TIMER_WGM_OCPWM()
Definition: avr_timer.h:97
Definition: avr_adc.h:98
Definition: avr_adc.h:101
avr_ioport_t portc
Definition: sim_90usb162.c:46
avr_twi_t twi
Definition: sim_mega128.c:56
avr_flash_t selfprog
Definition: sim_90usb162.c:43
#define AVR_TIMER_WGM_NORMAL16()
Definition: avr_timer.h:88
Definition: avr_timer.h:38
Definition: avr_adc.h:100
#define SIM_VECTOR_SIZE
Definition: sim_mega16.c:26
#define AVR_WATCHDOG_DECLARE(_WDSR, _vec)
This helps declare a watchdog block into a core.
Definition: avr_watchdog.h:66
Definition: avr_adc.h:96
Main AVR instance.
Definition: sim_avr.h:142
avr_spi_t spi
Definition: sim_90usb162.c:49
avr_extint_t extint
Definition: sim_90usb162.c:45
#define AVR_IO_REGBIT(_io, _bit)
Definition: sim_regbit.h:150
Definition: avr_adc.h:82
Definition for an IO port.
Definition: avr_ioport.h:97