FT2232调试记录(1):
FT2232调试记录(2):
FT2232调试记录(3):

FT2232 SPI读写函数:

参照SPI提供的文档:
FT2232调试记录(3)-LMLPHP

工程:FT2232调试记录(3)-LMLPHP

SPI 写函数:

FT_STATUS write_byte(FT_HANDLE handle,uint8_t address, uint16_t data)
{
uint32_t sizeToTransfer = 0;
uint32_t sizeTransfered=0;
//bool writeComplete=0;
//uint32_t retry=0;
//bool state;
FT_STATUS status;
/* Write command EWEN(with CS_High -> CS_Low) */
sizeToTransfer=11;
sizeTransfered=0;
byOutputBuffer[0]=0x9F;/* SPI_EWEN -> binary 10011xxxxxx (11bits) */
byOutputBuffer[1]=0xFF;
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);

/* CS_High + Write command + Address */
sizeToTransfer=1;
sizeTransfered=0;
byOutputBuffer[0] = 0xA0;/* Write command (3bits) */
byOutputBuffer[0] = byOutputBuffer[0] | ( ( address >> 3) & 0x0F );/*5 most significant add bits*/
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE);
APP_CHECK_STATUS(status);
/*Write 3 least sig address bits */
sizeToTransfer=3;
sizeTransfered=0;
byOutputBuffer[0] = ( address & 0x07 ) << 5; /* least significant 3 address bits */
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS);
APP_CHECK_STATUS(status);
/* Write 2 byte data + CS_Low */
sizeToTransfer=2;
sizeTransfered=0;
byOutputBuffer[0] = (uint8_t)(data & 0xFF);
byOutputBuffer[1] = (uint8_t)((data & 0xFF00)>>8);
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);
 /* Wait until D0 is high */
#if 1
 /* Strobe Chip Select */
sizeToTransfer=0;
sizeTransfered=0;
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE);
APP_CHECK_STATUS(status);
#ifndef __linux__
Sleep(10);
#endif
sizeToTransfer=0;
sizeTransfered=0;
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);
#else
retry=0;
state=FALSE;
SPI_IsBusy(handle,&state);
while((FALSE==state) && (retry<SPI_WRITE_COMPLETION_RETRY))
{
printf("SPI device is busy(%u)\n",(unsigned)retry);
SPI_IsBusy(ftHandle,&state);
retry++;
}
#endif
 /* Write command EWEN(with CS_High -> CS_Low) */
sizeToTransfer=11;
sizeTransfered=0;
byOutputBuffer[0]=0x8F;/* SPI_EWEN -> binary 10011xxxxxx (11bits) */
byOutputBuffer[1]=0xFF;
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);
return status;
}

SPI 读函数:

FT_STATUS read_byte(FT_HANDLE handle, uint8_t address, uint16_t *data)
{
uint32_t sizeToTransfer = 0;
uint32_t sizeTransfered;
//bool writeComplete=0;
//uint32_t retry=0;
//bool state;
FT_STATUS status;
/* CS_High + Write command + Address */
sizeToTransfer=1;
sizeTransfered=0;
byOutputBuffer[0] = 0xC0;/* Write command (3bits)*/
byOutputBuffer[0] = byOutputBuffer[0] | ( ( address >> 3) & 0x0F );/*5 most significant add bits*/
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE);
APP_CHECK_STATUS(status);
/*Write partial address bits */
sizeToTransfer=4;
sizeTransfered=0;
byOutputBuffer[0] = ( address & 0x07 ) << 5; /* least significant 3 address bits */
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS);
APP_CHECK_STATUS(status);
/*Read 2 bytes*/
sizeToTransfer=2;
sizeTransfered=0;
status = SPI_Read(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);
*data = (uint16_t)(byOutputBuffer[1]<<8);
*data = (*data & 0xFF00) | (0x00FF & (uint16_t)byOutputBuffer[0]);
return status;
}

SPI flash读写实验:

