fw: Implement power-on battery check

This commit is contained in:
Markus Koch 2020-07-31 18:23:58 +02:00
parent b4cfb40bb7
commit a396052487
4 changed files with 32 additions and 1 deletions

View File

@ -49,3 +49,28 @@ inline uint8_t batmon_get_voltage()
{
return vbat;
}
static uint8_t batmon_get_voltage_sync()
{
uint16_t sum = 0;
uint8_t cnt = 255;
ADCSRA |= (1 << ADSC);
do {
if (ADCSRA & (1 << ADIF)) {
sum += ADCH;
ADCSRA &= ~(1 << ADIF);
ADCSRA |= (1 << ADSC);
cnt--;
}
} while (cnt);
return (sum >> 8);
}
// Note: Call batmon_init() after using this function before using batmon_proc() again
inline uint8_t batmon_ok_to_boot()
{
return (batmon_get_voltage_sync() > BAT_THRES_POWER_ON);
}

View File

@ -12,5 +12,6 @@
void batmon_init();
uint8_t batmon_get_voltage();
uint8_t batmon_proc();
uint8_t batmon_ok_to_boot();
#endif // BATMON_H

View File

@ -88,6 +88,7 @@ int main()
MCUSR &= ~(1 << WDRF);
WDTCSR = (1 << WDCE);
cli();
_delay_ms(25);
suspend();

View File

@ -4,6 +4,7 @@
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "pm.h"
#include "batmon.h"
static void disable_periphery()
{
@ -58,7 +59,10 @@ void reset()
char* post()
{
// TODO: Implement battery voltage check
_delay_ms(500);
if (!batmon_ok_to_boot())
return PSTR("Low battery.");
return 0;
}