From 7efe2b7c51e1d21305154edad6ddf1b608784451 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Mon, 12 Jul 2021 23:07:05 +0300 Subject: [PATCH 1/3] Fix misconfigured ADC and remove now unnecessary filtering --- src/components/battery/BatteryController.cpp | 14 ++------ src/components/battery/BatteryController.h | 38 +------------------- 2 files changed, 4 insertions(+), 48 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 76ad8cb3..41b4c483 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -26,7 +26,6 @@ void Battery::Update() { return; } // Non blocking read - samples = 0; isReading = true; SaadcInit(); @@ -45,7 +44,7 @@ void Battery::SaadcInit() { .resistor_n = NRF_SAADC_RESISTOR_DISABLED, .gain = NRF_SAADC_GAIN1_5, .reference = NRF_SAADC_REFERENCE_INTERNAL, - .acq_time = NRF_SAADC_ACQTIME_3US, + .acq_time = NRF_SAADC_ACQTIME_40US, .mode = NRF_SAADC_MODE_SINGLE_ENDED, .burst = NRF_SAADC_BURST_ENABLED, .pin_p = batteryVoltageAdcInput, @@ -75,14 +74,7 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { percentRemaining = std::max(percentRemaining, 0); percentRemaining = std::min(percentRemaining, 100); - percentRemainingBuffer.insert(percentRemaining); - - samples++; - if (samples > percentRemainingSamples) { - nrfx_saadc_uninit(); - isReading = false; - } else { - nrfx_saadc_sample(); - } + nrfx_saadc_uninit(); + isReading = false; } } diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 26e24938..84d488ed 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -7,38 +7,6 @@ namespace Pinetime { namespace Controllers { - /** A simple circular buffer that can be used to average - out the sensor values. The total capacity of the CircBuffer - is given as the template parameter N. - */ - template class CircBuffer { - public: - CircBuffer() : arr {}, sz {}, cap {N}, head {} { - } - /** - insert member function overwrites the next data to the current - HEAD and moves the HEAD to the newly inserted value. - */ - void insert(const int num) { - head %= cap; - arr[head++] = num; - if (sz != cap) { - sz++; - } - } - - int GetAverage() const { - int sum = std::accumulate(arr.begin(), arr.end(), 0); - return (sum / sz); - } - - private: - std::array arr; /**< internal array used to store the values*/ - uint8_t sz; /**< The current size of the array.*/ - uint8_t cap; /**< Total capacity of the CircBuffer.*/ - uint8_t head; /**< The current head of the CircBuffer*/ - }; - class Battery { public: Battery(); @@ -47,7 +15,7 @@ namespace Pinetime { void Update(); int PercentRemaining() const { - return percentRemainingBuffer.GetAverage(); + return percentRemaining; } uint16_t Voltage() const { @@ -65,9 +33,6 @@ namespace Pinetime { static Battery* instance; nrf_saadc_value_t saadc_value; - static constexpr uint8_t percentRemainingSamples = 5; - CircBuffer percentRemainingBuffer {}; - static constexpr uint32_t chargingPin = 12; static constexpr uint32_t powerPresentPin = 19; static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7; @@ -83,7 +48,6 @@ namespace Pinetime { static void adcCallbackStatic(nrfx_saadc_evt_t const* event); bool isReading = false; - uint8_t samples = 0; }; } } From 79ee65bfd53d31dd282dbbb7952296cc6f1212eb Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Tue, 27 Jul 2021 22:41:48 +0300 Subject: [PATCH 2/3] Increase ADC precision --- src/components/battery/BatteryController.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 54b9ffd9..e334eeac 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -39,7 +39,7 @@ void Battery::SaadcInit() { nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED, .resistor_n = NRF_SAADC_RESISTOR_DISABLED, - .gain = NRF_SAADC_GAIN1_5, + .gain = NRF_SAADC_GAIN1_4, .reference = NRF_SAADC_REFERENCE_INTERNAL, .acq_time = NRF_SAADC_ACQTIME_40US, .mode = NRF_SAADC_MODE_SINGLE_ENDED, @@ -59,11 +59,11 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { APP_ERROR_CHECK(nrfx_saadc_buffer_convert(&saadc_value, 1)); // A hardware voltage divider divides the battery voltage by 2 - // ADC gain is 1/5 - // thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 10 + // ADC gain is 1/4 + // thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 8 // reference_voltage is 0.6V // p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024 - voltage = p_event->data.done.p_buffer[0] * 6000 / 1024; + voltage = p_event->data.done.p_buffer[0] * (0.6 * 8 * 1000) / 1024; if (voltage > battery_max) { percentRemaining = 100; From 09aaa5871071857dbcd955ce8ea8e33e95960678 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Sun, 1 Aug 2021 16:19:09 +0300 Subject: [PATCH 3/3] Remove floating point calculation --- src/components/battery/BatteryController.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index e334eeac..f8a64ecd 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -61,9 +61,9 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { // A hardware voltage divider divides the battery voltage by 2 // ADC gain is 1/4 // thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 8 - // reference_voltage is 0.6V + // reference_voltage is 600mV // p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024 - voltage = p_event->data.done.p_buffer[0] * (0.6 * 8 * 1000) / 1024; + voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024; if (voltage > battery_max) { percentRemaining = 100;