Commit ccfee457 authored by Jan Kasprzak's avatar Jan Kasprzak
Browse files

Deeper sleep when idle

parent e6421ff2
...@@ -10,8 +10,9 @@ ...@@ -10,8 +10,9 @@
//#define NUM_ADCS ZERO_ADC //#define NUM_ADCS ZERO_ADC
#define NUM_ADCS 2 #define NUM_ADCS 2
volatile static unsigned char current_adc;; volatile static unsigned char current_adc;
volatile unsigned char need_battery_adc; volatile unsigned char adc_enabled;
volatile unsigned char need_battery_adc, need_pwmled_adc;
static uint16_t adc_sum, read_zero, drop_count, read_count, n_reads_log; static uint16_t adc_sum, read_zero, drop_count, read_count, n_reads_log;
volatile uint16_t jiffies; volatile uint16_t jiffies;
...@@ -40,13 +41,23 @@ void start_next_adc() ...@@ -40,13 +41,23 @@ void start_next_adc()
drop_count = 1; drop_count = 1;
read_count = 1; read_count = 1;
n_reads_log = 0; n_reads_log = 0;
} else { } else if (need_pwmled_adc) {
current_adc = 0; current_adc = 0;
read_zero = 0; read_zero = 0;
drop_count = 1; drop_count = 1;
read_count = 1 << PWMLED_ADC_SHIFT; read_count = 1 << PWMLED_ADC_SHIFT;
n_reads_log = PWMLED_ADC_SHIFT; n_reads_log = PWMLED_ADC_SHIFT;
} else {
ADCSRA &= ~_BV(ADEN);
power_adc_disable();
adc_enabled = 0;
return;
}
if (!adc_enabled) {
power_adc_enable();
ADCSRA |= _BV(ADEN);
adc_enabled = 1;
} }
adc_sum = 0; adc_sum = 0;
...@@ -95,7 +106,9 @@ static uint16_t read_adc_sync() ...@@ -95,7 +106,9 @@ static uint16_t read_adc_sync()
void init_adc() void init_adc()
{ {
need_battery_adc = 0; need_battery_adc = 0;
need_pwmled_adc = 0;
current_adc = 0; current_adc = 0;
adc_enabled = 1;
power_adc_enable(); power_adc_enable();
ACSR |= _BV(ACD); // but disable the analog comparator ACSR |= _BV(ACD); // but disable the analog comparator
...@@ -124,6 +137,8 @@ void susp_adc() ...@@ -124,6 +137,8 @@ void susp_adc()
{ {
ADCSRA = 0; ADCSRA = 0;
DIDR0 = 0; DIDR0 = 0;
power_adc_disable();
adc_enabled = 0;
} }
ISR(ADC_vect) { // IRQ handler ISR(ADC_vect) { // IRQ handler
......
...@@ -77,8 +77,10 @@ unsigned char battery_gauge() ...@@ -77,8 +77,10 @@ unsigned char battery_gauge()
rv = 7; rv = 7;
} }
#if 0
if (rv == 1 && !initial_readings) if (rv == 1 && !initial_readings)
set_error(ERR_BATTERY); set_error(ERR_BATTERY);
#endif
#if 0 #if 0
log_byte(0xbb); log_byte(0xbb);
......
...@@ -21,13 +21,14 @@ void inline log_word(uint16_t word) { } ...@@ -21,13 +21,14 @@ void inline log_word(uint16_t word) { }
/* adc.c */ /* adc.c */
#define PWMLED_ADC_SHIFT 1 /* 1<<1 measurements per single callback */ #define PWMLED_ADC_SHIFT 1 /* 1<<1 measurements per single callback */
extern volatile unsigned char need_battery_adc; extern volatile unsigned char need_battery_adc;
extern volatile unsigned char adc_running; extern volatile unsigned char need_pwmled_adc;
extern volatile unsigned char adc_enabled;
void init_adc(); void init_adc();
void susp_adc(); void susp_adc();
/* pwm.c */ /* pwm.c */
#define PWM_MAX 0xFF #define PWM_MAX 0xFF
extern volatile unsigned char pwm_running; extern volatile unsigned char pwm_enabled;
void init_pwm(); void init_pwm();
void susp_pwm(); void susp_pwm();
void pwm_off(); void pwm_off();
......
...@@ -72,13 +72,15 @@ int main(void) ...@@ -72,13 +72,15 @@ int main(void)
#if 1 #if 1
while (1) { while (1) {
cli(); cli();
if (pwm_running) { if (pwm_enabled) {
set_sleep_mode(SLEEP_MODE_IDLE); set_sleep_mode(SLEEP_MODE_IDLE);
} else if (adc_running) { } else if (adc_enabled) {
set_sleep_mode(SLEEP_MODE_ADC); set_sleep_mode(SLEEP_MODE_ADC);
} else { } else {
set_sleep_mode(SLEEP_MODE_PWR_DOWN); set_sleep_mode(SLEEP_MODE_PWR_DOWN);
} }
sleep_enable();
// keep BOD active, no sleep_bod_disable(); // keep BOD active, no sleep_bod_disable();
sei(); sei();
sleep_cpu(); sleep_cpu();
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* Counts from 0 to 0xFF, without OCR1C compare. * Counts from 0 to 0xFF, without OCR1C compare.
*/ */
static unsigned char pwm_enabled; volatile unsigned char pwm_enabled;
static void inline enable_pll() static void inline enable_pll()
{ {
...@@ -57,6 +57,7 @@ void pwm_off() ...@@ -57,6 +57,7 @@ void pwm_off()
DDRB &= ~_BV(PB4); DDRB &= ~_BV(PB4);
PLLCSR &= ~(_BV(PLLE) | _BV(PCKE)); PLLCSR &= ~(_BV(PLLE) | _BV(PCKE));
power_timer1_disable();
pwm_enabled = 0; pwm_enabled = 0;
} }
...@@ -65,6 +66,7 @@ void pwm_set(uint8_t stride) ...@@ -65,6 +66,7 @@ void pwm_set(uint8_t stride)
OCR1B = stride; OCR1B = stride;
if (!pwm_enabled) { if (!pwm_enabled) {
power_timer1_enable();
enable_pll(); enable_pll();
DDRB |= _BV(PB4); DDRB |= _BV(PB4);
pwm_enabled = 1; pwm_enabled = 1;
......
...@@ -60,9 +60,11 @@ void pwmled_on_off(unsigned char mode) ...@@ -60,9 +60,11 @@ void pwmled_on_off(unsigned char mode)
if (mode) { if (mode) {
state = ST_ON; state = ST_ON;
mode_changed = 1; mode_changed = 1;
need_pwmled_adc = 1;
pwm_set(pwm_val); pwm_set(pwm_val);
} else { } else {
state = ST_OFF; state = ST_OFF;
need_pwmled_adc = 0;
pwm_off(); pwm_off();
} }
} }
...@@ -113,6 +115,7 @@ void pwmled_adc(uint16_t adcval) ...@@ -113,6 +115,7 @@ void pwmled_adc(uint16_t adcval)
if (pwm_val >= PWM_MAX if (pwm_val >= PWM_MAX
|| (pwm_val > (2*PWM_MAX/3) && adcval < 0x08)) { || (pwm_val > (2*PWM_MAX/3) && adcval < 0x08)) {
pwmled_err(); pwmled_err();
need_pwmled_adc = 0;
return; return;
} }
......
...@@ -17,10 +17,16 @@ void susp_wdt() ...@@ -17,10 +17,16 @@ void susp_wdt()
ISR(WDT_vect) { ISR(WDT_vect) {
++jiffies; ++jiffies;
if (pwm_enabled) {
need_pwmled_adc = 1;
}
if (jiffies & 0x000F) { if (jiffies & 0x000F) {
need_battery_adc = 1; // about every 1s need_battery_adc = 1; // about every 1s
} }
patterns_next_tick(); patterns_next_tick();
timer_check_buttons(); timer_check_buttons();
if (!adc_enabled)
start_next_adc(); // only if not running
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment