Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions mLRS/Common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,24 @@

#if defined DEVICE_IS_TRANSMITTER && defined USE_COM_ON_SERIAL
// TODO: when we swap ser/com, we may want to flush, we need to change baudrate
#ifdef DEVICE_HAS_SERIAL_ON_USB
#if defined DEVICE_HAS_SERIAL_ON_USB || defined DEVICE_HAS_SERIAL_OR_COM_ON_USB
#define SERORCOM_INIT ser_or_com_init();
#else
#define SERORCOM_INIT ser_or_com_init(); if (!ser_or_com_serial()) uartb_setbaudrate(TX_COM_BAUDRATE);
#endif
#define IFNSER(x) if (!ser_or_com_serial()) return x;
#define IFNCOM(x) if (ser_or_com_serial()) return x;
#ifdef DEVICE_HAS_SERIAL_OR_COM_ON_USB
// settings-based selection: USB used for Serial when SerialDestination=USB
static bool _ser_or_com_force_com = false;
void ser_or_com_init(void) { _ser_or_com_force_com = false; }
bool ser_or_com_serial(void)
{
if (_ser_or_com_force_com) return false;
return (Setup.Tx[Config.ConfigId].SerialDestination == SERIAL_DESTINATION_USB);
}
void ser_or_com_set_to_com(void) { _ser_or_com_force_com = true; }
#endif
#else
#define SERORCOM_INIT
#define IFNSER(x)
Expand Down Expand Up @@ -67,6 +78,17 @@ class tSerialPort : public tSerialBase
char getc(void) override { IFNSER(0); return usb_getc(); }
void flush(void) override { IFNSER(); usb_flush(); }
uint16_t bytes_available(void) override { IFNSER(0); return usb_rx_bytesavailable(); }
#elif defined DEVICE_HAS_SERIAL_OR_COM_ON_USB // runtime switch: USB when SerialDestination=USB, else UARTB
void InitOnce(void) override { usb_init(); }
void Init(void) override { uartb_init(); SERORCOM_INIT; }
void SetBaudRate(uint32_t baud) override { if (!ser_or_com_serial()) uartb_setprotocol(baud, XUART_PARITY_NO, UART_STOPBIT_1); }
bool full(void) { return ser_or_com_serial() ? usb_tx_full() : !uartb_tx_notfull(); }
void putbuf(uint8_t* const buf, uint16_t len) override { if (ser_or_com_serial()) usb_putbuf(buf, len); else uartb_putbuf(buf, len); }
bool available(void) override { return ser_or_com_serial() ? usb_rx_available() : uartb_rx_available(); }
char getc(void) override { return ser_or_com_serial() ? usb_getc() : uartb_getc(); }
void flush(void) override { if (ser_or_com_serial()) usb_flush(); else { uartb_rx_flush(); uartb_tx_flush(); } }
uint16_t bytes_available(void) override { return ser_or_com_serial() ? usb_rx_bytesavailable() : uartb_rx_bytesavailable(); }
bool has_systemboot(void) override { return uartb_has_systemboot(); }
#else
void Init(void) override { uartb_init(); SERORCOM_INIT; }
void SetBaudRate(uint32_t baud) override { IFNSER(); uartb_setprotocol(baud, XUART_PARITY_NO, UART_STOPBIT_1); }
Expand Down Expand Up @@ -137,7 +159,7 @@ class tComPort : public tSerialBase
#ifdef USE_COM_ON_SERIAL
public:
// we do not initialize it as it is initialized by serial
#ifdef DEVICE_HAS_SERIAL_ON_USB // USE_USB
#if defined DEVICE_HAS_SERIAL_ON_USB || defined DEVICE_HAS_SERIAL_OR_COM_ON_USB // USE_USB
bool full(void) { IFNCOM(false); return usb_tx_full(); }
void putbuf(uint8_t* const buf, uint16_t len) override { IFNCOM(); usb_putbuf(buf, len); }
bool available(void) override { IFNCOM(0); return usb_rx_available(); }
Expand Down
5 changes: 3 additions & 2 deletions mLRS/Common/hal/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ In tx-hal files:
#define DEVICE_HAS_SERIAL_ON_USB // board has the Serial port on native USB
#define DEVICE_HAS_NO_COM // board has no Com port
#define DEVICE_HAS_COM_ON_USB // board has the Com port on native USB
#define DEVICE_HAS_SERIAL_OR_COM_ON_USB // board shares USB between Serial or Com, selected by SerialDestination setting
#define DEVICE_HAS_NO_DEBUG // board has no Debug port
#define DEVICE_HAS_DEBUG_SWUART // implement Debug as software UART
#define DEVICE_HAS_I2C_DISPLAY // board has a DISPLAY on I2C, and 5-way switch
Expand Down Expand Up @@ -278,10 +279,10 @@ Note: Some "high-level" features are set for each device in the device_conf.h fi
#endif // DEVICE_IS_RECEIVER

