Skip to content

Commit

Permalink
feat(TX16S): User options for AUX1/AUX2 serial port power (ON/OFF)
Browse files Browse the repository at this point in the history
User controllable serial port power for TX16S
  • Loading branch information
pfeerick authored Apr 24, 2022
2 parents 0fabd8c + bcc8cdd commit f5b1b53
Show file tree
Hide file tree
Showing 45 changed files with 206 additions and 88 deletions.
21 changes: 17 additions & 4 deletions companion/src/firmwares/edgetx/yaml_generalsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const YamlLookupTable serialPortLut = {
const YamlLookupTable uartModeLut = {
{ GeneralSettings::AUX_SERIAL_OFF, "NONE" },
{ GeneralSettings::AUX_SERIAL_TELE_MIRROR, "TELEMETRY_MIRROR" },
{ GeneralSettings::AUX_SERIAL_TELE_IN, "TELEMETRY" },
{ GeneralSettings::AUX_SERIAL_TELE_IN, "TELEMETRY_IN" },
{ GeneralSettings::AUX_SERIAL_SBUS_TRAINER, "SBUS_TRAINER" },
{ GeneralSettings::AUX_SERIAL_LUA, "LUA" },
{ GeneralSettings::AUX_SERIAL_CLI, "CLI" },
Expand Down Expand Up @@ -214,9 +214,10 @@ Node convert<GeneralSettings>::encode(const GeneralSettings& rhs)

Node serialPort;
for (int i = 0; i < GeneralSettings::SP_COUNT; i++) {
if (rhs.serialPort[i] != UART_MODE_NONE) {
if (rhs.serialPort[i] != GeneralSettings::AUX_SERIAL_OFF || rhs.serialPower[i]) {
Node mode = uartModeLut << rhs.serialPort[i];
serialPort[LookupValue(serialPortLut, i)]["mode"] = mode;
serialPort[LookupValue(serialPortLut, i)]["power"] = (int)rhs.serialPower[i];
}
}
if (serialPort && serialPort.IsMap())
Expand Down Expand Up @@ -404,11 +405,13 @@ bool convert<GeneralSettings>::decode(const Node& node, GeneralSettings& rhs)
node["varioRepeat"] >> rhs.varioRepeat;
node["backgroundVolume"] >> ioffset_int(rhs.backgroundVolume, 2);

// depreciated v2.7 replaced by serialPort
if (node["auxSerialMode"]) {
node["auxSerialMode"] >> oldUartModeLut >>
rhs.serialPort[GeneralSettings::SP_AUX1];
}

// depreciated v2.7 replaced by serialPort
if (node["aux2SerialMode"]) {
node["aux2SerialMode"] >> oldUartModeLut >>
rhs.serialPort[GeneralSettings::SP_AUX2];
Expand All @@ -421,13 +424,23 @@ bool convert<GeneralSettings>::decode(const Node& node, GeneralSettings& rhs)
YAML::Node port_nr = port.first >> serialPortLut;
if (port_nr) {
int p = port_nr.as<int>();
if (p >= 0 && p < GeneralSettings::SP_COUNT && port.second.IsMap())
if (p >= 0 && p < GeneralSettings::SP_COUNT && port.second.IsMap()) {
port.second["mode"] >> uartModeLut >> rhs.serialPort[p];
// introduced v2.8
Node port_pwr = port.second["power"];
if (port_pwr && port_pwr.IsScalar()) {
try {
int pwr = port_pwr.as<int>();
if (pwr == 0 || pwr == 1)
rhs.serialPower[p] = pwr;
} catch(...) {}
}
}
}
}
}
}

node["antennaMode"] >> antennaModeLut >> rhs.antennaMode;
node["backlightColor"] >> rhs.backlightColor;
node["pwrOnSpeed"] >> rhs.pwrOnSpeed;
Expand Down
3 changes: 2 additions & 1 deletion companion/src/firmwares/eeprominterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ enum Capability {
TopBarZones,
HasModelsList,
HasFlySkyGimbals,
RotaryEncoderNavigation
RotaryEncoderNavigation,
HasSoftwareSerialPower
};

class EEPROMInterface
Expand Down
1 change: 1 addition & 0 deletions companion/src/firmwares/generalsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class GeneralSettings {
unsigned int mavbaud;
unsigned int switchUnlockStates;
unsigned int serialPort[SP_COUNT];
bool serialPower[SP_COUNT];
int antennaMode;
unsigned int backlightColor;
CustomFunctionData customFn[CPN_MAX_SPECIAL_FUNCTIONS];
Expand Down
2 changes: 2 additions & 0 deletions companion/src/firmwares/opentx/opentxinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,8 @@ int OpenTxFirmware::getCapability(::Capability capability)
IS_TARANIS_X9LITE(board) || IS_RADIOMASTER_TX12(board) ||
IS_RADIOMASTER_ZORRO(board) || IS_RADIOMASTER_TX16S(board) ||
IS_JUMPER_T18(board));
case HasSoftwareSerialPower:
return IS_RADIOMASTER_TX16S(board);
default:
return 0;
}
Expand Down
32 changes: 23 additions & 9 deletions companion/src/generaledit/hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,32 +219,46 @@ HardwarePanel::HardwarePanel(QWidget * parent, GeneralSettings & generalSettings
ExclusiveComboGroup *exclGroup = new ExclusiveComboGroup(
this, [=](const QVariant &value) { return value == 0; });

addSection(tr("Serial port"), row);

if (firmware->getCapability(HasAuxSerialMode)) {
QString lbl = "Serial Port";
if (IS_RADIOMASTER_TX16S(board))
lbl.append(" (TTL)");
QString lbl = "AUX1";
addLabel(tr("%1").arg(lbl), row, 0);
AutoComboBox *serialPortMode = new AutoComboBox(this);
serialPortMode->setModel(editorItemModels->getItemModel(auxmodelid));
serialPortMode->setField(generalSettings.serialPort[GeneralSettings::SP_AUX1]);
exclGroup->addCombo(serialPortMode);
addParams(row, serialPortMode);

AutoCheckBox *serialPortPower = new AutoCheckBox(this);
serialPortPower->setField(generalSettings.serialPower[GeneralSettings::SP_AUX1], this);
serialPortPower->setText(tr("Power"));

addParams(row, serialPortMode, serialPortPower);

if (!firmware->getCapability(HasSoftwareSerialPower))
serialPortPower->setVisible(false);
}

if (firmware->getCapability(HasAux2SerialMode)) {
QString lbl = "Serial Port 2";
if (IS_RADIOMASTER_TX16S(board))
lbl.append(" (TTL)");
QString lbl = "AUX2";
addLabel(tr("%1").arg(lbl), row, 0);
AutoComboBox *serialPortMode = new AutoComboBox(this);
serialPortMode->setModel(editorItemModels->getItemModel(auxmodelid));
serialPortMode->setField(generalSettings.serialPort[GeneralSettings::SP_AUX2]);
exclGroup->addCombo(serialPortMode);
addParams(row, serialPortMode);

AutoCheckBox *serialPortPower = new AutoCheckBox(this);
serialPortPower->setField(generalSettings.serialPower[GeneralSettings::SP_AUX2], this);
serialPortPower->setText(tr("Power"));

addParams(row, serialPortMode, serialPortPower);

if (!firmware->getCapability(HasSoftwareSerialPower))
serialPortPower->setVisible(false);
}

if (firmware->getCapability(HasVCPSerialMode)) {
addLabel(tr("Serial Port VCP"), row, 0);
addLabel(tr("VCP"), row, 0);
AutoComboBox *serialPortMode = new AutoComboBox(this);
serialPortMode->setModel(editorItemModels->getItemModel(vcpmodelid));
serialPortMode->setField(generalSettings.serialPort[GeneralSettings::SP_VCP]);
Expand Down
18 changes: 9 additions & 9 deletions radio/src/datastructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,33 @@ static inline void check_struct()
CHKSIZE(TrainerData, 16);

#if defined(PCBXLITES)
CHKSIZE(RadioData, 859);
CHKSIZE(RadioData, 861);
CHKSIZE(ModelData, 6161);
#elif defined(PCBXLITE)
CHKSIZE(RadioData, 857);
CHKSIZE(RadioData, 859);
CHKSIZE(ModelData, 6161);
#elif defined(RADIO_TPRO)
CHKSIZE(RadioData, 840);
CHKSIZE(RadioData, 842);
CHKSIZE(ModelData, 6186);
#elif defined(PCBX7)
CHKSIZE(RadioData, 863);
CHKSIZE(RadioData, 865);
CHKSIZE(ModelData, 6161);
#elif defined(PCBX9E)
CHKSIZE(RadioData, 953);
CHKSIZE(RadioData, 955);
CHKSIZE(ModelData, 6613);
#elif defined(PCBX9D) || defined(PCBX9DP)
CHKSIZE(RadioData, 895);
CHKSIZE(RadioData, 897);
CHKSIZE(ModelData, 6605);
#elif defined(PCBHORUS)
#if defined(PCBX10)
CHKSIZE(RadioData, 919);
CHKSIZE(RadioData, 921);
CHKSIZE(ModelData, 11025);
#else
CHKSIZE(RadioData, 901);
CHKSIZE(RadioData, 903);
CHKSIZE(ModelData, 11023);
#endif
#elif defined(PCBNV14)
CHKSIZE(RadioData, 849);
CHKSIZE(RadioData, 851);
CHKSIZE(ModelData, 10839);
#endif

Expand Down
3 changes: 2 additions & 1 deletion radio/src/datastructs_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "definitions.h"
#include "opentx_types.h"
#include "globals.h"
#include "serial.h"

#if defined(PCBTARANIS)
#define N_TARANIS_FIELD(x)
Expand Down Expand Up @@ -902,7 +903,7 @@ PACK(struct RadioData {

CUST_ATTR(auxSerialMode, r_serialMode, nullptr);
CUST_ATTR(aux2SerialMode, r_serialMode, nullptr);
NOBACKUP(uint16_t serialPort ARRAY(STORAGE_SERIAL_PORTS,struct_serialConfig,nullptr));
NOBACKUP(uint32_t serialPort ARRAY(SERIAL_CONF_BITS_PER_PORT,struct_serialConfig,nullptr));

EXTRA_GENERAL_FIELDS

Expand Down
16 changes: 16 additions & 0 deletions radio/src/gui/colorlcd/radio_hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,22 @@ class SerialConfigWindow : public FormGroup
aux->setAvailableHandler(
[=](int value) { return isSerialModeAvailable(port_nr, value); });
grid.nextLine();

#if defined(SWSERIALPOWER)
if (port_nr < SP_VCP)
{
new StaticText(this, grid.getLabelSlot(true), STR_AUX_SERIAL_PORT_POWER, 0, COLOR_THEME_PRIMARY1);
new CheckBox(
this, grid.getFieldSlot(1, 0),
[=] { return serialGetPower(port_nr); },
[=](int8_t newValue) {
serialSetPower(port_nr, (bool)newValue);
SET_DIRTY();
}
);
grid.nextLine();
}
#endif
}

if (display_ttl_warning) {
Expand Down
2 changes: 0 additions & 2 deletions radio/src/hal/serial_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

#include "serial_driver.h"

typedef void etx_pwr_driver;

typedef struct {
const char* name;
const etx_serial_driver_t* uart;
Expand Down
71 changes: 53 additions & 18 deletions radio/src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,27 @@ int8_t getSerialPortMode(uint8_t port_nr)
#if !defined(BOOT)
if (port_nr < MAX_SERIAL_PORTS) {
auto cfg = g_eeGeneral.serialPort;
cfg >>= port_nr * 4; // 4 bits per port
return cfg & 0xF;
cfg >>= port_nr * SERIAL_CONF_BITS_PER_PORT;
return cfg & SERIAL_CONF_MODE_MASK;
}
#endif

return UART_MODE_NONE;
}

static bool getSerialPower(uint8_t port_nr)
{
#if !defined(BOOT)
if (port_nr < MAX_SERIAL_PORTS) {
auto cfg = g_eeGeneral.serialPort;
cfg >>= (port_nr * SERIAL_CONF_BITS_PER_PORT);
return cfg & (1 << SERIAL_CONF_POWER_BIT);
}
#endif

return false;
}

struct SerialPortState
{
uint8_t mode;
Expand Down Expand Up @@ -189,7 +202,7 @@ static void serialSetCallBacks(int mode, void* ctx, const etx_serial_port_t* por
}
}

static void serialSetupPort(int mode, etx_serial_init& params, bool& power_required)
static void serialSetupPort(int mode, etx_serial_init& params)
{
switch (mode) {

Expand All @@ -203,7 +216,6 @@ static void serialSetupPort(int mode, etx_serial_init& params, bool& power_requi
#if !defined(BOOT)
case UART_MODE_TELEMETRY_MIRROR:
// TODO: query telemetry baudrate / add setting for module
power_required = true;
#if defined(CROSSFIRE)
if (modelTelemetryProtocol() == PROTOCOL_TELEMETRY_CROSSFIRE) {
params.baudrate = CROSSFIRE_TELEM_MIRROR_BAUDRATE;
Expand All @@ -217,7 +229,6 @@ static void serialSetupPort(int mode, etx_serial_init& params, bool& power_requi
if (modelTelemetryProtocol() == PROTOCOL_TELEMETRY_FRSKY_D_SECONDARY) {
params.baudrate = FRSKY_D_BAUDRATE;
params.rx_enable = true;
power_required = true;
}
break;

Expand All @@ -227,22 +238,19 @@ static void serialSetupPort(int mode, etx_serial_init& params, bool& power_requi
params.parity = ETX_Parity_Even;
params.stop_bits = ETX_StopBits_Two;
params.rx_enable = true;
power_required = true;
break;

#if defined(LUA)
case UART_MODE_LUA:
params.baudrate = LUA_DEFAULT_BAUDRATE;
params.rx_enable = true;
power_required = true;
break;
#endif

#if defined(INTERNAL_GPS)
case UART_MODE_GPS:
params.baudrate = GPS_USART_BAUDRATE;
params.rx_enable = true;
power_required = true;
break;
#endif
#endif
Expand All @@ -263,6 +271,26 @@ const etx_serial_port_t* serialGetPort(uint8_t port_nr)
return port;
}

static void serialSetPowerState(uint8_t port_nr)
{
const etx_serial_port_t* port = serialGetPort(port_nr);
if (!port) return;

if (port->set_pwr) {
port->set_pwr(getSerialPower(port_nr));
}
}

void serialSetPower(uint8_t port_nr, bool enabled)
{
if (port_nr >= MAX_SERIAL_PORTS) return;
uint32_t pwr = (enabled ? 1 : 0) << SERIAL_CONF_POWER_BIT;
uint32_t pwr_mask = (1 << SERIAL_CONF_POWER_BIT) << port_nr * SERIAL_CONF_BITS_PER_PORT;
g_eeGeneral.serialPort = (g_eeGeneral.serialPort & ~pwr_mask) |
(pwr << port_nr * SERIAL_CONF_BITS_PER_PORT);

serialSetPowerState(port_nr);
}

void serialInit(uint8_t port_nr, int mode)
{
Expand Down Expand Up @@ -292,21 +320,21 @@ void serialInit(uint8_t port_nr, int mode)
.rx_enable = false,
};

bool power_required = false;
serialSetupPort(mode, params, power_required);

serialSetupPort(mode, params);

#if defined(SWSERIALPOWER)
// Set power on/off
if (port_nr < SP_VCP)
serialSetPowerState(port_nr);
#endif

if (params.baudrate != 0) {
state->mode = mode;
state->port = port;

if (port) {
if (port->uart && port->uart->init)
state->usart_ctx = port->uart->init(&params);

// Set power on/off
if (port->set_pwr) {
port->set_pwr(power_required);
}
}

// Update callbacks once the port is setup
Expand All @@ -329,12 +357,19 @@ int serialGetMode(uint8_t port_nr)
return getSerialPortMode(port_nr);
}

bool serialGetPower(uint8_t port_nr)
{
return getSerialPower(port_nr);
}

void serialSetMode(uint8_t port_nr, int mode)
{
if (port_nr >= MAX_SERIAL_PORTS) return;
uint16_t m = mode & 0xF;
uint16_t m = mode & SERIAL_CONF_MODE_MASK;
g_eeGeneral.serialPort =
(g_eeGeneral.serialPort & ~(0xF << port_nr * 4)) | (m << port_nr * 4);
(g_eeGeneral.serialPort &
~(SERIAL_CONF_MODE_MASK << port_nr * SERIAL_CONF_BITS_PER_PORT)) |
(m << port_nr * SERIAL_CONF_BITS_PER_PORT);
}

// uint8_t serialTracesEnabled(int port_nr)
Expand Down
Loading

0 comments on commit f5b1b53

Please sign in to comment.