• ISL1208时钟芯片 Linux下 i2c 读写示例。


    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #define DEVICE_NAME "/dev/i2c-1"
    17. #define I2C_ADDR 0x6f // i2c设备地址
    18. static int i2c_write_bytes(int fd, uint8_t slave_addr, uint8_t reg_addr, uint8_t *values, uint8_t len)
    19. {
    20. uint8_t *outbuf = NULL;
    21. struct i2c_rdwr_ioctl_data packets;
    22. struct i2c_msg messages[1];
    23. outbuf = (uint8_t *)malloc(len + 1);
    24. if (!outbuf)
    25. {
    26. printf("Error: No memory for buffer\n");
    27. return -1;
    28. }
    29. outbuf[0] = reg_addr; // i2c设备要操作的reg地址
    30. memcpy(outbuf + 1, values, len);
    31. messages[0].addr = slave_addr; // i2c设备地址
    32. messages[0].flags = 0; // write flag
    33. messages[0].len = len + 1;
    34. messages[0].buf = outbuf; // 向reg写入的值
    35. /* Transfer the i2c packets to the kernel and verify it worked */
    36. packets.msgs = messages;
    37. packets.nmsgs = 1;
    38. if (ioctl(fd, I2C_RDWR, &packets) < 0)
    39. {
    40. printf("Error: Unable to send data");
    41. free(outbuf);
    42. return -1;
    43. }
    44. free(outbuf);
    45. return 0;
    46. }
    47. static int i2c_read_bytes(int fd, uint8_t slave_addr, uint8_t reg_addr, uint8_t *values, uint8_t len)
    48. {
    49. uint8_t outbuf[1];
    50. struct i2c_rdwr_ioctl_data packets;
    51. struct i2c_msg messages[2];
    52. outbuf[0] = reg_addr; // i2c设备要操作的reg地址
    53. messages[0].addr = slave_addr; // i2c设备地址
    54. messages[0].flags = 0; // write flag
    55. messages[0].len = sizeof(outbuf);
    56. messages[0].buf = outbuf;
    57. /* The data will get returned in this structure */
    58. messages[1].addr = slave_addr; // i2c设备地址
    59. messages[1].flags = I2C_M_RD /* | I2C_M_NOSTART*/; // write flag
    60. messages[1].len = len;
    61. messages[1].buf = values;
    62. /* Send the request to the kernel and get the result back */
    63. packets.msgs = messages;
    64. packets.nmsgs = 2;
    65. if (ioctl(fd, I2C_RDWR, &packets) < 0)
    66. {
    67. printf("Error: Unable to send data");
    68. return -1;
    69. }
    70. return 0;
    71. }
    72. int main()
    73. {
    74. int fd;
    75. bool cmdIsRd = false;
    76. char *arg_ptr = NULL;
    77. unsigned long len;
    78. unsigned int slave_addr, reg_addr;
    79. uint8_t buffer[1024];
    80. /* 2.打开I2C总线 */
    81. fd = open(DEVICE_NAME, O_RDWR);
    82. if (fd < 0)
    83. {
    84. printf("can not open file %s\n", DEVICE_NAME);
    85. return -1;
    86. }
    87. len = 2;
    88. buffer[0] = 0x11;
    89. buffer[1] = 0x12;
    90. // Alarm
    91. i2c_write_bytes(fd, I2C_ADDR, 0x0c, buffer, len);
    92. // RTC
    93. len = 12;
    94. i2c_read_bytes(fd, I2C_ADDR, 0x00, buffer, len);
    95. printf("read data =");
    96. for (int i = 0; i < len; i++)
    97. {
    98. printf("0x%02X ", buffer[i]);
    99. }
    100. printf("\n");
    101. return 0;
    102. }

    一、设备地址是8bit
    1、i2cdetect检测有几组i2c总线

                   i2cdetect -l

    2、i2cdetect检测挂载在i2c总线上器件

                   i2cdetect-r -y 1(检测i2c-1上的挂载情况)

    3、i2cdump查看器件所有寄存器的值

                 i2cdump -f -y 1 0x50 (查看i2c-1总线上0x50设备的所有寄存器值)

    4、i2cset设置单个寄存器值

                  i2cset -f -y 1 0x50 0x01 0xaa (往i2c-1总线上0x50设备0x01寄存器写0xaa)

    5、i2cget读取单个寄存器值

                   i2cget -f -y 1 0x50 0x01     (读取i2c-1总线上0x50设备0x01寄存器的值)


    二、设备地址是16bit
    例子:i2c总线1上挂设备,设备地址是 0x3020 ,要读取16个字节
                  i2ctransfer -y -f 1 w2@0x10 0x30 0x20 r16
    #w是写,2是写入2个字节,@0x10是寄存器地址,030 0x20是寄存器要设置的地址的高低位,r是读取,16 是读取16个字节

    例子:i2c总线1上挂设备,设备地址是 0x3020 ,要写16个字节数据,数据是0x55 0xaa
                    i2ctransfer -y -f 1 w4@0x10 0x30 0x20 0x55 0xaa
    #w是写,4是写入4个字节,@0x10是寄存器地址,0x30 0x20是寄存器要设置的地址的高低位,0x55 0xaa是要写入的值

  • 相关阅读:
    java插入百万级别的数据解决方案
    github-将本地代码上传到github上
    Linux知识汇总
    VMware使用Ubuntu安装搭建FusionCompute实验平台
    Nginx 从入门到精通-Nginx-Web服务器的瑞士军刀
    艾美捷支原体检测试剂盒说明书
    无胁科技-TVD每日漏洞情报-2022-8-10
    安卓考试答题APP源码
    你犯过程序员容易犯的这些错误吗?快来看看!
    aop注解快速实现数据脱敏返回
  • 原文地址:https://blog.csdn.net/2004v2004/article/details/134510179