Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/daq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ uint16_t adc_raw_clamp(float scale, float limit)
// even if we have a higher voltage limit, we must limit it
// to the max value the ADC will be able to deliver
const float limit_scaled = limit * scale;
if (limit_scaled < 0.0F) {
return 0;
}
return limit_scaled > (float)UINT16_MAX ? UINT16_MAX : (uint16_t)(limit_scaled);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/src/tests_daq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ void adc_alert_hv_overvoltage_triggering()
TEST_ASSERT_EQUAL(DCDC_CONTROL_OFF, dcdc.state);
}

void adc_raw_clamp_handles_negative_limits()
{
TEST_ASSERT_EQUAL_HEX(0, adc_raw_clamp(1.0F, -1.0F));
TEST_ASSERT_EQUAL_HEX(0, adc_raw_clamp(2.0F, -100.0F));
TEST_ASSERT_EQUAL_HEX(0, adc_raw_clamp(1.0F, 0.0F));
TEST_ASSERT_EQUAL_HEX(100, adc_raw_clamp(2.0F, 50.0F));
}

void adc_alert_overflow_prevention()
{
// try to set an alert that overflows the 12-bit ADC resolution
Expand Down Expand Up @@ -209,6 +217,7 @@ int daq_tests()
RUN_TEST(adc_alert_lv_overvoltage_triggering);
RUN_TEST(adc_alert_hv_overvoltage_triggering);
RUN_TEST(adc_alert_overflow_prevention);
RUN_TEST(adc_raw_clamp_handles_negative_limits);

return UNITY_END();
}