-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds3231.h
277 lines (259 loc) · 8.54 KB
/
ds3231.h
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define DS3231_CLOCK_REG_ADDR 0x00 // start address of registers
#define DS3231_ALARM1_REG_ADDRESS 0x07
#define DS3231_ALARM2_REG_ADDRESS 0x0B
#define DS3231_TEMP_REG_ADDR 0x11
#define DS3231_INTCN_BIT_NO 2 // index of INTCN bit right to left
#define DS3231_A2IE_A2F_BIT_NO 1 // index of A2IE bit right to left
#define DS3231_A1IE_A1F_BIT_NO 0 // index of A1IE bit right to left
#define DS3231_I2C_MASTER_TIMEOUT_MS 1000
/*!
* @brief Bus communication function pointer which should be mapped to
* the platform specific read functions of the user
*
* @param[in] reg_addr : Register address from which data is read.
* @param[out] reg_data : Pointer to data buffer where read data is stored.
* @param[in] len : Number of bytes of data to be read.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs.
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*
*/
typedef int8_t (*ds3231_read_fptr_t)(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr);
/*!
* @brief Bus communication function pointer which should be mapped to
* the platform specific write functions of the user
*
* @param[in] reg_addr : Register address to which the data is written.
* @param[in] reg_data : Pointer to data buffer in which data to be written
* is stored.
* @param[in] len : Number of bytes of data to be written.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*
*/
typedef int8_t (*ds3231_write_fptr_t)(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len,
void *intf_ptr);
/*!
* @brief ds3231 device
*/
typedef struct
{
/* Interface function pointer used to enable the device address for I2C */
void *i2c_chip_ptr;
/* user defined read I2C funtion pointer */
ds3231_read_fptr_t read;
/* user defined write I2C funtion pointer */
ds3231_write_fptr_t write;
} ds3231_dev;
/*!
* @brief ds3231 date time store
*/
typedef struct
{
uint8_t seconds; // 0-59
uint8_t minutes; // 0-59
uint8_t hours; // 0-23 (24 hour)
uint8_t day_of_week; // 1-7
uint8_t date; // 1-31
uint8_t month; // 1-12
uint16_t year; // 2000-2099
} ds3231_date_time;
/*!
* @brief ds3231 alarm 1 mode enum
*/
typedef enum
{
A1_ONCE_PER_SECOND = 0x0F, // once per second
A1_SECONDS_MATCH = 0x0E, // when seconds match
A1_MINUTES_SECONDS_MATCH = 0x0C, // when minutes and seconds match
A1_HOURS_MINUTES_SECONDS_MATCH = 0x08, // when hours, minutes and seconds match
A1_DATE_HOURS_MINUTES_SECONDS_MATCH = 0x00, // when date, hours, minutes and seconds match
A1_DAY_OF_WEEK_HOURS_MINUTES_SECONDS_MATCH = 0x10 // when day of week, hours, minutes and seconds match
} ds3231_alarm_1_alarm_rate;
/*!
* @brief ds3231 alarm 1 configuration
*/
typedef struct
{
uint8_t seconds;
uint8_t minutes;
uint8_t hours;
uint8_t day_date; // Range 1-7 // Range 0-31
ds3231_alarm_1_alarm_rate alarm_rate;
} ds3231_alarm_1;
/*!
* @brief ds3231 alarm 2 mode enum
*/
typedef enum
{
A2_ONCE_PER_MINUTE = 0x07, // once per minute
A2_MINUTES_MATCH = 0x06, // when minutes match
A2_HOURS_MINUTES_MATCH = 0x04, // when hours and seconds match
A2_DATE_HOURS_MINUTES_MATCH = 0x00, // when date, hours and minutes match
A2_DAY_OF_WEEK_HOURS_MINUTES_MATCH = 0x08 // when day of week, hours and minutes match
} ds3231_alarm_2_alarm_rate;
/*!
* @brief ds3231 alarm selector
*/
typedef enum
{
ALARM_1 = 0x00,
ALARM_2 = 0x01
} ds3231_alarm;
/*!
* @brief ds3231 INTCN selector
*/
typedef enum
{
INTCN_SQUARE_WAVE = 0x00,
INTCN_ALARM_INTERRUPT = 0x01
} ds3231_intcn;
/*!
* @brief ds3231 control registers adrresses
*/
typedef enum
{
CONTROL_REG_1 = 0x0E,
CONTROL_REG_2 = 0x0F
} ds3231_control_reg;
/*!
* @brief ds3231 alarm 2 configuration
*/
typedef struct
{
uint8_t minutes;
uint8_t hours;
uint8_t day_date; // Range 1-7 // Range 0-31
ds3231_alarm_2_alarm_rate alarm_rate;
} ds3231_alarm_2;
/*!
* @brief Setup datetime
*
* @param[in] clock Clock device
* @param[in] date_time Filled date_time struct ptr
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*/
int8_t ds3231_set_date_time(ds3231_dev *clock, ds3231_date_time *date_time);
/*!
* @brief Get datetime from chip
*
* @param[in] clock Clock device
* @param[out] date_time Date_time ptr
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*/
int8_t ds3231_get_date_time(ds3231_dev *clock, ds3231_date_time *date_time);
/*!
* @brief Setup first alarm
*
* @param[in] clock Clock device
* @param[in] date_time Filled ds3231_alarm_1 struct ptr
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*/
int8_t ds3231_set_alarm_1(ds3231_dev *clock, ds3231_alarm_1 *alarm);
/*!
* @brief Get first alarm settings from chip
*
* @param[in] clock Clock device
* @param[out] alarm ds3231_alarm_1 date_time ptr
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*/
int8_t ds3231_get_alarm_1(ds3231_dev *clock, ds3231_alarm_1 *alarm);
/*!
* @brief Setup second alarm
*
* @param[in] clock Clock device
* @param[in] date_time Filled ds3231_alarm_2 struct ptr
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*/
int8_t ds3231_set_alarm_2(ds3231_dev *clock, ds3231_alarm_2 *alarm);
/*!
* @brief Get second alarm settings from chip
*
* @param[in] clock Clock device
* @param[out] alarm ds3231_alarm_2 date_time ptr
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*/
int8_t ds3231_get_alarm_2(ds3231_dev *clock, ds3231_alarm_2 *alarm);
/*!
* @brief Get alarm flag status
*
* @param[in] clock Clock device
* @param[in] alarm Alarm selector
*
* @retval Alarm flag status
*/
bool ds3231_check_alarm_interrupt(ds3231_dev *clock, ds3231_alarm alarm);
/*!
* @brief Get alarm fired flag status
*
* @param[in] clock Clock device
* @param[in] alarm Alarm selector
*
* @retval Alarm fired flag status
*/
bool ds3231_check_alarm_fired(ds3231_dev *clock, ds3231_alarm alarm);
/*!
* @brief Reset fired flag. Use to restart alarm with existed settings
*
* @param[in] clock Clock device
* @param[in] alarm Alarm selector
*
* @retval Alarm fired flag status
*/
uint8_t ds3231_reset_alarm_fired(ds3231_dev *clock, ds3231_alarm alarm);
/*!
* @brief Enable or disable alarm interrupt
*
* @param[in] clock Clock device
* @param[in] alarm Alarm selector
* @param[in] enable Enable/disable
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*/
int8_t ds3231_set_alarm_interrupt(ds3231_dev *clock, ds3231_alarm alarm, bool enable);
/*!
* @brief Change INTCN flag status
*
* @param[in] clock Clock device
* @param[in] intcn_value New Value
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*/
int8_t ds3231_set_intcn_mode(ds3231_dev *clock, ds3231_intcn intcn_value);
/*!
* @brief Get value from chip temperature sensor
*
* @param[in] clock Clock device
*
* @retval Temperature in ℃
*/
float ds3231_get_temperature(ds3231_dev *clock);
#ifdef __cplusplus
}
#endif /* End of CPP guard */