add first version

This commit is contained in:
guozhanxin 2019-02-12 10:15:26 +08:00
parent 821c81e21a
commit 64813b6b07
12 changed files with 988 additions and 2765 deletions

View File

@ -239,6 +239,17 @@ config RT_USING_AUDIO
bool "Using Audio device drivers"
default n
config RT_USING_SENSOR
bool "Using Sensor device drivers"
select RT_USING_PIN
default n
if RT_USING_SENSOR
config RT_USING_SENSOR_TEST
bool "Use Sensor test"
default y
endif
menu "Using WiFi"
config RT_USING_WIFI
bool "Using Wi-Fi framework"

View File

@ -3,14 +3,11 @@
from building import *
cwd = GetCurrentDir()
src = ['sensor.cpp']
src = ['sensor.c']
CPPPATH = [cwd, cwd + '/../include']
if GetDepend('SENSOR_USING_MPU6050') and GetDepend('RT_USING_I2C'):
src += ['mpu6050_sensor.cpp'];
if GetDepend('SENSOR_USING_BMI055') and GetDepend('RT_USING_I2C'):
src += ['bmi055_sensor.cpp']
if GetDepend('RT_USING_SENSOR_TEST'):
src += ['sensor_test.c'];
group = DefineGroup('Sensors', src, depend = ['RT_USING_SENSOR', 'RT_USING_DEVICE'], CPPPATH = CPPPATH)

View File

@ -1,451 +0,0 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2015-1-11 RT_learning the first version
*/
#include <string.h>
#include <stdio.h>
#include <rtdevice.h>
#include "bmi055_sensor.h"
const static sensor_t _BMI055_sensor[] =
{
{
.name = "Accelerometer",
.vendor = "Bosch",
.version = sizeof(sensor_t),
.handle = 0,
.type = SENSOR_TYPE_ACCELEROMETER,
.maxRange = SENSOR_ACCEL_RANGE_16G,
.resolution = 1.0f,
.power = 0.5f,
.minDelay = 10000,
.fifoReservedEventCount = 0,
.fifoMaxEventCount = 64,
},
{
.name = "Gyroscope",
.vendor = "Bosch",
.version = sizeof(sensor_t),
.handle = 0,
.type = SENSOR_TYPE_GYROSCOPE,
.maxRange = SENSOR_GYRO_RANGE_2000DPS,
.resolution = 1.0f,
.power = 0.5f,
.minDelay = 10000,
.fifoReservedEventCount = 0,
.fifoMaxEventCount = 64,
}
};
BMI055::BMI055(int sensor_type, const char* iic_bus, int addr)
: SensorBase(sensor_type)
{
this->i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(iic_bus);
if (this->i2c_bus == NULL)
{
printf("BMI055: No IIC device:%s\n", iic_bus);
return;
}
this->i2c_addr = addr;
/* register to sensor manager */
SensorManager::registerSensor(this);
}
int BMI055::read_reg(rt_uint8_t reg, rt_uint8_t *value)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = this->i2c_addr;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = &reg;
msgs[0].len = 1;
msgs[1].addr = this->i2c_addr;
msgs[1].flags = RT_I2C_RD; /* Read from slave */
msgs[1].buf = (rt_uint8_t *)value;
msgs[1].len = 1;
if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2)
return RT_EOK;
return -RT_ERROR;
}
int BMI055::read_buffer(rt_uint8_t reg, rt_uint8_t* value, rt_size_t size)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = this->i2c_addr;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = &reg;
msgs[0].len = 1;
msgs[1].addr = this->i2c_addr;
msgs[1].flags = RT_I2C_RD; /* Read from slave */
msgs[1].buf = (rt_uint8_t *)value;
msgs[1].len = size;
if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2)
return RT_EOK;
return -RT_ERROR;
}
int BMI055::write_reg(rt_uint8_t reg, rt_uint8_t value)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = this->i2c_addr;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = &reg;
msgs[0].len = 1;
msgs[1].addr = this->i2c_addr;
msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START;
msgs[1].buf = (rt_uint8_t *)&value;
msgs[1].len = 1;
if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2)
return RT_EOK;
return -RT_ERROR;
}
BMI055_Accelerometer::BMI055_Accelerometer(const char* iic_name, int addr)
: BMI055(SENSOR_TYPE_ACCELEROMETER, iic_name, addr)
{
int index;
uint8_t id;
rt_uint8_t value[6] = {0};
rt_int32_t x, y, z;
SensorConfig config = {SENSOR_MODE_NORMAL, SENSOR_DATARATE_400HZ, SENSOR_ACCEL_RANGE_2G};
write_reg(BMI055_BGW_SOFTRESET, 0xB6); /* reset of the sensor P57 */
write_reg(BMI055_PMU_LPW, 0x00); /* PMU_LPW NORMAL mode P55 */
write_reg(BMI055_PMU_BW, 0x0A); /* 01010b 31.25 Hz P55 */
write_reg(BMI055_PMU_RANGE, 0x05); /* 0101b 卤4g range PMU_RANGE set acc +-4g/s P54 */
x_offset = y_offset = z_offset = 0;
x = y = z = 0;
/* read BMI055 id */
read_buffer(BMI055_ACC_BGW_CHIPID, &id, 1); /* BGW_CHIPID P47*/
if (id != BMI055_ACC_BGW_CHIPID_VALUE)
{
printf("Warning: not found BMI055 id: %02x\n", id);
}
/* get offset */
for (index = 0; index < 200; index ++)
{
read_buffer(BMI055_ACCD_X_LSB, value, 6); /*ACCD_X_LSB P47 */
x += (((rt_int16_t)value[1] << 8) | value[0]);
y += (((rt_int16_t)value[3] << 8) | value[2]);
z += (((rt_int16_t)value[5] << 8) | value[4]);
}
x_offset = x / 200;
y_offset = y / 200;
z_offset = z / 200;
this->enable = RT_FALSE;
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_2G;
this->config = config;
}
int
BMI055_Accelerometer::configure(SensorConfig *config)
{
int range;
uint8_t value;
if (config == RT_NULL) return -1;
/* TODO: set datarate */
/* get range and calc the sensitivity */
range = config->range.accel_range;
switch (range)
{
case SENSOR_ACCEL_RANGE_2G:
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_2G;
range = 0x03; //0011b
break;
case SENSOR_ACCEL_RANGE_4G:
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_4G;
range = 0x05; //0101b
break;
case SENSOR_ACCEL_RANGE_8G:
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_8G;
range = 0x01 << 3; //1000b
break;
case SENSOR_ACCEL_RANGE_16G:
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_16G;
range = 0x03 << 2; //1100b
break;
default:
return -1;
}
/* set range to sensor */
read_reg(BMI055_PMU_RANGE, &value); /* PMU_RANGE P54 */
value &= 0xF0;
value |= range;
write_reg(BMI055_PMU_RANGE, value);
return 0;
}
int
BMI055_Accelerometer::activate(int enable)
{
uint8_t value;
if (enable && this->enable == RT_FALSE)
{
/* enable accelerometer */
read_reg(BMI055_PMU_LPW, &value); /* P55 */
value &= ~(0x07 << 7);
write_reg(BMI055_PMU_LPW, value);
}
if (!enable && this->enable == RT_TRUE)
{
/* disable accelerometer */
read_reg(BMI055_PMU_LPW, &value);
value &= ~(0x07 << 7);
value |= (0x01 << 7);
write_reg(BMI055_PMU_LPW, value);
}
if (enable) this->enable = RT_TRUE;
else this->enable = RT_FALSE;
return 0;
}
int
BMI055_Accelerometer::poll(sensors_event_t *event)
{
rt_uint8_t value[6];
rt_int16_t x, y, z;
/* parameters check */
if (event == NULL) return -1;
/* get event data */
event->version = sizeof(sensors_event_t);
event->sensor = (int32_t) this;
event->timestamp = rt_tick_get();
event->type = SENSOR_TYPE_ACCELEROMETER;
read_buffer(0x02, value, 6);
/* get raw data */
x = (((rt_int16_t)value[1] << 8) | value[0]);
y = (((rt_int16_t)value[3] << 8) | value[2]);
z = (((rt_int16_t)value[5] << 8) | value[4]);
if (config.mode == SENSOR_MODE_RAW)
{
event->raw_acceleration.x = x;
event->raw_acceleration.y = y;
event->raw_acceleration.z = z;
}
else
{
x -= x_offset; y -= y_offset; z -= z_offset;
event->acceleration.x = x * this->sensitivity * SENSORS_GRAVITY_STANDARD;
event->acceleration.y = y * this->sensitivity * SENSORS_GRAVITY_STANDARD;
event->acceleration.z = z * this->sensitivity * SENSORS_GRAVITY_STANDARD;
}
return 0;
}
void
BMI055_Accelerometer::getSensor(sensor_t *sensor)
{
/* get sensor description */
if (sensor)
{
memcpy(sensor, &_BMI055_sensor[0], sizeof(sensor_t));
}
}
BMI055_Gyroscope::BMI055_Gyroscope(const char* iic_name, int addr)
: BMI055(SENSOR_TYPE_GYROSCOPE, iic_name, addr)
{
int index;
uint8_t id;
rt_uint8_t value[6];
rt_int32_t x, y, z;
/* initialize BMI055 */
write_reg(BMI055_MODE_LPM1_ADDR, 0x00); /* normal mode */
write_reg(BMI055_MODE_LPM2_ADDR, 0x80); /* fast powerup */
write_reg(BMI055_BW_ADDR, 0x03); /* ODR:400Hz Filter Bandwidth:47Hz */
write_reg(BMI055_RANGE_ADDR, 0x00); /* 2000dps */
x_offset = y_offset = z_offset = 0;
x = y = z = 0;
/* read BMI055 id */
read_buffer(BMI055_CHIP_ID_ADDR, &id, 1);
if (id != BMI055_GRRO_CHIP_ID)
{
printf("Warning: not found BMI055 id: %02x\n", id);
}
/* get offset */
for (index = 0; index < 200; index ++)
{
read_buffer(BMI055_RATE_X_LSB_ADDR, value, 6);
x += (((rt_int16_t)value[1] << 8) | value[0]);
y += (((rt_int16_t)value[3] << 8) | value[2]);
z += (((rt_int16_t)value[5] << 8) | value[4]);
}
x_offset = x / 200;
y_offset = y / 200;
z_offset = z / 200;
this->enable = RT_FALSE;
this->sensitivity = SENSOR_GYRO_SENSITIVITY_250DPS;
}
int
BMI055_Gyroscope::configure(SensorConfig *config)
{
int range;
uint8_t value;
if (config == RT_NULL) return -1;
/* TODO: set datarate */
/* get range and calc the sensitivity */
range = config->range.gyro_range;
switch (range)
{
//to do add more range e.g 125DPS
//case
case SENSOR_GYRO_RANGE_250DPS:
this->sensitivity = SENSOR_GYRO_SENSITIVITY_250DPS;
range = 0x11;
break;
case SENSOR_GYRO_RANGE_500DPS:
this->sensitivity = SENSOR_GYRO_SENSITIVITY_500DPS;
range = 0x10;
break;
case SENSOR_GYRO_RANGE_1000DPS:
this->sensitivity = SENSOR_GYRO_SENSITIVITY_1000DPS;
range = 0x01;
break;
case SENSOR_GYRO_RANGE_2000DPS:
this->sensitivity = SENSOR_GYRO_SENSITIVITY_2000DPS;
range = 0x00;
break;
default:
return -1;
}
/* set range to sensor */
read_reg(BMI055_RANGE_ADDR, &value);
value &= ~0x07;
value |= range;
write_reg(BMI055_RANGE_ADDR, value);
return 0;
}
int
BMI055_Gyroscope::activate(int enable)
{
uint8_t value;
if (enable && this->enable == RT_FALSE)
{
/* enable gyroscope */
read_reg(BMI055_MODE_LPM1_ADDR, &value);
value &= ~(0x1010 << 4); //{0; 0} NORMAL mode
write_reg(BMI055_MODE_LPM1_ADDR, value); //P101 NORMAL mode
}
if (!enable && this->enable == RT_TRUE)
{
/* disable gyroscope */
read_reg(BMI055_MODE_LPM1_ADDR, &value);
value &= ~(0x01 << 5); //set bit5 deep_suspend 0
value |= (0x01 << 7); //set bit1 suspend 1
write_reg(BMI055_MODE_LPM1_ADDR, value); //{1; 0} SUSPEND mode
}
if (enable) this->enable = RT_TRUE;
else this->enable = RT_FALSE;
return 0;
}
int
BMI055_Gyroscope::poll(sensors_event_t *event)
{
rt_uint8_t value[6];
rt_int16_t x, y, z;
/* parameters check */
if (event == NULL) return -1;
/* get event data */
event->version = sizeof(sensors_event_t);
event->sensor = (int32_t) this;
event->timestamp = rt_tick_get();
event->type = SENSOR_TYPE_GYROSCOPE;
read_buffer(BMI055_RATE_X_LSB_ADDR, value, 6);
/* get raw data */
x = (((rt_int16_t)value[1] << 8) | value[0]);
y = (((rt_int16_t)value[3] << 8) | value[2]);
z = (((rt_int16_t)value[5] << 8) | value[4]);
if (config.mode == SENSOR_MODE_RAW)
{
event->raw_gyro.x = x;
event->raw_gyro.y = y;
event->raw_gyro.z = z;
}
else
{
x -= x_offset; y -= y_offset; z -= z_offset;
event->gyro.x = x * this->sensitivity * SENSORS_DPS_TO_RADS;
event->gyro.y = y * this->sensitivity * SENSORS_DPS_TO_RADS;
event->gyro.z = z * this->sensitivity * SENSORS_DPS_TO_RADS;
}
return 0;
}
void
BMI055_Gyroscope::getSensor(sensor_t *sensor)
{
/* get sensor description */
if (sensor)
{
memcpy(sensor, &_BMI055_sensor[1], sizeof(sensor_t));
}
}

