Disable unused peripherals for power saving

This commit is contained in:
2025-11-09 17:33:26 +01:00
parent 39b2eb11e2
commit 3214f65813

53
main.c
View File

@ -46,6 +46,7 @@ volatile eMode eModeCurrent = ANIMATION_BLINK;
// Forward declarations // Forward declarations
ISR(PORTA_PORT_vect); ISR(PORTA_PORT_vect);
static void software_reset(void); static void software_reset(void);
static void configureLowPower(void);
void initPWM(void); void initPWM(void);
void setPWM_PA2(uint8_t duty); void setPWM_PA2(uint8_t duty);
static inline void leds_off(void); static inline void leds_off(void);
@ -62,6 +63,10 @@ void ledStaticFull(void);
*/ */
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(); initPWM();
@ -152,6 +157,54 @@ static inline void switchMode(void)
eModeCurrent = (eModeCurrent + 1) % MAX_COUNT; 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 * @brief Init PWM for PA2
*/ */