Android2.3.4 added gsensor

Source: Internet
Author: User

Sensor: lsm330dlc (acc + gyr)
1. Place the lsm330dlc driver code in the drivers/input/misc directory and add the following fields to the kconfig and Makefile files:

Kconfig
Config SENSORS_LSM330DLC
Tristate "ST's family of MEMS sensor modules of LSM330DLC"
Depends on I2C & INPUT & MACH_ZT6810
Select INPUT_POLLDEV
Default y
Help
This driver provides support for the LSM330DLC gyroscope and accelerator connected via I2C.


Makefile
Obj-$ (CONFIG_SENSORS_LSM330DLC) + = lsm330dlc_acc.o lsm330dlc_gyr.o


2. Modify the interrupt IO and data all-powerful IO in lsm330dlc. h according to the hardware schematic, for example:

/* Xandy modify the accelerator and gyro interrupt pins @ 2012/9/12 */
# Define INT1_GPIO_ACC 178
# Define INT2_GPIO_ACC 179
# Define INT1_GPIO_GYR 181
# Define INT2_GPIO_GYR 180
/* Xandy add Sensors enable pin */
# Define SENSOR_EN_GPIO 38


3. Modify the BSP file (corresponding to the board-xx.c file), The lsm330dlc driver registration (I2C bus), such:

# Ifdef CONFIG_SENSORS_LSM330DLC
Static struct i2c_board_info _ initdata zt6810_i2c_LSM330DLC [] = {
{
I2C_BOARD_INFO ("lsm330dlc_acc", 0x19 ),
},
{
I2C_BOARD_INFO ("lsm330dlc_gyr", 0x6B ),
},
};
# Endif
Static int _ init zt6810_i2c_init (void)
{
Omap_register_i2c_bus (1, 2600, zt6810_i2c_boardinfo,
ARRAY_SIZE (zt6810_i2c_boardinfo ));
/* Bus 2 is used for Battery ckeck (bq27410 )*/
# Ifdef CONFIG_BATTERY_MAX17049
Omap_register_i2c_bus (2,100, zt6810_i2c_MAX17049, ARRAY_SIZE (zt6810_i2c_MAX17049 ));
# Else
Omap_register_i2c_bus (2,100, NULL, 0 );
# Endif
/* Projector don't work reliably with 400 kHz */
# Ifdef CONFIG_SENSORS_LSM330DLC
Omap_register_i2c_bus (3,200, zt6810_i2c_LSM330DLC, ARRAY_SIZE (zt6810_i2c_LSM330DLC ));
# Endif
Return 0;
}


After the kernel is compiled, the uImage will be burned to the prototype. If there is no accident, go to the corresponding directory on the serial port terminal and ls will see the following content:

 

 

 

 

The acc is successfully registered on the I2C bus. The corresponding I2C bus number and address are 3-0019, while gyr is 3-006b.
4. Enter the following command in the serial port:

# Cat/proc/bus/input/devices

 

The following information is displayed:

# Cat/proc/bus/input/devices
I: Bus = 0019 Vendor = 0001 Product = 0001 Version = 0100
N: Name = "gpio-keys"
P: Phys = gpio-keys/input0
S: Sysfs =/devices/platform/gpio-keys/input/input0
U: Uniq =
H: Handlers = kbd event0
B: EV = 3
B: KEY = 4 0 0 0 100000 0 0 0
I: Bus = 0019 Vendor = 0001 Product = 0001 Version = 0003
N: Name = "TWL4030 Keypad"
P: Phys = twl4030_keypad/input0
S: Sysfs =/devices/platform/omap/omap_i2c.1/i2c-1/1-004a/twl4030_keypad/input/input1
U: Uniq =
H: Handlers = kbd event1
B: EV = 100013
B: KEY = 800 c1680 0 2000000 100000aa
B: MSC = 10
I: Bus = 0000 Vendor = 0000 Product = 0000 Version = 0000
N: Name = "ADS7846 Touchscreen"
P: Phys = spi1.0/input0
S: Sysfs =/devices/platform/omap2_mcspi.1/spi1.0/input/input2
U: Uniq =
H: Handlers = mouse0 event2
B: EV = B
B: Keys = 400 0 0 0 0 0 0 0 0 0 0
B: ABS = 1, 1000003
I: Bus = 0000 Vendor = 0000 Product = 0000 Version = 0000
N: Name = "twl4030_pwrbutton"
P: Phys = twl4030_pwrbutton/input0
S: Sysfs =/devices/platform/omap/omap_i2c.1/i2c-1/1-0049/twl4030_pwrbutton/input/input3
U: Uniq =
H: Handlers = kbd event3
B: EV = 3
B: KEY = 100000 0 0 0
I: Bus = 0018 Vendor = 0000 Product = 0000 Version = 0000
N: Name = "lsm330dlc_acc"
P: Phys =
S: Sysfs =/devices/platform/omap/omap_i2c.3/i2c-3/3-0019/input/input4
U: Uniq =
H: Handlers = js0 event4
B: EV = 9
B: ABS = 100 107
I: Bus = 0018 Vendor = 0000 Product = 0000 Version = 0000
N: Name = "lsm330dlc_gyr"
P: Phys =
S: Sysfs =/devices/platform/omap/omap_i2c.3/i2c-3/3-006b/input/input5
U: Uniq =
H: Handlers = js1 event5
B: EV = 9
B: ABS = 7

 


Some information about the input devices already registered by the system is listed here. The last two items are the corresponding input devices registered by the previously added sensor (acc + gyr ).

Why is the sensor registered as an input device? See the following content in the driver source code:

Static int lsm330dlc_acc_input_init (struct lsm330dlc_acc_data * acc)
{
Int err;
INIT_DELAYED_WORK (& acc-> input_work, lsm330dlc_acc_input_work_func );
Acc-> input_dev = input_allocate_device ();
If (! Acc-> input_dev ){
Err =-ENOMEM;
Dev_err (& acc-> client-> dev, "input device allocation failed \ n ");
Goto err0;
}
Acc-> input_dev-> open = lsm330dlc_acc_input_open;
Acc-> input_dev-> close = lsm330dlc_acc_input_close;
Acc-> input_dev-> name = LSM330DLC_ACC_DEV_NAME;
// Acc-> input_dev-> name = "accelerometer ";
Acc-> input_dev-> id. bustype = BUS_I2C;
Acc-> input_dev-> dev. parent = & acc-> client-> dev;
Input_set_drvdata (acc-> input_dev, acc );
Set_bit (EV_ABS, acc-> input_dev-> evbit );
/* Next is used for interruptA sources data if the case */
Set_bit (ABS_MISC, acc-> input_dev-> absbit );
/* Next is used for interruptB sources data if the case */
Set_bit (ABS_WHEEL, acc-> input_dev-> absbit );
Input_set_abs_params (acc-> input_dev, ABS_X,-G_MAX, G_MAX, FUZZ, FLAT );
Input_set_abs_params (acc-> input_dev, ABS_Y,-G_MAX, G_MAX, FUZZ, FLAT );
Input_set_abs_params (acc-> input_dev, ABS_Z,-G_MAX, G_MAX, FUZZ, FLAT );
/* Next is used for interruptA sources data if the case */
Input_set_abs_params (acc-> input_dev, ABS_MISC, INT_MIN, INT_MAX, 0, 0 );
/* Next is used for interruptB sources data if the case */
Input_set_abs_params (acc-> input_dev, ABS_WHEEL, INT_MIN, INT_MAX, 0, 0 );
Err = input_register_device (acc-> input_dev );
If (err ){
Dev_err (& acc-> client-> dev,
"Unable to register input device % s \ n ",
Acc-> input_dev-> name );
Goto err1;
}
 
Return 0;
Err1:
Input_free_device (acc-> input_dev );
Err0:
Return err;
}

Enter the following command in the terminal:
# Getevent
The following information is displayed:
# Getevent
Cocould not get driver version for/dev/input/mice, Not a typewriter
Add device 1:/dev/input/event2
Name: "ADS7846 Touchscreen"
Cocould not get driver version for/dev/input/mouse0, Not a typewriter
Add device 2:/dev/input/event0
Name: "gpio-keys"
Add device 3:/dev/input/event5
Name: "lsm330dlc_gyr"
Cocould not get driver version for/dev/input/js1, Invalid argument
Add device 4:/dev/input/event4
Name: "lsm330dlc_acc"
Cocould not get driver version for/dev/input/js0, Invalid argument
Add device 5:/dev/input/event1
Name: "TWL4030 Keypad"
Add device 6:/dev/input/event3
Name: "twl4030_pwrbutton"
/Dev/input/event4: 0003 0000 00000001
/Dev/input/event4: 0003 0001 ffffff5f
/Dev/input/event4: 0003 0002 000003df
/Dev/input/event4: 0000 0000 00000000
/Dev/input/event4: 0003 0000 fffffffe
/Dev/input/event4: 0003 0001 ffffff5e
/Dev/input/event4: 0003 0002 000003e2
/Dev/input/event4: 0000 0000 00000000
/Dev/input/event4: 0003 0000 00000002
/Dev/input/event4: 0003 0001 ffffff5d
/Dev/input/event4: 0003 0002 000003e5
/Dev/input/event4: 0000 0000 00000000
/Dev/input/event4: 0003 0000 00000001
/Dev/input/event4: 0003 0001 ffffff5f
/Dev/input/event4: 0003 0002 000003df
/Dev/input/event4: 0000 0000 00000000
/Dev/input/event4: 0003 0000 fffffffe
/Dev/input/event4: 0003 0001 ffffff66
/Dev/input/event4: 0003 0002 000003e4
......


The getevent command is used to obtain system input events. It is easy to see that event4 events have always occurred in the current system:
Add device 4:/dev/input/event4
Name: "lsm330dlc_acc"
It can be seen that the added sensor (acc) has started to work and continuously reports the current acceleration event to the system!
If the sensor driver is properly configured and compiled, but the information cannot be obtained, you need to check whether your hardware is normal, such as: whether the chip is consistent; whether there is virtual soldering, find an oscilloscope and check if the I2C waveform is abnormal!
5. After completing the above work, it does not mean that you have completed all the work. Now, although the sensor has started to work, when you enter the gravity sensing interface, such as system settings.
I found that the UI does not rotate at present, or that the next sensor test tool is the same.
This is because there is no channel for reporting the sensor event in HAL. Let's take a look at how to modify the code in HAL.
The sensor HAL Layer Code is in
Hardware/ti/omap3/libsensors
Directory.
Let's take a look at the sensors. cpp file. The sensor is encapsulated in the HAL layer.
Struct sensors_module_t HAL_MODULE_INFO_SYM = {
Common :{
Tag: HARDWARE_MODULE_TAG,
Version_major: 1,
Version_minor: 0,
Id: SENSORS_HARDWARE_MODULE_ID,
Name: "Sensor module ",
Author: "Texas Instruments Inc .",
Methods: & sensors_module_methods,
},
Get_sensors_list: sensors _ get_sensors_list,
};
This is a standard structure encapsulated by the HAL layer. Let's take a look at the following code:
Sensors_poll_context_t: sensors_poll_context_t ()
{
MSensors [accel] = new AccelSensor ();
MPollFds [accel]. fd = mSensors [accel]-> getFd ();
MPollFds [accel]. events = POLLIN;
MPollFds [accel]. revents = 0;
MSensors [gyro] = new GyroSensor ();
MPollFds [gyro]. fd = mSensors [gyro]-> getFd ();
MPollFds [gyro]. events = POLLIN;
MPollFds [gyro]. revents = 0;
Int wakeFds [2];
Int result = pipe (wakeFds );
LOGE_IF (result <0, "error creating wake pipe (% s)", strerror (errno ));
Fcntl (wakeFds [0], F_SETFL, O_NONBLOCK );
Fcntl (wakeFds [1], F_SETFL, O_NONBLOCK );
MWritePipeFd = wakeFds [1];
MPollFds [wake]. fd = wakeFds [0];
MPollFds [wake]. events = POLLIN;
MPollFds [wake]. revents = 0;
}
We can see that there are two new sensor devices, one is ACC, and the other is GYR! Enter accelSensor. cpp in the following constructor:
AccelSensor: AccelSensor ()
: SensorBase (NULL, "lsm330dlc_acc "),
MEnabled (0 ),
MInputReader (4 ),
MHasPendingEvent (false)
{
Memset (& mPendingEvent, 0, sizeof (mPendingEvent ));
MPendingEvent. version = sizeof (sensors_event_t );
MPendingEvent. sensor = ID_A;
MPendingEvent. type = SENSOR_TYPE_ACCELEROMETER;
MPendingEvent. acceleration. status = SENSOR_STATUS_ACCURACY_HIGH;
If (data_fd)
{
Strcpy (input_sysfs_path, ACCEL_SENSOR_NAME );
Input_sysfs_path_len = strlen (input_sysfs_path );
Enable (0, 1 );
}
}

 


