diff --git a/radio/src/CMakeLists.txt b/radio/src/CMakeLists.txt index e3b89c9d7..4098c966b 100644 --- a/radio/src/CMakeLists.txt +++ b/radio/src/CMakeLists.txt @@ -325,7 +325,7 @@ set(SRC mixer.cpp stamp.cpp timers.cpp - trainer_input.cpp + trainer.cpp ) if(GUI) diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index 43abbc78f..ccdf1ab7f 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -954,14 +954,14 @@ void checkThrottleStick() } #if defined(PWR_BUTTON_PRESS) - uint32_t pwr_check = pwrCheck(); - if (pwr_check == e_power_off) { + uint32_t power = pwrCheck(); + if (power == e_power_off) { break; } - else if (pwr_check == e_power_press) { + else if (power == e_power_press) { refresh = true; } - else if (pwr_check == e_power_on && refresh) { + else if (power == e_power_on && refresh) { RAISE_ALERT(STR_THROTTLEWARN, STR_THROTTLENOTIDLE, STR_PRESSANYKEYTOSKIP, AU_NONE); refresh = false; } @@ -1886,10 +1886,6 @@ void opentxInit() #endif backlightOn(); -#if defined(PCBSKY9X) && !defined(SIMU) - init_trainer_capture(); -#endif - startPulses(); wdt_enable(WDTO_500MS); diff --git a/radio/src/opentx.h b/radio/src/opentx.h index 918cb6822..10f7c872b 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -741,7 +741,7 @@ extern uint8_t g_vbat100mV; #define g_blinkTmr10ms (*(uint8_t*)&g_tmr10ms) extern uint8_t g_beepCnt; -#include "trainer_input.h" +#include "trainer.h" extern int32_t chans[MAX_OUTPUT_CHANNELS]; extern int16_t ex_chans[MAX_OUTPUT_CHANNELS]; // Outputs (before LIMITS) of the last perMain diff --git a/radio/src/switches.cpp b/radio/src/switches.cpp index 38ae149b0..de552d87f 100644 --- a/radio/src/switches.cpp +++ b/radio/src/switches.cpp @@ -25,10 +25,9 @@ #if defined(PCBHORUS) #define SWITCH_WARNING_LIST_X WARNING_LINE_X #define SWITCH_WARNING_LIST_Y WARNING_LINE_Y+3*FH - #define SWITCH_WARNING_LIST_INTERVAL 35 #elif LCD_W >= 212 #define SWITCH_WARNING_LIST_X 60 - #define SWITCH_WARNING_LIST_Y 4*FH+3 + #define SWITCH_WARNING_LIST_Y 4*FH+4 #else #define SWITCH_WARNING_LIST_X 4 #define SWITCH_WARNING_LIST_Y 4*FH+4 @@ -558,6 +557,10 @@ void checkSwitches() uint8_t bad_pots = 0, last_bad_pots = 0xff; #endif +#if defined(PWR_BUTTON_PRESS) + bool refresh = false; +#endif + while (1) { #if defined(PCBTARANIS) || defined(PCBHORUS) @@ -642,12 +645,13 @@ void checkSwitches() // first - display warning #if defined(PCBTARANIS) || defined(PCBHORUS) - if ((last_bad_switches != switches_states) || (last_bad_pots != bad_pots)) { - drawAlertBox(STR_SWITCHWARN, NULL, STR_PRESSANYKEYTOSKIP); + if (last_bad_switches != switches_states || last_bad_pots != bad_pots) { + drawAlertBox(STR_SWITCHWARN, nullptr, STR_PRESSANYKEYTOSKIP); if (last_bad_switches == 0xff || last_bad_pots == 0xff) { AUDIO_ERROR_MESSAGE(AU_SWITCH_ALERT); } - int x = SWITCH_WARNING_LIST_X, y = SWITCH_WARNING_LIST_Y; + int x = SWITCH_WARNING_LIST_X; + int y = SWITCH_WARNING_LIST_Y; int numWarnings = 0; for (int i=0; i> (i*2)]; drawSource(x, y, MIXSRC_FIRST_SWITCH+i, attr); lcdDrawChar(lcdNextPos, y, c, attr); x = lcdNextPos + 3; } - else if (numWarnings == 7) { - lcdDrawText(x, y, "...", 0); - } } } #endif } if (g_model.potsWarnMode) { - if (y == 4*FH+3) { - y = 6*FH-2; - x = 60; - } for (int i=0; i 1) { -#if defined(COLORLCD) - char s[8]; - // TODO add an helper - strncpy(s, &STR_VSRCRAW[1+(NUM_STICKS+1+i)*STR_VSRCRAW[0]], STR_VSRCRAW[0]); - s[int(STR_VSRCRAW[0])] = '\0'; -#else - lcdDrawTextAtIndex(x, y, STR_VSRCRAW, NUM_STICKS+1+i, INVERS); - if (IS_POT(POT1+i)) - lcdDrawChar(lcdNextPos, y, g_model.potsWarnPosition[i] > GET_LOWRES_POT_POSITION(i) ? 126 : 127, INVERS); - else - lcdDrawChar(lcdNextPos, y, g_model.potsWarnPosition[i] > GET_LOWRES_POT_POSITION(i) ? '\300' : '\301', INVERS); -#endif -#if defined(COLORLCD) if (++numWarnings < 6) { +#if defined(COLORLCD) + char s[8]; + // TODO add an helper + strncpy(s, &STR_VSRCRAW[1+(NUM_STICKS+1+i)*STR_VSRCRAW[0]], STR_VSRCRAW[0]); + s[int(STR_VSRCRAW[0])] = '\0'; lcdDrawText(x, y, s, ALARM_COLOR); - } - else if (numWarnings == 6) { - lcdDrawText(x, y, "...", ALARM_COLOR); - } - x += 40; + x += 40; #else - x = lcdNextPos + 3; + lcdDrawTextAtIndex(x, y, STR_VSRCRAW, NUM_STICKS + 1 + i, INVERS); + if (IS_POT(POT1 + i)) + lcdDrawChar(lcdNextPos, y, g_model.potsWarnPosition[i] > GET_LOWRES_POT_POSITION(i) ? 126 : 127, INVERS); + else + lcdDrawChar(lcdNextPos, y, g_model.potsWarnPosition[i] > GET_LOWRES_POT_POSITION(i) ? '\300' : '\301', INVERS); + x = lcdNextPos + 3; #endif + } } - } } } + + if (numWarnings >= 6) { +#if defined(COLORLCD) + lcdDrawText(x, y, "...", ALARM_COLOR); +#else + lcdDrawText(x, y, "...", 0); +#endif + } + last_bad_pots = bad_pots; #else if (last_bad_switches != switches_states) { @@ -747,7 +743,27 @@ void checkSwitches() last_bad_switches = switches_states; } - if (pwrCheck() == e_power_off || keyDown()) break; + if (keyDown()) + break; + +#if defined(PWR_BUTTON_PRESS) + uint32_t power = pwrCheck(); + if (power == e_power_off) { + break; + } + else if (power == e_power_press) { + refresh = true; + } + else if (power == e_power_on && refresh) { + last_bad_switches = 0xff; + last_bad_pots = 0xff; + refresh = false; + } +#else + if (pwrCheck() == e_power_off) { + break; + } +#endif doLoopCommonActions(); diff --git a/radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt b/radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt index fec7b825f..68002e0d0 100644 --- a/radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt +++ b/radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt @@ -70,7 +70,7 @@ set(BOOTLOADER_SRC ../../../../../targets/${TARGET_DIR}/backlight_driver.cpp ../../../../../targets/${TARGET_DIR}/keys_driver.cpp ../../../../../targets/${TARGET_DIR}/diskio.cpp - ../../../../../targets/${TARGET_DIR}/board_preinit.cpp + ../../../../../targets/${TARGET_DIR}/board.cpp ../../../../../targets/${TARGET_DIR}/pwr_driver.cpp ../../../../../targets/${TARGET_DIR}/bootloader/boot_menu.cpp ../usbd_usr.cpp diff --git a/radio/src/targets/common/arm/stm32/bootloader/boot.cpp b/radio/src/targets/common/arm/stm32/bootloader/boot.cpp index f1421abef..679317779 100644 --- a/radio/src/targets/common/arm/stm32/bootloader/boot.cpp +++ b/radio/src/targets/common/arm/stm32/bootloader/boot.cpp @@ -209,8 +209,8 @@ int main() RCC_APB2PeriphClockCmd(LCD_RCC_APB2Periph | BACKLIGHT_RCC_APB2Periph | RCC_APB2Periph_SYSCFG, ENABLE); keysInit(); - - boardPreInit(); + pwrInit(); + pwrOff(); // wait for inputs to stabilize for (uint32_t i = 0; i < 50000; i += 1) { @@ -227,7 +227,6 @@ int main() rotaryEncoderInit(); #endif - pwrInit(); pwrOn(); delaysInit(); // needed for lcdInit() @@ -489,12 +488,7 @@ int main() if (state != ST_FLASHING && state != ST_USB) { if (pwrOffPressed()) { - lcdClear(); - lcdOff(); // this drains LCD caps - pwrOff(); - for (;;) { - // Wait for power to go off - } + boardOff(); } } diff --git a/radio/src/targets/horus/CMakeLists.txt b/radio/src/targets/horus/CMakeLists.txt index e94df176b..7525f6d25 100644 --- a/radio/src/targets/horus/CMakeLists.txt +++ b/radio/src/targets/horus/CMakeLists.txt @@ -108,10 +108,12 @@ set(GUI_SRC ${WIDGETS_SRC} rle.cpp ) + if(DISK_CACHE) set(SRC ${SRC} disk_cache.cpp) add_definitions(-DDISK_CACHE) endif() + if(INTERNAL_GPS) set(SRC ${SRC} gps.cpp) add_definitions(-DINTERNAL_GPS) @@ -128,6 +130,7 @@ set(TARGET_SRC led_driver.cpp extmodule_driver.cpp trainer_driver.cpp + ../common/arm/stm32/heartbeat_driver.cpp ../common/arm/stm32/timers_driver.cpp ../common/arm/stm32/intmodule_serial_driver.cpp ../common/arm/stm32/bluetooth_driver.cpp diff --git a/radio/src/targets/horus/board.cpp b/radio/src/targets/horus/board.cpp index 51b22889a..8ba0c5278 100644 --- a/radio/src/targets/horus/board.cpp +++ b/radio/src/targets/horus/board.cpp @@ -29,6 +29,10 @@ extern "C" { } #endif +uint32_t shutdownRequest; // Stores intentional shutdown to avoid reboot loop +uint32_t shutdownReason; // Used for detecting unexpected reboots regardless of reason +uint32_t powerupReason __NOINIT; // Stores power up reason beyond initialization for emergency mode activation + HardwareOptions hardwareOptions; void watchdogInit(unsigned int duration) @@ -122,6 +126,7 @@ void boardInit() ENABLE); pwrInit(); + pwrOn(); delaysInit(); #if defined(DEBUG) @@ -184,52 +189,35 @@ void boardInit() void boardOff() { - BACKLIGHT_DISABLE(); + backlightEnable(0); while (pwrPressed()) { wdt_reset(); } SysTick->CTRL = 0; // turn off systick + +#if defined(PCBX12S) + // Shutdown the Audio amp + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Pin = AUDIO_SHUTDOWN_GPIO_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Init(AUDIO_SHUTDOWN_GPIO, &GPIO_InitStructure); + GPIO_ResetBits(AUDIO_SHUTDOWN_GPIO, AUDIO_SHUTDOWN_GPIO_PIN); +#endif + + // Shutdown the Haptic + hapticDone(); + + shutdownRequest = SHUTDOWN_REQUEST; + shutdownReason = NORMAL_POWER_OFF; + pwrOff(); } -uint8_t currentTrainerMode = 0xff; - -void checkTrainerSettings() -{ - uint8_t requiredTrainerMode = g_model.trainerData.mode; - if (requiredTrainerMode != currentTrainerMode) { - switch (currentTrainerMode) { - case TRAINER_MODE_MASTER_TRAINER_JACK: - stop_trainer_capture(); - break; - case TRAINER_MODE_SLAVE: - stop_trainer_ppm(); - break; - case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: - auxSerialStop(); - } - - currentTrainerMode = requiredTrainerMode; - switch (requiredTrainerMode) { - case TRAINER_MODE_SLAVE: - init_trainer_ppm(); - break; - case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: - if (g_eeGeneral.auxSerialMode == UART_MODE_SBUS_TRAINER) { - auxSerialSbusInit(); - break; - } - // no break - default: - // master is default - init_trainer_capture(); - break; - } - } -} - uint16_t getBatteryVoltage() { int32_t instant_vbat = anaIn(TX_VOLTAGE); // using filtered ADC value on purpose diff --git a/radio/src/targets/horus/board.h b/radio/src/targets/horus/board.h index 8eceb5587..a3acfc140 100644 --- a/radio/src/targets/horus/board.h +++ b/radio/src/targets/horus/board.h @@ -108,7 +108,6 @@ extern uint16_t sessionTimer; #endif // Board driver -void boardPreInit(void); void boardInit(void); void boardOff(void); @@ -219,6 +218,9 @@ void init_pxx2(uint8_t module); void disable_pxx2(uint8_t module); void disable_serial(uint8_t module); +void init_intmodule_heartbeat(); +void check_intmodule_heartbeat(); + void intmoduleStop(); void intmoduleSerialStart(uint32_t baudrate, uint8_t rxEnable); void intmodulePxxStart(); @@ -507,6 +509,8 @@ extern "C" { // Power driver #define SOFT_PWR_CTRL +extern uint32_t shutdownRequest; // Stores intentional shutdown to avoid reboot loop +extern uint32_t shutdownReason; // Used for detecting unexpected reboots regardless of reason void pwrInit(void); uint32_t pwrCheck(void); void pwrOn(void); @@ -655,9 +659,6 @@ void bluetoothWriteWakeup(void); uint8_t bluetoothIsWriting(void); void bluetoothDisable(void); -extern uint8_t currentTrainerMode; -void checkTrainerSettings(void); - #if defined(__cplusplus) #include "fifo.h" #include "dmafifo.h" diff --git a/radio/src/targets/horus/board_preinit.cpp b/radio/src/targets/horus/board_preinit.cpp deleted file mode 100644 index 54a24cb21..000000000 --- a/radio/src/targets/horus/board_preinit.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) OpenTX - * - * Based on code named - * 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 "board.h" - -void boardPreInit() -{ -} diff --git a/radio/src/targets/horus/hal.h b/radio/src/targets/horus/hal.h index 007fefec7..75d8a5c81 100644 --- a/radio/src/targets/horus/hal.h +++ b/radio/src/targets/horus/hal.h @@ -630,10 +630,16 @@ #endif // Heartbeat (not used) -#define TRAINER_MODULE_RCC_AHB1Periph RCC_AHB1Periph_GPIOD -#define INTMODULE_HEARTBEAT_GPIO GPIOD -#define INTMODULE_HEARTBEAT_GPIO_PIN GPIO_Pin_12 // PD.12 -#define INTMODULE_HEARTBEAT_TRIGGER EXTI_Trigger_Falling +#define INTMODULE_HEARTBEAT +#define INTMODULE_HEARTBEAT_RCC_AHB1Periph RCC_AHB1Periph_GPIOD +#define INTMODULE_HEARTBEAT_GPIO GPIOD +#define INTMODULE_HEARTBEAT_GPIO_PIN GPIO_Pin_12 +#define INTMODULE_HEARTBEAT_EXTI_PortSource EXTI_PortSourceGPIOD +#define INTMODULE_HEARTBEAT_EXTI_PinSource GPIO_PinSource12 +#define INTMODULE_HEARTBEAT_EXTI_LINE EXTI_Line12 +#define INTMODULE_HEARTBEAT_EXTI_IRQn EXTI15_10_IRQn +#define INTMODULE_HEARTBEAT_REUSE_INTERRUPT_ROTARY_ENCODER +#define INTMODULE_HEARTBEAT_TRIGGER EXTI_Trigger_Falling // Trainer Port #define TRAINER_RCC_AHB1Periph (RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA1) diff --git a/radio/src/targets/horus/pwr_driver.cpp b/radio/src/targets/horus/pwr_driver.cpp index 71ee994e4..b6bc505f7 100644 --- a/radio/src/targets/horus/pwr_driver.cpp +++ b/radio/src/targets/horus/pwr_driver.cpp @@ -21,10 +21,6 @@ #include "pwr.h" #include "board.h" -uint32_t shutdownRequest; // Stores intentional shutdown to avoid reboot loop -uint32_t shutdownReason; // Used for detecting unexpected reboots regardless of reason -uint32_t powerupReason __NOINIT; // Stores power up reason beyond initialization for emergency mode activation - void pwrInit() { GPIO_InitTypeDef GPIO_InitStructure; @@ -86,23 +82,6 @@ void pwrOn() void pwrOff() { -#if defined(PCBX12S) - // Shutdown the Audio amp - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.GPIO_Pin = AUDIO_SHUTDOWN_GPIO_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; - GPIO_Init(AUDIO_SHUTDOWN_GPIO, &GPIO_InitStructure); - GPIO_ResetBits(AUDIO_SHUTDOWN_GPIO, AUDIO_SHUTDOWN_GPIO_PIN); -#endif - - // Shutdown the Haptic - hapticDone(); - - shutdownRequest = SHUTDOWN_REQUEST; - shutdownReason = NORMAL_POWER_OFF; GPIO_ResetBits(PWR_ON_GPIO, PWR_ON_GPIO_PIN); } diff --git a/radio/src/targets/sky9x/board.cpp b/radio/src/targets/sky9x/board.cpp index 93f96e491..d25f83fda 100644 --- a/radio/src/targets/sky9x/board.cpp +++ b/radio/src/targets/sky9x/board.cpp @@ -498,16 +498,6 @@ void calcConsumption() } } -void checkTrainerSettings() -{ - if (SLAVE_MODE()) { - PIOC->PIO_PDR = PIO_PC22; - } - else { - PIOC->PIO_PER = PIO_PC22; - } -} - uint16_t getBatteryVoltage() { int32_t instant_vbat = anaIn(TX_VOLTAGE); // using filtered ADC value on purpose diff --git a/radio/src/targets/sky9x/board.h b/radio/src/targets/sky9x/board.h index e46da5850..37aa4d47d 100644 --- a/radio/src/targets/sky9x/board.h +++ b/radio/src/targets/sky9x/board.h @@ -34,7 +34,6 @@ extern uint16_t ResetReason; #define FIRMWARE_ADDRESS 0x00400000 // Board driver -void boardPreInit(void); void boardInit(void); #define boardOff() pwrOff() @@ -212,8 +211,8 @@ void calcConsumption(); // Trainer driver #define SLAVE_MODE() (pwrCheck() == e_power_trainer) #define TRAINER_CONNECTED() (PIOA->PIO_PDSR & PIO_PA8) -void checkTrainerSettings(); void init_trainer_capture(); +void stop_trainer_capture(); // Write Flash driver #define FLASH_PAGESIZE 256 diff --git a/radio/src/targets/sky9x/trainer_driver.cpp b/radio/src/targets/sky9x/trainer_driver.cpp index 151be4b30..e47a9e549 100644 --- a/radio/src/targets/sky9x/trainer_driver.cpp +++ b/radio/src/targets/sky9x/trainer_driver.cpp @@ -82,14 +82,14 @@ extern "C" void TC3_IRQHandler() //capture ppm in at 2MHz void init_trainer_capture() { + PIOC->PIO_PER = PIO_PC22; start_timer4() ; start_timer3() ; } -#if 0 void stop_trainer_capture() { + PIOC->PIO_PDR = PIO_PC22; TC1->TC_CHANNEL[0].TC_IDR = TC_IDR0_LDRAS ; NVIC_DisableIRQ(TC3_IRQn) ; } -#endif diff --git a/radio/src/targets/taranis/board.cpp b/radio/src/targets/taranis/board.cpp index 5bc5def1d..585241221 100644 --- a/radio/src/targets/taranis/board.cpp +++ b/radio/src/targets/taranis/board.cpp @@ -269,13 +269,13 @@ void boardInit() void boardOff() { -#if defined(STATUS_LEDS) +#if defined(STATUS_LEDS) && !defined(BOOT) ledOff(); #endif BACKLIGHT_DISABLE(); -#if defined(TOPLCD_GPIO) +#if defined(TOPLCD_GPIO) && !defined(BOOT) toplcdOff(); #endif @@ -288,64 +288,33 @@ void boardOff() lcdOff(); SysTick->CTRL = 0; // turn off systick pwrOff(); -} -uint8_t currentTrainerMode = 0xff; + // disable interrupts + __disable_irq(); -void checkTrainerSettings() -{ - uint8_t requiredTrainerMode = g_model.trainerData.mode; - if (requiredTrainerMode != currentTrainerMode) { - switch (currentTrainerMode) { - case TRAINER_MODE_MASTER_TRAINER_JACK: - stop_trainer_capture(); - break; - case TRAINER_MODE_SLAVE: - stop_trainer_ppm(); - break; - case TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE: - stop_trainer_module_cppm(); - break; - case TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE: - stop_trainer_module_sbus(); - break; -#if defined(TRAINER_BATTERY_COMPARTMENT) - case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: - auxSerialStop(); - break; -#endif + while (1) { + wdt_reset(); +#if defined(SIMU) + return; +#elif defined(PWR_BUTTON_PRESS) + // X9E/X7 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 + if (!pwrPressed()) { + // 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(); } - - currentTrainerMode = requiredTrainerMode; - switch (requiredTrainerMode) { - case TRAINER_MODE_SLAVE: - init_trainer_ppm(); - break; - case TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE: - init_trainer_module_cppm(); - break; - case TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE: - init_trainer_module_sbus(); - break; -#if defined(TRAINER_BATTERY_COMPARTMENT) - case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: - if (g_eeGeneral.auxSerialMode == UART_MODE_SBUS_TRAINER) { - auxSerialSbusInit(); - break; - } - // no break #endif - default: - // master is default - init_trainer_capture(); - break; - } - - if (requiredTrainerMode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE || requiredTrainerMode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE) - stop_intmodule_heartbeat(); - else - init_intmodule_heartbeat(); } + + // this function must not return! } uint16_t getBatteryVoltage() diff --git a/radio/src/targets/taranis/board.h b/radio/src/targets/taranis/board.h index f1a152e2b..6e8148b08 100644 --- a/radio/src/targets/taranis/board.h +++ b/radio/src/targets/taranis/board.h @@ -120,7 +120,6 @@ void rotaryEncoderCheck(void); extern uint16_t sessionTimer; // Board driver -void boardPreInit(void); void boardInit(void); void boardOff(void); @@ -249,10 +248,10 @@ void extmoduleSendNextFrame(); #define TRAINER_CONNECTED() (GPIO_ReadInputDataBit(TRAINER_DETECT_GPIO, TRAINER_DETECT_GPIO_PIN) == Bit_RESET) #endif #if defined(TRAINER_GPIO) - void init_trainer_ppm(void); - void stop_trainer_ppm(void); - void init_trainer_capture(void); - void stop_trainer_capture(void); + void init_trainer_ppm(); + void stop_trainer_ppm(); + void init_trainer_capture(); + void stop_trainer_capture(); #else #define init_trainer_ppm() #define stop_trainer_ppm() @@ -260,15 +259,15 @@ void extmoduleSendNextFrame(); #define stop_trainer_capture() #endif #if defined(TRAINER_MODULE_CPPM) - void init_trainer_module_cppm(void); - void stop_trainer_module_cppm(void); + void init_trainer_module_cppm(); + void stop_trainer_module_cppm(); #else #define init_trainer_module_cppm() #define stop_trainer_module_cppm() #endif #if defined(TRAINER_MODULE_SBUS) - void init_trainer_module_sbus(void); - void stop_trainer_module_sbus(void); + void init_trainer_module_sbus(); + void stop_trainer_module_sbus(); #else #define init_trainer_module_sbus() #define stop_trainer_module_sbus() @@ -877,9 +876,6 @@ void setTopBatteryValue(uint32_t volts); #define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE) -extern uint8_t currentTrainerMode; -void checkTrainerSettings(void); - #if defined(__cplusplus) #include "fifo.h" #include "dmafifo.h" diff --git a/radio/src/targets/taranis/board_preinit.cpp b/radio/src/targets/taranis/board_preinit.cpp deleted file mode 100644 index 116c2577d..000000000 --- a/radio/src/targets/taranis/board_preinit.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) OpenTX - * - * Based on code named - * 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 "board.h" - -void boardPreInit() -{ - GPIO_InitTypeDef GPIO_InitStructure; - 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; - - // Board PWR kept OFF - GPIO_ResetBits(PWR_ON_GPIO, PWR_ON_GPIO_PIN); - GPIO_InitStructure.GPIO_Pin = PWR_ON_GPIO_PIN; - GPIO_Init(PWR_ON_GPIO, &GPIO_InitStructure); - -#if defined(INTMODULE_BOOTCMD_GPIO) - GPIO_SetBits(INTMODULE_BOOTCMD_GPIO, INTMODULE_BOOTCMD_GPIO_PIN); - GPIO_InitStructure.GPIO_Pin = INTMODULE_BOOTCMD_GPIO_PIN; - GPIO_Init(INTMODULE_BOOTCMD_GPIO, &GPIO_InitStructure); -#endif -} diff --git a/radio/src/targets/taranis/pwr_driver.cpp b/radio/src/targets/taranis/pwr_driver.cpp index f0e9855be..b74e0d430 100644 --- a/radio/src/targets/taranis/pwr_driver.cpp +++ b/radio/src/targets/taranis/pwr_driver.cpp @@ -28,16 +28,30 @@ void pwrInit() GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; + // Board PWR + GPIO_InitStructure.GPIO_Pin = PWR_ON_GPIO_PIN; + GPIO_Init(PWR_ON_GPIO, &GPIO_InitStructure); + +#if defined(INTMODULE_BOOTCMD_GPIO) + GPIO_SetBits(INTMODULE_BOOTCMD_GPIO, INTMODULE_BOOTCMD_GPIO_PIN); + GPIO_InitStructure.GPIO_Pin = INTMODULE_BOOTCMD_GPIO_PIN; + GPIO_Init(INTMODULE_BOOTCMD_GPIO, &GPIO_InitStructure); +#endif + + // Internal module power GPIO_ResetBits(INTMODULE_PWR_GPIO, INTMODULE_PWR_GPIO_PIN); GPIO_InitStructure.GPIO_Pin = INTMODULE_PWR_GPIO_PIN; GPIO_Init(INTMODULE_PWR_GPIO, &GPIO_InitStructure); + // External module power EXTERNAL_MODULE_PWR_OFF(); GPIO_InitStructure.GPIO_Pin = EXTMODULE_PWR_GPIO_PIN; GPIO_Init(EXTMODULE_PWR_GPIO, &GPIO_InitStructure); - GPIO_InitStructure.GPIO_Pin = PWR_SWITCH_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + + // PWR switch + GPIO_InitStructure.GPIO_Pin = PWR_SWITCH_GPIO_PIN; GPIO_Init(PWR_SWITCH_GPIO, &GPIO_InitStructure); #if defined(TRAINER_DETECT_GPIO_PIN) @@ -67,30 +81,6 @@ void pwrOn() void pwrOff() { GPIO_ResetBits(PWR_ON_GPIO, PWR_ON_GPIO_PIN); - - // disable interrupts - __disable_irq(); - - while (1) { - wdt_reset(); -#if defined(PWR_BUTTON_PRESS) - // X9E/X7 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 - if (!pwrPressed()) { - // 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 - } - // this function must not return! } uint32_t pwrPressed() diff --git a/radio/src/tasks.cpp b/radio/src/tasks.cpp index d0d02b9fe..140e4676c 100644 --- a/radio/src/tasks.cpp +++ b/radio/src/tasks.cpp @@ -128,7 +128,7 @@ TASK_FUNCTION(mixerTask) } #else if (isForcePowerOffRequested()) { - pwrOff(); + boardOff(); } #endif diff --git a/radio/src/tests/CMakeLists.txt b/radio/src/tests/CMakeLists.txt index 20a253d1a..d2062a0f3 100644 --- a/radio/src/tests/CMakeLists.txt +++ b/radio/src/tests/CMakeLists.txt @@ -1,4 +1,4 @@ -set(GTEST_ROOT /usr CACHE string "Base path to Google Test headers and source.") +set(GTEST_ROOT /usr CACHE STRING "Base path to Google Test headers and source.") find_path(GTEST_INCDIR gtest/gtest.h HINTS "${GTEST_ROOT}/include" DOC "Path to Google Test header files folder ('gtest/gtest.h').") find_path(GTEST_SRCDIR src/gtest-all.cc HINTS "${GTEST_ROOT}" "${GTEST_ROOT}/src/gtest" DOC "Path of Google Test 'src' folder.") diff --git a/radio/src/trainer.cpp b/radio/src/trainer.cpp new file mode 100644 index 000000000..7761645ba --- /dev/null +++ b/radio/src/trainer.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * 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 "opentx.h" + +int16_t ppmInput[MAX_TRAINER_CHANNELS]; +uint8_t ppmInputValidityTimer; +uint8_t currentTrainerMode = 0xff; + +void checkTrainerSignalWarning() +{ + enum PpmInValidState_t { + PPM_IN_IS_NOT_USED=0, + PPM_IN_IS_VALID, + PPM_IN_INVALID + }; + + static uint8_t ppmInputValidState = PPM_IN_IS_NOT_USED; + + if (ppmInputValidityTimer && (ppmInputValidState == PPM_IN_IS_NOT_USED)) { + ppmInputValidState = PPM_IN_IS_VALID; + } + else if (!ppmInputValidityTimer && (ppmInputValidState == PPM_IN_IS_VALID)) { + ppmInputValidState = PPM_IN_INVALID; + AUDIO_TRAINER_LOST(); + } + else if (ppmInputValidityTimer && (ppmInputValidState == PPM_IN_INVALID)) { + ppmInputValidState = PPM_IN_IS_VALID; + AUDIO_TRAINER_BACK(); + } +} + +#if defined(PCBSKY9X) +void checkTrainerSettings() +{ + uint8_t requiredTrainerMode = SLAVE_MODE(); + + if (requiredTrainerMode != currentTrainerMode) { + currentTrainerMode = requiredTrainerMode; + if (requiredTrainerMode) + stop_trainer_capture(); + else + init_trainer_capture(); + } +} +#else +void checkTrainerSettings() +{ + uint8_t requiredTrainerMode = g_model.trainerData.mode; + + if (requiredTrainerMode != currentTrainerMode) { + switch (currentTrainerMode) { + case TRAINER_MODE_MASTER_TRAINER_JACK: + stop_trainer_capture(); + break; + + case TRAINER_MODE_SLAVE: + stop_trainer_ppm(); + break; + +#if defined(TRAINER_MODULE_CPPM) + case TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE: + stop_trainer_module_cppm(); + break; +#endif + +#if defined(TRAINER_MODULE_SBUS) + case TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE: + stop_trainer_module_sbus(); + break; +#endif + +#if defined(TRAINER_BATTERY_COMPARTMENT) + case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: + auxSerialStop(); + break; +#endif + } + + currentTrainerMode = requiredTrainerMode; + + switch (requiredTrainerMode) { + case TRAINER_MODE_SLAVE: + init_trainer_ppm(); + break; + +#if defined(TRAINER_MODULE_CPPM) + case TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE: + init_trainer_module_cppm(); + break; +#endif + +#if defined(TRAINER_MODULE_SBUS) + case TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE: + init_trainer_module_sbus(); + break; +#endif + +#if defined(TRAINER_BATTERY_COMPARTMENT) + case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: + if (g_eeGeneral.auxSerialMode == UART_MODE_SBUS_TRAINER) { + auxSerialSbusInit(); + break; + } + // no break +#endif + + default: + // master is default + init_trainer_capture(); + break; + } + +#if defined(TRAINER_MODULE_CPPM) || defined(TRAINER_MODULE_SBUS) + if (requiredTrainerMode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE || requiredTrainerMode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE) + stop_intmodule_heartbeat(); + else + init_intmodule_heartbeat(); +#else + init_intmodule_heartbeat(); +#endif + } +} +#endif diff --git a/radio/src/trainer_input.h b/radio/src/trainer.h similarity index 92% rename from radio/src/trainer_input.h rename to radio/src/trainer.h index 8f96ec605..6f5549cc7 100644 --- a/radio/src/trainer_input.h +++ b/radio/src/trainer.h @@ -18,10 +18,10 @@ * GNU General Public License for more details. */ -#ifndef _TRAINER_INPUT_H_ -#define _TRAINER_INPUT_H_ +#ifndef _TRAINER_H_ +#define _TRAINER_H_ -#include "opentx.h" +#include "dataconstants.h" // Trainer input channels extern int16_t ppmInput[MAX_TRAINER_CHANNELS]; @@ -30,9 +30,11 @@ extern int16_t ppmInput[MAX_TRAINER_CHANNELS]; #define PPM_IN_VALID_TIMEOUT 100 // 1s extern uint8_t ppmInputValidityTimer; +extern uint8_t currentTrainerMode; #define IS_TRAINER_INPUT_VALID() (ppmInputValidityTimer != 0) void checkTrainerSignalWarning(); +void checkTrainerSettings(); // Needs to be inlined to avoid slow function calls in ISR routines inline void captureTrainerPulses(uint16_t capture) @@ -66,4 +68,4 @@ inline void captureTrainerPulses(uint16_t capture) } } -#endif // _TRAINER_INPUT_H_ +#endif // _TRAINER_H_ diff --git a/radio/src/trainer_input.cpp b/radio/src/trainer_input.cpp deleted file mode 100644 index a84ed0d6e..000000000 --- a/radio/src/trainer_input.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) OpenTX - * - * Based on code named - * 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 "trainer_input.h" - -int16_t ppmInput[MAX_TRAINER_CHANNELS]; -uint8_t ppmInputValidityTimer; - - -#include "audio.h" - -void checkTrainerSignalWarning() -{ - enum PpmInValidState_t { - PPM_IN_IS_NOT_USED=0, - PPM_IN_IS_VALID, - PPM_IN_INVALID - }; - - static uint8_t ppmInputValidState = PPM_IN_IS_NOT_USED; - - if (ppmInputValidityTimer && (ppmInputValidState == PPM_IN_IS_NOT_USED)) { - ppmInputValidState = PPM_IN_IS_VALID; - } - else if (!ppmInputValidityTimer && (ppmInputValidState == PPM_IN_IS_VALID)) { - ppmInputValidState = PPM_IN_INVALID; - AUDIO_TRAINER_LOST(); - } - else if (ppmInputValidityTimer && (ppmInputValidState == PPM_IN_INVALID)) { - ppmInputValidState = PPM_IN_IS_VALID; - AUDIO_TRAINER_BACK(); - } -} -