Skip to content

Commit

Permalink
refactor: better HAL / hardware separation
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic committed Oct 30, 2023
1 parent 649be2e commit 5a1a0a9
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 200 deletions.
1 change: 1 addition & 0 deletions radio/src/boards/generic_stm32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(BOARD_LIB_SRC
${CMAKE_CURRENT_BINARY_DIR}/hal_adc_inputs.inc

boards/generic_stm32/module_ports.cpp
boards/generic_stm32/trainer_ports.cpp
boards/generic_stm32/aux_ports.cpp
boards/generic_stm32/sport_update.cpp
boards/generic_stm32/intmodule_heartbeat.cpp
Expand Down
142 changes: 142 additions & 0 deletions radio/src/boards/generic_stm32/trainer_ports.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* 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 "stm32_pulse_driver.h"
#include "trainer_driver.h"

#include "hal/module_port.h"
#include "dataconstants.h"

#include "hal.h"

void board_trainer_init()
{
#if defined(TRAINER_DETECT_GPIO_PIN)
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);

pinInit.Pin = TRAINER_DETECT_GPIO_PIN;
pinInit.Mode = LL_GPIO_MODE_INPUT;
pinInit.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(TRAINER_DETECT_GPIO, &pinInit);
#endif

trainer_init();
}

#if defined(TRAINER_GPIO)

static_assert(__IS_TRAINER_TIMER_OUT_CHANNEL_SUPPORTED(TRAINER_OUT_TIMER_Channel),
"Unsupported trainer timer output channel");

static_assert(__IS_TRAINER_TIMER_IN_CHANNEL_SUPPORTED(TRAINER_IN_TIMER_Channel),
"Unsupported trainer timer input channel");

static const stm32_pulse_timer_t trainerOutputTimer = {
.GPIOx = TRAINER_GPIO,
.GPIO_Pin = TRAINER_OUT_GPIO_PIN,
.GPIO_Alternate = TRAINER_GPIO_AF,
.TIMx = TRAINER_TIMER,
.TIM_Freq = TRAINER_TIMER_FREQ,
.TIM_Channel = TRAINER_OUT_TIMER_Channel,
.TIM_IRQn = TRAINER_TIMER_IRQn,
.DMAx = nullptr,
.DMA_Stream = 0,
.DMA_Channel = 0,
.DMA_IRQn = (IRQn_Type)0,
.DMA_TC_CallbackPtr = nullptr,
};

void trainer_init_dsc_out()
{
trainer_init_output(&trainerOutputTimer);
}

static const stm32_pulse_timer_t trainerInputTimer = {
.GPIOx = TRAINER_GPIO,
.GPIO_Pin = TRAINER_IN_GPIO_PIN,
.GPIO_Alternate = TRAINER_GPIO_AF,
.TIMx = TRAINER_TIMER,
.TIM_Freq = TRAINER_TIMER_FREQ,
.TIM_Channel = TRAINER_IN_TIMER_Channel,
.TIM_IRQn = TRAINER_TIMER_IRQn,
.DMAx = nullptr,
.DMA_Stream = 0,
.DMA_Channel = 0,
.DMA_IRQn = (IRQn_Type)0,
.DMA_TC_CallbackPtr = nullptr,
};

void trainer_init_dsc_in()
{
trainer_init_capture(&trainerInputTimer);
}

void trainer_stop_dsc() { trainer_stop(); }

#else
void trainer_init_dsc_out() {}
void trainer_init_dsc_in() {}
void trainer_stop_dsc() {}
#endif

bool is_trainer_dsc_connected()
{
#if defined(TRAINER_DETECT_GPIO_PIN)
bool set = LL_GPIO_IsInputPinSet(TRAINER_DETECT_GPIO, TRAINER_DETECT_GPIO_PIN);
#if defined(TRAINER_DETECT_INVERTED)
return !set;
#else
return set;
#endif
#else // TRAINER_DETECT_GPIO_PIN
return true;
#endif
}

void init_trainer_module_cppm()
{
auto port = modulePortFind(EXTERNAL_MODULE, ETX_MOD_TYPE_TIMER,
ETX_MOD_PORT_TIMER, ETX_Pol_Normal,
ETX_MOD_DIR_RX);
if (!port) return;

auto tim = (const stm32_pulse_timer_t*)port->hw_def;
if (!tim) return;

modulePortSetPower(EXTERNAL_MODULE,true);
trainer_init_capture(tim);
}

void stop_trainer_module_cppm()
{
trainer_stop();
modulePortSetPower(EXTERNAL_MODULE,false);
}

#if defined(TRAINER_TIMER_IRQHandler)
extern "C" void TRAINER_TIMER_IRQHandler() { trainer_timer_isr(); }
#endif

#if defined(TRAINER_MODULE_CPPM_TIMER_IRQHandler) && \
TRAINER_TIMER_IRQHandler != TRAINER_MODULE_CPPM_TIMER_IRQHandler
extern "C" void TRAINER_MODULE_CPPM_TIMER_IRQHandler() { trainer_timer_isr(); }
#endif
11 changes: 6 additions & 5 deletions radio/src/hal/trainer_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
#pragma once

// Startup init
void init_trainer();
void board_trainer_init();

// Output mode
void init_trainer_ppm();
void stop_trainer_ppm();
void trainer_init_dsc_out();

// Input mode
void init_trainer_capture();
void stop_trainer_capture();
void trainer_init_dsc_in();

// Stop input/output
void trainer_stop_dsc();

// Cable inserted?
bool is_trainer_dsc_connected();
Expand Down
6 changes: 0 additions & 6 deletions radio/src/myeeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@
#define GET_MODULE_PPM_DELAY(idx) (g_model.moduleData[idx].ppm.delay * 50 + 300)
#define GET_TRAINER_PPM_DELAY() (g_model.trainerData.delay * 50 + 300)

#if defined(HARDWARE_TRAINER_EXTERNAL_MODULE)
#define IS_TRAINER_EXTERNAL_MODULE() (g_model.trainerData.mode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE || g_model.trainerData.mode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE)
#else
#define IS_TRAINER_EXTERNAL_MODULE() false
#endif

#define IS_PLAY_FUNC(func) ((func) >= FUNC_PLAY_SOUND && func <= FUNC_PLAY_VALUE)

#if defined(GVARS)
Expand Down
Loading

0 comments on commit 5a1a0a9

Please sign in to comment.