#ifdef DEVICE_IS_TRANSMITTER
#if defined DEVICE_HAS_SERIAL_OR_COM // some devices have device dependent ways to select serial or com
#if defined DEVICE_HAS_SERIAL_OR_COM || defined DEVICE_HAS_SERIAL_OR_COM_ON_USB // some devices have device dependent ways to select serial or com
#define USE_SERIAL
#define USE_COM_ON_SERIAL
#ifdef DEVICE_HAS_SERIAL_ON_USB
#if defined DEVICE_HAS_SERIAL_ON_USB || defined DEVICE_HAS_SERIAL_OR_COM_ON_USB
#define USE_USB
#endif
#if defined DEBUG_ENABLED && !defined DEVICE_HAS_NO_DEBUG
Expand Down
4 changes: 2 additions & 2 deletions mLRS/Common/hal/matek/tx-hal-matek-mtx-db30-g474ce.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
#define DEVICE_HAS_I2C_DISPLAY_ROT180
#define DEVICE_HAS_ESP_WIFI_BRIDGE_ON_SERIAL2
#define DEVICE_HAS_ESP_WIFI_BRIDGE_CONFIGURE
#define DEVICE_HAS_COM_ON_USB
#define DEVICE_HAS_SERIAL_OR_COM_ON_USB
#define DEVICE_HAS_NO_DEBUG
//#define DEVICE_HAS_NO_SERIAL

//change these defaults
#undef SETUP_TX_SERIAL_DESTINATION
#define SETUP_TX_SERIAL_DESTINATION 1 // 0: serial port, 1: serial2 (BT/ESP) port, 2: mBridge
#define SETUP_TX_SERIAL_DESTINATION 1 // 0: serial port, 1: serial2 (BT/ESP) port, 2: mBridge, 3: USB


//-- Timers, Timing, EEPROM, and such stuff
Expand Down
5 changes: 4 additions & 1 deletion mLRS/Common/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void setup_configure_metadata(void)
SetupMetaData.Tx_InMode_allowed_mask = 0; // not available, do not display
#endif

// Tx SerialDestination: "serial,serial2,mbridge"
// Tx SerialDestination: "serial,serial2,mbridge,USB"
SetupMetaData.Tx_SerialDestination_allowed_mask = 0; // not available, do not display
#ifdef USE_SERIAL
SetupMetaData.Tx_SerialDestination_allowed_mask |= 0b001; // add serial
Expand All @@ -182,6 +182,9 @@ void setup_configure_metadata(void)
#ifdef DEVICE_HAS_JRPIN5
SetupMetaData.Tx_SerialDestination_allowed_mask |= 0b100; // add mbridge
#endif
#ifdef DEVICE_HAS_SERIAL_OR_COM_ON_USB
SetupMetaData.Tx_SerialDestination_allowed_mask |= 0b1000; // add USB option
#endif

