34#include "freertos/FreeRTOS.h"
40#define DS3231_ADDR 0x68
41#define DS3231_FREQ 400000
53DateTime::DateTime(
int seconds_,
int minutes_,
int hours_,
int dayOfMonth_,
int month_,
int year_)
57 dayOfMonth(dayOfMonth_),
66void DateTime::calcDayOfWeek()
68 static const int8_t t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
69 int y = year - (month < 3);
70 dayOfWeek = 1 + (y + y / 4 - y / 100 + y / 400 + t[month - 1] + dayOfMonth) % 7;
75time_t DateTime::timestamp(
int timezone)
81 m.tm_mday = dayOfMonth;
83 m.tm_year = year - 1900;
85 return mktime(&m) - 3600 * timezone;
96 m_dateTimeValid(false)
106 uint8_t status = readReg(0x0F);
108 m_dateTimeValid = (status & 0x80) == 0;
112uint8_t DS3231::readReg(
int reg)
115 m_i2c->
write(DS3231_ADDR, &b, 1, DS3231_FREQ);
116 m_available = m_i2c->
read(DS3231_ADDR, &b, 1, DS3231_FREQ);
121bool DS3231::writeReg(
int reg,
int value)
123 uint8_t buf[2] = { (uint8_t)reg, (uint8_t)value };
124 return m_i2c->
write(DS3231_ADDR, buf, 2, DS3231_FREQ);
132 m_i2c->
write(DS3231_ADDR, &b, 1, DS3231_FREQ);
134 m_available = m_i2c->
read(DS3231_ADDR, buf, 7, DS3231_FREQ);
138 m_datetime.
seconds = (buf[0] & 0x0f) + ((buf[0] & 0x70) >> 4) * 10;
139 m_datetime.
minutes = (buf[1] & 0x0f) + ((buf[1] & 0x70) >> 4) * 10;
140 m_datetime.
hours = (buf[2] & 0x0f) + ((buf[2] & 0x10) >> 4) * 10;
141 if (buf[2] & (1 << 6)) {
143 if (buf[2] & (1 << 5))
144 m_datetime.
hours += 12;
147 m_datetime.
hours += ((buf[2] & 0x20) >> 4) * 10;
150 m_datetime.
dayOfMonth = (buf[4] & 0x0f) + ((buf[4] & 0x30) >> 4) * 10;
151 m_datetime.
month = (buf[5] & 0x0f) + ((buf[5] & 0x10) >> 4) * 10;
152 m_datetime.
year = (buf[6] & 0x0f) + ((buf[6] & 0xf0) >> 4) * 10 + 2000;
163 buf[1] = (value.seconds % 10) | ((value.seconds / 10) << 4);
164 buf[2] = (value.minutes % 10) | ((value.minutes / 10) << 4);
165 buf[3] = (value.hours % 10) | ((value.hours / 10) << 4);
166 buf[4] = value.dayOfWeek;
167 buf[5] = (value.dayOfMonth % 10) | ((value.dayOfMonth / 10) << 4);;
168 buf[6] = (value.month % 10) | ((value.month / 10) << 4);
169 buf[7] = ((value.year - 2000) % 10) | (((value.year - 2000) / 10) << 4);
170 m_i2c->
write(DS3231_ADDR, buf, 8, DS3231_FREQ);
173 uint8_t st = readReg(0x0f);
174 writeReg(0x0f, st & 0x7f);
182 if ((readReg(0x0f) & (0b100)) == 0) {
184 uint8_t r = readReg(0x0e) | 0x20;
187 vTaskDelay(2 / portTICK_PERIOD_MS);
188 while ((readReg(0x0f) & (0b100)) != 0 && m_available) {
195 m_i2c->
write(DS3231_ADDR, &b, 1, DS3231_FREQ);
197 m_available = m_i2c->
read(DS3231_ADDR, buf, 2, DS3231_FREQ);
199 return (int8_t)buf[0] + 0.25 * (buf[1] >> 6);
205 uint8_t r = readReg(0x0e);
206 writeReg(0x0e, value ? (0x7f & r) : (0x80 | r));
This file contains the DS3231 (Real Time Clock) device driver.
double temperature()
Forces DS3231 to read current temperature.
bool setDateTime(DateTime const &value)
Sets current date and time.
DateTime const & datetime()
Queries DS3231 for current date and time.
void begin(I2C *i2c)
Initializes DS3231 driver.
void clockEnabled(bool value)
Enables or disables DS3231 oscillator.
bool write(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Sends a buffer to I2C bus.
int read(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Receives a buffer from I2C bus.
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs.
Represents date and time.