Sleep mode on tiny412 has issues #1144
-
If I compile my code for the 412 it won't come out of sleep mode on button press interrupt. BUT if I compile it for the 412 with optiboot it will come out of sleep mode, loop through my code once, then hang. I'm compiling to hex and then loading using USB to UPDI through using Python. // set up attiny412 to check for button, if power is off check hold for 3 seconds then turn on
// if power is on, turn off
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/power.h>
volatile bool powerState = false;
volatile bool wakeState = false;
volatile bool returnState = false;
volatile int y = 0;
const int ledPin = 2; // the number of the LED pin
const int bPin = 0; //button pin
int ledState = LOW; // ledState used to set the LED
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(bPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(bPin), PCINT0, CHANGE);
sei(); // Enable global interrupts
}
void loop() {
if (powerState && wakeState){powerState = false;
wakeState = false;
digitalWrite(ledPin, powerState); // Apply new power state
while (digitalRead(bPin) == false){delay(100); }}
if (!digitalRead(bPin) && debounceButton()) {
powerState = !powerState; // Toggle power state
digitalWrite(ledPin, powerState); // Apply new power state
while (digitalRead(bPin) == false){delay(100);}
}
if (!powerState) {
// Device is off, enter sleep mode
//SLPCTRL.CTRLA = SLPCTRL_SMODE_STDBY_gc; // set sleep mode to "standby"
SLPCTRL.CTRLA = SLPCTRL_SMODE_PDOWN_gc; // set sleep mode to "power down"
SLPCTRL.CTRLA |= SLPCTRL_SEN_bm; // enable sleep mode
sei();
sleep_cpu();
wakeState = true;
}
// for io testing
//ledState = !digitalRead(bPin); digitalWrite(ledPin, ledState);
}
bool debounceButton() {
// Wait for button to stabilize
y = 0;
if (powerState == true)
{delay(10); returnState = true;}
else{
while (digitalRead(bPin) == false && y < 300){
delay(10);
y = y + 1;}
if (y > 298){returnState = true;}
else {returnState = false;}
}
return returnState;
}
void PCINT0(){RTC.PITINTFLAGS = RTC_PI_bm;
sleep_disable();
}
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
why are you resetting RTC Interrupt flags if you have never used RTC before? As you're not clearing the PIN ISR flag, it just ends up in an eternal PIN ISR loop |
Beta Was this translation helpful? Give feedback.
-
I thought that's what I was doing but now that you point it out it's obvious I'm resetting the wrong thing. The need to reset an interrupt flag is new to me coming from the 328 chips. Would you mind helping me with the correct line of code for that?
…________________________________
From: MX682X ***@***.***>
Sent: Saturday, September 28, 2024 5:03 AM
To: SpenceKonde/megaTinyCore ***@***.***>
Cc: johnnylingo ***@***.***>; Author ***@***.***>
Subject: Re: [SpenceKonde/megaTinyCore] Sleep mode on tiny412 has issues (Discussion #1144)
void PCINT0(){RTC.PITINTFLAGS = RTC_PI_bm;
sleep_disable();
}
why are you resetting RTC Interrupt flags if you have never used RTC before? As you're not clearing the PIN ISR flag, it just ends up in an eternal PIN ISR loop
—
Reply to this email directly, view it on GitHub<#1144 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ABOLRV4JOB2KEVZAKBAUT3LZY2EJPAVCNFSM6AAAAABO5ZSDK2VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTANZYGMYDQMQ>.
You are receiving this because you authored the thread.
|
Beta Was this translation helpful? Give feedback.
Here is what I would have done. It contains a replacement of the the Interrupt handling, which probably would have worked as is, but attatch interrupt is always evil...