4
Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6
This file is part of simavr.
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.
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.
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/>.
22
#ifndef __AVR_ADC_H___
23
#define __AVR_ADC_H___
32
* simavr ADC allows external code to feed real voltages to the
33
* simulator, and the simulator uses it's 'real' reference voltage
34
* to do the right thing and return the 'proper' 10 bits ADC value
35
* to the AVR firmware.
37
* To send values to the ADC, register your code to wait for the
38
* ADC_IRQ_OUT_TRIGGER irq, and at that point send any of the
39
* ADC_IRQ_ADC* with Millivolts as value.
41
* External trigger is not done yet.
45
// input IRQ values. Values are /always/ volts * 1000 (millivolts)
46
ADC_IRQ_ADC0 = 0, ADC_IRQ_ADC1, ADC_IRQ_ADC2, ADC_IRQ_ADC3,
47
ADC_IRQ_ADC4, ADC_IRQ_ADC5, ADC_IRQ_ADC6, ADC_IRQ_ADC7,
48
ADC_IRQ_ADC8, ADC_IRQ_ADC9, ADC_IRQ_ADC10, ADC_IRQ_ADC11,
49
ADC_IRQ_ADC12, ADC_IRQ_ADC13, ADC_IRQ_ADC14, ADC_IRQ_ADC15,
50
ADC_IRQ_TEMP, // see the datasheet
52
ADC_IRQ_OUT_TRIGGER, // sends a avr_adc_mux_t
56
// Get the internal IRQ corresponding to the INT
57
#define AVR_IOCTL_ADC_GETIRQ AVR_IOCTL_DEF('a','d','c',' ')
60
* Definition of a ADC mux mode.
63
ADC_MUX_NONE = 0, // Nothing. return 0
64
ADC_MUX_NOISE, // Nothing. return something random
65
ADC_MUX_SINGLE, // Normal ADC pin reading
66
ADC_MUX_DIFF, // differential channels (src-diff)
67
ADC_MUX_TEMP, // internal temp sensor
68
ADC_MUX_REF, // reference voltage (in src * 100)
69
ADC_MUX_VCC4, // VCC/4
71
typedef struct avr_adc_mux_t {
72
unsigned long kind : 3, gain : 8, diff : 8, src : 13;
76
ADC_VREF_AREF = 0, // default mode
83
// ADC trigger sources
86
avr_adts_free_running,
87
avr_adts_analog_comparator_0,
88
avr_adts_analog_comparator_1,
89
avr_adts_analog_comparator_2,
90
avr_adts_analog_comparator_3,
91
avr_adts_external_interrupt_0,
92
avr_adts_timer_0_compare_match_a,
93
avr_adts_timer_0_compare_match_b,
94
avr_adts_timer_0_overflow,
95
avr_adts_timer_1_compare_match_b,
96
avr_adts_timer_1_overflow,
97
avr_adts_timer_1_capture_event,
98
avr_adts_pin_change_interrupt,
99
avr_adts_psc_module_0_sync_signal,
100
avr_adts_psc_module_1_sync_signal,
101
avr_adts_psc_module_2_sync_signal,
104
typedef struct avr_adc_t {
109
avr_regbit_t mux[6]; // if the last bit exists in the mux, we are an extended ADC
110
avr_regbit_t ref[3]; // reference voltages bits
111
uint16_t ref_values[7]; // ADC_VREF_*
113
avr_regbit_t adlar; // left/right adjustment bit
115
uint8_t r_adcsra; // ADC Control and Status Register A
116
avr_regbit_t aden; // ADC Enabled
117
avr_regbit_t adsc; // ADC Start Conversion
118
avr_regbit_t adate; // ADC Auto Trigger Enable
120
avr_regbit_t adps[3]; // Prescaler bits. Note that it's a frequency bit shift
122
uint8_t r_adcl, r_adch; // Data Registers
124
uint8_t r_adcsrb; // ADC Control and Status Register B
125
avr_regbit_t adts[4]; // Timing Source
126
avr_adts_type adts_op[16]; // ADTS type
127
uint8_t adts_mode; // the extracted ADTS mode
128
avr_regbit_t bin; // Bipolar Input Mode (tinyx5 have it)
129
avr_regbit_t ipr; // Input Polarity Reversal (tinyx5 have it)
131
avr_int_vector_t adc; // use ADIF and ADIE bits
136
avr_adc_mux_t muxmode[64];// maximum 6 bits of mux modes
137
uint16_t adc_values[16]; // current values on the ADCs
138
uint16_t temp; // temp sensor reading
140
uint8_t read_status; // marked one when adcl is read
143
void avr_adc_init(avr_t * avr, avr_adc_t * port);
147
* Helper macros for the Cores definition of muxes
149
#define AVR_ADC_SINGLE(_chan) { \
150
.kind = ADC_MUX_SINGLE, \
153
#define AVR_ADC_DIFF(_a,_b,_g) { \
154
.kind = ADC_MUX_DIFF, \
159
#define AVR_ADC_REF(_t) { \
160
.kind = ADC_MUX_REF, \
163
#define AVR_ADC_TEMP() { \
164
.kind = ADC_MUX_TEMP, \
167
#define AVR_ADC_VCC4() { \
168
.kind = ADC_MUX_VCC4, \
175
#endif /* __AVR_ADC_H___ */