Skip to content

Commit

Permalink
Merge pull request #244 from caternuson/iss243_busio
Browse files Browse the repository at this point in the history
Convert to BusIO and refactor
  • Loading branch information
drak7 authored Oct 29, 2021
2 parents 80bae7a + 3dff7be commit 27b5be2
Show file tree
Hide file tree
Showing 10 changed files with 1,006 additions and 1,237 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ STM32F2 | | | X |
Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!

# Dependencies
* [TinyWireM](https://github.com/adafruit/TinyWireM)
* [Adafruit BusIO](https://github.com/adafruit/Adafruit_BusIO)

# Contributing

Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=RTClib
version=1.14.2
version=2.0.0
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=A fork of Jeelab's fantastic RTC library
paragraph=Works with DS1307, DS3231, PCF8523, PCF8563 on multiple architectures
category=Timing
url=https://github.com/adafruit/RTClib
architectures=*
depends=TinyWireM
depends=Adafruit BusIO
134 changes: 134 additions & 0 deletions src/RTC_DS1307.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "RTClib.h"

#define DS1307_ADDRESS 0x68 ///< I2C address for DS1307
#define DS1307_CONTROL 0x07 ///< Control register
#define DS1307_NVRAM 0x08 ///< Start of RAM registers - 56 bytes, 0x08 to 0x3f

/**************************************************************************/
/*!
@brief Start I2C for the DS1307 and test succesful connection
@param wireInstance pointer to the I2C bus
@return True if Wire can find DS1307 or false otherwise.
*/
/**************************************************************************/
boolean RTC_DS1307::begin(TwoWire *wireInstance) {
if (i2c_dev)
delete i2c_dev;
i2c_dev = new Adafruit_I2CDevice(DS1307_ADDRESS, wireInstance);
if (!i2c_dev->begin())
return false;
return true;
}

/**************************************************************************/
/*!
@brief Is the DS1307 running? Check the Clock Halt bit in register 0
@return 1 if the RTC is running, 0 if not
*/
/**************************************************************************/
uint8_t RTC_DS1307::isrunning(void) { return !(read_register(0) >> 7); }

/**************************************************************************/
/*!
@brief Set the date and time in the DS1307
@param dt DateTime object containing the desired date/time
*/
/**************************************************************************/
void RTC_DS1307::adjust(const DateTime &dt) {
uint8_t buffer[8] = {0,
bin2bcd(dt.second()),
bin2bcd(dt.minute()),
bin2bcd(dt.hour()),
0,
bin2bcd(dt.day()),
bin2bcd(dt.month()),
bin2bcd(dt.year() - 2000U)};
i2c_dev->write(buffer, 8);
}

/**************************************************************************/
/*!
@brief Get the current date and time from the DS1307
@return DateTime object containing the current date and time
*/
/**************************************************************************/
DateTime RTC_DS1307::now() {
uint8_t buffer[7];
buffer[0] = 0;
i2c_dev->write_then_read(buffer, 1, buffer, 7);

return DateTime(bcd2bin(buffer[6]) + 2000U, bcd2bin(buffer[5]),
bcd2bin(buffer[4]), bcd2bin(buffer[2]), bcd2bin(buffer[1]),
bcd2bin(buffer[0] & 0x7F));
}

/**************************************************************************/
/*!
@brief Read the current mode of the SQW pin
@return Mode as Ds1307SqwPinMode enum
*/
/**************************************************************************/
Ds1307SqwPinMode RTC_DS1307::readSqwPinMode() {
return static_cast<Ds1307SqwPinMode>(read_register(DS1307_CONTROL) & 0x93);
}

/**************************************************************************/
/*!
@brief Change the SQW pin mode
@param mode The mode to use
*/
/**************************************************************************/
void RTC_DS1307::writeSqwPinMode(Ds1307SqwPinMode mode) {
write_register(DS1307_CONTROL, mode);
}

/**************************************************************************/
/*!
@brief Read data from the DS1307's NVRAM
@param buf Pointer to a buffer to store the data - make sure it's large
enough to hold size bytes
@param size Number of bytes to read
@param address Starting NVRAM address, from 0 to 55
*/
/**************************************************************************/
void RTC_DS1307::readnvram(uint8_t *buf, uint8_t size, uint8_t address) {
uint8_t addrByte = DS1307_NVRAM + address;
i2c_dev->write_then_read(&addrByte, 1, buf, size);
}

/**************************************************************************/
/*!
@brief Write data to the DS1307 NVRAM
@param address Starting NVRAM address, from 0 to 55
@param buf Pointer to buffer containing the data to write
@param size Number of bytes in buf to write to NVRAM
*/
/**************************************************************************/
void RTC_DS1307::writenvram(uint8_t address, uint8_t *buf, uint8_t size) {
uint8_t addrByte = DS1307_NVRAM + address;
i2c_dev->write(buf, size, true, &addrByte, 1);
}

/**************************************************************************/
/*!
@brief Shortcut to read one byte from NVRAM
@param address NVRAM address, 0 to 55
@return The byte read from NVRAM
*/
/**************************************************************************/
uint8_t RTC_DS1307::readnvram(uint8_t address) {
uint8_t data;
readnvram(&data, 1, address);
return data;
}

/**************************************************************************/
/*!
@brief Shortcut to write one byte to NVRAM
@param address NVRAM address, 0 to 55
@param data One byte to write
*/
/**************************************************************************/
void RTC_DS1307::writenvram(uint8_t address, uint8_t data) {
writenvram(address, &data, 1);
}
Loading

0 comments on commit 27b5be2

Please sign in to comment.