一. 软件需求
1、RK2118需要与wifi芯片RTL8721实现i2c通信
2、实现自定义通信协议。
二.i2C软件实现
代码路径:rk2118\components\hifi4\rtt\app\rkplayer\i2c_wifi_device.c
2.1 i2c设备初始化函数
static void rtl8721_init()
{
rt_err_t ret;
rtl8721 = rt_calloc(1, sizeof(struct wifi_dev));
RT_ASSERT(rtl8721);
rtl8721->i2c_client = rt_calloc(1, sizeof(struct rt_i2c_client));
RT_ASSERT(rtl8721->i2c_client);
rtl8721->i2c_client->bus = rt_i2c_bus_device_find(WIFI_I2C_BUS_NAME);
RT_ASSERT(rtl8721->i2c_client->bus);
if (rtl8721->i2c_client->bus == RT_NULL)
{
rt_kprintf("can't find %s device!\n", rtl8721->i2c_client->bus);
}
rtl8721->i2c_client->client_addr = WIFI_ADDR;
ret = write_reg(rtl8721->i2c_client, 0x00, 0x0);
if (ret != RT_EOK)
{
rt_kprintf("ERR: %s,rtl8721 reset failed: %d\n", __func__, ret);
}
rt_thread_mdelay(50);
}
2.2 i2c_write函数实现
static rt_err_t write_reg(struct rt_i2c_client *i2c_client, rt_uint8_t reg, rt_uint8_t data)
{
struct rt_i2c_msg msgs[1];
uint8_t data_buf[2]= {reg, data};
rt_err_t ret;
msgs[0].addr = i2c_client->client_addr;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = data_buf;
msgs[0].len = 2;
rt_kprintf(">>>>>>>>>>>>>>>>>>>sylon debug: %s: data_buf[0]:0x%x,data_buf[1]:0x%x,msgs[0].addr:0x%x\n",
__func__,data_buf[0],data_buf[1],msgs[0].addr);
ret = rt_i2c_transfer(i2c_client->bus, msgs, 1);
if (ret != 1)
{
rt_kprintf("ERR: %s: failed: (%d)\n", __func__, ret);
return ret;
}
return RT_EOK;
}
2.3 i2c_read函数实现
static rt_err_t read_regs(struct rt_i2c_client *i2c_client, uint8_t reg, uint8_t *data)
{
rt_err_t ret;
struct rt_i2c_msg msgs[2] =
{
{ .addr = i2c_client->client_addr, .flags = RT_I2C_WR, .buf = ®, .len = 1 },
{ .addr = i2c_client->client_addr, .flags = RT_I2C_RD, .buf = data, .len = 1 }
};
ret = rt_i2c_transfer(i2c_client->bus, msgs, 2);
rt_kprintf(">>>>>>>>>>>>>>>>>>>I2C read data (bus: %s addr: 0x%x, reg: 0x%x, "
"data:0x%x )\n", i2c_client->bus, i2c_client->client_addr, reg, *data);
if (ret != 2)
{
rt_kprintf("I2C read failed (bus: %s addr: 0x%x, reg: 0x%x, "
"ret: %d)\n", i2c_client->bus, i2c_client->client_addr, reg, ret);
return ret;
}
return RT_EOK;
}
三、I2C TEST
3.1 test函数
static void i2c_wifi(int argc, char **argv)
{
if (argc < 4)
goto out;
char *cmd;
uint8_t read_reg;
uint32_t regaddr = strtol(argv[2], NULL, 16);
uint32_t data = strtol(argv[3], NULL, 16);
rtl8721_init();
cmd = argv[1];
if (!rt_strcmp(cmd, "write"))
{
rt_err_t ret = write_reg(rtl8721->i2c_client, regaddr, data);
if (ret != RT_EOK)
{
rt_kprintf("ERR: %s,rtl8721 write_reg failed: %d\n", __func__, ret);
}
}
else if (!rt_strcmp(cmd, "read"))
{
read_regs(rtl8721->i2c_client, regaddr, &read_reg);
rt_kprintf("sylon debug: regaddr[0x%x] value:[0x%x] \n", regaddr, read_reg);
}else if (!rt_strcmp(cmd, "list"))
{
for (int i = 0; i < 0x35; i++)
{
uint8_t ret = read_regs(rtl8721->i2c_client, i, &read_reg);
rt_kprintf("sylon debug: reg[%d][0x%x][0x%x] ret=%d\n", i, i, read_reg, ret);
}
}
return;
out:
wifi_i2c_test();
return;
}
/* Export to the msh command list */
MSH_CMD_EXPORT(i2c_wifi, i2c wifi sample);
/MSH_CMD_EXPORT函数注册i2c_wifi到 RT-Thread shell commands 通过help命令查看/

3.2 test debug
i2c测试命令:(可查看wifi_i2c_test()函数)
Write data:
i2c_wifi write 0x10 0x02 0x00
Read data:
i2c_wifi read 0x10 0x02 0
Register list:
i2c_wifi list 0x10 0 0
测试实例:


495

被折叠的 条评论
为什么被折叠?



