1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

ips/i2c: move deinit to stop instead of destructor

accessing register space from destructor can cause use after free errors

Signed-off-by: Niklas Eiling <niklas.eiling@eonerc.rwth-aachen.de>
This commit is contained in:
Niklas Eiling 2024-02-10 16:18:57 +01:00 committed by Niklas Eiling
parent 2a74f7e8c2
commit 2c72af935a
2 changed files with 6 additions and 1 deletions

View file

@ -34,6 +34,7 @@ public:
virtual bool init() override;
virtual bool check() override;
virtual bool reset() override;
virtual bool stop() override;
bool write(u8 address, std::vector<u8> &data);
bool read(u8 address, std::vector<u8> &data, size_t max_read);
bool readRegister(u8 address, u8 reg, std::vector<u8> &data, size_t max_read);

View file

@ -21,7 +21,7 @@ I2c::I2c()
xConfig(), hwLock(), configDone(false), initDone(false), polling(false),
switchInstance(nullptr) {}
I2c::~I2c() { I2c::reset(); }
I2c::~I2c() {}
static void SendHandler(I2c *i2c, __attribute__((unused)) int bytesSend) {
i2c->transmitIntrs++;
@ -43,6 +43,7 @@ bool I2c::init() {
return true;
}
xConfig.BaseAddress = getBaseAddr(registerMemory);
logger->debug("I2C base address: {:#x}", xConfig.BaseAddress);
hwLock.lock();
ret = XIic_CfgInitialize(&xIic, &xConfig, xConfig.BaseAddress);
if (ret != XST_SUCCESS) {
@ -67,7 +68,10 @@ bool I2c::check() {
return getSwitch().selfTest();
}
bool I2c::stop() { return reset(); }
bool I2c::reset() {
logger->debug("I2C reset");
// we cannot lock here because this may be called in a destructor
XIic_Reset(&xIic);
irqs[i2cInterrupt].irqController->disableInterrupt(irqs[i2cInterrupt]);