/
ds3231.c
103 lines (85 loc) · 2.51 KB
/
ds3231.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @file ds3231.c
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
#define DS3231_I2C_ADDRESS 0x68
static inline uint8_t encode(int value)
{
int low;
low = (value % 10);
return ((((value - low) / 10) << 4) | low);
}
static inline int decode(uint8_t value)
{
return (10 * (value >> 4) + (value & 0x0f));
}
int ds3231_init(struct ds3231_driver_t *self_p,
struct i2c_driver_t *i2c_p)
{
self_p->i2c_p = i2c_p;
return (0);
}
int ds3231_set_date(struct ds3231_driver_t *self_p,
struct date_t *date_p)
{
uint8_t date[8];
date[0] = 0x00;
date[1] = encode(date_p->second);
date[2] = encode(date_p->minute);
date[3] = (((date_p->hour > 19) << 5)
+ encode(date_p->hour % 20));
date[4] = date_p->day;
date[5] = encode(date_p->date);
date[6] = encode(date_p->month);
date[7] = encode(date_p->year % 100);
if (i2c_write(self_p->i2c_p,
DS3231_I2C_ADDRESS,
date,
sizeof(date)) != sizeof(date)) {
return (-1);
}
return (0);
}
int ds3231_get_date(struct ds3231_driver_t *self_p,
struct date_t *date_p)
{
uint8_t date[7];
uint8_t address;
address = 0x0;
if (i2c_write(self_p->i2c_p,
DS3231_I2C_ADDRESS,
&address,
sizeof(address)) != sizeof(address)) {
return (-1);
}
if (i2c_read(self_p->i2c_p,
DS3231_I2C_ADDRESS,
date,
sizeof(date)) != sizeof(date)) {
return (-1);
}
date_p->second = decode(date[0]);
date_p->minute = decode(date[1]);
date_p->hour = (20 * ((date[2] >> 5) & 0x01)
+ decode(date[2] & 0x1f));
date_p->day = date[3];
date_p->date = decode(date[4]);
date_p->month = decode(date[5] & 0x7f);
date_p->year = decode(date[6]);
return (0);
}