00001
00039 #ifndef REGS_XMEGA_ADC_H_INCLUDED
00040 #define REGS_XMEGA_ADC_H_INCLUDED
00041
00042 #include <chip/memory-map.h>
00043 #include <io.h>
00044
00051
00052
00053 #define ADC_CTRLA 0x00 //!< Control register A
00054 #define ADC_CTRLB 0x01 //!< Control register B
00055 #define ADC_REFCTRL 0x02 //!< Reference control
00056 #define ADC_EVCTRL 0x03 //!< Event control
00057 #define ADC_PRESCALER 0x04 //!< ADC clock prescaling
00058
00059 #define ADC_INTFLAGS 0x06 //!< Interrupt flags for ADC channels
00060 #define ADC_TEMP 0x07 //!< Temporary register for 16-bit reads
00061
00062 #define ADC_CALL 0x0C //!< ADC calibration low byte
00063 #define ADC_CALH 0x0D //!< ADC calibration high byte
00064
00065 #define ADC_CH0RESL 0x10 //!< ADC channel 0 result low byte
00066 #define ADC_CH0RESH 0x11 //!< ADC channel 0 result high byte
00067 #define ADC_CH1RESL 0x12 //!< ADC channel 1 result low byte
00068 #define ADC_CH1RESH 0x13 //!< ADC channel 1 result high byte
00069 #define ADC_CH2RESL 0x14 //!< ADC channel 2 result low byte
00070 #define ADC_CH2RESH 0x15 //!< ADC channel 2 result high byte
00071 #define ADC_CH3RESL 0x16 //!< ADC channel 3 result low byte
00072 #define ADC_CH3RESH 0x17 //!< ADC channel 3 result high byte
00073 #define ADC_CMPL 0x18 //!< ADC compare low byte
00074 #define ADC_CMPH 0x19 //!< ADC compare high byte
00075
00076
00078
00079 #define ADC_ENABLE_BIT 0 //!< ADC enable
00080 #define ADC_FLUSH_BIT 1 //!< ADC flush
00081
00082 #define ADC_CHSTART_START 2 //!< ADC channel start single conversion
00083 #define ADC_CHSTART_SIZE 4 //!< ADC channel start single conversion
00084 #define ADC_DMASEL_START 6 //!< ADC channel DMA request selection
00085 #define ADC_DMASEL_SIZE 2 //!< ADC channel DMA request selection
00086
00087
00089
00090 #define ADC_CHSTART_0 1 //!< Start conversion on ADC channel 0
00091 #define ADC_CHSTART_1 2 //!< Start conversion on ADC channel 1
00092 #define ADC_CHSTART_2 4 //!< Start conversion on ADC channel 2
00093 #define ADC_CHSTART_3 8 //!< Start conversion on ADC channel 3
00094
00095
00097
00098 #define ADC_DMASEL_OFF 0 //!< No combined DMA request
00099 #define ADC_DMASEL_CH01 1 //!< ADC channel 0 and 1 can trigger DMA
00100 #define ADC_DMASEL_CH012 2 //!< ADC channel 0, 1 and 2 can trigger DMA
00101 #define ADC_DMASEL_CH0123 3 //!< All ADC channels can trigger DMA
00102
00103
00105
00106 #define ADC_RESOLUTION_START 1 //!< ADC conversion resolution
00107 #define ADC_RESOLUTION_SIZE 2 //!< ADC conversion resolution
00108 #define ADC_FREERUN_BIT 3 //!< ADC free running mode
00109 #define ADC_CONVMODE_BIT 4 //!< ADC signed/unsigned conversion mode
00110
00111
00113
00114 #define ADC_RESOLUTION_12BIT 0 //!< 12-bit result, right adjusted
00115 #define ADC_RESOLUTION_8BIT 2 //!< 8-bit result, right adjusted
00116 #define ADC_RESOLUTION_LEFT12BIT 3 //!< 12-bit result, left adjusted
00117
00118
00120
00121 #define ADC_TEMPREF_BIT 0 //!< Enable temperature reference for measurement
00122 #define ADC_BANDGAP_BIT 1 //!< Enable bandgap for measurement
00123
00124 #define ADC_REFSEL_START 4 //!< ADC reference selection
00125 #define ADC_REFSEL_SIZE 2 //!< ADC reference selection
00126
00127
00129
00130 #define ADC_REFSEL_INT1V 0 //!< Internal 1.00 V as reference
00131 #define ADC_REFSEL_INTVCC 1 //!< Internal VCC/1.6 V as reference
00132 #define ADC_REFSEL_AREFA 2 //!< External reference from AREFA
00133 #define ADC_REFSEL_AREFB 3 //!< External reference from AREFB
00134
00135
00137
00138 #define ADC_EVACT_START 0 //!< ADC event mode
00139 #define ADC_EVACT_SIZE 3 //!< ADC event mode
00140 #define ADC_EVSEL_START 3 //!< ADC event line selection
00141 #define ADC_EVSEL_SIZE 3 //!< ADC event line selection
00142 #define ADC_SWEEP_START 6 //!< ADC channel sweep selection
00143 #define ADC_SWEEP_SIZE 2 //!< ADC channel sweep selection
00144
00145
00147
00148
00149 #define ADC_EVACT_NONE 0
00150
00151 #define ADC_EVACT_CH0 1
00152
00153 #define ADC_EVACT_CH01 2
00154
00155 #define ADC_EVACT_CH012 3
00156
00157 #define ADC_EVACT_CH0123 4
00158
00159 #define ADC_EVACT_SWEEP 5
00160
00161 #define ADC_EVACT_SYNCSWEEP 6
00162
00163
00165
00166 #define ADC_EVSEL_0123 0 //!< Event channels 0, 1, 2 and 3 as event lines
00167 #define ADC_EVSEL_1234 1 //!< Event channels 1, 2, 3 and 4 as event lines
00168 #define ADC_EVSEL_2345 2 //!< Event channels 2, 3, 4 and 5 as event lines
00169 #define ADC_EVSEL_3456 3 //!< Event channels 3, 4, 5 and 6 as event lines
00170 #define ADC_EVSEL_4567 4 //!< Event channels 4, 5, 6 and 7 as event lines
00171 #define ADC_EVSEL_567 5 //!< Event channels 5, 6, and 7 as event lines
00172 #define ADC_EVSEL_67 6 //!< Event channels 6 and 7 as event lines
00173 #define ADC_EVSEL_7 7 //!< Event channel 7 as event line
00174
00175
00177
00178 #define ADC_SWEEP_0 0 //!< Sweep ADC channel 0 only
00179 #define ADC_SWEEP_01 1 //!< Sweep ADC channels 0 and 1
00180 #define ADC_SWEEP_012 2 //!< Sweep ADC channels 0, 1 and 2
00181 #define ADC_SWEEP_0123 3 //!< Sweep all ADC channels
00182
00183
00185
00186 #define ADC_PRESCALER_START 0 //!< ADC clock prescaling
00187 #define ADC_PRESCALER_SIZE 3 //!< ADC clock prescaling
00188
00189
00191
00192 #define ADC_PRESCALER_DIV4 0 //!< Prescale peripheral clock by 4
00193 #define ADC_PRESCALER_DIV8 1 //!< Prescale peripheral clock by 8
00194 #define ADC_PRESCALER_DIV16 2 //!< Prescale peripheral clock by 16
00195 #define ADC_PRESCALER_DIV32 3 //!< Prescale peripheral clock by 32
00196 #define ADC_PRESCALER_DIV64 4 //!< Prescale peripheral clock by 64
00197 #define ADC_PRESCALER_DIV128 5 //!< Prescale peripheral clock by 128
00198 #define ADC_PRESCALER_DIV256 6 //!< Prescale peripheral clock by 256
00199 #define ADC_PRESCALER_DIV512 7 //!< Prescale peripheral clock by 512
00200
00201
00203
00204 #define ADC_CH0IF_BIT 0 //!< ADC channel 0 interrupt flag
00205 #define ADC_CH1IF_BIT 1 //!< ADC channel 1 interrupt flag
00206 #define ADC_CH2IF_BIT 2 //!< ADC channel 2 interrupt flag
00207 #define ADC_CH3IF_BIT 4 //!< ADC channel 3 interrupt flag
00208
00209
00211
00212
00213 #define ADC_BIT(name) \
00214 (1U << ADC_##name##_BIT)
00215
00216 #define ADC_BF(name, value) \
00217 ((value) << ADC_##name##_START)
00218
00219 #define ADC_BFMASK(name) \
00220 (((1U << ADC_##name##_SIZE) - 1) << ADC_##name##_START)
00221
00222 #define ADC_BFEXT(name, regval) \
00223 (((regval) >> ADC_##name##_START) \
00224 & ((1U << ADC_##name##_SIZE) - 1))
00225
00226 #define ADC_BFINS(name, value, regval) \
00227 (((regval) & ~(((1U << ADC_##name##_SIZE) - 1) \
00228 << ADC_##name##_START)) \
00229 | ADC_BF(name, value))
00230
00231
00233
00234
00235 #define adc_read_reg(base, reg) \
00236 mmio_read8((void *)((uintptr_t)(base) + ADC_##reg))
00237
00238 #define adc_write_reg(base, reg, value) \
00239 mmio_write8((void *)((uintptr_t)(base) + ADC_##reg), (value))
00240
00241
00243
00244
00251
00252
00253 #define ADCCH_CH0 0x20 //!< ADC channel 0 register group
00254 #define ADCCH_CH1 0x28 //!< ADC channel 1 register group
00255 #define ADCCH_CH2 0x30 //!< ADC channel 2 register group
00256 #define ADCCH_CH3 0x38 //!< ADC channel 3 register group
00257
00258
00260
00261 #define ADCCH_CTRL 0x00 //!< ADC channel control
00262 #define ADCCH_MUXCTRL 0x01 //!< ADC channel MUX control
00263 #define ADCCH_INTCTRL 0x02 //!< ADC channel interrupt control
00264 #define ADCCH_INTFLAG 0x03 //!< ADC channel interrupt flag
00265 #define ADCCH_RESL 0x04 //!< ADC channel result low byte
00266 #define ADCCH_RESH 0x05 //!< ADC channel result high byte
00267
00268
00270
00271 #define ADCCH_INPUTMODE_START 0 //!< Channel input mode
00272 #define ADCCH_INPUTMODE_SIZE 2 //!< Channel input mode
00273 #define ADCCH_GAIN_START 2 //!< Channel gain setting
00274 #define ADCCH_GAIN_SIZE 3 //!< Channel gain setting
00275 #define ADCCH_START_BIT 7 //!< Start conversion on channel
00276
00277
00279
00280 #define ADCCH_INPUTMODE_INTERNAL 0 //!< Internal input signal
00281 #define ADCCH_INPUTMODE_SINGLEENDED 1 //!< Single-ended input signal
00282 #define ADCCH_INPUTMODE_DIFF 2 //!< Differential input signal
00283 #define ADCCH_INPUTMODE_DIFFWGAIN 3 //!< Differential input signal with gain
00284
00285
00287
00288 #define ADCCH_GAIN_1X 0 //!< Unity gain for differential signal
00289 #define ADCCH_GAIN_2X 1 //!< 2x gain for differential signal
00290 #define ADCCH_GAIN_4X 2 //!< 4x gain for differential signal
00291 #define ADCCH_GAIN_8X 3 //!< 8x gain for differential signal
00292 #define ADCCH_GAIN_16X 4 //!< 16x gain for differential signal
00293 #define ADCCH_GAIN_32X 5 //!< 32x gain for differential signal
00294 #define ADCCH_GAIN_64X 6 //!< 64x gain for differential signal
00295
00296
00298
00299 #define ADCCH_MUXNEG_START 0 //!< Negative input signal MUX selection
00300 #define ADCCH_MUXNEG_SIZE 2 //!< Negative input signal MUX selection
00301 #define ADCCH_MUXPOS_START 3 //!< Positive input signal MUX selection
00302 #define ADCCH_MUXPOS_SIZE 3 //!< Positive input signal MUX selection
00303 #define ADDCH_MUXPOS3_BIT 6 //!< Use pin on secondary port
00304
00305
00307
00308 #define ADCCH_MUXNEG_PIN0 0 //!< ADC0 pin as negative input
00309 #define ADCCH_MUXNEG_PIN1 1 //!< ADC1 pin as negative input
00310 #define ADCCH_MUXNEG_PIN2 2 //!< ADC2 pin as negative input
00311 #define ADCCH_MUXNEG_PIN3 3 //!< ADC3 pin as negative input
00312 #define ADCCH_MUXNEG_PIN4 4 //!< ADC4 pin as negative input
00313 #define ADCCH_MUXNEG_PIN5 5 //!< ADC5 pin as negative input
00314 #define ADCCH_MUXNEG_PIN6 6 //!< ADC6 pin as negative input
00315 #define ADCCH_MUXNEG_PIN7 7 //!< ADC7 pin as negative input
00316
00317
00319
00320
00321 #define ADCCH_MUXPOS_TEMP 0 //!< Temperature reference as positive input
00322 #define ADCCH_MUXPOS_BANDGAP 1 //!< Bandgap reference as positive input
00323 #define ADCCH_MUXPOS_SCALEDVCC 2 //!< VCC/10 as as positive input
00324 #define ADCCH_MUXPOS_DAC 3 //!< DAC output as positive input
00325
00326 #define ADCCH_MUXPOS_PIN0 0 //!< ADC0 pin as positive input
00327 #define ADCCH_MUXPOS_PIN1 1 //!< ADC1 pin as positive input
00328 #define ADCCH_MUXPOS_PIN2 2 //!< ADC2 pin as positive input
00329 #define ADCCH_MUXPOS_PIN3 3 //!< ADC3 pin as positive input
00330 #define ADCCH_MUXPOS_PIN4 4 //!< ADC4 pin as positive input
00331 #define ADCCH_MUXPOS_PIN5 5 //!< ADC5 pin as positive input
00332 #define ADCCH_MUXPOS_PIN6 6 //!< ADC6 pin as positive input
00333 #define ADCCH_MUXPOS_PIN7 7 //!< ADC7 pin as positive input
00334
00335
00337
00338 #define ADCCH_INTLVL_START 0 //!< ADC channel interrupt level
00339 #define ADCCH_INTLVL_SIZE 2 //!< ADC channel interrupt level
00340 #define ADCCH_INTMODE_START 2 //!< ADC channel interrupt mode
00341 #define ADCCH_INTMODE_SIZE 2 //!< ADC channel interrupt mode
00342
00343
00345
00346 #define ADCCH_INTLVL_OFF 0 //!< Interrupt disabled
00347 #define ADCCH_INTLVL_LO 1 //!< Low level interrupt
00348 #define ADCCH_INTLVL_MED 2 //!< Medium level interrupt
00349 #define ADCCH_INTLVL_HI 3 //!< High level interrupt
00350
00351
00353
00354
00355 #define ADCCH_INTMODE_COMPLETE 0
00356
00357 #define ADCCH_INTMODE_BELOW 1
00358
00359 #define ADCCH_INTMODE_ABOVE 3
00360
00361
00363
00364 #define ADCCH_IF_BIT 0 //!< ADC channel interrupt flag
00365
00366
00368
00369
00370 #define ADCCH_BIT(name) \
00371 (1U << ADCCH_##name##_BIT)
00372
00373 #define ADCCH_BF(name, value) \
00374 ((value) << ADCCH_##name##_START)
00375
00376 #define ADCCH_BFMASK(name) \
00377 (((1U << ADCCH_##name##_SIZE) - 1) << ADCCH_##name##_START)
00378
00379 #define ADCCH_BFEXT(name, regval) \
00380 (((regval) >> ADCCH_##name##_START) \
00381 & ((1U << ADCCH_##name##_SIZE) - 1))
00382
00383 #define ADCCH_BFINS(name, value, regval) \
00384 (((regval) & ~(((1U << ADCCH_##name##_SIZE) - 1) \
00385 << ADCCH_##name##_START)) \
00386 | ADCCH_BF(name, value))
00387
00388
00390
00391
00395 #define adcch_read_reg(base, ch, reg) \
00396 mmio_read8((void *)((uintptr_t)(base) + ADCCH_##ch + ADCCH_##reg))
00397
00401 #define adcch_write_reg(base, ch, reg, value) \
00402 mmio_write8((void *)((uintptr_t)(base) + ADCCH_##ch + ADCCH_##reg), (value))
00403
00404
00406
00407 #endif