24 #include "nrf_assert.h"
29 #define PTU_SIZEOF_LOAD_VAL_BUF 8
30 #define PTU_SENSORS_PTX_LOAD_VARIATION_MAX_PERIOD_MS 15
31 #define PTU_SENSORS_PTX_LOAD_VARIATION_MIN_PERIOD_MS 6
32 #define PTU_SENSORS_PTX_LOAD_VARIATION_MAX_PERIOD_TICKS APP_TIMER_TICKS(PTU_SENSORS_PTX_LOAD_VARIATION_MAX_PERIOD_MS, APP_TIMER_PRESCALER)
33 #define PTU_SENSORS_PTX_LOAD_VARIATION_MIN_PERIOD_TICKS APP_TIMER_TICKS(PTU_SENSORS_PTX_LOAD_VARIATION_MIN_PERIOD_MS, APP_TIMER_PRESCALER)
34 #define PREV(val) ((val - 1) < 0 ? PTU_SIZEOF_LOAD_VAL_BUF - 1 : (val - 1))
35 #define NEXT(val) ((val + 1) % PTU_SIZEOF_LOAD_VAL_BUF)
36 #define GOTO_NEXT_IDX(idx) (idx = NEXT(idx))
37 #define GET_BIT(val, idx) ((val >> idx) & 1)
45 typedef struct ptu_load_var_measurement_s {
64 static bool m_res_imp_buf_filled_once =
false;
65 static uint8_t m_res_imp_buf_index = 0;
67 uint16_t res_impedance_max = 0;
68 uint16_t res_impedance_min = 0xffff;
69 uint16_t res_impedance_var_max = 0;
73 m_res_imp_buf[m_res_imp_buf_index] = m_ptu_sensor_data.
res_impedance;
75 m_res_imp_buf_index++;
79 m_res_imp_buf_filled_once =
true;
85 if(m_res_imp_buf_filled_once)
90 if(m_res_imp_buf[i] > res_impedance_max)
92 res_impedance_max = m_res_imp_buf[i];
95 if(m_res_imp_buf[i] < res_impedance_min)
97 res_impedance_min = m_res_imp_buf[i];
101 res_impedance_var_max = (res_impedance_max - res_impedance_min);
106 err_code = app_timer_stop(m_load_var_detected_timer_id);
107 APP_ERROR_CHECK(err_code);
109 APP_ERROR_CHECK(err_code);
123 uint32_t reported_power_sum = 0;
124 uint32_t poweramp_input_power = (m_ptu_sensor_data.
v_ina*m_ptu_sensor_data.
i_ina)/ 100000 ;
129 APP_ERROR_CHECK(err_code);
253 }
while(i != first_top);
265 uint32_t err_code, curr_ticks;
268 APP_ERROR_CHECK(err_code);
270 err_code = app_timer_cnt_get(&curr_ticks);
271 APP_ERROR_CHECK(err_code);
288 if(sm_handler == NULL)
289 return NRF_ERROR_INVALID_PARAM;
293 err_code = app_timer_create(&m_load_var_detected_timer_id,
294 APP_TIMER_MODE_SINGLE_SHOT,
296 APP_ERROR_CHECK(err_code);
310 if (err_code != NRF_SUCCESS)
return err_code;
313 if (err_code != NRF_SUCCESS)
return err_code;
316 if (err_code != NRF_SUCCESS)
return err_code;
319 if (err_code != NRF_SUCCESS)
return err_code;
338 if (err_code != NRF_SUCCESS)
return err_code;
342 if(m_ptu_sensor_data.
i_ina > 0)
369 bool b_under_temperature =
false;
370 bool b_over_temperature =
false;
371 bool b_itx_over_current =
false;
372 bool b_itx_under_current =
false;
376 b_under_temperature =
true;
380 b_over_temperature =
true;
385 b_itx_under_current =
true;
389 b_itx_over_current =
true;
392 if (b_over_temperature || b_under_temperature || b_itx_over_current || b_itx_under_current)
406 ASSERT(sensors_data != NULL);
419 static uint8_t last_updated_index = 0;
420 uint8_t first_top, bottom;
#define PTU_P_TX_IN_LOAD_DETECT
static bool m_find_second_top_and_validate(uint8_t first_top, uint8_t bottom)
#define PTU_LOAD_VARIATION_DETECT_LIFETIME_MS
void(* ptu_sm_handler_t)(ptu_sm_signal_type_t signal)
static bool m_find_bottom(uint8_t *p_bottom, uint8_t first_top)
uint32_t ptu_sensors_read(void)
Read sensors. This function should typically be called regularly by a timer and can generate events b...
#define PTU_SENSORS_PTX_LOAD_VARIATION_MAX_PERIOD_TICKS
Max allowed period in ticks.
static ptu_sensor_data_t m_ptu_sensor_data
The latest version of the sensor data.
uint32_t ticks_diff(uint32_t ticks_now, uint32_t ticks_old)
uint32_t ptu_tmux_temperature_get(int16_t *p_temperature)
Test wrapper for ptu_hal_temperature_get().
bool ptu_sensors_valid_long_beacon_extension_load_variation_found(void)
Check if a valid PTX load variation has occured.
static bool m_find_first_top(uint8_t *p_first_top, uint8_t last_updated_idx)
uint32_t ptu_sensors_init(ptu_sm_handler_t sm_handler)
Initialize PTU Sensors. This function must be called before any other PTU Sensors function can be cal...
uint32_t ptu_reg_item_get_from_index(uint8_t index, ptu_reg_item_t **item_p)
Get registry item from index in database. Index must be < PTU_MAX_CONNECTIONS.
uint32_t ptu_tmux_vtx_get(uint16_t *p_vtx)
Test wrapper for ptu_hal_vtx_get().
#define PTU_SENSORS_PTX_LOAD_VARIATION_MIN_PERIOD_TICKS
Min allowed period in ticks.
APP_TIMER_DEF(m_load_var_detected_timer_id)
Load variation detect timer ID.
static void m_load_var_detect()
#define PTU_POWER_EFFICIENCY
#define PTU_TEMPERATURE_WARNING_SET
#define GOTO_NEXT_IDX(idx)
Circular buffer index update.
struct ptu_load_var_measurement_s ptu_sensors_load_variation_sample_t
Struct representing a single P_TX sample.
#define PTU_TEMPERATURE_MIN
static void m_rogue_object_detect(void)
#define PTU_Z_TX_IN_LOAD_DETECT
#define PTU_TEMPERATURE_WARNING_CLEAR
uint32_t ptu_tmux_ptx_get(uint16_t *p_ptx)
Test wrapper for ptu_hal_ptx_get().
static void m_save_ptx_sample(uint8_t last_updated_index)
#define APP_TIMER_PRESCALER
static ptu_sm_handler_t m_sm_handler
State machine event handler.
#define NEXT(val)
Get next index in circular buffer.
uint32_t ptu_tmux_poweramp_enable_get(bool *enable)
Test wrapper for ptu_hal_poweramp_enable_get();.
uint32_t ptu_sensors_data_get(const ptu_sensor_data_t **sensors_data)
Get the latest data read from the PTU sensors.
#define PTU_Z_TX_IN_NO_LOAD
#define PTU_Z_MEAS_BUF_LENGTH
pru_dynamic_t prev_pru_dynamic
static ptu_sensors_load_variation_sample_t m_lbe_samples[PTU_SIZEOF_LOAD_VAL_BUF]
Buffer used for storing P_TX_IN load variation data.
void ptu_sensors_clear_long_beacon_extension_load_variation_buffer(void)
Empty buffer containing load variation sample data for Long Beacon Extension.
#define PTU_SIZEOF_LOAD_VAL_BUF
Size of P_TX_IN load variation buffer.
static void m_load_var_detected_timer_handler(void *p_context)
#define PTU_TEMPERATURE_MAX
#define PREV(val)
Get previous index in circular buffer.
#define PTU_MAX_CONNECTIONS
uint32_t ptu_tmux_itx_get(uint16_t *p_itx)
Test wrapper for ptu_hal_itx_get().
ptu_reg_item_state_t state
#define PTU_ROGUE_OBJECT_DETECT_THRESHOLD
Definition of PTU sensor data.