Question

ATtiny816 Standby comparator wake on interupt

Looking for some advise on the best way to sleep and wake an ATtiny816, running megatinycore in arduino.

I have the built in comparator configured like so:

  #include <Comparator.h>
  // Configure Piezo knock detection comparator parameters
  Comparator.input_p = comparator::in_p::in0;
  Comparator.reference = comparator::ref::vref_0v55;
  Comparator.input_n = comparator::in_n::vref;
  Comparator.hysteresis = comparator::hyst::large;
  // Initialize comparator
  Comparator.init();
  Comparator.attachInterrupt(tapInterrupt, RISING);
  // Enable the Analog Comparator to run in standby mode, and set to low power mode
  AC_t& AC_struct = Comparator.getPeripheral();
  AC_struct.CTRLA |= AC_LPMODE_bm | AC_RUNSTDBY_bm;
  // Start comparator
  Comparator.start();

This works great, and its triggering the tapInterrupt fine. The issue is now I want to place the chip in standby mode to save battery, like so:

  wdt_disable();
  set_sleep_mode(SLEEP_MODE_STANDBY);
  sleep_enable();
  sleep_cpu();
  sleep_disable();

The issue is even though I have set the comparator to carry on running in standby, the interrupt isn't triggering, which I'm assuming is because of this section in the ATtiny816 manual:

In Standby sleep mode, the AC is disabled by default. If the Run in Standby Mode (RUNSTDBY) bit in the Control A (ACn.CTRLA) register is written to 1, the AC will continue to operate, but the Status register will not be updated, and no interrupts are generated if no other modules request the CLK_PER, but events and the pad output will be updated.

So I guess from that, I need to enable some other module/peripheral to remain active in standby, but my question is what else should I enable in standby that would fulfil this requirement, and also use the least amount of battery?, and how would I enable it in Arduino?

I have seen idle power mode would also work, but ideally I want it to use as little battery as possible.

Update: So I've had a play around with PIT, but because the comparator interrupt needs to be very responsive, as its basically acting like a switch to turn on the circuit, by the time its slept, woke up, checked for the comparator interrupt for a long enough time to make it feel responsive, then gone back to sleep again, it doesn't really save much power. I really need to get the comparator continuously working, while the rest of the chip is in standby, if that's possible?

This is what I have now to keep the RTC going, in the hopes that it would keep the comparator working during Standby:

// Set the sleep mode
  set_sleep_mode(SLEEP_MODE_STANDBY);
  // RTC initialization and configuration
  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.PITCTRLA = RTC_PERIOD_CYC16384_gc /* RTC Clock Cycles 16384, resulting in 32.768kHz/16384 = 2Hz */
  | RTC_PITEN_bm;  
  RTC.CTRLA = RTC_RUNSTDBY_bm;

However it still seems that the comparator interrupts still don't work. But then if you can't use the comparator during StandBy, I don't understand the purpose of having the RUNSTDBY flag, if you then can't actually use it in standby?

 2  76  2
1 Jan 1970

Solution

 2

For tinyAVR 0,1 and 2-series MCUs, there are 3 sleep modes, Idle, Standby and PowerDown mode. See the table 11.1 and 11.2 on datasheet for more details. In the nutshell, for Standby mode, only a subset of peripherals (not many) that have the RUNSTDBY bit in its register work during the sleep mode. RTC is one of those peripherals that have the RUNSTDBY bit available in CTRLA register and can be used as the clock source through the ULP_32.738k internal clock. While in Power Down mode, PIT (as part of the RTC) is the only peripheral (along with TWI address sleep walker) can be configured to wakeup from PowerDown sleep. You can setup RTC/PIT interrupt (depends on whether you'd want to use Standby mode or PowerDown mode) to wakeup and handle the AC.

Read chapter 11 of the datasheet and AVR Low Power Techniques application note for more details, or take a look at my Business Card + ATtiny3227 Dev Board on how I setup the PIT (and other configurations) to achieve sleep current of 1uA.

2024-07-03
hcheung