VesiScan BASIC origin: Piezo + IMU firmware initial code

- nRF52840 + SoftDevice S140 BLE firmware
- Piezo ultrasound TX driver (2MHz, 8ch MUX)
- ICM42670P IMU 6-axis driver
- Echo AFE chain (ADA2200 + ADC121S051)
- BLE NUS command parser (mpa/mpc/mdc/mec/maa/msp)
- FDS flash config storage
- pc_firm parser and ADC driver included

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Charles Kwon
2026-03-11 10:40:20 +09:00
parent a8ba31871e
commit b3adfd42e6
49 changed files with 8459 additions and 2887 deletions

View File

@@ -23,8 +23,8 @@
#include "fstorage.h"
#include "nrf_pwr_mgmt.h"
#include "measurements.h"
#include "main.h"
#include "debug_print.h"
/* File ID and Key used for the configuration record. */
@@ -54,114 +54,39 @@ static fds_record_t const m_dummy_record =
};
/* 공장 입력 항목 1 */
/* 공장 입력 항목 2 */
//char serial_number_dflt[12] = "2025AAMAY0FF";
//uint16_t pd_delay_us = 0;
//int8_t reset_status=0;
//uint32_t pd_adc_calibration_dflt_PD0[M_LED_NUM] =
// {1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000, 1000, 1000, 1000, /* PD01 */
//};
//uint32_t pd_adc_calibration_dflt_PD1[M_LED_NUM] =
// {1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000, 1000, 1000, 1000, /* PD1 */
//};
//uint32_t pd_adc_calibration_dflt_PD2[M_LED_NUM] =
// {1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000, 1000, 1000, 1000, /* MOD1 */
//};
///* 공장 입력 항목 5 */
//uint32_t dark_noise_for_pd_dflt[PD_NUM] = /*PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PD8, PD9, PD10, PD11, PD12, PD13, PD14, PD15, PD16, PD17, PD18, PD19, PD20, */
// {1111, 1111};
/* 공장 입력 항목 8 */
//int8_t pd_adc_cnt_dflt = 8; /* 8, 16, 24, 32 */
int8_t reset_status_dflt =99;
/* 공장 입력 항목 9 */
//uint16_t led_delay_us_dflt = 8000; /* 0 ~ 65535 */
/* 공장 입력 항목 10 */
//uint16_t pd_delay_us_dflt = 8000; /* 0 ~ 65535 */
#if FEATURE_SECURE_CONNECTION
/* Bonding information delete */
//bool bond_data_delete_dflt = true;//
/* Static Passkey */
int8_t reset_status_dflt = 99;
uint8_t static_passkey_dflt[6] = "123456";
#endif
//
//uint16_t led_power_dp_dflt[48] = {
// //NULL, 42, 29, 41, 31, 28, 28, 42, 28, 39, 29, 27, 27, 42, 29, 40, 30, 28, 28, 40, 27, 38, 28, 25, 22
// 25,25,25,18,19,24,24,25,25,18,19,25,23,25,25,18,19,24,24,25,25,19,20,25,24,25,25,25,18,19,24,24,25,25,18,19,25,23,25,25,18,19,24,24,25,25,19,20
//};
void fds_default_value_set(void)
{
/* HW Number - empty (set via BLE command) */
memset(m_config.hw_no, 0, 12);
/* Serial Number */
//memcpy(m_config.serial_number, serial_number_dflt, 12);
/* PD-ADC Calibration Value */
// for(uint8_t j = 0; j < M_LED_NUM; j++){
// m_config.pd_adc_calibration_PD0[j] = pd_adc_calibration_dflt_PD0[j];
// }
// for(uint8_t j = 0; j < M_LED_NUM; j++){
// m_config.pd_adc_calibration_PD1[j] = pd_adc_calibration_dflt_PD1[j];
// }
// for(uint8_t j = 0; j < M_LED_NUM; j++){
// m_config.pd_adc_calibration_PD2[j] = pd_adc_calibration_dflt_PD2[j];
// }
// /* Dark Noise of PD */
// for(uint8_t i = 0; i < PD_NUM; i++){
// m_config.dark_noise_for_pd[i] = dark_noise_for_pd_dflt[i];
// }
/* PD-ADC Cycle for 8, 16, 24, 32 */
// m_config.pd_adc_cnt = pd_adc_cnt_dflt;
/* LED ON Delay for usec */
// m_config.led_delay_us = led_delay_us_dflt;
/* PD ON Delay for usec */
// m_config.pd_delay_us = pd_delay_us_dflt;
m_config.reset_status = reset_status_dflt;
#if FEATURE_SECURE_CONNECTION
/* Bonding information delete */
// m_config.bond_data_delete = bond_data_delete_dflt;
/* Serial Number - default from FIRMWARE_SERIAL_NO */
memset(m_config.serial_no, 0, 12);
memcpy(m_config.serial_no, "VB026030000", 11);
/* Static Passkey */
memcpy(m_config.static_passkey, static_passkey_dflt, 6);
#endif
//for(uint8_t i = 0; i < PD_NUM; i++){
// for(uint8_t j = 0; j < LED_NUM; j++){
// m_config.led_pd_dac_v[j] = 1000;
// }
// }
// for(uint8_t j = 0; j < LED_NUM; j++){
// m_config.led_power_dp[j] = led_power_dp_dflt[j] ;
// }
//
}
/* Bond data delete */
m_config.bond_data_delete = 1;
/* Reset status */
m_config.reset_status = reset_status_dflt;
/* Measurement parameters */
m_config.pd_adc_cnt = 8;
m_config.pd_delay_us = 8000;
}
static volatile uint8_t fds_last_evt = 0xFF;
static void fds_evt_handler( fds_evt_t const *p_evt )
{
fds_last_evt = p_evt->id;
switch( p_evt->id )
{
case FDS_EVT_INIT:
@@ -191,20 +116,20 @@ static void fds_evt_handler( fds_evt_t const *p_evt )
}
if(go_NVIC_SystemReset == true) {
/* After flash writing completed, System Reset */
printf("Off FDS_ENVET\r\n");
NVIC_SystemReset(); //0112
DBG_PRINTF("Off FDS_EVENT\r\n");
NVIC_SystemReset();
}
}
break;
case FDS_EVT_DEL_RECORD:
break;
break;
case FDS_EVT_DEL_FILE:
break;
case FDS_EVT_GC:
break;
break;
default:
break;
@@ -215,9 +140,16 @@ static void fds_evt_handler( fds_evt_t const *p_evt )
/**@brief Wait for fds to initialize. */
static void wait_for_fds_ready( void )
{
uint32_t timeout = 0;
while( !m_fds_initialized )
{
nrf_pwr_mgmt_run();
nrf_delay_ms(1);
timeout++;
if (timeout > 3000) { /* 3 second timeout */
DBG_PRINTF("[FDS] TIMEOUT!\r\n");
break;
}
}
}
@@ -227,11 +159,21 @@ void config_load( void )
ret_code_t rc;
fds_record_desc_t desc = { 0 };
fds_find_token_t tok = { 0 };
uint8_t cfg_retry = 0;
cfg_load_start:
memset((char *)&desc, 0, sizeof(desc));
memset((char *)&tok, 0, sizeof(tok));
rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);
DBG_PRINTF("[FDS] find rc=%u\r\n", rc);
/* FDS may not be fully ready yet - retry before writing defaults */
if (rc != NRF_SUCCESS && cfg_retry < 10) {
cfg_retry++;
DBG_PRINTF("[FDS] retry %u/10\r\n", cfg_retry);
nrf_delay_ms(100);
goto cfg_load_start;
}
if( rc == NRF_SUCCESS )
{
@@ -240,21 +182,32 @@ void config_load( void )
/* Open the record and read its contents. */
rc = fds_record_open(&desc, &config);
APP_ERROR_CHECK(rc);
if (rc != NRF_SUCCESS) {
/* CRC error or corrupt record - delete and use defaults */
DBG_PRINTF("[FDS] open ERR=%u, deleting\r\n", rc);
(void)fds_record_delete(&desc);
fds_gc();
fds_default_value_set();
goto cfg_load_write_new;
}
/* Copy the configuration from flash into m_dummy_cfg. */
/* Copy the configuration from flash into m_config. */
memcpy(&m_config, config.p_data, sizeof(config_data_t));
/* Close the record when done reading. */
rc = fds_record_close(&desc);
APP_ERROR_CHECK(rc);
DBG_PRINTF("[FDS] magic=0x%08X (expect 0x%08X)\r\n",
m_config.magic_number, CONFIG_MAGIC_NUMBER_VALUE);
if( m_config.magic_number != (uint32_t)CONFIG_MAGIC_NUMBER_VALUE )
{ // first init
DBG_PRINTF("[FDS] FORMAT! overwriting with defaults\r\n");
rc = fds_record_delete(&desc);
APP_ERROR_CHECK(rc);
m_config.magic_number = CONFIG_MAGIC_NUMBER_VALUE;
// default....
fds_default_value_set();
@@ -271,16 +224,24 @@ void config_load( void )
}
goto cfg_load_start;
}
DBG_PRINTF("[FDS] Loaded OK\r\n");
}
else
{
/* System config not found; write a new one. */
cfg_load_write_new:
DBG_PRINTF("[FDS] New - writing defaults\r\n");
/* System config not found (or corrupt); write a new one. */
m_config.magic_number = CONFIG_MAGIC_NUMBER_VALUE;
// default....
fds_default_value_set();
fds_flag_write = true;
rc = fds_record_write(&desc, &m_dummy_record);
if (rc != NRF_SUCCESS) {
DBG_PRINTF("[FDS] Write ERR=%u\r\n", rc);
fds_flag_write = false;
}
while( fds_flag_write )
{
nrf_pwr_mgmt_run();
@@ -306,44 +267,58 @@ void config_save( void )
fds_record_desc_t desc = { 0 };
fds_find_token_t tok = { 0 };
rc = fds_gc();
APP_ERROR_CHECK(rc);
DBG_PRINTF("[CFG_SAVE] start\r\n");
/* Skip if a previous FDS operation is still in progress (non-blocking) */
if (fds_flag_write) {
DBG_PRINTF("[CFG_SAVE] busy, skipped\r\n");
return;
}
if( m_config.magic_number != (uint32_t)CONFIG_MAGIC_NUMBER_VALUE )
{
m_config.magic_number = CONFIG_MAGIC_NUMBER_VALUE;
}
config_save_start:
fds_flag_write = true;
memset((char *)&desc, 0, sizeof(desc));
memset((char *)&tok, 0, sizeof(tok));
rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);
DBG_PRINTF("[CFG_SAVE] find rc=%u\r\n", rc);
if( rc == NRF_SUCCESS )
{
/* Write the updated record to flash. */
fds_flag_write = true;
rc = fds_record_update(&desc, &m_dummy_record);
if( (rc != NRF_SUCCESS) && (rc == FDS_ERR_NO_SPACE_IN_FLASH) )
DBG_PRINTF("[CFG_SAVE] update rc=%u\r\n", rc);
if( rc == FDS_ERR_NO_SPACE_IN_FLASH )
{
fds_flag_write = false;
rc = fds_gc();
APP_ERROR_CHECK(rc);
goto config_save_start;
DBG_PRINTF("[CFG_SAVE] gc rc=%u, retry\r\n", rc);
fds_flag_write = true;
rc = fds_record_update(&desc, &m_dummy_record);
DBG_PRINTF("[CFG_SAVE] retry rc=%u\r\n", rc);
}
else
if( rc != NRF_SUCCESS )
{
APP_ERROR_CHECK(rc);
DBG_PRINTF("[CFG_SAVE] FAIL rc=%u\r\n", rc);
fds_flag_write = false;
}
}
else
{
DBG_PRINTF("[CFG_SAVE] not found, writing new\r\n");
fds_flag_write = true;
rc = fds_record_write(&desc, &m_dummy_record);
APP_ERROR_CHECK(rc);
goto config_save_start;
DBG_PRINTF("[CFG_SAVE] write rc=%u\r\n", rc);
if( rc != NRF_SUCCESS )
{
DBG_PRINTF("[CFG_SAVE] FAIL rc=%u\r\n", rc);
fds_flag_write = false;
}
}
DBG_PRINTF("[CFG_SAVE] done\r\n");
}
@@ -359,12 +334,16 @@ void fs_storage_init(void)
/* Register first to receive an event when initialization is complete. */
rc = fds_register(fds_evt_handler);
APP_ERROR_CHECK(rc);
rc = fds_init();
APP_ERROR_CHECK(rc);
/* Wait for fds to initialize. */
wait_for_fds_ready();
fds_stat_t stat = { 0 };
rc = fds_stat(&stat);
APP_ERROR_CHECK(rc);
}
DBG_PRINTF("[FDS] OK\r\n");
}