summaryrefslogtreecommitdiff
path: root/driver/temp_sensor/thermistor_ncp15wb.c
diff options
context:
space:
mode:
authorRavi Chandra Sadineni <ravisadineni@chromium.org>2016-08-15 18:24:15 -0700
committerchrome-bot <chrome-bot@chromium.org>2016-08-17 22:15:35 -0700
commit2d5827682d37e7930373790576deb31c91996f1c (patch)
treef01fc88b1214475a931635458109c7bece54d0ff /driver/temp_sensor/thermistor_ncp15wb.c
parent01732ed87cd42a6d4e8767c4b53cce8f02b6c2e9 (diff)
downloadchrome-ec-2d5827682d37e7930373790576deb31c91996f1c.tar.gz
Correct interpolation algorithm of thermal values.
Signed-off-by: Ravi Chandra Sadineni <ravisadineni@chromium.org> BRANCH=none BUG=chrome-os-partner:56206 TEST=make buildall -j Change-Id: I22ac65f33b27b8ce8ad0687971f47b82395aa781 Reviewed-on: https://chromium-review.googlesource.com/370402 Commit-Ready: David Hendricks <dhendrix@chromium.org> Commit-Ready: Ravi Chandra Sadineni <ravisadineni@chromium.org> Tested-by: Ravi Chandra Sadineni <ravisadineni@chromium.org> Reviewed-by: David Hendricks <dhendrix@chromium.org>
Diffstat (limited to 'driver/temp_sensor/thermistor_ncp15wb.c')
-rw-r--r--driver/temp_sensor/thermistor_ncp15wb.c35
1 files changed, 18 insertions, 17 deletions
diff --git a/driver/temp_sensor/thermistor_ncp15wb.c b/driver/temp_sensor/thermistor_ncp15wb.c
index 64ba9fea40..dae8251575 100644
--- a/driver/temp_sensor/thermistor_ncp15wb.c
+++ b/driver/temp_sensor/thermistor_ncp15wb.c
@@ -103,8 +103,8 @@ int thermistor_linear_interpolate(uint16_t mv,
const struct thermistor_info *info)
{
const struct thermistor_data_pair *data = info->data;
- int v0, v1, t0, t1, num_steps;
- int head, tail, mid;
+ 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);
@@ -113,39 +113,40 @@ int thermistor_linear_interpolate(uint16_t mv,
* 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)
+ if (mv > data[0].mv * info->scaling_factor)
return data[0].temp;
- else if (mv > data[info->num_pairs - 1].mv * info->scaling_factor)
+ 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;
- v0 = data[mid].mv * info->scaling_factor;
- v1 = data[mid + 1].mv * info->scaling_factor;
+ v_high = data[mid].mv * info->scaling_factor;
+ v_low = data[mid + 1].mv * info->scaling_factor;
- if ((mv >= v0) && (mv <= v1))
+ if ((mv <= v_high) && (mv >= v_low))
break;
- else if (mv < v0)
+ else if (mv > v_high)
tail = mid;
- else if (mv > v1)
+ else if (mv < v_low)
head = mid + 1;
}
- t0 = data[mid].temp;
- t1 = data[mid + 1].temp;
+ 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 lower voltage (v0):
- * 1. mv_per_deg_c = (v1 - v0) / (t1 - t0)
- * 2. num_steps = (mv - v0) / mv_per_deg_c
- * 3. result = t0 + num_steps
+ * 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 = ((mv - v0) * (t1 - t0)) / (v1 - v0);
- return t0 + num_steps;
+ num_steps = ((v_high - mv) * (t_high - t_low)) / (v_high - v_low);
+ return t_low + num_steps;
}