Makefile: Use CPPFLAGS for -I stuff
[simavr] / simavr / sim / avr_adc.h
1 /*
2         avr_adc.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 __AVR_ADC_H___
23 #define __AVR_ADC_H___
24
25 #include "sim_avr.h"
26
27 /*
28  * simavr ADC allows external code to feed real voltages to the
29  * simulator, and the simulator uses it's 'real' reference voltage
30  * to do the right thing and return the 'proper' 10 bits ADC value
31  * to the AVR firmware.
32  *
33  * To send values to the ADC, register your code to wait for the
34  * ADC_IRQ_OUT_TRIGGER irq, and at that point send any of the
35  * ADC_IRQ_ADC* with Millivolts as value.
36  *
37  * External trigger is not done yet.
38  */
39
40 enum {
41         // input IRQ values. Values are /always/ volts * 1000 (millivolts)
42         ADC_IRQ_ADC0 = 0, ADC_IRQ_ADC1, ADC_IRQ_ADC2, ADC_IRQ_ADC3,
43         ADC_IRQ_ADC4, ADC_IRQ_ADC5, ADC_IRQ_ADC6, ADC_IRQ_ADC7,
44         ADC_IRQ_ADC8, ADC_IRQ_ADC9, ADC_IRQ_ADC10, ADC_IRQ_ADC11,
45         ADC_IRQ_ADC12, ADC_IRQ_ADC13, ADC_IRQ_ADC14, ADC_IRQ_ADC15,
46         ADC_IRQ_TEMP,                   // see the datasheet
47         ADC_IRQ_IN_TRIGGER,
48         ADC_IRQ_OUT_TRIGGER,    // sends a avr_adc_mux_t
49         ADC_IRQ_COUNT
50 };
51
52 // Get the internal IRQ corresponding to the INT
53 #define AVR_IOCTL_ADC_GETIRQ AVR_IOCTL_DEF('a','d','c',' ')
54
55 /*
56  * Definition of a ADC mux mode.
57  */
58 enum {
59         ADC_MUX_NONE = 0,               // Nothing. return 0
60         ADC_MUX_NOISE,                  // Nothing. return something random
61         ADC_MUX_SINGLE,                 // Normal ADC pin reading
62         ADC_MUX_DIFF,                   // differencial channels (src-diff)
63         ADC_MUX_TEMP,                   // internal temp sensor
64         ADC_MUX_REF,                    // reference voltage (in src * 100)
65         ADC_MUX_VCC4,                   // VCC/4
66 };
67 typedef struct avr_adc_mux_t {
68         unsigned long kind : 3, gain : 8, diff : 8, src : 13;
69 } avr_adc_mux_t;
70
71 enum {
72         ADC_VREF_AREF   = 0,    // default mode
73         ADC_VREF_VCC,
74         ADC_VREF_AVCC,
75         ADC_VREF_V110   = 1100,
76         ADC_VREF_V256   = 2560,
77 };
78
79 typedef struct avr_adc_t {
80         avr_io_t                io;
81
82         uint8_t                 r_admux;
83         // if the last bit exists in the mux, we are an extended ADC
84         avr_regbit_t    mux[6];
85         avr_regbit_t    ref[3];         // reference voltages bits
86         uint16_t                ref_values[7]; // ADC_VREF_*
87
88         avr_regbit_t    adlar;          // left/right adjustment bit
89
90         uint8_t                 r_adcsra;       // ADC Control and Status Register A
91         avr_regbit_t    aden;           // ADC Enabled
92         avr_regbit_t    adsc;           // ADC Start Conversion
93         avr_regbit_t    adate;          // ADC Auto Trigger Enable
94
95         avr_regbit_t    adps[3];        // Prescaler bits. Note that it's a frequency bit shift
96
97         uint8_t                 r_adcl, r_adch; // Data Registers
98
99         uint8_t                 r_adcsrb;       // ADC Control and Status Register B
100         avr_regbit_t    adts[3];        // Timing Source
101         avr_regbit_t    bin;            // Bipolar Input Mode (tinyx5 have it)
102         avr_regbit_t    ipr;            // Input Polarity Reversal (tinyx5 have it)
103
104         // use ADIF and ADIE bits
105         avr_int_vector_t adc;
106
107         /*
108          * runtime bits
109          */
110         avr_adc_mux_t   muxmode[64];// maximum 6 bits of mux modes
111         uint16_t                adc_values[8];  // current values on the ADCs
112         uint16_t                temp;           // temp sensor reading
113         uint8_t                 first;
114         uint8_t                 read_status;    // marked one when adcl is read
115 } avr_adc_t;
116
117 void avr_adc_init(avr_t * avr, avr_adc_t * port);
118
119
120 /*
121  * Helper macros for the Cores definition of muxes
122  */
123 #define AVR_ADC_SINGLE(_chan) { \
124                 .kind = ADC_MUX_SINGLE, \
125                 .src = (_chan), \
126         }
127 #define AVR_ADC_DIFF(_a,_b,_g) { \
128                 .kind = ADC_MUX_DIFF, \
129                 .src = (_a), \
130                 .diff = (_b), \
131                 .gain = (_g), \
132         }
133 #define AVR_ADC_REF(_t) { \
134                 .kind = ADC_MUX_REF, \
135                 .src = (_t), \
136         }
137 #define AVR_ADC_TEMP() { \
138                 .kind = ADC_MUX_TEMP, \
139         }
140
141 #define AVR_ADC_VCC4() { \
142                 .kind = ADC_MUX_VCC4, \
143         }
144
145 #endif /* __AVR_ADC_H___ */