Skip to content

Instantly share code, notes, and snippets.

@pdp7
Last active September 8, 2019 06:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pdp7/cd47cdc2a6747c8b01a5 to your computer and use it in GitHub Desktop.
Save pdp7/cd47cdc2a6747c8b01a5 to your computer and use it in GitHub Desktop.
BeagleBone Green: ADXL345 Grove Acceleromter sensor

Overview

Device Tree Overlay

debian@beaglebone:~$ cat dtc/bb.org-overlays/src/arm/BB-BONE-ADXL345-00A0.dts
/*
 * For ADI ADXL345 Accelerometer:
 * 
 * This Device Tree Overlay is based on:
 * BB-BONE-CRYPTO-00A0.dts
 * Cape support for the CryptoCape
 * Copyright (C) 2014 Cryptotronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
/dts-v1/;
/plugin/;

#include <dt-bindings/board/am335x-bbw-bbb-base.h>
#include <dt-bindings/pinctrl/am33xx.h>

/ {
	compatible = "ti,beaglebone", "ti,beaglebone-black", "ti,beaglebone-green";

	/* identification */
	part-number = "BB-BONE-ADXL345";
	version = "00A0";

	fragment@0 {
		target = <&i2c2>;
		__overlay__ {
			#address-cells = <1>;
			#size-cells = <0>;

			adxl345@0 {
				compatible = "adi,adxl345";
				reg = <0x53>;
				interrupt-parent = <&gpio3>;
				interrupts = <19 0x0>;
			};
		};
	};
};

Load Driver

root@beaglebone:~# i2cdetect -r -y 2
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- 53 UU UU UU UU -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

root@beaglebone:~# echo '7 7 7 7' > /proc/sys/kernel/printk

root@beaglebone:~# modprobe adxl34x
root@beaglebone:~# modprobe adxl34x-i2c

root@beaglebone:~# lsmod
Module                  Size  Used by
adxl34x_i2c             3532  0 
adxl34x                10321  1 adxl34x_i2c

root@beaglebone:~# echo BB-BONE-ADXL345 > /sys/devices/platform/bone_capemgr/slots

root@beaglebone:~# dmesg
[  119.223587] bone_capemgr bone_capemgr: part_number 'BB-BONE-ADXL345', version 'N/A'
[  119.223628] bone_capemgr bone_capemgr: slot #4: override
[  119.223644] bone_capemgr bone_capemgr: Using override eeprom data at slot 4
[  119.223661] bone_capemgr bone_capemgr: slot #4: 'Override Board Name,00A0,Override Manuf,BB-BONE-ADXL345'
[  119.239210] adxl34x_i2c_probe: client=dd9ba400
[  119.243878] adxl34x_i2c_probe: id=  (null)
[  119.248239] adxl34x_i2c_probe: error=1
[  119.250619] adxl34x_probe: irq=145
[  119.259811] adxl34x_probe: pdata=  (null)
[  119.263191] adxl34x_probe: revid=229
[  119.267860] adxl34x_probe: sysfs_create_group returned err=0
[  119.279548] input: ADXL34x accelerometer as /devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2
[  119.282231] adxl34x_probe: input_register_device returned err=0
[  119.303226] adxl34x_probe: return ac=-604340736
[  119.308160] adxl34x_i2c_probe: ac=dbfa7e00
[  119.316952] bone_capemgr bone_capemgr: slot #4: dtbo 'BB-BONE-ADXL345-00A0.dtbo' loaded; overlay id #0

root@beaglebone:~#  cat /proc/interrupts 
           CPU0       
 16:      35886      INTC  68 Level     gp_timer
 19:          1      INTC  78 Level     wkup_m3_txev
 20:       8448      INTC  12 Level     49000000.edma_ccint
 22:         91      INTC  14 Level     49000000.edma_ccerrint
 26:          0      INTC  96 Level     44e07000.gpio
 33:          0  44e07000.gpio   6 Edge      48060000.mmc cd
 59:          0      INTC  98 Level     4804c000.gpio
 92:          0      INTC  32 Level     481ac000.gpio
