Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e04e79ac31 | |||
| 32b6fb800d | |||
| 24e5b2e205 |
@@ -41,7 +41,6 @@ static const CmdEntry m_cmd_table[] = {
|
||||
{ "msn?", true, Cmd_msn },
|
||||
{ "mst?", true, Cmd_mst },
|
||||
{ "msp?", true, Cmd_msp },
|
||||
{ "msi?", true, Cmd_msi },
|
||||
|
||||
/* D. Piezo ultrasound */
|
||||
{ "mpa?", true, Cmd_mpa },
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
* msn? -> rsn: battery ADC measurement
|
||||
* mso? -> rso: TMP235 temperature reading
|
||||
* msp? -> rsp: IMU 6-axis single read
|
||||
* msi? -> rsi: IMU motion streaming start
|
||||
* all_sensors() bulk-measurement helper used by the mbb? handler
|
||||
*============================================================================*/
|
||||
|
||||
@@ -74,34 +73,6 @@ int Cmd_msp(const ParsedCmd *cmd)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*==============================================================================
|
||||
* msi? -> rsi: IMU motion sensor streaming start
|
||||
*
|
||||
* Request: [TAG 4B "msi?"] [mode 1B] [CRC 2B]
|
||||
* mode = 'c': continuous (repeats on timer)
|
||||
* else : single shot (one read then auto stop)
|
||||
* Response: rsi: + IMU data (sent from main timer callback)
|
||||
*============================================================================*/
|
||||
int Cmd_msi(const ParsedCmd *cmd)
|
||||
{
|
||||
hw_i2c_init_once();
|
||||
|
||||
motion_raw_data_enabled = true;
|
||||
ble_got_new_data = false;
|
||||
|
||||
if (cmd->data_len > 0 && (char)cmd->data[0] == 'c')
|
||||
{
|
||||
motion_data_once = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
motion_data_once = true;
|
||||
}
|
||||
|
||||
main_timer_start();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*==============================================================================
|
||||
* all_sensors() - Bulk-measurement helper for the mbb? handler
|
||||
*
|
||||
@@ -146,8 +117,10 @@ void all_sensors(void)
|
||||
|
||||
info4 = false;
|
||||
|
||||
/* Assemble and transmit the rbb: packet */
|
||||
buf = ble_bin_buffer;
|
||||
/* Assemble and transmit the rbb: packet (dedicated buffer to avoid
|
||||
collision with ble_bin_buffer used by the async ADC state machine) */
|
||||
static uint8_t rbb_buf[20];
|
||||
buf = rbb_buf;
|
||||
buf[0] = 'r'; buf[1] = 'b'; buf[2] = 'b'; buf[3] = ':';
|
||||
buf[4] = (uint8_t)(info_batt >> 8);
|
||||
buf[5] = (uint8_t)(info_batt & 0xFF);
|
||||
|
||||
@@ -9,8 +9,6 @@
|
||||
int Cmd_msn(const ParsedCmd *cmd); /* msn? -> rsn: battery ADC measurement */
|
||||
int Cmd_mst(const ParsedCmd *cmd); /* mst? -> rso: TMP235 with piezo power cycle */
|
||||
int Cmd_msp(const ParsedCmd *cmd); /* msp? -> rsp: IMU 6-axis single read */
|
||||
int Cmd_msi(const ParsedCmd *cmd); /* msi? -> rsi: IMU streaming start */
|
||||
|
||||
/* Helper for the mbb? handler: sequentially measures battery / IMU / temperature, then emits a single rbb: response.
|
||||
* Called from Cmd_mbb() in cmd_piezo.c. */
|
||||
void all_sensors(void);
|
||||
|
||||
@@ -1353,6 +1353,9 @@ dr_adc_err_t maa_async_start(uint8_t freq_option, uint16_t delay_us, uint16_t nu
|
||||
return DR_ADC_ERR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* Clear BLE buffer to prevent stale data from previous measurement */
|
||||
memset(ble_buffer, 0, BLE_MTU_SIZE);
|
||||
|
||||
/* Initialize context */
|
||||
g_maa_ctx.freq_option = freq_option;
|
||||
g_maa_ctx.delay_us = delay_us;
|
||||
@@ -1377,7 +1380,11 @@ dr_adc_err_t maa_async_start(uint8_t freq_option, uint16_t delay_us, uint16_t nu
|
||||
{
|
||||
g_plat.log("[maa] maa_async_start: CH%u capture failed (%d)", ch, err);
|
||||
}
|
||||
maa_async_send_completion(0xFFF0 | ch);
|
||||
/* Clean up state only — do NOT send raa: here.
|
||||
The caller (Cmd_maa / Cmd_mbb) sends the raa: error packet
|
||||
to avoid duplicate raa: on the BLE link. */
|
||||
dr_piezo_power_off();
|
||||
g_maa_ctx.state = MAA_ASYNC_IDLE;
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
*
|
||||
* Measures battery voltage via nRF52840 SAADC on AIN2:
|
||||
* - 12-bit resolution, 4x oversampling
|
||||
* - Periodic monitoring via battery_loop timer (60 s interval)
|
||||
* - Auto power-off after 10 consecutive readings below 3500 mV
|
||||
* - Periodic safety check via battery_loop timer (60 s interval)
|
||||
* - Sequential: battery -> temperature measurement
|
||||
* - Auto power-off after 5 consecutive readings below 3500 mV or above 40 C
|
||||
* - In info4 mode (bulk sensor collection): stores to info_batt
|
||||
*
|
||||
* Voltage conversion:
|
||||
@@ -26,7 +27,8 @@
|
||||
#include "app_timer.h"
|
||||
#include "battery_saadc.h"
|
||||
#include "main_timer.h"
|
||||
#include "main.h"
|
||||
#include "tmp235_q1.h"
|
||||
#include "dr_piezo.h"
|
||||
#include "debug_print.h"
|
||||
|
||||
/* SAADC internal reference voltage (mV, float) */
|
||||
@@ -51,12 +53,25 @@ APP_TIMER_DEF(m_battery_loop_timer_id);
|
||||
/* Battery monitoring interval (ms) */
|
||||
#define BATTERY_LOOP_INTERVAL 60000
|
||||
|
||||
/* Safety check consecutive count threshold */
|
||||
#define SAFETY_CHECK_COUNT 5
|
||||
|
||||
/* Low-battery check flag — set by battery_loop, consumed by handler */
|
||||
bool low_battery_check = false;
|
||||
|
||||
/* Safety check mode flag — set by battery handler, consumed by tmp235 handler */
|
||||
bool safety_check_mode = false;
|
||||
|
||||
/* SAADC callback completion flag — used by all_sensors() to wait */
|
||||
volatile bool battery_saadc_done = false;
|
||||
|
||||
/* Safety check: cached battery voltage for use in safety_check_complete() */
|
||||
static float safety_batt_mv = 0;
|
||||
|
||||
/* Safety check: consecutive counters */
|
||||
static uint8_t low_battery_cnt = 0;
|
||||
static uint8_t over_temp_cnt = 0;
|
||||
|
||||
/* info4: bulk sensor collection mode flag */
|
||||
extern bool info4;
|
||||
|
||||
@@ -66,33 +81,83 @@ extern which_cmd_t cmd_type_t;
|
||||
extern uint8_t ble_bin_buffer[BLE_NUS_MAX_DATA_LEN];
|
||||
|
||||
/* info4 mode: cached battery voltage (mV) */
|
||||
volatile uint16_t info_batt;
|
||||
volatile uint16_t info_batt;
|
||||
|
||||
/* info4 sequential measurement control flags */
|
||||
extern bool go_temp;
|
||||
extern bool go_batt;
|
||||
|
||||
extern bool motion_raw_data_enabled ;
|
||||
extern bool motion_raw_data_enabled;
|
||||
extern bool ble_got_new_data;
|
||||
extern bool motion_data_once ;
|
||||
extern bool motion_data_once;
|
||||
|
||||
/*==============================================================================
|
||||
* safety_check_complete - Called by tmp235 handler after temperature measurement
|
||||
*
|
||||
* Checks both battery voltage and temperature against thresholds.
|
||||
* 5 consecutive readings exceeding either threshold triggers power OFF.
|
||||
*============================================================================*/
|
||||
void safety_check_complete(float temp_c)
|
||||
{
|
||||
DBG_PRINTF("[SAFETY] Batt=%d mV, Temp=%d.%d C\r\n",
|
||||
(int)safety_batt_mv, (int)temp_c, ((int)(temp_c * 10)) % 10);
|
||||
|
||||
/* Battery check */
|
||||
if (safety_batt_mv <= LOW_BATTERY_VOLTAGE)
|
||||
{
|
||||
low_battery_cnt++;
|
||||
DBG_PRINTF("[SAFETY] Low batt cnt=%d\r\n", low_battery_cnt);
|
||||
}
|
||||
else
|
||||
{
|
||||
low_battery_cnt = 0;
|
||||
}
|
||||
|
||||
/* Temperature check */
|
||||
if (temp_c >= OVER_TEMPERATURE_THRESHOLD)
|
||||
{
|
||||
over_temp_cnt++;
|
||||
DBG_PRINTF("[SAFETY] Over temp cnt=%d\r\n", over_temp_cnt);
|
||||
}
|
||||
else
|
||||
{
|
||||
over_temp_cnt = 0;
|
||||
}
|
||||
|
||||
/* Power OFF if either threshold exceeded 5 consecutive times */
|
||||
if (low_battery_cnt >= SAFETY_CHECK_COUNT)
|
||||
{
|
||||
low_battery_cnt = 0;
|
||||
DBG_PRINTF("[SAFETY] Low battery -> Power OFF\r\n");
|
||||
go_device_power_off = true;
|
||||
main_timer_start();
|
||||
}
|
||||
else if (over_temp_cnt >= SAFETY_CHECK_COUNT)
|
||||
{
|
||||
over_temp_cnt = 0;
|
||||
DBG_PRINTF("[SAFETY] Over temperature -> Power OFF\r\n");
|
||||
go_device_power_off = true;
|
||||
main_timer_start();
|
||||
}
|
||||
|
||||
dr_piezo_power_off();
|
||||
}
|
||||
|
||||
/*==============================================================================
|
||||
* battery_event_handler - SAADC conversion complete callback
|
||||
*
|
||||
* Converts the raw ADC value to battery voltage (mV) and then:
|
||||
* - Low-battery check mode: if <= 3500 mV for 10 consecutive times -> power OFF
|
||||
* - Low-battery check mode: store voltage, chain temperature measurement
|
||||
* - info4 mode: store to info_batt (no BLE send)
|
||||
* - Normal mode: send rsn: response over BLE or UART
|
||||
*============================================================================*/
|
||||
void battery_event_handler( nrf_drv_saadc_evt_t const * p_event )
|
||||
void battery_event_handler(nrf_drv_saadc_evt_t const * p_event)
|
||||
{
|
||||
static uint8_t low_battery_cnt = 0;
|
||||
|
||||
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
|
||||
{
|
||||
nrf_saadc_value_t register_val = 0;
|
||||
float batt_lvl_in_milli_volt_0 = 0; /* before divider correction */
|
||||
float batt_lvl_in_milli_volt_1 = 0; /* after divider correction */
|
||||
float batt_lvl_in_milli_volt_0 = 0;
|
||||
float batt_lvl_in_milli_volt_1 = 0;
|
||||
|
||||
register_val = p_event->data.done.p_buffer[0];
|
||||
|
||||
@@ -106,28 +171,21 @@ void battery_event_handler( nrf_drv_saadc_evt_t const * p_event )
|
||||
batt_lvl_in_milli_volt_0 = BATTERY_RESULT_IN_MILLI_VOLTS(register_val);
|
||||
|
||||
/* Resistor divider correction factor 1.42 */
|
||||
batt_lvl_in_milli_volt_1 = (batt_lvl_in_milli_volt_0) *1.42f;
|
||||
batt_lvl_in_milli_volt_1 = batt_lvl_in_milli_volt_0 * 1.42f;
|
||||
|
||||
/* --- Low-battery check mode (set by battery_loop timer) --- */
|
||||
if(low_battery_check == true)
|
||||
/* --- Safety check mode: store voltage, chain temperature measurement --- */
|
||||
if (low_battery_check == true)
|
||||
{
|
||||
low_battery_check = false;
|
||||
safety_batt_mv = batt_lvl_in_milli_volt_1;
|
||||
safety_check_mode = true;
|
||||
|
||||
if(batt_lvl_in_milli_volt_1 <= LOW_BATTERY_VOLTAGE)
|
||||
/* TMP235 shares piezo TX/RX power rail */
|
||||
if (!dr_piezo_is_power_on())
|
||||
{
|
||||
if(low_battery_cnt >= 10)
|
||||
{
|
||||
low_battery_cnt = 0;
|
||||
DBG_PRINTF("Save FDS parameters and then Power OFF\r\n");
|
||||
go_device_power_off = true;
|
||||
main_timer_start();
|
||||
}
|
||||
else
|
||||
{
|
||||
low_battery_cnt++;
|
||||
DBG_PRINTF("WARNING!!! low_battery cnt = %d, Batt = %d(mV)\r\n", low_battery_cnt, (int)batt_lvl_in_milli_volt_1);
|
||||
}
|
||||
dr_piezo_power_on();
|
||||
}
|
||||
tmp235_voltage_level_meas();
|
||||
}
|
||||
|
||||
/* --- info4 mode: store value for mbb? bulk response --- */
|
||||
@@ -145,8 +203,8 @@ void battery_event_handler( nrf_drv_saadc_evt_t const * p_event )
|
||||
}
|
||||
else if (cmd_type_t == CMD_BLE)
|
||||
{
|
||||
single_format_data(ble_bin_buffer, "rsn:", batt_lvl_in_milli_volt_1);
|
||||
dr_binary_tx_safe(ble_bin_buffer,3);
|
||||
single_format_data(ble_bin_buffer, "rsn:", batt_lvl_in_milli_volt_1);
|
||||
dr_binary_tx_safe(ble_bin_buffer, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,7 +223,8 @@ static void battery_configure(void)
|
||||
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, battery_event_handler);
|
||||
if (err_code != NRF_SUCCESS) {
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return; /* SAADC busy — skip this cycle, retry next */
|
||||
}
|
||||
|
||||
@@ -199,7 +258,7 @@ void battery_level_meas(void)
|
||||
* battery_loop - Periodic battery monitoring timer callback
|
||||
*
|
||||
* Sets the low-battery check flag and starts a measurement.
|
||||
* Skips if another sensor (IMU / info4) is already running (SAADC conflict).
|
||||
* Skips if info4 mode is active (SAADC conflict).
|
||||
*============================================================================*/
|
||||
void battery_loop(void * p_context)
|
||||
{
|
||||
|
||||
@@ -5,24 +5,35 @@
|
||||
*
|
||||
* API:
|
||||
* battery_level_meas() : one-shot measurement (async, result via callback)
|
||||
* battery_timer_init/start/stop() : 5-second periodic monitoring timer
|
||||
* battery_timer_init/start/stop() : 60-second periodic monitoring timer
|
||||
*
|
||||
* Auto power-off is triggered after 10 consecutive readings below
|
||||
* LOW_BATTERY_VOLTAGE (3500 mV).
|
||||
* Periodic safety check (every 60 s):
|
||||
* Battery -> Temperature sequential measurement via SAADC.
|
||||
* Auto power-off after 5 consecutive readings below LOW_BATTERY_VOLTAGE (3500 mV)
|
||||
* or above OVER_TEMPERATURE_THRESHOLD (40 C).
|
||||
*============================================================================*/
|
||||
|
||||
#ifndef _BATTERY_SAADC_H_
|
||||
#define _BATTERY_SAADC_H_
|
||||
|
||||
/* Low-battery threshold (mV) — 10 consecutive readings below this -> power OFF */
|
||||
/* Low-battery threshold (mV) — 5 consecutive readings below this -> power OFF */
|
||||
#define LOW_BATTERY_VOLTAGE 3500
|
||||
|
||||
/* Over-temperature threshold (deg C) — 5 consecutive readings above this -> power OFF */
|
||||
#define OVER_TEMPERATURE_THRESHOLD 40.0f
|
||||
|
||||
/* SAADC callback completion flag (used by all_sensors() to wait) */
|
||||
extern volatile bool battery_saadc_done;
|
||||
|
||||
/* Safety check mode flag — set by battery_loop, consumed by tmp235 handler */
|
||||
extern bool safety_check_mode;
|
||||
|
||||
/* Called by tmp235 handler when safety check temperature measurement completes */
|
||||
void safety_check_complete(float temp_c);
|
||||
|
||||
/* Start a single async battery measurement. Result handled in callback. */
|
||||
void battery_level_meas(void);
|
||||
/* Start the 5-second periodic battery monitoring timer. */
|
||||
/* Start the 60-second periodic battery monitoring timer. */
|
||||
void battery_timer_start(void);
|
||||
/* Stop the battery monitoring timer. */
|
||||
void battery_timer_stop(void);
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "tmp235_q1.h"
|
||||
#include "main.h"
|
||||
#include "main_timer.h"
|
||||
#include "battery_saadc.h"
|
||||
#include "debug_print.h"
|
||||
|
||||
/* SAADC internal reference (mV) */
|
||||
@@ -101,20 +102,27 @@ void tmp235_voltage_handler(nrf_drv_saadc_evt_t const * p_event)
|
||||
DBG_PRINTF("ERR!!! Temperature is over 150c\r\n");
|
||||
}
|
||||
|
||||
if (info4 == true)
|
||||
/* --- Safety check mode: pass temperature to battery module for judgment --- */
|
||||
if (safety_check_mode == true)
|
||||
{
|
||||
safety_check_mode = false;
|
||||
safety_check_complete(led_temp);
|
||||
}
|
||||
|
||||
/* --- info4 mode: store value for mbb? bulk response --- */
|
||||
else 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)
|
||||
else if (cmd_type_t == CMD_UART)
|
||||
{
|
||||
DBG_PRINTF("To%.2f\r\n\r\n",led_temp);
|
||||
DBG_PRINTF("To%.2f\r\n\r\n", led_temp);
|
||||
}
|
||||
else if(cmd_type_t == CMD_BLE)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
tmp235_saadc_done = true;
|
||||
|
||||
Reference in New Issue
Block a user