// Tx Buzzer: ""off,LP,rxLQ"
#ifdef DEVICE_HAS_BUZZER
Expand Down
2 changes: 1 addition & 1 deletion mLRS/Common/setup_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
X( Setup.Tx[0].ChannelsSource, LIST, "Tx Ch Source", "TX_CH_SOURCE", 0,0,0,"", "none,crsf,in,mbridge", SETUP_MSK_TX_CH_SOURCE )\
X( Setup.Tx[0].ChannelOrder, LIST, "Tx Ch Order", "TX_CH_ORDER", 0,0,0,"", SETUP_OPT_CH_ORDER, MSK_ALL )\
X( Setup.Tx[0].InMode, LIST, "Tx In Mode", "TX_IN_MODE", 0,0,0,"", "sbus,sbus inv", SETUP_MSK_TX_IN_MODE )\
X( Setup.Tx[0].SerialDestination, LIST, "Tx Ser Dest", "TX_SER_DEST", 0,0,0,"", "serial,serial2,mbridge", SETUP_MSK_TX_SER_DEST )\
X( Setup.Tx[0].SerialDestination, LIST, "Tx Ser Dest", "TX_SER_DEST", 0,0,0,"", "serial,serial2,mbridge,USB", SETUP_MSK_TX_SER_DEST )\
X( Setup.Tx[0].SerialBaudrate, LIST, "Tx Ser Baudrate", "TX_SER_BAUD", 0,0,0,"", SETUP_OPT_TX_SERIAL_BAUDRATE, MSK_ALL )\
X( Setup.Tx[0].SendRadioStatus, LIST, "Tx Snd RadioStat", "TX_SND_RADIOSTAT", 0,0,0,"", "off,1 Hz", MSK_ALL )\
X( Setup.Tx[0].MavlinkComponent, LIST, "Tx Mav Component", "TX_MAV_COMPONENT", 0,0,0,"", "off,enabled", MSK_ALL )\
Expand Down
1 change: 1 addition & 0 deletions mLRS/Common/setup_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ typedef enum {
SERIAL_DESTINATION_SERIAL = 0,
SERIAL_DESTINATION_SERIAL2,
SERIAL_DESTINATION_MBRIDGE,
SERIAL_DESTINATION_USB,
SERIAL_DESTINATION_NUM,
} TX_SERIAL_DESTINATION_ENUM;

