LibDriver MAX31856
Loading...
Searching...
No Matches
driver_max31856_interrupt_test.c
Go to the documentation of this file.
1
36
38
39static max31856_handle_t gs_handle;
40static uint8_t gs_flag = 0;
41
47static void a_callback(uint8_t type)
48{
49 switch (type)
50 {
52 {
53 max31856_interface_debug_print("max31856: irq cold junction out of range.\n");
54 gs_flag = 1;
55
56 break;
57 }
59 {
60 max31856_interface_debug_print("max31856: irq thermocouple out of range.\n");
61 gs_flag = 1;
62
63 break;
64 }
66 {
67 uint8_t res;
68 int16_t raw;
69 double deg;
70
71 max31856_interface_debug_print("max31856: irq cold junction high fault.\n");
72 gs_flag = 1;
73
74 /* read cold junction temperature */
75 res = max31856_read_cold_junction_temperature(&gs_handle, &raw, &deg);
76 if (res == 0)
77 {
78 /* output */
79 max31856_interface_debug_print("max31856: cold junction temperature is %0.03fC.\n", deg);
80 }
81
82
83 break;
84 }
86 {
87 uint8_t res;
88 int16_t raw;
89 double deg;
90
91 max31856_interface_debug_print("max31856: irq cold junction low fault.\n");
92 gs_flag = 1;
93
94 /* read cold junction temperature */
95 res = max31856_read_cold_junction_temperature(&gs_handle, &raw, &deg);
96 if (res == 0)
97 {
98 /* output */
99 max31856_interface_debug_print("max31856: cold junction temperature is %0.03fC.\n", deg);
100 }
101
102 break;
103 }
105 {
106 uint8_t res;
107 int32_t raw;
108 double deg;
109
110 max31856_interface_debug_print("max31856: irq thermocouple temperature high fault.\n");
111 gs_flag = 1;
112
113 /* continuous read */
114 res = max31856_continuous_read(&gs_handle, &raw, &deg);
115 if (res == 0)
116 {
117 /* output */
118 max31856_interface_debug_print("max31856: temperature is %0.03fC.\n", deg);
119 }
120
121 break;
122 }
124 {
125 uint8_t res;
126 int32_t raw;
127 double deg;
128
129 max31856_interface_debug_print("max31856: irq thermocouple temperature low fault.\n");
130 gs_flag = 1;
131
132 /* continuous read */
133 res = max31856_continuous_read(&gs_handle, &raw, &deg);
134 if (res == 0)
135 {
136 /* output */
137 max31856_interface_debug_print("max31856: temperature is %0.03fC.\n", deg);
138 }
139
140 break;
141 }
143 {
144 max31856_interface_debug_print("max31856: irq overvoltage or undervoltage input fault.\n");
145 gs_flag = 1;
146
147 break;
148 }
150 {
151 max31856_interface_debug_print("max31856: irq thermocouple open circuit fault.\n");
152 gs_flag = 1;
153
154 break;
155 }
156 default :
157 {
158 max31856_interface_debug_print("max31856: unknown code.\n");
159 gs_flag = 1;
160
161 break;
162 }
163 }
164}
165
174{
175 if (max31856_irq_handler(&gs_handle) != 0)
176 {
177 return 1;
178 }
179
180 return 0;
181}
182
197 float cold_junction_low_fault_threshold_deg,
198 float cold_junction_high_fault_threshold_deg,
199 float temperature_low_fault_threshold_deg,
200 float temperature_high_fault_threshold_deg,
201 uint32_t timeout)
202{
203 uint8_t res;
204 int8_t offset;
205 int8_t reg8;
206 int16_t reg16;
207 uint32_t i;
208 max31856_info_t info;
209
210 /* link interface function */
218 DRIVER_MAX31856_LINK_RECEIVE_CALLBACK(&gs_handle, a_callback);
219
220 /* max31856 info */
221 res = max31856_info(&info);
222 if (res != 0)
223 {
224 max31856_interface_debug_print("max31856: get info failed.\n");
225
226 return 1;
227 }
228 else
229 {
230 /* print chip information */
231 max31856_interface_debug_print("max31856: chip is %s.\n", info.chip_name);
232 max31856_interface_debug_print("max31856: manufacturer is %s.\n", info.manufacturer_name);
233 max31856_interface_debug_print("max31856: interface is %s.\n", info.interface);
234 max31856_interface_debug_print("max31856: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
235 max31856_interface_debug_print("max31856: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
236 max31856_interface_debug_print("max31856: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
237 max31856_interface_debug_print("max31856: max current is %0.2fmA.\n", info.max_current_ma);
238 max31856_interface_debug_print("max31856: max temperature is %0.1fC.\n", info.temperature_max);
239 max31856_interface_debug_print("max31856: min temperature is %0.1fC.\n", info.temperature_min);
240 }
241
242 /* start interrupt test */
243 max31856_interface_debug_print("max31856: start interrupt test.\n");
244
245 /* output */
246 max31856_interface_debug_print("max31856: cold junction low fault threshold %0.3fC.\n", cold_junction_low_fault_threshold_deg);
247 max31856_interface_debug_print("max31856: cold junction high fault threshold %0.3fC.\n", cold_junction_high_fault_threshold_deg);
248 max31856_interface_debug_print("max31856: temperature low fault threshold %0.3fC.\n", temperature_low_fault_threshold_deg);
249 max31856_interface_debug_print("max31856: temperature high fault threshold %0.3fC.\n", temperature_high_fault_threshold_deg);
250
251 /* max31856 init */
252 res = max31856_init(&gs_handle);
253 if (res != 0)
254 {
255 max31856_interface_debug_print("max31856: init failed.\n");
256
257 return 1;
258 }
259
260 /* set thermocouple type */
261 res = max31856_set_thermocouple_type(&gs_handle, type);
262 if (res != 0)
263 {
264 max31856_interface_debug_print("max31856: set thermocouple type failed.\n");
265 (void)max31856_deinit(&gs_handle);
266
267 return 1;
268 }
269
270 /* set open circuit detection mode3 */
272 if (res != 0)
273 {
274 max31856_interface_debug_print("max31856: set open circuit detection failed.\n");
275 (void)max31856_deinit(&gs_handle);
276
277 return 1;
278 }
279
280 /* enable cold junction sensor */
282 if (res != 0)
283 {
284 max31856_interface_debug_print("max31856: set cold junction sensor failed.\n");
285 (void)max31856_deinit(&gs_handle);
286
287 return 1;
288 }
289
290 /* set fault interrupt mode */
292 if (res != 0)
293 {
294 max31856_interface_debug_print("max31856: set fault mode failed.\n");
295 (void)max31856_deinit(&gs_handle);
296
297 return 1;
298 }
299
300 /* set noise rejection filter 50hz */
302 if (res != 0)
303 {
304 max31856_interface_debug_print("max31856: set noise rejection filter failed.\n");
305 (void)max31856_deinit(&gs_handle);
306
307 return 1;
308 }
309
310 /* set sample average 16 */
312 if (res != 0)
313 {
314 max31856_interface_debug_print("max31856: set sample average failed.\n");
315 (void)max31856_deinit(&gs_handle);
316
317 return 1;
318 }
319
320 /* disable cold junction high fault threshold mask */
322 if (res != 0)
323 {
324 max31856_interface_debug_print("max31856: set fault mask failed.\n");
325 (void)max31856_deinit(&gs_handle);
326
327 return 1;
328 }
329
330 /* disable cold junction low fault threshold mask */
332 if (res != 0)
333 {
334 max31856_interface_debug_print("max31856: set fault mask failed.\n");
335 (void)max31856_deinit(&gs_handle);
336
337 return 1;
338 }
339
340 /* disable thermocouple temperature high fault threshold mask */
342 if (res != 0)
343 {
344 max31856_interface_debug_print("max31856: set fault mask failed.\n");
345 (void)max31856_deinit(&gs_handle);
346
347 return 1;
348 }
349
350 /* disable thermocouple temperature low fault threshold mask */
352 if (res != 0)
353 {
354 max31856_interface_debug_print("max31856: set fault mask failed.\n");
355 (void)max31856_deinit(&gs_handle);
356
357 return 1;
358 }
359
360 /* disable over voltage or undervoltage input fault mask */
362 if (res != 0)
363 {
364 max31856_interface_debug_print("max31856: set fault mask failed.\n");
365 (void)max31856_deinit(&gs_handle);
366
367 return 1;
368 }
369
370 /* disable thermocouple open circuit fault mask */
372 if (res != 0)
373 {
374 max31856_interface_debug_print("max31856: set fault mask failed.\n");
375 (void)max31856_deinit(&gs_handle);
376
377 return 1;
378 }
379
380 /* 0.0 offset */
382 if (res != 0)
383 {
384 max31856_interface_debug_print("max31856: cold junction temperature offset convert to register failed.\n");
385 (void)max31856_deinit(&gs_handle);
386
387 return 1;
388 }
389
390 /* set cold junction temperature offset */
391 res = max31856_set_cold_junction_temperature_offset(&gs_handle, offset);
392 if (res != 0)
393 {
394 max31856_interface_debug_print("max31856: set cold junction temperature offset failed.\n");
395 (void)max31856_deinit(&gs_handle);
396
397 return 1;
398 }
399
400 /* convert */
401 res = max31856_cold_junction_fault_threshold_convert_to_register(&gs_handle, cold_junction_low_fault_threshold_deg, &reg8);
402 if (res != 0)
403 {
404 max31856_interface_debug_print("max31856: cold junction fault threshold convert to register failed.\n");
405 (void)max31856_deinit(&gs_handle);
406
407 return 1;
408 }
409
410 /* set cold junction low fault threshold */
412 if (res != 0)
413 {
414 max31856_interface_debug_print("max31856: set cold junction low fault threshold failed.\n");
415 (void)max31856_deinit(&gs_handle);
416
417 return 1;
418 }
419
420 /* convert */
421 res = max31856_cold_junction_fault_threshold_convert_to_register(&gs_handle, cold_junction_high_fault_threshold_deg, &reg8);
422 if (res != 0)
423 {
424 max31856_interface_debug_print("max31856: cold junction fault threshold convert to register failed.\n");
425 (void)max31856_deinit(&gs_handle);
426
427 return 1;
428 }
429
430 /* set cold junction high fault threshold */
432 if (res != 0)
433 {
434 max31856_interface_debug_print("max31856: set cold junction high fault threshold failed.\n");
435 (void)max31856_deinit(&gs_handle);
436
437 return 1;
438 }
439
440 /* convert */
441 res = max31856_temperature_fault_threshold_convert_to_register(&gs_handle, temperature_low_fault_threshold_deg, &reg16);
442 if (res != 0)
443 {
444 max31856_interface_debug_print("max31856: temperature fault threshold convert to register failed.\n");
445 (void)max31856_deinit(&gs_handle);
446
447 return 1;
448 }
449
450 /* set temperature low fault threshold */
451 res = max31856_set_temperature_low_fault_threshold(&gs_handle, reg16);
452 if (res != 0)
453 {
454 max31856_interface_debug_print("max31856: set temperature low fault threshold failed.\n");
455 (void)max31856_deinit(&gs_handle);
456
457 return 1;
458 }
459
460 /* convert */
461 res = max31856_temperature_fault_threshold_convert_to_register(&gs_handle, temperature_high_fault_threshold_deg, &reg16);
462 if (res != 0)
463 {
464 max31856_interface_debug_print("max31856: temperature fault threshold convert to register failed.\n");
465 (void)max31856_deinit(&gs_handle);
466
467 return 1;
468 }
469
470 /* set temperature high fault threshold */
471 res = max31856_set_temperature_high_fault_threshold(&gs_handle, reg16);
472 if (res != 0)
473 {
474 max31856_interface_debug_print("max31856: set temperature high fault threshold failed.\n");
475 (void)max31856_deinit(&gs_handle);
476
477 return 1;
478 }
479
480 /* clear fault */
481 res = max31856_clear_fault(&gs_handle);
482 if (res != 0)
483 {
484 max31856_interface_debug_print("max31856: clear fault failed.\n");
485 (void)max31856_deinit(&gs_handle);
486
487 return 1;
488 }
489
490 /* start continuous read */
491 res = max31856_start_continuous_read(&gs_handle);
492 if (res != 0)
493 {
494 max31856_interface_debug_print("max31856: start continuous read failed.\n");
495 (void)max31856_deinit(&gs_handle);
496
497 return 1;
498 }
499
500 /* init 0 */
501 gs_flag = 0;
502
503 /* wait for timeout */
504 for (i = 0; i < timeout; i++)
505 {
506 /* delay 1ms */
508
509 if (gs_flag != 0)
510 {
511 max31856_interface_debug_print("max31856: find interrupt.\n");
512
513 break;
514 }
515 }
516
517 /* finish interrupt test */
518 max31856_interface_debug_print("max31856: finish interrupt test.\n");
519 (void)max31856_stop_continuous_read(&gs_handle);
520 (void)max31856_deinit(&gs_handle);
521
522 return 0;
523}
driver max31856 interrupt test header file
uint8_t max31856_stop_continuous_read(max31856_handle_t *handle)
stop the chip reading
struct max31856_info_s max31856_info_t
max31856 information structure definition
uint8_t max31856_set_cold_junction_high_fault_threshold(max31856_handle_t *handle, int8_t threshold)
set cold junction high fault threshold
uint8_t max31856_set_cold_junction_sensor(max31856_handle_t *handle, max31856_bool_t enable)
enable or disable cold junction sensor
uint8_t max31856_set_thermocouple_type(max31856_handle_t *handle, max31856_thermocouple_type_t type)
set thermocouple type
uint8_t max31856_info(max31856_info_t *info)
get chip's information
uint8_t max31856_clear_fault(max31856_handle_t *handle)
clear fault
uint8_t max31856_start_continuous_read(max31856_handle_t *handle)
start the chip reading
uint8_t max31856_set_fault_mask(max31856_handle_t *handle, max31856_fault_mask_t mask, max31856_bool_t enable)
set fault mask
uint8_t max31856_set_open_circuit_detection(max31856_handle_t *handle, max31856_open_circuit_detection_t detection)
set open circuit detection
uint8_t max31856_set_temperature_low_fault_threshold(max31856_handle_t *handle, int16_t threshold)
set temperature low fault threshold
uint8_t max31856_continuous_read(max31856_handle_t *handle, int32_t *raw, double *deg)
read data from the chip continuously
uint8_t max31856_temperature_fault_threshold_convert_to_register(max31856_handle_t *handle, float deg, int16_t *reg)
convert the temperature fault threshold to the register raw data
uint8_t max31856_read_cold_junction_temperature(max31856_handle_t *handle, int16_t *raw, double *deg)
read cold junction temperature
uint8_t max31856_deinit(max31856_handle_t *handle)
close the chip
uint8_t max31856_set_noise_rejection_filter(max31856_handle_t *handle, max31856_noise_rejection_filter_t filter)
set noise rejection filter
max31856_thermocouple_type_t
max31856 thermocouple type enumeration definition
uint8_t max31856_init(max31856_handle_t *handle)
initialize the chip
uint8_t max31856_set_cold_junction_low_fault_threshold(max31856_handle_t *handle, int8_t threshold)
set cold junction low fault threshold
struct max31856_handle_s max31856_handle_t
max31856 handle structure definition
uint8_t max31856_cold_junction_fault_threshold_convert_to_register(max31856_handle_t *handle, float deg, int8_t *reg)
convert the cold junction fault threshold to the register raw data
uint8_t max31856_set_sample_average(max31856_handle_t *handle, max31856_sample_average_t average)
set sample average
uint8_t max31856_cold_junction_temperature_offset_convert_to_register(max31856_handle_t *handle, float deg, int8_t *reg)
convert the cold junction temperature offset to the register raw data
uint8_t max31856_set_fault_mode(max31856_handle_t *handle, max31856_fault_mode_t mode)
set fault mode
uint8_t max31856_irq_handler(max31856_handle_t *handle)
irq handler
uint8_t max31856_set_cold_junction_temperature_offset(max31856_handle_t *handle, int8_t offset)
set cold junction temperature offset
uint8_t max31856_set_temperature_high_fault_threshold(max31856_handle_t *handle, int16_t threshold)
set temperature high fault threshold
@ MAX31856_FAULT_STATUS_TCHIGH
@ MAX31856_FAULT_STATUS_CJHIGH
@ MAX31856_FAULT_STATUS_TCLOW
@ MAX31856_FAULT_STATUS_OVUV
@ MAX31856_FAULT_STATUS_TCRANGE
@ MAX31856_FAULT_STATUS_OPEN
@ MAX31856_FAULT_STATUS_CJLOW
@ MAX31856_FAULT_STATUS_CJRANGE
@ MAX31856_BOOL_TRUE
@ MAX31856_BOOL_FALSE
@ MAX31856_OPEN_CIRCUIT_DETECTION_MODE3
@ MAX31856_SAMPLE_AVERAGE_16
@ MAX31856_NOISE_REJECTION_FILTER_50HZ
@ MAX31856_FAULT_MODE_INTERRUPT
@ MAX31856_FAULT_MASK_OPEN
@ MAX31856_FAULT_MASK_TCLOW
@ MAX31856_FAULT_MASK_CJHIGH
@ MAX31856_FAULT_MASK_TCHIGH
@ MAX31856_FAULT_MASK_OVUV
@ MAX31856_FAULT_MASK_CJLOW
uint8_t max31856_interface_spi_read(uint8_t reg, uint8_t *buf, uint16_t len)
interface spi bus read
void max31856_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t max31856_interface_spi_init(void)
interface spi bus init
void max31856_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t max31856_interface_spi_write(uint8_t reg, uint8_t *buf, uint16_t len)
interface spi bus write
uint8_t max31856_interface_spi_deinit(void)
interface spi bus deinit
uint8_t max31856_interrupt_test_irq_handler(void)
interrupt test irq callback
uint8_t max31856_interrupt_test(max31856_thermocouple_type_t type, float cold_junction_low_fault_threshold_deg, float cold_junction_high_fault_threshold_deg, float temperature_low_fault_threshold_deg, float temperature_high_fault_threshold_deg, uint32_t timeout)
interrupt test
char manufacturer_name[32]