49 #define dprintf(FORMAT, args...) printf(FORMAT, ##args)
60 int I2C(uint8_t* write,uint16_t write_len,uint8_t* read,uint16_t read_len)
65 uint8_t write_res = I2C_MASTER_ERR_NONE;
66 uint8_t read_res = I2C_MASTER_ERR_NONE;
79 return !(write_res == I2C_MASTER_ERR_NONE && read_res == I2C_MASTER_ERR_NONE);
90 static uint8_t state = OFF;
105 memset(time.data, 0, 8);
109 rc =
I2C(time.data, 1, &time.data[1], 7);
115 t->tm_year = time.tm.year + time.tm.dyear * 10 + 100;
117 t->tm_mon = time.tm.mon + time.tm.dmon * 10 -1;
118 t->tm_mday = time.tm.date + time.tm.ddate * 10;
119 t->tm_hour = time.tm.hour + time.tm.dhour * 10;
120 t->tm_min = time.tm.min + time.tm.dmin * 10;
121 t->tm_sec = time.tm.sec + time.tm.dsec * 10;
123 dprintf(
"years %d, months %d, days %d, hours %d, minutes %d, seconds %d\n", t->tm_year, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
141 memset(time.data, 0, 8);
144 time.tm.dyear = (t->tm_year - 100) / 10;
145 time.tm.year = (t->tm_year - 100) % 10;
147 time.tm.dmon = (t->tm_mon + 1) / 10;
148 time.tm.mon = (t->tm_mon + 1) % 10;
149 time.tm.ddate = t->tm_mday / 10;
150 time.tm.date = t->tm_mday % 10;
152 time.tm.dhour = t->tm_hour / 10;
153 time.tm.hour = t->tm_hour % 10;
154 time.tm.dmin = t->tm_min / 10;
155 time.tm.min = t->tm_min % 10;
156 time.tm.dsec = t->tm_sec / 10;
157 time.tm.sec = t->tm_sec % 10;
159 dprintf(
"year %d%d, months %d%d, days %d%d, hours %d%d, minutes %d%d, seconds %d%d\n", time.tm.dyear, time.tm.year, time.tm.dmon, time.tm.mon, time.tm.ddate, time.tm.date, time.tm.dhour, time.tm.hour, time.tm.dmin, time.tm.min, time.tm.dsec, time.tm.sec);
161 rc =
I2C(time.data, 8,
NULL, 0);
184 ds_3231_alarm_t alarm;
187 memset(alarm.data, 0, 5);
190 alarm.tm.address = 7;
191 alarm.tm.ddate = t->tm_mday / 10;
192 alarm.tm.date = t->tm_mday % 10;
193 alarm.tm.dhour = t->tm_hour / 10;
194 alarm.tm.hour = t->tm_hour % 10;
195 alarm.tm.dmin = t->tm_min / 10;
196 alarm.tm.min = t->tm_min % 10;
197 alarm.tm.dsec = t->tm_sec / 10;
198 alarm.tm.sec = t->tm_sec % 10;
201 rc =
I2C(alarm.data, 5,
NULL, 0);
208 rc =
I2C(®s[0], 1, ®s[1], 2);
214 regs[1] |= DS3231_CONTROL_A1IE_SET_MASK;
215 regs[2] &= DS3231_STATUS_A1F_CLEAR_MASK;
238 rc =
I2C(®s[0], 1, ®s[1], 2);
244 regs[1] &= DS3231_CONTROL_A1IE_CLEAR_MASK;
245 regs[2] &= DS3231_STATUS_A1F_CLEAR_MASK;
266 uint8_t addr_set[6] = { 0x11 };
267 uint8_t temp_regs[21];
269 rc =
I2C(addr_set, 1, temp_regs, 2);
274 *temperature = ((temp_regs[0]) & 0x7f) * 100;
275 *temperature += ((temp_regs[1] & 0xc0) >> 6) * 25;
276 if (temp_regs[0] & 0x80)
279 dprintf(
"Temperature is %d\n", *temperature);
292 case DS3231_CONFIG_SET_TIME:
298 case DS3231_CONFIG_SET_ALARM:
300 case DS3231_CONFIG_CLEAR_ALARM:
305 dprintf(
"DS3231 Sensor Configure ...\n");
326 return (state == ON);
Sensors for DS3231 (RTC with Temperature Sensor).
void i2c_master_disable(void)
Disable master I2C module.
void i2c_master_enable(void)
Enable master I2C module.
int I2C(uint8_t *write, uint16_t write_len, uint8_t *read, uint16_t read_len)
DS3231 is connected to TWI Master on Port C (Pins 0 & 1).
int ds3231_set_alarm(struct tm *t)
ds3231_set_alarm
const struct sensors_sensor ds3231_sensor
Export the DS3231 sensor object.
uint8_t i2c_burst_send(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to send multiple bytes to a slave.
#define NULL
The null pointer.
uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to receive multiple bytes from a slave.
int ds3231_get_time(struct tm *t)
ds3231_get_time
int configure(int type, int c)
configure
int ds3231_clear_alarm(void)
ds3231_clear_alarm
int ds3231_set_time(struct tm *t)
ds3231_set_time
int ds3231_get_temperature(int *temperature)
ds3231_temperature
int status(int type)
status