Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADCPowerOptions() ADC_ENABLE and ADC_DISABLE appear to be exchanged #1078

Closed
andrewwatkin opened this issue Apr 3, 2024 · 1 comment
Closed

Comments

@andrewwatkin
Copy link

andrewwatkin commented Apr 3, 2024

Firstly, thank you for the monumental effort of providing megaTinyCore.

My tests on megaTinyCore ef7b787 show that ADCPowerOptions(ADC_DISABLE) enabled the ADC and ADCPowerOptions(ADC_ENABLE) disables it. This is the opposite of what I expected.

I am using an ATtiny826. So, for a series-2 device:

void ADCPowerOptions(uint8_t options) {

contains

    if (options & 0x20) {
      if (options & 0x10) {
        temp |= 1; // ADC on
      } else {
        temp &= 0xFE; // ADC off
      }
    }

from Arduino.h for series-2, the relevant options can be

  #define ADC_ENABLE      0x20
  #define ADC_DISABLE     0x30

temp.bit0 needs to be 1 to run the ADC according to spec 30.5.1 for ADC.CTRLA
Following the code,

  • ADC_ENABLE will do “ADC off”
  • ADC_DISABLE will do “ADC on”

Tested with a 10R resistor in the ATtiny826 VDD line:

  • With ADCPowerOptions(ADC_DISABLE), there is 1mV across the 10R, implying about 100uA.
  • With ADCPowerOptions(ADC_ENABLE), there is 0mV across the 10R, implying < 10uA (accuracy limited by my multimeter).

Code is

#define MILLIS_USE_TIMERNONE 1

#if defined(__AVR_ATtiny826__) || defined(__AVR_ATtiny1626__) || defined(__AVR_ATtiny3226__)
#else
#error "Only ATtiny Series 2 supported."
#endif
#include <avr/sleep.h>

void RTC_init(void)
{
  /* Initialize RTC: */
  while (RTC.STATUS > 0)
  {
    ;                                   /* Wait for all register to be synchronized */
  }
  RTC.CLKSEL = RTC_CLKSEL_INT32K_gc;    /* 32.768kHz Internal Ultra-Low-Power Oscillator (OSCULP32K) */

  RTC.PITINTCTRL = RTC_PI_bm;           /* PIT Interrupt: enabled */

  RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc /* RTC Clock Cycles 16384, resulting in 32.768kHz/16384 = 2Hz */
  | RTC_PITEN_bm;                       /* Enable PIT counter: enabled */
}

ISR(RTC_PIT_vect)
{
  RTC.PITINTFLAGS = RTC_PI_bm;          /* Clear interrupt flag by writing '1' (required) */
}

void setup() {
  // PA0 is the UPDI programming pin
  pinMode(PIN_PA1, OUTPUT);
  pinMode(PIN_PA2, OUTPUT);
  pinMode(PIN_PA3, OUTPUT);
  pinMode(PIN_PA4, OUTPUT);
  pinMode(PIN_PA5, OUTPUT);
  pinMode(PIN_PA6, OUTPUT);
  pinMode(PIN_PA7, OUTPUT);
  pinMode(PIN_PB0, OUTPUT);
  pinMode(PIN_PB1, OUTPUT);
  pinMode(PIN_PB2, OUTPUT);
  pinMode(PIN_PB3, OUTPUT);
  pinMode(PIN_PB4, OUTPUT);
  pinMode(PIN_PB5, OUTPUT);
  pinMode(PIN_PC0, OUTPUT);
  pinMode(PIN_PC1, OUTPUT);
  pinMode(PIN_PC2, OUTPUT);
  pinMode(PIN_PC3, OUTPUT);

  ADCPowerOptions(ADC_ENABLE);

  RTC_init();                           /* Initialize the RTC timer */

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  /* Set sleep mode to POWER DOWN mode */
  sleep_enable();                       /* Enable sleep mode, but not going to sleep yet */
}

void loop() {
  digitalWrite(PIN_PA5, HIGH); 
  sleep_cpu();                          /* Sleep the device and wait for an interrupt to continue */
  digitalWrite(PIN_PA5, LOW); 
  digitalWrite(PIN_PA4, CHANGE);              /* Device woke up and toggle LED on pin#7 */
}
@andrewwatkin
Copy link
Author

andrewwatkin commented Apr 5, 2024

I don't know the correct github was to reopen this issue. See #1082

I don't think 2dad406 has fixed the problem which is that ADC_ENABLE disables the ADC and ADC_DISABLE enables it.

To make sense the code in wiring_analog.c around line 419 requires that
ADC_ENABLE == _ADC_ENABLE_CTRL | _ADC_ENABLE_VAL.

But this is not the case in Arduino.h around line 224.

There is the same problem for the series-0 / series-1 code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant