world leader in high performance signal processing
Trace: » acceleration_sensor

ADXL345/6 Android Acceleration Sensor

Using the ADXL345/6 under Android as Acceleration Sensor

The ADXL345/6 can be easily used as Android Acceleration Sensor. A few steps are necessary to add appropriate sensor support.

Add Linux Driver Support:

Starting with linux-2.6.36 the ADXL34x driver is mainlined. If you are using an pre linux-2.6.36 kernel get the source from our repositories and add them to your kernel tree.

Example platform data:

#include <linux/input/adxl34x.h>
static const struct adxl34x_platform_data adxl34x_info = {
	.x_axis_offset = 0,
	.y_axis_offset = 0,
	.z_axis_offset = 0,
	.tap_threshold = 0x31,
	.tap_duration = 0x10,
	.tap_latency = 0x60,
	.tap_window = 0xF0,
	.tap_axis_control = 0,
	.act_axis_control = 0xFF,
	.activity_threshold = 5,
	.inactivity_threshold = 3,
	.inactivity_time = 4,
	.free_fall_threshold = 0x7,
	.free_fall_time = 0x20,
	.data_rate = 0x8,
	.data_range = ADXL_FULL_RES,
 
	.ev_type = EV_ABS,
	.ev_code_x = ABS_X,		/* EV_REL */
	.ev_code_y = ABS_Y,		/* EV_REL */
	.ev_code_z = ABS_Z,		/* EV_REL */
 
	.ev_code_tap = {BTN_0, BTN_1, BTN_2}, /* EV_KEY x,y,z */
 
/*	.ev_code_ff = KEY_F,*/		/* EV_KEY */
/*	.ev_code_act_inactivity = KEY_A,*/	/* EV_KEY */
	.power_mode = ADXL_AUTO_SLEEP | ADXL_LINK,
	.fifo_mode = ADXL_FIFO_STREAM,
	.orientation_enable = 0, /* Disable Orientation Reports */
	.deadzone_angle = ADXL_DEADZONE_ANGLE_10p8,
	.divisor_length =  ADXL_LP_FILTER_DIVISOR_16,
	/* EV_KEY {+Z, +Y, +X, -X, -Y, -Z} */
	.ev_codes_orient_3d = {BTN_Z, BTN_Y, BTN_X, BTN_A, BTN_B, BTN_C},
};

Create an Android sensor library

The readme file in libhardware states: The source code for the “board” variant, usually lives under partners/… The source code for “default” and “arch” would usually live under hardware/modules/.

The sensor file is typically source code for the “board” variant. However for simplicity this example puts it into a common folder structure under libhardware/modules/.

  • Create an sensors folder under libhardware/modules
  • Copy files sensors_adxl34x.c and Android.mk to libhardware/modules/sensors

The files can be downloaded here:

Android git


  • Make sure the makefile in the higher folder hierarchy picks up the one in libhardware/modules/sensors.
  • Add following lines to: libhardware/Android.mk
include $(addsuffix /Android.mk, $(addprefix $(LOCAL_PATH)/, \
			modules/sensors \
		))

In your platforms init.rc file make sure all ADXL34x drivers sysfs files {disable, rate} are owned by system:system

  • Add following lines to your init.rc script:
    chown system system /sys/class/input/event0/device/device/disable
    chown system system /sys/class/input/event0/device/device/rate

In case the ADXL345/6 is not always event0 you can alternatively approach via /sys/bus/i2c/devices/…

  • Rebuilt your Android tree

Upon start the Android log file (use logcat) should include following lines:

I/SystemServer( 1862): Sensor Service
[--snip--]
D/SensorManager( 2064): found sensor: Analog Devices ADXL345/6 3-axis Accelerometer, handle=0

Hints

Sample Rate

The ADXL345/6 sample rate is configurable. The maximum sample rate is by default limited to 200Hz, to change this limit modify define ADXL_MAX_SAMPLE_RATE_VAL in the sensor file.

Very high sample rates > 400Hz can freeze your system!

file: hardware/sensors/sensors_adxl34x.c

scm failed with exit code 1:
file does not exist in git

Platform data configuration

Don't use BTN_TOUCH

In case you experience strange input device gui interaction it might be the case that Android EventHub included the ADXL34x as input device. This is typically the case if you put an BTN_TOUCH key into your ADXL34x drivers platform data.

Upon start EventHub must emit classes=0x0:

I/EventHub( 1862): New device: path=/dev/input/event0 name=ADXL34x accelerometer id=0x10003 (of 0x4) index=4 fd=50 classes=0x0

classes=0x4 means that the ADXL34x driver is considered as Touchscreen.

Auto Sleep

The default platform configuration sets the driver automatically switch to sleep mode during periods of inactivity. To disable this feature remove ADXL_AUTO_SLEEP from power_mode.

Alternatively during runtime:

#echo 0 > /sys/class/input/event0/device/device/autosleep

For more information visit the ADXL34x driver page here: adxl34x

The Sources

file: hardware/sensors/Android.mk

scm failed with exit code 1:
file does not exist in git

file: hardware/sensors/sensors_adxl34x.c

scm failed with exit code 1:
file does not exist in git