diff --git a/radio/src/targets/nv14/CMakeLists.txt b/radio/src/targets/nv14/CMakeLists.txt index 4c75bcf0d..f98789ac5 100644 --- a/radio/src/targets/nv14/CMakeLists.txt +++ b/radio/src/targets/nv14/CMakeLists.txt @@ -134,9 +134,9 @@ set(FIRMWARE_TARGET_SRC ${TOUCH_DRIVER} board.cpp backlight_driver.cpp - pwr_driver.cpp sdram_driver.c startup_stm32f42_43xxx.s + ../common/arm/stm32/pwr_driver.cpp ../common/arm/stm32/sdio_sd.c ) diff --git a/radio/src/targets/nv14/battery_driver.cpp b/radio/src/targets/nv14/battery_driver.cpp index 9a86b0955..2f92e415b 100644 --- a/radio/src/targets/nv14/battery_driver.cpp +++ b/radio/src/targets/nv14/battery_driver.cpp @@ -143,7 +143,6 @@ void handle_battery_charge(uint32_t last_press_time) static uint32_t info_until = 0; static bool lcdInited = false; - if(boardState != BOARD_POWER_OFF) return; uint32_t now = get_tmr10ms(); uint16_t chargeState = get_battery_charge_state(); if(chargeState != CHARGE_UNKNOWN) { diff --git a/radio/src/targets/nv14/board.cpp b/radio/src/targets/nv14/board.cpp index edb991d55..1e5245a8b 100644 --- a/radio/src/targets/nv14/board.cpp +++ b/radio/src/targets/nv14/board.cpp @@ -139,35 +139,39 @@ void boardInit() init2MhzTimer(); init1msTimer(); TouchInit(); + usbInit(); uint32_t press_start = 0; uint32_t press_end = 0; if (UNEXPECTED_SHUTDOWN()) { pwrOn(); - } - - while (boardState == BOARD_POWER_OFF) { - uint32_t now = get_tmr10ms(); - if (pwrPressed()) { - press_end = now; - if (press_start == 0) press_start = now; - if ((now - press_start) > POWER_ON_DELAY) { + } else { + + // prime debounce state... + usbPlugged(); + + while (usbPlugged()) { + uint32_t now = get_tmr10ms(); + if (pwrPressed()) { + press_end = now; + if (press_start == 0) press_start = now; + if ((now - press_start) > POWER_ON_DELAY) { pwrOn(); break; + } + } else { + uint32_t press_end_touch = press_end; + if (touchPanelEventOccured()) { + touchPanelRead(); + press_end_touch = get_tmr10ms(); + } + press_start = 0; + handle_battery_charge(press_end_touch); + delay_ms(20); + press_end = 0; } } - else { - uint32_t press_end_touch = press_end; - if (touchPanelEventOccured()) { - touchPanelRead(); - press_end_touch = get_tmr10ms(); - } - press_start = 0; - handle_battery_charge(press_end_touch); - delay_ms(20); - press_end = 0; - } } keysInit(); @@ -179,10 +183,8 @@ void boardInit() adcInit(&stm32_hal_adc_driver); backlightInit(); lcdInit(); - usbInit(); hapticInit(); - boardState = BOARD_STARTED; #if defined(RTCLOCK) rtcInit(); // RTC must be initialized before rambackupRestore() is called @@ -190,20 +192,27 @@ void boardInit() #if defined(DEBUG) - DBGMCU_APB1PeriphConfig(DBGMCU_IWDG_STOP|DBGMCU_TIM1_STOP|DBGMCU_TIM2_STOP|DBGMCU_TIM3_STOP|DBGMCU_TIM4_STOP|DBGMCU_TIM5_STOP|DBGMCU_TIM6_STOP|DBGMCU_TIM7_STOP|DBGMCU_TIM8_STOP|DBGMCU_TIM9_STOP|DBGMCU_TIM10_STOP|DBGMCU_TIM11_STOP|DBGMCU_TIM12_STOP|DBGMCU_TIM13_STOP|DBGMCU_TIM14_STOP, ENABLE); + DBGMCU_APB1PeriphConfig( + DBGMCU_IWDG_STOP | DBGMCU_TIM1_STOP | DBGMCU_TIM2_STOP | + DBGMCU_TIM3_STOP | DBGMCU_TIM4_STOP | DBGMCU_TIM5_STOP | + DBGMCU_TIM6_STOP | DBGMCU_TIM7_STOP | DBGMCU_TIM8_STOP | + DBGMCU_TIM9_STOP | DBGMCU_TIM10_STOP | DBGMCU_TIM11_STOP | + DBGMCU_TIM12_STOP | DBGMCU_TIM13_STOP | DBGMCU_TIM14_STOP, + ENABLE); #endif - } + void boardOff() { lcd->drawFilledRect(0, 0, LCD_WIDTH, LCD_HEIGHT, SOLID, COLOR_THEME_FOCUS); lcdOff(); + SysTick->CTRL = 0; // turn off systick + while (pwrPressed()) { WDG_RESET(); } - - + #if defined(RTC_BACKUP_RAM) RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, DISABLE); PWR_BackupRegulatorCmd(DISABLE); @@ -211,15 +220,25 @@ void boardOff() RTC->BKP0R = SHUTDOWN_REQUEST; - SysTick->CTRL = 0; // turn off systick pwrOff(); -#if !defined (SIMU) -#if defined(HAPTIC) && !defined(BOOT) - haptic.event(AU_ERROR); - delay_ms(50); -#endif - while(1) { - NVIC_SystemReset(); + + // We reach here only in forced power situations, such as hw-debugging with external power + // Enter STM32 stop mode / deep-sleep + // Code snippet from ST Nucleo PWR_EnterStopMode example +#define PDMode 0x00000000U +#if defined(PWR_CR_MRUDS) && defined(PWR_CR_LPUDS) && defined(PWR_CR_FPDS) + MODIFY_REG(PWR->CR, (PWR_CR_PDDS | PWR_CR_LPDS | PWR_CR_FPDS | PWR_CR_LPUDS | PWR_CR_MRUDS), PDMode); +#elif defined(PWR_CR_MRLVDS) && defined(PWR_CR_LPLVDS) && defined(PWR_CR_FPDS) + MODIFY_REG(PWR->CR, (PWR_CR_PDDS | PWR_CR_LPDS | PWR_CR_FPDS | PWR_CR_LPLVDS | PWR_CR_MRLVDS), PDMode); +#else + MODIFY_REG(PWR->CR, (PWR_CR_PDDS| PWR_CR_LPDS), PDMode); +#endif /* PWR_CR_MRUDS && PWR_CR_LPUDS && PWR_CR_FPDS */ + +/* Set SLEEPDEEP bit of Cortex System Control Register */ + SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk)); + + // To avoid HardFault at return address, end in an endless loop + while (1) { + } -#endif } diff --git a/radio/src/targets/nv14/board.h b/radio/src/targets/nv14/board.h index d418c9d9e..c017400f9 100644 --- a/radio/src/targets/nv14/board.h +++ b/radio/src/targets/nv14/board.h @@ -352,7 +352,6 @@ enum EnumPowerupState BOARD_REBOOT = 0xC00010FF, }; -extern uint32_t boardState; #if defined(__cplusplus) enum PowerReason { diff --git a/radio/src/targets/nv14/pwr_driver.cpp b/radio/src/targets/nv14/pwr_driver.cpp deleted file mode 100644 index a62beea7d..000000000 --- a/radio/src/targets/nv14/pwr_driver.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (C) EdgeTX - * - * Based on code named - * opentx - https://github.com/opentx/opentx - * th9x - http://code.google.com/p/th9x - * er9x - http://code.google.com/p/er9x - * gruvin9x - http://code.google.com/p/gruvin9x - * - * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - */ - -#include "pwr.h" -#include "board.h" - -uint32_t boardState __NOINIT; - -void pwrInit() -{ - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.GPIO_Pin = PWR_ON_GPIO_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; - GPIO_Init(PWR_ON_GPIO, &GPIO_InitStructure); - - // TODO move this elsewhere! - // Init Module PWR - //GPIO_ResetBits(INTMODULE_PWR_ON_GPIO, INTMODULE_PWR_ON_GPIO_PIN); - //GPIO_InitStructure.GPIO_Pin = INTMODULE_PWR_ON_GPIO_PIN; - //GPIO_Init(INTMODULE_PWR_ON_GPIO, &GPIO_InitStructure); - - //Bluetooth - GPIO_SetBits(BLUETOOTH_ON_GPIO, BLUETOOTH_ON_GPIO_PIN); - GPIO_InitStructure.GPIO_Pin = BLUETOOTH_ON_GPIO_PIN; - GPIO_Init(BLUETOOTH_ON_GPIO, &GPIO_InitStructure); - - // Init PWR SWITCH PIN - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; - GPIO_InitStructure.GPIO_Pin = PWR_SWITCH_GPIO_PIN; - GPIO_Init(PWR_ON_GPIO, &GPIO_InitStructure); - - // Init SD-DETECT PIN - GPIO_ResetBits(SD_PRESENT_GPIO, SD_PRESENT_GPIO_PIN); - GPIO_InitStructure.GPIO_Pin = SD_PRESENT_GPIO_PIN; - GPIO_Init(SD_PRESENT_GPIO, &GPIO_InitStructure); - - //turn power off - GPIO_ResetBits(PWR_ON_GPIO, PWR_ON_GPIO_PIN); - //turn Bluetooth off - GPIO_ResetBits(PWR_ON_GPIO, BLUETOOTH_ON_GPIO_PIN); - - boardState = BOARD_POWER_OFF; -} - -void pwrOn() -{ - GPIO_SetBits(PWR_ON_GPIO, PWR_ON_GPIO_PIN); - boardState = BOARD_POWER_ON; -} - -void pwrSoftReboot(){ - boardState = BOARD_REBOOT; - NVIC_SystemReset(); -} -void pwrOff() -{ - // Shutdown the Haptic - hapticDone(); - boardState = BOARD_POWER_OFF; - GPIO_ResetBits(PWR_ON_GPIO, PWR_ON_GPIO_PIN); -} - -bool pwrPressed() -{ - return GPIO_ReadInputDataBit(PWR_ON_GPIO, PWR_SWITCH_GPIO_PIN) == Bit_RESET; -} - -void pwrResetHandler() -{ - if (boardState != BOARD_POWER_OFF) { - RCC->CSR |= RCC_CSR_RMVF; //clear all flags - } -}