프로젝트 정리: 앱 내부 구조 수정

- measurement/hal/system/command
This commit is contained in:
2026-04-15 15:28:09 +09:00
parent e272347664
commit 95894eacd4
66 changed files with 16078 additions and 16089 deletions

View File

@@ -0,0 +1,33 @@
// file: debug_print.h
/*******************************************************************************
* [한국어 설명] 디버그 출력 매크로 (조건부 컴파일)
*
* SEGGER RTT(Real Time Transfer)를 이용한 디버그 출력 매크로.
* J-Link 디버거를 통해 실시간으로 로그를 PC에 전송한다.
* UART를 사용하지 않으므로 시스템 타이밍에 미치는 영향이 적다.
*
* === 조건부 컴파일 ===
* ENABLE_PRINTF = 1: DBG_PRINTF가 SEGGER_RTT_printf(채널0)로 치환됨
* -> 실제 로그 출력 (디버깅 시 사용)
* ENABLE_PRINTF = 0: DBG_PRINTF가 빈 매크로로 치환됨
* -> 코드에서 완전히 제거 (릴리스 빌드 시 사용)
*
* === 사용법 ===
* DBG_PRINTF("값: %d\r\n", value); // printf와 동일한 포맷 문자열
* 출력은 SEGGER RTT Viewer 또는 J-Link RTT Client에서 확인.
******************************************************************************/
#ifndef DEBUG_PRINT_H
#define DEBUG_PRINT_H
#define ENABLE_PRINTF 1 /* 1=디버그 출력 활성화, 0=전역 비활성화 */
#if ENABLE_PRINTF
#include "SEGGER_RTT.h"
/* SEGGER RTT 채널 0으로 포맷 문자열 출력 */
#define DBG_PRINTF(...) SEGGER_RTT_printf(0, __VA_ARGS__)
#else
/* 빈 매크로: 컴파일러가 호출 코드를 완전히 제거 */
#define DBG_PRINTF(...) // Do nothing
#endif
#endif // DEBUG_PRINT_H

View File

