sweep feature added
This commit is contained in:
+150
-29
@@ -26,6 +26,7 @@
|
||||
#include "profile_storage.h"
|
||||
#include "storage_sd.h"
|
||||
#include "stm32_dac_output.h"
|
||||
#include "sweep.h"
|
||||
#include "telemetry.h"
|
||||
#include "temperature_control.h"
|
||||
#include "uart_transport.h"
|
||||
@@ -40,12 +41,16 @@
|
||||
#define APP_TEC_CHANNEL_COUNT 2u
|
||||
#define APP_TEC_DRIVE_MIN_CODE 1000u
|
||||
#define APP_TEC_DRIVE_MAX_CODE 56800u
|
||||
/* Largest swing that fits the drive range; the actual amplitude is further limited by headroom around the PID output. */
|
||||
#define APP_TEC_MODULATION_MAX_AMPLITUDE_CODE ((APP_TEC_DRIVE_MAX_CODE - APP_TEC_DRIVE_MIN_CODE) / 2u)
|
||||
#define APP_TEC_MODULATION_UPDATE_RATE_HZ 16000u
|
||||
#define APP_TEC_MODULATION_LUT_BITS 5u
|
||||
#define APP_TEC_MODULATION_LUT_SIZE (1u << APP_TEC_MODULATION_LUT_BITS)
|
||||
#define APP_TEC_MODULATION_PHASE_SHIFT (32u - APP_TEC_MODULATION_LUT_BITS)
|
||||
#define APP_TEC_MODULATION_ALLOWED_FLAGS \
|
||||
(APP_TEC_MODULATION_FLAG_ENABLE | APP_TEC_MODULATION_FLAG_CHANNEL_2)
|
||||
#define APP_SWEEP_TICK_US 10u
|
||||
#define APP_SWEEP_ALLOWED_FLAGS (APP_SWEEP_FLAG_ENABLE | APP_SWEEP_FLAG_TARGET_STM32_DAC)
|
||||
|
||||
/*
|
||||
* Boot-diagnostic codes exposed through QUERY_STATE (0x6666).
|
||||
@@ -151,8 +156,17 @@ typedef struct app_context_t {
|
||||
/* Written by PID loop. Read by TIM5. Purpose: latest slow TEC drive centers for both channels. */
|
||||
uint16_t tec_base_code[APP_TEC_CHANNEL_COUNT];
|
||||
|
||||
/* Written by main-loop SPI sections. Read by TIM5. Purpose: skip modulation writes while shared SPI is busy. */
|
||||
volatile uint8_t tec_modulation_spi_suspended;
|
||||
/* Written by main-loop SPI sections. Read by TIM5 and TIM2. Purpose: defer IRQ-driven DAC writes while shared SPI is busy. */
|
||||
volatile uint8_t irq_dac_writes_suspended;
|
||||
|
||||
/* Written by sweep command and TIM2. Read by TIM2 and the WORK loop. Purpose: laser 2 current staircase sweep. */
|
||||
sweep_t laser2_current_sweep;
|
||||
|
||||
/* Written by sweep command and TIM2. Read by TIM2 and STM32 DAC setters. Purpose: STM32 DAC staircase sweep. */
|
||||
sweep_t stm32_dac_sweep;
|
||||
|
||||
/* Written by STM32 DAC command, profiles and idle reset. Read when the DAC sweep stops. Purpose: manual DAC output setting. */
|
||||
stm32_dac_config_t stm32_dac;
|
||||
|
||||
/* Written when a profile is activated. Read mainly for future profile cycling/debug visibility. Purpose: snapshot of the currently selected standalone profile. */
|
||||
profile_t active_profile;
|
||||
@@ -180,11 +194,14 @@ static uint16_t app_clamp_tec_drive_code(int32_t value);
|
||||
static bool app_normalize_tec_modulation_config(const tec_modulation_config_t *input,
|
||||
tec_modulation_config_t *output);
|
||||
static void app_store_tec_modulation_config(const tec_modulation_config_t *config);
|
||||
static void app_suspend_tec_modulation_spi(void);
|
||||
static void app_resume_tec_modulation_spi(void);
|
||||
static void app_suspend_irq_dac_writes(void);
|
||||
static void app_resume_irq_dac_writes(void);
|
||||
static void app_write_laser_dac_channel(uint8_t channel, uint16_t value);
|
||||
static void app_write_tec_pid_output(uint8_t channel_index, uint16_t pid_code);
|
||||
static void app_write_tec_modulation_sample(void);
|
||||
static void app_set_stm32_dac(uint16_t code, uint8_t enabled);
|
||||
static void app_update_sweep_timer(void);
|
||||
static void app_stop_sweeps(void);
|
||||
static void app_prepare_status_response(void);
|
||||
static void app_capture_live_frame(bool update_temperature_loops, bool drive_laser_currents);
|
||||
static void app_decode_work_packet(const uint16_t *packet_words);
|
||||
@@ -204,6 +221,7 @@ static void app_handle_ad9833_control_packet(const app_packet_t *packet);
|
||||
static void app_handle_ds1809_control_packet(const app_packet_t *packet);
|
||||
static void app_handle_stm32_dac_control_packet(const app_packet_t *packet);
|
||||
static void app_handle_tec_modulation_control_packet(const app_packet_t *packet);
|
||||
static void app_handle_sweep_control_packet(const app_packet_t *packet);
|
||||
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);
|
||||
@@ -335,6 +353,7 @@ static void app_apply_work_config_to_board(void)
|
||||
|
||||
static void app_enter_idle_state(bool reset_ticks)
|
||||
{
|
||||
app_stop_sweeps();
|
||||
g_app.work_config = g_app.default_work_config;
|
||||
g_app.laser_config[0] = g_app.default_laser_config[0];
|
||||
g_app.laser_config[1] = g_app.default_laser_config[1];
|
||||
@@ -346,7 +365,7 @@ static void app_enter_idle_state(bool reset_ticks)
|
||||
ad9102_stop_output();
|
||||
ad9833_apply(0u, 0u, 0u);
|
||||
board_io_reset_runtime_outputs();
|
||||
stm32_dac_output_set(0u, 0u);
|
||||
app_set_stm32_dac(0u, 0u);
|
||||
|
||||
if (LL_SPI_IsEnabled(SPI2))
|
||||
{
|
||||
@@ -367,7 +386,7 @@ static void app_reset_tec_modulation(void)
|
||||
g_app.tec_modulation_phase_step = 0u;
|
||||
g_app.tec_base_code[0] = APP_TEC_PRECHARGE_CODE;
|
||||
g_app.tec_base_code[1] = APP_TEC_PRECHARGE_CODE;
|
||||
g_app.tec_modulation_spi_suspended = 0u;
|
||||
g_app.irq_dac_writes_suspended = 0u;
|
||||
}
|
||||
|
||||
static bool app_is_tec_channel_active(uint8_t channel_index)
|
||||
@@ -452,31 +471,31 @@ static void app_store_tec_modulation_config(const tec_modulation_config_t *confi
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
static void app_suspend_tec_modulation_spi(void)
|
||||
static void app_suspend_irq_dac_writes(void)
|
||||
{
|
||||
__disable_irq();
|
||||
if (g_app.tec_modulation_spi_suspended < 0xFFu)
|
||||
if (g_app.irq_dac_writes_suspended < 0xFFu)
|
||||
{
|
||||
++g_app.tec_modulation_spi_suspended;
|
||||
++g_app.irq_dac_writes_suspended;
|
||||
}
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
static void app_resume_tec_modulation_spi(void)
|
||||
static void app_resume_irq_dac_writes(void)
|
||||
{
|
||||
__disable_irq();
|
||||
if (g_app.tec_modulation_spi_suspended > 0u)
|
||||
if (g_app.irq_dac_writes_suspended > 0u)
|
||||
{
|
||||
--g_app.tec_modulation_spi_suspended;
|
||||
--g_app.irq_dac_writes_suspended;
|
||||
}
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
static void app_write_laser_dac_channel(uint8_t channel, uint16_t value)
|
||||
{
|
||||
app_suspend_tec_modulation_spi();
|
||||
app_suspend_irq_dac_writes();
|
||||
laser_dac_write_channel(channel, value);
|
||||
app_resume_tec_modulation_spi();
|
||||
app_resume_irq_dac_writes();
|
||||
}
|
||||
|
||||
static void app_write_tec_pid_output(uint8_t channel_index, uint16_t pid_code)
|
||||
@@ -515,7 +534,7 @@ static void app_write_tec_modulation_sample(void)
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_app.tec_modulation_spi_suspended != 0u)
|
||||
if (g_app.irq_dac_writes_suspended != 0u)
|
||||
{
|
||||
g_app.tec_modulation_phase += g_app.tec_modulation_phase_step;
|
||||
return;
|
||||
@@ -544,6 +563,37 @@ static void app_write_tec_modulation_sample(void)
|
||||
g_app.tec_modulation_phase += g_app.tec_modulation_phase_step;
|
||||
}
|
||||
|
||||
static void app_set_stm32_dac(uint16_t code, uint8_t enabled)
|
||||
{
|
||||
g_app.stm32_dac.code = code;
|
||||
g_app.stm32_dac.enabled = enabled;
|
||||
|
||||
/* A running sweep owns the output; the manual setting is restored when it stops. */
|
||||
if (g_app.stm32_dac_sweep.enabled == 0u)
|
||||
{
|
||||
stm32_dac_output_set(code, enabled);
|
||||
}
|
||||
}
|
||||
|
||||
static void app_update_sweep_timer(void)
|
||||
{
|
||||
if ((g_app.laser2_current_sweep.enabled != 0u) || (g_app.stm32_dac_sweep.enabled != 0u))
|
||||
{
|
||||
LL_TIM_EnableCounter(TIM2);
|
||||
}
|
||||
else
|
||||
{
|
||||
LL_TIM_DisableCounter(TIM2);
|
||||
}
|
||||
}
|
||||
|
||||
static void app_stop_sweeps(void)
|
||||
{
|
||||
sweep_stop(&g_app.laser2_current_sweep);
|
||||
sweep_stop(&g_app.stm32_dac_sweep);
|
||||
app_update_sweep_timer();
|
||||
}
|
||||
|
||||
static void app_apply_profile_auxiliary_outputs(const profile_t *profile)
|
||||
{
|
||||
if (profile == NULL)
|
||||
@@ -552,7 +602,7 @@ static void app_apply_profile_auxiliary_outputs(const profile_t *profile)
|
||||
}
|
||||
|
||||
ad9833_apply(profile->ad9833.enabled, profile->ad9833.triangle, profile->ad9833.frequency_word);
|
||||
stm32_dac_output_set(profile->stm32_dac.code, profile->stm32_dac.enabled);
|
||||
app_set_stm32_dac(profile->stm32_dac.code, profile->stm32_dac.enabled);
|
||||
}
|
||||
|
||||
static void app_prepare_status_response(void)
|
||||
@@ -630,9 +680,9 @@ static bool app_activate_profile(const profile_t *profile, uint16_t index, bool
|
||||
|
||||
if (profile->ds1809.apply_position)
|
||||
{
|
||||
app_suspend_tec_modulation_spi();
|
||||
app_suspend_irq_dac_writes();
|
||||
ds1809_apply_position_from_min(profile->ds1809.position_from_min);
|
||||
app_resume_tec_modulation_spi();
|
||||
app_resume_irq_dac_writes();
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -673,7 +723,10 @@ static void app_capture_live_frame(bool update_temperature_loops, bool drive_las
|
||||
if (drive_laser_currents)
|
||||
{
|
||||
app_write_laser_dac_channel(1u, g_app.laser_config[0].current_raw);
|
||||
app_write_laser_dac_channel(2u, g_app.laser_config[1].current_raw);
|
||||
if (g_app.laser2_current_sweep.enabled == 0u)
|
||||
{
|
||||
app_write_laser_dac_channel(2u, g_app.laser_config[1].current_raw);
|
||||
}
|
||||
}
|
||||
|
||||
(void)adc_mux_capture_internal_telemetry(&internal_telemetry);
|
||||
@@ -1112,7 +1165,7 @@ static void app_handle_ad9102_control_packet(const app_packet_t *packet)
|
||||
triangle = (flags & APP_AD9102_FLAG_TRIANGLE) ? 1u : 0u;
|
||||
sram_mode = (flags & APP_AD9102_FLAG_SRAM) ? 1u : 0u;
|
||||
|
||||
app_suspend_tec_modulation_spi();
|
||||
app_suspend_irq_dac_writes();
|
||||
if (sram_mode != 0u)
|
||||
{
|
||||
uint16_t sample_count;
|
||||
@@ -1185,7 +1238,7 @@ static void app_handle_ad9102_control_packet(const app_packet_t *packet)
|
||||
app_set_error(APP_STATUS_FLAG_AD9102_ERROR);
|
||||
}
|
||||
}
|
||||
app_resume_tec_modulation_spi();
|
||||
app_resume_irq_dac_writes();
|
||||
|
||||
g_app.status_bytes[1] = (uint8_t)(pat_status & 0x00FFu);
|
||||
app_prepare_status_response();
|
||||
@@ -1209,11 +1262,11 @@ static void app_handle_ad9833_control_packet(const app_packet_t *packet)
|
||||
lsw = (uint16_t)(packet->words[1] & 0x3FFFu);
|
||||
msw = (uint16_t)(packet->words[2] & 0x3FFFu);
|
||||
frequency_word = ((uint32_t)msw << 14) | (uint32_t)lsw;
|
||||
app_suspend_tec_modulation_spi();
|
||||
app_suspend_irq_dac_writes();
|
||||
ad9833_apply((flags & APP_AD9833_FLAG_ENABLE) ? 1u : 0u,
|
||||
(flags & APP_AD9833_FLAG_TRIANGLE) ? 1u : 0u,
|
||||
frequency_word);
|
||||
app_resume_tec_modulation_spi();
|
||||
app_resume_irq_dac_writes();
|
||||
app_prepare_status_response();
|
||||
}
|
||||
|
||||
@@ -1275,8 +1328,8 @@ static void app_handle_stm32_dac_control_packet(const app_packet_t *packet)
|
||||
return;
|
||||
}
|
||||
|
||||
stm32_dac_output_set((uint16_t)(packet->words[1] & 0x0FFFu),
|
||||
(packet->words[0] & APP_STM32_DAC_FLAG_ENABLE) ? 1u : 0u);
|
||||
app_set_stm32_dac((uint16_t)(packet->words[1] & 0x0FFFu),
|
||||
(packet->words[0] & APP_STM32_DAC_FLAG_ENABLE) ? 1u : 0u);
|
||||
app_prepare_status_response();
|
||||
}
|
||||
|
||||
@@ -1329,6 +1382,53 @@ static void app_handle_tec_modulation_control_packet(const app_packet_t *packet)
|
||||
app_prepare_status_response();
|
||||
}
|
||||
|
||||
static void app_handle_sweep_control_packet(const app_packet_t *packet)
|
||||
{
|
||||
sweep_config_t config;
|
||||
sweep_t *sweep;
|
||||
bool dac_target;
|
||||
bool started = true;
|
||||
|
||||
if ((packet == NULL) || !packet->checksum_valid ||
|
||||
((packet->words[0] & ~APP_SWEEP_ALLOWED_FLAGS) != 0u))
|
||||
{
|
||||
app_set_error(APP_STATUS_FLAG_UART_DECODE_ERROR);
|
||||
app_prepare_status_response();
|
||||
return;
|
||||
}
|
||||
|
||||
dac_target = (packet->words[0] & APP_SWEEP_FLAG_TARGET_STM32_DAC) != 0u;
|
||||
sweep = dac_target ? &g_app.stm32_dac_sweep : &g_app.laser2_current_sweep;
|
||||
sweep_stop(sweep);
|
||||
|
||||
if ((packet->words[0] & APP_SWEEP_FLAG_ENABLE) != 0u)
|
||||
{
|
||||
config.min_code = packet->words[1];
|
||||
config.max_code = packet->words[2];
|
||||
config.step_code = packet->words[3];
|
||||
config.step_period_us = packet->words[4];
|
||||
config.pause_ms = packet->words[5];
|
||||
|
||||
/* The laser current is only driven in WORK mode; idle entry stops the sweep again. */
|
||||
started = dac_target ? (config.max_code <= STM32_DAC_MAX_CODE) : (g_app.mode == APP_MODE_WORK);
|
||||
__disable_irq();
|
||||
started = started && sweep_start(sweep, &config, APP_SWEEP_TICK_US);
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
if (dac_target && (sweep->enabled == 0u))
|
||||
{
|
||||
stm32_dac_output_set(g_app.stm32_dac.code, g_app.stm32_dac.enabled);
|
||||
}
|
||||
app_update_sweep_timer();
|
||||
|
||||
if (!started)
|
||||
{
|
||||
app_set_error(APP_STATUS_FLAG_UART_DECODE_ERROR);
|
||||
}
|
||||
app_prepare_status_response();
|
||||
}
|
||||
|
||||
static void app_handle_ad9102_wave_control_packet(const app_packet_t *packet)
|
||||
{
|
||||
uint16_t opcode;
|
||||
@@ -1349,7 +1449,7 @@ static void app_handle_ad9102_wave_control_packet(const app_packet_t *packet)
|
||||
param0 = packet->words[1];
|
||||
param1 = packet->words[2];
|
||||
|
||||
app_suspend_tec_modulation_spi();
|
||||
app_suspend_irq_dac_writes();
|
||||
switch (opcode)
|
||||
{
|
||||
case APP_AD9102_WAVE_OPCODE_BEGIN:
|
||||
@@ -1391,7 +1491,7 @@ static void app_handle_ad9102_wave_control_packet(const app_packet_t *packet)
|
||||
app_set_error(APP_STATUS_FLAG_AD9102_ERROR);
|
||||
break;
|
||||
}
|
||||
app_resume_tec_modulation_spi();
|
||||
app_resume_irq_dac_writes();
|
||||
|
||||
app_prepare_status_response();
|
||||
}
|
||||
@@ -1406,13 +1506,13 @@ static void app_handle_ad9102_wave_data_packet(const app_packet_t *packet)
|
||||
return;
|
||||
}
|
||||
|
||||
app_suspend_tec_modulation_spi();
|
||||
app_suspend_irq_dac_writes();
|
||||
if (!ad9102_write_custom_chunk(&packet->words[1], packet->words[0]))
|
||||
{
|
||||
ad9102_cancel_custom_upload();
|
||||
app_set_error(APP_STATUS_FLAG_AD9102_ERROR);
|
||||
}
|
||||
app_resume_tec_modulation_spi();
|
||||
app_resume_irq_dac_writes();
|
||||
|
||||
app_prepare_status_response();
|
||||
}
|
||||
@@ -1506,6 +1606,10 @@ static void app_handle_packet(const app_packet_t *packet)
|
||||
app_handle_tec_modulation_control_packet(packet);
|
||||
break;
|
||||
|
||||
case APP_PACKET_KIND_SWEEP_CONTROL:
|
||||
app_handle_sweep_control_packet(packet);
|
||||
break;
|
||||
|
||||
case APP_PACKET_KIND_AD9102_WAVE_CONTROL:
|
||||
app_handle_ad9102_wave_control_packet(packet);
|
||||
break;
|
||||
@@ -1652,6 +1756,7 @@ void app_init(void)
|
||||
board_io_reset_runtime_outputs();
|
||||
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
|
||||
|
||||
LL_TIM_EnableIT_UPDATE(TIM2);
|
||||
LL_TIM_EnableIT_UPDATE(TIM5);
|
||||
LL_TIM_EnableCounter(TIM5);
|
||||
LL_TIM_EnableIT_UPDATE(TIM6);
|
||||
@@ -1764,6 +1869,22 @@ void app_on_tim7_tick(void)
|
||||
++g_app.tick_1ms;
|
||||
}
|
||||
|
||||
void app_on_tim2_tick(void)
|
||||
{
|
||||
uint16_t code;
|
||||
|
||||
/* The laser step is postponed, not dropped, while the main loop owns the shared DAC SPI bus. */
|
||||
if ((g_app.irq_dac_writes_suspended == 0u) &&
|
||||
sweep_tick(&g_app.laser2_current_sweep, &code))
|
||||
{
|
||||
laser_dac_write_channel(2u, code);
|
||||
}
|
||||
if (sweep_tick(&g_app.stm32_dac_sweep, &code))
|
||||
{
|
||||
stm32_dac_output_set(code, 1u);
|
||||
}
|
||||
}
|
||||
|
||||
void app_on_tim5_tick(void)
|
||||
{
|
||||
app_write_tec_modulation_sample();
|
||||
|
||||
@@ -58,6 +58,11 @@ void app_on_tim7_tick(void);
|
||||
*/
|
||||
void app_on_tim5_tick(void);
|
||||
|
||||
/**
|
||||
* @brief Notify the application core about the 10 us sweep tick.
|
||||
*/
|
||||
void app_on_tim2_tick(void);
|
||||
|
||||
/**
|
||||
* @brief Notify the application core that the USART DMA transmit finished.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user