125:          0      INTC  62 Level     481ae000.gpio
145:          0  481ae000.gpio  19 Level     2-0053
158:        101      INTC  72 Level     OMAP UART0
159:       1035      INTC  70 Level     44e0b000.i2c
160:         80      INTC  30 Level     4819c000.i2c
161:      29050      INTC  64 Level     mmc0
163:          0      INTC  77 Level     wkup_m3
169:          0      INTC  75 Level     rtc0
170:          0      INTC  76 Level     rtc0
174:       4045      INTC  41 Level     4a100000.ethernet
175:       4123      INTC  42 Level     4a100000.ethernet
179:          0      INTC  80 Level     48038000.mcasp_tx
180:          0      INTC  81 Level     48038000.mcasp_rx
181:          0      INTC  82 Level     4803c000.mcasp_tx
182:          0      INTC  83 Level     4803c000.mcasp_rx
183:          0      INTC 111 Level     48310000.rng
184:          0      INTC  18 Level     musb-hdrc.0.auto
185:          1      INTC  19 Level     musb-hdrc.1.auto
186:          0      INTC  17 Level     47400000.dma-controller
187:          0      INTC   7 Level     tps65217
IPI0:          0  CPU wakeup interrupts
IPI1:          0  Timer broadcast interrupts
IPI2:          0  Rescheduling interrupts
IPI3:          0  Function call interrupts
IPI4:          0  CPU stop interrupts
IPI5:          0  IRQ work interrupts
IPI6:          0  completion interrupts
Err:          0


root@beaglebone:~# find /sys -type f |grep 2-0053 | grep -v /power/ |xargs more 
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/name
::::::::::::::
adxl345
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/rate
::::::::::::::
8
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/disable
::::::::::::::
0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/id/version
::::::::::::::
0000
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/id/vendor
::::::::::::::
0000
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/id/product
::::::::::::::
0159
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/id/bustype
::::::::::::::
0018
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/name
::::::::::::::
ADXL34x accelerometer
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/phys
::::::::::::::
2-0053/input0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/uniq
::::::::::::::

::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/modalias
::::::::::::::
input:b0018v0000p0159e0000-e0,1,3,k14A,ra0,1,2,mlsfw
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/event2/dev
::::::::::::::
13:66
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/event2/uevent
::::::::::::::
MAJOR=13
MINOR=66
DEVNAME=input/event2
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/properties
::::::::::::::
0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/mouse0/dev
::::::::::::::
13:32
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/mouse0/uevent
::::::::::::::
MAJOR=13
MINOR=32
DEVNAME=input/mouse0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/uevent
::::::::::::::
PRODUCT=18/0/159/0
NAME="ADXL34x accelerometer"
PHYS="2-0053/input0"
PROP=0
EV=b
KEY=400 0 0 0 0 0 0 0 0 0 0
ABS=7
MODALIAS=input:b0018v0000p0159e0000-e0,1,3,k14A,ra0,1,2,mlsfw
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/capabilities/ff
::::::::::::::
0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/capabilities/ev
::::::::::::::
b
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/capabilities/sw
::::::::::::::
0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/capabilities/abs
::::::::::::::
7
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/capabilities/key
::::::::::::::
400 0 0 0 0 0 0 0 0 0 0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/capabilities/led
::::::::::::::
0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/capabilities/msc
::::::::::::::
0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/capabilities/rel
::::::::::::::
0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/input/input2/capabilities/snd
::::::::::::::
0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/modalias
::::::::::::::
i2c:adxl345
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/position
::::::::::::::
(0, 0, 0)
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/calibrate
::::::::::::::
0,0,0
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/uevent
::::::::::::::
DRIVER=adxl34x
OF_NAME=adxl345
OF_FULLNAME=/ocp/i2c@4819c000/adxl345@0
OF_COMPATIBLE_0=adi,adxl345
OF_COMPATIBLE_N=1
MODALIAS=i2c:adxl345
::::::::::::::
/sys/devices/platform/ocp/4819c000.i2c/i2c-2/2-0053/autosleep
::::::::::::::
1
root@beaglebone:~#      

root@beaglebone:~# evtest 
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0:	Speakup
/dev/input/event1:	tps65217_pwr_but
/dev/input/event2:	ADXL34x accelerometer
Select the device event number [0-2]: 2
Input driver version is 1.0.1
Input device ID: bus 0x18 vendor 0x0 product 0x159 version 0x0
Input device name: "ADXL34x accelerometer"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 330 (BTN_TOUCH)
  Event type 3 (EV_ABS)
    Event code 0 (ABS_X)
      Value      0
      Min    -4096
      Max     4096
      Fuzz       3
      Flat       3
    Event code 1 (ABS_Y)
      Value      0
      Min    -4096
      Max     4096
      Fuzz       3
      Flat       3
    Event code 2 (ABS_Z)
      Value      0
      Min    -4096
      Max     4096
      Fuzz       3
      Flat       3
