diff options
author | Yuval Peress <peress@chromium.org> | 2019-10-29 15:02:37 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-02-13 00:56:05 +0000 |
commit | 88808c3dda819f9da22272850d2b56ab1e538c9f (patch) | |
tree | 614c8f28cb17c232ac74ac252b615fcecd554ba6 /common/online_calibration.c | |
parent | 4acc29890130946ad574ea0f314b65497efb7ce2 (diff) | |
download | chrome-ec-88808c3dda819f9da22272850d2b56ab1e538c9f.tar.gz |
common: Migrate online calibration to own module
This change moves the code that handles caching the temperature
(which is the first step in online calibration) into a new
compilational unit.
TEST=None
BRANCH=None
BUG=b:138303429,chromium:1023858
Change-Id: Ib1fe3d2234dc2436e2bbfd4febd22196e5cdafef
Signed-off-by: Yuval Peress <peress@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1906340
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Diffstat (limited to 'common/online_calibration.c')
-rw-r--r-- | common/online_calibration.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/common/online_calibration.c b/common/online_calibration.c new file mode 100644 index 0000000000..5626a70107 --- /dev/null +++ b/common/online_calibration.c @@ -0,0 +1,73 @@ +/* Copyright 2020 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. + */ + +#include "accelgyro.h" +#include "hwtimer.h" +#include "online_calibration.h" +#include "common.h" + +/** Entry of the temperature cache */ +struct temp_cache_entry { + /** The temperature that's cached (-1 if invalid) */ + int temp; + /** The timestamp at which the temperature was cached */ + uint32_t timestamp; +}; + +/** Cache for internal sensor temperatures. */ +static struct temp_cache_entry sensor_temp_cache[SENSOR_COUNT]; + +static int get_temperature(struct motion_sensor_t *sensor, int *temp) +{ + struct temp_cache_entry *entry = + &sensor_temp_cache[motion_sensors - sensor]; + uint32_t now; + + if (sensor->drv->read_temp == NULL) + return EC_ERROR_UNIMPLEMENTED; + + now = __hw_clock_source_read(); + if (entry->temp < 0 || + time_until(entry->timestamp, now) > CONFIG_TEMP_CACHE_STALE_THRES) { + int t; + int rc = sensor->drv->read_temp(sensor, &t); + + if (rc == EC_SUCCESS) { + entry->temp = t; + entry->timestamp = now; + } else { + return rc; + } + } + + *temp = entry->temp; + return EC_SUCCESS; +} + +void online_calibration_init(void) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(sensor_temp_cache); i++) + sensor_temp_cache[i].temp = -1; +} + +int online_calibration_process_data( + struct ec_response_motion_sensor_data *data, + struct motion_sensor_t *sensor, + uint32_t timestamp) +{ + int rc; + int temperature; + + rc = get_temperature(sensor, &temperature); + /* + * TODO actual implementation will come in following CLs. + * The goal of this change is to establish the interface of + * online calibration with the rest of the code base. + */ + return rc; +} + |