2020-11-15 15:05:51 +01:00
|
|
|
#include "BatteryController.h"
|
2019-12-27 16:05:35 +01:00
|
|
|
#include <hal/nrf_gpio.h>
|
|
|
|
#include <libraries/log/nrf_log.h>
|
2020-09-27 20:02:47 +02:00
|
|
|
#include <algorithm>
|
2019-12-27 16:05:35 +01:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
|
|
|
void Battery::Init() {
|
2019-12-27 17:05:09 +01:00
|
|
|
nrf_gpio_cfg_input(chargingPin, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup);
|
|
|
|
nrf_gpio_cfg_input(powerPresentPin, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup);
|
2019-12-27 16:05:35 +01:00
|
|
|
|
|
|
|
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
|
|
|
|
nrfx_saadc_init(&adcConfig, SaadcEventHandler);
|
|
|
|
nrf_saadc_channel_config_t adcChannelConfig = {
|
|
|
|
.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
|
|
|
|
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
|
|
|
|
.gain = NRF_SAADC_GAIN1_5,
|
|
|
|
.reference = NRF_SAADC_REFERENCE_INTERNAL,
|
|
|
|
.acq_time = NRF_SAADC_ACQTIME_3US,
|
|
|
|
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
|
|
|
|
.burst = NRF_SAADC_BURST_DISABLED,
|
2019-12-27 17:05:09 +01:00
|
|
|
.pin_p = batteryVoltageAdcInput,
|
2019-12-27 16:05:35 +01:00
|
|
|
.pin_n = NRF_SAADC_INPUT_DISABLED
|
|
|
|
};
|
|
|
|
nrfx_saadc_channel_init(0, &adcChannelConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Battery::Update() {
|
2019-12-27 17:05:09 +01:00
|
|
|
isCharging = !nrf_gpio_pin_read(chargingPin);
|
|
|
|
isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
|
2019-12-27 16:05:35 +01:00
|
|
|
|
|
|
|
nrf_saadc_value_t value = 0;
|
|
|
|
nrfx_saadc_sample_convert(0, &value);
|
|
|
|
|
2019-12-27 17:05:09 +01:00
|
|
|
// see https://forum.pine64.org/showthread.php?tid=8147
|
2019-12-27 16:05:35 +01:00
|
|
|
voltage = (value * 2.0f) / (1024/3.0f);
|
2020-09-27 20:02:47 +02:00
|
|
|
percentRemaining = ((voltage - 3.55f)*100.0f)*3.9f;
|
|
|
|
percentRemaining = std::max(percentRemaining, 0.0f);
|
|
|
|
percentRemaining = std::min(percentRemaining, 100.0f);
|
2019-12-27 16:05:35 +01:00
|
|
|
|
2020-03-22 12:03:17 +01:00
|
|
|
// NRF_LOG_INFO("BATTERY " NRF_LOG_FLOAT_MARKER " %% - " NRF_LOG_FLOAT_MARKER " v", NRF_LOG_FLOAT(percentRemaining), NRF_LOG_FLOAT(voltage));
|
|
|
|
// NRF_LOG_INFO("POWER Charging : %d - Power : %d", isCharging, isPowerPresent);
|
2019-12-27 16:05:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Battery::SaadcEventHandler(nrfx_saadc_evt_t const * event) {
|
|
|
|
|
|
|
|
}
|