@@ -0,0 +1,292 @@
/*******************************************************************************
* @file led_control.c
* @brief LED 직접 제어 드라이버 (BSP 미사용)
* @date 2026-03-30
*
* app_timer 1개로 2색 LED(녹색/주황)의 복합 blink 패턴 구현
* 단순 on/off 상태는 타이머 없이 즉시 GPIO 제어
* 복합 패턴(에러 등)은 phase 기반 state machine으로 처리
******************************************************************************/
#include "led_control.h"
#include "nrf_gpio.h"
#include "app_timer.h"
/*==============================================================================
* 내부 상수
*============================================================================*/
#define MS_TO_TICKS(ms) APP_TIMER_TICKS(ms)
/*==============================================================================
* 색상
*============================================================================*/
#define COLOR_NONE 0
#define COLOR_GREEN 1
#define COLOR_ORANGE 2
/*==============================================================================
* 에러 패턴 상수 (No.7)
* 3Hz 깜빡 3회 = 166ms on + 166ms off × 3 = ~1초, 이후 꺼짐 1초
*============================================================================*/
#define ERROR_BLINK_ON_MS 166
#define ERROR_BLINK_OFF_MS 166
#define ERROR_BLINK_COUNT 3
#define ERROR_PAUSE_MS 1000
/*==============================================================================
* 패턴 테이블 (단순 blink 용)
*============================================================================*/
typedef struct
{
uint32_t on_ms; /* LED 켜짐 시간 (ms) */
uint32_t off_ms; /* LED 꺼짐 시간 (ms) */
uint8_t color; /* COLOR_GREEN 또는 COLOR_ORANGE */
bool repeat; /* true: 무한 반복, false: 1회 후 OFF */
} led_pattern_t;
static const led_pattern_t m_patterns[LED_STATE_COUNT] =
{
[LED_STATE_OFF] = { 0, 0, COLOR_NONE, false },
[LED_STATE_POWER_ON] = { 2000, 0, COLOR_GREEN, false }, /* 초록 점등 2초 → 유지 */
[LED_STATE_POWER_OFF] = { 2000, 0, COLOR_GREEN, false }, /* 초록 점등 2초 → OFF */
[LED_STATE_ADVERTISING] = { 500, 500, COLOR_GREEN, true }, /* 초록 점멸 1초 */
[LED_STATE_DETACH_WARNING] = { 1000, 3000, COLOR_GREEN, true }, /* 초록 1초 on / 3초 off */
[LED_STATE_ALIGN_SEARCHING] = { 1000, 1000, COLOR_ORANGE, true }, /* 주황 점멸 1초 */
[LED_STATE_ALIGN_COMPLETE] = { 3000, 1000, COLOR_GREEN, true }, /* 초록 3초 on / 1초 off */
[LED_STATE_ERROR] = { 0, 0, COLOR_ORANGE, true } /* 별도 state machine */
};
/*==============================================================================
* 모듈 변수
*============================================================================*/
APP_TIMER_DEF(m_led_timer);
static led_state_t m_current_state = LED_STATE_OFF;
static bool m_phase_on; /* true: LED 켜진 구간, false: 꺼진 구간 */
/* 에러 패턴 전용 */
static uint8_t m_error_blink_cnt; /* 현재까지 깜빡인 횟수 */
static uint8_t m_error_phase; /* 0: blink-on, 1: blink-off, 2: pause */
/*==============================================================================
* GPIO 헬퍼
*============================================================================*/
static inline void led_green_on(void)
{
#if LED_ACTIVE_LOW
nrf_gpio_pin_clear(LED_PIN_GREEN);
#else
nrf_gpio_pin_set(LED_PIN_GREEN);
#endif
}
static inline void led_green_off(void)
{
#if LED_ACTIVE_LOW
nrf_gpio_pin_set(LED_PIN_GREEN);
#else
nrf_gpio_pin_clear(LED_PIN_GREEN);
#endif
}
static inline void led_orange_on(void)
{
#if LED_ACTIVE_LOW
nrf_gpio_pin_clear(LED_PIN_ORANGE);
#else
nrf_gpio_pin_set(LED_PIN_ORANGE);
#endif
}
static inline void led_orange_off(void)
{
#if LED_ACTIVE_LOW
nrf_gpio_pin_set(LED_PIN_ORANGE);
#else
nrf_gpio_pin_clear(LED_PIN_ORANGE);
#endif
}
static void led_all_off(void)
{
led_green_off();
led_orange_off();
}
static void led_color_on(uint8_t color)
{
led_all_off();
if (color == COLOR_GREEN) led_green_on();
else if (color == COLOR_ORANGE) led_orange_on();
}
/*==============================================================================
* 타이머 시작 헬퍼
*============================================================================*/
static void timer_start_ms(uint32_t ms)
{
if (ms == 0) return;
app_timer_start(m_led_timer, MS_TO_TICKS(ms), NULL);
}
/*==============================================================================
* 에러 패턴 state machine (No.7)
* phase 0: LED ON (166ms) → phase 1
* phase 1: LED OFF (166ms) → cnt++ → cnt<3 ? phase 0 : phase 2
* phase 2: PAUSE (1000ms) → cnt=0, phase 0
*============================================================================*/
static void error_pattern_start(void)
{
m_error_blink_cnt = 0;
m_error_phase = 0;
led_color_on(COLOR_ORANGE);
timer_start_ms(ERROR_BLINK_ON_MS);
}
static void error_pattern_tick(void)
{
switch (m_error_phase)
{
case 0: /* ON 구간 끝 → OFF */
led_all_off();
m_error_phase = 1;
timer_start_ms(ERROR_BLINK_OFF_MS);
break;
case 1: /* OFF 구간 끝 */
m_error_blink_cnt++;
if (m_error_blink_cnt < ERROR_BLINK_COUNT)
{
/* 다시 ON */
m_error_phase = 0;
led_color_on(COLOR_ORANGE);
timer_start_ms(ERROR_BLINK_ON_MS);
}
else
{
/* 3회 완료 → pause */
m_error_phase = 2;
timer_start_ms(ERROR_PAUSE_MS);
}
break;
case 2: /* pause 끝 → 처음부터 */
m_error_blink_cnt = 0;
m_error_phase = 0;
led_color_on(COLOR_ORANGE);
timer_start_ms(ERROR_BLINK_ON_MS);
break;
default:
break;
}
}
/*==============================================================================
* 타이머 콜백
*============================================================================*/
static void led_timer_handler(void * p_context)
{
(void)p_context;
/* 에러 상태는 별도 처리 */
if (m_current_state == LED_STATE_ERROR)
{
error_pattern_tick();
return;
}
const led_pattern_t * p = &m_patterns[m_current_state];
if (m_phase_on)
{
/* ON→OFF 전환 */
led_all_off();
m_phase_on = false;
if (p->off_ms > 0)
{
timer_start_ms(p->off_ms);
}
else if (!p->repeat)
{
/* 1회성: off_ms == 0 이면 그냥 유지 (POWER_ON) 또는 끄기 (POWER_OFF) */
if (m_current_state == LED_STATE_POWER_OFF)
{
led_all_off();
m_current_state = LED_STATE_OFF;
}
/* POWER_ON: 점등 유지 상태 → 타이머 x */
}
}
else
{
/* OFF → ON 전환 */
if (p->repeat)
{
led_color_on(p->color);
m_phase_on = true;
timer_start_ms(p->on_ms);
}
/* repeat == false && off_ms > 0 인 경우는 현재 없음 */
}
}
/*==============================================================================
* 공개 함수
*============================================================================*/
void led_init(void)
{
/* GPIO 출력 설정 */
nrf_gpio_cfg_output(LED_PIN_GREEN);
nrf_gpio_cfg_output(LED_PIN_ORANGE);
led_all_off();
/* 타이머 생성 (single-shot) */
app_timer_create(&m_led_timer, APP_TIMER_MODE_SINGLE_SHOT, led_timer_handler);
m_current_state = LED_STATE_OFF;
}
void led_set_state(led_state_t state)
{
if (state >= LED_STATE_COUNT) return;
/* 이전 패턴 중단 */
app_timer_stop(m_led_timer);
led_all_off();
m_current_state = state;
m_phase_on = false;
const led_pattern_t * p = &m_patterns[state];
switch (state)
{
case LED_STATE_OFF:
/* 이미 all off */
break;
/* 에러 패턴: 별도 state machine */
case LED_STATE_ERROR:
error_pattern_start();
break;
/* 그 외: on 구간부터 시작 */
default:
led_color_on(p->color);
m_phase_on = true;
if (p->on_ms > 0)
{
timer_start_ms(p->on_ms);
}
break;
}
}
led_state_t led_get_state(void)
{
return m_current_state;
}

