LibDriver PCF8563
Loading...
Searching...
No Matches
driver_pcf8563_alarm.c
Go to the documentation of this file.
1
36
38
39static pcf8563_handle_t gs_handle;
40static int8_t gs_time_zone = 0;
41
50{
51 if (pcf8563_irq_handler(&gs_handle) != 0)
52 {
53 return 1;
54 }
55
56 return 0;
57}
58
67uint8_t pcf8563_alarm_init(void (*callback)(uint8_t type))
68{
69 uint8_t res;
70
71 /* link functions */
79 DRIVER_PCF8563_LINK_RECEIVE_CALLBACK(&gs_handle, callback);
80
81 /* init pcf8563 */
82 res = pcf8563_init(&gs_handle);
83 if (res != 0)
84 {
85 pcf8563_interface_debug_print("pcf8563: init failed.\n");
86
87 return 1;
88 }
89
90 /* disable rtc stop */
92 if (res != 0)
93 {
94 pcf8563_interface_debug_print("pcf8563: set rtc stop failed.\n");
95 (void)pcf8563_deinit(&gs_handle);
96
97 return 1;
98 }
99
100 /* disable test mode */
101 res = pcf8563_set_test_mode(&gs_handle, PCF8563_BOOL_FALSE);
102 if (res != 0)
103 {
104 pcf8563_interface_debug_print("pcf8563: set test mode failed.\n");
105 (void)pcf8563_deinit(&gs_handle);
106
107 return 1;
108 }
109
110 /* disable power on reset */
112 if (res != 0)
113 {
114 pcf8563_interface_debug_print("pcf8563: set power on reset failed.\n");
115 (void)pcf8563_deinit(&gs_handle);
116
117 return 1;
118 }
119
120 /* disable timer */
122 if (res != 0)
123 {
124 pcf8563_interface_debug_print("pcf8563: set timer enable failed.\n");
125 (void)pcf8563_deinit(&gs_handle);
126
127 return 1;
128 }
129
130 /* disable minute alarm */
132 if (res != 0)
133 {
134 pcf8563_interface_debug_print("pcf8563: set minute alarm enable failed.\n");
135 (void)pcf8563_deinit(&gs_handle);
136
137 return 1;
138 }
139
140 /* disable hour alarm */
142 if (res != 0)
143 {
144 pcf8563_interface_debug_print("pcf8563: set hour alarm enable failed.\n");
145 (void)pcf8563_deinit(&gs_handle);
146
147 return 1;
148 }
149
150 /* disable day alarm */
152 if (res != 0)
153 {
154 pcf8563_interface_debug_print("pcf8563: set day alarm enable failed.\n");
155 (void)pcf8563_deinit(&gs_handle);
156
157 return 1;
158 }
159
160 /* disable week alarm */
162 if (res != 0)
163 {
164 pcf8563_interface_debug_print("pcf8563: set week alarm enable failed.\n");
165 (void)pcf8563_deinit(&gs_handle);
166
167 return 1;
168 }
169
170 /* disable timer interrupt */
172 if (res != 0)
173 {
174 pcf8563_interface_debug_print("pcf8563: set timer interrupt failed.\n");
175 (void)pcf8563_deinit(&gs_handle);
176
177 return 1;
178 }
179
180 /* disable alarm interrupt */
182 if (res != 0)
183 {
184 pcf8563_interface_debug_print("pcf8563: set alarm interrupt failed.\n");
185 (void)pcf8563_deinit(&gs_handle);
186
187 return 1;
188 }
189
190 /* disable clock out */
192 if (res != 0)
193 {
194 pcf8563_interface_debug_print("pcf8563: set clock out enable failed.\n");
195 (void)pcf8563_deinit(&gs_handle);
196
197 return 1;
198 }
199
200 return 0;
201}
202
211{
212 if (pcf8563_deinit(&gs_handle) != 0)
213 {
214 return 1;
215 }
216
217 return 0;
218}
219
229{
230 /* set time */
231 if (pcf8563_set_time(&gs_handle, t) != 0)
232 {
233 return 1;
234 }
235
236 return 0;
237}
238
247uint8_t pcf8563_alarm_set_timestamp(time_t timestamp)
248{
250 struct tm *timeptr;
251
252 /* convert times */
253 timestamp += (time_t)(gs_time_zone * 3600);
254 timeptr = localtime(&timestamp);
255 t.date = (uint8_t)timeptr->tm_mday;
256 t.hour = (uint8_t)timeptr->tm_hour;
257 t.minute = (uint8_t)timeptr->tm_min;
258 t.month = (uint8_t)timeptr->tm_mon + 1;
259 t.second = (uint8_t)timeptr->tm_sec;
260 t.week = (uint8_t)timeptr->tm_wday;
261 t.year = (uint16_t)(timeptr->tm_year + 1900);
262
263 /* set time */
264 if (pcf8563_set_time(&gs_handle, &t) != 0)
265 {
266 return 1;
267 }
268
269 return 0;
270}
271
280{
281 gs_time_zone = zone;
282
283 return 0;
284}
285
295{
296 /* get time */
297 if (pcf8563_get_time(&gs_handle, t) != 0)
298 {
299 return 1;
300 }
301
302 return 0;
303}
304
313uint8_t pcf8563_alarm_get_timestamp(time_t *timestamp)
314{
316 struct tm timeptr;
317
318 /* get time */
319 if (pcf8563_get_time(&gs_handle, &t) != 0)
320 {
321 return 1;
322 }
323 timeptr.tm_year = t.year - 1900;
324 timeptr.tm_mon = t.month - 1;
325 timeptr.tm_wday = t.week;
326 timeptr.tm_mday = t.date;
327 timeptr.tm_hour = t.hour;
328 timeptr.tm_min = t.minute;
329 timeptr.tm_sec = t.second;
330
331 /* make time */
332 *timestamp = mktime(&timeptr) - gs_time_zone * 3600;
333
334 return 0;
335}
336
345{
346 *zone = gs_time_zone;
347
348 return 0;
349}
350
360uint8_t pcf8563_alarm_get_ascii_time(char *buf, uint8_t len)
361{
363
364 /* get time */
365 if (pcf8563_get_time(&gs_handle, &t) != 0)
366 {
367 return 1;
368 }
369
370 (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);
371
372 return 0;
373}
374
384{
385 uint8_t res;
386
387 /* set minute alarm */
388 res = pcf8563_set_minute_alarm_enable(&gs_handle, param->minute_enable);
389 if (res != 0)
390 {
391 return 1;
392 }
393
394 /* set minute */
395 res = pcf8563_set_minute_alarm(&gs_handle, param->minute);
396 if (res != 0)
397 {
398 return 1;
399 }
400
401 /* set hour alarm */
402 res = pcf8563_set_hour_alarm_enable(&gs_handle, param->hour_enable);
403 if (res != 0)
404 {
405 return 1;
406 }
407
408 /* set hour */
409 res = pcf8563_set_hour_alarm(&gs_handle, param->hour);
410 if (res != 0)
411 {
412 return 1;
413 }
414
415 /* set day alarm */
416 res = pcf8563_set_day_alarm_enable(&gs_handle, param->day_enable);
417 if (res != 0)
418 {
419 return 1;
420 }
421
422 /* set day */
423 res = pcf8563_set_day_alarm(&gs_handle, param->day);
424 if (res != 0)
425 {
426 return 1;
427 }
428
429 /* set week alarm */
430 res = pcf8563_set_week_alarm_enable(&gs_handle, param->week_enable);
431 if (res != 0)
432 {
433 return 1;
434 }
435
436 /* set week */
437 res = pcf8563_set_week_alarm(&gs_handle, param->week);
438 if (res != 0)
439 {
440 return 1;
441 }
442
443 /* set interrupt mode */
444 res = pcf8563_set_interrupt_mode(&gs_handle, param->mode);
445 if (res != 0)
446 {
447 return 1;
448 }
449
450 /* clear status */
452 if (res != 0)
453 {
454 pcf8563_interface_debug_print("pcf8563: clear status failed.\n");
455 (void)pcf8563_deinit(&gs_handle);
456
457 return 1;
458 }
459
460 /* enable alarm interrupt */
462 if (res != 0)
463 {
464 return 1;
465 }
466
467 return 0;
468}
469
478{
479 uint8_t res;
480
481 /* disable minute alarm */
483 if (res != 0)
484 {
485 return 1;
486 }
487
488 /* disable hour alarm */
490 if (res != 0)
491 {
492 return 1;
493 }
494
495 /* disable day alarm */
497 if (res != 0)
498 {
499 return 1;
500 }
501
502 /* disable week alarm */
504 if (res != 0)
505 {
506 return 1;
507 }
508
509 /* disable alarm interrupt */
511 if (res != 0)
512 {
513 return 1;
514 }
515
516 return 0;
517}
driver pcf8563 alarm header file
uint8_t pcf8563_set_week_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable week alarm
uint8_t pcf8563_set_hour_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable hour alarm
uint8_t pcf8563_set_timer_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable timer enable
uint8_t pcf8563_set_day_alarm(pcf8563_handle_t *handle, uint8_t day)
set day alarm
uint8_t pcf8563_deinit(pcf8563_handle_t *handle)
close the chip
uint8_t pcf8563_set_hour_alarm(pcf8563_handle_t *handle, uint8_t hour)
set hour alarm
uint8_t pcf8563_set_timer_interrupt(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable timer interrupt
uint8_t pcf8563_set_test_mode(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable test mode
uint8_t pcf8563_set_minute_alarm(pcf8563_handle_t *handle, uint8_t minute)
set minute alarm
uint8_t pcf8563_set_clock_out_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable clock out enable
uint8_t pcf8563_init(pcf8563_handle_t *handle)
initialize the chip
uint8_t pcf8563_set_time(pcf8563_handle_t *handle, pcf8563_time_t *t)
set the current time
struct pcf8563_time_s pcf8563_time_t
pcf8563 time structure definition
uint8_t pcf8563_irq_handler(pcf8563_handle_t *handle)
irq handler
uint8_t pcf8563_set_alarm_interrupt(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable alarm interrupt
uint8_t pcf8563_set_week_alarm(pcf8563_handle_t *handle, uint8_t week)
set week alarm
uint8_t pcf8563_set_day_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable day alarm
uint8_t pcf8563_set_minute_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable minute alarm
uint8_t pcf8563_set_rtc_stop(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable rtc stop
struct pcf8563_handle_s pcf8563_handle_t
pcf8563 handle structure definition
uint8_t pcf8563_set_interrupt_mode(pcf8563_handle_t *handle, pcf8563_interrupt_mode_t mode)
set the interrupt mode
uint8_t pcf8563_get_time(pcf8563_handle_t *handle, pcf8563_time_t *t)
get the current time
uint8_t pcf8563_clear_status(pcf8563_handle_t *handle, pcf8563_interrupt_event_t event)
clear status
uint8_t pcf8563_set_power_on_reset(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable power on reset
@ PCF8563_BOOL_TRUE
@ PCF8563_BOOL_FALSE
@ PCF8563_INTERRUPT_EVENT_ALARM
uint8_t pcf8563_alarm_set_timestamp(time_t timestamp)
alarm example set the time by a unix timestamp
struct pcf8563_alarm_param_s pcf8563_alarm_param_t
pcf8563 alarm param structure definition
uint8_t pcf8563_alarm_disable(void)
alarm example disable the alarm
uint8_t pcf8563_alarm_enable(pcf8563_alarm_param_t *param)
alarm example enable the alarm
uint8_t pcf8563_alarm_get_timestamp_time_zone(int8_t *zone)
alarm example get the local time zone
uint8_t pcf8563_alarm_set_time(pcf8563_time_t *t)
alarm example set the time
uint8_t pcf8563_alarm_irq_handler(void)
alarm example irq
uint8_t pcf8563_alarm_init(void(*callback)(uint8_t type))
alarm example init
uint8_t pcf8563_alarm_set_timestamp_time_zone(int8_t zone)
alarm example set the local time zone
uint8_t pcf8563_alarm_get_ascii_time(char *buf, uint8_t len)
alarm example get the ascii time
uint8_t pcf8563_alarm_get_timestamp(time_t *timestamp)
alarm example get the time in a unix timestamp
uint8_t pcf8563_alarm_deinit(void)
alarm example deinit
uint8_t pcf8563_alarm_get_time(pcf8563_time_t *t)
alarm example get the time
uint8_t pcf8563_interface_iic_init(void)
interface iic bus init
uint8_t pcf8563_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t pcf8563_interface_iic_deinit(void)
interface iic bus deinit
void pcf8563_interface_delay_ms(uint32_t ms)
interface delay ms
void pcf8563_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t pcf8563_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
pcf8563_interrupt_mode_t mode