From 77d3eae4d608f4a65fee786d4f31771daec36452 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Tue, 25 Aug 2015 20:36:51 +0200 Subject: [PATCH] Re #2681 and #2626: Power off refactoring, the 9XE RTC reset prevention code now active --- radio/src/opentx.cpp | 11 ++--- radio/src/opentx.h | 54 ++++++++++++--------- radio/src/targets/simu/simpgmspace.h | 1 + radio/src/targets/taranis/board_taranis.cpp | 21 +++++++- radio/src/targets/taranis/pwr_driver.c | 39 +++++++++------ radio/src/tasks_arm.cpp | 7 +-- 6 files changed, 78 insertions(+), 55 deletions(-) diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index f3f5938f9..2d28abebd 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -1260,12 +1260,7 @@ void alert(const pm_char * t, const pm_char *s MESSAGE_SOUND_ARG) #if defined(PCBTARANIS) && defined(REV9E) uint32_t pwr_check = pwrCheck(); if (pwr_check == e_power_off) { - // TODO this is quick & dirty - lcdOff(); - BACKLIGHT_OFF(); - topLcdOff(); - SysTick->CTRL = 0; // turn off systick - pwrOff(); + boardOff(); } else if (pwr_check == e_power_press) { refresh = true; @@ -1276,7 +1271,7 @@ void alert(const pm_char * t, const pm_char *s MESSAGE_SOUND_ARG) } #else if (pwrCheck() == e_power_off) { - pwrOff(); // turn power off now + boardOff(); // turn power off now } #endif } @@ -2577,7 +2572,7 @@ int main(void) opentxClose(); lcd_clear() ; lcdRefresh() ; - pwrOff(); // Only turn power off if necessary + boardOff(); // Only turn power off if necessary wdt_disable(); while(1); // never return from main() - there is no code to return back, if any delays occurs in physical power it does dead loop. #endif diff --git a/radio/src/opentx.h b/radio/src/opentx.h index 7ef82bdfb..f9bae86ae 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -291,33 +291,39 @@ #include "debug.h" #if defined(SIMU) -#include "targets/simu/simpgmspace.h" + #include "targets/simu/simpgmspace.h" #elif defined(CPUARM) -typedef const unsigned char pm_uchar; -typedef const char pm_char; -typedef const uint16_t pm_uint16_t; -typedef const uint8_t pm_uint8_t; -typedef const int16_t pm_int16_t; -typedef const int8_t pm_int8_t; -#define pgm_read_byte(address_short) (*(uint8_t*)(address_short)) -#define PSTR(adr) adr -#define PROGMEM -#define pgm_read_adr(x) *(x) -#define cli() -#define sei() -extern void boardInit(); + typedef const unsigned char pm_uchar; + typedef const char pm_char; + typedef const uint16_t pm_uint16_t; + typedef const uint8_t pm_uint8_t; + typedef const int16_t pm_int16_t; + typedef const int8_t pm_int8_t; + #define pgm_read_byte(address_short) (*(uint8_t*)(address_short)) + #define PSTR(adr) adr + #define PROGMEM + #define pgm_read_adr(x) *(x) + #define cli() + #define sei() + extern void boardInit(); + #if defined(PCBTARANIS) + extern void boardOff(); + #else + #define boardOff() pwrOff(); + #endif #else -#include -#include -#include "pgmtypes.h" + #define boardOff() pwrOff(); + #include + #include + #include "pgmtypes.h" -#include -#include -#include -#define F_CPU 16000000UL // 16 MHz -#include -#define pgm_read_adr(address_short) pgm_read_word(address_short) -#include + #include + #include + #include + #define F_CPU 16000000UL // 16 MHz + #include + #define pgm_read_adr(address_short) pgm_read_word(address_short) + #include #endif #if defined(PCBTARANIS) diff --git a/radio/src/targets/simu/simpgmspace.h b/radio/src/targets/simu/simpgmspace.h index 2785c08e2..0b7523494 100644 --- a/radio/src/targets/simu/simpgmspace.h +++ b/radio/src/targets/simu/simpgmspace.h @@ -377,6 +377,7 @@ void eepromReadBlock (uint8_t * pointer_ram, uint32_t address, uint32_t size); #define wdt_enable(...) sleep(1/*ms*/) #define wdt_reset() sleep(1/*ms*/) #define boardInit() +#define boardOff() #define OS_MutexID pthread_mutex_t extern OS_MutexID audioMutex; diff --git a/radio/src/targets/taranis/board_taranis.cpp b/radio/src/targets/taranis/board_taranis.cpp index 6b3ec98d7..4d103d15e 100644 --- a/radio/src/targets/taranis/board_taranis.cpp +++ b/radio/src/targets/taranis/board_taranis.cpp @@ -187,7 +187,7 @@ void boardInit() lcdRefreshWait(); } if (duration < PWR_PRESS_DURATION_MIN || duration >= PWR_PRESS_DURATION_MAX) { - pwrOff(); + boardOff(); } } else { @@ -198,8 +198,27 @@ void boardInit() backlightInit(); #endif } + +void boardOff() +{ + BACKLIGHT_OFF(); +#if defined(REV9E) + topLcdOff(); #endif +#if defined(REV9E) + while (pwrPressed()) { + wdt_reset(); + } +#endif + + lcdOff(); + SysTick->CTRL = 0; // turn off systick + pwrOff(); +} + +#endif // #if !defined(SIMU) + #if defined(USB_JOYSTICK) && !defined(SIMU) extern USB_OTG_CORE_HANDLE USB_OTG_dev; diff --git a/radio/src/targets/taranis/pwr_driver.c b/radio/src/targets/taranis/pwr_driver.c index a5a64db48..1d2038a5d 100644 --- a/radio/src/targets/taranis/pwr_driver.c +++ b/radio/src/targets/taranis/pwr_driver.c @@ -37,9 +37,6 @@ #include "board_taranis.h" #include "../../pwr.h" -extern volatile uint32_t g_tmr10ms; -#define get_tmr10ms() g_tmr10ms - void pwrInit() { // if any changes are done to the PWR PIN or pwrOn() function @@ -87,23 +84,33 @@ void pwrOff() { GPIO_ResetBits(PWR_GPIO, PWR_GPIO_PIN_ON); + // disable interrupts + __disable_irq(); + + #if defined(REV9E) - // 9E needs watchdog reset because CPU is still running while the power - // key is held pressed by the user - while (1) { + // 9E needs watchdog reset because CPU is still running while + // the power key is held pressed by the user. + // The power key should be released by now, but we must make sure + while (pwrPressed()) { wdt_reset(); -#if 0 - // It doesn't work correctly, if we press long on the pwr button, the radio restarts when the button is released - PWR->CR |= PWR_CR_CWUF; - /* Select STANDBY mode */ - PWR->CR |= PWR_CR_PDDS; - /* Set SLEEPDEEP bit of Cortex System Control Register */ - SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; - /* Request Wait For Event */ - __WFE(); -#endif } + // Put the CPU into sleep to reduce the consumption, + // it might help with the RTC reset issue + PWR->CR |= PWR_CR_CWUF; + /* Select STANDBY mode */ + PWR->CR |= PWR_CR_PDDS; + /* Set SLEEPDEEP bit of Cortex System Control Register */ + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + /* Request Wait For Event */ + __WFE(); #endif + + while(1) { + wdt_reset(); + } + + //this function must not return! } #if defined(REV9E) diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index 8d3762411..25e25cf61 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -196,12 +196,7 @@ void menusTask(void * pdata) #endif opentxClose(); - -#if !defined(SIMU) - SysTick->CTRL = 0; // turn off systick -#endif - - pwrOff(); // Only turn power off if necessary + boardOff(); // Only turn power off if necessary } extern void audioTask(void* pdata);