SimAVR
AVR Simulator
sim_tinyx4.h
Go to the documentation of this file.
1 /*
2  sim_tinyx4.h
3 
4  Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5  Jon Escombe <lists@dresco.co.uk>
6 
7  This file is part of simavr.
8 
9  simavr is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  simavr is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with simavr. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 
24 #ifndef __SIM_TINYX4_H__
25 #define __SIM_TINYX4_H__
26 
27 #include "sim_core_declare.h"
28 #include "avr_eeprom.h"
29 #include "avr_watchdog.h"
30 #include "avr_extint.h"
31 #include "avr_ioport.h"
32 #include "avr_adc.h"
33 #include "avr_timer.h"
34 
35 void tx4_init(struct avr_t * avr);
36 void tx4_reset(struct avr_t * avr);
37 
38 /*
39  * This is a template for all of the tinyx4 devices, hopefully
40  */
41 struct mcu_t {
42  avr_t core;
47  avr_adc_t adc;
49 };
50 
51 #ifdef SIM_CORENAME
52 
53 #ifndef SIM_VECTOR_SIZE
54 #error SIM_VECTOR_SIZE is not declared
55 #endif
56 #ifndef SIM_MMCU
57 #error SIM_MMCU is not declared
58 #endif
59 
60 const struct mcu_t SIM_CORENAME = {
61  .core = {
62  .mmcu = SIM_MMCU,
64 
65  .init = tx4_init,
66  .reset = tx4_reset,
67  },
68  AVR_EEPROM_DECLARE(EE_RDY_vect),
69  AVR_WATCHDOG_DECLARE(WDTCSR, WDT_vect),
70  .extint = {
71  AVR_EXTINT_TINY_DECLARE(0, 'B', PB2, GIFR),
72  },
73  .porta = {
74  .name = 'A', .r_port = PORTA, .r_ddr = DDRA, .r_pin = PINA,
75  .pcint = {
76  .enable = AVR_IO_REGBIT(GIMSK, PCIE0),
77  .raised = AVR_IO_REGBIT(GIFR, PCIF0),
78  .vector = PCINT0_vect,
79  },
80  .r_pcint = PCMSK0,
81  },
82  .portb = {
83  .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
84  .pcint = {
85  .enable = AVR_IO_REGBIT(GIMSK, PCIE1),
86  .raised = AVR_IO_REGBIT(GIFR, PCIF1),
87  .vector = PCINT1_vect,
88  },
89  .r_pcint = PCMSK1,
90  },
91  .adc = {
92  .r_admux = ADMUX,
93  .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1),
94  AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3),},
95  .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1), },
96  .ref_values = {
97  [0] = ADC_VREF_VCC, [1] = ADC_VREF_AVCC,
98  [2] = ADC_VREF_V110,
99  },
100 
101  .adlar = AVR_IO_REGBIT(ADMUX, ADLAR),
102  .r_adcsra = ADCSRA,
103  .aden = AVR_IO_REGBIT(ADCSRA, ADEN),
104  .adsc = AVR_IO_REGBIT(ADCSRA, ADSC),
105  .adate = AVR_IO_REGBIT(ADCSRA, ADATE),
106  .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),},
107 
108  .r_adch = ADCH,
109  .r_adcl = ADCL,
110 
111  .r_adcsrb = ADCSRB,
112  .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), AVR_IO_REGBIT(ADCSRB, ADTS2),},
113  .adts_op = {
114  [0] = avr_adts_free_running,
122  },
123  .bin = AVR_IO_REGBIT(ADCSRB, BIN),
124 
125  .muxmode = {
126  [0] = AVR_ADC_SINGLE(0), [1] = AVR_ADC_SINGLE(1),
127  [2] = AVR_ADC_SINGLE(2), [3] = AVR_ADC_SINGLE(3),
128 
129  [ 4] = AVR_ADC_DIFF(2, 2, 1), [ 5] = AVR_ADC_DIFF(2, 2, 20),
130  [ 6] = AVR_ADC_DIFF(2, 3, 1), [ 7] = AVR_ADC_DIFF(2, 3, 20),
131  [ 8] = AVR_ADC_DIFF(0, 0, 1), [ 9] = AVR_ADC_DIFF(0, 0, 20),
132  [10] = AVR_ADC_DIFF(0, 1, 1), [11] = AVR_ADC_DIFF(0, 1, 20),
133  [12] = AVR_ADC_REF(1100), // Vbg
134  [13] = AVR_ADC_REF(0), // GND
135  [15] = AVR_ADC_TEMP(),
136  },
137 
138  .adc = {
139  .enable = AVR_IO_REGBIT(ADCSRA, ADIE),
140  .raised = AVR_IO_REGBIT(ADCSRA, ADIF),
141  .vector = ADC_vect,
142  },
143  },
144  .timer0 = {
145  .name = '0',
146  .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
147  .wgm_op = {
148  [0] = AVR_TIMER_WGM_NORMAL8(),
149  [2] = AVR_TIMER_WGM_CTC(),
150  [3] = AVR_TIMER_WGM_FASTPWM8(),
151  [7] = AVR_TIMER_WGM_OCPWM(),
152  },
153  .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
154  .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
155 
156  .r_tcnt = TCNT0,
157 
158  .overflow = {
159  .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
160  .raised = AVR_IO_REGBIT(TIFR0, TOV0),
161  .vector = TIM0_OVF_vect,
162  },
163  .comp = {
164  [AVR_TIMER_COMPA] = {
165  .r_ocr = OCR0A,
166  .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3),
167  .com_pin = AVR_IO_REGBIT(PORTB, 0),
168  .interrupt = {
169  .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
170  .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
171  .vector = TIM0_COMPA_vect,
172  },
173  },
174  [AVR_TIMER_COMPB] = {
175  .r_ocr = OCR0B,
176  .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3),
177  .com_pin = AVR_IO_REGBIT(PORTB, 1),
178  .interrupt = {
179  .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
180  .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
181  .vector = TIM0_COMPB_vect,
182  },
183  },
184  },
185  },
186  .timer1 = {
187  .name = '1',
188  .disabled = AVR_IO_REGBIT(PRR,PRTIM1),
189  .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
190  AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
191  .wgm_op = {
192  [0] = AVR_TIMER_WGM_NORMAL16(),
193  [4] = AVR_TIMER_WGM_CTC(),
194  [5] = AVR_TIMER_WGM_FASTPWM8(),
195  [6] = AVR_TIMER_WGM_FASTPWM9(),
196  [7] = AVR_TIMER_WGM_FASTPWM10(),
197  [12] = AVR_TIMER_WGM_ICCTC(),
198  [14] = AVR_TIMER_WGM_ICPWM(),
199  [15] = AVR_TIMER_WGM_OCPWM(),
200  },
201  .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
202  .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* External clock T1 is not handled */},
203 
204  .r_tcnt = TCNT1L,
205  .r_tcnth = TCNT1H,
206  .r_icr = ICR1L,
207  .r_icrh = ICR1H,
208 
209  .ices = AVR_IO_REGBIT(TCCR1B, ICES1),
210  .icp = AVR_IO_REGBIT(PORTB, 0),
211 
212  .overflow = {
213  .enable = AVR_IO_REGBIT(TIMSK1, TOIE1),
214  .raised = AVR_IO_REGBIT(TIFR1, TOV1),
215  .vector = TIM1_OVF_vect,
216  },
217  .icr = {
218  .enable = AVR_IO_REGBIT(TIMSK1, ICIE1),
219  .raised = AVR_IO_REGBIT(TIFR1, ICF1),
220  .vector = TIM1_CAPT_vect,
221  },
222  .comp = {
223  [AVR_TIMER_COMPA] = {
224  .r_ocr = OCR1AL,
225  .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it
226  .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3),
227  .com_pin = AVR_IO_REGBIT(PORTB, 1),
228  .interrupt = {
229  .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A),
230  .raised = AVR_IO_REGBIT(TIFR1, OCF1A),
231  .vector = TIM1_COMPA_vect,
232  },
233  },
234  [AVR_TIMER_COMPB] = {
235  .r_ocr = OCR1BL,
236  .r_ocrh = OCR1BH,
237  .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
238  .com_pin = AVR_IO_REGBIT(PORTB, 2),
239  .interrupt = {
240  .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B),
241  .raised = AVR_IO_REGBIT(TIFR1, OCF1B),
242  .vector = TIM1_COMPB_vect,
243  },
244  },
245  },
246  },
247 };
248 #endif /* SIM_CORENAME */
249 
250 #endif /* __SIM_TINYX4_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
#define SIM_MMCU
Definition: sim_mega324.c:24
#define SIM_CORENAME
Definition: sim_mega324.c:25
#define AVR_EXTINT_TINY_DECLARE(_index, _portname, _portpin, _IFR)
Definition: avr_extint.h:115
void tx4_init(struct avr_t *avr)
Definition: sim_tinyx4.c:27
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
default mode
Definition: avr_adc.h:80
Definition: avr_adc.h:95
#define AVR_ADC_TEMP()
Definition: avr_adc.h:171
#define AVR_TIMER_WGM_ICCTC()
Definition: avr_timer.h:90
#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_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
void tx4_reset(struct avr_t *avr)
Definition: sim_tinyx4.c:41
Definition: avr_eeprom.h:35
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
#define AVR_IO_REGBITS(_io, _bit, _mask)
Definition: sim_regbit.h:153
avr_ioport_t porta
Definition: sim_mega128.c:51
#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
#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_extint_t extint
Definition: sim_90usb162.c:45
#define AVR_ADC_DIFF(_a, _b, _g)
Definition: avr_adc.h:159
#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