LibDriver PCF8563
Loading...
Searching...
No Matches
driver_pcf8563_timer.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_timer_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_timer_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_timer_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_timer_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
386{
387 uint8_t res;
388
389 /* set timer freq */
390 res = pcf8563_set_timer_freq(&gs_handle, freq);
391 if (res != 0)
392 {
393 return 1;
394 }
395
396 /* set timer value */
397 res = pcf8563_set_timer_value(&gs_handle, value);
398 if (res != 0)
399 {
400 return 1;
401 }
402
403 /* clear status */
405 if (res != 0)
406 {
407 pcf8563_interface_debug_print("pcf8563: clear status failed.\n");
408 (void)pcf8563_deinit(&gs_handle);
409
410 return 1;
411 }
412
413 /* enable timer interrupt */
415 if (res != 0)
416 {
417 pcf8563_interface_debug_print("pcf8563: set timer interrupt failed.\n");
418 (void)pcf8563_deinit(&gs_handle);
419
420 return 1;
421 }
422
423 /* enable timer */
425 if (res != 0)
426 {
427 return 1;
428 }
429
430 return 0;
431}
432
441{
442 uint8_t res;
443
444 /* disable timer interrupt */
446 if (res != 0)
447 {
448 pcf8563_interface_debug_print("pcf8563: set timer interrupt failed.\n");
449 (void)pcf8563_deinit(&gs_handle);
450
451 return 1;
452 }
453
454 /* disable timer */
456 if (res != 0)
457 {
458 return 1;
459 }
460
461 return 0;
462}
driver pcf8563 timer 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
pcf8563_timer_freq_t
pcf8563 timer freq enumeration definition
uint8_t pcf8563_deinit(pcf8563_handle_t *handle)
close the chip
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_timer_freq(pcf8563_handle_t *handle, pcf8563_timer_freq_t freq)
set timer freq
uint8_t pcf8563_set_clock_out_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable clock out enable
pcf8563_interrupt_mode_t
pcf8563 interrupt mode enumeration definition
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_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_timer_value(pcf8563_handle_t *handle, uint8_t value)
set timer value
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_TIMER
uint8_t pcf8563_timer_get_ascii_time(char *buf, uint8_t len)
timer example get the ascii time
uint8_t pcf8563_timer_get_time(pcf8563_time_t *t)
timer example get the time
uint8_t pcf8563_timer_set_time(pcf8563_time_t *t)
timer example set the time
uint8_t pcf8563_timer_get_timestamp(time_t *timestamp)
timer example get the time in a unix timestamp
uint8_t pcf8563_timer_enable(pcf8563_timer_freq_t freq, uint8_t value, pcf8563_interrupt_mode_t mode)
timer example enable the timer
uint8_t pcf8563_timer_disable(void)
timer example disable the timer
uint8_t pcf8563_timer_set_timestamp_time_zone(int8_t zone)
timer example set the local time zone
uint8_t pcf8563_timer_deinit(void)
timer example deinit
uint8_t pcf8563_timer_irq_handler(void)
timer example irq
uint8_t pcf8563_timer_set_timestamp(time_t timestamp)
timer example set the time by a unix timestamp
uint8_t pcf8563_timer_get_timestamp_time_zone(int8_t *zone)
timer example get the local time zone
uint8_t pcf8563_timer_init(void(*callback)(uint8_t type))
timer example init
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