summaryrefslogtreecommitdiff
path: root/driver/temp_sensor/thermistor.c
blob: ffa780cb07a2235bd76dab7910a3120ed0ce588b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
 * Common thermistor code for Chrome EC. See ./thermistor.md for table of
 * resistance of a 47kΩ B4050 thermistor
 */

#include "adc.h"
#include "common.h"
#include "gpio.h"
#include "temp_sensor/thermistor.h"
#include "util.h"

int thermistor_linear_interpolate(uint16_t mv,
		const struct thermistor_info *info)
{
	const struct thermistor_data_pair *data = info->data;
	int v_high = 0, v_low = 0, t_low, t_high, num_steps;
	int head, tail, mid = 0;

	/* We need at least two points to form a line. */
	ASSERT(info->num_pairs >= 2);

	/*
	 * If input value is out of bounds return the lowest or highest
	 * value in the data sets provided.
	 */
	if (mv > data[0].mv * info->scaling_factor)
		return data[0].temp;
	else if (mv < data[info->num_pairs - 1].mv * info->scaling_factor)
		return data[info->num_pairs - 1].temp;

	head = 0;
	tail = info->num_pairs - 1;
	while (head != tail) {
		mid = (head + tail) / 2;
		v_high = data[mid].mv * info->scaling_factor;
		v_low = data[mid + 1].mv * info->scaling_factor;

		if ((mv <= v_high) && (mv >= v_low))
			break;
		else if (mv > v_high)
			tail = mid;
		else if (mv < v_low)
			head = mid + 1;
	}

	t_low = data[mid].temp;
	t_high = data[mid + 1].temp;

	/*
	 * The obvious way of doing this is to figure out how many mV per
	 * degree are in between the two points (mv_per_deg_c), and then how
	 * many of those exist between the input voltage and voltage of
	 * lower temperature :
	 *   1. mv_per_deg_c = (v_high - v_low) / (t_high - t_low)
	 *   2. num_steps = (v_high - mv) / mv_per_deg_c
	 *   3. result = t_low + num_steps
	 *
	 * Combine #1 and #2 to mitigate precision loss due to integer division.
	 */
	num_steps = ((v_high - mv) * (t_high - t_low)) / (v_high - v_low);
	return t_low + num_steps;
}

#if defined(CONFIG_STEINHART_HART_3V3_51K1_47K_4050B) || \
	defined(CONFIG_STEINHART_HART_3V3_13K7_47K_4050B) || \
	defined(CONFIG_STEINHART_HART_6V0_51K1_47K_4050B) || \
	defined(CONFIG_STEINHART_HART_3V0_22K6_47K_4050B) || \
	defined(CONFIG_STEINHART_HART_3V3_30K9_47K_4050B) || \
	defined(CONFIG_ZEPHYR)
int thermistor_get_temperature(int idx_adc, int *temp_ptr,
			       const struct thermistor_info *info)
{
	int mv;

#ifdef CONFIG_TEMP_SENSOR_POWER_GPIO
	/*
	 * If the power rail for the thermistor circuit is not enabled, then
	 * need to ignore any ADC measurments.
	 */
	if (!gpio_get_level(CONFIG_TEMP_SENSOR_POWER_GPIO))
		return EC_ERROR_NOT_POWERED;
#endif /* CONFIG_TEMP_SENSOR_POWER_GPIO */
	mv = adc_read_channel(idx_adc);
	if (mv < 0)
		return EC_ERROR_UNKNOWN;

	*temp_ptr = thermistor_linear_interpolate(mv, info);
	*temp_ptr = C_TO_K(*temp_ptr);
	return EC_SUCCESS;
}
#endif

#ifdef CONFIG_STEINHART_HART_3V3_51K1_47K_4050B
/*
 * Data derived from Steinhart-Hart equation in a resistor divider circuit with
 * Vdd=3300mV, R = 51.1Kohm, and thermistor (B = 4050, T0 = 298.15 K, nominal
 * resistance (R0) = 47Kohm).
 */