View File

@@ -0,0 +1,52 @@
/*******************************************************************************
* @file led_control.h
* @brief LED 직접 제어 드라이버 (BSP 미사용)
* @date 2026-03-30
*
* 녹색(P0.12) + 주황(P0.29) 2색 LED를 app_timer 기반으로 제어
* 각 상태별 on/off 시간, 색상, 반복 패턴을 테이블로 관리
******************************************************************************/
#ifndef LED_CONTROL_H__
#define LED_CONTROL_H__
#include <stdint.h>
#include <stdbool.h>
/*==============================================================================
* LED 핀 정의
*============================================================================*/
#define LED_PIN_GREEN NRF_GPIO_PIN_MAP(0, 12) /* 녹색 LED */
#define LED_PIN_ORANGE NRF_GPIO_PIN_MAP(0, 29) /* 주황 LED */
#define LED_ACTIVE_LOW 1 /* 1 = Active Low (GPIO LOW에서 LED 켜짐) */
/*==============================================================================
* LED 상태 열거형
*============================================================================*/
typedef enum
{
LED_STATE_OFF = 0, /* 모든 LED 소등 */
LED_STATE_POWER_ON, /* 1: 전원 ON - 초록 소등 → 녹색 점등 2초 */
LED_STATE_POWER_OFF, /* 2: 전원 OFF - 초록 점등 2초 → 소등 */
LED_STATE_ADVERTISING, /* 3: 블루투스 스캐닝 - 초록 점멸 1초 간격(반복) */
LED_STATE_DETACH_WARNING, /* 4: 정상 작동중(미부착 시) - 초록 점등 1초 / 소등 3초 (반복) */
LED_STATE_ALIGN_SEARCHING, /* 5: 정렬모드(탐지중) - 주황 점멸 1초 간격 (반복) */
LED_STATE_ALIGN_COMPLETE, /* 6: 정렬모드(탐지 완료) - 초록 점등 3초 / 소등 1초 */
LED_STATE_ERROR, /* 7: 오류 발생 - 주황 3Hz 깜빡 3회 / 꺼짐 1초 (반복) */
LED_STATE_COUNT /* 배열 크기 */
} led_state_t;
/*==============================================================================
* 공개 함수
*============================================================================*/
/** @brief LED GPIO 초기화 + 타이머 생성 - main()에서 1회 호출 */
void led_init(void);
/** @brief LED 상태 변경 - 이전 패턴을 즉시 중단하고 새 패턴 시작 */
void led_set_state(led_state_t state);
/** @brief 현재 LED 상태 조회 */
led_state_t led_get_state(void);
#endif /* LED_CONTROL_H__ */

