Add saving cnf in ROM, workink with leds

This commit is contained in:
2026-03-30 18:42:53 +03:00
parent 0b982e5d4d
commit d43458c770
82 changed files with 43899 additions and 41684 deletions

View File

@@ -5,6 +5,11 @@
#include "usart.h"
#include "main.h"
#include "config/config_consts.h"
#include "config/config_types.h"
#include "config/config_defaults.h"
#include "config/config_store.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
@@ -12,20 +17,6 @@
#include <ctype.h>
#include <stdbool.h>
#define RX_TIMEOUT_VALUE_MS 0U
#define TX_TIMEOUT_VALUE_MS 3000U
#define RX_CONTINUOUS_ON 1U
#define RADIO_SYNCWORD_LEN 3U
#define RADIO_WHITENING_SEED 0x01FFU
#define RADIO_CRC_POLY 0x8005U
#define RADIO_CRC_SEED 0xFFFFU
#define UART_DATA_BUFFER_SIZE 220U
#define RADIO_MAX_PAYLOAD_SIZE 220U
#define TX_QUEUE_DEPTH 4U
#define CONFIG_LINE_SIZE 96U
#define CONFIG_ESCAPE_GUARD_MS 800U
#define DEFAULT_UART_PACKET_TIMEOUT_MS 20U
typedef enum
{
@@ -33,19 +24,6 @@ typedef enum
APP_MODE_CONFIG
} AppMode_t;
typedef struct
{
uint32_t rf_frequency;
int8_t tx_power;
uint32_t fsk_bitrate;
uint32_t fsk_bandwidth;
uint32_t fsk_fdev;
uint16_t fsk_preamble_len;
uint8_t syncword[RADIO_SYNCWORD_LEN];
uint16_t uart_packet_timeout_ms;
uint32_t uart_baudrate;
} BridgeConfig_t;
typedef struct
{
uint8_t data[RADIO_MAX_PAYLOAD_SIZE];
@@ -62,19 +40,7 @@ typedef struct
} EscapeDetector_t;
static RadioEvents_t RadioEvents;
static BridgeConfig_t g_cfg =
{
.rf_frequency = RF_FREQUENCY_DEFAULT,
.tx_power = TX_OUTPUT_POWER_DEFAULT,
.fsk_bitrate = FSK_DATARATE_DEFAULT,
.fsk_bandwidth = FSK_BANDWIDTH_DEFAULT,
.fsk_fdev = FSK_FDEV_DEFAULT,
.fsk_preamble_len = FSK_PREAMBLE_LENGTH_DEFAULT,
.syncword = {0xC1, 0x94, 0xC1},
.uart_packet_timeout_ms = DEFAULT_UART_PACKET_TIMEOUT_MS,
.uart_baudrate = 115200UL,
};
static BridgeConfig_t g_cfg;
static volatile uint8_t g_radio_tx_done = 0;
static volatile uint8_t g_radio_tx_timeout = 0;
static volatile uint8_t g_radio_rx_done = 0;
@@ -83,6 +49,10 @@ static volatile uint8_t g_radio_rx_error = 0;
static volatile int16_t g_last_rx_rssi = 0;
static volatile int8_t g_last_rx_cfo = 0;
static volatile uint8_t g_led_tx_ticks = 0U;
static volatile uint8_t g_led_rx_ticks = 0U;
static volatile uint8_t g_led_err_ticks = 0U;
static volatile uint8_t g_radio_busy = 0;
static volatile uint8_t g_radio_needs_rx_restart = 0;
@@ -116,6 +86,10 @@ static void OnTxTimeout(void);
static void OnRxTimeout(void);
static void OnRxError(void);
static void App_LedTxPulse(void);
static void App_LedRxPulse(void);
static void App_LedErrPulse(void);
static void UartRxByteCallback(uint8_t *rxChar, uint16_t size, uint8_t error);
static void App_ProcessRadioEvents(void);
static void App_ProcessUartPacketizer(void);
@@ -145,6 +119,11 @@ static char *App_SkipSpaces(char *s);
void SubghzApp_Init(void)
{
if (!Config_Load(&g_cfg))
{
Config_LoadDefaults(&g_cfg);
}
RadioEvents.TxDone = OnTxDone;
RadioEvents.RxDone = OnRxDone;
RadioEvents.TxTimeout = OnTxTimeout;
@@ -152,9 +131,19 @@ void SubghzApp_Init(void)
RadioEvents.RxError = OnRxError;
Radio.Init(&RadioEvents);
App_RadioApplyConfig();
App_ReconfigureUart(g_cfg.uart_baudrate);
App_RadioEnterRx();
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);
g_led_tx_ticks = 0U;
g_led_rx_ticks = 0U;
g_led_err_ticks = 0U;
g_uart_last_data_tick = HAL_GetTick();
(void)vcom_ReceiveInit(UartRxByteCallback);
@@ -185,6 +174,7 @@ static void App_ProcessRadioEvents(void)
{
g_radio_tx_timeout = 0U;
g_radio_busy = 0U;
App_LedErrPulse();
App_QueuePop();
App_Printf("\r\n[WARN] radio tx timeout\r\n");
g_radio_needs_rx_restart = 1U;
@@ -193,6 +183,7 @@ static void App_ProcessRadioEvents(void)
if (g_radio_rx_done != 0U)
{
g_radio_rx_done = 0U;
App_LedRxPulse();
g_stat_radio_packets_rx++;
g_stat_radio_bytes_rx += g_rx_payload_len;
@@ -208,6 +199,7 @@ static void App_ProcessRadioEvents(void)
{
g_radio_rx_timeout = 0U;
g_radio_rx_error = 0U;
App_LedErrPulse();
g_radio_needs_rx_restart = 1U;
}
@@ -273,8 +265,19 @@ static void App_StartNextTxIfPossible(void)
App_RadioConfigureTx();
g_radio_busy = 1U;
App_LedTxPulse();
(void)Radio.Send(g_tx_queue[g_tx_q_head].data, g_tx_queue[g_tx_q_head].len);
}
static void App_ApplyConfig(void)
{
App_RadioApplyConfig();
App_RadioConfigureRx();
App_RadioConfigureTx();
if (!Config_Save(&g_cfg)) {
App_Printf("Error while saving cnf\r\n");
}
}
static void App_RadioApplyConfig(void)
{
@@ -350,6 +353,13 @@ static void App_EnterConfigMode(void)
g_mode = APP_MODE_CONFIG;
App_Printf("\r\n\r\n[CONFIG MODE]\r\n");
App_Printf("type 'help' for commands\r\n");
if (!Config_Load(&g_cfg))
{
App_Printf("Error while loading cnf\r\n");
App_Printf("Cnf was reset to defaults\r\n");
Config_LoadDefaults(&g_cfg);
}
App_PrintConfigPrompt();
}
@@ -555,20 +565,14 @@ static void App_ConfigExecuteLine(char *line)
App_ExitConfigMode();
return;
}
if (strcmp(line, "save") == 0)
{
App_ApplyConfig();
return;
}
if (strcmp(line, "defaults") == 0)
{
g_cfg.rf_frequency = RF_FREQUENCY_DEFAULT;
g_cfg.tx_power = TX_OUTPUT_POWER_DEFAULT;
g_cfg.fsk_bitrate = FSK_DATARATE_DEFAULT;
g_cfg.fsk_bandwidth = FSK_BANDWIDTH_DEFAULT;
g_cfg.fsk_fdev = FSK_FDEV_DEFAULT;
g_cfg.fsk_preamble_len = FSK_PREAMBLE_LENGTH_DEFAULT;
g_cfg.syncword[0] = 0xC1U;
g_cfg.syncword[1] = 0x94U;
g_cfg.syncword[2] = 0xC1U;
g_cfg.uart_packet_timeout_ms = DEFAULT_UART_PACKET_TIMEOUT_MS;
App_RadioApplyConfig();
Config_LoadDefaults(&g_cfg);
App_Printf("defaults restored\r\n");
return;
}
@@ -582,7 +586,6 @@ static void App_ConfigExecuteLine(char *line)
return;
}
g_cfg.rf_frequency = u32;
App_RadioApplyConfig();
App_Printf("freq=%lu\r\n", (unsigned long)g_cfg.rf_frequency);
return;
}
@@ -596,7 +599,6 @@ static void App_ConfigExecuteLine(char *line)
return;
}
g_cfg.tx_power = (int8_t)pwr;
App_RadioApplyConfig();
App_Printf("power=%d\r\n", g_cfg.tx_power);
return;
}
@@ -610,7 +612,6 @@ static void App_ConfigExecuteLine(char *line)
return;
}
g_cfg.fsk_bitrate = u32;
App_RadioApplyConfig();
App_Printf("bitrate=%lu\r\n", (unsigned long)g_cfg.fsk_bitrate);
return;
}
@@ -624,7 +625,6 @@ static void App_ConfigExecuteLine(char *line)
return;
}
g_cfg.fsk_bandwidth = u32;
App_RadioApplyConfig();
App_Printf("bandwidth=%lu\r\n", (unsigned long)g_cfg.fsk_bandwidth);
return;
}
@@ -638,7 +638,6 @@ static void App_ConfigExecuteLine(char *line)
return;
}
g_cfg.fsk_fdev = u32;
App_RadioApplyConfig();
App_Printf("fdev=%lu\r\n", (unsigned long)g_cfg.fsk_fdev);
return;
}
@@ -652,7 +651,6 @@ static void App_ConfigExecuteLine(char *line)
return;
}
g_cfg.fsk_preamble_len = (uint16_t)u32;
App_RadioApplyConfig();
App_Printf("preamble=%u\r\n", g_cfg.fsk_preamble_len);
return;
}
@@ -693,7 +691,6 @@ static void App_ConfigExecuteLine(char *line)
return;
}
memcpy(g_cfg.syncword, sync, sizeof(sync));
App_RadioApplyConfig();
App_Printf("sync=%02X%02X%02X\r\n", g_cfg.syncword[0], g_cfg.syncword[1], g_cfg.syncword[2]);
return;
}
@@ -722,7 +719,8 @@ static void App_PrintHelp(void)
App_Printf(" preamble <bytes> - fsk preamble length\r\n");
App_Printf(" sync <hex6> - 3-byte syncword, example C194C1\r\n");
App_Printf(" timeout <ms> - uart silence before rf packet send\r\n");
App_Printf(" uart <baud> - change uart baudrate immediately\r\n");
App_Printf(" uart <baud> - change uart baudrate\r\n");
App_Printf(" save - save and apply changes\r\n");
App_Printf(" defaults - restore default config\r\n");
App_Printf(" exit - return to transparent bridge mode\r\n");
}
@@ -864,6 +862,61 @@ static char *App_SkipSpaces(char *s)
}
return s;
}
void SubghzApp_Timer1msIrq(void)
{
if (g_led_tx_ticks > 0U)
{
g_led_tx_ticks--;
if (g_led_tx_ticks == 0U)
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
}
}
if (g_led_rx_ticks > 0U)
{
g_led_rx_ticks--;
if (g_led_rx_ticks == 0U)
{
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
}
}
if (g_led_err_ticks > 0U)
{
g_led_err_ticks--;
if (g_led_err_ticks == 0U)
{
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2) // замени на свой таймер
{
SubghzApp_Timer1msIrq();
}
}
static void App_LedTxPulse(void)
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
g_led_tx_ticks = LED_PULSE_MS;
}
static void App_LedRxPulse(void)
{
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
g_led_rx_ticks = LED_PULSE_MS;
}
static void App_LedErrPulse(void)
{
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_SET);
g_led_err_ticks = LED_PULSE_MS;
}
static void OnTxDone(void)
{