18 Commits
1.0.0 ... 1.2.0

Author SHA256 Message Date
d59d3bc009 Merge pull request 'Disable unused peripherals for power saving' (#5) from feature/energy-saving into main
Reviewed-on: #5
2025-11-09 17:34:29 +01:00
3214f65813 Disable unused peripherals for power saving 2025-11-09 17:33:26 +01:00
39b2eb11e2 cleanup 2025-11-09 17:18:43 +01:00
126d55ed06 Sleep during delay instead of busy-wait 2025-11-09 17:11:54 +01:00
812f9a0c96 Merge pull request 'Add more modes for LEDs' (#4) from feature/modes into main
Reviewed-on: #4
2025-11-09 13:55:52 +01:00
e2cdb7470a format 2025-11-09 13:54:26 +01:00
ca5a36449a cleanup 2025-11-08 23:35:19 +01:00
256a77b3fa get hw pwm running 2025-11-08 23:27:42 +01:00
0e5b6587a7 hw pwm is shit 2025-11-08 23:07:29 +01:00
f300ce2386 cleanup 2025-11-08 22:35:54 +01:00
55426b5f2d check mode change only on btn release 2025-11-08 22:27:49 +01:00
a13381603b static mode and broken glow animation 2025-11-08 22:16:45 +01:00
a66a3ce2dc modes 2025-11-08 22:01:10 +01:00
042d38b0f5 Merge pull request 'Disable CPU while Rear Light is not used' (#3) from feature/power-off into main
Reviewed-on: #3
2025-10-25 14:49:38 +02:00
7e8165e7a2 cleanup 2025-10-25 14:47:08 +02:00
e0781257a6 do power down 2025-10-25 14:30:43 +02:00
35c66787de cleanup 2025-10-25 14:18:03 +02:00
c9d30ef44e first interrupt wakeup 2025-10-25 14:15:08 +02:00
2 changed files with 239 additions and 27 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: 4. Flash using pymcuprog:
```bash ```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) ## Hardware Setup (FT232 → ATtiny202 UPDI)
```bash ```bash

258
main.c
View File

@ -8,8 +8,10 @@
#include <stdbool.h> #include <stdbool.h>
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#define BUTTON_PIN_MASK 0x01 // PA0 TODO: using RESET/UPDI pin #define BUTTON_PIN_MASK 0x01 // PA0 used as RESET/UPDI pin
#define PA1_SET_MASK 0x02 ///< LED 12 #define PA1_SET_MASK 0x02 ///< LED 12
#define PA2_SET_MASK 0x04 ///< LED 36 #define PA2_SET_MASK 0x04 ///< LED 36
#define PA3_SET_MASK 0x08 ///< LED 78 #define PA3_SET_MASK 0x08 ///< LED 78
@ -18,43 +20,70 @@
#define MAIN_LOOP_SLEEP 10U // Main loop delay in ms #define MAIN_LOOP_SLEEP 10U // Main loop delay in ms
#define BUTTON_LONG_PRESS_DURATION_MS 1000U // Long press threshold #define BUTTON_LONG_PRESS_DURATION_MS 1000U // Long press threshold
#define BUTTON_IGNORE_DURATION_MS 1000U // Time button ignored after long press #define BUTTON_SHORT_PRESS_DURATION_MS 50U // Short press threshold
#define BUTTON_IGNORE_DURATION_MS 2000U // Time button ignored after long press
#define BUTTON_CONFIRMATION_BLINK_LOOPS 10U // Blink animation for confirmation
#define BUTTON_CONFIRMATION_BLINK_DURATION_MS 50U
#define GLOW_BRIGHTNESS_MIN 10U
#define GLOW_BRIGHTNESS_MAX 100U
/** Convert milliseconds to system ticks */ /** Convert milliseconds to system ticks */
#define MS_TO_TICKS(ms) ((ms) / MAIN_LOOP_SLEEP) #define MS_TO_TICKS(ms) ((ms) / MAIN_LOOP_SLEEP)
typedef enum _Mode
{
ANIMATION_BLINK,
ANIMATION_GLOW,
STATIC_FULL,
MAX_COUNT,
} eMode;
/** Global flags */ /** Global flags */
volatile bool bLedEnabled = true; volatile bool bLedEnabled = true;
volatile bool bBtnPressed = false; volatile bool bBtnPressed = false;
volatile eMode eModeCurrent = ANIMATION_BLINK;
// Forward declarations // Forward declarations
void blinkLed(bool resetCounters); ISR(PORTA_PORT_vect);
static void software_reset(void);
static void configureLowPower(void);
void initPWM(void);
void setPWM_PA2(uint8_t duty);
static inline void leds_off(void); static inline void leds_off(void);
static inline void leds_on(void); static inline void leds_on(void);
static void battery_level_indicator(void); static void battery_level_indicator(void);
static void handleSwitch(void); static bool handleSwitch(void);
static inline void switchMode(void);
void ledAnimationBlink(bool resetCounters);
void ledAnimationGlow(void);
void ledStaticFull(void);
/** /**
* @brief Main entry point * @brief Main entry point
*/ */
int main(void) int main(void)
{ {
// Disable unused peripherals for power saving
configureLowPower();
// Configure LED pins as outputs // Configure LED pins as outputs
VPORTA.DIR = (PA1_SET_MASK | PA2_SET_MASK | PA3_SET_MASK | PA6_SET_MASK | PA7_SET_MASK); VPORTA.DIR = (PA1_SET_MASK | PA2_SET_MASK | PA3_SET_MASK | PA6_SET_MASK | PA7_SET_MASK);
initPWM();
// Configure PA0 as input with pull-up // Configure PA0 as input with pull-up
VPORTA.DIR &= ~BUTTON_PIN_MASK; // Input VPORTA.DIR &= ~BUTTON_PIN_MASK; // Input
PORTA.PIN0CTRL = PORT_PULLUPEN_bm; // Pull-up enabled PORTA.PIN0CTRL = PORT_PULLUPEN_bm; // Pull-up enabled
// Ensure all LEDs off at startup leds_off(); // Ensure all LEDs off at startup
leds_off();
battery_level_indicator(); // TODO: Implement battery_level_indicator(); // TODO: Implement
bool bLedEnabledOld = bLedEnabled; bool bLedEnabledOld = bLedEnabled;
eModeCurrent = ANIMATION_BLINK; // Set the mode to start with
while (true) while (true)
{ {
handleSwitch(); // Check switch state bBtnPressed = handleSwitch(); // Check switch state
// Light LEDs while button is pressed // Light LEDs while button is pressed
if (bBtnPressed) if (bBtnPressed)
@ -66,38 +95,148 @@ int main(void)
leds_off(); leds_off();
} }
// Long press detected show confirmation blink // Long press detected --> show confirmation blink
if (bLedEnabledOld != bLedEnabled) if (bLedEnabledOld != bLedEnabled)
{ {
bLedEnabledOld = bLedEnabled; bLedEnabledOld = bLedEnabled;
for (uint8_t i = 0U; i < BUTTON_CONFIRMATION_BLINK_LOOPS; i++)
{
leds_off(); leds_off();
_delay_ms(BUTTON_IGNORE_DURATION_MS / 10); _delay_ms(BUTTON_CONFIRMATION_BLINK_DURATION_MS);
leds_on(); leds_on();
_delay_ms(BUTTON_IGNORE_DURATION_MS / 10); _delay_ms(BUTTON_CONFIRMATION_BLINK_DURATION_MS);
leds_off(); leds_off();
}
// Give time until button is released
_delay_ms(BUTTON_IGNORE_DURATION_MS); _delay_ms(BUTTON_IGNORE_DURATION_MS);
blinkLed(true); // reset blink state machine // Activate the interrupt for PA0
PORTA.PIN0CTRL = PORT_PULLUPEN_bm | PORT_ISC_FALLING_gc;
set_sleep_mode(SLEEP_MODE_STANDBY); // Deepest sleep mode (standby)
sleep_enable(); // Enable sleep
sei(); // Re-enable interrupts
sleep_cpu(); // MCU sleeps here
} }
else else
{ {
if (bLedEnabled && !bBtnPressed) if (bLedEnabled && !bBtnPressed)
{ {
blinkLed(false); // run normal blink switch (eModeCurrent)
{
case ANIMATION_BLINK:
ledAnimationBlink(false); // run normal blink
break;
case ANIMATION_GLOW:
ledAnimationGlow();
break;
case STATIC_FULL:
ledStaticFull();
break;
default:
break;
}
} }
} }
// Sleep during delay instead of busy-wait
sleep_enable();
_delay_ms(MAIN_LOOP_SLEEP); _delay_ms(MAIN_LOOP_SLEEP);
sleep_disable();
} }
} }
/**
* @brief Move to next mode
*/
static inline void switchMode(void)
{
eModeCurrent = (eModeCurrent + 1) % MAX_COUNT;
}
/**
* @brief Configure for lowest power consumption
*/
static void configureLowPower(void)
{
// Set unused pins as outputs LOW to prevent floating inputs
// Floating inputs can cause extra current consumption
PORTA.DIRSET = PIN4_bm | PIN5_bm; // Set PA4, PA5 as outputs if unused
PORTA.OUTCLR = PIN4_bm | PIN5_bm; // Drive them LOW
// Configure sleep mode
set_sleep_mode(SLEEP_MODE_IDLE); // Use IDLE when TCA0 PWM needs to run
// Use SLEEP_MODE_STANDBY for deep sleep when LEDs are off
// Disable unused timers
TCB0.CTRLA = 0; // Disable TCB0 if not used
// TCA0 is used for PWM, keep it enabled
// Disable ADC (Analog-to-Digital Converter)
ADC0.CTRLA &= ~ADC_ENABLE_bm;
// Disable AC (Analog Comparator)
AC0.CTRLA &= ~AC_ENABLE_bm;
// Disable unused USART
USART0.CTRLB = 0;
// Disable TWI (I2C) if not used
TWI0.MCTRLA = 0;
TWI0.SCTRLA = 0;
// Disable SPI
SPI0.CTRLA = 0;
// Disable Watchdog Timer (if not needed)
// Note: WDT can only be disabled during first 4 clock cycles after reset
// CCP = CCP_IOREG_gc;
// WDT.CTRLA = 0;
// Disable BOD (Brown-Out Detection) in sleep modes for lower power
// This is done via fuses, not runtime configurable
// Disable digital input buffers on unused pins to save power
// Only needed if pins are truly unused (floating)
// PORTA.PIN4CTRL = PORT_ISC_INPUT_DISABLE_gc; // PA4 if unused
// PORTA.PIN5CTRL = PORT_ISC_INPUT_DISABLE_gc; // PA5 if unused
}
/**
* @brief Init PWM for PA2
*/
void initPWM(void)
{
// No PORTMUX needed - PA2 is WO2 by default
// TCA0 in normal mode (single slope PWM)
TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP2EN_bm | TCA_SINGLE_WGMODE_SINGLESLOPE_gc;
// Set period for ~19.5kHz PWM (5MHz / 256 = ~19.5kHz)
TCA0.SINGLE.PER = 0xFF;
// Start timer with DIV1 (no prescaler)
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
}
/**
* @brief Set PWM duty cycle for PA2
*/
void setPWM_PA2(uint8_t duty)
{
TCA0.SINGLE.CMP2 = duty;
}
/** /**
* @brief Turn off all controlled LEDs (PA1, PA2, PA3) * @brief Turn off all controlled LEDs (PA1, PA2, PA3)
*/ */
static inline void leds_off(void) static inline void leds_off(void)
{ {
VPORTA.OUT &= (uint8_t) ~(PA1_SET_MASK | PA2_SET_MASK | PA3_SET_MASK); VPORTA.OUT &= (uint8_t) ~(PA1_SET_MASK | PA3_SET_MASK);
setPWM_PA2(0U);
} }
/** /**
@ -105,7 +244,8 @@ static inline void leds_off(void)
*/ */
static inline void leds_on(void) static inline void leds_on(void)
{ {
VPORTA.OUT |= (PA1_SET_MASK | PA2_SET_MASK | PA3_SET_MASK); VPORTA.OUT |= (PA1_SET_MASK | PA3_SET_MASK);
setPWM_PA2(255U);
} }
/** /**
@ -120,9 +260,10 @@ static void battery_level_indicator(void)
/** /**
* @brief Handle momentary switch input on PA0 * @brief Handle momentary switch input on PA0
* *
* A long press toggles ::bLedEnabled. * A long press toggles bLedEnabled.
* A short press swiches the mode.
*/ */
static void handleSwitch(void) static bool handleSwitch(void)
{ {
static uint16_t pressTicks = 0; ///< Press duration counter static uint16_t pressTicks = 0; ///< Press duration counter
static bool prevPressed = false; ///< Previous button state static bool prevPressed = false; ///< Previous button state
@ -131,22 +272,58 @@ static void handleSwitch(void)
if (pressed) if (pressed)
{ {
bBtnPressed = true;
if (pressTicks < 0xFFFF) if (pressTicks < 0xFFFF)
pressTicks++; // Prevent overflow pressTicks++; // Prevent overflow
} }
else else
{ {
bBtnPressed = false; // Button released
if (prevPressed)
{
// Check if it was a short press (not a long press)
if (pressTicks >= MS_TO_TICKS(BUTTON_SHORT_PRESS_DURATION_MS) &&
pressTicks < MS_TO_TICKS(BUTTON_LONG_PRESS_DURATION_MS))
{
switchMode();
}
}
pressTicks = 0; pressTicks = 0;
} }
if (prevPressed && pressTicks >= MS_TO_TICKS(BUTTON_LONG_PRESS_DURATION_MS)) if (pressed && pressTicks >= MS_TO_TICKS(BUTTON_LONG_PRESS_DURATION_MS))
{ {
bLedEnabled = !bLedEnabled; // Toggle LED blinking bLedEnabled = !bLedEnabled; // Toggle LED blinking
} }
prevPressed = pressed; prevPressed = pressed;
return pressed;
}
/**
* @brief Perform software reset
*/
static void software_reset(void)
{
CCP = CCP_IOREG_gc; // unlock protected registers
RSTCTRL.SWRR = 1; // trigger software reset
while (1)
; // wait for reset
}
/**
* @brief Interrupt service routine
*/
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
software_reset();
}
} }
/** /**
@ -164,7 +341,7 @@ static void handleSwitch(void)
* 18: all off, wait 250 ms → restart * 18: all off, wait 250 ms → restart
* Special: every 3rd cycle, all LEDs ON for 250 ms * Special: every 3rd cycle, all LEDs ON for 250 ms
*/ */
void blinkLed(bool resetCounters) void ledAnimationBlink(bool resetCounters)
{ {
const uint8_t T50 = MS_TO_TICKS(50); const uint8_t T50 = MS_TO_TICKS(50);
const uint8_t T100 = MS_TO_TICKS(100); const uint8_t T100 = MS_TO_TICKS(100);
@ -231,7 +408,7 @@ void blinkLed(bool resetCounters)
break; break;
case 12: // LED 36 on, wait 50 ms case 12: // LED 36 on, wait 50 ms
VPORTA.OUT |= PA2_SET_MASK; setPWM_PA2(255U);
if (counter >= T50) if (counter >= T50)
{ {
counter = 0; counter = 0;
@ -249,7 +426,7 @@ void blinkLed(bool resetCounters)
break; break;
case 16: // LED 36 on, wait 50 ms case 16: // LED 36 on, wait 50 ms
VPORTA.OUT |= PA2_SET_MASK; setPWM_PA2(255U);
if (counter >= T50) if (counter >= T50)
{ {
counter = 0; counter = 0;
@ -275,7 +452,7 @@ void blinkLed(bool resetCounters)
break; break;
case 20: // special: all LEDs on for 250 ms case 20: // special: all LEDs on for 250 ms
VPORTA.OUT |= (PA1_SET_MASK | PA2_SET_MASK | PA3_SET_MASK); leds_on();
if (counter >= T250) if (counter >= T250)
{ {
counter = 0; counter = 0;
@ -284,3 +461,38 @@ void blinkLed(bool resetCounters)
break; break;
} }
} }
/**
* @brief All LEDs with static full power
*/
void ledStaticFull(void)
{
leds_on();
}
/**
* @brief Inner LEDs with glow animation
*/
void ledAnimationGlow(void)
{
static uint8_t brightness = 0;
static int8_t direction = 1;
// Update brightness level every call (10ms)
brightness += direction;
// Reverse direction at limits
if (brightness >= GLOW_BRIGHTNESS_MAX)
{
brightness = GLOW_BRIGHTNESS_MAX;
direction = -1;
}
else if (brightness == GLOW_BRIGHTNESS_MIN)
{
brightness = GLOW_BRIGHTNESS_MIN;
direction = 1;
}
// Apply PWM brightness to LED 3-6
setPWM_PA2(brightness * 255 / 100); // Scale 0-100 to 0-255
}