From c80090f39fb62830a9517e6acee47a5267b63548 Mon Sep 17 00:00:00 2001 From: Michael Jakob Date: Sat, 29 Nov 2014 04:18:56 +0100 Subject: [PATCH 01/12] Spektrum Satelitte bind code ported from Baseflight includes support for a hardware bind plug (PB5 pin 41) Activate via OPTIONS="HARDWARE_BIND_PLUG" during make --- src/main/config/config.c | 1 + src/main/io/serial_cli.c | 1 + src/main/io/serial_msp.c | 2 ++ src/main/main.c | 15 +++++++- src/main/rx/rx.h | 1 + src/main/rx/spektrum.c | 75 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/main/config/config.c b/src/main/config/config.c index 5ee80256e0..a7bb455416 100644 --- a/src/main/config/config.c +++ b/src/main/config/config.c @@ -334,6 +334,7 @@ static void resetConf(void) resetTelemetryConfig(&masterConfig.telemetryConfig); masterConfig.rxConfig.serialrx_provider = 0; + masterConfig.rxConfig.spektrum_sat_bind = 0; masterConfig.rxConfig.midrc = 1500; masterConfig.rxConfig.mincheck = 1100; masterConfig.rxConfig.maxcheck = 1900; diff --git a/src/main/io/serial_cli.c b/src/main/io/serial_cli.c index 0d8131f150..f8101d7d39 100644 --- a/src/main/io/serial_cli.c +++ b/src/main/io/serial_cli.c @@ -271,6 +271,7 @@ const clivalue_t valueTable[] = { #endif { "serialrx_provider", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.serialrx_provider, 0, SERIALRX_PROVIDER_MAX }, + { "spektrum_sat_bind", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.spektrum_sat_bind, 0, 10 }, { "telemetry_provider", VAR_UINT8 | MASTER_VALUE, &masterConfig.telemetryConfig.telemetry_provider, 0, TELEMETRY_PROVIDER_MAX }, { "telemetry_switch", VAR_UINT8 | MASTER_VALUE, &masterConfig.telemetryConfig.telemetry_switch, 0, 1 }, diff --git a/src/main/io/serial_msp.c b/src/main/io/serial_msp.c index 59b5ab49b1..7a34b84e9f 100644 --- a/src/main/io/serial_msp.c +++ b/src/main/io/serial_msp.c @@ -1013,6 +1013,7 @@ static bool processOutCommand(uint8_t cmdMSP) case MSP_RX_CONFIG: headSerialReply(7); serialize8(masterConfig.rxConfig.serialrx_provider); + serialize8(masterConfig.rxConfig.spektrum_sat_bind); serialize16(masterConfig.rxConfig.maxcheck); serialize16(masterConfig.rxConfig.midrc); serialize16(masterConfig.rxConfig.mincheck); @@ -1288,6 +1289,7 @@ static bool processInCommand(void) case MSP_SET_RX_CONFIG: headSerialReply(0); masterConfig.rxConfig.serialrx_provider = read8(); + masterConfig.rxConfig.spektrum_sat_bind = read8(); masterConfig.rxConfig.maxcheck = read16(); masterConfig.rxConfig.midrc = read16(); masterConfig.rxConfig.mincheck = read16(); diff --git a/src/main/main.c b/src/main/main.c index be109e3395..bfe46347e7 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -105,7 +105,7 @@ void imuInit(void); void displayInit(rxConfig_t *intialRxConfig); void ledStripInit(ledConfig_t *ledConfigsToUse, hsvColor_t *colorsToUse, failsafe_t* failsafeToUse); void loop(void); - +void spektrumBind(rxConfig_t *rxConfig); #ifdef STM32F303xC // from system_stm32f30x.c @@ -150,6 +150,19 @@ void init(void) systemInit(); + // Spektrum sattelite bind - ported from Baseflight + if (feature(FEATURE_RX_MSP)) { + switch (masterConfig.rxConfig.serialrx_provider) { + case SERIALRX_SPEKTRUM1024: + case SERIALRX_SPEKTRUM2048: + // Spektrum satellite binding if enabled on startup. + // Must be called before that 100ms sleep so that we don't lose satellite's binding window after startup. + // The rest of Spektrum initialization will happen later - via spektrumInit() + spektrumBind(&masterConfig.rxConfig); + break; + } + } + delay(100); timerInit(); // timer must be initialized before any channel is allocated diff --git a/src/main/rx/rx.h b/src/main/rx/rx.h index 2c6807c3d1..117269f182 100644 --- a/src/main/rx/rx.h +++ b/src/main/rx/rx.h @@ -65,6 +65,7 @@ extern int16_t rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT]; // interval [1000;2 typedef struct rxConfig_s { uint8_t rcmap[MAX_MAPPABLE_RX_INPUTS]; // mapping of radio channels to internal RPYTA+ order uint8_t serialrx_provider; // type of UART-based receiver (0 = spek 10, 1 = spek 11, 2 = sbus). Must be enabled by FEATURE_RX_SERIAL first. + uint8_t spektrum_sat_bind; // number of bind pules for Spektrum sattelite recievers uint16_t midrc; // Some radios have not a neutral point centered on 1500. can be changed here uint16_t mincheck; // minimum rc end uint16_t maxcheck; // maximum rc end diff --git a/src/main/rx/spektrum.c b/src/main/rx/spektrum.c index f1e756dbad..146de97b35 100644 --- a/src/main/rx/spektrum.c +++ b/src/main/rx/spektrum.c @@ -21,12 +21,15 @@ #include "platform.h" +#include "drivers/gpio.h" #include "drivers/system.h" #include "drivers/serial.h" #include "drivers/serial_uart.h" #include "io/serial.h" +#include "config/config.h" + #include "rx/rx.h" #include "rx/spektrum.h" @@ -40,6 +43,8 @@ #define SPEKTRUM_BAUDRATE 115200 +#define BKP_SOFTRESET (0x50F7B007) + static uint8_t spek_chan_shift; static uint8_t spek_chan_mask; static bool rcFrameComplete = false; @@ -138,3 +143,73 @@ static uint16_t spektrumReadRawRC(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t ch return data; } + +uint32_t rccReadBkpDr(void) +{ + return *((uint16_t *)BKP_BASE + 0x04) | *((uint16_t *)BKP_BASE + 0x08) << 16; +} + +/* spektrumBind function ported from Baseflight. It's used to bind satellite receiver to TX. + * Function must be called immediately after startup so that we don't miss satellite bind window. + * Known parameters. Tested with DSMX satellite and DX8 radio. Framerate (11ms or 22ms) must be selected from TX. + * 9 = DSMX 11ms / DSMX 22ms + * 5 = DSM2 11ms 2048 / DSM2 22ms 1024 + */ +void spektrumBind(rxConfig_t *rxConfig) +{ + int i; + gpio_config_t gpio; + GPIO_TypeDef *spekBindPort; + uint16_t spekBindPin; + +#ifdef HARDWARE_BIND_PLUG + // Check status of bind plug and exit if not active + GPIO_TypeDef *hwBindPort; + uint16_t hwBindPin; + + hwBindPort = GPIOB; + hwBindPin = Pin_5; + gpio.speed = Speed_2MHz; + gpio.pin = hwBindPin; + gpio.mode = Mode_IPU; + gpioInit(hwBindPort, &gpio); + if (digitalIn(hwBindPort, hwBindPin)) + return; +#endif + + // USART2, PA3 + spekBindPort = GPIOA; + spekBindPin = Pin_3; + + // don't try to bind if: here after soft reset or bind flag is out of range + if (rccReadBkpDr() == BKP_SOFTRESET || rxConfig->spektrum_sat_bind == 0 || rxConfig->spektrum_sat_bind > 10) + return; + + gpio.speed = Speed_2MHz; + gpio.pin = spekBindPin; + gpio.mode = Mode_Out_OD; + gpioInit(spekBindPort, &gpio); + // RX line, set high + digitalHi(spekBindPort, spekBindPin); + // Bind window is around 20-140ms after powerup + delay(60); + + for (i = 0; i < rxConfig->spektrum_sat_bind; i++) { + // RX line, drive low for 120us + digitalLo(spekBindPort, spekBindPin); + delayMicroseconds(120); + // RX line, drive high for 120us + 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 (rccReadBkpDr() != BKP_SOFTRESET) { + rxConfig->spektrum_sat_bind = 0; + writeEEPROM(1, true); + } +#endif + +} From 3aaaa3f9f5460d8dcc4348e1db218bb3198dad02 Mon Sep 17 00:00:00 2001 From: Michael Jakob Date: Sat, 29 Nov 2014 14:43:03 +0100 Subject: [PATCH 02/12] Updates and fixes fix FEATURE_RX_SERIAL Delay for the hardware bind plug Bind code will only work on default NAZE serial port (document limitation) --- src/main/main.c | 2 +- src/main/rx/spektrum.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/main.c b/src/main/main.c index bfe46347e7..36a1a3c459 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -151,7 +151,7 @@ void init(void) systemInit(); // Spektrum sattelite bind - ported from Baseflight - if (feature(FEATURE_RX_MSP)) { + if (feature(FEATURE_RX_SERIAL)) { switch (masterConfig.rxConfig.serialrx_provider) { case SERIALRX_SPEKTRUM1024: case SERIALRX_SPEKTRUM2048: diff --git a/src/main/rx/spektrum.c b/src/main/rx/spektrum.c index 146de97b35..2c2aac6b1f 100644 --- a/src/main/rx/spektrum.c +++ b/src/main/rx/spektrum.c @@ -151,6 +151,7 @@ uint32_t rccReadBkpDr(void) /* spektrumBind function ported from Baseflight. It's used to bind satellite receiver to TX. * Function must be called immediately after startup so that we don't miss satellite bind window. + * It will only work for USART2, PA3 pin. * Known parameters. Tested with DSMX satellite and DX8 radio. Framerate (11ms or 22ms) must be selected from TX. * 9 = DSMX 11ms / DSMX 22ms * 5 = DSM2 11ms 2048 / DSM2 22ms 1024 @@ -173,6 +174,7 @@ void spektrumBind(rxConfig_t *rxConfig) gpio.pin = hwBindPin; gpio.mode = Mode_IPU; gpioInit(hwBindPort, &gpio); + delayMicroseconds(10); // allow configuration to settle if (digitalIn(hwBindPort, hwBindPin)) return; #endif From 1766857da6ba0cf37573c51fcaddd8fbf5310406 Mon Sep 17 00:00:00 2001 From: Michael Jakob Date: Mon, 1 Dec 2014 14:10:28 +0100 Subject: [PATCH 03/12] Spectrum bind code to be more geenic Actualy only activated for the NAZE taget for USART2 #define SPEKTRUM_BIND #define BIND_PORT GPIOA #define BIND_PIN Pin_3 This is to activate the Hardware bind pug feature #define HARDWARE_BIND_PLUG #define BINDPLUG_PORT GPIOB #define BINDPLUG_PIN Pin_5 can now be easyly added and configured for other tagets after testing. --- src/main/main.c | 2 ++ src/main/rx/spektrum.c | 15 ++++++++------- src/main/target/NAZE/target.h | 4 ++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/main.c b/src/main/main.c index 36a1a3c459..74e0e039fd 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -150,6 +150,7 @@ void init(void) systemInit(); +#ifdef SPEKTRUM_BIND // Spektrum sattelite bind - ported from Baseflight if (feature(FEATURE_RX_SERIAL)) { switch (masterConfig.rxConfig.serialrx_provider) { @@ -162,6 +163,7 @@ void init(void) break; } } +#endif delay(100); diff --git a/src/main/rx/spektrum.c b/src/main/rx/spektrum.c index 2c2aac6b1f..4e14567953 100644 --- a/src/main/rx/spektrum.c +++ b/src/main/rx/spektrum.c @@ -43,8 +43,6 @@ #define SPEKTRUM_BAUDRATE 115200 -#define BKP_SOFTRESET (0x50F7B007) - static uint8_t spek_chan_shift; static uint8_t spek_chan_mask; static bool rcFrameComplete = false; @@ -144,6 +142,9 @@ static uint16_t spektrumReadRawRC(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t ch return data; } +#ifdef SPEKTRUM_BIND +#define BKP_SOFTRESET (0x50F7B007) + uint32_t rccReadBkpDr(void) { return *((uint16_t *)BKP_BASE + 0x04) | *((uint16_t *)BKP_BASE + 0x08) << 16; @@ -151,7 +152,6 @@ uint32_t rccReadBkpDr(void) /* spektrumBind function ported from Baseflight. It's used to bind satellite receiver to TX. * Function must be called immediately after startup so that we don't miss satellite bind window. - * It will only work for USART2, PA3 pin. * Known parameters. Tested with DSMX satellite and DX8 radio. Framerate (11ms or 22ms) must be selected from TX. * 9 = DSMX 11ms / DSMX 22ms * 5 = DSM2 11ms 2048 / DSM2 22ms 1024 @@ -168,8 +168,8 @@ void spektrumBind(rxConfig_t *rxConfig) GPIO_TypeDef *hwBindPort; uint16_t hwBindPin; - hwBindPort = GPIOB; - hwBindPin = Pin_5; + hwBindPort = BINDPLUG_PORT; + hwBindPin = BINDPLUG_PIN; gpio.speed = Speed_2MHz; gpio.pin = hwBindPin; gpio.mode = Mode_IPU; @@ -180,8 +180,8 @@ void spektrumBind(rxConfig_t *rxConfig) #endif // USART2, PA3 - spekBindPort = GPIOA; - spekBindPin = Pin_3; + spekBindPort = BIND_PORT; + spekBindPin = BIND_PIN; // don't try to bind if: here after soft reset or bind flag is out of range if (rccReadBkpDr() == BKP_SOFTRESET || rxConfig->spektrum_sat_bind == 0 || rxConfig->spektrum_sat_bind > 10) @@ -215,3 +215,4 @@ void spektrumBind(rxConfig_t *rxConfig) #endif } +#endif diff --git a/src/main/target/NAZE/target.h b/src/main/target/NAZE/target.h index 8fc63bb802..5b84e39fb3 100644 --- a/src/main/target/NAZE/target.h +++ b/src/main/target/NAZE/target.h @@ -112,3 +112,7 @@ #define TELEMETRY #define SERIAL_RX #define AUTOTUNE + +#define SPEKTRUM_BIND +#define BIND_PORT GPIOA +#define BIND_PIN Pin_3 From 97523e832cfd7b34bbe04b569adedddb3adab94c Mon Sep 17 00:00:00 2001 From: Michael Jakob Date: Mon, 1 Dec 2014 17:22:57 +0100 Subject: [PATCH 04/12] Enable Spektrum bind for CJMCU, CC3D, NAZE32PRO targets --- src/main/rx/spektrum.c | 1 - src/main/target/CC3D/target.h | 5 +++++ src/main/target/CJMCU/target.h | 5 +++++ src/main/target/NAZE/target.h | 1 + src/main/target/NAZE32PRO/target.h | 5 +++++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/rx/spektrum.c b/src/main/rx/spektrum.c index 4e14567953..55d4416953 100644 --- a/src/main/rx/spektrum.c +++ b/src/main/rx/spektrum.c @@ -179,7 +179,6 @@ void spektrumBind(rxConfig_t *rxConfig) return; #endif - // USART2, PA3 spekBindPort = BIND_PORT; spekBindPin = BIND_PIN; diff --git a/src/main/target/CC3D/target.h b/src/main/target/CC3D/target.h index d933362b1d..e8607dd059 100644 --- a/src/main/target/CC3D/target.h +++ b/src/main/target/CC3D/target.h @@ -76,3 +76,8 @@ #define TELEMETRY #define SERIAL_RX #define AUTOTUNE + +#define SPEKTRUM_BIND +// USART3, PB11 (Flexport) +#define BIND_PORT GPIOB +#define BIND_PIN Pin_11 diff --git a/src/main/target/CJMCU/target.h b/src/main/target/CJMCU/target.h index 0e2da29e77..60e74e528f 100644 --- a/src/main/target/CJMCU/target.h +++ b/src/main/target/CJMCU/target.h @@ -61,3 +61,8 @@ #define SENSORS_SET (SENSOR_ACC | SENSOR_MAG) #define SERIAL_RX + +#define SPEKTRUM_BIND +// USART2, PA3 +#define BIND_PORT GPIOA +#define BIND_PIN Pin_3 diff --git a/src/main/target/NAZE/target.h b/src/main/target/NAZE/target.h index 5b84e39fb3..3226382e37 100644 --- a/src/main/target/NAZE/target.h +++ b/src/main/target/NAZE/target.h @@ -114,5 +114,6 @@ #define AUTOTUNE #define SPEKTRUM_BIND +// USART2, PA3 #define BIND_PORT GPIOA #define BIND_PIN Pin_3 diff --git a/src/main/target/NAZE32PRO/target.h b/src/main/target/NAZE32PRO/target.h index 12ae41b82f..5376ef9aaa 100644 --- a/src/main/target/NAZE32PRO/target.h +++ b/src/main/target/NAZE32PRO/target.h @@ -46,3 +46,8 @@ #define TELEMETRY #define SERIAL_RX #define AUTOTUNE + +#define SPEKTRUM_BIND +// USART2, PA3 +#define BIND_PORT GPIOA +#define BIND_PIN Pin_3 From 7b2c3be563f5c9ca6d6787d38e7f6edcb26178a4 Mon Sep 17 00:00:00 2001 From: Michael Jakob Date: Wed, 3 Dec 2014 08:28:57 +0100 Subject: [PATCH 05/12] Remove softreset detection to make it more simple User needs to reset spektrum_sat_bind manualy after binding the sattelite receiver and the hardware bind plug is not present. --- src/main/rx/spektrum.c | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/src/main/rx/spektrum.c b/src/main/rx/spektrum.c index 55d4416953..632396d149 100644 --- a/src/main/rx/spektrum.c +++ b/src/main/rx/spektrum.c @@ -143,13 +143,6 @@ static uint16_t spektrumReadRawRC(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t ch } #ifdef SPEKTRUM_BIND -#define BKP_SOFTRESET (0x50F7B007) - -uint32_t rccReadBkpDr(void) -{ - return *((uint16_t *)BKP_BASE + 0x04) | *((uint16_t *)BKP_BASE + 0x08) << 16; -} - /* spektrumBind function ported from Baseflight. It's used to bind satellite receiver to TX. * Function must be called immediately after startup so that we don't miss satellite bind window. * Known parameters. Tested with DSMX satellite and DX8 radio. Framerate (11ms or 22ms) must be selected from TX. @@ -182,8 +175,8 @@ void spektrumBind(rxConfig_t *rxConfig) spekBindPort = BIND_PORT; spekBindPin = BIND_PIN; - // don't try to bind if: here after soft reset or bind flag is out of range - if (rccReadBkpDr() == BKP_SOFTRESET || rxConfig->spektrum_sat_bind == 0 || rxConfig->spektrum_sat_bind > 10) + // don't try to bind if: bind flag is out of range + if (rxConfig->spektrum_sat_bind == 0 || rxConfig->spektrum_sat_bind > 10) return; gpio.speed = Speed_2MHz; @@ -203,15 +196,5 @@ 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 (rccReadBkpDr() != BKP_SOFTRESET) { - rxConfig->spektrum_sat_bind = 0; - writeEEPROM(1, true); - } -#endif - } #endif From c09877e5b9d68f6e1bf55202299e5d8e88068d62 Mon Sep 17 00:00:00 2001 From: Michael Jakob Date: Fri, 5 Dec 2014 08:09:07 +0100 Subject: [PATCH 06/12] Fix spelling errors in comments --- src/main/rx/rx.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/rx/rx.h b/src/main/rx/rx.h index 117269f182..7d06f8e9d1 100644 --- a/src/main/rx/rx.h +++ b/src/main/rx/rx.h @@ -65,7 +65,7 @@ extern int16_t rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT]; // interval [1000;2 typedef struct rxConfig_s { uint8_t rcmap[MAX_MAPPABLE_RX_INPUTS]; // mapping of radio channels to internal RPYTA+ order uint8_t serialrx_provider; // type of UART-based receiver (0 = spek 10, 1 = spek 11, 2 = sbus). Must be enabled by FEATURE_RX_SERIAL first. - uint8_t spektrum_sat_bind; // number of bind pules for Spektrum sattelite recievers + uint8_t spektrum_sat_bind; // number of bind pulses for Spektrum satellite receivers uint16_t midrc; // Some radios have not a neutral point centered on 1500. can be changed here uint16_t mincheck; // minimum rc end uint16_t maxcheck; // maximum rc end From 1be3e8e550c0550711efb21935c24e37a249f51e Mon Sep 17 00:00:00 2001 From: Michael Jakob Date: Sat, 6 Dec 2014 17:43:26 +0100 Subject: [PATCH 07/12] 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 From 8d1cfe633e0b9734b7c2253dacae32664376bbc4 Mon Sep 17 00:00:00 2001 From: Michael Jakob Date: Sun, 7 Dec 2014 11:56:36 +0100 Subject: [PATCH 08/12] Implement softreset detection for STM32F3xx based targets Function still needs to be tested on target hardware --- src/main/drivers/system_stm32f30x.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/drivers/system_stm32f30x.c b/src/main/drivers/system_stm32f30x.c index 7b347bd74a..8dcae72937 100644 --- a/src/main/drivers/system_stm32f30x.c +++ b/src/main/drivers/system_stm32f30x.c @@ -70,6 +70,8 @@ void enableGPIOPowerUsageAndNoiseReductions(void) bool isMPUSoftReset(void) { - // not implemented yet for STM32F3xx - return false; + if (RCC->CSR & RCC_CSR_SFTRSTF) + return true; + else + return false; } From ca7991d532877fb7560b790f7be9d7a42bfacdc6 Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Wed, 17 Dec 2014 22:35:17 +0000 Subject: [PATCH 09/12] Update MSP_RX_CONFIG to include the ability to confugure spektrum satellite binding. --- src/main/io/serial_msp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/io/serial_msp.c b/src/main/io/serial_msp.c index 7a34b84e9f..697a75f8c1 100644 --- a/src/main/io/serial_msp.c +++ b/src/main/io/serial_msp.c @@ -1013,10 +1013,11 @@ static bool processOutCommand(uint8_t cmdMSP) case MSP_RX_CONFIG: headSerialReply(7); serialize8(masterConfig.rxConfig.serialrx_provider); - serialize8(masterConfig.rxConfig.spektrum_sat_bind); serialize16(masterConfig.rxConfig.maxcheck); serialize16(masterConfig.rxConfig.midrc); serialize16(masterConfig.rxConfig.mincheck); + serialize8(masterConfig.rxConfig.spektrum_sat_bind); + break; case MSP_RSSI_CONFIG: headSerialReply(1); @@ -1289,10 +1290,10 @@ static bool processInCommand(void) case MSP_SET_RX_CONFIG: headSerialReply(0); masterConfig.rxConfig.serialrx_provider = read8(); - masterConfig.rxConfig.spektrum_sat_bind = read8(); masterConfig.rxConfig.maxcheck = read16(); masterConfig.rxConfig.midrc = read16(); masterConfig.rxConfig.mincheck = read16(); + masterConfig.rxConfig.spektrum_sat_bind = read8(); break; case MSP_SET_RSSI_CONFIG: From 19525fdd3cabd4c975cceadc92e31c906fdc9145 Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Wed, 17 Dec 2014 23:10:03 +0000 Subject: [PATCH 10/12] Removing unnecessary comment. --- src/main/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/main.c b/src/main/main.c index 74e0e039fd..a68b97ddf0 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -151,7 +151,6 @@ void init(void) systemInit(); #ifdef SPEKTRUM_BIND - // Spektrum sattelite bind - ported from Baseflight if (feature(FEATURE_RX_SERIAL)) { switch (masterConfig.rxConfig.serialrx_provider) { case SERIALRX_SPEKTRUM1024: From 8b59e03ea398c807cc316f342e37701b5131dfe9 Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Wed, 17 Dec 2014 23:10:23 +0000 Subject: [PATCH 11/12] Add SPEKTRIM_BIND support to SPARKY and EUSTM32F103RC targets. --- src/main/target/EUSTM32F103RC/target.h | 4 ++++ src/main/target/SPARKY/target.h | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/main/target/EUSTM32F103RC/target.h b/src/main/target/EUSTM32F103RC/target.h index e99bd4575a..7bdc8eed31 100644 --- a/src/main/target/EUSTM32F103RC/target.h +++ b/src/main/target/EUSTM32F103RC/target.h @@ -99,3 +99,7 @@ #define SERIAL_RX #define AUTOTUNE +#define SPEKTRUM_BIND +// USART2, PA3 +#define BIND_PORT GPIOA +#define BIND_PIN Pin_3 diff --git a/src/main/target/SPARKY/target.h b/src/main/target/SPARKY/target.h index 156986f876..a26c9eaa98 100644 --- a/src/main/target/SPARKY/target.h +++ b/src/main/target/SPARKY/target.h @@ -82,3 +82,7 @@ #define GPS #define DISPLAY +#define SPEKTRUM_BIND +// USART2, PA3 +#define BIND_PORT GPIOA +#define BIND_PIN Pin_3 From 78d4f24ad2143e3f3c46087511e7b34d72fedbe4 Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Thu, 18 Dec 2014 00:50:30 +0000 Subject: [PATCH 12/12] Various cleanups for Spektrum binding support. --- src/main/io/serial_cli.c | 3 +- src/main/rx/spektrum.c | 72 ++++++++++++++++++++++------------------ src/main/rx/spektrum.h | 3 ++ 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/src/main/io/serial_cli.c b/src/main/io/serial_cli.c index f8101d7d39..6d4295c754 100644 --- a/src/main/io/serial_cli.c +++ b/src/main/io/serial_cli.c @@ -51,6 +51,7 @@ #include "io/rc_controls.h" #include "io/serial.h" #include "io/ledstrip.h" +#include "rx/spektrum.h" #include "sensors/battery.h" #include "sensors/boardalignment.h" #include "sensors/sensors.h" @@ -271,7 +272,7 @@ const clivalue_t valueTable[] = { #endif { "serialrx_provider", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.serialrx_provider, 0, SERIALRX_PROVIDER_MAX }, - { "spektrum_sat_bind", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.spektrum_sat_bind, 0, 10 }, + { "spektrum_sat_bind", VAR_UINT8 | MASTER_VALUE, &masterConfig.rxConfig.spektrum_sat_bind, SPEKTRUM_SAT_BIND_DISABLED, SPEKTRUM_SAT_BIND_MAX}, { "telemetry_provider", VAR_UINT8 | MASTER_VALUE, &masterConfig.telemetryConfig.telemetry_provider, 0, TELEMETRY_PROVIDER_MAX }, { "telemetry_switch", VAR_UINT8 | MASTER_VALUE, &masterConfig.telemetryConfig.telemetry_switch, 0, 1 }, diff --git a/src/main/rx/spektrum.c b/src/main/rx/spektrum.c index 81e98fb0a7..d933c3ce25 100644 --- a/src/main/rx/spektrum.c +++ b/src/main/rx/spektrum.c @@ -143,6 +143,30 @@ static uint16_t spektrumReadRawRC(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t ch } #ifdef SPEKTRUM_BIND + +bool spekShouldBind(uint8_t spektrum_sat_bind) +{ +#ifdef HARDWARE_BIND_PLUG + gpio_config_t cfg = { + BINDPLUG_PIN, + Mode_IPU, + Speed_2MHz + }; + gpioInit(BINDPLUG_PORT, &cfg); + + // Check status of bind plug and exit if not active + delayMicroseconds(10); // allow configuration to settle + if (digitalIn(BINDPLUG_PORT, BINDPLUG_PIN)) { + return false; + } +#endif + + return !( + isMPUSoftReset() || + spektrum_sat_bind == SPEKTRUM_SAT_BIND_DISABLED || + spektrum_sat_bind > SPEKTRUM_SAT_BIND_MAX + ); +} /* spektrumBind function ported from Baseflight. It's used to bind satellite receiver to TX. * Function must be called immediately after startup so that we don't miss satellite bind window. * Known parameters. Tested with DSMX satellite and DX8 radio. Framerate (11ms or 22ms) must be selected from TX. @@ -152,57 +176,41 @@ static uint16_t spektrumReadRawRC(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t ch void spektrumBind(rxConfig_t *rxConfig) { int i; - gpio_config_t gpio; - GPIO_TypeDef *spekBindPort; - uint16_t spekBindPin; -#ifdef HARDWARE_BIND_PLUG - // Check status of bind plug and exit if not active - GPIO_TypeDef *hwBindPort; - uint16_t hwBindPin; - - hwBindPort = BINDPLUG_PORT; - hwBindPin = BINDPLUG_PIN; - gpio.speed = Speed_2MHz; - gpio.pin = hwBindPin; - gpio.mode = Mode_IPU; - gpioInit(hwBindPort, &gpio); - delayMicroseconds(10); // allow configuration to settle - if (digitalIn(hwBindPort, hwBindPin)) + if (!spekShouldBind(rxConfig->spektrum_sat_bind)) { return; -#endif + } - spekBindPort = BIND_PORT; - spekBindPin = BIND_PIN; + gpio_config_t cfg = { + BIND_PIN, + Mode_Out_OD, + Speed_2MHz + }; + gpioInit(BIND_PORT, &cfg); - // 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; - gpio.pin = spekBindPin; - gpio.mode = Mode_Out_OD; - gpioInit(spekBindPort, &gpio); // RX line, set high - digitalHi(spekBindPort, spekBindPin); + digitalHi(BIND_PORT, BIND_PIN); + // Bind window is around 20-140ms after powerup delay(60); for (i = 0; i < rxConfig->spektrum_sat_bind; i++) { + // RX line, drive low for 120us - digitalLo(spekBindPort, spekBindPin); + digitalLo(BIND_PORT, BIND_PIN); delayMicroseconds(120); + // RX line, drive high for 120us - digitalHi(spekBindPort, spekBindPin); + digitalHi(BIND_PORT, BIND_PIN); 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 + // If we came here as a result of hard reset (power up, with 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); + saveConfigAndNotify(); } #endif diff --git a/src/main/rx/spektrum.h b/src/main/rx/spektrum.h index 501d707c44..ea40e44aef 100644 --- a/src/main/rx/spektrum.h +++ b/src/main/rx/spektrum.h @@ -17,5 +17,8 @@ #pragma once +#define SPEKTRUM_SAT_BIND_DISABLED 0 +#define SPEKTRUM_SAT_BIND_MAX 10 + bool spektrumFrameComplete(void); void spektrumUpdateSerialRxFunctionConstraint(functionConstraint_t *functionConstraint);