전원 버튼 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

@@ -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");