Skip to content

Commit

Permalink
Should fix #1078
Browse files Browse the repository at this point in the history
  • Loading branch information
SpenceKonde committed Apr 5, 2024
1 parent ef7b787 commit 2dad406
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
31 changes: 18 additions & 13 deletions megaavr/cores/megatinycore/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 13 additions & 8 deletions megaavr/cores/megatinycore/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,36 +400,41 @@ 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 {
ADC0.CTRLA &= ~ADC_LOWLAT_bm;
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;
Expand Down

0 comments on commit 2dad406

Please sign in to comment.