diff --git a/common/i2c.cc b/common/i2c.cc index 9508179c6e..3d6c79efc5 100644 --- a/common/i2c.cc +++ b/common/i2c.cc @@ -31,10 +31,14 @@ I2CBus::I2CBus(uint8_t bus_id) { } I2CBus::~I2CBus() { - if (i2c_fd >= 0) { close(i2c_fd); } + if (i2c_fd >= 0) { + close(i2c_fd); + } } int I2CBus::read_register(uint8_t device_address, uint register_address, uint8_t *buffer, uint8_t len) { + std::lock_guard lk(m); + int ret = 0; ret = HANDLE_EINTR(ioctl(i2c_fd, I2C_SLAVE, device_address)); @@ -48,6 +52,8 @@ fail: } int I2CBus::set_register(uint8_t device_address, uint register_address, uint8_t data) { + std::lock_guard lk(m); + int ret = 0; ret = HANDLE_EINTR(ioctl(i2c_fd, I2C_SLAVE, device_address)); diff --git a/common/i2c.h b/common/i2c.h index 0669116bb8..ca0d4635b8 100644 --- a/common/i2c.h +++ b/common/i2c.h @@ -1,12 +1,14 @@ #pragma once #include +#include #include class I2CBus { private: int i2c_fd; + std::mutex m; public: I2CBus(uint8_t bus_id);