LibDriver DS1302
Loading...
Searching...
No Matches
driver_ds1302_basic.c
Go to the documentation of this file.
1
36
37#include "driver_ds1302_basic.h"
38
39static ds1302_handle_t gs_handle;
40static int8_t gs_time_zone = 0;
41
49uint8_t ds1302_basic_init(void)
50{
51 uint8_t res;
52
53 /* link functions */
68
69 /* init ds1302 */
70 res = ds1302_init(&gs_handle);
71 if (res != 0)
72 {
73 ds1302_interface_debug_print("ds1302: init failed.\n");
74
75 return 1;
76 }
77
78 /* set oscillator */
79 res = ds1302_set_oscillator(&gs_handle, DS1302_BOOL_TRUE);
80 if (res != 0)
81 {
82 ds1302_interface_debug_print("ds1302: set oscillator failed.\n");
83 (void)ds1302_deinit(&gs_handle);
84
85 return 1;
86 }
87
88 /* disable write protect */
90 if (res != 0)
91 {
92 ds1302_interface_debug_print("ds1302: set write protect failed.\n");
93 (void)ds1302_deinit(&gs_handle);
94
95 return 1;
96 }
97
98 return 0;
99}
100
109{
110 if (ds1302_deinit(&gs_handle) != 0)
111 {
112 return 1;
113 }
114
115 return 0;
116}
117
127{
128 /* set time */
129 if (ds1302_set_time(&gs_handle, t) != 0)
130 {
131 return 1;
132 }
133
134 return 0;
135}
136
145uint8_t ds1302_basic_set_timestamp(time_t timestamp)
146{
148 struct tm *timeptr;
149
150 /* convert times */
151 timestamp += (time_t)(gs_time_zone * 3600);
152 timeptr = localtime(&timestamp);
153 t.am_pm = DS1302_AM;
154 t.date = (uint8_t)timeptr->tm_mday;
156 t.hour = (uint8_t)timeptr->tm_hour;
157 t.minute = (uint8_t)timeptr->tm_min;
158 t.month = (uint8_t)timeptr->tm_mon + 1;
159 t.second = (uint8_t)timeptr->tm_sec;
160 if (timeptr->tm_wday == 0)
161 {
162 t.week = 7;
163 }
164 else
165 {
166 t.week = (uint8_t)timeptr->tm_wday;
167 }
168 t.year = (uint16_t)(timeptr->tm_year + 1900);
169
170 /* set time */
171 if (ds1302_set_time(&gs_handle, &t) != 0)
172 {
173 return 1;
174 }
175
176 return 0;
177}
178
187{
188 gs_time_zone = zone;
189
190 return 0;
191}
192
202{
203 /* get time */
204 if (ds1302_get_time(&gs_handle, t) != 0)
205 {
206 return 1;
207 }
208
209 return 0;
210}
211
220uint8_t ds1302_basic_get_timestamp(time_t *timestamp)
221{
223 struct tm timeptr;
224
225 /* get time */
226 if (ds1302_get_time(&gs_handle, &t) != 0)
227 {
228 return 1;
229 }
230 timeptr.tm_year = t.year - 1900;
231 timeptr.tm_mon = t.month - 1;
232 timeptr.tm_wday = t.week;
233 timeptr.tm_mday = t.date;
234 if (t.format == DS1302_FORMAT_24H)
235 {
236 timeptr.tm_hour = t.hour;
237 }
238 else
239 {
240 timeptr.tm_hour = t.hour % 12 + t.am_pm * 12;
241 }
242 timeptr.tm_min = t.minute;
243 timeptr.tm_sec = t.second;
244
245 /* make time */
246 *timestamp = mktime(&timeptr) - gs_time_zone * 3600;
247
248 return 0;
249}
250
259{
260 *zone = gs_time_zone;
261
262 return 0;
263}
264
274uint8_t ds1302_basic_get_ascii_time(char *buf, uint8_t len)
275{
277
278 /* get time */
279 if (ds1302_get_time(&gs_handle, &t) != 0)
280 {
281 return 1;
282 }
283
284 if (t.format == DS1302_FORMAT_24H)
285 {
286 (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);
287 }
288 else
289 {
290 (void)snprintf(buf, len, "%04d-%02d-%02d %s %02d:%02d:%02d %d.\n", t.year, t.month, t.date, (t.am_pm == DS1302_AM) ? "AM" : "PM",
291 t.hour, t.minute, t.second, t.week
292 );
293 }
294
295 return 0;
296}
297
308uint8_t ds1302_basic_read_ram(uint8_t addr, uint8_t *buf, uint8_t len)
309{
310 if (ds1302_read_ram(&gs_handle, addr, buf, len) != 0)
311 {
312 return 1;
313 }
314
315 return 0;
316}
317
328uint8_t ds1302_basic_write_ram(uint8_t addr, uint8_t *buf, uint8_t len)
329{
330 if (ds1302_write_ram(&gs_handle, addr, buf, len) != 0)
331 {
332 return 1;
333 }
334
335 return 0;
336}
driver ds1302 basic header file
uint8_t ds1302_init(ds1302_handle_t *handle)
initialize the chip
uint8_t ds1302_set_write_protect(ds1302_handle_t *handle, ds1302_bool_t enable)
enable or disable write protect
struct ds1302_time_s ds1302_time_t
ds1302 time structure definition
uint8_t ds1302_deinit(ds1302_handle_t *handle)
close the chip
uint8_t ds1302_set_oscillator(ds1302_handle_t *handle, ds1302_bool_t enable)
enable or disable the oscillator
uint8_t ds1302_get_time(ds1302_handle_t *handle, ds1302_time_t *t)
get the current time
uint8_t ds1302_read_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
read ram
struct ds1302_handle_s ds1302_handle_t
ds1302 handle structure definition
uint8_t ds1302_write_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
write ram
uint8_t ds1302_set_time(ds1302_handle_t *handle, ds1302_time_t *t)
set the current time
@ DS1302_BOOL_FALSE
@ DS1302_BOOL_TRUE
@ DS1302_FORMAT_24H
@ DS1302_AM
uint8_t ds1302_basic_set_timestamp(time_t timestamp)
basic example set the time by a unix timestamp
uint8_t ds1302_basic_read_ram(uint8_t addr, uint8_t *buf, uint8_t len)
basic example read ram
uint8_t ds1302_basic_get_ascii_time(char *buf, uint8_t len)
basic example get the ascii time
uint8_t ds1302_basic_get_timestamp(time_t *timestamp)
basic example get the time in a unix timestamp
uint8_t ds1302_basic_set_timestamp_time_zone(int8_t zone)
basic example set the local time zone
uint8_t ds1302_basic_deinit(void)
basic example deinit
uint8_t ds1302_basic_get_timestamp_time_zone(int8_t *zone)
basic example get the local time zone
uint8_t ds1302_basic_get_time(ds1302_time_t *t)
basic example get the time
uint8_t ds1302_basic_set_time(ds1302_time_t *t)
basic example set the time
uint8_t ds1302_basic_init(void)
basic example init
uint8_t ds1302_basic_write_ram(uint8_t addr, uint8_t *buf, uint8_t len)
basic example write ram
uint8_t ds1302_interface_ce_gpio_init(void)
interface ce gpio init
uint8_t ds1302_interface_io_gpio_write(uint8_t value)
interface io gpio write
uint8_t ds1302_interface_sclk_gpio_deinit(void)
interface sclk gpio deinit
uint8_t ds1302_interface_sclk_gpio_write(uint8_t value)
interface sclk gpio write
void ds1302_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t ds1302_interface_io_gpio_read(uint8_t *value)
interface io gpio read
uint8_t ds1302_interface_io_gpio_deinit(void)
interface io gpio deinit
uint8_t ds1302_interface_io_gpio_init(void)
interface io gpio init
void ds1302_interface_delay_us(uint32_t us)
interface delay us
void ds1302_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t ds1302_interface_sclk_gpio_init(void)
interface sclk gpio init
uint8_t ds1302_interface_ce_gpio_deinit(void)
interface ce gpio deinit
uint8_t ds1302_interface_ce_gpio_write(uint8_t value)
interface ce gpio write
ds1302_am_pm_t am_pm
ds1302_format_t format