From 0e4684e8282aaf0886a868646b18edd20436df8e Mon Sep 17 00:00:00 2001 From: jflyper Date: Mon, 23 Apr 2018 21:44:40 +0900 Subject: [PATCH 1/3] Serial port function mask initializer as a config helper --- make/source.mk | 1 + src/main/target/OMNIBUSF4/config.c | 16 +++++++++--- src/main/target/OMNIBUSF7/config.c | 17 +++++++------ src/main/target/SPRACINGF3NEO/config.c | 13 +++++++--- src/main/target/config_helper.c | 34 ++++++++++++++++++++++++++ src/main/target/config_helper.h | 30 +++++++++++++++++++++++ 6 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 src/main/target/config_helper.c create mode 100644 src/main/target/config_helper.h diff --git a/make/source.mk b/make/source.mk index b940201ccb..68fe369295 100644 --- a/make/source.mk +++ b/make/source.mk @@ -84,6 +84,7 @@ COMMON_SRC = \ sensors/battery.c \ sensors/current.c \ sensors/voltage.c \ + target/config_helper.c \ OSD_SLAVE_SRC = \ io/displayport_max7456.c \ diff --git a/src/main/target/OMNIBUSF4/config.c b/src/main/target/OMNIBUSF4/config.c index 4e33cc0eac..35c3dc41c8 100644 --- a/src/main/target/OMNIBUSF4/config.c +++ b/src/main/target/OMNIBUSF4/config.c @@ -24,11 +24,22 @@ #ifdef USE_TARGET_CONFIG +#include "config_helper.h" + #include "io/serial.h" #include "pg/max7456.h" #include "pg/pg.h" +#ifdef EXUAVF4PRO +static targetSerialPortFunction_t targetSerialPortFunction[] = { + { 0, FUNCTION_TELEMETRY_SMARTPORT }, + { 2, FUNCTION_VTX_TRAMP }, + { 3, FUNCTION_RCDEVICE }, + { 5, FUNCTION_RX_SERIAL }, +}; +#endif + void targetConfiguration(void) { #ifdef OMNIBUSF4BASE @@ -37,10 +48,7 @@ void targetConfiguration(void) #endif #ifdef EXUAVF4PRO - serialConfigMutable()->portConfigs[1].functionMask = FUNCTION_TELEMETRY_SMARTPORT; - serialConfigMutable()->portConfigs[2].functionMask = FUNCTION_VTX_TRAMP; - serialConfigMutable()->portConfigs[3].functionMask = FUNCTION_RCDEVICE; - serialConfigMutable()->portConfigs[4].functionMask = FUNCTION_RX_SERIAL; + targetSerialPortFunctionConfig(targetSerialPortFunction, ARRAYLEN(targetSerialPortFunction)); #endif } #endif diff --git a/src/main/target/OMNIBUSF7/config.c b/src/main/target/OMNIBUSF7/config.c index de97012a69..41c49f45b0 100644 --- a/src/main/target/OMNIBUSF7/config.c +++ b/src/main/target/OMNIBUSF7/config.c @@ -24,16 +24,19 @@ #ifdef USE_TARGET_CONFIG +#include "config_helper.h" + #include "io/serial.h" +static targetSerialPortFunction_t targetSerialPortFunction[] = { +#if defined(OMNIBUSF7V2) && defined(ESC_SENSOR_UART) + // OMNIBUS F7 V2 has an option to connect UART7_RX to ESC telemetry + { ESC_SENSOR_UART, FUNCTION_ESC_SENSOR }, +#endif +}; + void targetConfiguration(void) { -// OMNIBUS F7 V2 has an option to connect UART7_RX to ESC telemetry -#if defined(OMNIBUSF7V2) && defined(ESC_SENSOR_UART) - serialPortConfig_t *serialEscSensorUartConfig = serialFindPortConfiguration(ESC_SENSOR_UART); - if (serialEscSensorUartConfig) { - serialEscSensorUartConfig->functionMask = FUNCTION_ESC_SENSOR; - } -#endif + targetSerialPortFunctionConfig(targetSerialPortFunction, ARRAYLEN(targetSerialPortFunction)); } #endif diff --git a/src/main/target/SPRACINGF3NEO/config.c b/src/main/target/SPRACINGF3NEO/config.c index 579b96daf0..27ae12904c 100644 --- a/src/main/target/SPRACINGF3NEO/config.c +++ b/src/main/target/SPRACINGF3NEO/config.c @@ -49,13 +49,20 @@ #include "fc/config.h" #ifdef USE_TARGET_CONFIG + +#include "config_helper.h" + +static targetSerialPortFunction_t targetSerialPortFunction[] = { + { 0, FUNCTION_MSP }, + { TELEMETRY_UART, TELEMETRY_PROVIDER_DEFAULT }, + { GPS_UART, FUNCTION_GPS }, +}; + void targetConfiguration(void) { barometerConfigMutable()->baro_hardware = BARO_DEFAULT; compassConfigMutable()->mag_hardware = MAG_DEFAULT; - serialConfigMutable()->portConfigs[1].functionMask = FUNCTION_MSP; // So Bluetooth users don't have to change anything. - serialConfigMutable()->portConfigs[findSerialPortIndexByIdentifier(TELEMETRY_UART)].functionMask = TELEMETRY_PROVIDER_DEFAULT; - serialConfigMutable()->portConfigs[findSerialPortIndexByIdentifier(GPS_UART)].functionMask = FUNCTION_GPS; + targetSerialPortFunctionConfig(targetSerialPortFunction, ARRAYLEN(targetSerialPortFunction)); telemetryConfigMutable()->halfDuplex = true; } #endif diff --git a/src/main/target/config_helper.c b/src/main/target/config_helper.c new file mode 100644 index 0000000000..e863efe2f0 --- /dev/null +++ b/src/main/target/config_helper.c @@ -0,0 +1,34 @@ +/* + * This file is part of Cleanflight and Betaflight. + * + * Cleanflight and Betaflight are free software: you can redistribute + * this software and/or modify this software under the terms of the + * GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Cleanflight and Betaflight are distributed in the hope that they + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this software. + * + * If not, see . + */ + +#include "config_helper.h" + +#ifdef USE_TARGET_CONFIG + +void targetSerialPortFunctionConfig(targetSerialPortFunction_t *config, size_t count) +{ + for (size_t i = 0 ; i < count ; i++) { + int index = findSerialPortIndexByIdentifier(config[i].identifier); + if (index >= 0) { + serialConfigMutable()->portConfigs[index].functionMask = config[i].function; + } + } +} +#endif diff --git a/src/main/target/config_helper.h b/src/main/target/config_helper.h new file mode 100644 index 0000000000..7d1f4f9212 --- /dev/null +++ b/src/main/target/config_helper.h @@ -0,0 +1,30 @@ +/* + * This file is part of Cleanflight and Betaflight. + * + * Cleanflight and Betaflight are free software: you can redistribute + * this software and/or modify this software under the terms of the + * GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Cleanflight and Betaflight are distributed in the hope that they + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this software. + * + * If not, see . + */ + +#pragma once + +#include "io/serial.h" + +typedef struct targetSerialPortFunction_s { + serialPortIdentifier_e identifier; + serialPortFunction_e function; +} targetSerialPortFunction_t; + +void targetSerialPortFunctionConfig(targetSerialPortFunction_t *config, size_t count); From cd5256bc0aa54a541afdea71e930d85cab882417 Mon Sep 17 00:00:00 2001 From: jflyper Date: Mon, 23 Apr 2018 23:10:26 +0900 Subject: [PATCH 2/3] Workaround pattern for empty function array. --- src/main/target/OMNIBUSF7/config.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/target/OMNIBUSF7/config.c b/src/main/target/OMNIBUSF7/config.c index 41c49f45b0..29349b04e4 100644 --- a/src/main/target/OMNIBUSF7/config.c +++ b/src/main/target/OMNIBUSF7/config.c @@ -32,6 +32,8 @@ static targetSerialPortFunction_t targetSerialPortFunction[] = { #if defined(OMNIBUSF7V2) && defined(ESC_SENSOR_UART) // OMNIBUS F7 V2 has an option to connect UART7_RX to ESC telemetry { ESC_SENSOR_UART, FUNCTION_ESC_SENSOR }, +#else + { SERIAL_PORT_NONE, FUNCTION_NONE }, #endif }; From cbb028402a8b29cdc5fa5f03dd9f351f2fe58696 Mon Sep 17 00:00:00 2001 From: jflyper Date: Tue, 24 Apr 2018 07:25:48 +0900 Subject: [PATCH 3/3] Move target dependent defs from target.h into config.c --- src/main/target/OMNIBUSF4/config.c | 8 ++++---- src/main/target/SPRACINGF3NEO/config.c | 11 ++++++++--- src/main/target/SPRACINGF3NEO/target.h | 5 ----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/target/OMNIBUSF4/config.c b/src/main/target/OMNIBUSF4/config.c index 35c3dc41c8..157bb3b809 100644 --- a/src/main/target/OMNIBUSF4/config.c +++ b/src/main/target/OMNIBUSF4/config.c @@ -33,10 +33,10 @@ #ifdef EXUAVF4PRO static targetSerialPortFunction_t targetSerialPortFunction[] = { - { 0, FUNCTION_TELEMETRY_SMARTPORT }, - { 2, FUNCTION_VTX_TRAMP }, - { 3, FUNCTION_RCDEVICE }, - { 5, FUNCTION_RX_SERIAL }, + { SERIAL_PORT_USART1, FUNCTION_TELEMETRY_SMARTPORT }, + { SERIAL_PORT_USART3, FUNCTION_VTX_TRAMP }, + { SERIAL_PORT_UART4, FUNCTION_RCDEVICE }, + { SERIAL_PORT_USART6, FUNCTION_RX_SERIAL }, }; #endif diff --git a/src/main/target/SPRACINGF3NEO/config.c b/src/main/target/SPRACINGF3NEO/config.c index 27ae12904c..a9c1137b68 100644 --- a/src/main/target/SPRACINGF3NEO/config.c +++ b/src/main/target/SPRACINGF3NEO/config.c @@ -52,10 +52,15 @@ #include "config_helper.h" +#define GPS_UART SERIAL_PORT_USART3 + +#define TELEMETRY_UART SERIAL_PORT_UART5 +#define TELEMETRY_PROVIDER_DEFAULT FUNCTION_TELEMETRY_SMARTPORT + static targetSerialPortFunction_t targetSerialPortFunction[] = { - { 0, FUNCTION_MSP }, - { TELEMETRY_UART, TELEMETRY_PROVIDER_DEFAULT }, - { GPS_UART, FUNCTION_GPS }, + { SERIAL_PORT_USART1, FUNCTION_MSP }, + { TELEMETRY_UART, TELEMETRY_PROVIDER_DEFAULT }, + { GPS_UART, FUNCTION_GPS }, }; void targetConfiguration(void) diff --git a/src/main/target/SPRACINGF3NEO/target.h b/src/main/target/SPRACINGF3NEO/target.h index 903449b8e8..537d7954d8 100644 --- a/src/main/target/SPRACINGF3NEO/target.h +++ b/src/main/target/SPRACINGF3NEO/target.h @@ -172,14 +172,9 @@ #define DEFAULT_RX_FEATURE FEATURE_RX_SERIAL #define DEFAULT_FEATURES (FEATURE_TRANSPONDER | FEATURE_RSSI_ADC | FEATURE_TELEMETRY | FEATURE_OSD | FEATURE_LED_STRIP) -#define GPS_UART SERIAL_PORT_USART3 - #define SERIALRX_UART SERIAL_PORT_USART2 #define SERIALRX_PROVIDER SERIALRX_SBUS -#define TELEMETRY_UART SERIAL_PORT_UART5 -#define TELEMETRY_PROVIDER_DEFAULT FUNCTION_TELEMETRY_SMARTPORT - #define USE_BUTTONS // Physically located on the optional OSD/VTX board. #define BUTTON_A_PIN PD2