Commit 89a614a6 authored by Schoumi's avatar Schoumi

list ftdi device and get lpc ids

parents
*.apk
# Generated files
bin/
gen/
# Gradle files
.gradle/
build/
/*/build/
gradlew*
# Local configuration file (sdk path, etc)
local.properties
# Proguard folder generated by Eclipse
proguard/
# Log Files
*.log
#android-studio file
.idea
*.iml
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "fr.mobdev.lpcprog"
minSdkVersion 12
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories{
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
//compile 'com.github.felHR85:UsbSerial:4.1.1'
compile files('src/main/libs/d2xx.jar')
}
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/schoumi/projet/sdk/android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
package fr.mobdev.lpcprog;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class DeviceAdapter extends RecyclerView.Adapter<DeviceHolder> {
private List<UsbDevice> devices;
public DeviceAdapter(List<UsbDevice> devices){
this.devices = devices;
}
@Override
public DeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_item,parent,false);
return new DeviceHolder(view);
}
@Override
public void onBindViewHolder(DeviceHolder holder, int position) {
holder.setupDevice(devices.get(position));
}
@Override
public int getItemCount() {
return devices.size();
}
}
class DeviceHolder extends RecyclerView.ViewHolder {
public DeviceHolder(View itemView) {
super(itemView);
}
public void setupDevice(UsbDevice device){
if(Build.VERSION.SDK_INT >= 21) {
TextView name = (TextView) itemView.findViewById(R.id.device_name);
name.setText("Name: " + device.getProductName());
}
TextView vendor = (TextView) itemView.findViewById(R.id.device_vendor_id);
vendor.setText("Vendor Id: "+device.getVendorId());
TextView product = (TextView) itemView.findViewById(R.id.device_id);
product.setText("Product id: " + device.getProductId());
}
}
package fr.mobdev.lpcprog;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.provider.Settings;
import com.felhr.usbserial.UsbSerialDevice;
import com.felhr.usbserial.UsbSerialInterface;
public class IspManager {
//defines
private static final int REP_BUF = 100;
private static final byte[] SYNC_START = "?".getBytes();
private static final byte[] SYNC = "Synchronized\r\n".getBytes();
private static final byte[] SYNC_OK = "OK\r\n".getBytes();
private static final byte[] DATA_BLOCK = "OK\r\n".getBytes();
private static final byte[] DATA_BLOCK_RESEND = "RESEND\r\n".getBytes();
private static final byte[] SYNC_ECHO_OFF = "A 0\r\n".getBytes();
private static final byte[] READ_UID = "N\r\n".getBytes();
private static final byte[] READ_PART_ID = "J\r\n".getBytes();
private static final byte[] READ_BOOT_VERSION = "K\r\n".getBytes();
//members
private static IspManager instance = null;
private UsbSerialDevice serialPort = null;
private IspManager(){
}
public static IspManager getInstance(){
if(instance == null)
instance = new IspManager();
return instance;
}
public boolean setupDevice(UsbDevice device, UsbDeviceConnection connection, int baudRate, int data, int stops, int parity, int flow){
serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);
if(serialPort.syncOpen()) {
serialPort.setBaudRate(baudRate);
serialPort.setDataBits(data);
serialPort.setStopBits(stops);
serialPort.setParity(parity);
serialPort.setFlowControl(flow);
return true;
}
return false;
}
public boolean synchronize(int crystal_freq){
if(serialPort == null)
return false;
byte[] buf;
if(serialPort.syncWrite(SYNC_START,500) != SYNC_START.length){
System.out.println("Start");
return false;
}
buf = new byte[SYNC.length];
serialPort.syncRead(buf,500);
String result = new String(buf);
String expected = new String(SYNC);
if(result.compareTo(expected) != 0) {
printbuffer(buf);
System.out.println("Start answer "+result);
return false;
}
if(serialPort.syncWrite(SYNC,500) != SYNC.length) {
System.out.println("SYNC write");
return false;
}
clearBuffer(SYNC.length);
buf = new byte[SYNC_OK.length];
serialPort.syncRead(buf,500);
result = new String(buf);
expected = new String(SYNC_OK);
if(result.compareTo(expected) != 0) {
printbuffer(buf);
System.out.println("SYNC OK "+result);
return false;
}
String freq = String.valueOf(crystal_freq)+"\r\n";
if(serialPort.syncWrite(freq.getBytes(),500) != freq.length()) {
System.out.println("freq write");
return false;
}
clearBuffer(freq.length());
buf = new byte[SYNC_OK.length];
serialPort.syncRead(buf,500);
result = new String(buf);
expected = new String(SYNC_OK);
if(result.compareTo(expected) != 0) {
System.out.println("freq answer "+result);
printbuffer(buf);
return false;
}
if(serialPort.syncWrite(SYNC_ECHO_OFF,500) != SYNC_ECHO_OFF.length) {
System.out.println("Sync Echo Off write");
return false;
}
clearBuffer(SYNC_ECHO_OFF.length);
buf = new byte[3];
serialPort.syncRead(buf,500);
return true;
}
private void printbuffer(byte[] buffer){
for(int i = 0; i < buffer.length; i++){
System.out.println(String.format("%02x",buffer[i]));
}
}
private void clearBuffer(int size){
if(serialPort == null)
return;
byte[] buf = new byte[size];
int cpt=0;
do{
int nb = serialPort.syncRead(buf,500);
System.out.println(String.format("%02x",buf[0]));
break;/*
if(nb < 0){
cpt++;
if(cpt >=100) {
System.out.println("timeout");
break;
}
} else if( nb == 0){
System.out.println("nb = 0");
return;
}*/
}while(buf[0] == '\r' && buf[0] != '\n');
if(buf[0] == '\r') {
serialPort.syncRead(buf, 5);
System.out.println(String.format("%02x", buf[0]));
}
System.out.println("end");
}
public String[] readUid(){
if(serialPort == null)
return null;
byte[] buf = new byte[REP_BUF];
int ret = sendCommand(READ_UID,null);
if(ret != 0){
System.out.println("Send Command "+ret);
return null;
}
int size = serialPort.syncRead(buf,1);
if(size<=0){
System.out.println("Send size "+size);
return null;
}
String[] uids = new String[4];
for(int i = 0; i < 4; i++){
uids[0] = Long.toHexString(byteArrayToLong(buf,8*i+i*2));
}
System.out.println("uids ok");
return uids;
}
public String readPartId(){
return null;
}
public String[] readBootVersion(){
return null;
}
public int sendCommand(byte[] cmd, String[] args){
byte[] buf = new byte[3];
if(serialPort.syncWrite(cmd,15) != cmd.length)
return -5;
int len = serialPort.syncRead(buf, 15);
System.out.println("Len "+len);
for(int i = 0; i < 3; i++){
System.out.println(String.format("%02x", buf[i]));
}
if(len <= 0)
return -4;
int ret = parseRetCode(buf);
return ret;
}
private int parseRetCode(byte[] data){
String str = new String(data);
str = str.replace("\r\n","");
int ret = Integer.parseInt(str);
//int ret = byteArrayToInt(data,0);
return ret;
}
private int byteArrayToInt(byte[] array, int offset){
int value = 0;
value |= (array[offset] & 0xff) << 24;
value |= (array[offset+1] & 0xff) << 16;
value |= (array[offset+2] & 0xff) << 8;
value |= (array[offset+3] & 0xff);
return value;
}
private long byteArrayToLong(byte[] array, int offset){
long value = 0;
value |= (array[offset] & 0xff) << 56;
value |= (array[offset+1] & 0xff) << 48;
value |= (array[offset+2] & 0xff) << 40;
value |= (array[offset+3] & 0xff) << 32;
value |= (array[offset+4] & 0xff) << 24;
value |= (array[offset+5] & 0xff) << 16;
value |= (array[offset+6] & 0xff) << 8;
value |= (array[offset+7] & 0xff);
return value;
}
}
package fr.mobdev.lpcprog;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UsbComm {
private static final String ACTION_USB_PERMISSION = "fr.mobdev.usb_perm";
private UsbManager manager = null;
private UsbDeviceConnection connection = null;
private static int val = 0;
private UsbDevice device = null;
public UsbComm(Context context){
manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
}
public boolean searchForDevice(){
HashMap<String, UsbDevice> usbDevices = manager.getDeviceList();
if(!usbDevices.isEmpty()) {
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
UsbDevice device = entry.getValue();
int deviceVID = device.getVendorId();
int devicePID = device.getProductId();
if (deviceVID != 0x1d6b && devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) {
return true;
}
}
}
return false;
}
public List<UsbDevice> getDevices(){
List<UsbDevice> devices = new ArrayList<>();
HashMap<String, UsbDevice> usbDevices = manager.getDeviceList();
if(!usbDevices.isEmpty()) {
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
UsbDevice device = entry.getValue();
int deviceVID = device.getVendorId();
int devicePID = device.getProductId();
if (deviceVID != 0x1d6b && devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) {
devices.add(device);
}
}
}
return devices;
}
public boolean askForPermission(UsbDevice device, Activity activity){
if(!manager.hasPermission(device)) {
PendingIntent intent = PendingIntent.getBroadcast(activity, 0, new Intent(ACTION_USB_PERMISSION), 0);
manager.requestPermission(device, intent);
return false;
}
return true;
}
public UsbDeviceConnection openConnection(UsbDevice device, Activity activity){
if(!manager.hasPermission(device)) {
askForPermission(device, activity);
return null;
}
if(connection == null || this.device != device) {
connection = manager.openDevice(device);
this.device = device;
}
return connection;
}
}
package fr.mobdev.lpcprog;
import android.app.Application;
import android.test.ApplicationTestCase;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.mobdev.lpcprog">
<uses-feature
android:name="android.hardware.usb.host"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DeviceActivity"></activity>
</application>
</manifest>
\ No newline at end of file
package fr.mobdev.lpcprog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.List;
public class DeviceActivity extends AppCompatActivity {
private UsbComm comm;
private USBDevice dev;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.device);
comm = UsbComm.getInstance(this);
List<USBDevice> devices = comm.getDevices();
int pid = getIntent().getIntExtra("PID",-1);
int vid = getIntent().getIntExtra("VID",-1);
for(USBDevice device : devices){
if(device.PID == pid && device.VID == vid){
dev = device;
System.out.println("device found");
break;
}
}
if(dev != null) {
TextView name = (TextView) findViewById(R.id.name);
name.setText("Name: " + dev.description);
TextView vendor = (TextView) findViewById(R.id.vendor_id);
vendor.setText(String.format("Vendor Id: %04x", dev.VID));
TextView product = (TextView) findViewById(R.id.id);
product.setText(String.format("Product id: %04x", dev.PID));
}
doUsb();
}
private void doUsb(){
System.out.println("doUsb");
new Thread(new Runnable() {
@Override
public void run() {
boolean conn = comm.openConnection(dev);
IspManager manager = IspManager.getInstance();
String uidStr = "No UID Found or error";
String bootStr = "No Boot Version Found or error";
String partidStr = "No Part ID Found or error";
if(conn && manager.setupDevice(dev, 115200)) {
if(!manager.synchronizeIfPossible(10000)) {
System.out.println("Sync failed :(");
return;
}
String[] version = manager.readBootVersion();
String partid = manager.readPartId();
String[] uids = manager.readUid();
if (uids != null) {
uidStr = String.format("UID: %08x - %08x - %08x - %08x",Long.parseLong(uids[0]),
Long.parseLong(uids[1]),
Long.parseLong(uids[2]),
Long.parseLong(uids[3]));
}
if (version != null) {
bootStr = "Boot Version: " + version[0] + "." + version[1];
}
if(partid != null){
partidStr = String.format("Part Id %08x",Long.parseLong(partid));
}
}
updateIDS(uidStr,partidStr,bootStr);
}
}).start();
}
private void updateIDS(final String uid, final String partid, final String boot) {
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView view = (TextView) findViewById(R.id.uids);
view.setText(uid);
view = (TextView) findViewById(R.id.part_id);
view.setText(partid);
view = (TextView) findViewById(R.id.boot_version);
view.setText(boot);
}
});
}
}
package fr.mobdev.lpcprog;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class DeviceAdapter extends RecyclerView.Adapter<DeviceHolder> {
private List<USBDevice> devices;
public DeviceAdapter(List<USBDevice> devices){
this.devices = devices;
}
@Override
public DeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_item,parent,false);
return new DeviceHolder(view);
}
@Override
public void onBindViewHolder(DeviceHolder holder, int position) {
holder.setupDevice(devices.get(position));
}
@Override
public int getItemCount() {
return devices.size();
}
}
class DeviceHolder extends RecyclerView.ViewHolder {
public DeviceHolder(View itemView) {
super(itemView);
}
public void setupDevice(USBDevice device){
TextView name = (TextView) itemView.findViewById(R.id.device_name);
name.setText("Name: " + device.description);
TextView vendor = (TextView) itemView.findViewById(R.id.device_vendor_id);
vendor.setText(String.format("Vendor Id: %04x",device.VID));
TextView product = (TextView) itemView.findViewById(R.id.device_id);
product.setText(String.format("Product id: %04x", device.PID));
}
}
This diff is collapsed.
package fr.mobdev.lpcprog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private UsbComm comm;
private List<USBDevice> devices;
private int value = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
comm = UsbComm.getInstance(this);
final GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e){
return true;
}
});
RecyclerView view = (RecyclerView) findViewById(R.id.device_list);
view.setLayoutManager(new LinearLayoutManager(this));
view.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e){
View v = view.findChildViewUnder(e.getX(), e.getY());
if(v != null && gestureDetector.onTouchEvent(e)){
int pos = view.getChildAdapterPosition(v);
System.out.println("touch "+ ++value);
onItemClick(pos);
return true;
}
return false;