1
0
Fork 0
mirror of https://github.com/iNavFlight/inav.git synced 2025-07-20 06:45:14 +03:00

Merge pull request #10271 from MUSTARDTIGERFPV/serialpassthrough-modes

Allow serialpassthrough to set parity & stop bits
This commit is contained in:
Marcelo Bezerra 2024-08-01 21:35:48 +01:00 committed by GitHub
commit 3cc11ce397
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 109 additions and 3 deletions

View file

@ -107,7 +107,7 @@ While connected to the CLI, all Logical Switches are temporarily disabled (5.1.0
| `save` | Save and reboot | | `save` | Save and reboot |
| `sd_info` | Sdcard info | | `sd_info` | Sdcard info |
| `serial` | Configure serial ports. [Usage](Serial.md) | | `serial` | Configure serial ports. [Usage](Serial.md) |
| `serialpassthrough` | Passthrough serial data to port, with `<id> <baud> <mode>`, where `id` is the zero based port index, `baud` is a standard baud rate, and mode is `rx`, `tx`, or both (`rxtx`) | | `serialpassthrough` | Passthrough serial data to port, with `<id> <baud> <mode> <options>`, where `id` is the zero based port index, `baud` is a standard baud rate, mode is `rx`, `tx`, or both (`rxtx`), and options is a short string like `8N1` or `8E2` |
| `servo` | Configure servos | | `servo` | Configure servos |
| `set` | Change setting with name=value or blank or * for list | | `set` | Change setting with name=value or blank or * for list |
| `smix` | Custom servo mixer | | `smix` | Custom servo mixer |

View file

@ -86,6 +86,11 @@ void serialSetMode(serialPort_t *instance, portMode_t mode)
instance->vTable->setMode(instance, mode); instance->vTable->setMode(instance, mode);
} }
void serialSetOptions(serialPort_t *instance, portOptions_t options)
{
instance->vTable->setOptions(instance, options);
}
void serialWriteBufShim(void *instance, const uint8_t *data, int count) void serialWriteBufShim(void *instance, const uint8_t *data, int count)
{ {
serialWriteBuf((serialPort_t *)instance, data, count); serialWriteBuf((serialPort_t *)instance, data, count);

View file

@ -95,6 +95,8 @@ struct serialPortVTable {
void (*setMode)(serialPort_t *instance, portMode_t mode); void (*setMode)(serialPort_t *instance, portMode_t mode);
void (*setOptions)(serialPort_t *instance, portOptions_t options);
void (*writeBuf)(serialPort_t *instance, const void *data, int count); void (*writeBuf)(serialPort_t *instance, const void *data, int count);
bool (*isConnected)(const serialPort_t *instance); bool (*isConnected)(const serialPort_t *instance);
@ -113,6 +115,7 @@ void serialWriteBuf(serialPort_t *instance, const uint8_t *data, int count);
uint8_t serialRead(serialPort_t *instance); uint8_t serialRead(serialPort_t *instance);
void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate); void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate);
void serialSetMode(serialPort_t *instance, portMode_t mode); void serialSetMode(serialPort_t *instance, portMode_t mode);
void serialSetOptions(serialPort_t *instance, portOptions_t options);
bool isSerialTransmitBufferEmpty(const serialPort_t *instance); bool isSerialTransmitBufferEmpty(const serialPort_t *instance);
void serialPrint(serialPort_t *instance, const char *str); void serialPrint(serialPort_t *instance, const char *str);
uint32_t serialGetBaudRate(serialPort_t *instance); uint32_t serialGetBaudRate(serialPort_t *instance);

View file

@ -623,6 +623,11 @@ void softSerialSetMode(serialPort_t *instance, portMode_t mode)
instance->mode = mode; instance->mode = mode;
} }
void softSerialSetOptions(serialPort_t *instance, portOptions_t options)
{
instance->options = options;
}
bool isSoftSerialTransmitBufferEmpty(const serialPort_t *instance) bool isSoftSerialTransmitBufferEmpty(const serialPort_t *instance)
{ {
return instance->txBufferHead == instance->txBufferTail; return instance->txBufferHead == instance->txBufferTail;
@ -636,6 +641,7 @@ static const struct serialPortVTable softSerialVTable = {
.serialSetBaudRate = softSerialSetBaudRate, .serialSetBaudRate = softSerialSetBaudRate,
.isSerialTransmitBufferEmpty = isSoftSerialTransmitBufferEmpty, .isSerialTransmitBufferEmpty = isSoftSerialTransmitBufferEmpty,
.setMode = softSerialSetMode, .setMode = softSerialSetMode,
.setOptions = softSerialSetOptions,
.isConnected = NULL, .isConnected = NULL,
.writeBuf = NULL, .writeBuf = NULL,
.beginWrite = NULL, .beginWrite = NULL,

View file

@ -317,6 +317,12 @@ void tcpSetMode(serialPort_t *instance, portMode_t mode)
UNUSED(mode); UNUSED(mode);
} }
void tcpSetOptions(serialPort_t *instance, portOptions_t options)
{
UNUSED(instance);
UNUSED(options);
}
static const struct serialPortVTable tcpVTable[] = { static const struct serialPortVTable tcpVTable[] = {
{ {
.serialWrite = tcpWrite, .serialWrite = tcpWrite,
@ -326,6 +332,7 @@ static const struct serialPortVTable tcpVTable[] = {
.serialSetBaudRate = tcpSetBaudRate, .serialSetBaudRate = tcpSetBaudRate,
.isSerialTransmitBufferEmpty = isTcpTransmitBufferEmpty, .isSerialTransmitBufferEmpty = isTcpTransmitBufferEmpty,
.setMode = tcpSetMode, .setMode = tcpSetMode,
.setOptions = tcpSetOptions,
.isConnected = tcpIsConnected, .isConnected = tcpIsConnected,
.writeBuf = tcpWritBuf, .writeBuf = tcpWritBuf,
.beginWrite = NULL, .beginWrite = NULL,

View file

@ -175,6 +175,13 @@ void uartSetMode(serialPort_t *instance, portMode_t mode)
uartReconfigure(uartPort); uartReconfigure(uartPort);
} }
void uartSetOptions(serialPort_t *instance, portOptions_t options)
{
uartPort_t *uartPort = (uartPort_t *)instance;
uartPort->port.options = options;
uartReconfigure(uartPort);
}
uint32_t uartTotalRxBytesWaiting(const serialPort_t *instance) uint32_t uartTotalRxBytesWaiting(const serialPort_t *instance)
{ {
const uartPort_t *s = (const uartPort_t*)instance; const uartPort_t *s = (const uartPort_t*)instance;
@ -255,6 +262,7 @@ const struct serialPortVTable uartVTable[] = {
.serialSetBaudRate = uartSetBaudRate, .serialSetBaudRate = uartSetBaudRate,
.isSerialTransmitBufferEmpty = isUartTransmitBufferEmpty, .isSerialTransmitBufferEmpty = isUartTransmitBufferEmpty,
.setMode = uartSetMode, .setMode = uartSetMode,
.setOptions = uartSetOptions,
.isConnected = NULL, .isConnected = NULL,
.writeBuf = NULL, .writeBuf = NULL,
.beginWrite = NULL, .beginWrite = NULL,

View file

@ -185,6 +185,13 @@ void uartSetMode(serialPort_t *instance, portMode_t mode)
uartReconfigure(uartPort); uartReconfigure(uartPort);
} }
void uartSetOptions(serialPort_t *instance, portOptions_t options)
{
uartPort_t *uartPort = (uartPort_t *)instance;
uartPort->port.options = options;
uartReconfigure(uartPort);
}
uint32_t uartTotalRxBytesWaiting(const serialPort_t *instance) uint32_t uartTotalRxBytesWaiting(const serialPort_t *instance)
{ {
uartPort_t *s = (uartPort_t*)instance; uartPort_t *s = (uartPort_t*)instance;
@ -266,6 +273,7 @@ const struct serialPortVTable uartVTable[] = {
.serialSetBaudRate = uartSetBaudRate, .serialSetBaudRate = uartSetBaudRate,
.isSerialTransmitBufferEmpty = isUartTransmitBufferEmpty, .isSerialTransmitBufferEmpty = isUartTransmitBufferEmpty,
.setMode = uartSetMode, .setMode = uartSetMode,
.setOptions = uartSetOptions,
.isConnected = NULL, .isConnected = NULL,
.writeBuf = NULL, .writeBuf = NULL,
.beginWrite = NULL, .beginWrite = NULL,

View file

@ -178,6 +178,13 @@ void uartSetMode(serialPort_t *instance, portMode_t mode)
uartReconfigure(uartPort); uartReconfigure(uartPort);
} }
void uartSetOptions(serialPort_t *instance, portOptions_t options)
{
uartPort_t *uartPort = (uartPort_t *)instance;
uartPort->port.options = options;
uartReconfigure(uartPort);
}
uint32_t uartTotalRxBytesWaiting(const serialPort_t *instance) uint32_t uartTotalRxBytesWaiting(const serialPort_t *instance)
{ {
const uartPort_t *s = (const uartPort_t*)instance; const uartPort_t *s = (const uartPort_t*)instance;
@ -260,6 +267,7 @@ const struct serialPortVTable uartVTable[] = {
.serialSetBaudRate = uartSetBaudRate, .serialSetBaudRate = uartSetBaudRate,
.isSerialTransmitBufferEmpty = isUartTransmitBufferEmpty, .isSerialTransmitBufferEmpty = isUartTransmitBufferEmpty,
.setMode = uartSetMode, .setMode = uartSetMode,
.setOptions = uartSetOptions,
.isConnected = NULL, .isConnected = NULL,
.writeBuf = NULL, .writeBuf = NULL,
.beginWrite = NULL, .beginWrite = NULL,

View file

@ -67,6 +67,14 @@ static void usbVcpSetMode(serialPort_t *instance, portMode_t mode)
// TODO implement // TODO implement
} }
static void usbVcpSetOptions(serialPort_t *instance, portOptions_t options)
{
UNUSED(instance);
UNUSED(options);
// TODO implement
}
static bool isUsbVcpTransmitBufferEmpty(const serialPort_t *instance) static bool isUsbVcpTransmitBufferEmpty(const serialPort_t *instance)
{ {
UNUSED(instance); UNUSED(instance);
@ -184,6 +192,7 @@ static const struct serialPortVTable usbVTable[] = {
.serialSetBaudRate = usbVcpSetBaudRate, .serialSetBaudRate = usbVcpSetBaudRate,
.isSerialTransmitBufferEmpty = isUsbVcpTransmitBufferEmpty, .isSerialTransmitBufferEmpty = isUsbVcpTransmitBufferEmpty,
.setMode = usbVcpSetMode, .setMode = usbVcpSetMode,
.setOptions = usbVcpSetOptions,
.isConnected = usbVcpIsConnected, .isConnected = usbVcpIsConnected,
.writeBuf = usbVcpWriteBuf, .writeBuf = usbVcpWriteBuf,
.beginWrite = usbVcpBeginWrite, .beginWrite = usbVcpBeginWrite,

View file

@ -308,6 +308,12 @@ static void usbVcpSetMode(serialPort_t *instance, portMode_t mode)
UNUSED(mode); UNUSED(mode);
} }
static void usbVcpSetOptions(serialPort_t *instance, portOptions_t options)
{
UNUSED(instance);
UNUSED(options);
}
static bool isUsbVcpTransmitBufferEmpty(const serialPort_t *instance) static bool isUsbVcpTransmitBufferEmpty(const serialPort_t *instance)
{ {
UNUSED(instance); UNUSED(instance);
@ -434,6 +440,7 @@ static const struct serialPortVTable usbVTable[] = {
.serialSetBaudRate = usbVcpSetBaudRate, .serialSetBaudRate = usbVcpSetBaudRate,
.isSerialTransmitBufferEmpty = isUsbVcpTransmitBufferEmpty, .isSerialTransmitBufferEmpty = isUsbVcpTransmitBufferEmpty,
.setMode = usbVcpSetMode, .setMode = usbVcpSetMode,
.setOptions = usbVcpSetOptions,
.isConnected = usbVcpIsConnected, .isConnected = usbVcpIsConnected,
.writeBuf = usbVcpWriteBuf, .writeBuf = usbVcpWriteBuf,
.beginWrite = usbVcpBeginWrite, .beginWrite = usbVcpBeginWrite,

View file

@ -914,6 +914,42 @@ static void cliSerial(char *cmdline)
} }
#ifdef USE_SERIAL_PASSTHROUGH #ifdef USE_SERIAL_PASSTHROUGH
portOptions_t constructPortOptions(char *options) {
if (strlen(options) != 3 || options[0] != '8') {
// Invalid format
return -1;
}
portOptions_t result = 0;
switch (options[1]) {
case 'N':
result |= SERIAL_PARITY_NO;
break;
case 'E':
result |= SERIAL_PARITY_EVEN;
break;
default:
// Invalid format
return -1;
}
switch (options[2]) {
case '1':
result |= SERIAL_STOPBITS_1;
break;
case '2':
result |= SERIAL_STOPBITS_2;
break;
default:
// Invalid format
return -1;
}
return result;
}
static void cliSerialPassthrough(char *cmdline) static void cliSerialPassthrough(char *cmdline)
{ {
char * saveptr; char * saveptr;
@ -926,6 +962,7 @@ static void cliSerialPassthrough(char *cmdline)
int id = -1; int id = -1;
uint32_t baud = 0; uint32_t baud = 0;
unsigned mode = 0; unsigned mode = 0;
portOptions_t options = SERIAL_NOT_INVERTED;
char* tok = strtok_r(cmdline, " ", &saveptr); char* tok = strtok_r(cmdline, " ", &saveptr);
int index = 0; int index = 0;
@ -943,6 +980,9 @@ static void cliSerialPassthrough(char *cmdline)
if (strstr(tok, "tx") || strstr(tok, "TX")) if (strstr(tok, "tx") || strstr(tok, "TX"))
mode |= MODE_TX; mode |= MODE_TX;
break; break;
case 3:
options |= constructPortOptions(tok);
break;
} }
index++; index++;
tok = strtok_r(NULL, " ", &saveptr); tok = strtok_r(NULL, " ", &saveptr);
@ -960,7 +1000,7 @@ static void cliSerialPassthrough(char *cmdline)
passThroughPort = openSerialPort(id, FUNCTION_NONE, NULL, NULL, passThroughPort = openSerialPort(id, FUNCTION_NONE, NULL, NULL,
baud, mode, baud, mode,
SERIAL_NOT_INVERTED); options);
if (!passThroughPort) { if (!passThroughPort) {
tfp_printf("Port %d could not be opened.\r\n", id); tfp_printf("Port %d could not be opened.\r\n", id);
return; return;
@ -976,6 +1016,11 @@ static void cliSerialPassthrough(char *cmdline)
passThroughPort->mode, mode); passThroughPort->mode, mode);
serialSetMode(passThroughPort, mode); serialSetMode(passThroughPort, mode);
} }
if (options && passThroughPort->options != options) {
tfp_printf("Adjusting options from %d to %d.\r\n",
passThroughPort->options, options);
serialSetOptions(passThroughPort, options);
}
// If this port has a rx callback associated we need to remove it now. // If this port has a rx callback associated we need to remove it now.
// Otherwise no data will be pushed in the serial port buffer! // Otherwise no data will be pushed in the serial port buffer!
if (passThroughPort->rxCallback) { if (passThroughPort->rxCallback) {
@ -4515,7 +4560,7 @@ const clicmd_t cmdTable[] = {
CLI_COMMAND_DEF("save", "save and reboot", NULL, cliSave), CLI_COMMAND_DEF("save", "save and reboot", NULL, cliSave),
CLI_COMMAND_DEF("serial", "configure serial ports", NULL, cliSerial), CLI_COMMAND_DEF("serial", "configure serial ports", NULL, cliSerial),
#ifdef USE_SERIAL_PASSTHROUGH #ifdef USE_SERIAL_PASSTHROUGH
CLI_COMMAND_DEF("serialpassthrough", "passthrough serial data to port", "<id> [baud] [mode] : passthrough to serial", cliSerialPassthrough), CLI_COMMAND_DEF("serialpassthrough", "passthrough serial data to port", "<id> [baud] [mode] [options]: passthrough to serial", cliSerialPassthrough),
#endif #endif
CLI_COMMAND_DEF("servo", "configure servos", NULL, cliServo), CLI_COMMAND_DEF("servo", "configure servos", NULL, cliServo),
#ifdef USE_PROGRAMMING_FRAMEWORK #ifdef USE_PROGRAMMING_FRAMEWORK