View File

@@ -0,0 +1,280 @@
/*******************************************************************************
TEST medi50 Dec 23
*******************************************************************************
*
* [모듈 개요]
* 메인 이벤트 루프 타이머 모듈 (10ms 간격, 싱글샷 모드).
*
* 센서 데이터 수집 및 시스템 제어 이벤트를 플래그 기반으로 디스패치한다.
* app_timer 싱글샷 모드를 사용하므로, 이벤트 처리 완료 후
* 필요 시 main_timer_start()로 수동 재시작해야 한다.
*
* [이벤트 플래그 및 처리 순서]
* motion_raw_data_enabled → IMU 데이터 읽기 (icm42670_main 호출)
* - motion_data_once == true: 단발성 읽기 (HW I2C 초기화 후 1회)
* - motion_data_once == false: 연속 읽기 (BLE 전송 대기 중이 아닐 때)
* go_batt → 배터리 전압 측정 (battery_level_meas)
* go_temp → 온도 측정 (tmp235_voltage_level_meas)
* go_device_power_off → 디바이스 전원 OFF (device_power_off)
* go_sleep_mode_enter → 슬립 모드 진입 (sleep_mode_enter)
* go_NVIC_SystemReset → NVIC 시스템 리셋
*
* [info4 모드 측정 순서]
* IMU 연속 읽기 → go_batt(배터리) → go_temp(온도) → motion_data_once(IMU 단발)
* 온도 측정 완료 시 motion_data_once=true로 설정하여 다시 IMU로 돌아간다.
*
* [타이머 설정]
* - 일반 모드: 10ms 간격 (MAIN_LOOP_INTERVAL)
* - FEATURE_DETAIL_VALUE_FULL 모드: 80ms 간격 (디테일 프린트아웃용)
*
******************************************************************************/
#include "sdk_common.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "boards.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_util_platform.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_log.h"
#include "nrf_drv_gpiote.h"
#include "battery_saadc.h"
#include "app_timer.h"
#include "main.h"
#include "app_raw_main.h"
#include "main_timer.h"
#include "tmp235_q1.h"
//#include "fstorage.h"
#include "power_control.h"
#include "main.h" /* 2026-03-17: cmd_parse.h 삭제 → main.h */
#include "debug_print.h"
#include "i2c_manager.h" //add cj
/* 메인 루프 싱글샷 타이머 인스턴스 */
APP_TIMER_DEF(m_main_loop_timer_id);
#if FEATURE_DETAIL_VALUE_FULL
/* 디테일 프린트아웃 모드: 80ms 간격으로 메인 루프 실행 */
#define MAIN_LOOP_INTERVAL 80 /* 디테일 프린트아웃이 있을경우 Full_Mode Main Prosessing 수행하는 타이머 */
//extern bool pd_adc_full_a_start;
//extern bool pd_adc_full_b_start;
//extern bool pd_adc_full_c_start;
//extern bool pd_adc_full_d_start;
//extern bool pd_adc_full_end;
extern which_cmd_t cmd_type_t;
#else
/* 일반 모드: 10ms 간격으로 메인 루프 실행 */
#define MAIN_LOOP_INTERVAL 10
#endif
/* ========================================================================== */
/* 이벤트 플래그 (외부 모듈에서 설정, main_loop에서 처리) */
/* ========================================================================== */
bool go_batt= false; /* 배터리 측정 요청 플래그 */
bool go_temp= false; /* 온도 측정 요청 플래그 */
bool go_device_power_off = false; /* 디바이스 전원 OFF 요청 플래그 */
bool go_sleep_mode_enter = false; /* 슬립 모드 진입 요청 플래그 */
bool go_NVIC_SystemReset = false; /* 시스템 리셋 요청 플래그 */
bool motion_raw_data_enabled = false; /* IMU 모션 데이터 읽기 활성화 플래그 */
bool ble_got_new_data = false; /* BLE로 새 데이터 전송 완료 여부 */
bool motion_data_once = false; /* IMU 단발성 읽기 모드 (true: 1회만 읽기) */
/**
* @brief 메인 이벤트 루프 (싱글샷 타이머 콜백)
*
* 플래그 기반 이벤트 디스패처로, 설정된 플래그에 따라 해당 처리를 수행한다.
* 싱글샷 타이머이므로 연속 실행이 필요한 경우 처리 내부에서 main_timer_start()를
* 다시 호출하여 타이머를 재시작해야 한다.
*
* [처리 우선순위] (코드 순서대로 검사)
* 1. IMU 모션 데이터 (motion_raw_data_enabled)
* 2. 배터리 측정 (go_batt)
* 3. 온도 측정 (go_temp)
* 4. 전원 OFF (go_device_power_off)
* 5. 슬립 모드 (go_sleep_mode_enter)
* 6. 시스템 리셋 (go_NVIC_SystemReset)
*/
void main_loop(void * p_context) /* For x ms */
{
UNUSED_PARAMETER(p_context);
#if FEATURE_DETAIL_VALUE_FULL
// if(pd_adc_full_a_start == true) { // A mode
// main_timer_stop();
// printf("main_loop_A\r\n");
// full_adc_start();
// }else if(pd_adc_full_b_start == true) { // B mode
// main_timer_stop();
// printf("main_loop_B\r\n");
// full_adc_start();
// }else if(pd_adc_full_c_start == true) { // C mode
// main_timer_stop();
// printf("main_loop_C\r\n");
// full_adc_start();
// }else if(pd_adc_full_d_start == true) { // D mode
// main_timer_stop();
// printf("main_loop_D\r\n");
// full_adc_start();
// }else if(pd_adc_full_end == true) { // Completed
// pd_adc_full_end = false;
// main_timer_stop();
// printf("main_loop_END\r\n");
// if(cmd_type_t == CMD_BLE) {
// full_send_timer_start();
// }
// }
#endif
// For Motion Data Sampling
/* ---- IMU 모션 데이터 읽기 ---- */
/*
* motion_raw_data_enabled가 true이면 IMU(ICM42670P) 데이터를 읽는다.
*
* motion_data_once == true:
* 단발성 읽기 모드. HW I2C를 초기화한 후 icm42670_main()을 1회 호출.
* info4 모드에서 배터리/온도 측정 후 다시 IMU로 돌아올 때 사용.
*
* motion_data_once == false:
* 연속 읽기 모드. BLE 전송 대기 중(ble_got_new_data==false)이면
* icm42670_main()을 호출하고 10ms 후 타이머를 재시작하여 반복 실행.
*/
if(motion_raw_data_enabled == true) {
main_timer_stop(); /* 타이머 정지 (재진입 방지) */
if(motion_data_once == true)
{
/* 단발성 모드: HW I2C 초기화 후 IMU 데이터 1회 읽기 */
hw_i2c_init_once(); /* HW TWI 모드로 전환 (400kHz) */
icm42670_main(); /* IMU 데이터 읽기 및 처리 */
}
else{
/* 연속 모드: BLE 전송 대기 중이 아니면 반복 읽기 */
if(ble_got_new_data==false){
//for(uint16_t i=0 ; i<60 ;i++)
//{
DBG_PRINTF("IMU \r\n");
icm42670_main(); /* IMU 데이터 읽기 */
motion_raw_data_enabled = true; /* 플래그 유지 (연속 읽기) */
main_timer_start_ms(1000); /* 1초 후 다음 IMU 읽기 */
}
// else if(ble_got_new_data==true){
// motion_data_once = true;
// }
}
}
/* ---- 배터리 전압 측정 ---- */
/*
* go_batt 플래그가 true이면 배터리 레벨을 측정한다.
* info4 모드에서 IMU 연속 읽기 이후 호출되는 단계.
* 측정 완료 후 타이머가 정지된 상태로 유지된다.
*/
if(go_batt == true) {
DBG_PRINTF("IMU BATT\r\n");
main_timer_stop(); /* 타이머 정지 */
go_batt = false; /* 플래그 소비 (1회 실행) */
// go_temp = true;
battery_level_meas(); /* SAADC를 이용한 배터리 전압 측정 */
// nrf_delay_ms(20);
// m48_adc_start_init();
// main_timer_start();
}
/* ---- 온도 측정 ---- */
/*
* go_temp 플래그가 true이면 TMP235-Q1 센서로 온도를 측정한다.
* info4 모드에서 배터리 측정 이후 호출되는 단계.
* 측정 완료 후 motion_data_once=true로 설정하여
* 다음 IMU 읽기는 단발성 모드(HW I2C 재초기화)로 전환된다.
*/
if(go_temp == true) {
DBG_PRINTF("IMU Temp\r\n");
main_timer_stop(); /* 타이머 정지 */
// go_batt = false;
go_temp = false; /* 플래그 소비 (1회 실행) */
motion_data_once = true; /* 다음 IMU 읽기를 단발성 모드로 전환 */
tmp235_voltage_level_meas(); /* TMP235-Q1 온도 센서 전압 측정 */
// motion_raw_data_enabled = true;
// main_timer_start();
}
/* ---- 시스템 제어 이벤트 처리 ---- */
/* 디바이스 전원 OFF 처리 */
if(go_device_power_off == true){
main_timer_stop(); /* 타이머 정지 */
DBG_PRINTF("Off main_timer\r\n");
device_power_off(); /* 디바이스 전원 OFF 실행 */
}
/* 슬립 모드 진입 처리 */
if(go_sleep_mode_enter == true){
main_timer_stop(); /* 타이머 정지 */
DBG_PRINTF("sleep main timer\r\n");
sleep_mode_enter(); /* 슬립 모드 진입 실행 */
}
/* NVIC 시스템 리셋 처리 */
if(go_NVIC_SystemReset == true) {
main_timer_stop(); /* 타이머 정지 */
NVIC_SystemReset(); /* ARM Cortex-M4 시스템 리셋 */
}
}
/**
* @brief 메인 루프 타이머 시작
*
* 싱글샷 모드로 MAIN_LOOP_INTERVAL(10ms 또는 80ms) 후 main_loop()를 호출
*/
void main_timer_start(void)
{
APP_ERROR_CHECK(app_timer_start(m_main_loop_timer_id, APP_TIMER_TICKS(MAIN_LOOP_INTERVAL), NULL));
}
/**
* @brief 지정된 간격(ms)으로 메인 루프 타이머 시작
*
* IMU 연속 스트리밍 등 기본 간격과 다른 주기가 필요할 때 사용
*/
void main_timer_start_ms(uint32_t interval_ms)
{
APP_ERROR_CHECK(app_timer_start(m_main_loop_timer_id, APP_TIMER_TICKS(interval_ms), NULL));
}
/**
* @brief 메인 루프 타이머 정지
*/
void main_timer_stop(void)
{
APP_ERROR_CHECK(app_timer_stop(m_main_loop_timer_id));
}
/**
* @brief 메인 루프 타이머 초기화 (앱 시작 시 1회 호출)
*
* 싱글샷 모드 타이머를 생성하고, 콜백으로 main_loop()를 등록
* 싱글샷이므로 매 호출마다 main_timer_start()로 수동 재시작해야 함
*/
void main_timer_init(void)
{
APP_ERROR_CHECK(app_timer_create(&m_main_loop_timer_id, APP_TIMER_MODE_SINGLE_SHOT, main_loop));
}

