From e2a6debab916e727e6917af1dd3fcce0dd4a9dc3 Mon Sep 17 00:00:00 2001 From: "andy.zwj" Date: Thu, 8 Feb 2018 15:05:18 +0800 Subject: [PATCH] add lsm6dsl and lps22hb driver --- device/sensor/drv/drv_acc_bosch_bma253.c | 20 +- device/sensor/drv/drv_acc_gyro_st_lsm6dsl.c | 846 ++++++++++++++++++ device/sensor/drv/drv_baro_bosch_bmp280.c | 12 +- device/sensor/drv/drv_baro_st_lps22hb.c | 420 +++++++++ device/sensor/drv/drv_gyro_bosch_bmg160.c | 19 +- device/sensor/drv/drv_humi_bosch_bme280.c | 17 +- device/sensor/hal/sensor_hal.c | 15 +- device/sensor/sensor.mk | 7 +- example/uDataapp/uData_sample.c | 2 +- .../uData/abs_data_model/abs_data_model.c | 9 +- framework/uData/service_mgr/service_mgr.c | 30 +- framework/uData/uData_interface.c | 14 +- include/aos/uData.h | 3 +- include/hal/sensor.h | 4 +- 14 files changed, 1365 insertions(+), 53 deletions(-) create mode 100644 device/sensor/drv/drv_acc_gyro_st_lsm6dsl.c create mode 100644 device/sensor/drv/drv_baro_st_lps22hb.c diff --git a/device/sensor/drv/drv_acc_bosch_bma253.c b/device/sensor/drv/drv_acc_bosch_bma253.c index a025539ac3..ac94f88867 100644 --- a/device/sensor/drv/drv_acc_bosch_bma253.c +++ b/device/sensor/drv/drv_acc_bosch_bma253.c @@ -189,7 +189,7 @@ static uint32_t current_factor = 0; i2c_dev_t bma253_ctx = { .port = 1, - .config.address_width = 7, + .config.address_width = 8, .config.freq = 400000, .config.dev_addr = BMA253_I2C_ADDR1, }; @@ -204,7 +204,7 @@ static int drv_acc_bosch_bma253_soft_reset(i2c_dev_t* drv) return 0; } -static int drv_acc_bosch_bma253_validate_id(i2c_dev_t* drv, uint8_t id_addr, uint8_t id_value) +static int drv_acc_bosch_bma253_validate_id(i2c_dev_t* drv, uint8_t id_value) { uint8_t value = 0x00; int ret = 0; @@ -213,7 +213,7 @@ static int drv_acc_bosch_bma253_validate_id(i2c_dev_t* drv, uint8_t id_addr, ui return -1; } - ret = sensor_i2c_read(drv, id_addr, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + ret = sensor_i2c_read(drv, BMA253_CHIP_ID_ADDR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } @@ -411,12 +411,18 @@ static int drv_acc_bosch_bma253_close(void) static int drv_acc_bosch_bma253_read(void *buf, size_t len) { int ret = 0; + size_t size; uint8_t reg[6]; - accel_data_t *accel = buf; + accel_data_t *accel = (accel_data_t *)buf; if(buf == NULL){ return -1; } + size = sizeof(accel_data_t); + if(len < size){ + return -1; + } + ret = sensor_i2c_read(&bma253_ctx, BMA253_X_AXIS_LSB_ADDR, ®[0], I2C_REG_LEN, I2C_OP_RETRIES); ret |= sensor_i2c_read(&bma253_ctx, BMA253_X_AXIS_MSB_ADDR, ®[1], I2C_REG_LEN, I2C_OP_RETRIES); ret |= sensor_i2c_read(&bma253_ctx, BMA253_Y_AXIS_LSB_ADDR, ®[2], I2C_REG_LEN, I2C_OP_RETRIES); @@ -443,8 +449,8 @@ static int drv_acc_bosch_bma253_read(void *buf, size_t len) accel->data[DATA_AXIS_Z] = accel->data[DATA_AXIS_Z] * ACCELEROMETER_UNIT_FACTOR / current_factor; } accel->timestamp = aos_now_ms(); - len = sizeof(accel_data_t); - return 0; + + return (int)size; } static int drv_acc_bosch_bma253_ioctl(int cmd, unsigned long arg) @@ -506,7 +512,7 @@ int drv_acc_bosch_bma253_init(void){ return -1; } - ret = drv_acc_bosch_bma253_validate_id(&bma253_ctx, BMA253_CHIP_ID_ADDR, BMA253_CHIP_ID_VALUE); + ret = drv_acc_bosch_bma253_validate_id(&bma253_ctx, BMA253_CHIP_ID_VALUE); if(unlikely(ret)){ return -1; } diff --git a/device/sensor/drv/drv_acc_gyro_st_lsm6dsl.c b/device/sensor/drv/drv_acc_gyro_st_lsm6dsl.c new file mode 100644 index 0000000000..9c6ca3d0d2 --- /dev/null +++ b/device/sensor/drv/drv_acc_gyro_st_lsm6dsl.c @@ -0,0 +1,846 @@ +/* +* Copyright (C) 2015-2017 Alibaba Group Holding Limited +* +* +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "common.h" +#include "hal/sensor.h" + + +#define LSM6DSL_I2C_ADDR1 (0x6A) +#define LSM6DSL_I2C_ADDR2 (0x6B) +#define LSM6DSL_I2C_ADDR_TRANS(n) ((n)<<1) +#define LSM6DSL_I2C_ADDR LSM6DSL_I2C_ADDR_TRANS(LSM6DSL_I2C_ADDR1) + +#define LSM6DSL_ACC_GYRO_FUNC_CFG_ACCESS 0x01 +#define LSM6DSL_ACC_GYRO_SENSOR_SYNC_TIME 0x04 +#define LSM6DSL_ACC_GYRO_SENSOR_RES_RATIO 0x05 +#define LSM6DSL_ACC_GYRO_FIFO_CTRL1 0x06 +#define LSM6DSL_ACC_GYRO_FIFO_CTRL2 0x07 +#define LSM6DSL_ACC_GYRO_FIFO_CTRL3 0x08 +#define LSM6DSL_ACC_GYRO_FIFO_CTRL4 0x09 +#define LSM6DSL_ACC_GYRO_FIFO_CTRL5 0x0A +#define LSM6DSL_ACC_GYRO_DRDY_PULSE_CFG_G 0x0B +#define LSM6DSL_ACC_GYRO_INT1_CTRL 0x0D +#define LSM6DSL_ACC_GYRO_INT2_CTRL 0x0E +#define LSM6DSL_ACC_GYRO_WHO_AM_I_REG 0x0F +#define LSM6DSL_ACC_GYRO_CTRL1_XL 0x10 +#define LSM6DSL_ACC_GYRO_CTRL2_G 0x11 +#define LSM6DSL_ACC_GYRO_CTRL3_C 0x12 +#define LSM6DSL_ACC_GYRO_CTRL4_C 0x13 +#define LSM6DSL_ACC_GYRO_CTRL5_C 0x14 +#define LSM6DSL_ACC_GYRO_CTRL6_C 0x15 +#define LSM6DSL_ACC_GYRO_CTRL7_G 0x16 +#define LSM6DSL_ACC_GYRO_CTRL8_XL 0x17 +#define LSM6DSL_ACC_GYRO_CTRL9_XL 0x18 +#define LSM6DSL_ACC_GYRO_CTRL10_C 0x19 + + +#define LSM6DSL_ACC_GYRO_MASTER_CONFIG 0x1A +#define LSM6DSL_ACC_GYRO_WAKE_UP_SRC 0x1B +#define LSM6DSL_ACC_GYRO_TAP_SRC 0x1C +#define LSM6DSL_ACC_GYRO_D6D_SRC 0x1D +#define LSM6DSL_ACC_GYRO_STATUS_REG 0x1E + +#define LSM6DSL_ACC_GYRO_OUT_TEMP_L 0x20 +#define LSM6DSL_ACC_GYRO_OUT_TEMP_H 0x21 +#define LSM6DSL_ACC_GYRO_OUTX_L_G 0x22 +#define LSM6DSL_ACC_GYRO_OUTX_H_G 0x23 +#define LSM6DSL_ACC_GYRO_OUTY_L_G 0x24 +#define LSM6DSL_ACC_GYRO_OUTY_H_G 0x25 +#define LSM6DSL_ACC_GYRO_OUTZ_L_G 0x26 +#define LSM6DSL_ACC_GYRO_OUTZ_H_G 0x27 +#define LSM6DSL_ACC_GYRO_OUTX_L_XL 0x28 +#define LSM6DSL_ACC_GYRO_OUTX_H_XL 0x29 +#define LSM6DSL_ACC_GYRO_OUTY_L_XL 0x2A +#define LSM6DSL_ACC_GYRO_OUTY_H_XL 0x2B +#define LSM6DSL_ACC_GYRO_OUTZ_L_XL 0x2C +#define LSM6DSL_ACC_GYRO_OUTZ_H_XL 0x2D +#define LSM6DSL_ACC_GYRO_SENSORHUB1_REG 0x2E +#define LSM6DSL_ACC_GYRO_SENSORHUB2_REG 0x2F +#define LSM6DSL_ACC_GYRO_SENSORHUB3_REG 0x30 +#define LSM6DSL_ACC_GYRO_SENSORHUB4_REG 0x31 +#define LSM6DSL_ACC_GYRO_SENSORHUB5_REG 0x32 +#define LSM6DSL_ACC_GYRO_SENSORHUB6_REG 0x33 +#define LSM6DSL_ACC_GYRO_SENSORHUB7_REG 0x34 +#define LSM6DSL_ACC_GYRO_SENSORHUB8_REG 0x35 +#define LSM6DSL_ACC_GYRO_SENSORHUB9_REG 0x36 +#define LSM6DSL_ACC_GYRO_SENSORHUB10_REG 0x37 +#define LSM6DSL_ACC_GYRO_SENSORHUB11_REG 0x38 +#define LSM6DSL_ACC_GYRO_SENSORHUB12_REG 0x39 +#define LSM6DSL_ACC_GYRO_FIFO_STATUS1 0x3A +#define LSM6DSL_ACC_GYRO_FIFO_STATUS2 0x3B +#define LSM6DSL_ACC_GYRO_FIFO_STATUS3 0x3C +#define LSM6DSL_ACC_GYRO_FIFO_STATUS4 0x3D +#define LSM6DSL_ACC_GYRO_FIFO_DATA_OUT_L 0x3E +#define LSM6DSL_ACC_GYRO_FIFO_DATA_OUT_H 0x3F +#define LSM6DSL_ACC_GYRO_TIMESTAMP0_REG 0x40 +#define LSM6DSL_ACC_GYRO_TIMESTAMP1_REG 0x41 +#define LSM6DSL_ACC_GYRO_TIMESTAMP2_REG 0x42 + +#define LSM6DSL_ACC_GYRO_TIMESTAMP_L 0x49 +#define LSM6DSL_ACC_GYRO_TIMESTAMP_H 0x4A + +#define LSM6DSL_ACC_GYRO_STEP_COUNTER_L 0x4B +#define LSM6DSL_ACC_GYRO_STEP_COUNTER_H 0x4C + +#define LSM6DSL_ACC_GYRO_SENSORHUB13_REG 0x4D +#define LSM6DSL_ACC_GYRO_SENSORHUB14_REG 0x4E +#define LSM6DSL_ACC_GYRO_SENSORHUB15_REG 0x4F +#define LSM6DSL_ACC_GYRO_SENSORHUB16_REG 0x50 +#define LSM6DSL_ACC_GYRO_SENSORHUB17_REG 0x51 +#define LSM6DSL_ACC_GYRO_SENSORHUB18_REG 0x52 + +#define LSM6DSL_ACC_GYRO_FUNC_SRC 0x53 +#define LSM6DSL_ACC_GYRO_TAP_CFG1 0x58 +#define LSM6DSL_ACC_GYRO_TAP_THS_6D 0x59 +#define LSM6DSL_ACC_GYRO_INT_DUR2 0x5A +#define LSM6DSL_ACC_GYRO_WAKE_UP_THS 0x5B +#define LSM6DSL_ACC_GYRO_WAKE_UP_DUR 0x5C +#define LSM6DSL_ACC_GYRO_FREE_FALL 0x5D +#define LSM6DSL_ACC_GYRO_MD1_CFG 0x5E +#define LSM6DSL_ACC_GYRO_MD2_CFG 0x5F + +#define LSM6DSL_ACC_GYRO_OUT_MAG_RAW_X_L 0x66 +#define LSM6DSL_ACC_GYRO_OUT_MAG_RAW_X_H 0x67 +#define LSM6DSL_ACC_GYRO_OUT_MAG_RAW_Y_L 0x68 +#define LSM6DSL_ACC_GYRO_OUT_MAG_RAW_Y_H 0x69 +#define LSM6DSL_ACC_GYRO_OUT_MAG_RAW_Z_L 0x6A +#define LSM6DSL_ACC_GYRO_OUT_MAG_RAW_Z_H 0x6B + +#define LSM6DSL_ACC_GYRO_X_OFS_USR 0x73 +#define LSM6DSL_ACC_GYRO_Y_OFS_USR 0x74 +#define LSM6DSL_ACC_GYRO_Z_OFS_USR 0x75 + +#define LSM6DSL_CHIP_ID_VALUE (0x6A) + +#define LSM6DSL_RESET_VALUE (0x1) +#define LSM6DSL_RESET_MSK (0X1) +#define LSM6DSL_RESET_POS (0) + +#define LSM6DSL_ACC_ODR_POWER_DOWN (0X00) +#define LSM6DSL_ACC_ODR_1_6_HZ (0X0B) +#define LSM6DSL_ACC_ODR_12_5_HZ (0x01) +#define LSM6DSL_ACC_ODR_26_HZ (0x02) +#define LSM6DSL_ACC_ODR_52_HZ (0x03) +#define LSM6DSL_ACC_ODR_104_HZ (0x04) +#define LSM6DSL_ACC_ODR_208_HZ (0x05) +#define LSM6DSL_ACC_ODR_416_HZ (0x06) +#define LSM6DSL_ACC_ODR_833_HZ (0x07) +#define LSM6DSL_ACC_ODR_1_66_KHZ (0x08) +#define LSM6DSL_ACC_ODR_3_33_KHZ (0x09) +#define LSM6DSL_ACC_ODR_6_66_KHZ (0x0A) +#define LSM6DSL_ACC_ODR_MSK (0XF0) +#define LSM6DSL_ACC_ODR_POS (4) + +#define LSM6DSL_GYRO_ODR_POWER_DOWN (0X00) +#define LSM6DSL_GYRO_ODR_12_5_HZ (0x01) +#define LSM6DSL_GYRO_ODR_26_HZ (0x02) +#define LSM6DSL_GYRO_ODR_52_HZ (0x03) +#define LSM6DSL_GYRO_ODR_104_HZ (0x04) +#define LSM6DSL_GYRO_ODR_208_HZ (0x05) +#define LSM6DSL_GYRO_ODR_416_HZ (0x06) +#define LSM6DSL_GYRO_ODR_833_HZ (0x07) +#define LSM6DSL_GYRO_ODR_1_66_KHZ (0x08) +#define LSM6DSL_GYRO_ODR_3_33_KHZ (0x09) +#define LSM6DSL_GYRO_ODR_6_66_KHZ (0x0A) +#define LSM6DSL_GYRO_ODR_MSK (0XF0) +#define LSM6DSL_GYRO_ODR_POS (4) + +#define LSM6DSL_ACC_RANGE_2G (0x0) +#define LSM6DSL_ACC_RANGE_4G (0x2) +#define LSM6DSL_ACC_RANGE_8G (0x3) +#define LSM6DSL_ACC_RANGE_16G (0x1) +#define LSM6DSL_ACC_RANGE_MSK (0X0C) +#define LSM6DSL_ACC_RANGE_POS (2) + +#define LSM6DSL_ACC_SENSITIVITY_2G (61) +#define LSM6DSL_ACC_SENSITIVITY_4G (122) +#define LSM6DSL_ACC_SENSITIVITY_8G (244) +#define LSM6DSL_ACC_SENSITIVITY_16G (488) + +#define LSM6DSL_GYRO_RANGE_245 (0x0) +#define LSM6DSL_GYRO_RANGE_500 (0x1) +#define LSM6DSL_GYRO_RANGE_1000 (0x2) +#define LSM6DSL_GYRO_RANGE_2000 (0x3) +#define LSM6DSL_GYRO_RANGE_MSK (0X0C) +#define LSM6DSL_GYRO_RANGE_POS (2) + +#define LSM6DSL_GYRO_SENSITIVITY_245DPS (875) +#define LSM6DSL_GYRO_SENSITIVITY_500DPS (1750) +#define LSM6DSL_GYRO_SENSITIVITY_1000DPS (3500) +#define LSM6DSL_GYRO_SENSITIVITY_2000DPS (7000) + +#define LSM6DSL_SHIFT_EIGHT_BITS (8) +#define LSM6DSL_16_BIT_SHIFT (0xFF) +#define LSM6DSL_ACC_MUL (1000) +#define LSM6DSL_GYRO_MUL (100) + +#define LSM6DSL_ACC_DEFAULT_ODR_100HZ (100) +#define LSM6DSL_GYRO_DEFAULT_ODR_100HZ (100) + +#define LSM6DSL_GET_BITSLICE(regvar, bitname)\ +((regvar & bitname##_MSK) >> bitname##_POS) + +#define LSM6DSL_SET_BITSLICE(regvar, bitname, val)\ +((regvar & ~bitname##_MSK) | ((val< 3330) + return LSM6DSL_ACC_ODR_6_66_KHZ; + else if(hz > 1660) + return LSM6DSL_ACC_ODR_3_33_KHZ; + else if(hz > 833) + return LSM6DSL_ACC_ODR_1_66_KHZ; + else if(hz > 416) + return LSM6DSL_ACC_ODR_833_HZ; + else if(hz > 208) + return LSM6DSL_ACC_ODR_416_HZ; + else if(hz > 104) + return LSM6DSL_ACC_ODR_208_HZ; + else if(hz > 52) + return LSM6DSL_ACC_ODR_104_HZ; + else if(hz > 26) + return LSM6DSL_ACC_ODR_52_HZ; + else if(hz > 13) + return LSM6DSL_ACC_ODR_26_HZ; + else if(hz >= 2) + return LSM6DSL_ACC_ODR_12_5_HZ; + else + return LSM6DSL_ACC_ODR_1_6_HZ; + +} + + +static int drv_acc_st_lsm6dsl_set_odr(i2c_dev_t* drv, uint32_t hz) +{ + int ret = 0; + uint8_t value = 0x00; + uint8_t odr = drv_acc_st_lsm6dsl_hz2odr(hz); + + ret = sensor_i2c_read(drv, LSM6DSL_ACC_GYRO_CTRL1_XL, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + value = LSM6DSL_SET_BITSLICE(value,LSM6DSL_ACC_ODR,odr); + + ret = sensor_i2c_write(drv, LSM6DSL_ACC_GYRO_CTRL1_XL, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + return 0; +} + +static int drv_acc_st_lsm6dsl_set_range(i2c_dev_t* drv, uint32_t range) +{ + int ret = 0; + uint8_t value = 0x00; + uint8_t tmp = 0; + + ret = sensor_i2c_read(drv, LSM6DSL_ACC_GYRO_CTRL1_XL, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + switch (range){ + case ACC_RANGE_2G:{ + tmp = LSM6DSL_ACC_RANGE_2G; + }break; + + case ACC_RANGE_4G:{ + tmp = LSM6DSL_ACC_RANGE_4G; + }break; + + case ACC_RANGE_8G:{ + tmp = LSM6DSL_ACC_RANGE_8G; + }break; + + case ACC_RANGE_16G:{ + tmp = LSM6DSL_ACC_RANGE_16G; + }break; + + default:break; + } + + value = LSM6DSL_SET_BITSLICE(value,LSM6DSL_ACC_RANGE,tmp); + ret = sensor_i2c_write(drv, LSM6DSL_ACC_GYRO_CTRL1_XL, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + if((range >= ACC_RANGE_2G)&&(range <= ACC_RANGE_16G)){ + cur_acc_factor = lsm6dsl_acc_factor[range]; + } + + return 0; +} + + +static void drv_acc_st_lsm6dsl_irq_handle(void) +{ + /* no handle so far */ +} + +static int drv_acc_st_lsm6dsl_open(void) +{ + int ret = 0; + + ret = drv_acc_st_lsm6dsl_set_power_mode(&lsm6dsl_ctx, DEV_POWER_ON); + if(unlikely(ret)){ + return -1; + } + + ret = drv_acc_st_lsm6dsl_set_range(&lsm6dsl_ctx, ACC_RANGE_8G); + if(unlikely(ret)){ + return -1; + } + + ret = drv_acc_st_lsm6dsl_set_odr(&lsm6dsl_ctx, LSM6DSL_ACC_DEFAULT_ODR_100HZ); + if(unlikely(ret)){ + return -1; + } + + return 0; + +} + +static int drv_acc_st_lsm6dsl_close(void) +{ + int ret = 0; + ret = drv_acc_st_lsm6dsl_set_power_mode(&lsm6dsl_ctx, DEV_POWER_OFF); + if(unlikely(ret)){ + return -1; + } + return 0; +} + +static int drv_acc_st_lsm6dsl_read(void *buf, size_t len) +{ + int ret = 0; + size_t size; + uint8_t reg[6]; + accel_data_t *accel = (accel_data_t *)buf; + if(buf == NULL){ + return -1; + } + + size = sizeof(accel_data_t); + if(len < size){ + return -1; + } + + ret = sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTX_L_XL, ®[0], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTX_H_XL, ®[1], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTY_L_XL, ®[2], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTY_H_XL, ®[3], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTZ_L_XL, ®[4], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTZ_H_XL, ®[5], I2C_REG_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + accel->data[DATA_AXIS_X] = (int16_t)((((int16_t)((int8_t)reg[1]))<< LSM6DSL_SHIFT_EIGHT_BITS)|(reg[0])); + accel->data[DATA_AXIS_Y] = (int16_t)((((int16_t)((int8_t)reg[3]))<< LSM6DSL_SHIFT_EIGHT_BITS)|(reg[2])); + accel->data[DATA_AXIS_Z] = (int16_t)((((int16_t)((int8_t)reg[5]))<< LSM6DSL_SHIFT_EIGHT_BITS)|(reg[4])); + + if(cur_acc_factor != 0){ + accel->data[DATA_AXIS_X] = (accel->data[DATA_AXIS_X] * cur_acc_factor)/LSM6DSL_ACC_MUL; + accel->data[DATA_AXIS_Y] = (accel->data[DATA_AXIS_Y] * cur_acc_factor)/LSM6DSL_ACC_MUL; + accel->data[DATA_AXIS_Z] = (accel->data[DATA_AXIS_Z] * cur_acc_factor)/LSM6DSL_ACC_MUL; + + } + accel->timestamp = aos_now_ms(); + + return (int)size; +} + +static int drv_acc_st_lsm6dsl_ioctl(int cmd, unsigned long arg) +{ + int ret = 0; + + switch(cmd){ + case SENSOR_IOCTL_ODR_SET:{ + ret = drv_acc_st_lsm6dsl_set_odr(&lsm6dsl_ctx, arg); + if(unlikely(ret)){ + return -1; + } + }break; + case SENSOR_IOCTL_RANGE_SET:{ + ret = drv_acc_st_lsm6dsl_set_range(&lsm6dsl_ctx, arg); + if(unlikely(ret)){ + return -1; + } + }break; + case SENSOR_IOCTL_SET_POWER:{ + ret = drv_acc_st_lsm6dsl_set_power_mode(&lsm6dsl_ctx, arg); + if(unlikely(ret)){ + return -1; + } + }break; + case SENSOR_IOCTL_GET_INFO:{ + /* fill the dev info here */ + dev_sensor_info_t *info =arg; + *(info->model) = "LSM6DSL"; + info->range_max = 16; + info->range_min = 2; + info->unit = mg; + }break; + + default:break; + } + + return 0; +} + + +int drv_acc_st_lsm6dsl_init(void){ + int ret = 0; + sensor_obj_t sensor; + + /* fill the sensor obj parameters here */ + sensor.io_port = I2C_PORT; + sensor.tag = TAG_DEV_ACC; + sensor.path = dev_acc_path; + sensor.open = drv_acc_st_lsm6dsl_open; + sensor.close = drv_acc_st_lsm6dsl_close; + sensor.read = drv_acc_st_lsm6dsl_read; + sensor.write = NULL; + sensor.ioctl = drv_acc_st_lsm6dsl_ioctl; + sensor.irq_handle = drv_acc_st_lsm6dsl_irq_handle; + sensor.bus = &lsm6dsl_ctx; + + ret = sensor_create_obj(&sensor); + if(unlikely(ret)){ + return -1; + } + ret = drv_acc_gyro_st_lsm6dsl_validate_id(&lsm6dsl_ctx, LSM6DSL_CHIP_ID_VALUE); + if(unlikely(ret)){ + return -1; + } + + if(0 == g_lsm6dslflag) + { + ret = drv_acc_gyro_st_lsm6dsl_soft_reset(&lsm6dsl_ctx); + if(unlikely(ret)){ + return -1; + } + ret = drv_acc_st_lsm6dsl_set_power_mode(&lsm6dsl_ctx, DEV_POWER_OFF); + if(unlikely(ret)){ + return -1; + } + g_lsm6dslflag = 1; + } + else + { + LOG("%s %s acc do not need reset\n", SENSOR_STR, __func__); + } + + LOG("%s %s successfully \n", SENSOR_STR, __func__); + return 0; +} + + +static int drv_gyro_st_lsm6dsl_set_power_mode(i2c_dev_t* drv, dev_power_mode_e mode) +{ + uint8_t value,value1 = 0x00; + int ret = 0; + + ret = sensor_i2c_read(drv, LSM6DSL_ACC_GYRO_CTRL2_G, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + switch(mode){ + case DEV_POWER_ON:{ + + value = LSM6DSL_SET_BITSLICE(value,LSM6DSL_GYRO_ODR,LSM6DSL_GYRO_ODR_12_5_HZ); + ret = sensor_i2c_write(drv, LSM6DSL_ACC_GYRO_CTRL2_G, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + }break; + + case DEV_POWER_OFF:{ + value = LSM6DSL_SET_BITSLICE(value,LSM6DSL_GYRO_ODR,LSM6DSL_GYRO_ODR_POWER_DOWN); + ret = sensor_i2c_write(drv, LSM6DSL_ACC_GYRO_CTRL2_G, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + }break; + + case DEV_SLEEP:{ + value = LSM6DSL_SET_BITSLICE(value,LSM6DSL_GYRO_ODR,LSM6DSL_GYRO_ODR_12_5_HZ); + ret = sensor_i2c_write(drv, LSM6DSL_ACC_GYRO_CTRL2_G, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + }break; + + default:break; + } + return 0; +} + +static uint8_t drv_gyro_st_lsm6dsl_hz2odr(uint32_t hz) +{ + if(hz > 3330) + return LSM6DSL_GYRO_ODR_6_66_KHZ; + else if(hz > 1660) + return LSM6DSL_GYRO_ODR_3_33_KHZ; + else if(hz > 833) + return LSM6DSL_GYRO_ODR_1_66_KHZ; + else if(hz > 416) + return LSM6DSL_GYRO_ODR_833_HZ; + else if(hz > 208) + return LSM6DSL_GYRO_ODR_416_HZ; + else if(hz > 104) + return LSM6DSL_GYRO_ODR_208_HZ; + else if(hz > 52) + return LSM6DSL_GYRO_ODR_104_HZ; + else if(hz > 26) + return LSM6DSL_GYRO_ODR_52_HZ; + else if(hz > 13) + return LSM6DSL_GYRO_ODR_26_HZ; + else + return LSM6DSL_GYRO_ODR_12_5_HZ; + +} + + +static int drv_gyro_st_lsm6dsl_set_odr(i2c_dev_t* drv, uint32_t hz) +{ + int ret = 0; + uint8_t value = 0x00; + uint8_t odr = drv_gyro_st_lsm6dsl_hz2odr(hz); + + ret = sensor_i2c_read(drv, LSM6DSL_ACC_GYRO_CTRL2_G, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + value = LSM6DSL_SET_BITSLICE(value,LSM6DSL_GYRO_ODR,odr); + + ret = sensor_i2c_write(drv, LSM6DSL_ACC_GYRO_CTRL2_G, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + return 0; +} + +static int drv_gyro_st_lsm6dsl_set_range(i2c_dev_t* drv, uint32_t range) +{ + int ret = 0; + uint8_t value = 0x00; + uint8_t tmp = 0; + + ret = sensor_i2c_read(drv, LSM6DSL_ACC_GYRO_CTRL2_G, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + switch (range){ + case GYRO_RANGE_250DPS:{ + tmp = LSM6DSL_GYRO_RANGE_245; + }break; + + case GYRO_RANGE_500DPS:{ + tmp = LSM6DSL_GYRO_RANGE_500; + }break; + + case GYRO_RANGE_1000DPS:{ + tmp = LSM6DSL_GYRO_RANGE_1000; + }break; + + case GYRO_RANGE_2000DPS:{ + tmp = LSM6DSL_GYRO_RANGE_2000; + }break; + + default:break; + } + + value = LSM6DSL_SET_BITSLICE(value,LSM6DSL_GYRO_RANGE,tmp); + ret = sensor_i2c_write(drv, LSM6DSL_ACC_GYRO_CTRL2_G, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + if((range >= GYRO_RANGE_250DPS)&&(range <= GYRO_RANGE_2000DPS)){ + cur_gyro_factor = lsm6dsl_gyro_factor[range]; + } + + return 0; +} + + +static void drv_gyro_st_lsm6dsl_irq_handle(void) +{ + /* no handle so far */ +} + +static int drv_gyro_st_lsm6dsl_open(void) +{ + int ret = 0; + ret = drv_gyro_st_lsm6dsl_set_power_mode(&lsm6dsl_ctx, DEV_POWER_ON); + if(unlikely(ret)){ + return -1; + } + + ret = drv_gyro_st_lsm6dsl_set_range(&lsm6dsl_ctx, GYRO_RANGE_1000DPS); + if(unlikely(ret)){ + return -1; + } + + ret = drv_gyro_st_lsm6dsl_set_odr(&lsm6dsl_ctx, LSM6DSL_GYRO_DEFAULT_ODR_100HZ); + if(unlikely(ret)){ + return -1; + } + + return 0; + +} + +static int drv_gyro_st_lsm6dsl_close(void) +{ + int ret = 0; + ret = drv_gyro_st_lsm6dsl_set_power_mode(&lsm6dsl_ctx, DEV_POWER_OFF); + if(unlikely(ret)){ + return -1; + } + return 0; +} + +static int drv_gyro_st_lsm6dsl_read(void *buf, size_t len) +{ + int ret = 0; + size_t size; + uint8_t reg[6]; + gyro_data_t *gyro = (gyro_data_t *)buf; + if(buf == NULL){ + return -1; + } + + size = sizeof(gyro_data_t); + if(len < size){ + return -1; + } + + ret = sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTX_L_G, ®[0], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTX_H_G, ®[1], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTY_L_G, ®[2], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTY_H_G, ®[3], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTZ_L_G, ®[4], I2C_REG_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lsm6dsl_ctx, LSM6DSL_ACC_GYRO_OUTZ_H_G, ®[5], I2C_REG_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + gyro->data[DATA_AXIS_X] = (int16_t)((((int32_t)((int8_t)reg[1]))<< LSM6DSL_SHIFT_EIGHT_BITS)|(reg[0])); + gyro->data[DATA_AXIS_Y] = (int16_t)((((int32_t)((int8_t)reg[3]))<< LSM6DSL_SHIFT_EIGHT_BITS)|(reg[2])); + gyro->data[DATA_AXIS_Z] = (int16_t)((((int32_t)((int8_t)reg[5]))<< LSM6DSL_SHIFT_EIGHT_BITS)|(reg[4])); + + if(cur_gyro_factor != 0){ + gyro->data[DATA_AXIS_X] = (gyro->data[DATA_AXIS_X] * cur_gyro_factor)/LSM6DSL_GYRO_MUL; + gyro->data[DATA_AXIS_Y] = (gyro->data[DATA_AXIS_Y] * cur_gyro_factor)/LSM6DSL_GYRO_MUL; + gyro->data[DATA_AXIS_Z] = (gyro->data[DATA_AXIS_Z] * cur_gyro_factor)/LSM6DSL_GYRO_MUL; + } + gyro->timestamp = aos_now_ms(); + + return (int)size; +} + +static int drv_gyro_st_lsm6dsl_ioctl(int cmd, unsigned long arg) +{ + int ret = 0; + + switch(cmd){ + case SENSOR_IOCTL_ODR_SET:{ + ret = drv_gyro_st_lsm6dsl_set_odr(&lsm6dsl_ctx, arg); + if(unlikely(ret)){ + return -1; + } + }break; + case SENSOR_IOCTL_RANGE_SET:{ + ret = drv_gyro_st_lsm6dsl_set_range(&lsm6dsl_ctx, arg); + if(unlikely(ret)){ + return -1; + } + }break; + case SENSOR_IOCTL_SET_POWER:{ + ret = drv_gyro_st_lsm6dsl_set_power_mode(&lsm6dsl_ctx, arg); + if(unlikely(ret)){ + return -1; + } + }break; + case SENSOR_IOCTL_GET_INFO:{ + /* fill the dev info here */ + dev_sensor_info_t *info =arg; + *(info->model) = "LSM6DSL"; + info->range_max = 16; + info->range_min = 2; + info->unit = mg; + }break; + + default:break; + } + + return 0; +} + +int drv_gyro_st_lsm6dsl_init(void){ + int ret = 0; + sensor_obj_t sensor; + + /* fill the sensor obj parameters here */ + sensor.io_port = I2C_PORT; + sensor.tag = TAG_DEV_GYRO; + sensor.path = dev_gyro_path; + sensor.open = drv_gyro_st_lsm6dsl_open; + sensor.close = drv_gyro_st_lsm6dsl_close; + sensor.read = drv_gyro_st_lsm6dsl_read; + sensor.write = NULL; + sensor.ioctl = drv_gyro_st_lsm6dsl_ioctl; + sensor.irq_handle = drv_gyro_st_lsm6dsl_irq_handle; + sensor.bus = &lsm6dsl_ctx; + + ret = sensor_create_obj(&sensor); + if(unlikely(ret)){ + return -1; + } + + ret = drv_acc_gyro_st_lsm6dsl_validate_id(&lsm6dsl_ctx, LSM6DSL_CHIP_ID_VALUE); + if(unlikely(ret)){ + return -1; + } + + if(0 == g_lsm6dslflag){ + ret = drv_acc_gyro_st_lsm6dsl_soft_reset(&lsm6dsl_ctx); + if(unlikely(ret)){ + return -1; + } + + ret = drv_gyro_st_lsm6dsl_set_power_mode(&lsm6dsl_ctx, DEV_POWER_OFF); + if(unlikely(ret)){ + return -1; + } + g_lsm6dslflag = 1; + } + else{ + LOG("%s %s gyro do not need reset\n", SENSOR_STR, __func__); + } + + /* update the phy sensor info to sensor hal */ + LOG("%s %s successfully \n", SENSOR_STR, __func__); + return 0; +} + + + + + diff --git a/device/sensor/drv/drv_baro_bosch_bmp280.c b/device/sensor/drv/drv_baro_bosch_bmp280.c index 12a76db7ea..f96f1104cc 100644 --- a/device/sensor/drv/drv_baro_bosch_bmp280.c +++ b/device/sensor/drv/drv_baro_bosch_bmp280.c @@ -305,7 +305,7 @@ static bmp280_calib_param_t g_bmp280_calib_table; i2c_dev_t bmp280_ctx = { .port = 1, - .config.address_width = 7, + .config.address_width = 8, .config.freq = 400000, .config.dev_addr = BMP280_I2C_ADDR, }; @@ -732,12 +732,17 @@ static int drv_baro_bosch_bmp280_close(void) static int drv_baro_bosch_bmp280_read(void *buf, size_t len) { int ret = 0; + size_t size = 0; barometer_data_t* pdata = (barometer_data_t*)buf; if(buf == NULL){ return -1; } - + size = sizeof(barometer_data_t); + if(len < size){ + return -1; + } + ret = drv_baro_bosch_bmp280_cali_temp(&bmp280_ctx); if(unlikely(ret)){ return ret; @@ -749,9 +754,8 @@ static int drv_baro_bosch_bmp280_read(void *buf, size_t len) } pdata->timestamp = aos_now_ms(); - len = sizeof(barometer_data_t); - return 0; + return (int)size; } static int drv_baro_bosch_bmp280_write(const void *buf, size_t len) diff --git a/device/sensor/drv/drv_baro_st_lps22hb.c b/device/sensor/drv/drv_baro_st_lps22hb.c new file mode 100644 index 0000000000..0589f01479 --- /dev/null +++ b/device/sensor/drv/drv_baro_st_lps22hb.c @@ -0,0 +1,420 @@ +/* +* Copyright (C) 2015-2017 Alibaba Group Holding Limited +* +* +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "common.h" +#include "hal/sensor.h" + +/* ST BARO SENSOR REGISTER MAP */ +#define LPS22HB_BIT(x) ((uint8_t)x) +#define LPS22HB_ADDRESS (uint8_t)0xB8 +#define LPS22HB_DriverVersion_Major (uint8_t)1 +#define LPS22HB_DriverVersion_Minor (uint8_t)0 +#define LPS22HB_DriverVersion_Point (uint8_t)0 +#define LPS22HB_WHO_AM_I_REG (uint8_t)0x0F +#define LPS22HB_WHO_AM_I_VAL (uint8_t)0xB1 +#define LPS22HB_REF_P_XL_REG (uint8_t)0x15 +#define LPS22HB_REF_P_L_REG (uint8_t)0x16 +#define LPS22HB_REF_P_H_REG (uint8_t)0x17 +#define LPS22HB_RES_CONF_REG (uint8_t)0x1A +#define LPS22HB_RES_CONF_REG (uint8_t)0x1A +#define LPS22HB_RES_CONF_REG (uint8_t)0x1A +#define LPS22HB_LCEN_MASK (uint8_t)0x01 +#define LPS22HB_LCEN_POWERON (uint8_t)0x00 +#define LPS22HB_LCEN_LOWPOWER (uint8_t)0x01 +#define LPS22HB_CTRL_REG1 (uint8_t)0x10 +#define LPS22HB_ODR_MASK (uint8_t)0x70 +#define LPS22HB_LPFP_MASK (uint8_t)0x08 +#define LPS22HB_LPFP_CUTOFF_MASK (uint8_t)0x04 +#define LPS22HB_BDU_MASK (uint8_t)0x02 +#define LPS22HB_SIM_MASK (uint8_t)0x01 +#define LPS22HB_LPFP_BIT LPS22HB_BIT(3) +#define LPS22HB_CTRL_REG2 (uint8_t)0x11 +#define LPS22HB_BOOT_BIT LPS22HB_BIT(7) +#define LPS22HB_FIFO_EN_BIT LPS22HB_BIT(6) +#define LPS22HB_WTM_EN_BIT LPS22HB_BIT(5) +#define LPS22HB_ADD_INC_BIT LPS22HB_BIT(4) +#define LPS22HB_I2C_BIT LPS22HB_BIT(3) +#define LPS22HB_SW_RESET_BIT LPS22HB_BIT(2) +#define LPS22HB_FIFO_EN_MASK (uint8_t)0x40 +#define LPS22HB_WTM_EN_MASK (uint8_t)0x20 +#define LPS22HB_ADD_INC_MASK (uint8_t)0x10 +#define LPS22HB_I2C_MASK (uint8_t)0x08 +#define LPS22HB_ONE_SHOT_MASK (uint8_t)0x01 +#define LPS22HB_CTRL_REG3 (uint8_t)0x12 +#define LPS22HB_PP_OD_BIT LPS22HB_BIT(6) +#define LPS22HB_FIFO_FULL_BIT LPS22HB_BIT(5) +#define LPS22HB_FIFO_FTH_BIT LPS22HB_BIT(4) +#define LPS22HB_FIFO_OVR_BIT LPS22HB_BIT(3) +#define LPS22HB_DRDY_BIT LPS22HB_BIT(2) +#define LPS22HB_INT_H_L_MASK (uint8_t)0x80 +#define LPS22HB_PP_OD_MASK (uint8_t)0x40 +#define LPS22HB_FIFO_FULL_MASK (uint8_t)0x20 +#define LPS22HB_FIFO_FTH_MASK (uint8_t)0x10 +#define LPS22HB_FIFO_OVR_MASK (uint8_t)0x08 +#define LPS22HB_DRDY_MASK (uint8_t)0x04 +#define LPS22HB_INT_S12_MASK (uint8_t)0x03 +#define LPS22HB_INTERRUPT_CFG_REG (uint8_t)0x0B +#define LPS22HB_DIFF_EN_BIT LPS22HB_BIT(3) +#define LPS22HB_LIR_BIT LPS22HB_BIT(2) +#define LPS22HB_PLE_BIT LPS22HB_BIT(1) +#define LPS22HB_PHE_BIT LPS22HB_BIT(0) +#define LPS22HB_AUTORIFP_MASK (uint8_t)0x80 +#define LPS22HB_RESET_ARP_MASK (uint8_t)0x40 +#define LPS22HB_AUTOZERO_MASK (uint8_t)0x20 +#define LPS22HB_RESET_AZ_MASK (uint8_t)0x10 +#define LPS22HB_DIFF_EN_MASK (uint8_t)0x08 +#define LPS22HB_LIR_MASK (uint8_t)0x04 +#define LPS22HB_PLE_MASK (uint8_t)0x02 +#define LPS22HB_PHE_MASK (uint8_t)0x01 +#define LPS22HB_INTERRUPT_SOURCE_REG (uint8_t)0x25 +#define LPS22HB_BOOT_STATUS_BIT LPS22HB_BIT(7) +#define LPS22HB_IA_BIT LPS22HB_BIT(2) +#define LPS22HB_PL_BIT LPS22HB_BIT(1) +#define LPS22HB_PH_BIT LPS22HB_BIT(0) +#define LPS22HB_BOOT_STATUS_MASK (uint8_t)0x80 +#define LPS22HB_IA_MASK (uint8_t)0x04 +#define LPS22HB_PL_MASK (uint8_t)0x02 +#define LPS22HB_PH_MASK (uint8_t)0x01 +#define LPS22HB_STATUS_REG (uint8_t)0x27 +#define LPS22HB_TOR_BIT LPS22HB_BIT(5) +#define LPS22HB_POR_BIT LPS22HB_BIT(4) +#define LPS22HB_TDA_BIT LPS22HB_BIT(1) +#define LPS22HB_PDA_BIT LPS22HB_BIT(0) +#define LPS22HB_TOR_MASK (uint8_t)0x20 +#define LPS22HB_POR_MASK (uint8_t)0x10 +#define LPS22HB_TDA_MASK (uint8_t)0x02 +#define LPS22HB_PDA_MASK (uint8_t)0x01 +#define LPS22HB_PRESS_OUT_XL_REG (uint8_t)0x28 +#define LPS22HB_PRESS_OUT_L_REG (uint8_t)0x29 +#define LPS22HB_PRESS_OUT_H_REG (uint8_t)0x2A +#define LPS22HB_TEMP_OUT_L_REG (uint8_t)0x2B +#define LPS22HBH_TEMP_OUT_H_REG (uint8_t)0x2C +#define LPS22HB_THS_P_LOW_REG (uint8_t)0x0C +#define LPS22HB_THS_P_HIGH_REG (uint8_t)0x0D +#define LPS22HB_CTRL_FIFO_REG (uint8_t)0x14 +#define LPS22HB_FIFO_MODE_MASK (uint8_t)0xE0 +#define LPS22HB_WTM_POINT_MASK (uint8_t)0x1F +#define LPS22HB_STATUS_FIFO_REG (uint8_t)0x26 +#define LPS22HB_FTH_FIFO_BIT LPS22HB_BIT(7) +#define LPS22HB_OVR_FIFO_BIT LPS22HB_BIT(6) +#define LPS22HB_FTH_FIFO_MASK (uint8_t)0x80 +#define LPS22HB_OVR_FIFO_MASK (uint8_t)0x40 +#define LPS22HB_LEVEL_FIFO_MASK (uint8_t)0x3F +#define LPS22HB_FIFO_EMPTY (uint8_t)0x00 +#define LPS22HB_FIFO_FULL (uint8_t)0x18 +#define LPS22HB_RPDS_H_REG (uint8_t)0x19 +#define LPS22HB_CLOCK_TREE_CONFIG (uint8_t)0x43 +#define LPS22HB_CTE_MASK (uint8_t)0x20 + +#define LPS22HB_I2C_ADDR1_R (uint8_t)0xB9 +#define LPS22HB_I2C_ADDR1_W (uint8_t)0xB8 +#define LPS22HB_I2C_ADDR2_R (uint8_t)0xBB +#define LPS22HB_I2C_ADDR2_W (uint8_t)0xBZ + +#define LPS22HB_I2C_ADDR1 (0x5C) +#define LPS22HB_I2C_ADDR2 (0x5D) +#define LPS22HB_I2C_ADDR_TRANS(n) ((n)<<1) +#define LPS22HB_I2C_ADDR LPS22HB_I2C_ADDR_TRANS(LPS22HB_I2C_ADDR2) + + +typedef enum { + LPS22HB_ODR_ONE_SHOT = (uint8_t)0x00, /*!< Output Data Rate: one shot */ + LPS22HB_ODR_1HZ = (uint8_t)0x10, /*!< Output Data Rate: 1Hz */ + LPS22HB_ODR_10HZ = (uint8_t)0x20, /*!< Output Data Rate: 10Hz */ + LPS22HB_ODR_25HZ = (uint8_t)0x30, /*!< Output Data Rate: 25Hz */ + LPS22HB_ODR_50HZ = (uint8_t)0x40, /*!< Output Data Rate: 50Hz */ + LPS22HB_ODR_75HZ = (uint8_t)0x50 /*!< Output Data Rate: 75Hz */ +} lps22hb_odr_e; + +typedef enum { + LPS22HB_BDU_CONTINUOUS_UPDATE = (uint8_t)0x00, /*!< Data updated continuously */ + LPS22HB_BDU_NO_UPDATE = (uint8_t)0x02 /*!< Data updated after a read operation */ +} lps22hb_bdu_e; + +i2c_dev_t lps22hb_ctx = { + .port = 1, + .config.address_width = 8, + .config.freq = 400000, + .config.dev_addr = LPS22HB_I2C_ADDR, +}; + +static lps22hb_odr_e drv_baro_st_lps22hb_hz2odr(int hz) +{ + if(hz > 50) + return LPS22HB_ODR_75HZ; + else if(hz > 25) + return LPS22HB_ODR_50HZ; + else if(hz > 10) + return LPS22HB_ODR_25HZ; + else if(hz > 1) + return LPS22HB_ODR_10HZ; + else + return LPS22HB_ODR_1HZ; +} + +static int drv_baro_st_lps22hb_validate_id(i2c_dev_t* drv, uint8_t id_value) +{ + uint8_t value = 0x00; + int ret = 0; + + if(drv == NULL){ + return -1; + } + + ret = sensor_i2c_read(drv, LPS22HB_WHO_AM_I_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + if (id_value != value){ + return -1; + } + return 0; +} + +static int drv_baro_st_lps22hb_set_power_mode(i2c_dev_t* drv, dev_power_mode_e mode) +{ + uint8_t value = 0x00; + int ret = 0; + + ret = sensor_i2c_read(drv, LPS22HB_RES_CONF_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + switch(mode){ + case DEV_POWER_ON:{ + value &= ~LPS22HB_LCEN_MASK; + value |= LPS22HB_LCEN_POWERON; + ret = sensor_i2c_write(drv, LPS22HB_RES_CONF_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + }break; + + case DEV_POWER_OFF:{ + value |= LPS22HB_LCEN_LOWPOWER; + ret = sensor_i2c_write(drv, LPS22HB_RES_CONF_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + }break; + + default:break; + } + return 0; +} + +static int drv_baro_st_lps22hb_set_odr(i2c_dev_t* drv, lps22hb_odr_e odr) +{ + uint8_t value = 0x00; + int ret = 0; + + ret = sensor_i2c_read(drv, LPS22HB_CTRL_REG1, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + value &= ~LPS22HB_ODR_MASK; + value |= (uint8_t)odr; + + ret = sensor_i2c_write(drv, LPS22HB_CTRL_REG1, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + return 0; +} + +static int drv_baro_st_lps22hb_set_bdu(i2c_dev_t* drv, lps22hb_bdu_e bdu) +{ + uint8_t value = 0x00; + int ret = 0; + + ret = sensor_i2c_read(drv, LPS22HB_CTRL_REG1, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + value &= ~LPS22HB_BDU_MASK; + value |= (uint8_t)bdu; + + ret = sensor_i2c_write(drv, LPS22HB_CTRL_REG1, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + return 0; +} + +static int drv_baro_st_lps22hb_set_default_config(i2c_dev_t* drv) +{ + uint8_t value = 0x00; + int ret = 0; + ret = drv_baro_st_lps22hb_set_power_mode(drv, DEV_POWER_OFF); + if(unlikely(ret)){ + return ret; + } + ret = drv_baro_st_lps22hb_set_odr(drv, LPS22HB_ODR_25HZ); + if(unlikely(ret)){ + return ret; + } + ret = drv_baro_st_lps22hb_set_bdu(drv, LPS22HB_BDU_NO_UPDATE); + if(unlikely(ret)){ + return ret; + } + /* you also can set the low-pass filter and cut off config here */ + return 0; +} + + +static void drv_baro_st_lps22hb_irq_handle(void) +{ + /* no handle so far */ +} + +static int drv_baro_st_lps22hb_open(void) +{ + int ret = 0; + ret = drv_baro_st_lps22hb_set_power_mode(&lps22hb_ctx, DEV_POWER_ON); + if(unlikely(ret)){ + return -1; + } + LOG("%s %s successfully \n", SENSOR_STR, __func__); + return 0; + +} + +static int drv_baro_st_lps22hb_close(void) +{ + int ret = 0; + ret = drv_baro_st_lps22hb_set_power_mode(&lps22hb_ctx, DEV_POWER_OFF); + if(unlikely(ret)){ + return -1; + } + LOG("%s %s successfully \n", SENSOR_STR, __func__); + return 0; +} + + +static int drv_baro_st_lps22hb_read(void *buf, size_t len) +{ + int ret = 0; + size_t size; + uint8_t data[3]; + barometer_data_t* pdata = (barometer_data_t*)buf; + if(buf == NULL){ + return -1; + } + + size = sizeof(barometer_data_t); + if(len < size){ + return -1; + } + + ret = sensor_i2c_read(&lps22hb_ctx, LPS22HB_PRESS_OUT_XL_REG, &data[0], I2C_DATA_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lps22hb_ctx, LPS22HB_PRESS_OUT_L_REG, &data[1], I2C_DATA_LEN, I2C_OP_RETRIES); + ret |= sensor_i2c_read(&lps22hb_ctx, LPS22HB_PRESS_OUT_H_REG, &data[2], I2C_DATA_LEN, I2C_OP_RETRIES); + if(unlikely(ret)){ + return ret; + } + + /* hatch the baro data here*/ + for(int i=0; i<3; i++){ + pdata->p |= (((uint32_t)data[i]) << (8*i)); + } + + /* convert the 2's complement 24 bit to 2's complement 32 bit */ + if((pdata->p & 0x00800000) != 0){ + pdata->p |= 0xFF000000; + } + pdata->p = ((pdata->p)*100)/4096; + pdata->p = pdata->p/100; + pdata->timestamp = aos_now_ms(); + + return (int)size; +} + +static int drv_baro_st_lps22hb_write(const void *buf, size_t len) +{ + return 0; +} + +static int drv_baro_st_lps22hb_ioctl(int cmd, unsigned long arg) +{ + int ret = 0; + + switch(cmd){ + case SENSOR_IOCTL_ODR_SET:{ + lps22hb_odr_e odr = drv_baro_st_lps22hb_hz2odr(arg); + ret = drv_baro_st_lps22hb_set_odr(&lps22hb_ctx, odr); + if(unlikely(ret)){ + return -1; + } + }break; + case SENSOR_IOCTL_SET_POWER:{ + ret = drv_baro_st_lps22hb_set_power_mode(&lps22hb_ctx, arg); + if(unlikely(ret)){ + return -1; + } + }break; + case SENSOR_IOCTL_GET_INFO:{ + /* fill the dev info here */ + dev_sensor_info_t *info =arg; + *(info->model) = "LPS22HB"; + info->range_max = 16; + info->range_min = 4; + info->unit = pa; + + }break; + + default:break; + } + + LOG("%s %s successfully \n", SENSOR_STR, __func__); + return 0; +} + +int drv_baro_st_lps22hb_init(void){ + int ret = 0; + sensor_obj_t sensor; + + /* fill the sensor obj parameters here */ + sensor.tag = TAG_DEV_BARO; + sensor.path = dev_baro_path; + sensor.io_port = I2C_PORT; + sensor.open = drv_baro_st_lps22hb_open; + sensor.close = drv_baro_st_lps22hb_close; + sensor.read = drv_baro_st_lps22hb_read; + sensor.write = drv_baro_st_lps22hb_write; + sensor.ioctl = drv_baro_st_lps22hb_ioctl; + sensor.irq_handle = drv_baro_st_lps22hb_irq_handle; + sensor.bus = &lps22hb_ctx; + + + ret = sensor_create_obj(&sensor); + if(unlikely(ret)){ + return -1; + } + + ret = drv_baro_st_lps22hb_validate_id(&lps22hb_ctx, LPS22HB_WHO_AM_I_VAL); + if(unlikely(ret)){ + return -1; + } + /* set the default config for the sensor here */ + ret = drv_baro_st_lps22hb_set_default_config(&lps22hb_ctx); + if(unlikely(ret)){ + return -1; + } + + LOG("%s %s successfully \n", SENSOR_STR, __func__); + return 0; +} + diff --git a/device/sensor/drv/drv_gyro_bosch_bmg160.c b/device/sensor/drv/drv_gyro_bosch_bmg160.c index c3b4760b78..9afce19846 100644 --- a/device/sensor/drv/drv_gyro_bosch_bmg160.c +++ b/device/sensor/drv/drv_gyro_bosch_bmg160.c @@ -694,12 +694,12 @@ static uint32_t current_factor = 0; i2c_dev_t bmg160_ctx = { .port = 1, - .config.address_width = 7, + .config.address_width = 8, .config.freq = 400000, .config.dev_addr = BMG160_I2C_ADDR1, }; -static int drv_gyro_bosch_bmg160_validate_id(i2c_dev_t* drv, uint8_t id_addr, uint8_t id_value) +static int drv_gyro_bosch_bmg160_validate_id(i2c_dev_t* drv, uint8_t id_value) { uint8_t value = 0x00; int ret = 0; @@ -708,7 +708,7 @@ static int drv_gyro_bosch_bmg160_validate_id(i2c_dev_t* drv, uint8_t id_addr, u return -1; } - ret = sensor_i2c_read(drv, id_addr, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + ret = sensor_i2c_read(drv, BMG160_CHIP_ID_ADDR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } @@ -896,12 +896,18 @@ static int drv_gyro_bosch_bmg160_close(void) static int drv_gyro_bosch_bmg160_read(void *buf, size_t len) { int ret = 0; + size_t size = 0; uint8_t reg[6]; - gyro_data_t *gyro = buf; + gyro_data_t *gyro = (gyro_data_t *)buf; if(buf == NULL){ return -1; } + size = sizeof(gyro_data_t); + if(len < size){ + return -1; + } + ret = sensor_i2c_read(&bmg160_ctx, BMG160_RATE_X_LSB_ADDR, ®[0], I2C_REG_LEN, I2C_OP_RETRIES); ret |= sensor_i2c_read(&bmg160_ctx, BMG160_RATE_X_MSB_ADDR, ®[1], I2C_REG_LEN, I2C_OP_RETRIES); ret |= sensor_i2c_read(&bmg160_ctx, BMG160_RATE_Y_LSB_ADDR, ®[2], I2C_REG_LEN, I2C_OP_RETRIES); @@ -923,8 +929,7 @@ static int drv_gyro_bosch_bmg160_read(void *buf, size_t len) } gyro->timestamp = aos_now_ms(); - len = sizeof(gyro_data_t); - return 0; + return (int)size; } static int drv_gyro_bosch_bmg160_ioctl(int cmd, unsigned long arg) @@ -987,7 +992,7 @@ int drv_gyro_bosch_bmg160_init(void) return -1; } - ret = drv_gyro_bosch_bmg160_validate_id(&bmg160_ctx, BMG160_CHIP_ID_ADDR, BMG160_CHIP_ID_VALUE); + ret = drv_gyro_bosch_bmg160_validate_id(&bmg160_ctx, BMG160_CHIP_ID_VALUE); if(unlikely(ret)){ return -1; } diff --git a/device/sensor/drv/drv_humi_bosch_bme280.c b/device/sensor/drv/drv_humi_bosch_bme280.c index 3fd732867e..452f865456 100644 --- a/device/sensor/drv/drv_humi_bosch_bme280.c +++ b/device/sensor/drv/drv_humi_bosch_bme280.c @@ -181,7 +181,7 @@ static bme280_config_t g_bme280_config; i2c_dev_t bme280_ctx = { .port = 1, - .config.address_width = 7, + .config.address_width = 8, .config.freq = 400000, .config.dev_addr = BME280_I2C_ADDR, }; @@ -271,7 +271,7 @@ static int drv_humi_bosch_bme280_soft_reset(i2c_dev_t* drv) return 0; } -static int drv_humi_bosch_bme280_validate_id(i2c_dev_t* drv, uint8_t id_addr, uint8_t id_value) +static int drv_humi_bosch_bme280_validate_id(i2c_dev_t* drv, uint8_t id_value) { uint8_t value = 0x00; int ret = 0; @@ -280,7 +280,7 @@ static int drv_humi_bosch_bme280_validate_id(i2c_dev_t* drv, uint8_t id_addr, u return -1; } - ret = sensor_i2c_read(drv, id_addr, &value, I2C_DATA_LEN, I2C_OP_RETRIES); + ret = sensor_i2c_read(drv, BME280_CHIP_ID_ADDR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } @@ -633,6 +633,7 @@ static int drv_humi_bosch_bme280_close(void) static int drv_humi_bosch_bme280_read(void *buf, size_t len) { int ret = 0; + size_t size = 0; uint8_t data[BME280_TEMP_DATA_LEN] = {0}; uint32_t data_lsb; uint32_t data_msb; @@ -641,8 +642,9 @@ static int drv_humi_bosch_bme280_read(void *buf, size_t len) if(buf == NULL){ return -1; } - - if(len < sizeof(humidity_data_t)){ + + size = sizeof(humidity_data_t); + if(len < size){ return -1; } @@ -657,8 +659,7 @@ static int drv_humi_bosch_bme280_read(void *buf, size_t len) } pdata->timestamp = aos_now_ms(); - - return 0; + return (int)size; } static int drv_humi_bosch_bme280_ioctl(int cmd, unsigned long arg) @@ -715,7 +716,7 @@ int drv_humi_bosch_bme280_init(void){ return -1; } - ret = drv_humi_bosch_bme280_validate_id(&bme280_ctx, BME280_CHIP_ID_ADDR, BME280_CHIP_ID); + ret = drv_humi_bosch_bme280_validate_id(&bme280_ctx, BME280_CHIP_ID); if(unlikely(ret)){ return -1; } diff --git a/device/sensor/hal/sensor_hal.c b/device/sensor/hal/sensor_hal.c index 199bb0a7d2..7a33f3b689 100644 --- a/device/sensor/hal/sensor_hal.c +++ b/device/sensor/hal/sensor_hal.c @@ -238,12 +238,12 @@ static ssize_t sensor_read(file_t *f, void *buf, size_t len) } ret = g_sensor_obj[index]->read(buf, len); - if(ret != 0){ + if(ret < 0){ goto error; } LOG("%s %s successfully\n", SENSOR_STR, __func__); - return len; + return ret; error: return -1; @@ -314,6 +314,17 @@ int sensor_init(void){ drv_baro_bosch_bmp280_init(); #endif /* AOS_SENSOR_BARO_BOSCH_BMP280 */ +#ifdef AOS_SENSOR_ACC_ST_LSM6DSL + drv_acc_st_lsm6dsl_init(); +#endif /* AOS_SENSOR_ACC_ST_LSM6DSL */ + +#ifdef AOS_SENSOR_GYRO_ST_LSM6DSL + drv_gyro_st_lsm6dsl_init(); +#endif /* AOS_SENSOR_GYRO_ST_LSM6DSL */ + +#ifdef AOS_SENSOR_BARO_ST_LPS22HB + drv_baro_st_lps22hb_init(); +#endif /* AOS_SENSOR_BARO_ST_LPS22HB */ ret = sensor_hal_register(); if(ret != 0){ return -1; diff --git a/device/sensor/sensor.mk b/device/sensor/sensor.mk index b0a7df652b..6bb96949eb 100644 --- a/device/sensor/sensor.mk +++ b/device/sensor/sensor.mk @@ -7,7 +7,9 @@ $(NAME)_SOURCES += \ hal/sensor_drv_api.c \ drv/drv_humi_bosch_bme280.c \ drv/drv_acc_bosch_bma253.c \ - drv/drv_baro_bosch_bmp280.c + drv/drv_baro_bosch_bmp280.c \ + drv/drv_acc_gyro_st_lsm6dsl.c\ + drv/drv_baro_st_lps22hb.c $(NAME)_CFLAGS += -Wall -Werror @@ -17,3 +19,6 @@ GLOBAL_DEFINES += AOS_SENSOR #GLOBAL_DEFINES += AOS_SENSOR_HUMI_BOSCH_BME280 #GLOBAL_DEFINES += AOS_SENSOR_ACC_BOSCH_BMA253 #GLOBAL_DEFINES += AOS_SENSOR_BARO_BOSCH_BMP280 +#GLOBAL_DEFINES += AOS_SENSOR_ACC_ST_LSM6DSL +#GLOBAL_DEFINES += AOS_SENSOR_GYRO_ST_LSM6DSL +#GLOBAL_DEFINES += AOS_SENSOR_BARO_ST_LPS22HB diff --git a/example/uDataapp/uData_sample.c b/example/uDataapp/uData_sample.c index ff71afcb89..bd46905624 100644 --- a/example/uDataapp/uData_sample.c +++ b/example/uDataapp/uData_sample.c @@ -24,7 +24,7 @@ void uData_report_demo(input_event_t *event, void *priv_data) if(event->code == CODE_UDATA_REPORT_PUBLISH){ int ret = 0; - ret = uData_report_publish(&buf); + ret = uData_report_publish(event,&buf); if(ret == 0){ barometer_data_t* data = buf.payload; printf("uData_application::::::::::::::type = (%d)\n", buf.type); diff --git a/framework/uData/abs_data_model/abs_data_model.c b/framework/uData/abs_data_model/abs_data_model.c index 4d8fd3daba..a6a4f2d9d6 100644 --- a/framework/uData/abs_data_model/abs_data_model.c +++ b/framework/uData/abs_data_model/abs_data_model.c @@ -308,15 +308,14 @@ int abs_data_close(sensor_tag_e tag) int abs_data_read(sensor_tag_e tag, void* pdata, uint32_t nbyte) { int ret = 0; + size_t size = 0; int index = 0; /* read the physical sensor data by posix way */ - ret = aos_read(g_sensor_node[tag].fd, pdata, nbyte); - if(ret <= 0){ + size = aos_read(g_sensor_node[tag].fd, pdata, nbyte); + if(size <= 0){ LOG("%s %s %s %d\n", uDATA_STR, __func__, ERROR_LINE, __LINE__); return -1; } - /* update the legth of the data here */ - nbyte = ret; /* check if calibrated aglo registed base on this sensor. yes for invoking the callback here */ @@ -331,7 +330,7 @@ int abs_data_read(sensor_tag_e tag, void* pdata, uint32_t nbyte) } LOG("%s %s successfully \n", uDATA_STR, __func__); - return 0; + return (int)size; } int abs_data_ioctl(sensor_tag_e tag, void* config) diff --git a/framework/uData/service_mgr/service_mgr.c b/framework/uData/service_mgr/service_mgr.c index 7f94216bb4..72c192e869 100644 --- a/framework/uData/service_mgr/service_mgr.c +++ b/framework/uData/service_mgr/service_mgr.c @@ -13,7 +13,7 @@ static uData_service_t* g_service_db[UDATA_MAX_CNT]; static uint32_t g_service_cnt = 0; -static udata_pkg_t g_pkg_buf; +static udata_pkg_t g_pkg_buf[UDATA_MAX_CNT]; static uData_service_t* uData_get_service_obj(sensor_tag_e tag) { @@ -47,12 +47,15 @@ static int uData_dev_enable(sensor_tag_e tag) } return 0; } -int uData_get_report_pkg(void* buf) +int uData_get_report_pkg(void* buf,int index) { if(buf == NULL){ return -1; } - memcpy(buf, &g_pkg_buf, sizeof(udata_pkg_t)); + if(index >= UDATA_MAX_CNT){ + return -1; + } + memcpy(buf, &g_pkg_buf[index], sizeof(udata_pkg_t)); return 0; } static int uData_install_report_pkg(int index, void* pdata, size_t len) @@ -60,15 +63,19 @@ static int uData_install_report_pkg(int index, void* pdata, size_t len) if(pdata == NULL){ return -1; } + if(index >= UDATA_MAX_CNT){ + return -1; + } /* intall the report data package here */ - memset(&g_pkg_buf, 0, sizeof(udata_pkg_t)); - g_pkg_buf.valid = true; - g_pkg_buf.type = g_service_db[index]->type; - if(sizeof(g_pkg_buf.payload) < len){ + memset(&g_pkg_buf[index], 0, sizeof(udata_pkg_t)); + g_pkg_buf[index].valid = true; + g_pkg_buf[index].type = g_service_db[index]->type; + if(sizeof(g_pkg_buf[index].payload) < len){ return -1; } - memcpy(g_pkg_buf.payload, pdata, len); - aos_post_event(EV_UDATA, CODE_UDATA_REPORT_PUBLISH, NULL); + memcpy(g_pkg_buf[index].payload, pdata, len); + + aos_post_event(EV_UDATA, CODE_UDATA_REPORT_PUBLISH, index); return 0; } @@ -268,8 +275,9 @@ static void uData_service_dispatcher(input_event_t *event, void *priv_data) case CODE_UDATA_DEV_READ:{ memset(service->payload, 0, DATA_SIZE); ret = abs_data_read(event->value, service->payload, DATA_SIZE); - if(unlikely(ret)){ - return -1; + if(ret <= 0){ + LOG("%s %s fail line:%d \n", uDATA_STR, __func__,__LINE__); + return; } }break; diff --git a/framework/uData/uData_interface.c b/framework/uData/uData_interface.c index ea5a1dcf12..1ec81a0112 100644 --- a/framework/uData/uData_interface.c +++ b/framework/uData/uData_interface.c @@ -12,15 +12,21 @@ #include #include "uData_com_desc.h" -int uData_report_publish(void *pdata) +int uData_report_publish(input_event_t *event,void *pdata) { + if(event == NULL){ + return -1; + } if(pdata == NULL){ return -1; } - uData_get_report_pkg(pdata); - return 0; + if(event->value >= UDATA_MAX_CNT){ + return -1; + } + + return uData_get_report_pkg(pdata,event->value); } -AOS_EXPORT(int, uData_report_publish, void *); +AOS_EXPORT(int, uData_report_publish, input_event_t *, void *); int uData_dev_ioctl(udata_t* pkg, uint8_t cmd, void* parm) { diff --git a/include/aos/uData.h b/include/aos/uData.h index df99a7194d..0c7204d0c4 100644 --- a/include/aos/uData.h +++ b/include/aos/uData.h @@ -8,8 +8,7 @@ #include "../../framework/uData/include/uData_com_desc.h" - -int uData_report_publish(void *pdata); +int uData_report_publish(input_event_t *event, void *pdata); int uData_dev_ioctl(udata_t* pkg, uint8_t cmd, long long parm); int uData_subscribe(udata_type_e type); int uData_unsubscribe(udata_type_e type); diff --git a/include/hal/sensor.h b/include/hal/sensor.h index a46e39f417..ad775ee1ab 100644 --- a/include/hal/sensor.h +++ b/include/hal/sensor.h @@ -279,6 +279,7 @@ typedef enum{ ACC_RANGE_4G, ACC_RANGE_8G, ACC_RANGE_16G, + ACC_RANGE_MAX }acc_range_e; typedef enum{ @@ -286,7 +287,8 @@ typedef enum{ GYRO_RANGE_250DPS, GYRO_RANGE_500DPS, GYRO_RANGE_1000DPS, - GYRO_RANGE_2000DPS + GYRO_RANGE_2000DPS, + GYRO_RANGE_MAX }gyro_range_e;