繁体   English   中英

具有Spartan 6 FPGA的USB主机传输Android

[英]USB host transfer Android with Spartan 6 FPGA

因此,我需要在Android中编写一个应用程序,该应用程序必须能够读取来自FPGA SPARTAN 6的数据。

我的应用程序能够识别何时连接USB,并可以获取有关接口和该接口上的端点的信息。 但是,当应用程序失败时,它需要读取某人在按下按钮时发送的数据。

我的代码如下:

package com.example.usb;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.*;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends Activity 
{
 private UsbManager usbManager;
private UsbDevice device;
private TextView tv;

Handler handler = new Handler()
{
    public  void handleMessage(Message m)
    {
        Integer datos = (Integer)m.obj;
        tv.setText(datos + "\n");
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.tv);
    usbManager = (UsbManager)getSystemService(Context.USB_SERVICE); 
}

@Override
protected void onResume() 
{
    super.onResume();
    Intent intent = getIntent();
    String action = intent.getAction();
    if (action.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))
    {
        //conectar dispositivo
        device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        //muestraDatos(device);
        UsbInterface intf = device.getInterface(1);
        UsbEndpoint epIN = getIN(device);
        //tv.setText(epIN.getDirection() + " " + epIN.getType());
        UsbDeviceConnection connection = usbManager.openDevice(device);
        new Thread(new Read(connection,intf,epIN,handler)).start();
    }
}

private UsbEndpoint getIN(UsbDevice device)
{
    UsbInterface intf = device.getInterface(1);
    UsbEndpoint ep = null;
    int numep = intf.getEndpointCount();
    for (int i = 0 ; i < numep ; i++)
    {
        UsbEndpoint aux = intf.getEndpoint(i);
        if (aux.getDirection() == UsbConstants.USB_DIR_IN && aux.getType() ==   UsbConstants.USB_ENDPOINT_XFER_BULK)
        {
            ep = aux;
        }
    }
    return ep;
}

线程读取(应该从FPGA读取)是:

package com.example.usb;

import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.os.Handler;
import android.os.Message;

public class Read implements Runnable 
{
 private UsbDeviceConnection connection;
 private UsbEndpoint epIN;
private Handler handler;
private UsbInterface intf;

   public Read(UsbDeviceConnection connection,UsbInterface intf,UsbEndpoint epIN,Handler handler)
{
     this.connection = connection;
     this.epIN = epIN;
     this.handler = handler;
     this.intf = intf;
 }

public void run()
{
    byte datos[] = new byte[50];
    int read_data = 0;

    if (connection.claimInterface(intf,true))
    {
        /*Message sms = new Message();
        sms.obj = "Success caliming interface: " + intf.getEndpointCount();
        handler.sendMessage(sms);*/
        //connection.controlTransfer(0x00000080, 0x03, 0x4138, 0, null, 0, 0);


        while (true)
        {
            try
            {
                Thread.sleep(500);
            }
            catch(InterruptedException e){}
            read_data = connection.bulkTransfer(epIN, datos, datos.length, 100);
            datos = new byte[50];
            //if (read_data > -1)
            //{
                Message sms = new Message();
                sms.obj = read_data;
                handler.sendMessage(sms);
            //}
        }
    }
    else
    {
        Message sms = new Message();
        sms.obj = "Fail claiming interface ";
        handler.sendMessage(sms);
    }



}

}

我的目标只是显示使用bulktransfer函数读取的字节数,但不显示任何内容。 另外,我相信我必须使用controlTransfer函数,但是官方文档没有解释有关该函数的任何内容,它只是一个黑暗的参考。 我看过类似Android 4.0.3的帖子 USB主机-通过 带有健康设备的 controlTransfer和此Serial到USB Android应用程序 发送数据,但对于函数中每个参数的含义还不清楚。 任何人都知道我在做什么错和/或对controTransfer函数有很好的解释吗?

嗯,您需要一个USB到串行接口设备来连接到您的Android主机,在FPGA端,它是一个普通的串行接口。

您可以在以下页面之一上查看示例:

http://www.fpga4fun.com/SerialInterface.htmlhttp://www.bealto.com/fpga-uart.html

基本上,您无法从USB端口获取数据。 这可能是由于以下几个原因:

  1. USB接口不起作用(从FPGA端开始)。

  2. USB接口可用,但FPGA不发送数据。

  3. USB接口有效,FPGA确实通过Labview接口发送数据,但Android程序仍然不接收任何数据。

  1. 您必须将驱动程序代码写入(或下载)到USB模块(在FPGA板上)。
  2. 您必须更改FPGA代码。
  3. 它适用于Labview,但不适用于您的程序。 也许FPGA和Labview已经就协议达成了一致。 例如Labview发送一些命令,而FGPA发送回数据。

您不了解FPGA和硬件方面,如果您了解Labview代码,仍然有可能。 还原工程师如何执行Labview代码,然后在android上尝试。

Labview程序不是很难理解。

好吧,我终于能够解决问题。 我所连接的FPGA上的芯片是atmega32q4。 这是一个CDC设备类,因此,在此基础上,我必须找到正确的参数才能使controlTransfer()函数正常工作。 但是,我无法理解该功能的工作原理或CDC设备上的功能的用途(例如:SET_LINE_CODING,GET_LINE_CODING,ETC)。 然后,我找到了链接, 链接为您提供了与controlTransfer函数一起使用的一组参数,它的工作原理很吸引人。

以下是能够从芯片读取数据的代码:

package com.example.calentadorsolar;

import android.hardware.usb.*;
import android.os.Handler;
import android.os.Message;

public class Lectura_Solar implements Runnable
{
private UsbDeviceConnection connection;
private UsbEndpoint epIN;
private Handler handler;
private UsbInterface intf;
private Datos_Solar aux;

public Lectura_Solar(UsbDeviceConnection connection,UsbInterface intf,UsbEndpoint epIN, Handler   handler)
{
    this.connection = connection;
    this.epIN = epIN;
    this.handler = handler;
    this.intf = intf;
    aux = new Datos_Solar();
}



public void run()
{
    connection.claimInterface(intf,true);
    connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
    connection.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
                    0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);
    byte datos [] = new byte[64];
    while (true)
    {
        int data = connection.bulkTransfer(epIN, datos, datos.length, 5000);
        try
        {
            Thread.sleep(500);
        }
        catch (Exception e){}
        if (datos[0] == 0)
        {
            aux.temp1 = String.valueOf(datos[1]);
            aux.temp2 = String.valueOf(datos[2]);
            aux.temp3 = String.valueOf(datos[3]);
            aux.temp4 = String.valueOf(datos[4]);
            this.sendMessage(aux);//here we send the data to the main GUI
        }

    }
}

public void sendMessage(Datos_Solar message)
   {
    Message sms = new Message();
    sms.obj = message;
    handler.sendMessage(sms);
   }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM