Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Presented by
Date
Event
SFO15-505: Introducing
I2C and GPIO userspace
APIs for Android
Satish PatelSatish Patel
Friday 25 ...
Scope
● Background
● High level diagram
● API reference
● Why native libs ?
● Why SDK Addon ?
● Application reference
● De...
Background
● Modularity spreading across all domains
○ e.g. Project ARA based on concept - Phoneblocks
● Plenty i2c/spi/gp...
High Level Diagram - I2C
I2C DEVICE
HARDWARE
/dev/i2c-x
KERNEL
ioctl
I2C NATIVE API
(libandroidruntime.so) II2cManagerServ...
High Level Diagram - GPIO
GPIO DEVICE
HARDWARE
/sys/class/gpio
KERNEL
IGpioManagerService
GpioService
GpioManager
libudev
...
API Reference - I2C
APIs Description
I2cManager :
getInstance(Context context)
Returns a new instance of this class
I2cMan...
API Reference - GPIO
APIs Description
GpioManager :
getInstance(Context context)
Returns a new instance of this class
Gpio...
Why libudev ?
● Hotplug does not guarantee static device node
● I2C HAL opens device based on “product id” not with
actual...
Why Native Libs?
● I2cDevice class has PFD:ParcelFileDescriptor
● JAVA developers can use - i/o stream to do
transaction w...
Why Native Libs? Contd..
I2cService
● lPC lentancy
● Some apps need
continuous data from the
device might suffer
Native Ex...
Why SDK Addon?
● Enable device specific features to app developers
● Avoid distribution of whole OEM specific SDK -
Can ru...
Application Front
<?xml version="1.0" encoding="utf-8"?>
<manifest ….
<uses-feature android:name="android.hardware.i2c" />...
Application Front - open device & set slave
import com.google.ara.i2c.I2cManager;
import com.google.ara.i2c.I2cDevice;
imp...
Application Front - i2c transactions
// use native APIs
public void readValueNative() {
byte[] reg = new byte[1];
byte[] d...
Application Front - Using Java I/O
public void openI2cDevice() {
mDevice =i2cm.openDevice();
mPfd = mDevice.getPfd();
// o...
Application Front - GPIO
<?xml version="1.0" encoding="utf-8"?>
<manifest ….
<uses-feature android:name="android.hardware....
References
● Helium - PXA1928 based release with i2c/gpio support
available at
http://releases-ara-mdk.linaro.org/ara_pxa1...
Demo !!
Thank You
Upcoming SlideShare
Loading in …5
×

SFO15-505: Introducing I2C & GPIO user space APIs for Android

2,238 views

Published on

SFO15-505: Introducing I2C & GPIO user space APIs for Android
Speaker: Satish Patel
Date: September 25, 2015

★ Session Description ★
Android doesn‰’t give application access to hardware over bus interfacing APIs like I2C, SPI, GPIO, UART, etc. This is going to change in future as Android apps needs to work with multiple different devices like sensors, controllers, etc directly to meet the efficiency and speed required. In this presentation we will introduce the APIs, architecture design and a working demo.

★ Resources ★
Video:
Presentation: http://www.slideshare.net/linaroorg/sfo15505-introducing-i2c-gpio-user-space-apis-for-android
Etherpad: pad.linaro.org/p/sfo15-505
Pathable: https://sfo15.pathable.com/meetings/303097

★ Event Details ★
Linaro Connect San Francisco 2015 - #SFO15
September 21-25, 2015
Hyatt Regency Hotel
http://www.linaro.org
http://connect.linaro.org

Published in: Technology
  • Be the first to comment

SFO15-505: Introducing I2C & GPIO user space APIs for Android

  1. 1. Presented by Date Event SFO15-505: Introducing I2C and GPIO userspace APIs for Android Satish PatelSatish Patel Friday 25 September 2015 SFO15
  2. 2. Scope ● Background ● High level diagram ● API reference ● Why native libs ? ● Why SDK Addon ? ● Application reference ● Demo!! Master Slave VCC SDA SCL GND GPIO
  3. 3. Background ● Modularity spreading across all domains ○ e.g. Project ARA based on concept - Phoneblocks ● Plenty i2c/spi/gpio based sensors/devices.. - But no straight APIs to Android at present ● It’s a kick start to build bridge to access non- conventional protocols inside Android
  4. 4. High Level Diagram - I2C I2C DEVICE HARDWARE /dev/i2c-x KERNEL ioctl I2C NATIVE API (libandroidruntime.so) II2cManagerService I2cService I2cManagerI2cDevice libudev I2C APP ------------------------------------ I2C SDK Addon lib I2C HAL HAL FRAMEWORK APPLICATION Read app meta-data and extra device information for which access is requested
  5. 5. High Level Diagram - GPIO GPIO DEVICE HARDWARE /sys/class/gpio KERNEL IGpioManagerService GpioService GpioManager libudev GPIO APP ------------------------------------ GPIO SDK Addon lib GPIO HAL FRAMEWORK APPLICATION HAL
  6. 6. API Reference - I2C APIs Description I2cManager : getInstance(Context context) Returns a new instance of this class I2cManager : openDevice() Opens an i2c device so it can be used to send and receive data using I2cDevice I2cDevice : closePfd() Close ParcelFileDescriptor attached to i2c device I2cDevice : doTransaction (I2cMessage... msgs) Perform i2c transaction I2cDevice : setSlave(int addr) Set i2c slave address I2cDevice : getPfd() Returns ParcelFileDescriptor of an i2c device
  7. 7. API Reference - GPIO APIs Description GpioManager : getInstance(Context context) Returns a new instance of this class GpioManager : openGpio(int gpio, java.lang.String direction) Returns file descriptor of gpio device value closeGpio(int gpio) Close mentioned gpio number
  8. 8. Why libudev ? ● Hotplug does not guarantee static device node ● I2C HAL opens device based on “product id” not with actual device node ● HAL finds correct i2c device based on “product id” and map the device node. So HAL need libudev help!!! char *find_i2cdevice(const char *dev_name) shell@pxa1928:/ # cat /sys/class/i2c-dev/i2c-0/device/0-0030/name 88pm800
  9. 9. Why Native Libs? ● I2cDevice class has PFD:ParcelFileDescriptor ● JAVA developers can use - i/o stream to do transaction with i2c device using PFD ● Still “I want more…” ○ set slave address ○ multiple i2c transactions ○ setting 10 bit address scheme
  10. 10. Why Native Libs? Contd.. I2cService ● lPC lentancy ● Some apps need continuous data from the device might suffer Native Extension (I2cDevice) ● Runs in application context ● APIs ○ setSlave() ○ doTransaction() ● Work on opened PFD ● No special permission needed SERVICE APP BINDER
  11. 11. Why SDK Addon? ● Enable device specific features to app developers ● Avoid distribution of whole OEM specific SDK - Can run on existing AOSP SDK - Size matters !!! ● Only Stubs will be visible to App developer ● Easy of integration - Android Studio
  12. 12. Application Front <?xml version="1.0" encoding="utf-8"?> <manifest …. <uses-feature android:name="android.hardware.i2c" /> <application… <uses-library android:name="com.google.ara.i2c" /> <activity….. <intent-filter> <category android:name="android.intent.category. DEFAULT" /> <action android:name="android.hardware.i2c.action. I2C_DEVICE_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.i2c.action. I2C_DEVICE_ATTACHED" android:resource="@xml/i2c_filter" /> </activity> </application> </manifest> res/xml/i2c_filter.xml <?xml version="1.0" encoding="utf-8"?> <resources> <i2c-device product-id="<ABC>" vendor-id=” <XYZ>" version="1.0"/> </resources>
  13. 13. Application Front - open device & set slave import com.google.ara.i2c.I2cManager; import com.google.ara.i2c.I2cDevice; import com.google.ara.i2c.I2cMessage; …... …... private I2cManager i2cm; I2cDevice mDevice; void openI2cDevice() { i2cm = I2cManager.getInstance(this); mDevice = i2cm.openDevice(); ..….. .…... } // set slave addr I2cDevice mDevice; /* open i2c device before setting slave address */ public void setSlave() { int ret; ret = mDevice.setSlave(0x4c); //0x4c is for ref# if(ret == 0) Log.d(TAG, "Slave address set!!"); else Log.d(TAG, "Error in setting slave address"); }
  14. 14. Application Front - i2c transactions // use native APIs public void readValueNative() { byte[] reg = new byte[1]; byte[] data = new byte[1]; int ret = 0; reg[0] = (byte)0x3A; /* Two operation is necessary for read operation * first write the register we want to read * and then read the data */ I2cMessage[] msg = new I2cMessage[2]; msg[0] = new I2cMessage(0x30, 0, 1, reg); /* slave addr, write, length, reg */ msg[1] = new I2cMessage(0x30, 1, 1, data); /* slave addr, read, lenght, data */ ret = mDevice.doTransaction(msg); String hexString = Integer.toHexString(data[0]); Log.d(TAG, "Read Value Native, hex string:" + hexString); }
  15. 15. Application Front - Using Java I/O public void openI2cDevice() { mDevice =i2cm.openDevice(); mPfd = mDevice.getPfd(); // open file stream using fd mFile = pfd.getFileDescriptor(); mInputStream = new FileInputStream(pfile); mOutputStream = new FileOutputStream(pfile); ……… } //reading the value from i2c device public void readValue () { byte[] data = {0,0,0,0,0}; byte[] cmd = new byte[1]; cmd[0] = (byte) 0x3A; setSlave(); // first write the cmd for register we want to read and then read the value try { mOutputStream.write(cmd); mOutputStream.flush(); mInputStream.read(data); } catch (IOException e) { Log.d(TAG, "Something wrong with device read and write"); }
  16. 16. Application Front - GPIO <?xml version="1.0" encoding="utf-8"?> <manifest …. <uses-feature android:name="android.hardware.gpio" /> <application… <uses-library android:name="com.google.ara.gpio" /> <activity….. <meta-data android:name="android.hardware.gpio.action. GPIO_DEVICE_ATTACHED" android:resource="@xml/gpio_filter" /> </activity> </application> </manifest> res/xml/gpio_filter.xml <?xml version="1.0" encoding="utf-8"?> <resources> <gpio-device product-id="<ABC>" vendor- id=”<XYZ>" version="1.0"/> </resources>
  17. 17. References ● Helium - PXA1928 based release with i2c/gpio support available at http://releases-ara-mdk.linaro.org/ara_pxa1928-5.1.1-open/ ● Release includes prebuilt binary, demo application binary and sources
  18. 18. Demo !!
  19. 19. Thank You

×