Android—GSensor驱动与HAL/Framework

原创 2013年12月05日 14:16:45

1、驱动文件RANGE:

(1)

/*
  Range: unit(ug 1g=1 000 000 ug)
  option(2g,4g,8g)
  G would be defined on android HAL
  Precision: bit wide of valid data
  Boundary: Max positive count
  Gravity_step: gravity value indicated by per count
 */
#define FREAD_MASK				0 /* enabled(1<<1) only if reading MSB 8bits*/
#define MMA845X_RANGE			2000000
/* mma8452 */
#define MMA8452_PRECISION       12
#define MMA8452_BOUNDARY        (0x1 << (MMA8452_PRECISION - 1))
#define MMA8452_GRAVITY_STEP    MMA845X_RANGE / MMA8452_BOUNDARY


(2)

result = ((int)high_byte << (MMA8452_PRECISION-8)) | ((int)low_byte >> (16-MMA8452_PRECISION)); //合并数据
if (result < MMA8452_BOUNDARY) //判断符号
    result = result* MMA8452_GRAVITY_STEP;
else
    result = ~( ((~result & (0x7fff>>(16-MMA8452_PRECISION)) ) + 1) * MMA8452_GRAVITY_STEP) + 1;
		


2、HAL层

(1)MMA IOCTL (mma8452.h )
#define MMAIO 0xA1
/* IOCTLs for MMA8452 library */
#define ECS_IOCTL_INIT                  _IO(MMAIO, 0x01)
// #define ECS_IOCTL_RESET                _IO(MMAIO, 0x04)
#define ECS_IOCTL_CLOSE           _IO(MMAIO, 0x02)
#define ECS_IOCTL_START             _IO(MMAIO, 0x03)
// #define ECS_IOCTL_GETDATA               _IOR(MMAIO, 0x08, char[RBUFF_SIZE+1])
/* IOCTLs for APPs */
#define ECS_IOCTL_APP_SET_RATE _IOW(MMAIO, 0x10, char)
(2)(nusensors.h)
#define ACCELERATION_RATIO_ANDROID_TO_HW        (9.80665f / 1000000)
(3)(MmaSensor.cpp)
void MmaSensor::processEvent(int code, int value)
{    
    switch (code) {
        case EVENT_TYPE_ACCEL_X:
            mPendingMask |= 1<<Accelerometer;
            mPendingEvents[Accelerometer].acceleration.x = value * ACCELERATION_RATIO_ANDROID_TO_HW;
            break;
        case EVENT_TYPE_ACCEL_Y:
            mPendingMask |= 1<<Accelerometer;
            mPendingEvents[Accelerometer].acceleration.y = value * ACCELERATION_RATIO_ANDROID_TO_HW;
            break;
        case EVENT_TYPE_ACCEL_Z:
            mPendingMask |= 1<<Accelerometer;
            mPendingEvents[Accelerometer].acceleration.z = value * ACCELERATION_RATIO_ANDROID_TO_HW;
            break;
    }
}
(4)Sensor_params.h
//ACCEL_ID_LIS331
#define ACCEL_LIS331_RANGE      (2.480f*GRAVITY_EARTH)
#define ACCEL_LIS331_RESOLUTION     (.001f*GRAVITY_EARTH)
#define ACCEL_LIS331_POWER      (1.0f)
//ACCEL_ID_LSM303DLX
#define ACCEL_LSM303DLX_RANGE       (2.480f*GRAVITY_EARTH)
#define ACCEL_LSM303DLX_RESOLUTION  (0.001f*GRAVITY_EARTH)
#define ACCEL_LSM303DLX_POWER       (1.0f)
//ACCEL_ID_LIS3DH
#define ACCEL_LIS3DH_RANGE      (2.480f*GRAVITY_EARTH)
#define ACCEL_LIS3DH_RESOLUTION     (0.001f*GRAVITY_EARTH)
#define ACCEL_LIS3DH_POWER      (1.0f)
//ACCEL_ID_KXSD9
#define ACCEL_KXSD9_RANGE       (2.5006f*GRAVITY_EARTH)
#define ACCEL_KXSD9_RESOLUTION      (0.001f*GRAVITY_EARTH)
#define ACCEL_KXSD9_POWER       (1.0f)
//ACCEL_ID_KXTF9
#define ACCEL_KXTF9_RANGE       (1.0f*GRAVITY_EARTH)
#define ACCEL_KXTF9_RESOLUTION      (0.033f*GRAVITY_EARTH)
#define ACCEL_KXTF9_POWER       (0.35f)
//ACCEL_ID_BMA150
#define ACCEL_BMA150_RANGE      (2.0f*GRAVITY_EARTH)
#define ACCEL_BMA150_RESOLUTION     (0.004f*GRAVITY_EARTH)
#define ACCEL_BMA150_POWER      (0.2f)
//ACCEL_ID_BMA222
#define ACCEL_BMA222_RANGE      (2.0f*GRAVITY_EARTH)
#define ACCEL_BMA222_RESOLUTION     (0.001f*GRAVITY_EARTH)
#define ACCEL_BMA222_POWER      (0.1f)
//ACCEL_ID_BMA250
#define ACCEL_BMA250_RANGE      (2.0f*GRAVITY_EARTH)
#define ACCEL_BMA250_RESOLUTION     (0.00391f*GRAVITY_EARTH)
#define ACCEL_BMA250_POWER      (0.139f)
//ACCEL_ID_ADXL34X
#define ACCEL_ADXL34X_RANGE     (2.0f*GRAVITY_EARTH)
#define ACCEL_ADXL34X_RESOLUTION    (0.001f*GRAVITY_EARTH)
#define ACCEL_ADXL34X_POWER     (1.0f)
//ACCEL_ID_MMA8450
#define ACCEL_MMA8450_RANGE     (2.0f*GRAVITY_EARTH)
#define ACCEL_MMA8450_RESOLUTION    (0.001f*GRAVITY_EARTH)
#define ACCEL_MMA8450_POWER     (1.0f)
//ACCEL_ID_MMA845X
#define ACCEL_MMA845X_RANGE     (2.0f*GRAVITY_EARTH)
#define ACCEL_MMA845X_RESOLUTION    (0.001f*GRAVITY_EARTH)
#define ACCEL_MMA845X_POWER     (1.0f)
//ACCEL_ID_MPU6050
#define ACCEL_MPU6050_RANGE     (2.0f*GRAVITY_EARTH)
#define ACCEL_MPU6050_RESOLUTION    (0.004f*GRAVITY_EARTH)
#define ACCEL_MPU6050_POWER     (0.0f)
/******************************************/
//GYRO MPU3050
#define RAD_P_DEG (3.14159f/180.0f)
#define GYRO_MPU3050_RANGE      (2000.0f*RAD_P_DEG)
#define GYRO_MPU3050_RESOLUTION     (32.8f*RAD_P_DEG)
#define GYRO_MPU3050_POWER      (6.1f)
//GYRO MPU6050
#define GYRO_MPU6050_RANGE      (2000.0f*RAD_P_DEG)
#define GYRO_MPU6050_RESOLUTION     (16.4f*RAD_P_DEG)
#define GYRO_MPU6050_POWER      (5.5f)

