전원 버튼 BSP 제거, GPIO 제어로 전환

- 전원 OFF 로직 추가(main_s)
- processing 변수 삭제
This commit is contained in:
2026-04-16 16:55:59 +09:00
parent 742681554e
commit 24a4be94df
6 changed files with 86 additions and 59 deletions

View File

@@ -36,7 +36,6 @@ extern volatile bool data_tx_in_progress;
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Device state / flags * Device state / flags
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
extern volatile bool processing;
extern bool device_status; extern bool device_status;
extern bool con_single; extern bool con_single;
extern bool lock_check; extern bool lock_check;

View File

@@ -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 */ uint8_t m_decrypted_text[AES_BLOCK_SIZE] = {0}; /* AES decryption result buffer */
volatile uint8_t Sj_type; /* Command type identifier */ 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 */ bool power_off_duble_prohibit = false; /* Power-off double prevention flag */
volatile bool power_state = false; /* Power state tracking */ 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; static uint32_t last_update_tick = 0;
uint32_t now_tick = app_timer_cnt_get(); uint32_t now_tick = app_timer_cnt_get();
if (last_update_tick == 0 || if (last_update_tick == 0 || app_timer_cnt_diff_compute(now_tick, last_update_tick) >= APP_TIMER_TICKS(30000))
app_timer_cnt_diff_compute(now_tick, last_update_tick) >= APP_TIMER_TICKS(30000))
{ {
ble_gap_conn_params_t conn_params; ble_gap_conn_params_t conn_params;
conn_params.min_conn_interval = MIN_CONN_INTERVAL; 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 * 0x3D MIC failure, 0x3E connection failed to be established
* -> maintain unlimited advertising + block sleep entry * -> 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_STATUS_CODE_LMP_RESPONSE_TIMEOUT) ||
(disc_reason == BLE_HCI_CONN_TERMINATED_DUE_TO_MIC_FAILURE)|| (disc_reason == BLE_HCI_CONN_TERMINATED_DUE_TO_MIC_FAILURE)||
(disc_reason == BLE_HCI_CONN_FAILED_TO_BE_ESTABLISHED); (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])); UNUSED_VARIABLE(app_uart_get(&data_array[index]));
index++; index++;
if ((data_array[index - 1] == '\n') || if ((data_array[index - 1] == '\n') || (data_array[index - 1] == '\r') || (index >= m_ble_nus_max_data_len))
(data_array[index - 1] == '\r') ||
(index >= m_ble_nus_max_data_len))
{ {
if (index > 1) 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) * Power Button State Machine (main_s)
* *
* Called repeatedly by a 5ms single-shot timer (m_power_on_delay_timer). * 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 * [Boot phase] (booted == false)
* - cnt_s >= 150 (~0.75s): normal boot sequence start * - 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) * - cnt_s > 1000 (~5s): factory reset (passkey reset + power OFF)
* - m_reset_status == 2: reboot after software reset * - 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) static void main_s(void * p_context)
{ {
UNUSED_PARAMETER(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); 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 (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); led_set_state(LED_STATE_OFF);
power_control_handler(OFF); power_control_handler(OFF);
cnt_s = 0; 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); power_control_handler(ON);
nrf_delay_ms(100); nrf_delay_ms(100);
bond_data_delete = true; bond_data_delete = true;
m_config.bond_data_delete = (uint8_t)bond_data_delete; m_config.bond_data_delete = (uint8_t)bond_data_delete;
const char pass_init[PASSKEY_LENGTH] = DEFAULT_PASSKEY; //const char pass_init[PASSKEY_LENGTH] = DEFAULT_PASSKEY;
memcpy(m_config.static_passkey, pass_init, PASSKEY_LENGTH); //memcpy(m_config.static_passkey, pass_init, PASSKEY_LENGTH); /* passkey reset */
config_save(); config_save();
nrf_delay_ms(1000); nrf_delay_ms(1000);
go_device_power_off = true; go_device_power_off = true;
main_timer_start(); 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); DBG_PRINTF("[BTN] Boot (cnt=%d)\r\n", cnt_s);
device_reset = false; device_reset = false;
@@ -1648,6 +1674,11 @@ static void main_s(void * p_context)
DBG_PRINTF("[BOOT] ADV started\r\n"); DBG_PRINTF("[BOOT] ADV started\r\n");
m_reset_status = 1; m_reset_status = 1;
DBG_PRINTF("[BOOT] Ready\r\n"); DBG_PRINTF("[BOOT] Ready\r\n");
/* Boot complete -> switch to running phase */
booted = true;
cnt_s = 0;
timers_start();
} }
} }
else else
@@ -1655,7 +1686,7 @@ static void main_s(void * p_context)
cnt_s++; cnt_s++;
device_reset = false; device_reset = false;
if (cnt_s == 200) if (cnt_s == 400)
{ {
led_set_state(LED_STATE_POWER_ON); led_set_state(LED_STATE_POWER_ON);
DBG_PRINTF("[BTN] 2.0s\r\n"); DBG_PRINTF("[BTN] 2.0s\r\n");

View File

@@ -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 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 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 */ /* 2026-03-17: Global variables moved from cmd_parse.c to main.c */
extern char SERIAL_NO[SERIAL_NO_LENGTH]; /* Serial number */ extern char SERIAL_NO[SERIAL_NO_LENGTH]; /* Serial number */

View File

@@ -62,7 +62,6 @@ extern bool info4;
extern char ble_tx_buffer[BLE_NUS_MAX_DATA_LEN]; extern char ble_tx_buffer[BLE_NUS_MAX_DATA_LEN];
extern bool go_device_power_off; extern bool go_device_power_off;
extern volatile bool processing;
extern which_cmd_t cmd_type_t; extern which_cmd_t cmd_type_t;
extern uint8_t ble_bin_buffer[BLE_NUS_MAX_DATA_LEN]; 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; info_batt = batt_lvl_in_milli_volt_1;
} }
/* --- Normal mode: send rsn: BLE / UART response --- */ /* --- Normal mode: send rsn: BLE response --- */
else else
{ {
if (cmd_type_t == CMD_UART) if (cmd_type_t == CMD_UART)
@@ -206,16 +205,13 @@ void battery_loop(void * p_context)
{ {
UNUSED_PARAMETER(p_context); UNUSED_PARAMETER(p_context);
if (processing == true || info4 == true) if (info4 == true)
{ {
processing = false ;
return; return;
} }
else
{ low_battery_check = true;
low_battery_check = true; battery_level_meas();
battery_level_meas();
}
} }
/* Start the periodic battery monitoring timer. */ /* Start the periodic battery monitoring timer. */

View File

@@ -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), * 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. * 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) */ 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 */ /* One-shot mode: init HW I2C then read IMU data once */
hw_i2c_init_once(); /* Switch to HW TWI mode (400kHz) */ hw_i2c_init_once(); /* Switch to HW TWI mode (400kHz) */
icm42670_main(); /* Read and process IMU data */ icm42670_main(); /* Read and process IMU data */
} }
else{ else
{
/* Continuous mode: repeat read if not waiting for BLE TX */ /* Continuous mode: repeat read if not waiting for BLE TX */
if(ble_got_new_data==false){ if (ble_got_new_data==false)
//for(uint16_t i=0 ; i<60 ;i++) {
//{ //for(uint16_t i=0 ; i<60 ;i++)
DBG_PRINTF("IMU \r\n"); //{
DBG_PRINTF("IMU \r\n");
icm42670_main(); /* Read IMU data */ icm42670_main(); /* Read IMU data */
motion_raw_data_enabled = true; /* Keep flag set (continuous read) */ motion_raw_data_enabled = true; /* Keep flag set (continuous read) */
main_timer_start_ms(1000); /* Next IMU read after 1s */ main_timer_start_ms(1000); /* Next IMU read after 1s */
} }
// else if(ble_got_new_data==true){ // else if(ble_got_new_data==true){
// motion_data_once = 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. * Called after IMU continuous read in info4 mode.
* Timer remains stopped after measurement. * Timer remains stopped after measurement.
*/ */
if(go_batt == true) { if (go_batt == true)
{
DBG_PRINTF("IMU BATT\r\n"); DBG_PRINTF("IMU BATT\r\n");
main_timer_stop(); /* Stop timer */ main_timer_stop(); /* Stop timer */
go_batt = false; /* Consume flag (one-shot) */ 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 * After completion, sets motion_data_once=true so the next IMU read
* uses one-shot mode (with HW I2C re-init). * uses one-shot mode (with HW I2C re-init).
*/ */
if(go_temp == true) { if (go_temp == true)
{
DBG_PRINTF("IMU Temp\r\n"); DBG_PRINTF("IMU Temp\r\n");
main_timer_stop(); /* Stop timer */ main_timer_stop(); /* Stop timer */
// go_batt = false; // go_batt = false;
@@ -218,21 +223,24 @@ void main_loop(void * p_context) /* For x ms */
/* ---- System control event handling ---- */ /* ---- System control event handling ---- */
/* Device power OFF handling */ /* Device power OFF handling */
if(go_device_power_off == true){ if (go_device_power_off == true)
{
main_timer_stop(); /* Stop timer */ main_timer_stop(); /* Stop timer */
DBG_PRINTF("Off main_timer\r\n"); DBG_PRINTF("Off main_timer\r\n");
device_power_off(); /* Execute device power OFF */ device_power_off(); /* Execute device power OFF */
} }
/* Sleep mode entry handling */ /* Sleep mode entry handling */
if(go_sleep_mode_enter == true){ if (go_sleep_mode_enter == true)
{
main_timer_stop(); /* Stop timer */ main_timer_stop(); /* Stop timer */
DBG_PRINTF("sleep main timer\r\n"); DBG_PRINTF("sleep main timer\r\n");
sleep_mode_enter(); /* Execute sleep mode entry */ sleep_mode_enter(); /* Execute sleep mode entry */
} }
/* NVIC system reset handling */ /* NVIC system reset handling */
if(go_NVIC_SystemReset == true) { if (go_NVIC_SystemReset == true)
{
main_timer_stop(); /* Stop timer */ main_timer_stop(); /* Stop timer */
NVIC_SystemReset(); /* ARM Cortex-M4 system reset */ NVIC_SystemReset(); /* ARM Cortex-M4 system reset */
} }

View File

@@ -48,26 +48,20 @@ APP_TIMER_DEF(m_power_timer_id);
/* Power sequence current step (0: I2C init, 1: reserved, 2: complete) */ /* Power sequence current step (0: I2C init, 1: reserved, 2: complete) */
static uint8_t p_order; static uint8_t p_order;
/* Data processing flag (declared in external module) */
extern volatile bool processing;
/* Power sequence lock flag (true = sequence in progress) */ /* Power sequence lock flag (true = sequence in progress) */
bool lock_check = false; bool lock_check = false;
/** /**
* @brief Enter device sleep mode * @brief Enter device sleep mode
* *
* Clears the processing flag so the main loop stops
* processing sensor data.
*
* @return 0 (always succeeds) * @return 0 (always succeeds)
*/ */
int device_sleep_mode(void){ int device_sleep_mode(void)
{
int rc = 0; int rc = 0;
nrf_delay_ms(2); nrf_delay_ms(2);
DBG_PRINTF("Device_Sleep_Mode OK!\r\n"); DBG_PRINTF("Device_Sleep_Mode OK!\r\n");
nrf_delay_ms(10); nrf_delay_ms(10);
processing = false; /* Clear processing flag -> stop sensor handling */
return rc; return rc;
} }