Expand Down
8 changes: 7 additions & 1 deletion mLRS/CommonTx/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ bool except_str_from_bindphrase(char* const ext, char* const bind_phrase, uint8_
#define CLI_BUF_SIZE 128


#ifdef DEVICE_HAS_COM_ON_USB
#if defined(DEVICE_HAS_COM_ON_USB) || defined(DEVICE_HAS_SERIAL_OR_COM_ON_USB)
#if USB_TXBUFSIZE >= 2048
#define CLI_PRINT_CHUNKS_CNT_MAX 200
#else // we assume 512
Expand Down Expand Up @@ -368,6 +368,11 @@ void tTxCli::Init(tSerialBase* const _comport, uint16_t _frame_rate_ms)

state = CLI_STATE_NORMAL;

// USB is much faster than UART - allow more chunks per call
#if defined(DEVICE_HAS_COM_ON_USB) || defined(DEVICE_HAS_SERIAL_OR_COM_ON_USB)
print_chunks_max = 32;
#else
// UART: throttle based on frame rate to avoid TX buffer overflow
// 9 ms, 20 ms, 32 ms, 53 ms
if (_frame_rate_ms < 19) {
print_chunks_max = 1;
Expand All @@ -378,6 +383,7 @@ void tTxCli::Init(tSerialBase* const _comport, uint16_t _frame_rate_ms)
} else {
print_chunks_max = 8;
}
#endif
if (print_chunks_max > CLI_PRINT_CHUNKS_CNT_MAX) print_chunks_max = CLI_PRINT_CHUNKS_CNT_MAX;

print_index = 0;
Expand Down
8 changes: 4 additions & 4 deletions mLRS/CommonTx/esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include <ctype.h>


#if defined DEVICE_HAS_ESP_WIFI_BRIDGE_ON_SERIAL && defined USE_COM_ON_SERIAL
#if defined DEVICE_HAS_ESP_WIFI_BRIDGE_ON_SERIAL && defined USE_COM_ON_SERIAL && !defined DEVICE_HAS_SERIAL_OR_COM_ON_USB
#error ESP: ESP wireless bridge is on serial but board has serial/com !
#endif

Expand Down Expand Up @@ -308,7 +308,7 @@ void tTxEspWifiBridge::passthrough_do_flashing(void)
dtr_rts_last = dtr_rts;
#endif

#ifdef DEVICE_HAS_COM_ON_USB
#if defined DEVICE_HAS_COM_ON_USB || defined DEVICE_HAS_SERIAL_OR_COM_ON_USB
if (usb_baudrate() != baudrate) {
baudrate = usb_baudrate();
ser->SetBaudRate(baudrate);
Expand Down Expand Up @@ -390,7 +390,7 @@ void tTxEspWifiBridge::passthrough_do(void)

uint32_t baudrate = 115200; // Note: this is what is used for flashing, can be different to ESP_CONFIGURE setting
ser->SetBaudRate(baudrate);
#if defined DEVICE_HAS_ESP_WIFI_BRIDGE_ON_SERIAL2 && defined USE_COM_ON_SERIAL
#if defined USE_COM_ON_SERIAL && (defined DEVICE_HAS_ESP_WIFI_BRIDGE_ON_SERIAL || defined DEVICE_HAS_ESP_WIFI_BRIDGE_ON_SERIAL2 || defined DEVICE_HAS_SERIAL_OR_COM_ON_USB)
ser_or_com_set_to_com();
#endif
ser->flush();
Expand All @@ -401,7 +401,7 @@ void tTxEspWifiBridge::passthrough_do(void)
leds.TickPassthrough_ms();
}

#ifdef DEVICE_HAS_COM_ON_USB
#if defined DEVICE_HAS_COM_ON_USB || defined DEVICE_HAS_SERIAL_OR_COM_ON_USB
if (usb_baudrate() != baudrate) {
baudrate = usb_baudrate();
ser->SetBaudRate(baudrate);
Expand Down
7 changes: 5 additions & 2 deletions mLRS/CommonTx/hc04.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <ctype.h>


#if defined DEVICE_HAS_HC04_MODULE_ON_SERIAL && defined USE_COM_ON_SERIAL
#if defined DEVICE_HAS_HC04_MODULE_ON_SERIAL && defined USE_COM_ON_SERIAL && !defined DEVICE_HAS_SERIAL_OR_COM_ON_USB
#error HC04 bluetooth module is on serial but board has serial/com !
#endif

Expand Down Expand Up @@ -71,6 +71,9 @@ void tTxHc04Bridge::Init(tSerialBase* const _comport, tSerialBase* const _serial
ser = _serialport;
ser_baud = _serial_baudrate;

#ifdef DEVICE_HAS_SERIAL_OR_COM_ON_USB
if (ser_or_com_serial()) return; // serial routed to USB, HC04 is on UARTB so skip autoconfigure
#endif
run_autoconfigure();
}

Expand Down Expand Up @@ -128,7 +131,7 @@ void tTxHc04Bridge::passthrough_do(void)
{
ser->SetBaudRate(ser_baud);
ser->flush();
#if defined DEVICE_HAS_HC04_MODULE_ON_SERIAL2 && defined USE_COM_ON_SERIAL
#if defined USE_COM_ON_SERIAL && (defined DEVICE_HAS_HC04_MODULE_ON_SERIAL2 || defined DEVICE_HAS_SERIAL_OR_COM_ON_USB)
ser_or_com_set_to_com();
#endif

Expand Down
4 changes: 4 additions & 0 deletions mLRS/CommonTx/mavlink_interface_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ void tTxMavlink::Init(tSerialBase* const _serialport, tSerialBase* const _mbridg
ser = _mbridge;
ser2 = nullptr;
break;
case SERIAL_DESTINATION_USB:
ser = _serialport; // serial object routes to USB internally via ser_or_com_serial()
ser2 = nullptr;
break;
default:
while(1){} // must not happen
}
Expand Down
3 changes: 3 additions & 0 deletions mLRS/CommonTx/msp_interface_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ void tTxMsp::Init(tSerialBase* const _serialport, tSerialBase* const _serial2por
case SERIAL_DESTINATION_MBRIDGE:
ser = nullptr;
break;
case SERIAL_DESTINATION_USB:
ser = _serialport; // serial object routes to USB internally via ser_or_com_serial()
break;
default:
while(1){} // must not happen
}
Expand Down
3 changes: 3 additions & 0 deletions mLRS/CommonTx/sx_serial_interface_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ void tTxSxSerial::Init(tSerialBase* const _serialport, tSerialBase* const _mbrid
case SERIAL_DESTINATION_MBRIDGE:
ser = _mbridge;
break;
case SERIAL_DESTINATION_USB:
ser = _serialport; // serial object routes to USB internally via ser_or_com_serial()
break;
default:
while(1){} // must not happen
}
Expand Down
1 change: 1 addition & 0 deletions mLRS/modules/stm32-usb-device/usbd_cdc_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ static int8_t CDC_Receive(uint8_t* pbuf, uint32_t* length)
void _cdc_transmit(void)
{
USBD_CDC_HandleTypeDef* hcdc = (USBD_CDC_HandleTypeDef*)husbd_CDC.pClassData;
if (hcdc == NULL) return; // not yet enumerated by host
if (hcdc->TxState != 0) return;

uint8_t len = 0;
Expand Down