View File

@@ -0,0 +1,37 @@
/*******************************************************************************
* @file timer_routine.h
* @author CandyPops Co.
* @version V1.0.0
* @date 2022-09-05
* @brief
*******************************************************************************
*
* [헤더 개요]
* 메인 이벤트 루프 타이머의 공용 인터페이스 헤더.
*
* 10ms(일반) 또는 80ms(디테일) 간격의 싱글샷 타이머를 사용하여
* main_loop() 콜백에서 센서 데이터 수집 및 시스템 제어를 수행한다.
*
* [주요 함수]
* main_timer_start() : 타이머 시작 (싱글샷, 수동 재시작 필요)
* main_timer_stop() : 타이머 정지
* main_timer_init() : 타이머 초기화 (앱 시작 시 1회 호출)
*
******************************************************************************/
#ifndef TIMER_ROUTINE_H__
#define TIMER_ROUTINE_H__
/** @brief 메인 루프 타이머 시작 (싱글샷, MAIN_LOOP_INTERVAL 후 main_loop 호출) */
void main_timer_start(void);
/** @brief 지정된 간격(ms)으로 메인 루프 타이머 시작 */
void main_timer_start_ms(uint32_t interval_ms);
/** @brief 메인 루프 타이머 정지 */
void main_timer_stop(void);
/** @brief 메인 루프 타이머 초기화 (앱 시작 시 1회, 싱글샷 모드로 생성) */
void main_timer_init(void);
#endif //TIMER_ROUTINE_H__

