first interrupt wakeup

This commit is contained in:
2025-10-25 14:15:08 +02:00
parent f40f533f02
commit c9d30ef44e
2 changed files with 57 additions and 1 deletions

View File

@ -76,7 +76,7 @@ Hardware: FT232 USB-UART adapter connected to UPDI with a 4.7 kΩ resistor.
```
4. Flash using pymcuprog:
```bash
pymcuprog -t uart -u /dev/ttyUSB0 -d attiny202 write -f build/main.hex
pymcuprog -t uart -u /dev/ttyUSB0 -d attiny202 erase && pymcuprog -t uart -u /dev/ttyUSB0 -d attiny202 write -f build/main.hex
```
## Hardware Setup (FT232 → ATtiny202 UPDI)
```bash

56
main.c
View File

@ -8,6 +8,10 @@
#include <stdbool.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/cpufunc.h> // for CCP
#define BUTTON_PIN_MASK 0x01 // PA0 TODO: using RESET/UPDI pin
#define PA1_SET_MASK 0x02 ///< LED 12
@ -33,17 +37,54 @@ static inline void leds_off(void);
static inline void leds_on(void);
static void battery_level_indicator(void);
static void handleSwitch(void);
int main(void);
void software_reset(void)
{
CCP = CCP_IOREG_gc; // unlock protected registers
RSTCTRL.SWRR = 1; // trigger software reset
cli(); // disable interrupts
// Unlock protected registers
CCP = CCP_IOREG_gc;
// Enable WDT with shortest period (~8 cycles)
WDT.CTRLA = WDT_PERIOD_8CLK_gc;
while (1)
; // wait for reset
}
ISR(PORTA_PORT_vect)
{
// Clear interrupt flags for PA0
PORTA.INTFLAGS = BUTTON_PIN_MASK;
if (!(VPORTA.IN & BUTTON_PIN_MASK)) // check PA0 low
{
// Turn off all LEDs
leds_off();
software_reset();
}
}
/**
* @brief Main entry point
*/
int main(void)
{
bLedEnabled = true;
bBtnPressed = false;
cli(); // disable interrupts
// register8_t backuop_dir = VPORTA.DIR;
// Configure LED pins as outputs
VPORTA.DIR = (PA1_SET_MASK | PA2_SET_MASK | PA3_SET_MASK | PA6_SET_MASK | PA7_SET_MASK);
// Configure PA0 as input with pull-up
VPORTA.DIR &= ~BUTTON_PIN_MASK; // Input
// register8_t backup_ctl = PORTA.PIN0CTRL;
PORTA.PIN0CTRL = PORT_PULLUPEN_bm; // Pull-up enabled
// Ensure all LEDs off at startup
@ -79,6 +120,21 @@ int main(void)
_delay_ms(BUTTON_IGNORE_DURATION_MS);
blinkLed(true); // reset blink state machine
leds_off();
// TODO: activate the interrupt for PA0
PORTA.PIN0CTRL = PORT_PULLUPEN_bm | PORT_ISC_FALLING_gc;
sei(); // Enable global interrupts
while (1)
{
leds_off();
_delay_ms(BUTTON_IGNORE_DURATION_MS / 20);
leds_on();
_delay_ms(BUTTON_IGNORE_DURATION_MS / 20);
}
// TODO: add a isr that disabled all leds and loops in while(1);
}
else
{