From 2dad406f43c81c02787a88ce7d517a85450cc983 Mon Sep 17 00:00:00 2001 From: Spence Konde Date: Fri, 5 Apr 2024 13:58:28 -0400 Subject: [PATCH] Should fix #1078 --- megaavr/cores/megatinycore/Arduino.h | 31 +++++++++++++--------- megaavr/cores/megatinycore/wiring_analog.c | 21 +++++++++------ 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/megaavr/cores/megatinycore/Arduino.h b/megaavr/cores/megatinycore/Arduino.h index 3fa1a724..7d3a6225 100644 --- a/megaavr/cores/megatinycore/Arduino.h +++ b/megaavr/cores/megatinycore/Arduino.h @@ -207,19 +207,24 @@ #define ADC_ACC512S badArg("Sign chopping is only supported on Ex-series") #define ADC_ACC1024S badArg("Sign chopping is only supported on Ex-series") - #define LOW_LAT_ON 0x03 // deprecated - #define LOW_LAT_OFF 0x02 // deprecated - #define ADC_LOWLAT_ON 0x03 - #define ADC_LOWLAT_OFF 0x02 - #define PGA_KEEP_ON 0x08 - #define PGA_AUTO_OFF 0x0C - #define PGA_OFF_ONCE 0x04 - #define ADC_ENABLE 0x20 - #define ADC_DISABLE 0x30 - #define ADC_STANDBY_ON 0xC0 - #define ADC_STANDBY_OFF 0x80 - - + #define _PGA_CFG_MASK 0x03 + #define _ADC_LOWLAT_CTRL 0x08 + #define _ADC_LOWLAT_VAL 0x04 + #define _ADC_ENABLE_VAl 0x10 + #define _ADC_ENABLE_CTRL 0x20 + #define _ADC_STANDBY_VAl 0X40 + #define _ADC_STANDBY_CTRL 0x80 + #define PGA_OFF_ONCE 0x01 + #define PGA_KEEP_ON 0x02 + #define PGA_AUTO_OFF 0x03 + #define LOW_LAT_ON 0x0C // deprecated + #define LOW_LAT_OFF 0x08 // deprecated + #define ADC_LOWLAT_ON 0x0C + #define ADC_LOWLAT_OFF 0x08 + #define ADC_ENABLE 0x20 + #define ADC_DISABLE 0x30 + #define ADC_STANDBY_ON 0xC0 + #define ADC_STANDBY_OFF 0x80 #endif /* Errors in analogReadEnh and analogReadDiff are large negative numbers, diff --git a/megaavr/cores/megatinycore/wiring_analog.c b/megaavr/cores/megatinycore/wiring_analog.c index 7d47caa9..55cb7bac 100644 --- a/megaavr/cores/megatinycore/wiring_analog.c +++ b/megaavr/cores/megatinycore/wiring_analog.c @@ -400,11 +400,15 @@ void DACReference(__attribute__ ((unused))uint8_t mode) { // 01 = Turn off PGA, settings unchanged. It will be enabled next time is requested, but will not automatically turn off. // 10 = Turn on PGA, and don't turn it off automatically. // 11 = turn off PGA now and automatically after each use + // + // (set rstby)(to this value)(set enable)(to this value), + + uint8_t temp = ADC0.CTRLA; //performance. - if (options & 0x02) { + if (options & _ADC_LOWLAT_CTRL) { ADC0.CTRLA = 0; // hopwfully workaround lowlat errata by ensuring that everything is turned off. // and configuring lowlat mode. - if (options & 0x01) { + if (options & _ADC_LOWLAT_VAL) { ADC0.CTRLA |= ADC_LOWLAT_bm; temp |= ADC_LOWLAT_bm; } else { @@ -412,24 +416,25 @@ void DACReference(__attribute__ ((unused))uint8_t mode) { temp &= ~ADC_LOWLAT_bm; } } - if (options & 0x20) { - if (options & 0x10) { + if (options & _ADC_ENABLE_CTRL) { + if (options & _ADC_ENABLE_VAL) { temp |= 1; // ADC on } else { temp &= 0xFE; // ADC off } } - if (options & 0x80) { - if (options & 0x40) { + if (options & _ADC_STANDBY_CTRL) { + if (options & _ADC_STANDBY_VAL) { temp |= 0x80; // run standby } else { temp &= 0x7F; // no run standby } } ADC0.CTRLA = temp; //now we apply enable and standby, and lowlat has been turned on, hopefully that's good enough for the errata. - if (options & 0x04) { // turn off PGA. + options &= _PGA_CFG_MASK; + if (options & 0x01) { // turn off PGA. ADC0.PGACTRL &= ~ADC_PGAEN_bm; - if (options & 0x08) { + if (options & 0x02) { _analog_options &= 0x7F; } else { _analog_options |= 0x80;