View File

@ -1,334 +0,0 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2015-1-11 RT_learning the first version
*/
#ifndef __BMI055_H__
#define __BMI055_H__
#include <sensor.h>
/**************************************************************************************************/
/************************************Register map accelerometer************************************/
#define BMI055_ACC_I2C_ADDR1 0x18 //SDO is low(GND)
#define BMI055_ACC_I2C_ADDR2 0x19 //SDO is high(VCC)
#define BMI055_ACC_DEFAULT_ADDRESS BMI055_ACC_I2C_ADDR2 //in the LPC54102 SPM-S
#define BMI055_ACC_BGW_CHIPID_VALUE 0xFA
#define BMI055_ACC_BGW_CHIPID 0x00
/**<Address of ACC Chip ID Register */
#define BMI055_ACCD_X_LSB 0x02
/**< Address of X axis ACC LSB Register */
#define BMI055_ACCD_X_MSB 0x03
/**< Address of X axis ACC MSB Register */
#define BMI055_ACCD_Y_LSB 0x04
/**< Address of Y axis ACC LSB Register */
#define BMI055_ACCD_Y_MSB 0x05
/**< Address of Y axis ACC MSB Register */
#define BMI055_ACCD_Z_LSB 0x06
/**< Address of Z axis ACC LSB Register */
#define BMI055_ACCD_Z_MSB 0x07
/**< Address of Z axis ACC MSB Register */
#define BMI055_ACCD_TEMP 0x08
/**< Address of Temperature Data Register */
/* Status Register */
#define BMI055_INT_STATUS_0 0x09
/**< Address of Interrupt status Register 0 */
#define BMI055_INT_STATUS_1 0x0A
/**< Address of Interrupt status Register 1 */
#define BMI055_INT_STATUS_2 0x0B
/**< Address of Interrupt status Register 2 */
#define BMI055_INT_STATUS_3 0x0C
/**< Address of Interrupt status Register 3 */
#define BMI055_FIFO_STATUS 0x0E
/**< Address of FIFO status Register */
/* Control Register */
#define BMI055_PMU_RANGE 0x0F
/**< Address of Range address Register */
#define BMI055_PMU_BW 0x10
/**< Address of Bandwidth Register */
#define BMI055_PMU_LPW 0x11
/**< Address of PMU LPW */
#define BMI055_PMU_LOW_POWER 0x12
/**< Address of PMU LOW POWER */
#define BMI055_ACCD_HBW 0x13
/**< Address of ACCD HBW */
#define BMI055_BGW_SOFTRESET 0x14
/**< Address of BGW SOFTRESET */
#define BMI055_INT_EN_0 0x16
/**< Address of interrupt engines in group 0 */
#define BMI055_INT_EN_1 0x17
/**< Address of interrupt engines in group 1 */
#define BMI055_INT_EN_2 0x18
/**< Address of interrupt engines in group 2 */
#define BMI055_INT_MAP_0 0x19
/**< Address of Interrupt MAP 0 */
#define BMI055_INT_MAP_1 0x1A
/**< Address of Interrupt MAP 1 */
#define BMI055_INT_MAP_2 0x1B
/**< Address of Interrupt MAP 2 */
#define BMI055_INT_SRC 0x1E
/**< Address of Interrupt source */
#define BMI055_INT_OUT_CTRL 0x20
/**< Address of Interrupt Register */
#define BMI055_INT_RST_LATCH 0x21
/**< Address of Interrupt reset and mode Register */
#define BMI055_INT_0 0x22
/**< Address of low-g Interrupt delay time Register */
#define BMI055_INT_1 0x23
/**< Address of low-g Interrupt threshold Register */
#define BMI055_INT_2 0x24
/**< Address of Interrupt 2 Register */
#define BMI055_INT_3 0x25
/**< Address of high-g Interrupt delay time Register */
#define BMI055_INT_4 0x26
/**< Address of high-g Interrupt threshold Register */
#define BMI055_INT_5 0x27
/**< Address of high-g Interrupt 5 Register */
#define BMI055_INT_6 0x28
/**< Address of any-motion Interrupt threshold Register */
#define BMI055_INT_7 0x29
/**< Address of slow/no-motion interrupt threshold Register */
#define BMI055_INT_8 0x2A
/**< Address of high-g Interrupt 8 Register */
#define BMI055_INT_9 0x2B
/**< Address of high-g Interrupt 9 Register */
#define BMI055_INT_A 0x2C
/**< Address of Interrupt A Register */
#define BMI055_INT_B 0x2D
/**< Address of Interrupt B Register */
#define BMI055_INT_C 0x2E
/**< Address of Interrupt C Register */
#define BMI055_INT_D 0x2F
/**< Address of Interrupt D Register */
#define BMI055_FIFO_CONFIG_0 0x30
/**< Address of FIFO CONFIG 0 Register */
#define BMI055_PMU_SELF_TEST 0x32
/**< Address of PMU SELF TEST Register */
#define BMI055_TRIM_NVM_CTRL 0x33
/**< Address of TRIM NVM CTRL Register */
#define BMI055_BGW_SPI3_WDT 0x34
/**< Address of BGW SPI3 WDT Register */
#define BMI055_OFC_CTRL 0x36
/**< Address of OFC CTRL Register */
#define BMI055_OFC_SETTING 0x37
/**< Address of OFC SETTING Register */
#define BMI055_OFC_OFFSET_X 0x38
/**< Address of OFC OFFSET X Register */
#define BMI055_OFC_OFFSET_Y 0x39
/**< Address of OFC OFFSET Y Register */
#define BMI055_OFC_OFFSET_Z 0x3A
/**< Address of OFC OFFSET Z Register */
/* Trim Register */
#define BMI055_TRIM_GP0 0x3B
/**< Address of TRIM GP0 Register */
#define BMI055_TRIM_GP1 0x3C
/**< Address of TRIM GP1 Register */
/* Control Register */
#define BMI055_FIFO_CONFIG_1 0x3E
/**< Address of FIFO CONFIG 1 Register */
/* Data Register */
#define BMI055_FIFO_DATA 0x3F
/**< Address of FIFO DATA Register */
/**************************************************************************************************/
/**************************************************************************************************/
/************************************Register map gyroscope****************************************/
/**< This refers BMI055 return type as signed */
// #define BMI055_I2C_ADDR1 0x68 //SDO is low(GND)
// #define BMI055_I2C_ADDR2 0x69 //SDO is high(VCC)
#define BMI055_GYRO_I2C_ADDR1 0x68 //SDO is low(GND)
#define BMI055_GYRO_I2C_ADDR2 0x69 //SDO is high(VCC)
#define BMI055_GYRO_DEFAULT_ADDRESS BMI055_GYRO_I2C_ADDR2
#define BMI055_GRRO_CHIP_ID 0x0F
/*Define of registers*/
/* Hard Wired */
#define BMI055_CHIP_ID_ADDR 0x00
/**<Address of Chip ID Register*/
/* Data Register */
#define BMI055_RATE_X_LSB_ADDR 0x02
/**< Address of X axis Rate LSB Register */
#define BMI055_RATE_X_MSB_ADDR 0x03
/**< Address of X axis Rate MSB Register */
#define BMI055_RATE_Y_LSB_ADDR 0x04
/**< Address of Y axis Rate LSB Register */
#define BMI055_RATE_Y_MSB_ADDR 0x05
/**< Address of Y axis Rate MSB Register */
#define BMI055_RATE_Z_LSB_ADDR 0x06
/**< Address of Z axis Rate LSB Register */
#define BMI055_RATE_Z_MSB_ADDR 0x07
/**< Address of Z axis Rate MSB Register */
#define BMI055_TEMP_ADDR 0x08
/**< Address of Temperature Data LSB Register */
/* Status Register */
#define BMI055_INTR_STAT0_ADDR 0x09
/**< Address of Interrupt status Register 0 */
#define BMI055_INTR_STAT1_ADDR 0x0A
/**< Address of Interrupt status Register 1 */
#define BMI055_INTR_STAT2_ADDR 0x0B
/**< Address of Interrupt status Register 2 */
#define BMI055_INTR_STAT3_ADDR 0x0C
/**< Address of Interrupt status Register 3 */
#define BMI055_FIFO_STAT_ADDR 0x0E
/**< Address of FIFO status Register */
/* Control Register */
#define BMI055_RANGE_ADDR 0x0F
/**< Address of Range address Register */
#define BMI055_BW_ADDR 0x10
/**< Address of Bandwidth Register */
#define BMI055_MODE_LPM1_ADDR 0x11
/**< Address of Mode LPM1 Register */
#define BMI055_MODE_LPM2_ADDR 0x12
/**< Address of Mode LPM2 Register */
#define BMI055_HIGH_BW_ADDR 0x13
/**< Address of Rate HIGH_BW Register */
#define BMI055_BGW_SOFT_RST_ADDR 0x14
/**< Address of BGW Softreset Register */
#define BMI055_INTR_ENABLE0_ADDR 0x15
/**< Address of Interrupt Enable 0 */
#define BMI055_INTR_ENABLE1_ADDR 0x16
/**< Address of Interrupt Enable 1 */
#define BMI055_INTR_MAP_ZERO_ADDR 0x17
/**< Address of Interrupt MAP 0 */
#define BMI055_INTR_MAP_ONE_ADDR 0x18
/**< Address of Interrupt MAP 1 */
#define BMI055_INTR_MAP_TWO_ADDR 0x19
/**< Address of Interrupt MAP 2 */
#define BMI055_INTR_ZERO_ADDR 0x1A
/**< Address of Interrupt 0 register */
#define BMI055_INTR_ONE_ADDR 0x1B
/**< Address of Interrupt 1 register */
#define BMI055_INTR_TWO_ADDR 0x1C
/**< Address of Interrupt 2 register */
#define BMI055_INTR_4_ADDR 0x1E
/**< Address of Interrupt 4 register */
#define BMI055_RST_LATCH_ADDR 0x21
/**< Address of Reset Latch Register */
#define BMI055_HIGHRATE_THRES_X_ADDR 0x22
/**< Address of High Th x Address register */
#define BMI055_HIGHRATE_DURN_X_ADDR 0x23
/**< Address of High Dur x Address register */
#define BMI055_HIGHRATE_THRES_Y_ADDR 0x24
/**< Address of High Th y Address register */
#define BMI055_HIGHRATE_DURN_Y_ADDR 0x25
/**< Address of High Dur y Address register */
#define BMI055_HIGHRATE_THRES_Z_ADDR 0x26
/**< Address of High Th z Address register */
#define BMI055_HIGHRATE_DURN_Z_ADDR 0x27
/**< Address of High Dur z Address register */
#define BMI055_SOC_ADDR 0x31
/**< Address of SOC register */
#define BMI055_A_FOC_ADDR 0x32
/**< Address of A_FOC Register */
#define BMI055_TRIM_NVM_CTRL_ADDR 0x33
/**< Address of Trim NVM control register */
#define BMI055_BGW_SPI3_WDT_ADDR 0x34
/**< Address of BGW SPI3,WDT Register */
/* Trim Register */
#define BMI055_OFC1_ADDR 0x36
/**< Address of OFC1 Register */
#define BMI055_OFC2_ADDR 0x37
/**< Address of OFC2 Register */
#define BMI055_OFC3_ADDR 0x38
/**< Address of OFC3 Register */
#define BMI055_OFC4_ADDR 0x39
/**< Address of OFC4 Register */
#define BMI055_TRIM_GP0_ADDR 0x3A
/**< Address of Trim GP0 Register */
#define BMI055_TRIM_GP1_ADDR 0x3B
/**< Address of Trim GP1 Register */
#define BMI055_SELECTF_TEST_ADDR 0x3C
/**< Address of BGW Self test Register */
/* Control Register */
#define BMI055_FIFO_CGF1_ADDR 0x3D
/**< Address of FIFO CGF0 Register */
#define BMI055_FIFO_CGF0_ADDR 0x3E
/**< Address of FIFO CGF1 Register */
/* Data Register */
#define BMI055_FIFO_DATA_ADDR 0x3F
/**< Address of FIFO Data Register */
/**************************************************************************************************/
class BMI055 :public SensorBase
{
public:
BMI055(int sensor_type, const char* iic_bus, int addr);
int read_reg(rt_uint8_t reg, rt_uint8_t* value);
int write_reg(rt_uint8_t reg, rt_uint8_t value);
int read_buffer(rt_uint8_t reg, rt_uint8_t* value, rt_size_t size);
private:
struct rt_i2c_bus_device *i2c_bus;
int i2c_addr;
};
class BMI055_Accelerometer:public BMI055
{
public:
BMI055_Accelerometer(const char* iic_name, int addr);
virtual int configure(SensorConfig *config);
virtual int activate(int enable);
virtual int poll(sensors_event_t *event);
virtual void getSensor(sensor_t *sensor);
private:
rt_int16_t x_offset, y_offset, z_offset;
rt_bool_t enable;
float sensitivity;
};
class BMI055_Gyroscope:public BMI055
{
public:
BMI055_Gyroscope(const char* iic_name, int addr);
virtual int configure(SensorConfig *config);
virtual int activate(int enable);
virtual int poll(sensors_event_t *event);
virtual void getSensor(sensor_t *sensor);
private:
rt_int16_t x_offset, y_offset, z_offset;
rt_bool_t enable;
float sensitivity;
};
#endif

