【问题标题】:USB host transfer Android with Spartan 6 FPGA带有 Spartan 6 FPGA 的 USB 主机传输 Android
【发布时间】:2013-12-06 11:25:27
【问题描述】:

所以我需要在 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 Host - sending data via controlTransferSerial to USB Android application with health device这样的帖子,但是函数中每个参数的含义并不是很清楚。有人知道我做错了什么和/或对 controTransfer 函数有很好的解释吗?

【问题讨论】:

  • 您首先要更好地了解确切您的硬件设备需要哪些操作 - 这不会是没有文档文档的人会做的事情您正在使用的特定设备将知道,除非您能够说它实现了某些特定的众所周知的方案。你真的是直接在 FPGA 中实现 USB 引擎(可能但不常见)还是使用 USB 接口芯片?
  • 嗯,我确实没有在 FPGA 中实现任何东西。 FPGA 应该发送一串位,我需要捕获这些位。拥有 FPGA 的人在 labview 中实现了一个读取程序,他可以从中读取 FPGA 发送的内容。我对labview或电子学一无所知,我只是使用Android附带的主机API,但同样,它不起作用。而且我不知道硬件设备需要什么样的操作,我完全迷失在这里,这就是为什么我要帮忙。谢谢。
  • 在您提供自定义硬件的全面文档之前,没有人可以帮助您 - 您在上面的回复中完全忽略了这些部分。您必须向开发板/fpga 开发人员询问有关其 USB 接口的信息,否则您将无法将开发板与 Android 或任何其他 USB 主机一起使用。
  • 您需要询问已实现的 USB 接口的完整规格 - 编写驱动程序所需的一切。否则,您将无法知道必须将哪些数据移入或移出哪些端点才能真正完成任何事情。
  • 张贴你所做的事情作为你自己的答案,然后接受它。您可能需要等待几天才能这样做,不确定。

标签: java android fpga


【解决方案1】:

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

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

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

【讨论】:

  • 不,这只是众多可能方式中的一种。没有特别的理由相信它是已经实施的。
  • 这个问题强烈暗示 FPGA 代码已经由其他人开发,并且没有说明它实现了异步串行接口,或者正在使用 USB 串行转换器。唯一提到 USB-serial 是在对另一个 Android 项目的引用中,该项目显然是为了解 Android USB API 而咨询的。因此,基本上,您在这里对发布者当前系统的适用性进行了疯狂的猜测,即使适用,您发布的内容也是指向 FPGA 开发人员部分问题的链接,而不是问题主题的 Android 开发人员的链接。
  • 好吧,也许对你来说,但对我来说,问这个问题的人似乎并不明白他在处理什么样的界面,如果你阅读他问题的最后一段,那就是为什么我提到它的 FPGA 部分,Java 部分应该非常简单且易于实现。
  • 您似乎不明白的是,提出问题的人并不是负责FPGA代码的工程师。您的建议涵盖了他们的同事已经解决的问题(很可能以不同的方式),而不是他们询问的问题。
【解决方案2】:

基本上您无法从 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 程序并不难理解。

【讨论】:

    【解决方案3】:

    好吧,我终于能够解决问题了。我正在与之连接的 FPGA 上的芯片是 atmega32q4。这是一个 CDC 设备类,因此我必须找到正确的参数才能使函数 controlTransfer() 工作。但是,我无法理解该函数的工作原理或 CDC 设备上的函数的用途(例如:SET_LINE_CODING、GET_LINE_CODING、ETC)。然后我找到了this 链接,它为您提供了一组与 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);
       }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 2011-09-25
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多