G-Sensor process in Android

Source: Internet
Author: User

1. What needs to be done to make G-sensor work normally:

The G-sensor driver file includes:

Driver/i2c/chips/lis331dl. c
Driver/i2c/chips/sensorioctl. h
Include/linux/lis331dl. h

And in the/kernel/arch/arm/mach-s3c6410/mach-ur6410.c file i2c chanel1 Structure Variable i2c_devs1 [] _ initdata need to add G-sensor device information,
To make the driver loaded successfully.
Add a structure variable to the file.
// JayLin add for Gsensor
Struct lis331dl_platform_data lisGsensor_platform_data = {
. Name = "lis331dl ",
. Pin_clk = 0,
. Pin_data = 0,
. Open_drain = 1,
. Interrupt = IRQ_EINT (3 ),
};
This structure variable is referenced in i2c_devs1 [] _ initdata.

/Kernel/arch/arm/mach-s3c6410/mach-ur6410.c must contain lis331dl. h.

Add mknod/dev/sensorioctl c 51 201 to the last line of rootfs/system/etc/init. board. sh & create a node for ioctl.

Put the compiled sensor. so under/rootfs/system/lib/hw.

The sensor. so and driver use ioctl to control the status of G-sensor. The command number of ioctl is defined in the header file sensorioctl. h and placed in
In kernel/include/linux
And
Under androidsourcecode/hardware/libhardware/include/hardware
For driver and sensor. so.

The general process of G-sensor driver is as follows:

After the system starts up, first load the i2c bus driver and then the device driver.
In the init function of the device driver, call i2c_add_driver (& lis331dl_i2c_driver) to register i2c_driver. This function registers the driver to the i2c_bus_type bus. The matching rule of this bus is based on the i2c_client name and
The name in id_table in i2c_driver is matched.
The i2c_client registers the onboard information that is automatically created by the system, the process of registering the onboard information is to add the device information of G-sensor to the i2c_devs1 [] _ initdata structure variable of i2c chanel1 in the/kernel/arch/arm/mach-s3c6410/mach-ur6410.c file..
When the match is successful, the probe () function in i2c_driver starts to execute.
The Probe () function provides the following functions:
1. Obtain the initialization information from the i2c_client structure.
2. Create a working queue for G-sensor
2. register the input_device Device
3. Read the Chip ID
4. Set registers to enable G-sensor
5. Set and start the interrupt
When G-sensor reports data, an interruption is triggered. Then, a task with a reported value is submitted to the queue in the interrupt processing function and the interruption is prohibited.
Read the G-sensor data in the work queue and report it to the input subsystem. The final enable is interrupted.

2. The general process from apk to G-sensor driver is as follows:

Android defines Sensor APIs in hardware/libhardware/include/hardware/sensor. h. The following eight API functions must be provided in sensor. so:
[Control]
Int (* open_data_source) (struct sensors_control_device_t * dev );
Int (* activate) (struct sensors_control_device_t * dev, int handle, int enabled );
Int (* set_delay) (struct sensors_control_device_t * dev, int32_t MS );
Int (* wake) (struct sensors_control_device_t * dev );
[Data]
Int (* data_open) (struct sensors_data_device_t * dev, int fd );
Int (* data_close) (struct sensors_data_device_t * dev );
Int (* poll) (struct sensors_data_device_t * dev, sensors_data_t * data );
[Module]
Int (* get_sensors_list) (struct sensors_module_t * module, struct sensor_t const ** list );

The SensorService is responsible for the state control of Sensor at the JAVA layer. Its java code and JNI code are located:
Frameworks/base/services/java/com/android/server/SensorService. java
Frameworks/base/services/jni/com_android_server_SensorService.cpp

In the Java layer, SensorManager is responsible for data control of Sensor. Its java code and JNI code are located:
Frameworks/base/core/java/android/hardware/SensorManager. java
Frameworks/base/core/jni/android_hardware_SensorManager.cpp

In android framework, sensorService. java and sensorManager. java are used to communicate with sensor.
The specific communication of sensorService. java is implemented by calling the method in sensorService. cpp through JNI.
The specific communication of sensorManager. java is implemented by calling the method in sensorManager. cpp through JNI.

SensorService. cpp and sensorManger. cpp communicate with sensor. so through hardware. c. SensorService. cpp controls the sensor status and sensorManger. cpp controls the sensor data.
Sensor. so controls the sensor driver status through ioctl and reads data collected by G-sensor by opening the device file corresponding to the sensor driver.

The android SDK provides four classes for sensor communication: sensor, sensorEvent, sensorEventListener, and sensorManager. The sensorEventListener is used to register the sensor type to be listened to in sensorManager.

SensorManager. java provides the registrater () and unregistrater () interfaces for sensorEventListener.
SensorManager. java continuously polls data from sensor. so. The obtained data is sent to the sensorEventListener. java that listens to this type of sensor. SensorEventListener. java can listen to data from a specific type of sensorManager by logging in sensorManager. java.

