SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP192Channel.cpp
Go to the documentation of this file.
1
30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_AXP192
32
33#include "AXP192Channel.hpp"
34#include "AXP192Regs.hpp"
35
36
38{
39}
40
41static uint8_t getEnableBit(uint8_t channel)
42{
43 switch (channel) {
44 case 0: return 0;
45 case 1: return 4;
46 case 2: return 1;
47 case 3: return 2;
48 case 4: return 3;
49 case 5: return 0xFF;
50 case 6: return 6;
51 default: return 0xFF;
52 }
53}
54
55bool AXP192Channel::enable(uint8_t channel, bool enable)
56{
57 if (channel >= CHANNEL_COUNT) return false;
58
59 if (channel == CH_LDOio) {
61 if (val < 0) return false;
62 val &= 0xF8;
63 if (enable) {
64 val |= 0x02;
65 }
66 return _core.writeReg(axp192_regs::gpio::GPIO0_CTL, static_cast<uint8_t>(val)) >= 0;
67 }
68
69 uint8_t bit = getEnableBit(channel);
70 if (bit == 0xFF) return false;
73}
74
75bool AXP192Channel::isEnabled(uint8_t channel)
76{
77 if (channel >= CHANNEL_COUNT) return false;
78
79 if (channel == CH_LDOio) {
81 if (val < 0) return false;
82 return (val & 0x02) != 0;
83 }
84
85 uint8_t bit = getEnableBit(channel);
86 if (bit == 0xFF) return false;
88}
89
90bool AXP192Channel::setVoltage(uint8_t channel, uint16_t mV)
91{
92 if (channel >= CHANNEL_COUNT) return false;
93
94 switch (channel) {
95 case CH_DCDC1: {
96 if (mV < 700) mV = 700;
97 if (mV > 3500) mV = 3500;
98 uint8_t code = (mV - 700) / 25;
99 return _core.updateBits(axp192_regs::pwr::DC1_VOLTAGE, 0x7F, code) >= 0;
100 }
101 case CH_DCDC2: {
102 if (mV < 700) mV = 700;
103 if (mV > 2275) mV = 2275;
104 uint8_t code = (mV - 700) / 25;
105 return _core.updateBits(axp192_regs::pwr::DC2OUT_VOL, 0x3F, code) >= 0;
106 }
107 case CH_DCDC3: {
108 if (mV < 700) mV = 700;
109 if (mV > 3500) mV = 3500;
110 uint8_t code = (mV - 700) / 25;
111 return _core.updateBits(axp192_regs::pwr::DC3OUT_VOL, 0x7F, code) >= 0;
112 }
113 case CH_LDO2: {
114 if (mV < 1800) mV = 1800;
115 if (mV > 3300) mV = 3300;
116 uint8_t code = (mV - 1800) / 100;
117 return _core.updateBits(axp192_regs::pwr::LDO23OUT_VOL, 0xF0, code << 4) >= 0;
118 }
119 case CH_LDO3: {
120 if (mV < 1800) mV = 1800;
121 if (mV > 3300) mV = 3300;
122 uint8_t code = (mV - 1800) / 100;
123 return _core.updateBits(axp192_regs::pwr::LDO23OUT_VOL, 0x0F, code) >= 0;
124 }
125 case CH_LDOio: {
126 if (mV < 1800) mV = 1800;
127 if (mV > 3300) mV = 3300;
128 uint8_t code = (mV - 1800) / 100;
129 return _core.updateBits(axp192_regs::gpio::GPIO0_VOL, 0xF0, code << 4) >= 0;
130 }
131 case CH_EXTEN:
132 return false;
133 default:
134 return false;
135 }
136}
137
138uint16_t AXP192Channel::getVoltage(uint8_t channel)
139{
140 if (channel >= CHANNEL_COUNT) return 0;
141
142 switch (channel) {
143 case CH_DCDC1: {
144 int val = _core.readReg(axp192_regs::pwr::DC1_VOLTAGE);
145 if (val < 0) return 0;
146 return ((val & 0x7F) * 25) + 700;
147 }
148 case CH_DCDC2: {
149 int val = _core.readReg(axp192_regs::pwr::DC2OUT_VOL);
150 if (val < 0) return 0;
151 return ((val & 0x3F) * 25) + 700;
152 }
153 case CH_DCDC3: {
154 int val = _core.readReg(axp192_regs::pwr::DC3OUT_VOL);
155 if (val < 0) return 0;
156 return (val * 25) + 700;
157 }
158 case CH_LDO2: {
160 if (val < 0) return 0;
161 uint8_t code = (val >> 4) & 0x0F;
162 return (code * 100) + 1800;
163 }
164 case CH_LDO3: {
166 if (val < 0) return 0;
167 uint8_t code = val & 0x0F;
168 return (code * 100) + 1800;
169 }
170 case CH_LDOio: {
171 int val = _core.readReg(axp192_regs::gpio::GPIO0_VOL);
172 if (val < 0) return 0;
173 uint8_t code = (val >> 4) & 0x0F;
174 return (code * 100) + 1800;
175 }
176 case CH_EXTEN:
177 return 0;
178 default:
179 return 0;
180 }
181}
182
183uint8_t AXP192Channel::count() const
184{
185 return CHANNEL_COUNT;
186}
187
189{
190 static const Info channelInfo[] = {
191 {CH_DCDC1, Type::DCDC, 700, 3500, 25, "DCDC1"},
192 {CH_DCDC2, Type::DCDC, 700, 2275, 25, "DCDC2"},
193 {CH_DCDC3, Type::DCDC, 700, 3500, 25, "DCDC3"},
194 {CH_LDO2, Type::LDO, 1800, 3300, 100, "LDO2"},
195 {CH_LDO3, Type::LDO, 1800, 3300, 100, "LDO3"},
196 {CH_LDOio, Type::LDO, 1800, 3300, 100, "LDOio"},
197 {CH_EXTEN, Type::LDO, 0, 0, 0, "EXTEN"},
198 };
199 if (channel >= CHANNEL_COUNT) {
200 return {0, Type::DCDC, 0, 0, 0, "UNKNOWN"};
201 }
202 return channelInfo[channel];
203}
204
205#endif
@license MIT License
@license MIT License
static constexpr uint8_t CH_LDOio
bool setVoltage(uint8_t channel, uint16_t mV) override
Set output voltage for a channel.
static constexpr uint8_t CH_DCDC1
bool enable(uint8_t channel, bool enable) override
Enable or disable a power output channel.
Info getInfo(uint8_t channel) const override
Get channel information (type, name, min/max/step)
static constexpr uint8_t CH_LDO2
static constexpr uint8_t CH_LDO3
static constexpr uint8_t CH_DCDC3
static constexpr uint8_t CH_DCDC2
static constexpr uint8_t CH_EXTEN
uint16_t getVoltage(uint8_t channel) override
Get current output voltage for a channel.
static constexpr uint8_t CHANNEL_COUNT
uint8_t count() const override
Get total number of power output channels.
AXP192Channel(AXP192Core &core)
Construct channel control interface.
bool isEnabled(uint8_t channel) override
Check whether a power output channel is enabled.
@ LDO
Linear low-dropout regulator.
@ DCDC
Switching buck/boost converter.
bool clrRegBit(uint8_t reg, uint8_t bit)
bool getRegBit(uint8_t reg, uint8_t bit)
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
bool setRegBit(uint8_t reg, uint8_t bit)
Static metadata for a single power output channel.
static constexpr uint8_t GPIO0_VOL
static constexpr uint8_t GPIO0_CTL
static constexpr uint8_t LDO23_DC123_EXT_CTL
static constexpr uint8_t LDO23OUT_VOL
static constexpr uint8_t DC1_VOLTAGE
static constexpr uint8_t DC3OUT_VOL
static constexpr uint8_t DC2OUT_VOL