Re: Using SPI/PWM/I2C/GPIO with Android on BBB

5,713 views
Skip to first unread message

pe...@icecavern.com

unread,
Jun 20, 2013, 4:53:07 AM6/20/13
to beagl...@googlegroups.com
I'd also like to know this. I'm looking to write my own app to run on the BBB to control an I2C PWM driver device and read temperatures over DOW, and GPIO to build an Aquarium controller. Sounds like our projects are quite similar :)

Pete

On Monday, June 10, 2013 4:54:32 PM UTC+1, Ryan Ramchandar wrote:
Hi all, 

I am planning to run Android on a BBB when the image is released (to the best of my knowledge this is not the case yet). Since I am familiar with Android development this is the approach I would like to take. I am planning to replace the launcher application with my own so that Android will boot in to it automatically. I would like to read various sensors (temp, CO2, air quality etc.), set LEDs and communicate with another ARM chip via SPI or I2C from my app.

My question is, from an Android application how can I access the GPIO, PWM, SPI and I2C interfaces on the BBB? 

Thanks,
Ryan R.

Ryan Ramchandar

unread,
Jun 21, 2013, 10:46:39 AM6/21/13
to beagl...@googlegroups.com
Thanks bmfc187 for your in depth response. It was very helpful. Since I am a bit new to this, I know Android apps are written in JAVA and the code you posted is for C. Does that mean I need to use the Android NDK to access the SPI bus?


On Thu, Jun 20, 2013 at 9:37 AM, bmfc187 <bmf...@gmail.com> wrote:
Well, all the kernel drivers are there...have you downloaded the source?  You can look in /kernel/drivers/ and find directories for SPI, GPIO, PWM, I2C, etc.  It would appear that...aha!  If you look in /kernel/Documentation/, i think you will find the information you are looking for.  Below is an excerpt from  /kernel/Documentation/i2c/dev-interface:


C example
=========

So let's say you want to access an i2c adapter from a C program. The
first thing to do is "#include <linux/i2c-dev.h>". Please note that
there are two files named "i2c-dev.h" out there, one is distributed
with the Linux kernel and is meant to be included from kernel
driver code, the other one is distributed with i2c-tools and is
meant to be included from user-space programs. You obviously want
the second one here.

Now, you have to decide which adapter you want to access. You should
inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
Adapter numbers are assigned somewhat dynamically, so you can not
assume much about them. They can even change from one boot to the next.

Next thing, open the device file, as follows:

  int file;
  int adapter_nr = 2; /* probably dynamically determined */
  char filename[20];
 
  snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
  file = open(filename, O_RDWR);
  if (file < 0) {
    /* ERROR HANDLING; you can check errno to see what went wrong */
    exit(1);
  }

When you have opened the device, you must specify with what device
address you want to communicate:

  int addr = 0x40; /* The I2C address */

  if (ioctl(file, I2C_SLAVE, addr) < 0) {
    /* ERROR HANDLING; you can check errno to see what went wrong */
    exit(1);
  }

Well, you are all set up now. You can now use SMBus commands or plain
I2C to communicate with your device. SMBus commands are preferred if
the device supports them. Both are illustrated below.

END EXCERPT

The sources I downloaded came from here.  (if you already have the sources you can ignore this, just putting it out there for reference) Just scroll down the table to the heading "TI Andorid JB 4.1.2 AM335x Sources" and select the first item, TI Android JB 4.1.2 DevKitV4.0.1 AM335x Sources. This link will download a TI-**********.bin to your computer.  SImply rename the file and remove the ".bin" suffix from the filename.  You can also change the name to something shorter if you don't want to type all that stuff in, I just changed it to "TI-Android".  You will have to set the file as executable either by doing

chmod a+x [filename]

in a terminal, or by right-clicking on the file in nautilus and going to properties>permissions> and check "allow executing fils as program".  Then open up a terminal, and cd to the directory you saved the file to, and type in the name of the file to execute it.  This will bring up a user agreement, you must scroll to the bottom by pressing enter to scroll to the next line, then type "I ACCEPT" at the end.  Careful, if you scroll too fast, you will be pressing enter when the "I ACCEPT" prompt comes up and the terminal will exit without doing anything.  After you type "I ACCEPT" and press enter, the downloader will start and you will find your source directory in the same directory you downloaded your file to. 