Properties:
Testing ... (interrupt to exit)


@pdp7
Copy link
Author

pdp7 commented Feb 28, 2016

http://derekmolloy.ie/kernel-gpio-programming-buttons-and-leds/
This handler function is then registered with an interrupt request (IRQ) using the request_irq() function as follows:

result = request_irq(irqNumber,               // The interrupt number requested
         (irq_handler_t) ebbgpio_irq_handler, // The pointer to the handler function (above)
         IRQF_TRIGGER_RISING,                 // Interrupt is on rising edge (button press in Fig.1)
         "ebb_gpio_handler",                  // Used in /proc/interrupts to identify the owner
         NULL);                               // The *dev_id for shared interrupt lines, NULL here

The irqNumber is determined automatically in the code example in Listing 2 by using the interrupt number that is associated with the respective GPIO number — importantly, the GPIO number is not the interrupt number, however, there is a direct one-to-one mapping.

@pdp7
Copy link
Author

pdp7 commented Feb 28, 2016

@pdp7
Copy link
Author

pdp7 commented Feb 28, 2016

Tried interrupts = <140 0x0>

[  167.097401] bone_capemgr bone_capemgr: part_number 'BB-BONE-ADXL345', version 'N/A'
[  167.097444] bone_capemgr bone_capemgr: slot #4: override
[  167.097460] bone_capemgr bone_capemgr: Using override eeprom data at slot 4
[  167.097477] bone_capemgr bone_capemgr: slot #4: 'Override Board Name,00A0,Override Manuf,BB-BONE-ADXL345'
[  167.113453] ------------[ cut here ]------------
[  167.113507] WARNING: CPU: 0 PID: 741 at kernel/irq/irqdomain.c:346 irq_domain_associate+0x148/0x1d4()
[  167.113517] error: hwirq 0x8c is too large for 481ae000.gpio
[  167.113525] Modules linked in: adxl34x_i2c adxl34x omap_sham omap_aes omap_rng rng_core 8021q snd_soc_davinci_mcasp garp snd_soc_edma mrp snd_soc_omap stp llc snd_soc_core snd_pcm_dmaengine snd_pcm snd_timer snd soundcore spi_omap2_mcspi evdev
[  167.113607] CPU: 0 PID: 741 Comm: bash Not tainted 4.5.0-rc4-armv7-devel-r47 #5
[  167.113614] Hardware name: Generic AM33XX (Flattened Device Tree)
[  167.113655] [<c001bf2c>] (unwind_backtrace) from [<c0015c28>] (show_stack+0x20/0x24)
[  167.113675] [<c0015c28>] (show_stack) from [<c058e278>] (dump_stack+0x8c/0xa0)
[  167.113695] [<c058e278>] (dump_stack) from [<c004df1c>] (warn_slowpath_common+0x94/0xc4)
[  167.113710] [<c004df1c>] (warn_slowpath_common) from [<c004df9c>] (warn_slowpath_fmt+0x50/0x6c)
[  167.113725] [<c004df9c>] (warn_slowpath_fmt) from [<c00b6b58>] (irq_domain_associate+0x148/0x1d4)
[  167.113741] [<c00b6b58>] (irq_domain_associate) from [<c00b7208>] (irq_create_mapping+0xcc/0x1e0)
[  167.113756] [<c00b7208>] (irq_create_mapping) from [<c00b7bb4>] (irq_create_fwspec_mapping+0xbc/0x200)
[  167.113770] [<c00b7bb4>] (irq_create_fwspec_mapping) from [<c00b7d6c>] (irq_create_of_mapping+0x74/0x90)
[  167.113791] [<c00b7d6c>] (irq_create_of_mapping) from [<c097d0c4>] (of_irq_get+0x5c/0x80)
[  167.113811] [<c097d0c4>] (of_irq_get) from [<c089e664>] (i2c_device_probe+0x1f0/0x230)
[  167.113836] [<c089e664>] (i2c_device_probe) from [<c073d8c0>] (driver_probe_device+0x254/0x444)
[  167.113853] [<c073d8c0>] (driver_probe_device) from [<c073dbe4>] (__device_attach_driver+0x94/0xbc)
[  167.113868] [<c073dbe4>] (__device_attach_driver) from [<c073b28c>] (bus_for_each_drv+0x84/0xc8)
[  167.113883] [<c073b28c>] (bus_for_each_drv) from [<c073d4b8>] (__device_attach+0xe0/0x174)
[  167.113898] [<c073d4b8>] (__device_attach) from [<c073dc68>] (device_initial_probe+0x1c/0x20)
[  167.113912] [<c073dc68>] (device_initial_probe) from [<c073c61c>] (bus_probe_device+0x94/0x9c)
[  167.113926] [<c073c61c>] (bus_probe_device) from [<c073a128>] (device_add+0x39c/0x5e4)
[  167.113941] [<c073a128>] (device_add) from [<c073a394>] (device_register+0x24/0x28)
[  167.113958] [<c073a394>] (device_register) from [<c08a0f6c>] (i2c_new_device+0x15c/0x1f0)
[  167.113973] [<c08a0f6c>] (i2c_new_device) from [<c08a150c>] (of_i2c_register_device+0x130/0x1f4)
[  167.113987] [<c08a150c>] (of_i2c_register_device) from [<c08a1680>] (of_i2c_notify+0xb0/0x104)
[  167.114001] [<c08a1680>] (of_i2c_notify) from [<c006ffd4>] (notifier_call_chain+0x54/0x94)
[  167.114016] [<c006ffd4>] (notifier_call_chain) from [<c0070430>] (__blocking_notifier_call_chain+0x58/0x70)
[  167.114030] [<c0070430>] (__blocking_notifier_call_chain) from [<c0070470>] (blocking_notifier_call_chain+0x28/0x30)
[  167.114044] [<c0070470>] (blocking_notifier_call_chain) from [<c0978980>] (__of_changeset_entry_notify+0xb4/0x108)
[  167.114059] [<c0978980>] (__of_changeset_entry_notify) from [<c0979334>] (__of_changeset_apply+0x98/0x170)
[  167.114076] [<c0979334>] (__of_changeset_apply) from [<c097ffcc>] (__of_overlay_create+0x444/0x62c)
[  167.114092] [<c097ffcc>] (__of_overlay_create) from [<c09801d4>] (of_overlay_create+0x20/0x24)
[  167.114110] [<c09801d4>] (of_overlay_create) from [<c0767c10>] (capemgr_load_slot+0x3f4/0x5ec)
[  167.114126] [<c0767c10>] (capemgr_load_slot) from [<c07697d4>] (slots_store+0x144/0x318)
[  167.114139] [<c07697d4>] (slots_store) from [<c0737ba8>] (dev_attr_store+0x28/0x34)
[  167.114164] [<c0737ba8>] (dev_attr_store) from [<c024da14>] (sysfs_kf_write+0x50/0x54)
[  167.114181] [<c024da14>] (sysfs_kf_write) from [<c024d17c>] (kernfs_fop_write+0xd0/0x1dc)
[  167.114204] [<c024d17c>] (kernfs_fop_write) from [<c01cf3b0>] (__vfs_write+0x48/0x104)
[  167.114220] [<c01cf3b0>] (__vfs_write) from [<c01d0318>] (vfs_write+0xa0/0x1ac)
[  167.114234] [<c01d0318>] (vfs_write) from [<c01d0ff0>] (SyS_write+0x64/0xcc)
[  167.114257] [<c01d0ff0>] (SyS_write) from [<c0011060>] (ret_fast_syscall+0x0/0x3c)
[  167.114266] ---[ end trace c4a14a455bb18cfe ]---
[  167.114299] adxl34x_i2c_probe: client=dbf93200
[  167.118975] adxl34x_i2c_probe: id=  (null)
[  167.123313] adxl34x_i2c_probe: error=1
[  167.125716] adxl34x_probe: irq=0
[  167.129132] adxl34x 2-0053: no IRQ?
[  167.132788] adxl34x_probe: err_out
[  167.136195] adxl34x_i2c_probe: ac=ffffffed
[  167.158481] bone_capemgr bone_capemgr: slot #4: dtbo 'BB-BONE-ADXL345-00A0.dtbo' loaded; overlay id #0

@pdp7
Copy link
Author

pdp7 commented Mar 2, 2016

I'm running Linux 4.5-rc4 built with Robert's linux-dev scripts:
https://github.com/RobertCNelson/linux-dev

@pdp7
Copy link
Author

pdp7 commented Mar 2, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment