aboutsummaryrefslogtreecommitdiff
path: root/recipes-demo/de10-nano-adxl-apps/files/README_SANDBOX.txt
blob: 6ac13df993fed88fc546a8395d06d3c8275e2ee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
This readme describes how the Analog Devices ADXL345 Input 3-Axis Digital
Accelerometer Linux Driver is deployed on the DE10 Nano target environment. You
may find the following references useful for more information on this topic as
well.

<linux-source-tree>/drivers/input/misc/adxl34x.c
<linux-source-tree>/drivers/input/misc/adxl34x.h
<linux-source-tree>/drivers/input/misc/adxl34x-i2c.c
<linux-source-tree>/drivers/input/misc/adxl34x-spi.c
<linux-source-tree>/include/linux/input/adxl34x.h

http://wiki.analog.com/resources/tools-software/linux-drivers/input-misc/adxl345

If you cut and paste the following function into a console running on the DE10
Nano target you can extract the useful information contained in the run time
device tree maintained by the kernel in the procfs.
################################################################################
# find adxl34x in device tree
################################################################################
function find_adxl_dt () {
for NEXT in $(find -L /proc/device-tree -name "compatible" | sort)
do
cat ${NEXT} | grep "adi,adxl345" > /dev/null && {
ADXL_DIRNAME="$(dirname ${NEXT})"
echo ${ADXL_DIRNAME}
echo -e "\tcompatible = '$(cat ${ADXL_DIRNAME}/compatible)'"
echo -e "\t      name = '$(cat ${ADXL_DIRNAME}/name)'"
REG_HEX="$(hexdump -v -e '"0x"' -e '4/1 "%02x"' "${ADXL_DIRNAME}/reg")"
echo -e "\t       reg = '${REG_HEX}'"
}
done
}
################################################################################

When we run the function above on the DE10 Nano target it searches for nodes
containing the 'compatible' string containing 'adi,adxl345' which is the
identifier of the ADI accelerometer in our system.  The function then prints the
path to the node that it found and extracts the compatible string and 'name' and
'reg' properties and prints those statistics out as well.

root@de10-nano:~# find_adxl_dt
/proc/device-tree/soc/i2c@ffc04000/adxl345@0
        compatible = 'adi,adxl345'
              name = 'adxl345'
               reg = '0x00000053'
root@de10-nano:~#

We can tell from the above output that this accelerometer is attached to the
I2C controller at 0xFFC04000, and the I2C address of the accelerometer is 0x53.

There are a number of ways that we can now find the sysfs resources that refer
to this hardware:

root@de10-nano:~# ls /sys/bus/i2c
devices            drivers_autoprobe  uevent
drivers            drivers_probe
root@de10-nano:~# ls /sys/bus/i2c/devices/
0-0053  i2c-0
root@de10-nano:~# ls /sys/bus/i2c/drivers
adxl34x     dummy       ltc2978     pmbus
at24        lcd-comm    max1619     rtc-ds1307
root@de10-nano:~# ls /sys/bus/i2c/drivers/adxl34x/
0-0053  bind    uevent  unbind
root@de10-nano:~#

In the '/sys/bus/i2c/' directory there is a 'devices' and 'drivers' directory.
The 'devices' directory contains a link called '0-0053' which represents I2C bus
0 device address 0x53.  The 'drivers' directory contains a 'adxl34x' directory
which in turn contains a link called '0-0053' as well.  Both of these links
point into the actual device entry:

root@de10-nano:~# ls -l /sys/bus/i2c/devices/0-0053
lrwxrwxrwx    1 root     root             0 Jan  1  1970 /sys/bus/i2c/devices/0-0053 -> ../../../devices/platform/soc/ffc04000.i2c/i2c-0/0-0053
root@de10-nano:~# ls -l /sys/bus/i2c/drivers/adxl34x/0-0053
lrwxrwxrwx    1 root     root             0 Jul 11 15:21 /sys/bus/i2c/drivers/adxl34x/0-0053 -> ../../../../devices/platform/soc/ffc04000.i2c/i2c-0/0-0053
root@de10-nano:~#

So if we look at the contents of the actual device directory we see the expected
sysfs files provided by the adxl34x driver:

