fw: Implement charging indicator

This commit is contained in:
Markus Koch 2020-08-09 12:14:21 +02:00
parent 859c898d8a
commit 82614c65d7
5 changed files with 26 additions and 7 deletions

View File

@ -24,6 +24,10 @@ void batmon_init()
// Enable ADC
ADCSRA |= (1 << ADEN);
ADCSRA |= (1 << ADSC);
// Configure charging indicator pin
PORT_CHI(DDR) &= ~(1 << PIN_CHI);
PORT_CHI(PORT) &= ~(1 << PIN_CHI);
}
uint8_t batmon_proc()
@ -74,3 +78,8 @@ inline uint8_t batmon_ok_to_boot()
{
return (batmon_get_voltage_sync() > BAT_THRES_POWER_ON);
}
inline uint8_t batmon_is_charging()
{
return !(PORT_CHI(PIN) & PIN_CHI);
}

View File

@ -6,6 +6,9 @@
#define PORT_ADCC(t) t##C
#define PIN_ADCC (1 << 1)
#define PORT_CHI(t) t##B /* Charging indicator */
#define PIN_CHI (1 << 7)
// Nominal 3.7V: 195; Full 4.2V: 220
#define BAT_THRES_POWER_ON 175 // TODO: Minimum ADC value to allow turning the device on (3.3V)
#define BAT_THRES_POWER_OFF 165 // TODO: ADC value to (hard) power off the device. (3.1V)
@ -14,5 +17,6 @@ void batmon_init();
uint8_t batmon_get_voltage();
uint8_t batmon_proc();
uint8_t batmon_ok_to_boot();
uint8_t batmon_is_charging();
#endif // BATMON_H

View File

@ -4,7 +4,7 @@
#include "input.h"
#include "systick.h"
static volatile int8_t rotary_delta = 0;
static volatile uint8_t rotary_delta = 0;
static volatile uint8_t sw_event = 0;
void input_init()
@ -19,7 +19,7 @@ void input_init()
PORT_SBCMON(PORT) |= PIN_SBCMON;
}
inline int8_t get_rotary_delta()
inline uint8_t get_rotary_delta()
{
return rotary_delta;
}
@ -42,10 +42,12 @@ static inline void input_proc_rotary()
if (a == 1 && last_a == 0) {
silly_debounce = ROTARY_DEBOUNCE;
if (rotary_delta < 31)
rotary_delta += !!(a ^ b);
}
if (a == 0 && last_a == 1) {
silly_debounce = ROTARY_DEBOUNCE;
if (rotary_delta > 0)
rotary_delta -= !(a ^ b);
}
@ -96,7 +98,7 @@ inline uint8_t get_switch_event()
inline void input_clear_events()
{
rotary_delta = 0;
rotary_delta = 16;
sw_event = 0;
}

View File

@ -14,7 +14,7 @@
void input_init();
void input_proc();
int8_t get_rotary_delta();
uint8_t get_rotary_delta();
uint8_t get_switch_event();
void input_clear_events();
uint8_t get_sbc_state();

View File

@ -65,7 +65,7 @@ uint8_t spi_proc()
x = 0;
y = Y_MAX;
SPDR = get_switch_event();
spdr_next = get_rotary_delta();
spdr_next = get_rotary_delta() | (batmon_is_charging() << 7);
}
return spi_cs;
@ -138,6 +138,10 @@ int main()
if (batmon_proc())
reset();
}
if (PORT_KEYPAD(PIN) == 0b11111001) {
_delay_ms(500);
reset();
}
}
return 0;
}