From 1be3e8e550c0550711efb21935c24e37a249f51e Mon Sep 17 00:00:00 2001 From: Michael Jakob Date: Sat, 6 Dec 2014 17:43:26 +0100 Subject: [PATCH] Added detection of soft reset and swich of bind mode after first hard reset (power on) if hardware bind plug is not configured. Now completly tested. --- src/main/drivers/system.h | 1 + src/main/drivers/system_stm32f10x.c | 16 ++++++++++++++++ src/main/drivers/system_stm32f30x.c | 6 ++++++ src/main/rx/spektrum.c | 14 ++++++++++++-- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/drivers/system.h b/src/main/drivers/system.h index 6a0f16c0dd..0996b2d4ea 100644 --- a/src/main/drivers/system.h +++ b/src/main/drivers/system.h @@ -30,6 +30,7 @@ void failureMode(uint8_t mode); // bootloader/IAP void systemReset(void); void systemResetToBootloader(void); +bool isMPUSoftReset(void); void enableGPIOPowerUsageAndNoiseReductions(void); // current crystal frequency - 8 or 12MHz diff --git a/src/main/drivers/system_stm32f10x.c b/src/main/drivers/system_stm32f10x.c index a212b75b6e..b3d887da70 100644 --- a/src/main/drivers/system_stm32f10x.c +++ b/src/main/drivers/system_stm32f10x.c @@ -22,11 +22,19 @@ #include "platform.h" #include "gpio.h" +#include "system.h" #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000) +#define BKP_SOFTRESET (0x50F7B007) void systemReset(void) { + // write magic value that we're doing a soft reset + RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); + PWR->CR |= PWR_CR_DBP; + *((uint16_t *)BKP_BASE + 0x04) = BKP_SOFTRESET & 0xffff; + *((uint16_t *)BKP_BASE + 0x08) = (BKP_SOFTRESET & 0xffff0000) >> 16; + // Generate system reset SCB->AIRCR = AIRCR_VECTKEY_MASK | (uint32_t)0x04; } @@ -52,3 +60,11 @@ void enableGPIOPowerUsageAndNoiseReductions(void) gpioInit(GPIOB, &gpio); gpioInit(GPIOC, &gpio); } + +bool isMPUSoftReset(void) +{ + if ((*((uint16_t *)BKP_BASE + 0x04) | *((uint16_t *)BKP_BASE + 0x08) << 16) == BKP_SOFTRESET) + return true; + else + return false; +} diff --git a/src/main/drivers/system_stm32f30x.c b/src/main/drivers/system_stm32f30x.c index b530e4802a..7b347bd74a 100644 --- a/src/main/drivers/system_stm32f30x.c +++ b/src/main/drivers/system_stm32f30x.c @@ -67,3 +67,9 @@ void enableGPIOPowerUsageAndNoiseReductions(void) gpioInit(GPIOE, &gpio); gpioInit(GPIOF, &gpio); } + +bool isMPUSoftReset(void) +{ + // not implemented yet for STM32F3xx + return false; +} diff --git a/src/main/rx/spektrum.c b/src/main/rx/spektrum.c index 632396d149..81e98fb0a7 100644 --- a/src/main/rx/spektrum.c +++ b/src/main/rx/spektrum.c @@ -175,8 +175,8 @@ void spektrumBind(rxConfig_t *rxConfig) spekBindPort = BIND_PORT; spekBindPin = BIND_PIN; - // don't try to bind if: bind flag is out of range - if (rxConfig->spektrum_sat_bind == 0 || rxConfig->spektrum_sat_bind > 10) + // don't try to bind if: here after soft reset or bind flag is out of range + if (isMPUSoftReset() || rxConfig->spektrum_sat_bind == 0 || rxConfig->spektrum_sat_bind > 10) return; gpio.speed = Speed_2MHz; @@ -196,5 +196,15 @@ void spektrumBind(rxConfig_t *rxConfig) digitalHi(spekBindPort, spekBindPin); delayMicroseconds(120); } + +#ifndef HARDWARE_BIND_PLUG + // If we came here as a result of hard reset (power up, with mcfg.spektrum_sat_bind set), then reset it back to zero and write config + // Don't reset if hardware bind plug is present + if (!isMPUSoftReset()) { + rxConfig->spektrum_sat_bind = 0; + writeEEPROM(1, true); + } +#endif + } #endif