#define THERMISTOR_SCALING_FACTOR_51_47 11
static const struct thermistor_data_pair thermistor_data_51_47[] = {
	{ 2484 / THERMISTOR_SCALING_FACTOR_51_47, 0   },
	{ 2142 / THERMISTOR_SCALING_FACTOR_51_47, 10  },
	{ 1767 / THERMISTOR_SCALING_FACTOR_51_47, 20  },
	{ 1400 / THERMISTOR_SCALING_FACTOR_51_47, 30  },
	{ 1072 / THERMISTOR_SCALING_FACTOR_51_47, 40  },
	{  802 / THERMISTOR_SCALING_FACTOR_51_47, 50  },
	{  593 / THERMISTOR_SCALING_FACTOR_51_47, 60  },
	{  436 / THERMISTOR_SCALING_FACTOR_51_47, 70  },
	{  321 / THERMISTOR_SCALING_FACTOR_51_47, 80  },
	{  276 / THERMISTOR_SCALING_FACTOR_51_47, 85  },
	{  237 / THERMISTOR_SCALING_FACTOR_51_47, 90  },
	{  204 / THERMISTOR_SCALING_FACTOR_51_47, 95  },
	{  177 / THERMISTOR_SCALING_FACTOR_51_47, 100 },
};

static const struct thermistor_info thermistor_info_51_47 = {
	.scaling_factor = THERMISTOR_SCALING_FACTOR_51_47,
	.num_pairs = ARRAY_SIZE(thermistor_data_51_47),
	.data = thermistor_data_51_47,
};

int get_temp_3v3_51k1_47k_4050b(int idx_adc, int *temp_ptr)
{
	return thermistor_get_temperature(idx_adc, temp_ptr,
			&thermistor_info_51_47);
}
#endif /* CONFIG_STEINHART_HART_3V3_51K1_47K_4050B */

#ifdef CONFIG_STEINHART_HART_3V3_13K7_47K_4050B
/*
 * Data derived from Steinhart-Hart equation in a resistor divider circuit with
 * Vdd=3300mV, R = 13.7Kohm, and thermistor (B = 4050, T0 = 298.15 K, nominal
 * resistance (R0) = 47Kohm).
 */
#define THERMISTOR_SCALING_FACTOR_13_47 13
static const struct thermistor_data_pair thermistor_data_13_47[] = {
	{ 3033 / THERMISTOR_SCALING_FACTOR_13_47, 0   },
	{ 2882 / THERMISTOR_SCALING_FACTOR_13_47, 10  },
	{ 2677 / THERMISTOR_SCALING_FACTOR_13_47, 20  },
	{ 2420 / THERMISTOR_SCALING_FACTOR_13_47, 30  },
	{ 2119 / THERMISTOR_SCALING_FACTOR_13_47, 40  },
	{ 1799 / THERMISTOR_SCALING_FACTOR_13_47, 50  },
	{ 1485 / THERMISTOR_SCALING_FACTOR_13_47, 60  },
	{ 1197 / THERMISTOR_SCALING_FACTOR_13_47, 70  },
	{  947 / THERMISTOR_SCALING_FACTOR_13_47, 80  },
	{  839 / THERMISTOR_SCALING_FACTOR_13_47, 85  },
	{  741 / THERMISTOR_SCALING_FACTOR_13_47, 90  },
	{  653 / THERMISTOR_SCALING_FACTOR_13_47, 95  },
	{  576 / THERMISTOR_SCALING_FACTOR_13_47, 100 },
};

static const struct thermistor_info thermistor_info_13_47 = {
	.scaling_factor = THERMISTOR_SCALING_FACTOR_13_47,
	.num_pairs = ARRAY_SIZE(thermistor_data_13_47),
	.data = thermistor_data_13_47,
};

int get_temp_3v3_13k7_47k_4050b(int idx_adc, int *temp_ptr)
{
	return thermistor_get_temperature(idx_adc, temp_ptr,
			&thermistor_info_13_47);
}
#endif /* CONFIG_STEINHART_HART_3V3_13K7_47K_4050B */

#ifdef CONFIG_STEINHART_HART_6V0_51K1_47K_4050B
/*
 * Data derived from Steinhart-Hart equation in a resistor divider circuit with
 * Vdd=6000mV, R = 51.1Kohm, and thermistor (B = 4050, T0 = 298.15 K, nominal
 * resistance (R0) = 47Kohm).
 */
#define THERMISTOR_SCALING_FACTOR_6V0_51_47 18
static const struct thermistor_data_pair thermistor_data_6v0_51_47[] = {
	{ 4517 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 0   },
	{ 3895 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 10  },
	{ 3214 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 20  },
	{ 2546 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 30  },
	{ 1950 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 40  },
	{ 1459 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 50  },
	{ 1079 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 60  },
	{  794 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 70  },
	{  584 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 80  },
	{  502 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 85  },
	{  432 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 90  },
	{  372 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 95  },
	{  322 / THERMISTOR_SCALING_FACTOR_6V0_51_47, 100 },
};

static const struct thermistor_info thermistor_info_6v0_51_47 = {
	.scaling_factor = THERMISTOR_SCALING_FACTOR_6V0_51_47,
	.num_pairs = ARRAY_SIZE(thermistor_data_6v0_51_47),
	.data = thermistor_data_6v0_51_47,
};

