esp-open-rtos/extras/ds1307/ds1307.c

153 lines
3.7 KiB
C
Raw Normal View History

2016-10-09 09:14:17 +00:00
/*
* Driver for DS1307 RTC
*
* Part of esp-open-rtos
* Copyright (C) 2016 Ruslan V. Uss <unclerus@gmail.com>
* BSD Licensed as described in the file LICENSE
*/
#include "ds1307.h"
#include <stdio.h>
#define RAM_SIZE 56
#define TIME_REG 0
#define CONTROL_REG 7
#define RAM_REG 8
#define CH_BIT (1 << 7)
#define HOUR12_BIT (1 << 6)
#define PM_BIT (1 << 5)
#define SQWE_BIT (1 << 4)
#define OUT_BIT (1 << 7)
#define CH_MASK 0x7f
#define SECONDS_MASK 0x7f
#define HOUR12_MASK 0x1f
#define HOUR24_MASK 0x3f
#define SQWEF_MASK 0x03
#define SQWE_MASK 0xef
#define OUT_MASK 0x7f
static uint8_t bcd2dec(uint8_t val)
{
return (val >> 4) * 10 + (val & 0x0f);
}
static uint8_t dec2bcd(uint8_t val)
{
return ((val / 10) << 4) + (val % 10);
}
2017-10-18 19:25:48 +00:00
static uint8_t read_register(i2c_dev_t *dev, uint8_t reg)
2016-10-09 09:14:17 +00:00
{
uint8_t val;
2017-09-01 09:29:32 +00:00
i2c_slave_read(dev->bus, dev->addr, &reg, &val, 1);
2016-10-09 09:14:17 +00:00
return val;
}
2017-10-18 19:25:48 +00:00
static void update_register(i2c_dev_t *dev, uint8_t reg, uint8_t mask, uint8_t val)
2016-10-09 09:14:17 +00:00
{
2017-10-18 19:25:48 +00:00
uint8_t buf = (read_register(dev, reg) & mask) | val;
2016-10-09 09:14:17 +00:00
2017-09-01 09:29:32 +00:00
i2c_slave_write(dev->bus, dev->addr, &reg, &buf, 1);
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
void ds1307_start(i2c_dev_t *dev, bool start)
2016-10-09 09:14:17 +00:00
{
2017-09-01 09:29:32 +00:00
update_register(dev, TIME_REG, CH_MASK, start ? 0 : CH_BIT);
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
bool ds1307_is_running(i2c_dev_t *dev)
2016-10-09 09:14:17 +00:00
{
2017-09-01 09:29:32 +00:00
return !(read_register(dev, TIME_REG) & CH_BIT);
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
void ds1307_get_time(i2c_dev_t *dev, struct tm *time)
2016-10-09 09:14:17 +00:00
{
uint8_t buf[7];
2017-10-18 19:25:48 +00:00
uint8_t reg = TIME_REG;
2016-10-09 09:14:17 +00:00
2017-10-18 19:25:48 +00:00
i2c_slave_read(dev->bus, dev->addr, &reg, buf, 7);
2016-10-09 09:14:17 +00:00
time->tm_sec = bcd2dec(buf[0] & SECONDS_MASK);
time->tm_min = bcd2dec(buf[1]);
if (buf[2] & HOUR12_BIT)
{
// RTC in 12-hour mode
2016-11-01 09:40:19 +00:00
time->tm_hour = bcd2dec(buf[2] & HOUR12_MASK) - 1;
2016-10-09 09:14:17 +00:00
if (buf[2] & PM_BIT)
time->tm_hour += 12;
}
2017-10-18 19:25:48 +00:00
else
time->tm_hour = bcd2dec(buf[2] & HOUR24_MASK);
2016-10-09 09:14:17 +00:00
time->tm_wday = bcd2dec(buf[3]) - 1;
time->tm_mday = bcd2dec(buf[4]);
2017-10-18 19:25:48 +00:00
time->tm_mon = bcd2dec(buf[5]) - 1;
2016-10-09 09:14:17 +00:00
time->tm_year = bcd2dec(buf[6]) + 2000;
}
2017-10-18 19:25:48 +00:00
void ds1307_set_time(i2c_dev_t *dev, const struct tm *time)
2016-10-09 09:14:17 +00:00
{
uint8_t buf[8];
buf[0] = TIME_REG;
buf[1] = dec2bcd(time->tm_sec);
buf[2] = dec2bcd(time->tm_min);
buf[3] = dec2bcd(time->tm_hour);
2016-11-05 10:12:47 +00:00
buf[4] = dec2bcd(time->tm_wday + 1);
2016-10-09 09:14:17 +00:00
buf[5] = dec2bcd(time->tm_mday);
2016-11-05 10:12:47 +00:00
buf[6] = dec2bcd(time->tm_mon + 1);
2016-10-09 09:14:17 +00:00
buf[7] = dec2bcd(time->tm_year - 2000);
2017-10-18 19:25:48 +00:00
i2c_slave_write(dev->bus, dev->addr, &buf[0], &buf[1], 7);
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
void ds1307_enable_squarewave(i2c_dev_t *dev, bool enable)
2016-10-09 09:14:17 +00:00
{
2017-09-01 09:29:32 +00:00
update_register(dev, CONTROL_REG, SQWE_MASK, enable ? SQWE_BIT : 0);
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
bool ds1307_is_squarewave_enabled(i2c_dev_t *dev)
2016-10-09 09:14:17 +00:00
{
2017-09-01 09:29:32 +00:00
return read_register(dev, CONTROL_REG) & SQWE_BIT;
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
void ds1307_set_squarewave_freq(i2c_dev_t *dev, ds1307_squarewave_freq_t freq)
2016-10-09 09:14:17 +00:00
{
2017-09-01 09:29:32 +00:00
update_register(dev, CONTROL_REG, SQWEF_MASK, (uint8_t)freq);
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
ds1307_squarewave_freq_t ds1307_get_squarewave_freq(i2c_dev_t *dev)
2016-10-09 09:14:17 +00:00
{
2017-09-01 09:29:32 +00:00
return (ds1307_squarewave_freq_t)(read_register(dev, CONTROL_REG) & SQWEF_MASK);
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
bool ds1307_get_output(i2c_dev_t *dev)
2016-10-09 09:14:17 +00:00
{
2017-09-01 09:29:32 +00:00
return read_register(dev, CONTROL_REG) & OUT_BIT;
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
void ds1307_set_output(i2c_dev_t *dev, bool value)
2016-10-09 09:14:17 +00:00
{
2017-09-01 09:29:32 +00:00
update_register(dev, CONTROL_REG, OUT_MASK, value ? OUT_BIT : 0);
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
int ds1307_read_ram(i2c_dev_t *dev, uint8_t offset, uint8_t *buf, uint8_t len)
2016-10-09 09:14:17 +00:00
{
2017-10-18 19:25:48 +00:00
if (offset + len > RAM_SIZE)
return -EINVAL;
uint8_t reg = RAM_REG + offset;
2016-10-09 09:14:17 +00:00
2017-09-01 09:29:32 +00:00
return i2c_slave_read(dev->bus, dev->addr, &reg, buf, len);
2016-10-09 09:14:17 +00:00
}
2017-10-18 19:25:48 +00:00
int ds1307_write_ram(i2c_dev_t *dev, uint8_t offset, uint8_t *buf, uint8_t len)
2016-10-09 09:14:17 +00:00
{
2017-10-18 19:25:48 +00:00
if (offset + len > RAM_SIZE)
return -EINVAL;
uint8_t reg = RAM_REG + offset;
2016-10-09 09:14:17 +00:00
2017-09-01 09:29:32 +00:00
return i2c_slave_write(dev->bus, dev->addr, &reg, buf, len);
2016-10-09 09:14:17 +00:00
}