Skip to content

Commit

Permalink
test(die-temp): unit test for die temperature
Browse files Browse the repository at this point in the history
test calculation function

Signed-off-by: Cyril Fougeray <[email protected]>
  • Loading branch information
fouge committed Dec 23, 2024
1 parent 7d52b3f commit fb340f0
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions main_board/src/temperature/sensors/temperature.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ BUILD_ASSERT((int)(MAIN_BOARD_OVERTEMP_C - OVERTEMP_TO_NOMINAL_DROP_C) > 0 &&
"Unsigned integer will underflow");

static const uint16_t *cal1_addr =
(uint16_t *)DT_PROP(DT_INST(0, st_stm32_temp_cal), ts_cal1_addr);
static const int16_t cal1_temp =
DT_PROP(DT_INST(0, st_stm32_temp_cal), ts_cal1_temp);
(uint16_t *)DT_PROP(DT_NODELABEL(die_temp), ts_cal1_addr);
static const int16_t cal1_temp = DT_PROP(DT_NODELABEL(die_temp), ts_cal1_temp);
static const uint16_t *cal2_addr =
(uint16_t *)DT_PROP(DT_INST(0, st_stm32_temp_cal), ts_cal2_addr);
static const int16_t cal2_temp =
DT_PROP(DT_INST(0, st_stm32_temp_cal), ts_cal2_temp);
(uint16_t *)DT_PROP(DT_NODELABEL(die_temp), ts_cal2_addr);
static const int16_t cal2_temp = DT_PROP(DT_NODELABEL(die_temp), ts_cal2_temp);
static const uint16_t cal_vref_mv =
DT_PROP(DT_INST(0, st_stm32_temp_cal), ts_cal_vrefanalog);
DT_PROP(DT_NODELABEL(die_temp), ts_cal_vrefanalog);

struct sensor_and_channel; // forward declaration

Expand Down Expand Up @@ -489,7 +487,7 @@ init_sensor_and_channel(struct sensor_and_channel *x)
}

static int16_t
calculate_die_temperature(uint16_t vref_mv, uint16_t ts_data_raw)
calculate_die_temperature(const uint16_t vref_mv, const uint16_t ts_data_raw)
{
int32_t temperature_degrees = ((int32_t)cal2_temp - (int32_t)cal1_temp) *
ts_data_raw * vref_mv / cal_vref_mv /
Expand Down Expand Up @@ -938,6 +936,19 @@ overtemp_callback(struct sensor_and_channel *sensor_and_channel)

#include <zephyr/ztest.h>

ZTEST(hil, test_calculate_die_temperature)
{
uint16_t vref_mv = 2048;
uint16_t ts_data_raw = 1530;

int16_t temperature = calculate_die_temperature(vref_mv, ts_data_raw);
zassert_equal(temperature, 31);

ts_data_raw = 0;
temperature = calculate_die_temperature(vref_mv, ts_data_raw);
zassert_equal(temperature, -277);
}

ZTEST(hil, test_overtemp)
{
#define FAKE_SENSOR_OVERTEMP_C 50
Expand Down

0 comments on commit fb340f0

Please sign in to comment.