SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP517Gpio.cpp
Go to the documentation of this file.
1#include "../../../SensorBuildOpt.h"
2#if !SENSORLIB_EXCLUDE_PMIC_AXP517
3
4#include "AXP517Gpio.hpp"
5#include "AXP517Regs.hpp"
6
7
9 : _core(core)
10{
11}
12
14{
15 return 1;
16}
17
22
24{
25 if (pin != 0) return Status::InvalidPin;
26 // bit4: 0 input, 1 output
27 return _core.updateBits(axp517_regs::ctrl::GPIO_CFG, 0x10, (dir == Direction::Output) ? 0x10 : 0x00) >= 0 ? Status::Ok : Status::Failed;
28}
29
31{
32 if (pin != 0) return Direction::Input;
33 int v = readRaw();
34 if (v < 0) return Direction::Input;
35 return (v & 0x10) ? Direction::Output : Direction::Input;
36}
37
39{
40 if (pin != 0) return Status::InvalidPin;
41 // bit7: 0 floating(push-pull), 1 open drain output
42 return _core.updateBits(axp517_regs::ctrl::GPIO_CFG, 0x80, (drive == DriveType::OpenDrain) ? 0x80 : 0x00) >= 0 ? Status::Ok : Status::Failed;
43}
44
46{
47 if (pin != 0) return DriveType::PushPull;
48 int v = readRaw();
49 if (v < 0) return DriveType::PushPull;
50 return (v & 0x80) ? DriveType::OpenDrain : DriveType::PushPull;
51}
52
54{
55 if (pin != 0) return false;
56 // bit[3:2]: 0=by reg11[1:0], 1=PD_IRQ
57 uint8_t field = (src == OutputSource::PdIrq) ? 0x01 : 0x00;
58 return _core.updateBits(axp517_regs::ctrl::GPIO_CFG, 0x0C, (uint8_t)(field << 2)) >= 0;
59}
60
62{
63 if (pin != 0) return OutputSource::ByReg11;
64 int v = readRaw();
65 if (v < 0) return OutputSource::ByReg11;
66
67 uint8_t field = (uint8_t)((v >> 2) & 0x03);
68 return (field == 0x01) ? OutputSource::PdIrq : OutputSource::ByReg11;
69}
70
71PmicGpioBase::Status AXP517Gpio::read(uint8_t pin, bool &high)
72{
73 if (pin != 0) return Status::InvalidPin;
74 int v = readRaw();
75 if (v < 0) return Status::Failed;
76
77 // bit1: GPIO input status
78 high = (v & 0x02) != 0;
79 return Status::Ok;
80}
81
83{
84 if (pin != 0) return Status::InvalidPin;
85
87 return Status::Failed;
88 }
89
90 if (getDirection(pin) != Direction::Output) {
91 return Status::Failed;
92 }
93
94 DriveType drive = getDrive(pin);
95
96 if (drive == DriveType::OpenDrain) {
97 // bit6: OD output configure: 0 hiz, 1 low
98 // Open-drain does not support forced drive high: High -> HiZ (external pull-up)
99 uint8_t od = (level == Level::Low) ? 0x40 : 0x00;
101 }
102
103 uint8_t code = 0;
104 switch (level) {
105 case Level::HiZ: code = 0b00; break;
106 case Level::Low: code = 0b01; break;
107 case Level::High: code = 0b10; break;
108 default: code = 0b00; break;
109 }
110
111 // bits[1:0]
112 return _core.updateBits(axp517_regs::ctrl::GPIO_CFG, 0x03, code) >= 0 ? Status::Ok : Status::Failed;
113}
114
115#endif
@license MIT License
@license MIT License
uint8_t level
AXP517 low-level core driver.
DriveType getDrive(uint8_t pin) override
Get GPIO pin drive type.
OutputSource getOutputSource(uint8_t pin)
Get current GPIO output source.
bool setOutputSource(uint8_t pin, OutputSource src)
Select GPIO output source.
Status write(uint8_t pin, Level level) override
Set output level.
uint8_t getPinCount() const override
Get number of GPIO pins (1)
Status setDirection(uint8_t pin, Direction dir) override
Set GPIO pin direction.
AXP517Gpio(AXP517Core &core)
Construct an AXP517 GPIO driver.
Definition AXP517Gpio.cpp:8
Status setDrive(uint8_t pin, DriveType drive) override
Set GPIO pin drive type.
Status read(uint8_t pin, bool &high) override
Read input level.
OutputSource
Hardware source selected for the GPIO output path.
@ ByReg11
by reg11[1:0]
Direction getDirection(uint8_t pin) override
Get GPIO pin direction.
int readRaw()
Read raw REG11 for debug.
DriveType
GPIO drive type.
@ OpenDrain
Open-drain output: only pulls low, high-impedance for high.
@ PushPull
Push-pull output: actively drives high or low.
Level
Output level control.
@ Low
Logic low (active drive low or open-drain pulled low)
@ High
Logic high (active drive high)
@ HiZ
High-impedance (input mode or open-drain not pulling low)
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.
int readReg(uint8_t reg) const
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
static constexpr uint8_t GPIO_CFG
GPIO configure.