int get_temp_6v0_51k1_47k_4050b(int idx_adc, int *temp_ptr)
{
	return thermistor_get_temperature(idx_adc, temp_ptr,
			&thermistor_info_6v0_51_47);
}
#endif /* CONFIG_STEINHART_HART_6V0_51K1_47K_4050B */

#ifdef CONFIG_STEINHART_HART_3V0_22K6_47K_4050B
/*
 * Data derived from Steinhart-Hart equation in a resistor divider circuit with
 * Vdd=3000mV, R = 22.6Kohm, and thermistor (B = 4050, T0 = 298.15 K, nominal
 * resistance (R0) = 47Kohm).
 */
#define THERMISTOR_SCALING_FACTOR_22_47 11
static const struct thermistor_data_pair thermistor_data_22_47[] = {
	{ 2619 / THERMISTOR_SCALING_FACTOR_22_47, 0   },
	{ 2421 / THERMISTOR_SCALING_FACTOR_22_47, 10  },
	{ 2168 / THERMISTOR_SCALING_FACTOR_22_47, 20  },
	{ 1875 / THERMISTOR_SCALING_FACTOR_22_47, 30  },
	{ 1563 / THERMISTOR_SCALING_FACTOR_22_47, 40  },
	{ 1262 / THERMISTOR_SCALING_FACTOR_22_47, 50  },
	{  994 / THERMISTOR_SCALING_FACTOR_22_47, 60  },
	{  769 / THERMISTOR_SCALING_FACTOR_22_47, 70  },
	{  588 / THERMISTOR_SCALING_FACTOR_22_47, 80  },
	{  513 / THERMISTOR_SCALING_FACTOR_22_47, 85  },
	{  448 / THERMISTOR_SCALING_FACTOR_22_47, 90  },
	{  390 / THERMISTOR_SCALING_FACTOR_22_47, 95  },
	{  340 / THERMISTOR_SCALING_FACTOR_22_47, 100 },
};

static const struct thermistor_info thermistor_info_22_47 = {
	.scaling_factor = THERMISTOR_SCALING_FACTOR_22_47,
	.num_pairs = ARRAY_SIZE(thermistor_data_22_47),
	.data = thermistor_data_22_47,
};

int get_temp_3v0_22k6_47k_4050b(int idx_adc, int *temp_ptr)
{
	return thermistor_get_temperature(idx_adc, temp_ptr,
			&thermistor_info_22_47);
}
#endif /* CONFIG_STEINHART_HART_3V0_22K6_47K_4050B */

#ifdef CONFIG_STEINHART_HART_3V3_30K9_47K_4050B
/*
 * Data derived from Steinhart-Hart equation in a resistor divider circuit with
 * Vdd=3300mV, R = 30.9Kohm, and thermistor (B = 4050, T0 = 298.15 K, nominal
 * resistance (R0) = 47Kohm).
 */
#define THERMISTOR_SCALING_FACTOR_31_47 11
static const struct thermistor_data_pair thermistor_data_31_47[] = {
	{ 2753 / THERMISTOR_SCALING_FACTOR_31_47, 0   },
	{ 2487 / THERMISTOR_SCALING_FACTOR_31_47, 10  },
	{ 2165 / THERMISTOR_SCALING_FACTOR_31_47, 20  },
	{ 1813 / THERMISTOR_SCALING_FACTOR_31_47, 30  },
	{ 1145 / THERMISTOR_SCALING_FACTOR_31_47, 50  },
	{  878 / THERMISTOR_SCALING_FACTOR_31_47, 60  },
	{  665 / THERMISTOR_SCALING_FACTOR_31_47, 70  },
	{  500 / THERMISTOR_SCALING_FACTOR_31_47, 80  },
	{  375 / THERMISTOR_SCALING_FACTOR_31_47, 90  },
	{  282 / THERMISTOR_SCALING_FACTOR_31_47, 100 },
};

static const struct thermistor_info thermistor_info_31_47 = {
	.scaling_factor = THERMISTOR_SCALING_FACTOR_31_47,
	.num_pairs = ARRAY_SIZE(thermistor_data_31_47),
	.data = thermistor_data_31_47,
};

int get_temp_3v3_30k9_47k_4050b(int idx_adc, int *temp_ptr)
{
	return thermistor_get_temperature(idx_adc, temp_ptr,
			&thermistor_info_31_47);
}
#endif /* CONFIG_STEINHART_HART_3V3_30K9_47K_4050B */