코드 정리

- 주석 영문으로 변경
- Allman 스타일로 통일
This commit is contained in:
2026-04-16 12:01:51 +09:00
parent c98d9ae14e
commit 2861cb9815
35 changed files with 2406 additions and 2557 deletions

View File

@@ -1,27 +1,14 @@
/*******************************************************************************
* @file tmp235_q1.c
* @author CandyPops Co.
* @version V1.0.0
* @date 2022-09-05
* @brief
******************************************************************************/
/*******************************************************************************
* [모듈 개요] TMP235-Q1 아날로그 온도센서 드라이버
/*==============================================================================
* tmp235_q1.c - TMP235-Q1 analogue temperature sensor driver
*
* TMP235-Q1은 온도에 비례하는 아날로그 전압(Vout)을 출력하는 센서
* nRF52840 SAADC의 AIN3 채널로 Vout을 읽고, mV로 변환한 뒤 온도(°C)로 계산
* Reads the TMP235-Q1 analogue output via SAADC AIN3 and converts to deg C.
*
* 온도 계산 공식 (구간별 선형 보간):
* - Vout <= 1500mV (0~100°C): Ta = (Vout - 500) / 10.0
* - Vout <= 1750mV (100~125°C): Ta = (Vout - 1500) / 10.1 + 100
* - Vout <= 2000mV (125~150°C): Ta = (Vout - 1752.5) / 10.6 + 125
* - Vout > 2000mV: 오류 (150°C 초과)
*
* info4 모드(전체 센서 수집) 동작 순서:
* 배터리(go_batt) → 온도(go_temp) → IMU(motion_raw_data_enabled)
* 온도 측정 완료 시 go_temp=false, motion_raw_data_enabled=true 로 전환
******************************************************************************/
* Temperature conversion (piecewise linear, per datasheet):
* Vout <= 1500 mV (0..100 C) : Ta = (Vout - 500) / 10.0
* Vout <= 1750 mV (100..125 C): Ta = (Vout - 1500) / 10.1 + 100
* Vout <= 2000 mV (125..150 C): Ta = (Vout - 1752.5) / 10.6 + 125
* Vout > 2000 mV : out of sensor range
*============================================================================*/
#include "sdk_common.h"
@@ -36,181 +23,138 @@
#include "ble_nus.h"
#include "tmp235_q1.h"
#include "main.h"
/* 2026-03-17: cmd_parse.h 삭제 — main.h는 이미 포함됨 */
#include "main_timer.h"
#include "debug_print.h"
/* SAADC 내부 기준전압 600mV (부동소수점) */
#define TMP235_REF_VOLTAGE_IN_MILLIVOLTS 600.0f /**< Reference voltage (in milli volts) used by ADC while doing conversion. */
/* 1/3 프리스케일링 보상 계수 x6 (부동소수점) */
#define TMP235_PRE_SCALING_COMPENSATION 6.0f /**< The ADC is configured to use VDD with 1/3 prescaling as input. And hence the result of conversion is to be multiplied by 3 to get the actual value of the battery voltage.*/
/* 12비트 ADC 최대값 4096 (부동소수점, 분해능 기준) */
#define TMP235_ADC_RES_12BITS 4096.0f /**< Maximum digital value for 12-bit ADC conversion. */
/**@brief Macro to convert the result of ADC conversion in millivolts.
*
* @param[in] ADC_VALUE ADC result.
*
* @retval Result converted to millivolts.
*/
/* ADC 원시값 → TMP235 출력전압(mV) 변환 매크로: ADC x (600/4096) x 6 */
/* SAADC internal reference (mV) */
#define TMP235_REF_VOLTAGE_IN_MILLIVOLTS 600.0f
/* 1/3 prescaling compensation (x6) */
#define TMP235_PRE_SCALING_COMPENSATION 6.0f
/* 12-bit ADC full scale */
#define TMP235_ADC_RES_12BITS 4096.0f
/* Convert raw ADC value to TMP235 output voltage (mV) */
#define TMP235_VOUT_IN_MILLI_VOLTS(ADC_VALUE)\
((((ADC_VALUE) * TMP235_REF_VOLTAGE_IN_MILLIVOLTS) / TMP235_ADC_RES_12BITS) * TMP235_PRE_SCALING_COMPENSATION)
/* SAADC 변환 결과 저장 버퍼 (1채널) */
static nrf_saadc_value_t adc_buf;
extern char ble_tx_buffer[BLE_NUS_MAX_DATA_LEN];
extern uint8_t ble_bin_buffer[BLE_NUS_MAX_DATA_LEN] ;
extern char ble_tx_buffer[BLE_NUS_MAX_DATA_LEN];
extern uint8_t ble_bin_buffer[BLE_NUS_MAX_DATA_LEN];
/* 현재 명령 소스: CMD_UART 또는 CMD_BLE */
extern which_cmd_t cmd_type_t;
extern bool info4;
extern bool go_temp;
/* info4: 전체 센서 데이터 수집 모드 플래그 */
extern bool info4; // main.c
/* info4 mode: cached temperature (deg C x 100, integer) */
volatile uint16_t info_temp;
extern bool motion_raw_data_enabled;
/* 온도 측정 순서 제어 플래그 */
extern bool go_temp; // main_timer.c
/* info4 모드에서 온도값 임시 저장 (°C x 100, 정수 표현) */
volatile uint16_t info_temp; //48_C
extern bool motion_raw_data_enabled;
/* SAADC 완료 플래그 — all_sensors()에서 콜백 완료 대기용 */
/* SAADC completion flag — used by all_sensors() to wait */
volatile bool tmp235_saadc_done = false;
/**@brief Function for handling the ADC interrupt.
*
* @details This function will fetch the conversion result from the ADC, convert the value into
* percentage and send it to peer.
*/
/**
* @brief TMP235 온도센서 ADC 완료 콜백
/*==============================================================================
* tmp235_voltage_handler - SAADC conversion complete callback
*
* SAADC 변환 완료 시 호출된다.
* ADC값 → Vout(mV) → 온도(°C) 순으로 변환하고, 동작 모드에 따라:
* - info4 모드: info_temp에 저장 (°C x 100 정수), 이후 IMU 측정으로 전환
* - 일반 모드: BLE("rso:" 바이너리) 또는 UART로 온도값 전송
*/
void tmp235_voltage_handler(nrf_drv_saadc_evt_t const * p_event) /* TMP325 Vout reading */
* ADC value -> Vout (mV) -> temperature (deg C), then:
* - info4 mode: store to info_temp (C x 100 integer)
* - Normal mode: send rso: response over BLE or UART
*============================================================================*/
void tmp235_voltage_handler(nrf_drv_saadc_evt_t const * p_event)
{
float led_temp; /* 계산된 온도 (°C, 부동소수점) */
float led_temp_16; /* BLE 전송용 온도 (°C x 100, 부동소수점) */
float led_temp;
float led_temp_16;
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
nrf_saadc_value_t adc_result;
float tmp235_voltage_in_milli_volts = 0;
float tmp235_voltage_in_milli_volts = 0;
/* ADC 변환 결과 읽기 */
adc_result = p_event->data.done.p_buffer[0];
//DBG_PRINTF("[TMP] adc=%d\r\n", adc_result);
/* SAADC 해제 — 배터리/압력센서 측정과 하드웨어 공유 */
nrf_drv_saadc_channel_uninit(0); // 채널 먼저 해제
nrf_drv_saadc_uninit();
//nrf_drv_saadc_uninit(); // 이전: 드라이버 먼저 해제
//nrf_drv_saadc_channel_uninit(0);
/* Release SAADC — shared with battery / pressure ADC */
nrf_drv_saadc_channel_uninit(0);
nrf_drv_saadc_uninit();
/* ADC값 → TMP235 출력전압(mV) 변환 */
/* ADC -> TMP235 output voltage (mV) */
tmp235_voltage_in_milli_volts = TMP235_VOUT_IN_MILLI_VOLTS(adc_result);
/*
* Vout → 온도(°C) 변환 (구간별 선형 보간)
* TMP235 데이터시트 기반:
* 0~100°C 구간: 기울기 10.0 mV/°C, 오프셋 500mV
* 100~125°C 구간: 기울기 10.1 mV/°C
* 125~150°C 구간: 기울기 10.6 mV/°C
*/
if(tmp235_voltage_in_milli_volts <= 1500)
{
/* 0~100°C: Ta = (Vout - 500mV) / 10.0 mV/°C */
led_temp = (tmp235_voltage_in_milli_volts - 500.0f) / 10.0f + 0.0f;
}
else if(tmp235_voltage_in_milli_volts <= 1750)
{
/* 100~125°C: 기울기가 10.1로 약간 증가 */
led_temp = (tmp235_voltage_in_milli_volts - 1500.0f) / 10.1f + 100.0f;
}
else if(tmp235_voltage_in_milli_volts <= 2000)
{
/* 125~150°C: 기울기가 10.6으로 더 증가 */
led_temp = (tmp235_voltage_in_milli_volts - 1752.5f) / 10.6f + 125.0f;
}
else
{
/* 150°C 초과 — 센서 측정 범위 벗어남 */
DBG_PRINTF("ERR!!! Temprature is over 150c\r\n");
}
/* Vout -> temperature (piecewise linear per datasheet) */
if(tmp235_voltage_in_milli_volts <= 1500)
{
/* 0..100 C: slope 10.0 mV/C, offset 500 mV */
led_temp = (tmp235_voltage_in_milli_volts - 500.0f) / 10.0f + 0.0f;
}
else if(tmp235_voltage_in_milli_volts <= 1750)
{
/* 100..125 C: slope 10.1 mV/C */
led_temp = (tmp235_voltage_in_milli_volts - 1500.0f) / 10.1f + 100.0f;
}
else if(tmp235_voltage_in_milli_volts <= 2000)
{
/* 125..150 C: slope 10.6 mV/C */
led_temp = (tmp235_voltage_in_milli_volts - 1752.5f) / 10.6f + 125.0f;
}
else
{
/* Out of sensor range (>150 C) */
DBG_PRINTF("ERR!!! Temperature is over 150c\r\n");
}
/* info4 모드: 온도값을 정수(°C x 100)로 저장 (예: 36.50°C → 3650) */
if (info4 == true)
{
info_temp = (uint16_t)(led_temp * 100);
}
/* UART 모드: 소수점 2자리까지 텍스트로 출력 */
else if(cmd_type_t == CMD_UART)
{
DBG_PRINTF("To%.2f\r\n\r\n",led_temp);
}
/* BLE 모드: °C x 100 정수를 "rso:" 헤더로 바이너리 전송 */
else if(cmd_type_t == CMD_BLE)
{
led_temp_16 = led_temp * 100;
single_format_data(ble_bin_buffer, "rso:", (uint16_t)led_temp_16);
if (info4 == true)
{
/* Store as integer (e.g. 36.50 C -> 3650) */
info_temp = (uint16_t)(led_temp * 100);
}
else if(cmd_type_t == CMD_UART)
{
DBG_PRINTF("To%.2f\r\n\r\n",led_temp);
}
else if(cmd_type_t == CMD_BLE)
{
led_temp_16 = led_temp * 100;
single_format_data(ble_bin_buffer, "rso:", (uint16_t)led_temp_16);
dr_binary_tx_safe(ble_bin_buffer,3);
}
dr_binary_tx_safe(ble_bin_buffer,3);
// sprintf(ble_tx_buffer, "To%.2f\r\n",led_temp);
// data_tx_handler(ble_tx_buffer);
}
tmp235_saadc_done = true;
tmp235_saadc_done = true;
}
}
/**
* @brief TMP235 온도센서 SAADC 초기화 및 측정 시작
/*==============================================================================
* tmp235_init - Initialise SAADC for TMP235 and start measurement
*
* AIN3 채널을 싱글엔드 모드로 설정하고 즉시 샘플링을 트리거한다.
* 결과는 tmp235_voltage_handler 콜백에서 비동기로 처리된다.
*/
* AIN3, single-ended, 12-bit, 4x oversampling, burst enabled.
* Triggers sampling immediately; result arrives via tmp235_voltage_handler.
*============================================================================*/
void tmp235_init(void)
{
/* SAADC 드라이버 초기화 (4x 오버샘플링으로 노이즈 저감) */
nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
saadc_config.oversample = NRF_SAADC_OVERSAMPLE_4X;
ret_code_t err_code = nrf_drv_saadc_init(&saadc_config, tmp235_voltage_handler);
APP_ERROR_CHECK(err_code);
nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
saadc_config.oversample = NRF_SAADC_OVERSAMPLE_4X;
ret_code_t err_code = nrf_drv_saadc_init(&saadc_config, tmp235_voltage_handler);
APP_ERROR_CHECK(err_code);
/* AIN3 채널 설정: TMP235-Q1 Vout 핀 (싱글엔드 입력, burst 활성화) */
nrf_saadc_channel_config_t config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN3);
config.burst = NRF_SAADC_BURST_ENABLED;
err_code = nrf_drv_saadc_channel_init(0, &config);
APP_ERROR_CHECK(err_code);
nrf_saadc_channel_config_t config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN3);
config.burst = NRF_SAADC_BURST_ENABLED;
err_code = nrf_drv_saadc_channel_init(0, &config);
APP_ERROR_CHECK(err_code);
/* ADC 버퍼 등록 (1채널, 1샘플) */
err_code = nrf_drv_saadc_buffer_convert(&adc_buf, 1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(&adc_buf, 1);
APP_ERROR_CHECK(err_code);
/* 즉시 ADC 샘플링 시작 (비동기) */
err_code = nrf_drv_saadc_sample();
APP_ERROR_CHECK(err_code);
}
/* Ta = (Vout Voffs ) / Tc + Tinfl */
/**
* @brief 온도 측정 외부 호출 함수
/*==============================================================================
* tmp235_voltage_level_meas - External entry point for one-shot reading
*
* tmp235_init()을 호출하여 SAADC 초기화 + 측정을 일괄 수행한다.
* 내부적으로 init 시 바로 샘플링이 시작되므로 별도 sample 호출 불필요.
*/
* Calls tmp235_init() which both initialises and triggers sampling.
*============================================================================*/
void tmp235_voltage_level_meas(void)
{
tmp235_init(); // init 함수에 있는 걸 그냥 여기 넣어도
//tmp235_uninit();
tmp235_init();
}

