FTDI Community

General Category => Discussion - Software => Topic started by: Vijay on June 21, 2021, 01:14:06 PM

Title: FT4232 libmpsse SPI interface
Post by: Vijay on June 21, 2021, 01:14:06 PM
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ftd2xx.h"
#include "libMPSSE_spi.h"
#include "QDebug"

uint8 buffer[256] = {0};
 FT_HANDLE ft4232_handle=NULL;
 FT_STATUS read_byte( unsigned short address, unsigned char *data)
 {
     unsigned short spi_addr;
     spi_addr=address;
     uint32 sizeToTransfer = 0;
     uint32 sizeTransfered;
     FT_STATUS status;

         sizeToTransfer=2;
         sizeTransfered=0;
         buffer[0] = (unsigned char)spi_addr>>8;
         buffer[1]=(unsigned char)spi_addr;

         status = SPI_Write(ft4232_handle, buffer, sizeToTransfer, &sizeTransfered,
             SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
             SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE);

         //read 1 byte
         sizeToTransfer=1;
             sizeTransfered=0;
             status = SPI_Read(ft4232_handle, buffer, sizeToTransfer, &sizeTransfered,
                 SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
                 SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
             *data=buffer[0];

             return status;
    }
 FT_STATUS write_byte( unsigned short address, unsigned char data)
  {
      unsigned short spi_addr;
      spi_addr=address;
      uint32 sizeToTransfer = 0;
      uint32 sizeTransfered;
       FT_STATUS status;
      sizeToTransfer=3;
      sizeTransfered=0;
      bool state;
      buffer[0] = (unsigned char)spi_addr>>8 ;
      buffer[1]=(unsigned char)spi_addr;
        buffer[2] = data;
          status = SPI_Write(ft4232_handle, buffer, sizeToTransfer, &sizeTransfered,
              SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
              SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE|
              SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE);
          return status;
  }
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    Init_libMPSSE();

    FT_STATUS ftStatus;
    FT_DEVICE_LIST_INFO_NODE listinfo;
    uint32 channel_cnt=0;

    ftStatus=SPI_GetNumChannels(&channel_cnt);
        if(FT_OK!=ftStatus)
            qDebug() << "FT4232 channel_cnt --> failed";
        else
            qDebug() << "FT4232 channel_cnt --> success" <<"FT4232 chn_cnt="<<channel_cnt;

     ftStatus=SPI_GetChannelInfo(1,&listinfo);
        if(FT_OK!=ftStatus)
              qDebug() << "listing FT4232 info failed";
         else
             qDebug() << "listing FT4232 info success";
qDebug() <<"Device=" <<listinfo.Description;
     ftStatus=SPI_OpenChannel(1,&ft4232_handle);
            if(FT_OK!=ftStatus)
                qDebug() << "opening FT4232 failed";
            else
                qDebug() << "opening FT4232 success";

          ChannelConfig ft4232_ch_cnfg;
          ft4232_ch_cnfg.ClockRate=20000000;
          ft4232_ch_cnfg.configOptions= SPI_CONFIG_OPTION_MODE0 | SPI_CONFIG_OPTION_CS_DBUS3 | SPI_CONFIG_OPTION_CS_ACTIVELOW;
          ft4232_ch_cnfg.LatencyTimer=75;
            ft4232_ch_cnfg.Pin = 0x00000000;
      ftStatus=SPI_InitChannel(ft4232_handle,&ft4232_ch_cnfg);
          if(FT_OK!=ftStatus)
              qDebug() << "Initialize FT4232 failed";
          else
              qDebug() << "Initialize FT4232 success";
        ftStatus=write_byte(0x0000,0x90);
        Sleeper::sleep(5);

        ftStatus=write_byte(0x0000,0x10);
        Sleeper::sleep(5);

        unsigned char rx;
       ftStatus=read_byte(0x800C,&rx);
        qDebug() << "value read="<<rx;


     ftStatus=SPI_CloseChannel(ft4232_handle);
     if(FT_OK!=ftStatus)
         qDebug() << "Closing FT4232 failed";
     else
         qDebug() << "Closing FT4232 success";

    Cleanup_libMPSSE();
}
Statement:
2nd channel of FT4232H is connected to 2 SPI Slave. In the above code,I was trying to interface FT4232H with 1 SPI SLave with the help of libmpsse spi library, but I was not able to perform Read/Write Operation whereas API's return value are SUCCESS,. SPI Slave device accepts 24bits(16 bit address+8 bit data) from SPI Master which is done in write_byte function in above code, then for receiving data from SPI Slave in which 16bits address is first written into slave & reading 8bits from slave, which is done in read_byte function of above code. Kindly help me resolve my problem & let me also know whether coding is correct
Title: Re: FT4232 libmpsse SPI interface
Post by: Vijay on June 22, 2021, 05:44:01 AM
Hello FTDI Community. Kindly help me to resolve my problem which I mentioned above.
Title: Re: FT4232 libmpsse SPI interface
Post by: FTDI Community on June 23, 2021, 11:25:48 AM
Hello,

Could you advise what you see on the MOSI/MISO/CS/SCK lines when you run the code? If you have a logic analyser or a PC-based scope could you capture the lines to see if you are getting the expected waveforms.

Which SPI mode does your SPI peripheral use? Note that MPSSE only supports mode 0 and 2,

Note that you should also check the sizeTransfered value after a read or write to ensure your requested bytes were all transferred.

What was the error which you saw, did you get the expected number of bytes back but they were the wrong values?

One further thing to also check is that you have opened the right channel, depending which channel A or B your SPI device is connected to.

Best Regards, FTDI Community