42#define CHIP_NAME "Maxim Integrated DS1307"
43#define MANUFACTURER_NAME "Maxim Integrated"
44#define SUPPLY_VOLTAGE_MIN 4.5f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 1.50f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
54#define DS1307_REG_SECOND 0x00
55#define DS1307_REG_MINUTE 0x01
56#define DS1307_REG_HOUR 0x02
57#define DS1307_REG_WEEK 0x03
58#define DS1307_REG_DATE 0x04
59#define DS1307_REG_MONTH 0x05
60#define DS1307_REG_YEAR 0x06
61#define DS1307_REG_CONTROL 0x07
62#define DS1307_REG_RAM 0x08
67#define DS1307_ADDRESS 0xD0
79static uint8_t a_ds1307_iic_write(
ds1307_handle_t *handle, uint8_t reg, uint8_t data)
102static uint8_t a_ds1307_iic_multiple_write(
ds1307_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
125static uint8_t a_ds1307_iic_multiple_read(
ds1307_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
143static uint8_t a_ds1307_hex2bcd(uint8_t val)
160static uint8_t a_ds1307_bcd2hex(uint8_t val)
165 val = (val >> 4) & 0x0F;
206 if ((t->
year < 2000) || (t->
year > 2100))
208 handle->
debug_print(
"ds1307: year can't be over 2100 or less than 2000.\n");
214 handle->
debug_print(
"ds1307: month can't be zero or over than 12.\n");
218 if ((t->
week == 0) || (t->
week > 7))
220 handle->
debug_print(
"ds1307: week can't be zero or over than 7.\n");
224 if ((t->
date == 0) || (t->
date > 31))
226 handle->
debug_print(
"ds1307: date can't be zero or over than 31.\n");
230 if ((t->
hour < 1) || (t->
hour > 12))
232 handle->
debug_print(
"ds1307: hour can't be over than 12 or less 1.\n");
238 handle->
debug_print(
"ds1307: minute can't be over than 59.\n");
244 handle->
debug_print(
"ds1307: second can't be over than 59.\n");
251 if ((t->
year < 2000) || (t->
year > 2100))
253 handle->
debug_print(
"ds1307: year can't be over 2100 or less than 2000.\n");
259 handle->
debug_print(
"ds1307: month can't be zero or over than 12.\n");
263 if ((t->
week == 0) || (t->
week > 7))
265 handle->
debug_print(
"ds1307: week can't be zero or over than 7.\n");
269 if ((t->
date == 0) || (t->
date > 31))
271 handle->
debug_print(
"ds1307: date can't be zero or over than 31.\n");
277 handle->
debug_print(
"ds1307: hour can't be over than 23.\n");
283 handle->
debug_print(
"ds1307: minute can't be over than 59.\n");
289 handle->
debug_print(
"ds1307: second can't be over than 59.\n");
296 handle->
debug_print(
"ds1307: format is invalid.\n");
304 handle->
debug_print(
"ds1307: read second failed.\n");
311 handle->
debug_print(
"ds1307: write second failed.\n");
318 handle->
debug_print(
"ds1307: write minute failed.\n");
324 reg = (uint8_t)((1 << 6) | (t->
am_pm << 5) | a_ds1307_hex2bcd(t->
hour));
328 reg = (0 << 6) | a_ds1307_hex2bcd(t->
hour);
333 handle->
debug_print(
"ds1307: write hour failed.\n");
340 handle->
debug_print(
"ds1307: write week failed.\n");
347 handle->
debug_print(
"ds1307: write date failed.\n");
354 handle->
debug_print(
"ds1307: write century and month failed.\n");
358 year = t->
year - 2000;
359 res = a_ds1307_iic_write(handle,
DS1307_REG_YEAR, a_ds1307_hex2bcd((uint8_t)year));
362 handle->
debug_print(
"ds1307: write year failed.\n");
401 memset(buf, 0,
sizeof(uint8_t) * 7);
405 handle->
debug_print(
"ds1307: multiple read failed.\n");
409 t->
year = a_ds1307_bcd2hex(buf[6]) + 2000;
410 t->
month = a_ds1307_bcd2hex(buf[5] & 0x1F);
411 t->
week = a_ds1307_bcd2hex(buf[3] & 0x7);
412 t->
date = a_ds1307_bcd2hex(buf[4] & 0x3F);
417 t->
hour = a_ds1307_bcd2hex(buf[2] & 0x1F);
421 t->
hour = a_ds1307_bcd2hex(buf[2] & 0x3F);
423 t->
minute = a_ds1307_bcd2hex(buf[1]);
424 t->
second = a_ds1307_bcd2hex(buf[0] & (~(1 << 7)));
457 handle->
debug_print(
"ds1307: read second failed.\n");
462 prev |= (!enable) << 7;
466 handle->
debug_print(
"ds1307: write second failed.\n");
499 res = a_ds1307_iic_multiple_read(handle,
DS1307_REG_SECOND, (uint8_t *)&prev, 1);
502 handle->
debug_print(
"ds1307: read second failed.\n");
539 handle->
debug_print(
"ds1307: read control failed.\n");
548 handle->
debug_print(
"ds1307: write control failed.\n");
584 handle->
debug_print(
"ds1307: read control failed.\n");
621 handle->
debug_print(
"ds1307: read control failed.\n");
630 handle->
debug_print(
"ds1307: write control failed.\n");
666 handle->
debug_print(
"ds1307: read control failed.\n");
703 handle->
debug_print(
"ds1307: read control failed.\n");
712 handle->
debug_print(
"ds1307: write control failed.\n");
748 handle->
debug_print(
"ds1307: read control failed.\n");
790 if (addr + len - 1 > 55)
797 res = a_ds1307_iic_multiple_read(handle, (uint8_t)(
DS1307_REG_RAM + addr),
842 if (addr + len - 1 > 55)
849 res = a_ds1307_iic_multiple_write(handle, (uint8_t)(
DS1307_REG_RAM + addr),
853 handle->
debug_print(
"ds1307: write ram failed.\n");
883 handle->
debug_print(
"ds1307: iic_init is null.\n");
889 handle->
debug_print(
"ds1307: iic_deinit is null.\n");
895 handle->
debug_print(
"ds1307: iic_write is null.\n");
901 handle->
debug_print(
"ds1307: iic_read is null.\n");
907 handle->
debug_print(
"ds1307: delay_ms is null.\n");
946 handle->
debug_print(
"ds1307: iic deinit failed.\n");
#define DS1307_ADDRESS
chip address definition
#define SUPPLY_VOLTAGE_MAX
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define DS1307_REG_CONTROL
#define CHIP_NAME
chip information definition
#define DS1307_REG_MINUTE
#define DS1307_REG_SECOND
chip register definition
driver ds1307 header file
uint8_t ds1307_info(ds1307_info_t *info)
get chip's information
ds1307_am_pm_t
ds1307 am pm enumeration definition
struct ds1307_info_s ds1307_info_t
ds1307 information structure definition
uint8_t ds1307_get_output_level(ds1307_handle_t *handle, ds1307_output_level_t *level)
get the output level
struct ds1307_handle_s ds1307_handle_t
ds1307 handle structure definition
ds1307_square_wave_frequency_t
ds1307 square wave frequency enumeration definition
uint8_t ds1307_get_square_wave_frequency(ds1307_handle_t *handle, ds1307_square_wave_frequency_t *freq)
get the square wave frequency
ds1307_format_t
ds1307 format enumeration definition
uint8_t ds1307_read_ram(ds1307_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
read ram
uint8_t ds1307_get_oscillator(ds1307_handle_t *handle, ds1307_bool_t *enable)
get the chip oscillator status
uint8_t ds1307_set_output_mode(ds1307_handle_t *handle, ds1307_output_mode_t mode)
set the output mode
struct ds1307_time_s ds1307_time_t
ds1307 time structure definition
uint8_t ds1307_get_output_mode(ds1307_handle_t *handle, ds1307_output_mode_t *mode)
get the output mode
uint8_t ds1307_set_oscillator(ds1307_handle_t *handle, ds1307_bool_t enable)
enable or disable the oscillator
uint8_t ds1307_init(ds1307_handle_t *handle)
initialize the chip
uint8_t ds1307_set_time(ds1307_handle_t *handle, ds1307_time_t *t)
set the current time
ds1307_output_level_t
ds1307 output level enumeration definition
uint8_t ds1307_write_ram(ds1307_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
write ram
uint8_t ds1307_set_output_level(ds1307_handle_t *handle, ds1307_output_level_t level)
set the output level
uint8_t ds1307_set_square_wave_frequency(ds1307_handle_t *handle, ds1307_square_wave_frequency_t freq)
set the square wave frequency
ds1307_bool_t
ds1307 bool enumeration definition
uint8_t ds1307_deinit(ds1307_handle_t *handle)
close the chip
uint8_t ds1307_get_time(ds1307_handle_t *handle, ds1307_time_t *t)
get the current time
ds1307_output_mode_t
ds1307 output mode enumeration definition
uint8_t ds1307_get_reg(ds1307_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t ds1307_set_reg(ds1307_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
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