SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP202Channel.cpp
Go to the documentation of this file.
1
30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_AXP202
32
33#include "AXP202Channel.hpp"
34#include "AXP202Regs.hpp"
35
36
37const uint16_t AXP202Channel::ldo4Table_[16] = {
38 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900,
39 2000, 2500, 2700, 2800, 3000, 3100, 3200, 3300
40};
41
43{
44}
45
46bool AXP202Channel::enable(uint8_t channel, bool enable)
47{
48 if (channel >= CHANNEL_COUNT) return false;
49
50 switch (channel) {
51 case CH_DCDC2:
54 case CH_DCDC3:
57 case CH_LDO2:
60 case CH_LDO3:
63 case CH_LDO4:
66 case CH_EXTEN:
69 case CH_LDOio: {
71 if (val < 0) return false;
72 if (enable) {
73 val = (val & 0xF8) | 0x03;
74 } else {
75 val = (val & 0xF8);
76 }
77 return _core.writeReg(axp202_regs::gpio::GPIO0_CTL, static_cast<uint8_t>(val)) >= 0;
78 }
79 default:
80 return false;
81 }
82}
83
84bool AXP202Channel::isEnabled(uint8_t channel)
85{
86 if (channel >= CHANNEL_COUNT) return false;
87
88 switch (channel) {
89 case CH_DCDC2:
91 case CH_DCDC3:
93 case CH_LDO2:
95 case CH_LDO3:
97 case CH_LDO4:
99 case CH_EXTEN:
101 case CH_LDOio: {
102 int val = _core.readReg(axp202_regs::gpio::GPIO0_CTL);
103 if (val < 0) return false;
104 return (val & 0x07) == 0x03;
105 }
106 default:
107 return false;
108 }
109}
110
111bool AXP202Channel::setVoltage(uint8_t channel, uint16_t mV)
112{
113 if (channel >= CHANNEL_COUNT) return false;
114
115 switch (channel) {
116 case CH_DCDC2: {
117 if (mV < 700) mV = 700;
118 if (mV > 2275) mV = 2275;
119 uint8_t code = (mV - 700) / 25;
120 return _core.updateBits(axp202_regs::power::DC2OUT_VOL, 0x3F, code) >= 0;
121 }
122 case CH_DCDC3: {
123 if (mV < 700) mV = 700;
124 if (mV > 3500) mV = 3500;
125 uint8_t code = (mV - 700) / 25;
126 return _core.updateBits(axp202_regs::power::DC3OUT_VOL, 0x7F, code) >= 0;
127 }
128 case CH_LDO2: {
129 if (mV < 1800) mV = 1800;
130 if (mV > 3300) mV = 3300;
131 uint8_t code = (mV - 1800) / 100;
132 return _core.updateBits(axp202_regs::power::LDO24OUT_VOL, 0xF0, code << 4) >= 0;
133 }
134 case CH_LDO3: {
135 if (mV < 700) mV = 700;
136 if (mV > 3500) mV = 3500;
137 uint8_t code = (mV - 700) / 25;
138 return _core.updateBits(axp202_regs::power::LDO3OUT_VOL, 0x7F, code) >= 0;
139 }
140 case CH_LDO4: {
141 int index = 0;
142 uint16_t bestDiff = static_cast<uint16_t>(ldo4Table_[0] > mV ? (ldo4Table_[0] - mV) : (mV - ldo4Table_[0]));
143 for (int i = 0; i < 16; ++i) {
144 uint16_t diff = static_cast<uint16_t>(ldo4Table_[i] > mV ? (ldo4Table_[i] - mV) : (mV - ldo4Table_[i]));
145 if (diff < bestDiff) {
146 bestDiff = diff;
147 index = i;
148 }
149 }
150 return _core.updateBits(axp202_regs::power::LDO24OUT_VOL, 0x0F, static_cast<uint8_t>(index)) >= 0;
151 }
152 case CH_LDOio: {
153 if (mV < 1800) mV = 1800;
154 if (mV > 3300) mV = 3300;
155 uint8_t code = (mV - 1800) / 100;
156 return _core.updateBits(axp202_regs::gpio::GPIO0_VOL, 0xF0, code << 4) >= 0;
157 }
158 case CH_EXTEN:
159 return false;
160 default:
161 return false;
162 }
163}
164
165uint16_t AXP202Channel::getVoltage(uint8_t channel)
166{
167 if (channel >= CHANNEL_COUNT) return 0;
168
169 switch (channel) {
170 case CH_DCDC2: {
172 if (val < 0) return 0;
173 return ((val & 0x3F) * 25) + 700;
174 }
175 case CH_DCDC3: {
177 if (val < 0) return 0;
178 return ((val & 0x7F) * 25) + 700;
179 }
180 case CH_LDO2: {
182 if (val < 0) return 0;
183 uint8_t code = (val >> 4) & 0x0F;
184 return (code * 100) + 1800;
185 }
186 case CH_LDO3: {
188 if (val < 0) return 0;
189 if (val & 0x80) {
190 return 0;
191 }
192 return ((val & 0x7F) * 25) + 700;
193 }
194 case CH_LDO4: {
196 if (val < 0) return 0;
197 uint8_t idx = val & 0x0F;
198 if (idx >= 16) return 0;
199 return ldo4Table_[idx];
200 }
201 case CH_LDOio: {
202 int val = _core.readReg(axp202_regs::gpio::GPIO0_VOL);
203 if (val < 0) return 0;
204 uint8_t code = (val >> 4) & 0x0F;
205 return (code * 100) + 1800;
206 }
207 case CH_EXTEN:
208 return 0;
209 default:
210 return 0;
211 }
212}
213
214uint8_t AXP202Channel::count() const
215{
216 return CHANNEL_COUNT;
217}
218
220{
221 static const Info channelInfo[] = {
222 {CH_DCDC2, Type::DCDC, 700, 2275, 25, "DCDC2"},
223 {CH_DCDC3, Type::DCDC, 700, 3500, 25, "DCDC3"},
224 {CH_LDO2, Type::LDO, 1800, 3300, 100, "LDO2"},
225 {CH_LDO3, Type::LDO, 700, 3500, 25, "LDO3"},
226 {CH_LDO4, Type::LDO, 1250, 3300, 0, "LDO4"},
227 {CH_LDOio, Type::LDO, 1800, 3300, 100, "LDOio"},
228 {CH_EXTEN, Type::LDO, 0, 0, 0, "EXTEN"},
229 };
230 if (channel >= CHANNEL_COUNT) {
231 return {0, Type::DCDC, 0, 0, 0, "UNKNOWN"};
232 }
233 return channelInfo[channel];
234}
235
236#endif
@license MIT License
@license MIT License
static constexpr uint8_t CH_LDO3
static constexpr uint8_t CH_DCDC2
static constexpr uint8_t CH_EXTEN
bool setVoltage(uint8_t channel, uint16_t mV) override
Set the output voltage of a power channel.
bool enable(uint8_t channel, bool enable) override
Enable or disable a power output channel.
uint16_t getVoltage(uint8_t channel) override
Get the current output voltage of a power channel.
uint8_t count() const override
Get the number of available power channels.
static constexpr uint8_t CH_LDO4
AXP202Channel(AXP202Core &core)
Construct AXP202 channel control interface.
static constexpr uint8_t CH_LDO2
static constexpr uint8_t CH_LDOio
static constexpr uint8_t CHANNEL_COUNT
static constexpr uint8_t CH_DCDC3
Info getInfo(uint8_t channel) const override
Get information about a specific channel.
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)
uint8_t i
Static metadata for a single power output channel.
static constexpr uint8_t GPIO0_CTL
static constexpr uint8_t GPIO0_VOL
static constexpr uint8_t LDO24OUT_VOL
static constexpr uint8_t LDO234_DC23_CTL
static constexpr uint8_t DC3OUT_VOL
static constexpr uint8_t DC2OUT_VOL
static constexpr uint8_t LDO3OUT_VOL
static constexpr uint8_t BIT_DCDC3
static constexpr uint8_t BIT_EXTEN
static constexpr uint8_t BIT_DCDC2
static constexpr uint8_t BIT_LDO2
static constexpr uint8_t BIT_LDO4
static constexpr uint8_t BIT_LDO3