View File

@@ -1,31 +1,22 @@
/*******************************************************************************
* @file tmp235_q1.h
* @author CandyPops Co.
* @version V1.0.0
* @date 2022-09-05
* @brief
******************************************************************************/
/*******************************************************************************
* [헤더 개요] TMP235-Q1 아날로그 온도센서 드라이버 인터페이스
/*==============================================================================
* tmp235_q1.h - TMP235-Q1 analogue temperature sensor driver interface
*
* TMP235-Q1의 아날로그 전압 출력을 SAADC(AIN3)로 읽어
* 온도(°C)로 변환하는 기능의 외부 호출용 API를 선언한다.
* Reads the TMP235-Q1 analogue voltage output via SAADC (AIN3) and converts
* it to temperature (deg C).
*
* 주요 API:
* - tmp235_init() : SAADC 초기화 + 즉시 측정 시작 (내부 사용)
* - tmp235_voltage_level_meas() : 온도 1회 측정 (외부 호출용 래퍼)
* Conversion: Ta(C) = (Vout_mV - 500) / 10.0 (valid 0..100 C)
*
* 온도 변환: Vout(mV) → Ta(°C) = (Vout - 500) / 10.0 (0~100°C 구간)
******************************************************************************/
* API:
* tmp235_init() : initialise SAADC + start measurement (internal)
* tmp235_voltage_level_meas() : one-shot temperature reading (external wrapper)
*============================================================================*/
#ifndef _TMP235_Q1_H_
#define _TMP235_Q1_H_
/** @brief TMP235 SAADC 초기화 및 측정 시작 (AIN3 채널) */
/* Initialise SAADC for TMP235 and start measurement (AIN3). */
void tmp235_init(void);
/** @brief 온도 1회 측정 외부 호출 함수 (내부적으로 tmp235_init 호출) */
/* External entry point for a single temperature reading. */
void tmp235_voltage_level_meas(void);
#endif /* !_TMP235_Q1_H_ */