View File

@@ -0,0 +1,189 @@
/*******************************************************************************
TEST medi50 Dec 23
//=========power_control.c====================
*******************************************************************************
*
* [모듈 개요]
* 디바이스 전원 시퀀스를 관리하는 모듈 (전원 켜기 / 끄기 / 슬립).
*
* [전원 켜기 흐름]
* device_activated()
* → power_loop 타이머 시작 → 즉시 완료 (센서 초기화 불필요)
* → 센서(IMU)는 측정 명령 시 imu_read_direct()가 자체 처리
*
* [슬립 모드]
* device_sleep_mode()
* → processing 플래그 해제
*
* [재활성화]
* device_reactivated()
* → I2C 재초기화 후 전원 시퀀스를 처음부터 다시 시작
*
* [타이머]
* 싱글샷 모드 app_timer, 20ms 간격으로 power_loop 호출
*
******************************************************************************/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "power_control.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "app_timer.h"
#include "debug_print.h"
#include "i2c_manager.h"
/* 전원 시퀀스용 싱글샷 타이머 인스턴스 */
APP_TIMER_DEF(m_power_timer_id);
/* 전원 시퀀스 상태머신의 타이머 간격 (20ms) */
// 2025-12-08 change to #define POWER_LOOP_INTERVAL 30 -> 20
#define POWER_LOOP_INTERVAL 20
/* 전원 시퀀스 현재 단계 (0: I2C 초기화, 1: 예약, 2: 완료) */
static uint8_t p_order;
/* 데이터 처리 중 플래그 (외부 모듈에서 선언) */
extern volatile bool processing;
/* 전원 시퀀스 잠금 플래그 (true = 전원 시퀀스 진행 중) */
bool lock_check = false;
/**
* @brief 디바이스 슬립 모드 진입
*
* 데이터 처리 플래그(processing)를 해제하여
* 메인 루프가 더 이상 센서 데이터를 처리하지 않도록 한다.
*
* @return 0 (항상 성공)
*/
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; /* 데이터 처리 플래그 해제 → 센서 처리 중단 */
return rc;
}
/**
* @brief 디바이스 전원 켜기 (전원 시퀀스 시작)
*
* 전원 시퀀스 단계를 0으로 초기화하고, 잠금 플래그를 설정한 뒤
* 타이머를 시작하여 power_loop() 상태머신을 구동한다.
*
* @return 0 (항상 성공)
*/
int device_activated(void){
int rc = 0;
p_order = 0; /* 상태머신 시작 단계 (Step 0: I2C 초기화) */
lock_check =true; /* 전원 시퀀스 진행 중 잠금 */
power_timer_start(); /* 20ms 후 power_loop() 첫 호출 */
return rc;
}
/**
* @brief Power-up sequence state machine
*
* Executes hardware initialization steps
* Called by app_timer at POWER_LOOP_INTERVAL (20ms)
*
* Sequence:
* 0: I2C init
* 1: (reserved)
* 2: Complete
*/
/*
* 전원 시퀀스 상태머신 (20ms 싱글샷 타이머 콜백).
*
* [실행 흐름]
* 1) 타이머 콜백 진입 → 타이머 정지 (싱글샷이므로)
* 2) 현재 p_order에 해당하는 초기화 단계 실행
* 3) p_order < 2이면 다음 단계로 진행하고 타이머 재시작
* 4) p_order == 2이면 전원 시퀀스 완료
*
* 센서 초기화 불필요 — imu_read_direct()가 매 측정 시 자체 처리
*/
void power_loop(void *p_context)
{
UNUSED_PARAMETER(p_context);
power_timer_stop(); /* 현재 타이머 정지 (싱글샷 → 수동 재시작 필요) */
/* 센서 초기화 불필요 — imu_read_direct()가 매 측정 시 자체 처리 */
/* 추후 전원 시퀀스 단계가 필요하면 여기에 switch(p_order) 추가 */
p_order = 2;
/* Advance to next step or finish */
/* 다음 단계로 진행하거나, 시퀀스 완료 처리 */
if (p_order < 2) {
p_order++; /* 다음 단계로 이동 */
power_timer_start(); /* 20ms 후 다음 단계 실행 */
} else {
/* 전원 시퀀스 전체 완료 */
DBG_PRINTF("[PWR] Device Activated OK!\r\n");
}
}
/**
* @brief 디바이스 재활성화 (슬립 복귀 시)
*
* I2C를 재초기화하고, 전원 시퀀스를 처음부터 다시 시작한다.
* 슬립 모드에서 깨어날 때 호출된다.
*
* @return 0 (항상 성공)
*/
int device_reactivated(void){
int rc = 0;
sw_i2c_init_once(); /* I2C 버스 재초기화 */
nrf_delay_ms(10); /* 안정화 대기 */
lock_check = true; /* 전원 시퀀스 진행 중 잠금 */
p_order = 0; /* 상태머신을 Step 0부터 재시작 */
power_timer_start(); /* 20ms 후 power_loop() 시작 */
return rc;
}
/**
* @brief 전원 시퀀스 타이머 시작
*
* 싱글샷 모드로 POWER_LOOP_INTERVAL(20ms) 후 power_loop()를 호출한다.
*/
void power_timer_start(void)
{
APP_ERROR_CHECK(app_timer_start(m_power_timer_id, APP_TIMER_TICKS(POWER_LOOP_INTERVAL), NULL));
}
/**
* @brief 전원 시퀀스 타이머 정지
*/
void power_timer_stop(void)
{
APP_ERROR_CHECK(app_timer_stop(m_power_timer_id));
}
/**
* @brief 전원 시퀀스 타이머 초기화 (앱 시작 시 1회 호출)
*
* 싱글샷 모드 타이머를 생성하고, 콜백으로 power_loop()를 등록한다.
* 싱글샷이므로 매 단계마다 power_timer_start()로 수동 재시작해야 한다.
*/
void power_timer_init(void) //active start
{
APP_ERROR_CHECK(app_timer_create(&m_power_timer_id, APP_TIMER_MODE_SINGLE_SHOT, power_loop));
// 2025-12-08 change to APP_TIMER_MODE_REPEATED mode
//APP_ERROR_CHECK(app_timer_create(&m_power_timer_id, APP_TIMER_MODE_REPEATED, power_loop));
}

