SensorLib 0.5.1
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP517Irq.cpp
Go to the documentation of this file.
1
30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_AXP517
32
33#include "AXP517Irq.hpp"
34#include "AXP517Regs.hpp"
35#include "AXP517TcpcRegs.hpp"
36
37namespace
38{
39bool readPmicRegisters(AXP517Core &core, uint8_t firstReg, uint8_t *buffer,
40 size_t length)
41{
42 for (size_t i = 0; i < length; ++i) {
43 int value = core.readReg(static_cast<uint8_t>(firstReg + i));
44 if (value < 0) return false;
45 buffer[i] = static_cast<uint8_t>(value);
46 }
47 return true;
48}
49
50bool writePmicRegisters(AXP517Core &core, uint8_t firstReg,
51 const uint8_t *buffer, size_t length)
52{
53 for (size_t i = 0; i < length; ++i) {
54 if (core.writeReg(static_cast<uint8_t>(firstReg + i), buffer[i]) < 0) {
55 return false;
56 }
57 }
58 return true;
59}
60} // namespace
61
62AXP517Irq::AXP517Irq(AXP517Core &core) : _core(core)
63{
64}
65
66bool AXP517Irq::enable(uint64_t mask)
67{
68 mask &= ~IRQ_RESERVED_MASK;
69
70 uint8_t buffer[4];
71
72 if (!readPmicRegisters(_core, axp517_regs::irq::ENABLE0, buffer, sizeof(buffer))) {
73 SENSORLIB_LOG_E("Failed to read IRQ enable registers");
74 return false;
75 }
76 uint64_t current = (static_cast<uint64_t>(buffer[0]) << 24) |
77 (static_cast<uint64_t>(buffer[1]) << 16) |
78 (static_cast<uint64_t>(buffer[2]) << 8) |
79 (static_cast<uint64_t>(buffer[3]));
80 current &= (~IRQ_RESERVED_MASK);
81 current |= mask;
82 buffer[0] = static_cast<uint8_t>((current >> 24) & 0xFF);
83 buffer[1] = static_cast<uint8_t>((current >> 16) & 0xFF);
84 buffer[2] = static_cast<uint8_t>((current >> 8) & 0xFF);
85 buffer[3] = static_cast<uint8_t>(current & 0xFF);
86 if (!writePmicRegisters(_core, axp517_regs::irq::ENABLE0, buffer, sizeof(buffer))) {
87 return false;
88 }
89 return true;
90}
91
92bool AXP517Irq::disable(uint64_t mask)
93{
94 mask &= ~IRQ_RESERVED_MASK;
95
96 uint8_t buffer[4];
97 if (!readPmicRegisters(_core, axp517_regs::irq::ENABLE0, buffer, sizeof(buffer))) {
98 SENSORLIB_LOG_E("Failed to read IRQ enable registers");
99 return false;
100 }
101 uint64_t current = (static_cast<uint64_t>(buffer[0]) << 24) |
102 (static_cast<uint64_t>(buffer[1]) << 16) |
103 (static_cast<uint64_t>(buffer[2]) << 8) |
104 (static_cast<uint64_t>(buffer[3]));
105
106 current &= (~IRQ_RESERVED_MASK);
107 current &= (~mask);
108 buffer[0] = static_cast<uint8_t>((current >> 24) & 0xFF);
109 buffer[1] = static_cast<uint8_t>((current >> 16) & 0xFF);
110 buffer[2] = static_cast<uint8_t>((current >> 8) & 0xFF);
111 buffer[3] = static_cast<uint8_t>(current & 0xFF);
112 if (!writePmicRegisters(_core, axp517_regs::irq::ENABLE0, buffer, sizeof(buffer))) {
113 return false;
114 }
115 return true;
116}
117
118uint64_t AXP517Irq::readStatus(bool clear)
119{
120 uint8_t buffer[4];
121 int pdAlertL = _core.readReg(axp517::tcpc::PD_ALERTL);
122 int pdAlertH = _core.readReg(axp517::tcpc::PD_ALERTH);
123 if (pdAlertL < 0 || pdAlertH < 0) {
124 SENSORLIB_LOG_E("Failed to read PD alert registers");
125 return 0;
126 }
127
128 if (!readPmicRegisters(_core, axp517_regs::irq::STATUS0, buffer, sizeof(buffer))) {
129 SENSORLIB_LOG_E("Failed to read IRQ status registers");
130 return 0;
131 }
132
133 uint16_t pdAlert = static_cast<uint16_t>(pdAlertL) |
134 (static_cast<uint16_t>(pdAlertH) << 8);
135 uint64_t mask = (static_cast<uint64_t>(pdAlert) << 32) |
136 (static_cast<uint64_t>(buffer[0]) << 24) |
137 (static_cast<uint64_t>(buffer[1]) << 16) |
138 (static_cast<uint64_t>(buffer[2]) << 8) |
139 (static_cast<uint64_t>(buffer[3]));
140 mask &= (~IRQ_RESERVED_MASK);
141 if (clear && mask != 0) {
142 this->clearStatus();
143 }
144 return mask;
145}
146
148{
149 uint8_t buffer[4] = {0xFF, 0xFF, 0xFF, 0xFF};
150 int fault = _core.readReg(axp517::tcpc::FAULT_STATUS);
151 if (fault < 0) {
152 SENSORLIB_LOG_E("Failed to read PD fault status register");
153 return false;
154 }
155 uint8_t faultStatus = static_cast<uint8_t>(fault);
156 if (faultStatus && _core.writeReg(axp517::tcpc::FAULT_STATUS, faultStatus) < 0) {
157 SENSORLIB_LOG_E("Failed to clear PD fault status register");
158 return false;
159 }
160 if (_core.writeReg(axp517::tcpc::PD_ALERTL, 0xFF) < 0 ||
161 _core.writeReg(axp517::tcpc::PD_ALERTH, 0xFF) < 0) {
162 SENSORLIB_LOG_E("Failed to clear PD alert registers");
163 return false;
164 }
165 if (!writePmicRegisters(_core, axp517_regs::irq::STATUS0, buffer, sizeof(buffer))) {
166 SENSORLIB_LOG_E("Failed to clear IRQ status registers");
167 return false;
168 }
169 return true;
170}
171
172#endif
@license MIT License
@license MIT License
@license MIT License
#define SENSORLIB_LOG_E(...)
AXP517 low-level core driver.
bool enable(uint64_t mask) override
Enable one or more PMIC IRQ sources.
Definition AXP517Irq.cpp:66
bool disable(uint64_t mask) override
Disable one or more PMIC IRQ sources.
Definition AXP517Irq.cpp:92
uint64_t readStatus(bool clear=true) override
Read the current IRQ status.
AXP517Irq(AXP517Core &core)
Construct an AXP517 IRQ helper.
Definition AXP517Irq.cpp:62
bool clearStatus() override
Clear all pending PMIC IRQ and TCPC alert status bits.
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
uint8_t i
static constexpr uint8_t ENABLE0
IRQ Enable 0.
static constexpr uint8_t STATUS0
IRQ Status 0.