SimAVR
AVR Simulator
sim_megaxm1.h
Go to the documentation of this file.
1 /*
2  sim_megaxm1.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_lin.h"
33 #include "avr_lin.h"
34 #include "avr_adc.h"
35 #include "avr_timer.h"
36 #include "avr_spi.h"
37 
38 void mxm1_init(struct avr_t * avr);
39 void mxm1_reset(struct avr_t * avr);
40 
41 /*
42  * This is a template for all of the xm1 devices, hopefully
43  */
44 struct mcu_t {
45  avr_t core;
52  avr_adc_t adc;
54  avr_spi_t spi;
55 };
56 
57 #ifdef SIM_CORENAME
58 
59 #ifndef SIM_VECTOR_SIZE
60 #error SIM_VECTOR_SIZE is not declared
61 #endif
62 #ifndef SIM_MMCU
63 #error SIM_MMCU is not declared
64 #endif
65 
66 const struct mcu_t SIM_CORENAME = {
67  .core = {
68  .mmcu = SIM_MMCU,
70 
71  .init = mxm1_init,
72  .reset = mxm1_reset,
73  },
74  AVR_EEPROM_DECLARE_NOEEPM(EE_READY_vect),
75  AVR_SELFPROG_DECLARE(SPMCSR, SPMEN, SPM_READY_vect),
76  AVR_WATCHDOG_DECLARE(WDTCSR, WDT_vect),
77  .extint = {
78  AVR_EXTINT_DECLARE(0, 'D', 6),
79  AVR_EXTINT_DECLARE(1, 'B', 2),
80  AVR_EXTINT_DECLARE(2, 'B', 5),
81  AVR_EXTINT_DECLARE(3, 'C', 0),
82  },
83  .portb = {
84  .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
85  .pcint = {
86  .enable = AVR_IO_REGBIT(PCICR, PCIE0),
87  .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
88  .vector = PCINT0_vect,
89  },
90  .r_pcint = PCMSK0,
91  },
92  .portc = {
93  .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
94  .pcint = {
95  .enable = AVR_IO_REGBIT(PCICR, PCIE1),
96  .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
97  .vector = PCINT1_vect,
98  },
99  .r_pcint = PCMSK1,
100  },
101  .portd = {
102  .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
103  .pcint = {
104  .enable = AVR_IO_REGBIT(PCICR, PCIE2),
105  .raised = AVR_IO_REGBIT(PCIFR, PCIF2),
106  .vector = PCINT2_vect,
107  },
108  .r_pcint = PCMSK2,
109  },
110  .porte = {
111  .name = 'E', .r_port = PORTE, .r_ddr = DDRE, .r_pin = PINE,
112  .pcint = {
113  .enable = AVR_IO_REGBIT(PCICR, PCIE2),
114  .raised = AVR_IO_REGBIT(PCIFR, PCIF2),
115  .vector = PCINT2_vect,
116  },
117  .r_pcint = PCMSK3,
118  },
119 
120  .lin = {
121  .r_linbtr = LINBTR,
122  .r_linbrrh = LINBRRH,
123  .r_linbrrl = LINBRRL,
124 
125  .lena = AVR_IO_REGBIT( LINCR, LENA),
126  .ldisr = AVR_IO_REGBIT( LINBTR, LDISR),
127  .lbt = AVR_IO_REGBITS( LINBTR, LBT0, 0x3F), // 5 bits
128 
129  .uart = {
130  .name = '0',
131  .r_udr = LINDAT,
132 
133  .txen = AVR_IO_REGBIT(LINCR, LCMD0),
134  .rxen = AVR_IO_REGBIT(LINCR, LCMD1),
135 
136  // note that control and BAUD calculation is handled via LIN regs above
137  .r_ucsra = 0,
138  .r_ucsrb = 0,
139  .r_ucsrc = 0,
140  .r_ubrrl = 0,
141  .r_ubrrh = 0,
142 
143  .rxc = {
144  .enable = AVR_IO_REGBIT(LINENIR, LENRXOK),
145  .raised = AVR_IO_REGBIT(LINSIR, LRXOK),
146  .vector = LIN_TC_vect,
147  },
148  .txc = {
149  .enable = AVR_IO_REGBIT(LINENIR, LENTXOK),
150  .raised = AVR_IO_REGBIT(LINSIR, LTXOK),
151  .vector = LIN_TC_vect,
152  },
153  /* .udrc doesn't exist in the LIN UART */
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),AVR_IO_REGBIT(ADMUX, MUX4)},
160  .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1)},
161  .ref_values = { [1] = ADC_VREF_AVCC, [3] = ADC_VREF_V256, },
162 
163  .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
164  .r_adcsra = ADCSRA,
165  .aden = AVR_IO_REGBIT(ADCSRA, ADEN),
166  .adsc = AVR_IO_REGBIT(ADCSRA, ADSC),
167  .adate = AVR_IO_REGBIT(ADCSRA, ADATE),
168  .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),},
169 
170  .r_adch = ADCH,
171  .r_adcl = ADCL,
172 
173  .r_adcsrb = ADCSRB,
174  .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2), AVR_IO_REGBIT(ADCSRB, ADTS3),},
175  .adts_op = {
176  [0] = avr_adts_free_running,
190  },
191 
192  .muxmode = {
193  [0] = AVR_ADC_SINGLE(0), [1] = AVR_ADC_SINGLE(1),
194  [2] = AVR_ADC_SINGLE(2), [3] = AVR_ADC_SINGLE(3),
195  [4] = AVR_ADC_SINGLE(4), [5] = AVR_ADC_SINGLE(5),
196  [6] = AVR_ADC_SINGLE(6), [7] = AVR_ADC_SINGLE(7),
197  [8] = AVR_ADC_SINGLE(8), [9] = AVR_ADC_SINGLE(9),
198  [10] = AVR_ADC_SINGLE(10), [11] = AVR_ADC_TEMP(),
199  // AMP0/1/2 is missing, no clue what to do ...
200  [17] = AVR_ADC_REF(2560), // 1.1V
201  [18] = AVR_ADC_REF(0), // GND
202  },
203  .adc = {
204  .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
205  .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
206  .vector = ADC_vect,
207  },
208  },
209  .timer0 = {
210  .name = '0',
211  .disabled = AVR_IO_REGBIT(PRR,PRTIM0),
212  .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
213  .wgm_op = {
214  [0] = AVR_TIMER_WGM_NORMAL8(),
215  [2] = AVR_TIMER_WGM_CTC(),
216  [3] = AVR_TIMER_WGM_FASTPWM8(),
217  [7] = AVR_TIMER_WGM_OCPWM(),
218  },
219  .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
220  .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
221 
222  .r_tcnt = TCNT0,
223 
224  .overflow = {
225  .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
226  .raised = AVR_IO_REGBIT(TIFR0, TOV0),
227  .vector = TIMER0_OVF_vect,
228  },
229  .comp = {
230  [AVR_TIMER_COMPA] = {
231  .r_ocr = OCR0A,
232  .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3),
233  .com_pin = AVR_IO_REGBIT(PORTD, 3),
234  .interrupt = {
235  .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
236  .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
237  .vector = TIMER0_COMPA_vect,
238  },
239  },
240  [AVR_TIMER_COMPB] = {
241  .r_ocr = OCR0B,
242  .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3),
243  .com_pin = AVR_IO_REGBIT(PORTE, 1),
244  .interrupt = {
245  .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
246  .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
247  .vector = TIMER0_COMPB_vect,
248  }
249  }
250  }
251  },
252  .timer1 = {
253  .name = '1',
254  .disabled = AVR_IO_REGBIT(PRR,PRTIM1),
255  .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
256  AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
257  .wgm_op = {
258  [0] = AVR_TIMER_WGM_NORMAL16(),
259  [4] = AVR_TIMER_WGM_CTC(),
260  [5] = AVR_TIMER_WGM_FASTPWM8(),
261  [6] = AVR_TIMER_WGM_FASTPWM9(),
262  [7] = AVR_TIMER_WGM_FASTPWM10(),
263  [12] = AVR_TIMER_WGM_ICCTC(),
264  [14] = AVR_TIMER_WGM_ICPWM(),
265  [15] = AVR_TIMER_WGM_OCPWM(),
266  },
267  .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
268  .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* External clock T1 is not handled */},
269 
270  .r_tcnt = TCNT1L,
271  .r_tcnth = TCNT1H,
272  .r_icr = ICR1L,
273  .r_icrh = ICR1H,
274 
275  .ices = AVR_IO_REGBIT(TCCR1B, ICES1),
276  .icp = AVR_IO_REGBIT(PORTD, 4), // default port for ICP1 (A)
277 
278  .overflow = {
279  .enable = AVR_IO_REGBIT(TIMSK1, TOIE1),
280  .raised = AVR_IO_REGBIT(TIFR1, TOV1),
281  .vector = TIMER1_OVF_vect,
282  },
283  .icr = {
284  .enable = AVR_IO_REGBIT(TIMSK1, ICIE1),
285  .raised = AVR_IO_REGBIT(TIFR1, ICF1),
286  .vector = TIMER1_CAPT_vect,
287  },
288  .comp = {
289  [AVR_TIMER_COMPA] = {
290  .r_ocr = OCR1AL,
291  .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it
292  .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3),
293  .com_pin = AVR_IO_REGBIT(PORTD, 2),
294  .interrupt = {
295  .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A),
296  .raised = AVR_IO_REGBIT(TIFR1, OCF1A),
297  .vector = TIMER1_COMPA_vect,
298  },
299  },
300  [AVR_TIMER_COMPB] = {
301  .r_ocr = OCR1BL,
302  .r_ocrh = OCR1BH,
303  .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
304  .com_pin = AVR_IO_REGBIT(PORTC, 1),
305  .interrupt = {
306  .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B),
307  .raised = AVR_IO_REGBIT(TIFR1, OCF1B),
308  .vector = TIMER1_COMPB_vect,
309  },
310  },
311  },
312  },
313  AVR_SPI_DECLARE(PRR, PRSPI),
314 };
315 #endif /* SIM_CORENAME */
316 
317 #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 mxm1_reset(struct avr_t *avr)
Definition: sim_megaxm1.c:45
Definition: avr_adc.h:104
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
avr_lin_t lin
Definition: sim_megaxm1.h:51
#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
Definition: avr_adc.h:95
#define AVR_ADC_TEMP()
Definition: avr_adc.h:171
#define AVR_TIMER_WGM_ICCTC()
Definition: avr_timer.h:90
Definition: avr_adc.h:94
#define AVR_EEPROM_DECLARE_NOEEPM(_vector)
no EEPM registers in atmega128
Definition: avr_eeprom.h:88
#define AVR_SELFPROG_DECLARE(_spmr, _spen, _vector)
Definition: avr_flash.h:82
#define AVR_TIMER_WGM_FASTPWM9()
Definition: avr_timer.h:92
Definition: avr_adc.h:83
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
void mxm1_init(struct avr_t *avr)
Definition: sim_megaxm1.c:26
avr_watchdog_t watchdog
Definition: sim_90usb162.c:44
Definition: sim_90usb162.c:40
Definition: avr_adc.h:105
Definition: avr_adc.h:103
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
avr_t core
Definition: sim_90usb162.c:41
#define DEFAULT_CORE(_vector_size)
Definition: sim_core_declare.h:52
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
avr_ioport_t porte
Definition: sim_mega128.c:51
#define AVR_IO_REGBITS(_io, _bit, _mask)
Definition: sim_regbit.h:153
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_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:92
Definition: avr_adc.h:96
Main AVR instance.
Definition: sim_avr.h:142
avr_spi_t spi
Definition: sim_90usb162.c:49
Definition: avr_adc.h:93
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
Definition: avr_lin.h:37