diff options
Diffstat (limited to 'arch')
| -rw-r--r-- | arch/attiny1616.c | 9 | ||||
| -rw-r--r-- | arch/attiny1616.h | 24 | ||||
| -rw-r--r-- | arch/avr32dd20.h | 13 |
3 files changed, 40 insertions, 6 deletions
diff --git a/arch/attiny1616.c b/arch/attiny1616.c index 330c809..2770d06 100644 --- a/arch/attiny1616.c +++ b/arch/attiny1616.c @@ -29,8 +29,7 @@ inline void clock_prescale_set(uint8_t n) { inline void mcu_set_admux_therm() { // put the ADC in temperature mode // attiny1616 datasheet section 30.3.2.6 - VREF.CTRLA = (VREF.CTRLA & (~VREF_ADC0REFSEL_gm)) - | VREF_ADC0REFSEL_1V1_gc; // Set Vbg ref to 1.1V + mcu_set_adc0_vref(VREF_ADC0REFSEL_1V1_gc); // Set Vbg ref to 1.1V ADC0.MUXPOS = ADC_MUXPOS_TEMPSENSE_gc; // read temperature ADC0.CTRLB = ADC_SAMPNUM_ACC4_gc; // 10-bit result + 4x oversampling ADC0.CTRLC = ADC_SAMPCAP_bm @@ -46,8 +45,7 @@ inline void mcu_set_admux_voltage() { ADC0.CTRLD |= ADC_INITDLY_DLY16_gc; #ifdef USE_VOLTAGE_DIVIDER // measure an arbitrary pin // result = resolution * Vdiv / 1.1V - VREF.CTRLA = (VREF.CTRLA & (~VREF_ADC0REFSEL_gm)) - | VREF_ADC0REFSEL_1V1_gc; // Set Vbg ref to 1.1V + mcu_set_adc0_vref(VREF_ADC0REFSEL_1V1_gc); // Set Vbg ref to 1.1V ADC0.MUXPOS = ADMUX_VOLTAGE_DIVIDER; // read the requested ADC pin ADC0.CTRLB = ADC_SAMPNUM_ACC4_gc; // 12-bit result, 4x oversampling ADC0.CTRLC = ADC_SAMPCAP_bm @@ -55,8 +53,7 @@ inline void mcu_set_admux_voltage() { | ADC_REFSEL_INTREF_gc; // Use internal ADC reference #else // measure VDD pin // result = resolution * 1.5V / Vbat - VREF.CTRLA = (VREF.CTRLA & (~VREF_ADC0REFSEL_gm)) - | VREF_ADC0REFSEL_1V5_gc; // Set Vbg ref to 1.5V + mcu_set_adc0_vref(VREF_ADC0REFSEL_1V5_gc); // Set Vbg ref to 1.5V ADC0.MUXPOS = ADC_MUXPOS_INTREF_gc; // read internal reference ADC0.CTRLB = ADC_SAMPNUM_ACC4_gc; // 12-bit result, 4x oversampling ADC0.CTRLC = ADC_SAMPCAP_bm diff --git a/arch/attiny1616.h b/arch/attiny1616.h index 57b0023..940973e 100644 --- a/arch/attiny1616.h +++ b/arch/attiny1616.h @@ -18,6 +18,8 @@ inline void mcu_clock_speed(); // this should work, but needs further validation inline void clock_prescale_set(uint8_t n); +// TODO: allow hwdef to define a base clock speed, +// and adjust these values accordingly typedef enum { // Actual clock is 20 MHz, but assume that 10 MHz is the top speed and work from there @@ -34,8 +36,30 @@ typedef enum } clock_div_t; +////////// DAC controls ////////// + +#define DAC_LVL DAC0.DATA // 0 to 255, for 0V to Vref +#define DAC_VREF VREF.CTRLA // 0.55V, 1.1V, 1.5V, 2.5V, or 4.3V + +// set only the relevant bits of the Vref register +#define mcu_set_dac_vref(x) \ + VREF.CTRLA = x | (VREF.CTRLA & (~VREF_DAC0REFSEL_gm)); + +// Vref values +// (for the DAC bits, not the ADC bits) +#define V05 V055 +#define V055 VREF_DAC0REFSEL_0V55_gc +#define V11 VREF_DAC0REFSEL_1V1_gc +#define V25 VREF_DAC0REFSEL_2V5_gc +#define V43 VREF_DAC0REFSEL_4V34_gc +#define V15 VREF_DAC0REFSEL_1V5_gc + ////////// ADC voltage / temperature ////////// +// set only the relevant bits of the Vref register +#define mcu_set_adc0_vref(x) \ + VREF.CTRLA = x | (VREF.CTRLA & (~VREF_ADC0REFSEL_gm)); + #define hwdef_set_admux_therm mcu_set_admux_therm inline void mcu_set_admux_therm(); diff --git a/arch/avr32dd20.h b/arch/avr32dd20.h index 82951de..09b4096 100644 --- a/arch/avr32dd20.h +++ b/arch/avr32dd20.h @@ -30,6 +30,19 @@ typedef enum } clock_div_t; +////////// DAC controls ////////// + +// main LED outputs +#define DAC_LVL DAC0_DATA // 0 to 1023, for 0V to Vref +#define DAC_VREF VREF_DAC0REF // 1.024V, 2.048V, 4.096V, or 2.5V + +// Vref values (suitable for DAC and ADC0) +#define V10 VREF_REFSEL_1V024_gc +#define V20 VREF_REFSEL_2V048_gc +#define V25 VREF_REFSEL_2V500_gc +#define V40 VREF_REFSEL_4V096_gc + + ////////// ADC voltage / temperature ////////// #define hwdef_set_admux_therm mcu_set_admux_therm |
