SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP192Gpio.cpp
Go to the documentation of this file.
1#include "../../../SensorBuildOpt.h"
2#if !SENSORLIB_EXCLUDE_PMIC_AXP192
3
4#include "AXP192Gpio.hpp"
5#include "AXP192Regs.hpp"
6
7
9{
10}
11
13{
14 return 6;
15}
16
18{
19 switch (pin) {
20 case 0:
21 // GPIO0_CTL[2:0]: 000=open-drain out, 001=input
23 dir == Direction::Input ? 0x01 : 0x00) >= 0 ? Status::Ok : Status::Failed;
24 case 1:
25 // GPIO1_CTL[2:0]: 000=open-drain out, 001=input
27 dir == Direction::Input ? 0x01 : 0x00) >= 0 ? Status::Ok : Status::Failed;
28 case 2:
29 // GPIO2_CTL[2:0]: 000=open-drain out, 001=input
31 dir == Direction::Input ? 0x01 : 0x00) >= 0 ? Status::Ok : Status::Failed;
32 case 3: {
33 // GPIO34_CTL[7]=1 enables GPIO mode for GPIO3/4.
34 // GPIO3 mode: [1:0] 01=output, 10=input.
36 if (v < 0) return Status::Failed;
37 v |= 0x80;
38 v &= ~0x03;
39 v |= (dir == Direction::Input) ? 0x02 : 0x01;
40 return _core.writeReg(axp192_regs::gpio::GPIO34_CTL, static_cast<uint8_t>(v)) >= 0 ? Status::Ok : Status::Failed;
41 }
42 case 4: {
43 // GPIO34_CTL[7]=1 enables GPIO mode for GPIO3/4.
44 // GPIO4 mode: [3:2] 01=output, 10=input.
46 if (v < 0) return Status::Failed;
47 v |= 0x80;
48 v &= ~0x0C;
49 v |= (dir == Direction::Input) ? 0x08 : 0x04;
50 return _core.writeReg(axp192_regs::gpio::GPIO34_CTL, static_cast<uint8_t>(v)) >= 0 ? Status::Ok : Status::Failed;
51 }
52 case 5: {
53 // GPIO5_CTL[7]=1 GPIO5 mode, GPIO5_CTL[6]: 0=output,1=input
55 if (v < 0) return Status::Failed;
56 v |= 0x80;
57 if (dir == Direction::Input) {
58 v |= 0x40;
59 } else {
60 v &= ~0x40;
61 }
62 return _core.writeReg(axp192_regs::gpio::GPIO5_CTL, static_cast<uint8_t>(v)) >= 0 ? Status::Ok : Status::Failed;
63 }
64 default:
65 return Status::InvalidPin;
66 }
67}
68
70{
71 int v;
72 switch (pin) {
73 case 0:
75 if (v < 0) return Direction::Input;
76 return ((v & 0x07) == 0x01) ? Direction::Input : Direction::Output;
77 case 1:
79 if (v < 0) return Direction::Input;
80 return ((v & 0x07) == 0x01) ? Direction::Input : Direction::Output;
81 case 2:
83 if (v < 0) return Direction::Input;
84 return ((v & 0x07) == 0x01) ? Direction::Input : Direction::Output;
85 case 3:
87 if (v < 0) return Direction::Input;
88 return ((v & 0x03) == 0x02) ? Direction::Input : Direction::Output;
89 case 4:
91 if (v < 0) return Direction::Input;
92 return (((v >> 2) & 0x03) == 0x02) ? Direction::Input : Direction::Output;
93 case 5:
95 if (v < 0) return Direction::Input;
96 return (v & 0x40) ? Direction::Input : Direction::Output;
97 default:
98 return Direction::Input;
99 }
100}
101
103{
104 if (pin >= getPinCount()) return Status::InvalidPin;
105 (void)drive;
106 // AXP192 GPIO drive type is implicit in function select (REG 90H/92H/93H).
107 // 0x00 = NMOS open-drain output. Cannot be independently configured.
108 return Status::Failed;
109}
110
112{
113 (void)pin;
114 // AXP192 GPIOs are NMOS open-drain when used as GPIO output (function 0x00)
116}
117
119{
120 uint8_t reg;
121 uint8_t bit;
122 switch (pin) {
123 case 0: reg = axp192_regs::gpio::GPIO012_SIGNAL; bit = 4; break;
124 case 1: reg = axp192_regs::gpio::GPIO012_SIGNAL; bit = 5; break;
125 case 2: reg = axp192_regs::gpio::GPIO012_SIGNAL; bit = 6; break;
126 case 3: reg = axp192_regs::gpio::GPIO34_SIGNAL; bit = 4; break;
127 case 4: reg = axp192_regs::gpio::GPIO34_SIGNAL; bit = 5; break;
128 case 5: reg = axp192_regs::gpio::GPIO5_CTL; bit = 4; break;
129 default: return Status::InvalidPin;
130 }
131 high = _core.getRegBit(reg, bit);
132 return Status::Ok;
133}
134
136{
137 uint8_t reg;
138 uint8_t bit;
139
140 // AXP192 GPIO outputs are open-drain for most pins:
141 // Low -> NMOS on, HiZ/High -> NMOS off.
142 bool release = (level == Level::HiZ || level == Level::High);
143
144 switch (pin) {
145 case 0: reg = axp192_regs::gpio::GPIO012_SIGNAL; bit = 0; break;
146 case 1: reg = axp192_regs::gpio::GPIO012_SIGNAL; bit = 1; break;
147 case 2: reg = axp192_regs::gpio::GPIO012_SIGNAL; bit = 2; break;
148 case 3: reg = axp192_regs::gpio::GPIO34_SIGNAL; bit = 0; break;
149 case 4: reg = axp192_regs::gpio::GPIO34_SIGNAL; bit = 1; break;
150 case 5: reg = axp192_regs::gpio::GPIO5_CTL; bit = 5; break;
151 default: return Status::InvalidPin;
152 }
153
154 return (release ? _core.setRegBit(reg, bit) : _core.clrRegBit(reg, bit)) ? Status::Ok : Status::Failed;
155}
156
158{
159 switch (pin) {
160 case 0:
161 // REG 90H [2:0]: GPIO0 function select
162 return _core.updateBits(axp192_regs::gpio::GPIO0_CTL, 0x07, func & 0x07) >= 0 ? Status::Ok : Status::Failed;
163 case 1:
164 // REG 92H [2:0]: GPIO1 function select
165 return _core.updateBits(axp192_regs::gpio::GPIO1_CTL, 0x07, func & 0x07) >= 0 ? Status::Ok : Status::Failed;
166 case 2:
167 // REG 93H [2:0]: GPIO2 function select
168 return _core.updateBits(axp192_regs::gpio::GPIO2_CTL, 0x07, func & 0x07) >= 0 ? Status::Ok : Status::Failed;
169 case 3: {
170 // REG 95H: GPIO3/4 shared register. bit7=1 enables GPIO mode.
171 // GPIO3 function: [1:0] 00=ext charge, 01=NMOS out, 10=input, 11=ADC
173 if (v < 0) return Status::Failed;
174 v |= 0x80; // enable GPIO mode
175 v &= ~0x03;
176 v |= (func & 0x03);
177 return _core.writeReg(axp192_regs::gpio::GPIO34_CTL, static_cast<uint8_t>(v)) >= 0 ? Status::Ok : Status::Failed;
178 }
179 case 4: {
180 // REG 95H: GPIO4 function: [3:2] 00=ext charge, 01=NMOS out, 10=input
182 if (v < 0) return Status::Failed;
183 v |= 0x80; // enable GPIO mode
184 v &= ~0x0C;
185 v |= ((func & 0x03) << 2);
186 return _core.writeReg(axp192_regs::gpio::GPIO34_CTL, static_cast<uint8_t>(v)) >= 0 ? Status::Ok : Status::Failed;
187 }
188 case 5: {
189 // REG 9EH: GPIO5/N_RSTO. bit7=1 for GPIO5 mode, bit6=direction.
190 // No function multiplexing beyond input/output. Only 0=output, 1=input.
192 if (v < 0) return Status::Failed;
193 v |= 0x80; // enable GPIO5 mode
194 if (func == 0x01) {
195 v |= 0x40; // input
196 } else {
197 v &= ~0x40; // output
198 }
199 return _core.writeReg(axp192_regs::gpio::GPIO5_CTL, static_cast<uint8_t>(v)) >= 0 ? Status::Ok : Status::Failed;
200 }
201 default:
202 return Status::InvalidPin;
203 }
204}
205
206uint8_t AXP192Gpio::getFunction(uint8_t pin)
207{
208 int v;
209 switch (pin) {
210 case 0:
212 if (v < 0) return 0xFF;
213 return v & 0x07;
214 case 1:
216 if (v < 0) return 0xFF;
217 return v & 0x07;
218 case 2:
220 if (v < 0) return 0xFF;
221 return v & 0x07;
222 case 3:
224 if (v < 0) return 0xFF;
225 return v & 0x03;
226 case 4:
228 if (v < 0) return 0xFF;
229 return (v >> 2) & 0x03;
230 case 5:
232 if (v < 0) return 0xFF;
233 return (v & 0x40) ? 0x01 : 0x00;
234 default:
235 return 0xFF;
236 }
237}
238
240{
241 if (pin >= getPinCount()) return Status::InvalidPin;
242 if (pin > 2) return Status::Failed;
243 // REG 97H [2:0]: GPIO0-2 pull-down enable (1=enable, 0=disable)
244 // Only pull-down is supported, pull-up returns false
245 if (pull == Pull::Up) return Status::Failed;
246 uint8_t bit = (1 << pin);
247 if (pull == Pull::Down) {
249 }
250 // Pull::None
252}
253
255{
256 if (pin > 2) return Pull::None;
257 // REG 97H [2:0]: GPIO0-2 pull-down enable
259}
260
261bool AXP192Gpio::setPwmFrequency(uint8_t pin, uint8_t x)
262{
263 // PWM1 → GPIO1 (REG 0x98), PWM2 → GPIO2 (REG 0x9B)
264 uint8_t reg;
265 switch (pin) {
266 case 1: reg = axp192_regs::gpio::PWM1_FREQ; break;
267 case 2: reg = axp192_regs::gpio::PWM2_FREQ; break;
268 default: return false;
269 }
270 return _core.writeReg(reg, x) >= 0;
271}
272
273bool AXP192Gpio::setPwmDutyCycle(uint8_t pin, uint8_t y1, uint8_t y2)
274{
275 // PWM1 → GPIO1 (REG 0x99=Y1, 0x9A=Y2), PWM2 → GPIO2 (REG 0x9C=Y1, 0x9D=Y2)
276 uint8_t reg_y1, reg_y2;
277 switch (pin) {
278 case 1:
281 break;
282 case 2:
285 break;
286 default:
287 return false;
288 }
289 if (y1 == 0) return false; // Y1 must be > 0 to avoid division by zero
290 if (_core.writeReg(reg_y1, y1) < 0) return false;
291 return _core.writeReg(reg_y2, y2) >= 0;
292}
293
294bool AXP192Gpio::setPwm(uint8_t pin, uint8_t x, uint8_t y1, uint8_t y2)
295{
296 // Set pin function to PWM mode (0x02)
297 if (!isOk(setFunction(pin, 0x02))) return false;
298 // Set direction to output
299 if (!isOk(setDirection(pin, Direction::Output))) return false;
300 // Configure frequency
301 if (!setPwmFrequency(pin, x)) return false;
302 // Configure duty cycle
303 return setPwmDutyCycle(pin, y1, y2);
304}
305
306#endif
@license MIT License
@license MIT License
uint8_t level
Status setDirection(uint8_t pin, Direction dir) override
Set GPIO pin direction.
bool setPwm(uint8_t pin, uint8_t x, uint8_t y1, uint8_t y2)
Configure PWM output parameters in one call.
Status setFunction(uint8_t pin, uint8_t func) override
Set GPIO pin function mode.
AXP192Gpio(AXP192Core &core)
Construct GPIO interface.
Definition AXP192Gpio.cpp:8
uint8_t getFunction(uint8_t pin) override
Get current GPIO pin function mode.
Status setPull(uint8_t pin, Pull pull) override
Set internal pull-down resistor.
Status write(uint8_t pin, Level level) override
Write digital level to a GPIO pin.
bool setPwmFrequency(uint8_t pin, uint8_t x)
Set PWM output frequency for GPIO1 (PWM1) or GPIO2 (PWM2)
DriveType getDrive(uint8_t pin) override
Get GPIO pin drive type.
Direction getDirection(uint8_t pin) override
Get GPIO pin direction.
Status setDrive(uint8_t pin, DriveType drive) override
Set GPIO pin drive type.
uint8_t getPinCount() const override
Get number of GPIO pins (6: GPIO0-GPIO5)
bool setPwmDutyCycle(uint8_t pin, uint8_t y1, uint8_t y2)
Set PWM duty cycle for GPIO1 (PWM1) or GPIO2 (PWM2)
Pull getPull(uint8_t pin) override
Get current pull-down setting.
Status read(uint8_t pin, bool &high) override
Read digital level of a GPIO pin.
DriveType
GPIO drive type.
@ OpenDrain
Open-drain output: only pulls low, high-impedance for high.
Level
Output level control.
@ High
Logic high (active drive high)
@ HiZ
High-impedance (input mode or open-drain not pulling low)
Pull
Internal pull resistor configuration.
@ Down
Pull-down enabled.
@ Up
Pull-up enabled.
@ None
No pull resistor.
static constexpr bool isOk(Status s)
Direction
GPIO pin direction.
@ Output
Configure pin as output (drive external circuits)
@ Input
Configure pin as input (read external signals)
Status
GPIO operation status code.
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 constexpr uint8_t GPIO1_CTL
static constexpr uint8_t PWM2_FREQ
static constexpr uint8_t GPIO012_SIGNAL
static constexpr uint8_t GPIO34_SIGNAL
static constexpr uint8_t PWM2_DUTY2
static constexpr uint8_t GPIO2_CTL
static constexpr uint8_t GPIO012_PULLDOWN
static constexpr uint8_t GPIO0_CTL
static constexpr uint8_t PWM1_FREQ
static constexpr uint8_t PWM1_DUTY2
static constexpr uint8_t PWM1_DUTY1
static constexpr uint8_t GPIO34_CTL
static constexpr uint8_t GPIO5_CTL
static constexpr uint8_t PWM2_DUTY1
int16_t x[5]