FT4222H学习笔记7 - I2C Master

FT4222H的I2C支持I2C v2.1和v3版本。

 

【初始化函数】

[DllImport("LibFT4222.dll", CallingConvention = CallingConvention.Cdecl)]

public static extern eFT4222Status FT4222_I2CMaster_Init(IntPtr ftHandle, UInt32 kbps);

参数kbps的范围是60到3400,表示I2C的频率范围从60K到3.4M bps。

 

【写函数】

FT4222H读写函数分2类,基本型和扩展型,基本型是FT4222H的各个版本(A、B、C、D四个版本)都支持,扩展型A版本的芯片不支持。但是A版本已经没有了,所以基本只要了解扩张型的函数版本。

[DllImport("LibFT4222.dll", CallingConvention = CallingConvention.Cdecl)]

public static extern eFT4222Status FT4222_I2CMaster_WriteEx(

     IntPtr ftHandle, UInt16 deviceAddress, byte flag, byte[] buffer, UInt16 bufferSize, ref UInt16 sizeTransferred);

参数deviceAddress是I2C从设备的地址,比如AT24C256的地址为0xA0,这个参数就是0x50;参数flag是定义一次写数据过程中的结构组成,有3个定义,分别为START = 0x02、REPEAT_START = 0x03、STOP = 0x04,如何一个数据过程中需要START和STOP,则Flag = 0x02 | 0x04;参数buffer是写入的数据;参数bufferSize是写入数据的长度;参数sizeTransferred是实际写入数据长度。

 

【读函数】

[DllImport("LibFT4222.dll", CallingConvention = CallingConvention.Cdecl)]

public static extern eFT4222Status FT4222_I2CMaster_ReadEx(

     IntPtr ftHandle, UInt16 deviceAddress, byte flag, byte[] buffer, UInt16 bufferSize, ref UInt16 sizeTransferred);

参数的含义与写一样。

 

【获取状态】

[DllImport("LibFT4222.dll", CallingConvention = CallingConvention.Cdecl)]

public static extern eFT4222Status FT4222_I2CMaster_GetStatus(

     IntPtr ftHandle, ref byte controllerStatus);

参数controllerStatus 是I2C控制的状态,每个bit的含义(为1时的含义)如下:

bit 0: 控制器忙,其他标志位无效。

bit 1: 错误条件

bit 2: 从属地址未响应

bit 3: 数据未响应

bit 4: 仲裁丢失

bit 5: 控制器空闲

bit 6: 总线忙

 

常用的I2C读写函数例程如下:

public static bool i2cRead(byte slaveAddr, byte addrbit, UInt16 addr, byte[] dat, UInt32 len)

{

     UInt32 bufOffset = 0;

     UInt16 count;

     UInt16 maxSize = 60 * 1024;

     bool result = true;

     if (len == 0)

         return result;

 

     while (len > 0)

     {

         count = (len > maxSize) ? (UInt16)maxSize : (UInt16)len;

         byte addrLen = (byte)((addrbit == 16) ? 2 : 1);

         UInt16 index = 0;

         byte[] addrBuf = new byte[addrLen];

         if (addrbit == 16)

              addrBuf[index++] = (byte)(addr >> 8);

         addrBuf[index++] = (byte)(addr & 0xff);

         ushort sizeTransferred = 0;

         FT4222H.eFT4222Status ftStatus = 0;

         ftStatus = FT4222H.FT4222_I2CMaster_WriteEx(i2cMHandle, slaveAddr, 0x02, addrBuf, addrLen, ref sizeTransferred);

         if (ftStatus != FT4222H.eFT4222Status.FT4222_OK)

         {

              Console.WriteLine("I2CM read(wr addr) fail:" + ftStatus);

              result = false;

              break;

         }

         byte[] rdBuf = new byte[count];

         ftStatus = FT4222H.FT4222_I2CMaster_ReadEx(i2cMHandle, slaveAddr, 0x03 | 0x04, rdBuf, count, ref sizeTransferred);

         if (ftStatus != FT4222H.eFT4222Status.FT4222_OK)

         {

              Console.WriteLine("I2CM read fail:" + ftStatus);

              result = false;

              break;

         }

         Buffer.BlockCopy(rdBuf, 0, dat, (int)bufOffset, count);

         bufOffset += count;

         addr += count;

         len -= count;

     }

     return result;

}

 

public static bool i2cWrite(byte slaveAddr, byte addrbit, UInt16 addr, byte[] dat, UInt32 len)

{

     UInt32 bufOffset = 0;

     UInt16 count;

     UInt16 maxSize = 60 * 1024;

     bool result = true;

     if (len == 0)

         return result;

 

     while(len > 0)

     {

         count = (len > maxSize) ? (UInt16)maxSize : (UInt16)len;

         byte addrLen = (byte)((addrbit == 16) ? 2 : 1);

         UInt16 index = 0;

         byte[] allBuf = new byte[count + addrLen];

         if (addrbit == 16)

              allBuf[index++] = (byte)(addr >> 8);

         allBuf[index++] = (byte)(addr & 0xff);

         Buffer.BlockCopy(dat, (int)bufOffset, allBuf, index, count);

         ushort sizeTransferred = 0;

         FT4222H.eFT4222Status ftStatus = 0;

         ftStatus = FT4222H.FT4222_I2CMaster_WriteEx(i2cMHandle, slaveAddr, 0x06, allBuf, (UInt16)allBuf.Length, ref sizeTransferred);

         if(ftStatus != FT4222H.eFT4222Status.FT4222_OK)

         {

              Console.WriteLine("I2CM Write fail:" + ftStatus);

              result = false;

              break;

         }

 

         do

         {

              byte controlStat = 0;

              // to check if i2c progress succeed

              FT4222H.FT4222_I2CMaster_GetStatus(i2cMHandle, ref controlStat);

              if (controlStat == 0x20)

                   break;

              if (FT4222H.eFT4222Status.FT4222_OK == ftStatus)

              {

                   // is there any error occur

                   Console.WriteLine("I2CM Write status:" + controlStat);

                   if ((controlStat & 0x02) == 0x02)

                   {

                       // error happen

                       // reset i2c master to default

                       FT4222H.FT4222_I2CMaster_Reset(i2cMHandle);

                       result = false;

                       break;

                   }

              }

              else

              {

                   // get status failed

                   result = false;

                   break;

              }

         } while (true);

         if (result == false)

              break;

 

         bufOffset += count;

         addr += count;

         len -= count;

     }

     //

     return result;

}

 

Platform: FT4222H + AT24C256

Clock(KHz)

RD

WR

60

6315B

395B

100

10295B

601B

200

17904B

964B

400

34310B

1448B

1000

61706B

1518B

2000

1.2MB

1575B

3400

1.05MB

1557B

注:2M和4M的频率测试都失败,读回来的数据对不上。

 

 

 

 

你可能感兴趣的:(USB)