-
Notifications
You must be signed in to change notification settings - Fork 1
/
ds3231.c
186 lines (160 loc) · 4.78 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* ------------------------------------------------------------------
* DS3231 Library for AVR Devices
* ------------------------------------------------------------------ */
#include "ds3231.h"
/**
* Read RTC EEPROM Data
*/
static int8_t ds3231_read_eeprom ( uint8_t addr, void *data, size_t len )
{
return i2c_start ( ) ||
i2c_addr ( DS3231_I2C_SLAW_WRITE ) ||
i2c_tx_byte ( addr ) ||
i2c_restart ( ) ||
i2c_addr ( DS3231_I2C_SLAW_READ ) ||
i2c_rx_data ( ( uint8_t * ) data, len ) || i2c_stop ( );
}
/**
* Write RTC EEPROM Data
*/
static int8_t ds3231_write_eeprom ( uint8_t addr, const void *data, size_t len )
{
return i2c_start ( ) ||
i2c_addr ( DS3231_I2C_SLAW_WRITE ) ||
i2c_tx_byte ( addr ) || i2c_tx_data ( ( const uint8_t * ) data, len ) || i2c_stop ( );
}
/**
* Read RTC Clock Data
*/
int8_t ds3231_read_clock ( struct ds3231_clock_t *clock )
{
struct ds3231_raw_clock_t raw;
if ( ds3231_read_eeprom ( DS3231_EEPROM_CLOCK, &raw, sizeof ( raw ) ) )
{
return 1;
}
clock->seconds = raw.seconds10 * 10 + raw.seconds;
clock->minutes = raw.minutes10 * 10 + raw.minutes;
clock->hours = raw.hours10 * 10 + raw.hours;
clock->day = raw.hours10 * 10 + raw.day;
clock->date = raw.date10 * 10 + raw.date;
clock->century = raw.century;
clock->month = raw.month10 * 10 + raw.month;
clock->year = raw.year10 * 10 + raw.year;
return 0;
}
/**
* Write RTC Clock Data
*/
int8_t ds3231_write_clock ( const struct ds3231_clock_t *clock )
{
struct ds3231_raw_clock_t raw = { 0 };
raw.seconds10 = clock->seconds / 10;
raw.seconds = clock->seconds % 10;
raw.minutes10 = clock->minutes / 10;
raw.minutes = clock->minutes % 10;
raw.hours10 = clock->hours / 10;
raw.hours = clock->hours % 10;
raw.day = clock->day;
raw.date10 = clock->date / 10;
raw.date = clock->date % 10;
raw.century = clock->century;
raw.month10 = clock->month / 10;
raw.month = clock->month % 10;
raw.year10 = clock->year / 10;
raw.year = clock->year % 10;
return ds3231_write_eeprom ( DS3231_EEPROM_CLOCK, &raw, sizeof ( raw ) );
}
/**
* Read RTC Alarm #1 Data
*/
int8_t ds3231_read_alarm1 ( struct ds3231_alarm_t *alarm )
{
struct ds3231_raw_alarm1_t raw;
if ( ds3231_read_eeprom ( DS3231_EEPROM_ALARM1, &raw, sizeof ( raw ) ) )
{
return 1;
}
alarm->m1 = raw.a1m1;
alarm->seconds = raw.seconds10 * 10 + raw.seconds;
alarm->m2 = raw.a1m2;
alarm->minutes = raw.minutes10 * 10 + raw.minutes;
alarm->m3 = raw.a1m3;
alarm->hours = raw.hours10 * 10 + raw.hours;
alarm->m4 = raw.a1m4;
alarm->date = raw.date10 * 10 + raw.date;
return 0;
}
/**
* Write RTC Alarm #1 Data
*/
int8_t ds3231_write_alarm1 ( const struct ds3231_alarm_t *alarm )
{
struct ds3231_raw_alarm1_t raw = { 0 };
raw.a1m1 = alarm->m1;
raw.seconds10 = alarm->seconds / 10;
raw.seconds = alarm->seconds % 10;
raw.a1m2 = alarm->m2;
raw.minutes10 = alarm->minutes / 10;
raw.minutes = alarm->minutes % 10;
raw.a1m3 = alarm->m3;
raw.hours10 = alarm->hours / 10;
raw.hours = alarm->hours % 10;
raw.a1m4 = alarm->m4;
raw.date10 = alarm->date / 10;
raw.date = alarm->date % 10;
return ds3231_write_eeprom ( DS3231_EEPROM_ALARM1, &raw, sizeof ( raw ) );
}
/**
* Read RTC Alarm #2 Data
*/
int8_t ds3231_read_alarm2 ( struct ds3231_alarm_t *alarm )
{
struct ds3231_raw_alarm2_t raw;
if ( ds3231_read_eeprom ( DS3231_EEPROM_ALARM2, &raw, sizeof ( raw ) ) )
{
return 1;
}
alarm->m1 = 0;
alarm->seconds = 0;
alarm->m2 = raw.a2m2;
alarm->minutes = raw.minutes10 * 10 + raw.minutes;
alarm->m3 = raw.a2m3;
alarm->hours = raw.hours10 * 10 + raw.hours;
alarm->m4 = raw.a2m4;
alarm->date = raw.date10 * 10 + raw.date;
return 0;
}
/**
* Write RTC Alarm #2 Data
*/
int8_t ds3231_write_alarm2 ( const struct ds3231_alarm_t *alarm )
{
struct ds3231_raw_alarm2_t raw = { 0 };
raw.a2m2 = alarm->m2;
raw.minutes10 = alarm->minutes / 10;
raw.minutes = alarm->minutes % 10;
raw.a2m3 = alarm->m3;
raw.hours10 = alarm->hours / 10;
raw.hours = alarm->hours % 10;
raw.a2m4 = alarm->m4;
raw.date10 = alarm->date / 10;
raw.date = alarm->date % 10;
return ds3231_write_eeprom ( DS3231_EEPROM_ALARM2, &raw, sizeof ( raw ) );
}
/**
* Read RTC Control Data
*/
int8_t ds3231_read_control ( struct ds3231_control_t *control )
{
return ds3231_read_eeprom ( DS3231_EEPROM_CONTROL, control,
sizeof ( struct ds3231_control_t ) );
}
/**
* Write RTC Control Data
*/
int8_t ds3231_write_control ( const struct ds3231_control_t *control )
{
return ds3231_write_eeprom ( DS3231_EEPROM_CONTROL, control,
sizeof ( struct ds3231_control_t ) );
}