Feature RTC support added (experimental)
This commit is contained in:
parent
1efaf9a04e
commit
a3414c5622
@ -11,6 +11,11 @@
|
||||
#include "bme680mems.h"
|
||||
#endif
|
||||
|
||||
// Needed for RTC time sync if RTC present on board
|
||||
#ifdef HAS_RTC
|
||||
#include "rtc.h"
|
||||
#endif
|
||||
|
||||
void doHousekeeping(void);
|
||||
void do_timesync(void);
|
||||
uint64_t uptime(void);
|
||||
|
@ -17,6 +17,11 @@
|
||||
#include <Wire.h>
|
||||
#endif
|
||||
|
||||
// Needed for RTC time sync if RTC present on board
|
||||
#ifdef HAS_RTC
|
||||
#include "rtc.h"
|
||||
#endif
|
||||
|
||||
extern QueueHandle_t LoraSendQueue;
|
||||
|
||||
void onEvent(ev_t ev);
|
||||
|
16
include/rtc.h
Normal file
16
include/rtc.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef _RTC_H
|
||||
#define _RTC_H
|
||||
|
||||
#include "globals.h"
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
|
||||
extern RtcDS3231<TwoWire> Rtc; // Make RTCDS3231 instance globally availabe
|
||||
|
||||
int rtc_init(void);
|
||||
int set_rtc(uint32_t UTCTime);
|
||||
int set_rtc(RtcDateTime now);
|
||||
uint32_t get_rtc();
|
||||
float get_rtc_temp();
|
||||
|
||||
#endif
|
@ -128,6 +128,16 @@ void do_timesync() {
|
||||
if (gps.time.isValid()) {
|
||||
setTime(gps.time.hour(), gps.time.minute(), gps.time.second(),
|
||||
gps.date.day(), gps.date.month(), gps.date.year());
|
||||
#ifdef HAS_RTC
|
||||
RtcDateTime now;
|
||||
now.year = gps.time.year();
|
||||
now.month = gps.date.month();
|
||||
now.dayOfMonth = gps.date.day();
|
||||
now.hour = gps.time.hour();
|
||||
now.minute = gps.time.minute();
|
||||
now.second = gps.time.second();
|
||||
set_rtc(now);
|
||||
#endif
|
||||
ESP_LOGI(TAG, "Time synced by GPS to %02d:%02d:%02d", hour(), minute(),
|
||||
second());
|
||||
return;
|
||||
|
@ -455,6 +455,9 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
|
||||
|
||||
// Update system time with time read from the network
|
||||
setTime(*pUserUTCTime);
|
||||
#ifdef HAS_RTC
|
||||
set_rtc(*pUserUTCTime);
|
||||
#endif
|
||||
ESP_LOGI(TAG, "Time synced by LoRa network to %02d:%02d:%02d", hour(),
|
||||
minute(), second());
|
||||
}
|
@ -171,6 +171,12 @@ void setup() {
|
||||
0); // CPU core
|
||||
#endif
|
||||
|
||||
// initialize RTC
|
||||
#ifdef HAS_RTC
|
||||
strcat_P(features, " RTC");
|
||||
assert(rtc_init());
|
||||
#endif
|
||||
|
||||
// initialize wifi antenna
|
||||
#ifdef HAS_ANTENNA_SWITCH
|
||||
strcat_P(features, " ANT");
|
||||
|
112
src/rtc.cpp
Normal file
112
src/rtc.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
#ifdef HAS_RTC
|
||||
|
||||
#include "rtc.h"
|
||||
|
||||
// Local logging tag
|
||||
static const char TAG[] = "main";
|
||||
|
||||
RtcDS3231<TwoWire> Rtc(Wire);
|
||||
|
||||
// initialize RTC
|
||||
int rtc_init() {
|
||||
|
||||
// return = 0 -> error / return = 1 -> success
|
||||
|
||||
// block i2c bus access
|
||||
if (xSemaphoreTake(I2Caccess, (2 * DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) ==
|
||||
pdTRUE) {
|
||||
|
||||
Wire.begin(HAS_RTC);
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
|
||||
if (!Rtc.IsDateTimeValid()) {
|
||||
ESP_LOGW(TAG, "RTC has no valid RTC date/time, setting to compilation date");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning()) {
|
||||
ESP_LOGI(TAG, "RTC not running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
if (now < compiled) {
|
||||
ESP_LOGI(TAG, "RTC date/time is older than compilation date, updating)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
// configure RTC chip
|
||||
Rtc.Enable32kHzPin(false);
|
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "I2c bus busy - RTC initialization error");
|
||||
goto error;
|
||||
}
|
||||
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
ESP_LOGI(TAG, "RTC initialized");
|
||||
return 1;
|
||||
|
||||
error:
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
return 0;
|
||||
|
||||
} // rtc_init()
|
||||
|
||||
int set_rtc(uint32_t UTCTime) {
|
||||
// return = 0 -> error / return = 1 -> success
|
||||
#ifdef HAS_RTC
|
||||
// block i2c bus access
|
||||
while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) {
|
||||
Rtc.SetDateTime(RtcDateTime(UTCTime));
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
return 1;
|
||||
} // while
|
||||
return 0;
|
||||
#endif
|
||||
} // set_rtc()
|
||||
|
||||
int set_rtc(RtcDateTime now) {
|
||||
// return = 0 -> error / return = 1 -> success
|
||||
#ifdef HAS_RTC
|
||||
// block i2c bus access
|
||||
while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) {
|
||||
Rtc.SetDateTime(now);
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
return 1;
|
||||
} // while
|
||||
return 0;
|
||||
#endif
|
||||
} // set_rtc()
|
||||
|
||||
uint32_t get_rtc() {
|
||||
#ifdef HAS_RTC
|
||||
// block i2c bus access
|
||||
while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) {
|
||||
if (!Rtc.IsDateTimeValid()) {
|
||||
ESP_LOGW(TAG, "RTC lost confidence in the DateTime");
|
||||
return 0;
|
||||
}
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
return Rtc.GetDateTime();
|
||||
} // while
|
||||
return 0;
|
||||
#endif
|
||||
} // get_rtc()
|
||||
|
||||
float get_rtc_temp() {
|
||||
#ifdef HAS_RTC
|
||||
// block i2c bus access
|
||||
while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) {
|
||||
RtcTemperature temp = Rtc.GetTemperature();
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
return temp.AsFloatDegC();
|
||||
} // while
|
||||
return 0;
|
||||
#endif
|
||||
} // get_rtc()
|
||||
|
||||
#endif // HAS_RTC
|
Loading…
Reference in New Issue
Block a user