FT4222H学习笔记8 - I2C Slave

FT4222H的I2CSlave也支持I2C v2.1和v3版本。当FT4222H接收到I2C上的数据会暂存在一个256字节的缓存器中,然后再发送给USB。

 

【初始化函数】

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

public static extern eFT4222Status FT4222_I2CSlave_Init(IntPtr ftHandle);

复位初始化后I2C地址默认是0x40.

 

【从地址管理】

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

public static extern eFT4222Status FT4222_I2CSlave_GetAddress(IntPtr ftHandle, ref byte addr);

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

public static extern eFT4222Status FT4222_I2CSlave_SetAddress(IntPtr ftHandle, byte addr);

FT4222_I2CSlave_GetAddress获取当前设置的I2C从地址值;FT4222_I2CSlave_SetAddress设置I2C从地址值。

 

【获取接收队列中数据的长度】

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

public static extern eFT4222Status FT4222_I2CSlave_GetRxStatus(IntPtr ftHandle, ref UInt16 pRxSize);

参数pRxSize是接收队列(即Master发送过来的数据)中的数据长度。

 

【写函数】

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

public static extern eFT4222Status FT4222_I2CSlave_Write(

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

参数buffer是写入的数据;参数bufferSize是写入的数据个数;参数sizeTransferred是实际写的数据个数。

 

【读函数】

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

public static extern eFT4222Status FT4222_I2CSlave_Read(

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

参数的含义与写一样。

 

同样使用STM32F030平台作为Master验证FT4222H的I2C Slave功能,在STM32F030端接收发送数据,并把读到的数据打印到串口。

写测试代码如下,假设设备是8bit地址,地址为0x10,发送127个字节数据

for(i = 0; i < length; i++)

{

         uartProtocolBuf[i] = i;

         Printf("%2x ", uartProtocolBuf[i]);

}

if(i2cWrite(0x80, 8, 0x10, uartProtocolBuf, length) == FALSE)

         Printf("I2C Slave Write fail\n");

串口打印信息如下:

I2C SLAVE TEST Write Data 127

 0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e

FT4222H端接收到的数据如下:

10 00 01 02 03 04 05 06 07 08 12 0A 0B 0C 0D 1C 0F 10 22 12 13 14 15 16 2E 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 C2 62 63 64 65 66 67 68 69 6A 6B D8 6D 6E DE 70 71 72 73 E8 75 76 77 78 79 F4 7B 7C 7D 7E

 

读测试代码,假设设备是8bit地址,地址为0x10,接收2个字节的数据

if(i2cRead(0x80, 8, 0x10, uartProtocolBuf, length) == FALSE)

         Printf("I2C Slave Read fail\n");

Printf("I2C Slave Read Data:");

for(i = 0; i < length; i++)

{

         if(i % 16 == 0)

                   Printf("\n");

         Printf("%2x ", uartProtocolBuf[i]);

}

Printf("\n");

 

FT4222H端发送的数据:

aa 55

MCU串口的信息:

I2C SLAVE TEST Read Data 2

I2C Slave Read Data:

aa 55

FT4222H端接收的数据:

10

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值