(6)MPLSensor.cpp

void MPLSensor::fillAccel(unsigned char accel, struct sensor_t *list)
{
    switch (accel) {
    case ACCEL_ID_LIS331:
        list[Accelerometer].maxRange = ACCEL_LIS331_RANGE;
        list[Accelerometer].resolution = ACCEL_LIS331_RESOLUTION;
        list[Accelerometer].power = ACCEL_LIS331_POWER;
        break;


    case ACCEL_ID_LIS3DH:
        list[Accelerometer].maxRange = ACCEL_LIS3DH_RANGE;
        list[Accelerometer].resolution = ACCEL_LIS3DH_RESOLUTION;
        list[Accelerometer].power = ACCEL_LIS3DH_POWER;
        break;


    case ACCEL_ID_KXSD9:
        list[Accelerometer].maxRange = ACCEL_KXSD9_RANGE;
        list[Accelerometer].resolution = ACCEL_KXSD9_RESOLUTION;
        list[Accelerometer].power = ACCEL_KXSD9_POWER;
        break;


    case ACCEL_ID_KXTF9:
        list[Accelerometer].maxRange = ACCEL_KXTF9_RANGE;
        list[Accelerometer].resolution = ACCEL_KXTF9_RESOLUTION;
        list[Accelerometer].power = ACCEL_KXTF9_POWER;
        break;


    case ACCEL_ID_BMA150:
        list[Accelerometer].maxRange = ACCEL_BMA150_RANGE;
        list[Accelerometer].resolution = ACCEL_BMA150_RESOLUTION;
        list[Accelerometer].power = ACCEL_BMA150_POWER;
        break;


    case ACCEL_ID_BMA222:
        list[Accelerometer].maxRange = ACCEL_BMA222_RANGE;
        list[Accelerometer].resolution = ACCEL_BMA222_RESOLUTION;
        list[Accelerometer].power = ACCEL_BMA222_POWER;
        break;


    case ACCEL_ID_BMA250:
        list[Accelerometer].maxRange = ACCEL_BMA250_RANGE;
        list[Accelerometer].resolution = ACCEL_BMA250_RESOLUTION;
        list[Accelerometer].power = ACCEL_BMA250_POWER;
        break;


    case ACCEL_ID_ADXL34X:
        list[Accelerometer].maxRange = ACCEL_ADXL34X_RANGE;
        list[Accelerometer].resolution = ACCEL_ADXL34X_RESOLUTION;
        list[Accelerometer].power = ACCEL_ADXL34X_POWER;
        break;


    case ACCEL_ID_MMA8450:
        list[Accelerometer].maxRange = ACCEL_MMA8450_RANGE;    
        list[Accelerometer].maxRange = ACCEL_MMA8450_RANGE;
        list[Accelerometer].maxRange = ACCEL_MMA8450_RANGE;
        break;


    case ACCEL_ID_MMA845X:
        list[Accelerometer].maxRange = ACCEL_MMA845X_RANGE;
        list[Accelerometer].resolution = ACCEL_MMA845X_RESOLUTION;
        list[Accelerometer].power = ACCEL_MMA845X_POWER;
        break;


    case ACCEL_ID_MPU6050:
        list[Accelerometer].maxRange = ACCEL_MPU6050_RANGE;
        list[Accelerometer].resolution = ACCEL_MPU6050_RESOLUTION;
        list[Accelerometer].power = ACCEL_MPU6050_POWER;
        break;
    default:
        ALOGE("unknown accel id -- accel params will be wrong.");
        break;
    }
}

