RTC fix time lag correction

This commit is contained in:
Emanuele Trabattoni
2025-08-03 11:27:07 +02:00
parent b19ed89158
commit a1a66ebf8e
4 changed files with 23 additions and 3 deletions

Binary file not shown.

View File

@@ -1,5 +1,6 @@
#include "PCF85063_Driver.h"
#include <ctime>
#include <utils.h>
namespace drivers
{
@@ -18,7 +19,7 @@ namespace drivers
success &= m_i2c.write(m_address, RTC_CTRL_2_ADDR, {def_conf2});
}
// set clock correction pulses
const uint8_t correction = 0b10000011; // fast mode +3 correction pulses, because clock is beyond
const uint8_t correction = 0xE7; // fast mode -25 correction pulses, because clock is beyond
success &= m_i2c.write(m_address, RTC_OFFSET_ADDR, {correction});
if (!success)
LOG_ERROR("RTC Init Failure");
@@ -160,6 +161,22 @@ namespace drivers
return false;
}
const bool PCF85063::setOffset(const uint8_t ofst)
{
LOG_INFO("RTC set offset [", printHex(ofst).c_str(), "]");
return m_i2c.write(m_address, RTC_OFFSET_ADDR, {ofst});
}
const uint8_t PCF85063::getOffset()
{
std::vector<uint8_t> buf;
if (m_i2c.read(m_address, RTC_OFFSET_ADDR, 1, buf)) {
LOG_INFO("RTC get offset [", printHex(buf.front()).c_str(), "]");
return buf.front();
}
return {};
}
const std::string PCF85063::getTimeStr()
{
datetime_t dt;

View File

@@ -83,7 +83,7 @@ namespace drivers
} datetime_t;
public:
PCF85063(I2C &i2c, const uint8_t address, const uint8_t ctrl1 = RTC_CTRL_1_DEFAULT, const uint8_t ctrl2 = RTC_CTRL_2_DEFAULT);
PCF85063(I2C &i2c, const uint8_t address = PCF85063_ADDRESS, const uint8_t ctrl1 = RTC_CTRL_1_DEFAULT, const uint8_t ctrl2 = RTC_CTRL_2_DEFAULT);
const bool reset(void);
@@ -100,6 +100,9 @@ namespace drivers
const bool readAlarm(datetime_t &time);
const bool getAlarmFlag(uint8_t &flags);
const bool setOffset(const uint8_t ofst);
const uint8_t getOffset();
const std::string getTimeStr();
static const std::string datetime2str(const datetime_t &datetime);

View File

@@ -34,7 +34,7 @@ void loop()
// Declared here to keep devices local to the main loop otherwise the kernel crashes //
auto i2c = drivers::I2C();
auto bus = drivers::MODBUS(9600, SERIAL_8N1);
auto rtc = drivers::PCF85063(i2c, PCF85063_ADDRESS);
auto rtc = drivers::PCF85063(i2c);
auto eth = drivers::Ethernet(conf.m_ethHostname);
auto tmp = drivers::R4DCB08(bus, conf.m_modbusTemperatureAddr);
auto seneca = drivers::S50140(bus, conf.m_modbusSenecaAddr);