FT2232 I2C 8位设备地址 8位寄存器地址 8位数据 读写示例

参考官方文档:AN_135_MPSSE_Basics.pdf, AN_113_FTDI_Hi_Speed_USB_To_I2C_Example.pdf

有疑问的部分,色标已经标出。具体的执行时序流程,参考上一篇。

// Set initial states of the MPSSE interface
// - low byte, both pin directions and output values
// Pin name   Signal      Direction   Config    Initial State     Config
// ADBUS0   TCK/SK    output       1                 high             1       -------------------->SCL(BIT0)
// ADBUS1   TDI/DO     output       1                 low              1       ---------------------->output SDA(BIT1)
// ADBUS2   TDO/DI     input         0                                     0       ----------------------->input SDA
// ADBUS3   TMS/CS    output      0                 high              0
// ADBUS4   GPIOL0     output      1                  low              0
// ADBUS5   GPIOL1     output      0                  low              0
// ADBUS6   GPIOL2     output      0                  high             0
// ADBUS7   GPIOL3     output      0                  high             0

//---------------------I2C--------------------------------------------------------------------------------------------------
void FT2232I2cByteWrOneTime(unsigned char DeviceAddr,unsigned char RegAddr,unsigned char RegVal)
{
    DWORD dwCount;
    FT_STATUS ftStatus = FT_OK;
    //-------------I2cStart-------------------------
    for(dwCount=0; dwCount < 4; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 600ns is achieved
    {

      Set initial states of the MPSSE interface
        OutputBuffer[dwNumBytesToSend++] = 0x80; Configure data bits low-byte of MPSSE port, Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; Initial state config above, Set SDA, SCL high, WP disabled by SK, DO at bit „1‟, GPIOL0 at bit „0‟ 
        OutputBuffer[dwNumBytesToSend++] = 0x13; Direction config above, Set SK,DO,GPIOL0 pins as output with bit „1‟, other pins as input with bit „0‟
    }

    for(dwCount=0; dwCount < 20; dwCount++) 
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x01; //Set SDA low, SCL high, WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount < 20; dwCount++) 
    {
    OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
    OutputBuffer[dwNumBytesToSend++] = 0x00; 
    OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    //Sleep(10);        //Delay for a while

    //-------------I2cSendByteAndNotCheckACK_DeviceAddr-------------------------
    
    OutputBuffer[dwNumBytesToSend++] = MSB_FALLING_EDGE_CLOCK_BYTE_OUT; //Clock data byte out on -ve Clock Edge MSB first
    OutputBuffer[dwNumBytesToSend++] = '\x00';
    OutputBuffer[dwNumBytesToSend++] = '\x00'; //Data length of 0x0000 means 1 byte data to clock out(8 bit)
    OutputBuffer[dwNumBytesToSend++] = DeviceAddr;

    //Get slave's ack
    for(dwCount=0; dwCount<4; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<10; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<40; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    //Sleep(10);        //Delay for a while

    //-------------I2cSendByteAndNotCheckACK_RegAddr-------------------------
    OutputBuffer[dwNumBytesToSend++] = MSB_FALLING_EDGE_CLOCK_BYTE_OUT; //Clock data byte out on -ve Clock Edge MSB first
    OutputBuffer[dwNumBytesToSend++] = '\x00';
    OutputBuffer[dwNumBytesToSend++] = '\x00'; //Data length of 0x0000 means 1 byte data to clock out(8 bit)
    OutputBuffer[dwNumBytesToSend++] = RegAddr;
    //Get slave's ack
    for(dwCount=0; dwCount<4; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<10; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<40; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    //Sleep(10);        //Delay for a while

    //-------------I2cSendByteAndNotCheckACK_RegVal-------------------------
    OutputBuffer[dwNumBytesToSend++] = MSB_FALLING_EDGE_CLOCK_BYTE_OUT; //Clock data byte out on -ve Clock Edge MSB first
    OutputBuffer[dwNumBytesToSend++] = '\x00';
    OutputBuffer[dwNumBytesToSend++] = '\x00'; //Data length of 0x0000 means 1 byte data to clock out(8 bit)
    OutputBuffer[dwNumBytesToSend++] = RegVal;
    //Get slave's ack
    for(dwCount=0; dwCount<4; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<10; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<40; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    //Sleep(100);        //Delay for a while

    //------------I2cStop-----------------
    for(dwCount=0; dwCount<10; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x00; //Set SDA low, SCL high, WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount<20; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x01; //Set SDA low, SCL high, WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount<20; dwCount++) // Repeat commands to ensure the minimum period of the stop hold time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; //Set SDA, SCL high, WP disabled by SK, DO at bit 1, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    //Tristate the SCL, SDA pins
    OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
    OutputBuffer[dwNumBytesToSend++] = 0x03; //Set SCL low, WP disabled by SK, DO, GPIOL0 at bit 0
    OutputBuffer[dwNumBytesToSend++] = 0x10; //Set GPIOL0 pins as output with bit 1, SK, DO and other pins as input with bit 0
    //Sleep(1);        //Delay for a while
    //-------------Send off all commands--------------
    ftStatus = FT_Write(ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
    dwNumBytesToSend = 0; //Clear output buffer
    //Sleep(1);    

}


int FT2232I2cByteRdOneTime(unsigned char DeviceAddr,unsigned char RegAddr)
{
    DWORD dwCount;
    FT_STATUS ftStatus = FT_OK;

    unsigned char Value;
    BOOL bSucceed = TRUE;
    char DataLen=4;
    UCHAR* pDataB = new UCHAR[DataLen];
    memset(pDataB,0,DataLen);
    //Purge USB receive buffer first before read operation
    ftStatus = FT_GetQueueStatus(ftHandle, &dwNumInputBuffer); // Get the number of bytes in the device receive buffer
    if ((ftStatus == FT_OK) && (dwNumInputBuffer > 0))
    FT_Read(ftHandle, &InputBuffer, dwNumInputBuffer, &dwNumBytesRead); //Read out all the data from receive buffer

    //-------------I2cStart-------------------------
    for(dwCount=0; dwCount < 4; dwCount++) // 
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; //Set SDA, SCL high, WP disabled by SK, DO at bit 1, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount < 20; dwCount++) // 
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x01; //Set SDA low, SCL high, WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
    OutputBuffer[dwNumBytesToSend++] = 0x00; 
    OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    //Sleep(10);        //Delay for a while
    //-------------I2cSendByteAndNotCheckACK_DeviceAddr-------------------------
    
    OutputBuffer[dwNumBytesToSend++] = MSB_FALLING_EDGE_CLOCK_BYTE_OUT; //Clock data byte out on -ve Clock Edge MSB first
    OutputBuffer[dwNumBytesToSend++] = '\x00';
    OutputBuffer[dwNumBytesToSend++] = '\x00'; //Data length of 0x0000 means 1 byte data to clock out(8 bit)
    OutputBuffer[dwNumBytesToSend++] = DeviceAddr;

    //Get slave's ack
    for(dwCount=0; dwCount<4; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<10; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<40; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    //Sleep(10);        //Delay for a while

    //-------------I2cSendByteAndNotCheckACK_RegAddr-------------------------
    OutputBuffer[dwNumBytesToSend++] = MSB_FALLING_EDGE_CLOCK_BYTE_OUT; //Clock data byte out on -ve Clock Edge MSB first
    OutputBuffer[dwNumBytesToSend++] = '\x00';
    OutputBuffer[dwNumBytesToSend++] = '\x00'; //Data length of 0x0000 means 1 byte data to clock out(8 bit)
    OutputBuffer[dwNumBytesToSend++] = RegAddr;

    //Get slave's ack
    for(dwCount=0; dwCount<4; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<10; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<40; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    //Sleep(10);        //Delay for a while

    //-------------I2cReStart-------------------------
    for(dwCount=0; dwCount<4; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x00; //Set SDA low, SCL high, WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount<20; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x01; //Set SDA low, SCL high, WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount<20; dwCount++) 
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; //Set SDA, SCL high, WP disabled by SK, DO at bit 1, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }


    for(dwCount=0; dwCount < 40; dwCount++) 
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x01; //Set SDA low, SCL high, WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount < 20; dwCount++) 
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x00;
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    //Sleep(10);        //Delay for a while

    //-------------I2cSendByteAndNotCheckACK_DeviceAddr+1-------------------------
    OutputBuffer[dwNumBytesToSend++] = MSB_FALLING_EDGE_CLOCK_BYTE_OUT; //Clock data byte out on -ve Clock Edge MSB first
    OutputBuffer[dwNumBytesToSend++] = '\x00';
    OutputBuffer[dwNumBytesToSend++] = '\x00'; //Data length of 0x0000 means 1 byte data to clock out(8 bit)
    OutputBuffer[dwNumBytesToSend++] = DeviceAddr+1;

    //Get slave's ack
    for(dwCount=0; dwCount<4; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<10; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    for(dwCount=0; dwCount<40; dwCount++) // 
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; 
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    //Sleep(140);        //Delay for a while

    //-------------I2cReceiveByte-------------------------
    OutputBuffer[dwNumBytesToSend++] = '\x20'; //0x20:  rising edge latch data
    OutputBuffer[dwNumBytesToSend++] = '\x00';
    OutputBuffer[dwNumBytesToSend++] = '\x00'; //Data length of 0x0000 means 1 byte data to clock in
    OutputBuffer[dwNumBytesToSend++] = '\x87'; //Send answer back immediate command
    //pDataB[0] = FT2232I2cReceiveByte(); // Read value back

    //------------I2cSendNAck--------------------
    for(dwCount=0; dwCount<4; dwCount++) // Repeat commands to ensure the minimum period of the stop hold time ie 2.5us is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; //Set SDA high, SCL low(20180720), WP disabled by SK at bit 0, DO, GPIOL0 at bit 1
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount<10; dwCount++) // Repeat commands to ensure the minimum period of the stop hold time ie 2.5us is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; //Set SDA high, SCL high(20180720), WP disabled by SK at bit 0, DO, GPIOL0 at bit 1
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount<40; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 2.5us is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x02; //Set SDA high, SCL low(20180720), WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    //for(dwCount=0; dwCount<12; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 2.5us is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x00; //Set SDA low, SCL low(20180720), WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    //Sleep(100);        //Delay for a while
    //------------I2cStop-----------------
    for(dwCount=0; dwCount<6; dwCount++) // Repeat commands to ensure the minimum period of the stop setup time ie 800ns is achieved
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x00; //Set SDA low, SCL high, WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount<20; dwCount++) 
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x01; //Set SDA low, SCL high, WP disabled by SK at bit 1, DO, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }

    for(dwCount=0; dwCount<10; dwCount++) 
    {
        OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
        OutputBuffer[dwNumBytesToSend++] = 0x03; //Set SDA, SCL high, WP disabled by SK, DO at bit 1, GPIOL0 at bit 0
        OutputBuffer[dwNumBytesToSend++] = 0x13; //Set SK,DO,GPIOL0 pins as output with bit 1, other pins as input with bit 0
    }
    //Tristate the SCL, SDA pins
    OutputBuffer[dwNumBytesToSend++] = 0x80; //Command to set directions of lower 8 pins and force value on bits set as output
    OutputBuffer[dwNumBytesToSend++] = 0x03; //Set SCL low, WP disabled by SK, DO, GPIOL0 at bit 0
    OutputBuffer[dwNumBytesToSend++] = 0x10; //Set GPIOL0 pins as output with bit 1, SK, DO and other pins as input with bit 0

    //-------------Send off all commands--------------
    ftStatus = FT_Write(ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent); //Send off the commands
    dwNumBytesToSend = 0; //Clear output buffer
    //Sleep(0.1);        //Delay for a while
    ftStatus = FT_Read(ftHandle, InputBuffer, 1, &dwNumBytesRead);
    if ((ftStatus != FT_OK) || (dwNumBytesRead == 0))
    { 
            return 0; /*Error*/ 
    }
    //Value = InputBuffer[0];
    return InputBuffer[0];
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页