SimAVR
AVR Simulator
sim_megax.h
Go to the documentation of this file.
1 /*
2  sim_megax.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 #ifndef __SIM_MEGAX_H__
23 #define __SIM_MEGAX_H__
24 
25 #include "sim_core_declare.h"
26 #include "avr_eeprom.h"
27 #include "avr_flash.h"
28 #include "avr_watchdog.h"
29 #include "avr_extint.h"
30 #include "avr_ioport.h"
31 #include "avr_uart.h"
32 #include "avr_adc.h"
33 #include "avr_timer.h"
34 #include "avr_spi.h"
35 #include "avr_twi.h"
36 
37 void mx_init(struct avr_t * avr);
38 void mx_reset(struct avr_t * avr);
39 
40 /*
41  * This is a template for all of the 8/16/32 devices, hopefully
42  */
43 struct mcu_t {
44  avr_t core;
51  avr_adc_t adc;
53  avr_spi_t spi;
54  avr_twi_t twi;
55  // PORTA exists on m16 and 32, but not on 8.
56  // It is still necessary to declare this as otherwise
57  // the core_megax shared constructor will be confused
59 };
60 
61 #ifdef SIM_CORENAME
62 
63 #ifndef SIM_VECTOR_SIZE
64 #error SIM_VECTOR_SIZE is not declared
65 #endif
66 #ifndef SIM_MMCU
67 #error SIM_MMCU is not declared
68 #endif
69 
70 #ifndef EFUSE_DEFAULT
71 #define EFUSE_DEFAULT 0xff
72 #endif
73 #define EICRA MCUCR
74 #define EIMSK GICR
75 #define EIFR GIFR
76 
77 const struct mcu_t SIM_CORENAME = {
78  .core = {
79  .mmcu = SIM_MMCU,
81 
82  .init = mx_init,
83  .reset = mx_reset,
84  },
85  AVR_EEPROM_DECLARE_NOEEPM(EE_RDY_vect),
86  AVR_SELFPROG_DECLARE(SPMCR, SPMEN, SPM_RDY_vect),
88  .extint = {
89  AVR_EXTINT_DECLARE(0, 'D', PD2),
90  AVR_EXTINT_DECLARE(1, 'D', PD3),
91 #ifdef INT2
92  AVR_ASYNC_EXTINT_DECLARE(2, 'B', PB2),
93 #endif
94  },
95 #ifdef PORTA
96  AVR_IOPORT_DECLARE(a, 'A', A),
97 #endif
98  AVR_IOPORT_DECLARE(b, 'B', B),
99  AVR_IOPORT_DECLARE(c, 'C', C),
100  AVR_IOPORT_DECLARE(d, 'D', D),
101  .uart = {
102  // no PRUSART .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
103  .name = '0',
104  .r_udr = UDR,
105 
106  .txen = AVR_IO_REGBIT(UCSRB, TXEN),
107  .rxen = AVR_IO_REGBIT(UCSRB, RXEN),
108  .ucsz = AVR_IO_REGBITS(UCSRC, UCSZ0, 0x3), // 2 bits
109  .ucsz2 = AVR_IO_REGBIT(UCSRB, UCSZ2), // 1 bits
110 
111  .r_ucsra = UCSRA,
112  .r_ucsrb = UCSRB,
113  .r_ucsrc = UCSRC,
114  .r_ubrrl = UBRRL,
115  .r_ubrrh = UBRRH,
116  .rxc = {
117  .enable = AVR_IO_REGBIT(UCSRB, RXCIE),
118  .raised = AVR_IO_REGBIT(UCSRA, RXC),
119  .vector = USART_RXC_vect,
120  },
121  .txc = {
122  .enable = AVR_IO_REGBIT(UCSRB, TXCIE),
123  .raised = AVR_IO_REGBIT(UCSRA, TXC),
124  .vector = USART_TXC_vect,
125  },
126  .udrc = {
127  .enable = AVR_IO_REGBIT(UCSRB, UDRIE),
128  .raised = AVR_IO_REGBIT(UCSRA, UDRE),
129  .vector = USART_UDRE_vect,
130  },
131  },
132  .adc = {
133  .r_admux = ADMUX,
134  .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1),
135  AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3),},
136  .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1)},
137  .ref_values = { [1] = ADC_VREF_AVCC, [3] = ADC_VREF_V256 },
138 
139  .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
140  .r_adcsra = ADCSRA,
141  .aden = AVR_IO_REGBIT(ADCSRA, ADEN),
142  .adsc = AVR_IO_REGBIT(ADCSRA, ADSC),
143  // no ADATE .adate = AVR_IO_REGBIT(ADCSRA, ADATE),
144  .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),},
145 
146  .r_adch = ADCH,
147  .r_adcl = ADCL,
148 
149  //.r_adcsrb = ADCSRB,
150  // .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2),},
151 
152  .muxmode = {
153  [0] = AVR_ADC_SINGLE(0), [1] = AVR_ADC_SINGLE(1),
154  [2] = AVR_ADC_SINGLE(2), [3] = AVR_ADC_SINGLE(3),
155  [4] = AVR_ADC_SINGLE(4), [5] = AVR_ADC_SINGLE(5),
156  [6] = AVR_ADC_SINGLE(6), [7] = AVR_ADC_SINGLE(7),
157 
158  [14] = AVR_ADC_REF(1300), // 1.30V
159  [15] = AVR_ADC_REF(0), // GND
160  },
161 
162  .adc = {
163  .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
164  .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
165  .vector = ADC_vect,
166  },
167  },
168  .timer0 = {
169  .name = '0',
170  .wgm_op = {
171  [0] = AVR_TIMER_WGM_NORMAL8(),
172  // CTC etc. are missing because atmega8 does not support them on timer0
173  },
174  .cs = { AVR_IO_REGBIT(TCCR0, CS00), AVR_IO_REGBIT(TCCR0, CS01), AVR_IO_REGBIT(TCCR0, CS02) },
175  .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */},
176 
177  .r_tcnt = TCNT0,
178 
179  .overflow = {
180  .enable = AVR_IO_REGBIT(TIMSK, TOIE0),
181  .raised = AVR_IO_REGBIT(TIFR, TOV0),
182  .vector = TIMER0_OVF_vect,
183  },
184  // Compare Output Mode is missing for timer0 as atmega8 does not support it
185  },
186  .timer1 = {
187  .name = '1',
188  .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
189  AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
190  .wgm_op = {
191  [0] = AVR_TIMER_WGM_NORMAL16(),
192  // TODO: 1 PWM phase correct 8bit
193  // 2 PWM phase correct 9bit
194  // 3 PWM phase correct 10bit
195  [4] = AVR_TIMER_WGM_CTC(),
196  [5] = AVR_TIMER_WGM_FASTPWM8(),
197  [6] = AVR_TIMER_WGM_FASTPWM9(),
198  [7] = AVR_TIMER_WGM_FASTPWM10(),
199  // TODO: 8, 9 PWM phase and freq correct ICR & 10, 11
200  [12] = AVR_TIMER_WGM_ICCTC(),
201  [14] = AVR_TIMER_WGM_ICPWM(),
202  [15] = AVR_TIMER_WGM_OCPWM(),
203  },
204  .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
205  .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* TODO: 2 External clocks */},
206 
207  .r_tcnt = TCNT1L,
208  .r_icr = ICR1L,
209  .r_icrh = ICR1H,
210  .r_tcnth = TCNT1H,
211 
212  .ices = AVR_IO_REGBIT(TCCR1B, ICES1),
213  .icp = AVR_IO_REGBIT(ICP_PORT, ICP_PIN),
214 
215  .overflow = {
216  .enable = AVR_IO_REGBIT(TIMSK, TOIE1),
217  .raised = AVR_IO_REGBIT(TIFR, TOV1),
218  .vector = TIMER1_OVF_vect,
219  },
220  .icr = {
221  .enable = AVR_IO_REGBIT(TIMSK, TICIE1),
222  .raised = AVR_IO_REGBIT(TIFR, ICF1),
223  .vector = TIMER1_CAPT_vect,
224  },
225  .comp = {
226  [AVR_TIMER_COMPA] = {
227  .r_ocr = OCR1AL,
228  .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it
229  .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3),
230  .com_pin = AVR_IO_REGBIT(PORTB, PB5),
231  .interrupt = {
232  .enable = AVR_IO_REGBIT(TIMSK, OCIE1A),
233  .raised = AVR_IO_REGBIT(TIFR, OCF1A),
234  .vector = TIMER1_COMPA_vect,
235  },
236  },
237  [AVR_TIMER_COMPB] = {
238  .r_ocr = OCR1BL,
239  .r_ocrh = OCR1BH,
240  .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
241  .com_pin = AVR_IO_REGBIT(PORTB, PB6),
242  .interrupt = {
243  .enable = AVR_IO_REGBIT(TIMSK, OCIE1B),
244  .raised = AVR_IO_REGBIT(TIFR, OCF1B),
245  .vector = TIMER1_COMPB_vect,
246  },
247  },
248  },
249 
250  },
251  .timer2 = {
252  .name = '2',
253  .wgm = { AVR_IO_REGBIT(TCCR2, WGM20), AVR_IO_REGBIT(TCCR2, WGM21) },
254  .wgm_op = {
255  [0] = AVR_TIMER_WGM_NORMAL8(),
256  // TODO 1 pwm phase correct
257  [2] = AVR_TIMER_WGM_CTC(),
258  [3] = AVR_TIMER_WGM_FASTPWM8(),
259  },
260  .cs = { AVR_IO_REGBIT(TCCR2, CS20), AVR_IO_REGBIT(TCCR2, CS21), AVR_IO_REGBIT(TCCR2, CS22) },
261  .cs_div = { 0, 0, 3 /* 8 */, 4 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ /* TODO external clock */ },
262 
263  .r_tcnt = TCNT2,
264 
265  // asynchronous timer source bit.. if set, use 32khz frequency
266  .as2 = AVR_IO_REGBIT(ASSR, AS2),
267 
268  .overflow = {
269  .enable = AVR_IO_REGBIT(TIMSK, TOIE2),
270  .raised = AVR_IO_REGBIT(TIFR, TOV2),
271  .vector = TIMER2_OVF_vect,
272  },
273  .comp = {
274  [AVR_TIMER_COMPA] = {
275  .r_ocr = OCR2,
276  .com = AVR_IO_REGBITS(TCCR2, COM20, 0x3),
277  .com_pin = AVR_IO_REGBIT(PORTB, PB7), // same as timer1C
278  .interrupt = {
279  .enable = AVR_IO_REGBIT(TIMSK, OCIE2),
280  .raised = AVR_IO_REGBIT(TIFR, OCF2),
281  .vector = TIMER2_COMP_vect,
282  },
283  },
284  },
285  },
286  AVR_SPI_DECLARE(0, 0),
287  .twi = {
288 
289  .r_twcr = TWCR,
290  .r_twsr = TWSR,
291  .r_twbr = TWBR,
292  .r_twdr = TWDR,
293  .r_twar = TWAR,
294  // no .r_twamr = TWAMR,
295 
296  .twen = AVR_IO_REGBIT(TWCR, TWEN),
297  .twea = AVR_IO_REGBIT(TWCR, TWEA),
298  .twsta = AVR_IO_REGBIT(TWCR, TWSTA),
299  .twsto = AVR_IO_REGBIT(TWCR, TWSTO),
300  .twwc = AVR_IO_REGBIT(TWCR, TWWC),
301 
302  .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f), // 5 bits
303  .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3), // 2 bits
304 
305  .twi = {
306  .enable = AVR_IO_REGBIT(TWCR, TWIE),
307  .raised = AVR_IO_REGBIT(TWCR, TWINT),
308  .raise_sticky = 1,
309  .vector = TWI_vect,
310  },
311  },
312 
313 };
314 
315 #endif /* SIM_CORENAME */
316 
317 #endif /* __SIM_MEGAX_H__ */
Definition: avr_adc.h:81
#define PB5
Definition: sim_mega128rfr2.c:49
#define AVR_TIMER_WGM_NORMAL8()
Definition: avr_timer.h:87
Definition: avr_watchdog.h:35
avr_eeprom_t eeprom
Definition: sim_90usb162.c:42
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
void mx_reset(struct avr_t *avr)
Definition: sim_megax.c:50
#define PD3
Definition: sim_mega128rfr2.c:57
avr_ioport_t portb
Definition: sim_90usb162.c:46
#define AVR_ADC_REF(_t)
Definition: avr_adc.h:166
#define ICP_PORT
Definition: sim_mega16.c:30
avr_t * avr
Definition: run_avr.c:54
#define AVR_ADC_SINGLE(_chan)
Definition: avr_adc.h:154
#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_WATCHDOG_DECLARE_128(_WDSR, _vec)
Definition: avr_watchdog.h:82
#define AVR_TIMER_WGM_ICCTC()
Definition: avr_timer.h:90
#define PB7
Definition: sim_mega128rfr2.c:51
avr_timer_t timer2
Definition: sim_mega128.c:54
#define AVR_EEPROM_DECLARE_NOEEPM(_vector)
no EEPM registers in atmega128
Definition: avr_eeprom.h:88
#define AVR_IOPORT_DECLARE(_lname, _cname, _uname)
Definition: avr_ioport.h:119
#define AVR_SELFPROG_DECLARE(_spmr, _spen, _vector)
Definition: avr_flash.h:82
#define AVR_TIMER_WGM_FASTPWM9()
Definition: avr_timer.h:92
#define ICP_PIN
Definition: sim_mega16.c:31
Definition: avr_adc.h:83
#define PD2
Definition: sim_mega128rfr2.c:56
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
#define AVR_ASYNC_EXTINT_DECLARE(_index, _portname, _portpin)
Asynchronous External Interrupt, for example INT2 on the m16 and m32 Uses only 1 interrupt sense cont...
Definition: avr_extint.h:91
avr_watchdog_t watchdog
Definition: sim_90usb162.c:44
Definition: sim_90usb162.c:40
#define PB6
Definition: sim_mega128rfr2.c:50
#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
#define D(_w)
Definition: avr_ioport.c:24
avr_t core
Definition: sim_90usb162.c:41
#define _VECTOR(v)
Definition: sim_core_declare.h:35
#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
avr_ioport_t porta
Definition: sim_mega128.c:51
void mx_init(struct avr_t *avr)
Definition: sim_megax.c:27
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
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
#define SIM_VECTOR_SIZE
Definition: sim_mega16.c:26
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 for an IO port.
Definition: avr_ioport.h:97