Piezo 측정 파라미터 FDS 저장/측정 경로 범위 검증 통일
- frequency: 1.7M(3) / 1.8M(0) / 1.9M(9) / 2.0M(2) / 2.1M(1) / 2.2M(4) * 그 외의 값이 들어올 경우 1로 저장(기본 fallback 2.1MHz) - cycles: 최소 3 ~ 최대 7 * 3 미만의 값이 들어올 경우 3, 7 초과의 값이 들어올 경우 7 저장 - avgeraging: 최소 1 ~ 최대 10 * 1 미만의 값이 들어올 경우 1, 10 초과의 값이 들어올 경우 10 저장 - dealy_us: 최소 0 ~ 최대 50 * 0 미만의 값이 들어올 경우 0, 50 초과의 값이 들어올 경우 50 저장 - num samples: 최소 80 ~ 최대 119 * 80 미만의 값이 들어올 경우 80, 119 초과의 값이 들어올 경우 119 저장
This commit is contained in:
@@ -17,6 +17,88 @@
|
|||||||
#include "dr_piezo.h"
|
#include "dr_piezo.h"
|
||||||
#include "dr_adc121s051.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
|
* 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]
|
* 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]
|
* 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)
|
* Error: rcs: + 0xFFFF (insufficient data)
|
||||||
*============================================================================*/
|
*============================================================================*/
|
||||||
int Cmd_mcs(const ParsedCmd *cmd)
|
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, 3, &delay_us);
|
||||||
dr_get_u16(cmd, 4, &num_samples);
|
dr_get_u16(cmd, 4, &num_samples);
|
||||||
|
|
||||||
m_config.piezo_freq_option = (uint8_t)freq;
|
m_config.piezo_freq_option = clamp_piezo_freq_option(freq);
|
||||||
m_config.piezo_cycles = (uint8_t)cycles;
|
m_config.piezo_cycles = clamp_piezo_cycles(cycles);
|
||||||
m_config.piezo_averaging = averaging;
|
m_config.piezo_averaging = clamp_piezo_averaging(averaging);
|
||||||
m_config.piezo_delay_us = delay_us;
|
m_config.piezo_delay_us = clamp_piezo_delay_us(delay_us);
|
||||||
m_config.piezo_num_samples = num_samples;
|
m_config.piezo_num_samples = clamp_piezo_num_samples(num_samples);
|
||||||
config_save();
|
config_save();
|
||||||
|
|
||||||
uint8_t *buf = ble_bin_buffer;
|
uint8_t *buf = ble_bin_buffer;
|
||||||
|
|||||||
@@ -61,11 +61,11 @@ typedef struct
|
|||||||
uint32_t life_cycle; /* 4B - device usage count */
|
uint32_t life_cycle; /* 4B - device usage count */
|
||||||
|
|
||||||
/* Piezo measurement parameters - 8B */
|
/* 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_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) */
|
uint8_t piezo_cycles; /* 1B - burst pulse cycle count (3~7) */
|
||||||
uint16_t piezo_averaging; /* 2B - averages per channel (1..10) */
|
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_delay_us; /* 2B - delay from TX pulse to ADC start (us) (0~50) */
|
||||||
uint16_t piezo_num_samples; /* 2B - ADC sample count (80..140) */
|
uint16_t piezo_num_samples; /* 2B - ADC sample count (80~119) */
|
||||||
|
|
||||||
/* Factory provisioning lock */
|
/* Factory provisioning lock */
|
||||||
uint8_t factory_provisioned; /* 1B - 0=passkey not set, 1=passkey set (locked) */
|
uint8_t factory_provisioned; /* 1B - 0=passkey not set, 1=passkey set (locked) */
|
||||||
|
|||||||
+89
-49
@@ -57,6 +57,74 @@ extern void dr_piezo_power_off(void);
|
|||||||
|
|
||||||
#include "parser.h"
|
#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
|
* 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;
|
return DR_ADC_ERR_INVALID_PARAM;
|
||||||
}
|
}
|
||||||
if (num_samples == 0 || num_samples > DR_ADC_ECHO_SAMPLES_MAX)
|
freq_option = normalize_freq_option(freq_option);
|
||||||
{
|
delay_us = clamp_measure_delay_us(delay_us);
|
||||||
return DR_ADC_ERR_INVALID_PARAM;
|
num_samples = clamp_measure_num_samples(num_samples);
|
||||||
}
|
cycles = clamp_measure_cycles(cycles);
|
||||||
if (freq_option > 9)
|
averaging = clamp_measure_averaging(averaging);
|
||||||
{
|
|
||||||
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 */
|
|
||||||
}
|
|
||||||
if (piezo_ch >= MAA_NUM_CHANNELS)
|
if (piezo_ch >= MAA_NUM_CHANNELS)
|
||||||
{
|
{
|
||||||
piezo_ch = 0; /* clamp channel range */
|
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;
|
return DR_ADC_ERR_INVALID_PARAM;
|
||||||
}
|
}
|
||||||
if (num_samples == 0 || num_samples > MAA_SAMPLES_MAX)
|
freq_option = normalize_freq_option(freq_option);
|
||||||
{
|
delay_us = clamp_measure_delay_us(delay_us);
|
||||||
return DR_ADC_ERR_INVALID_PARAM;
|
num_samples = clamp_measure_num_samples(num_samples);
|
||||||
}
|
cycles = clamp_measure_cycles(cycles);
|
||||||
if (freq_option > 3)
|
averaging = clamp_measure_averaging(averaging);
|
||||||
{
|
|
||||||
freq_option = 0;
|
|
||||||
}
|
|
||||||
if (cycles < 3 || cycles > 7)
|
|
||||||
{
|
|
||||||
cycles = 5;
|
|
||||||
}
|
|
||||||
if (averaging == 0)
|
|
||||||
{
|
|
||||||
averaging = 1;
|
|
||||||
}
|
|
||||||
if (averaging > 1000)
|
|
||||||
{
|
|
||||||
averaging = 1000;
|
|
||||||
}
|
|
||||||
if (piezo_ch > (MAA_NUM_CHANNELS - 1))
|
if (piezo_ch > (MAA_NUM_CHANNELS - 1))
|
||||||
{
|
{
|
||||||
piezo_ch = 0;
|
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:
|
case 3:
|
||||||
dr_piezo_burst_sw_17mhz(cycles);
|
dr_piezo_burst_sw_17mhz(cycles);
|
||||||
break;
|
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 */
|
/* 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;
|
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 */
|
/* Clear BLE buffer to prevent stale data from previous measurement */
|
||||||
memset(ble_buffer, 0, BLE_MTU_SIZE);
|
memset(ble_buffer, 0, BLE_MTU_SIZE);
|
||||||
|
|
||||||
/* Initialize context */
|
/* Initialize context */
|
||||||
g_maa_ctx.freq_option = freq_option;
|
g_maa_ctx.freq_option = normalize_freq_option(freq_option);
|
||||||
g_maa_ctx.delay_us = delay_us;
|
g_maa_ctx.delay_us = clamp_measure_delay_us(delay_us);
|
||||||
g_maa_ctx.num_samples = num_samples;
|
g_maa_ctx.num_samples = clamp_measure_num_samples(num_samples);
|
||||||
g_maa_ctx.cycles = (cycles < 3 || cycles > 7) ? 5 : cycles;
|
g_maa_ctx.cycles = clamp_measure_cycles(cycles);
|
||||||
g_maa_ctx.averaging = (averaging == 0) ? 1 : ((averaging > 1000) ? 1000 : averaging);
|
g_maa_ctx.averaging = clamp_measure_averaging(averaging);
|
||||||
g_maa_ctx.ble_buffer = ble_buffer;
|
g_maa_ctx.ble_buffer = ble_buffer;
|
||||||
g_maa_ctx.current_ch = 0;
|
g_maa_ctx.current_ch = 0;
|
||||||
g_maa_ctx.current_pkt = 0;
|
g_maa_ctx.current_pkt = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user