SensorLib 0.5.0
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
37AXP517Irq::AXP517Irq(AXP517Core &core) : _core(core)
38{
39}
40
41bool AXP517Irq::enable(uint64_t mask)
42{
43 mask &= ~IRQ_RESERVED_MASK;
44
45 uint8_t buffer[4];
46
47 if (_core.readRegBuff(axp517_regs::irq::ENABLE0, buffer, sizeof(buffer)) < 0) {
48 SENSORLIB_LOG_E("Failed to read IRQ enable registers");
49 return false;
50 }
51 uint64_t current = (static_cast<uint64_t>(buffer[0]) << 24) |
52 (static_cast<uint64_t>(buffer[1]) << 16) |
53 (static_cast<uint64_t>(buffer[2]) << 8) |
54 (static_cast<uint64_t>(buffer[3]));
55 current &= (~IRQ_RESERVED_MASK);
56 current |= mask;
57 buffer[0] = static_cast<uint8_t>((current >> 24) & 0xFF);
58 buffer[1] = static_cast<uint8_t>((current >> 16) & 0xFF);
59 buffer[2] = static_cast<uint8_t>((current >> 8) & 0xFF);
60 buffer[3] = static_cast<uint8_t>(current & 0xFF);
61 if (_core.writeRegBuff(axp517_regs::irq::ENABLE0, buffer, sizeof(buffer)) < 0) {
62 return false;
63 }
64 return true;
65}
66
67bool AXP517Irq::disable(uint64_t mask)
68{
69 mask &= ~IRQ_RESERVED_MASK;
70
71 uint8_t buffer[4];
72 if (_core.readRegBuff(axp517_regs::irq::ENABLE0, buffer, sizeof(buffer)) < 0) {
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
81 current &= (~IRQ_RESERVED_MASK);
82 current &= (~mask);
83 buffer[0] = static_cast<uint8_t>((current >> 24) & 0xFF);
84 buffer[1] = static_cast<uint8_t>((current >> 16) & 0xFF);
85 buffer[2] = static_cast<uint8_t>((current >> 8) & 0xFF);
86 buffer[3] = static_cast<uint8_t>(current & 0xFF);
87 if (_core.writeRegBuff(axp517_regs::irq::ENABLE0, buffer, sizeof(buffer)) < 0) {
88 return false;
89 }
90 return true;
91}
92
93uint64_t AXP517Irq::readStatus(bool clear)
94{
95 uint8_t buffer[4];
96 int pdAlertL = _core.readReg(axp517::tcpc::PD_ALERTL);
97 int pdAlertH = _core.readReg(axp517::tcpc::PD_ALERTH);
98 if (pdAlertL < 0 || pdAlertH < 0) {
99 SENSORLIB_LOG_E("Failed to read PD alert registers");
100 return 0;
101 }
102
103 if (_core.readRegBuff(axp517_regs::irq::STATUS0, buffer, sizeof(buffer)) < 0) {
104 SENSORLIB_LOG_E("Failed to read IRQ status registers");
105 return 0;
106 }
107
108 uint16_t pdAlert = static_cast<uint16_t>(pdAlertL) |
109 (static_cast<uint16_t>(pdAlertH) << 8);
110 uint64_t mask = (static_cast<uint64_t>(pdAlert) << 32) |
111 (static_cast<uint64_t>(buffer[0]) << 24) |
112 (static_cast<uint64_t>(buffer[1]) << 16) |
113 (static_cast<uint64_t>(buffer[2]) << 8) |
114 (static_cast<uint64_t>(buffer[3]));
115 mask &= (~IRQ_RESERVED_MASK);
116 if (clear && mask != 0) {
117 this->clearStatus();
118 }
119 return mask;
120}
121
123{
124 uint8_t buffer[4] = {0xFF, 0xFF, 0xFF, 0xFF};
125 int fault = _core.readReg(axp517::tcpc::FAULT_STATUS);
126 if (fault < 0) {
127 SENSORLIB_LOG_E("Failed to read PD fault status register");
128 return false;
129 }
130 uint8_t faultStatus = static_cast<uint8_t>(fault);
131 if (faultStatus && _core.writeReg(axp517::tcpc::FAULT_STATUS, faultStatus) < 0) {
132 SENSORLIB_LOG_E("Failed to clear PD fault status register");
133 return false;
134 }
135 if (_core.writeReg(axp517::tcpc::PD_ALERTL, 0xFF) < 0 ||
136 _core.writeReg(axp517::tcpc::PD_ALERTH, 0xFF) < 0) {
137 SENSORLIB_LOG_E("Failed to clear PD alert registers");
138 return false;
139 }
140 if (_core.writeRegBuff(axp517_regs::irq::STATUS0, buffer, sizeof(buffer)) < 0) {
141 SENSORLIB_LOG_E("Failed to clear IRQ status registers");
142 return false;
143 }
144 return true;
145}
146
147#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:41
bool disable(uint64_t mask) override
Disable one or more PMIC IRQ sources.
Definition AXP517Irq.cpp:67
uint64_t readStatus(bool clear=true) override
Read the current IRQ status.
Definition AXP517Irq.cpp:93
AXP517Irq(AXP517Core &core)
Construct an AXP517 IRQ helper.
Definition AXP517Irq.cpp:37
bool clearStatus() override
Clear all pending PMIC IRQ and TCPC alert status bits.
int readReg(uint8_t reg) const
int writeRegBuff(uint8_t reg, uint8_t *buf, size_t len)
int writeReg(uint8_t reg, uint8_t val)
int readRegBuff(uint8_t reg, uint8_t *buf, size_t len) const
static constexpr uint8_t ENABLE0
IRQ Enable 0.
static constexpr uint8_t STATUS0
IRQ Status 0.