전원 버튼 롱프레스 본드 삭제

- 전원 켜진 상태에서 15초 이상 전원 버튼 누르고 있는 경우 본드 삭제
- system workqueue 사용
This commit is contained in:
2026-07-10 17:14:49 +09:00
parent 1d11758162
commit 4c9a89463d
+59 -3
View File
@@ -6,9 +6,14 @@
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/dfu/mcuboot.h>
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
#include <hal/nrf_power.h>
#if IS_ENABLED(CONFIG_BT_SMP)
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#endif
#include "main.h"
#include "debug_print.h"
@@ -43,6 +48,7 @@ LOG_MODULE_REGISTER(vesiscan, LOG_LEVEL_INF);
/* 전원 버튼 상태머신 (5ms 폴링) */
#define BOOT_THRESHOLD 300 // 5ms x 400 = 2초 -> 초기화 시간 고려 체감상 2초에 맞춤
#define BOND_RESET_THRESHOLD (15000 / POWER_ON_DELAY)
/* 전원 제어 GPIO */
static const struct gpio_dt_spec power_hold = GPIO_DT_SPEC_GET(POWER_HOLD_NODE, gpios);
@@ -78,8 +84,10 @@ static bool dfu_reset_resume_request; // DFU 완료 리셋 후 자동 복
static uint32_t boot_reset_reason; // RESETREAS 원본 값
static struct k_work adv_start_work; // advertising 시작 work
static struct k_work adv_stop_work; // advertising 중지 work
static struct k_work bond_reset_work; // 15s power-button bond reset work
static bool dfu_led_active; // DFU 업로드 중 초록 LED ON
static const char *power_off_pending_reason = "none";
static bool bond_reset_pending;
static enum mgmt_cb_return dfu_status_cb(uint32_t event,
enum mgmt_cb_return prev_status,
@@ -170,6 +178,34 @@ static void adv_stop_work_handler(struct k_work *work)
ble_advertising_stop();
}
static void bond_reset_work_handler(struct k_work *work)
{
ARG_UNUSED(work);
DBG_ERR("[BTN] 15s long press -> BLE bond reset\r\n");
battery_timer_stop();
ble_advertising_stop();
#if IS_ENABLED(CONFIG_BT_SMP)
int err = bt_unpair(BT_ID_DEFAULT, NULL);
if (err)
{
DBG_ERR("[BTN] bt_unpair failed err=%d\r\n", err);
}
else
{
bond_data_delete = true;
DBG_PRINTF("[BTN] BLE bond data deleted\r\n");
}
#else
bond_data_delete = true;
DBG_PRINTF("[BTN] BLE bond delete skipped (BT_SMP disabled)\r\n");
#endif
k_msleep(100);
sys_reboot(SYS_REBOOT_COLD);
}
/* BLE RX 콜백 */
static void ble_rx_handler(const uint8_t *data, uint16_t len)
{
@@ -413,7 +449,6 @@ static void main_s(struct k_timer *timer)
if (cnt_s == BOOT_THRESHOLD) // 2초 도달: 래치 + 부팅 완료
{
device_on = true;
cnt_s = 0; // 카운터 리셋: 안 하면 다음 틱에서 ON→OFF 분기가 cnt_s >= 200 조건을 즉시 만족하여 전원 OFF됨
power_control_handler(ON, "button-2s-latch");
led_set_state(LED_STATE_ADVERTISING);
k_work_submit(&adv_start_work);
@@ -425,18 +460,39 @@ static void main_s(struct k_timer *timer)
/* 전원 OFF 시퀀스 (ON → OFF) */
else
{
if (!boot_btn_released) // 부팅 시 눌렀던 버튼 아직 안 놓음 → 대기
if (!boot_btn_released) // 부팅 때 누른 버튼 아직 안 놓였으면 대기
{
if (!button_pressed)
{
boot_btn_released = true;
cnt_s = 0;
}
else
{
cnt_s++;
if ((cnt_s >= BOND_RESET_THRESHOLD) && !bond_reset_pending)
{
bond_reset_pending = true;
k_work_submit(&bond_reset_work);
return;
}
}
}
else if (button_pressed) // 버튼 새로 누르고 있음
{
cnt_s++;
if (cnt_s >= BOOT_THRESHOLD) // 2초 이상 → 전원 OFF
if ((cnt_s >= BOND_RESET_THRESHOLD) && !bond_reset_pending)
{
bond_reset_pending = true;
k_work_submit(&bond_reset_work);
return;
}
}
else // 버튼 놓음 -> 카운터 리셋
{
if (cnt_s >= BOOT_THRESHOLD)
{
battery_timer_stop();
k_work_submit(&adv_stop_work);