root@de10-nano:~# ls /sys/devices/platform/soc/ffc04000.i2c/i2c-0/0-0053
autosleep  disable    input      name       power      subsystem
calibrate  driver     modalias   position   rate       uevent
root@de10-nano:~#

Some of these sysfs files are provided by the adxl34x driver to provide access
from userspace into the configuration and settings of the driver.  These files
are 'autosleep', 'disable', 'calibrate', 'position', and 'rate'.  Please see
the adxl34x driver sources and documentation on how these files are used.

The 'input' sysfs file provided in the device directory will tell us which input
event device the adxl345 has been register as.  If we follow this path we see:

root@de10-nano:~# ls /sys/devices/platform/soc/ffc04000.i2c/i2c-0/0-0053/input/
input0

And following into 'input0' we see:

root@de10-nano:~# ls /sys/devices/platform/soc/ffc04000.i2c/i2c-0/0-0053/input/input0/
capabilities  id            name          properties    uniq
device        modalias      phys          subsystem
event0        mouse0        power         uevent
root@de10-nano:~# cat /sys/devices/platform/soc/ffc04000.i2c/i2c-0/0-0053/input/input0/name
ADXL34x accelerometer

Now if we go into the 'event0' we can see the 'dev' entry that contains the
device node details:

root@de10-nano:~# ls /sys/devices/platform/soc/ffc04000.i2c/i2c-0/0-0053/input/input0/event0/
dev        device     power      subsystem  uevent
root@de10-nano:~# hexdump -Cv /sys/devices/platform/soc/ffc04000.i2c/i2c-0/0-0053/input/input0/event0/dev
00000000  31 33 3a 36 34 0a                                 |13:64.|
00000006

And if we then list '/dev/input/' looking for device major 13 and  minor 64, we
can see that indeed '/dev/input/event0' is our accelerometer input device.

root@de10-nano:~# ls /dev/input/ -l
total 0
drwxr-xr-x    2 root     root            80 Jul  9 16:25 by-path
crw-rw----    1 root     input      13,  64 Jan  1  1970 event0
crw-rw----    1 root     input      13,  65 Jul  9 16:25 event1
crw-rw----    1 root     input      13,  63 Jul  9 16:25 mice
crw-rw----    1 root     input      13,  32 Jan  1  1970 mouse0
root@de10-nano:~#

There is actually a very convenient way to discover the input device by using
the 'by-path' directory like this:

root@de10-nano:~# ls /dev/input/by-path/
platform-ffc04000.i2c-event  platform-soc:keys-event
root@de10-nano:~# ls -l /dev/input/by-path/platform-ffc04000.i2c-event
lrwxrwxrwx    1 root     root             9 Jan  1  1970 /dev/input/by-path/platform-ffc04000.i2c-event -> ../event0
root@de10-nano:~#

--------------------------------------------------------------------------------
Example programs and scripts
--------------------------------------------------------------------------------
This directory contains a few examples to demonstrate how to interact with the
accelerometer on the DE10 Nano board.  There is a shell script called
'adxl_calibrate.sh' which shows how to calibrate the accelerometer.  There is a
C program called 'watch_adxl.c' that shows how to interact with the sysfs files
and the input event node provided by the adxl34x driver.  And there is a C
program called 'tap_detect.c' which demonstrates how to detect the tap events
that the accelerometer produces.

To build the 'watch_adxl.c' application simply run the 'build_watch_adxl.sh'
shell script.  That will compile the 'watch_adxl.c' source file and produce the
executable 'watch_adxl' application.  Refer to the 'build_watch_adxl.sh' script
to see how the application is actually compiled and refer to the C program
source file for more details on how it actually works.

To build the 'tap_detect.c' application simply run the 'build_tap_detect.sh'
shell script.  That will compile the 'tap_detect.c' source file and produce the
executable 'tap_detect' application.  Refer to the 'build_tap_detect.sh' script
to see how the application is actually compiled and refer to the C program
source file for more details on how it actually works.

Refer to the 'adxl_calibrate.sh' source file for more details on how it
actually works.

Once you've built the applications, you can run both the script and the
applications like this:

./adxl_calibrate.sh		<<< to run the script
./watch_adxl			<<< to run the program
./tap_detect			<<< to run the program