2016-05-18 15:24:46 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-05-18 15:24:46 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SENSOR_LIS3MDL_H__
|
|
|
|
#define __SENSOR_LIS3MDL_H__
|
|
|
|
|
|
|
|
#include <device.h>
|
|
|
|
#include <misc/util.h>
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 23:32:08 +08:00
|
|
|
#include <zephyr/types.h>
|
2016-05-18 15:24:46 +08:00
|
|
|
#include <gpio.h>
|
|
|
|
|
|
|
|
#define SYS_LOG_DOMAIN "LIS3MDL"
|
2016-10-06 00:49:41 +08:00
|
|
|
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_SENSOR_LEVEL
|
2016-12-18 01:56:56 +08:00
|
|
|
#include <logging/sys_log.h>
|
2016-05-18 15:24:46 +08:00
|
|
|
|
2017-05-04 17:49:15 +08:00
|
|
|
#define LIS3MDL_I2C_ADDR_BASE 0x1C
|
|
|
|
#define LIS3MDL_I2C_ADDR_MASK (~BIT(1))
|
2016-05-18 15:24:46 +08:00
|
|
|
|
|
|
|
/* guard against invalid CONFIG_I2C_ADDR values */
|
|
|
|
#if (CONFIG_LIS3MDL_I2C_ADDR & LIS3MDL_I2C_ADDR_MASK) != LIS3MDL_I2C_ADDR_BASE
|
|
|
|
#error "Invalid value for CONFIG_LIS3MDL_I2C_ADDR"
|
|
|
|
#endif
|
|
|
|
|
2017-05-04 17:49:15 +08:00
|
|
|
#define LIS3MDL_REG_WHO_AM_I 0x0F
|
|
|
|
#define LIS3MDL_CHIP_ID 0x3D
|
|
|
|
|
|
|
|
#define LIS3MDL_REG_CTRL1 0x20
|
|
|
|
#define LIS3MDL_TEMP_EN_MASK BIT(7)
|
|
|
|
#define LIS3MDL_TEMP_EN_SHIFT 7
|
|
|
|
#define LIS3MDL_OM_MASK (BIT(6) | BIT(5))
|
|
|
|
#define LIS3MDL_OM_SHIFT 5
|
|
|
|
#define LIS3MDL_MAG_DO_MASK (BIT(4) | BIT(3) | BIT(2))
|
|
|
|
#define LIS3MDL_DO_SHIFT 2
|
|
|
|
#define LIS3MDL_FAST_ODR_MASK BIT(1)
|
|
|
|
#define LIS3MDL_FAST_ODR_SHIFT 1
|
|
|
|
#define LIS3MDL_ST_MASK BIT(0)
|
|
|
|
#define LIS3MDL_ST_SHIFT 0
|
2016-05-18 15:24:46 +08:00
|
|
|
|
|
|
|
#define LIS3MDL_ODR_BITS(om_bits, do_bits, fast_odr) \
|
|
|
|
(((om_bits) << LIS3MDL_OM_SHIFT) | \
|
|
|
|
((do_bits) << LIS3MDL_DO_SHIFT) | \
|
|
|
|
((fast_odr) << LIS3MDL_FAST_ODR_SHIFT))
|
|
|
|
|
2017-05-04 17:49:15 +08:00
|
|
|
#define LIS3MDL_REG_CTRL2 0x21
|
|
|
|
#define LIS3MDL_FS_MASK (BIT(6) | BIT(5))
|
|
|
|
#define LIS3MDL_FS_SHIFT 5
|
|
|
|
#define LIS3MDL_REBOOT_MASK BIT(3)
|
|
|
|
#define LIS3MDL_REBOOT_SHIFT 3
|
|
|
|
#define LIS3MDL_SOFT_RST_MASK BIT(2)
|
|
|
|
#define LIS3MDL_SOFT_RST_SHIFT 2
|
|
|
|
|
|
|
|
#define LIS3MDL_FS_IDX ((CONFIG_LIS3MDL_FS / 4) - 1)
|
2016-05-18 15:24:46 +08:00
|
|
|
|
|
|
|
/* guard against invalid CONFIG_LIS3MDL_FS values */
|
|
|
|
#if CONFIG_LIS3MDL_FS % 4 != 0 || LIS3MDL_FS_IDX < -1 || LIS3MDL_FS_IDX >= 4
|
|
|
|
#error "Invalid value for CONFIG_LIS3MDL_FS"
|
|
|
|
#endif
|
|
|
|
|
2017-05-04 17:49:15 +08:00
|
|
|
#define LIS3MDL_REG_CTRL3 0x22
|
|
|
|
#define LIS3MDL_LP_MASK BIT(5)
|
|
|
|
#define LIS3MDL_LP_SHIFT 5
|
|
|
|
#define LIS3MDL_SIM_MASK BIT(2)
|
|
|
|
#define LIS3MDL_SIM_SHIFT 2
|
|
|
|
#define LIS3MDL_MD_MASK (BIT(1) | BIT(0))
|
|
|
|
#define LIS3MDL_MD_SHIFT 0
|
|
|
|
|
|
|
|
#define LIS3MDL_MD_CONTINUOUS 0
|
|
|
|
#define LIS3MDL_MD_SINGLE 1
|
|
|
|
#define LIS3MDL_MD_POWER_DOWN 2
|
|
|
|
#define LIS3MDL_MD_POWER_DOWN_AUTO 3
|
|
|
|
|
|
|
|
#define LIS3MDL_REG_CTRL4 0x23
|
|
|
|
#define LIS3MDL_OMZ_MASK (BIT(3) | BIT(2))
|
|
|
|
#define LIS3MDL_OMZ_SHIFT 2
|
|
|
|
#define LIS3MDL_BLE_MASK BIT(1)
|
|
|
|
#define LIS3MDL_BLE_SHIFT 1
|
|
|
|
|
|
|
|
#define LIS3MDL_REG_CTRL5 0x24
|
|
|
|
#define LIS3MDL_FAST_READ_MASK BIT(7)
|
|
|
|
#define LIS3MDL_FAST_READ_SHIFT 7
|
|
|
|
#define LIS3MDL_BDU_MASK BIT(6)
|
|
|
|
#define LIS3MDL_BDU_SHIFT 6
|
|
|
|
|
|
|
|
#define LIS3MDL_BDU_EN (1 << LIS3MDL_BDU_SHIFT)
|
|
|
|
|
|
|
|
#define LIS3MDL_REG_SAMPLE_START 0x28
|
|
|
|
|
|
|
|
#define LIS3MDL_REG_INT_CFG 0x30
|
|
|
|
#define LIS3MDL_INT_X_EN BIT(7)
|
|
|
|
#define LIS3MDL_INT_Y_EN BIT(6)
|
|
|
|
#define LIS3MDL_INT_Z_EN BIT(5)
|
|
|
|
#define LIS3MDL_INT_XYZ_EN \
|
2016-05-18 15:24:46 +08:00
|
|
|
(LIS3MDL_INT_X_EN | LIS3MDL_INT_Y_EN | LIS3MDL_INT_Z_EN)
|
|
|
|
|
|
|
|
static const char * const lis3mdl_odr_strings[] = {
|
|
|
|
"0.625", "1.25", "2.5", "5", "10", "20",
|
|
|
|
"40", "80", "155", "300", "560", "1000"
|
|
|
|
};
|
|
|
|
|
2017-04-21 23:03:20 +08:00
|
|
|
static const u8_t lis3mdl_odr_bits[] = {
|
2016-05-18 15:24:46 +08:00
|
|
|
LIS3MDL_ODR_BITS(0, 0, 0), /* 0.625 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(0, 1, 0), /* 1.25 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(0, 2, 0), /* 2.5 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(0, 3, 0), /* 5 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(0, 4, 0), /* 10 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(0, 5, 0), /* 20 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(0, 6, 0), /* 40 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(0, 7, 0), /* 80 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(3, 0, 1), /* 155 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(2, 0, 1), /* 300 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(1, 0, 1), /* 560 Hz */
|
|
|
|
LIS3MDL_ODR_BITS(0, 0, 1) /* 1000 Hz */
|
|
|
|
};
|
|
|
|
|
2017-04-21 23:03:20 +08:00
|
|
|
static const u16_t lis3mdl_magn_gain[] = {
|
2016-05-18 15:24:46 +08:00
|
|
|
6842, 3421, 2281, 1711
|
|
|
|
};
|
|
|
|
|
|
|
|
struct lis3mdl_data {
|
|
|
|
struct device *i2c;
|
2017-04-21 23:03:20 +08:00
|
|
|
s16_t x_sample;
|
|
|
|
s16_t y_sample;
|
|
|
|
s16_t z_sample;
|
|
|
|
s16_t temp_sample;
|
2016-05-18 15:24:46 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_LIS3MDL_TRIGGER
|
|
|
|
struct device *gpio;
|
|
|
|
struct gpio_callback gpio_cb;
|
|
|
|
|
|
|
|
struct sensor_trigger data_ready_trigger;
|
|
|
|
sensor_trigger_handler_t data_ready_handler;
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
#if defined(CONFIG_LIS3MDL_TRIGGER_OWN_THREAD)
|
2017-06-03 05:08:45 +08:00
|
|
|
K_THREAD_STACK_MEMBER(thread_stack, CONFIG_LIS3MDL_THREAD_STACK_SIZE);
|
2016-11-10 21:21:34 +08:00
|
|
|
struct k_sem gpio_sem;
|
2017-05-10 02:59:40 +08:00
|
|
|
struct k_thread thread;
|
2016-11-10 23:05:53 +08:00
|
|
|
#elif defined(CONFIG_LIS3MDL_TRIGGER_GLOBAL_THREAD)
|
2016-11-10 21:21:34 +08:00
|
|
|
struct k_work work;
|
2016-05-18 15:24:46 +08:00
|
|
|
struct device *dev;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_LIS3MDL_TRIGGER */
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_LIS3MDL_TRIGGER
|
|
|
|
int lis3mdl_trigger_set(struct device *dev,
|
|
|
|
const struct sensor_trigger *trig,
|
|
|
|
sensor_trigger_handler_t handler);
|
|
|
|
|
|
|
|
int lis3mdl_sample_fetch(struct device *dev, enum sensor_channel chan);
|
|
|
|
|
|
|
|
int lis3mdl_init_interrupt(struct device *dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __SENSOR_LIS3MDL__ */
|