Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)

idf_component_register(SRCS "src/ErriezDS3231.cpp"
INCLUDE_DIRS "." "src/"
REQUIRES arduino)

project(ErriezDS3231)
60 changes: 49 additions & 11 deletions src/ErriezDS3231.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,56 @@
* Documentation: https://erriez.github.io/ErriezDS3231
*/

#if (defined(__AVR__) || defined(ARDUINO_ARCH_SAM))

#if defined(__AVR__) || defined(ARDUINO_ARCH_SAM)
#include <avr/pgmspace.h>
#elif defined(ESP8266) || defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
#include "pgmspace.h"
#else
#include <pgmspace.h>
#define pgm_read_byte(addr) \
(*(const unsigned char *)(addr)) ///< PROGMEM workaround for non-AVR
#endif

#if !defined(__ARM_ARCH) && !defined(ENERGIA) && !defined(ESP8266) && \
!defined(ESP32) && !defined(__arc__)
#include <util/delay.h>
#endif





#include <Wire.h>

#include "ErriezDS3231.h"

/*!
* \brief Initialize and detect DS3231 RTC.
* @param theWire interface Pointer.
* \details
* Call this function from setup().
* \retval true
* RTC detected.
* \retval false
* RTC not detected.
*/
bool ErriezDS3231::begin(TwoWire *theWire)
{
_wire = theWire;
// Check zero bits in status register
if (readRegister(DS3231_REG_STATUS) & 0x70) {
return false;
}

// DS3231 detected
return true;
}

/*!
* \brief Initialize and detect DS3231 RTC.
*
* \details
* Uses default TwoWire interface.
* Call this function from setup().
* \retval true
* RTC detected.
Expand All @@ -51,6 +88,7 @@
*/
bool ErriezDS3231::begin()
{
_wire = &Wire;
// Check zero bits in status register
if (readRegister(DS3231_REG_STATUS) & 0x70) {
return false;
Expand Down Expand Up @@ -869,12 +907,12 @@ bool ErriezDS3231::writeRegister(uint8_t reg, uint8_t value)
bool ErriezDS3231::writeBuffer(uint8_t reg, void *buffer, uint8_t writeLen)
{
// Start I2C transfer by writing the I2C address, register number and optional buffer
Wire.beginTransmission(DS3231_ADDR);
Wire.write(reg);
_wire->beginTransmission(DS3231_ADDR);
_wire->write(reg);
for (uint8_t i = 0; i < writeLen; i++) {
Wire.write(((uint8_t *)buffer)[i]);
_wire->write(((uint8_t *)buffer)[i]);
}
if (Wire.endTransmission(true) != 0) {
if (_wire->endTransmission(true) != 0) {
return false;
}

Expand All @@ -897,15 +935,15 @@ bool ErriezDS3231::writeBuffer(uint8_t reg, void *buffer, uint8_t writeLen)
bool ErriezDS3231::readBuffer(uint8_t reg, void *buffer, uint8_t readLen)
{
// Start I2C transfer by writing the I2C address and register number
Wire.beginTransmission(DS3231_ADDR);
Wire.write(reg);
_wire->beginTransmission(DS3231_ADDR);
_wire->write(reg);
// Generate a repeated start, followed by a read buffer
if (Wire.endTransmission(false) != 0) {
if (_wire->endTransmission(false) != 0) {
return false;
}
Wire.requestFrom((uint8_t)DS3231_ADDR, readLen);
_wire->requestFrom((uint8_t)DS3231_ADDR, readLen);
for (uint8_t i = 0; i < readLen; i++) {
((uint8_t *)buffer)[i] = (uint8_t)Wire.read();
((uint8_t *)buffer)[i] = (uint8_t)_wire->read();
}

return true;
Expand Down
6 changes: 6 additions & 0 deletions src/ErriezDS3231.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <stdint.h>
#include <time.h>
#include <wire.h>

//! DS3231 registers
#define DS3231_REG_SECONDS 0x00 //!< Seconds register
Expand Down Expand Up @@ -150,6 +151,7 @@ class ErriezDS3231
{
public:
// Initialize
bool begin(TwoWire *theWire);
bool begin();

// Oscillator functions
Expand Down Expand Up @@ -201,6 +203,10 @@ class ErriezDS3231
// Read/write buffer
bool readBuffer(uint8_t reg, void *buffer, uint8_t len);
bool writeBuffer(uint8_t reg, void *buffer, uint8_t len);

private:
TwoWire *_wire; /**< Wire object */

};

#endif // ERRIEZ_DS3231_H_