LibDriver DS3231
Loading...
Searching...
No Matches
driver_ds3231_output.c
Go to the documentation of this file.
1
37
39
40static ds3231_handle_t gs_handle;
41static int8_t gs_time_zone = 0;
42
50uint8_t ds3231_output_init(void)
51{
52 uint8_t res;
53 int8_t reg;
54
55 /* link functions */
64
65 /* init ds3231 */
66 res = ds3231_init(&gs_handle);
67 if (res != 0)
68 {
69 ds3231_interface_debug_print("ds3231: init failed.\n");
70
71 return 1;
72 }
73
74 /* set oscillator */
75 res = ds3231_set_oscillator(&gs_handle, DS3231_BOOL_TRUE);
76 if (res != 0)
77 {
78 ds3231_interface_debug_print("ds3231: set oscillator failed.\n");
79 (void)ds3231_deinit(&gs_handle);
80
81 return 1;
82 }
83
84 /* disable alarm1 */
86 if (res != 0)
87 {
88 ds3231_interface_debug_print("ds3231: set alarm1 interrupt failed.\n");
89 (void)ds3231_deinit(&gs_handle);
90
91 return 1;
92 }
93
94 /* disable alarm2 */
96 if (res != 0)
97 {
98 ds3231_interface_debug_print("ds3231: set alarm2 interrupt failed.\n");
99 (void)ds3231_deinit(&gs_handle);
100
101 return 1;
102 }
103
104 /* set square wave */
105 res = ds3231_set_pin(&gs_handle, DS3231_PIN_SQUARE_WAVE);
106 if (res != 0)
107 {
108 ds3231_interface_debug_print("ds3231: set pin failed.\n");
109 (void)ds3231_deinit(&gs_handle);
110
111 return 1;
112 }
113
114 /* convert to register */
116 if (res != 0)
117 {
118 ds3231_interface_debug_print("ds3231: convert to register failed.\n");
119 (void)ds3231_deinit(&gs_handle);
120
121 return 1;
122 }
123
124 /* set aging offset */
125 res = ds3231_set_aging_offset(&gs_handle, reg);
126 if (res != 0)
127 {
128 ds3231_interface_debug_print("ds3231: set aging offset failed.\n");
129 (void)ds3231_deinit(&gs_handle);
130
131 return 1;
132 }
133
134 return 0;
135}
136
145{
146 if (ds3231_deinit(&gs_handle) != 0)
147 {
148 return 1;
149 }
150 else
151 {
152 return 0;
153 }
154}
155
165{
166 /* set time */
167 if (ds3231_set_time(&gs_handle, t) != 0)
168 {
169 return 1;
170 }
171 else
172 {
173 return 0;
174 }
175}
176
185uint8_t ds3231_output_set_timestamp(time_t timestamp)
186{
188 struct tm *timeptr;
189
190 /* convert times */
191 timestamp += (time_t)(gs_time_zone * 3600);
192 timeptr = localtime(&timestamp);
193 t.am_pm = DS3231_AM;
194 t.date = (uint8_t)timeptr->tm_mday;
196 t.hour = (uint8_t)timeptr->tm_hour;
197 t.minute = (uint8_t)timeptr->tm_min;
198 t.month = (uint8_t)timeptr->tm_mon + 1;
199 t.second = (uint8_t)timeptr->tm_sec;
200 if (timeptr->tm_wday == 0)
201 {
202 t.week = 7;
203 }
204 else
205 {
206 t.week = (uint8_t)timeptr->tm_wday;
207 }
208 t.year = (uint16_t)(timeptr->tm_year + 1900);
209
210 /* set time */
211 if (ds3231_set_time(&gs_handle, &t) != 0)
212 {
213 return 1;
214 }
215 else
216 {
217 return 0;
218 }
219}
220
229{
230 gs_time_zone = zone;
231
232 return 0;
233}
234
244{
245 /* get time */
246 if (ds3231_get_time(&gs_handle, t) != 0)
247 {
248 return 1;
249 }
250 else
251 {
252 return 0;
253 }
254}
255
264uint8_t ds3231_output_get_timestamp(time_t *timestamp)
265{
267 struct tm timeptr;
268
269 /* get time */
270 if (ds3231_get_time(&gs_handle, &t) != 0)
271 {
272 return 1;
273 }
274 timeptr.tm_year = t.year - 1900;
275 timeptr.tm_mon = t.month - 1;
276 timeptr.tm_wday = t.week;
277 timeptr.tm_mday = t.date;
278 if (t.format == DS3231_FORMAT_24H)
279 {
280 timeptr.tm_hour = t.hour;
281 }
282 else
283 {
284 timeptr.tm_hour = t.hour % 12 + t.am_pm * 12;
285 }
286 timeptr.tm_min = t.minute;
287 timeptr.tm_sec = t.second;
288
289 /* make time */
290 *timestamp = mktime(&timeptr) - gs_time_zone * 3600;
291
292 return 0;
293}
294
303{
304 *zone = gs_time_zone;
305
306 return 0;
307}
308
318uint8_t ds3231_output_get_temperature(int16_t *raw, float *s)
319{
320 /* get temperature */
321 if (ds3231_get_temperature(&gs_handle, raw, s) != 0)
322 {
323 return 1;
324 }
325 else
326 {
327 return 0;
328 }
329}
330
340uint8_t ds3231_output_get_ascii_time(char *buf, uint8_t len)
341{
343
344 /* get time */
345 if (ds3231_get_time(&gs_handle, &t) != 0)
346 {
347 return 1;
348 }
349
350 if (t.format == DS3231_FORMAT_24H)
351 {
352 (void)snprintf(buf, len, "%04d-%02d-%02d %02d:%02d:%02d %d.\n", t.year, t.month, t.date, t.hour, t.minute, t.second, t.week);
353 }
354 else
355 {
356 (void)snprintf(buf, len, "%04d-%02d-%02d %s %02d:%02d:%02d %d.\n", t.year, t.month, t.date, (t.am_pm == DS3231_AM) ? "AM" : "PM",
357 t.hour, t.minute, t.second, t.week
358 );
359 }
360
361 return 0;
362}
363
373{
374 /* set square wave */
375 if (ds3231_set_square_wave(&gs_handle, enable) != 0)
376 {
377 return 1;
378 }
379 else
380 {
381 return 0;
382 }
383}
384
394{
395 /* get square wave */
396 if (ds3231_get_square_wave(&gs_handle, enable) != 0)
397 {
398 return 1;
399 }
400 else
401 {
402 return 0;
403 }
404}
405
415{
416 /* set 32khz_output */
417 if (ds3231_set_32khz_output(&gs_handle, enable) != 0)
418 {
419 return 1;
420 }
421 else
422 {
423 return 0;
424 }
425}
426
436{
437 /* get 32khz_output */
438 if (ds3231_get_32khz_output(&gs_handle, enable) != 0)
439 {
440 return 1;
441 }
442 else
443 {
444 return 0;
445 }
446}
driver ds3231 output header file
uint8_t ds3231_get_32khz_output(ds3231_handle_t *handle, ds3231_bool_t *enable)
get the 32KHz output status
uint8_t ds3231_set_aging_offset(ds3231_handle_t *handle, int8_t offset)
set the chip aging offset
uint8_t ds3231_get_temperature(ds3231_handle_t *handle, int16_t *raw, float *s)
get the chip temperature
uint8_t ds3231_set_pin(ds3231_handle_t *handle, ds3231_pin_t pin)
set the chip pin function
uint8_t ds3231_aging_offset_convert_to_register(ds3231_handle_t *handle, float offset, int8_t *reg)
convert a aging offset value to a register raw data
uint8_t ds3231_set_32khz_output(ds3231_handle_t *handle, ds3231_bool_t enable)
enable or disable the 32KHz output
uint8_t ds3231_set_square_wave(ds3231_handle_t *handle, ds3231_bool_t enable)
enable or disable the square wave output
uint8_t ds3231_get_square_wave(ds3231_handle_t *handle, ds3231_bool_t *enable)
get the square wave output status
uint8_t ds3231_set_alarm_interrupt(ds3231_handle_t *handle, ds3231_alarm_t alarm, ds3231_bool_t enable)
enable or disable the alarm interrupt
uint8_t ds3231_set_oscillator(ds3231_handle_t *handle, ds3231_bool_t enable)
enable or disable the oscillator
uint8_t ds3231_set_time(ds3231_handle_t *handle, ds3231_time_t *t)
set the current time
struct ds3231_time_s ds3231_time_t
ds3231 time structure definition
struct ds3231_handle_s ds3231_handle_t
ds3231 handle structure definition
uint8_t ds3231_init(ds3231_handle_t *handle)
initialize the chip
uint8_t ds3231_get_time(ds3231_handle_t *handle, ds3231_time_t *t)
get the current time
uint8_t ds3231_deinit(ds3231_handle_t *handle)
close the chip
ds3231_bool_t
ds3231 bool enumeration definition
@ DS3231_AM
@ DS3231_ALARM_2
@ DS3231_ALARM_1
@ DS3231_FORMAT_24H
@ DS3231_PIN_SQUARE_WAVE
@ DS3231_BOOL_TRUE
@ DS3231_BOOL_FALSE
#define DS3231_OUTPUT_DEFAULT_AGING_OFFSET
ds3231 output example default definition
uint8_t ds3231_output_set_timestamp(time_t timestamp)
output example set the time by a unix timestamp
uint8_t ds3231_output_set_square_wave(ds3231_bool_t enable)
output example enable or disable the square wave
uint8_t ds3231_output_set_32khz_output(ds3231_bool_t enable)
output example enable or disable the 32KHz output
uint8_t ds3231_output_get_timestamp_time_zone(int8_t *zone)
output example get the local time zone
uint8_t ds3231_output_set_time(ds3231_time_t *t)
output example set the time
uint8_t ds3231_output_get_32khz_output(ds3231_bool_t *enable)
output example get the 32KHz output status
uint8_t ds3231_output_get_temperature(int16_t *raw, float *s)
output example get the current temperature
uint8_t ds3231_output_set_timestamp_time_zone(int8_t zone)
output example set the local time zone
uint8_t ds3231_output_get_timestamp(time_t *timestamp)
output example get the time in a unix timestamp
uint8_t ds3231_output_init(void)
output example init
uint8_t ds3231_output_get_ascii_time(char *buf, uint8_t len)
output example get the ascii time
uint8_t ds3231_output_get_time(ds3231_time_t *t)
output example get the time
uint8_t ds3231_output_get_square_wave(ds3231_bool_t *enable)
output example get the square wave status
uint8_t ds3231_output_deinit(void)
output example deinit
uint8_t ds3231_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t ds3231_interface_iic_init(void)
interface iic bus init
uint8_t ds3231_interface_iic_deinit(void)
interface iic bus deinit
void ds3231_interface_delay_ms(uint32_t ms)
interface delay ms
void ds3231_interface_debug_print(const char *const fmt,...)
interface print format data
void ds3231_interface_receive_callback(uint8_t type)
interface receive callback
uint8_t ds3231_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
ds3231_am_pm_t am_pm
ds3231_format_t format