Browse Source

fix(clang-format)

xmkb
Nick 4 years ago
parent
commit
f5909f9c26
  1. 117
      app/drivers/zephyr/battery_voltage_divider.c

117
app/drivers/zephyr/battery_voltage_divider.c

@ -21,95 +21,92 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#define VBATT DT_PATH(vbatt) #define VBATT DT_PATH(vbatt)
struct battery_config { struct battery_config {
struct device *adc; struct device *adc;
struct adc_channel_cfg acc; struct adc_channel_cfg acc;
struct adc_sequence as; struct adc_sequence as;
int16_t adc_raw; int16_t adc_raw;
}; };
static struct battery_config battery_config; static struct battery_config battery_config;
static int lithium_ion_mv_to_pct(int16_t bat_mv) { static int lithium_ion_mv_to_pct(int16_t bat_mv) {
// Magic function that maps mV to this discharge graph from adafruit: https://learn.adafruit.com/li-ion-and-lipoly-batteries/voltages // Magic function that maps mV to this discharge graph from adafruit:
return round(106.818 + (-0.032685 - 106.818) / pow(1 + pow(bat_mv/3679.35, 58.979), 0.347386)); // https://learn.adafruit.com/li-ion-and-lipoly-batteries/voltages
return round(106.818 +
(-0.032685 - 106.818) / pow(1 + pow(bat_mv / 3679.35, 58.979), 0.347386));
} }
static void battery_read(struct k_work *workd) { static void battery_read(struct k_work *workd) {
struct battery_config *cfg = &battery_config; struct battery_config *cfg = &battery_config;
struct adc_sequence *as = &cfg->as; struct adc_sequence *as = &cfg->as;
int rc = adc_read(cfg->adc, as); int rc = adc_read(cfg->adc, as);
as->calibrate = false; as->calibrate = false;
if (rc == 0) { if (rc == 0) {
int32_t val = cfg->adc_raw; int32_t val = cfg->adc_raw;
adc_raw_to_millivolts(adc_ref_internal(cfg->adc), cfg->acc.gain, as->resolution, &val); adc_raw_to_millivolts(adc_ref_internal(cfg->adc), cfg->acc.gain, as->resolution, &val);
rc = val * (uint64_t)DT_PROP(VBATT, full_ohms) / DT_PROP(VBATT, output_ohms); rc = val * (uint64_t)DT_PROP(VBATT, full_ohms) / DT_PROP(VBATT, output_ohms);
LOG_DBG("ADC raw %d ~ %d mV => %d mV\n", cfg->adc_raw, val, rc); LOG_DBG("ADC raw %d ~ %d mV => %d mV\n", cfg->adc_raw, val, rc);
int percent = lithium_ion_mv_to_pct(rc); int percent = lithium_ion_mv_to_pct(rc);
LOG_DBG("Percent: %d", percent); LOG_DBG("Percent: %d", percent);
} else { } else {
LOG_DBG("Failed to read ADC: %d", rc); LOG_DBG("Failed to read ADC: %d", rc);
} }
} }
K_WORK_DEFINE(battery_work, battery_read); K_WORK_DEFINE(battery_work, battery_read);
static void battery_handler(struct k_timer *timer) static void battery_handler(struct k_timer *timer) { k_work_submit(&battery_work); }
{
k_work_submit(&battery_work);
}
K_TIMER_DEFINE(battery_tick, battery_handler, NULL); K_TIMER_DEFINE(battery_tick, battery_handler, NULL);
static int battery_setup(struct device *_arg) { static int battery_setup(struct device *_arg) {
struct battery_config *cfg = &battery_config; struct battery_config *cfg = &battery_config;
struct adc_sequence *as = &cfg->as; struct adc_sequence *as = &cfg->as;
struct adc_channel_cfg *acc = &cfg->acc; struct adc_channel_cfg *acc = &cfg->acc;
cfg->adc = device_get_binding(DT_IO_CHANNELS_LABEL(VBATT)); cfg->adc = device_get_binding(DT_IO_CHANNELS_LABEL(VBATT));
if (cfg->adc == NULL) { if (cfg->adc == NULL) {
LOG_ERR("ADC %s failed to retrieve", DT_IO_CHANNELS_LABEL(VBATT)); LOG_ERR("ADC %s failed to retrieve", DT_IO_CHANNELS_LABEL(VBATT));
return -ENOENT; return -ENOENT;
} }
*as = (struct adc_sequence){ *as = (struct adc_sequence){
.channels = BIT(0), .channels = BIT(0),
.buffer = &cfg->adc_raw, .buffer = &cfg->adc_raw,
.buffer_size = sizeof(cfg->adc_raw), .buffer_size = sizeof(cfg->adc_raw),
.oversampling = 4, .oversampling = 4,
.calibrate = true, .calibrate = true,
}; };
#ifdef CONFIG_ADC_NRFX_SAADC #ifdef CONFIG_ADC_NRFX_SAADC
*acc = (struct adc_channel_cfg){ *acc = (struct adc_channel_cfg){
.gain = ADC_GAIN_1_5, .gain = ADC_GAIN_1_5,
.reference = ADC_REF_INTERNAL, .reference = ADC_REF_INTERNAL,
.acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40), .acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40),
.input_positive = SAADC_CH_PSELP_PSELP_AnalogInput0 + DT_IO_CHANNELS_INPUT(VBATT), .input_positive = SAADC_CH_PSELP_PSELP_AnalogInput0 + DT_IO_CHANNELS_INPUT(VBATT),
}; };
as->resolution = 12; as->resolution = 12;
#else #else
#error Unsupported ADC #error Unsupported ADC
#endif #endif
int adc_rc = adc_channel_setup(cfg->adc, acc); int adc_rc = adc_channel_setup(cfg->adc, acc);
LOG_DBG("AIN%u setup returned %d", DT_IO_CHANNELS_INPUT(VBATT), adc_rc); LOG_DBG("AIN%u setup returned %d", DT_IO_CHANNELS_INPUT(VBATT), adc_rc);
if (adc_rc != 0) { if (adc_rc != 0) {
return adc_rc; return adc_rc;
} }
k_timer_start(&battery_tick, K_NO_WAIT, K_SECONDS(5)); k_timer_start(&battery_tick, K_NO_WAIT, K_SECONDS(5));
return 0; return 0;
} }
SYS_INIT(battery_setup, SYS_INIT(battery_setup, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
APPLICATION,
CONFIG_APPLICATION_INIT_PRIORITY);
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ #endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
Loading…
Cancel
Save