├── ADCDemo ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── Android.mk ├── AndroidManifest.xml ├── build.sh ├── libs │ └── armeabi │ │ └── libfriendlyarm-hardware.so ├── project.properties ├── res │ ├── drawable │ │ └── icon.png │ ├── layout │ │ ├── adc_listview_item.xml │ │ └── adc_main.xml │ └── values │ │ └── strings.xml └── src │ └── com │ └── friendlyarm │ ├── ADCDemo │ ├── ADC.java │ └── ADCDemoMainActivity.java │ ├── AndroidSDK │ ├── FileCtlEnum.java │ ├── GPIOEnum.java │ ├── HardwareControler.java │ ├── SPI.java │ └── SPIEnum.java │ └── Utils │ └── CommonFuncs.java ├── GPIO_LED_Demo ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── Android.mk ├── AndroidManifest.xml ├── build.sh ├── libs │ └── armeabi │ │ └── libfriendlyarm-hardware.so ├── project.properties ├── res │ ├── drawable │ │ └── icon.png │ ├── layout │ │ ├── checkbox_listview_item.xml │ │ └── gpiodemo_main.xml │ └── values │ │ └── strings.xml └── src │ └── com │ └── friendlyarm │ ├── AndroidSDK │ ├── FileCtlEnum.java │ ├── GPIOEnum.java │ ├── HardwareControler.java │ ├── SPI.java │ └── SPIEnum.java │ ├── GPIODemo │ ├── GPIODemoMainActivity.java │ └── GPIOPin.java │ └── Utils │ └── CommonFuncs.java ├── I2C_LCD1602_Demo ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── Android.mk ├── AndroidManifest.xml ├── build.sh ├── libs │ └── armeabi │ │ └── libfriendlyarm-hardware.so ├── project.properties ├── res │ ├── drawable-hdpi │ │ └── icon.png │ ├── drawable-ldpi │ │ └── icon.png │ ├── drawable-mdpi │ │ └── icon.png │ ├── layout │ │ └── mainactivity.xml │ └── values │ │ └── strings.xml └── src │ └── com │ └── friendlyarm │ ├── AndroidSDK │ ├── FileCtlEnum.java │ ├── GPIOEnum.java │ ├── HardwareControler.java │ ├── SPI.java │ └── SPIEnum.java │ ├── LCD1602Demo │ ├── MainActivity.java │ └── PCF8574.java │ └── Utils │ └── CommonFuncs.java ├── PWMDemo ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── Android.mk ├── AndroidManifest.xml ├── build.sh ├── libs │ └── armeabi │ │ └── libfriendlyarm-hardware.so ├── project.properties ├── res │ ├── drawable │ │ └── icon.png │ ├── layout │ │ └── pwmtestingactivity.xml │ └── values │ │ └── strings.xml └── src │ └── com │ └── friendlyarm │ ├── AndroidSDK │ ├── FileCtlEnum.java │ ├── GPIOEnum.java │ ├── HardwareControler.java │ ├── SPI.java │ └── SPIEnum.java │ ├── PWMDemo │ └── PWMTestingActivity.java │ └── Utils │ ├── BorderScrollView.java │ └── CommonFuncs.java ├── README.md ├── SDCardDemo ├── Android.mk ├── AndroidManifest.xml ├── build.sh ├── ic_launcher-web.png ├── res │ ├── drawable-hdpi │ │ ├── ic_action_search.png │ │ └── ic_launcher.png │ ├── drawable-ldpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ ├── ic_action_search.png │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ ├── ic_action_search.png │ │ └── ic_launcher.png │ ├── layout │ │ └── activity_main.xml │ ├── menu │ │ └── activity_main.xml │ ├── values-v11 │ │ └── styles.xml │ ├── values-v14 │ │ └── styles.xml │ └── values │ │ ├── strings.xml │ │ └── styles.xml └── src │ └── com │ └── FriendlyARM │ └── sdcarddemo │ └── MainActivity.java ├── SPI_OLED_Demo ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── Android.mk ├── AndroidManifest.xml ├── Smart4418SDK+OLED.png ├── build.sh ├── libs │ └── armeabi │ │ └── libfriendlyarm-hardware.so ├── project.properties ├── res │ ├── drawable │ │ └── icon.png │ ├── layout │ │ └── mainactivity.xml │ └── values │ │ └── strings.xml └── src │ └── com │ └── friendlyarm │ ├── AndroidSDK │ ├── FileCtlEnum.java │ ├── GPIOEnum.java │ ├── HardwareControler.java │ ├── SPI.java │ └── SPIEnum.java │ ├── SPI_OLED │ ├── MainActivity.java │ └── OLED.java │ └── Utils │ └── CommonFuncs.java ├── SerialPortDemo ├── .classpath ├── AndroidManifest.xml ├── libs │ └── armeabi │ │ └── libfriendlyarm-hardware.so ├── res │ ├── drawable │ │ └── icon.png │ ├── layout │ │ ├── serialport_dataprocessview.xml │ │ └── serialport_dataprocessview_landscape.xml │ └── values │ │ └── strings.xml └── src │ └── com │ └── friendlyarm │ ├── AndroidSDK │ ├── FileCtlEnum.java │ ├── GPIOEnum.java │ ├── HardwareControler.java │ ├── SPI.java │ └── SPIEnum.java │ ├── SerialPortDemo │ └── SerialPortDataProcessView.java │ └── Utils │ ├── BorderScrollView.java │ └── CommonFuncs.java └── 友善电子Android硬件开发指南.pdf /ADCDemo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /ADCDemo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ADCDemoMainActivity 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /ADCDemo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.source=1.6 5 | -------------------------------------------------------------------------------- /ADCDemo/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_SRC_FILES := $(call all-subdir-java-files) 5 | 6 | LOCAL_PACKAGE_NAME := ADCDemo 7 | 8 | LOCAL_CERTIFICATE := platform 9 | LOCAL_MODULE_TAGS := eng 10 | 11 | include $(BUILD_PACKAGE) 12 | -------------------------------------------------------------------------------- /ADCDemo/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /ADCDemo/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ANDROIDPATH=/opt/FriendlyARM/smart4418/android/ 4 | APPNAME=ADCDemo 5 | APKNAME=${APPNAME}-r1.0.apk 6 | PRJPATH=$PWD 7 | cd $PRJPATH 8 | rm -rf bin gen 9 | 10 | cd $ANDROIDPATH 11 | rm -f $APKNAME 12 | . setenv 13 | mmm $PRJPATH -B 14 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi2/security/platform.x509.pem vendor/friendly-arm/nanopi2/security/platform.pk8 out/target/product/nanopi2/obj/APPS/${APPNAME}_intermediates/package.apk.unsigned ${APPNAME}-unaligned.apk 15 | zipalign -v 4 ${APPNAME}-unaligned.apk $APKNAME 16 | adb install -r ${APKNAME} 17 | -------------------------------------------------------------------------------- /ADCDemo/libs/armeabi/libfriendlyarm-hardware.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/ADCDemo/libs/armeabi/libfriendlyarm-hardware.so -------------------------------------------------------------------------------- /ADCDemo/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-17 15 | -------------------------------------------------------------------------------- /ADCDemo/res/drawable/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/ADCDemo/res/drawable/icon.png -------------------------------------------------------------------------------- /ADCDemo/res/layout/adc_listview_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 15 | 16 | 26 | 27 | -------------------------------------------------------------------------------- /ADCDemo/res/layout/adc_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | -------------------------------------------------------------------------------- /ADCDemo/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | ADC Demo 4 | ADC Testing: /dev/adc 5 | 6 | -------------------------------------------------------------------------------- /ADCDemo/src/com/friendlyarm/ADCDemo/ADC.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.ADCDemo; 2 | 3 | public class ADC { 4 | 5 | int value = 0; 6 | String name = null; 7 | 8 | public ADC(int value, String name) { 9 | super(); 10 | this.value = value; 11 | this.name = name; 12 | } 13 | 14 | public int getValue() { 15 | return value; 16 | } 17 | 18 | public void setValue(int value) { 19 | this.value = value; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /ADCDemo/src/com/friendlyarm/ADCDemo/ADCDemoMainActivity.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.ADCDemo; 2 | import java.util.ArrayList; 3 | import java.util.Timer; 4 | import java.util.TimerTask; 5 | import android.app.Activity; 6 | import android.content.Context; 7 | import android.os.Bundle; 8 | import android.os.Handler; 9 | import android.os.Message; 10 | import android.util.Log; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.ArrayAdapter; 15 | import android.widget.ListView; 16 | import android.widget.TextView; 17 | 18 | import com.friendlyarm.AndroidSDK.HardwareControler; 19 | 20 | public class ADCDemoMainActivity extends Activity { 21 | 22 | MyCustomAdapter dataAdapter = null; 23 | 24 | @Override 25 | public void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.adc_main); 28 | Log.d("ADCDemo", "BoardID: " + boardType); 29 | timer.schedule(task, 0, 500); 30 | } 31 | 32 | private int boardType = HardwareControler.getBoardType(); 33 | private Timer timer = new Timer(); 34 | private Handler handler = new Handler() { 35 | public void handleMessage(Message msg) { 36 | 37 | // Array list of countries 38 | ArrayList adcValueList = new ArrayList(); 39 | 40 | switch (msg.what) { 41 | case 1: 42 | if (boardType == HardwareControler.S3C6410_COMMON) { 43 | int[] channels = { 0, 1, 4, 5, 6, 7 }; 44 | int[] adc = HardwareControler.readADCWithChannels(channels); 45 | 46 | for (int i = 0; i < 6; i++) { 47 | ADC adcObj = new ADC(adc[i], String.format("[AIN%d]", 48 | channels[i])); 49 | adcValueList.add(adcObj); 50 | } 51 | 52 | } else if (boardType == HardwareControler.S5PV210_COMMON) { 53 | int[] channels = { 0, 1, 6, 7, 8, 9 }; 54 | int[] adc = HardwareControler.readADCWithChannels(channels); 55 | 56 | for (int i = 0; i < 6; i++) { 57 | ADC adcObj = new ADC(adc[i], String.format("[AIN%d]", 58 | channels[i])); 59 | adcValueList.add(adcObj); 60 | } 61 | 62 | } else if (boardType == HardwareControler.S5P4412_COMMON) { 63 | int[] channels = { 0, 1, 2, 3 }; 64 | int[] adc = HardwareControler.readADCWithChannels(channels); 65 | 66 | for (int i = 0; i < 4; i++) { 67 | ADC adcObj = new ADC(adc[i], String.format("[AIN%d]", 68 | channels[i])); 69 | adcValueList.add(adcObj); 70 | } 71 | 72 | } else if (boardType == HardwareControler.Smart4418SDK) { 73 | int[] channels = { 1, 3, 4, 5, 6, 7 }; 74 | int[] adc = HardwareControler.readADCWithChannels(channels); 75 | 76 | for (int i = 0; i < channels.length; i++) { 77 | ADC adcObj = new ADC(adc[i], String.format("[AIN%d]", 78 | channels[i])); 79 | adcValueList.add(adcObj); 80 | } 81 | } else { 82 | int adc = HardwareControler.readADCWithChannel(0); 83 | ADC adcObj = new ADC(adc, String.format("[AIN%d]", 0)); 84 | adcValueList.add(adcObj); 85 | } 86 | 87 | dataAdapter = new MyCustomAdapter(getApplicationContext(), 88 | R.layout.adc_listview_item, adcValueList); 89 | ListView listView = (ListView) findViewById(R.id.listView1); 90 | listView.setAdapter(dataAdapter); 91 | 92 | break; 93 | } 94 | super.handleMessage(msg); 95 | } 96 | }; 97 | private TimerTask task = new TimerTask() { 98 | public void run() { 99 | Message message = new Message(); 100 | message.what = 1; 101 | handler.sendMessage(message); 102 | } 103 | }; 104 | 105 | 106 | private class MyCustomAdapter extends ArrayAdapter { 107 | 108 | private ArrayList adcValueList; 109 | 110 | public MyCustomAdapter(Context context, int textViewResourceId, 111 | ArrayList adcValueList) { 112 | super(context, textViewResourceId, adcValueList); 113 | this.adcValueList = new ArrayList(); 114 | this.adcValueList.addAll(adcValueList); 115 | } 116 | 117 | private class ViewHolder { 118 | TextView nameTextView; 119 | TextView valueTextView; 120 | } 121 | 122 | @Override 123 | public View getView(int position, View convertView, ViewGroup parent) { 124 | 125 | ViewHolder holder = null; 126 | Log.v("ConvertView", String.valueOf(position)); 127 | 128 | if (convertView == null) { 129 | LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 130 | convertView = vi.inflate(R.layout.adc_listview_item, null); 131 | 132 | holder = new ViewHolder(); 133 | 134 | holder.nameTextView = (TextView) convertView 135 | .findViewById(R.id.listTextView1); 136 | 137 | holder.valueTextView = (TextView) convertView 138 | .findViewById(R.id.listTextView2); 139 | 140 | convertView.setTag(holder); 141 | 142 | } else { 143 | holder = (ViewHolder) convertView.getTag(); 144 | } 145 | 146 | ADC adcObj = adcValueList.get(position); 147 | if (holder.nameTextView != null) { 148 | holder.nameTextView.setText(adcObj.getName()); 149 | } 150 | if (holder.valueTextView != null) { 151 | holder.valueTextView.setText(String.valueOf(adcObj.getValue())); 152 | } 153 | 154 | return convertView; 155 | 156 | } 157 | 158 | } 159 | } -------------------------------------------------------------------------------- /ADCDemo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | 3 | public class FileCtlEnum { 4 | /* File Flag */ 5 | public final static int O_ACCMODE = 00000003; 6 | public final static int O_RDONLY = 00000000; 7 | public final static int O_WRONLY = 00000001; 8 | public final static int O_RDWR = 00000002; 9 | public final static int O_CREAT = 00000100; /* not fcntl */ 10 | public final static int O_EXCL = 00000200; /* not fcntl */ 11 | public final static int O_NOCTTY = 00000400; /* not fcntl */ 12 | public final static int O_TRUNC = 00001000; /* not fcntl */ 13 | public final static int O_APPEND = 00002000; 14 | public final static int O_NONBLOCK = 00004000; 15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */ 16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */ 17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */ 18 | public final static int O_LARGEFILE = 00100000; 19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */ 20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */ 21 | public final static int O_NOATIME = 01000000; 22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */ 23 | } 24 | -------------------------------------------------------------------------------- /ADCDemo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | 3 | /* 4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42) 5 | and have the following read/write attributes: 6 | 7 | /sys/class/gpio/gpioN/ 8 | 9 | "direction" ... reads as either "in" or "out". This value may 10 | normally be written. Writing as "out" defaults to 11 | initializing the value as low. To ensure glitch free 12 | operation, values "low" and "high" may be written to 13 | configure the GPIO as an output with that initial value. 14 | 15 | Note that this attribute *will not exist* if the kernel 16 | doesn't support changing the direction of a GPIO, or 17 | it was exported by kernel code that didn't explicitly 18 | allow userspace to reconfigure this GPIO's direction. 19 | 20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO 21 | is configured as an output, this value may be written; 22 | any nonzero value is treated as high. 23 | 24 | If the pin can be configured as interrupt-generating interrupt 25 | and if it has been configured to generate interrupts (see the 26 | description of "edge"), you can poll(2) on that file and 27 | poll(2) will return whenever the interrupt was triggered. If 28 | you use poll(2), set the events POLLPRI and POLLERR. If you 29 | use select(2), set the file descriptor in exceptfds. After 30 | poll(2) returns, either lseek(2) to the beginning of the sysfs 31 | file and read the new value or close the file and re-open it 32 | to read the value. 33 | */ 34 | 35 | public class GPIOEnum { 36 | //Direction 37 | public final static int IN = 1; 38 | public final static int OUT = 2; 39 | 40 | //Value 41 | public final static int LOW = 0; 42 | public final static int HIGH = 1; 43 | } 44 | -------------------------------------------------------------------------------- /ADCDemo/src/com/friendlyarm/AndroidSDK/HardwareControler.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | import android.util.Log; 3 | 4 | public class HardwareControler 5 | { 6 | /* File */ 7 | static public native int open(String devName, int flags); 8 | static public native int write(int fd, byte[] data); 9 | static public native int read(int fd, byte[] buf, int len); 10 | static public native int select(int fd, int sec, int usec); 11 | static public native void close(int fd); 12 | static public native int ioctlWithIntValue(int fd, int cmd, int value); 13 | 14 | /* Serial Port */ 15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits ); 16 | static public native int openSerialPortEx( String devName 17 | , long baud 18 | , int dataBits 19 | , int stopBits 20 | , String parityBit 21 | , String flowCtrl 22 | ); 23 | 24 | /* LED */ 25 | static public native int setLedState( int ledID, int ledState ); 26 | 27 | /* PWM */ 28 | static public native int PWMPlay(int frequency); 29 | static public native int PWMStop(); 30 | static public native int PWMPlayEx(int gpioPin, int frequency); 31 | static public native int PWMStopEx(int gpioPin); 32 | 33 | /* ADC */ 34 | static public native int readADC(); 35 | static public native int readADCWithChannel(int channel); 36 | static public native int[] readADCWithChannels(int[] channels); 37 | 38 | /* I2C */ 39 | static public native int setI2CSlave(int fd, int slave); 40 | static public native int setI2CTimeout(int fd, int timeout); 41 | static public native int setI2CRetries(int fd, int retries); 42 | static public native int I2CReadByteData(int fd, int pos, int wait_us); 43 | static public native int I2CReadByte(int fd, int wait_us); 44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us); 45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us); 46 | 47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms); 48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms); 49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms); 50 | 51 | /* SPI */ 52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits ); 53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits ); 54 | static public native int setSPIBitOrder( int spi_fd, int order); 55 | static public native int setSPIClockDivider( int spi_fd, int divider); 56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed); 57 | static public native int setSPIDataMode( int spi_fd, int mode); 58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits); 59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits); 60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits); 61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits); 62 | 63 | /* GPIO */ 64 | static public native int exportGPIOPin(int pin); 65 | static public native int unexportGPIOPin(int pin); 66 | //GPIOEnum.LOW or GPIOEnum.HIGH 67 | static public native int setGPIOValue(int pin, int value); 68 | static public native int getGPIOValue(int pin); 69 | //GPIOEnum.IN or GPIOEnum.OUT 70 | static public native int setGPIODirection(int pin, int direction); 71 | static public native int getGPIODirection(int pin); 72 | 73 | /* OldInterface: for EEPROM */ 74 | static public native int openI2CDevice(); 75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData); 76 | static public native int readByteDataFromI2C(int fd, int pos); 77 | 78 | static public native int getBoardType(); 79 | /* getBoardType return value: */ 80 | public static final int S3C6410_COMMON = 6410; 81 | public static final int S5PV210_COMMON = 210; 82 | public static final int S5P4412_COMMON = 4412; 83 | public static final int S5P4418_BASE = 4418; 84 | public static final int NanoPi2 = S5P4418_BASE+0; 85 | public static final int NanoPC_T2 = S5P4418_BASE+1; 86 | public static final int NanoPi_S2 = S5P4418_BASE+2; 87 | public static final int Smart4418 = S5P4418_BASE+3; 88 | public static final int NanoPi2_Fire = S5P4418_BASE+4; 89 | public static final int NanoPi_M2 = S5P4418_BASE+5; 90 | public static final int NanoPi_M2A = S5P4418_BASE+7; 91 | public static final int Smart4418SDK = S5P4418_BASE+0x103; 92 | public static final int S5P4418_MAX = Smart4418SDK; 93 | public static final int S5P6818_BASE = 6818; 94 | public static final int NanoPC_T3 = S5P6818_BASE+1; 95 | public static final int NanoPi_S3 = S5P6818_BASE+2; 96 | public static final int Smart6818 = S5P6818_BASE+3; 97 | public static final int NanoPi_M3 = S5P6818_BASE+7; 98 | public static final int S5P6818_MAX = NanoPi_M3; 99 | 100 | static public boolean isS5P4418Board() { 101 | int boardtype = getBoardType(); 102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX; 103 | } 104 | 105 | static public boolean isS5P6818Board() { 106 | int boardtype = getBoardType(); 107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX; 108 | } 109 | 110 | static { 111 | try { 112 | System.loadLibrary("friendlyarm-hardware"); 113 | } catch (UnsatisfiedLinkError e) { 114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!"); 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /ADCDemo/src/com/friendlyarm/AndroidSDK/SPI.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | import android.util.Log; 3 | import android.widget.Toast; 4 | 5 | import com.friendlyarm.AndroidSDK.HardwareControler; 6 | import com.friendlyarm.AndroidSDK.SPIEnum; 7 | import com.friendlyarm.AndroidSDK.GPIOEnum; 8 | import com.friendlyarm.AndroidSDK.FileCtlEnum; 9 | 10 | public class SPI { 11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI"; 12 | private int spi_mode = 0; 13 | private int spi_bits = 8; 14 | private int spi_delay = 0; 15 | private int spi_speed = 500000; 16 | private int spi_byte_order = SPIEnum.LSBFIRST; 17 | 18 | private static final String devName = "/dev/spidev1.0"; 19 | private int spi_fd = -1; 20 | 21 | public void begin() { 22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR ); 23 | if (spi_fd >= 0) { 24 | Log.d(TAG, "open " + devName + "ok!"); 25 | 26 | /* spi init */ 27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits ); 28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits ); 29 | } else { 30 | Log.d(TAG, "open " + devName + "failed!"); 31 | spi_fd = -1; 32 | } 33 | } 34 | 35 | public void end() { 36 | if (spi_fd != -1) { 37 | HardwareControler.close(spi_fd); 38 | spi_fd = -1; 39 | } 40 | } 41 | 42 | public void setBitOrder(int order) { 43 | if (spi_fd < 0) { 44 | return ; 45 | } 46 | spi_byte_order = SPIEnum.MSBFIRST; 47 | if(spi_byte_order == SPIEnum.LSBFIRST) { 48 | spi_mode |= SPIEnum.SPI_LSB_FIRST; 49 | } else { 50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST; 51 | } 52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order ); 53 | 54 | } 55 | 56 | public void setClockDivider(int divider) { 57 | if (spi_fd < 0) { 58 | return ; 59 | } 60 | spi_speed = 66666666/(2*(divider+1)); 61 | if(spi_speed > 500000) { 62 | spi_speed = 500000; 63 | } 64 | HardwareControler.setSPIClockDivider( spi_fd, divider); 65 | } 66 | 67 | public void setDataMode(int mode) { 68 | if (spi_fd < 0) { 69 | return ; 70 | } 71 | switch(mode) 72 | { 73 | case SPIEnum.SPI_MODE0: 74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL); 75 | break; 76 | case SPIEnum.SPI_MODE1: 77 | spi_mode &= ~(SPIEnum.SPI_CPOL); 78 | spi_mode |= (SPIEnum.SPI_CPHA); 79 | break; 80 | case SPIEnum.SPI_MODE2: 81 | spi_mode |= (SPIEnum.SPI_CPOL); 82 | spi_mode &= ~(SPIEnum.SPI_CPHA); 83 | break; 84 | case SPIEnum.SPI_MODE3: 85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL); 86 | break; 87 | default: 88 | Log.e(TAG, "error data mode"); 89 | } 90 | 91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode ); 92 | } 93 | 94 | public void setChipSelectPolarity(int cs, int active) { 95 | 96 | } 97 | 98 | public void chipSelect(int cs) { 99 | 100 | } 101 | 102 | public byte transfer(int value) { 103 | if (spi_fd < 0) { 104 | return 0; 105 | } 106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /ADCDemo/src/com/friendlyarm/AndroidSDK/SPIEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | public class SPIEnum { 3 | 4 | // SPIBitOrder 5 | public final static int LSBFIRST = 0; ///< LSB First 6 | public final static int MSBFIRST = 1; ///< MSB First 7 | 8 | // SPIMode 9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0 10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1 11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0 12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1 13 | 14 | 15 | public final static int SPI_CPHA = 0x01; 16 | public final static int SPI_CPOL = 0x02; 17 | public final static int SPI_CS_HIGH = 0x04; 18 | public final static int SPI_LSB_FIRST = 0x08; 19 | public final static int SPI_3WIRE = 0x10; 20 | public final static int SPI_LOOP = 0x20; 21 | public final static int SPI_NO_CS = 0x40; 22 | public final static int SPI_READY = 0x80; 23 | 24 | // SPIClockDivider 25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz 26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz 27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz 28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz 29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz 30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz 31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz 32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz 33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz 34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz 35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz 36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz 37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz 38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz 39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz 40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz 41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz 42 | } -------------------------------------------------------------------------------- /ADCDemo/src/com/friendlyarm/Utils/CommonFuncs.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.Utils; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.widget.Toast; 6 | import android.app.Activity; 7 | import android.os.Bundle; 8 | import android.os.Handler; 9 | import android.os.Message; 10 | import android.content.Context; 11 | import android.content.DialogInterface; 12 | 13 | public class CommonFuncs { 14 | public static void showAlertDialog(Context context, String message) { 15 | AlertDialog.Builder builder = new AlertDialog.Builder(context); 16 | builder.setMessage(message) 17 | .setCancelable(false) 18 | .setPositiveButton("Close", 19 | new DialogInterface.OnClickListener() { 20 | public void onClick(DialogInterface dialog, int id) { 21 | } 22 | }); 23 | AlertDialog alert = builder.create(); 24 | alert.show(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | GPIO_LED_Demo 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.source=1.6 5 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_SRC_FILES := $(call all-subdir-java-files) 5 | 6 | LOCAL_PACKAGE_NAME := GPIODemo 7 | 8 | LOCAL_CERTIFICATE := platform 9 | LOCAL_MODULE_TAGS := eng 10 | 11 | include $(BUILD_PACKAGE) 12 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | cd /opt/FriendlyARM/android_examples/jni-demos/GPIO_LED_Demo 5 | rm -rf gen bin 6 | 7 | cd /opt/FriendlyARM/smart4418/android/ 8 | rm -f GPIODemo-r1.0.apk 9 | . setenv 10 | mmm /opt/FriendlyARM/android_examples/jni-demos/GPIO_LED_Demo 11 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi3/security/platform.x509.pem vendor/friendly-arm/nanopi3/security/platform.pk8 /opt/FriendlyARM/smart4418/android/out/target/product/nanopi3/obj/APPS/GPIODemo_intermediates/package.apk.unsigned GPIODemo-unaligned.apk 12 | zipalign -v 4 GPIODemo-unaligned.apk GPIODemo-r1.0.apk 13 | # out/host/linux-x86/bin/zipalign -f 4 out/target/product/nanopi3/obj/APPS/GPIODemo_intermediates/package.apk.unaligned out/target/product/nanopi3/obj/APPS/GPIODemo_intermediates/package.apk.aligned 14 | adb install -r GPIODemo-r1.0.apk 15 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/libs/armeabi/libfriendlyarm-hardware.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/GPIO_LED_Demo/libs/armeabi/libfriendlyarm-hardware.so -------------------------------------------------------------------------------- /GPIO_LED_Demo/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-17 15 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/res/drawable/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/GPIO_LED_Demo/res/drawable/icon.png -------------------------------------------------------------------------------- /GPIO_LED_Demo/res/layout/checkbox_listview_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/res/layout/gpiodemo_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | GPIO Demo 4 | GPIO Demo 5 | LED 6 | 7 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | 3 | public class FileCtlEnum { 4 | /* File Flag */ 5 | public final static int O_ACCMODE = 00000003; 6 | public final static int O_RDONLY = 00000000; 7 | public final static int O_WRONLY = 00000001; 8 | public final static int O_RDWR = 00000002; 9 | public final static int O_CREAT = 00000100; /* not fcntl */ 10 | public final static int O_EXCL = 00000200; /* not fcntl */ 11 | public final static int O_NOCTTY = 00000400; /* not fcntl */ 12 | public final static int O_TRUNC = 00001000; /* not fcntl */ 13 | public final static int O_APPEND = 00002000; 14 | public final static int O_NONBLOCK = 00004000; 15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */ 16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */ 17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */ 18 | public final static int O_LARGEFILE = 00100000; 19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */ 20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */ 21 | public final static int O_NOATIME = 01000000; 22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */ 23 | } 24 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | 3 | /* 4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42) 5 | and have the following read/write attributes: 6 | 7 | /sys/class/gpio/gpioN/ 8 | 9 | "direction" ... reads as either "in" or "out". This value may 10 | normally be written. Writing as "out" defaults to 11 | initializing the value as low. To ensure glitch free 12 | operation, values "low" and "high" may be written to 13 | configure the GPIO as an output with that initial value. 14 | 15 | Note that this attribute *will not exist* if the kernel 16 | doesn't support changing the direction of a GPIO, or 17 | it was exported by kernel code that didn't explicitly 18 | allow userspace to reconfigure this GPIO's direction. 19 | 20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO 21 | is configured as an output, this value may be written; 22 | any nonzero value is treated as high. 23 | 24 | If the pin can be configured as interrupt-generating interrupt 25 | and if it has been configured to generate interrupts (see the 26 | description of "edge"), you can poll(2) on that file and 27 | poll(2) will return whenever the interrupt was triggered. If 28 | you use poll(2), set the events POLLPRI and POLLERR. If you 29 | use select(2), set the file descriptor in exceptfds. After 30 | poll(2) returns, either lseek(2) to the beginning of the sysfs 31 | file and read the new value or close the file and re-open it 32 | to read the value. 33 | */ 34 | 35 | public class GPIOEnum { 36 | //Direction 37 | public final static int IN = 1; 38 | public final static int OUT = 2; 39 | 40 | //Value 41 | public final static int LOW = 0; 42 | public final static int HIGH = 1; 43 | } 44 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/HardwareControler.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | import android.util.Log; 3 | 4 | public class HardwareControler 5 | { 6 | /* File */ 7 | static public native int open(String devName, int flags); 8 | static public native int write(int fd, byte[] data); 9 | static public native int read(int fd, byte[] buf, int len); 10 | static public native int select(int fd, int sec, int usec); 11 | static public native void close(int fd); 12 | static public native int ioctlWithIntValue(int fd, int cmd, int value); 13 | 14 | /* Serial Port */ 15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits ); 16 | static public native int openSerialPortEx( String devName 17 | , long baud 18 | , int dataBits 19 | , int stopBits 20 | , String parityBit 21 | , String flowCtrl 22 | ); 23 | 24 | /* LED */ 25 | static public native int setLedState( int ledID, int ledState ); 26 | 27 | /* PWM */ 28 | static public native int PWMPlay(int frequency); 29 | static public native int PWMStop(); 30 | static public native int PWMPlayEx(int gpioPin, int frequency); 31 | static public native int PWMStopEx(int gpioPin); 32 | 33 | /* ADC */ 34 | static public native int readADC(); 35 | static public native int readADCWithChannel(int channel); 36 | static public native int[] readADCWithChannels(int[] channels); 37 | 38 | /* I2C */ 39 | static public native int setI2CSlave(int fd, int slave); 40 | static public native int setI2CTimeout(int fd, int timeout); 41 | static public native int setI2CRetries(int fd, int retries); 42 | static public native int I2CReadByteData(int fd, int pos, int wait_us); 43 | static public native int I2CReadByte(int fd, int wait_us); 44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us); 45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us); 46 | 47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms); 48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms); 49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms); 50 | 51 | /* SPI */ 52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits ); 53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits ); 54 | static public native int setSPIBitOrder( int spi_fd, int order); 55 | static public native int setSPIClockDivider( int spi_fd, int divider); 56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed); 57 | static public native int setSPIDataMode( int spi_fd, int mode); 58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits); 59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits); 60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits); 61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits); 62 | 63 | /* GPIO */ 64 | static public native int exportGPIOPin(int pin); 65 | static public native int unexportGPIOPin(int pin); 66 | //GPIOEnum.LOW or GPIOEnum.HIGH 67 | static public native int setGPIOValue(int pin, int value); 68 | static public native int getGPIOValue(int pin); 69 | //GPIOEnum.IN or GPIOEnum.OUT 70 | static public native int setGPIODirection(int pin, int direction); 71 | static public native int getGPIODirection(int pin); 72 | 73 | /* OldInterface: for EEPROM */ 74 | static public native int openI2CDevice(); 75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData); 76 | static public native int readByteDataFromI2C(int fd, int pos); 77 | 78 | static public native int getBoardType(); 79 | /* getBoardType return value: */ 80 | public static final int S3C6410_COMMON = 6410; 81 | public static final int S5PV210_COMMON = 210; 82 | public static final int S5P4412_COMMON = 4412; 83 | public static final int S5P4418_BASE = 4418; 84 | public static final int NanoPi2 = S5P4418_BASE+0; 85 | public static final int NanoPC_T2 = S5P4418_BASE+1; 86 | public static final int NanoPi_S2 = S5P4418_BASE+2; 87 | public static final int Smart4418 = S5P4418_BASE+3; 88 | public static final int NanoPi2_Fire = S5P4418_BASE+4; 89 | public static final int NanoPi_M2 = S5P4418_BASE+5; 90 | public static final int NanoPi_M2A = S5P4418_BASE+7; 91 | public static final int Smart4418SDK = S5P4418_BASE+0x103; 92 | public static final int S5P4418_MAX = Smart4418SDK; 93 | public static final int S5P6818_BASE = 6818; 94 | public static final int NanoPC_T3 = S5P6818_BASE+1; 95 | public static final int NanoPi_S3 = S5P6818_BASE+2; 96 | public static final int Smart6818 = S5P6818_BASE+3; 97 | public static final int NanoPi_M3 = S5P6818_BASE+7; 98 | public static final int S5P6818_MAX = NanoPi_M3; 99 | 100 | static public boolean isS5P4418Board() { 101 | int boardtype = getBoardType(); 102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX; 103 | } 104 | 105 | static public boolean isS5P6818Board() { 106 | int boardtype = getBoardType(); 107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX; 108 | } 109 | 110 | static { 111 | try { 112 | System.loadLibrary("friendlyarm-hardware"); 113 | } catch (UnsatisfiedLinkError e) { 114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!"); 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/SPI.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | import android.util.Log; 3 | import android.widget.Toast; 4 | 5 | import com.friendlyarm.AndroidSDK.HardwareControler; 6 | import com.friendlyarm.AndroidSDK.SPIEnum; 7 | import com.friendlyarm.AndroidSDK.GPIOEnum; 8 | import com.friendlyarm.AndroidSDK.FileCtlEnum; 9 | 10 | public class SPI { 11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI"; 12 | private int spi_mode = 0; 13 | private int spi_bits = 8; 14 | private int spi_delay = 0; 15 | private int spi_speed = 500000; 16 | private int spi_byte_order = SPIEnum.LSBFIRST; 17 | 18 | private static final String devName = "/dev/spidev1.0"; 19 | private int spi_fd = -1; 20 | 21 | public void begin() { 22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR ); 23 | if (spi_fd >= 0) { 24 | Log.d(TAG, "open " + devName + "ok!"); 25 | 26 | /* spi init */ 27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits ); 28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits ); 29 | } else { 30 | Log.d(TAG, "open " + devName + "failed!"); 31 | spi_fd = -1; 32 | } 33 | } 34 | 35 | public void end() { 36 | if (spi_fd != -1) { 37 | HardwareControler.close(spi_fd); 38 | spi_fd = -1; 39 | } 40 | } 41 | 42 | public void setBitOrder(int order) { 43 | if (spi_fd < 0) { 44 | return ; 45 | } 46 | spi_byte_order = SPIEnum.MSBFIRST; 47 | if(spi_byte_order == SPIEnum.LSBFIRST) { 48 | spi_mode |= SPIEnum.SPI_LSB_FIRST; 49 | } else { 50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST; 51 | } 52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order ); 53 | 54 | } 55 | 56 | public void setClockDivider(int divider) { 57 | if (spi_fd < 0) { 58 | return ; 59 | } 60 | spi_speed = 66666666/(2*(divider+1)); 61 | if(spi_speed > 500000) { 62 | spi_speed = 500000; 63 | } 64 | HardwareControler.setSPIClockDivider( spi_fd, divider); 65 | } 66 | 67 | public void setDataMode(int mode) { 68 | if (spi_fd < 0) { 69 | return ; 70 | } 71 | switch(mode) 72 | { 73 | case SPIEnum.SPI_MODE0: 74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL); 75 | break; 76 | case SPIEnum.SPI_MODE1: 77 | spi_mode &= ~(SPIEnum.SPI_CPOL); 78 | spi_mode |= (SPIEnum.SPI_CPHA); 79 | break; 80 | case SPIEnum.SPI_MODE2: 81 | spi_mode |= (SPIEnum.SPI_CPOL); 82 | spi_mode &= ~(SPIEnum.SPI_CPHA); 83 | break; 84 | case SPIEnum.SPI_MODE3: 85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL); 86 | break; 87 | default: 88 | Log.e(TAG, "error data mode"); 89 | } 90 | 91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode ); 92 | } 93 | 94 | public void setChipSelectPolarity(int cs, int active) { 95 | 96 | } 97 | 98 | public void chipSelect(int cs) { 99 | 100 | } 101 | 102 | public byte transfer(int value) { 103 | if (spi_fd < 0) { 104 | return 0; 105 | } 106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/src/com/friendlyarm/AndroidSDK/SPIEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | public class SPIEnum { 3 | 4 | // SPIBitOrder 5 | public final static int LSBFIRST = 0; ///< LSB First 6 | public final static int MSBFIRST = 1; ///< MSB First 7 | 8 | // SPIMode 9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0 10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1 11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0 12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1 13 | 14 | 15 | public final static int SPI_CPHA = 0x01; 16 | public final static int SPI_CPOL = 0x02; 17 | public final static int SPI_CS_HIGH = 0x04; 18 | public final static int SPI_LSB_FIRST = 0x08; 19 | public final static int SPI_3WIRE = 0x10; 20 | public final static int SPI_LOOP = 0x20; 21 | public final static int SPI_NO_CS = 0x40; 22 | public final static int SPI_READY = 0x80; 23 | 24 | // SPIClockDivider 25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz 26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz 27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz 28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz 29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz 30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz 31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz 32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz 33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz 34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz 35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz 36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz 37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz 38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz 39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz 40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz 41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz 42 | } -------------------------------------------------------------------------------- /GPIO_LED_Demo/src/com/friendlyarm/GPIODemo/GPIODemoMainActivity.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.GPIODemo; 2 | import java.util.ArrayList; 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Timer; 6 | import java.util.TimerTask; 7 | 8 | import android.app.Activity; 9 | import android.content.Context; 10 | import android.os.Bundle; 11 | import android.os.Handler; 12 | import android.os.Message; 13 | import android.util.Log; 14 | import android.view.LayoutInflater; 15 | import android.view.View; 16 | import android.view.ViewGroup; 17 | import android.view.View.OnClickListener; 18 | import android.widget.AdapterView; 19 | import android.widget.ArrayAdapter; 20 | import android.widget.Button; 21 | import android.widget.CheckBox; 22 | import android.widget.ListView; 23 | import android.widget.TextView; 24 | import android.widget.Toast; 25 | import android.os.SystemProperties; 26 | import android.widget.AdapterView.OnItemClickListener; 27 | 28 | import com.friendlyarm.AndroidSDK.GPIOEnum; 29 | import com.friendlyarm.AndroidSDK.HardwareControler; 30 | 31 | public class GPIODemoMainActivity extends Activity { 32 | 33 | private static final String TAG = "GPIODemo"; 34 | private Timer timer = new Timer(); 35 | private int step = 0; 36 | private Map demoGPIOPins = new HashMap(); 37 | 38 | static int STEP_INIT_GPIO_DIRECTION = 1; 39 | static int STEP_SETALLPIN_TO_HIGH = 2; 40 | static int STEP_INIT_VIEW = 3; 41 | 42 | @Override 43 | public void onDestroy() { 44 | timer.cancel(); 45 | super.onDestroy(); 46 | } 47 | 48 | 49 | private Handler handler = new Handler() { 50 | public void handleMessage(Message msg) { 51 | switch (msg.what) { 52 | case 1: 53 | timer.cancel(); 54 | // Generate list View from ArrayList 55 | displayListView(); 56 | break; 57 | } 58 | super.handleMessage(msg); 59 | } 60 | }; 61 | 62 | private TimerTask init_task = new TimerTask() { 63 | public void run() { 64 | Log.d(TAG, "init_task " + step); 65 | if (step == STEP_INIT_GPIO_DIRECTION) { 66 | for (Integer value: demoGPIOPins.values()) { 67 | if (HardwareControler.setGPIODirection(value, GPIOEnum.OUT) == 0) { 68 | } else { 69 | Log.v("TimerTask", String.format("setGPIODirection(%d) failed", value)); 70 | } 71 | } 72 | 73 | step ++; 74 | } else if (step == STEP_SETALLPIN_TO_HIGH) { 75 | for (Integer value: demoGPIOPins.values()) { 76 | if (HardwareControler.setGPIOValue(value, GPIOEnum.HIGH) == 0) { 77 | } else { 78 | Log.v("TimerTask", String.format("setGPIOValue(%d) failed", value)); 79 | } 80 | } 81 | 82 | step ++; 83 | } else if (step == STEP_INIT_VIEW) { 84 | Message message = new Message(); 85 | message.what = 1; 86 | handler.sendMessage(message); 87 | } 88 | 89 | } 90 | }; 91 | ////////////////////////////////////// 92 | 93 | MyCustomAdapter dataAdapter = null; 94 | 95 | @Override 96 | public void onCreate(Bundle savedInstanceState) { 97 | super.onCreate(savedInstanceState); 98 | setContentView(R.layout.gpiodemo_main); 99 | setTitle("GPIO Demo"); 100 | 101 | int boardType = HardwareControler.getBoardType(); 102 | if (boardType == HardwareControler.Smart4418SDK) { 103 | demoGPIOPins.put("GPIOC9", 73); 104 | demoGPIOPins.put("GPIOC10", 74); 105 | demoGPIOPins.put("GPIOC11", 75); 106 | demoGPIOPins.put("GPIOC12", 76); 107 | } else if (boardType == HardwareControler.NanoPC_T2 || boardType == HardwareControler.NanoPC_T3) { 108 | demoGPIOPins.put("Pin17", 68); 109 | demoGPIOPins.put("Pin18", 71); 110 | demoGPIOPins.put("Pin19", 72); 111 | demoGPIOPins.put("Pin20", 88); 112 | demoGPIOPins.put("Pin21", 92); 113 | demoGPIOPins.put("Pin22", 58); 114 | } else if (SystemProperties.get("ro.build.version.release").contains("4.1.2")) { 115 | demoGPIOPins.put("LED 1", 281); 116 | demoGPIOPins.put("LED 2", 282); 117 | demoGPIOPins.put("LED 3", 283); 118 | demoGPIOPins.put("LED 4", 284); 119 | } else { 120 | demoGPIOPins.put("LED 1", 79); 121 | demoGPIOPins.put("LED 2", 80); 122 | demoGPIOPins.put("LED 3", 81); 123 | demoGPIOPins.put("LED 4", 82); 124 | } 125 | 126 | // export all pins 127 | for (Integer value: demoGPIOPins.values()) { 128 | if (HardwareControler.exportGPIOPin(value) == 0) { 129 | } else { 130 | Toast.makeText(this, String.format("exportGPIOPin(%d) failed!", value), 131 | Toast.LENGTH_SHORT).show(); 132 | } 133 | } 134 | 135 | step = STEP_INIT_GPIO_DIRECTION; 136 | timer.schedule(init_task, 100, 100); 137 | } 138 | 139 | private void displayListView() { 140 | Log.d(TAG, "displayListView"); 141 | 142 | ArrayList pinList = new ArrayList(); 143 | 144 | for (Map.Entry entry : demoGPIOPins.entrySet()) { 145 | GPIOPin pin = new GPIOPin(entry.getValue(), entry.getKey(), false); 146 | pinList.add(pin); 147 | } 148 | 149 | dataAdapter = new MyCustomAdapter(this, R.layout.checkbox_listview_item, 150 | pinList); 151 | ListView listView = (ListView) findViewById(R.id.listView1); 152 | listView.setAdapter(dataAdapter); 153 | 154 | listView.setOnItemClickListener(new OnItemClickListener() { 155 | public void onItemClick(AdapterView parent, View view, 156 | int position, long id) { 157 | GPIOPin pin = (GPIOPin) parent.getItemAtPosition(position); 158 | if (HardwareControler.setGPIOValue(pin.code, 159 | pin.isSelected()?GPIOEnum.LOW:GPIOEnum.HIGH) == 0) { 160 | } else { 161 | Log.v(TAG, String.format("setGPIOValue(%d) failed", pin.code)); 162 | } 163 | } 164 | }); 165 | 166 | } 167 | 168 | private class MyCustomAdapter extends ArrayAdapter { 169 | private ArrayList pinList; 170 | public MyCustomAdapter(Context context, int textViewResourceId, 171 | ArrayList pinList) { 172 | super(context, textViewResourceId, pinList); 173 | this.pinList = new ArrayList(); 174 | this.pinList.addAll(pinList); 175 | } 176 | 177 | private class ViewHolder { 178 | CheckBox name; 179 | } 180 | 181 | @Override 182 | public View getView(int position, View convertView, ViewGroup parent) { 183 | 184 | ViewHolder holder = null; 185 | Log.v(TAG, String.valueOf(position)); 186 | 187 | if (convertView == null) { 188 | LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 189 | convertView = vi.inflate(R.layout.checkbox_listview_item, null); 190 | 191 | holder = new ViewHolder(); 192 | holder.name = (CheckBox) convertView 193 | .findViewById(R.id.checkBox1); 194 | convertView.setTag(holder); 195 | 196 | holder.name.setOnClickListener(new View.OnClickListener() { 197 | public void onClick(View v) { 198 | CheckBox cb = (CheckBox) v; 199 | GPIOPin pin = (GPIOPin) cb.getTag(); 200 | pin.setSelected(cb.isChecked()); 201 | if (HardwareControler.setGPIOValue(pin.code, 202 | pin.isSelected() ? GPIOEnum.LOW:GPIOEnum.HIGH) == 0) { 203 | } else { 204 | Log.v(TAG, String.format("setGPIOValue(%d) failed", pin.code)); 205 | } 206 | } 207 | }); 208 | } else { 209 | holder = (ViewHolder) convertView.getTag(); 210 | } 211 | 212 | GPIOPin pin = pinList.get(position); 213 | holder.name.setText(pin.getName()); 214 | holder.name.setChecked(pin.isSelected()); 215 | holder.name.setTag(pin); 216 | 217 | return convertView; 218 | 219 | } 220 | 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /GPIO_LED_Demo/src/com/friendlyarm/GPIODemo/GPIOPin.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.GPIODemo; 2 | 3 | public class GPIOPin { 4 | 5 | int code = 0; 6 | String name = null; 7 | boolean selected = false; 8 | 9 | public GPIOPin(int code, String name, boolean selected) { 10 | super(); 11 | this.code = code; 12 | this.name = name; 13 | this.selected = selected; 14 | } 15 | 16 | public int getCode() { 17 | return code; 18 | } 19 | public void setCode(int code) { 20 | this.code = code; 21 | } 22 | public String getName() { 23 | return name; 24 | } 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public boolean isSelected() { 30 | return selected; 31 | } 32 | public void setSelected(boolean selected) { 33 | this.selected = selected; 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /GPIO_LED_Demo/src/com/friendlyarm/Utils/CommonFuncs.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.Utils; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.widget.Toast; 6 | import android.app.Activity; 7 | import android.os.Bundle; 8 | import android.os.Handler; 9 | import android.os.Message; 10 | import android.content.Context; 11 | import android.content.DialogInterface; 12 | 13 | public class CommonFuncs { 14 | public static void showAlertDialog(Context context, String message) { 15 | AlertDialog.Builder builder = new AlertDialog.Builder(context); 16 | builder.setMessage(message) 17 | .setCancelable(false) 18 | .setPositiveButton("Close", 19 | new DialogInterface.OnClickListener() { 20 | public void onClick(DialogInterface dialog, int id) { 21 | } 22 | }); 23 | AlertDialog alert = builder.create(); 24 | alert.show(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | LCD1602Demo 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.source=1.6 5 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_SRC_FILES := $(call all-subdir-java-files) 5 | 6 | LOCAL_PACKAGE_NAME := LCD1602 7 | 8 | LOCAL_CERTIFICATE := platform 9 | LOCAL_MODULE_TAGS := eng 10 | 11 | include $(BUILD_PACKAGE) 12 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ANDROIDPATH=/opt/FriendlyARM/smart4418/android/ 4 | APPNAME=LCD1602 5 | APKNAME=${APPNAME}-r1.0.apk 6 | PRJPATH=$PWD 7 | cd $PRJPATH 8 | rm -rf bin gen 9 | 10 | cd $ANDROIDPATH 11 | rm -f $APKNAME 12 | . setenv 13 | mmm $PRJPATH -B 14 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi2/security/platform.x509.pem vendor/friendly-arm/nanopi2/security/platform.pk8 out/target/product/nanopi2/obj/APPS/${APPNAME}_intermediates/package.apk.unsigned ${APPNAME}-unaligned.apk 15 | zipalign -v 4 ${APPNAME}-unaligned.apk $APKNAME 16 | adb install -r ${APKNAME} 17 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/libs/armeabi/libfriendlyarm-hardware.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/I2C_LCD1602_Demo/libs/armeabi/libfriendlyarm-hardware.so -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-17 15 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/res/drawable-hdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/I2C_LCD1602_Demo/res/drawable-hdpi/icon.png -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/res/drawable-ldpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/I2C_LCD1602_Demo/res/drawable-ldpi/icon.png -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/res/drawable-mdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/I2C_LCD1602_Demo/res/drawable-mdpi/icon.png -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/res/layout/mainactivity.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 9 | 12 | 13 | 16 | 17 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | LCD1602 4 | LCD1602 5 | Apply 6 | 7 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | 3 | public class FileCtlEnum { 4 | /* File Flag */ 5 | public final static int O_ACCMODE = 00000003; 6 | public final static int O_RDONLY = 00000000; 7 | public final static int O_WRONLY = 00000001; 8 | public final static int O_RDWR = 00000002; 9 | public final static int O_CREAT = 00000100; /* not fcntl */ 10 | public final static int O_EXCL = 00000200; /* not fcntl */ 11 | public final static int O_NOCTTY = 00000400; /* not fcntl */ 12 | public final static int O_TRUNC = 00001000; /* not fcntl */ 13 | public final static int O_APPEND = 00002000; 14 | public final static int O_NONBLOCK = 00004000; 15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */ 16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */ 17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */ 18 | public final static int O_LARGEFILE = 00100000; 19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */ 20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */ 21 | public final static int O_NOATIME = 01000000; 22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */ 23 | } 24 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | 3 | /* 4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42) 5 | and have the following read/write attributes: 6 | 7 | /sys/class/gpio/gpioN/ 8 | 9 | "direction" ... reads as either "in" or "out". This value may 10 | normally be written. Writing as "out" defaults to 11 | initializing the value as low. To ensure glitch free 12 | operation, values "low" and "high" may be written to 13 | configure the GPIO as an output with that initial value. 14 | 15 | Note that this attribute *will not exist* if the kernel 16 | doesn't support changing the direction of a GPIO, or 17 | it was exported by kernel code that didn't explicitly 18 | allow userspace to reconfigure this GPIO's direction. 19 | 20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO 21 | is configured as an output, this value may be written; 22 | any nonzero value is treated as high. 23 | 24 | If the pin can be configured as interrupt-generating interrupt 25 | and if it has been configured to generate interrupts (see the 26 | description of "edge"), you can poll(2) on that file and 27 | poll(2) will return whenever the interrupt was triggered. If 28 | you use poll(2), set the events POLLPRI and POLLERR. If you 29 | use select(2), set the file descriptor in exceptfds. After 30 | poll(2) returns, either lseek(2) to the beginning of the sysfs 31 | file and read the new value or close the file and re-open it 32 | to read the value. 33 | */ 34 | 35 | public class GPIOEnum { 36 | //Direction 37 | public final static int IN = 1; 38 | public final static int OUT = 2; 39 | 40 | //Value 41 | public final static int LOW = 0; 42 | public final static int HIGH = 1; 43 | } 44 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/HardwareControler.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | import android.util.Log; 3 | 4 | public class HardwareControler 5 | { 6 | /* File */ 7 | static public native int open(String devName, int flags); 8 | static public native int write(int fd, byte[] data); 9 | static public native int read(int fd, byte[] buf, int len); 10 | static public native int select(int fd, int sec, int usec); 11 | static public native void close(int fd); 12 | static public native int ioctlWithIntValue(int fd, int cmd, int value); 13 | 14 | /* Serial Port */ 15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits ); 16 | static public native int openSerialPortEx( String devName 17 | , long baud 18 | , int dataBits 19 | , int stopBits 20 | , String parityBit 21 | , String flowCtrl 22 | ); 23 | 24 | /* LED */ 25 | static public native int setLedState( int ledID, int ledState ); 26 | 27 | /* PWM */ 28 | static public native int PWMPlay(int frequency); 29 | static public native int PWMStop(); 30 | static public native int PWMPlayEx(int gpioPin, int frequency); 31 | static public native int PWMStopEx(int gpioPin); 32 | 33 | /* ADC */ 34 | static public native int readADC(); 35 | static public native int readADCWithChannel(int channel); 36 | static public native int[] readADCWithChannels(int[] channels); 37 | 38 | /* I2C */ 39 | static public native int setI2CSlave(int fd, int slave); 40 | static public native int setI2CTimeout(int fd, int timeout); 41 | static public native int setI2CRetries(int fd, int retries); 42 | static public native int I2CReadByteData(int fd, int pos, int wait_us); 43 | static public native int I2CReadByte(int fd, int wait_us); 44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us); 45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us); 46 | 47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms); 48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms); 49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms); 50 | 51 | /* SPI */ 52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits ); 53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits ); 54 | static public native int setSPIBitOrder( int spi_fd, int order); 55 | static public native int setSPIClockDivider( int spi_fd, int divider); 56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed); 57 | static public native int setSPIDataMode( int spi_fd, int mode); 58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits); 59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits); 60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits); 61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits); 62 | 63 | /* GPIO */ 64 | static public native int exportGPIOPin(int pin); 65 | static public native int unexportGPIOPin(int pin); 66 | //GPIOEnum.LOW or GPIOEnum.HIGH 67 | static public native int setGPIOValue(int pin, int value); 68 | static public native int getGPIOValue(int pin); 69 | //GPIOEnum.IN or GPIOEnum.OUT 70 | static public native int setGPIODirection(int pin, int direction); 71 | static public native int getGPIODirection(int pin); 72 | 73 | /* OldInterface: for EEPROM */ 74 | static public native int openI2CDevice(); 75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData); 76 | static public native int readByteDataFromI2C(int fd, int pos); 77 | 78 | static public native int getBoardType(); 79 | /* getBoardType return value: */ 80 | public static final int S3C6410_COMMON = 6410; 81 | public static final int S5PV210_COMMON = 210; 82 | public static final int S5P4412_COMMON = 4412; 83 | public static final int S5P4418_BASE = 4418; 84 | public static final int NanoPi2 = S5P4418_BASE+0; 85 | public static final int NanoPC_T2 = S5P4418_BASE+1; 86 | public static final int NanoPi_S2 = S5P4418_BASE+2; 87 | public static final int Smart4418 = S5P4418_BASE+3; 88 | public static final int NanoPi2_Fire = S5P4418_BASE+4; 89 | public static final int NanoPi_M2 = S5P4418_BASE+5; 90 | public static final int NanoPi_M2A = S5P4418_BASE+7; 91 | public static final int Smart4418SDK = S5P4418_BASE+0x103; 92 | public static final int S5P4418_MAX = Smart4418SDK; 93 | public static final int S5P6818_BASE = 6818; 94 | public static final int NanoPC_T3 = S5P6818_BASE+1; 95 | public static final int NanoPi_S3 = S5P6818_BASE+2; 96 | public static final int Smart6818 = S5P6818_BASE+3; 97 | public static final int NanoPi_M3 = S5P6818_BASE+7; 98 | public static final int S5P6818_MAX = NanoPi_M3; 99 | 100 | static public boolean isS5P4418Board() { 101 | int boardtype = getBoardType(); 102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX; 103 | } 104 | 105 | static public boolean isS5P6818Board() { 106 | int boardtype = getBoardType(); 107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX; 108 | } 109 | 110 | static { 111 | try { 112 | System.loadLibrary("friendlyarm-hardware"); 113 | } catch (UnsatisfiedLinkError e) { 114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!"); 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/SPI.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | import android.util.Log; 3 | import android.widget.Toast; 4 | 5 | import com.friendlyarm.AndroidSDK.HardwareControler; 6 | import com.friendlyarm.AndroidSDK.SPIEnum; 7 | import com.friendlyarm.AndroidSDK.GPIOEnum; 8 | import com.friendlyarm.AndroidSDK.FileCtlEnum; 9 | 10 | public class SPI { 11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI"; 12 | private int spi_mode = 0; 13 | private int spi_bits = 8; 14 | private int spi_delay = 0; 15 | private int spi_speed = 500000; 16 | private int spi_byte_order = SPIEnum.LSBFIRST; 17 | 18 | private static final String devName = "/dev/spidev1.0"; 19 | private int spi_fd = -1; 20 | 21 | public void begin() { 22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR ); 23 | if (spi_fd >= 0) { 24 | Log.d(TAG, "open " + devName + "ok!"); 25 | 26 | /* spi init */ 27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits ); 28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits ); 29 | } else { 30 | Log.d(TAG, "open " + devName + "failed!"); 31 | spi_fd = -1; 32 | } 33 | } 34 | 35 | public void end() { 36 | if (spi_fd != -1) { 37 | HardwareControler.close(spi_fd); 38 | spi_fd = -1; 39 | } 40 | } 41 | 42 | public void setBitOrder(int order) { 43 | if (spi_fd < 0) { 44 | return ; 45 | } 46 | spi_byte_order = SPIEnum.MSBFIRST; 47 | if(spi_byte_order == SPIEnum.LSBFIRST) { 48 | spi_mode |= SPIEnum.SPI_LSB_FIRST; 49 | } else { 50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST; 51 | } 52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order ); 53 | 54 | } 55 | 56 | public void setClockDivider(int divider) { 57 | if (spi_fd < 0) { 58 | return ; 59 | } 60 | spi_speed = 66666666/(2*(divider+1)); 61 | if(spi_speed > 500000) { 62 | spi_speed = 500000; 63 | } 64 | HardwareControler.setSPIClockDivider( spi_fd, divider); 65 | } 66 | 67 | public void setDataMode(int mode) { 68 | if (spi_fd < 0) { 69 | return ; 70 | } 71 | switch(mode) 72 | { 73 | case SPIEnum.SPI_MODE0: 74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL); 75 | break; 76 | case SPIEnum.SPI_MODE1: 77 | spi_mode &= ~(SPIEnum.SPI_CPOL); 78 | spi_mode |= (SPIEnum.SPI_CPHA); 79 | break; 80 | case SPIEnum.SPI_MODE2: 81 | spi_mode |= (SPIEnum.SPI_CPOL); 82 | spi_mode &= ~(SPIEnum.SPI_CPHA); 83 | break; 84 | case SPIEnum.SPI_MODE3: 85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL); 86 | break; 87 | default: 88 | Log.e(TAG, "error data mode"); 89 | } 90 | 91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode ); 92 | } 93 | 94 | public void setChipSelectPolarity(int cs, int active) { 95 | 96 | } 97 | 98 | public void chipSelect(int cs) { 99 | 100 | } 101 | 102 | public byte transfer(int value) { 103 | if (spi_fd < 0) { 104 | return 0; 105 | } 106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/src/com/friendlyarm/AndroidSDK/SPIEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | public class SPIEnum { 3 | 4 | // SPIBitOrder 5 | public final static int LSBFIRST = 0; ///< LSB First 6 | public final static int MSBFIRST = 1; ///< MSB First 7 | 8 | // SPIMode 9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0 10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1 11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0 12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1 13 | 14 | 15 | public final static int SPI_CPHA = 0x01; 16 | public final static int SPI_CPOL = 0x02; 17 | public final static int SPI_CS_HIGH = 0x04; 18 | public final static int SPI_LSB_FIRST = 0x08; 19 | public final static int SPI_3WIRE = 0x10; 20 | public final static int SPI_LOOP = 0x20; 21 | public final static int SPI_NO_CS = 0x40; 22 | public final static int SPI_READY = 0x80; 23 | 24 | // SPIClockDivider 25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz 26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz 27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz 28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz 29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz 30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz 31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz 32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz 33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz 34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz 35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz 36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz 37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz 38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz 39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz 40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz 41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz 42 | } -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/src/com/friendlyarm/LCD1602Demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.LCD1602Demo; 2 | 3 | import android.app.Activity; 4 | import android.view.View; 5 | import android.view.View.OnClickListener; 6 | import android.widget.Button; 7 | import android.widget.EditText; 8 | import android.widget.TextView; 9 | import android.os.Bundle; 10 | 11 | import com.friendlyarm.AndroidSDK.HardwareControler; 12 | import com.friendlyarm.AndroidSDK.FileCtlEnum; 13 | import com.friendlyarm.LCD1602Demo.R; 14 | /* for thread */ 15 | import android.os.Message; 16 | import android.os.Handler; 17 | import android.os.Looper; 18 | 19 | public class MainActivity extends Activity implements OnClickListener { 20 | EditText writeEditor; 21 | TextView statusView; 22 | private Handler messageHandler; 23 | Thread writeThread = null; 24 | int devFD = -1; 25 | final int MAX_LINE_SIZE = 16; 26 | 27 | private class MessageHandler extends Handler { 28 | public MessageHandler(Looper looper) { 29 | super(looper); 30 | } 31 | 32 | @Override 33 | public void handleMessage(Message msg) { 34 | String result = (String) msg.obj; 35 | if (result.equals("ERROR")) { 36 | statusView.setText("Status: Error"); 37 | } else if (result.equals("DONE")) { 38 | statusView.setText("Status: Done"); 39 | } else { 40 | statusView.setText("Status: Processing"); 41 | } 42 | } 43 | } 44 | 45 | @Override 46 | public void onDestroy() { 47 | if (devFD >= 0) { 48 | HardwareControler.close(devFD); 49 | devFD = -1; 50 | } 51 | super.onDestroy(); 52 | } 53 | 54 | @Override 55 | public void onCreate(Bundle savedInstanceState) { 56 | super.onCreate(savedInstanceState); 57 | setContentView(R.layout.mainactivity); 58 | 59 | writeEditor = (EditText) findViewById(R.id.writeEditor); 60 | writeEditor.setText("Don't give up and don't give in."); 61 | 62 | statusView = (TextView) findViewById(R.id.statusView); 63 | Button writeButton = (Button)findViewById(R.id.sendButton); 64 | writeButton.setOnClickListener(this); 65 | 66 | Looper looper = Looper.myLooper(); 67 | messageHandler = new MessageHandler(looper); 68 | 69 | devFD = HardwareControler.open("/dev/i2c-0", FileCtlEnum.O_RDWR); 70 | if (devFD < 0) { 71 | statusView.setText("Fail to open I2C device."); 72 | writeEditor.setEnabled(false); 73 | writeButton.setEnabled(false); 74 | } else { 75 | // for LCD1602 (chip: pcf8574) 76 | if (HardwareControler.setI2CSlave(devFD, 0x27) < 0) { 77 | statusView.setText("Fail to set I2C slave."); 78 | writeEditor.setEnabled(false); 79 | writeButton.setEnabled(false); 80 | } else { 81 | statusView.setText("Status: Ready"); 82 | } 83 | } 84 | } 85 | 86 | private int LCD1602Init() throws InterruptedException { 87 | Thread.sleep(0,1000*15); 88 | if (PCF8574.writeCmd4(devFD, (byte)(0x03 << 4)) == -1) { 89 | return -1; 90 | } 91 | Thread.sleep(0,100*41); 92 | if (PCF8574.writeCmd4(devFD, (byte)(0x03 << 4)) == -1) { 93 | return -1; 94 | } 95 | Thread.sleep(0,100); 96 | if (PCF8574.writeCmd4(devFD, (byte)(0x03 << 4)) == -1) { 97 | return -1; 98 | } 99 | if (PCF8574.writeCmd4(devFD, (byte)(0x02 << 4)) == -1) { 100 | return -1; 101 | } 102 | if (PCF8574.writeCmd8(devFD, (byte)(0x28)) == -1) { 103 | return -1; 104 | } 105 | if (PCF8574.writeCmd8(devFD, (byte)(0x0c)) == -1) { 106 | return -1; 107 | } 108 | Thread.sleep(0,2000); 109 | if (PCF8574.writeCmd8(devFD, (byte)(0x06)) == -1) { 110 | return -1; 111 | } 112 | if (PCF8574.writeCmd8(devFD, (byte)(0x02)) == -1) { 113 | return -1; 114 | } 115 | Thread.sleep(0,2000); 116 | return 0; 117 | } 118 | 119 | private int LCD1602DispStr(byte x, byte y, String str) throws InterruptedException 120 | { 121 | byte addr; 122 | addr = (byte)(((y + 2) * 0x40) + x); 123 | 124 | if (PCF8574.writeCmd8(devFD, addr) == -1) { 125 | return -1; 126 | } 127 | byte[] strBytes = str.getBytes(); 128 | byte b; 129 | 130 | for (int i = 0; i < strBytes.length && i 0) { 142 | ret = LCD1602DispStr((byte)0, (byte)1, line2); 143 | } 144 | return ret; 145 | } 146 | 147 | private int LCD1602Clear() throws InterruptedException { 148 | if (PCF8574.writeCmd8(devFD, (byte)0x01) == -1) { 149 | return -1; 150 | } 151 | return 0; 152 | } 153 | 154 | private boolean lcd1602Inited = false; 155 | private void sendMessage(String msg) { 156 | Message message = Message.obtain(); 157 | message.obj = msg; 158 | messageHandler.sendMessage(message); 159 | } 160 | private void startWriteEEPROMThread(final String displayText) { 161 | statusView.setText("Status: Processing"); 162 | new Thread() { 163 | @Override 164 | public void run() { 165 | if (!lcd1602Inited) { 166 | try { 167 | if (LCD1602Init() == 0) { 168 | lcd1602Inited = true; 169 | } 170 | } catch (Exception e) { 171 | sendMessage("ERROR"); 172 | return ; 173 | } 174 | } 175 | 176 | if (!lcd1602Inited) { 177 | sendMessage("ERROR"); 178 | return ; 179 | } 180 | 181 | try { 182 | LCD1602Clear(); 183 | Thread.sleep(100, 0); 184 | 185 | String line1 = ""; 186 | String line2 = ""; 187 | for (int i=0; i= MAX_LINE_SIZE) { 189 | if (line2.length() >= MAX_LINE_SIZE) { 190 | break; 191 | } else { 192 | line2 = line2 + displayText.charAt(i); 193 | } 194 | } else { 195 | line1 = line1 + displayText.charAt(i); 196 | } 197 | } 198 | 199 | if (LCD1602DispLines(line1, line2) == -1) { 200 | sendMessage("ERROR"); 201 | return ; 202 | } 203 | } catch (Exception e) { 204 | sendMessage("ERROR"); 205 | return ; 206 | } 207 | sendMessage("DONE"); 208 | } 209 | 210 | }.start(); 211 | } 212 | 213 | public void onClick(View v) { 214 | switch (v.getId()) { 215 | case R.id.sendButton: 216 | startWriteEEPROMThread(writeEditor.getText().toString()); 217 | break; 218 | default: 219 | break; 220 | } 221 | } 222 | 223 | } 224 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/src/com/friendlyarm/LCD1602Demo/PCF8574.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.LCD1602Demo; 2 | 3 | import com.friendlyarm.AndroidSDK.HardwareControler; 4 | 5 | public class PCF8574 { 6 | static final byte RS = 0x01; 7 | static final byte En = 0x04; 8 | static final byte BL = 0x08; 9 | 10 | static int pulseEnable(int devFD, byte data) throws InterruptedException 11 | { 12 | if (HardwareControler.I2CWriteByte(devFD, (byte)(data | En), 1) != 0) { 13 | return -1; 14 | } 15 | if (HardwareControler.I2CWriteByte(devFD, (byte)(data & ~En), 0) != 0) { 16 | return -1; 17 | } 18 | return 0; 19 | } 20 | 21 | static int writeCmd4(int devFD, byte command) throws InterruptedException 22 | { 23 | if (HardwareControler.I2CWriteByte(devFD, (byte)(command | BL), 0) != 0) { 24 | return -1; 25 | } 26 | if (pulseEnable(devFD, (byte)(command | BL)) != 0) { 27 | return -1; 28 | } 29 | return 0; 30 | } 31 | 32 | static int writeCmd8(int devFD, byte command) throws InterruptedException 33 | { 34 | byte command_H = (byte) (command & 0xf0); 35 | byte command_L = (byte) ((command << 4) & 0xf0); 36 | if (writeCmd4(devFD, command_H) == -1) { 37 | return -1; 38 | } 39 | 40 | if (writeCmd4(devFD, command_L) == -1) { 41 | return -1; 42 | } 43 | return 0; 44 | } 45 | 46 | static int writeData4(int devFD, byte data) throws InterruptedException 47 | { 48 | if (HardwareControler.I2CWriteByte(devFD, (byte)(data | RS | BL), 0) != 0) { 49 | return -1; 50 | } 51 | if (pulseEnable(devFD, (byte)(data | RS | BL)) == -1) { 52 | return -1; 53 | } 54 | return 0; 55 | } 56 | 57 | static int writeData8(int devFD, byte data) throws InterruptedException 58 | { 59 | byte data_H = (byte) (data & 0xf0); 60 | byte data_L = (byte) ((data << 4) & 0xf0); 61 | if (writeData4(devFD, data_H) == -1) { 62 | return -1; 63 | } 64 | if (writeData4(devFD, data_L) == -1) { 65 | return -1; 66 | } 67 | return 0; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /I2C_LCD1602_Demo/src/com/friendlyarm/Utils/CommonFuncs.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.Utils; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.widget.Toast; 6 | import android.app.Activity; 7 | import android.os.Bundle; 8 | import android.os.Handler; 9 | import android.os.Message; 10 | import android.content.Context; 11 | import android.content.DialogInterface; 12 | 13 | public class CommonFuncs { 14 | public static void showAlertDialog(Context context, String message) { 15 | AlertDialog.Builder builder = new AlertDialog.Builder(context); 16 | builder.setMessage(message) 17 | .setCancelable(false) 18 | .setPositiveButton("Close", 19 | new DialogInterface.OnClickListener() { 20 | public void onClick(DialogInterface dialog, int id) { 21 | } 22 | }); 23 | AlertDialog alert = builder.create(); 24 | alert.show(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /PWMDemo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PWMDemo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | PWMTestingActivity 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /PWMDemo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.source=1.6 5 | -------------------------------------------------------------------------------- /PWMDemo/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_SRC_FILES := $(call all-subdir-java-files) 5 | 6 | LOCAL_PACKAGE_NAME := PWM 7 | 8 | LOCAL_CERTIFICATE := platform 9 | LOCAL_MODULE_TAGS := eng 10 | 11 | include $(BUILD_PACKAGE) 12 | -------------------------------------------------------------------------------- /PWMDemo/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /PWMDemo/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ANDROIDPATH=/opt/FriendlyARM/smart4418/android/ 4 | APPNAME=PWM 5 | APKNAME=${APPNAME}-r1.0.apk 6 | PRJPATH=$PWD 7 | cd $PRJPATH 8 | rm -rf bin gen 9 | 10 | cd $ANDROIDPATH 11 | rm -f $APKNAME 12 | . setenv 13 | mmm $PRJPATH -B 14 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi2/security/platform.x509.pem vendor/friendly-arm/nanopi2/security/platform.pk8 out/target/product/nanopi2/obj/APPS/${APPNAME}_intermediates/package.apk.unsigned ${APPNAME}-unaligned.apk 15 | zipalign -v 4 ${APPNAME}-unaligned.apk $APKNAME 16 | adb install -r ${APKNAME} 17 | -------------------------------------------------------------------------------- /PWMDemo/libs/armeabi/libfriendlyarm-hardware.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/PWMDemo/libs/armeabi/libfriendlyarm-hardware.so -------------------------------------------------------------------------------- /PWMDemo/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-17 15 | -------------------------------------------------------------------------------- /PWMDemo/res/drawable/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/PWMDemo/res/drawable/icon.png -------------------------------------------------------------------------------- /PWMDemo/res/layout/pwmtestingactivity.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 7 | 10 | 11 | 13 | 14 | 18 | 19 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /PWMDemo/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PWM Demo 4 | PWM Testing: /dev/pwm 5 | + 6 | - 7 | 1000 8 | Frequency: 9 | Start 10 | Stop 11 | Back 12 | 13 | -------------------------------------------------------------------------------- /PWMDemo/src/com/friendlyarm/AndroidSDK/FileCtlEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | 3 | public class FileCtlEnum { 4 | /* File Flag */ 5 | public final static int O_ACCMODE = 00000003; 6 | public final static int O_RDONLY = 00000000; 7 | public final static int O_WRONLY = 00000001; 8 | public final static int O_RDWR = 00000002; 9 | public final static int O_CREAT = 00000100; /* not fcntl */ 10 | public final static int O_EXCL = 00000200; /* not fcntl */ 11 | public final static int O_NOCTTY = 00000400; /* not fcntl */ 12 | public final static int O_TRUNC = 00001000; /* not fcntl */ 13 | public final static int O_APPEND = 00002000; 14 | public final static int O_NONBLOCK = 00004000; 15 | public final static int O_DSYNC = 00010000; /* used to be O_SYNC, see below */ 16 | public final static int FASYNC = 00020000; /* fcntl, for BSD compatibility */ 17 | public final static int O_DIRECT = 00040000; /* direct disk access hint */ 18 | public final static int O_LARGEFILE = 00100000; 19 | public final static int O_DIRECTORY = 00200000; /* must be a directory */ 20 | public final static int O_NOFOLLOW = 00400000; /* don't follow links */ 21 | public final static int O_NOATIME = 01000000; 22 | public final static int O_CLOEXEC = 02000000; /* set close_on_exec */ 23 | } 24 | -------------------------------------------------------------------------------- /PWMDemo/src/com/friendlyarm/AndroidSDK/GPIOEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | 3 | /* 4 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42) 5 | and have the following read/write attributes: 6 | 7 | /sys/class/gpio/gpioN/ 8 | 9 | "direction" ... reads as either "in" or "out". This value may 10 | normally be written. Writing as "out" defaults to 11 | initializing the value as low. To ensure glitch free 12 | operation, values "low" and "high" may be written to 13 | configure the GPIO as an output with that initial value. 14 | 15 | Note that this attribute *will not exist* if the kernel 16 | doesn't support changing the direction of a GPIO, or 17 | it was exported by kernel code that didn't explicitly 18 | allow userspace to reconfigure this GPIO's direction. 19 | 20 | "value" ... reads as either 0 (low) or 1 (high). If the GPIO 21 | is configured as an output, this value may be written; 22 | any nonzero value is treated as high. 23 | 24 | If the pin can be configured as interrupt-generating interrupt 25 | and if it has been configured to generate interrupts (see the 26 | description of "edge"), you can poll(2) on that file and 27 | poll(2) will return whenever the interrupt was triggered. If 28 | you use poll(2), set the events POLLPRI and POLLERR. If you 29 | use select(2), set the file descriptor in exceptfds. After 30 | poll(2) returns, either lseek(2) to the beginning of the sysfs 31 | file and read the new value or close the file and re-open it 32 | to read the value. 33 | */ 34 | 35 | public class GPIOEnum { 36 | //Direction 37 | public final static int IN = 1; 38 | public final static int OUT = 2; 39 | 40 | //Value 41 | public final static int LOW = 0; 42 | public final static int HIGH = 1; 43 | } 44 | -------------------------------------------------------------------------------- /PWMDemo/src/com/friendlyarm/AndroidSDK/HardwareControler.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | import android.util.Log; 3 | 4 | public class HardwareControler 5 | { 6 | /* File */ 7 | static public native int open(String devName, int flags); 8 | static public native int write(int fd, byte[] data); 9 | static public native int read(int fd, byte[] buf, int len); 10 | static public native int select(int fd, int sec, int usec); 11 | static public native void close(int fd); 12 | static public native int ioctlWithIntValue(int fd, int cmd, int value); 13 | 14 | /* Serial Port */ 15 | static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits ); 16 | static public native int openSerialPortEx( String devName 17 | , long baud 18 | , int dataBits 19 | , int stopBits 20 | , String parityBit 21 | , String flowCtrl 22 | ); 23 | 24 | /* LED */ 25 | static public native int setLedState( int ledID, int ledState ); 26 | 27 | /* PWM */ 28 | static public native int PWMPlay(int frequency); 29 | static public native int PWMStop(); 30 | static public native int PWMPlayEx(int gpioPin, int frequency); 31 | static public native int PWMStopEx(int gpioPin); 32 | 33 | /* ADC */ 34 | static public native int readADC(); 35 | static public native int readADCWithChannel(int channel); 36 | static public native int[] readADCWithChannels(int[] channels); 37 | 38 | /* I2C */ 39 | static public native int setI2CSlave(int fd, int slave); 40 | static public native int setI2CTimeout(int fd, int timeout); 41 | static public native int setI2CRetries(int fd, int retries); 42 | static public native int I2CReadByteData(int fd, int pos, int wait_us); 43 | static public native int I2CReadByte(int fd, int wait_us); 44 | static public native int I2CWriteByteData(int fd, int pos, byte byteData, int wait_us); 45 | static public native int I2CWriteByte(int fd, byte byteData, int wait_us); 46 | 47 | /* Discard */ static public native int readByteFromI2C(int fd, int pos, int wait_ms); 48 | /* Discard */ static public native int writeByteToI2C(int fd, int pos, byte byteData, int wait_ms); 49 | /* Discard */ static public native int writeByteToI2C2(int fd, byte byteData, int wait_ms); 50 | 51 | /* SPI */ 52 | static public native int setSPIWriteBitsPerWord( int spi_fd, int bits ); 53 | static public native int setSPIReadBitsPerWord( int spi_fd, int bits ); 54 | static public native int setSPIBitOrder( int spi_fd, int order); 55 | static public native int setSPIClockDivider( int spi_fd, int divider); 56 | static public native int setSPIMaxSpeed( int spi_fd, int spi_speed); 57 | static public native int setSPIDataMode( int spi_fd, int mode); 58 | static public native int SPItransferOneByte( int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits); 59 | static public native int SPItransferBytes(int spi_fd, byte[] writeData, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits); 60 | static public native int writeBytesToSPI(int spi_fd, byte[] writeData, int spi_delay, int spi_speed, int spi_bits); 61 | static public native int readBytesFromSPI(int spi_fd, byte[] readBuff, int spi_delay, int spi_speed, int spi_bits); 62 | 63 | /* GPIO */ 64 | static public native int exportGPIOPin(int pin); 65 | static public native int unexportGPIOPin(int pin); 66 | //GPIOEnum.LOW or GPIOEnum.HIGH 67 | static public native int setGPIOValue(int pin, int value); 68 | static public native int getGPIOValue(int pin); 69 | //GPIOEnum.IN or GPIOEnum.OUT 70 | static public native int setGPIODirection(int pin, int direction); 71 | static public native int getGPIODirection(int pin); 72 | 73 | /* OldInterface: for EEPROM */ 74 | static public native int openI2CDevice(); 75 | static public native int writeByteDataToI2C(int fd, int pos, byte byteData); 76 | static public native int readByteDataFromI2C(int fd, int pos); 77 | 78 | static public native int getBoardType(); 79 | /* getBoardType return value: */ 80 | public static final int S3C6410_COMMON = 6410; 81 | public static final int S5PV210_COMMON = 210; 82 | public static final int S5P4412_COMMON = 4412; 83 | public static final int S5P4418_BASE = 4418; 84 | public static final int NanoPi2 = S5P4418_BASE+0; 85 | public static final int NanoPC_T2 = S5P4418_BASE+1; 86 | public static final int NanoPi_S2 = S5P4418_BASE+2; 87 | public static final int Smart4418 = S5P4418_BASE+3; 88 | public static final int NanoPi2_Fire = S5P4418_BASE+4; 89 | public static final int NanoPi_M2 = S5P4418_BASE+5; 90 | public static final int NanoPi_M2A = S5P4418_BASE+7; 91 | public static final int Smart4418SDK = S5P4418_BASE+0x103; 92 | public static final int S5P4418_MAX = Smart4418SDK; 93 | public static final int S5P6818_BASE = 6818; 94 | public static final int NanoPC_T3 = S5P6818_BASE+1; 95 | public static final int NanoPi_S3 = S5P6818_BASE+2; 96 | public static final int Smart6818 = S5P6818_BASE+3; 97 | public static final int NanoPi_M3 = S5P6818_BASE+7; 98 | public static final int S5P6818_MAX = NanoPi_M3; 99 | 100 | static public boolean isS5P4418Board() { 101 | int boardtype = getBoardType(); 102 | return boardtype>=S5P4418_BASE && boardtype<=S5P4418_MAX; 103 | } 104 | 105 | static public boolean isS5P6818Board() { 106 | int boardtype = getBoardType(); 107 | return boardtype>=S5P6818_BASE && boardtype<=S5P6818_MAX; 108 | } 109 | 110 | static { 111 | try { 112 | System.loadLibrary("friendlyarm-hardware"); 113 | } catch (UnsatisfiedLinkError e) { 114 | Log.d("HardwareControler", "libfriendlyarm-hardware library not found!"); 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /PWMDemo/src/com/friendlyarm/AndroidSDK/SPI.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | import android.util.Log; 3 | import android.widget.Toast; 4 | 5 | import com.friendlyarm.AndroidSDK.HardwareControler; 6 | import com.friendlyarm.AndroidSDK.SPIEnum; 7 | import com.friendlyarm.AndroidSDK.GPIOEnum; 8 | import com.friendlyarm.AndroidSDK.FileCtlEnum; 9 | 10 | public class SPI { 11 | private static final String TAG = "com.friendlyarm.AndroidSDK.SPI"; 12 | private int spi_mode = 0; 13 | private int spi_bits = 8; 14 | private int spi_delay = 0; 15 | private int spi_speed = 500000; 16 | private int spi_byte_order = SPIEnum.LSBFIRST; 17 | 18 | private static final String devName = "/dev/spidev1.0"; 19 | private int spi_fd = -1; 20 | 21 | public void begin() { 22 | spi_fd = HardwareControler.open( devName, FileCtlEnum.O_RDWR ); 23 | if (spi_fd >= 0) { 24 | Log.d(TAG, "open " + devName + "ok!"); 25 | 26 | /* spi init */ 27 | HardwareControler.setSPIWriteBitsPerWord( spi_fd, spi_bits ); 28 | HardwareControler.setSPIReadBitsPerWord( spi_fd, spi_bits ); 29 | } else { 30 | Log.d(TAG, "open " + devName + "failed!"); 31 | spi_fd = -1; 32 | } 33 | } 34 | 35 | public void end() { 36 | if (spi_fd != -1) { 37 | HardwareControler.close(spi_fd); 38 | spi_fd = -1; 39 | } 40 | } 41 | 42 | public void setBitOrder(int order) { 43 | if (spi_fd < 0) { 44 | return ; 45 | } 46 | spi_byte_order = SPIEnum.MSBFIRST; 47 | if(spi_byte_order == SPIEnum.LSBFIRST) { 48 | spi_mode |= SPIEnum.SPI_LSB_FIRST; 49 | } else { 50 | spi_mode &= ~SPIEnum.SPI_LSB_FIRST; 51 | } 52 | HardwareControler.setSPIBitOrder( spi_fd, spi_byte_order ); 53 | 54 | } 55 | 56 | public void setClockDivider(int divider) { 57 | if (spi_fd < 0) { 58 | return ; 59 | } 60 | spi_speed = 66666666/(2*(divider+1)); 61 | if(spi_speed > 500000) { 62 | spi_speed = 500000; 63 | } 64 | HardwareControler.setSPIClockDivider( spi_fd, divider); 65 | } 66 | 67 | public void setDataMode(int mode) { 68 | if (spi_fd < 0) { 69 | return ; 70 | } 71 | switch(mode) 72 | { 73 | case SPIEnum.SPI_MODE0: 74 | spi_mode &= ~(SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL); 75 | break; 76 | case SPIEnum.SPI_MODE1: 77 | spi_mode &= ~(SPIEnum.SPI_CPOL); 78 | spi_mode |= (SPIEnum.SPI_CPHA); 79 | break; 80 | case SPIEnum.SPI_MODE2: 81 | spi_mode |= (SPIEnum.SPI_CPOL); 82 | spi_mode &= ~(SPIEnum.SPI_CPHA); 83 | break; 84 | case SPIEnum.SPI_MODE3: 85 | spi_mode |= (SPIEnum.SPI_CPHA|SPIEnum.SPI_CPOL); 86 | break; 87 | default: 88 | Log.e(TAG, "error data mode"); 89 | } 90 | 91 | HardwareControler.setSPIDataMode( spi_fd, spi_mode ); 92 | } 93 | 94 | public void setChipSelectPolarity(int cs, int active) { 95 | 96 | } 97 | 98 | public void chipSelect(int cs) { 99 | 100 | } 101 | 102 | public byte transfer(int value) { 103 | if (spi_fd < 0) { 104 | return 0; 105 | } 106 | return (byte) HardwareControler.SPItransferOneByte(spi_fd, (byte) value, spi_delay, spi_speed, spi_bits); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /PWMDemo/src/com/friendlyarm/AndroidSDK/SPIEnum.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.AndroidSDK; 2 | public class SPIEnum { 3 | 4 | // SPIBitOrder 5 | public final static int LSBFIRST = 0; ///< LSB First 6 | public final static int MSBFIRST = 1; ///< MSB First 7 | 8 | // SPIMode 9 | public final static int SPI_MODE0 = 0; ///< CPOL = 0, CPHA = 0 10 | public final static int SPI_MODE1 = 1; ///< CPOL = 0, CPHA = 1 11 | public final static int SPI_MODE2 = 2; ///< CPOL = 1, CPHA = 0 12 | public final static int SPI_MODE3 = 3; ///< CPOL = 1, CPHA = 1 13 | 14 | 15 | public final static int SPI_CPHA = 0x01; 16 | public final static int SPI_CPOL = 0x02; 17 | public final static int SPI_CS_HIGH = 0x04; 18 | public final static int SPI_LSB_FIRST = 0x08; 19 | public final static int SPI_3WIRE = 0x10; 20 | public final static int SPI_LOOP = 0x20; 21 | public final static int SPI_NO_CS = 0x40; 22 | public final static int SPI_READY = 0x80; 23 | 24 | // SPIClockDivider 25 | public final static int SPI_CLOCK_DIV65536 = 0; ///< 65536 = 256us = 4kHz 26 | public final static int SPI_CLOCK_DIV32768 = 32768; ///< 32768 = 126us = 8kHz 27 | public final static int SPI_CLOCK_DIV16384 = 16384; ///< 16384 = 64us = 15.625kHz 28 | public final static int SPI_CLOCK_DIV8192 = 8192; ///< 8192 = 32us = 31.25kHz 29 | public final static int SPI_CLOCK_DIV4096 = 4096; ///< 4096 = 16us = 62.5kHz 30 | public final static int SPI_CLOCK_DIV2048 = 2048; ///< 2048 = 8us = 125kHz 31 | public final static int SPI_CLOCK_DIV1024 = 1024; ///< 1024 = 4us = 250kHz 32 | public final static int SPI_CLOCK_DIV512 = 512; ///< 512 = 2us = 500kHz 33 | public final static int SPI_CLOCK_DIV256 = 256; ///< 256 = 1us = 1MHz 34 | public final static int SPI_CLOCK_DIV128 = 128; ///< 128 = 500ns = = 2MHz 35 | public final static int SPI_CLOCK_DIV64 = 64; ///< 64 = 250ns = 4MHz 36 | public final static int SPI_CLOCK_DIV32 = 32; ///< 32 = 125ns = 8MHz 37 | public final static int SPI_CLOCK_DIV16 = 16; ///< 16 = 50ns = 20MHz 38 | public final static int SPI_CLOCK_DIV8 = 8; ///< 8 = 25ns = 40MHz 39 | public final static int SPI_CLOCK_DIV4 = 4; ///< 4 = 12.5ns 80MHz 40 | public final static int SPI_CLOCK_DIV2 = 2; ///< 2 = 6.25ns = 160MHz 41 | public final static int SPI_CLOCK_DIV1 = 1; ///< 0 = 256us = 4kHz 42 | } -------------------------------------------------------------------------------- /PWMDemo/src/com/friendlyarm/PWMDemo/PWMTestingActivity.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.PWMDemo; 2 | 3 | import com.friendlyarm.AndroidSDK.HardwareControler; 4 | import com.friendlyarm.PWMDemo.R; 5 | import com.friendlyarm.Utils.CommonFuncs; 6 | 7 | import android.app.Activity; 8 | import android.content.res.Configuration; 9 | import android.os.Bundle; 10 | import android.widget.TextView; 11 | import android.util.Log; 12 | import android.view.View; 13 | import android.widget.Button; 14 | import android.widget.EditText; 15 | import android.view.View.OnClickListener; 16 | 17 | public class PWMTestingActivity extends Activity implements OnClickListener { 18 | HardwareControler hw; 19 | EditText textEditor; 20 | Button btnSub; 21 | Button btnAdd; 22 | 23 | final int smart4418PWMGPIOPin = 97; //GPIOD1, Smart4418/NanoPC-T2/NanoPC-T3 24 | 25 | /** Called when the activity is first created. */ 26 | @Override 27 | public void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.pwmtestingactivity); 30 | 31 | btnSub = ((Button)findViewById(R.id.btnSub)); 32 | btnAdd = ((Button)findViewById(R.id.btnAdd)); 33 | 34 | btnSub.setOnClickListener(this); 35 | btnAdd.setOnClickListener(this); 36 | 37 | textEditor = ((EditText)findViewById(R.id.fEditor)); 38 | hw = new HardwareControler(); 39 | } 40 | 41 | private void playPWM() 42 | { 43 | int f; 44 | f = Integer.valueOf(textEditor.getText().toString()).intValue(); 45 | int boardType = HardwareControler.getBoardType(); 46 | Log.d("PWMDemo", "boardtype = " + boardType); 47 | 48 | if (HardwareControler.isS5P4418Board() || HardwareControler.isS5P6818Board()) { 49 | if (hw.PWMPlayEx(smart4418PWMGPIOPin, f) != 0) { 50 | CommonFuncs.showAlertDialog(this,"Fail to play!"); 51 | } 52 | } else { 53 | if (hw.PWMPlay(f) != 0) { 54 | CommonFuncs.showAlertDialog(this,"Fail to play!"); 55 | } 56 | } 57 | } 58 | 59 | private void stopPWM() 60 | { 61 | if (HardwareControler.isS5P4418Board() || HardwareControler.isS5P6818Board()) { 62 | if (hw.PWMStopEx(smart4418PWMGPIOPin) != 0) { 63 | CommonFuncs.showAlertDialog(this,"Fail to stop!"); 64 | } 65 | } else { 66 | if (hw.PWMStop() != 0) { 67 | CommonFuncs.showAlertDialog(this,"Fail to stop!"); 68 | } 69 | } 70 | } 71 | 72 | public void onClick(View v) { 73 | int f; 74 | switch (v.getId()) { 75 | case R.id.btnSub: 76 | f = Integer.valueOf(textEditor.getText().toString()).intValue() - 100; 77 | if (f < 1) { 78 | f = 1; 79 | } 80 | textEditor.setText( "" + f ); 81 | playPWM(); 82 | break; 83 | case R.id.btnAdd: 84 | f = Integer.valueOf(textEditor.getText().toString()).intValue() + 100; 85 | if (f < 1) { 86 | f = 1; 87 | } 88 | textEditor.setText( "" + f ); 89 | playPWM(); 90 | break; 91 | default: 92 | break; 93 | } 94 | } 95 | 96 | @Override 97 | public void onStart() { 98 | super.onStart(); 99 | } 100 | 101 | @Override 102 | public void onRestart() { 103 | super.onRestart(); 104 | } 105 | 106 | @Override 107 | public void onResume() { 108 | super.onResume(); 109 | playPWM(); 110 | } 111 | 112 | @Override 113 | public void onPause() { 114 | stopPWM(); 115 | super.onPause(); 116 | } 117 | 118 | @Override 119 | public void onStop() { 120 | stopPWM(); 121 | super.onStop(); 122 | } 123 | 124 | @Override 125 | public void onDestroy() { 126 | stopPWM(); 127 | super.onDestroy(); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /PWMDemo/src/com/friendlyarm/Utils/BorderScrollView.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.Utils; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Paint; 6 | import android.util.AttributeSet; 7 | import android.widget.ScrollView; 8 | 9 | 10 | public class BorderScrollView extends ScrollView 11 | { 12 | @Override 13 | protected void onDraw(Canvas canvas) 14 | { 15 | super.onDraw(canvas); 16 | Paint paint = new Paint(); 17 | paint.setColor(android.graphics.Color.GRAY); 18 | canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint); 19 | canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint); 20 | canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint); 21 | canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint); 22 | } 23 | public BorderScrollView(Context context, AttributeSet attrs) 24 | { 25 | super(context, attrs); 26 | } 27 | } -------------------------------------------------------------------------------- /PWMDemo/src/com/friendlyarm/Utils/CommonFuncs.java: -------------------------------------------------------------------------------- 1 | package com.friendlyarm.Utils; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.widget.Toast; 6 | import android.app.Activity; 7 | import android.os.Bundle; 8 | import android.os.Handler; 9 | import android.os.Message; 10 | import android.content.Context; 11 | import android.content.DialogInterface; 12 | 13 | public class CommonFuncs { 14 | public static void showAlertDialog(Context context, String message) { 15 | AlertDialog.Builder builder = new AlertDialog.Builder(context); 16 | builder.setMessage(message) 17 | .setCancelable(false) 18 | .setPositiveButton("Close", 19 | new DialogInterface.OnClickListener() { 20 | public void onClick(DialogInterface dialog, int id) { 21 | } 22 | }); 23 | AlertDialog alert = builder.create(); 24 | alert.show(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidHardwareAccess 2 | Examples for accessing FriendlyELEC's development board hardware on android. 3 | 4 | ## Applicable Boards & OS 5 | 6 | * NanoPC-T3/NanoPi-M3: Android 5 7 | * Smart4418 SDK/NanoPC-T2/NanoPi-M2/NanoPi S2: Android 5, Android 4.4 8 | * Tiny4412: Android 4.2, Android 5 9 | * Tiny210/Smart210/Mini210: Android 4.2 10 | * Tiny6410/Mini6410: Android 2.3 11 | 12 | ## Accessible Interfaces & Ports 13 | 14 | * Serial Port 15 | * PWM 16 | * EEPROM 17 | * ADC 18 | * LED 19 | * LCD 1602 (I2C) 20 | * OLED (SPI) 21 | 22 | I2C, SPI, GPIO are accessible. 23 | 24 | ![image](https://github.com/friendlyarm/AndroidHardwareAccess/raw/master/SPI_OLED_Demo/Smart4418SDK%2BOLED.png) 25 | 26 | ## Document 27 | [WiKi](http://wiki.friendlyarm.com/wiki/index.php/Android_Thing:_Android_Hardware_Programming) 28 | [Guide to API in Chinese](https://github.com/friendlyarm/AndroidHardwareAccess/blob/master/%E5%8F%8B%E5%96%84%E7%94%B5%E5%AD%90Android%E7%A1%AC%E4%BB%B6%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97.pdf) 29 | -------------------------------------------------------------------------------- /SDCardDemo/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_MODULE_TAGS := eng 5 | 6 | 7 | LOCAL_SRC_FILES := \ 8 | $(call all-java-files-under, src) 9 | 10 | LOCAL_PACKAGE_NAME := SDCardDemo 11 | 12 | include $(BUILD_PACKAGE) 13 | 14 | # additionally, build tests in sub-folders in a separate .apk 15 | include $(call all-makefiles-under,$(LOCAL_PATH)) 16 | -------------------------------------------------------------------------------- /SDCardDemo/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 17 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /SDCardDemo/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ANDROIDPATH=/opt/FriendlyARM/smart4418/android/ 4 | APPNAME=SDCardDemo 5 | APKNAME=${APPNAME}-r1.0.apk 6 | PRJPATH=$PWD 7 | cd $PRJPATH 8 | rm -rf bin gen 9 | 10 | cd $ANDROIDPATH 11 | rm -f $APKNAME 12 | . setenv 13 | mmm $PRJPATH -B 14 | java -jar out/host/linux-x86/framework/signapk.jar vendor/friendly-arm/nanopi2/security/platform.x509.pem vendor/friendly-arm/nanopi2/security/platform.pk8 out/target/product/nanopi2/obj/APPS/${APPNAME}_intermediates/package.apk.unsigned ${APPNAME}-unaligned.apk 15 | zipalign -v 4 ${APPNAME}-unaligned.apk $APKNAME 16 | adb install -r ${APKNAME} 17 | -------------------------------------------------------------------------------- /SDCardDemo/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/ic_launcher-web.png -------------------------------------------------------------------------------- /SDCardDemo/res/drawable-hdpi/ic_action_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-hdpi/ic_action_search.png -------------------------------------------------------------------------------- /SDCardDemo/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /SDCardDemo/res/drawable-ldpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-ldpi/ic_launcher.png -------------------------------------------------------------------------------- /SDCardDemo/res/drawable-mdpi/ic_action_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-mdpi/ic_action_search.png -------------------------------------------------------------------------------- /SDCardDemo/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /SDCardDemo/res/drawable-xhdpi/ic_action_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-xhdpi/ic_action_search.png -------------------------------------------------------------------------------- /SDCardDemo/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friendlyarm/AndroidHardwareAccess/8084bdb9d85b9e466aea5544769ffb30d60dfda3/SDCardDemo/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /SDCardDemo/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 |