FT2232调试记录(3)-LMLPHP

相关源码

bsp_ft2232.h

#ifndef BSP_FT2232_H
#define BSP_FT2232_H

#include "mainwindow.h"
//#include "ui_mainwindow.h"

#include<stdio.h>
#include<stdlib.h>
///* OS specific libraries */
#ifdef _WIN32
#include<windows.h>
#endif


#include <windows.h>


#include "../lib/ftd2xx.h"
#include "../lib/libmpsse_spi.h"
//#include "../lib/libmpsse_i2c.h"


class bsp_ft2232
{
private:



public:
     enum typePinA{pinA3=3,pinA4=4};
     enum typePinB{pinB3=3,pinB4=4};
     bsp_ft2232();
     int bsp_GetDevcieNumber();
     void bsp_ledA(enum typePinA chl,bool highlow);
     void bsp_ledB(enum typePinB chl,bool highlow);
     void bsp_ledtest(bool highlow);
};

#endif // BSP_FT2232_H

bsp_ft2232.c

#include "bsp_ft2232.h"


/******************************************************************************/
/* Macro and type defines
 */
/******************************************************************************/
/* Helper macros */
#define APP_CHECK_STATUS(exp) {                                                                              \
    if(exp!=FT_OK){                                                                                          \
                qDebug("%s:%d:%s(): status(0x%x) != FT_OK\n",__FILE__, __LINE__, __FUNCTION__,exp);          \
                exit(1);                                                                                     \
                  }else{ qDebug ("status ok!");                                                              \
                 }                                                                                           \
};
#define CHECK_NULL(exp){                                                                                     \
    if(exp==NULL){                                                                                           \
                qDebug("%s:%d:%s(): NULL expression encountered \n",__FILE__, __LINE__, __FUNCTION__);       \
                exit(1);                                                                                     \
                }else{   qDebug ("not null !");                                                              \
                }                                                                                            \
};

#define SPI_DEVICE_BUFFER_SIZE 256
FT_HANDLE ftHandle[2];
FT_STATUS status;
DWORD     numDevs;
ChannelConfig channelConf;
uint8_t gpio_setting[2]={0xff,0xff}; //0001 1000 =0x18;
uint8_t byOutputBuffer[SPI_DEVICE_BUFFER_SIZE];

DWORD dwNumBytesToSend = 0; //Index of output buffer
DWORD dwNumBytesSent = 0;
DWORD dwNumBytesRead = 0;
DWORD dwNumInputBuffer = 0;

FT_STATUS read_byte(FT_HANDLE handle, uint8_t address, uint16_t *data)
{
uint32_t sizeToTransfer = 0;
uint32_t sizeTransfered;
//bool writeComplete=0;
//uint32_t retry=0;
//bool state;
FT_STATUS status;
/* CS_High + Write command + Address */
sizeToTransfer=1;
sizeTransfered=0;
byOutputBuffer[0] = 0xC0;/* Write command (3bits)*/
byOutputBuffer[0] = byOutputBuffer[0] | ( ( address >> 3) & 0x0F );/*5 most significant add bits*/
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE);
APP_CHECK_STATUS(status);
/*Write partial address bits */
sizeToTransfer=4;
sizeTransfered=0;
byOutputBuffer[0] = ( address & 0x07 ) << 5; /* least significant 3 address bits */
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS);
APP_CHECK_STATUS(status);
/*Read 2 bytes*/
sizeToTransfer=2;
sizeTransfered=0;
status = SPI_Read(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);
*data = (uint16_t)(byOutputBuffer[1]<<8);
*data = (*data & 0xFF00) | (0x00FF & (uint16_t)byOutputBuffer[0]);
return status;
}