View File

@ -1,458 +0,0 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2014-12-20 Bernard the first version
* 2015-1-11 RT_learning modify the mpu6050 initialize
*/
#include <string.h>
#include <stdio.h>
#include <rtdevice.h>
#include "mpu6050_sensor.h"
const static sensor_t _MPU6050_sensor[] =
{
{
.name = "Accelerometer",
.vendor = "Invensense",
.version = sizeof(sensor_t),
.handle = 0,
.type = SENSOR_TYPE_ACCELEROMETER,
.maxRange = SENSOR_ACCEL_RANGE_16G,
.resolution = 1.0f,
.power = 0.5f,
.minDelay = 10000,
.fifoReservedEventCount = 0,
.fifoMaxEventCount = 64,
},
{
.name = "Gyroscope",
.vendor = "Invensense",
.version = sizeof(sensor_t),
.handle = 0,
.type = SENSOR_TYPE_GYROSCOPE,
.maxRange = SENSOR_GYRO_RANGE_2000DPS,
.resolution = 1.0f,
.power = 0.5f,
.minDelay = 10000,
.fifoReservedEventCount = 0,
.fifoMaxEventCount = 64,
}
};
MPU6050::MPU6050(int sensor_type, const char* iic_bus, int addr)
: SensorBase(sensor_type)
{
this->i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(iic_bus);
if (this->i2c_bus == NULL)
{
printf("MPU6050: No IIC device:%s\n", iic_bus);
return;
}
this->i2c_addr = addr;
/* register to sensor manager */
SensorManager::registerSensor(this);
}
int MPU6050::read_reg(rt_uint8_t reg, rt_uint8_t *value)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = this->i2c_addr;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = &reg;
msgs[0].len = 1;
msgs[1].addr = this->i2c_addr;
msgs[1].flags = RT_I2C_RD; /* Read from slave */
msgs[1].buf = (rt_uint8_t *)value;
msgs[1].len = 1;
if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2)
return RT_EOK;
return -RT_ERROR;
}
int MPU6050::read_buffer(rt_uint8_t reg, rt_uint8_t* value, rt_size_t size)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = this->i2c_addr;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = &reg;
msgs[0].len = 1;
msgs[1].addr = this->i2c_addr;
msgs[1].flags = RT_I2C_RD; /* Read from slave */
msgs[1].buf = (rt_uint8_t *)value;
msgs[1].len = size;
if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2)
return RT_EOK;
return -RT_ERROR;
}
int MPU6050::write_reg(rt_uint8_t reg, rt_uint8_t value)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = this->i2c_addr;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = &reg;
msgs[0].len = 1;
msgs[1].addr = this->i2c_addr;
msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START;
msgs[1].buf = (rt_uint8_t *)&value;
msgs[1].len = 1;
if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2)
return RT_EOK;
return -RT_ERROR;
}
MPU6050_Accelerometer::MPU6050_Accelerometer(const char* iic_name, int addr)
: MPU6050(SENSOR_TYPE_ACCELEROMETER, iic_name, addr)
{
int index;
uint8_t id;
rt_uint8_t value[6] = {0};
rt_int32_t x, y, z;
SensorConfig config = {SENSOR_MODE_NORMAL, SENSOR_DATARATE_400HZ, SENSOR_ACCEL_RANGE_2G};
/* initialize MPU6050 */
write_reg(MPU6050_PWR_MGMT_1, 0x80); /* reset mpu6050 device */
write_reg(MPU6050_SMPLRT_DIV, 0x00); /* Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV) */
write_reg(MPU6050_PWR_MGMT_1, 0x03); /* Wake up device , set device clock Z axis gyroscope */
write_reg(MPU6050_CONFIG, 0x03); /* set DLPF_CFG 42Hz */
write_reg(MPU6050_GYRO_CONFIG, 0x18); /* set gyro 2000deg/s */
write_reg(MPU6050_ACCEL_CONFIG, 0x08); /* set acc +-4g/s */
x_offset = y_offset = z_offset = 0;
x = y = z = 0;
/* read MPU6050 id */
read_buffer(MPU6050_WHOAMI, &id, 1);
if (id != MPU6050_ID)
{
printf("Warning: not found MPU6050 id: %02x\n", id);
}
/* get offset */
for (index = 0; index < 200; index ++)
{
read_buffer(MPU6050_ACCEL_XOUT_H, value, 6);
x += (((rt_int16_t)value[0] << 8) | value[1]);
y += (((rt_int16_t)value[2] << 8) | value[3]);
z += (((rt_int16_t)value[4] << 8) | value[5]);
}
x_offset = x / 200;
y_offset = y / 200;
z_offset = z / 200;
this->enable = RT_FALSE;
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_2G;
this->config = config;
}
int
MPU6050_Accelerometer::configure(SensorConfig *config)
{
int range;
uint8_t value;
if (config == RT_NULL) return -1;
/* TODO: set datarate */
/* get range and calc the sensitivity */
range = config->range.accel_range;
switch (range)
{
case SENSOR_ACCEL_RANGE_2G:
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_2G;
range = 0;
break;
case SENSOR_ACCEL_RANGE_4G:
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_4G;
range = 0x01 << 2;
break;
case SENSOR_ACCEL_RANGE_8G:
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_8G;
range = 0x02 << 2;
break;
case SENSOR_ACCEL_RANGE_16G:
this->sensitivity = SENSOR_ACCEL_SENSITIVITY_16G;
range = 0x03 << 2;
break;
default:
return -1;
}
/* set range to sensor */
read_reg(MPU6050_ACCEL_CONFIG, &value);
value &= ~(0x3 << 2);
value |= range;
write_reg(MPU6050_ACCEL_CONFIG, value);
return 0;
}
int
MPU6050_Accelerometer::activate(int enable)
{
uint8_t value;
if (enable && this->enable == RT_FALSE)
{
/* enable accelerometer */
read_reg(MPU6050_PWR_MGMT_2, &value);
value &= ~(0x07 << 2);
write_reg(MPU6050_PWR_MGMT_2, value);
}
if (!enable && this->enable == RT_TRUE)
{
/* disable accelerometer */
read_reg(MPU6050_PWR_MGMT_2, &value);
value |= (0x07 << 2);
write_reg(MPU6050_PWR_MGMT_2, value);
}
if (enable) this->enable = RT_TRUE;
else this->enable = RT_FALSE;
return 0;
}
int
MPU6050_Accelerometer::poll(sensors_event_t *event)
{
rt_uint8_t value[6];
rt_int16_t x, y, z;
/* parameters check */
if (event == NULL) return -1;
/* get event data */
event->version = sizeof(sensors_event_t);
event->sensor = (int32_t) this;
event->timestamp = rt_tick_get();
event->type = SENSOR_TYPE_ACCELEROMETER;
read_buffer(MPU6050_ACCEL_XOUT_H, value, 6);
/* get raw data */
x = (((rt_int16_t)value[0] << 8) | value[1]);
y = (((rt_int16_t)value[2] << 8) | value[3]);
z = (((rt_int16_t)value[4] << 8) | value[5]);
if (config.mode == SENSOR_MODE_RAW)
{
event->raw_acceleration.x = x;
event->raw_acceleration.y = y;
event->raw_acceleration.z = z;
}
else
{
x -= x_offset; y -= y_offset; z -= z_offset;
event->acceleration.x = x * this->sensitivity * SENSORS_GRAVITY_STANDARD;
event->acceleration.y = y * this->sensitivity * SENSORS_GRAVITY_STANDARD;
event->acceleration.z = z * this->sensitivity * SENSORS_GRAVITY_STANDARD;
}
return 0;
}
void
MPU6050_Accelerometer::getSensor(sensor_t *sensor)
{
/* get sensor description */
if (sensor)
{
memcpy(sensor, &_MPU6050_sensor[0], sizeof(sensor_t));
}
}
MPU6050_Gyroscope::MPU6050_Gyroscope(const char* iic_name, int addr)
: MPU6050(SENSOR_TYPE_GYROSCOPE, iic_name, addr)
{
int index;
uint8_t id;
rt_uint8_t value[6];
rt_int32_t x, y, z;
/* initialize MPU6050 */
write_reg(MPU6050_PWR_MGMT_1, 0x80); /* reset mpu6050 device */
write_reg(MPU6050_SMPLRT_DIV, 0x00); /* Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV) */
write_reg(MPU6050_PWR_MGMT_1, 0x03); /* Wake up device , set device clock Z axis gyroscope */
write_reg(MPU6050_CONFIG, 0x03); /* set DLPF_CFG 42Hz */
write_reg(MPU6050_GYRO_CONFIG, 0x18); /* set gyro 2000deg/s */
write_reg(MPU6050_ACCEL_CONFIG, 0x08); /* set acc +-4g/s */
x_offset = y_offset = z_offset = 0;
x = y = z = 0;
/* read MPU6050 id */
read_reg(MPU6050_WHOAMI, &id);
if (id != MPU6050_ID)
{
printf("Warning: not found MPU6050 id: %02x\n", id);
}
/* get offset */
for (index = 0; index < 200; index ++)
{
read_buffer(MPU6050_GYRO_XOUT_H, value, 6);
x += (((rt_int16_t)value[0] << 8) | value[1]);
y += (((rt_int16_t)value[2] << 8) | value[3]);
z += (((rt_int16_t)value[4] << 8) | value[5]);
}
x_offset = x / 200;
y_offset = y / 200;
z_offset = z / 200;
this->enable = RT_FALSE;
this->sensitivity = SENSOR_GYRO_SENSITIVITY_250DPS;
}
int
MPU6050_Gyroscope::configure(SensorConfig *config)
{
int range;
uint8_t value;
if (config == RT_NULL) return -1;
/* TODO: set datarate */
/* get range and calc the sensitivity */
range = config->range.gyro_range;
switch (range)
{
case SENSOR_GYRO_RANGE_250DPS:
this->sensitivity = SENSOR_GYRO_SENSITIVITY_250DPS;
range = 0;
break;
case SENSOR_GYRO_RANGE_500DPS:
this->sensitivity = SENSOR_GYRO_SENSITIVITY_500DPS;
range = 0x01 << 2;
break;
case SENSOR_GYRO_RANGE_1000DPS:
this->sensitivity = SENSOR_GYRO_SENSITIVITY_1000DPS;
range = 0x02 << 2;
break;
case SENSOR_GYRO_RANGE_2000DPS:
this->sensitivity = SENSOR_GYRO_SENSITIVITY_2000DPS;
range = 0x03 << 2;
break;
default:
return -1;
}
/* set range to sensor */
read_reg(MPU6050_GYRO_CONFIG, &value);
value &= ~(0x3 << 2);
value |= range;
write_reg(MPU6050_GYRO_CONFIG, value);
return 0;
}
int
MPU6050_Gyroscope::activate(int enable)
{
uint8_t value;
if (enable && this->enable == RT_FALSE)
{
/* enable gyroscope */
read_reg(MPU6050_PWR_MGMT_1, &value);
value &= ~(0x01 << 4);
write_reg(MPU6050_PWR_MGMT_1, value);
read_reg(MPU6050_PWR_MGMT_2, &value);
value &= ~(0x07 << 0);
write_reg(MPU6050_PWR_MGMT_2, value);
}
if (!enable && this->enable == RT_TRUE)
{
/* disable gyroscope */
read_reg(MPU6050_PWR_MGMT_2, &value);
value |= (0x07 << 0);
write_reg(MPU6050_PWR_MGMT_2, value);
}
if (enable) this->enable = RT_TRUE;
else this->enable = RT_FALSE;
return 0;
}
int
MPU6050_Gyroscope::poll(sensors_event_t *event)
{
rt_uint8_t value[6];
rt_int16_t x, y, z;
/* parameters check */
if (event == NULL) return -1;
/* get event data */
event->version = sizeof(sensors_event_t);
event->sensor = (int32_t) this;
event->timestamp = rt_tick_get();
event->type = SENSOR_TYPE_GYROSCOPE;
read_buffer(MPU6050_GYRO_XOUT_H, value, 6);
/* get raw data */
x = (((rt_int16_t)value[0] << 8) | value[1]);
y = (((rt_int16_t)value[2] << 8) | value[3]);
z = (((rt_int16_t)value[4] << 8) | value[5]);
if (config.mode == SENSOR_MODE_RAW)
{
event->raw_gyro.x = x;
event->raw_gyro.y = y;
event->raw_gyro.z = z;
}
else
{
x -= x_offset; y -= y_offset; z -= z_offset;
event->gyro.x = x * this->sensitivity * SENSORS_DPS_TO_RADS;
event->gyro.y = y * this->sensitivity * SENSORS_DPS_TO_RADS;
event->gyro.z = z * this->sensitivity * SENSORS_DPS_TO_RADS;
}
return 0;
}
void
MPU6050_Gyroscope::getSensor(sensor_t *sensor)
{
/* get sensor description */
if (sensor)
{
memcpy(sensor, &_MPU6050_sensor[1], sizeof(sensor_t));
}
}