The yellow background annotation needs to be modified. The macro definition of ACCEL_SENSOR_NAME is as follows:
/* Xandy add AccelSensor name and GyroSensor name macro definition */
# Ifdef CONFIG_SENSOR_LSM330DLC
# Define ACCEL_SENSOR_NAME "/sys/bus/i2c/drivers/lsm330dlc_acc/3-0019 /"
# Define GYRO_SENSOR_NAME "/sys/bus/i2c/drivers/lsm330dlc_gyr/3-006b /"
# Else
# Define ACCEL_SENSOR_NAME "/sys/bus/i2c/drivers/lis331dlh/1-0018 /"
# Define GYRO_SENSOR_NAME "/sys/bus/i2c/drivers/lda-4200dh_i2c/1-0068 /"
# Endif
The macro switch CONFIG_SENSOR_LSM330DLC is defined as follows in the corresponding Android. mk:
# Xandy add macro define for accel and gyro sensors
Ifeq ($ (SENSOR_LSM330DLC), true)
LOCAL_CFLAGS + =-DCONFIG_SENSOR_LSM330DLC
Endif
SENSOR_LSM330DLC is defined as follows in BoardConfig. mk of the corresponding project (under the device directory, corresponding to your vendor at this time:
# Xandy add accel and gyro sensors
BOARD_HAS_SENSOR: = true
SENSOR_LSM330DLC: = true
These are actually macro definitions and compilation switch settings. This is designed to facilitate switching between different projects (vendor) and add new device support.
The modification of gyr hal is the same as that of ACC. I will not mention it here!
After these modifications, use mm to compile the corresponding lib and push the lib to the directory of the prototype file system through adb. If everything works properly after the system is restarted, now you can enter the UI rotation interface, and it will take effect,
However, the rotation direction is quite different from the running direction at this time, so you have to modify the following macro in sensors. h In Sensor HAL:
# Ifdef CONFIG_SENSOR_LSM330DLC
# Define EVENT_TYPE_ACCEL_X REL_X
# Define EVENT_TYPE_ACCEL_Y REL_Y
# Define EVENT_TYPE_ACCEL_Z REL_Z
# Else
# Error you must define ACCEL X Y Z
# Endif


# Ifdef CONFIG_SENSOR_LSM330DLC
# Define EVENT_TYPE_GYRO_X REL_X // REL_RX
# Define EVENT_TYPE_GYRO_Y REL_Y // REL_RY
# Define EVENT_TYPE_GYRO_Z REL_Z // REL_RZ
# Else
# Error you must define GYRO X Y Z
# Endif
Modify the definitions of x, y, and z as follows:
# Ifdef CONFIG_SENSOR_LSM330DLC
# Define EVENT_TYPE_GYRO_X REL_Z
# Define EVENT_TYPE_GYRO_Y REL_X
# Define EVENT_TYPE_GYRO_Z REL_Y
# Else
# Error you must define GYRO X Y Z
# Endif
Depends on the location of your hardware.


Now the sensor debugging has basically been completed. If there is anything to modify, it will be fine-tuned, such as sensitivity.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.