Files
OpenCellular/driver/temp_sensor/thermistor.h
David Hendricks 201211f2dc thermistor: Add generic linear interpolation algorithm
The existing algorithm makes several assumptions for a particular
thermistor circuit. This patch introduces a more generic version
that can be used for multiple thermistors on a single board.

The idea is to approximate a curve produced by solving for voltage
measued by an ADC using the Steinhart-Hart equation. For a straight
line one only needs two data points. For a steady curve data
points can be distributed evenly. For the most part, though, data
points should be provided after a significant change in slope.

More data points give more accuracy at the expense of memory, and
we mostly only care about accuracy in the range between "warm"
and "too hot" so only a few data points should be used.

BUG=chrome-os-partner:54818
BRANCH=none
TEST=added unit test, needs real testing

Change-Id: I046e61dbfd1e8c26c2a533777f222f5413938556
Signed-off-by: David Hendricks <dhendrix@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/344781
Reviewed-by: Shawn N <shawnn@chromium.org>
2016-08-01 20:01:49 -07:00

59 lines
1.7 KiB
C

/* Copyright 2015 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.
*/
/* Thermistor module for Chrome EC */
#ifndef __CROS_EC_TEMP_SENSOR_THERMISTOR_H
#define __CROS_EC_TEMP_SENSOR_THERMISTOR_H
struct thermistor_data_pair {
uint8_t mv; /* Scaled voltage level at ADC (in mV) */
uint8_t temp; /* Temperature in Celsius */
};
struct thermistor_info {
uint8_t scaling_factor; /* Scaling factor for voltage in data pair. */
uint8_t num_pairs; /* Number of data pairs. */
/*
* Values between given data pairs will be calculated as points on
* a line. Pairs can be derived using the Steinhart-Hart equation.
*
* Guidelines for data sets:
* - Must contain at least two pairs.
* - First and last pairs are the min and max.
* - Pairs must be sorted in ascending order.
* - 5 pairs should provide reasonable accuracy in most cases. Use
* points where the slope changes significantly or to recalibrate
* the algorithm if needed.
*/
const struct thermistor_data_pair *data;
};
/**
* @brief Calculate temperature using linear interpolation of data points.
*
* Given a set of datapoints, the algorithm will calculate the "step" in
* between each one in order to interpolate missing entries.
*
* @param mv Value read from ADC (in millivolts).
* @param info Reference data set and info.
*
* @return temperature in C
*/
int thermistor_linear_interpolate(uint16_t mv,
const struct thermistor_info *info);
/**
* ncp15wb temperature conversion routine.
*
* @param adc 10bit raw data on adc.
*
* @return temperature in C.
*/
int ncp15wb_calculate_temp(uint16_t adc);
#endif /* __CROS_EC_TEMP_SENSOR_THERMISTOR_NCP15WB_H */