summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/main_menu.c2
-rw-r--r--apps/screens.c2
-rw-r--r--firmware/drivers/button.c4
-rw-r--r--firmware/export/powermgmt.h2
-rw-r--r--firmware/powermgmt.c32
5 files changed, 26 insertions, 16 deletions
diff --git a/apps/main_menu.c b/apps/main_menu.c
index deaeb9b2ec..d8818a1115 100644
--- a/apps/main_menu.c
+++ b/apps/main_menu.c
@@ -299,7 +299,7 @@ bool info_menu(void)
299#ifdef HAVE_LCD_CHARCELLS 299#ifdef HAVE_LCD_CHARCELLS
300static bool do_shutdown(void) 300static bool do_shutdown(void)
301{ 301{
302 sys_poweroff(false); 302 sys_poweroff();
303 return false; 303 return false;
304} 304}
305#endif 305#endif
diff --git a/apps/screens.c b/apps/screens.c
index bba5b279e2..b28432d1fd 100644
--- a/apps/screens.c
+++ b/apps/screens.c
@@ -1259,7 +1259,7 @@ bool shutdown_screen(void)
1259 switch(button) 1259 switch(button)
1260 { 1260 {
1261 case BUTTON_OFF: 1261 case BUTTON_OFF:
1262 sys_poweroff(false); 1262 sys_poweroff();
1263 break; 1263 break;
1264 1264
1265 default: 1265 default:
diff --git a/firmware/drivers/button.c b/firmware/drivers/button.c
index b0f8aa1e4d..4d1652984b 100644
--- a/firmware/drivers/button.c
+++ b/firmware/drivers/button.c
@@ -132,7 +132,9 @@ static void button_tick(void)
132#endif 132#endif
133 repeat_count > POWEROFF_COUNT) 133 repeat_count > POWEROFF_COUNT)
134 { 134 {
135 queue_post(&button_queue, SYS_POWEROFF, NULL); 135 /* Tell the main thread that it's time to
136 power off */
137 sys_poweroff();
136 138
137 /* Safety net for players without hardware 139 /* Safety net for players without hardware
138 poweroff */ 140 poweroff */
diff --git a/firmware/export/powermgmt.h b/firmware/export/powermgmt.h
index ecd22f4643..3c4a61fb87 100644
--- a/firmware/export/powermgmt.h
+++ b/firmware/export/powermgmt.h
@@ -134,6 +134,6 @@ int get_sleep_timer(void);
134void set_car_adapter_mode(bool setting); 134void set_car_adapter_mode(bool setting);
135void reset_poweroff_timer(void); 135void reset_poweroff_timer(void);
136void shutdown_hw(void); 136void shutdown_hw(void);
137void sys_poweroff(bool halt); 137void sys_poweroff(void);
138 138
139#endif 139#endif
diff --git a/firmware/powermgmt.c b/firmware/powermgmt.c
index c3fa8cb45f..6d6aba523e 100644
--- a/firmware/powermgmt.c
+++ b/firmware/powermgmt.c
@@ -65,6 +65,8 @@ static int wrcount;
65#define DEBUG_STACK 0 65#define DEBUG_STACK 0
66#endif 66#endif
67 67
68static int shutdown_timeout = 0;
69
68#ifdef SIMULATOR /***********************************************************/ 70#ifdef SIMULATOR /***********************************************************/
69 71
70int battery_level(void) 72int battery_level(void)
@@ -392,7 +394,7 @@ static void handle_auto_poweroff(void)
392 if(TIME_AFTER(current_tick, last_event_tick + timeout) && 394 if(TIME_AFTER(current_tick, last_event_tick + timeout) &&
393 TIME_AFTER(current_tick, last_disk_activity + timeout)) 395 TIME_AFTER(current_tick, last_disk_activity + timeout))
394 { 396 {
395 sys_poweroff(true); 397 sys_poweroff();
396 } 398 }
397 } 399 }
398 else 400 else
@@ -415,7 +417,7 @@ static void handle_auto_poweroff(void)
415#endif 417#endif
416 { 418 {
417 DEBUGF("Sleep timer timeout. Shutting off...\n"); 419 DEBUGF("Sleep timer timeout. Shutting off...\n");
418 sys_poweroff(true); 420 sys_poweroff();
419 } 421 }
420 } 422 }
421 } 423 }
@@ -524,6 +526,14 @@ static void power_thread_sleep(int ticks)
524 sleep(small_ticks); 526 sleep(small_ticks);
525 ticks -= small_ticks; 527 ticks -= small_ticks;
526 528
529 /* If the power off timeout expires, the main thread has failed
530 to shut down the system, and we need to force a power off */
531 if(shutdown_timeout) {
532 shutdown_timeout -= small_ticks;
533 if(shutdown_timeout <= 0)
534 power_off();
535 }
536
527#ifdef HAVE_ALARM_MOD 537#ifdef HAVE_ALARM_MOD
528 power_thread_rtc_process(); 538 power_thread_rtc_process();
529#endif 539#endif
@@ -568,9 +578,6 @@ static void power_thread(void)
568{ 578{
569 int i; 579 int i;
570 short *phps, *phpd; /* power history rotation pointers */ 580 short *phps, *phpd; /* power history rotation pointers */
571#if CONFIG_BATTERY == BATT_LIION2200
572 int charging_current;
573#endif
574#ifdef HAVE_CHARGE_CTRL 581#ifdef HAVE_CHARGE_CTRL
575 unsigned int target_voltage; /* desired topoff/trickle voltage level */ 582 unsigned int target_voltage; /* desired topoff/trickle voltage level */
576 int charge_max_time_now = 0; /* max. charging duration, calculated at 583 int charge_max_time_now = 0; /* max. charging duration, calculated at
@@ -606,11 +613,10 @@ static void power_thread(void)
606 tells us the charging current from the LTC1734. When DC is 613 tells us the charging current from the LTC1734. When DC is
607 connected (either via the external adapter, or via USB), we try 614 connected (either via the external adapter, or via USB), we try
608 to determine if it is actively charging or only maintaining the 615 to determine if it is actively charging or only maintaining the
609 charge. My tests show that ADC readings is below about 0x80 means 616 charge. My tests show that ADC readings below about 0x80 means
610 that the LTC1734 is only maintaining the charge. */ 617 that the LTC1734 is only maintaining the charge. */
611 if(charger_inserted()) { 618 if(charger_inserted()) {
612 charging_current = adc_read(ADC_EXT_POWER); 619 if(adc_read(ADC_EXT_POWER) < 0x80) {
613 if(charging_current < 0x80) {
614 charge_state = TRICKLE; 620 charge_state = TRICKLE;
615 } else { 621 } else {
616 charge_state = CHARGING; 622 charge_state = CHARGING;
@@ -878,12 +884,14 @@ void powermgmt_init(void)
878 884
879#endif /* SIMULATOR */ 885#endif /* SIMULATOR */
880 886
881void sys_poweroff(bool halt) 887void sys_poweroff(void)
882{ 888{
883 logf("sys_poweroff(%d)", halt); 889 logf("sys_poweroff()");
890 /* If the main thread fails to shut down the system, we will force a
891 power off after an 8 second timeout */
892 shutdown_timeout = HZ*8;
893
884 queue_post(&button_queue, SYS_POWEROFF, NULL); 894 queue_post(&button_queue, SYS_POWEROFF, NULL);
885 while(halt)
886 yield();
887} 895}
888 896
889/* Various hardware housekeeping tasks relating to shutting down the jukebox */ 897/* Various hardware housekeeping tasks relating to shutting down the jukebox */