summaryrefslogtreecommitdiff
path: root/driver/stm_mems_common.h
diff options
context:
space:
mode:
authorMario Tesi <mario.tesi@st.com>2016-11-18 09:48:42 +0100
committerchrome-bot <chrome-bot@chromium.org>2017-02-09 03:10:22 -0800
commit8db50ce410cd71a6b4429fb91a3be9e103312f8f (patch)
treedef49c0684d3d0b1e10491ed475afb9ebbc1eb22 /driver/stm_mems_common.h
parentb6c5e8e7a2edee6f167fe1282db6e1263fa6988e (diff)
downloadchrome-ec-8db50ce410cd71a6b4429fb91a3be9e103312f8f.tar.gz
driver: accel: Add acc driver basics and FIFO for LIS2DH/LIS2DH12
Add driver for acc sensor ST lis2dh/lis2dh12 Support interrupt management for FIFO watermark Starting to share common code with other devices like lsm6dsm/lsm6dsl (acc/gyro) or new lis2mdl (mag) TODO: Add all embedded functions support (click, tap and so on) BUG=none BRANCH=master TEST=Tested on discovery BOARD with sensor connected on EC i2c master bus. Added motion sense task on discovery board task list, added gpio info in board configuration file and tested with motion sense console commands. Data for acc seems ok: can successfully change ODR and full scale range. Also FIFO and interrupt tested Device tested is lis2dh (lis2dh12 simply differs for low pin count but share the same registers) Change-Id: I16abeac3f139a604094b38d8d8b857a62c93a242 Signed-off-by: Mario Tesi <mario.tesi@st.com> Reviewed-on: https://chromium-review.googlesource.com/412700 Commit-Ready: mario tesi <mario.tesi@st.com> Tested-by: mario tesi <mario.tesi@st.com> Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
Diffstat (limited to 'driver/stm_mems_common.h')
-rw-r--r--driver/stm_mems_common.h127
1 files changed, 127 insertions, 0 deletions
diff --git a/driver/stm_mems_common.h b/driver/stm_mems_common.h
new file mode 100644
index 0000000000..ca08de95de
--- /dev/null
+++ b/driver/stm_mems_common.h
@@ -0,0 +1,127 @@
+/* Copyright (c) 2016 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.
+ */
+
+/* Commons acc/gyro function for ST sensors oin Chrome EC */
+
+#ifndef __CROS_EC_ST_COMMONS_H
+#define __CROS_EC_ST_COMMONS_H
+
+#include "common.h"
+#include "util.h"
+#include "accelgyro.h"
+#include "console.h"
+#include "i2c.h"
+#include "driver/accel_lis2dh.h"
+
+/* Common debug funcions */
+#define CPRINTF(format, args...) cprintf(CC_ACCEL, format "\n", ## args)
+
+/* Set auto increment subaddress on multiple access */
+#define AUTO_INC 0x80
+
+/* X, Y, Z axis data len */
+#define OUT_XYZ_SIZE 6
+
+/* Common define for poswer off ODR */
+#define ODR_POWER_OFF_VAL 0x00
+
+#ifdef CONFIG_ACCEL_FIFO
+#define FIFO_BUFFER_NUM_PATTERN 16
+/* Define number of data to be read from FIFO each time
+ * It must be a multiple of OUT_XYZ_SIZE.
+ * In case of LSM6DSM FIFO contains pattern depending ODR
+ * of Acc/gyro, be sure that FIFO can contains almost
+ * FIFO_BUFFER_NUM_PATTERNpattern
+ */
+#define FIFO_READ_LEN (FIFO_BUFFER_NUM_PATTERN * OUT_XYZ_SIZE)
+
+#endif /* CONFIG_ACCEL_FIFO */
+
+/**
+ * Read single register
+ */
+inline int raw_read8(const int port, const int addr, const int reg,
+ int *data_ptr);
+
+/**
+ * Write single register
+ */
+inline int raw_write8(const int port, const int addr, const int reg,
+ int data);
+
+/**
+ * Read n bytes for read
+ * NOTE: Some chip use MSB for auto-increments in SUB address
+ * MSB must be set for autoincrement in multi read when auto_inc
+ * is set
+ */
+int raw_read_n(const int port, const int addr, const uint8_t reg,
+ uint8_t *data_ptr, const int len, int auto_inc);
+
+ /**
+ * write_data_with_mask - Write register with mask
+ * @s: Motion sensor pointer
+ * @reg: Device register
+ * @mask: The mask to search
+ * @data: Data pointer
+ */
+int write_data_with_mask(const struct motion_sensor_t *s, int reg,
+ uint8_t mask, uint8_t data);
+
+ /**
+ * set_resolution - Set bit resolution
+ * @s: Motion sensor pointer
+ * @res: Bit resolution
+ * @rnd: Round bit
+ */
+int set_resolution(const struct motion_sensor_t *s, int res, int rnd);
+
+ /**
+ * get_resolution - Get bit resolution
+ * @s: Motion sensor pointer
+ *
+ * TODO: must support multiple resolution
+ */
+int get_resolution(const struct motion_sensor_t *s);
+
+/**
+ * set_offset - Set data offset
+ * @s: Motion sensor pointer
+ * @offset: offset vector
+ * @temp: Temp
+ */
+int set_offset(const struct motion_sensor_t *s,
+ const int16_t *offset, int16_t temp);
+
+/**
+ * get_offset - Get data offset
+ * @s: Motion sensor pointer
+ * @offset: offset vector
+ * @temp: Temp
+ */
+int get_offset(const struct motion_sensor_t *s, int16_t *offset, int16_t *temp);
+
+/**
+ * get_data_rate - Get data rate (ODR)
+ * @s: Motion sensor pointer
+ */
+int get_data_rate(const struct motion_sensor_t *s);
+
+/**
+ * normalize - Apply to LSB data sensitivity and rotation
+ * @s: Motion sensor pointer
+ * @v: vector
+ * @data: LSB raw data
+ */
+void normalize(const struct motion_sensor_t *s, vector_3_t v, uint8_t *data);
+
+/* Internal data structure for sensors */
+struct stprivate_data {
+ struct accelgyro_saved_data_t base;
+ int16_t offset[3];
+ uint8_t resol;
+};
+
+#endif /* __CROS_EC_ST_COMMONS_H */