Previously we managed to measure the inclination angle and calculated pitch and roll of xtrinsic mma8491q .
In the guide we will be measuring the 45 degree tilt detection using the dedicated pin-outs in xtrinsic.
Before moving on we will be automating the accelerometer EN pin GPIO setup process, so that we don't have to manually setup the gpio everytime we restart Riotboard.
below code will do that.
system("devmem 0x20E009C w 0x5 > /dev/null "); // setup EN pin high.
system("echo 112 > /sys/class/gpio/export");
system("echo out > /sys/class/gpio/gpio112/direction");
system("echo 1 > /sys/class/gpio/gpio112/value");
Tilt detection procedure
The MMA8491Q offers a 45 degree tilt detection per axis.
There are dedicated pins for each axis, which can be easily implemented in driver to detect tilts.
from xtrinsic datasheet pg-7, pins 2,3 & 4 in CN1 are used to detect 45 degree tilts on Z-axis, Y-Axis and X-axis.
locating the pins on xtrinsic board ...
Setting up Hardware ...
we will be using 8 pins of Xtrinsic board.
Pin Mapping for Xtrinsic Accelerometer.
8 Male to Female jumper wires are used to connect Riotboard Expansion port pins to Xtrinsic CN1 & CN2 .
Connecting female headers in Xtrinsic CN2 Connector.
Setup after connections ...
Pins 2,3,4 in Xtrinsic are connected to Pins 7,9,11 in J13 port on Riotboard.
Pin7 = GPIO4_17 = (4-1)*32+17 = 113
Pin9 = GPIO4_18 = (4-1)*32+18 = 114
Pin11 = GPIO4_19 = (4-1)*32+19 = 115
determine PIN location from i.MX processor reference manual : page 2004
GPIO4_17 = DI0_PIN15 = 0x20E00A0
GPIO4_18 = DI0_PIN2 = 0x20E00A4
GPIO4_19 = DI0_PIN3 = 0x20E00A8
Configure GPIO settings for the 3 tilt pins connected:
system("devmem 0x20E00A0 w 0x5 ");
system("echo 113 > /sys/class/gpio/export");
system("echo in > /sys/class/gpio/gpio113/direction");
system("devmem 0x20E00A4 w 0x5 ");
system("echo 114 > /sys/class/gpio/export");
system("echo in > /sys/class/gpio/gpio114/direction");
system("devmem 0x20E00A8 w 0x5 ");
system("echo 115 > /sys/class/gpio/export");
system("echo in > /sys/class/gpio/gpio115/direction");
After exporting pins, a file named value will be available in gpio directory under sysfs filesystem.
In linux the exact location is /sys/class/gpio/gpioNUM/value
below code helps to determine if X-Axis is tilted more than 45 degree, If value contains 1, it means Tilted:
fd_x = open("/sys/class/gpio/gpio115/value",O_RDONLY);
- - - -
- - - -
ret = read(fd_x,buf,sizeof(1));
if(ret>0 && (atoi(buf)==1))
printf("X is tilted\n");
if(lseek(fd_x,0,SEEK_SET) < 0) return 1; //return back read cursor to beginning of file
Lets Tilt and test:
Tilt X-axis :
Tilt Y-axis :
Tilt Z-axis :
The completed code is in attachments.
So we completed Xtrinsic Accelerometer integration on Riotboard.