42#define CHIP_NAME "Maxim Integrated DS1302"
43#define MANUFACTURER_NAME "Maxim Integrated"
44#define SUPPLY_VOLTAGE_MIN 2.0f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 1.28f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
54#define DS1302_REG_SECOND (0 << 1)
55#define DS1302_REG_MINUTE (1 << 1)
56#define DS1302_REG_HOUR (2 << 1)
57#define DS1302_REG_DATE (3 << 1)
58#define DS1302_REG_MONTH (4 << 1)
59#define DS1302_REG_WEEK (5 << 1)
60#define DS1302_REG_YEAR (6 << 1)
61#define DS1302_REG_CONTROL (7 << 1)
62#define DS1302_REG_CHARGE (8 << 1)
67#define DS1302_COMMAND_RTC (0 << 6)
68#define DS1302_COMMAND_RAM (1 << 6)
69#define DS1302_COMMAND_BURST (0x1F << 1)
81static uint8_t a_ds1302_write(
ds1302_handle_t *handle, uint8_t reg, uint8_t data)
88 prev = (1 << 7) | reg;
95 for (i = 0; i < 8; i++)
97 if ((temp & 0x01) != 0)
128 for (i = 0; i < 8; i++)
130 if ((temp & 0x01) != 0)
184static uint8_t a_ds1302_read(
ds1302_handle_t *handle, uint8_t reg, uint8_t *data)
191 prev = (1 << 7) | reg;
198 for (i = 0; i < 8; i++)
200 if ((temp & 0x01) != 0)
231 for (i = 0; i < 8; i++)
282static uint8_t a_ds1302_multiple_write(
ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
287 for (i = 0; i < len; i++)
289 res = a_ds1302_write(handle, (uint8_t)(reg + (i << 1)), buf[i]);
310static uint8_t a_ds1302_multiple_read(
ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
315 for (i = 0; i < len; i++)
317 res = a_ds1302_read(handle, (uint8_t)(reg + (i << 1) | 0x01), &buf[i]);
338static uint8_t a_ds1302_burst_write(
ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
346 prev = (1 << 7) | reg;
353 for (i = 0; i < 8; i++)
355 if ((temp & 0x01) != 0)
385 for (j = 0; j < len; j++)
388 for (i = 0; i < 8; i++)
390 if ((temp & 0x01) != 0)
446static uint8_t a_ds1302_burst_read(
ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
454 prev = (1 << 7) | reg | 0x01;
461 for (i = 0; i < 8; i++)
463 if ((temp & 0x01) != 0)
493 for (j = 0; j < len; j++)
496 for (i = 0; i < 8; i++)
543static uint8_t a_ds1302_hex2bcd(uint8_t val)
560static uint8_t a_ds1302_bcd2hex(uint8_t val)
565 val = (val >> 4) & 0x0F;
606 if ((t->
year < 2000) || (t->
year > 2100))
608 handle->
debug_print(
"ds1302: year can't be over 2100 or less than 2000.\n");
614 handle->
debug_print(
"ds1302: month can't be zero or over than 12.\n");
618 if ((t->
week == 0) || (t->
week > 7))
620 handle->
debug_print(
"ds1302: week can't be zero or over than 7.\n");
624 if ((t->
date == 0) || (t->
date > 31))
626 handle->
debug_print(
"ds1302: date can't be zero or over than 31.\n");
630 if ((t->
hour < 1) || (t->
hour > 12))
632 handle->
debug_print(
"ds1302: hour can't be over than 12 or less 1.\n");
638 handle->
debug_print(
"ds1302: minute can't be over than 59.\n");
644 handle->
debug_print(
"ds1302: second can't be over than 59.\n");
651 if ((t->
year < 2000) || (t->
year > 2100))
653 handle->
debug_print(
"ds1302: year can't be over 2100 or less than 2000.\n");
659 handle->
debug_print(
"ds1302: month can't be zero or over than 12.\n");
663 if ((t->
week == 0) || (t->
week > 7))
665 handle->
debug_print(
"ds1302: week can't be zero or over than 7.\n");
669 if ((t->
date == 0) || (t->
date > 31))
671 handle->
debug_print(
"ds1302: date can't be zero or over than 31.\n");
677 handle->
debug_print(
"ds1302: hour can't be over than 23.\n");
683 handle->
debug_print(
"ds1302: minute can't be over than 59.\n");
689 handle->
debug_print(
"ds1302: second can't be over than 59.\n");
696 handle->
debug_print(
"ds1302: format is invalid.\n");
704 handle->
debug_print(
"ds1302: read second failed.\n");
709 a_ds1302_hex2bcd(t->
second) | reg & (1 << 7));
712 handle->
debug_print(
"ds1302: write second failed.\n");
719 handle->
debug_print(
"ds1302: write minute failed.\n");
725 reg = (uint8_t)((1 << 7) | (t->
am_pm << 5) | a_ds1302_hex2bcd(t->
hour));
729 reg = (0 << 7) | a_ds1302_hex2bcd(t->
hour);
734 handle->
debug_print(
"ds1302: write hour failed.\n");
741 handle->
debug_print(
"ds1302: write week failed.\n");
748 handle->
debug_print(
"ds1302: write date failed.\n");
755 handle->
debug_print(
"ds1302: write century and month failed.\n");
759 year = t->
year - 2000;
763 handle->
debug_print(
"ds1302: write year failed.\n");
802 memset(buf, 0,
sizeof(uint8_t) * 7);
807 handle->
debug_print(
"ds1302: multiple read failed.\n");
811 t->
year = a_ds1302_bcd2hex(buf[6]) + 2000;
812 t->
month = a_ds1302_bcd2hex(buf[4] & 0x1F);
813 t->
week = a_ds1302_bcd2hex(buf[5] & 0x7);
814 t->
date = a_ds1302_bcd2hex(buf[3] & 0x3F);
819 t->
hour = a_ds1302_bcd2hex(buf[2] & 0x1F);
823 t->
hour = a_ds1302_bcd2hex(buf[2] & 0x3F);
825 t->
minute = a_ds1302_bcd2hex(buf[1]);
826 t->
second = a_ds1302_bcd2hex(buf[0] & (~(1 << 7)));
860 handle->
debug_print(
"ds1302: read second failed.\n");
865 prev |= (!enable) << 7;
870 handle->
debug_print(
"ds1302: write second failed.\n");
904 (uint8_t *)&prev, 1);
907 handle->
debug_print(
"ds1302: read second failed.\n");
945 handle->
debug_print(
"ds1302: read control failed.\n");
954 handle->
debug_print(
"ds1302: write control failed.\n");
991 handle->
debug_print(
"ds1302: read control failed.\n");
1027 handle->
debug_print(
"ds1302: write charge failed.\n");
1062 handle->
debug_print(
"ds1302: read charge failed.\n");
1103 if (addr + len - 1 > 30)
1114 handle->
debug_print(
"ds1302: read ram failed.\n");
1155 if (addr + len - 1 > 30)
1166 handle->
debug_print(
"ds1302: write ram failed.\n");
1196 handle->
debug_print(
"ds1302: ce_gpio_init is null.\n");
1202 handle->
debug_print(
"ds1302: ce_gpio_deinit is null.\n");
1208 handle->
debug_print(
"ds1302: ce_gpio_write is null.\n");
1214 handle->
debug_print(
"ds1302: sclk_gpio_init is null.\n");
1220 handle->
debug_print(
"ds1302: sclk_gpio_deinit is null.\n");
1226 handle->
debug_print(
"ds1302: sclk_gpio_write is null.\n");
1232 handle->
debug_print(
"ds1302: io_gpio_init is null.\n");
1238 handle->
debug_print(
"ds1302: io_gpio_deinit is null.\n");
1244 handle->
debug_print(
"ds1302: io_gpio_write is null.\n");
1250 handle->
debug_print(
"ds1302: io_gpio_read is null.\n");
1256 handle->
debug_print(
"ds1302: delay_ms is null.\n");
1263 handle->
debug_print(
"ds1302: ce gpio init failed.\n");
1269 handle->
debug_print(
"ds1302: sclk gpio init failed.\n");
1276 handle->
debug_print(
"ds1302: io gpio init failed.\n");
1310 handle->
debug_print(
"ds1302: ce gpio deinit failed.\n");
1316 handle->
debug_print(
"ds1302: sclk gpio deinit failed.\n");
1322 handle->
debug_print(
"ds1302: io gpio deinit failed.\n");
1507 if (a_ds1302_multiple_write(handle, reg, buf, len) != 0)
1539 if (a_ds1302_multiple_read(handle, reg, buf, len) != 0)
#define DS1302_COMMAND_BURST
#define DS1302_REG_SECOND
chip register definition
#define DS1302_REG_CONTROL
#define DS1302_COMMAND_RAM
#define DS1302_COMMAND_RTC
chip command definition
#define SUPPLY_VOLTAGE_MAX
#define DS1302_REG_CHARGE
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DS1302_REG_MINUTE
driver ds1302 header file
uint8_t ds1302_clock_burst_read(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
clock burst read
uint8_t ds1302_ram_burst_write(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
ram burst write
uint8_t ds1302_clock_burst_write(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
clock burst write
uint8_t ds1302_ram_burst_read(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
ram burst read
uint8_t ds1302_init(ds1302_handle_t *handle)
initialize the chip
uint8_t ds1302_set_write_protect(ds1302_handle_t *handle, ds1302_bool_t enable)
enable or disable write protect
struct ds1302_time_s ds1302_time_t
ds1302 time structure definition
ds1302_bool_t
ds1302 bool enumeration definition
uint8_t ds1302_deinit(ds1302_handle_t *handle)
close the chip
ds1302_format_t
ds1302 format enumeration definition
struct ds1302_info_s ds1302_info_t
ds1302 information structure definition
uint8_t ds1302_set_oscillator(ds1302_handle_t *handle, ds1302_bool_t enable)
enable or disable the oscillator
uint8_t ds1302_get_write_protect(ds1302_handle_t *handle, ds1302_bool_t *enable)
get write protect status
ds1302_am_pm_t
ds1302 am pm enumeration definition
uint8_t ds1302_get_time(ds1302_handle_t *handle, ds1302_time_t *t)
get the current time
uint8_t ds1302_info(ds1302_info_t *info)
get chip's information
uint8_t ds1302_read_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
read ram
uint8_t ds1302_get_oscillator(ds1302_handle_t *handle, ds1302_bool_t *enable)
get the chip oscillator status
struct ds1302_handle_s ds1302_handle_t
ds1302 handle structure definition
uint8_t ds1302_write_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
write ram
uint8_t ds1302_set_charge(ds1302_handle_t *handle, uint8_t charge)
set charge
uint8_t ds1302_set_time(ds1302_handle_t *handle, ds1302_time_t *t)
set the current time
uint8_t ds1302_get_charge(ds1302_handle_t *handle, uint8_t *charge)
get charge
uint8_t ds1302_set_reg(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
set the chip register
uint8_t ds1302_get_reg(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
get the chip register
uint8_t(* ce_gpio_deinit)(void)
uint8_t(* io_gpio_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* sclk_gpio_write)(uint8_t value)
uint8_t(* sclk_gpio_deinit)(void)
uint8_t(* io_gpio_write)(uint8_t value)
void(* debug_print)(const char *const fmt,...)
void(* delay_us)(uint32_t us)
uint8_t(* sclk_gpio_init)(void)
uint8_t(* ce_gpio_init)(void)
uint8_t(* ce_gpio_write)(uint8_t value)
uint8_t(* io_gpio_deinit)(void)
uint8_t(* io_gpio_read)(uint8_t *value)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v