i2c_sample.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "rtconfig.h"
  2. #if defined BSP_USING_I2C
  3. #include "drv_log.h"
  4. #include "drv_i2c.h"
  5. #include "fi2c.h"
  6. #include "fi2c_hw.h"
  7. #include "fio_mux.h"
  8. #include "fmio_hw.h"
  9. #include "fmio.h"
  10. #include "fparameters.h"
  11. static struct rt_i2c_bus_device *i2c_test_bus = RT_NULL;
  12. int i2c_sample(int argc, char *argv[])
  13. {
  14. char name[RT_NAME_MAX];
  15. #if defined(FIREFLY_DEMO_BOARD)
  16. rt_strncpy(name, "MIO1", RT_NAME_MAX);
  17. #endif
  18. #if defined(E2000D_DEMO_BOARD)||defined(E2000Q_DEMO_BOARD)
  19. rt_strncpy(name, "MIO15", RT_NAME_MAX);
  20. #endif
  21. i2c_test_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
  22. rt_uint8_t read_buf[2] = {0x0, 0x0};
  23. rt_uint8_t write_buf[2] = {0x0, 0x1};
  24. if (i2c_test_bus == RT_NULL)
  25. {
  26. rt_kprintf("can't find %s device!\n", name);
  27. }
  28. else
  29. {
  30. rt_kprintf("find %s device!!!!\n", name);
  31. }
  32. struct rt_i2c_msg read_msgs;
  33. read_msgs.addr = 0x50;
  34. read_msgs.flags = RT_I2C_RD;
  35. read_msgs.buf = read_buf;
  36. read_msgs.len = 1;
  37. rt_i2c_transfer(i2c_test_bus, &read_msgs, 1);
  38. rt_kprintf("read_buf = %x\n", *read_msgs.buf);
  39. struct rt_i2c_msg write_msgs;
  40. write_msgs.addr = 0x50;
  41. write_msgs.flags = RT_I2C_WR;
  42. write_msgs.buf = write_buf;
  43. write_msgs.len = 1;
  44. rt_i2c_transfer(i2c_test_bus, &write_msgs, 1);
  45. read_buf[0] = 0x02;
  46. rt_i2c_transfer(i2c_test_bus, &read_msgs, 1);
  47. rt_kprintf("read_buf = %x\n", *read_msgs.buf);
  48. return RT_EOK;
  49. }
  50. MSH_CMD_EXPORT(i2c_sample, i2c device sample);
  51. #endif