Files
celrs/celrs/serial.c
T
portersky df3d399610 feat: adopt ELRS USB CRSF frame format, add skeleton modules
Switch CRC from CCITT (0x07) to DVB-S2 (0xD5) to match ELRS.
Adopt ELRS USB frame format: [addr][length][type][payload][crc].
Update frame type constants to match current ELRS protocol values.

New skeleton modules (stub implementations with TODO comments):
  - crsf_telemetry.h/.c: telemetry decoders (GPS, battery, link..)
  - crsf_stream.h/.c: incremental streaming frame reader
  - crsf_param.h/.c: parameter protocol (read/write/set_power)

Retained old frame format as *_legacy functions for backward
compatibility with existing telemetry tool and tests.

Add cel_serial_find_elrs_port() and cel_serial_open_probe()
stubs to serial module for port auto-detection and baud probing.
2026-06-14 20:47:56 +02:00

100 lines
3.0 KiB
C

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "celrs/serial.h"
#include "celrs/platform/serial_internal.h"
#include <stdlib.h>
#include <string.h>
struct cel_serial_port {
char path[256];
int baud_rate;
cel_serial_platform_handle handle;
};
cel_serial_port* cel_serial_open(char const* path, int baud_rate) {
if (path == NULL) return NULL;
cel_serial_platform_handle handle = cel_serial_platform_open(path, baud_rate);
if (handle == CEL_SERIAL_PLATFORM_INVALID_HANDLE) return NULL;
cel_serial_port* port = (cel_serial_port*)calloc(1, sizeof(cel_serial_port));
if (port == NULL) {
cel_serial_platform_close(handle);
return NULL;
}
strncpy(port->path, path, sizeof(port->path) - 1);
port->path[sizeof(port->path) - 1] = '\0';
port->baud_rate = baud_rate;
port->handle = handle;
return port;
}
void cel_serial_close(cel_serial_port* port) {
if (port == NULL) return;
cel_serial_platform_close(port->handle);
free(port);
}
size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len) {
if (port == NULL) return 0;
return cel_serial_platform_read(port->handle, buf, len);
}
size_t cel_serial_write(cel_serial_port* port, uint8_t const* buf, size_t len) {
if (port == NULL) return 0;
return cel_serial_platform_write(port->handle, buf, len);
}
void cel_serial_flush(cel_serial_port* port) {
if (port == NULL) return;
cel_serial_platform_flush(port->handle);
}
int cel_serial_list_ports(char*** out_ports, int max_ports) {
if (out_ports == NULL) return -1;
if (max_ports <= 0) max_ports = 64;
return cel_serial_platform_list_ports(out_ports, max_ports);
}
void cel_serial_free_ports(char** ports, int count) {
if (ports == NULL) return;
for (int i = 0; i < count; i++) {
free(ports[i]);
}
free(ports);
}
int cel_serial_find_elrs_port(char* out, size_t out_size) {
/* TODO: enumerate serial ports via cel_serial_list_ports().
* Match port description against keywords (case-insensitive):
* "silicon labs", "cp210", "elrs", "expresslrs", "bayck",
* "ch340", "ch343", "ftdi", "uart"
* Return first match. Copy device path into out.
* Return 0 on success, -1 if no match found. */
(void)out;
(void)out_size;
return -1;
}
cel_serial_port* cel_serial_open_probe(char const* path,
int const bauds[], int count,
int* out_baud) {
/* TODO: try opening the port at each baud rate in order.
* Default probe order: 921600, 400000, 420000.
* Set DTR=0, RTS=0 before opening to avoid module reset.
* On first successful open, store baud in *out_baud and return port.
* If all fail, return NULL.
* NOTE: current cel_serial_open() doesn't support DTR/RTS control.
* This needs a platform-level extension (see serial_internal.h). */
(void)path;
(void)bauds;
(void)count;
(void)out_baud;
return NULL;
}