View File

@ -1,189 +0,0 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2014-12-20 Bernard the first version
* 2015-1-11 RT_learning modify the mpu6050 ID
*/
#ifndef MPU6050_SENSOR_H__
#define MPU6050_SENSOR_H__
#include <sensor.h>
#define MPU6050_ADDRESS_AD0_LOW 0x68 // address pin low (GND), default for InvenSense evaluation board
#define MPU6050_ADDRESS_AD0_HIGH 0x69 // address pin high (VCC)
#define MPU6050_DEFAULT_ADDRESS MPU6050_ADDRESS_AD0_LOW
#define MPU6050_XG_OFFS_TC 0x00
#define MPU6050_YG_OFFS_TC 0x01
#define MPU6050_ZG_OFFS_TC 0x02
#define MPU6050_X_FINE_GAIN 0x03
#define MPU6050_Y_FINE_GAIN 0x04
#define MPU6050_Z_FINE_GAIN 0x05
#define MPU6050_XA_OFFS_H 0x06
#define MPU6050_XA_OFFS_L 0x07
#define MPU6050_YA_OFFS_H 0x08
#define MPU6050_YA_OFFS_L 0x09
#define MPU6050_ZA_OFFS_H 0x0A
#define MPU6050_ZA_OFFS_L 0x0B
#define MPU6050_PRODUCT_ID 0x0C
#define MPU6050_SELF_TEST_X 0x0D
#define MPU6050_SELF_TEST_Y 0x0E
#define MPU6050_SELF_TEST_Z 0x0F
#define MPU6050_SELF_TEST_A 0x10
#define MPU6050_XG_OFFS_USRH 0x13
#define MPU6050_XG_OFFS_USRL 0x14
#define MPU6050_YG_OFFS_USRH 0x15
#define MPU6050_YG_OFFS_USRL 0x16
#define MPU6050_ZG_OFFS_USRH 0x17
#define MPU6050_ZG_OFFS_USRL 0x18
#define MPU6050_SMPLRT_DIV 0x19
#define MPU6050_CONFIG 0x1A
#define MPU6050_GYRO_CONFIG 0x1B
#define MPU6050_ACCEL_CONFIG 0x1C
#define MPU6050_ACCEL_CONFIG_2 0x1D
#define MPU6050_LP_ACCEL_ODR 0x1E
#define MPU6050_MOT_THR 0x1F
#define MPU6050_FIFO_EN 0x23
#define MPU6050_I2C_MST_CTRL 0x24
#define MPU6050_I2C_SLV0_ADDR 0x25
#define MPU6050_I2C_SLV0_REG 0x26
#define MPU6050_I2C_SLV0_CTRL 0x27
#define MPU6050_I2C_SLV1_ADDR 0x28
#define MPU6050_I2C_SLV1_REG 0x29
#define MPU6050_I2C_SLV1_CTRL 0x2A
#define MPU6050_I2C_SLV2_ADDR 0x2B
#define MPU6050_I2C_SLV2_REG 0x2C
#define MPU6050_I2C_SLV2_CTRL 0x2D
#define MPU6050_I2C_SLV3_ADDR 0x2E
#define MPU6050_I2C_SLV3_REG 0x2F
#define MPU6050_I2C_SLV3_CTRL 0x30
#define MPU6050_I2C_SLV4_ADDR 0x31
#define MPU6050_I2C_SLV4_REG 0x32
#define MPU6050_I2C_SLV4_DO 0x33
#define MPU6050_I2C_SLV4_CTRL 0x34
#define MPU6050_I2C_SLV4_DI 0x35
#define MPU6050_I2C_MST_STATUS 0x36
#define MPU6050_INT_PIN_CFG 0x37
#define MPU6050_INT_ENABLE 0x38
#define MPU6050_ACCEL_XOUT_H 0x3B
#define MPU6050_ACCEL_XOUT_L 0x3C
#define MPU6050_ACCEL_YOUT_H 0x3D
#define MPU6050_ACCEL_YOUT_L 0x3E
#define MPU6050_ACCEL_ZOUT_H 0x3F
#define MPU6050_ACCEL_ZOUT_L 0x40
#define MPU6050_TEMP_OUT_H 0x41
#define MPU6050_TEMP_OUT_L 0x42
#define MPU6050_GYRO_XOUT_H 0x43
#define MPU6050_GYRO_XOUT_L 0x44
#define MPU6050_GYRO_YOUT_H 0x45
#define MPU6050_GYRO_YOUT_L 0x46
#define MPU6050_GYRO_ZOUT_H 0x47
#define MPU6050_GYRO_ZOUT_L 0x48
#define MPU6050_EXT_SENS_DATA_00 0x49
#define MPU6050_EXT_SENS_DATA_01 0x4A
#define MPU6050_EXT_SENS_DATA_02 0x4B
#define MPU6050_EXT_SENS_DATA_03 0x4C
#define MPU6050_EXT_SENS_DATA_04 0x4D
#define MPU6050_EXT_SENS_DATA_05 0x4E
#define MPU6050_EXT_SENS_DATA_06 0x4F
#define MPU6050_EXT_SENS_DATA_07 0x50
#define MPU6050_EXT_SENS_DATA_08 0x51
#define MPU6050_EXT_SENS_DATA_09 0x52
#define MPU6050_EXT_SENS_DATA_10 0x53
#define MPU6050_EXT_SENS_DATA_11 0x54
#define MPU6050_EXT_SENS_DATA_12 0x55
#define MPU6050_EXT_SENS_DATA_13 0x56
#define MPU6050_EXT_SENS_DATA_14 0x57
#define MPU6050_EXT_SENS_DATA_15 0x58
#define MPU6050_EXT_SENS_DATA_16 0x59
#define MPU6050_EXT_SENS_DATA_17 0x5A
#define MPU6050_EXT_SENS_DATA_18 0x5B
#define MPU6050_EXT_SENS_DATA_19 0x5C
#define MPU6050_EXT_SENS_DATA_20 0x5D
#define MPU6050_EXT_SENS_DATA_21 0x5E
#define MPU6050_EXT_SENS_DATA_22 0x5F
#define MPU6050_EXT_SENS_DATA_23 0x60
#define MPU6050_I2C_SLV0_DO 0x63
#define MPU6050_I2C_SLV1_DO 0x64
#define MPU6050_I2C_SLV2_DO 0x65
#define MPU6050_I2C_SLV3_DO 0x66
#define MPU6050_I2C_MST_DELAY_CTRL 0x67
#define MPU6050_SIGNAL_PATH_RESET 0x68
#define MPU6050_MOT_DETECT_CTRL 0x69
#define MPU6050_USER_CTRL 0x6A
#define MPU6050_PWR_MGMT_1 0x6B
#define MPU6050_PWR_MGMT_2 0x6C
#define MPU6050_BANK_SEL 0x6D
#define MPU6050_MEM_START_ADDR 0x6E
#define MPU6050_MEM_R_W 0x6F
#define MPU6050_DMP_CFG_1 0x70
#define MPU6050_DMP_CFG_2 0x71
#define MPU6050_FIFO_COUNTH 0x72
#define MPU6050_FIFO_COUNTL 0x73
#define MPU6050_FIFO_R_W 0x74
#define MPU6050_WHOAMI 0x75
#define MPU6050_XA_OFFSET_H 0x77
#define MPU6050_XA_OFFSET_L 0x78
#define MPU6050_YA_OFFSET_H 0x7A
#define MPU6050_YA_OFFSET_L 0x7B
#define MPU6050_ZA_OFFSET_H 0x7D
#define MPU6050_ZA_OFFSET_L 0x7E
#define MPU6050_ID 0x68
class MPU6050 :public SensorBase
{
public:
MPU6050(int sensor_type, const char* iic_bus, int addr);
int read_reg(rt_uint8_t reg, rt_uint8_t* value);
int write_reg(rt_uint8_t reg, rt_uint8_t value);
int read_buffer(rt_uint8_t reg, rt_uint8_t* value, rt_size_t size);
private:
struct rt_i2c_bus_device *i2c_bus;
int i2c_addr;
};
class MPU6050_Accelerometer:public MPU6050
{
public:
MPU6050_Accelerometer(const char* iic_name, int addr);
virtual int configure(SensorConfig *config);
virtual int activate(int enable);
virtual int poll(sensors_event_t *event);
virtual void getSensor(sensor_t *sensor);
private:
rt_int16_t x_offset, y_offset, z_offset;
rt_bool_t enable;
float sensitivity;
};
class MPU6050_Gyroscope:public MPU6050
{
public:
MPU6050_Gyroscope(const char* iic_name, int addr);
virtual int configure(SensorConfig *config);
virtual int activate(int enable);
virtual int poll(sensors_event_t *event);
virtual void getSensor(sensor_t *sensor);
private:
rt_int16_t x_offset, y_offset, z_offset;
rt_bool_t enable;
float sensitivity;
};
#endif

