RTC Driver OK

This commit is contained in:
Emanuele Trabattoni
2025-07-10 17:11:21 +02:00
parent 8f701ce81a
commit 7fd4a284af
4 changed files with 34 additions and 7 deletions

View File

@@ -3,8 +3,7 @@
#include <cstring> #include <cstring>
#include <endian.h> #include <endian.h>
#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE //#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE
#include "utils.h" #include "utils.h"
namespace drivers namespace drivers

View File

@@ -1,4 +1,4 @@
#include "WS_PCF85063.h" #include "PCF85063_Driver.h"
#include <ctime> #include <ctime>
namespace drivers namespace drivers
@@ -157,6 +157,12 @@ namespace drivers
return false; return false;
} }
const std::string PCF85063::getTimeStr() {
datetime_t dt;
readDatetime(dt);
return datetime2str(dt);
}
const std::string PCF85063::datetime2str(datetime_t &datetime) const std::string PCF85063::datetime2str(datetime_t &datetime)
{ {
tm dtime; tm dtime;

View File

@@ -100,6 +100,7 @@ namespace drivers
I2C &m_i2c; I2C &m_i2c;
uint8_t m_address; uint8_t m_address;
public:
typedef struct typedef struct
{ {
uint16_t year; uint16_t year;
@@ -129,6 +130,8 @@ namespace drivers
const bool readAlarm(datetime_t &time); const bool readAlarm(datetime_t &time);
const bool getAlarmFlag(uint8_t &flags); const bool getAlarmFlag(uint8_t &flags);
const std::string getTimeStr();
private: private:
const std::string datetime2str(datetime_t &datetime); const std::string datetime2str(datetime_t &datetime);
const uint8_t decToBcd(const int val); const uint8_t decToBcd(const int val);

View File

@@ -5,6 +5,9 @@
#include <Arduino.h> #include <Arduino.h>
#include <RS485_Driver.h> #include <RS485_Driver.h>
#include <TCA9554PWR_Driver.h> #include <TCA9554PWR_Driver.h>
#include <PCF85063_Driver.h>
#include <ETH.h>
#include "utils.h" #include "utils.h"
@@ -25,16 +28,28 @@ void loop()
const uint8_t baseRegister(0x00); const uint8_t baseRegister(0x00);
//////////////// DEVICES //////////////// //////////////// DEVICES ////////////////
// Declared here to keep devices local to the main loop otherwise the kernel crashes //
auto i2c = drivers::I2C(); auto i2c = drivers::I2C();
auto relays = drivers::TCA9554PWR(i2c, TCA9554_ADDRESS); auto relays = drivers::TCA9554PWR(i2c, TCA9554_ADDRESS);
auto bus = drivers::MODBUS(9600, SERIAL_8N1); auto bus = drivers::MODBUS(9600, SERIAL_8N1);
auto rtc = drivers::PCF85063(i2c, PCF85063_ADDRESS);
////////////////////////////////////////
///////// MAIN LOOP INSIDE LOOP ////////
////////////////////////////////////////
while (true) while (true)
{ {
LOG_INFO("\n\n\n\n[", k++, "] Loop"); LOG_INFO("[", k++, "] Loop");
std::vector<uint16_t> results; std::vector<uint16_t> results;
std::vector<bool> values; std::vector<bool> values;
drivers::PCF85063::datetime_t dt;
rtc.readDatetime(dt);
LOG_INFO("Current Datetime", rtc.getTimeStr().c_str());
if (bus.readHoldingRegisters(tempBoardAddr, baseRegister, 8, results)) if (bus.readHoldingRegisters(tempBoardAddr, baseRegister, 8, results))
{ {
for (auto i(0); i < results.size(); i++) for (auto i(0); i < results.size(); i++)
@@ -43,7 +58,7 @@ void loop()
} }
results.clear(); results.clear();
} }
delay(100); delay(5000);
LOG_INFO("\n\n====>Write Status 1"); LOG_INFO("\n\n====>Write Status 1");
bus.writeCoils(relayBoardAddr, 0x00, {true, false, true, false, true, false, true, false}); bus.writeCoils(relayBoardAddr, 0x00, {true, false, true, false, true, false, true, false});
@@ -63,4 +78,8 @@ void loop()
delay(5000); delay(5000);
} }
////////////////////////////////////////
///////// MAIN LOOP INSIDE LOOP ////////
////////////////////////////////////////
} }