FT_STATUS write_byte(FT_HANDLE handle,uint8_t address, uint16_t data)
{
uint32_t sizeToTransfer = 0;
uint32_t sizeTransfered=0;
//bool writeComplete=0;
//uint32_t retry=0;
//bool state;
FT_STATUS status;
/* Write command EWEN(with CS_High -> CS_Low) */
sizeToTransfer=11;
sizeTransfered=0;
byOutputBuffer[0]=0x9F;/* SPI_EWEN -> binary 10011xxxxxx (11bits) */
byOutputBuffer[1]=0xFF;
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);

/* CS_High + Write command + Address */
sizeToTransfer=1;
sizeTransfered=0;
byOutputBuffer[0] = 0xA0;/* Write command (3bits) */
byOutputBuffer[0] = byOutputBuffer[0] | ( ( address >> 3) & 0x0F );/*5 most significant add bits*/
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE);
APP_CHECK_STATUS(status);
/*Write 3 least sig address bits */
sizeToTransfer=3;
sizeTransfered=0;
byOutputBuffer[0] = ( address & 0x07 ) << 5; /* least significant 3 address bits */
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS);
APP_CHECK_STATUS(status);
/* Write 2 byte data + CS_Low */
sizeToTransfer=2;
sizeTransfered=0;
byOutputBuffer[0] = (uint8_t)(data & 0xFF);
byOutputBuffer[1] = (uint8_t)((data & 0xFF00)>>8);
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BYTES|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);
 /* Wait until D0 is high */
#if 1
 /* Strobe Chip Select */
sizeToTransfer=0;
sizeTransfered=0;
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE);
APP_CHECK_STATUS(status);
#ifndef __linux__
Sleep(10);
#endif
sizeToTransfer=0;
sizeTransfered=0;
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);
#else
retry=0;
state=FALSE;
SPI_IsBusy(handle,&state);
while((FALSE==state) && (retry<SPI_WRITE_COMPLETION_RETRY))
{
printf("SPI device is busy(%u)\n",(unsigned)retry);
SPI_IsBusy(ftHandle,&state);
retry++;
}
#endif
 /* Write command EWEN(with CS_High -> CS_Low) */
sizeToTransfer=11;
sizeTransfered=0;
byOutputBuffer[0]=0x8F;/* SPI_EWEN -> binary 10011xxxxxx (11bits) */
byOutputBuffer[1]=0xFF;
status = SPI_Write(handle, byOutputBuffer, sizeToTransfer, ( (unsigned long *)(&sizeTransfered) ),
SPI_TRANSFER_OPTIONS_SIZE_IN_BITS|
SPI_TRANSFER_OPTIONS_CHIPSELECT_ENABLE|
SPI_TRANSFER_OPTIONS_CHIPSELECT_DISABLE);
APP_CHECK_STATUS(status);
return status;
}


