I2C driver

This commit is contained in:
Emanuele Trabattoni
2025-06-21 16:34:11 +02:00
parent 2a33316ba8
commit 83a63c1241
6 changed files with 100 additions and 41 deletions

View File

@@ -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);
}

View File

@@ -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;
}
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<uint8_t> &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<uint8_t>(Wire.read());
}
busy.unlock();
return true;
}
const bool I2C::Write(const uint8_t deviceAddr, const uint8_t deviceReg, const std::vector<uint8_t> &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

View File

@@ -1,10 +1,25 @@
#pragma once
#include <Wire.h>
#include <vector>
#include <mutex>
#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);
public:
I2C(void);
~I2C(void);
const bool Read(const uint8_t deviceAddr, const uint8_t deviceReg, const uint8_t len, std::vector<uint8_t> &data);
const bool Write(const uint8_t deviceAddr, const uint8_t deviceReg, const std::vector<uint8_t> &data);
};
}

View File

@@ -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

View File

@@ -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)