SpenceKonde / megaTinyCore

Arduino core for the tinyAVR 0/1/2-series - Ones's digit 2,4,5,7 (pincount, 8,14,20,24), tens digit 0, 1, or 2 (featureset), preceded by flash in kb. Library maintainers: porting help available!
Other
554 stars 144 forks source link

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

Closed andrewwatkin closed 6 months ago

andrewwatkin commented 6 months ago

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

My tests on megaTinyCore ef7b7870ee695b 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,

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

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 commented 6 months ago

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

I don't think 2dad406f43 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.