diff --git a/project/ble_peripheral/ble_app_bladder_patch/command/cmd_common.h b/project/ble_peripheral/ble_app_bladder_patch/command/cmd_common.h index 7dc3276..a3d88fa 100644 --- a/project/ble_peripheral/ble_app_bladder_patch/command/cmd_common.h +++ b/project/ble_peripheral/ble_app_bladder_patch/command/cmd_common.h @@ -36,7 +36,6 @@ extern volatile bool data_tx_in_progress; /*------------------------------------------------------------------------------ * Device state / flags *----------------------------------------------------------------------------*/ -extern volatile bool processing; extern bool device_status; extern bool con_single; extern bool lock_check; diff --git a/project/ble_peripheral/ble_app_bladder_patch/main.c b/project/ble_peripheral/ble_app_bladder_patch/main.c index ba92006..4685fff 100644 --- a/project/ble_peripheral/ble_app_bladder_patch/main.c +++ b/project/ble_peripheral/ble_app_bladder_patch/main.c @@ -228,7 +228,6 @@ uint8_t m_encrypted_text2[AES_BLOCK_SIZE] = {0}; /* AES encryption result buf uint8_t m_decrypted_text[AES_BLOCK_SIZE] = {0}; /* AES decryption result buffer */ volatile uint8_t Sj_type; /* Command type identifier */ -volatile bool processing; /* Sensor data processing flag (prevents duplicate commands) */ bool power_off_duble_prohibit = false; /* Power-off double prevention flag */ volatile bool power_state = false; /* Power state tracking */ @@ -612,8 +611,7 @@ static void nus_data_handler(ble_nus_evt_t * p_evt) { static uint32_t last_update_tick = 0; uint32_t now_tick = app_timer_cnt_get(); - if (last_update_tick == 0 || - app_timer_cnt_diff_compute(now_tick, last_update_tick) >= APP_TIMER_TICKS(30000)) + if (last_update_tick == 0 || app_timer_cnt_diff_compute(now_tick, last_update_tick) >= APP_TIMER_TICKS(30000)) { ble_gap_conn_params_t conn_params; conn_params.min_conn_interval = MIN_CONN_INTERVAL; @@ -1043,7 +1041,7 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) * 0x3D MIC failure, 0x3E connection failed to be established * -> maintain unlimited advertising + block sleep entry */ - bool unintended_disc = (disc_reason == BLE_HCI_CONNECTION_TIMEOUT) || + bool unintended_disc = (disc_reason == BLE_HCI_CONNECTION_TIMEOUT) || (disc_reason == BLE_HCI_STATUS_CODE_LMP_RESPONSE_TIMEOUT) || (disc_reason == BLE_HCI_CONN_TERMINATED_DUE_TO_MIC_FAILURE)|| (disc_reason == BLE_HCI_CONN_FAILED_TO_BE_ESTABLISHED); @@ -1226,9 +1224,7 @@ void uart_event_handle(app_uart_evt_t * p_event) UNUSED_VARIABLE(app_uart_get(&data_array[index])); index++; - if ((data_array[index - 1] == '\n') || - (data_array[index - 1] == '\r') || - (index >= m_ble_nus_max_data_len)) + if ((data_array[index - 1] == '\n') || (data_array[index - 1] == '\r') || (index >= m_ble_nus_max_data_len)) { if (index > 1) { @@ -1595,12 +1591,19 @@ void dr_binary_tx_safe(uint8_t const *ble_bin_buff, uint16_t length) * Power Button State Machine (main_s) * * Called repeatedly by a 5ms single-shot timer (m_power_on_delay_timer). - * Action determined by cnt_s value at button release: - * - cnt_s < 150 (< 0.75s): short press -> power OFF - * - cnt_s >= 150 (~0.75s): normal boot sequence start + * + * [Boot phase] (booted == false) + * - cnt_s < 400 (< 2s): short press -> power OFF + * - cnt_s >= 400 (~2s): normal boot sequence start * - cnt_s > 1000 (~5s): factory reset (passkey reset + power OFF) * - m_reset_status == 2: reboot after software reset + * + * [Running phase] (booted == true) + * - Button pressed 2s (cnt_s >= 400): power OFF + * - Button released: reset counter, keep polling *============================================================================*/ +static bool booted = false; + static void main_s(void * p_context) { UNUSED_PARAMETER(p_context); @@ -1608,30 +1611,53 @@ static void main_s(void * p_context) bool button_released = nrf_gpio_pin_read(POWER_BUTTON); + /* ---- Running phase: post-boot button polling ---- */ + if (booted) + { + if (!button_released) + { + cnt_s++; + if (cnt_s == 400) /* 400 x 5ms = 2000ms = 2s */ + { + DBG_PRINTF("[BTN] Power OFF\r\n"); + led_set_state(LED_STATE_POWER_OFF); + go_device_power_off = true; + main_timer_start(); + return; + } + } + else + { + cnt_s = 0; /* Counter reset when button is not pressed */ + } + timers_start(); + return; + } + + /* ---- Boot phase ---- */ if (button_released) { - if ((cnt_s < 200) && (m_reset_status != 2)) + if ((cnt_s < 400) && (m_reset_status != 2)) { - DBG_PRINTF("[BTN] Short->OFF\r\n"); led_set_state(LED_STATE_OFF); power_control_handler(OFF); cnt_s = 0; } - else if (cnt_s > 1000) + else if (cnt_s > 2000) /* Bonding delete */ { - DBG_PRINTF("[BTN] Long->Reset\r\n"); + DBG_PRINTF("[BTN] Bonding Deleted\r\n"); power_control_handler(ON); nrf_delay_ms(100); bond_data_delete = true; m_config.bond_data_delete = (uint8_t)bond_data_delete; - const char pass_init[PASSKEY_LENGTH] = DEFAULT_PASSKEY; - memcpy(m_config.static_passkey, pass_init, PASSKEY_LENGTH); + //const char pass_init[PASSKEY_LENGTH] = DEFAULT_PASSKEY; + //memcpy(m_config.static_passkey, pass_init, PASSKEY_LENGTH); /* passkey reset */ config_save(); nrf_delay_ms(1000); go_device_power_off = true; main_timer_start(); } - else if (cnt_s > 200 || (m_reset_status == 2)) + else if (cnt_s > 400 || (m_reset_status == 2)) { DBG_PRINTF("[BTN] Boot (cnt=%d)\r\n", cnt_s); device_reset = false; @@ -1648,6 +1674,11 @@ static void main_s(void * p_context) DBG_PRINTF("[BOOT] ADV started\r\n"); m_reset_status = 1; DBG_PRINTF("[BOOT] Ready\r\n"); + + /* Boot complete -> switch to running phase */ + booted = true; + cnt_s = 0; + timers_start(); } } else @@ -1655,7 +1686,7 @@ static void main_s(void * p_context) cnt_s++; device_reset = false; - if (cnt_s == 200) + if (cnt_s == 400) { led_set_state(LED_STATE_POWER_ON); DBG_PRINTF("[BTN] 2.0s\r\n"); diff --git a/project/ble_peripheral/ble_app_bladder_patch/main.h b/project/ble_peripheral/ble_app_bladder_patch/main.h index d694fd5..dcd3d2f 100644 --- a/project/ble_peripheral/ble_app_bladder_patch/main.h +++ b/project/ble_peripheral/ble_app_bladder_patch/main.h @@ -156,7 +156,6 @@ void ascii_format_data(uint8_t *buffer, const char *tag, const char *data_ascii, *============================================================================*/ extern volatile bool data_tx_in_progress; /* BLE TX in progress flag */ extern volatile bool ble_connection_st; /* BLE connection state (0=disconnected, 1=connected) */ -extern volatile bool processing; /* Sensor data processing flag (prevents duplicate commands) */ /* 2026-03-17: Global variables moved from cmd_parse.c to main.c */ extern char SERIAL_NO[SERIAL_NO_LENGTH]; /* Serial number */ diff --git a/project/ble_peripheral/ble_app_bladder_patch/measurement/battery/battery_saadc.c b/project/ble_peripheral/ble_app_bladder_patch/measurement/battery/battery_saadc.c index 2f948ae..825dd3c 100644 --- a/project/ble_peripheral/ble_app_bladder_patch/measurement/battery/battery_saadc.c +++ b/project/ble_peripheral/ble_app_bladder_patch/measurement/battery/battery_saadc.c @@ -62,7 +62,6 @@ extern bool info4; extern char ble_tx_buffer[BLE_NUS_MAX_DATA_LEN]; extern bool go_device_power_off; -extern volatile bool processing; extern which_cmd_t cmd_type_t; extern uint8_t ble_bin_buffer[BLE_NUS_MAX_DATA_LEN]; @@ -137,7 +136,7 @@ void battery_event_handler( nrf_drv_saadc_evt_t const * p_event ) info_batt = batt_lvl_in_milli_volt_1; } - /* --- Normal mode: send rsn: BLE / UART response --- */ + /* --- Normal mode: send rsn: BLE response --- */ else { if (cmd_type_t == CMD_UART) @@ -206,16 +205,13 @@ void battery_loop(void * p_context) { UNUSED_PARAMETER(p_context); - if (processing == true || info4 == true) + if (info4 == true) { - processing = false ; return; } - else - { - low_battery_check = true; - battery_level_meas(); - } + + low_battery_check = true; + battery_level_meas(); } /* Start the periodic battery monitoring timer. */ diff --git a/project/ble_peripheral/ble_app_bladder_patch/system/main_timer.c b/project/ble_peripheral/ble_app_bladder_patch/system/main_timer.c index e8ec55e..920e676 100644 --- a/project/ble_peripheral/ble_app_bladder_patch/system/main_timer.c +++ b/project/ble_peripheral/ble_app_bladder_patch/system/main_timer.c @@ -152,28 +152,31 @@ void main_loop(void * p_context) /* For x ms */ * Continuous read mode. If not waiting for BLE TX (ble_got_new_data==false), * call icm42670_main() and restart timer after 10ms for repeated execution. */ - if(motion_raw_data_enabled == true) { + if (motion_raw_data_enabled == true) + { main_timer_stop(); /* Stop timer (prevent re-entry) */ - if(motion_data_once == true) + if (motion_data_once == true) { /* One-shot mode: init HW I2C then read IMU data once */ - hw_i2c_init_once(); /* Switch to HW TWI mode (400kHz) */ - icm42670_main(); /* Read and process IMU data */ - } - else{ + hw_i2c_init_once(); /* Switch to HW TWI mode (400kHz) */ + icm42670_main(); /* Read and process IMU data */ + } + else + { /* Continuous mode: repeat read if not waiting for BLE TX */ - if(ble_got_new_data==false){ - //for(uint16_t i=0 ; i<60 ;i++) - //{ - DBG_PRINTF("IMU \r\n"); + if (ble_got_new_data==false) + { + //for(uint16_t i=0 ; i<60 ;i++) + //{ + DBG_PRINTF("IMU \r\n"); - icm42670_main(); /* Read IMU data */ - motion_raw_data_enabled = true; /* Keep flag set (continuous read) */ - main_timer_start_ms(1000); /* Next IMU read after 1s */ - } -// else if(ble_got_new_data==true){ -// motion_data_once = true; -// } + icm42670_main(); /* Read IMU data */ + motion_raw_data_enabled = true; /* Keep flag set (continuous read) */ + main_timer_start_ms(1000); /* Next IMU read after 1s */ + } + // else if(ble_got_new_data==true){ + // motion_data_once = true; + // } } } @@ -184,7 +187,8 @@ void main_loop(void * p_context) /* For x ms */ * Called after IMU continuous read in info4 mode. * Timer remains stopped after measurement. */ - if(go_batt == true) { + if (go_batt == true) + { DBG_PRINTF("IMU BATT\r\n"); main_timer_stop(); /* Stop timer */ go_batt = false; /* Consume flag (one-shot) */ @@ -203,7 +207,8 @@ void main_loop(void * p_context) /* For x ms */ * After completion, sets motion_data_once=true so the next IMU read * uses one-shot mode (with HW I2C re-init). */ - if(go_temp == true) { + if (go_temp == true) + { DBG_PRINTF("IMU Temp\r\n"); main_timer_stop(); /* Stop timer */ // go_batt = false; @@ -218,21 +223,24 @@ void main_loop(void * p_context) /* For x ms */ /* ---- System control event handling ---- */ /* Device power OFF handling */ - if(go_device_power_off == true){ + if (go_device_power_off == true) + { main_timer_stop(); /* Stop timer */ DBG_PRINTF("Off main_timer\r\n"); device_power_off(); /* Execute device power OFF */ } /* Sleep mode entry handling */ - if(go_sleep_mode_enter == true){ + if (go_sleep_mode_enter == true) + { main_timer_stop(); /* Stop timer */ DBG_PRINTF("sleep main timer\r\n"); sleep_mode_enter(); /* Execute sleep mode entry */ } /* NVIC system reset handling */ - if(go_NVIC_SystemReset == true) { + if (go_NVIC_SystemReset == true) + { main_timer_stop(); /* Stop timer */ NVIC_SystemReset(); /* ARM Cortex-M4 system reset */ } diff --git a/project/ble_peripheral/ble_app_bladder_patch/system/power/power_control.c b/project/ble_peripheral/ble_app_bladder_patch/system/power/power_control.c index b357b22..721d529 100644 --- a/project/ble_peripheral/ble_app_bladder_patch/system/power/power_control.c +++ b/project/ble_peripheral/ble_app_bladder_patch/system/power/power_control.c @@ -48,26 +48,20 @@ APP_TIMER_DEF(m_power_timer_id); /* Power sequence current step (0: I2C init, 1: reserved, 2: complete) */ static uint8_t p_order; -/* Data processing flag (declared in external module) */ -extern volatile bool processing; - /* Power sequence lock flag (true = sequence in progress) */ bool lock_check = false; /** * @brief Enter device sleep mode * - * Clears the processing flag so the main loop stops - * processing sensor data. - * * @return 0 (always succeeds) */ -int device_sleep_mode(void){ +int device_sleep_mode(void) +{ int rc = 0; nrf_delay_ms(2); DBG_PRINTF("Device_Sleep_Mode OK!\r\n"); nrf_delay_ms(10); - processing = false; /* Clear processing flag -> stop sensor handling */ return rc; }