LibDriver HMC5883L
Loading...
Searching...
No Matches
driver_hmc5883l_read_test.c
Go to the documentation of this file.
1
37
39
40static hmc5883l_handle_t gs_handle;
41
50uint8_t hmc5883l_read_test(uint32_t times)
51{
52 uint8_t res;
53 uint32_t i;
54 int16_t raw[3];
55 float m_gauss[3];
56 hmc5883l_info_t info;
57
58 /* link interface function */
66
67 /* get hmc5883l info */
68 res = hmc5883l_info(&info);
69 if (res != 0)
70 {
71 hmc5883l_interface_debug_print("hmc5883l: get info failed.\n");
72
73 return 1;
74 }
75 else
76 {
77 /* print chip information */
78 hmc5883l_interface_debug_print("hmc5883l: chip is %s.\n", info.chip_name);
79 hmc5883l_interface_debug_print("hmc5883l: manufacturer is %s.\n", info.manufacturer_name);
80 hmc5883l_interface_debug_print("hmc5883l: interface is %s.\n", info.interface);
81 hmc5883l_interface_debug_print("hmc5883l: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
82 hmc5883l_interface_debug_print("hmc5883l: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
83 hmc5883l_interface_debug_print("hmc5883l: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
84 hmc5883l_interface_debug_print("hmc5883l: max current is %0.2fmA.\n", info.max_current_ma);
85 hmc5883l_interface_debug_print("hmc5883l: max temperature is %0.1fC.\n", info.temperature_max);
86 hmc5883l_interface_debug_print("hmc5883l: min temperature is %0.1fC.\n", info.temperature_min);
87 }
88
89 /* hmc5883l init */
90 res = hmc5883l_init(&gs_handle);
91 if (res != 0)
92 {
93 hmc5883l_interface_debug_print("hmc5883l: init failed.\n");
94
95 return 1;
96 }
97
98 /* set average sample 8 */
100 if (res != 0)
101 {
102 hmc5883l_interface_debug_print("hmc5883l: set average sample failed.\n");
103 (void)hmc5883l_deinit(&gs_handle);
104
105 return 1;
106 }
107
108 /* set 75 data output rate */
110 if (res != 0)
111 {
112 hmc5883l_interface_debug_print("hmc5883l: set data output rate failed.\n");
113 (void)hmc5883l_deinit(&gs_handle);
114
115 return 1;
116 }
117
118 /* set normal mode */
119 res = hmc5883l_set_mode(&gs_handle, HMC5883L_MODE_NORMAL);
120 if (res != 0)
121 {
122 hmc5883l_interface_debug_print("hmc5883l: set mode failed.\n");
123 (void)hmc5883l_deinit(&gs_handle);
124
125 return 1;
126 }
127
128 /* set 820 gain */
129 res = hmc5883l_set_gain(&gs_handle, HMC5883L_GAIN_820);
130 if (res != 0)
131 {
132 hmc5883l_interface_debug_print("hmc5883l: set gain failed.\n");
133 (void)hmc5883l_deinit(&gs_handle);
134
135 return 1;
136 }
137
138 /* enable high speed iic */
139 res = hmc5883l_enable_high_speed_iic(&gs_handle);
140 if (res != 0)
141 {
142 hmc5883l_interface_debug_print("hmc5883l: enable high speed iic failed.\n");
143 (void)hmc5883l_deinit(&gs_handle);
144
145 return 1;
146 }
147
148 /* start read test */
149 hmc5883l_interface_debug_print("hmc5883l: start read test.\n");
150
151 /* single read test */
152 hmc5883l_interface_debug_print("hmc5883l: single read test.\n");
153 for (i=0; i<times; i++)
154 {
155 /* delay 1 s*/
157
158 /* single read */
159 res = hmc5883l_single_read(&gs_handle, (int16_t *)raw, m_gauss);
160 if (res != 0)
161 {
162 hmc5883l_interface_debug_print("hmc5883l: single read failed.\n");
163 (void)hmc5883l_deinit(&gs_handle);
164
165 return 1;
166 }
167
168 /* print result */
169 hmc5883l_interface_debug_print("hmc5883l: single read x %.1f.\n", m_gauss[0]);
170 hmc5883l_interface_debug_print("hmc5883l: single read y %.1f.\n", m_gauss[1]);
171 hmc5883l_interface_debug_print("hmc5883l: single read z %.1f.\n", m_gauss[2]);
172 }
173
174 /* continuous read test */
175 hmc5883l_interface_debug_print("hmc5883l: continuous read test.\n");
176
177 /* start continuous read */
178 res = hmc5883l_start_continuous_read(&gs_handle);
179 if (res != 0)
180 {
181 hmc5883l_interface_debug_print("hmc5883l: start continuous read failed.\n");
182 (void)hmc5883l_deinit(&gs_handle);
183
184 return 1;
185 }
186 for (i=0; i<times; i++)
187 {
188 /* delay 1 s*/
190
191 /* single read */
192 res = hmc5883l_continuous_read(&gs_handle, (int16_t *)raw, m_gauss);
193 if (res != 0)
194 {
195 hmc5883l_interface_debug_print("hmc5883l: continuous read failed.\n");
196 (void)hmc5883l_deinit(&gs_handle);
197
198 return 1;
199 }
200
201 /* print result */
202 hmc5883l_interface_debug_print("hmc5883l: continuous read x %.1f.\n", m_gauss[0]);
203 hmc5883l_interface_debug_print("hmc5883l: continuous read y %.1f.\n", m_gauss[1]);
204 hmc5883l_interface_debug_print("hmc5883l: continuous read z %.1f.\n", m_gauss[2]);
205 }
206
207 /* stop continuous read */
208 res = hmc5883l_stop_continuous_read(&gs_handle);
209 if (res != 0)
210 {
211 hmc5883l_interface_debug_print("hmc5883l: stop continuous read failed.\n");
212 (void)hmc5883l_deinit(&gs_handle);
213
214 return 1;
215 }
216
217 /* gain test */
218 hmc5883l_interface_debug_print("hmc5883l: gain test.\n");
219
220 /* set gain 1370 */
221 res = hmc5883l_set_gain(&gs_handle, HMC5883L_GAIN_1370);
222 if (res != 0)
223 {
224 hmc5883l_interface_debug_print("hmc5883l: set gain failed.\n");
225 (void)hmc5883l_deinit(&gs_handle);
226
227 return 1;
228 }
229 hmc5883l_interface_debug_print("hmc5883l: set gain 1370.\n");
230 for (i=0; i<times; i++)
231 {
232 /* delay 1 s*/
234
235 /* single read */
236 res = hmc5883l_single_read(&gs_handle, (int16_t *)raw, m_gauss);
237 if (res != 0)
238 {
239 hmc5883l_interface_debug_print("hmc5883l: single read failed.\n");
240 (void)hmc5883l_deinit(&gs_handle);
241
242 return 1;
243 }
244
245 /* print result*/
246 hmc5883l_interface_debug_print("hmc5883l: single read x %.1f.\n", m_gauss[0]);
247 hmc5883l_interface_debug_print("hmc5883l: single read y %.1f.\n", m_gauss[1]);
248 hmc5883l_interface_debug_print("hmc5883l: single read z %.1f.\n", m_gauss[2]);
249 }
250
251 /* set gain 1090 */
252 res = hmc5883l_set_gain(&gs_handle, HMC5883L_GAIN_1090);
253 if (res != 0)
254 {
255 hmc5883l_interface_debug_print("hmc5883l: set gain failed.\n");
256 (void)hmc5883l_deinit(&gs_handle);
257
258 return 1;
259 }
260 hmc5883l_interface_debug_print("hmc5883l: set gain 1090.\n");
261 for (i=0; i<times; i++)
262 {
263 /* delay 1 s*/
265
266 /* single read */
267 res = hmc5883l_single_read(&gs_handle, (int16_t *)raw, m_gauss);
268 if (res != 0)
269 {
270 hmc5883l_interface_debug_print("hmc5883l: single read failed.\n");
271 (void)hmc5883l_deinit(&gs_handle);
272
273 return 1;
274 }
275
276 /* print result */
277 hmc5883l_interface_debug_print("hmc5883l: single read x %.1f.\n", m_gauss[0]);
278 hmc5883l_interface_debug_print("hmc5883l: single read y %.1f.\n", m_gauss[1]);
279 hmc5883l_interface_debug_print("hmc5883l: single read z %.1f.\n", m_gauss[2]);
280 }
281
282 /* set gain 820 */
283 res = hmc5883l_set_gain(&gs_handle, HMC5883L_GAIN_820);
284 if (res != 0)
285 {
286 hmc5883l_interface_debug_print("hmc5883l: set gain failed.\n");
287 (void)hmc5883l_deinit(&gs_handle);
288
289 return 1;
290 }
291 hmc5883l_interface_debug_print("hmc5883l: set gain 820.\n");
292 for (i=0; i<times; i++)
293 {
294 /* delay 1 s*/
296
297 /* single read */
298 res = hmc5883l_single_read(&gs_handle, (int16_t *)raw, m_gauss);
299 if (res != 0)
300 {
301 hmc5883l_interface_debug_print("hmc5883l: single read failed.\n");
302 (void)hmc5883l_deinit(&gs_handle);
303
304 return 1;
305 }
306
307 /* print result */
308 hmc5883l_interface_debug_print("hmc5883l: single read x %.1f.\n", m_gauss[0]);
309 hmc5883l_interface_debug_print("hmc5883l: single read y %.1f.\n", m_gauss[1]);
310 hmc5883l_interface_debug_print("hmc5883l: single read z %.1f.\n", m_gauss[2]);
311 }
312
313 /* set gain 660 */
314 res = hmc5883l_set_gain(&gs_handle, HMC5883L_GAIN_660);
315 if (res != 0)
316 {
317 hmc5883l_interface_debug_print("hmc5883l: set gain failed.\n");
318 (void)hmc5883l_deinit(&gs_handle);
319
320 return 1;
321 }
322 hmc5883l_interface_debug_print("hmc5883l: set gain 660.\n");
323 for (i=0; i<times; i++)
324 {
325 /* delay 1 s */
327
328 /* single read */
329 res = hmc5883l_single_read(&gs_handle, (int16_t *)raw, m_gauss);
330 if (res != 0)
331 {
332 hmc5883l_interface_debug_print("hmc5883l: single read failed.\n");
333 (void)hmc5883l_deinit(&gs_handle);
334
335 return 1;
336 }
337
338 /* print result */
339 hmc5883l_interface_debug_print("hmc5883l: single read x %.1f.\n", m_gauss[0]);
340 hmc5883l_interface_debug_print("hmc5883l: single read y %.1f.\n", m_gauss[1]);
341 hmc5883l_interface_debug_print("hmc5883l: single read z %.1f.\n", m_gauss[2]);
342 }
343
344 /* set gain 440 */
345 res = hmc5883l_set_gain(&gs_handle, HMC5883L_GAIN_440);
346 if (res != 0)
347 {
348 hmc5883l_interface_debug_print("hmc5883l: set gain failed.\n");
349 (void)hmc5883l_deinit(&gs_handle);
350
351 return 1;
352 }
353 hmc5883l_interface_debug_print("hmc5883l: set gain 440.\n");
354 for (i=0; i<times; i++)
355 {
356 /* delay 1 s */
358
359 /* single read */
360 res = hmc5883l_single_read(&gs_handle, (int16_t *)raw, m_gauss);
361 if (res != 0)
362 {
363 hmc5883l_interface_debug_print("hmc5883l: single read failed.\n");
364 (void)hmc5883l_deinit(&gs_handle);
365
366 return 1;
367 }
368
369 /* print result */
370 hmc5883l_interface_debug_print("hmc5883l: single read x %.1f.\n", m_gauss[0]);
371 hmc5883l_interface_debug_print("hmc5883l: single read y %.1f.\n", m_gauss[1]);
372 hmc5883l_interface_debug_print("hmc5883l: single read z %.1f.\n", m_gauss[2]);
373 }
374
375 /* set gain 390 */
376 res = hmc5883l_set_gain(&gs_handle, HMC5883L_GAIN_390);
377 if (res != 0)
378 {
379 hmc5883l_interface_debug_print("hmc5883l: set gain failed.\n");
380 (void)hmc5883l_deinit(&gs_handle);
381
382 return 1;
383 }
384 hmc5883l_interface_debug_print("hmc5883l: set gain 390.\n");
385 for (i=0; i<times; i++)
386 {
387 /* delay 1 s */
389
390 /* single read */
391 res = hmc5883l_single_read(&gs_handle, (int16_t *)raw, m_gauss);
392 if (res != 0)
393 {
394 hmc5883l_interface_debug_print("hmc5883l: single read failed.\n");
395 (void)hmc5883l_deinit(&gs_handle);
396
397 return 1;
398 }
399
400 /* print result */
401 hmc5883l_interface_debug_print("hmc5883l: single read x %.1f.\n", m_gauss[0]);
402 hmc5883l_interface_debug_print("hmc5883l: single read y %.1f.\n", m_gauss[1]);
403 hmc5883l_interface_debug_print("hmc5883l: single read z %.1f.\n", m_gauss[2]);
404 }
405
406 /* set gain 330 */
407 res = hmc5883l_set_gain(&gs_handle, HMC5883L_GAIN_330);
408 if (res != 0)
409 {
410 hmc5883l_interface_debug_print("hmc5883l: set gain failed.\n");
411 (void)hmc5883l_deinit(&gs_handle);
412
413 return 1;
414 }
415 hmc5883l_interface_debug_print("hmc5883l: set gain 330.\n");
416 for (i=0; i<times; i++)
417 {
418 /* delay 1 s */
420
421 /* single read */
422 res = hmc5883l_single_read(&gs_handle, (int16_t *)raw, m_gauss);
423 if (res != 0)
424 {
425 hmc5883l_interface_debug_print("hmc5883l: single read failed.\n");
426 (void)hmc5883l_deinit(&gs_handle);
427
428 return 1;
429 }
430
431 /* print result */
432 hmc5883l_interface_debug_print("hmc5883l: single read x %.1f.\n", m_gauss[0]);
433 hmc5883l_interface_debug_print("hmc5883l: single read y %.1f.\n", m_gauss[1]);
434 hmc5883l_interface_debug_print("hmc5883l: single read z %.1f.\n", m_gauss[2]);
435 }
436
437 /* set gain 230 */
438 res = hmc5883l_set_gain(&gs_handle, HMC5883L_GAIN_230);
439 if (res != 0)
440 {
441 hmc5883l_interface_debug_print("hmc5883l: set gain failed.\n");
442 (void)hmc5883l_deinit(&gs_handle);
443
444 return 1;
445 }
446 hmc5883l_interface_debug_print("hmc5883l: set gain 230.\n");
447 for (i=0; i<times; i++)
448 {
449 /* delay 1 s */
451
452 /* single read */
453 res = hmc5883l_single_read(&gs_handle, (int16_t *)raw, m_gauss);
454 if (res != 0)
455 {
456 hmc5883l_interface_debug_print("hmc5883l: single read failed.\n");
457 (void)hmc5883l_deinit(&gs_handle);
458
459 return 1;
460 }
461
462 /* print result */
463 hmc5883l_interface_debug_print("hmc5883l: single read x %.1f.\n", m_gauss[0]);
464 hmc5883l_interface_debug_print("hmc5883l: single read y %.1f.\n", m_gauss[1]);
465 hmc5883l_interface_debug_print("hmc5883l: single read z %.1f.\n", m_gauss[2]);
466 }
467
468 /* finish read test */
469 hmc5883l_interface_debug_print("hmc5883l: finish read test.\n");
470 (void)hmc5883l_deinit(&gs_handle);
471
472 return 0;
473}
driver hmc5883l read test header file
uint8_t hmc5883l_set_mode(hmc5883l_handle_t *handle, hmc5883l_mode_t mode)
set the chip mode
uint8_t hmc5883l_set_gain(hmc5883l_handle_t *handle, hmc5883l_gain_t gain)
set the chip gain
uint8_t hmc5883l_set_average_sample(hmc5883l_handle_t *handle, hmc5883l_average_sample_t average_sample)
set the average sample rate
uint8_t hmc5883l_init(hmc5883l_handle_t *handle)
initialize the chip
uint8_t hmc5883l_continuous_read(hmc5883l_handle_t *handle, int16_t raw[3], float m_gauss[3])
read data continuously
uint8_t hmc5883l_set_data_output_rate(hmc5883l_handle_t *handle, hmc5883l_data_output_rate_t data_rate)
set the data output rate
uint8_t hmc5883l_info(hmc5883l_info_t *info)
get chip's information
uint8_t hmc5883l_deinit(hmc5883l_handle_t *handle)
close the chip
struct hmc5883l_handle_s hmc5883l_handle_t
hmc5883l handle structure definition
struct hmc5883l_info_s hmc5883l_info_t
hmc5883l information structure definition
uint8_t hmc5883l_single_read(hmc5883l_handle_t *handle, int16_t raw[3], float m_gauss[3])
read data once
uint8_t hmc5883l_start_continuous_read(hmc5883l_handle_t *handle)
start reading data
uint8_t hmc5883l_enable_high_speed_iic(hmc5883l_handle_t *handle)
enable the high speed iic
uint8_t hmc5883l_stop_continuous_read(hmc5883l_handle_t *handle)
stop reading data
@ HMC5883L_AVERAGE_SAMPLE_8
@ HMC5883L_MODE_NORMAL
@ HMC5883L_GAIN_390
@ HMC5883L_GAIN_330
@ HMC5883L_GAIN_1090
@ HMC5883L_GAIN_1370
@ HMC5883L_GAIN_820
@ HMC5883L_GAIN_230
@ HMC5883L_GAIN_440
@ HMC5883L_GAIN_660
@ HMC5883L_DATA_OUTPUT_RATE_75
uint8_t hmc5883l_interface_iic_deinit(void)
interface iic bus deinit
uint8_t hmc5883l_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
void hmc5883l_interface_debug_print(const char *const fmt,...)
interface print format data
void hmc5883l_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t hmc5883l_interface_iic_init(void)
interface iic bus init
uint8_t hmc5883l_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t hmc5883l_read_test(uint32_t times)
read test
char manufacturer_name[32]