summaryrefslogtreecommitdiff
path: root/common/online_calibration.c
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2020-03-09 11:59:52 -0600
committerCommit Bot <commit-bot@chromium.org>2020-06-14 02:33:37 +0000
commit13d9ff85b01bf69c78e4e3623797b4c1f5c56ffa (patch)
tree448d5b0b758cf594363ff24e6ca5d5ba7dbb94f1 /common/online_calibration.c
parentbc66c458cf35a122f4f50e111be834e69410d996 (diff)
downloadchrome-ec-13d9ff85b01bf69c78e4e3623797b4c1f5c56ffa.tar.gz
common: online_calibration: Add support for magnetometer
Add support for magnetometers in online calibration. BRANCH=None BUG=b:138303797,chromium:1023858 TEST=Added new unit test Change-Id: I3a6dafb2f5fab9b11ac8bd3b53ae4976002d18cd Signed-off-by: Yuval Peress <peress@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2095519
Diffstat (limited to 'common/online_calibration.c')
-rw-r--r--common/online_calibration.c44
1 files changed, 36 insertions, 8 deletions
diff --git a/common/online_calibration.c b/common/online_calibration.c
index dd2c154260..6c090a6abc 100644
--- a/common/online_calibration.c
+++ b/common/online_calibration.c
@@ -8,6 +8,7 @@
#include "hwtimer.h"
#include "online_calibration.h"
#include "common.h"
+#include "mag_cal.h"
#include "util.h"
#include "vec3.h"
#include "task.h"
@@ -108,8 +109,11 @@ void online_calibration_init(void)
switch (s->type) {
case MOTIONSENSE_TYPE_ACCEL: {
- accel_cal_reset((struct accel_cal *)
- type_specific_data);
+ accel_cal_reset((struct accel_cal *)type_specific_data);
+ break;
+ }
+ case MOTIONSENSE_TYPE_MAG: {
+ init_mag_cal((struct mag_cal_t *)type_specific_data);
break;
}
default:
@@ -153,14 +157,11 @@ int online_calibration_process_data(
struct motion_sensor_t *sensor,
uint32_t timestamp)
{
+ size_t sensor_num = motion_sensors - sensor;
int rc;
int temperature;
struct online_calib_data *calib_data;
- rc = get_temperature(sensor, &temperature);
- if (rc != EC_SUCCESS)
- return rc;
-
calib_data = sensor->online_calib_data;
switch (sensor->type) {
case MOTIONSENSE_TYPE_ACCEL: {
@@ -168,11 +169,14 @@ int online_calibration_process_data(
(struct accel_cal *)(calib_data->type_specific_data);
fpv3_t fdata;
+ /* Temperature is required for accelerometer calibration. */
+ rc = get_temperature(sensor, &temperature);
+ if (rc != EC_SUCCESS)
+ return rc;
+
data_int16_to_fp(sensor, data->data, fdata);
if (accel_cal_accumulate(cal, timestamp, fdata[X], fdata[Y],
fdata[Z], temperature)) {
- int sensor_num = motion_sensors - sensor;
-
mutex_lock(&g_calib_cache_mutex);
/* Convert result to the right scale. */
data_fp_to_int16(sensor, cal->bias, calib_data->cache);
@@ -185,6 +189,30 @@ int online_calibration_process_data(
}
break;
}
+ case MOTIONSENSE_TYPE_MAG: {
+ struct mag_cal_t *cal =
+ (struct mag_cal_t *) (calib_data->type_specific_data);
+ int idata[] = {
+ (int)data->data[X],
+ (int)data->data[Y],
+ (int)data->data[Z],
+ };
+
+ if (mag_cal_update(cal, idata)) {
+ mutex_lock(&g_calib_cache_mutex);
+ /* Copy the values */
+ calib_data->cache[X] = cal->bias[X];
+ calib_data->cache[Y] = cal->bias[Y];
+ calib_data->cache[Z] = cal->bias[Z];
+ /* Set valid and dirty. */
+ sensor_calib_cache_valid_map |= BIT(sensor_num);
+ sensor_calib_cache_dirty_map |= BIT(sensor_num);
+ mutex_unlock(&g_calib_cache_mutex);
+ /* Notify the AP. */
+ mkbp_send_event(EC_MKBP_EVENT_ONLINE_CALIBRATION);
+ }
+ break;
+ }
default:
break;
}