LibDriver CS1238
Loading...
Searching...
No Matches
driver_cs1238_read_test.c
Go to the documentation of this file.
1
36
38#include <stdlib.h>
39
43static cs1238_handle_t gs_handle;
44
56uint8_t cs1238_read_test(float reference_voltage_v, int32_t temperature_raw, float temperature_deg, uint32_t times)
57{
58 uint8_t res;
59 uint32_t i;
60 cs1238_info_t info;
61
62 /* link interface function */
74
75 /* get information */
76 res = cs1238_info(&info);
77 if (res != 0)
78 {
79 cs1238_interface_debug_print("cs1238: get info failed.\n");
80
81 return 1;
82 }
83 else
84 {
85 /* print chip info */
86 cs1238_interface_debug_print("cs1238: chip is %s.\n", info.chip_name);
87 cs1238_interface_debug_print("cs1238: manufacturer is %s.\n", info.manufacturer_name);
88 cs1238_interface_debug_print("cs1238: interface is %s.\n", info.interface);
89 cs1238_interface_debug_print("cs1238: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
90 cs1238_interface_debug_print("cs1238: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
91 cs1238_interface_debug_print("cs1238: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
92 cs1238_interface_debug_print("cs1238: max current is %0.2fmA.\n", info.max_current_ma);
93 cs1238_interface_debug_print("cs1238: max temperature is %0.1fC.\n", info.temperature_max);
94 cs1238_interface_debug_print("cs1238: min temperature is %0.1fC.\n", info.temperature_min);
95 }
96
97 /* start read test */
98 cs1238_interface_debug_print("cs1238: start read test.\n");
99
100 /* cs1238 init */
101 res = cs1238_init(&gs_handle);
102 if (res != 0)
103 {
104 cs1238_interface_debug_print("cs1238: init failed.\n");
105
106 return 1;
107 }
108
109 /* set reference voltage */
110 res = cs1238_set_reference_voltage(&gs_handle, reference_voltage_v);
111 if (res != 0)
112 {
113 cs1238_interface_debug_print("cs1238: set reference voltage failed.\n");
114 (void)cs1238_deinit(&gs_handle);
115
116 return 1;
117 }
118
119 /* set temperature calibration point */
120 res = cs1238_set_temperature_calibration_point(&gs_handle, temperature_raw, temperature_deg);
121 if (res != 0)
122 {
123 cs1238_interface_debug_print("cs1238: set temperature calibration point failed.\n");
124 (void)cs1238_deinit(&gs_handle);
125
126 return 1;
127 }
128
129 /* wake up */
130 res = cs1238_wake_up(&gs_handle);
131 if (res != 0)
132 {
133 cs1238_interface_debug_print("cs1238: wake up failed.\n");
134 (void)cs1238_deinit(&gs_handle);
135
136 return 1;
137 }
138
139 /* enable reference voltage output */
141 if (res != 0)
142 {
143 cs1238_interface_debug_print("cs1238: set reference voltage output failed.\n");
144 (void)cs1238_deinit(&gs_handle);
145
146 return 1;
147 }
148
149 /* set adc rate 10hz */
150 res = cs1238_set_adc_rate(&gs_handle, CS1238_ADC_RATE_10HZ);
151 if (res != 0)
152 {
153 cs1238_interface_debug_print("cs1238: set adc rate failed.\n");
154 (void)cs1238_deinit(&gs_handle);
155
156 return 1;
157 }
158
159 /* set channel a */
160 res = cs1238_set_channel(&gs_handle, CS1238_CHANNEL_A);
161 if (res != 0)
162 {
163 cs1238_interface_debug_print("cs1238: set channel failed.\n");
164 (void)cs1238_deinit(&gs_handle);
165
166 return 1;
167 }
168
169 /* output */
170 cs1238_interface_debug_print("cs1238: set channel a.\n");
171
172 /* output */
173 cs1238_interface_debug_print("cs1238: set pga 1.\n");
174
175 /* set pga 1 */
176 res = cs1238_set_pga(&gs_handle, CS1238_PGA_1);
177 if (res != 0)
178 {
179 cs1238_interface_debug_print("cs1238: set pga failed.\n");
180 (void)cs1238_deinit(&gs_handle);
181
182 return 1;
183 }
184
185 for (i = 0; i < times; i++)
186 {
187 int32_t raw;
188 double mv;
189
190 /* delay 1000ms */
192
193 /* read data */
194 res = cs1238_read(&gs_handle, &raw, &mv);
195 if (res != 0)
196 {
197 cs1238_interface_debug_print("cs1238: read failed.\n");
198 (void)cs1238_deinit(&gs_handle);
199
200 return 1;
201 }
202
203 /* output */
204 cs1238_interface_debug_print("cs1238: adc is %fmV.\n", mv);
205 }
206
207 /* output */
208 cs1238_interface_debug_print("cs1238: set pga 2.\n");
209
210 /* set pga 2 */
211 res = cs1238_set_pga(&gs_handle, CS1238_PGA_2);
212 if (res != 0)
213 {
214 cs1238_interface_debug_print("cs1238: set pga failed.\n");
215 (void)cs1238_deinit(&gs_handle);
216
217 return 1;
218 }
219
220 for (i = 0; i < times; i++)
221 {
222 int32_t raw;
223 double mv;
224
225 /* delay 1000ms */
227
228 /* read data */
229 res = cs1238_read(&gs_handle, &raw, &mv);
230 if (res != 0)
231 {
232 cs1238_interface_debug_print("cs1238: read failed.\n");
233 (void)cs1238_deinit(&gs_handle);
234
235 return 1;
236 }
237
238 /* output */
239 cs1238_interface_debug_print("cs1238: adc is %fmV.\n", mv);
240 }
241
242 /* output */
243 cs1238_interface_debug_print("cs1238: set pga 64.\n");
244
245 /* set pga 64 */
246 res = cs1238_set_pga(&gs_handle, CS1238_PGA_64);
247 if (res != 0)
248 {
249 cs1238_interface_debug_print("cs1238: set pga failed.\n");
250 (void)cs1238_deinit(&gs_handle);
251
252 return 1;
253 }
254
255 for (i = 0; i < times; i++)
256 {
257 int32_t raw;
258 double mv;
259
260 /* delay 1000ms */
262
263 /* read data */
264 res = cs1238_read(&gs_handle, &raw, &mv);
265 if (res != 0)
266 {
267 cs1238_interface_debug_print("cs1238: read failed.\n");
268 (void)cs1238_deinit(&gs_handle);
269
270 return 1;
271 }
272
273 /* output */
274 cs1238_interface_debug_print("cs1238: adc is %fmV.\n", mv);
275 }
276
277 /* output */
278 cs1238_interface_debug_print("cs1238: set pga 128.\n");
279
280 /* set pga 128 */
281 res = cs1238_set_pga(&gs_handle, CS1238_PGA_128);
282 if (res != 0)
283 {
284 cs1238_interface_debug_print("cs1238: set pga failed.\n");
285 (void)cs1238_deinit(&gs_handle);
286
287 return 1;
288 }
289
290 for (i = 0; i < times; i++)
291 {
292 int32_t raw;
293 double mv;
294
295 /* delay 1000ms */
297
298 /* read data */
299 res = cs1238_read(&gs_handle, &raw, &mv);
300 if (res != 0)
301 {
302 cs1238_interface_debug_print("cs1238: read failed.\n");
303 (void)cs1238_deinit(&gs_handle);
304
305 return 1;
306 }
307
308 /* output */
309 cs1238_interface_debug_print("cs1238: adc is %fmV.\n", mv);
310 }
311
312 /* output */
313 cs1238_interface_debug_print("cs1238: set adc rate 10hz.\n");
314
315 /* set adc rate 10hz */
316 res = cs1238_set_adc_rate(&gs_handle, CS1238_ADC_RATE_10HZ);
317 if (res != 0)
318 {
319 cs1238_interface_debug_print("cs1238: set adc rate failed.\n");
320 (void)cs1238_deinit(&gs_handle);
321
322 return 1;
323 }
324
325 for (i = 0; i < times; i++)
326 {
327 int32_t raw;
328 double mv;
329
330 /* delay 1000ms */
332
333 /* read data */
334 res = cs1238_read(&gs_handle, &raw, &mv);
335 if (res != 0)
336 {
337 cs1238_interface_debug_print("cs1238: read failed.\n");
338 (void)cs1238_deinit(&gs_handle);
339
340 return 1;
341 }
342
343 /* output */
344 cs1238_interface_debug_print("cs1238: adc is %fmV.\n", mv);
345 }
346
347 /* output */
348 cs1238_interface_debug_print("cs1238: set adc rate 40hz.\n");
349
350 /* set adc rate 40hz */
351 res = cs1238_set_adc_rate(&gs_handle, CS1238_ADC_RATE_40HZ);
352 if (res != 0)
353 {
354 cs1238_interface_debug_print("cs1238: set adc rate failed.\n");
355 (void)cs1238_deinit(&gs_handle);
356
357 return 1;
358 }
359
360 for (i = 0; i < times; i++)
361 {
362 int32_t raw;
363 double mv;
364
365 /* delay 1000ms */
367
368 /* read data */
369 res = cs1238_read(&gs_handle, &raw, &mv);
370 if (res != 0)
371 {
372 cs1238_interface_debug_print("cs1238: read failed.\n");
373 (void)cs1238_deinit(&gs_handle);
374
375 return 1;
376 }
377
378 /* output */
379 cs1238_interface_debug_print("cs1238: adc is %fmV.\n", mv);
380 }
381
382 /* output */
383 cs1238_interface_debug_print("cs1238: set adc rate 640hz.\n");
384
385 /* set adc rate 640hz */
387 if (res != 0)
388 {
389 cs1238_interface_debug_print("cs1238: set adc rate failed.\n");
390 (void)cs1238_deinit(&gs_handle);
391
392 return 1;
393 }
394
395 for (i = 0; i < times; i++)
396 {
397 int32_t raw;
398 double mv;
399
400 /* delay 1000ms */
402
403 /* read data */
404 res = cs1238_read(&gs_handle, &raw, &mv);
405 if (res != 0)
406 {
407 cs1238_interface_debug_print("cs1238: read failed.\n");
408 (void)cs1238_deinit(&gs_handle);
409
410 return 1;
411 }
412
413 /* output */
414 cs1238_interface_debug_print("cs1238: adc is %fmV.\n", mv);
415 }
416
417 /* output */
418 cs1238_interface_debug_print("cs1238: set adc rate 1280hz.\n");
419
420 /* set adc rate 1280hz */
422 if (res != 0)
423 {
424 cs1238_interface_debug_print("cs1238: set adc rate failed.\n");
425 (void)cs1238_deinit(&gs_handle);
426
427 return 1;
428 }
429
430 for (i = 0; i < times; i++)
431 {
432 int32_t raw;
433 double mv;
434
435 /* delay 1000ms */
437
438 /* read data */
439 res = cs1238_read(&gs_handle, &raw, &mv);
440 if (res != 0)
441 {
442 cs1238_interface_debug_print("cs1238: read failed.\n");
443 (void)cs1238_deinit(&gs_handle);
444
445 return 1;
446 }
447
448 /* output */
449 cs1238_interface_debug_print("cs1238: adc is %fmV.\n", mv);
450 }
451
452 /* set channel b */
453 res = cs1238_set_channel(&gs_handle, CS1238_CHANNEL_B);
454 if (res != 0)
455 {
456 cs1238_interface_debug_print("cs1238: set channel failed.\n");
457 (void)cs1238_deinit(&gs_handle);
458
459 return 1;
460 }
461
462 /* output */
463 cs1238_interface_debug_print("cs1238: set channel b.\n");
464
465 for (i = 0; i < times; i++)
466 {
467 int32_t raw;
468 double mv;
469
470 /* delay 1000ms */
472
473 /* read data */
474 res = cs1238_read(&gs_handle, &raw, &mv);
475 if (res != 0)
476 {
477 cs1238_interface_debug_print("cs1238: read failed.\n");
478 (void)cs1238_deinit(&gs_handle);
479
480 return 1;
481 }
482
483 /* output */
484 cs1238_interface_debug_print("cs1238: adc is %fmV.\n", mv);
485 }
486
487 /* set pga 1 */
488 res = cs1238_set_pga(&gs_handle, CS1238_PGA_1);
489 if (res != 0)
490 {
491 cs1238_interface_debug_print("cs1238: set pga failed.\n");
492 (void)cs1238_deinit(&gs_handle);
493
494 return 1;
495 }
496
497 /* set channel temperature */
499 if (res != 0)
500 {
501 cs1238_interface_debug_print("cs1238: set channel failed.\n");
502 (void)cs1238_deinit(&gs_handle);
503
504 return 1;
505 }
506
507 /* output */
508 cs1238_interface_debug_print("cs1238: read temperature test.\n");
509
510 for (i = 0; i < times; i++)
511 {
512 int32_t raw;
513 float deg;
514
515 /* delay 1000ms */
517
518 /* read data */
519 res = cs1238_read_temperature(&gs_handle, &raw, &deg);
520 if (res != 0)
521 {
522 cs1238_interface_debug_print("cs1238: read temperature failed.\n");
523 (void)cs1238_deinit(&gs_handle);
524
525 return 1;
526 }
527
528 /* output */
529 cs1238_interface_debug_print("cs1238: raw is 0x%0X, temperature is %0.2fC.\n", raw, deg);
530 }
531
532 /* finish read test */
533 cs1238_interface_debug_print("cs1238: finish read test.\n");
534 (void)cs1238_deinit(&gs_handle);
535
536 return 0;
537}
driver cs1238 read test header file
uint8_t cs1238_set_temperature_calibration_point(cs1238_handle_t *handle, int32_t temperature_raw, float temperature_deg)
set temperature calibration point
uint8_t cs1238_read(cs1238_handle_t *handle, int32_t *raw, double *mv)
read voltage
uint8_t cs1238_read_temperature(cs1238_handle_t *handle, int32_t *raw, float *degrees)
read temperature
struct cs1238_handle_s cs1238_handle_t
cs1238 handle structure definition
uint8_t cs1238_wake_up(cs1238_handle_t *handle)
wake up
uint8_t cs1238_set_reference_voltage(cs1238_handle_t *handle, float volt)
set reference voltage
uint8_t cs1238_set_adc_rate(cs1238_handle_t *handle, cs1238_adc_rate_t rate)
set adc rate
uint8_t cs1238_info(cs1238_info_t *info)
get chip's information
uint8_t cs1238_init(cs1238_handle_t *handle)
initialize the chip
uint8_t cs1238_set_pga(cs1238_handle_t *handle, cs1238_pga_t pga)
set adc pga
uint8_t cs1238_set_reference_voltage_output(cs1238_handle_t *handle, cs1238_bool_t enable)
enable or disable reference voltage output
struct cs1238_info_s cs1238_info_t
cs1238 information structure definition
uint8_t cs1238_deinit(cs1238_handle_t *handle)
close the chip
uint8_t cs1238_set_channel(cs1238_handle_t *handle, cs1238_channel_t channel)
set adc channel
@ CS1238_BOOL_TRUE
@ CS1238_ADC_RATE_40HZ
@ CS1238_ADC_RATE_1280HZ
@ CS1238_ADC_RATE_10HZ
@ CS1238_ADC_RATE_640HZ
@ CS1238_PGA_2
@ CS1238_PGA_1
@ CS1238_PGA_64
@ CS1238_PGA_128
@ CS1238_CHANNEL_TEMPERATURE
@ CS1238_CHANNEL_B
@ CS1238_CHANNEL_A
uint8_t cs1238_interface_sclk_gpio_deinit(void)
interface sclk gpio deinit
void cs1238_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t cs1238_interface_dout_gpio_read(uint8_t *level)
interface dout gpio read
uint8_t cs1238_interface_sclk_gpio_write(uint8_t level)
interface sclk gpio write
uint8_t cs1238_interface_dout_gpio_write(uint8_t level)
interface dout gpio write
uint8_t cs1238_interface_sclk_gpio_init(void)
interface sclk gpio init
uint8_t cs1238_interface_dout_gpio_init(void)
interface dout gpio init
uint8_t cs1238_interface_dout_gpio_deinit(void)
interface dout gpio deinit
void cs1238_interface_delay_us(uint32_t us)
interface delay us
void cs1238_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t cs1238_read_test(float reference_voltage_v, int32_t temperature_raw, float temperature_deg, uint32_t times)
read test
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]