Compare commits
6 Commits
9213c814d6
...
feature/ba
| Author | SHA256 | Date | |
|---|---|---|---|
| 15fd31a6ce | |||
| 5f1141399f | |||
| d59d3bc009 | |||
| 3214f65813 | |||
| 39b2eb11e2 | |||
| 126d55ed06 |
195
main.c
195
main.c
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* @file main.c
|
||||
* @brief Bike rear light implementation for ATTINY202 with low-power standby.
|
||||
* @brief Bike rear light implementation for ATTINY202.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
@ -17,13 +18,17 @@
|
||||
#define PA6_SET_MASK 0x40 ///< Green LED pin
|
||||
#define PA7_SET_MASK 0x80 ///< Red LED
|
||||
|
||||
#define MAIN_LOOP_SLEEP 50U // Loop period in ms
|
||||
#define MAIN_LOOP_SLEEP 10U // Main loop delay in ms
|
||||
#define BUTTON_LONG_PRESS_DURATION_MS 1000U // Long press threshold
|
||||
#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
|
||||
#define INTERNAL_VREF_MV 1024UL
|
||||
|
||||
/** Convert milliseconds to system ticks */
|
||||
#define MS_TO_TICKS(ms) ((ms) / MAIN_LOOP_SLEEP)
|
||||
|
||||
typedef enum _Mode
|
||||
@ -42,6 +47,7 @@ volatile eMode eModeCurrent = ANIMATION_BLINK;
|
||||
// Forward declarations
|
||||
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);
|
||||
@ -53,27 +59,15 @@ void ledAnimationBlink(bool resetCounters);
|
||||
void ledAnimationGlow(void);
|
||||
void ledStaticFull(void);
|
||||
|
||||
/* --- Timer init: Use RTC PIT for periodic wake-up --- */
|
||||
void init_timer(void)
|
||||
{
|
||||
RTC.CLKSEL = RTC_CLKSEL_INT1K_gc; // 1 kHz ULP clock for RTC
|
||||
while (RTC.STATUS > 0)
|
||||
{
|
||||
} // Wait for sync
|
||||
|
||||
RTC.PITINTCTRL = RTC_PI_bm; // Enable PIT interrupt
|
||||
RTC.PITCTRLA = RTC_PERIOD_CYC64_gc // ≈64 ms wake-up (~50 ms)
|
||||
| RTC_PITEN_bm; // Enable PIT
|
||||
}
|
||||
|
||||
ISR(RTC_PIT_vect)
|
||||
{
|
||||
RTC.PITINTFLAGS = RTC_PI_bm; // Clear interrupt flag
|
||||
}
|
||||
|
||||
/* --- MAIN --- */
|
||||
/**
|
||||
* @brief Main entry point
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
|
||||
// Disable unused peripherals for power saving
|
||||
configureLowPower();
|
||||
|
||||
// Configure LED pins as outputs
|
||||
VPORTA.DIR = (PA1_SET_MASK | PA2_SET_MASK | PA3_SET_MASK | PA6_SET_MASK | PA7_SET_MASK);
|
||||
initPWM();
|
||||
@ -82,20 +76,14 @@ int main(void)
|
||||
VPORTA.DIR &= ~BUTTON_PIN_MASK; // Input
|
||||
PORTA.PIN0CTRL = PORT_PULLUPEN_bm; // Pull-up enabled
|
||||
|
||||
leds_off(); // Ensure all LEDs off at startup
|
||||
battery_level_indicator(); // TODO: Implement
|
||||
leds_off(); // Ensure all LEDs off at startup
|
||||
|
||||
bool bLedEnabledOld = bLedEnabled;
|
||||
eModeCurrent = ANIMATION_BLINK; // Set the mode to start with
|
||||
|
||||
cli();
|
||||
init_timer();
|
||||
sei();
|
||||
|
||||
set_sleep_mode(SLEEP_MODE_STANDBY);
|
||||
|
||||
while (1)
|
||||
while (true)
|
||||
{
|
||||
battery_level_indicator();
|
||||
bBtnPressed = handleSwitch(); // Check switch state
|
||||
|
||||
// Light LEDs while button is pressed
|
||||
@ -131,7 +119,6 @@ int main(void)
|
||||
set_sleep_mode(SLEEP_MODE_STANDBY); // Deepest sleep mode (standby)
|
||||
sleep_enable(); // Enable sleep
|
||||
|
||||
cli(); // Disable global interrupts
|
||||
sei(); // Re-enable interrupts
|
||||
sleep_cpu(); // MCU sleeps here
|
||||
}
|
||||
@ -156,8 +143,9 @@ int main(void)
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep during delay instead of busy-wait
|
||||
sleep_enable();
|
||||
sleep_cpu(); // Sleep until PIT wakes
|
||||
_delay_ms(MAIN_LOOP_SLEEP);
|
||||
sleep_disable();
|
||||
}
|
||||
}
|
||||
@ -170,6 +158,55 @@ static inline void switchMode(void)
|
||||
eModeCurrent = (eModeCurrent + 1) % MAX_COUNT;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configure for lowest power consumption
|
||||
*/
|
||||
static void configureLowPower(void)
|
||||
{
|
||||
return;
|
||||
// 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
|
||||
*/
|
||||
@ -213,13 +250,93 @@ static inline void leds_on(void)
|
||||
setPWM_PA2(255U);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read battery voltage using internal 1.1V reference
|
||||
* @return Estimated battery voltage in millivolts
|
||||
*/
|
||||
uint16_t readBatteryVoltage(void)
|
||||
{
|
||||
// Enable ADC with proper configuration
|
||||
ADC0.CTRLC = ADC_REFSEL_VDDREF_gc | // VCC as reference
|
||||
ADC_PRESC_DIV4_gc; // Prescaler DIV4
|
||||
|
||||
// 10-bit resolution (default)
|
||||
ADC0.CTRLA = ADC_RESSEL_10BIT_gc;
|
||||
|
||||
// Select internal voltage reference as input to measure
|
||||
ADC0.MUXPOS = ADC_MUXPOS_INTREF_gc;
|
||||
|
||||
// Enable ADC
|
||||
ADC0.CTRLA |= ADC_ENABLE_bm;
|
||||
|
||||
// Wait for ADC to stabilize
|
||||
_delay_us(100);
|
||||
|
||||
// Dummy conversion for stability
|
||||
ADC0.COMMAND = ADC_STCONV_bm;
|
||||
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm))
|
||||
;
|
||||
ADC0.INTFLAGS = ADC_RESRDY_bm; // Clear flag
|
||||
|
||||
// Actual conversion
|
||||
ADC0.COMMAND = ADC_STCONV_bm;
|
||||
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm))
|
||||
;
|
||||
|
||||
uint16_t adcResult = ADC0.RES;
|
||||
|
||||
// Disable ADC to save power
|
||||
ADC0.CTRLA |= !ADC_ENABLE_bm;
|
||||
|
||||
// Check for valid reading
|
||||
if (adcResult == 0 || adcResult >= 1023)
|
||||
{
|
||||
return 0; // Invalid reading
|
||||
}
|
||||
|
||||
// Calculate VCC: V_VCC = VREF × 1023 / ADC_result
|
||||
uint32_t voltage_mv = (INTERNAL_VREF_MV * 1023 / adcResult);
|
||||
|
||||
return (uint16_t)voltage_mv;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Battery monitoring
|
||||
*/
|
||||
static void battery_level_indicator(void)
|
||||
{
|
||||
// TODO: Implement
|
||||
VPORTA.OUT |= (PA6_SET_MASK | PA7_SET_MASK); // green + red OFF
|
||||
uint16_t voltage = readBatteryVoltage();
|
||||
|
||||
// 1S LiPo voltage ranges:
|
||||
// Good: >=3700mV
|
||||
// Low: >=3500mV -->
|
||||
|
||||
// VPORTA.OUT &= ~(PA6_SET_MASK | PA7_SET_MASK); // Turn off both LEDs first
|
||||
VPORTA.OUT |= (PA6_SET_MASK | PA7_SET_MASK); // Red Debug ON
|
||||
|
||||
if (voltage <= 65534)
|
||||
{
|
||||
// Green OFF, Red ON - Low battery
|
||||
//VPORTA.OUT &= ~PA7_SET_MASK; // Red Debug OFF
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
if (voltage >= 3700)
|
||||
{
|
||||
// Green ON, Red OFF - Good battery
|
||||
VPORTA.OUT &= ~PA6_SET_MASK; // Green ON (active low)
|
||||
}
|
||||
else if (voltage >= 3500)
|
||||
{
|
||||
// Both ON (yellow/orange) - Medium battery
|
||||
VPORTA.OUT &= ~(PA6_SET_MASK | PA7_SET_MASK);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Green OFF, Red ON - Low battery
|
||||
VPORTA.OUT &= ~PA7_SET_MASK; // Red ON (active low)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -421,7 +538,7 @@ void ledAnimationBlink(bool resetCounters)
|
||||
if (counter >= T250)
|
||||
{
|
||||
counter = 0;
|
||||
state = 0;
|
||||
state = 0; // restart normal sequence
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -447,14 +564,14 @@ void ledAnimationGlow(void)
|
||||
brightness += direction;
|
||||
|
||||
// Reverse direction at limits
|
||||
if (brightness >= 100U)
|
||||
if (brightness >= GLOW_BRIGHTNESS_MAX)
|
||||
{
|
||||
brightness = 100U;
|
||||
brightness = GLOW_BRIGHTNESS_MAX;
|
||||
direction = -1;
|
||||
}
|
||||
else if (brightness == 10U)
|
||||
else if (brightness == GLOW_BRIGHTNESS_MIN)
|
||||
{
|
||||
brightness = 10U;
|
||||
brightness = GLOW_BRIGHTNESS_MIN;
|
||||
direction = 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user