使用FT245同步模式写入芯片

时间:2015-10-16 11:14:48

标签: c vhdl fpga libusb ftdi

我正在使用连接到FPGA的FT232H设备,我正在尝试写一些字节。读取(传输FPGA-> PC)工作正常,但写入(传输PC-> FPGA)根本不起作用。我使用以下代码:

libusb_open(board, &board_handle);
if (libusb_kernel_driver_active(board_handle, 0) == 1) {
    if(libusb_detach_kernel_driver(board_handle, 0) == 0);
}
libusb_set_configuration(board_handle, 1);
libusb_claim_interface(board_handle, 0);
libusb_control_transfer(board_handle, 0x40, 0x0B, 0x00FF, 0x01, NULL, 0, 5000);
libusb_control_transfer(board_handle, 0x40, 0x0B, 0x40FF, 0x01, NULL, 0, 5000);
libusb_bulk_transfer(board, 0x02, bufout, 3, &transfered, 5000);
bufin = calloc(512, 1);
libusb_bulk_transfer(board, 0x81, bufin, 512, &transfered, 5000);

Bufout充满了数据。当我试图将在FPGA上生成的一些数据发送到PC时,没有问题; bufin填充了正确的数据。

但是当我试图将一些数据发送到FPGA并将其显示在LED上或将其发回时,问题就开始了。

无论bufout内容如何,​​我在FPGA站点收到的每个字节都是0xFF。 Bufoutbufin都声明为unsigned char *。

unsigned char *bufin, *bufout;

令人惊讶(或不)接收的字节数与PC匹配的字节数相匹配,但所有字节的值都为0xFF。

我做错了吗?

我尝试使用libftdi,但效果是一样的(我觉得libftdi使用libusb作为引擎并不奇怪。)

也许我忘了在主机方面调用一些重要的功能?

FPGA方面的代码也非常简单:

process(ftdi_clk, sys_rst)
begin
    if sys_rst = '0'then
        ftdi_wr <= '1';
        ftdi_data <= "ZZZZZZZZ";
        ftdi_rd <= '1';
        ftdi_oe <= '1';
        read <= '1';
    elsif rising_edge(ftdi_clk) then
        if ftdi_txe = '0' then
            ftdi_wr <= '0';
            ftdi_data <= buf;
        else
            ftdi_wr <= '1';
            ftdi_data <= "ZZZZZZZZ";
        end if;
        if (read = '0') and (ftdi_rxf = '0') then
            ftdi_rd <= '0';
            buf <= ftdi_data;
        else
            ftdi_rd <= '1';
        end if;
        if ftdi_rxf = '0' then
            ftdi_oe <= '0';
            read <= '0';
        else
            ftdi_oe <= '1';
            read <= '1';
        end if;
    end if;
end process;
编辑:我检查了所有可能的电气配置,上拉,I / O电压,一切似乎都很好。仍然从FTDI传输到FPGA的所有数据都是1,在2个独立的芯片上进行检查,因此很可能是软件问题。我已经检查了模拟,甚至是后拟合模拟,通信应该根据文档工作。

EDIT2:我尝试过原始供应商库。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ftd2xx.h>

int main(){
    FT_STATUS ftStatus;
    FT_HANDLE ftHandle;
    DWORD BytesWritten;
    unsigned char data[512];
    int i;
    FT_PROGRAM_DATA ftData = {
    0x00000000, 0xFFFFFFFF, // Headers
    0x00000005,  // Version (5 = 232H)
    0x0403, 0x6014, // VID:PID
    "StackOverflow", "Stack", "StackBoard", NULL,
    500, 0, 1, 1, // MaxPower, PnP, SelfPowered, Remote WakeUp
    // FT232B
    0, 0, 0, 0, 0, 0, 0,
    // FT2232
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    // FT232R
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    // FT2232H
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0,
    // FT4232H
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0,
    // FT232H
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
    0, 0, 0, 0, 0, 0, 0,
    };
    ftStatus = FT_Open(0, &ftHandle);
    ftStatus = FT_SetTimeouts(ftHandle, 5000, 5000);
    ftStatus = FT_EE_Program(ftHandle, &ftData);
    ftStatus = FT_EE_Program(ftHandle, &ftData);
    ftStatus = FT_SetBitMode(ftHandle, 0xFF, FT_BITMODE_SYNC_FIFO); 
    for(i = 0; i<512; i++) data[i] = 0x02;
    ftStatus = FT_Write(ftHandle, data, 512, &BytesWritten);
    printf("%d bytes written\n", BytesWritten);
    ftStatus = FT_Read(ftHandle, &data, 512, &BytesWritten);
    printf("%d bytes read\n", BytesWritten);
    for(i = 0; i<BytesWritten; i++) printf("%#2x ", data[i]);
    FT_Close(ftHandle);
}

仍然完全相同的行为。我已经将linux内核更新到最新版本(4.2.3),但结果是一样的。可悲的是,我检查了几台不同的机器和3种不同的芯片。

1 个答案:

答案 0 :(得分:0)

不确定这是否是您唯一的问题,但我认为您在ftdi_data上发生了一些短路。它可以在ftdi_oe激活的同时由FPGA驱动。