After you have the source navigate to /kernel/Documentation/ and you will see directories for ACPI, SPI, I2C, etc, (I dont see directories for PWM or GPIO but there is a gpio.txt and pwm.txt that seem to have the information for those interfaces, respectively).  For SPI the file you want appears to be /kernel/Documentation/spi/spi-summary.

hope this helps...





--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/gHR-1fvezjI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

bmfc187

unread,
Jun 21, 2013, 5:19:38 PM6/21/13
to beagl...@googlegroups.com
Not necessarily.  The code snippet i posted is an EXAMPLE of how you would access the functionality of the driver from within a C program.  These instructions are from the Linux kernel source documentation, and so they do not take into account that you are programming for Android.  This is why the example is given in C and not Java.  So since, presumably, you would be writing an android app to interface with the kernel driver, you would be writing the equivalent functionality in Java.  If you WANT to use C/C++, then you CAN use the NDK and write your code in C/C++. But you don't HAVE to. 

Andrew Henderson

unread,
Jun 21, 2013, 5:51:15 PM6/21/13
to beagl...@googlegroups.com
On Friday, June 21, 2013 10:46:39 AM UTC-4, Ryan Ramchandar wrote:
Thanks bmfc187 for your in depth response. It was very helpful. Since I am a bit new to this, I know Android apps are written in JAVA and the code you posted is for C. Does that mean I need to use the Android NDK to access the SPI bus?

I only have a minute or two for a quick response, so I'm afraid that I can't go super in-depth with what you need to do.  For accessing device drivers under Android, you'll have a native code component that will interface with the driver by opening the driver device file in the fileystem and then interacting with it via ioctl() calls.  bmfc187 explained this part.  This native component will then be called from your app's Java code via JNI.  Take a look at the following to get an idea of what I mean: http://electrofriends.com/articles/jni/jni-part1-java-native-interface/

Andrew

Mihir Jariwala

unread,
Feb 25, 2014, 12:13:41 AM2/25/14
to beagl...@googlegroups.com
Accoding to your guidelines i downloaded the source from following webpage http://downloads.ti.com/sitara_android/esd/TI_Android_DevKit/TI_Android_JB_4_2_2_DevKit_4_1_1/index_FDS.html


the source successfully gets executed and a directory is created in same folder.  

chmod a+x [filename]

in a terminal, or by right-clicking on the file in nautilus and going to properties>permissions> and check "allow executing fils as program".  Then open up a terminal, and cd to the directory you saved the file to, and type in the name of the file to execute it.  This will bring up a user agreement, you must scroll to the bottom by pressing enter to scroll to the next line, then type "I ACCEPT" at the end.  Careful, if you scroll too fast, you will be pressing enter when the "I ACCEPT" prompt comes up and the terminal will exit without doing anything.  After you type "I ACCEPT" and press enter, the downloader will start and you will find your source directory in the same directory you downloaded your file to. 

But when i open the created folder its empty so I am unable to navigate to th said folder. I am working on beaglebone and will be using SPI interface. I will be workin on android.
 
After you have the source navigate to /kernel/Documentation/ and you will see directories for ACPI, SPI, I2C, etc, (I dont see directories for PWM or GPIO but there is a gpio.txt and pwm.txt that seem to have the information for those interfaces, respectively).  For SPI the file you want appears to be /kernel/Documentation/spi/spi-summary.

hope this helps...

Please do provide a feedback on the same I have attached the image of the open directory below

 

LongQi Zhang

unread,
Aug 25, 2014, 12:04:16 AM8/25/14
to beagl...@googlegroups.com, ryan...@gmail.com
I'm also searching this, this should be helpful.
http://processors.wiki.ti.com/index.php/Android_Beaglebone_Weather_Cape

bmfc187

unread,
Jun 20, 2013, 10:37:17 AM6/20/13
to beagl...@googlegroups.com
chmod a+x [filename]

in a terminal, or by right-clicking on the file in nautilus and going to properties>permissions> and check "allow executing fils as program".  Then open up a terminal, and cd to the directory you saved the file to, and type in the name of the file to execute it.  This will bring up a user agreement, you must scroll to the bottom by pressing enter to scroll to the next line, then type "I ACCEPT" at the end.  Careful, if you scroll too fast, you will be pressing enter when the "I ACCEPT" prompt comes up and the terminal will exit without doing anything.  After you type "I ACCEPT" and press enter, the downloader will start and you will find your source directory in the same directory you downloaded your file to. 

Reply all
Reply to author
Forward
0 new messages