bsp_ft2232::bsp_ft2232()
{
    FT_STATUS status;

#ifdef _MSC_VER
//Init_libMPSSE();
#endif
    uint32_t channels;
    channelConf.ClockRate = 5000;
    channelConf.LatencyTimer= 255;
    channelConf.configOptions = SPI_CONFIG_OPTION_MODE0 | SPI_CONFIG_OPTION_CS_DBUS3;
    channelConf.Pin = 0x00000000;/*FinalVal-FinalDir-InitVal-InitDir (for dir 0=in, 1=out)*/

    status = SPI_GetNumChannels((DWORD *)&channels);
    APP_CHECK_STATUS(status);
    qDebug("Number of available SPI channels = %d\n",channels);

    /* Open the first available channel */
    status = SPI_OpenChannel(0,&ftHandle[0]);
    APP_CHECK_STATUS(status);
    qDebug("\nhandle=0x%x status=0x%x\n",ftHandle[0],status);
    status = SPI_InitChannel(ftHandle[0],&channelConf);
    APP_CHECK_STATUS(status);


//    status = SPI_CloseChannel(ftHandle[0]);

    /* Open the first available channel */
    status = SPI_OpenChannel(1,&ftHandle[1]);
    APP_CHECK_STATUS(status);
    qDebug("\nhandle=0x%x status=0x%x\n",ftHandle[1],status);
    status = SPI_InitChannel(ftHandle[1],&channelConf);
    APP_CHECK_STATUS(status);


//    status = SPI_CloseChannel(ftHandle[1]);




//    FT_DEVICE_LIST_INFO_NODE devList[2];
//    status = SPI_GetChannelInfo(0,&devList[0]);
//    APP_CHECK_STATUS(status);
//    qDebug("Information on channel number %d:\n",0);
//    /* print the dev info */
//    qDebug(" Flags=0x%x\n",devList[0].Flags);
//    qDebug(" Type=0x%x\n",devList[0].Type);
//    qDebug(" ID=0x%x\n",devList[0].ID);
//    qDebug(" LocId=0x%x\n",devList[0].LocId);
//    qDebug(" SerialNumber=%s\n",devList[0].SerialNumber);
//    qDebug(" Description=%s\n",devList[0].Description);
//    qDebug(" ftHandle=0x%x\n",devList[0].ftHandle);/*is 0 unless open*/

//    status = SPI_GetChannelInfo(1,&devList[1]);
//    APP_CHECK_STATUS(status);
//    qDebug("Information on channel number %d:\n",1);
//    /* print the dev info */
//    qDebug(" Flags=0x%x\n",devList[1].Flags);
//    qDebug(" Type=0x%x\n",devList[1].Type);
//    qDebug(" ID=0x%x\n",devList[1].ID);
//    qDebug(" LocId=0x%x\n",devList[1].LocId);
//    qDebug(" SerialNumber=%s\n",devList[1].SerialNumber);
//    qDebug(" Description=%s\n",devList[1].Description);
//    qDebug(" ftHandle=0x%x\n",devList[1].ftHandle);/*is 0 unless open*/



}

int bsp_ft2232::bsp_GetDevcieNumber()
{
    status = FT_CreateDeviceInfoList(&numDevs);		// Get the number of FTDI devices
    APP_CHECK_STATUS(status);
    qDebug("%d FTDI devices found - the count includes individual ports on a single chip\n", (int)numDevs);
    return  numDevs;
}

void bsp_ft2232::bsp_ledA(enum typePinA chl, bool highlow)
{
uint8_t val=0;
FT_ReadGPIO(ftHandle[0],&val);
FT_WriteGPIO(ftHandle[0],gpio_setting[0], ( highlow ? (val|(0x1<<(chl)) ) : (val&(~(0x1<<(chl))) ) ) );
}

void bsp_ft2232::bsp_ledB(enum typePinB chl, bool highlow)
{
    uint8_t val=0;
    FT_ReadGPIO(ftHandle[1],&val);
    FT_WriteGPIO(ftHandle[1],gpio_setting[1], ( highlow ? (val|(0x1<<(chl)) ) : (val&(~(0x1<<(chl))) ) ) );
}

void bsp_ft2232::bsp_ledtest(bool highlow)
{
    if(highlow)
    {
        dwNumBytesToSend = 0;
        byOutputBuffer[dwNumBytesToSend++] = 0x82;
        byOutputBuffer[dwNumBytesToSend++] = 0xFF;
        byOutputBuffer[dwNumBytesToSend++] = 0xFF;
        status = FT_Write(ftHandle[0], byOutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
        qDebug("on %d\r\n",dwNumBytesSent);
        dwNumBytesSent=0;
        dwNumBytesToSend = 0;

    }else{
        dwNumBytesToSend = 0;
        byOutputBuffer[dwNumBytesToSend++] = 0x82;
        byOutputBuffer[dwNumBytesToSend++] = 0x00;
        byOutputBuffer[dwNumBytesToSend++] = 0xFF;
        status = FT_Write(ftHandle[0], byOutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
        qDebug("off %d\r\n",dwNumBytesSent);
        dwNumBytesSent=0;
        dwNumBytesToSend = 0;
    }
}




02-15 01:13