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
22 changes: 11 additions & 11 deletions src/components/battery/BatteryController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Battery* Battery::instance = nullptr;

Battery::Battery() {
instance = this;
nrf_gpio_cfg_input(PinMap::Charging, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled);
nrf_gpio_cfg_input(PinMap::Charging, NRF_GPIO_PIN_NOPULL);
SaadcInit();
}

void Battery::ReadPowerState() {
Expand All @@ -34,7 +35,6 @@ void Battery::MeasureVoltage() {
}
// Non blocking read
isReading = true;
SaadcInit();

nrfx_saadc_sample();
}
Expand All @@ -44,7 +44,11 @@ void Battery::AdcCallbackStatic(nrfx_saadc_evt_t const* event) {
}

void Battery::SaadcInit() {
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
nrfx_saadc_config_t adcConfig;
adcConfig.low_power_mode = true;
adcConfig.resolution = NRF_SAADC_RESOLUTION_14BIT;
adcConfig.oversample = NRF_SAADC_OVERSAMPLE_256X;
adcConfig.interrupt_priority = APP_IRQ_PRIORITY_LOW;
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, AdcCallbackStatic));

nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
Expand Down Expand Up @@ -72,22 +76,18 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
// ADC gain is 1/4
// thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 8
// reference_voltage is 600mV
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024;
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * (2^(sampling res) )
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 16384;

uint8_t newPercent = 100;
if (!isFull) {
// max. voltage while charging is higher than when discharging
newPercent = std::min(approx.GetValue(voltage), isCharging ? uint8_t {99} : uint8_t {100});
}

if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) {
firstMeasurement = false;
percentRemaining = newPercent;
systemTask->PushMessage(System::Messages::BatteryPercentageUpdated);
}
percentRemaining = newPercent;
systemTask->PushMessage(System::Messages::BatteryPercentageUpdated);

nrfx_saadc_uninit();
isReading = false;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/components/battery/BatteryController.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace Pinetime {
bool isFull = false;
bool isCharging = false;
bool isPowerPresent = false;
bool firstMeasurement = true;

void SaadcInit();

Expand Down
2 changes: 1 addition & 1 deletion src/systemtask/SystemTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ namespace Pinetime {
void GoToRunning();
void GoToSleep();
void UpdateMotion();
static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000);
static constexpr TickType_t batteryMeasurementPeriod = 60 * configTICK_RATE_HZ;

SystemMonitor monitor;
};
Expand Down