LibDriver MAG3110
Loading...
Searching...
No Matches
driver_mag3110_read_test.c
Go to the documentation of this file.
1
36
38
39static mag3110_handle_t gs_handle;
40
49uint8_t mag3110_read_test(uint32_t times)
50{
51 uint8_t res;
52 int8_t raw_s;
53 uint32_t i;
54 float degree;
55 int16_t raw[3];
56 float ut[3];
57 mag3110_info_t info;
58
59 /* link interface function */
67
68 /* get mag3110 info */
69 res = mag3110_info(&info);
70 if (res != 0)
71 {
72 mag3110_interface_debug_print("mag3110: get info failed.\n");
73
74 return 1;
75 }
76 else
77 {
78 /* print chip information */
79 mag3110_interface_debug_print("mag3110: chip is %s.\n", info.chip_name);
80 mag3110_interface_debug_print("mag3110: manufacturer is %s.\n", info.manufacturer_name);
81 mag3110_interface_debug_print("mag3110: interface is %s.\n", info.interface);
82 mag3110_interface_debug_print("mag3110: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
83 mag3110_interface_debug_print("mag3110: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
84 mag3110_interface_debug_print("mag3110: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
85 mag3110_interface_debug_print("mag3110: max current is %0.2fmA.\n", info.max_current_ma);
86 mag3110_interface_debug_print("mag3110: max temperature is %0.1fC.\n", info.temperature_max);
87 mag3110_interface_debug_print("mag3110: min temperature is %0.1fC.\n", info.temperature_min);
88 }
89
90 /* mag3110 init */
91 res = mag3110_init(&gs_handle);
92 if (res != 0)
93 {
94 mag3110_interface_debug_print("mag3110: init failed.\n");
95
96 return 1;
97 }
98
99 /* start read test */
100 mag3110_interface_debug_print("mag3110: start read test.\n");
101
102 /* set offset x 0x0000 */
103 res = mag3110_set_offset_x(&gs_handle, 0x0000);
104 if (res != 0)
105 {
106 mag3110_interface_debug_print("mag3110: set offset x failed.\n");
107 (void)mag3110_deinit(&gs_handle);
108
109 return 1;
110 }
111
112 /* set offset y 0x0000 */
113 res = mag3110_set_offset_y(&gs_handle, 0x0000);
114 if (res != 0)
115 {
116 mag3110_interface_debug_print("mag3110: set offset y failed.\n");
117 (void)mag3110_deinit(&gs_handle);
118
119 return 1;
120 }
121
122 /* set offset z 0x0000 */
123 res = mag3110_set_offset_z(&gs_handle, 0x0000);
124 if (res != 0)
125 {
126 mag3110_interface_debug_print("mag3110: set offset z failed.\n");
127 (void)mag3110_deinit(&gs_handle);
128
129 return 1;
130 }
131
132 /* disable offset correction */
134 if (res != 0)
135 {
136 mag3110_interface_debug_print("mag3110: set disable offset correction failed.\n");
137 (void)mag3110_deinit(&gs_handle);
138
139 return 1;
140 }
141
142 /* set 10hz, 128 over sample */
144 if (res != 0)
145 {
146 mag3110_interface_debug_print("mag3110: set rate over sample failed.\n");
147 (void)mag3110_deinit(&gs_handle);
148
149 return 1;
150 }
151
152 /* disable automatic magnetic sensor reset */
154 if (res != 0)
155 {
156 mag3110_interface_debug_print("mag3110: set automatic magnetic sensor reset failed.\n");
157 (void)mag3110_deinit(&gs_handle);
158
159 return 1;
160 }
161
162 /* disable trigger mode */
164 if (res != 0)
165 {
166 mag3110_interface_debug_print("mag3110: set trigger mode failed.\n");
167 (void)mag3110_deinit(&gs_handle);
168
169 return 1;
170 }
171
172 /* disable fast read mode */
174 if (res != 0)
175 {
176 mag3110_interface_debug_print("mag3110: set fast read mode failed.\n");
177 (void)mag3110_deinit(&gs_handle);
178
179 return 1;
180 }
181
182 /* set active mode */
183 res = mag3110_set_mode(&gs_handle, MAG3110_MODE_ACTIVE);
184 if (res != 0)
185 {
186 mag3110_interface_debug_print("mag3110: set mode failed.\n");
187 (void)mag3110_deinit(&gs_handle);
188
189 return 1;
190 }
191
192 /* normal read mode test */
193 mag3110_interface_debug_print("mag3110: normal read mode test.\n");
194
195 for (i = 0; i < times; i++)
196 {
197 /* delay 1 s*/
199
200 /* single read */
201 res = mag3110_read(&gs_handle, (int16_t *)raw, ut);
202 if (res != 0)
203 {
204 mag3110_interface_debug_print("mag3110: read failed.\n");
205 (void)mag3110_deinit(&gs_handle);
206
207 return 1;
208 }
209
210 /* print result */
211 mag3110_interface_debug_print("mag3110: x is %.1fuT.\n", ut[0]);
212 mag3110_interface_debug_print("mag3110: y is %.1fuT.\n", ut[1]);
213 mag3110_interface_debug_print("mag3110: z is %.1fuT.\n", ut[2]);
214 }
215
216 /* enable fast read mode */
218 if (res != 0)
219 {
220 mag3110_interface_debug_print("mag3110: set fast read mode failed.\n");
221 (void)mag3110_deinit(&gs_handle);
222
223 return 1;
224 }
225
226 /* fast read mode test */
227 mag3110_interface_debug_print("mag3110: fast read mode test.\n");
228
229 for (i = 0; i < times; i++)
230 {
231 /* delay 1 s*/
233
234 /* read */
235 res = mag3110_read(&gs_handle, (int16_t *)raw, ut);
236 if (res != 0)
237 {
238 mag3110_interface_debug_print("mag3110: read failed.\n");
239 (void)mag3110_deinit(&gs_handle);
240
241 return 1;
242 }
243
244 /* print result */
245 mag3110_interface_debug_print("mag3110: x is %.1fuT.\n", ut[0]);
246 mag3110_interface_debug_print("mag3110: y is %.1fuT.\n", ut[1]);
247 mag3110_interface_debug_print("mag3110: z is %.1fuT.\n", ut[2]);
248 }
249
250 /* disable fast read mode */
252 if (res != 0)
253 {
254 mag3110_interface_debug_print("mag3110: set fast read mode failed.\n");
255 (void)mag3110_deinit(&gs_handle);
256
257 return 1;
258 }
259
260 /* enable trigger mode */
262 if (res != 0)
263 {
264 mag3110_interface_debug_print("mag3110: set trigger mode failed.\n");
265 (void)mag3110_deinit(&gs_handle);
266
267 return 1;
268 }
269
270 /* trigger mode test */
271 mag3110_interface_debug_print("mag3110: trigger mode test.\n");
272
273 for (i = 0; i < times; i++)
274 {
275 /* delay 1 s*/
277
278 /* read */
279 res = mag3110_read(&gs_handle, (int16_t *)raw, ut);
280 if (res != 0)
281 {
282 mag3110_interface_debug_print("mag3110: read failed.\n");
283 (void)mag3110_deinit(&gs_handle);
284
285 return 1;
286 }
287
288 /* print result */
289 mag3110_interface_debug_print("mag3110: x is %.1fuT.\n", ut[0]);
290 mag3110_interface_debug_print("mag3110: y is %.1fuT.\n", ut[1]);
291 mag3110_interface_debug_print("mag3110: z is %.1fuT.\n", ut[2]);
292 }
293
294 /* read die temperature test */
295 mag3110_interface_debug_print("mag3110: read die temperature test.\n");
296
297 for (i = 0; i < times; i++)
298 {
299 /* delay 1 s*/
301
302 /* read */
303 res = mag3110_read_die_temperature(&gs_handle, &raw_s, &degree);
304 if (res != 0)
305 {
306 mag3110_interface_debug_print("mag3110: read die temperature failed.\n");
307 (void)mag3110_deinit(&gs_handle);
308
309 return 1;
310 }
311
312 /* print result */
313 mag3110_interface_debug_print("mag3110: die temperature is %.1fC.\n", degree + 30.0f);
314 }
315
316 /* finish read test */
317 mag3110_interface_debug_print("mag3110: finish read test.\n");
318 (void)mag3110_deinit(&gs_handle);
319
320 return 0;
321}
driver mag3110 read test header file
uint8_t mag3110_set_rate_over_sample(mag3110_handle_t *handle, mag3110_rate_over_sample_t rate_over_sample)
set rate over sample
uint8_t mag3110_info(mag3110_info_t *info)
get chip's information
uint8_t mag3110_set_trigger_mode(mag3110_handle_t *handle, mag3110_bool_t enable)
enable or disable trigger mode
struct mag3110_handle_s mag3110_handle_t
mag3110 handle structure definition
uint8_t mag3110_set_mode(mag3110_handle_t *handle, mag3110_mode_t mode)
set mode
uint8_t mag3110_set_disable_offset_correction(mag3110_handle_t *handle, mag3110_bool_t enable)
enable or disable offset correction
uint8_t mag3110_deinit(mag3110_handle_t *handle)
close the chip
uint8_t mag3110_init(mag3110_handle_t *handle)
initialize the chip
uint8_t mag3110_set_offset_y(mag3110_handle_t *handle, uint16_t offset)
set offset y
uint8_t mag3110_set_offset_x(mag3110_handle_t *handle, uint16_t offset)
set offset x
struct mag3110_info_s mag3110_info_t
mag3110 information structure definition
uint8_t mag3110_set_automatic_magnetic_sensor_reset(mag3110_handle_t *handle, mag3110_bool_t enable)
enable or disable automatic magnetic sensor reset
uint8_t mag3110_read(mag3110_handle_t *handle, int16_t raw[3], float ut[3])
read data
uint8_t mag3110_set_fast_read_mode(mag3110_handle_t *handle, mag3110_bool_t enable)
enable or disable fast read mode
uint8_t mag3110_read_die_temperature(mag3110_handle_t *handle, int8_t *raw, float *degree)
read die temperature
uint8_t mag3110_set_offset_z(mag3110_handle_t *handle, uint16_t offset)
set offset z
@ MAG3110_BOOL_TRUE
@ MAG3110_BOOL_FALSE
@ MAG3110_RATE_10HZ_OVER_SAMPLE_128
@ MAG3110_MODE_ACTIVE
uint8_t mag3110_interface_iic_deinit(void)
interface iic bus deinit
void mag3110_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t mag3110_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t mag3110_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t mag3110_interface_iic_init(void)
interface iic bus init
void mag3110_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t mag3110_read_test(uint32_t times)
read test
uint32_t driver_version
char manufacturer_name[32]