Skip to content

Commit 9fb3755

Browse files
authored
Merge pull request #483 from Riksu9000/fix_adc
Fix misconfigured ADC and remove now unnecessary filtering
2 parents 707446e + 09aaa58 commit 9fb3755

2 files changed

Lines changed: 17 additions & 58 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ void Battery::Update() {
2323
return;
2424
}
2525
// Non blocking read
26-
samples = 0;
2726
isReading = true;
2827
SaadcInit();
2928

@@ -40,9 +39,9 @@ void Battery::SaadcInit() {
4039

4140
nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
4241
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
43-
.gain = NRF_SAADC_GAIN1_5,
42+
.gain = NRF_SAADC_GAIN1_4,
4443
.reference = NRF_SAADC_REFERENCE_INTERNAL,
45-
.acq_time = NRF_SAADC_ACQTIME_3US,
44+
.acq_time = NRF_SAADC_ACQTIME_40US,
4645
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
4746
.burst = NRF_SAADC_BURST_ENABLED,
4847
.pin_p = batteryVoltageAdcInput,
@@ -60,22 +59,21 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
6059
APP_ERROR_CHECK(nrfx_saadc_buffer_convert(&saadc_value, 1));
6160

6261
// A hardware voltage divider divides the battery voltage by 2
63-
// ADC gain is 1/5
64-
// thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 10
65-
// reference_voltage is 0.6V
62+
// ADC gain is 1/4
63+
// thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 8
64+
// reference_voltage is 600mV
6665
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
67-
voltage = p_event->data.done.p_buffer[0] * 6000 / 1024;
68-
percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min);
69-
percentRemaining = std::max(percentRemaining, 0);
70-
percentRemaining = std::min(percentRemaining, 100);
71-
percentRemainingBuffer.Insert(percentRemaining);
72-
73-
samples++;
74-
if (samples > percentRemainingSamples) {
75-
nrfx_saadc_uninit();
76-
isReading = false;
66+
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024;
67+
68+
if (voltage > battery_max) {
69+
percentRemaining = 100;
70+
} else if (voltage < battery_min) {
71+
percentRemaining = 0;
7772
} else {
78-
nrfx_saadc_sample();
73+
percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min);
7974
}
75+
76+
nrfx_saadc_uninit();
77+
isReading = false;
8078
}
8179
}

src/components/battery/BatteryController.h

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,6 @@
77
namespace Pinetime {
88
namespace Controllers {
99

10-
/** A simple circular buffer that can be used to average
11-
out the sensor values. The total capacity of the CircBuffer
12-
is given as the template parameter N.
13-
*/
14-
template <int N> class CircBuffer {
15-
public:
16-
CircBuffer() : arr {}, sz {}, cap {N}, head {} {
17-
}
18-
/**
19-
insert member function overwrites the next data to the current
20-
HEAD and moves the HEAD to the newly inserted value.
21-
*/
22-
void Insert(const uint8_t num) {
23-
head %= cap;
24-
arr[head++] = num;
25-
if (sz != cap) {
26-
sz++;
27-
}
28-
}
29-
30-
uint8_t GetAverage() const {
31-
int sum = std::accumulate(arr.begin(), arr.end(), 0);
32-
return static_cast<uint8_t>(sum / sz);
33-
}
34-
35-
private:
36-
std::array<uint8_t, N> arr; /**< internal array used to store the values*/
37-
uint8_t sz; /**< The current size of the array.*/
38-
uint8_t cap; /**< Total capacity of the CircBuffer.*/
39-
uint8_t head; /**< The current head of the CircBuffer*/
40-
};
41-
4210
class Battery {
4311
public:
4412
Battery();
@@ -47,10 +15,7 @@ namespace Pinetime {
4715
void Update();
4816

4917
uint8_t PercentRemaining() const {
50-
auto avg = percentRemainingBuffer.GetAverage();
51-
avg = std::min(avg, static_cast<uint8_t>(100));
52-
avg = std::max(avg, static_cast<uint8_t>(0));
53-
return avg;
18+
return percentRemaining;
5419
}
5520

5621
uint16_t Voltage() const {
@@ -69,14 +34,11 @@ namespace Pinetime {
6934
static Battery* instance;
7035
nrf_saadc_value_t saadc_value;
7136

72-
static constexpr uint8_t percentRemainingSamples = 5;
73-
CircBuffer<percentRemainingSamples> percentRemainingBuffer {};
74-
7537
static constexpr uint32_t chargingPin = 12;
7638
static constexpr uint32_t powerPresentPin = 19;
7739
static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7;
7840
uint16_t voltage = 0;
79-
int percentRemaining = -1;
41+
uint8_t percentRemaining = 0;
8042

8143
bool isCharging = false;
8244
bool isPowerPresent = false;
@@ -87,7 +49,6 @@ namespace Pinetime {
8749
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);
8850

8951
bool isReading = false;
90-
uint8_t samples = 0;
9152
};
9253
}
9354
}

0 commit comments

Comments
 (0)