SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP192AdcTraits_impl.hpp
Go to the documentation of this file.
1
34#pragma once
35
36#include "AXP192AdcTraits.hpp"
37#include "AXP192Regs.hpp"
38
39namespace axp1xx
40{
41
43{
44 switch (ch) {
47 return (ch == PmicAdcBase::Channel::VBUS_VOLTAGE) ? 0x08 : 0x04;
50 return (ch == PmicAdcBase::Channel::ACIN_VOLTAGE) ? 0x20 : 0x10;
53 return (ch == PmicAdcBase::Channel::BAT_VOLTAGE) ? 0x80 : 0x40;
55 return 0x02; // APS_VOLTAGE
57 return 0x8000; // REG83H bit 7
58 default:
59 return 0;
60 }
61}
62
63inline uint16_t AdcTraitsBase<AXP192Core>::readH8L4(AXP192Core &core, uint8_t regH, uint8_t regL)
64{
65 int h = core.readReg(regH);
66 int l = core.readReg(regL);
67 if (h < 0 || l < 0) return 0;
68 return (static_cast<uint16_t>(h) << 4) | (l & 0x0F);
69}
70
71inline uint16_t AdcTraitsBase<AXP192Core>::readH8L5(AXP192Core &core, uint8_t regH, uint8_t regL)
72{
73 int h = core.readReg(regH);
74 int l = core.readReg(regL);
75 if (h < 0 || l < 0) return 0;
76 return (static_cast<uint16_t>(h) << 5) | (l & 0x1F);
77}
78
80{
81 switch (ch) {
84 if (s < 0) return false;
85 if (!((s >> 5) & 0x01)) {
86 out = 0.0f;
87 return true;
88 }
90 out = static_cast<float>(raw) * axp192_regs::factory::VBUS_VOLTAGE_STEP;
91 break;
92 }
95 if (s < 0) return false;
96 if (!((s >> 5) & 0x01)) {
97 out = 0.0f;
98 return true;
99 }
100 uint16_t raw = readH8L4(core, axp192_regs::adc::VBUS_CUR_H8, axp192_regs::adc::VBUS_CUR_L4);
101 out = static_cast<float>(raw) * axp192_regs::factory::VBUS_CURRENT_STEP;
102 break;
103 }
106 if (s < 0) return false;
107 if (!((s >> 5) & 0x01)) {
108 out = 0.0f;
109 return true;
110 }
112 out = static_cast<float>(raw) * axp192_regs::factory::BAT_VOLTAGE_STEP;
113 break;
114 }
117 if (s < 0) return false;
118 if (!((s >> 5) & 0x01)) {
119 out = 0.0f;
120 return true;
121 }
122 bool charging = (s >> 6) & 0x01;
123 if (charging) {
125 out = static_cast<float>(raw) * axp192_regs::factory::BAT_CHARGE_CUR_STEP;
126 } else {
128 out = -static_cast<float>(raw) * axp192_regs::factory::BAT_DISCHARGE_CUR_STEP;
129 }
130 break;
131 }
134 out = static_cast<float>(raw) * axp192_regs::factory::APS_VOLTAGE_STEP;
135 break;
136 }
140 break;
141 }
143 int s = core.readReg(axp192_regs::bmu::STATUS);
144 if (s < 0) return false;
145 if (!((s >> 7) & 0x01)) {
146 out = 0.0f;
147 return true;
148 }
149 uint16_t raw = readH8L4(core, axp192_regs::ac::ACIN_VOL_H8, axp192_regs::ac::ACIN_VOL_L4);
150 out = static_cast<float>(raw) * axp192_regs::factory::ACIN_VOLTAGE_STEP;
151 break;
152 }
154 int s = core.readReg(axp192_regs::bmu::STATUS);
155 if (s < 0) return false;
156 if (!((s >> 7) & 0x01)) {
157 out = 0.0f;
158 return true;
159 }
160 uint16_t raw = readH8L4(core, axp192_regs::ac::ACIN_CUR_H8, axp192_regs::ac::ACIN_CUR_L4);
161 out = static_cast<float>(raw) * axp192_regs::factory::ACIN_CURRENT_STEP;
162 break;
163 }
166 default:
167 return false;
168 }
169 return true;
170}
171
172} // namespace axp1xx
@license MIT License
@license MIT License
Channel
ADC channel bitmask.
@ BAT_PERCENTAGE
Battery percentage.
@ DIE_TEMPERATURE
Die temperature.
@ BAT_VOLTAGE
Battery voltage.
@ BAT_TEMPERATURE
Battery temperature.
@ ACIN_VOLTAGE
ACIN voltage.
@ BAT_CURRENT
Battery current.
@ VBUS_CURRENT
VBUS current.
@ VBUS_VOLTAGE
VBUS voltage.
@ ACIN_CURRENT
ACIN current.
@ VSYS_VOLTAGE
VSYS voltage.
int readReg(uint8_t reg) const
Base traits class for AXP1xxAdc template specialization.
static bool readChannel(CoreType &core, PmicAdcBase::Channel ch, float &out)
Main ADC channel reading implementation.
static uint32_t getChannelMask(PmicAdcBase::Channel ch)
Map a Channel enum to its hardware enable mask.
static constexpr uint8_t ACIN_VOL_L4
static constexpr uint8_t ACIN_CUR_L4
static constexpr uint8_t ACIN_CUR_H8
static constexpr uint8_t ACIN_VOL_H8
static constexpr uint8_t BAT_AVERCHGCUR_L5
static constexpr uint8_t APS_AVERVOL_H8
static constexpr uint8_t BAT_AVERDISCHGCUR_L5
static constexpr uint8_t INTERNAL_TEMP_H8
static constexpr uint8_t VBUS_VOL_L4
static constexpr uint8_t BAT_AVERCHGCUR_H8
static constexpr uint8_t BAT_AVERDISCHGCUR_H8
static constexpr uint8_t VBUS_CUR_L4
static constexpr uint8_t VBUS_CUR_H8
static constexpr uint8_t APS_AVERVOL_L4
static constexpr uint8_t BAT_AVERVOL_L4
static constexpr uint8_t INTERNAL_TEMP_L4
static constexpr uint8_t BAT_AVERVOL_H8
static constexpr uint8_t VBUS_VOL_H8
static constexpr uint8_t MODE_CHGSTATUS
static constexpr uint8_t STATUS
static constexpr float INTERNAL_TEMP_OFFSET
Internal temperature offset: 144.7 degC.
static constexpr float ACIN_VOLTAGE_STEP
ACIN voltage/current steps: 1.7 mV/0.625 mA per LSB.
static constexpr float BAT_VOLTAGE_STEP
BAT voltage step: 1.1 mV per LSB.
static constexpr float VBUS_CURRENT_STEP
VBUS current step: 0.375 mA per LSB.
static constexpr float APS_VOLTAGE_STEP
APS voltage step: 1.4 mV per LSB.
static constexpr float INTERNAL_TEMP_STEP
Internal temperature step: 0.1 degC per LSB.
static constexpr float ACIN_CURRENT_STEP
ACIN current step: 0.625 mA per LSB.
static constexpr float BAT_DISCHARGE_CUR_STEP
BAT discharge current step: 0.5 mA per LSB.
static constexpr float BAT_CHARGE_CUR_STEP
BAT charge current step: 0.5 mA per LSB.
static constexpr float VBUS_VOLTAGE_STEP
VBUS voltage step: 1.7 mV per LSB.