首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android与fpga板卡的通信

android与fpga板卡的通信
EN

Stack Overflow用户
提问于 2013-01-26 19:35:52
回答 1查看 1.3K关注 0票数 2

我正在尝试将数据从我的android设备传输到FPGA板。android设备支持USB host模式,所连接的FPGA板采用RS_232口。因此,我得到了一个USB-RS232转换设备(CP210x)用于连接。我按照这个页面上提到的方式对传输进行了编码:http://developer.android.com/guide/topics/connectivity/usb/host.html我的代码是:

代码语言:javascript
复制
public class MainActivity extends Activity
    implements View.OnClickListener, Runnable {

private static final String TAG = "TransferData";

private Button sendB;
private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection mConnection;
private UsbEndpoint mEndpointIntr;
private static final char DATA = 'A';
static UsbDevice device;
static int TIMEOUT = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    sendB = (Button)findViewById(R.id.send);
    sendB.setOnClickListener(this);

    mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);

}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();

    Intent intent = getIntent();
    Log.d(TAG, "intent: " + intent);
    String action = intent.getAction();

    device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
        setDevice(device);
    } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
        if (mDevice != null && mDevice.equals(device)) {
            setDevice(null);
        }
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
}

private void setDevice(UsbDevice device) {
    Log.d(TAG, "setDevice " + device);
    if (device.getInterfaceCount() != 1) {
        Log.e(TAG, "could not find interface");
        return;
    }
    else Log.w(TAG, "found interface");
    UsbInterface intf = device.getInterface(0);
    // device should have one endpoint
    if (intf.getEndpointCount() == 0) {
        Log.e(TAG, "could not find endpoint");
        return;
    }
    else Log.w(TAG, "found endpoint");
    // endpoint should be of type interrupt

    Log.w("endpoint", "" + intf.getEndpointCount());
    UsbEndpoint ep = intf.getEndpoint(1);
    if (ep.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) {
        Log.e(TAG, "endpoint is not interrupt type");
        return;
    }
    else Log.w(TAG, "endpoint is interrupt");
    mDevice = device;
    mEndpointIntr = ep;
    if (device != null) {
        UsbDeviceConnection connection = mUsbManager.openDevice(device);
        if (connection != null && connection.claimInterface(intf, true)) {
            Log.d(TAG, "open SUCCESS");
            mConnection = connection;
            Thread thread = new Thread(this);
            thread.start();

        } else {
            Log.d(TAG, "open FAIL");
            mConnection = null;
        }
     }
}

private void sendCommand(char control) {
        if (mConnection != null) {
            byte[] message = new byte[1];
            message[0] = (byte)control;
            // Send command via a control request on endpoint zero
            UsbInterface intf = device.getInterface(0);
            UsbEndpoint endpoint = intf.getEndpoint(1);
            mConnection.claimInterface(intf,true);
            int res =  mConnection.bulkTransfer(endpoint, message, message.length, TIMEOUT);
            Log.w("data sent","result " + res);
        }
}

public void onClick(View v) {
    if (v == sendB) {
        sendCommand(DATA);
    }
}

@Override
public void run() {
    ByteBuffer buffer = ByteBuffer.allocate(1);
    UsbRequest request = new UsbRequest();
    request.initialize(mConnection, mEndpointIntr);
    byte status = -1;
        request.queue(buffer, 1);
        // send poll status command
        sendCommand(DATA);
        // wait for status event
        if (mConnection.requestWait() == request) {
            byte newStatus = buffer.get(0);
            if (newStatus != status) {
                Log.d(TAG, "got status " + newStatus);
                status = newStatus;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        } else {
            Log.e(TAG, "requestWait failed, exiting");
        }
}

打开设备成功,发送结果为1,但是接收到的数据没有反映在FPGA板上。我在做什么?

EN

回答 1

Stack Overflow用户

发布于 2013-02-26 07:41:22

尝试在FPGA上使用TX切换RX。其他通信是否与此连接一起工作?FPGA上有没有一种可以服务于UART的设计?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14536533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档