1 /*
2  * Copyright (c) 2022 HAW Hamburg FTZ-DIWIP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef _MAX31865_H
7 #define _MAX31865_H
8 
9 #define DT_DRV_COMPAT maxim_max31865
10 
11 #include <math.h>
12 
13 #include <zephyr/drivers/sensor.h>
14 #include <zephyr/drivers/sensor/max31865.h>
15 #include <zephyr/drivers/spi.h>
16 #include <zephyr/kernel.h>
17 #include <zephyr/logging/log.h>
18 #include <zephyr/sys/util_macro.h>
19 #include <zephyr/sys/byteorder.h>
20 LOG_MODULE_REGISTER(MAX31865, CONFIG_SENSOR_LOG_LEVEL);
21 
22 #define MAX31865_FAULT_HIGH_THRESHOLD BIT(7)
23 #define MAX31865_FAULT_LOW_THRESHOLD  BIT(6)
24 #define MAX31865_FAULT_REFIN	      BIT(5)
25 #define MAX31865_FAULT_REFIN_FORCE    BIT(4)
26 #define MAX31865_FAULT_RTDIN_FORCE    BIT(3)
27 #define MAX31865_FAULT_VOLTAGE	      BIT(2)
28 
29 #define MAX31865_FAULT_DETECTION_NONE	  (0x00 << 2)
30 #define MAX31865_FAULT_DETECTION_AUTO	  (0x01 << 2)
31 #define MAX31865_FAULT_DETECTION_MANUAL_1 (0x02 << 2)
32 #define MAX31865_FAULT_DETECTION_MANUAL_2 (0x03 << 2)
33 
34 /* Read Register Address */
35 #define REG_CONFIG	       0x00
36 #define REG_RTD_MSB	       0x01
37 #define REG_RTD_LSB	       0x02
38 #define REG_HIGH_FAULT_THR_MSB 0x03
39 #define REG_HIGH_FAULT_THR_LSB 0x04
40 #define REG_LOW_FAULT_THR_MSB  0x05
41 #define REG_LOW_FAULT_THR_LSB  0x06
42 #define REG_FAULT_STATUS       0x07
43 #define WR(reg)		       ((reg) | 0x80)
44 
45 /* Bitmask to clear fault status bits D5, D3, and D2 */
46 #define FAULT_BITS_CLEAR_MASK 0x2C
47 
48 /**
49  * RTD data, RTD current, and measurement reference
50  * voltage. The ITS-90 standard is used; other RTDs
51  * may have coefficients defined by the DIN 43760 or
52  * the U.S. Industrial (American) standard.
53  */
54 
55 #define RTD_A_ITS90	   3.9080e-3
56 #define RTD_A_USINDUSTRIAL 3.9692e-3
57 #define RTD_A_DIN43760	   3.9848e-3
58 #define RTD_B_ITS90	   -5.870e-7
59 #define RTD_B_USINDUSTRIAL -5.8495e-7
60 #define RTD_B_DIN43760	   -5.8019e-7
61 
62 /**
63  * RTD coefficient C is required only for temperatures
64  * below 0 deg. C.  The selected RTD coefficient set
65  * is specified below.
66  */
67 #define RTD_A (RTD_A_ITS90)
68 #define RTD_B (RTD_B_ITS90)
69 
70 /*
71  * For under zero, taken from
72  * https://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf
73  */
74 static const double RTD_C[6] = {-242.02, 2.2228, 2.5859e-3, 4.8260e-6, 2.8183e-8, 1.5243e-10};
75 
76 struct max31865_data {
77 	double temperature;
78 	uint8_t config_control_bits;
79 };
80 
81 /**
82  * Configuration struct to the MAX31865.
83  */
84 struct max31865_config {
85 	const struct spi_dt_spec spi;
86 	uint16_t resistance_at_zero;
87 	uint16_t resistance_reference;
88 	bool conversion_mode;
89 	bool one_shot;
90 	bool three_wire;
91 	uint8_t fault_cycle;
92 	bool filter_50hz;
93 	uint16_t low_threshold;
94 	uint16_t high_threshold;
95 };
96 
97 /* Bit manipulation macros */
98 #define TESTBIT(data, pos) ((0u == (data & BIT(pos))) ? 0u : 1u)
99 
100 #endif
101