i2c_sample.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2020-12-15 liuhy first implementation.
  21. */
  22. /*
  23. * 程序清单:这是一个 I2C 设备使用例程
  24. * 例程导出了 i2c_io_sample 命令到控制终端
  25. * 命令调用格式:i2c_io_sample
  26. * 命令解释:使用默认的I2C总线设备i2c0
  27. * 程序功能:通过 I2C 设备接收数据并打印,然后将接收的字符加1输出。
  28. */
  29. #include <rtthread.h>
  30. #include <rtdevice.h>
  31. #ifdef RT_USING_I2C
  32. #define I2C_BUS_NAME "i2c0" /* I2C总线设备名称 */
  33. #define SLAVE_ADDR 0x2D /* 从机地址 */
  34. #define STR_LEN 16 /* 接收发送的数据长度 */
  35. static void i2c_io_sample(int argc, char *argv[])
  36. {
  37. struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C总线设备句柄 */
  38. struct rt_i2c_msg temp_msg; /* I2C消息 */
  39. rt_uint8_t buffer[STR_LEN] = { 0U };
  40. rt_uint32_t i,num_msg;
  41. rt_size_t s_stat;
  42. i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(I2C_BUS_NAME); /* 通过名字获取I2C总线设备的句柄 */
  43. if( i2c_bus == RT_NULL)
  44. {
  45. rt_kprintf("can't find i2c device :%s !\n",I2C_BUS_NAME);
  46. return;
  47. }
  48. /*初始化消息*/
  49. temp_msg.addr = SLAVE_ADDR; /* 从机地址 */
  50. temp_msg.len = STR_LEN; /* 传输的数据长度 */
  51. temp_msg.buf = buffer; /* 读写缓存器 */
  52. num_msg = 1; /* 传输一条消息 */
  53. temp_msg.flags = RT_I2C_RD; /* I2C读 */
  54. s_stat = rt_i2c_transfer(i2c_bus,&temp_msg,num_msg); /* 传输消息 */
  55. rt_thread_mdelay(400);
  56. if( s_stat == num_msg )
  57. {
  58. rt_kprintf("receive successful. \n receive messege : %s \n:",buffer);
  59. for( i = 0 ; i < STR_LEN ; i++)
  60. rt_kprintf(" %x",(unsigned int)buffer[i]);
  61. rt_kprintf("\n");
  62. }
  63. else
  64. {
  65. rt_kprintf("device s% recieve fail \n buffer : s%\n",I2C_BUS_NAME,buffer);
  66. return;
  67. }
  68. for( i = 0 ; i < STR_LEN ; i++)
  69. buffer[i]++;
  70. temp_msg.flags = RT_I2C_WR; /* I2C写 */
  71. s_stat = rt_i2c_transfer(i2c_bus,&temp_msg,num_msg); /* 传输一条 */
  72. rt_thread_mdelay(400);
  73. if( s_stat == num_msg )
  74. {
  75. rt_kprintf(" send successful \n messege : %s \n:",buffer);
  76. for( i = 0 ; i < STR_LEN ; i++)
  77. rt_kprintf(" %x",(unsigned int)buffer[i]);
  78. rt_kprintf("\n");
  79. }
  80. else
  81. {
  82. rt_kprintf("device s% send fail \n",I2C_BUS_NAME);
  83. return;
  84. }
  85. return;
  86. }
  87. /* 导出到 msh 命令列表中 */
  88. MSH_CMD_EXPORT(i2c_io_sample, i2c io sample);
  89. #endif