View File

@@ -0,0 +1,48 @@
/*******************************************************************************
* @file power_control.h
* @author CandyPops Co.
* @version V1.0.0
* @date 2022-09-05
* @brief
*******************************************************************************
*
* [헤더 개요]
* 디바이스 전원 시퀀스 관리 모듈의 공용 인터페이스 헤더.
*
* [주요 함수 요약]
* device_activated() : 전원 켜기 → power_loop 상태머신 시작
* device_sleep_mode() : 슬립 모드 진입 (처리 중단)
* device_reactivated() : 슬립 복귀 → 전원 시퀀스 재시작
* power_loop() : 20ms 간격 전원 시퀀스 상태머신 콜백
* power_timer_start/stop : 전원 시퀀스 타이머 제어
* power_timer_init() : 전원 시퀀스 타이머 초기화 (앱 시작 시 1회)
*
******************************************************************************/
#ifndef _POWER_CONTROL_H_
#define _POWER_CONTROL_H_
#include "main.h"
/** @brief 디바이스 슬립 모드 진입 (processing 해제) */
int device_sleep_mode(void);
/** @brief 디바이스 전원 켜기 (전원 시퀀스 상태머신 시작) */
int device_activated(void);
/** @brief 디바이스 재활성화 (슬립 복귀 시 I2C 재초기화 후 시퀀스 재시작) */
int device_reactivated(void);
/** @brief 전원 시퀀스 상태머신 (20ms 타이머 콜백, Step 0→1→2) */
void power_loop(void * p_context); /* For x ms */
/** @brief 전원 시퀀스 타이머 시작 (20ms 싱글샷) */
void power_timer_start(void);
/** @brief 전원 시퀀스 타이머 정지 */
void power_timer_stop(void);;
/** @brief 전원 시퀀스 타이머 초기화 (앱 시작 시 1회 호출) */
void power_timer_init(void);
#endif //_POWER_CONTROL_H_