diff --git a/project/ble_peripheral/ble_app_bladder_patch/command/handlers/cmd_piezo.c b/project/ble_peripheral/ble_app_bladder_patch/command/handlers/cmd_piezo.c index b0bb065..99afd7c 100644 --- a/project/ble_peripheral/ble_app_bladder_patch/command/handlers/cmd_piezo.c +++ b/project/ble_peripheral/ble_app_bladder_patch/command/handlers/cmd_piezo.c @@ -17,6 +17,88 @@ #include "dr_piezo.h" #include "dr_adc121s051.h" +/*------------------------------------------------------------------------------ + * Internal clamp helpers for persisted piezo configuration + *----------------------------------------------------------------------------*/ +static uint8_t clamp_piezo_freq_option(uint16_t raw_freq) +{ + /* Keep the existing option-code protocol: + * 0=1.8MHz, 1=2.1MHz, 2=2.0MHz, 3=1.7MHz, 4=2.2MHz, 9=1.9MHz + * Unknown values fall back to 2.1MHz (code 1). + */ + switch (raw_freq) + { + case 3: /* 1.7MHz */ + return 3; + + case 0: /* 1.8MHz */ + return 0; + + case 9: /* 1.9MHz */ + return 9; + + case 2: /* 2.0MHz */ + return 2; + + case 1: /* 2.1MHz */ + return 1; + + case 4: /* 2.2MHz */ + return 4; + + default: + return 1; /* unknown code -> default to 2.1MHz */ + } +} + +static uint8_t clamp_piezo_cycles(uint16_t cycles) +{ + if (cycles < 3) + { + return 3; + } + if (cycles > 7) + { + return 7; + } + return (uint8_t)cycles; +} + +static uint16_t clamp_piezo_averaging(uint16_t averaging) +{ + if (averaging < 1) + { + return 1; + } + if (averaging > 10) + { + return 10; + } + return averaging; +} + +static uint16_t clamp_piezo_delay_us(uint16_t delay_us) +{ + if (delay_us > 50) + { + return 50; + } + return delay_us; +} + +static uint16_t clamp_piezo_num_samples(uint16_t num_samples) +{ + if (num_samples < 80) + { + return 80; + } + if (num_samples > 119) + { + return 119; + } + return num_samples; +} + /*============================================================================== * mpa? -> rpa: Enable piezo TX/RX circuit * @@ -303,6 +385,13 @@ int Cmd_mcf(const ParsedCmd *cmd) * * Request: [TAG 4B "mcs?"] [freq 2B] [cycles 2B] [avg 2B] [delay_us 2B] [num_samples 2B] [CRC 2B] * Response: [TAG 4B "rcs:"] [stored 5 values] [CRC 2B] + * Notes: + * - Stored values are clamped before saving + * - freq : 1.8/1.9/2.0/2.1/2.2 MHz only + * - cycles : 3..7 + * - avg : 1..10 + * - delay_us : 0..50 + * - samples : 80..119 * Error: rcs: + 0xFFFF (insufficient data) *============================================================================*/ int Cmd_mcs(const ParsedCmd *cmd) @@ -324,11 +413,11 @@ int Cmd_mcs(const ParsedCmd *cmd) dr_get_u16(cmd, 3, &delay_us); dr_get_u16(cmd, 4, &num_samples); - m_config.piezo_freq_option = (uint8_t)freq; - m_config.piezo_cycles = (uint8_t)cycles; - m_config.piezo_averaging = averaging; - m_config.piezo_delay_us = delay_us; - m_config.piezo_num_samples = num_samples; + m_config.piezo_freq_option = clamp_piezo_freq_option(freq); + m_config.piezo_cycles = clamp_piezo_cycles(cycles); + m_config.piezo_averaging = clamp_piezo_averaging(averaging); + m_config.piezo_delay_us = clamp_piezo_delay_us(delay_us); + m_config.piezo_num_samples = clamp_piezo_num_samples(num_samples); config_save(); uint8_t *buf = ble_bin_buffer; diff --git a/project/ble_peripheral/ble_app_bladder_patch/hal/fds/fstorage.h b/project/ble_peripheral/ble_app_bladder_patch/hal/fds/fstorage.h index a05d9e0..75f3ecb 100644 --- a/project/ble_peripheral/ble_app_bladder_patch/hal/fds/fstorage.h +++ b/project/ble_peripheral/ble_app_bladder_patch/hal/fds/fstorage.h @@ -61,11 +61,11 @@ typedef struct uint32_t life_cycle; /* 4B - device usage count */ /* Piezo measurement parameters - 8B */ - uint8_t piezo_freq_option; /* 1B - TX pulse frequency (0=1.8M, 1=2.1M, 2=2.0M, 3=1.7M) */ - uint8_t piezo_cycles; /* 1B - burst pulse cycle count (3..7) */ - uint16_t piezo_averaging; /* 2B - averages per channel (1..10) */ - uint16_t piezo_delay_us; /* 2B - delay from TX pulse to ADC start (us) (0..30) */ - uint16_t piezo_num_samples; /* 2B - ADC sample count (80..140) */ + uint8_t piezo_freq_option; /* 1B - TX pulse frequency option (9=1.9M, 2=2.0M, 1=2.1M, 4=2.2M) */ + uint8_t piezo_cycles; /* 1B - burst pulse cycle count (3~7) */ + uint16_t piezo_averaging; /* 2B - averages per channel (1~10) */ + uint16_t piezo_delay_us; /* 2B - delay from TX pulse to ADC start (us) (0~50) */ + uint16_t piezo_num_samples; /* 2B - ADC sample count (80~119) */ /* Factory provisioning lock */ uint8_t factory_provisioned; /* 1B - 0=passkey not set, 1=passkey set (locked) */ diff --git a/project/ble_peripheral/ble_app_bladder_patch/measurement/adc121s051/dr_adc121s051.c b/project/ble_peripheral/ble_app_bladder_patch/measurement/adc121s051/dr_adc121s051.c index b8f1ba6..615c7be 100644 --- a/project/ble_peripheral/ble_app_bladder_patch/measurement/adc121s051/dr_adc121s051.c +++ b/project/ble_peripheral/ble_app_bladder_patch/measurement/adc121s051/dr_adc121s051.c @@ -57,6 +57,74 @@ extern void dr_piezo_power_off(void); #include "parser.h" +/*------------------------------------------------------------------------------ + * Internal helpers + *----------------------------------------------------------------------------*/ +static uint8_t normalize_freq_option(uint8_t freq_option) +{ + switch (freq_option) + { + case 0: /* 1.8MHz */ + case 1: /* 2.1MHz */ + case 2: /* 2.0MHz */ + case 3: /* 1.7MHz */ + case 4: /* 2.2MHz */ + case 9: /* 1.9MHz */ + return freq_option; + + default: + return 1; /* Invalid -> default to 2.1MHz */ + } +} + +static uint8_t clamp_measure_cycles(uint8_t cycles) +{ + if (cycles < 3) + { + return 3; + } + if (cycles > 7) + { + return 7; + } + return cycles; +} + +static uint16_t clamp_measure_averaging(uint16_t averaging) +{ + if (averaging < 1) + { + return 1; + } + if (averaging > 10) + { + return 10; + } + return averaging; +} + +static uint16_t clamp_measure_num_samples(uint16_t num_samples) +{ + if (num_samples < 80) + { + return 80; + } + if (num_samples > 119) + { + return 119; + } + return num_samples; +} + +static uint16_t clamp_measure_delay_us(uint16_t delay_us) +{ + if (delay_us > 50) + { + return 50; + } + return delay_us; +} + /*============================================================================== * DEBUG CONFIGURATION *============================================================================*/ @@ -579,26 +647,11 @@ dr_adc_err_t dr_adc_burst_capture_transmit(uint8_t freq_option, uint16_t delay_u { return DR_ADC_ERR_INVALID_PARAM; } - if (num_samples == 0 || num_samples > DR_ADC_ECHO_SAMPLES_MAX) - { - return DR_ADC_ERR_INVALID_PARAM; - } - if (freq_option > 9) - { - freq_option = 0; /* Invalid -> default 1.8MHz */ - } - if (cycles < 3 || cycles > 7) - { - cycles = 5; /* Valid range: 3~7, default 5 */ - } - if (averaging == 0) - { - averaging = 1; /* Minimum 1 */ - } - if (averaging > 1000) - { - averaging = 1000; /* Maximum 1000 */ - } + freq_option = normalize_freq_option(freq_option); + delay_us = clamp_measure_delay_us(delay_us); + num_samples = clamp_measure_num_samples(num_samples); + cycles = clamp_measure_cycles(cycles); + averaging = clamp_measure_averaging(averaging); if (piezo_ch >= MAA_NUM_CHANNELS) { piezo_ch = 0; /* clamp channel range */ @@ -821,26 +874,11 @@ dr_adc_err_t dr_adc_capture_channel_only(uint8_t freq_option, uint16_t delay_us, { return DR_ADC_ERR_INVALID_PARAM; } - if (num_samples == 0 || num_samples > MAA_SAMPLES_MAX) - { - return DR_ADC_ERR_INVALID_PARAM; - } - if (freq_option > 3) - { - freq_option = 0; - } - if (cycles < 3 || cycles > 7) - { - cycles = 5; - } - if (averaging == 0) - { - averaging = 1; - } - if (averaging > 1000) - { - averaging = 1000; - } + freq_option = normalize_freq_option(freq_option); + delay_us = clamp_measure_delay_us(delay_us); + num_samples = clamp_measure_num_samples(num_samples); + cycles = clamp_measure_cycles(cycles); + averaging = clamp_measure_averaging(averaging); if (piezo_ch > (MAA_NUM_CHANNELS - 1)) { piezo_ch = 0; @@ -893,6 +931,12 @@ dr_adc_err_t dr_adc_capture_channel_only(uint8_t freq_option, uint16_t delay_us, case 3: dr_piezo_burst_sw_17mhz(cycles); break; + case 4: + dr_piezo_burst_sw_22mhz(cycles); + break; + case 9: + dr_piezo_burst_sw_19mhz(cycles); + break; } /* delay from TX burst to ADC start */ @@ -1281,20 +1325,16 @@ dr_adc_err_t maa_async_start(uint8_t freq_option, uint16_t delay_us, uint16_t nu { return DR_ADC_ERR_INVALID_PARAM; } - if (num_samples == 0 || num_samples > DR_ADC_ECHO_SAMPLES_MAX) - { - return DR_ADC_ERR_INVALID_PARAM; - } /* Clear BLE buffer to prevent stale data from previous measurement */ memset(ble_buffer, 0, BLE_MTU_SIZE); /* Initialize context */ - g_maa_ctx.freq_option = freq_option; - g_maa_ctx.delay_us = delay_us; - g_maa_ctx.num_samples = num_samples; - g_maa_ctx.cycles = (cycles < 3 || cycles > 7) ? 5 : cycles; - g_maa_ctx.averaging = (averaging == 0) ? 1 : ((averaging > 1000) ? 1000 : averaging); + g_maa_ctx.freq_option = normalize_freq_option(freq_option); + g_maa_ctx.delay_us = clamp_measure_delay_us(delay_us); + g_maa_ctx.num_samples = clamp_measure_num_samples(num_samples); + g_maa_ctx.cycles = clamp_measure_cycles(cycles); + g_maa_ctx.averaging = clamp_measure_averaging(averaging); g_maa_ctx.ble_buffer = ble_buffer; g_maa_ctx.current_ch = 0; g_maa_ctx.current_pkt = 0;