View File

@ -0,0 +1,449 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-01-31 flybreak first version
*/
#include "sensor.h"
#define DBG_ENABLE
#define DBG_LEVEL DBG_INFO
#define DBG_SECTION_NAME "sensor"
#define DBG_COLOR
#include <rtdbg.h>
#include <string.h>
static char *const sensor_name_str[] =
{
"none",
"acce_", /* Accelerometer */
"gyro_", /* Gyroscope */
"mag_", /* Magnetometer */
"temp_", /* Temperature */
"humi_", /* Relative Humidity */
"baro_", /* Barometer */
"li_", /* Ambient light */
"pr_", /* Proximity */
"hr_", /* Heart Rate */
"tvoc_", /* TVOC Level */
"noi_", /* Noise Loudness */
"step_" /* Step sensor */
};
/* Sensor interrupt correlation function */
/*
* Sensor interrupt handler function
*/
void rt_sensor_cb(rt_sensor_t sen)
{
if (sen->parent.rx_indicate == RT_NULL)
{
return;
}
/* The buffer is not empty. Read the data in the buffer first */
if (sen->data_len > 0)
{
sen->parent.rx_indicate(&sen->parent, sen->data_len / sizeof(struct rt_sensor_data));
}
else if (sen->config.mode == RT_SEN_MODE_INT)
{
/* The interrupt mode only produces one data at a time */
sen->parent.rx_indicate(&sen->parent, 1);
}
else if (sen->config.mode == RT_SEN_MODE_FIFO)
{
sen->parent.rx_indicate(&sen->parent, sen->info.fifo_max);
}
}
/* ISR for sensor interrupt */
static void irq_callback(void *args)
{
rt_sensor_t sensor = args;
rt_uint8_t i;
if (sensor->module)
{
/* Invoke a callback for all sensors in the module */
for (i = 0; i < sensor->module->sen_num; i++)
{
rt_sensor_cb(sensor->module->sen[i]);
}
}
else
{
rt_sensor_cb(sensor);
}
}
/* Sensor interrupt initialization function */
static rt_err_t rt_sensor_irq_init(rt_sensor_t sensor)
{
if (sensor->config.irq_pin.pin == RT_PIN_NONE)
{
return -RT_EINVAL;
}
rt_pin_mode(sensor->config.irq_pin.pin, sensor->config.irq_pin.mode);
if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLDOWN)
{
rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING, irq_callback, (void *)sensor);
}
else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLUP)
{
rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_FALLING, irq_callback, (void *)sensor);
}
else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT)
{
rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING_FALLING, irq_callback, (void *)sensor);
}
rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_TRUE);
LOG_I("interrupt init success");
return 0;
}
/* Sensor interrupt enable */
static void rt_sensor_irq_enable(rt_sensor_t sensor)
{
if (sensor->config.irq_pin.pin != RT_PIN_NONE)
{
rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_TRUE);
}
}
/* Sensor interrupt disable */
static void rt_sensor_irq_disable(rt_sensor_t sensor)
{
if (sensor->config.irq_pin.pin != RT_PIN_NONE)
{
rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE);
}
}
/* RT-Thread Device Interface */
static rt_err_t rt_sensor_init(rt_device_t dev)
{
rt_sensor_t sensor = (rt_sensor_t)dev;
RT_ASSERT(dev != RT_NULL);
if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf == RT_NULL)
{
/* Allocate memory for the sensor buffer */
sensor->data_buf = rt_malloc(sizeof(struct rt_sensor_data) * sensor->info.fifo_max);
if (sensor->data_buf == RT_NULL)
{
return -RT_ENOMEM;
}
}
return RT_EOK;
}
static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
{
rt_sensor_t sensor = (rt_sensor_t)dev;
RT_ASSERT(dev != RT_NULL);
if (sensor->module)
{
/* take the module mutex */
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
}
if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
{
/* If polling mode is supported, configure it to polling mode */
if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_MODE, (void *)RT_SEN_MODE_POLLING) == RT_EOK)
{
sensor->config.mode = RT_SEN_MODE_POLLING;
}
}
else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
{
/* If interrupt mode is supported, configure it to interrupt mode */
if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_MODE, (void *)RT_SEN_MODE_INT) == RT_EOK)
{
sensor->config.mode = RT_SEN_MODE_INT;
/* Initialization sensor interrupt */
rt_sensor_irq_init(sensor);
}
}
else if (oflag & RT_SEN_FLAG_FIFO && dev->flag & RT_SEN_FLAG_FIFO)
{
/* If fifo mode is supported, configure it to fifo mode */
if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_MODE, (void *)RT_SEN_MODE_FIFO) == RT_EOK)
{
sensor->config.mode = RT_SEN_MODE_FIFO;
/* Initialization sensor interrupt */
rt_sensor_irq_init(sensor);
}
}
else
{
return -RT_EINVAL;
}
/* Configure power mode to normal mode */
if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_POWER, (void *)RT_SEN_POWER_NORMAL) == RT_EOK)
{
sensor->config.power = RT_SEN_POWER_NORMAL;
}
if (sensor->module)
{
/* release the module mutex */
rt_mutex_release(sensor->module->lock);
}
return RT_EOK;
}
static rt_err_t rt_sensor_close(rt_device_t dev)
{
rt_sensor_t sensor = (rt_sensor_t)dev;
RT_ASSERT(dev != RT_NULL);
if (sensor->module)
{
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
}
/* Configure power mode to power down mode */
if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_POWER, (void *)RT_SEN_POWER_DOWN) == RT_EOK)
{
sensor->config.power = RT_SEN_POWER_DOWN;
}
/* Sensor disable interrupt */
rt_sensor_irq_disable(sensor);
if (sensor->module)
{
rt_mutex_release(sensor->module->lock);
}
return RT_EOK;
}
static rt_size_t rt_sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t len)
{
rt_sensor_t sensor = (rt_sensor_t)dev;
rt_size_t result = 0;
RT_ASSERT(dev != RT_NULL);
if (buf == NULL || len == 0)
{
return 0;
}
if (sensor->module)
{
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
}
/* The buffer is not empty. Read the data in the buffer first */
if (sensor->data_len > 0)
{
if (len > sensor->data_len / sizeof(struct rt_sensor_data))
{
len = sensor->data_len / sizeof(struct rt_sensor_data);
}
rt_memcpy(buf, sensor->data_buf, len * sizeof(struct rt_sensor_data));
/* Clear the buffer */
sensor->data_len = 0;
result = len;
}
else
{
/* If the buffer is empty read the data */
result = sensor->ops->fetch_data(sensor, buf, len);
}
if (sensor->module)
{
rt_mutex_release(sensor->module->lock);
}
return result;
}
static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
{
rt_sensor_t sensor = (rt_sensor_t)dev;
rt_err_t result = RT_EOK;
RT_ASSERT(dev != RT_NULL);
if (sensor->module)
{
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
}
switch (cmd)
{
case RT_SEN_CTRL_GET_ID:
if (args)
{
sensor->ops->control(sensor, RT_SEN_CTRL_GET_ID, args);
}
break;
case RT_SEN_CTRL_GET_INFO:
if (args)
{
rt_memcpy(args, &sensor->info, sizeof(struct rt_sensor_info));
}
break;
case RT_SEN_CTRL_SET_RANGE:
/* Configuration measurement range */
result = sensor->ops->control(sensor, RT_SEN_CTRL_SET_RANGE, args);
if (result == RT_EOK)
{
sensor->config.range = (rt_int32_t)args;
LOG_D("set range %d", sensor->config.range);
}
break;
case RT_SEN_CTRL_SET_ODR:
/* Configuration data output rate */
result = sensor->ops->control(sensor, RT_SEN_CTRL_SET_ODR, args);
if (result == RT_EOK)
{
sensor->config.odr = (rt_uint32_t)args & 0xFFFF;
LOG_D("set odr %d", sensor->config.odr);
}
break;
case RT_SEN_CTRL_SET_MODE:
/* Configuration sensor work mode */
result = sensor->ops->control(sensor, RT_SEN_CTRL_SET_MODE, args);
if (result == RT_EOK)
{
sensor->config.mode = (rt_uint32_t)args & 0xFF;
LOG_D("set work mode code:", sensor->config.mode);
if (sensor->config.mode == RT_SEN_MODE_POLLING)
{
rt_sensor_irq_disable(sensor);
}
else if (sensor->config.mode == RT_SEN_MODE_INT || sensor->config.mode == RT_SEN_MODE_FIFO)
{
rt_sensor_irq_enable(sensor);
}
}
break;
case RT_SEN_CTRL_SET_POWER:
/* Configuration sensor power mode */
result = sensor->ops->control(sensor, RT_SEN_CTRL_SET_POWER, args);
if (result == RT_EOK)
{
sensor->config.power = (rt_uint32_t)args & 0xFF;
LOG_D("set power mode code:", sensor->config.power);
}
break;
case RT_SEN_CTRL_SELF_TEST:
/* Device self-test */
result = sensor->ops->control(sensor, RT_SEN_CTRL_SELF_TEST, args);
break;
default:
return -RT_ERROR;
}
if (sensor->module)
{
rt_mutex_release(sensor->module->lock);
}
return result;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops rt_sensor_ops =
{
rt_sensor_init,
rt_sensor_open,
rt_sensor_close,
rt_sensor_read,
RT_NULL,
rt_sensor_control
};
#endif
/*
* sensor register
*/
int rt_hw_sensor_register(rt_sensor_t sensor,
const char *name,
rt_uint32_t flag,
void *data)
{
rt_int8_t result;
rt_device_t device;
RT_ASSERT(sensor != RT_NULL);
char *sensor_name = RT_NULL, *device_name = RT_NULL;
/* Add a type name for the sensor device */
sensor_name = sensor_name_str[sensor->info.type];
device_name = rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
if (device_name == RT_NULL)
{
LOG_E("device_name calloc failed!");
return -RT_ERROR;
}
rt_memcpy(device_name, sensor_name, rt_strlen(sensor_name) + 1);
strcat(device_name, name);
if (sensor->module != RT_NULL && sensor->module->lock == RT_NULL)
{
/* Create a mutex lock for the module */
sensor->module->lock = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
if (sensor->module->lock == RT_NULL)
{
rt_free(device_name);
return -RT_ERROR;
}
}
device = &sensor->parent;
#ifdef RT_USING_DEVICE_OPS
device->ops = &rt_sensor_ops;
#else
device->init = rt_sensor_init;
device->open = rt_sensor_open;
device->close = rt_sensor_close;
device->read = rt_sensor_read;
device->write = RT_NULL;
device->control = rt_sensor_control;
#endif
device->type = RT_Device_Class_Sensor;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
device->user_data = data;
result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE);
if (result != RT_EOK)
{
LOG_E("rt_sensor register err code: %d", result);
return result;
}
LOG_I("rt_sensor init success");
return RT_EOK;
}