3、Framework-Java-->SensorEvent.java

     *     public void onSensorChanged(SensorEvent event)
     *     {
     *          // alpha is calculated as t / (t + dT)
     *          // with t, the low-pass filter's time-constant
     *          // and dT, the event delivery rate
     *
     *          final float alpha = 0.8;
     *
     *          gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
     *          gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
     *          gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
     *
     *          linear_acceleration[0] = event.values[0] - gravity[0];
     *          linear_acceleration[1] = event.values[1] - gravity[1];
     *          linear_acceleration[2] = event.values[2] - gravity[2];
     *     }



相关文章推荐

sensor__HAl分析(实现)

首先是sensor这个模块的id定义,主要是实现了一个sensors_module_t结构: struct sensors_module_t HAL_MODULE_INFO_SYM = { ...
  • new_abc
  • new_abc
  • 2013年05月24日 21:10
  • 9748

Android之传感器系统(Gsensor)

最近在解关于Gsensor的Bug,Bug还没解掉,反而把Android的Gsensor流程走了一遍。好久不写博客了,不能偷懒啊,学了东西还是得总结一下,好让后来人不要费多少工夫,同时抛砖引玉,大家一...

linux Gsensor驱动(bma250为例子)

1 Gsensor 驱动概述   本文以Bma250驱动为例子,详细介绍Gsensor设计的一个模板。  gsensor驱动在系统中的层次如下图所示: Gsensor驱动(bma250为例子)"...

G-Sensor 8452驱动及相关

8452是一款G-Sensor芯片,采用I2C跟主芯片通讯,采用中断方式跟操作系统协作。通过内部检测XYZ三个方向的加速度,实现各种应用。 (1)原理框图如下:               现在来实...

Android5.0编写hello从APP->framework->jni->hal->驱动的BUG

Android5.0编写hello从APP->framework->jni->hal->驱动的BUG     大概一周的时间,终于把Android5.0整个框架给调通了,期间遇到许许多多的B...

Android应用层到Framework到HAL再到驱动层的整个流程分析

本篇参考老罗的实例进行总结。老罗写六篇,层层嵌套,他告诉了我们流程,但没有说编程思想,所以,即使知道怎么做也很快会忘调,因此打算总结下每层之间是怎么调用的,以加深印象。不对细节进行探讨。细节可以参见老...

Android摄像头相关源码分析: 设备驱动, HAL, Framework

kohntzx@gmail.com 藏经阁 首页 ...

Android之 看“马达”如何贯通Android系统 (从硬件设计 --> 驱动 --> HAL --> JNI --> Framework --> Application)

在Android 2.3(Gingerbread) 系统的时候,我写过一篇关于“Android 震动马达系统“的文章,当时的Linux内核还是2.6版本的。写那篇文章的目的,是想彻底的了解从硬件到驱动...
  • xie0812
  • xie0812
  • 2014年01月23日 07:52
  • 880

android-framework-hal(免积分下载)

  • 2012年12月10日 22:23
  • 3.25MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android—GSensor驱动与HAL/Framework
举报原因:
原因补充:

(最多只允许输入30个字)