1 
2 #include "rtconfig.h"
3 #if defined BSP_USING_I2C_MSG
4 #include "drv_log.h"
5 #include "drv_i2c.h"
6 #define TEST_DEVICE_ADDR 0x53
7 static struct rt_i2c_bus_device *i2c_test_bus = RT_NULL;
i2c_msg_sample(int argc,char * argv[])8 int i2c_msg_sample(int argc, char *argv[])
9 {
10     rt_uint8_t write_content[] = {"Phytium Rt-thread I2C Msg Driver Test Successfully !!"};
11     rt_uint8_t write_addr[2] = {0x0, 0x0};
12     rt_uint8_t write_buf[2 + sizeof(write_content)];
13     rt_memcpy(write_buf, write_addr, 2);
14     rt_memcpy(write_buf + 2, write_content, sizeof(write_content));
15 
16     rt_uint8_t read_buf[2 + sizeof(write_content)];
17     rt_memcpy(read_buf, write_addr, 2);
18 
19     char name[RT_NAME_MAX];
20     rt_strncpy(name, "I2C3_MSG", RT_NAME_MAX);
21     i2c_test_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
22     if (i2c_test_bus == RT_NULL)
23     {
24         rt_kprintf("can't find %s device!\n", name);
25     }
26     else
27     {
28         rt_kprintf("find %s device!!!!\n", name);
29     }
30 
31     struct rt_i2c_msg write_msgs;
32     write_msgs.addr = TEST_DEVICE_ADDR;
33     write_msgs.flags = RT_I2C_WR;
34     write_msgs.buf = write_buf;
35     write_msgs.len = sizeof(write_buf);
36     rt_i2c_transfer(i2c_test_bus, &write_msgs, 1);
37 
38     struct rt_i2c_msg read_msgs;
39     read_msgs.addr = TEST_DEVICE_ADDR;
40     read_msgs.flags = RT_I2C_RD;
41     read_msgs.buf = read_buf;
42     read_msgs.len = sizeof(read_buf);
43     rt_i2c_transfer(i2c_test_bus, &read_msgs, 1);
44 
45     for (rt_uint8_t i = 0; i < sizeof(write_content); i++)
46     {
47         if (read_buf[i] != write_content[i])
48         {
49             return -RT_ERROR;
50         }
51     }
52     printf("%s\n", read_buf);
53     return RT_EOK;
54 }
55 MSH_CMD_EXPORT(i2c_msg_sample, i2c msg device sample);
56 
57 #endif