View File

@ -1,228 +0,0 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2014-08-03 Bernard the first version
*/
#include <stddef.h>
#include <string.h>
#include "sensor.h"
/**
* SensorBase
*/
SensorBase::SensorBase(int type)
{
memset(&(this->config), 0x0, sizeof(SensorConfig));
this->type = type;
this->next = this->prev = NULL;
subscribe(NULL, NULL);
}
SensorBase::~SensorBase()
{
}
int SensorBase::getType(void)
{
return this->type;
}
int SensorBase::setConfig(SensorConfig *config)
{
int result;
/* configure to the low level sensor */
result = this->configure(config);
if (result == 0)
{
this->config = *config;
}
return result;
}
int SensorBase::getConfig(SensorConfig *config)
{
*config = this->config;
return 0;
}
int SensorBase::subscribe(SensorEventHandler_t handler, void *user_data)
{
this->evtHandler = handler;
this->userData = user_data;
return 0;
}
int SensorBase::publish(void)
{
if (this->evtHandler != NULL)
{
/* invoke subscribed handler */
(*evtHandler)(this->userData);
}
return 0;
}
/**
* Sensor Manager
*/
/* sensors list */
static SensorBase *sensor_list = NULL;
SensorManager::SensorManager()
{
}
SensorManager::~SensorManager()
{
}
int SensorManager::registerSensor(SensorBase *sensor)
{
RT_ASSERT(sensor != RT_NULL);
/* add sensor into the list */
if (sensor_list == NULL)
{
sensor->prev = sensor->next = sensor;
}
else
{
sensor_list->prev->next = sensor;
sensor->prev = sensor_list->prev;
sensor_list->prev = sensor;
sensor->next = sensor_list;
}
/* point the sensorList to this sensor */
sensor_list = sensor;
return 0;
}
int SensorManager::unregisterSensor(SensorBase *sensor)
{
/* disconnect sensor list */
sensor->next->prev = sensor->prev;
sensor->prev->next = sensor->next;
/* check the sensorList */
if (sensor == sensor_list)
{
if (sensor->next == sensor) sensor_list = NULL; /* empty list */
else sensor_list = sensor->next;
}
/* re-initialize sensor node */
sensor->next = sensor->prev = sensor;
return 0;
}
SensorBase *SensorManager::getDefaultSensor(int type)
{
SensorBase *sensor = sensor_list;
if (sensor == NULL) return NULL;
do
{
/* find the same type */
if (sensor->getType() == type) return sensor;
sensor = sensor->next;
}while (sensor != sensor_list);
return NULL;
}
int SensorManager::subscribe(int type, SensorEventHandler_t handler, void *user_data)
{
SensorBase *sensor;
sensor = SensorManager::getDefaultSensor(type);
if (sensor != NULL)
{
sensor->subscribe(handler, user_data);
return 0;
}
return -1;
}
int SensorManager::sensorEventReady(SensorBase *sensor)
{
return 0;
}
int SensorManager::pollSensor(SensorBase *sensor, sensors_event_t *events, int number, int duration)
{
rt_tick_t tick;
int result, index;
if (sensor == NULL) return -1;
tick = rt_tick_get();
for (index = 0; index < number; index ++)
{
result = sensor->poll(&events[index]);
if (result < 0) break;
if (rt_tick_get() - tick > duration) break;
}
return index;
}
rt_sensor_t rt_sensor_get_default(int type)
{
return (rt_sensor_t)SensorManager::getDefaultSensor(type);
}
int rt_sensor_subscribe(rt_sensor_t sensor, SensorEventHandler_t handler, void *user_data)
{
SensorBase *sensor_base;
if (sensor == NULL) return -1;
sensor_base = (SensorBase*)sensor;
return sensor_base->subscribe(handler, user_data);
}
int rt_sensor_poll(rt_sensor_t sensor, sensors_event_t *event)
{
SensorBase *sensor_base;
if (sensor == NULL || event == NULL) return -1;
sensor_base = (SensorBase*)sensor;
return sensor_base->poll(event);
}
int rt_sensor_configure(rt_sensor_t sensor, SensorConfig *config)
{
SensorBase *sensor_base;
if (sensor == NULL || config == NULL) return -1;
sensor_base = (SensorBase*)sensor;
return sensor_base->setConfig(config);
}
int rt_sensor_activate(rt_sensor_t sensor, int enable)
{
SensorBase *sensor_base;
if (sensor == NULL) return -1;
sensor_base = (SensorBase*)sensor;
return sensor_base->activate(enable);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,361 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-01-31 flybreak first version
*/
#include "sensor.h"
#define DBG_ENABLE
#define DBG_LEVEL DBG_INFO
#define DBG_SECTION_NAME "sensor.test"
#define DBG_COLOR
#include <rtdbg.h>
#include <stdlib.h>
#include <string.h>
static rt_sem_t sensor_rx_sem = RT_NULL;
static void sensor_show_data(rt_size_t num, rt_sensor_t sensor, struct rt_sensor_data *sensor_data)
{
switch (sensor->info.type)
{
case RT_SEN_CLASS_ACCE:
LOG_I("num:%3d, x:%5d, y:%5d, z:%5d, timestamp:%5d", num, sensor_data->data.acce.x, sensor_data->data.acce.y, sensor_data->data.acce.z, sensor_data->timestamp);
break;
case RT_SEN_CLASS_GYRO:
LOG_I("num:%3d, x:%8d, y:%8d, z:%8d, timestamp:%5d", num, sensor_data->data.gyro.x, sensor_data->data.gyro.y, sensor_data->data.gyro.z, sensor_data->timestamp);
break;
case RT_SEN_CLASS_MAG:
LOG_I("num:%3d, x:%5d, y:%5d, z:%5d, timestamp:%5d", num, sensor_data->data.mag.x, sensor_data->data.mag.y, sensor_data->data.mag.z, sensor_data->timestamp);
break;
case RT_SEN_CLASS_HUMI:
LOG_I("num:%3d, humi:%3d.%d%%, timestamp:%5d", num, sensor_data->data.humi / 10, sensor_data->data.humi % 10, sensor_data->timestamp);
break;
case RT_SEN_CLASS_TEMP:
LOG_I("num:%3d, temp:%3d.%dC, timestamp:%5d", num, sensor_data->data.temp / 10, sensor_data->data.temp % 10, sensor_data->timestamp);
break;
case RT_SEN_CLASS_BARO:
LOG_I("num:%3d, press:%5d, timestamp:%5d", num, sensor_data->data.baro, sensor_data->timestamp);
break;
case RT_SEN_CLASS_STEP:
LOG_I("num:%3d, step:%5d, timestamp:%5d", num, sensor_data->data.step, sensor_data->timestamp);
break;
default:
break;
}
}
rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
{
rt_sem_release(sensor_rx_sem);
return 0;
}
static void sensor_fifo_rx_entry(void *parameter)
{
rt_device_t dev = parameter;
rt_sensor_t sensor = parameter;
struct rt_sensor_data *data = RT_NULL;
struct rt_sensor_info info;
rt_size_t res, i;
rt_device_control(dev, RT_SEN_CTRL_GET_INFO, &info);
data = rt_malloc(sizeof(struct rt_sensor_data) * info.fifo_max);
if (data == RT_NULL)
{
LOG_E("Memory allocation failed!");
}
while (1)
{
rt_sem_take(sensor_rx_sem, RT_WAITING_FOREVER);
res = rt_device_read(dev, 0, data, info.fifo_max);
for (i = 0; i < res; i++)
{
sensor_show_data(i, sensor, &data[i]);
}
}
}
static void sensor_fifo(int argc, char **argv)
{
static rt_thread_t tid1 = RT_NULL;
rt_device_t dev = RT_NULL;
rt_sensor_t sensor;
dev = rt_device_find(argv[1]);
if (dev == RT_NULL)
{
LOG_E("Can't find device:%s", argv[1]);
return;
}
sensor = (rt_sensor_t)dev;
if (sensor_rx_sem == RT_NULL)
{
sensor_rx_sem = rt_sem_create("sen_rx_sem", 0, RT_IPC_FLAG_FIFO);
}
else
{
LOG_E("The thread is running, please reboot and try again");
return;
}
tid1 = rt_thread_create("sen_rx_thread",
sensor_fifo_rx_entry, sensor,
1024,
15, 5);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
rt_device_set_rx_indicate(dev, rx_callback);
if (rt_device_open(dev, RT_SEN_FLAG_FIFO) != RT_EOK)
{
LOG_E("open device failed!");
return;
}
rt_device_control(dev, RT_SEN_CTRL_SET_ODR, (void *)20);
}
#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(sensor_fifo, Sensor fifo mode test function);
#endif
static void sensor_irq_rx_entry(void *parameter)
{
rt_device_t dev = parameter;
rt_sensor_t sensor = parameter;
struct rt_sensor_data data;
rt_size_t res, i = 0;
while (1)
{
rt_sem_take(sensor_rx_sem, RT_WAITING_FOREVER);
res = rt_device_read(dev, 0, &data, 1);
if (res == 1)
{
sensor_show_data(i++, sensor, &data);
}
}
}
static void sensor_int(int argc, char **argv)
{
static rt_thread_t tid1 = RT_NULL;
rt_device_t dev = RT_NULL;
rt_sensor_t sensor;
dev = rt_device_find(argv[1]);
if (dev == RT_NULL)
{
LOG_E("Can't find device:%s", argv[1]);
return;
}
sensor = (rt_sensor_t)dev;
if (sensor_rx_sem == RT_NULL)
{
sensor_rx_sem = rt_sem_create("sen_rx_sem", 0, RT_IPC_FLAG_FIFO);
}
else
{
LOG_E("The thread is running, please reboot and try again");
return;
}
tid1 = rt_thread_create("sen_rx_thread",
sensor_irq_rx_entry, sensor,
1024,
15, 5);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
rt_device_set_rx_indicate(dev, rx_callback);
if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
{
LOG_E("open device failed!");
return;
}
rt_device_control(dev, RT_SEN_CTRL_SET_ODR, (void *)20);
}
#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(sensor_int, Sensor interrupt mode test function);
#endif
static void sensor_polling(int argc, char **argv)
{
uint16_t num = 10;
rt_device_t dev = RT_NULL;
rt_sensor_t sensor;
struct rt_sensor_data data;
rt_size_t res, i;
dev = rt_device_find(argv[1]);
if (dev == RT_NULL)
{
LOG_E("Can't find device:%s", argv[1]);
return;
}
if (argc > 2)
num = atoi(argv[2]);
sensor = (rt_sensor_t)dev;
if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK)
{
LOG_E("open device failed!");
return;
}
rt_device_control(dev, RT_SEN_CTRL_SET_ODR, (void *)100);
for (i = 0; i < num; i++)
{
res = rt_device_read(dev, 0, &data, 1);
if (res != 1)
{
LOG_E("read data failed!size is %d", res);
}
else
{
sensor_show_data(i, sensor, &data);
}
rt_thread_mdelay(100);
}
rt_device_close(dev);
}
#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(sensor_polling, Sensor polling mode test function);
#endif
static void sensor(int argc, char **argv)
{
static rt_device_t dev = RT_NULL;
struct rt_sensor_data data;
rt_size_t res, i;
/* If the number of arguments less than 2 */
if (argc < 2)
{
rt_kprintf("\n");
rt_kprintf("sensor [OPTION] [PARAM]\n");
rt_kprintf(" probe <dev_name> Probe sensor by given name\n");
rt_kprintf(" info Get sensor info\n");
rt_kprintf(" sr <var> Set range to var\n");
rt_kprintf(" sm <var> Set work mode to var\n");
rt_kprintf(" sp <var> Set power mode to var\n");
rt_kprintf(" sodr <var> Set output date rate to var\n");
rt_kprintf(" read [num] Read [num] times sensor\n");
rt_kprintf(" num default 5\n");
return ;
}
else if (!strcmp(argv[1], "info"))
{
struct rt_sensor_info info;
rt_device_control(dev, RT_SEN_CTRL_GET_INFO, &info);
rt_kprintf("vendor :%d\n", info.vendor);
rt_kprintf("model :%s\n", info.model);
rt_kprintf("unit :%d\n", info.unit);
rt_kprintf("range_max :%d\n", info.range_max);
rt_kprintf("range_min :%d\n", info.range_min);
rt_kprintf("period_min:%d\n", info.period_min);
rt_kprintf("fifo_max :%d\n", info.fifo_max);
}
else if (!strcmp(argv[1], "read"))
{
uint16_t num = 5;
if (dev == RT_NULL)
{
LOG_W("Please probe sensor device first!");
return ;
}
if (argc == 3)
{
num = atoi(argv[2]);
}
for (i = 0; i < num; i++)
{
res = rt_device_read(dev, 0, &data, 1);
if (res != 1)
{
LOG_E("read data failed!size is %d", res);
}
else
{
sensor_show_data(i, (rt_sensor_t)dev, &data);
}
rt_thread_mdelay(100);
}
}
else if (argc == 3)
{
if (!strcmp(argv[1], "probe"))
{
rt_uint8_t reg = 0xFF;
if (dev)
{
rt_device_close(dev);
}
dev = rt_device_find(argv[2]);
if (dev == RT_NULL)
{
LOG_E("Can't find device:%s", argv[1]);
return;
}
if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK)
{
LOG_E("open device failed!");
return;
}
rt_device_control(dev, RT_SEN_CTRL_GET_ID, &reg);
LOG_I("device id: 0x%x!", reg);
}
else if (dev == RT_NULL)
{
LOG_W("Please probe sensor first!");
return ;
}
else if (!strcmp(argv[1], "sr"))
{
rt_device_control(dev, RT_SEN_CTRL_SET_RANGE, (void *)atoi(argv[2]));
}
else if (!strcmp(argv[1], "sm"))
{
rt_device_control(dev, RT_SEN_CTRL_SET_MODE, (void *)atoi(argv[2]));
}
else if (!strcmp(argv[1], "sp"))
{
rt_device_control(dev, RT_SEN_CTRL_SET_POWER, (void *)atoi(argv[2]));
}
else if (!strcmp(argv[1], "sodr"))
{
rt_device_control(dev, RT_SEN_CTRL_SET_ODR, (void *)atoi(argv[2]));
}
else
{
LOG_W("Unknown command, please enter 'sensor' get help information!");
}
}
else
{
LOG_W("Unknown command, please enter 'sensor' get help information!");
}
}
#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(sensor, sensor test function);
#endif

View File

@ -800,6 +800,7 @@ static char *const device_type_str[] =
"Portal Device",
"Timer Device",
"Miscellaneous Device",
"Sensor Device",
"Unknown"
};

View File

@ -846,6 +846,7 @@ enum rt_device_class_type
RT_Device_Class_Portal, /**< Portal device */
RT_Device_Class_Timer, /**< Timer device */
RT_Device_Class_Miscellaneous, /**< Miscellaneous device */
RT_Device_Class_Sensor, /**< Sensor device */
RT_Device_Class_Unknown /**< unknown device */
};