From c16d642d66a8a04233f43632aaee57ce303111cb Mon Sep 17 00:00:00 2001 From: blckmn Date: Wed, 15 Jun 2016 22:28:08 +1000 Subject: [PATCH 1/3] Changes to sonar for new IO --- src/main/drivers/pwm_mapping.c | 9 ++- src/main/drivers/pwm_mapping.h | 13 ++-- src/main/drivers/sonar_hcsr04.c | 33 ++++----- src/main/drivers/sonar_hcsr04.h | 4 -- src/main/main.c | 9 ++- src/main/sensors/sonar.c | 75 ++------------------ src/main/target/CC3D/target.h | 3 + src/main/target/EUSTM32F103RC/sonar_config.c | 30 ++++++++ src/main/target/EUSTM32F103RC/target.h | 2 + src/main/target/FURYF3/target.h | 3 + src/main/target/NAZE/sonar_config.c | 30 ++++++++ src/main/target/NAZE/target.h | 1 + src/main/target/OLIMEXINO/target.h | 2 + src/main/target/PORT103R/sonar_config.c | 30 ++++++++ src/main/target/PORT103R/target.h | 2 + src/main/target/RMDO/target.h | 2 + src/main/target/SPARKY/target.h | 3 + src/main/target/SPRACINGF3/target.h | 2 + src/main/target/SPRACINGF3MINI/target.h | 10 +-- 19 files changed, 150 insertions(+), 113 deletions(-) create mode 100644 src/main/target/EUSTM32F103RC/sonar_config.c create mode 100644 src/main/target/NAZE/sonar_config.c create mode 100644 src/main/target/PORT103R/sonar_config.c diff --git a/src/main/drivers/pwm_mapping.c b/src/main/drivers/pwm_mapping.c index e8c6e6b31..ffba5063b 100755 --- a/src/main/drivers/pwm_mapping.c +++ b/src/main/drivers/pwm_mapping.c @@ -189,12 +189,11 @@ pwmOutputConfiguration_t *pwmInit(drv_pwm_config_t *init) #endif #ifdef SONAR - if (init->sonarGPIOConfig && + if (init->sonarConfig && ( - CheckGPIOPin(timerHardwarePtr->pin, init->sonarGPIOConfig->gpio, init->sonarGPIOConfig->triggerPin) || - CheckGPIOPin(timerHardwarePtr->pin, init->sonarGPIOConfig->gpio, init->sonarGPIOConfig->echoPin) - ) - ) { + timerHardwarePtr->pin == init->sonarConfig->triggerPin || + timerHardwarePtr->pin == init->sonarConfig->echoPin + )) { continue; } #endif diff --git a/src/main/drivers/pwm_mapping.h b/src/main/drivers/pwm_mapping.h index 0d70fafc5..4c94681bc 100644 --- a/src/main/drivers/pwm_mapping.h +++ b/src/main/drivers/pwm_mapping.h @@ -16,7 +16,7 @@ */ #pragma once -#include "gpio.h" + #include "timer.h" #define MAX_PWM_MOTORS 12 @@ -42,11 +42,10 @@ #define ONESHOT42_TIMER_MHZ 24 #define ONESHOT125_TIMER_MHZ 8 -typedef struct sonarGPIOConfig_s { - GPIO_TypeDef *gpio; - uint16_t triggerPin; - uint16_t echoPin; -} sonarGPIOConfig_t; +typedef struct sonarIOConfig_s { + ioTag_t triggerPin; + ioTag_t echoPin; +} sonarIOConfig_t; typedef struct drv_pwm_config_s { bool useParallelPWM; @@ -78,7 +77,7 @@ typedef struct drv_pwm_config_s { uint16_t motorPwmRate; uint16_t idlePulse; // PWM value to use when initializing the driver. set this to either PULSE_1MS (regular pwm), // some higher value (used by 3d mode), or 0, for brushed pwm drivers. - sonarGPIOConfig_t *sonarGPIOConfig; + sonarIOConfig_t *sonarConfig; } drv_pwm_config_t; enum { diff --git a/src/main/drivers/sonar_hcsr04.c b/src/main/drivers/sonar_hcsr04.c index a835bfa05..dbe72acd3 100644 --- a/src/main/drivers/sonar_hcsr04.c +++ b/src/main/drivers/sonar_hcsr04.c @@ -22,7 +22,6 @@ #include "build_config.h" #include "system.h" -#include "gpio.h" #include "nvic.h" #include "io.h" #include "exti.h" @@ -43,8 +42,9 @@ static uint32_t lastMeasurementAt; static sonarHardware_t const *sonarHardware; extiCallbackRec_t hcsr04_extiCallbackRec; + static IO_t echoIO; -//static IO_t triggerIO; +static IO_t triggerIO; void hcsr04_extiHandler(extiCallbackRec_t* cb) { @@ -52,7 +52,7 @@ void hcsr04_extiHandler(extiCallbackRec_t* cb) uint32_t timing_stop; UNUSED(cb); - if (digitalIn(sonarHardware->echo_gpio, sonarHardware->echo_pin) != 0) { + if (IORead(echoIO) != 0) { timing_start = micros(); } else { @@ -71,32 +71,27 @@ void hcsr04_init(const sonarHardware_t *initialSonarHardware, sonarRange_t *sona sonarRange->detectionConeExtendedDeciDegrees = HCSR04_DETECTION_CONE_EXTENDED_DECIDEGREES; #if !defined(UNIT_TEST) - gpio_config_t gpio; #ifdef STM32F10X // enable AFIO for EXTI support RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); #endif -#ifdef STM32F303xC - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); - +#if defined(STM32F3) || defined(STM32F4) /* Enable SYSCFG clock otherwise the EXTI irq handlers are not called */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); #endif // trigger pin - gpio.pin = sonarHardware->trigger_pin; - gpio.mode = Mode_Out_PP; - gpio.speed = Speed_2MHz; - gpioInit(sonarHardware->trigger_gpio, &gpio); - - // echo pin - gpio.pin = sonarHardware->echo_pin; - gpio.mode = Mode_IN_FLOATING; - gpioInit(sonarHardware->echo_gpio, &gpio); + triggerIO = IOGetByTag(sonarHardware->triggerIO); + IOInit(triggerIO, OWNER_SONAR, RESOURCE_INPUT); + IOConfigGPIO(triggerIO, IOCFG_OUT_PP); - echoIO = IOGetByTag(sonarHardware->echoIO); + // echo pin + echoIO = IOGetByTag(sonarHardware->echoIO); + IOInit(echoIO, OWNER_SONAR, RESOURCE_INPUT); + IOConfigGPIO(echoIO, IOCFG_IN_FLOATING); + #ifdef USE_EXTI EXTIHandlerInit(&hcsr04_extiCallbackRec, hcsr04_extiHandler); EXTIConfig(echoIO, &hcsr04_extiCallbackRec, NVIC_PRIO_SONAR_EXTI, EXTI_Trigger_Rising_Falling); // TODO - priority! @@ -123,10 +118,10 @@ void hcsr04_start_reading(void) lastMeasurementAt = now; - digitalHi(sonarHardware->trigger_gpio, sonarHardware->trigger_pin); + IOHi(triggerIO); // The width of trig signal must be greater than 10us delayMicroseconds(11); - digitalLo(sonarHardware->trigger_gpio, sonarHardware->trigger_pin); + IOLo(triggerIO); #endif } diff --git a/src/main/drivers/sonar_hcsr04.h b/src/main/drivers/sonar_hcsr04.h index 99c89a4c8..311fa233f 100644 --- a/src/main/drivers/sonar_hcsr04.h +++ b/src/main/drivers/sonar_hcsr04.h @@ -21,10 +21,6 @@ #include "io.h" typedef struct sonarHardware_s { - uint16_t trigger_pin; - GPIO_TypeDef* trigger_gpio; - uint16_t echo_pin; - GPIO_TypeDef* echo_gpio; ioTag_t triggerIO; ioTag_t echoIO; } sonarHardware_t; diff --git a/src/main/main.c b/src/main/main.c index d836deee0..a964b6182 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -265,12 +265,11 @@ void init(void) if (feature(FEATURE_SONAR)) { sonarHardware = sonarGetHardwareConfiguration(&masterConfig.batteryConfig); - sonarGPIOConfig_t sonarGPIOConfig = { - .gpio = SONAR_GPIO, - .triggerPin = sonarHardware->echo_pin, - .echoPin = sonarHardware->trigger_pin, + sonarIOConfig_t sonarConfig = { + .triggerPin = sonarHardware->triggerIO, + .echoPin = sonarHardware->echoIO }; - pwm_params.sonarGPIOConfig = &sonarGPIOConfig; + pwm_params.sonarConfig = &sonarConfig; } #endif diff --git a/src/main/sensors/sonar.c b/src/main/sensors/sonar.c index 6a55389b8..5225747f3 100644 --- a/src/main/sensors/sonar.c +++ b/src/main/sensors/sonar.c @@ -26,7 +26,7 @@ #include "common/axis.h" #include "drivers/sonar_hcsr04.h" -#include "drivers/gpio.h" +#include "drivers/io.h" #include "config/runtime_config.h" #include "config/config.h" @@ -48,77 +48,14 @@ float sonarMaxTiltCos; static int32_t calculatedAltitude; +#ifndef SONAR_CUSTOM_CONFIG const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryConfig) { -#if defined(NAZE) || defined(EUSTM32F103RC) || defined(PORT103R) - static const sonarHardware_t const sonarPWM56 = { - .trigger_pin = Pin_8, // PWM5 (PB8) - 5v tolerant - .trigger_gpio = GPIOB, - .echo_pin = Pin_9, // PWM6 (PB9) - 5v tolerant - .echo_gpio = GPIOB, - .triggerIO = IO_TAG(PB8), - .echoIO = IO_TAG(PB9), - }; - static const sonarHardware_t sonarRC78 = { - .trigger_pin = Pin_0, // RX7 (PB0) - only 3.3v ( add a 1K Ohms resistor ) - .trigger_gpio = GPIOB, - .echo_pin = Pin_1, // RX8 (PB1) - only 3.3v ( add a 1K Ohms resistor ) - .echo_gpio = GPIOB, - .triggerIO = IO_TAG(PB0), - .echoIO = IO_TAG(PB1), - }; - // If we are using softserial, parallel PWM or ADC current sensor, then use motor pins 5 and 6 for sonar, otherwise use rc pins 7 and 8 - if (feature(FEATURE_SOFTSERIAL) - || feature(FEATURE_RX_PARALLEL_PWM ) - || (feature(FEATURE_CURRENT_METER) && batteryConfig->currentMeterType == CURRENT_SENSOR_ADC)) { - return &sonarPWM56; - } else { - return &sonarRC78; - } -#elif defined(OLIMEXINO) +#if defined(SONAR_TRIGGER_PIN) && defined(SONAR_ECHO_PIN) UNUSED(batteryConfig); static const sonarHardware_t const sonarHardware = { - .trigger_pin = Pin_0, // RX7 (PB0) - only 3.3v ( add a 1K Ohms resistor ) - .trigger_gpio = GPIOB, - .echo_pin = Pin_1, // RX8 (PB1) - only 3.3v ( add a 1K Ohms resistor ) - .echo_gpio = GPIOB, - .triggerIO = IO_TAG(PB0), - .echoIO = IO_TAG(PB1), - }; - return &sonarHardware; -#elif defined(CC3D) - UNUSED(batteryConfig); - static const sonarHardware_t const sonarHardware = { - .trigger_pin = Pin_5, // (PB5) - .trigger_gpio = GPIOB, - .echo_pin = Pin_0, // (PB0) - only 3.3v ( add a 1K Ohms resistor ) - .echo_gpio = GPIOB, - .triggerIO = IO_TAG(PB5), - .echoIO = IO_TAG(PB0), - }; - return &sonarHardware; - -// TODO - move sonar pin selection to CLI -#elif defined(SPRACINGF3) || defined(SPRACINGF3MINI) || defined(FURYF3) || defined(RMDO) - UNUSED(batteryConfig); - static const sonarHardware_t const sonarHardware = { - .trigger_pin = Pin_0, // RC_CH7 (PB0) - only 3.3v ( add a 1K Ohms resistor ) - .trigger_gpio = GPIOB, - .echo_pin = Pin_1, // RC_CH8 (PB1) - only 3.3v ( add a 1K Ohms resistor ) - .echo_gpio = GPIOB, - .triggerIO = IO_TAG(PB0), - .echoIO = IO_TAG(PB1), - }; - return &sonarHardware; -#elif defined(SPARKY) - UNUSED(batteryConfig); - static const sonarHardware_t const sonarHardware = { - .trigger_pin = Pin_2, // PWM6 (PA2) - only 3.3v ( add a 1K Ohms resistor ) - .trigger_gpio = GPIOA, - .echo_pin = Pin_1, // PWM7 (PB1) - only 3.3v ( add a 1K Ohms resistor ) - .echo_gpio = GPIOB, - .triggerIO = IO_TAG(PA2), - .echoIO = IO_TAG(PB1), + .triggerIO = IO_TAG(SONAR_TRIGGER_PIN), + .echoIO = IO_TAG(SONAR_ECHO_PIN), }; return &sonarHardware; #elif defined(UNIT_TEST) @@ -128,7 +65,7 @@ const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryCon #error Sonar not defined for target #endif } - +#endif void sonarInit(const sonarHardware_t *sonarHardware) { sonarRange_t sonarRange; diff --git a/src/main/target/CC3D/target.h b/src/main/target/CC3D/target.h index 708d4547a..dc39a80c5 100644 --- a/src/main/target/CC3D/target.h +++ b/src/main/target/CC3D/target.h @@ -118,6 +118,9 @@ #define DISPLAY #define SONAR +#define SONAR_ECHO_PIN PB0 +#define SONAR_TRIGGER_PIN PB5 + #undef GPS #undef BARO diff --git a/src/main/target/EUSTM32F103RC/sonar_config.c b/src/main/target/EUSTM32F103RC/sonar_config.c new file mode 100644 index 000000000..c563fa9f9 --- /dev/null +++ b/src/main/target/EUSTM32F103RC/sonar_config.c @@ -0,0 +1,30 @@ + +#include "drivers/sonar_hcsr04.h" +#include "drivers/io.h" +#include "config/runtime_config.h" +#include "config/config.h" + +#include "sensors/battery.h" +#include "sensors/sonar.h" + +#ifdef SONAR_CUSTOM_CONFIG +const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryConfig) +{ + static const sonarHardware_t const sonarPWM56 = { + .triggerIO = IO_TAG(PB8), + .echoIO = IO_TAG(PB9), + }; + static const sonarHardware_t sonarRC78 = { + .triggerIO = IO_TAG(PB0), + .echoIO = IO_TAG(PB1), + }; + // If we are using softserial, parallel PWM or ADC current sensor, then use motor pins 5 and 6 for sonar, otherwise use rc pins 7 and 8 + if (feature(FEATURE_SOFTSERIAL) + || feature(FEATURE_RX_PARALLEL_PWM ) + || (feature(FEATURE_CURRENT_METER) && batteryConfig->currentMeterType == CURRENT_SENSOR_ADC)) { + return &sonarPWM56; + } else { + return &sonarRC78; + } +} +#endif \ No newline at end of file diff --git a/src/main/target/EUSTM32F103RC/target.h b/src/main/target/EUSTM32F103RC/target.h index ef7534c66..83fe60e92 100644 --- a/src/main/target/EUSTM32F103RC/target.h +++ b/src/main/target/EUSTM32F103RC/target.h @@ -69,6 +69,8 @@ #define MAG_AK8975_ALIGN CW180_DEG_FLIP #define SONAR +#define SONAR_CUSTOM_CONFIG + #define DISPLAY #define USE_USART1 diff --git a/src/main/target/FURYF3/target.h b/src/main/target/FURYF3/target.h index fb4b899ea..c68ce2c08 100644 --- a/src/main/target/FURYF3/target.h +++ b/src/main/target/FURYF3/target.h @@ -184,6 +184,9 @@ #define WS2811_DMA_HANDLER_IDENTIFER DMA1_CH2_HANDLER #define SONAR +#define SONAR_ECHO_PIN PB1 +#define SONAR_TRIGGER_PIN PB0 + #define DEFAULT_RX_FEATURE FEATURE_RX_PPM #define DEFAULT_FEATURES FEATURE_BLACKBOX diff --git a/src/main/target/NAZE/sonar_config.c b/src/main/target/NAZE/sonar_config.c new file mode 100644 index 000000000..c563fa9f9 --- /dev/null +++ b/src/main/target/NAZE/sonar_config.c @@ -0,0 +1,30 @@ + +#include "drivers/sonar_hcsr04.h" +#include "drivers/io.h" +#include "config/runtime_config.h" +#include "config/config.h" + +#include "sensors/battery.h" +#include "sensors/sonar.h" + +#ifdef SONAR_CUSTOM_CONFIG +const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryConfig) +{ + static const sonarHardware_t const sonarPWM56 = { + .triggerIO = IO_TAG(PB8), + .echoIO = IO_TAG(PB9), + }; + static const sonarHardware_t sonarRC78 = { + .triggerIO = IO_TAG(PB0), + .echoIO = IO_TAG(PB1), + }; + // If we are using softserial, parallel PWM or ADC current sensor, then use motor pins 5 and 6 for sonar, otherwise use rc pins 7 and 8 + if (feature(FEATURE_SOFTSERIAL) + || feature(FEATURE_RX_PARALLEL_PWM ) + || (feature(FEATURE_CURRENT_METER) && batteryConfig->currentMeterType == CURRENT_SENSOR_ADC)) { + return &sonarPWM56; + } else { + return &sonarRC78; + } +} +#endif \ No newline at end of file diff --git a/src/main/target/NAZE/target.h b/src/main/target/NAZE/target.h index ffabc5e58..d447ffe1f 100644 --- a/src/main/target/NAZE/target.h +++ b/src/main/target/NAZE/target.h @@ -108,6 +108,7 @@ #define MAG_HMC5883_ALIGN CW180_DEG #define SONAR +#define SONAR_CUSTOM_CONFIG #define DISPLAY #define USE_USART1 diff --git a/src/main/target/OLIMEXINO/target.h b/src/main/target/OLIMEXINO/target.h index 86a430472..c50c1d51f 100644 --- a/src/main/target/OLIMEXINO/target.h +++ b/src/main/target/OLIMEXINO/target.h @@ -59,6 +59,8 @@ #define USE_MAG_HMC5883 #define SONAR +#define SONAR_ECHO_PIN PB1 +#define SONAR_TRIGGER_PIN PB0 #define USE_USART1 #define USE_USART2 diff --git a/src/main/target/PORT103R/sonar_config.c b/src/main/target/PORT103R/sonar_config.c new file mode 100644 index 000000000..c563fa9f9 --- /dev/null +++ b/src/main/target/PORT103R/sonar_config.c @@ -0,0 +1,30 @@ + +#include "drivers/sonar_hcsr04.h" +#include "drivers/io.h" +#include "config/runtime_config.h" +#include "config/config.h" + +#include "sensors/battery.h" +#include "sensors/sonar.h" + +#ifdef SONAR_CUSTOM_CONFIG +const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryConfig) +{ + static const sonarHardware_t const sonarPWM56 = { + .triggerIO = IO_TAG(PB8), + .echoIO = IO_TAG(PB9), + }; + static const sonarHardware_t sonarRC78 = { + .triggerIO = IO_TAG(PB0), + .echoIO = IO_TAG(PB1), + }; + // If we are using softserial, parallel PWM or ADC current sensor, then use motor pins 5 and 6 for sonar, otherwise use rc pins 7 and 8 + if (feature(FEATURE_SOFTSERIAL) + || feature(FEATURE_RX_PARALLEL_PWM ) + || (feature(FEATURE_CURRENT_METER) && batteryConfig->currentMeterType == CURRENT_SENSOR_ADC)) { + return &sonarPWM56; + } else { + return &sonarRC78; + } +} +#endif \ No newline at end of file diff --git a/src/main/target/PORT103R/target.h b/src/main/target/PORT103R/target.h index c0dfc4aa3..1cf06398c 100644 --- a/src/main/target/PORT103R/target.h +++ b/src/main/target/PORT103R/target.h @@ -87,6 +87,8 @@ #define USE_FLASH_M25P16 #define SONAR +#define SONAR_CUSTOM_CONFIG + #define DISPLAY #define USE_USART1 diff --git a/src/main/target/RMDO/target.h b/src/main/target/RMDO/target.h index 09a3497ea..24234ea27 100644 --- a/src/main/target/RMDO/target.h +++ b/src/main/target/RMDO/target.h @@ -52,6 +52,8 @@ #define USE_FLASH_M25P16 #define SONAR +#define SONAR_ECHO_PIN PB1 +#define SONAR_TRIGGER_PIN PB0 #define USE_USART1 #define USE_USART2 diff --git a/src/main/target/SPARKY/target.h b/src/main/target/SPARKY/target.h index 7e3e2ed9d..32b783d64 100644 --- a/src/main/target/SPARKY/target.h +++ b/src/main/target/SPARKY/target.h @@ -156,6 +156,9 @@ // USART2, PA3 #define BIND_PIN PA3 +//#define SONAR +//#define SONAR_ECHO_PIN PB1 +//#define SONAR_TRIGGER_PIN PA2 // available IO pins (from schematics) //#define TARGET_IO_PORTA (BIT(1)|BIT(2)|BIT(3)|BIT(4)|BIT(6)|BIT(7)|BIT(8)|BIT(9)|BIT(10)|BIT(11)|BIT(12)|BIT(13)|BIT(14)|BIT(15)) diff --git a/src/main/target/SPRACINGF3/target.h b/src/main/target/SPRACINGF3/target.h index c041b54d9..52263deb8 100644 --- a/src/main/target/SPRACINGF3/target.h +++ b/src/main/target/SPRACINGF3/target.h @@ -61,6 +61,8 @@ #define USE_FLASH_M25P16 #define SONAR +#define SONAR_ECHO_PIN PB1 +#define SONAR_TRIGGER_PIN PB0 #define USE_USART1 #define USE_USART2 diff --git a/src/main/target/SPRACINGF3MINI/target.h b/src/main/target/SPRACINGF3MINI/target.h index 5272a4903..6063abd97 100644 --- a/src/main/target/SPRACINGF3MINI/target.h +++ b/src/main/target/SPRACINGF3MINI/target.h @@ -24,9 +24,9 @@ // early prototype had slightly different pin mappings. //#define SPRACINGF3MINI_MKII_REVA -#define LED0 PB3 +#define LED0 PB3 -#define BEEPER PC15 +#define BEEPER PC15 #define BEEPER_INVERTED #define USABLE_TIMER_CHANNEL_COUNT 12 // 8 Outputs; PPM; LED Strip; 2 additional PWM pins also on UART3 RX/TX pins. @@ -50,7 +50,7 @@ //#define USE_FAKE_ACC #define USE_ACC_MPU6500 -#define ACC_MPU6500_ALIGN CW180_DEG +#define ACC_MPU6500_ALIGN CW180_DEG #define GYRO_MPU6500_ALIGN CW180_DEG #define BARO @@ -64,11 +64,13 @@ #define MAG_AK8975_ALIGN CW90_DEG_FLIP #define SONAR +#define SONAR_ECHO_PIN PB1 +#define SONAR_TRIGGER_PIN PB0 #define USB_IO #define USB_CABLE_DETECTION -#define USB_DETECT_PIN PB5 +#define USB_DETECT_PIN PB5 #define USE_VCP #define USE_USART1 From e18aeb8dc2771ffd121be405e6dd49215a1aa8ae Mon Sep 17 00:00:00 2001 From: blckmn Date: Wed, 15 Jun 2016 22:29:09 +1000 Subject: [PATCH 2/3] Whitespace --- src/main/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/main.c b/src/main/main.c index a964b6182..0df3685ba 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -269,7 +269,7 @@ void init(void) .triggerPin = sonarHardware->triggerIO, .echoPin = sonarHardware->echoIO }; - pwm_params.sonarConfig = &sonarConfig; + pwm_params.sonarConfig = &sonarConfig; } #endif From cd25f0fa968daa4969df7173381cce7308f8ef4b Mon Sep 17 00:00:00 2001 From: blckmn Date: Thu, 16 Jun 2016 16:35:00 +1000 Subject: [PATCH 3/3] Updated as per code review from @martinbudden --- src/main/sensors/sonar.c | 9 +++++++-- src/main/target/EUSTM32F103RC/sonar_config.c | 14 ++++++-------- src/main/target/NAZE/sonar_config.c | 4 +--- src/main/target/PORT103R/sonar_config.c | 14 ++++++-------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/sensors/sonar.c b/src/main/sensors/sonar.c index 5225747f3..73859f391 100644 --- a/src/main/sensors/sonar.c +++ b/src/main/sensors/sonar.c @@ -48,7 +48,10 @@ float sonarMaxTiltCos; static int32_t calculatedAltitude; -#ifndef SONAR_CUSTOM_CONFIG +#ifdef SONAR_CUSTOM_CONFIG +const sonarHardware_t *sonarGetTargetHardwareConfiguration(batteryConfig_t *batteryConfig); +#endif + const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryConfig) { #if defined(SONAR_TRIGGER_PIN) && defined(SONAR_ECHO_PIN) @@ -58,6 +61,8 @@ const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryCon .echoIO = IO_TAG(SONAR_ECHO_PIN), }; return &sonarHardware; +#elif defined(SONAR_CUSTOM_CONFIG) + return sonarGetTargetHardwareConfiguration(batteryConfig); #elif defined(UNIT_TEST) UNUSED(batteryConfig); return 0; @@ -65,7 +70,7 @@ const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryCon #error Sonar not defined for target #endif } -#endif + void sonarInit(const sonarHardware_t *sonarHardware) { sonarRange_t sonarRange; diff --git a/src/main/target/EUSTM32F103RC/sonar_config.c b/src/main/target/EUSTM32F103RC/sonar_config.c index c563fa9f9..e2f4fb7b1 100644 --- a/src/main/target/EUSTM32F103RC/sonar_config.c +++ b/src/main/target/EUSTM32F103RC/sonar_config.c @@ -7,16 +7,15 @@ #include "sensors/battery.h" #include "sensors/sonar.h" -#ifdef SONAR_CUSTOM_CONFIG -const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryConfig) +const sonarHardware_t *sonarGetTargetHardwareConfiguration(batteryConfig_t *batteryConfig) { static const sonarHardware_t const sonarPWM56 = { - .triggerIO = IO_TAG(PB8), - .echoIO = IO_TAG(PB9), + .triggerIO = IO_TAG(PB8), + .echoIO = IO_TAG(PB9), }; static const sonarHardware_t sonarRC78 = { - .triggerIO = IO_TAG(PB0), - .echoIO = IO_TAG(PB1), + .triggerIO = IO_TAG(PB0), + .echoIO = IO_TAG(PB1), }; // If we are using softserial, parallel PWM or ADC current sensor, then use motor pins 5 and 6 for sonar, otherwise use rc pins 7 and 8 if (feature(FEATURE_SOFTSERIAL) @@ -26,5 +25,4 @@ const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryCon } else { return &sonarRC78; } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/target/NAZE/sonar_config.c b/src/main/target/NAZE/sonar_config.c index c563fa9f9..3362146c0 100644 --- a/src/main/target/NAZE/sonar_config.c +++ b/src/main/target/NAZE/sonar_config.c @@ -7,8 +7,7 @@ #include "sensors/battery.h" #include "sensors/sonar.h" -#ifdef SONAR_CUSTOM_CONFIG -const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryConfig) +const sonarHardware_t *sonarGetTargetHardwareConfiguration(batteryConfig_t *batteryConfig) { static const sonarHardware_t const sonarPWM56 = { .triggerIO = IO_TAG(PB8), @@ -27,4 +26,3 @@ const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryCon return &sonarRC78; } } -#endif \ No newline at end of file diff --git a/src/main/target/PORT103R/sonar_config.c b/src/main/target/PORT103R/sonar_config.c index c563fa9f9..e2f4fb7b1 100644 --- a/src/main/target/PORT103R/sonar_config.c +++ b/src/main/target/PORT103R/sonar_config.c @@ -7,16 +7,15 @@ #include "sensors/battery.h" #include "sensors/sonar.h" -#ifdef SONAR_CUSTOM_CONFIG -const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryConfig) +const sonarHardware_t *sonarGetTargetHardwareConfiguration(batteryConfig_t *batteryConfig) { static const sonarHardware_t const sonarPWM56 = { - .triggerIO = IO_TAG(PB8), - .echoIO = IO_TAG(PB9), + .triggerIO = IO_TAG(PB8), + .echoIO = IO_TAG(PB9), }; static const sonarHardware_t sonarRC78 = { - .triggerIO = IO_TAG(PB0), - .echoIO = IO_TAG(PB1), + .triggerIO = IO_TAG(PB0), + .echoIO = IO_TAG(PB1), }; // If we are using softserial, parallel PWM or ADC current sensor, then use motor pins 5 and 6 for sonar, otherwise use rc pins 7 and 8 if (feature(FEATURE_SOFTSERIAL) @@ -26,5 +25,4 @@ const sonarHardware_t *sonarGetHardwareConfiguration(batteryConfig_t *batteryCon } else { return &sonarRC78; } -} -#endif \ No newline at end of file +} \ No newline at end of file