LibDriver DS3231
Loading...
Searching...
No Matches
driver_ds3231_readwrite_test.c
Go to the documentation of this file.
1
37
39#include <stdlib.h>
40
41static ds3231_handle_t gs_handle;
42
51uint8_t ds3231_readwrite_test(uint32_t times)
52{
53 uint8_t res;
54 int8_t reg;
55 uint32_t i;
56 int16_t raw;
57 float s;
58 ds3231_info_t info;
59 ds3231_time_t time_in, time_out;
60
61 /* link functions */
70
71 /* get ds3231 info */
72 res = ds3231_info(&info);
73 if (res != 0)
74 {
75 ds3231_interface_debug_print("ds3231: get info failed.\n");
76
77 return 1;
78 }
79 else
80 {
81 /* print ds3231 info */
82 ds3231_interface_debug_print("ds3231: chip is %s.\n", info.chip_name);
83 ds3231_interface_debug_print("ds3231: manufacturer is %s.\n", info.manufacturer_name);
84 ds3231_interface_debug_print("ds3231: interface is %s.\n", info.interface);
85 ds3231_interface_debug_print("ds3231: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
86 ds3231_interface_debug_print("ds3231: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
87 ds3231_interface_debug_print("ds3231: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
88 ds3231_interface_debug_print("ds3231: max current is %0.2fmA.\n", info.max_current_ma);
89 ds3231_interface_debug_print("ds3231: max temperature is %0.1fC.\n", info.temperature_max);
90 ds3231_interface_debug_print("ds3231: min temperature is %0.1fC.\n", info.temperature_min);
91 }
92
93 /* start readwrite test */
94 ds3231_interface_debug_print("ds3231: start readwrite test.\n");
95
96 /* init ds3231 */
97 res = ds3231_init(&gs_handle);
98 if (res != 0)
99 {
100 ds3231_interface_debug_print("ds3231: init failed.\n");
101
102 return 1;
103 }
104
105 /* set oscillator */
106 res = ds3231_set_oscillator(&gs_handle, DS3231_BOOL_TRUE);
107 if (res != 0)
108 {
109 ds3231_interface_debug_print("ds3231: set oscillator failed.\n");
110 (void)ds3231_deinit(&gs_handle);
111
112 return 1;
113 }
114
115 /* disable alarm1 */
117 if (res != 0)
118 {
119 ds3231_interface_debug_print("ds3231: set alarm1 interrupt failed.\n");
120 (void)ds3231_deinit(&gs_handle);
121
122 return 1;
123 }
124
125 /* disable alarm2 */
127 if (res != 0)
128 {
129 ds3231_interface_debug_print("ds3231: set alarm2 interrupt failed.\n");
130 (void)ds3231_deinit(&gs_handle);
131
132 return 1;
133 }
134
135 /* set square wave */
136 res = ds3231_set_pin(&gs_handle, DS3231_PIN_SQUARE_WAVE);
137 if (res != 0)
138 {
139 ds3231_interface_debug_print("ds3231: set pin failed.\n");
140 (void)ds3231_deinit(&gs_handle);
141
142 return 1;
143 }
144
145 /* disable square wave */
146 res = ds3231_set_square_wave(&gs_handle, DS3231_BOOL_FALSE);
147 if (res != 0)
148 {
149 ds3231_interface_debug_print("ds3231: set square wave failed.\n");
150 (void)ds3231_deinit(&gs_handle);
151
152 return 1;
153 }
154
155 /* disable 32khz output */
157 if (res != 0)
158 {
159 ds3231_interface_debug_print("ds3231: set 32khz output failed.\n");
160 (void)ds3231_deinit(&gs_handle);
161
162 return 1;
163 }
164
165 /* convert to register */
166 res = ds3231_aging_offset_convert_to_register(&gs_handle, 0, (int8_t *)&reg);
167 if (res != 0)
168 {
169 ds3231_interface_debug_print("ds3231: convert to register failed.\n");
170 (void)ds3231_deinit(&gs_handle);
171
172 return 1;
173 }
174
175 /* set aging offset */
176 res = ds3231_set_aging_offset(&gs_handle, reg);
177 if (res != 0)
178 {
179 ds3231_interface_debug_print("ds3231: set aging offset failed.\n");
180 (void)ds3231_deinit(&gs_handle);
181
182 return 1;
183 }
184
185 /* ds3231_set_time/ds3231_get_time test */
186 ds3231_interface_debug_print("ds3231: ds3231_set_time/ds3231_get_time test.\n");
187
188 /* 12H format */
189 time_in.format = DS3231_FORMAT_12H;
190 time_in.am_pm = DS3231_PM;
191 time_in.year = rand() % 100 + 2000;
192 time_in.month = rand() % 12 + 1;
193 time_in.date = rand() % 20 + 1;
194 time_in.date = rand() % 20 + 1;
195 time_in.week = rand() % 7 + 1;
196 time_in.hour = rand() % 11 + 1;
197 time_in.minute = rand() % 60;
198 time_in.second = rand() % 60;
199 ds3231_interface_debug_print("ds3231: set time %04d-%02d-%02d PM %02d:%02d:%02d %d in 12 format.\n",
200 time_in.year, time_in.month, time_in.date,
201 time_in.hour, time_in.minute, time_in.second, time_in.week
202 );
203 res = ds3231_set_time(&gs_handle, &time_in);
204 if (res != 0)
205 {
206 ds3231_interface_debug_print("ds3231: set time failed.\n");
207 (void)ds3231_deinit(&gs_handle);
208
209 return 1;
210 }
211 for (i = 0; i < times; i++)
212 {
214 res = ds3231_get_time(&gs_handle, &time_out);
215 if (res != 0)
216 {
217 ds3231_interface_debug_print("ds3231: get time failed.\n");
218 (void)ds3231_deinit(&gs_handle);
219
220 return 1;
221 }
222 ds3231_interface_debug_print("ds3231: time is %04d-%02d-%02d PM %02d:%02d:%02d %d.\n",
223 time_out.year, time_out.month, time_out.date,
224 time_out.hour, time_out.minute, time_out.second, time_out.week
225 );
226 }
227
228 /* 24H format */
229 time_in.format = DS3231_FORMAT_24H;
230 time_in.am_pm = DS3231_AM;
231 time_in.year = rand() % 100 + 2090;
232 time_in.month = rand() % 12 + 1;
233 time_in.date = rand() % 20 + 1;
234 time_in.week = rand() % 7 + 1;
235 time_in.hour = rand() % 12 + 12;
236 time_in.minute = rand() % 60;
237 time_in.second = rand() % 60;
238 ds3231_interface_debug_print("ds3231: set time %04d-%02d-%02d %02d:%02d:%02d %d in 24 format.\n",
239 time_in.year, time_in.month, time_in.date,
240 time_in.hour, time_in.minute, time_in.second, time_in.week
241 );
242 res = ds3231_set_time(&gs_handle, &time_in);
243 if (res != 0)
244 {
245 ds3231_interface_debug_print("ds3231: set time failed.\n");
246 (void)ds3231_deinit(&gs_handle);
247
248 return 1;
249 }
250 for (i = 0; i < times; i++)
251 {
253 res = ds3231_get_time(&gs_handle, &time_out);
254 if (res != 0)
255 {
256 ds3231_interface_debug_print("ds3231: get time failed.\n");
257 (void)ds3231_deinit(&gs_handle);
258
259 return 1;
260 }
261 ds3231_interface_debug_print("ds3231: time is %04d-%02d-%02d %02d:%02d:%02d %d.\n",
262 time_out.year, time_out.month, time_out.date,
263 time_out.hour, time_out.minute, time_out.second, time_out.week
264 );
265 }
266 ds3231_interface_debug_print("ds3231: read temperature.\n");
267 for (i = 0; i < times; i++)
268 {
270 res = ds3231_get_temperature(&gs_handle, (int16_t *)&raw, (float *)&s);
271 if (res != 0)
272 {
273 ds3231_interface_debug_print("ds3231: get temperature failed.\n");
274 (void)ds3231_deinit(&gs_handle);
275
276 return 1;
277 }
278 ds3231_interface_debug_print("ds3231: temperature is %0.2f.\n", s);
279 }
280
281 /* finish readwrite test */
282 ds3231_interface_debug_print("ds3231: finish readwrite test.\n");
283 (void)ds3231_deinit(&gs_handle);
284
285 return 0;
286}
driver ds3231 readwrite test 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
uint8_t ds3231_info(ds3231_info_t *info)
get chip's information
struct ds3231_handle_s ds3231_handle_t
ds3231 handle structure definition
uint8_t ds3231_init(ds3231_handle_t *handle)
initialize the chip
struct ds3231_info_s ds3231_info_t
ds3231 information structure definition
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_PM
@ DS3231_AM
@ DS3231_ALARM_2
@ DS3231_ALARM_1
@ DS3231_FORMAT_24H
@ DS3231_FORMAT_12H
@ DS3231_PIN_SQUARE_WAVE
@ DS3231_BOOL_TRUE
@ DS3231_BOOL_FALSE
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
uint8_t ds3231_readwrite_test(uint32_t times)
readwrite test
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]
ds3231_am_pm_t am_pm
ds3231_format_t format