2011-06-13 21:17:09 +00:00
|
|
|
#include <sysinit.h>
|
|
|
|
|
|
|
|
#include "basic/basic.h"
|
2011-08-02 19:01:48 +00:00
|
|
|
#include "funk/nrf24l01p.h"
|
2011-06-13 21:17:09 +00:00
|
|
|
|
2012-01-19 22:01:00 +00:00
|
|
|
#define VOLTAGE_SAMPLES 8
|
|
|
|
static uint32_t voltage=5000*VOLTAGE_SAMPLES;
|
2011-08-04 23:22:34 +00:00
|
|
|
static uint8_t chrg=1;
|
2011-06-13 21:17:09 +00:00
|
|
|
|
|
|
|
void VoltageCheck(void){
|
2012-01-19 22:01:00 +00:00
|
|
|
uint32_t v;
|
2011-08-04 23:22:34 +00:00
|
|
|
chrg=gpioGetValue(RB_PWR_CHRG);
|
2012-01-19 22:01:00 +00:00
|
|
|
//slow down the adc for our high impedance voltage devider
|
2012-01-16 14:50:48 +00:00
|
|
|
ADC_AD0CR = ((CFG_CPU_CCLK / SCB_SYSAHBCLKDIV) / 100000 - 1 ) << 8;
|
2012-01-19 22:01:00 +00:00
|
|
|
v = adcRead(1);
|
2012-01-16 14:50:48 +00:00
|
|
|
//speed it up again
|
|
|
|
ADC_AD0CR = ((CFG_CPU_CCLK / SCB_SYSAHBCLKDIV) / 1000000 - 1 ) << 8;
|
2011-08-04 23:22:34 +00:00
|
|
|
|
2012-01-19 22:01:00 +00:00
|
|
|
v *= 10560;
|
|
|
|
v /= 1024;
|
2012-01-16 14:50:48 +00:00
|
|
|
|
|
|
|
//add the drop over the voltage switch
|
2012-01-19 22:01:00 +00:00
|
|
|
v += 50;
|
|
|
|
|
|
|
|
voltage -= voltage/VOLTAGE_SAMPLES;
|
|
|
|
voltage += v;
|
2012-01-16 14:50:48 +00:00
|
|
|
|
|
|
|
//battery is assumed empty if the volatge falls bellow 3.5V
|
2012-01-19 22:01:00 +00:00
|
|
|
if( voltage < (3500*VOLTAGE_SAMPLES) ){
|
|
|
|
//if( voltage < 3500 ){
|
2011-08-01 03:31:47 +00:00
|
|
|
nrf_off();
|
2011-06-13 21:17:09 +00:00
|
|
|
gpioSetValue (RB_PWR_GOOD, 0);
|
2012-01-16 14:50:48 +00:00
|
|
|
gpioSetValue (RB_LCD_BL, 0);
|
|
|
|
|
|
|
|
//put the chip into deep power down
|
2011-06-13 21:17:09 +00:00
|
|
|
SCB_SCR |= SCB_SCR_SLEEPDEEP;
|
|
|
|
PMU_PMUCTRL = PMU_PMUCTRL_DPDEN_DEEPPOWERDOWN;
|
|
|
|
__asm volatile ("WFI");
|
|
|
|
};
|
|
|
|
};
|
2011-06-13 21:39:21 +00:00
|
|
|
|
|
|
|
uint32_t GetVoltage(void){
|
2012-01-19 22:01:00 +00:00
|
|
|
return voltage/8;
|
|
|
|
//return voltage;
|
2011-06-13 21:39:21 +00:00
|
|
|
};
|
2011-08-04 23:22:34 +00:00
|
|
|
|
|
|
|
uint8_t GetChrgStat(void){
|
|
|
|
return !chrg;
|
|
|
|
};
|