Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch & ADC refactor #2631

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/macosx_cpn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
setup-python: 'false'

- name: Install dependencies
run: python3 -m pip install --upgrade pip Pillow lz4 clang
run: python3 -m pip install --upgrade pip Pillow lz4 clang jinja2

- name: Patch GitHub macOS build
run: brew install pkg-config
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/win_cpn-64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
mingw-w64-x86_64-SDL2 \
mingw-w64-x86_64-clang \
mingw-w64-x86_64-nsis
python -m pip install clang
python -m pip install clang jinja2

- name: Install Qt
uses: jurplel/install-qt-action@v3
Expand Down
1 change: 0 additions & 1 deletion cmake/GenericDefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ endif()
set(CMAKE_COLOR_MAKEFILE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_STANDARD 11)
add_definitions(-D_GLIBCXX_USE_C99=1) # proper to_string definition

set(RADIO_DIRECTORY ${PROJECT_SOURCE_DIR}/radio)
Expand Down
56 changes: 49 additions & 7 deletions cmake/Macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ macro(git_id RESULT)
endif()
endmacro(git_id)

macro(use_cxx11)
if (CMAKE_VERSION VERSION_LESS "3.1" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set (CMAKE_CXX_FLAGS "--std=gnu++11 ${CMAKE_CXX_FLAGS}")
endif ()
endmacro(use_cxx11)

macro(PrintTargetReport targetName)
if(CMAKE_CXX_COMPILER MATCHES "/(clang-)?cl\\.exe$")
Expand Down Expand Up @@ -60,7 +55,6 @@ function(AddCompilerFlags output)
endforeach()

# Add hotfix for arm64
set(ARGS ${ARGS} -Wno-asm-operand-widths -Wno-pragma-once-outside-header)

set(${output} ${${output}} ${ARGS} PARENT_SCOPE)
endfunction()
Expand All @@ -73,18 +67,66 @@ function(GenerateDatacopy source output)
# Fetch defines / include directories in use
AddCompilerFlags(GEN_DATACOPY_ARGS)

# Hack to get rid of warnings in StdPeriph lib

set(GEN_DATACOPY_ARGS
# source file MUST be the first argument
${CMAKE_CURRENT_SOURCE_DIR}/${source}
-DBACKUP ${GEN_DATACOPY_ARGS} ${SYSROOT_ARG})

set(GEN_DATACOPY_CMD
${PYTHON_EXECUTABLE} ${GEN_DATACOPY} ${GEN_DATACOPY_ARGS})

add_custom_command(
OUTPUT ${output}
COMMAND ${GEN_DATACOPY_CMD} > ${output}
DEPENDS ${GEN_DATACOPY_DEPEND}
)
endfunction()

function(AddHardwareDefTarget output)

AddCompilerFlags(HW_DEF_ARGS)

set(HW_DEF_SRC ${RADIO_DIRECTORY}/src/targets/${TARGET_DIR}/hal.h)

separate_arguments(flags UNIX_COMMAND ${CMAKE_CXX_FLAGS})
foreach(flag ${flags})
set(HW_DEF_ARGS ${HW_DEF_ARGS} ${flag})
endforeach()

set(GEN_HW_DEFS ${CMAKE_CXX_COMPILER} ${HW_DEF_ARGS} -x c++-header -E -dM ${HW_DEF_SRC})
set(GEN_HW_DEFS ${GEN_HW_DEFS} | grep -v "^#define _" | sort)

set(GEN_JSON ${PYTHON_EXECUTABLE} ${RADIO_DIRECTORY}/util/hw_defs/generate_hw_def.py)
set(GEN_JSON ${GEN_JSON} -i defines -T ${FLAVOUR} -)

add_custom_command(OUTPUT ${output}
COMMAND ${GEN_HW_DEFS} | ${GEN_JSON} > ${output}
DEPENDS ${HW_DEF_SRC} ${RADIO_DIRECTORY}/util/hw_defs/generate_hw_def.py
)

add_custom_command(OUTPUT ${output}.h
COMMAND ${GEN_HW_DEFS} > ${output}.h
DEPENDS ${HW_DEF_SRC} ${RADIO_DIRECTORY}/util/hw_defs/generate_hw_def.py
)
endfunction()

function(AddHWGenTarget input template output)

# Script
set(GEN_JSON ${PYTHON_EXECUTABLE} ${RADIO_DIRECTORY}/util/hw_defs/generate_hw_def.py)

# Inputs
set(INPUT_JSON ${CMAKE_CURRENT_BINARY_DIR}/${input})
set(TEMPLATE ${RADIO_DIRECTORY}/util/hw_defs/${template}.jinja)
set(LEGACY_JSON ${RADIO_DIRECTORY}/util/hw_defs/legacy_names.py)

# Command
set(GEN_JSON ${GEN_JSON} -t ${TEMPLATE} -T ${FLAVOUR} ${INPUT_JSON})

add_custom_command(OUTPUT ${output}
COMMAND ${GEN_JSON} > ${output}
DEPENDS ${INPUT_JSON} ${LEGACY_JSON} ${TEMPLATE}
)
endfunction()
1 change: 1 addition & 0 deletions cmake/toolchain/arm-none-eabi.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# arm-none-eabi toolchain
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_CXX_STANDARD 11)

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

