LibDriver DS3231
Loading...
Searching...
No Matches
driver_ds3231_basic.c
Go to the documentation of this file.
1
37
38#include "driver_ds3231_basic.h"
39
40static ds3231_handle_t gs_handle;
41static int8_t gs_time_zone = 0;
42
50uint8_t ds3231_basic_init(void)
51{
52 uint8_t res;
53 int8_t reg;
54
55 /* link functions */
64
65 /* init ds3231 */
66 res = ds3231_init(&gs_handle);
67 if (res != 0)
68 {
69 ds3231_interface_debug_print("ds3231: init failed.\n");
70
71 return 1;
72 }
73
74 /* set oscillator */
75 res = ds3231_set_oscillator(&gs_handle, DS3231_BOOL_TRUE);
76 if (res != 0)
77 {
78 ds3231_interface_debug_print("ds3231: set oscillator failed.\n");
79 (void)ds3231_deinit(&gs_handle);
80
81 return 1;
82 }
83
84 /* disable alarm1 */
86 if (res != 0)
87 {
88 ds3231_interface_debug_print("ds3231: set alarm1 interrupt failed.\n");
89 (void)ds3231_deinit(&gs_handle);
90
91 return 1;
92 }
93
94 /* disable alarm2 */
96 if (res != 0)
97 {
98 ds3231_interface_debug_print("ds3231: set alarm2 interrupt failed.\n");
99 (void)ds3231_deinit(&gs_handle);
100
101 return 1;
102 }
103
104 /* set square wave */
105 res = ds3231_set_pin(&gs_handle, DS3231_PIN_SQUARE_WAVE);
106 if (res != 0)
107 {
108 ds3231_interface_debug_print("ds3231: set pin failed.\n");
109 (void)ds3231_deinit(&gs_handle);
110
111 return 1;
112 }
113
114 /* disable square wave */
115 res = ds3231_set_square_wave(&gs_handle, DS3231_BOOL_FALSE);
116 if (res != 0)
117 {
118 ds3231_interface_debug_print("ds3231: set square wave failed.\n");
119 (void)ds3231_deinit(&gs_handle);
120
121 return 1;
122 }
123
124 /* disable 32khz output */
126 if (res != 0)
127 {
128 ds3231_interface_debug_print("ds3231: set 32khz output failed.\n");
129 (void)ds3231_deinit(&gs_handle);
130
131 return 1;
132 }
133
134 /* convert to register */
136 if (res != 0)
137 {
138 ds3231_interface_debug_print("ds3231: convert to register failed.\n");
139 (void)ds3231_deinit(&gs_handle);
140
141 return 1;
142 }
143
144 /* set aging offset */
145 res = ds3231_set_aging_offset(&gs_handle, reg);
146 if (res != 0)
147 {
148 ds3231_interface_debug_print("ds3231: set aging offset failed.\n");
149 (void)ds3231_deinit(&gs_handle);
150
151 return 1;
152 }
153
154 return 0;
155}
156
165{
166 if (ds3231_deinit(&gs_handle) != 0)
167 {
168 return 1;
169 }
170 else
171 {
172 return 0;
173 }
174}
175
185{
186 /* set time */
187 if (ds3231_set_time(&gs_handle, t) != 0)
188 {
189 return 1;
190 }
191 else
192 {
193 return 0;
194 }
195}
196
205uint8_t ds3231_basic_set_timestamp(time_t timestamp)
206{
208 struct tm *timeptr;
209
210 /* convert times */
211 timestamp += (time_t)(gs_time_zone * 3600);
212 timeptr = localtime(&timestamp);
213 t.am_pm = DS3231_AM;
214 t.date = (uint8_t)timeptr->tm_mday;
216 t.hour = (uint8_t)timeptr->tm_hour;
217 t.minute = (uint8_t)timeptr->tm_min;
218 t.month = (uint8_t)timeptr->tm_mon + 1;
219 t.second = (uint8_t)timeptr->tm_sec;
220 if (timeptr->tm_wday == 0)
221 {
222 t.week = 7;
223 }
224 else
225 {
226 t.week = (uint8_t)timeptr->tm_wday;
227 }
228 t.year = (uint16_t)(timeptr->tm_year + 1900);
229
230 /* set time */
231 if (ds3231_set_time(&gs_handle, &t) != 0)
232 {
233 return 1;
234 }
235 else
236 {
237 return 0;
238 }
239}
240
249{
250 gs_time_zone = zone;
251
252 return 0;
253}
254
264{
265 /* get time */
266 if (ds3231_get_time(&gs_handle, t) != 0)
267 {
268 return 1;
269 }
270 else
271 {
272 return 0;
273 }
274}
275
284uint8_t ds3231_basic_get_timestamp(time_t *timestamp)
285{
287 struct tm timeptr;
288
289 /* get time */
290 if (ds3231_get_time(&gs_handle, &t) != 0)
291 {
292 return 1;
293 }
294 timeptr.tm_year = t.year - 1900;
295 timeptr.tm_mon = t.month - 1;
296 timeptr.tm_wday = t.week;
297 timeptr.tm_mday = t.date;
298 if (t.format == DS3231_FORMAT_24H)
299 {
300 timeptr.tm_hour = t.hour;
301 }
302 else
303 {
304 timeptr.tm_hour = t.hour % 12 + t.am_pm * 12;
305 }
306 timeptr.tm_min = t.minute;
307 timeptr.tm_sec = t.second;
308
309 /* make time */
310 *timestamp = mktime(&timeptr) - gs_time_zone * 3600;
311
312 return 0;
313}
314
323{
324 *zone = gs_time_zone;
325
326 return 0;
327}
328
338uint8_t ds3231_basic_get_temperature(int16_t *raw, float *s)
339{
340 /* get temperature */
341 if (ds3231_get_temperature(&gs_handle, raw, s) != 0)
342 {
343 return 1;
344 }
345 else
346 {
347 return 0;
348 }
349}
350
360uint8_t ds3231_basic_get_ascii_time(char *buf, uint8_t len)
361{
363
364 /* get time */
365 if (ds3231_get_time(&gs_handle, &t) != 0)
366 {
367 return 1;
368 }
369
370 if (t.format == DS3231_FORMAT_24H)
371 {
372 (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);
373 }
374 else
375 {
376 (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",
377 t.hour, t.minute, t.second, t.week
378 );
379 }
380
381 return 0;
382}
driver ds3231 basic 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
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
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_ALARM_2
@ DS3231_ALARM_1
@ DS3231_FORMAT_24H
@ DS3231_PIN_SQUARE_WAVE
@ DS3231_BOOL_TRUE
@ DS3231_BOOL_FALSE
uint8_t ds3231_basic_get_temperature(int16_t *raw, float *s)
basic example get the current temperature
uint8_t ds3231_basic_set_time(ds3231_time_t *t)
basic example set the time
uint8_t ds3231_basic_get_timestamp_time_zone(int8_t *zone)
basic example get the local time zone
uint8_t ds3231_basic_set_timestamp(time_t timestamp)
basic example set the time by a unix timestamp
uint8_t ds3231_basic_set_timestamp_time_zone(int8_t zone)
basic example set the local time zone
uint8_t ds3231_basic_init(void)
basic example init
#define DS3231_BASIC_DEFAULT_AGING_OFFSET
ds3231 basic example default definition
uint8_t ds3231_basic_get_time(ds3231_time_t *t)
basic example get the time
uint8_t ds3231_basic_get_ascii_time(char *buf, uint8_t len)
basic example get the ascii time
uint8_t ds3231_basic_get_timestamp(time_t *timestamp)
basic example get the time in a unix timestamp
uint8_t ds3231_basic_deinit(void)
basic 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
void ds3231_interface_receive_callback(uint8_t type)
interface receive callback
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