define SBUS in config
This commit is contained in:
29
README.md
29
README.md
@ -93,11 +93,21 @@ GND -> Common Ground
|
|||||||
- Add 100-500µF capacitor near strips
|
- Add 100-500µF capacitor near strips
|
||||||
- Add 330Ω resistor on data line
|
- Add 330Ω resistor on data line
|
||||||
|
|
||||||
### PWM Signal
|
|
||||||
|
### PWM Signal Mode
|
||||||
- Standard RC PWM: 1000-2000µs pulse width
|
- Standard RC PWM: 1000-2000µs pulse width
|
||||||
- 1500µs threshold for mode switching
|
- 1500µs threshold for mode switching
|
||||||
- Rising edge >1500µs after <1500µs triggers next mode
|
- Rising edge >1500µs after <1500µs triggers next mode
|
||||||
|
|
||||||
|
|
||||||
|
### SBUS Mode (FrSky X4R-SB)
|
||||||
|
```
|
||||||
|
X4R-SB SBUS/CH4 → ESP32 UART1 RX Pin (configured GPIO)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note**: The FrSky X4R-SB outputs inverted SBUS. The module automatically handles this inversion.
|
||||||
|
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
### Adding New Animations
|
### Adding New Animations
|
||||||
@ -118,6 +128,23 @@ idf.py monitor
|
|||||||
# Exit monitor: Ctrl+]
|
# Exit monitor: Ctrl+]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Debug Output (SBUS Mode Only)
|
||||||
|
|
||||||
|
```c
|
||||||
|
// Print all 16 channels to console
|
||||||
|
rcsignal_debug_print_channels();
|
||||||
|
```
|
||||||
|
|
||||||
|
Example output:
|
||||||
|
```
|
||||||
|
I (12345) RCSIGNAL: SBUS Channels:
|
||||||
|
I (12345) RCSIGNAL: CH1: 992 CH2: 992 CH3: 172 CH4: 1811
|
||||||
|
I (12345) RCSIGNAL: CH5: 992 CH6: 992 CH7: 992 CH8: 992
|
||||||
|
I (12345) RCSIGNAL: CH9: 992 CH10: 992 CH11: 992 CH12: 992
|
||||||
|
I (12345) RCSIGNAL: CH13: 992 CH14: 992 CH15: 992 CH16: 992
|
||||||
|
I (12345) RCSIGNAL: Trigger channel (CH4): 1811
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
See [LICENSE](LICENSE)
|
See [LICENSE](LICENSE)
|
||||||
|
|||||||
@ -26,7 +26,11 @@ static const char *TAG = "CONFIG";
|
|||||||
#define HARDCODED_CONFIG_LED_STRIP_B_PIN 3U
|
#define HARDCODED_CONFIG_LED_STRIP_B_PIN 3U
|
||||||
#define HARDCODED_CONFIG_LED_STRIP_A_COUNT 9U
|
#define HARDCODED_CONFIG_LED_STRIP_A_COUNT 9U
|
||||||
#define HARDCODED_CONFIG_LED_STRIP_B_COUNT 9U
|
#define HARDCODED_CONFIG_LED_STRIP_B_COUNT 9U
|
||||||
#define HARDCODED_CONFIG_PWM_PIN 1U
|
#define HARDCODED_CONFIG_RC_SIGNAL_PIN 1U
|
||||||
|
#define HARDCODED_CONFIG_USE_SBUS_MODE true
|
||||||
|
#define HARDCODED_CONFIG_SBUS_TRIGGER_CHANNEL 3U // Channel 4
|
||||||
|
#define HARDCODED_CONFIG_SBUS_THRESHOLD_LOW 800U
|
||||||
|
#define HARDCODED_CONFIG_SBUS_THRESHOLD_HIGH 1100U
|
||||||
|
|
||||||
#if defined(CONFIG_IDF_TARGET_ESP32C3)
|
#if defined(CONFIG_IDF_TARGET_ESP32C3)
|
||||||
#define HARDCODED_CONFIG_LOCALBTN_PIN 9
|
#define HARDCODED_CONFIG_LOCALBTN_PIN 9
|
||||||
@ -44,8 +48,13 @@ static config_t current_config = {
|
|||||||
.led_pin_strip_b = -1,
|
.led_pin_strip_b = -1,
|
||||||
.led_count_strip_a = -1,
|
.led_count_strip_a = -1,
|
||||||
.led_count_strip_b = -1,
|
.led_count_strip_b = -1,
|
||||||
.pwm_pin = -1,
|
.rc_signal_pin = -1,
|
||||||
.localBtn_pin = -1};
|
.localBtn_pin = -1,
|
||||||
|
.use_sbus_mode = false,
|
||||||
|
.sbus_trigger_channel = 3,
|
||||||
|
.sbus_threshold_low = 800,
|
||||||
|
.sbus_threshold_high = 1100,
|
||||||
|
};
|
||||||
|
|
||||||
static void calculate_config_hash(const config_t *cfg, uint8_t *out_hash);
|
static void calculate_config_hash(const config_t *cfg, uint8_t *out_hash);
|
||||||
|
|
||||||
@ -88,7 +97,13 @@ static esp_err_t load_config_from_nvs(void)
|
|||||||
ESP_LOGI(TAG, " Strip B: GPIO%d", current_config.led_pin_strip_b);
|
ESP_LOGI(TAG, " Strip B: GPIO%d", current_config.led_pin_strip_b);
|
||||||
ESP_LOGI(TAG, " Strip A LED count: %d", current_config.led_count_strip_a);
|
ESP_LOGI(TAG, " Strip A LED count: %d", current_config.led_count_strip_a);
|
||||||
ESP_LOGI(TAG, " Strip B LED count: %d", current_config.led_count_strip_b);
|
ESP_LOGI(TAG, " Strip B LED count: %d", current_config.led_count_strip_b);
|
||||||
ESP_LOGI(TAG, " PWM Pin: GPIO%d", current_config.pwm_pin);
|
ESP_LOGI(TAG, " RC Signal Pin: GPIO%d", current_config.rc_signal_pin);
|
||||||
|
ESP_LOGI(TAG, " RC Signal Mode: %s", current_config.use_sbus_mode ? "SBUS" : "PWM");
|
||||||
|
if (current_config.use_sbus_mode)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, " SBUS Trigger Channel: %d", current_config.sbus_trigger_channel + 1);
|
||||||
|
ESP_LOGI(TAG, " SBUS Thresholds: %d / %d", current_config.sbus_threshold_low, current_config.sbus_threshold_high);
|
||||||
|
}
|
||||||
ESP_LOGI(TAG, " Local btn Pin: GPIO%d", current_config.localBtn_pin);
|
ESP_LOGI(TAG, " Local btn Pin: GPIO%d", current_config.localBtn_pin);
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@ -131,8 +146,12 @@ esp_err_t config_reset_config(void)
|
|||||||
current_config.led_pin_strip_b = -1;
|
current_config.led_pin_strip_b = -1;
|
||||||
current_config.led_count_strip_a = -1;
|
current_config.led_count_strip_a = -1;
|
||||||
current_config.led_count_strip_b = -1;
|
current_config.led_count_strip_b = -1;
|
||||||
current_config.pwm_pin = -1;
|
current_config.rc_signal_pin = -1;
|
||||||
current_config.localBtn_pin = -1;
|
current_config.localBtn_pin = -1;
|
||||||
|
current_config.use_sbus_mode = false;
|
||||||
|
current_config.sbus_trigger_channel = 3;
|
||||||
|
current_config.sbus_threshold_low = 800;
|
||||||
|
current_config.sbus_threshold_high = 1100;
|
||||||
|
|
||||||
return save_config_to_nvs();
|
return save_config_to_nvs();
|
||||||
}
|
}
|
||||||
@ -143,8 +162,12 @@ void config_get_config(config_t *const cnf)
|
|||||||
cnf->led_pin_strip_b = current_config.led_pin_strip_b;
|
cnf->led_pin_strip_b = current_config.led_pin_strip_b;
|
||||||
cnf->led_count_strip_a = current_config.led_count_strip_a;
|
cnf->led_count_strip_a = current_config.led_count_strip_a;
|
||||||
cnf->led_count_strip_b = current_config.led_count_strip_b;
|
cnf->led_count_strip_b = current_config.led_count_strip_b;
|
||||||
cnf->pwm_pin = current_config.pwm_pin;
|
cnf->rc_signal_pin = current_config.rc_signal_pin;
|
||||||
cnf->localBtn_pin = current_config.localBtn_pin;
|
cnf->localBtn_pin = current_config.localBtn_pin;
|
||||||
|
cnf->use_sbus_mode = current_config.use_sbus_mode;
|
||||||
|
cnf->sbus_trigger_channel = current_config.sbus_trigger_channel;
|
||||||
|
cnf->sbus_threshold_low = current_config.sbus_threshold_low;
|
||||||
|
cnf->sbus_threshold_high = current_config.sbus_threshold_high;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t config_init(void)
|
esp_err_t config_init(void)
|
||||||
@ -168,8 +191,12 @@ esp_err_t config_init(void)
|
|||||||
current_config.led_pin_strip_b = HARDCODED_CONFIG_LED_STRIP_B_PIN;
|
current_config.led_pin_strip_b = HARDCODED_CONFIG_LED_STRIP_B_PIN;
|
||||||
current_config.led_count_strip_a = HARDCODED_CONFIG_LED_STRIP_A_COUNT;
|
current_config.led_count_strip_a = HARDCODED_CONFIG_LED_STRIP_A_COUNT;
|
||||||
current_config.led_count_strip_b = HARDCODED_CONFIG_LED_STRIP_B_COUNT;
|
current_config.led_count_strip_b = HARDCODED_CONFIG_LED_STRIP_B_COUNT;
|
||||||
current_config.pwm_pin = HARDCODED_CONFIG_PWM_PIN;
|
current_config.rc_signal_pin = HARDCODED_CONFIG_RC_SIGNAL_PIN;
|
||||||
current_config.localBtn_pin = HARDCODED_CONFIG_LOCALBTN_PIN;
|
current_config.localBtn_pin = HARDCODED_CONFIG_LOCALBTN_PIN;
|
||||||
|
current_config.use_sbus_mode = HARDCODED_CONFIG_USE_SBUS_MODE;
|
||||||
|
current_config.sbus_trigger_channel = HARDCODED_CONFIG_SBUS_TRIGGER_CHANNEL;
|
||||||
|
current_config.sbus_threshold_low = HARDCODED_CONFIG_SBUS_THRESHOLD_LOW;
|
||||||
|
current_config.sbus_threshold_high = HARDCODED_CONFIG_SBUS_THRESHOLD_HIGH;
|
||||||
|
|
||||||
save_config_to_nvs();
|
save_config_to_nvs();
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -17,12 +17,19 @@
|
|||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int8_t led_pin_strip_a; // GPIO pin for LED strip A (-1 = not configured)
|
int8_t led_pin_strip_a; // GPIO pin for LED strip A (-1 = not configured)
|
||||||
int8_t led_pin_strip_b; // GPIO pin for LED strip B (-1 = not configured)
|
int8_t led_pin_strip_b; // GPIO pin for LED strip B (-1 = not configured)
|
||||||
int8_t led_count_strip_a; // LED count for LED strip A (-1 = not configured)
|
int8_t led_count_strip_a; // LED count for LED strip A (-1 = not configured)
|
||||||
int8_t led_count_strip_b; // LED count for LED strip B (-1 = not configured)
|
int8_t led_count_strip_b; // LED count for LED strip B (-1 = not configured)
|
||||||
int8_t pwm_pin; // GPIO pin for PWM input (-1 = not configured)
|
int8_t rc_signal_pin; // GPIO pin for RC signal input (-1 = not configured)
|
||||||
int8_t localBtn_pin; // GPIO pin for local btn input (-1 = not configured)
|
int8_t localBtn_pin; // GPIO pin for local btn input (-1 = not configured)
|
||||||
|
|
||||||
|
// RC Signal mode settings
|
||||||
|
bool use_sbus_mode; // true = SBUS mode, false = PWM mode
|
||||||
|
uint8_t sbus_trigger_channel; // SBUS channel for mode trigger (0-15, typically 3 for CH4)
|
||||||
|
uint16_t sbus_threshold_low; // SBUS low threshold (default 800)
|
||||||
|
uint16_t sbus_threshold_high; // SBUS high threshold (default 1100)
|
||||||
|
|
||||||
uint8_t hash[CONFIG_HASH_LEN]; // SHA256 Hash of config
|
uint8_t hash[CONFIG_HASH_LEN]; // SHA256 Hash of config
|
||||||
} config_t;
|
} config_t;
|
||||||
|
|
||||||
|
|||||||
@ -69,7 +69,7 @@ esp_err_t control_init(void)
|
|||||||
ESP_ERROR_CHECK(gpio_install_isr_service(0));
|
ESP_ERROR_CHECK(gpio_install_isr_service(0));
|
||||||
|
|
||||||
// Initialize RC signal
|
// Initialize RC signal
|
||||||
ret = rcsignal_init(current_config.pwm_pin);
|
ret = rcsignal_init(¤t_config);
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "RC signal init failed: %s", esp_err_to_name(ret));
|
ESP_LOGE(TAG, "RC signal init failed: %s", esp_err_to_name(ret));
|
||||||
|
|||||||
326
main/rcsignal.c
326
main/rcsignal.c
@ -1,25 +1,22 @@
|
|||||||
/**
|
/**
|
||||||
* @file rcsignal.c
|
* @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 "rcsignal.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
|
#include "driver/uart.h"
|
||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
|
||||||
#ifdef USE_SBUS_MODE
|
|
||||||
#include "driver/uart.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static const char *TAG = "RCSIGNAL";
|
static const char *TAG = "RCSIGNAL";
|
||||||
|
|
||||||
#ifdef USE_SBUS_MODE
|
|
||||||
// SBUS protocol constants
|
// SBUS protocol constants
|
||||||
#define SBUS_FRAME_SIZE 25
|
#define SBUS_FRAME_SIZE 25
|
||||||
#define SBUS_HEADER 0x0F
|
#define SBUS_HEADER 0x0F
|
||||||
@ -29,59 +26,59 @@ static const char *TAG = "RCSIGNAL";
|
|||||||
#define SBUS_CH_MIN 172
|
#define SBUS_CH_MIN 172
|
||||||
#define SBUS_CH_CENTER 992
|
#define SBUS_CH_CENTER 992
|
||||||
#define SBUS_CH_MAX 1811
|
#define SBUS_CH_MAX 1811
|
||||||
#define SBUS_THRESHOLD_LOW 800
|
|
||||||
#define SBUS_THRESHOLD_HIGH 1100
|
|
||||||
#define UART_NUM UART_NUM_1
|
#define UART_NUM UART_NUM_1
|
||||||
#define UART_BUF_SIZE 256
|
#define UART_BUF_SIZE 256
|
||||||
#else
|
|
||||||
// PWM mode constants
|
// PWM mode constants
|
||||||
#define PULSE_THRESHOLD_US 1500
|
#define PULSE_THRESHOLD_US 1500
|
||||||
#define SIGNAL_TIMEOUT_MS 100
|
#define SIGNAL_TIMEOUT_MS 100
|
||||||
#endif
|
|
||||||
static struct
|
static struct
|
||||||
{
|
{
|
||||||
int8_t gpio_pin;
|
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 uint16_t channels[SBUS_NUM_CHANNELS];
|
||||||
volatile int64_t last_frame_time;
|
volatile int64_t last_frame_time;
|
||||||
volatile bool signal_active;
|
|
||||||
volatile bool pull_detected;
|
|
||||||
uint8_t rx_buffer[SBUS_FRAME_SIZE];
|
uint8_t rx_buffer[SBUS_FRAME_SIZE];
|
||||||
#else
|
|
||||||
|
// PWM state
|
||||||
volatile uint32_t pulse_width_us;
|
volatile uint32_t pulse_width_us;
|
||||||
volatile int64_t last_edge_time;
|
volatile int64_t last_edge_time;
|
||||||
volatile int64_t pulse_start_time;
|
volatile int64_t pulse_start_time;
|
||||||
volatile bool last_level;
|
volatile bool last_level;
|
||||||
|
|
||||||
|
// Common state
|
||||||
volatile bool signal_active;
|
volatile bool signal_active;
|
||||||
volatile bool pull_detected;
|
volatile bool pull_detected;
|
||||||
#endif
|
|
||||||
uint8_t current_mode;
|
uint8_t current_mode;
|
||||||
rcsignal_mode_change_callback_t callback;
|
rcsignal_mode_change_callback_t callback;
|
||||||
bool initialized;
|
bool initialized;
|
||||||
TaskHandle_t monitor_task;
|
TaskHandle_t monitor_task;
|
||||||
} rcsignal = {
|
} rcsignal = {
|
||||||
.gpio_pin = -1,
|
.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},
|
.channels = {0},
|
||||||
.last_frame_time = 0,
|
.last_frame_time = 0,
|
||||||
.signal_active = false,
|
|
||||||
.pull_detected = false,
|
|
||||||
.rx_buffer = {0},
|
.rx_buffer = {0},
|
||||||
#else
|
|
||||||
.pulse_width_us = 0,
|
.pulse_width_us = 0,
|
||||||
.last_edge_time = 0,
|
.last_edge_time = 0,
|
||||||
.pulse_start_time = 0,
|
.pulse_start_time = 0,
|
||||||
.last_level = false,
|
.last_level = false,
|
||||||
.signal_active = false,
|
.signal_active = false,
|
||||||
.pull_detected = false,
|
.pull_detected = false,
|
||||||
#endif
|
|
||||||
.current_mode = 0,
|
.current_mode = 0,
|
||||||
.callback = NULL,
|
.callback = NULL,
|
||||||
.initialized = false,
|
.initialized = false,
|
||||||
.monitor_task = NULL,
|
.monitor_task = NULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifndef USE_SBUS_MODE
|
|
||||||
// PWM Mode: GPIO ISR handler
|
// PWM Mode: GPIO ISR handler
|
||||||
static void IRAM_ATTR gpio_isr_handler(void *arg)
|
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;
|
rcsignal.last_level = level;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
// SBUS Mode: Parse SBUS frame
|
// SBUS Mode: Parse SBUS frame
|
||||||
static bool parse_sbus_frame(const uint8_t *frame, uint16_t *channels)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
static void monitor_task(void *arg)
|
static void monitor_task(void *arg)
|
||||||
{
|
{
|
||||||
#ifdef USE_SBUS_MODE
|
if (rcsignal.use_sbus_mode)
|
||||||
while (1)
|
|
||||||
{
|
{
|
||||||
// Read SBUS data from UART
|
// SBUS mode
|
||||||
int len = uart_read_bytes(UART_NUM, rcsignal.rx_buffer, SBUS_FRAME_SIZE, pdMS_TO_TICKS(20));
|
while (1)
|
||||||
|
|
||||||
if (len == SBUS_FRAME_SIZE)
|
|
||||||
{
|
{
|
||||||
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
|
uint16_t temp_channels[SBUS_NUM_CHANNELS];
|
||||||
for (int i = 0; i < SBUS_NUM_CHANNELS; i++)
|
|
||||||
|
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();
|
// Check for signal timeout
|
||||||
rcsignal.signal_active = true;
|
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
|
vTaskDelay(pdMS_TO_TICKS(5));
|
||||||
uint16_t ch4_value = rcsignal.channels[SBUS_TRIGGER_CHANNEL];
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// PWM mode
|
||||||
|
uint32_t last_pulse_width = 0;
|
||||||
|
|
||||||
// Detect pull low
|
while (1)
|
||||||
if (ch4_value < SBUS_THRESHOLD_LOW)
|
{
|
||||||
|
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;
|
rcsignal.pull_detected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect rising edge (pull high after low)
|
if (rcsignal.pulse_width_us > PULSE_THRESHOLD_US && rcsignal.pull_detected)
|
||||||
if (ch4_value > SBUS_THRESHOLD_HIGH && rcsignal.pull_detected)
|
|
||||||
{
|
{
|
||||||
|
// Mode change detected
|
||||||
rcsignal.pull_detected = false;
|
rcsignal.pull_detected = false;
|
||||||
|
|
||||||
if (rcsignal.callback)
|
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)");
|
ESP_LOGI(TAG, "RC signal disabled (no pin configured)");
|
||||||
return ESP_OK;
|
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
|
if (rcsignal.use_sbus_mode)
|
||||||
// SBUS Mode: Configure UART with inverted RX
|
{
|
||||||
ESP_LOGI(TAG, "Initializing SBUS mode on GPIO%d", pin);
|
// 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 = {
|
uart_config_t uart_config = {
|
||||||
.baud_rate = SBUS_BAUDRATE,
|
.baud_rate = SBUS_BAUDRATE,
|
||||||
.data_bits = UART_DATA_8_BITS,
|
.data_bits = UART_DATA_8_BITS,
|
||||||
.parity = UART_PARITY_EVEN,
|
.parity = UART_PARITY_EVEN,
|
||||||
.stop_bits = UART_STOP_BITS_2,
|
.stop_bits = UART_STOP_BITS_2,
|
||||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||||
.source_clk = UART_SCLK_APB,
|
.source_clk = UART_SCLK_APB,
|
||||||
};
|
};
|
||||||
|
|
||||||
ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
|
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_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));
|
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)
|
// Set inverted RX for FrSky receivers (they output inverted SBUS)
|
||||||
ESP_ERROR_CHECK(uart_set_line_inverse(UART_NUM, UART_SIGNAL_RXD_INV));
|
ESP_ERROR_CHECK(uart_set_line_inverse(UART_NUM, UART_SIGNAL_RXD_INV));
|
||||||
|
|
||||||
ESP_LOGI(TAG, "SBUS UART configured with inverted RX");
|
ESP_LOGI(TAG, "SBUS UART configured with inverted RX");
|
||||||
#else
|
}
|
||||||
// PWM Mode: Configure GPIO with interrupts
|
else
|
||||||
ESP_LOGI(TAG, "Initializing PWM mode on GPIO%d", pin);
|
{
|
||||||
|
// PWM Mode: Configure GPIO with interrupts
|
||||||
|
ESP_LOGI(TAG, "Initializing PWM mode on GPIO%d", rcsignal.gpio_pin);
|
||||||
|
|
||||||
gpio_config_t io_conf = {
|
gpio_config_t io_conf = {
|
||||||
.pin_bit_mask = (1ULL << pin),
|
.pin_bit_mask = (1ULL << rcsignal.gpio_pin),
|
||||||
.mode = GPIO_MODE_INPUT,
|
.mode = GPIO_MODE_INPUT,
|
||||||
.pull_up_en = GPIO_PULLUP_ENABLE,
|
.pull_up_en = GPIO_PULLUP_ENABLE,
|
||||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||||
.intr_type = GPIO_INTR_ANYEDGE,
|
.intr_type = GPIO_INTR_ANYEDGE,
|
||||||
};
|
};
|
||||||
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
||||||
|
|
||||||
// Install ISR service
|
// Add ISR handler (ISR service must be installed by caller)
|
||||||
ESP_ERROR_CHECK(gpio_isr_handler_add(pin, gpio_isr_handler, NULL));
|
ESP_ERROR_CHECK(gpio_isr_handler_add(rcsignal.gpio_pin, gpio_isr_handler, NULL));
|
||||||
#endif
|
}
|
||||||
|
|
||||||
// Create monitor task
|
// Create monitor task
|
||||||
BaseType_t ret = xTaskCreate(monitor_task, "rcsignal_monitor", 2048, NULL, 5, &rcsignal.monitor_task);
|
BaseType_t ret = xTaskCreate(monitor_task, "rcsignal_monitor", 2048, NULL, 5, &rcsignal.monitor_task);
|
||||||
if (ret != pdPASS)
|
if (ret != pdPASS)
|
||||||
{
|
{
|
||||||
#ifdef USE_SBUS_MODE
|
if (rcsignal.use_sbus_mode)
|
||||||
uart_driver_delete(UART_NUM);
|
{
|
||||||
#else
|
uart_driver_delete(UART_NUM);
|
||||||
gpio_isr_handler_remove(pin);
|
}
|
||||||
gpio_uninstall_isr_service();
|
else
|
||||||
#endif
|
{
|
||||||
|
gpio_isr_handler_remove(rcsignal.gpio_pin);
|
||||||
|
}
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,14 +324,17 @@ void rcsignal_deinit(void)
|
|||||||
rcsignal.monitor_task = NULL;
|
rcsignal.monitor_task = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_SBUS_MODE
|
if (rcsignal.use_sbus_mode)
|
||||||
uart_driver_delete(UART_NUM);
|
|
||||||
#else
|
|
||||||
if (rcsignal.gpio_pin >= 0)
|
|
||||||
{
|
{
|
||||||
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;
|
rcsignal.initialized = false;
|
||||||
}
|
}
|
||||||
@ -331,18 +346,21 @@ void rcsignal_register_callback(rcsignal_mode_change_callback_t callback)
|
|||||||
|
|
||||||
uint32_t rcsignal_get_pulse_width(void)
|
uint32_t rcsignal_get_pulse_width(void)
|
||||||
{
|
{
|
||||||
#ifdef USE_SBUS_MODE
|
if (rcsignal.use_sbus_mode)
|
||||||
// In SBUS mode, return channel 4 value mapped to microseconds
|
|
||||||
// SBUS: 172-1811 -> PWM: ~1000-2000us
|
|
||||||
if (rcsignal.signal_active)
|
|
||||||
{
|
{
|
||||||
uint16_t ch_val = rcsignal.channels[SBUS_TRIGGER_CHANNEL];
|
// In SBUS mode, return trigger channel value mapped to microseconds
|
||||||
return 1000 + ((ch_val - SBUS_CH_MIN) * 1000) / (SBUS_CH_MAX - SBUS_CH_MIN);
|
// 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)
|
bool rcsignal_is_active(void)
|
||||||
@ -355,10 +373,9 @@ uint8_t rcsignal_get_current_mode(void)
|
|||||||
return rcsignal.current_mode;
|
return rcsignal.current_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_SBUS_MODE
|
|
||||||
uint16_t rcsignal_get_sbus_channel(uint8_t channel)
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -367,6 +384,12 @@ uint16_t rcsignal_get_sbus_channel(uint8_t channel)
|
|||||||
|
|
||||||
void rcsignal_debug_print_channels(void)
|
void rcsignal_debug_print_channels(void)
|
||||||
{
|
{
|
||||||
|
if (!rcsignal.use_sbus_mode)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "Not in SBUS mode");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!rcsignal.signal_active)
|
if (!rcsignal.signal_active)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "No SBUS 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]);
|
rcsignal.channels[14], rcsignal.channels[15]);
|
||||||
|
|
||||||
// Highlight the trigger channel
|
// Highlight the trigger channel
|
||||||
ESP_LOGI(TAG, "Trigger channel (CH%d): %d", SBUS_TRIGGER_CHANNEL + 1,
|
ESP_LOGI(TAG, "Trigger channel (CH%d): %d", rcsignal.sbus_trigger_channel + 1,
|
||||||
rcsignal.channels[SBUS_TRIGGER_CHANNEL]);
|
rcsignal.channels[rcsignal.sbus_trigger_channel]);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|||||||
@ -1,24 +1,19 @@
|
|||||||
/**
|
/**
|
||||||
* @file rcsignal.h
|
* @file rcsignal.h
|
||||||
* @brief RC PWM signal reading and parsing module
|
* @brief RC PWM/SBUS signal reading and parsing module
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef RCSIGNAL_H
|
#ifndef RCSIGNAL_H
|
||||||
#define RCSIGNAL_H
|
#define RCSIGNAL_H
|
||||||
|
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
// Define to switch between PWM and SBUS mode
|
// SBUS protocol constants (always defined for use in both modes)
|
||||||
// Comment out to use PWM mode, uncomment to use SBUS mode
|
#define SBUS_NUM_CHANNELS 16 // SBUS supports 16 proportional channels
|
||||||
#define USE_SBUS_MODE
|
|
||||||
|
|
||||||
#ifdef USE_SBUS_MODE
|
|
||||||
#define SBUS_NUM_CHANNELS 16 // SBUS supports 16 proportional channels
|
|
||||||
#define SBUS_TRIGGER_CHANNEL 3 // Channel 4 (index 3) for mode trigger
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Callback function type for mode changes
|
* @brief Callback function type for mode changes
|
||||||
@ -27,10 +22,10 @@ typedef void (*rcsignal_mode_change_callback_t)();
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize RC signal reading
|
* @brief Initialize RC signal reading
|
||||||
* @param pin GPIO pin for PWM input (-1 to disable)
|
* @param config Pointer to configuration structure
|
||||||
* @return ESP_OK on success
|
* @return ESP_OK on success
|
||||||
*/
|
*/
|
||||||
esp_err_t rcsignal_init(int8_t pin);
|
esp_err_t rcsignal_init(const config_t *config);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Deinitialize RC signal reading
|
* @brief Deinitialize RC signal reading
|
||||||
@ -61,18 +56,16 @@ bool rcsignal_is_active(void);
|
|||||||
*/
|
*/
|
||||||
uint8_t rcsignal_get_current_mode(void);
|
uint8_t rcsignal_get_current_mode(void);
|
||||||
|
|
||||||
#ifdef USE_SBUS_MODE
|
|
||||||
/**
|
/**
|
||||||
* @brief Get SBUS channel value
|
* @brief Get SBUS channel value (only valid in SBUS mode)
|
||||||
* @param channel Channel index (0-15)
|
* @param channel Channel index (0-15)
|
||||||
* @return Channel value (172-1811) or 0 if invalid
|
* @return Channel value (172-1811) or 0 if invalid/not in SBUS mode
|
||||||
*/
|
*/
|
||||||
uint16_t rcsignal_get_sbus_channel(uint8_t channel);
|
uint16_t rcsignal_get_sbus_channel(uint8_t channel);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Debug function to print all SBUS channels
|
* @brief Debug function to print all SBUS channels (only valid in SBUS mode)
|
||||||
*/
|
*/
|
||||||
void rcsignal_debug_print_channels(void);
|
void rcsignal_debug_print_channels(void);
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif // RCSIGNAL_H
|
#endif // RCSIGNAL_H
|
||||||
|
|||||||
Reference in New Issue
Block a user