Stabilize USB control firmware

This commit is contained in:
Ayzen
2026-04-26 18:39:55 +03:00
parent ea1fbb071d
commit d32db245fa
51 changed files with 11583 additions and 11479 deletions

View File

@ -97,6 +97,14 @@ typedef struct app_context_t {
/* Written by app_uart_protocol_feed_byte() and reset helpers. Read by UART IRQ path and timeout logic. Purpose: incremental UART parser state. */
app_uart_protocol_parser_t parser;
/*
* Written by USART IRQ after a complete packet is parsed.
* Consumed by app_run_once() so device, SPI, delay, and SD-card work
* stays out of interrupt context.
*/
volatile uint8_t pending_uart_packet_ready;
app_packet_t pending_uart_packet;
/* Written by app_capture_live_frame() and telemetry helpers. Read by telemetry finalisation/serialisation. Purpose: canonical structured telemetry snapshot. */
telemetry_frame_t telemetry;
@ -157,6 +165,7 @@ static void app_handle_ad9102_wave_control_packet(const app_packet_t *packet);
static void app_handle_ad9102_wave_data_packet(const app_packet_t *packet);
static void app_handle_profile_save_data_packet(const app_packet_t *packet);
static void app_process_work_mode(void);
static void app_handle_pending_uart_packet(void);
static void app_flush_tx_request(void);
static void app_check_uart_timeout(void);
static void app_handle_pending_event(void);
@ -197,6 +206,7 @@ static void app_reset_runtime_state(bool reset_ticks)
telemetry_reset(&g_app.telemetry);
app_uart_protocol_reset(&g_app.parser);
g_app.pending_uart_packet_ready = 0u;
uart_transport_reset();
if (reset_ticks)
@ -633,6 +643,12 @@ static bool app_handle_profile_boot(void)
FILINFO profile_index_info;
FRESULT profile_index_result;
/* Legacy behaviour: saved SD startup data is used only without USB. */
if (board_io_is_usb_connected())
{
return false;
}
app_set_boot_diag(APP_BOOT_DIAG_STARTED);
if (!storage_sd_is_available())
@ -1224,6 +1240,31 @@ static void app_process_work_mode(void)
}
}
static void app_handle_pending_uart_packet(void)
{
app_packet_t packet;
uint8_t packet_ready = 0u;
if (g_app.pending_uart_packet_ready == 0u)
{
return;
}
__disable_irq();
if (g_app.pending_uart_packet_ready != 0u)
{
packet = g_app.pending_uart_packet;
g_app.pending_uart_packet_ready = 0u;
packet_ready = 1u;
}
__enable_irq();
if (packet_ready != 0u)
{
app_handle_packet(&packet);
}
}
static void app_flush_tx_request(void)
{
switch (g_app.tx_request)
@ -1304,7 +1345,10 @@ void app_init(void)
app_initialise_default_configuration();
app_reset_runtime_state(true);
ui_status_init();
if (!board_io_is_usb_connected())
{
ui_status_init();
}
app_set_mode(APP_MODE_IDLE);
g_app.resume_mode = APP_MODE_IDLE;
app_set_boot_diag(APP_BOOT_DIAG_NONE);
@ -1330,9 +1374,16 @@ void app_run_once(void)
app_event_t ui_event;
app_ensure_uart_rx_enabled();
if (g_app.mode == APP_MODE_WORK)
{
app_process_work_mode();
}
ui_event = ui_status_poll_event(g_app.tick_10ms);
app_post_event(ui_event);
app_handle_pending_uart_packet();
app_handle_pending_event();
switch (g_app.mode)
@ -1389,7 +1440,15 @@ void app_on_uart_byte(uint8_t byte)
}
else if (result == APP_PROTOCOL_FEED_PACKET_READY)
{
app_handle_packet(&packet);
if (g_app.pending_uart_packet_ready == 0u)
{
g_app.pending_uart_packet = packet;
g_app.pending_uart_packet_ready = 1u;
}
else
{
app_set_error(APP_STATUS_FLAG_UART_ERROR);
}
}
}

View File

@ -40,6 +40,10 @@ uint16_t adc_mux_read_external_channel(uint8_t channel_index)
++delay_counter;
}
LL_SPI_Disable(SPI4);
while (delay_counter < 500u)
{
++delay_counter;
}
HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_SET);
result = LL_SPI_ReceiveData16(SPI4);
}
@ -58,6 +62,10 @@ uint16_t adc_mux_read_external_channel(uint8_t channel_index)
++delay_counter;
}
LL_SPI_Disable(SPI5);
while (delay_counter < 500u)
{
++delay_counter;
}
HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_SET);
result = LL_SPI_ReceiveData16(SPI5);
}
@ -76,6 +84,10 @@ uint16_t adc_mux_read_external_channel(uint8_t channel_index)
++delay_counter;
}
LL_SPI_Disable(SPI4);
while (delay_counter < 500u)
{
++delay_counter;
}
HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_SET);
result = LL_SPI_ReceiveData16(SPI4);
}
@ -94,6 +106,10 @@ uint16_t adc_mux_read_external_channel(uint8_t channel_index)
++delay_counter;
}
LL_SPI_Disable(SPI5);
while (delay_counter < 500u)
{
++delay_counter;
}
HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_SET);
result = LL_SPI_ReceiveData16(SPI5);
}

View File

@ -112,9 +112,16 @@ void ui_status_set_error(uint8_t error_flags)
app_event_t ui_status_poll_event(uint32_t tick_10ms)
{
bool raw_pressed = board_io_is_standalone_ui_button_pressed();
bool raw_pressed;
app_event_t event = APP_EVENT_NONE;
if (g_initialised == 0u)
{
return APP_EVENT_NONE;
}
raw_pressed = board_io_is_standalone_ui_button_pressed();
if (raw_pressed != g_button.raw_pressed)
{
g_button.raw_pressed = raw_pressed;