LibDriver DS3231
Loading...
Searching...
No Matches
driver_ds3231_alarm.c
Go to the documentation of this file.
1
37
38#include "driver_ds3231_alarm.h"
39
40static ds3231_handle_t gs_handle;
41static int8_t gs_time_zone = 0;
42
51{
52 /* irq handler */
53 if (ds3231_irq_handler(&gs_handle) != 0)
54 {
55 return 1;
56 }
57 else
58 {
59 return 0;
60 }
61}
62
71uint8_t ds3231_alarm_init(void (*alarm_receive_callback)(uint8_t type))
72{
73 uint8_t res;
74 int8_t reg;
75
76 /* link functions */
84 DRIVER_DS3231_LINK_RECEIVE_CALLBACK(&gs_handle, alarm_receive_callback);
85
86 /* init ds3231 */
87 res = ds3231_init(&gs_handle);
88 if (res != 0)
89 {
90 ds3231_interface_debug_print("ds3231: init failed.\n");
91
92 return 1;
93 }
94
95 /* set oscillator */
96 res = ds3231_set_oscillator(&gs_handle, DS3231_BOOL_TRUE);
97 if (res != 0)
98 {
99 ds3231_interface_debug_print("ds3231: set oscillator failed.\n");
100 (void)ds3231_deinit(&gs_handle);
101
102 return 1;
103 }
104
105 /* set interrupt */
106 res = ds3231_set_pin(&gs_handle, DS3231_PIN_INTERRUPT);
107 if (res != 0)
108 {
109 ds3231_interface_debug_print("ds3231: set pin failed.\n");
110 (void)ds3231_deinit(&gs_handle);
111
112 return 1;
113 }
114
115 /* disable square wave */
116 res = ds3231_set_square_wave(&gs_handle, DS3231_BOOL_FALSE);
117 if (res != 0)
118 {
119 ds3231_interface_debug_print("ds3231: set square wave failed.\n");
120 (void)ds3231_deinit(&gs_handle);
121
122 return 1;
123 }
124
125 /* disable 32khz output */
127 if (res != 0)
128 {
129 ds3231_interface_debug_print("ds3231: set 32khz output failed.\n");
130 (void)ds3231_deinit(&gs_handle);
131
132 return 1;
133 }
134
135 /* convert to register */
137 if (res != 0)
138 {
139 ds3231_interface_debug_print("ds3231: convert to register failed.\n");
140 (void)ds3231_deinit(&gs_handle);
141
142 return 1;
143 }
144
145 /* set aging offset */
146 res = ds3231_set_aging_offset(&gs_handle, reg);
147 if (res != 0)
148 {
149 ds3231_interface_debug_print("ds3231: set aging offset failed.\n");
150 (void)ds3231_deinit(&gs_handle);
151
152 return 1;
153 }
154
155 return 0;
156}
157
166{
167 if (ds3231_deinit(&gs_handle) != 0)
168 {
169 return 1;
170 }
171 else
172 {
173 return 0;
174 }
175}
176
187{
188 /* set alarm1 */
189 if (ds3231_set_alarm1(&gs_handle, t, mode) != 0)
190 {
191 return 1;
192 }
193 else
194 {
195 return 0;
196 }
197}
198
209{
210 /* get alarm1 */
211 if (ds3231_get_alarm1(&gs_handle, t, mode) != 0)
212 {
213 return 1;
214 }
215 else
216 {
217 return 0;
218 }
219}
220
231{
232 /* set alarm2 */
233 if (ds3231_set_alarm2(&gs_handle, t, mode) != 0)
234 {
235 return 1;
236 }
237 else
238 {
239 return 0;
240 }
241}
242
253{
254 /* get alarm2 */
255 if (ds3231_get_alarm2(&gs_handle, t, mode) != 0)
256 {
257 return 1;
258 }
259 else
260 {
261 return 0;
262 }
263}
264
274{
275 /* alarm clear */
276 if (ds3231_alarm_clear(&gs_handle, alarm) != 0)
277 {
278 return 1;
279 }
280 else
281 {
282 return 0;
283 }
284}
285
295{
296 /* clear alarm flag */
297 if (ds3231_alarm_clear(&gs_handle, alarm) != 0)
298 {
299 return 1;
300 }
301
302 /* enable alarm */
303 if (ds3231_set_alarm_interrupt(&gs_handle, alarm, DS3231_BOOL_TRUE) != 0)
304 {
305 return 1;
306 }
307 else
308 {
309 return 0;
310 }
311}
312
322{
323 /* disable alarm */
324 if (ds3231_set_alarm_interrupt(&gs_handle, alarm, DS3231_BOOL_FALSE) != 0)
325 {
326 return 1;
327 }
328 else
329 {
330 return 0;
331 }
332}
333
343{
344 /* set time */
345 if (ds3231_set_time(&gs_handle, t) != 0)
346 {
347 return 1;
348 }
349 else
350 {
351 return 0;
352 }
353}
354
363uint8_t ds3231_alarm_set_timestamp(time_t timestamp)
364{
366 struct tm *timeptr;
367
368 /* convert times */
369 timestamp += (time_t)(gs_time_zone * 3600);
370 timeptr = localtime(&timestamp);
371 t.am_pm = DS3231_AM;
372 t.date = (uint8_t)timeptr->tm_mday;
374 t.hour = (uint8_t)timeptr->tm_hour;
375 t.minute = (uint8_t)timeptr->tm_min;
376 t.month = (uint8_t)timeptr->tm_mon + 1;
377 t.second = (uint8_t)timeptr->tm_sec;
378 if (timeptr->tm_wday == 0)
379 {
380 t.week = 7;
381 }
382 else
383 {
384 t.week = (uint8_t)timeptr->tm_wday;
385 }
386 t.year = (uint16_t)(timeptr->tm_year + 1900);
387
388 /* set time */
389 if (ds3231_set_time(&gs_handle, &t) != 0)
390 {
391 return 1;
392 }
393 else
394 {
395 return 0;
396 }
397}
398
407{
408 gs_time_zone = zone;
409
410 return 0;
411}
412
422{
423 /* get time */
424 if (ds3231_get_time(&gs_handle, t) != 0)
425 {
426 return 1;
427 }
428 else
429 {
430 return 0;
431 }
432}
433
442uint8_t ds3231_alarm_get_timestamp(time_t *timestamp)
443{
445 struct tm timeptr;
446
447 /* get time */
448 if (ds3231_get_time(&gs_handle, &t) != 0)
449 {
450 return 1;
451 }
452 timeptr.tm_year = t.year - 1900;
453 timeptr.tm_mon = t.month - 1;
454 timeptr.tm_wday = t.week;
455 timeptr.tm_mday = t.date;
456 if (t.format == DS3231_FORMAT_24H)
457 {
458 timeptr.tm_hour = t.hour;
459 }
460 else
461 {
462 timeptr.tm_hour = t.hour % 12 + t.am_pm * 12;
463 }
464 timeptr.tm_min = t.minute;
465 timeptr.tm_sec = t.second;
466
467 /* make time */
468 *timestamp = mktime(&timeptr) - gs_time_zone * 3600;
469
470 return 0;
471}
472
481{
482 *zone = gs_time_zone;
483
484 return 0;
485}
486
496uint8_t ds3231_alarm_get_temperature(int16_t *raw, float *s)
497{
498 /* get temperature */
499 if (ds3231_get_temperature(&gs_handle, raw, s) != 0)
500 {
501 return 1;
502 }
503 else
504 {
505 return 0;
506 }
507}
508
518uint8_t ds3231_alarm_alarm_ascii_time(char *buf, uint8_t len)
519{
521
522 /* get time */
523 if (ds3231_get_time(&gs_handle, &t) != 0)
524 {
525 return 1;
526 }
527
528 if (t.format == DS3231_FORMAT_24H)
529 {
530 (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);
531 }
532 else
533 {
534 (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",
535 t.hour, t.minute, t.second, t.week
536 );
537 }
538
539 return 0;
540}
driver ds3231 alarm header file
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_set_alarm_interrupt(ds3231_handle_t *handle, ds3231_alarm_t alarm, ds3231_bool_t enable)
enable or disable the alarm interrupt
ds3231_alarm2_mode_t
ds3231 alarm2 enumeration definition
uint8_t ds3231_get_alarm1(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm1_mode_t *mode)
get the alarm1 time
ds3231_alarm1_mode_t
ds3231 alarm1 enumeration definition
uint8_t ds3231_set_alarm2(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm2_mode_t mode)
set the alarm2 time
uint8_t ds3231_alarm_clear(ds3231_handle_t *handle, ds3231_alarm_t alarm)
clear the alarm flag
uint8_t ds3231_set_alarm1(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm1_mode_t mode)
set the alarm1 time
uint8_t ds3231_get_alarm2(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm2_mode_t *mode)
get the alarm2 time
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
ds3231_alarm_t
ds3231 alarm enumeration definition
uint8_t ds3231_irq_handler(ds3231_handle_t *handle)
irq handler
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_AM
@ DS3231_FORMAT_24H
@ DS3231_PIN_INTERRUPT
@ DS3231_BOOL_TRUE
@ DS3231_BOOL_FALSE
uint8_t ds3231_alarm_get_time(ds3231_time_t *t)
alarm example get the time
uint8_t ds3231_alarm_set_alarm2(ds3231_time_t *t, ds3231_alarm2_mode_t mode)
alarm example set the alarm 2
uint8_t ds3231_alarm_set_alarm1(ds3231_time_t *t, ds3231_alarm1_mode_t mode)
alarm example set the alarm 1
uint8_t ds3231_alarm_enable(ds3231_alarm_t alarm)
enable the alarm
uint8_t ds3231_alarm_set_timestamp(time_t timestamp)
alarm example set the time by a unix timestamp
uint8_t ds3231_alarm_get_temperature(int16_t *raw, float *s)
alarm example get the current temperature
uint8_t ds3231_alarm_init(void(*alarm_receive_callback)(uint8_t type))
alarm example init
uint8_t ds3231_alarm_disable(ds3231_alarm_t alarm)
disable the alarm
uint8_t ds3231_alarm_clear_flag(ds3231_alarm_t alarm)
alarm clear the interrupt flag
#define DS3231_ALARM_DEFAULT_AGING_OFFSET
ds3231 alarm example default definition
uint8_t ds3231_alarm_set_time(ds3231_time_t *t)
alarm example set the time
uint8_t ds3231_alarm_set_timestamp_time_zone(int8_t zone)
alarm example set the local time zone
uint8_t ds3231_alarm_get_alarm1(ds3231_time_t *t, ds3231_alarm1_mode_t *mode)
alarm example get the alarm 1
uint8_t ds3231_alarm_irq_handler(void)
alarm irq handler
uint8_t ds3231_alarm_get_timestamp(time_t *timestamp)
alarm example get the time in a unix timestamp
uint8_t ds3231_alarm_get_timestamp_time_zone(int8_t *zone)
alarm example get the local time zone
uint8_t ds3231_alarm_get_alarm2(ds3231_time_t *t, ds3231_alarm2_mode_t *mode)
alarm example get the alarm 2
uint8_t ds3231_alarm_alarm_ascii_time(char *buf, uint8_t len)
alarm example get the ascii time
uint8_t ds3231_alarm_deinit(void)
alarm 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
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