power_test.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include "../minunit.h"
  4. static void power_test_deinit(void) {
  5. // Try to reset to default charging voltage
  6. furi_hal_power_set_battery_charging_voltage(4.208f);
  7. }
  8. MU_TEST(test_power_charge_voltage_exact) {
  9. // Power of 16mV charge voltages get applied exactly
  10. // (bq25896 charge controller works in 16mV increments)
  11. //
  12. // This test may need adapted if other charge controllers are used in the future.
  13. for(uint16_t charge_mv = 3840; charge_mv <= 4208; charge_mv += 16) {
  14. float charge_volt = (float)charge_mv / 1000.0f;
  15. furi_hal_power_set_battery_charging_voltage(charge_volt);
  16. mu_assert_double_eq(charge_volt, furi_hal_power_get_battery_charging_voltage());
  17. }
  18. }
  19. MU_TEST(test_power_charge_voltage_floating_imprecision) {
  20. // 4.016f should act as 4.016 V, even with floating point imprecision
  21. furi_hal_power_set_battery_charging_voltage(4.016f);
  22. mu_assert_double_eq(4.016f, furi_hal_power_get_battery_charging_voltage());
  23. }
  24. MU_TEST(test_power_charge_voltage_inexact) {
  25. // Charge voltages that are not power of 16mV get truncated down
  26. furi_hal_power_set_battery_charging_voltage(3.841f);
  27. mu_assert_double_eq(3.840, furi_hal_power_get_battery_charging_voltage());
  28. furi_hal_power_set_battery_charging_voltage(3.900f);
  29. mu_assert_double_eq(3.888, furi_hal_power_get_battery_charging_voltage());
  30. furi_hal_power_set_battery_charging_voltage(4.200f);
  31. mu_assert_double_eq(4.192, furi_hal_power_get_battery_charging_voltage());
  32. }
  33. MU_TEST(test_power_charge_voltage_invalid_clamped) {
  34. // Out-of-range charge voltages get clamped to 3.840 V and 4.208 V
  35. furi_hal_power_set_battery_charging_voltage(3.808f);
  36. mu_assert_double_eq(3.840, furi_hal_power_get_battery_charging_voltage());
  37. // NOTE: Intentionally picking a small increment above 4.208 V to reduce the risk of an
  38. // unhappy battery if this fails.
  39. furi_hal_power_set_battery_charging_voltage(4.240f);
  40. mu_assert_double_eq(4.208, furi_hal_power_get_battery_charging_voltage());
  41. }
  42. MU_TEST_SUITE(test_power_suite) {
  43. MU_RUN_TEST(test_power_charge_voltage_exact);
  44. MU_RUN_TEST(test_power_charge_voltage_floating_imprecision);
  45. MU_RUN_TEST(test_power_charge_voltage_inexact);
  46. MU_RUN_TEST(test_power_charge_voltage_invalid_clamped);
  47. power_test_deinit();
  48. }
  49. int run_minunit_test_power() {
  50. MU_RUN_SUITE(test_power_suite);
  51. return MU_EXIT_CODE;
  52. }