LibDriver DS1307
Loading...
Searching...
No Matches
driver_ds1307_basic.c
Go to the documentation of this file.
1
36
37#include "driver_ds1307_basic.h"
38
39static ds1307_handle_t gs_handle;
40static int8_t gs_time_zone = 0;
41
49uint8_t ds1307_basic_init(void)
50{
51 uint8_t res;
52
53 /* link functions */
61
62 /* init ds1307 */
63 res = ds1307_init(&gs_handle);
64 if (res != 0)
65 {
66 ds1307_interface_debug_print("ds1307: init failed.\n");
67
68 return 1;
69 }
70
71 /* set oscillator */
72 res = ds1307_set_oscillator(&gs_handle, DS1307_BOOL_TRUE);
73 if (res != 0)
74 {
75 ds1307_interface_debug_print("ds1307: set oscillator failed.\n");
76 (void)ds1307_deinit(&gs_handle);
77
78 return 1;
79 }
80
81 /* set output mode */
83 if (res != 0)
84 {
85 ds1307_interface_debug_print("ds1307: set output mode failed.\n");
86 (void)ds1307_deinit(&gs_handle);
87
88 return 1;
89 }
90
91 return 0;
92}
93
102{
103 if (ds1307_deinit(&gs_handle) != 0)
104 {
105 return 1;
106 }
107
108 return 0;
109}
110
120{
121 /* set time */
122 if (ds1307_set_time(&gs_handle, t) != 0)
123 {
124 return 1;
125 }
126
127 return 0;
128}
129
138uint8_t ds1307_basic_set_timestamp(time_t timestamp)
139{
141 struct tm *timeptr;
142
143 /* convert times */
144 timestamp += (time_t)(gs_time_zone * 3600);
145 timeptr = localtime(&timestamp);
146 t.am_pm = DS1307_AM;
147 t.date = (uint8_t)timeptr->tm_mday;
149 t.hour = (uint8_t)timeptr->tm_hour;
150 t.minute = (uint8_t)timeptr->tm_min;
151 t.month = (uint8_t)timeptr->tm_mon + 1;
152 t.second = (uint8_t)timeptr->tm_sec;
153 if (timeptr->tm_wday == 0)
154 {
155 t.week = 7;
156 }
157 else
158 {
159 t.week = (uint8_t)timeptr->tm_wday;
160 }
161 t.year = (uint16_t)(timeptr->tm_year + 1900);
162
163 /* set time */
164 if (ds1307_set_time(&gs_handle, &t) != 0)
165 {
166 return 1;
167 }
168
169 return 0;
170}
171
180{
181 gs_time_zone = zone;
182
183 return 0;
184}
185
195{
196 /* get time */
197 if (ds1307_get_time(&gs_handle, t) != 0)
198 {
199 return 1;
200 }
201
202 return 0;
203}
204
213uint8_t ds1307_basic_get_timestamp(time_t *timestamp)
214{
216 struct tm timeptr;
217
218 /* get time */
219 if (ds1307_get_time(&gs_handle, &t) != 0)
220 {
221 return 1;
222 }
223 timeptr.tm_year = t.year - 1900;
224 timeptr.tm_mon = t.month - 1;
225 timeptr.tm_wday = t.week;
226 timeptr.tm_mday = t.date;
227 if (t.format == DS1307_FORMAT_24H)
228 {
229 timeptr.tm_hour = t.hour;
230 }
231 else
232 {
233 timeptr.tm_hour = t.hour % 12 + t.am_pm * 12;
234 }
235 timeptr.tm_min = t.minute;
236 timeptr.tm_sec = t.second;
237
238 /* make time */
239 *timestamp = mktime(&timeptr) - gs_time_zone * 3600;
240
241 return 0;
242}
243
252{
253 *zone = gs_time_zone;
254
255 return 0;
256}
257
267uint8_t ds1307_basic_get_ascii_time(char *buf, uint8_t len)
268{
270
271 /* get time */
272 if (ds1307_get_time(&gs_handle, &t) != 0)
273 {
274 return 1;
275 }
276
277 if (t.format == DS1307_FORMAT_24H)
278 {
279 (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);
280 }
281 else
282 {
283 (void)snprintf(buf, len, "%04d-%02d-%02d %s %02d:%02d:%02d %d.\n", t.year, t.month, t.date, (t.am_pm == DS1307_AM) ? "AM" : "PM",
284 t.hour, t.minute, t.second, t.week
285 );
286 }
287
288 return 0;
289}
290
301uint8_t ds1307_basic_read_ram(uint8_t addr, uint8_t *buf, uint8_t len)
302{
303 if (ds1307_read_ram(&gs_handle, addr, buf, len) != 0)
304 {
305 return 1;
306 }
307
308 return 0;
309}
310
321uint8_t ds1307_basic_write_ram(uint8_t addr, uint8_t *buf, uint8_t len)
322{
323 if (ds1307_write_ram(&gs_handle, addr, buf, len) != 0)
324 {
325 return 1;
326 }
327
328 return 0;
329}
driver ds1307 basic header file
struct ds1307_handle_s ds1307_handle_t
ds1307 handle structure definition
uint8_t ds1307_read_ram(ds1307_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
read ram
uint8_t ds1307_set_output_mode(ds1307_handle_t *handle, ds1307_output_mode_t mode)
set the output mode
struct ds1307_time_s ds1307_time_t
ds1307 time structure definition
uint8_t ds1307_set_oscillator(ds1307_handle_t *handle, ds1307_bool_t enable)
enable or disable the oscillator
uint8_t ds1307_init(ds1307_handle_t *handle)
initialize the chip
uint8_t ds1307_set_time(ds1307_handle_t *handle, ds1307_time_t *t)
set the current time
uint8_t ds1307_write_ram(ds1307_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
write ram
uint8_t ds1307_deinit(ds1307_handle_t *handle)
close the chip
uint8_t ds1307_get_time(ds1307_handle_t *handle, ds1307_time_t *t)
get the current time
@ DS1307_AM
@ DS1307_FORMAT_24H
@ DS1307_BOOL_TRUE
@ DS1307_OUTPUT_MODE_LEVEL
uint8_t ds1307_basic_write_ram(uint8_t addr, uint8_t *buf, uint8_t len)
basic example write ram
uint8_t ds1307_basic_set_time(ds1307_time_t *t)
basic example set the time
uint8_t ds1307_basic_init(void)
basic example init
uint8_t ds1307_basic_get_ascii_time(char *buf, uint8_t len)
basic example get the ascii time
uint8_t ds1307_basic_read_ram(uint8_t addr, uint8_t *buf, uint8_t len)
basic example read ram
uint8_t ds1307_basic_get_timestamp(time_t *timestamp)
basic example get the time in a unix timestamp
uint8_t ds1307_basic_deinit(void)
basic example deinit
uint8_t ds1307_basic_set_timestamp_time_zone(int8_t zone)
basic example set the local time zone
uint8_t ds1307_basic_set_timestamp(time_t timestamp)
basic example set the time by a unix timestamp
uint8_t ds1307_basic_get_timestamp_time_zone(int8_t *zone)
basic example get the local time zone
uint8_t ds1307_basic_get_time(ds1307_time_t *t)
basic example get the time
void ds1307_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t ds1307_interface_iic_deinit(void)
interface iic bus deinit
uint8_t ds1307_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t ds1307_interface_iic_init(void)
interface iic bus init
uint8_t ds1307_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
void ds1307_interface_delay_ms(uint32_t ms)
interface delay ms
ds1307_am_pm_t am_pm
ds1307_format_t format