define SBUS in config

This commit is contained in:
2026-02-14 21:58:39 +01:00
parent 4ccfc5128b
commit 40d2880fdc
6 changed files with 259 additions and 183 deletions

View File

@ -1,25 +1,22 @@
/**
* @file rcsignal.c
* @brief RC PWM signal reading implementation using edge capture
* @brief RC PWM/SBUS signal reading implementation with runtime mode selection
*/
#include "rcsignal.h"
#include "config.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "esp_timer.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#ifdef USE_SBUS_MODE
#include "driver/uart.h"
#endif
#include <string.h>
static const char *TAG = "RCSIGNAL";
#ifdef USE_SBUS_MODE
// SBUS protocol constants
#define SBUS_FRAME_SIZE 25
#define SBUS_HEADER 0x0F
@ -29,59 +26,59 @@ static const char *TAG = "RCSIGNAL";
#define SBUS_CH_MIN 172
#define SBUS_CH_CENTER 992
#define SBUS_CH_MAX 1811
#define SBUS_THRESHOLD_LOW 800
#define SBUS_THRESHOLD_HIGH 1100
#define UART_NUM UART_NUM_1
#define UART_BUF_SIZE 256
#else
// PWM mode constants
#define PULSE_THRESHOLD_US 1500
#define SIGNAL_TIMEOUT_MS 100
#endif
static struct
{
int8_t gpio_pin;
#ifdef USE_SBUS_MODE
bool use_sbus_mode; // Runtime mode selection
uint8_t sbus_trigger_channel; // SBUS trigger channel
uint16_t sbus_threshold_low; // SBUS low threshold
uint16_t sbus_threshold_high; // SBUS high threshold
// SBUS state
volatile uint16_t channels[SBUS_NUM_CHANNELS];
volatile int64_t last_frame_time;
volatile bool signal_active;
volatile bool pull_detected;
uint8_t rx_buffer[SBUS_FRAME_SIZE];
#else
// PWM state
volatile uint32_t pulse_width_us;
volatile int64_t last_edge_time;
volatile int64_t pulse_start_time;
volatile bool last_level;
// Common state
volatile bool signal_active;
volatile bool pull_detected;
#endif
uint8_t current_mode;
rcsignal_mode_change_callback_t callback;
bool initialized;
TaskHandle_t monitor_task;
} rcsignal = {
.gpio_pin = -1,
#ifdef USE_SBUS_MODE
.use_sbus_mode = false,
.sbus_trigger_channel = 3,
.sbus_threshold_low = 800,
.sbus_threshold_high = 1100,
.channels = {0},
.last_frame_time = 0,
.signal_active = false,
.pull_detected = false,
.rx_buffer = {0},
#else
.pulse_width_us = 0,
.last_edge_time = 0,
.pulse_start_time = 0,
.last_level = false,
.signal_active = false,
.pull_detected = false,
#endif
.current_mode = 0,
.callback = NULL,
.initialized = false,
.monitor_task = NULL,
};
#ifndef USE_SBUS_MODE
// PWM Mode: GPIO ISR handler
static void IRAM_ATTR gpio_isr_handler(void *arg)
{
@ -106,7 +103,7 @@ static void IRAM_ATTR gpio_isr_handler(void *arg)
rcsignal.last_level = level;
}
#else
// SBUS Mode: Parse SBUS frame
static bool parse_sbus_frame(const uint8_t *frame, uint16_t *channels)
{
@ -136,43 +133,95 @@ static bool parse_sbus_frame(const uint8_t *frame, uint16_t *channels)
return true;
}
#endif
static void monitor_task(void *arg)
{
#ifdef USE_SBUS_MODE
while (1)
if (rcsignal.use_sbus_mode)
{
// Read SBUS data from UART
int len = uart_read_bytes(UART_NUM, rcsignal.rx_buffer, SBUS_FRAME_SIZE, pdMS_TO_TICKS(20));
if (len == SBUS_FRAME_SIZE)
// SBUS mode
while (1)
{
uint16_t temp_channels[SBUS_NUM_CHANNELS];
// Read SBUS data from UART
int len = uart_read_bytes(UART_NUM, rcsignal.rx_buffer, SBUS_FRAME_SIZE, pdMS_TO_TICKS(20));
if (parse_sbus_frame(rcsignal.rx_buffer, temp_channels))
if (len == SBUS_FRAME_SIZE)
{
// Copy parsed channels
for (int i = 0; i < SBUS_NUM_CHANNELS; i++)
uint16_t temp_channels[SBUS_NUM_CHANNELS];
if (parse_sbus_frame(rcsignal.rx_buffer, temp_channels))
{
rcsignal.channels[i] = temp_channels[i];
// Copy parsed channels
for (int i = 0; i < SBUS_NUM_CHANNELS; i++)
{
rcsignal.channels[i] = temp_channels[i];
}
rcsignal.last_frame_time = esp_timer_get_time();
rcsignal.signal_active = true;
// Check trigger channel for mode change
uint16_t ch_value = rcsignal.channels[rcsignal.sbus_trigger_channel];
// Detect pull low
if (ch_value < rcsignal.sbus_threshold_low)
{
rcsignal.pull_detected = true;
}
// Detect rising edge (pull high after low)
if (ch_value > rcsignal.sbus_threshold_high && rcsignal.pull_detected)
{
rcsignal.pull_detected = false;
if (rcsignal.callback)
{
rcsignal.callback();
}
}
}
}
rcsignal.last_frame_time = esp_timer_get_time();
rcsignal.signal_active = true;
// Check for signal timeout
int64_t now = esp_timer_get_time();
if (rcsignal.signal_active && (now - rcsignal.last_frame_time) > (SIGNAL_TIMEOUT_MS * 1000))
{
rcsignal.signal_active = false;
memset((void *)rcsignal.channels, 0, sizeof(rcsignal.channels));
}
// Check channel 4 for mode trigger
uint16_t ch4_value = rcsignal.channels[SBUS_TRIGGER_CHANNEL];
vTaskDelay(pdMS_TO_TICKS(5));
}
}
else
{
// PWM mode
uint32_t last_pulse_width = 0;
// Detect pull low
if (ch4_value < SBUS_THRESHOLD_LOW)
while (1)
{
vTaskDelay(pdMS_TO_TICKS(10));
// Check for signal timeout
int64_t now = esp_timer_get_time();
if (rcsignal.signal_active && (now - rcsignal.last_edge_time) > (SIGNAL_TIMEOUT_MS * 1000))
{
rcsignal.signal_active = false;
rcsignal.pulse_width_us = 0;
}
// Detect mode change (rising edge on PWM signal > 1500us)
if (rcsignal.pulse_width_us != last_pulse_width)
{
last_pulse_width = rcsignal.pulse_width_us;
if (rcsignal.pulse_width_us < PULSE_THRESHOLD_US)
{
rcsignal.pull_detected = true;
}
// Detect rising edge (pull high after low)
if (ch4_value > SBUS_THRESHOLD_HIGH && rcsignal.pull_detected)
if (rcsignal.pulse_width_us > PULSE_THRESHOLD_US && rcsignal.pull_detected)
{
// Mode change detected
rcsignal.pull_detected = false;
if (rcsignal.callback)
@ -182,116 +231,79 @@ static void monitor_task(void *arg)
}
}
}
// Check for signal timeout
int64_t now = esp_timer_get_time();
if (rcsignal.signal_active && (now - rcsignal.last_frame_time) > (100 * 1000))
{
rcsignal.signal_active = false;
memset((void *)rcsignal.channels, 0, sizeof(rcsignal.channels));
}
vTaskDelay(pdMS_TO_TICKS(5));
}
#else
// PWM mode
uint32_t last_pulse_width = 0;
while (1)
{
vTaskDelay(pdMS_TO_TICKS(10));
// Check for signal timeout
int64_t now = esp_timer_get_time();
if (rcsignal.signal_active && (now - rcsignal.last_edge_time) > (SIGNAL_TIMEOUT_MS * 1000))
{
rcsignal.signal_active = false;
rcsignal.pulse_width_us = 0;
}
// Detect mode change (rising edge on PWM signal > 1500us)
if (rcsignal.pulse_width_us != last_pulse_width)
{
last_pulse_width = rcsignal.pulse_width_us;
if (rcsignal.pulse_width_us < PULSE_THRESHOLD_US)
{
rcsignal.pull_detected = true;
}
if (rcsignal.pulse_width_us > PULSE_THRESHOLD_US && rcsignal.pull_detected)
{
// Mode change detected
rcsignal.pull_detected = false;
if (rcsignal.callback)
{
rcsignal.callback();
}
}
}
}
#endif
}
esp_err_t rcsignal_init(int8_t pin)
esp_err_t rcsignal_init(const config_t *config)
{
if (pin < 0)
if (!config || config->rc_signal_pin < 0)
{
ESP_LOGI(TAG, "RC signal disabled (no pin configured)");
return ESP_OK;
}
rcsignal.gpio_pin = pin;
// Store configuration
rcsignal.gpio_pin = config->rc_signal_pin;
rcsignal.use_sbus_mode = config->use_sbus_mode;
rcsignal.sbus_trigger_channel = config->sbus_trigger_channel;
rcsignal.sbus_threshold_low = config->sbus_threshold_low;
rcsignal.sbus_threshold_high = config->sbus_threshold_high;
#ifdef USE_SBUS_MODE
// SBUS Mode: Configure UART with inverted RX
ESP_LOGI(TAG, "Initializing SBUS mode on GPIO%d", pin);
if (rcsignal.use_sbus_mode)
{
// SBUS Mode: Configure UART with inverted RX
ESP_LOGI(TAG, "Initializing SBUS mode on GPIO%d", rcsignal.gpio_pin);
ESP_LOGI(TAG, " Trigger channel: CH%d", rcsignal.sbus_trigger_channel + 1);
ESP_LOGI(TAG, " Thresholds: %d / %d", rcsignal.sbus_threshold_low, rcsignal.sbus_threshold_high);
uart_config_t uart_config = {
.baud_rate = SBUS_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_EVEN,
.stop_bits = UART_STOP_BITS_2,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
uart_config_t uart_config = {
.baud_rate = SBUS_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_EVEN,
.stop_bits = UART_STOP_BITS_2,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM, UART_PIN_NO_CHANGE, pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(UART_NUM, UART_BUF_SIZE * 2, 0, 0, NULL, 0));
ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM, UART_PIN_NO_CHANGE, rcsignal.gpio_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(UART_NUM, UART_BUF_SIZE * 2, 0, 0, NULL, 0));
// Set inverted RX for FrSky receivers (they output inverted SBUS)
ESP_ERROR_CHECK(uart_set_line_inverse(UART_NUM, UART_SIGNAL_RXD_INV));
// Set inverted RX for FrSky receivers (they output inverted SBUS)
ESP_ERROR_CHECK(uart_set_line_inverse(UART_NUM, UART_SIGNAL_RXD_INV));
ESP_LOGI(TAG, "SBUS UART configured with inverted RX");
#else
// PWM Mode: Configure GPIO with interrupts
ESP_LOGI(TAG, "Initializing PWM mode on GPIO%d", pin);
ESP_LOGI(TAG, "SBUS UART configured with inverted RX");
}
else
{
// PWM Mode: Configure GPIO with interrupts
ESP_LOGI(TAG, "Initializing PWM mode on GPIO%d", rcsignal.gpio_pin);
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << pin),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_ANYEDGE,
};
ESP_ERROR_CHECK(gpio_config(&io_conf));
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << rcsignal.gpio_pin),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_ANYEDGE,
};
ESP_ERROR_CHECK(gpio_config(&io_conf));
// Install ISR service
ESP_ERROR_CHECK(gpio_isr_handler_add(pin, gpio_isr_handler, NULL));
#endif
// Add ISR handler (ISR service must be installed by caller)
ESP_ERROR_CHECK(gpio_isr_handler_add(rcsignal.gpio_pin, gpio_isr_handler, NULL));
}
// Create monitor task
BaseType_t ret = xTaskCreate(monitor_task, "rcsignal_monitor", 2048, NULL, 5, &rcsignal.monitor_task);
if (ret != pdPASS)
{
#ifdef USE_SBUS_MODE
uart_driver_delete(UART_NUM);
#else
gpio_isr_handler_remove(pin);
gpio_uninstall_isr_service();
#endif
if (rcsignal.use_sbus_mode)
{
uart_driver_delete(UART_NUM);
}
else
{
gpio_isr_handler_remove(rcsignal.gpio_pin);
}
return ESP_FAIL;
}
@ -312,14 +324,17 @@ void rcsignal_deinit(void)
rcsignal.monitor_task = NULL;
}
#ifdef USE_SBUS_MODE
uart_driver_delete(UART_NUM);
#else
if (rcsignal.gpio_pin >= 0)
if (rcsignal.use_sbus_mode)
{
gpio_isr_handler_remove(rcsignal.gpio_pin);
uart_driver_delete(UART_NUM);
}
else
{
if (rcsignal.gpio_pin >= 0)
{
gpio_isr_handler_remove(rcsignal.gpio_pin);
}
}
#endif
rcsignal.initialized = false;
}
@ -331,18 +346,21 @@ void rcsignal_register_callback(rcsignal_mode_change_callback_t callback)
uint32_t rcsignal_get_pulse_width(void)
{
#ifdef USE_SBUS_MODE
// In SBUS mode, return channel 4 value mapped to microseconds
// SBUS: 172-1811 -> PWM: ~1000-2000us
if (rcsignal.signal_active)
if (rcsignal.use_sbus_mode)
{
uint16_t ch_val = rcsignal.channels[SBUS_TRIGGER_CHANNEL];
return 1000 + ((ch_val - SBUS_CH_MIN) * 1000) / (SBUS_CH_MAX - SBUS_CH_MIN);
// In SBUS mode, return trigger channel value mapped to microseconds
// SBUS: 172-1811 -> PWM: ~1000-2000us
if (rcsignal.signal_active)
{
uint16_t ch_val = rcsignal.channels[rcsignal.sbus_trigger_channel];
return 1000 + ((ch_val - SBUS_CH_MIN) * 1000) / (SBUS_CH_MAX - SBUS_CH_MIN);
}
return 0;
}
else
{
return rcsignal.pulse_width_us;
}
return 0;
#else
return rcsignal.pulse_width_us;
#endif
}
bool rcsignal_is_active(void)
@ -355,10 +373,9 @@ uint8_t rcsignal_get_current_mode(void)
return rcsignal.current_mode;
}
#ifdef USE_SBUS_MODE
uint16_t rcsignal_get_sbus_channel(uint8_t channel)
{
if (channel >= SBUS_NUM_CHANNELS)
if (!rcsignal.use_sbus_mode || channel >= SBUS_NUM_CHANNELS)
{
return 0;
}
@ -367,6 +384,12 @@ uint16_t rcsignal_get_sbus_channel(uint8_t channel)
void rcsignal_debug_print_channels(void)
{
if (!rcsignal.use_sbus_mode)
{
ESP_LOGW(TAG, "Not in SBUS mode");
return;
}
if (!rcsignal.signal_active)
{
ESP_LOGW(TAG, "No SBUS signal active");
@ -388,7 +411,6 @@ void rcsignal_debug_print_channels(void)
rcsignal.channels[14], rcsignal.channels[15]);
// Highlight the trigger channel
ESP_LOGI(TAG, "Trigger channel (CH%d): %d", SBUS_TRIGGER_CHANNEL + 1,
rcsignal.channels[SBUS_TRIGGER_CHANNEL]);
ESP_LOGI(TAG, "Trigger channel (CH%d): %d", rcsignal.sbus_trigger_channel + 1,
rcsignal.channels[rcsignal.sbus_trigger_channel]);
}
#endif