43#define CHIP_NAME "Maxim Integrated DS3231"
44#define MANUFACTURER_NAME "Maxim Integrated"
45#define SUPPLY_VOLTAGE_MIN 2.3f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 0.65f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
55#define DS3231_REG_SECOND 0x00
56#define DS3231_REG_MINUTE 0x01
57#define DS3231_REG_HOUR 0x02
58#define DS3231_REG_WEEK 0x03
59#define DS3231_REG_DATE 0x04
60#define DS3231_REG_MONTH 0x05
61#define DS3231_REG_YEAR 0x06
62#define DS3231_REG_ALARM1_SECOND 0x07
63#define DS3231_REG_ALARM1_MINUTE 0x08
64#define DS3231_REG_ALARM1_HOUR 0x09
65#define DS3231_REG_ALARM1_WEEK 0x0A
66#define DS3231_REG_ALARM2_MINUTE 0x0B
67#define DS3231_REG_ALARM2_HOUR 0x0C
68#define DS3231_REG_ALARM2_WEEK 0x0D
69#define DS3231_REG_CONTROL 0x0E
70#define DS3231_REG_STATUS 0x0F
71#define DS3231_REG_XTAL 0x10
72#define DS3231_REG_TEMPERATUREH 0x11
73#define DS3231_REG_TEMPERATUREL 0x12
78#define DS3231_ADDRESS 0xD0
90static uint8_t a_ds3231_iic_write(
ds3231_handle_t *handle, uint8_t reg, uint8_t data)
113static uint8_t a_ds3231_iic_multiple_read(
ds3231_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
131static uint8_t a_ds3231_hex2bcd(uint8_t val)
148static uint8_t a_ds3231_bcd2hex(uint8_t val)
153 val = (val >> 4) & 0x0F;
195 if ((t->
year < 2000) || (t->
year > 2200))
197 handle->
debug_print(
"ds3231: year can't be over 2200 or less than 2000.\n");
203 handle->
debug_print(
"ds3231: month can't be zero or over than 12.\n");
207 if ((t->
week == 0) || (t->
week > 7))
209 handle->
debug_print(
"ds3231: week can't be zero or over than 7.\n");
213 if ((t->
date == 0) || (t->
date > 31))
215 handle->
debug_print(
"ds3231: date can't be zero or over than 31.\n");
219 if ((t->
hour < 1) || (t->
hour > 12))
221 handle->
debug_print(
"ds3231: hour can't be over than 12 or less 1.\n");
227 handle->
debug_print(
"ds3231: minute can't be over than 59.\n");
233 handle->
debug_print(
"ds3231: second can't be over than 59.\n");
240 if ((t->
year < 2000) || (t->
year > 2200))
242 handle->
debug_print(
"ds3231: year can't be over 2200 or less than 2000.\n");
248 handle->
debug_print(
"ds3231: month can't be zero or over than 12.\n");
252 if ((t->
week == 0) || (t->
week > 7))
254 handle->
debug_print(
"ds3231: week can't be zero or over than 7.\n");
258 if ((t->
date == 0) || (t->
date > 31))
260 handle->
debug_print(
"ds3231: date can't be zero or over than 31.\n");
266 handle->
debug_print(
"ds3231: hour can't be over than 23.\n");
272 handle->
debug_print(
"ds3231: minute can't be over than 59.\n");
278 handle->
debug_print(
"ds3231: second can't be over than 59.\n");
285 handle->
debug_print(
"ds3231: format is invalid.\n");
293 handle->
debug_print(
"ds3231: write second failed.\n");
300 handle->
debug_print(
"ds3231: write minute failed.\n");
306 reg = (uint8_t)((1 << 6) | (t->
am_pm << 5) | a_ds3231_hex2bcd(t->
hour));
310 reg = (0 << 6) | a_ds3231_hex2bcd(t->
hour);
315 handle->
debug_print(
"ds3231: write hour failed.\n");
322 handle->
debug_print(
"ds3231: write week failed.\n");
329 handle->
debug_print(
"ds3231: write date failed.\n");
333 year = t->
year - 2000;
346 handle->
debug_print(
"ds3231: write century and month failed.\n");
350 res = a_ds3231_iic_write(handle,
DS3231_REG_YEAR, a_ds3231_hex2bcd((uint8_t)year));
353 handle->
debug_print(
"ds3231: write year failed.\n");
392 memset(buf, 0,
sizeof(uint8_t) * 7);
396 handle->
debug_print(
"ds3231: multiple read failed.\n");
400 t->
year = a_ds3231_bcd2hex(buf[6]) + 2000 + ((buf[5] >> 7) & 0x01) * 100;
401 t->
month = a_ds3231_bcd2hex(buf[5]&0x1F);
402 t->
week = a_ds3231_bcd2hex(buf[3]);
403 t->
date = a_ds3231_bcd2hex(buf[4]);
408 t->
hour = a_ds3231_bcd2hex(buf[2] & 0x1F);
412 t->
hour = a_ds3231_bcd2hex(buf[2] & 0x3F);
414 t->
minute = a_ds3231_bcd2hex(buf[1]);
415 t->
second = a_ds3231_bcd2hex(buf[0]);
453 if ((t->
week == 0) || (t->
week > 7))
455 handle->
debug_print(
"ds3231: week can't be zero or over than 7.\n");
459 if ((t->
date == 0) || (t->
date > 31))
461 handle->
debug_print(
"ds3231: date can't be zero or over than 31.\n");
465 if ((t->
hour < 1) || (t->
hour > 12))
467 handle->
debug_print(
"ds3231: hour can't be over than 12 or less 1.\n");
473 handle->
debug_print(
"ds3231: minute can't be over than 59.\n");
479 handle->
debug_print(
"ds3231: second can't be over than 59.\n");
486 if ((t->
week == 0) || (t->
week > 7))
488 handle->
debug_print(
"ds3231: week can't be zero or over than 7.\n");
492 if ((t->
date == 0) || (t->
date > 31))
494 handle->
debug_print(
"ds3231: date can't be zero or over than 31.\n");
500 handle->
debug_print(
"ds3231: hour can't be over than 23.\n");
506 handle->
debug_print(
"ds3231: minute can't be over than 59.\n");
512 handle->
debug_print(
"ds3231: second can't be over than 59.\n");
519 handle->
debug_print(
"ds3231: format is invalid.\n");
527 handle->
debug_print(
"ds3231: write alarm1 second failed.\n");
534 handle->
debug_print(
"ds3231: write alarm1 minute failed.\n");
540 reg = (uint8_t)((((mode >> 2) & 0x01) << 7) | (1 << 6) | (t->
am_pm << 5) | a_ds3231_hex2bcd(t->
hour));
544 reg = (((mode >> 2) & 0x01) << 7) | a_ds3231_hex2bcd(t->
hour);
549 handle->
debug_print(
"ds3231: write alarm1 hour failed.\n");
555 reg = (((mode >> 3) & 0x01) << 7) | (1 << 6) | a_ds3231_hex2bcd(t->
week);
559 reg = (((mode >> 3) & 0x01) << 7) | a_ds3231_hex2bcd(t->
date);
564 handle->
debug_print(
"ds3231: write alarm1 week failed.\n");
604 memset(buf, 0,
sizeof(uint8_t) * 4);
608 handle->
debug_print(
"ds3231: multiple read failed.\n");
614 if (((buf[3] >> 6) & 0x01) != 0)
616 t->
week = a_ds3231_bcd2hex(buf[3] & 0x0F);
622 t->
date = a_ds3231_bcd2hex(buf[3] & 0x3F);
628 t->
hour = a_ds3231_bcd2hex(buf[2]&0x1F);
632 t->
hour = a_ds3231_bcd2hex(buf[2]&0x3F);
634 t->
minute = a_ds3231_bcd2hex(buf[1] & 0x7F);
635 t->
second = a_ds3231_bcd2hex(buf[0] & 0x7F);
636 *mode = (
ds3231_alarm1_mode_t)(((buf[0]>>7)&0x01)<<0 | ((buf[1]>>7)&0x01)<<1 | ((buf[2]>>7)&0x01)<<2 | ((buf[3]>>7)&0x01)<<3 |
637 ((buf[3] >> 6)&0x01)<<4
676 if ((t->
week == 0) || (t->
week > 7))
678 handle->
debug_print(
"ds3231: week can't be zero or over than 7.\n");
682 if ((t->
date == 0) || (t->
date > 31))
684 handle->
debug_print(
"ds3231: date can't be zero or over than 31.\n");
688 if ((t->
hour < 1) || (t->
hour > 12))
690 handle->
debug_print(
"ds3231: hour can't be over than 12 or less 1.\n");
696 handle->
debug_print(
"ds3231: minute can't be over than 59.\n");
703 if ((t->
week == 0) || (t->
week > 7))
705 handle->
debug_print(
"ds3231: week can't be zero or over than 7.\n");
709 if ((t->
date == 0) || (t->
date > 31))
711 handle->
debug_print(
"ds3231: date can't be zero or over than 31.\n");
717 handle->
debug_print(
"ds3231: hour can't be over than 23.\n");
723 handle->
debug_print(
"ds3231: minute can't be over than 59.\n");
730 handle->
debug_print(
"ds3231: format is invalid.\n");
738 handle->
debug_print(
"ds3231: write alarm2 minute failed.\n");
744 reg = (uint8_t)((((mode >> 1) & 0x01) << 7) | (1 << 6) | (t->
am_pm << 5) | a_ds3231_hex2bcd(t->
hour));
748 reg = (((mode >> 1) & 0x01) << 7) | a_ds3231_hex2bcd(t->
hour);
753 handle->
debug_print(
"ds3231: write alarm2 hour failed.\n");
759 reg = (((mode >> 2) & 0x01) << 7) | (1 << 6) | a_ds3231_hex2bcd(t->
week);
763 reg = (((mode >> 2) & 0x01) << 7) | a_ds3231_hex2bcd(t->
date);
768 handle->
debug_print(
"ds3231: write alarm2 week failed.\n");
808 memset(buf, 0,
sizeof(uint8_t) * 3);
812 handle->
debug_print(
"ds3231: multiple read failed.\n");
818 if (((buf[2] >> 6) & 0x01) != 0)
820 t->
week = a_ds3231_bcd2hex(buf[2] & 0x0F);
826 t->
date = a_ds3231_bcd2hex(buf[2] & 0x3F);
832 t->
hour = a_ds3231_bcd2hex(buf[1]&0x1F);
836 t->
hour = a_ds3231_bcd2hex(buf[1]&0x3F);
838 t->
minute = a_ds3231_bcd2hex(buf[0] & 0x7F);
840 *mode = (
ds3231_alarm2_mode_t)(((buf[0]>>7)&0x01)<<0 | ((buf[1]>>7)&0x01)<<1 | ((buf[2]>>7)&0x01)<<2 | ((buf[2]>>6)&0x01)<<4);
873 handle->
debug_print(
"ds3231: read control failed.\n");
878 prev |= (!enable) << 7;
882 handle->
debug_print(
"ds3231: write control failed.\n");
918 handle->
debug_print(
"ds3231: read control failed.\n");
956 handle->
debug_print(
"ds3231: read control failed.\n");
960 prev &= ~(1 << alarm);
961 prev |= enable << alarm;
965 handle->
debug_print(
"ds3231: write control failed.\n");
1002 handle->
debug_print(
"ds3231: read control failed.\n");
1039 handle->
debug_print(
"ds3231: read control failed.\n");
1048 handle->
debug_print(
"ds3231: write control failed.\n");
1084 handle->
debug_print(
"ds3231: read control failed.\n");
1121 handle->
debug_print(
"ds3231: read control failed.\n");
1126 prev |= enable << 6;
1130 handle->
debug_print(
"ds3231: write control failed.\n");
1166 handle->
debug_print(
"ds3231: read control failed.\n");
1203 memset(buf, 0,
sizeof(uint8_t) * 2);
1207 handle->
debug_print(
"ds3231: read control failed.\n");
1216 handle->
debug_print(
"ds3231: write control failed.\n");
1224 res = a_ds3231_iic_multiple_read(handle,
DS3231_REG_STATUS, (uint8_t *)&prev, 1);
1227 handle->
debug_print(
"ds3231: read status failed.\n");
1231 if (((prev >> 2) & 0x01) == 0)
1246 handle->
debug_print(
"ds3231: read temperature failed.\n");
1250 *raw = (int16_t)(((uint16_t)buf[0]) << 8) | buf[1];
1252 if (((*raw) & 0x0200) != 0)
1254 *raw = (*raw) | 0xFC00U;
1256 *s = (float)(*raw) * 0.25f;
1285 res = a_ds3231_iic_multiple_read(handle,
DS3231_REG_STATUS, (uint8_t *)status, 1);
1288 handle->
debug_print(
"ds3231: read status failed.\n");
1323 handle->
debug_print(
"ds3231: write offset failed.\n");
1355 res = a_ds3231_iic_multiple_read(handle,
DS3231_REG_XTAL, (uint8_t *)offset, 1);
1358 handle->
debug_print(
"ds3231: read offset failed.\n");
1388 *reg = (int8_t)(offset / 0.12f);
1415 *offset = (float)(reg) * 0.12f;
1443 res = a_ds3231_iic_multiple_read(handle,
DS3231_REG_STATUS, (uint8_t *)&prev, 1);
1446 handle->
debug_print(
"ds3231: read status failed.\n");
1493 handle->
debug_print(
"ds3231: iic_init is null.\n");
1499 handle->
debug_print(
"ds3231: iic_deinit is null.\n");
1505 handle->
debug_print(
"ds3231: iic_write is null.\n");
1511 handle->
debug_print(
"ds3231: iic_read is null.\n");
1517 handle->
debug_print(
"ds3231: delay_ms is null.\n");
1523 handle->
debug_print(
"ds3231: receive_callback is null.\n");
1530 handle->
debug_print(
"ds3231: iic init failed.\n");
1534 res = a_ds3231_iic_multiple_read(handle,
DS3231_REG_STATUS, (uint8_t *)&prev, 1);
1537 handle->
debug_print(
"ds3231: read status failed.\n");
1546 handle->
debug_print(
"ds3231: write status failed.\n");
1579 handle->
debug_print(
"ds3231: iic deinit failed.\n");
1613 res = a_ds3231_iic_multiple_read(handle,
DS3231_REG_STATUS, (uint8_t *)&prev, 1);
1616 handle->
debug_print(
"ds3231: read status failed.\n");
1620 prev &= ~(1 << alarm);
1624 handle->
debug_print(
"ds3231: write status failed.\n");
1657 res = a_ds3231_iic_multiple_read(handle,
DS3231_REG_STATUS, (uint8_t *)&prev, 1);
1660 handle->
debug_print(
"ds3231: read status failed.\n");
1665 prev |= enable << 3;
1669 handle->
debug_print(
"ds3231: write status failed.\n");
1702 res = a_ds3231_iic_multiple_read(handle,
DS3231_REG_STATUS, (uint8_t *)&prev, 1);
1705 handle->
debug_print(
"ds3231: read status failed.\n");
#define DS3231_REG_MINUTE
#define DS3231_REG_CONTROL
#define DS3231_REG_ALARM2_MINUTE
#define DS3231_REG_ALARM2_WEEK
#define SUPPLY_VOLTAGE_MAX
#define DS3231_ADDRESS
chip address definition
#define DS3231_REG_ALARM1_MINUTE
#define DS3231_REG_ALARM2_HOUR
#define DS3231_REG_SECOND
chip register definition
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define DS3231_REG_ALARM1_HOUR
#define DS3231_REG_ALARM1_SECOND
#define DS3231_REG_TEMPERATUREH
#define DS3231_REG_ALARM1_WEEK
#define CHIP_NAME
chip information definition
#define DS3231_REG_STATUS
driver ds3231 header file
uint8_t ds3231_get_32khz_output(ds3231_handle_t *handle, ds3231_bool_t *enable)
get the 32KHz output status
uint8_t ds3231_get_pin(ds3231_handle_t *handle, ds3231_pin_t *pin)
get the chip pin function
uint8_t ds3231_set_aging_offset(ds3231_handle_t *handle, int8_t offset)
set the chip aging offset
uint8_t ds3231_get_temperature(ds3231_handle_t *handle, int16_t *raw, float *s)
get the chip temperature
uint8_t ds3231_set_pin(ds3231_handle_t *handle, ds3231_pin_t pin)
set the chip pin function
uint8_t ds3231_aging_offset_convert_to_data(ds3231_handle_t *handle, int8_t reg, float *offset)
convert a register raw data to a converted aging offset data
uint8_t ds3231_get_aging_offset(ds3231_handle_t *handle, int8_t *offset)
get the chip aging offset
uint8_t ds3231_aging_offset_convert_to_register(ds3231_handle_t *handle, float offset, int8_t *reg)
convert a aging offset value to a register raw data
uint8_t ds3231_set_32khz_output(ds3231_handle_t *handle, ds3231_bool_t enable)
enable or disable the 32KHz output
uint8_t ds3231_set_square_wave(ds3231_handle_t *handle, ds3231_bool_t enable)
enable or disable the square wave output
uint8_t ds3231_get_square_wave(ds3231_handle_t *handle, ds3231_bool_t *enable)
get the square wave output status
uint8_t ds3231_get_alarm_interrupt(ds3231_handle_t *handle, ds3231_alarm_t alarm, ds3231_bool_t *enable)
get the alarm interrupt status
uint8_t ds3231_set_alarm_interrupt(ds3231_handle_t *handle, ds3231_alarm_t alarm, ds3231_bool_t enable)
enable or disable the alarm interrupt
ds3231_alarm2_mode_t
ds3231 alarm2 enumeration definition
uint8_t ds3231_get_alarm1(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm1_mode_t *mode)
get the alarm1 time
ds3231_alarm1_mode_t
ds3231 alarm1 enumeration definition
uint8_t ds3231_set_alarm2(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm2_mode_t mode)
set the alarm2 time
uint8_t ds3231_alarm_clear(ds3231_handle_t *handle, ds3231_alarm_t alarm)
clear the alarm flag
uint8_t ds3231_set_alarm1(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm1_mode_t mode)
set the alarm1 time
uint8_t ds3231_get_alarm2(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm2_mode_t *mode)
get the alarm2 time
@ DS3231_ALARM1_MODE_WEEK_HOUR_MINUTE_SECOND_MATCH
ds3231_am_pm_t
ds3231 am pm enumeration definition
uint8_t ds3231_set_oscillator(ds3231_handle_t *handle, ds3231_bool_t enable)
enable or disable the oscillator
uint8_t ds3231_set_time(ds3231_handle_t *handle, ds3231_time_t *t)
set the current time
uint8_t ds3231_get_oscillator(ds3231_handle_t *handle, ds3231_bool_t *enable)
get the chip oscillator status
struct ds3231_time_s ds3231_time_t
ds3231 time structure definition
ds3231_alarm_t
ds3231 alarm enumeration definition
ds3231_format_t
ds3231 format enumeration definition
ds3231_pin_t
ds3231 pin enumeration definition
uint8_t ds3231_info(ds3231_info_t *info)
get chip's information
uint8_t ds3231_irq_handler(ds3231_handle_t *handle)
irq handler
struct ds3231_handle_s ds3231_handle_t
ds3231 handle structure definition
uint8_t ds3231_init(ds3231_handle_t *handle)
initialize the chip
struct ds3231_info_s ds3231_info_t
ds3231 information structure definition
uint8_t ds3231_get_time(ds3231_handle_t *handle, ds3231_time_t *t)
get the current time
uint8_t ds3231_deinit(ds3231_handle_t *handle)
close the chip
ds3231_bool_t
ds3231 bool enumeration definition
uint8_t ds3231_get_status(ds3231_handle_t *handle, uint8_t *status)
get the chip status
uint8_t ds3231_set_reg(ds3231_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t ds3231_get_reg(ds3231_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
void(* delay_ms)(uint32_t ms)
void(* receive_callback)(uint8_t type)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v