From 83a63c1241862276176aa42c853f2dfc7a537a66 Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Sat, 21 Jun 2025 16:34:11 +0200 Subject: [PATCH] I2C driver --- lib/ETH/WS_ETH.cpp | 2 +- lib/I2C/I2C_Driver.cpp | 110 +++++++++++++++++++++---------- lib/I2C/I2C_Driver.h | 25 +++++-- lib/{ETH => RTC}/WS_PCF85063.cpp | 2 +- lib/{ETH => RTC}/WS_PCF85063.h | 0 src/MAIN_WIFI_MQTT.cpp | 2 +- 6 files changed, 100 insertions(+), 41 deletions(-) rename lib/{ETH => RTC}/WS_PCF85063.cpp (98%) rename lib/{ETH => RTC}/WS_PCF85063.h (100%) diff --git a/lib/ETH/WS_ETH.cpp b/lib/ETH/WS_ETH.cpp index ce31fb4..029516a 100644 --- a/lib/ETH/WS_ETH.cpp +++ b/lib/ETH/WS_ETH.cpp @@ -116,5 +116,5 @@ void Acquisition_time(void) { // Get the netwo PCF85063_Time.hour = localTime->tm_hour; PCF85063_Time.minute = localTime->tm_min; PCF85063_Time.second = localTime->tm_sec; - PCF85063_Set_All(PCF85063_Time); + //PCF85063_Set_All(PCF85063_Time); } diff --git a/lib/I2C/I2C_Driver.cpp b/lib/I2C/I2C_Driver.cpp index 826bfda..dc16728 100644 --- a/lib/I2C/I2C_Driver.cpp +++ b/lib/I2C/I2C_Driver.cpp @@ -1,36 +1,80 @@ #include "I2C_Driver.h" - -void I2C_Init(void) { - Wire.begin( I2C_SDA_PIN, I2C_SCL_PIN); -} - - -bool I2C_Read(uint8_t Driver_addr, uint8_t Reg_addr, uint8_t *Reg_data, uint32_t Length) +namespace drivers { - Wire.beginTransmission(Driver_addr); - Wire.write(Reg_addr); - if ( Wire.endTransmission(true)){ - printf("The I2C transmission fails. - I2C Read\r\n"); - return -1; - } - Wire.requestFrom(Driver_addr, Length); - for (int i = 0; i < Length; i++) { - *Reg_data++ = Wire.read(); - } - return 0; -} -bool I2C_Write(uint8_t Driver_addr, uint8_t Reg_addr, const uint8_t *Reg_data, uint32_t Length) -{ - Wire.beginTransmission(Driver_addr); - Wire.write(Reg_addr); - for (int i = 0; i < Length; i++) { - Wire.write(*Reg_data++); - } - if ( Wire.endTransmission(true)) - { - printf("The I2C transmission fails. - I2C Write\r\n"); - return -1; - } - return 0; -} \ No newline at end of file + + I2C::I2C() + { + Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN); + isInitialized = true; + } + + I2C::~I2C() + { + Wire.end(); + isInitialized = true; + } + + const bool I2C::Read(const uint8_t deviceAddr, const uint8_t deviceReg, const uint8_t len, std::vector &data) + { + busy.try_lock(); + Wire.beginTransmission(deviceAddr); + Wire.write(deviceReg); + switch (Wire.endTransmission(true)) + { + case 0: + break; // no error, break switch + case 1: + log_e("Data to long to fit in buffer: [%d]", len); + case 2: + log_e("Received NAK on address transmit"); + case 3: + log_e("Received NAK on data transmit"); + case 4: + log_e("Unknown Error"); + return false; + } + const uint8_t nBytes = Wire.requestFrom(deviceAddr, len); + if (nBytes < len) + { + log_w("Received data is less than expected: len[%d], nBytes[%d]", len, nBytes); + } + data.clear(); + data.resize(nBytes); // resize out buffer to received data len, no check if data len is correct + for (auto i = 0; i < nBytes; i++) + { + data[i] = static_cast(Wire.read()); + } + busy.unlock(); + return true; + } + + const bool I2C::Write(const uint8_t deviceAddr, const uint8_t deviceReg, const std::vector &data) + { + busy.lock(); + Wire.beginTransmission(deviceAddr); + Wire.write(deviceReg); + for (auto d : data) + { + Wire.write(d); + } + + switch (Wire.endTransmission(true)) + { + case 0: + break; // no error, break switch + case 1: + log_e("Data to long to fit in buffer: [%d]", data.size()); + case 2: + log_e("Received NAK on address transmit"); + case 3: + log_e("Received NAK on data transmit"); + case 4: + log_e("Unknown Error"); + return false; + } + busy.unlock(); + return true; + } + +} // namespace drivers diff --git a/lib/I2C/I2C_Driver.h b/lib/I2C/I2C_Driver.h index ca8d388..c982c3d 100644 --- a/lib/I2C/I2C_Driver.h +++ b/lib/I2C/I2C_Driver.h @@ -1,10 +1,25 @@ #pragma once #include +#include +#include -#define I2C_SCL_PIN 41 -#define I2C_SDA_PIN 42 +#define I2C_SCL_PIN 41 +#define I2C_SDA_PIN 42 -void I2C_Init(void); +namespace drivers { + + class I2C { + private: + bool isInitialized = false; + std::mutex busy; -bool I2C_Read(uint8_t Driver_addr, uint8_t Reg_addr, uint8_t *Reg_data, uint32_t Length); -bool I2C_Write(uint8_t Driver_addr, uint8_t Reg_addr, const uint8_t *Reg_data, uint32_t Length); \ No newline at end of file + public: + I2C(void); + ~I2C(void); + + const bool Read(const uint8_t deviceAddr, const uint8_t deviceReg, const uint8_t len, std::vector &data); + const bool Write(const uint8_t deviceAddr, const uint8_t deviceReg, const std::vector &data); + + }; + +} diff --git a/lib/ETH/WS_PCF85063.cpp b/lib/RTC/WS_PCF85063.cpp similarity index 98% rename from lib/ETH/WS_PCF85063.cpp rename to lib/RTC/WS_PCF85063.cpp index 3a48448..c69201a 100644 --- a/lib/ETH/WS_PCF85063.cpp +++ b/lib/RTC/WS_PCF85063.cpp @@ -20,7 +20,7 @@ void PCF85063_Init(void) // PCF85063 initialized uint8_t Value = RTC_CTRL_1_DEFAULT|RTC_CTRL_1_CAP_SEL; I2C_Write(PCF85063_ADDRESS, RTC_CTRL_1_ADDR, &Value, 1); - I2C_Read(PCF85063_ADDRESS, RTC_CTRL_1_ADDR, &Value, 1); + I2C_Read(PCF85063_ADDRESS, RTC_CTRL_1_ADDR, &Value, 1); if(Value & RTC_CTRL_1_STOP) printf("PCF85063 failed to be initialized.state :%d\r\n",Value); else diff --git a/lib/ETH/WS_PCF85063.h b/lib/RTC/WS_PCF85063.h similarity index 100% rename from lib/ETH/WS_PCF85063.h rename to lib/RTC/WS_PCF85063.h diff --git a/src/MAIN_WIFI_MQTT.cpp b/src/MAIN_WIFI_MQTT.cpp index 055e7dd..b3df6c6 100644 --- a/src/MAIN_WIFI_MQTT.cpp +++ b/src/MAIN_WIFI_MQTT.cpp @@ -17,7 +17,7 @@ uint32_t Simulated_time=0; // Analog time counting void setup() { Flash_test(); GPIO_Init(); // RGB . Buzzer GPIO - I2C_Init(); + //I2C_Init(); RTC_Init();// RTC SD_Init(); Serial_Init(); // UART(RS485/CAN)