When the system starts systemProcess, sensorService. java is started and JNI method _ sensor_control_init () is called in the sensorService. java constructor ().
The android_int () method in sensorService. cpp is executed. This function will call the method hw_get_module () in hardware. c. This function also finds sensor. so under system/lib/hw by calling the load () function.
When searching, it will be based on harware. sensor defined in c. *. so extension search in sequence. When the first match is found, it is stopped and the sensor. A global variable HAL_MODULE_INFO_SYM defined in so is returned. This variable contains
An important information is a function pointer open contained in a member structure variable. The pointer refers to a function that assigns a value to a device structure variable to bring out the sensorService. cpp and sensorManager. all information required for communication between cpp and sensor.
The device structure variables are available in two variants: sensorService. cpp and sensorManaer. cpp. Some function pointers mainly point to the function that communicates with the sensor.
SensorService. cpp and sensorManager. cpp call the inline function open () of sensors. h after obtaining the HAL_MODULE_INFO_SYM structure and retrieve the required device information through the open function pointer of HAL_MODULE_INFO_SYM.

When activityManager. java is started, the system starts sensorManager. java. It also calls the method hw_get_module () in hardware. c to bring HAL_MODULE_INFO_SYM back.

3. About the implementation of Rotate:

When the system starts windowManger. java, it starts phoneWindowManager. java, which has an internal class myOrientationListener extended from javasworientationlistener. java.
Javasworientationlistener. java is a helper class that is called by windowManger. java to receive data when the device direction changes.
Invalid worientationlistener. java is embedded in sensorManger. java registers and listens back to the data transmitted by G-sensor, that is, the acceleration in the direction of x, y, and z. After receiving the data, it is converted and processed, if the Roate condition is met, the implementation class windowManagerService of the IwindowManager interface is called. the setRotation () method in java implements screen conversion.

SensorManager obtains Sensor data from the device through polling. The structure of Sensor data is defined in sensor. h,
The SensorManager processes only the vector. v, vector. status, and time fields and distributes them to registered listeners.

For example, the first vector. v contains information values in the x, y, and z directions, which are registered by mongoworientataionlister,
After SensorManager obtains these three values, it passes them to worker worientataionlister. The latter code is located:
Frameworkd/base/core/java/android/view/javasworientationlistener. java
WindowOrientataionLister calculates the orientation corresponding to the device after receiving these three values, and executes the onOrientationChanged function for further uploading.

Javasworientataionlister is a pure virtual class. If you need to control the direction in the APK, You can reload an instance,
The Android system instance is in PhoneWindowManager. java and is named MyOrientationListener.
Frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager. java

To rotate, MyOrientationListener will call the following code to rotate the window:
MWindowManager. setRotation (rotation, false, mFancyRotationAnimation );

Summary:
1. Some problems encountered when port the lis302 G-sensor driver from the spi bus to the lis331 i2c bus:
A). The interrupt pin used by lis331 is different from lis302. The hardware schematic shows that lis331 uses GPN3. therefore, you need to set writel (readl (s364xx_gpncon) in the driver's probe )&~ (0xc0) | (0x80), 89c64xx_gpncon );
B) The hardware schematic shows that the clock line and data line of lis331 use i2c chanel1. Therefore, the i2c chanel1 is the structure variable i2c_devs1 [] _ initdata in the/kernel/arch/arm/mach-s3c6410/mach-ur6410.c file.
Add the device information of G-sensor so that the driver is loaded successfully.
C). The lis331 driver is an interrupt driver. Each time G-sensor collects new data, it will interrupt the driver. The driver must use i2cbus to retrieve the data from G-sensor. The i2cbus read/write operations may sleep, but the function that may sleep cannot be called during the interruption. Therefore, the work_queue mechanism provided by linux is used to solve the problem.

Problem B) principle:
I2c drivers include bus drivers and device drivers. Bus drivers only provide read/write mechanisms for a specific bus and do not communicate with each other. Using the functions provided by the i2c bus driver, device drivers can ignore the differences between different bus controllers and communicate with hardware devices without considering their details.
A bus driver usually requires two modules: struct i2c_adapter and struct i2c_algorithm defined in include/linux/i2c. h
Struct i2c_algorithm is designed for i2c bus drivers to communicate with specific i2c bus. Many i2c bus drivers define and use their own algorithm. For some i2c bus drivers, many algorithm have been written.
Drivers/i2c/buses contains all i2c bus drivers, and drivers/i2c/algos contains all algorithm.

The device driver communicates with the i2c device through the read/write functions in the bus driver. A device driver is described in two modules: struct i2c_driver and struct i2c_client.
I2c_client represents a device that is located on the adapter bus and is driven by a driver. It binds bus drivers, device drivers, and device addresses together.

2. problems encountered when implementing ioctl between sensor. so and driver:

Related Article

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.