Compare commits
11 Commits
main
...
7a7d677bfe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a7d677bfe | ||
|
|
9530aab1c1 | ||
|
|
19197aa022 | ||
|
|
79e5760d19 | ||
|
|
dcbe637ccc | ||
|
|
b5de72a6d1 | ||
|
|
adb15962c6 | ||
|
|
83a63c1241 | ||
|
|
2a33316ba8 | ||
|
|
12ab46f826 | ||
| 7ea491905f |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -91,7 +91,7 @@ dkms.conf
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
#!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
|
||||
44
.vscode/launch.json
vendored
44
.vscode/launch.json
vendored
@@ -1,44 +0,0 @@
|
||||
// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY
|
||||
//
|
||||
// PlatformIO Debugging Solution
|
||||
//
|
||||
// Documentation: https://docs.platformio.org/en/latest/plus/debugging.html
|
||||
// Configuration: https://docs.platformio.org/en/latest/projectconf/sections/env/options/debug/index.html
|
||||
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "platformio-debug",
|
||||
"request": "launch",
|
||||
"name": "PIO Debug",
|
||||
"executable": "C:/Users/ematr/Documents/VScode/ETcontroller_PRO/.pio/build/esp32-s3-waveshare8/firmware.elf",
|
||||
"projectEnvName": "esp32-s3-waveshare8",
|
||||
"toolchainBinDir": "C:/Users/ematr/.platformio/packages/toolchain-xtensa-esp-elf/bin",
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"preLaunchTask": {
|
||||
"type": "PlatformIO",
|
||||
"task": "Pre-Debug"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "platformio-debug",
|
||||
"request": "launch",
|
||||
"name": "PIO Debug (skip Pre-Debug)",
|
||||
"executable": "C:/Users/ematr/Documents/VScode/ETcontroller_PRO/.pio/build/esp32-s3-waveshare8/firmware.elf",
|
||||
"projectEnvName": "esp32-s3-waveshare8",
|
||||
"toolchainBinDir": "C:/Users/ematr/.platformio/packages/toolchain-xtensa-esp-elf/bin",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
},
|
||||
{
|
||||
"type": "platformio-debug",
|
||||
"request": "launch",
|
||||
"name": "PIO Debug (without uploading)",
|
||||
"executable": "C:/Users/ematr/Documents/VScode/ETcontroller_PRO/.pio/build/esp32-s3-waveshare8/firmware.elf",
|
||||
"projectEnvName": "esp32-s3-waveshare8",
|
||||
"toolchainBinDir": "C:/Users/ematr/.platformio/packages/toolchain-xtensa-esp-elf/bin",
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"loadMode": "manual"
|
||||
}
|
||||
]
|
||||
}
|
||||
BIN
docs/Esp32-s3-wroom-1_wroom-1u_datasheet_en.pdf
Normal file
BIN
docs/Esp32-s3-wroom-1_wroom-1u_datasheet_en.pdf
Normal file
Binary file not shown.
BIN
docs/Esp32-s3_datasheet_en.pdf
Normal file
BIN
docs/Esp32-s3_datasheet_en.pdf
Normal file
Binary file not shown.
70303
docs/Esp32-s3_technical_reference_manual_en.pdf
Normal file
70303
docs/Esp32-s3_technical_reference_manual_en.pdf
Normal file
File diff suppressed because one or more lines are too long
BIN
docs/PCF85063A.pdf
Normal file
BIN
docs/PCF85063A.pdf
Normal file
Binary file not shown.
BIN
docs/tca9554.pdf
Normal file
BIN
docs/tca9554.pdf
Normal file
Binary file not shown.
@@ -8,7 +8,7 @@ static bool eth_connected_Old = false;
|
||||
IPAddress ETH_ip;
|
||||
// NTP setup
|
||||
WiFiUDP udp;
|
||||
NTPClient timeClient(udp, "pool.ntp.org", timezone*3600, 60000); // NTP server, time offset in seconds, update interval
|
||||
NTPClient timeClient(udp, "pool.ntp.org", TZ*3600, 60000); // NTP server, time offset in seconds, update interval
|
||||
|
||||
void onEvent(arduino_event_id_t event, arduino_event_info_t info) {
|
||||
switch (event) {
|
||||
@@ -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);
|
||||
}
|
||||
@@ -3,10 +3,6 @@
|
||||
#include <ETH.h>
|
||||
#include <SPI.h>
|
||||
|
||||
#include "WS_PCF85063.h"
|
||||
#include "WS_GPIO.h"
|
||||
#include "WS_RTC.h"
|
||||
|
||||
// Set this to 1 to enable dual Ethernet support
|
||||
#define USE_TWO_ETH_PORTS 0
|
||||
|
||||
@@ -35,7 +31,7 @@
|
||||
ETHClass ETH1(1);
|
||||
#endif
|
||||
|
||||
#define timezone 8 // china
|
||||
#define TZ 1 // rome
|
||||
|
||||
void ETH_Init(void);
|
||||
void ETH_Loop(void);
|
||||
80
lib/GPIO/TCA9554PWR_Driver.cpp
Normal file
80
lib/GPIO/TCA9554PWR_Driver.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "WS_TCA9554PWR.h"
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
TCA9554PWR::TCA9554PWR(I2C &i2c, const uint8_t address) : m_i2c(i2c), m_address(address)
|
||||
{
|
||||
writeRegister(TCA9554_OUTPUT_REG, Low); // set all pins to Low state
|
||||
writeRegister(TCA9554_CONFIG_REG, TCA9554_OUT_MODE); // set all pins as output (relay mode for this board)
|
||||
}
|
||||
|
||||
const bool TCA9554PWR::writeRegister(const uint8_t reg, const uint8_t val)
|
||||
{
|
||||
if (m_i2c.write(m_address, reg, {val}))
|
||||
return true;
|
||||
log_e("Unable to write register: reg[%d], val[%d] ", reg, val);
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool TCA9554PWR::readRegister(const uint8_t reg, uint8_t &val)
|
||||
{
|
||||
std::vector<uint8_t> data;
|
||||
if (m_i2c.read(m_address, reg, 1, data))
|
||||
{
|
||||
val = data.back();
|
||||
return true;
|
||||
}
|
||||
log_e("Unable to read register: reg[%d]");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool TCA9554PWR::setOut(const uint8_t ch, const bool state)
|
||||
{
|
||||
uint8_t currState(0);
|
||||
uint8_t newState(0);
|
||||
|
||||
if (ch < EXIO_PIN1 || ch > EXIO_PIN8)
|
||||
{
|
||||
log_e("Invalid write to output channel: [%d]", ch);
|
||||
return false;
|
||||
}
|
||||
if (!readPort(currState))
|
||||
return false;
|
||||
if (state)
|
||||
newState = (High << ch) | currState;
|
||||
else
|
||||
newState = (~(High << ch)) & currState;
|
||||
return setPort(newState);
|
||||
}
|
||||
|
||||
const bool TCA9554PWR::setPort(const uint8_t state)
|
||||
{
|
||||
if (writeRegister(TCA9554_OUTPUT_REG, state))
|
||||
return true;
|
||||
log_e("Unable to write IO port: state[%02x]", state);
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool TCA9554PWR::readOut(const uint8_t ch)
|
||||
{
|
||||
uint8_t currState(0);
|
||||
if (ch < EXIO_PIN1 || ch > EXIO_PIN8)
|
||||
{
|
||||
log_e("Invalid read to output channel: [%d]", ch);
|
||||
return false;
|
||||
}
|
||||
if (!readPort(currState))
|
||||
return false;
|
||||
return (currState && (High >> ch));
|
||||
}
|
||||
|
||||
const bool TCA9554PWR::readPort(uint8_t &state)
|
||||
{
|
||||
if (readRegister(TCA9554_INPUT_REG, state))
|
||||
return true;
|
||||
log_e("Unable to read IO port: state[%02x]", state);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
47
lib/GPIO/TCA9554PWR_Driver.h
Normal file
47
lib/GPIO/TCA9554PWR_Driver.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
#include "I2C_Driver.h"
|
||||
|
||||
/****************************************************** The macro defines the TCA9554PWR information ******************************************************/
|
||||
|
||||
#define TCA9554_ADDRESS 0x20 // TCA9554PWR I2C address
|
||||
#define TCA9554_INPUT_REG 0x00 // Input register, input level
|
||||
#define TCA9554_OUTPUT_REG 0x01 // Output register, high and low level output
|
||||
#define TCA9554_POLARITY_REG 0x02 // The Polarity Inversion register (register 2) allows polarity inversion of pins defined as inputs by the Configuration register.
|
||||
#define TCA9554_CONFIG_REG 0x03 // Configuration register, mode configuration
|
||||
|
||||
#define TCA9554_OUT_MODE 0x00 // Configuration register value, output mode
|
||||
#define TCA9554_IN_MODE 0xff // Configuration register value, input mode
|
||||
|
||||
#define Low 0x00
|
||||
#define High 0x01
|
||||
|
||||
#define EXIO_PIN1 0
|
||||
#define EXIO_PIN2 1
|
||||
#define EXIO_PIN3 2
|
||||
#define EXIO_PIN4 3
|
||||
#define EXIO_PIN5 4
|
||||
#define EXIO_PIN6 5
|
||||
#define EXIO_PIN7 6
|
||||
#define EXIO_PIN8 7
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
class TCA9554PWR
|
||||
{
|
||||
I2C &m_i2c;
|
||||
uint8_t m_address;
|
||||
|
||||
private:
|
||||
const bool writeRegister(const uint8_t reg, const uint8_t val);
|
||||
const bool readRegister(const uint8_t reg, uint8_t &val);
|
||||
|
||||
public:
|
||||
TCA9554PWR(I2C &i2c, const uint8_t address);
|
||||
|
||||
const bool setOut(const uint8_t channel, const bool state);
|
||||
const bool setPort(const uint8_t state);
|
||||
|
||||
const bool readOut(const uint8_t channel);
|
||||
const bool readPort(uint8_t &state);
|
||||
};
|
||||
}
|
||||
@@ -11,7 +11,7 @@ bool Relay_Open(uint8_t CHx)
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
bool Relay_Closs(uint8_t CHx)
|
||||
bool Relay_Close(uint8_t CHx)
|
||||
{
|
||||
if(!Set_EXIO(CHx, false)){
|
||||
printf("Failed to Closs CH%d!!!\r\n", CHx);
|
||||
@@ -35,7 +35,7 @@ bool Relay_CHx(uint8_t CHx, bool State)
|
||||
if(State)
|
||||
result = Relay_Open(CHx);
|
||||
else
|
||||
result = Relay_Closs(CHx);
|
||||
result = Relay_Close(CHx);
|
||||
if(!result)
|
||||
Failure_Flag = 1;
|
||||
return result;
|
||||
@@ -233,12 +233,12 @@ void Relay_Immediate_CHxn(Status_adjustment * Relay_n, uint8_t Mode_Flag)
|
||||
else if(Mode_Flag == RTC_Mode)
|
||||
printf("RTC Data :\r\n");
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if(Relay_n[i] == STATE_Open || Relay_n[i] == STATE_Closs){
|
||||
if(Relay_n[i] == STATE_Open || Relay_n[i] == STATE_Close){
|
||||
Relay_Flag[i] = (bool)Relay_n[i];
|
||||
ret = Relay_CHx(i+1,Relay_n[i]);
|
||||
if(Relay_n[i] == STATE_Open)
|
||||
printf("|*** Relay CH%d on ***|\r\n",i+1);
|
||||
else if(Relay_n[i] == STATE_Closs)
|
||||
else if(Relay_n[i] == STATE_Close)
|
||||
printf("|*** Relay CH%d off ***|\r\n",i+1);
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@
|
||||
#define RTC_Mode 6
|
||||
|
||||
typedef enum {
|
||||
STATE_Closs = 0, // Closs Relay
|
||||
STATE_Close = 0, // Closs Relay
|
||||
STATE_Open = 1, // Open Relay
|
||||
STATE_Retain = 2, // Stay in place
|
||||
} Status_adjustment;
|
||||
@@ -44,7 +44,7 @@ typedef enum {
|
||||
extern bool Relay_Flag[8]; // Relay current status flag
|
||||
|
||||
void Relay_Init(void);
|
||||
bool Relay_Closs(uint8_t CHx);
|
||||
bool Relay_Close(uint8_t CHx);
|
||||
bool Relay_Open(uint8_t CHx);
|
||||
bool Relay_CHx_Toggle(uint8_t CHx);
|
||||
bool Relay_CHx(uint8_t CHx, bool State);
|
||||
80
lib/I2C/I2C_Driver.cpp
Normal file
80
lib/I2C/I2C_Driver.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "I2C_Driver.h"
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
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
|
||||
26
lib/I2C/I2C_Driver.h
Normal file
26
lib/I2C/I2C_Driver.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
#include <Wire.h>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#define I2C_SCL_PIN 41
|
||||
#define I2C_SDA_PIN 42
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
class I2C
|
||||
{
|
||||
private:
|
||||
bool isInitialized = false;
|
||||
std::mutex busy;
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
}
|
||||
182
lib/RTC/PCF85063_Driver.cpp
Normal file
182
lib/RTC/PCF85063_Driver.cpp
Normal file
@@ -0,0 +1,182 @@
|
||||
#include "WS_PCF85063.h"
|
||||
#include <ctime>
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
PCF85063::PCF85063(I2C &i2c, const uint8_t address, const uint8_t ctrl1, const uint8_t ctrl2) : m_i2c(i2c), m_address(address)
|
||||
{
|
||||
bool success(true);
|
||||
if (ctrl1 == RTC_CTRL_1_DEFAULT)
|
||||
{
|
||||
const uint8_t def_conf1 = RTC_CTRL_1_DEFAULT | RTC_CTRL_1_CAP_SEL; // 12.5pF cap and 24h format
|
||||
success &= m_i2c.write(m_address, RTC_CTRL_1_ADDR, {def_conf1});
|
||||
}
|
||||
if (ctrl2 == RTC_CTRL_2_DEFAULT)
|
||||
{
|
||||
const uint8_t def_conf2 = RTC_CTRL_2_DEFAULT | RTC_CTRL_2_MI; // enable 1 minute interrupt
|
||||
success &= m_i2c.write(m_address, RTC_CTRL_2_ADDR, {def_conf2});
|
||||
}
|
||||
if (!success)
|
||||
log_e("RTC Init Failure");
|
||||
}
|
||||
|
||||
const bool PCF85063::reset(void)
|
||||
{
|
||||
log_i("RTC Reset Initiated");
|
||||
const uint8_t cfg = RTC_CTRL_1_DEFAULT | RTC_CTRL_1_CAP_SEL | RTC_CTRL_1_SR;
|
||||
if (m_i2c.write(m_address, RTC_CTRL_1_ADDR, {cfg}))
|
||||
return true;
|
||||
log_e("RTC Reset Failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool PCF85063::setTime(const datetime_t time)
|
||||
{
|
||||
const std::vector<uint8_t> buf = {
|
||||
decToBcd(time.second),
|
||||
decToBcd(time.minute),
|
||||
decToBcd(time.hour)};
|
||||
if (m_i2c.write(m_address, RTC_SECOND_ADDR, buf))
|
||||
return true;
|
||||
log_e("RTC setTime failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool PCF85063::setDate(const datetime_t date)
|
||||
{
|
||||
const std::vector<uint8_t> buf = {
|
||||
decToBcd(date.day),
|
||||
decToBcd(date.dotw),
|
||||
decToBcd(date.month),
|
||||
decToBcd(date.year - YEAR_OFFSET)};
|
||||
if (m_i2c.write(m_address, RTC_DAY_ADDR, buf))
|
||||
return true;
|
||||
log_e("RTC setDate failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool PCF85063::setDatetime(const datetime_t datetime)
|
||||
{
|
||||
return setDate(datetime) && setTime(datetime);
|
||||
}
|
||||
|
||||
const bool PCF85063::readDate(datetime_t &datetime)
|
||||
{
|
||||
std::vector<uint8_t> buf;
|
||||
if (m_i2c.read(m_address, RTC_DAY_ADDR, 4, buf))
|
||||
{
|
||||
datetime.day = bcdToDec(buf[0] & 0x3F);
|
||||
datetime.dotw = bcdToDec(buf[1] & 0x07);
|
||||
datetime.month = bcdToDec(buf[2] & 0x1F);
|
||||
datetime.year = bcdToDec(buf[3]) + YEAR_OFFSET;
|
||||
return true;
|
||||
}
|
||||
log_e("RTC readDate Failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool PCF85063::readTime(datetime_t &datetime)
|
||||
{
|
||||
std::vector<uint8_t> buf;
|
||||
if (m_i2c.read(m_address, RTC_SECOND_ADDR, 3, buf))
|
||||
{
|
||||
datetime.second = bcdToDec(buf[0] & 0x7F);
|
||||
datetime.minute = bcdToDec(buf[1] & 0x7F);
|
||||
datetime.hour = bcdToDec(buf[2] & 0x3F);
|
||||
return true;
|
||||
}
|
||||
log_e("RTC readTime Failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool PCF85063::readDatetime(datetime_t &datetime)
|
||||
{
|
||||
return readTime(datetime) && readDate(datetime);
|
||||
}
|
||||
|
||||
const bool PCF85063::enableAlarm(const bool enable)
|
||||
{
|
||||
bool success(true);
|
||||
std::vector<uint8_t> currStatus(1, RTC_CTRL_2_DEFAULT);
|
||||
|
||||
success &= m_i2c.read(m_address, RTC_CTRL_2_ADDR, 1, currStatus);
|
||||
currStatus.at(0) &= ~RTC_CTRL_2_AF; // clear alarm flag
|
||||
|
||||
if (enable)
|
||||
currStatus.at(0) |= RTC_CTRL_2_AIE; // enable alarm
|
||||
else
|
||||
currStatus.at(0) &= ~RTC_CTRL_2_AIE; // disable alarm
|
||||
|
||||
if (m_i2c.write(m_address, RTC_CTRL_2_ADDR, currStatus))
|
||||
return true;
|
||||
log_e("RTC enableAlarm failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool PCF85063::setAlarm(datetime_t time)
|
||||
{
|
||||
const std::vector<uint8_t> buf = {
|
||||
(uint8_t)(decToBcd(time.second) & (~RTC_ALARM)),
|
||||
(uint8_t)(decToBcd(time.minute) & (~RTC_ALARM)),
|
||||
(uint8_t)(decToBcd(time.hour) & (~RTC_ALARM)),
|
||||
(uint8_t)(RTC_ALARM), // disalbe day
|
||||
(uint8_t)(RTC_ALARM) // disalbe weekday
|
||||
};
|
||||
if (m_i2c.write(m_address, RTC_SECOND_ALARM, buf))
|
||||
return true;
|
||||
log_e("RTC setAlarm failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool PCF85063::readAlarm(datetime_t &time)
|
||||
{
|
||||
std::vector<uint8_t> buf;
|
||||
if (m_i2c.read(m_address, RTC_SECOND_ALARM, 5, buf))
|
||||
{
|
||||
time.second = (uint8_t)bcdToDec(buf[0] & 0x7F);
|
||||
time.minute = (uint8_t)bcdToDec(buf[1] & 0x7F);
|
||||
time.hour = (uint8_t)bcdToDec(buf[2] & 0x3F);
|
||||
time.day = (uint8_t)bcdToDec(buf[3] & 0x3F);
|
||||
time.dotw = (uint8_t)bcdToDec(buf[4] & 0x07);
|
||||
return true;
|
||||
}
|
||||
log_e("RTC readAlarm failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool PCF85063::getAlarmFlag(uint8_t &flags)
|
||||
{
|
||||
std::vector<uint8_t> buf;
|
||||
if (m_i2c.read(m_address, RTC_CTRL_2_ADDR, 1, buf))
|
||||
{
|
||||
flags = buf.at(0);
|
||||
return true;
|
||||
}
|
||||
log_e("RTC readAlarmFlags failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string PCF85063::datetime2str(datetime_t &datetime)
|
||||
{
|
||||
tm dtime;
|
||||
dtime.tm_sec = datetime.second;
|
||||
dtime.tm_min = datetime.minute;
|
||||
dtime.tm_hour = datetime.hour;
|
||||
dtime.tm_wday = datetime.dotw;
|
||||
dtime.tm_mday = datetime.day;
|
||||
dtime.tm_mon = datetime.month;
|
||||
dtime.tm_year = datetime.year - 1900; // time offset in structure according cpp reference
|
||||
return std::string(std::asctime(&dtime));
|
||||
}
|
||||
|
||||
const uint8_t PCF85063::decToBcd(const int val)
|
||||
{
|
||||
return (uint8_t)((val / 10 * 16) + (val % 10));
|
||||
}
|
||||
|
||||
const int PCF85063::bcdToDec(uint8_t val)
|
||||
{
|
||||
return (const int)((val / 16 * 10) + (val % 16));
|
||||
}
|
||||
}
|
||||
138
lib/RTC/PCF85063_Driver.h
Normal file
138
lib/RTC/PCF85063_Driver.h
Normal file
@@ -0,0 +1,138 @@
|
||||
#pragma once
|
||||
|
||||
#include "I2C_Driver.h"
|
||||
#include <string>
|
||||
|
||||
// PCF85063_ADDRESS
|
||||
#define PCF85063_ADDRESS (0x51)
|
||||
|
||||
#define YEAR_OFFSET (1970)
|
||||
// registar overview - crtl & status reg
|
||||
#define RTC_CTRL_1_ADDR (0x00)
|
||||
#define RTC_CTRL_2_ADDR (0x01)
|
||||
#define RTC_OFFSET_ADDR (0x02)
|
||||
#define RTC_RAM_by_ADDR (0x03)
|
||||
// registar overview - time & data reg
|
||||
#define RTC_SECOND_ADDR (0x04)
|
||||
#define RTC_MINUTE_ADDR (0x05)
|
||||
#define RTC_HOUR_ADDR (0x06)
|
||||
#define RTC_DAY_ADDR (0x07)
|
||||
#define RTC_WDAY_ADDR (0x08)
|
||||
#define RTC_MONTH_ADDR (0x09)
|
||||
#define RTC_YEAR_ADDR (0x0A) // years 0-99; calculate real year = 1970 + RCC reg year
|
||||
// registar overview - alarm reg
|
||||
#define RTC_SECOND_ALARM (0x0B)
|
||||
#define RTC_MINUTE_ALARM (0x0C)
|
||||
#define RTC_HOUR_ALARM (0x0D)
|
||||
#define RTC_DAY_ALARM (0x0E)
|
||||
#define RTC_WDAY_ALARM (0x0F)
|
||||
// registar overview - timer reg
|
||||
#define RTC_TIMER_VAL (0x10)
|
||||
#define RTC_TIMER_MODE (0x11)
|
||||
|
||||
// RTC_CTRL_1 registar
|
||||
#define RTC_CTRL_1_EXT_TEST (0x80)
|
||||
#define RTC_CTRL_1_STOP (0x20) // 0-RTC clock runs 1- RTC clock is stopped
|
||||
#define RTC_CTRL_1_SR (0X10) // 0-no software reset 1-initiate software rese
|
||||
#define RTC_CTRL_1_CIE (0X04) // 0-no correction interrupt generated 1-interrupt pulses are generated at every correction cycle
|
||||
#define RTC_CTRL_1_12_24 (0X02) // 0-24H 1-12H
|
||||
#define RTC_CTRL_1_CAP_SEL (0X01) // 0-7PF 1-12.5PF
|
||||
|
||||
// RTC_CTRL_2 registar
|
||||
#define RTC_CTRL_2_AIE (0X80) // alarm interrupt 0-disalbe 1-enable
|
||||
#define RTC_CTRL_2_AF (0X40) // alarm flag 0-inactive/cleared 1-active/unchanged
|
||||
#define RTC_CTRL_2_MI (0X20) // minute interrupt 0-disalbe 1-enable
|
||||
#define RTC_CTRL_2_HMI (0X10) // half minute interrupt
|
||||
#define RTC_CTRL_2_TF (0X08)
|
||||
|
||||
//
|
||||
#define RTC_OFFSET_MODE (0X80)
|
||||
|
||||
//
|
||||
#define RTC_TIMER_MODE_TE (0X04) // timer enable 0-disalbe 1-enable
|
||||
#define RTC_TIMER_MODE_TIE (0X02) // timer interrupt enable 0-disalbe 1-enable
|
||||
#define RTC_TIMER_MODE_TI_TP (0X01) // timer interrupt mode 0-interrupt follows timer flag 1-interrupt generates a pulse
|
||||
|
||||
// format
|
||||
#define RTC_ALARM (0x80) // set AEN_x registers
|
||||
#define RTC_CTRL_1_DEFAULT (0x00)
|
||||
#define RTC_CTRL_2_DEFAULT (0x00)
|
||||
|
||||
#define RTC_TIMER_FLAG (0x08)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t year;
|
||||
uint8_t month;
|
||||
uint8_t day;
|
||||
uint8_t dotw;
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
uint8_t second;
|
||||
} datetime_t;
|
||||
|
||||
const unsigned char MonthStr[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
||||
const unsigned char Week[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
||||
|
||||
extern datetime_t datetime;
|
||||
void PCF85063_Init(void);
|
||||
void PCF85063_Reset(void);
|
||||
void PCF85063Task(void *parameter);
|
||||
|
||||
void PCF85063_Set_Time(datetime_t time);
|
||||
void PCF85063_Set_Date(datetime_t date);
|
||||
void PCF85063_Set_All(datetime_t time);
|
||||
|
||||
void PCF85063_Read_Time(datetime_t *time);
|
||||
|
||||
void PCF85063_Enable_Alarm(void);
|
||||
uint8_t PCF85063_Get_Alarm_Flag(void);
|
||||
void PCF85063_Set_Alarm(datetime_t time);
|
||||
void PCF85063_Read_Alarm(datetime_t *time);
|
||||
|
||||
void datetime_to_str(char *datetime_str, datetime_t time);
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
class PCF85063
|
||||
{
|
||||
I2C &m_i2c;
|
||||
uint8_t m_address;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t year;
|
||||
uint8_t month;
|
||||
uint8_t day;
|
||||
uint8_t dotw;
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
uint8_t second;
|
||||
} 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);
|
||||
|
||||
const bool reset(void);
|
||||
|
||||
const bool setTime(const datetime_t time);
|
||||
const bool setDate(const datetime_t date);
|
||||
const bool setDatetime(const datetime_t datetime);
|
||||
|
||||
const bool readDate(datetime_t &datetime);
|
||||
const bool readTime(datetime_t &datetime);
|
||||
const bool readDatetime(datetime_t &datetime);
|
||||
|
||||
const bool enableAlarm(const bool enable);
|
||||
const bool setAlarm(const datetime_t time);
|
||||
const bool readAlarm(datetime_t &time);
|
||||
const bool getAlarmFlag(uint8_t &flags);
|
||||
|
||||
private:
|
||||
const std::string datetime2str(datetime_t &datetime);
|
||||
const uint8_t decToBcd(const int val);
|
||||
const int bcdToDec(const uint8_t val);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
#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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include <Wire.h>
|
||||
|
||||
#define I2C_SCL_PIN 41
|
||||
#define I2C_SDA_PIN 42
|
||||
|
||||
void I2C_Init(void);
|
||||
|
||||
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);
|
||||
@@ -17,8 +17,8 @@ uint32_t Simulated_time=0; // Analog time counting
|
||||
void setup() {
|
||||
Flash_test();
|
||||
GPIO_Init(); // RGB . Buzzer GPIO
|
||||
I2C_Init();
|
||||
RTC_Init();// RTC
|
||||
//I2C_Init();
|
||||
//RTC_Init(); // RTC
|
||||
SD_Init();
|
||||
Serial_Init(); // UART(RS485/CAN)
|
||||
MQTT_Init();// MQTT
|
||||
|
||||
219
src/WS_CAN.cpp
219
src/WS_CAN.cpp
@@ -1,219 +0,0 @@
|
||||
#include "WS_CAN.h"
|
||||
|
||||
static bool driver_installed = false;
|
||||
|
||||
void CAN_Init(void)
|
||||
{ // Initializing serial port
|
||||
// Initialize configuration structures using macro initializers
|
||||
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TXD1, (gpio_num_t)RXD1, TWAI_MODE_NORMAL);
|
||||
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_250KBITS(); //Look in the api-reference for other speed sets.
|
||||
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
|
||||
|
||||
// Install TWAI driver
|
||||
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
|
||||
printf("Driver installed\r\n");
|
||||
} else {
|
||||
printf("Failed to install driver\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Start TWAI driver
|
||||
if (twai_start() == ESP_OK) {
|
||||
printf("Driver started\r\n");
|
||||
} else {
|
||||
printf("Failed to start driver\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Reconfigure alerts to detect TX alerts and Bus-Off errors
|
||||
uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_ERROR | TWAI_ALERT_RX_QUEUE_FULL | TWAI_ALERT_TX_IDLE | TWAI_ALERT_TX_SUCCESS | TWAI_ALERT_TX_FAILED;
|
||||
if (twai_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK) {
|
||||
printf("CAN Alerts reconfigured\r\n");
|
||||
} else {
|
||||
printf("Failed to reconfigure alerts\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// TWAI driver is now successfully installed and started
|
||||
driver_installed = true;
|
||||
|
||||
xTaskCreatePinnedToCore(
|
||||
CANTask,
|
||||
"CANTask",
|
||||
4096,
|
||||
NULL,
|
||||
3,
|
||||
NULL,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
static void send_message_Test(void) {
|
||||
// Send message
|
||||
// Configure message to transmit
|
||||
twai_message_t message;
|
||||
message.identifier = 0x0F6;
|
||||
message.data_length_code = 4;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
message.data[i] = i;
|
||||
}
|
||||
|
||||
// Queue message for transmission
|
||||
if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
|
||||
printf("Message queued for transmission\n");
|
||||
} else {
|
||||
printf("Failed to queue message for transmission\n");
|
||||
}
|
||||
}
|
||||
// Standard frames ID: 0x000 to 0x7FF
|
||||
// Extended frames ID: 0x00000000 to 0x1FFFFFFF
|
||||
// Frame_type : 1:Extended frames 0:Standard frames
|
||||
void send_message(uint32_t CAN_ID, uint8_t* Data, uint8_t Data_length, bool Frame_type) {
|
||||
// Send message
|
||||
// Configure message to transmit
|
||||
twai_message_t message;
|
||||
message.identifier = CAN_ID;
|
||||
message.rtr = 0; // Disable remote frame
|
||||
if(CAN_ID > 0x7FF){
|
||||
if(!Frame_type)
|
||||
printf("The frame type is set incorrectly and data will eventually be sent as an extended frame!!!!\r\n");
|
||||
message.extd = 1;
|
||||
}
|
||||
else
|
||||
message.extd = Frame_type;
|
||||
if(Data_length > 8){
|
||||
uint16_t Frame_count = (Data_length / 8);
|
||||
for (int i = 0; i < Frame_count; i++) {
|
||||
message.data_length_code = 8;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
message.data[j] = Data[j + (i * 8)];
|
||||
}
|
||||
// Queue message for transmission
|
||||
if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
|
||||
printf("Message queued for transmission\n");
|
||||
} else {
|
||||
printf("Failed to queue message for transmission\n");
|
||||
}
|
||||
}
|
||||
if(Data_length % 8){
|
||||
uint8_t Data_length_Now = Data_length % 8;
|
||||
message.data_length_code = Data_length_Now;
|
||||
for (int k = 0; k < Data_length_Now; k++) {
|
||||
message.data[k] = Data[k + (Data_length - Data_length_Now)];
|
||||
}
|
||||
// Queue message for transmission
|
||||
if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
|
||||
printf("Message queued for transmission\n");
|
||||
} else {
|
||||
printf("Failed to queue message for transmission\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
message.data_length_code = Data_length;
|
||||
for (int i = 0; i < Data_length; i++) {
|
||||
message.data[i] = Data[i];
|
||||
}
|
||||
// Queue message for transmission
|
||||
if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
|
||||
printf("Message queued for transmission\n");
|
||||
} else {
|
||||
printf("Failed to queue message for transmission\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void handle_rx_message(twai_message_t &message) {
|
||||
// Process received message
|
||||
if (message.extd) {
|
||||
printf("Message is in Extended Format\r\n");
|
||||
} else {
|
||||
printf("Message is in Standard Format\r\n");
|
||||
}
|
||||
printf("ID: %lx\nByte:", message.identifier);
|
||||
if (!(message.rtr)) {
|
||||
if (message.data_length_code > 0) {
|
||||
printf(" Data: ");
|
||||
for (int i = 0; i < message.data_length_code; i++) {
|
||||
printf("%02x ", message.data[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
// printf("Send back the received data!\r\n");
|
||||
// send_message(message.identifier, message.data, message.data_length_code, message.extd);
|
||||
} else {
|
||||
printf(" No data available\r\n");
|
||||
}
|
||||
} else {
|
||||
printf("This is a Remote Transmission Request (RTR) frame.\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long previousMillis = 0; // will store last time a message was send
|
||||
#if Communication_failure_Enable
|
||||
static unsigned long previous_bus_error_time = 0; // To store the last time a BUS_ERROR was printed
|
||||
#endif
|
||||
void CAN_Loop(void)
|
||||
{
|
||||
if(driver_installed){
|
||||
// Check if an alert happened
|
||||
uint32_t alerts_triggered;
|
||||
twai_read_alerts(&alerts_triggered, pdMS_TO_TICKS(POLLING_RATE_MS));
|
||||
twai_status_info_t twaistatus;
|
||||
twai_get_status_info(&twaistatus);
|
||||
|
||||
// Handle alerts
|
||||
if (alerts_triggered & TWAI_ALERT_ERR_PASS) {
|
||||
printf("Alert: TWAI controller has become error passive.\r\n");
|
||||
}
|
||||
if (alerts_triggered & TWAI_ALERT_BUS_ERROR) {
|
||||
// printf("Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.\r\n");
|
||||
// printf("Bus error count: %ld\n", twaistatus.bus_error_count);
|
||||
#if Communication_failure_Enable
|
||||
unsigned long currentMillis = millis();
|
||||
// Only print the message if more than 2 seconds have passed since the last time it was printed
|
||||
if (currentMillis - previous_bus_error_time >= BUS_ERROR_INTERVAL_MS) {
|
||||
printf("Note if there are other devices on the CAN bus (other devices must be present) and that the rate of the device is the same as set in this program\r\n");
|
||||
previous_bus_error_time = currentMillis; // Update the last print time
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if (alerts_triggered & TWAI_ALERT_RX_QUEUE_FULL) {
|
||||
printf("Alert: The RX queue is full causing a received frame to be lost.\r\n");
|
||||
printf("RX buffered: %ld\t", twaistatus.msgs_to_rx);
|
||||
printf("RX missed: %ld\t", twaistatus.rx_missed_count);
|
||||
printf("RX overrun %ld\n", twaistatus.rx_overrun_count);
|
||||
}
|
||||
if (alerts_triggered & TWAI_ALERT_TX_FAILED) {
|
||||
printf("Alert: The Transmission failed.\r\n");
|
||||
printf("TX buffered: %ld\t", twaistatus.msgs_to_tx);
|
||||
printf("TX error: %ld\t", twaistatus.tx_error_counter);
|
||||
printf("TX failed: %ld\n", twaistatus.tx_failed_count);
|
||||
}
|
||||
if (alerts_triggered & TWAI_ALERT_TX_SUCCESS) {
|
||||
printf("Alert: The Transmission was successful.\r\n");
|
||||
printf("TX buffered: %ld\t \r\n", twaistatus.msgs_to_tx);
|
||||
}
|
||||
|
||||
// Receive messages if any are available
|
||||
if (alerts_triggered & TWAI_ALERT_RX_DATA) {
|
||||
// One or more messages received. Handle all.
|
||||
twai_message_t message; // This is the structure used to store the received CAN message.
|
||||
while (twai_receive(&message, 0) == ESP_OK) {
|
||||
handle_rx_message(message); // This function will process the received message.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CANTask(void *parameter) {
|
||||
// send_message_Test();
|
||||
// uint8_t Data[27]={0x80, 0x2A, 0xC3, 0x58, 0x17, 0x11, 0x4D, 0x3F, 0x3B, 0xCE, 0x0F, 0xFF, 0x79, 0x20, 0xB4, 0x40, 0x5D, 0x29, 0x05, 0x49, 0xE6, 0x12, 0x57, 0x0E, 0x6D, 0xC9, 0xAE};
|
||||
// send_message(0x079,Data,27);
|
||||
while(1){
|
||||
CAN_Loop();
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
22
src/WS_CAN.h
22
src/WS_CAN.h
@@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "driver/twai.h"
|
||||
#include "WS_GPIO.h"
|
||||
|
||||
// Interval:
|
||||
#define TRANSMIT_RATE_MS 1000
|
||||
// Interval:
|
||||
#define POLLING_RATE_MS 1000
|
||||
|
||||
#define Communication_failure_Enable 0 // If the CAN bus is faulty for a long time, determine whether to forcibly exit
|
||||
|
||||
#if Communication_failure_Enable
|
||||
#define BUS_ERROR_INTERVAL_MS 5000 // Send a message every 2 seconds (2000 ms)
|
||||
#endif
|
||||
|
||||
|
||||
void CAN_Init(void);
|
||||
void CAN_Loop(void);
|
||||
void CANTask(void *parameter);
|
||||
|
||||
void send_message(uint32_t CAN_ID, uint8_t* Data, uint8_t Data_length);
|
||||
@@ -1,189 +0,0 @@
|
||||
#include "WS_PCF85063.h"
|
||||
|
||||
datetime_t datetime= {0};
|
||||
datetime_t Update_datetime= {0};
|
||||
static uint8_t decToBcd(int val);
|
||||
static int bcdToDec(uint8_t val);
|
||||
|
||||
|
||||
void Time_printf(void *parameter) {
|
||||
while(1){
|
||||
char datetime_str[50];
|
||||
datetime_to_str(datetime_str,datetime);
|
||||
printf("Time:%s\r\n",datetime_str);
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
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);
|
||||
if(Value & RTC_CTRL_1_STOP)
|
||||
printf("PCF85063 failed to be initialized.state :%d\r\n",Value);
|
||||
else
|
||||
printf("PCF85063 is running,state :%d\r\n",Value);
|
||||
|
||||
//
|
||||
// Update_datetime.year = 2024;
|
||||
// Update_datetime.month = 9;
|
||||
// Update_datetime.day = 20;
|
||||
// Update_datetime.dotw = 5;
|
||||
// Update_datetime.hour = 9;
|
||||
// Update_datetime.minute = 50;
|
||||
// Update_datetime.second = 0;
|
||||
// PCF85063_Set_All(Update_datetime);
|
||||
xTaskCreatePinnedToCore(
|
||||
PCF85063Task,
|
||||
"PCF85063Task",
|
||||
4096,
|
||||
NULL,
|
||||
3,
|
||||
NULL,
|
||||
0
|
||||
);
|
||||
// xTaskCreatePinnedToCore(
|
||||
// Time_printf,
|
||||
// "Time_printf",
|
||||
// 4096,
|
||||
// NULL,
|
||||
// 3,
|
||||
// NULL,
|
||||
// 0
|
||||
// );
|
||||
}
|
||||
|
||||
void PCF85063Task(void *parameter) {
|
||||
while(1){
|
||||
PCF85063_Read_Time(&datetime);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void PCF85063_Reset() // Reset PCF85063
|
||||
{
|
||||
uint8_t Value = RTC_CTRL_1_DEFAULT|RTC_CTRL_1_CAP_SEL|RTC_CTRL_1_SR;
|
||||
esp_err_t ret = I2C_Write(PCF85063_ADDRESS, RTC_CTRL_1_ADDR, &Value, 1);
|
||||
if(ret != ESP_OK)
|
||||
printf("PCF85063 : Reset failure\r\n");
|
||||
}
|
||||
void PCF85063_Set_Time(datetime_t time) // Set Time
|
||||
{
|
||||
uint8_t buf[3] = {decToBcd(time.second),
|
||||
decToBcd(time.minute),
|
||||
decToBcd(time.hour)};
|
||||
esp_err_t ret = I2C_Write(PCF85063_ADDRESS, RTC_SECOND_ADDR, buf, sizeof(buf));
|
||||
if(ret != ESP_OK)
|
||||
printf("PCF85063 : Time setting failure\r\n");
|
||||
}
|
||||
void PCF85063_Set_Date(datetime_t date) // Set Date
|
||||
{
|
||||
uint8_t buf[4] = {decToBcd(date.day),
|
||||
decToBcd(date.dotw),
|
||||
decToBcd(date.month),
|
||||
decToBcd(date.year - YEAR_OFFSET)};
|
||||
esp_err_t ret = I2C_Write(PCF85063_ADDRESS, RTC_DAY_ADDR, buf, sizeof(buf));
|
||||
if(ret != ESP_OK)
|
||||
printf("PCF85063 : Date setting failed\r\n");
|
||||
}
|
||||
|
||||
void PCF85063_Set_All(datetime_t time) // Set Time And Date
|
||||
{
|
||||
uint8_t buf[7] = {decToBcd(time.second),
|
||||
decToBcd(time.minute),
|
||||
decToBcd(time.hour),
|
||||
decToBcd(time.day),
|
||||
decToBcd(time.dotw),
|
||||
decToBcd(time.month),
|
||||
decToBcd(time.year - YEAR_OFFSET)};
|
||||
esp_err_t ret = I2C_Write(PCF85063_ADDRESS, RTC_SECOND_ADDR, buf, sizeof(buf));
|
||||
if(ret != ESP_OK)
|
||||
printf("PCF85063 : Failed to set the date and time\r\n");
|
||||
}
|
||||
|
||||
void PCF85063_Read_Time(datetime_t *time) // Read Time And Date
|
||||
{
|
||||
uint8_t buf[7] = {0};
|
||||
esp_err_t ret = I2C_Read(PCF85063_ADDRESS, RTC_SECOND_ADDR, buf, sizeof(buf));
|
||||
if(ret != ESP_OK)
|
||||
printf("PCF85063 : Time read failure\r\n");
|
||||
else{
|
||||
time->second = bcdToDec(buf[0] & 0x7F);
|
||||
time->minute = bcdToDec(buf[1] & 0x7F);
|
||||
time->hour = bcdToDec(buf[2] & 0x3F);
|
||||
time->day = bcdToDec(buf[3] & 0x3F);
|
||||
time->dotw = bcdToDec(buf[4] & 0x07);
|
||||
time->month = bcdToDec(buf[5] & 0x1F);
|
||||
time->year = bcdToDec(buf[6]) + YEAR_OFFSET;
|
||||
}
|
||||
}
|
||||
|
||||
void PCF85063_Enable_Alarm() // Enable Alarm and Clear Alarm flag
|
||||
{
|
||||
uint8_t Value = RTC_CTRL_2_DEFAULT | RTC_CTRL_2_AIE;
|
||||
Value &= ~RTC_CTRL_2_AF;
|
||||
esp_err_t ret = I2C_Write(PCF85063_ADDRESS, RTC_CTRL_2_ADDR, &Value, 1);
|
||||
if(ret != ESP_OK)
|
||||
printf("PCF85063 : Failed to enable Alarm Flag and Clear Alarm Flag \r\n");
|
||||
}
|
||||
|
||||
uint8_t PCF85063_Get_Alarm_Flag() // Get Alarm flag
|
||||
{
|
||||
uint8_t Value = 0;
|
||||
esp_err_t ret = I2C_Read(PCF85063_ADDRESS, RTC_CTRL_2_ADDR, &Value, 1);
|
||||
if(ret != ESP_OK)
|
||||
printf("PCF85063 : Failed to obtain a warning flag.\r\n");
|
||||
else
|
||||
Value &= RTC_CTRL_2_AF | RTC_CTRL_2_AIE;
|
||||
//printf("Value = 0x%x",Value);
|
||||
return Value;
|
||||
}
|
||||
|
||||
void PCF85063_Set_Alarm(datetime_t time) // Set Alarm
|
||||
{
|
||||
|
||||
uint8_t buf[5] ={
|
||||
decToBcd(time.second)&(~RTC_ALARM),
|
||||
decToBcd(time.minute)&(~RTC_ALARM),
|
||||
decToBcd(time.hour)&(~RTC_ALARM),
|
||||
//decToBcd(time.day)&(~RTC_ALARM),
|
||||
//decToBcd(time.dotw)&(~RTC_ALARM)
|
||||
RTC_ALARM, //disalbe day
|
||||
RTC_ALARM //disalbe weekday
|
||||
};
|
||||
esp_err_t ret = I2C_Write(PCF85063_ADDRESS, RTC_SECOND_ALARM, buf, sizeof(buf));
|
||||
if(ret != ESP_OK)
|
||||
printf("PCF85063 : Failed to set alarm flag\r\n");
|
||||
}
|
||||
|
||||
void PCF85063_Read_Alarm(datetime_t *time) // Read Alarm
|
||||
{
|
||||
uint8_t buf[5] = {0};
|
||||
esp_err_t ret = I2C_Read(PCF85063_ADDRESS, RTC_SECOND_ALARM, buf, sizeof(buf));
|
||||
if(ret != ESP_OK)
|
||||
printf("PCF85063 : Failed to read the alarm sign\r\n");
|
||||
else{
|
||||
time->second = bcdToDec(buf[0] & 0x7F);
|
||||
time->minute = bcdToDec(buf[1] & 0x7F);
|
||||
time->hour = bcdToDec(buf[2] & 0x3F);
|
||||
time->day = bcdToDec(buf[3] & 0x3F);
|
||||
time->dotw = bcdToDec(buf[4] & 0x07);
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t decToBcd(int val) // Convert normal decimal numbers to binary coded decimal
|
||||
{
|
||||
return (uint8_t)((val / 10 * 16) + (val % 10));
|
||||
}
|
||||
static int bcdToDec(uint8_t val) // Convert binary coded decimal to normal decimal numbers
|
||||
{
|
||||
return (int)((val / 16 * 10) + (val % 16));
|
||||
}
|
||||
void datetime_to_str(char *datetime_str,datetime_t time)
|
||||
{
|
||||
sprintf(datetime_str, " %d.%d.%d %d:%d:%d %s", time.year, time.month,
|
||||
time.day, time.hour, time.minute, time.second, Week[time.dotw]);
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "I2C_Driver.h"
|
||||
|
||||
//PCF85063_ADDRESS
|
||||
#define PCF85063_ADDRESS (0x51)
|
||||
//
|
||||
#define YEAR_OFFSET (1970)
|
||||
// registar overview - crtl & status reg
|
||||
#define RTC_CTRL_1_ADDR (0x00)
|
||||
#define RTC_CTRL_2_ADDR (0x01)
|
||||
#define RTC_OFFSET_ADDR (0x02)
|
||||
#define RTC_RAM_by_ADDR (0x03)
|
||||
// registar overview - time & data reg
|
||||
#define RTC_SECOND_ADDR (0x04)
|
||||
#define RTC_MINUTE_ADDR (0x05)
|
||||
#define RTC_HOUR_ADDR (0x06)
|
||||
#define RTC_DAY_ADDR (0x07)
|
||||
#define RTC_WDAY_ADDR (0x08)
|
||||
#define RTC_MONTH_ADDR (0x09)
|
||||
#define RTC_YEAR_ADDR (0x0A) // years 0-99; calculate real year = 1970 + RCC reg year
|
||||
// registar overview - alarm reg
|
||||
#define RTC_SECOND_ALARM (0x0B)
|
||||
#define RTC_MINUTE_ALARM (0x0C)
|
||||
#define RTC_HOUR_ALARM (0x0D)
|
||||
#define RTC_DAY_ALARM (0x0E)
|
||||
#define RTC_WDAY_ALARM (0x0F)
|
||||
// registar overview - timer reg
|
||||
#define RTC_TIMER_VAL (0x10)
|
||||
#define RTC_TIMER_MODE (0x11)
|
||||
|
||||
//RTC_CTRL_1 registar
|
||||
#define RTC_CTRL_1_EXT_TEST (0x80)
|
||||
#define RTC_CTRL_1_STOP (0x20) //0-RTC clock runs 1- RTC clock is stopped
|
||||
#define RTC_CTRL_1_SR (0X10) //0-no software reset 1-initiate software rese
|
||||
#define RTC_CTRL_1_CIE (0X04) //0-no correction interrupt generated 1-interrupt pulses are generated at every correction cycle
|
||||
#define RTC_CTRL_1_12_24 (0X02) //0-24H 1-12H
|
||||
#define RTC_CTRL_1_CAP_SEL (0X01) //0-7PF 1-12.5PF
|
||||
|
||||
//RTC_CTRL_2 registar
|
||||
#define RTC_CTRL_2_AIE (0X80) //alarm interrupt 0-disalbe 1-enable
|
||||
#define RTC_CTRL_2_AF (0X40) //alarm flag 0-inactive/cleared 1-active/unchanged
|
||||
#define RTC_CTRL_2_MI (0X20) //minute interrupt 0-disalbe 1-enable
|
||||
#define RTC_CTRL_2_HMI (0X10) //half minute interrupt
|
||||
#define RTC_CTRL_2_TF (0X08)
|
||||
|
||||
//
|
||||
#define RTC_OFFSET_MODE (0X80)
|
||||
|
||||
//
|
||||
#define RTC_TIMER_MODE_TE (0X04) //timer enable 0-disalbe 1-enable
|
||||
#define RTC_TIMER_MODE_TIE (0X02) //timer interrupt enable 0-disalbe 1-enable
|
||||
#define RTC_TIMER_MODE_TI_TP (0X01) //timer interrupt mode 0-interrupt follows timer flag 1-interrupt generates a pulse
|
||||
|
||||
// format
|
||||
#define RTC_ALARM (0x80) // set AEN_x registers
|
||||
#define RTC_CTRL_1_DEFAULT (0x00)
|
||||
#define RTC_CTRL_2_DEFAULT (0x00)
|
||||
|
||||
#define RTC_TIMER_FLAG (0x08)
|
||||
|
||||
typedef struct {
|
||||
uint16_t year;
|
||||
uint8_t month;
|
||||
uint8_t day;
|
||||
uint8_t dotw;
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
uint8_t second;
|
||||
}datetime_t;
|
||||
|
||||
|
||||
const unsigned char MonthStr[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"};
|
||||
const unsigned char Week[7][5] = {"SUN","Mon","Tues","Wed","Thur","Fri","Sat"};
|
||||
|
||||
extern datetime_t datetime;
|
||||
void PCF85063_Init(void);
|
||||
void PCF85063_Reset(void);
|
||||
void PCF85063Task(void *parameter);
|
||||
|
||||
void PCF85063_Set_Time(datetime_t time);
|
||||
void PCF85063_Set_Date(datetime_t date);
|
||||
void PCF85063_Set_All(datetime_t time);
|
||||
|
||||
void PCF85063_Read_Time(datetime_t *time);
|
||||
|
||||
|
||||
void PCF85063_Enable_Alarm(void);
|
||||
uint8_t PCF85063_Get_Alarm_Flag();
|
||||
void PCF85063_Set_Alarm(datetime_t time);
|
||||
void PCF85063_Read_Alarm(datetime_t *time);
|
||||
|
||||
void datetime_to_str(char *datetime_str,datetime_t time);
|
||||
|
||||
|
||||
// weekday format
|
||||
// 0 - sunday
|
||||
// 1 - monday
|
||||
// 2 - tuesday
|
||||
// 3 - wednesday
|
||||
// 4 - thursday
|
||||
// 5 - friday
|
||||
// 6 - saturday
|
||||
350
src/WS_RTC.cpp
350
src/WS_RTC.cpp
@@ -1,350 +0,0 @@
|
||||
#include "WS_RTC.h"
|
||||
|
||||
Timing_RTC CHx_State[Timing_events_Number_MAX]; // Set a maximum of Timing_events_Number_MAX timers
|
||||
char Event_str[Timing_events_Number_MAX][1000];
|
||||
static Timing_RTC CHx_State_Default; // Event initial state
|
||||
const unsigned char Event_cycle[4][13] = {"Aperiodicity","everyday","Weekly","monthly"};
|
||||
|
||||
void RTC_Init(void){
|
||||
PCF85063_Init();
|
||||
xTaskCreatePinnedToCore(
|
||||
RTCTask,
|
||||
"RTCTask",
|
||||
4096,
|
||||
NULL,
|
||||
3,
|
||||
NULL,
|
||||
0
|
||||
);
|
||||
}
|
||||
uint8_t Timing_events_Num = 0;
|
||||
void RTCTask(void *parameter)
|
||||
{
|
||||
static uint8_t Time_Old = 0;
|
||||
while(1){
|
||||
if(Timing_events_Num){
|
||||
for (int i = 0; i < Timing_events_Number_MAX; i++){
|
||||
if(CHx_State[i].Enable_Flag){
|
||||
if(CHx_State[i].Time.hour == datetime.hour && CHx_State[i].Time.minute == datetime.minute && CHx_State[i].Time.second == datetime.second && datetime.second != Time_Old){ // The event time is consistent with the current time
|
||||
switch(CHx_State[i].repetition_State){
|
||||
case Repetition_NONE:
|
||||
if(CHx_State[i].Time.year == datetime.year && CHx_State[i].Time.month == datetime.month && CHx_State[i].Time.day == datetime.day){ // Executes at the defined date and time
|
||||
TimerEvent_handling(CHx_State[i]);
|
||||
TimerEvent_Del(CHx_State[i]);
|
||||
}
|
||||
break;
|
||||
case Repetition_everyday:
|
||||
TimerEvent_handling(CHx_State[i]);
|
||||
break;
|
||||
case Repetition_Weekly:
|
||||
if(CHx_State[i].Time.dotw == datetime.dotw){
|
||||
TimerEvent_handling(CHx_State[i]);
|
||||
}
|
||||
break;
|
||||
case Repetition_monthly:
|
||||
if(CHx_State[i].Time.day == datetime.day){
|
||||
TimerEvent_handling(CHx_State[i]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
printf("Event error!!!!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Time_Old = datetime.second;
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void TimerEvent_handling(Timing_RTC event){
|
||||
uint8_t Retain_channels = 0;
|
||||
printf("Event %d : \r\n", event.Event_Number);
|
||||
char datetime_str[50];
|
||||
datetime_to_str(datetime_str,event.Time);
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
if(*(&(event.Relay_CH1)+i) == STATE_Retain) // Find the modified channel
|
||||
Retain_channels ++; // Number of unmodified channels
|
||||
}
|
||||
if(Retain_channels < Relay_Number_MAX - 1){
|
||||
printf("%s\r\n", datetime_str);
|
||||
printf("CHx Open : ");
|
||||
int j = 0;
|
||||
for (j = 0; j < Relay_Number_MAX; j++) {
|
||||
if(*(&(event.Relay_CH1)+j) == STATE_Open)
|
||||
printf("CH%d ", j+1);
|
||||
}
|
||||
printf("\r\nCHx Closs : ");
|
||||
for (j = 0; j < Relay_Number_MAX; j++) {
|
||||
if(*(&(event.Relay_CH1)+j) == STATE_Closs)
|
||||
printf("CH%d ", j+1);
|
||||
}
|
||||
if(Retain_channels){
|
||||
printf("\r\nCHx Retain : ");
|
||||
for (j = 0; j < Relay_Number_MAX; j++) {
|
||||
if(*(&(event.Relay_CH1)+j) == STATE_Retain)
|
||||
printf("CH%d ", j+1);
|
||||
}
|
||||
}
|
||||
printf("\r\n");
|
||||
Relay_Immediate_CHxn(&(event.Relay_CH1), RTC_Mode);
|
||||
printf("\r\n");
|
||||
}
|
||||
else if(Retain_channels == Relay_Number_MAX - 1){ // Modified a channel (use TimerEvent_CHx_Set())
|
||||
printf("%s\r\n", datetime_str);
|
||||
for (int x = 0; x < Relay_Number_MAX; x++) {
|
||||
if(*(&(event.Relay_CH1)+x) != STATE_Retain){ // Find the modified channel
|
||||
if(*(&(event.Relay_CH1)+x)){
|
||||
printf("CH%d Open\r\n", x);
|
||||
Relay_Immediate(x, true, RTC_Mode);
|
||||
printf("\r\n");
|
||||
}
|
||||
else{
|
||||
printf("CH%d Closs\r\n", x);
|
||||
Relay_Immediate(x, false, RTC_Mode);
|
||||
printf("\r\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
printf("Event error or no relay control!!!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void TimerEvent_CHx_Set(datetime_t time,uint8_t CHx, bool State, Repetition_event Repetition)
|
||||
{
|
||||
char datetime_str[50];
|
||||
datetime_to_str(datetime_str,datetime);
|
||||
printf("Now Time: %s!!!!\r\n", datetime_str);
|
||||
if(CHx > Relay_Number_MAX){
|
||||
printf("Timing_CHx_Set(function): Error passing parameter CHx!!!!\r\n");
|
||||
return;
|
||||
}
|
||||
if(Timing_events_Num + 1 >= Timing_events_Number_MAX)
|
||||
{
|
||||
printf("Note : The number of scheduled events is full.\r\n");
|
||||
}
|
||||
else{
|
||||
RGB_Open_Time(50, 36, 0, 1000, 0);
|
||||
CHx_State[Timing_events_Num].Enable_Flag = true;
|
||||
CHx_State[Timing_events_Num].Event_Number = Timing_events_Num + 1;
|
||||
*(&(CHx_State[Timing_events_Num].Relay_CH1)+CHx) = (Status_adjustment)State;
|
||||
CHx_State[Timing_events_Num].Time = time;
|
||||
CHx_State[Timing_events_Num].repetition_State = Repetition;
|
||||
Timing_events_Num ++;
|
||||
datetime_to_str(datetime_str,time);
|
||||
if(State){
|
||||
printf("New timing event%d :\r\n %s set CH%d Open ----- %s\r\n\r\n", Timing_events_Num, datetime_str, CHx, Event_cycle[Repetition]);
|
||||
sprintf(Event_str[Timing_events_Num-1], "Event %d : %s set CH%d Open ----- %s\\n\\n", Timing_events_Num, datetime_str, CHx, Event_cycle[Repetition]);
|
||||
}
|
||||
else{
|
||||
printf("New timing event%d :\r\n %s set CH%d Closs ----- %s\r\n\r\n", Timing_events_Num, datetime_str, CHx, Event_cycle[Repetition]);
|
||||
sprintf(Event_str[Timing_events_Num-1], "Event %d : %s set CH%d Closs ----- %s\\n\\n", Timing_events_Num, datetime_str, CHx, Event_cycle[Repetition]);
|
||||
}
|
||||
Buzzer_Open_Time(700, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void TimerEvent_CHxs_Set(datetime_t time,uint8_t PinState, Repetition_event Repetition)
|
||||
{
|
||||
|
||||
char datetime_str[50];
|
||||
datetime_to_str(datetime_str,datetime);
|
||||
printf("Now Time: %s!!!!\r\n", datetime_str);
|
||||
if(Timing_events_Num + 1 >= Timing_events_Number_MAX)
|
||||
{
|
||||
printf("Note : The number of scheduled events is full.\r\n");
|
||||
}
|
||||
else{
|
||||
RGB_Open_Time(50, 36, 0, 1000, 0);
|
||||
CHx_State[Timing_events_Num].Enable_Flag = true;
|
||||
CHx_State[Timing_events_Num].Event_Number = Timing_events_Num + 1;
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
*(&(CHx_State[Timing_events_Num].Relay_CH1)+i) = (Status_adjustment)((PinState >> i) & 0x01);
|
||||
}
|
||||
CHx_State[Timing_events_Num].Time = time;
|
||||
CHx_State[Timing_events_Num].repetition_State = Repetition;
|
||||
Timing_events_Num ++;
|
||||
datetime_to_str(datetime_str,time);
|
||||
printf("New timing event%d :\r\n %s \r\n",Timing_events_Num, datetime_str);
|
||||
printf(" CHx :");
|
||||
for (int i = 0; i < Relay_Number_MAX; i++)
|
||||
printf("CH%d ", i+1);
|
||||
printf("\r\n State :");
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
if((PinState >> i) & 0x01)
|
||||
printf("Open ");
|
||||
else
|
||||
printf("Closs ");
|
||||
}
|
||||
printf("\r\n");
|
||||
printf(" ----- %s\r\n\r\n", Event_cycle[Repetition]);
|
||||
printf("\r\n");
|
||||
Buzzer_Open_Time(700, 0);
|
||||
|
||||
int len = 0;
|
||||
char Event_content[1000];
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, " CHx :");
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "CH%d ", i + 1);
|
||||
}
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "\\n State :");
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
if ((PinState >> i) & 0x01)
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "Open ");
|
||||
else
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "Closs ");
|
||||
}
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "\\n ----- %s\\n\\n", Event_cycle[Repetition]);
|
||||
// printf("%s\r\n", Event_content);
|
||||
sprintf(Event_str[Timing_events_Num-1], "Event %d : %s \\n%s", Timing_events_Num, datetime_str,Event_content);
|
||||
}
|
||||
}
|
||||
void TimerEvent_CHxn_Set(datetime_t time,Status_adjustment *Relay_n, Repetition_event Repetition)
|
||||
{
|
||||
char datetime_str[50];
|
||||
datetime_to_str(datetime_str,datetime);
|
||||
printf("Now Time: %s!!!!\r\n", datetime_str);
|
||||
if(Timing_events_Num + 1 >= Timing_events_Number_MAX)
|
||||
{
|
||||
printf("Note : The number of scheduled events is full.\r\n");
|
||||
}
|
||||
else{
|
||||
RGB_Open_Time(50, 36, 0, 1000, 0);
|
||||
CHx_State[Timing_events_Num].Enable_Flag = true;
|
||||
CHx_State[Timing_events_Num].Event_Number = Timing_events_Num + 1;
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
*(&(CHx_State[Timing_events_Num].Relay_CH1)+i) = Relay_n[i];
|
||||
}
|
||||
CHx_State[Timing_events_Num].Time = time;
|
||||
CHx_State[Timing_events_Num].repetition_State = Repetition;
|
||||
Timing_events_Num ++;
|
||||
datetime_to_str(datetime_str,time);
|
||||
printf("New timing event%d :\r\n %s \r\n",Timing_events_Num, datetime_str);
|
||||
printf(" CHx :");
|
||||
for (int i = 0; i < Relay_Number_MAX; i++)
|
||||
printf("CH%d ", i+1);
|
||||
printf("\r\n State :");
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
if(Relay_n[i] == STATE_Open)
|
||||
printf("Open ");
|
||||
else if(Relay_n[i] == STATE_Closs)
|
||||
printf("Closs ");
|
||||
else if(Relay_n[i] == STATE_Retain)
|
||||
printf("Retain ");
|
||||
}
|
||||
printf("\r\n");
|
||||
printf(" ----- %s\r\n\r\n", Event_cycle[Repetition]);
|
||||
printf("\r\n");
|
||||
Buzzer_Open_Time(700, 0);
|
||||
|
||||
int len = 0;
|
||||
char Event_content[1000];
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, " CHx :");
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "CH%d ", i + 1);
|
||||
}
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "\\n State :");
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
if (Relay_n[i] == STATE_Open)
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "Open ");
|
||||
else if(Relay_n[i] == STATE_Closs)
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "Closs ");
|
||||
else if(Relay_n[i] == STATE_Retain)
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "Retain ");
|
||||
}
|
||||
len += snprintf(Event_content + len, sizeof(Event_content) - len, "\\n ----- %s\\n\\n", Event_cycle[Repetition]);
|
||||
// printf("%s\r\n", Event_content);
|
||||
sprintf(Event_str[Timing_events_Num-1], "Event %d : %s \\n%s", Timing_events_Num, datetime_str,Event_content);
|
||||
}
|
||||
}
|
||||
|
||||
void TimerEvent_printf(Timing_RTC event){
|
||||
uint8_t Retain_channels = 0;
|
||||
uint8_t open[8]={0};
|
||||
printf("Event %d : \r\n", event.Event_Number);
|
||||
char datetime_str[50];
|
||||
datetime_to_str(datetime_str,event.Time);
|
||||
for (int i = 0; i < Relay_Number_MAX; i++) {
|
||||
if(*(&(event.Relay_CH1)+i) == STATE_Retain) // Find the modified channel
|
||||
Retain_channels ++; // Number of unmodified channels
|
||||
else
|
||||
open[i] = *(&(event.Relay_CH1)+i);
|
||||
}
|
||||
if(Retain_channels == 0){ // All channels have been modified (use TimerEvent_CHxs_Set())
|
||||
printf("%s\r\n", datetime_str);
|
||||
printf(" CHx Open : ");
|
||||
for (int j = 0; j < Relay_Number_MAX; j++) {
|
||||
if(open[j])
|
||||
printf("CH%d ", j);
|
||||
}
|
||||
printf("\r\n CHx Closs : ");
|
||||
for (int k = 0; k < Relay_Number_MAX; k++) {
|
||||
if(!open[k])
|
||||
printf("CH%d ", k);
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
else if(Retain_channels == Relay_Number_MAX - 1){ // Modified a channel (use TimerEvent_CHx_Set())
|
||||
printf("%s ,", datetime_str);
|
||||
for (int x = 0; x < Relay_Number_MAX; x++) {
|
||||
if(*(&(event.Relay_CH1)+x) != STATE_Retain){ // Find the modified channel
|
||||
if(*(&(event.Relay_CH1)+x))
|
||||
printf("CH%d Open\r\n", x);
|
||||
else
|
||||
printf("CH%d Closs\r\n", x);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
printf("%s\r\n", datetime_str);
|
||||
printf("CHx Open : ");
|
||||
int j = 0;
|
||||
for (j = 0; j < Relay_Number_MAX; j++) {
|
||||
if(open[j] == STATE_Open)
|
||||
printf("CH%d ", j+1);
|
||||
}
|
||||
printf("\r\nCHx Closs : ");
|
||||
for (j = 0; j < Relay_Number_MAX; j++) {
|
||||
if(open[j] == STATE_Closs)
|
||||
printf("CH%d ", j+1);
|
||||
}
|
||||
printf("\r\nCHx Retain : ");
|
||||
for (j = 0; j < Relay_Number_MAX; j++) {
|
||||
if(open[j] == STATE_Retain)
|
||||
printf("CH%d ", j+1);
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void TimerEvent_printf_ALL(void)
|
||||
{
|
||||
printf("/******************* Current RTC event *******************/ \r\n");
|
||||
for (int i = 0; i < Timing_events_Number_MAX; i++) {
|
||||
if(CHx_State[i].Enable_Flag)
|
||||
TimerEvent_printf(CHx_State[i]);
|
||||
}
|
||||
printf("/******************* Current RTC event *******************/\r\n\r\n ");
|
||||
}
|
||||
void TimerEvent_Del(Timing_RTC event){
|
||||
RGB_Open_Time(20, 0, 50, 1000, 0);
|
||||
printf("Example Delete an RTC event%d\r\n\r\n",event.Event_Number);
|
||||
for (int i = event.Event_Number; i < Timing_events_Number_MAX; i++) {
|
||||
CHx_State[i].Event_Number = CHx_State[i].Event_Number -1;
|
||||
CHx_State[i-1] = CHx_State[i];
|
||||
}
|
||||
CHx_State[Timing_events_Number_MAX - 1] = CHx_State_Default;
|
||||
memset(Event_str[Timing_events_Number_MAX - 1], 0, sizeof(Event_str[Timing_events_Number_MAX - 1]));
|
||||
Timing_events_Num --;
|
||||
}
|
||||
void TimerEvent_Del_Number(uint8_t Event_Number){
|
||||
TimerEvent_Del(CHx_State[Event_Number - 1]);
|
||||
Buzzer_Open_Time(700, 300);
|
||||
}
|
||||
45
src/WS_RTC.h
45
src/WS_RTC.h
@@ -1,45 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "WS_PCF85063.h"
|
||||
#include "WS_Relay.h"
|
||||
#include "WS_GPIO.h"
|
||||
|
||||
#define Timing_events_Number_MAX 10 // Indicates the number of timers that can be set
|
||||
|
||||
typedef enum {
|
||||
Repetition_NONE = 0, // aperiodicity
|
||||
Repetition_everyday = 1, // The event is repeated at this time every day
|
||||
Repetition_Weekly = 2, // This event is repeated every week at this time
|
||||
Repetition_monthly = 3, // This event is repeated every month at this time
|
||||
} Repetition_event;
|
||||
|
||||
typedef struct {
|
||||
bool Enable_Flag = false; // The timer event enabled flag.
|
||||
uint8_t Event_Number = 0; // Current event sequence number
|
||||
Status_adjustment Relay_CH1 = STATE_Retain; // The CH1 status is changed periodically
|
||||
Status_adjustment Relay_CH2 = STATE_Retain; // The CH2 status is changed periodically
|
||||
Status_adjustment Relay_CH3 = STATE_Retain; // The CH3 status is changed periodically
|
||||
Status_adjustment Relay_CH4 = STATE_Retain; // The CH4 status is changed periodically
|
||||
Status_adjustment Relay_CH5 = STATE_Retain; // The CH5 status is changed periodically
|
||||
Status_adjustment Relay_CH6 = STATE_Retain; // The CH6 status is changed periodically
|
||||
Status_adjustment Relay_CH7 = STATE_Retain; // The CH7 status is changed periodically
|
||||
Status_adjustment Relay_CH8 = STATE_Retain; // The CH8 status is changed periodically
|
||||
datetime_t Time;
|
||||
Repetition_event repetition_State = Repetition_NONE; // Periodic execution
|
||||
}Timing_RTC;
|
||||
|
||||
extern uint8_t Timing_events_Num;
|
||||
extern Timing_RTC CHx_State[Timing_events_Number_MAX];
|
||||
extern char Event_str[Timing_events_Number_MAX][1000];
|
||||
|
||||
void RTCTask(void *parameter);
|
||||
void TimerEvent_handling(Timing_RTC event);
|
||||
void TimerEvent_printf(Timing_RTC event);
|
||||
void TimerEvent_Del(Timing_RTC event);
|
||||
|
||||
void RTC_Init(void);
|
||||
void TimerEvent_CHx_Set(datetime_t time,uint8_t CHx, bool State, Repetition_event Repetition);
|
||||
void TimerEvent_CHxs_Set(datetime_t time,uint8_t PinState, Repetition_event Repetition);
|
||||
void TimerEvent_CHxn_Set(datetime_t time,Status_adjustment *Relay_n, Repetition_event Repetition);
|
||||
void TimerEvent_printf_ALL(void);
|
||||
void TimerEvent_Del_Number(uint8_t Event_Number);
|
||||
@@ -3,6 +3,6 @@ void Serial_Init()
|
||||
{
|
||||
if(RS485_CAN_Enable)
|
||||
RS485_Init();
|
||||
else
|
||||
CAN_Init();
|
||||
//else
|
||||
//CAN_Init();
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "WS_Information.h"
|
||||
#include "WS_RS485.h"
|
||||
#include "WS_CAN.h"
|
||||
//#include "WS_CAN.h"
|
||||
|
||||
void Serial_Init(); // Example Initialize the system serial port and RS485
|
||||
void Serial_Loop(); // Read RS485 data, parse and control relays
|
||||
|
||||
@@ -1,107 +0,0 @@
|
||||
#include "WS_TCA9554PWR.h"
|
||||
|
||||
/***************************************************** Operation register REG ****************************************************/
|
||||
uint8_t Read_REG(uint8_t REG) // Read the value of the TCA9554PWR register REG
|
||||
{
|
||||
Wire.beginTransmission(TCA9554_ADDRESS);
|
||||
Wire.write(REG);
|
||||
uint8_t result = Wire.endTransmission();
|
||||
if (result != 0) {
|
||||
printf("Data Transfer Failure !!!\r\n");
|
||||
}
|
||||
Wire.requestFrom(TCA9554_ADDRESS, 1);
|
||||
uint8_t bitsStatus = Wire.read();
|
||||
return bitsStatus;
|
||||
}
|
||||
uint8_t Write_REG(uint8_t REG,uint8_t Data) // Write Data to the REG register of the TCA9554PWR
|
||||
{
|
||||
Wire.beginTransmission(TCA9554_ADDRESS);
|
||||
Wire.write(REG);
|
||||
Wire.write(Data);
|
||||
uint8_t result = Wire.endTransmission();
|
||||
if (result != 0) {
|
||||
printf("Data write failure!!!\r\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/********************************************************** Set EXIO mode **********************************************************/
|
||||
void Mode_EXIO(uint8_t Pin,uint8_t State) // Set the mode of the TCA9554PWR Pin. The default is Output mode (output mode or input mode). State: 0= Output mode 1= input mode
|
||||
{
|
||||
uint8_t bitsStatus = Read_REG(TCA9554_CONFIG_REG);
|
||||
uint8_t Data = (0x01 << (Pin-1)) | bitsStatus;
|
||||
uint8_t result = Write_REG(TCA9554_CONFIG_REG,Data);
|
||||
if (result != 0) {
|
||||
printf("I/O Configuration Failure !!!\r\n");
|
||||
}
|
||||
}
|
||||
void Mode_EXIOS(uint8_t PinState) // Set the mode of the 7 pins from the TCA9554PWR with PinState
|
||||
{
|
||||
uint8_t result = Write_REG(TCA9554_CONFIG_REG,PinState);
|
||||
if (result != 0) {
|
||||
printf("I/O Configuration Failure !!!\r\n");
|
||||
}
|
||||
}
|
||||
/********************************************************** Read EXIO status **********************************************************/
|
||||
uint8_t Read_EXIO(uint8_t Pin) // Read the level of the TCA9554PWR Pin
|
||||
{
|
||||
uint8_t inputBits = Read_REG(TCA9554_INPUT_REG);
|
||||
uint8_t bitStatus = (inputBits >> (Pin-1)) & 0x01;
|
||||
return bitStatus;
|
||||
}
|
||||
uint8_t Read_EXIOS(uint8_t REG = TCA9554_INPUT_REG) // Read the level of all pins of TCA9554PWR, the default read input level state, want to get the current IO output state, pass the parameter TCA9554_OUTPUT_REG, such as Read_EXIOS(TCA9554_OUTPUT_REG);
|
||||
{
|
||||
uint8_t inputBits = Read_REG(REG);
|
||||
return inputBits;
|
||||
}
|
||||
|
||||
/********************************************************** Set the EXIO output status **********************************************************/
|
||||
bool Set_EXIO(uint8_t Pin,uint8_t State) // Sets the level state of the Pin without affecting the other pins
|
||||
{
|
||||
uint8_t Data;
|
||||
if(State < 2 && Pin < 9 && Pin > 0){
|
||||
uint8_t bitsStatus = Read_EXIOS(TCA9554_OUTPUT_REG);
|
||||
if(State == 1)
|
||||
Data = (0x01 << (Pin-1)) | bitsStatus;
|
||||
else if(State == 0)
|
||||
Data = (~(0x01 << (Pin-1))) & bitsStatus;
|
||||
uint8_t result = Write_REG(TCA9554_OUTPUT_REG,Data);
|
||||
if (result != 0) {
|
||||
printf("Failed to set GPIO!!!\r\n");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Parameter error, please enter the correct parameter!\r\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
bool Set_EXIOS(uint8_t PinState) // Set 7 pins to the PinState state such as :PinState=0x23, 0010 0011 state (the highest bit is not used)
|
||||
{
|
||||
uint8_t result = Write_REG(TCA9554_OUTPUT_REG,PinState);
|
||||
if (result != 0) {
|
||||
printf("Failed to set GPIO!!!\r\n");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/********************************************************** Flip EXIO state **********************************************************/
|
||||
bool Set_Toggle(uint8_t Pin) // Flip the level of the TCA9554PWR Pin
|
||||
{
|
||||
uint8_t bitsStatus = Read_EXIO(Pin);
|
||||
uint8_t result = Set_EXIO(Pin,(bool)!bitsStatus);
|
||||
if (!result) {
|
||||
printf("Failed to Toggle GPIO!!!\r\n");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/********************************************************* TCA9554PWR Initializes the device ***********************************************************/
|
||||
void TCA9554PWR_Init(uint8_t PinMode, uint8_t PinState) // Set the seven pins to PinState state, for example :PinState=0x23, 0010 0011 State (Output mode or input mode) 0= Output mode 1= Input mode. The default value is output mode
|
||||
{
|
||||
Set_EXIOS(PinState);
|
||||
Mode_EXIOS(PinMode);
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include "I2C_Driver.h"
|
||||
|
||||
/****************************************************** The macro defines the TCA9554PWR information ******************************************************/
|
||||
|
||||
#define TCA9554_ADDRESS 0x20 // TCA9554PWR I2C address
|
||||
#define TCA9554_INPUT_REG 0x00 // Input register,input level
|
||||
#define TCA9554_OUTPUT_REG 0x01 // Output register, high and low level output
|
||||
#define TCA9554_Polarity_REG 0x02 // The Polarity Inversion register (register 2) allows polarity inversion of pins defined as inputs by the Configuration register.
|
||||
#define TCA9554_CONFIG_REG 0x03 // Configuration register, mode configuration
|
||||
|
||||
|
||||
#define Low 0
|
||||
#define High 1
|
||||
#define EXIO_PIN1 1
|
||||
#define EXIO_PIN2 2
|
||||
#define EXIO_PIN3 3
|
||||
#define EXIO_PIN4 4
|
||||
#define EXIO_PIN5 5
|
||||
#define EXIO_PIN6 6
|
||||
#define EXIO_PIN7 7
|
||||
#define EXIO_PIN8 8
|
||||
|
||||
/***************************************************** Operation register REG ****************************************************/
|
||||
uint8_t Read_REG(uint8_t REG); // Read the value of the TCA9554PWR register REG
|
||||
uint8_t Write_REG(uint8_t REG,uint8_t Data); // Write Data to the REG register of the TCA9554PWR
|
||||
/********************************************************** Set EXIO mode **********************************************************/
|
||||
void Mode_EXIO(uint8_t Pin,uint8_t State); // Set the mode of the TCA9554PWR Pin. The default is Output mode (output mode or input mode). State: 0= Output mode 1= input mode
|
||||
void Mode_EXIOS(uint8_t PinState); // Set the mode of the 7 pins from the TCA9554PWR with PinState
|
||||
/********************************************************** Read EXIO status **********************************************************/
|
||||
uint8_t Read_EXIO(uint8_t Pin); // Read the level of the TCA9554PWR Pin
|
||||
uint8_t Read_EXIOS(uint8_t REG); // Read the level of all pins of TCA9554PWR, the default read input level state, want to get the current IO output state, pass the parameter TCA9554_OUTPUT_REG, such as Read_EXIOS(TCA9554_OUTPUT_REG);
|
||||
/********************************************************** Set the EXIO output status **********************************************************/
|
||||
bool Set_EXIO(uint8_t Pin,uint8_t State); // Sets the level state of the Pin without affecting the other pins
|
||||
bool Set_EXIOS(uint8_t PinState); // Set 7 pins to the PinState state such as :PinState=0x23, 0010 0011 state (the highest bit is not used)
|
||||
/********************************************************** Flip EXIO state **********************************************************/
|
||||
bool Set_Toggle(uint8_t Pin); // Flip the level of the TCA9554PWR Pin
|
||||
/********************************************************* TCA9554PWR Initializes the device ***********************************************************/
|
||||
void TCA9554PWR_Init(uint8_t PinMode = 0x00, uint8_t PinState = 0x00); // Set the seven pins to PinState state, for example :PinState=0x23, 0010 0011 State (the highest bit is not used) (Output mode or input mode) 0= Output mode 1= Input mode. The default value is output mode
|
||||
Reference in New Issue
Block a user