ds1307: add eeprom read/write
This commit is contained in:
parent
b6320b0ddf
commit
b69a63e5f4
|
@ -34,6 +34,8 @@
|
|||
#include "i2c_master.h" // i2c utilities
|
||||
|
||||
#define RTC_DS1307_I2C_ADDR 0x68 /**< DS1307 I2C address (fixed to 0b1101000) */
|
||||
#define RTC_DS1307_EEPROM_ADDR 0x50 /**< AT24C32 EEPROM I2C address (0b1010xxx, xxx settable using pins), often also present on tiny RTC modules */
|
||||
#define RTC_DS1307_EEPROM_SIZE 4096 /**< EEPROM size in bytes (32kb/4096B for AT24C32, 32kb/8192B for AT24C64) */
|
||||
|
||||
void rtc_ds1307_setup(void)
|
||||
{
|
||||
|
@ -186,7 +188,7 @@ uint8_t* rtc_ds1307_read_time(void)
|
|||
return time;
|
||||
}
|
||||
|
||||
bool rtc_ds1307_read_ram(uint8_t* data, uint8_t start, uint8_t length)
|
||||
bool rtc_ds1307_read_ram(uint8_t start, uint8_t* data, uint8_t length)
|
||||
{
|
||||
// sanity checks
|
||||
if (data==NULL || length==0) { // nothing to read
|
||||
|
@ -200,6 +202,20 @@ bool rtc_ds1307_read_ram(uint8_t* data, uint8_t start, uint8_t length)
|
|||
return i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read RAM (starting at 0x08)
|
||||
}
|
||||
|
||||
bool rtc_ds1307_read_rom(uint16_t start, uint8_t* data, uint16_t length)
|
||||
{
|
||||
// sanity checks
|
||||
if (data==NULL || length==0) { // nothing to read
|
||||
return false;
|
||||
}
|
||||
if (start>RTC_DS1307_EEPROM_SIZE-1 || start+length>RTC_DS1307_EEPROM_SIZE) { // out of bounds ROM
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint8_t address[] = {start>>8, start}; // memory address for data
|
||||
return i2c_master_read(RTC_DS1307_EEPROM_ADDR, address, LENGTH(address), data, LENGTH(data)); // read ROM
|
||||
}
|
||||
|
||||
bool rtc_ds1307_oscillator_disable(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to write CH value data over I2C
|
||||
|
@ -383,7 +399,7 @@ bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint
|
|||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_ram(uint8_t* data, uint8_t start, uint8_t length)
|
||||
bool rtc_ds1307_write_ram(uint8_t start, uint8_t* data, uint8_t length)
|
||||
{
|
||||
// sanity checks
|
||||
if (data==NULL || length==0) { // nothing to read
|
||||
|
@ -395,3 +411,19 @@ bool rtc_ds1307_write_ram(uint8_t* data, uint8_t start, uint8_t length)
|
|||
const uint8_t address[] = {0x08+start}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write RAM (starting at 0x08)
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_rom(uint16_t start, uint8_t* data, uint8_t length)
|
||||
{
|
||||
// sanity checks
|
||||
if (data==NULL || length==0) { // nothing to read
|
||||
return false;
|
||||
}
|
||||
if (start>RTC_DS1307_EEPROM_SIZE-1 || start+length>RTC_DS1307_EEPROM_SIZE) { // out of bounds ROM
|
||||
return false;
|
||||
}
|
||||
if (length>32) { // can't write more than 32 bytes pages
|
||||
return false;
|
||||
}
|
||||
const uint8_t address[] = {start>>8, start}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_EEPROM_ADDR, address, LENGTH(address), data, LENGTH(data)); // write ROM
|
||||
}
|
||||
|
|
|
@ -65,12 +65,20 @@ uint8_t rtc_ds1307_read_year(void);
|
|||
*/
|
||||
uint8_t* rtc_ds1307_read_time(void);
|
||||
/** read user RAM from RTC IC
|
||||
* @param[out] data array to store the RAM read
|
||||
* @param[in] start start of the user RAM to read (0-55)
|
||||
* @param[out] data array to store the RAM read
|
||||
* @param[in] length number of user RAM bytes to read (0-55)
|
||||
* @return if read succeeded
|
||||
*/
|
||||
bool rtc_ds1307_read_ram(uint8_t* data, uint8_t start, uint8_t length);
|
||||
bool rtc_ds1307_read_ram(uint8_t start, uint8_t* data, uint8_t length);
|
||||
/** read ROM from AT24Cxx EEPROM
|
||||
* @note only applies if an AT24Cxx EEPROM is also on the I2C bus, such as on the tiny RTC module
|
||||
* @param[in] start start address of the ROM to read (maximum depends on the EEPROM)
|
||||
* @param[out] data array to store the ROM read
|
||||
* @param[in] length number of bytes to read (maximum depends on the EEPROM)
|
||||
* @return if read succeeded
|
||||
*/
|
||||
bool rtc_ds1307_read_rom(uint16_t start, uint8_t* data, uint16_t length);
|
||||
/** disable RTC IC oscillator
|
||||
* @return if disabling oscillator succeeded
|
||||
*/
|
||||
|
@ -130,11 +138,18 @@ bool rtc_ds1307_write_year(uint8_t year);
|
|||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint8_t year);
|
||||
/** write user RAM from RTC IC
|
||||
* @param[in] data array of byte to write in RAM
|
||||
/** write to user RAM on RTC IC
|
||||
* @param[in] start start of the user RAM to write (0-55)
|
||||
* @param[in] data array of byte to write in RAM
|
||||
* @param[in] length number of user RAM bytes to write (0-55)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_ram(uint8_t* data, uint8_t start, uint8_t length);
|
||||
|
||||
bool rtc_ds1307_write_ram(uint8_t start, uint8_t* data, uint8_t length);
|
||||
/** write to ROM on AT24Cxx EEPROM
|
||||
* @note only applies if an AT24Cxx EEPROM is also on the I2C bus, such as on the tiny RTC module
|
||||
* @param[in] start start address of the ROM to write (maximum depends on the EEPROM)
|
||||
* @param[in] data array to write in ROM
|
||||
* @param[in] length number of bytes to write (maximum depends on the EEPROM)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_rom(uint16_t start, uint8_t* data, uint8_t length);
|
||||
|
|
Loading…
Reference in New Issue