1
\$\begingroup\$

I'm having issues with my current AVR-C project that runs on an ATMega328p. After issuing the sleep command the chip wakes up again immediately. I want to wakeup with an external interrupt, but while trying to find the source of the issue I disabled them as well, expecting the IC to stay in power down forever.

void system_sleep() {
    // adc disable
    ADCSRA = 0;

    wdt_disable();
    cli();

    // EICRA = _BV(ISC10) | _BV(ISC11);
    // EIMSK = _BV(INT1);
    EIMSK = 0;
    EICRA = 0;

    set_sleep_mode(SLEEP_MODE_PWR_DOWN);

    sleep_bod_disable();
    sei();
    sleep_cpu();

    sleep_disable();

    // disable external interrupt
    EIMSK = 0;
    EICRA = 0;

    adc_init();
    wdt_enable(WDTO_1S);
}

Any ideas what could cause that? Is there an easy way to check what wakeup source triggered sleep to end?

\$\endgroup\$
6
  • \$\begingroup\$ Any interrupt will wake it. This is one function, we have no idea what other sources might also be enabled; the simplest assumption is that there are others. Trigger source is recorded by interrupt flags, and in turn CPU branching to the respective ISR if used. \$\endgroup\$ Sep 19, 2023 at 1:27
  • \$\begingroup\$ For power down (according to the data sheet) only INT, PCINT and I²C, assuming I understand it right. I'm not using either of the latter two in the project. The only other interrupt vectors are a timer and the adc, which also both are disabled \$\endgroup\$ Sep 19, 2023 at 1:45
  • \$\begingroup\$ When you do SEI, any active interrupt condition triggers on the next instruction. If you actually want it to sleep, you should clear the interrupt flag register before doing SEI \$\endgroup\$
    – david
    Sep 19, 2023 at 2:32
  • \$\begingroup\$ @david do you mean the external interrupt flag register EIFR? I gave that a go and it didn't help \$\endgroup\$ Sep 19, 2023 at 3:26
  • \$\begingroup\$ I was just thinking generically of the SEI/Sleep sequence. What interrupts have you enabled? Maybe TXC bit (UART register)? \$\endgroup\$
    – david
    Sep 19, 2023 at 5:15

1 Answer 1

3
\$\begingroup\$

The solution to the problem is not very exciting, as it was me simply overlooking something and for some reason didn't see it staring at the code for hours. I was simply missing a call to sleep_enable. The following works completely as expected.

cli();
sleep_enable();
sleep_bod_disable();
sei();

sleep_cpu();
sleep_disable();
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Please put a green check mark in this answer, as it will be useful to others who have this kind of problem. Thank you. \$\endgroup\$ Oct 19, 2023 at 0:13
  • 1
    \$\begingroup\$ Thanks, I completely forgot about that \$\endgroup\$ Oct 20, 2023 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.