/******************************************************************************* 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 #include #include #include #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)); }