diff --git a/src/main/io/serial_1wire.c b/src/main/io/serial_1wire.c index a51ad24f31..67a3f76f76 100644 --- a/src/main/io/serial_1wire.c +++ b/src/main/io/serial_1wire.c @@ -16,6 +16,7 @@ * * Ported from https://github.com/4712/BLHeliSuite/blob/master/Interfaces/Arduino1Wire/Source/Arduino1Wire_C/Arduino1Wire.c * by Nathan Tsoi + * Several updates by 4712 in order to optimize interaction with BLHeliSuite */ #include @@ -29,61 +30,50 @@ #include "drivers/light_led.h" #include "drivers/system.h" #include "io/serial_1wire.h" +#include "io/beeper.h" #include "drivers/pwm_mapping.h" #include "flight/mixer.h" -uint8_t escCount; +uint8_t escCount; // we detect the hardware dynamically static escHardware_t escHardware[MAX_PWM_MOTORS]; static void gpio_set_mode(GPIO_TypeDef* gpio, uint16_t pin, GPIO_Mode mode) { - gpio_config_t cfg; - cfg.pin = pin; - cfg.mode = mode; - cfg.speed = Speed_10MHz; - gpioInit(gpio, &cfg); + gpio_config_t cfg; + cfg.pin = pin; + cfg.mode = mode; + cfg.speed = Speed_10MHz; + gpioInit(gpio, &cfg); } - static uint32_t GetPinPos(uint32_t pin) { - uint32_t pinPos; - for (pinPos = 0; pinPos < 16; pinPos++) { - uint32_t pinMask = (0x1 << pinPos); - if (pin & pinMask) { + uint32_t pinPos; + for (pinPos = 0; pinPos < 16; pinPos++) { + uint32_t pinMask = (0x1 << pinPos); + if (pin & pinMask) { // pos found return pinPos; - } - } - return 0; + } + } + return 0; } - void usb1WireInitialize() { escCount = 0; - memset(&escHardware, 0, sizeof(escHardware)); - + memset(&escHardware,0,sizeof(escHardware)); pwmOutputConfiguration_t *pwmOutputConfiguration = pwmGetOutputConfiguration(); - - for (uint8_t i = 0; i < pwmOutputConfiguration->outputCount; i++) { + for (volatile uint8_t i = 0; i < pwmOutputConfiguration->outputCount; i++) { if ((pwmOutputConfiguration->portConfigurations[i].flags & PWM_PF_MOTOR) == PWM_PF_MOTOR) { - - if (motor[pwmOutputConfiguration->portConfigurations[i].index] > 0) { - + if(motor[pwmOutputConfiguration->portConfigurations[i].index] > 0) { escHardware[escCount].gpio = pwmOutputConfiguration->portConfigurations[i].timerHardware->gpio; escHardware[escCount].pin = pwmOutputConfiguration->portConfigurations[i].timerHardware->pin; escHardware[escCount].pinpos = GetPinPos(escHardware[escCount].pin); - - gpio_set_mode(escHardware[escCount].gpio, escHardware[escCount].pin, Mode_IPU); //GPIO_Mode_IPU + gpio_set_mode(escHardware[escCount].gpio,escHardware[escCount].pin, Mode_IPU); //GPIO_Mode_IPU escCount++; } } } -#ifdef BEEPER - // fix for buzzer often starts beeping continuously when the ESCs are read - // switch beeper off until reboot - gpio_set_mode(BEEP_GPIO, BEEP_PIN, Mode_IN_FLOATING); // set input no pull up or down -#endif } #ifdef STM32F10X @@ -140,7 +130,6 @@ static void gpioSetOne(uint32_t escIndex, GPIO_Mode mode) { #define ESC_OUTPUT(escIndex) gpioSetOne(escIndex, Mode_Out_PP) #endif - #define RX_LED_OFF LED0_OFF #define RX_LED_ON LED0_ON #define TX_LED_OFF LED1_OFF @@ -150,16 +139,22 @@ static void gpioSetOne(uint32_t escIndex, GPIO_Mode mode) { // RX line control when data should be read or written from the single line void usb1WirePassthrough(uint8_t escIndex) { +#ifdef BEEPER + // fix for buzzer often starts beeping continuously when the ESCs are read + // switch beeper silent here + beeperSilence(); +#endif + // disable all interrupts __disable_irq(); + // prepare MSP UART port for direct pin access // reset all the pins GPIO_ResetBits(S1W_RX_GPIO, S1W_RX_PIN); GPIO_ResetBits(S1W_TX_GPIO, S1W_TX_PIN); // configure gpio gpio_set_mode(S1W_RX_GPIO, S1W_RX_PIN, Mode_IPU); gpio_set_mode(S1W_TX_GPIO, S1W_TX_PIN, Mode_Out_PP); - // hey user, turn on your ESC now #ifdef STM32F10X // reset our gpio register pointers and bitmask values @@ -170,10 +165,9 @@ void usb1WirePassthrough(uint8_t escIndex) ESC_SET_HI(escIndex); TX_SET_HIGH; // Wait for programmer to go from 1 -> 0 indicating incoming data - while (RX_HI) - ; + while(RX_HI); - while (1) { + while(1) { // A new iteration on this loop starts when we have data from the programmer (read_programmer goes low) // Setup escIndex pin to send data, pullup is the default ESC_OUTPUT(escIndex); @@ -185,10 +179,9 @@ void usb1WirePassthrough(uint8_t escIndex) RX_LED_OFF; TX_LED_ON; // Wait for programmer to go 0 -> 1 - uint32_t ct = 3333; - while (!RX_HI) { - if (ct > 0) - ct--; // count down until 0; + uint32_t ct=3333; + while(!RX_HI) { + if (ct > 0) ct--; // count down until 0; // check for low time ->ct=3333 ~600uS //byte LO time for 0 @ 19200 baud -> 9*52 uS => 468.75uS // App must send a 0 at 9600 baud (or lower) which has a LO time of at 104uS (or more) > 0 = 937.5uS LO // BLHeliSuite will use 4800 baud @@ -197,17 +190,16 @@ void usb1WirePassthrough(uint8_t escIndex) // At first Echo to the esc, which helps to charge input capacities at ESC ESC_SET_HI(escIndex); // Listen to the escIndex, input mode, pullup resistor is on - gpio_set_mode(escHardware[escIndex].gpio, escHardware[escIndex].pin, - Mode_IPU); + gpio_set_mode(escHardware[escIndex].gpio, escHardware[escIndex].pin, Mode_IPU); TX_LED_OFF; - if (ct == 0) - break; //we reached zero + if (ct==0) break; //we reached zero // Listen to the escIndex while there is no data from the programmer while (RX_HI) { if (ESC_HI(escIndex)) { TX_SET_HIGH; RX_LED_OFF; - } else { + } + else { TX_SET_LO; RX_LED_ON; } @@ -221,5 +213,4 @@ void usb1WirePassthrough(uint8_t escIndex) __enable_irq(); return; } - #endif diff --git a/src/main/io/serial_1wire.h b/src/main/io/serial_1wire.h index a5855d6aec..1e560ed938 100644 --- a/src/main/io/serial_1wire.h +++ b/src/main/io/serial_1wire.h @@ -25,9 +25,9 @@ extern uint8_t escCount; typedef struct { - GPIO_TypeDef* gpio; - uint16_t pinpos; - uint16_t pin; + GPIO_TypeDef* gpio; + uint16_t pinpos; + uint16_t pin; } escHardware_t; diff --git a/src/main/io/serial_cli.c b/src/main/io/serial_cli.c index 3fcb1e0486..86fff896f6 100644 --- a/src/main/io/serial_cli.c +++ b/src/main/io/serial_cli.c @@ -51,7 +51,6 @@ #include "io/gimbal.h" #include "io/rc_controls.h" #include "io/serial.h" -#include "io/serial_1wire.h" #include "io/ledstrip.h" #include "io/flashfs.h" #include "io/beeper.h" @@ -149,10 +148,6 @@ static void cliFlashRead(char *cmdline); #endif #endif -#ifdef USE_SERIAL_1WIRE_CLI -static void cliUSB1Wire(char *cmdline); -#endif - // buffer static char cliBuffer[48]; static uint32_t bufferIndex = 0; @@ -229,9 +224,6 @@ typedef struct { // should be sorted a..z for bsearch() const clicmd_t cmdTable[] = { -#ifdef USE_SERIAL_1WIRE_CLI - CLI_COMMAND_DEF("1wire", "1-wire interface to escs", "", cliUSB1Wire), -#endif CLI_COMMAND_DEF("adjrange", "configure adjustment ranges", NULL, cliAdjustmentRange), CLI_COMMAND_DEF("aux", "configure modes", NULL, cliAux), #ifdef LED_STRIP @@ -2317,27 +2309,6 @@ static void cliStatus(char *cmdline) printf("Cycle Time: %d, I2C Errors: %d, config size: %d\r\n", cycleTime, i2cErrorCounter, sizeof(master_t)); } -#ifdef USE_SERIAL_1WIRE_CLI -static void cliUSB1Wire(char *cmdline) -{ - if (isEmpty(cmdline)) { - cliShowParseError(); - return; - } else { - usb1WireInitialize(); - - int i; - i = atoi(cmdline); - if (i >= 0 && i < escCount) { - printf("Switching to BlHeli mode on motor port %d\r\n", i); - usb1WirePassthrough(i); - } else { - cliShowArgumentRangeError("motor", 0, escCount - 1); - } - } -} -#endif - static void cliVersion(char *cmdline) { UNUSED(cmdline); diff --git a/src/main/target/CC3D/target.h b/src/main/target/CC3D/target.h index 9d49fcb8d0..2f1f91ce32 100644 --- a/src/main/target/CC3D/target.h +++ b/src/main/target/CC3D/target.h @@ -121,7 +121,6 @@ #define USE_CLI #define USE_SERIAL_1WIRE -#define USE_SERIAL_1WIRE_CLI // FlexPort (pin 21/22, TX/RX respectively): // Note, FlexPort has 10k pullups on both TX and RX diff --git a/src/main/target/MOTOLAB/target.h b/src/main/target/MOTOLAB/target.h index a27fe497b2..d75beca0eb 100644 --- a/src/main/target/MOTOLAB/target.h +++ b/src/main/target/MOTOLAB/target.h @@ -177,7 +177,6 @@ #define BIND_PIN Pin_4 #define USE_SERIAL_1WIRE -#define USE_SERIAL_1WIRE_CLI #define S1W_TX_GPIO GPIOB #define S1W_TX_PIN GPIO_Pin_6 diff --git a/src/main/target/NAZE/target.h b/src/main/target/NAZE/target.h index 3f58b0d94a..96153ab8f6 100644 --- a/src/main/target/NAZE/target.h +++ b/src/main/target/NAZE/target.h @@ -188,7 +188,6 @@ #define BIND_PIN Pin_3 #define USE_SERIAL_1WIRE -#define USE_SERIAL_1WIRE_CLI // STM32F103CBT6-LQFP48 Pin30 (PA9) TX - PC3 connects to onboard CP2102 RX #define S1W_TX_GPIO GPIOA diff --git a/src/main/target/PORT103R/target.h b/src/main/target/PORT103R/target.h index 6e0bd12735..b97443f856 100644 --- a/src/main/target/PORT103R/target.h +++ b/src/main/target/PORT103R/target.h @@ -157,7 +157,6 @@ #define USE_CLI #define USE_SERIAL_1WIRE -#define USE_SERIAL_1WIRE_CLI #define S1W_TX_GPIO GPIOA #define S1W_TX_PIN GPIO_Pin_9 diff --git a/src/main/target/RMDO/target.h b/src/main/target/RMDO/target.h index 8ec37f3905..3327d9cda1 100644 --- a/src/main/target/RMDO/target.h +++ b/src/main/target/RMDO/target.h @@ -165,7 +165,6 @@ #define BIND_PIN Pin_11 #define USE_SERIAL_1WIRE -#define USE_SERIAL_1WIRE_CLI #define S1W_TX_GPIO GPIOA #define S1W_TX_PIN GPIO_Pin_9 diff --git a/src/main/target/SPARKY/target.h b/src/main/target/SPARKY/target.h index 6002838115..aa82b183e0 100644 --- a/src/main/target/SPARKY/target.h +++ b/src/main/target/SPARKY/target.h @@ -151,7 +151,6 @@ #endif #define USE_SERIAL_1WIRE -#define USE_SERIAL_1WIRE_CLI #define S1W_TX_GPIO GPIOB #define S1W_TX_PIN GPIO_Pin_6 diff --git a/src/main/target/SPRACINGF3/target.h b/src/main/target/SPRACINGF3/target.h index 6eb0ef2070..7eb891a1e0 100644 --- a/src/main/target/SPRACINGF3/target.h +++ b/src/main/target/SPRACINGF3/target.h @@ -173,7 +173,6 @@ #define BIND_PIN Pin_11 #define USE_SERIAL_1WIRE -#define USE_SERIAL_1WIRE_CLI #define S1W_TX_GPIO GPIOA #define S1W_TX_PIN GPIO_Pin_9 diff --git a/src/main/target/STM32F3DISCOVERY/target.h b/src/main/target/STM32F3DISCOVERY/target.h index f1267b9701..7ebc02d13b 100644 --- a/src/main/target/STM32F3DISCOVERY/target.h +++ b/src/main/target/STM32F3DISCOVERY/target.h @@ -100,7 +100,6 @@ #define USE_CLI #define USE_SERIAL_1WIRE -#define USE_SERIAL_1WIRE_CLI // STM32F3DISCOVERY TX - PD5 connects to UART RX #define S1W_TX_GPIO GPIOD