Expand Down
9 changes: 5 additions & 4 deletions cmake/toolchain/native.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Native toolchain
set(CMAKE_CXX_STANDARD 17)

if(APPLE)
set(CMAKE_C_FLAGS "-Wno-asm-operand-widths -Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS "-Wno-asm-operand-widths -Wno-deprecated-declarations")
set(CMAKE_C_FLAGS_DEBUG "-Wno-asm-operand-widths -Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS_DEBUG "-Wno-asm-operand-widths -Wno-deprecated-declarations")
set(CMAKE_C_FLAGS "-Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS "-Wno-deprecated-declarations")
set(CMAKE_C_FLAGS_DEBUG "-Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS_DEBUG "-Wno-deprecated-declarations")
endif()

if(MINGW OR WIN32)
Expand Down
2 changes: 0 additions & 2 deletions companion/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS}")

use_cxx11() # ensure gnu++11 in CXX_FLAGS with CMake < 3.1

include_directories(
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}
Expand Down
2 changes: 0 additions & 2 deletions companion/src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ if(GTEST_INCDIR AND GTEST_SRCDIR AND Qt5Widgets_FOUND)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 ${WARNING_FLAGS}")

use_cxx11() # ensure gnu++11 in CXX_FLAGS with CMake < 3.1

add_executable(gtests-companion EXCLUDE_FROM_ALL ${TEST_SRC_FILES} ${CMAKE_CURRENT_SOURCE_DIR}/location.h.in)
add_dependencies(gtests-companion gtests-companion-lib)
target_link_libraries(gtests-companion gtests-companion-lib simulation firmwares storage common)
Expand Down
4 changes: 0 additions & 4 deletions companion/src/thirdparty/maxlibqt/src/widgets/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
cmake_minimum_required(VERSION 2.8.12)
cmake_policy(SET CMP0020 NEW)
set(CMAKE_CXX_STANDARD 11)
if (CMAKE_VERSION VERSION_LESS "3.1" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set (CMAKE_CXX_FLAGS "--std=gnu++11 ${CMAKE_CXX_FLAGS}")
endif ()

find_package(Qt5Core)
find_package(Qt5Widgets)
Expand Down
42 changes: 17 additions & 25 deletions radio/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set(TRANSLATIONS "EN" CACHE STRING "Radio language, one of: ${RADIO_LANGUAGES}")
set_property(CACHE TRANSLATIONS PROPERTY STRINGS ${RADIO_LANGUAGES})
set(SPLASH "DEFAULT" CACHE STRING "Splash (DEFAULT/OFF/FRSKY)")
set_property(CACHE SPLASH PROPERTY STRINGS DEFAULT OFF FRSKY)
set(PPM_UNIT "PERCENT_PREC1" CACHE STRING "PPM display unit (US/PERCENT_PREC1/PERCENT_PREC0)")
set(PPM_UNIT "PERCENT_PREC0" CACHE STRING "PPM display unit (US/PERCENT_PREC1/PERCENT_PREC0)")
set_property(CACHE PPM_UNIT PROPERTY STRINGS US PERCENT_PREC1 PERCENT_PREC0)
set(DEFAULT_MODE "" CACHE STRING "Default sticks mode")
set(POPUP_LEVEL 2 CACHE STRING "Popup level")
Expand Down Expand Up @@ -48,7 +48,6 @@ option(DEBUG_SEGGER_RTT "Debug output to Segger RTT" OFF)
option(DEBUG_WINDOWS "Turn on windows traces" OFF)
option(DEBUG_YAML "Turn on YAML traces" OFF)
option(DEBUG_LABELS "Turn on Labels traces" OFF)
option(FRSKY_STICKS "Reverse sticks for FrSky sticks" OFF)
option(NANO "Use nano newlib and binalloc")
option(TEST_BUILD_WARNING "Warn this is a test build" OFF)
option(MODULE_PROTOCOL_FCC "Add support for FCC modules" ON)
Expand All @@ -73,15 +72,6 @@ set(FIRMWARE_C_FLAGS_DEBUG "-g" CACHE STRING "Additional flags for firmware targ
set(FIRMWARE_CXX_FLAGS "" CACHE STRING "Additional flags for firmware target c++ compiler (note: all CMAKE_CXX_FLAGS[_*] are ignored for firmware/bootloader).")
set(FIRMWARE_CXX_FLAGS_DEBUG "-g" CACHE STRING "Additional flags for firmware target (Debug config) c++ compiler (note: CMAKE_CXX_FLAGS_DEBUG is ignored for firmware/bootloader).")

set(GCC_ARM_PATH "" CACHE STRING "Alternative GCC ARM path")

set(FIRMWARE_C_COMPILER "${GCC_ARM_PATH}arm-none-eabi-gcc" CACHE STRING "Specific C compiler for firmware target.")
set(FIRMWARE_CXX_COMPILER "${GCC_ARM_PATH}arm-none-eabi-g++" CACHE STRING "Specific C++ compiler for firmware target.")
set(FIRMWARE_ASM_COMPILER "${GCC_ARM_PATH}arm-none-eabi-as" CACHE STRING "Specific assembler for firmware target.")
set(FIRMWARE_OBJCOPY "${GCC_ARM_PATH}arm-none-eabi-objcopy" CACHE STRING "Specific objcopy for firmware target.")
set(FIRMWARE_SIZE "${GCC_ARM_PATH}arm-none-eabi-size" CACHE STRING "Specific size for firmware target.")
set(FIRMWARE_RANLIB "${GCC_ARM_PATH}arm-none-eabi-ranlib" CACHE STRING "Specific ranlib for firmware target.")

set(THIRDPARTY_DIR thirdparty)
set(LUA_DIR ${THIRDPARTY_DIR}/Lua/src)
set(RTOS_DIR ${THIRDPARTY_DIR}/FreeRTOS)
Expand Down Expand Up @@ -111,16 +101,17 @@ else()
message(FATAL_ERROR "Unknown PCB '${PCB}'")
endif()

set(HW_DESC_JSON ${FLAVOUR}.json)
AddHardwareDefTarget(${HW_DESC_JSON})

# enable generating JSON definition separately for debugging
add_custom_target(hardware_defs DEPENDS ${HW_DESC_JSON} ${HW_DESC_JSON}.h)

include(hal/CMakeLists.txt)

add_subdirectory(bitmaps)
add_subdirectory(fonts)

if(NOT PCB STREQUAL 9XRPRO)
option(DBLKEYS "Double Keys" ON)
if(DBLKEYS)
add_definitions(-DDBLKEYS)
endif()
endif()

if(CPU_TYPE STREQUAL STM32F4)
include(targets/common/arm/stm32/f4/CMakeLists.txt)
endif()
Expand All @@ -131,6 +122,7 @@ endif()

if(CPU_FAMILY STREQUAL STM32)
include(targets/common/arm/stm32/CMakeLists.txt)
include(boards/generic_stm32/CMakeLists.txt)
endif()

if(ARCH STREQUAL ARM)
Expand Down Expand Up @@ -185,7 +177,7 @@ if(RTC_BACKUP_RAM)
)

# Add custom target for debugging
add_custom_target(datacopy DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/datacopy.cpp)
add_custom_target(datacopy DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/datacopy.inc)
endif()

if(LUA)
Expand Down Expand Up @@ -325,10 +317,6 @@ if(DEBUG_LABELS)
add_definitions(-DDEBUG_LABELS)
endif()

if(FRSKY_STICKS)
add_definitions(-DFRSKY_STICKS)
endif()

if(IMU_LSM6DS33)
add_definitions(-DIMU_LSM6DS33)
endif()
Expand Down Expand Up @@ -405,6 +393,7 @@ set(SRC
functions.cpp
strhelpers.cpp
switches.cpp
analogs.cpp
mixer.cpp
mixer_scheduler.cpp
stamp.cpp
Expand All @@ -413,7 +402,8 @@ set(SRC
model_init.cpp
serial.cpp
sbus.cpp
hal/module_port.cpp
input_mapping.cpp
inactivity_timer.cpp
tasks/mixer_task.cpp
)

Expand Down Expand Up @@ -556,7 +546,7 @@ endif()

add_definitions(-DFREE_RTOS)

add_executable(firmware ${SRC} ${FIRMWARE_HEADERS})
add_executable(firmware ${SRC})
link_libraries(firmware -lstdc++)
add_dependencies(firmware ${RADIO_DEPENDENCIES})
set_target_properties(firmware PROPERTIES EXCLUDE_FROM_ALL TRUE)
Expand Down Expand Up @@ -618,3 +608,5 @@ if(CPU_FAMILY STREQUAL STM32)
endif()

PrintTargetReport("firmware")

AddHardwareDefTarget(hardware_defs ${RADIO_DIRECTORY}/src/targets/hw_defs/${FLAVOUR}.json)
100 changes: 100 additions & 0 deletions radio/src/analogs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it 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.
*/

#include "analogs.h"
#include "dataconstants.h"
#include "opentx_helpers.h"

#include "hal/adc_driver.h"

static char _custom_names[MAX_ANALOG_INPUTS][LEN_ANA_NAME + 1] = { 0 };

void analogSetCustomLabel(uint8_t type, uint8_t idx, const char* str, size_t len)
{
if (idx >= adcGetMaxInputs(type)) return;
idx += adcGetInputOffset(type);

strncpy(_custom_names[idx], str, min<size_t>(LEN_ANA_NAME, len));
_custom_names[idx][LEN_ANA_NAME] = '\0';
}

const char* analogGetCustomLabel(uint8_t type, uint8_t idx)
{
if (idx >= adcGetMaxInputs(type)) return "";
idx += adcGetInputOffset(type);

return _custom_names[idx];
}

bool analogHasCustomLabel(uint8_t type, uint8_t idx)
{
return *analogGetCustomLabel(type, idx) != 0;
}

static int _lookup_input_idx(uint8_t type, const char* name, size_t len,
const char* (*fct)(uint8_t,uint8_t))
{
auto max_inputs = adcGetMaxInputs(type);
if (!max_inputs) return -1;

for (uint8_t i = 0; i < max_inputs; i++) {
if (!strncmp(fct(type, i), name, len)) return i;
}

return -1;
}

int analogLookupPhysicalIdx(uint8_t type, const char* name, size_t len)
{
return _lookup_input_idx(type, name, len, adcGetInputName);
}

static int analogLookupLabelIdx(uint8_t type, const char* name, size_t len)
{
return _lookup_input_idx(type, name, len, adcGetInputLabel);
}

const char* analogGetPhysicalName(uint8_t type, uint8_t idx)
{
return adcGetInputName(type, idx);
}

const char* analogGetCanonicalName(uint8_t type, uint8_t idx)
{
// Main controls are special cases here as
// we use the label slot to place the specific names
// (2-gimbal radios vs. surface radios)

if (type == ADC_INPUT_MAIN)
return adcGetInputLabel(type, idx);

return adcGetInputName(type, idx);
}

int analogLookupCanonicalIdx(uint8_t type, const char* name, size_t len)
{
if (type == ADC_INPUT_MAIN) {
return analogLookupLabelIdx(type, name, len);
}

return analogLookupPhysicalIdx(type, name, len);
}

Loading