{"id":245466,"date":"2022-07-01T09:02:19","date_gmt":"2022-07-01T01:02:19","guid":{"rendered":"https:\/\/gulass.cn\/?p=245466"},"modified":"2022-06-23T18:05:16","modified_gmt":"2022-06-23T10:05:16","slug":"debug-i2c","status":"publish","type":"post","link":"https:\/\/gulass.cn\/debug-i2c.html","title":{"rendered":"\u5982\u4f55debug\u901a\u4fe1\u534f\u8bae I2C \u5b50\u7cfb\u7edf"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u5e94\u8be5\u6536\u5230 ACK \u4fe1\u53f7\u7684\u65f6\u5019\u6ca1\u6709\u6536\u5230 ACK \u4fe1\u53f7\uff0ci2c controller \u5c31\u4f1a\u4ea7\u751f\u4e00\u4e2a ACK error \u7684\u4e2d\u65ad\uff0c\u544a\u8bc9 i2c driver \u53d1\u751f\u4e86 ACK error\u3002\u901a\u5e38\u60c5\u51b5\u662f slave \u672c\u8eab\u7684\u95ee\u9898\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

I2C \u5e38\u89c1\u6709\u4e24\u79cd\u9519\u8bef\uff1aI2C ACK error\u3001I2C timeout<\/p>\n

\u4e00\u3001I2C ACK error<\/strong><\/div>\n

\u5728\u5e94\u8be5\u6536\u5230 ACK \u4fe1\u53f7\u7684\u65f6\u5019\u6ca1\u6709\u6536\u5230 ACK \u4fe1\u53f7\uff0ci2c controller \u5c31\u4f1a\u4ea7\u751f\u4e00\u4e2a ACK error \u7684\u4e2d\u65ad\uff0c\u544a\u8bc9 i2c driver \u53d1\u751f\u4e86 ACK error\u3002\u901a\u5e38\u60c5\u51b5\u662f slave \u672c\u8eab\u7684\u95ee\u9898\u3002<\/p>\n

\"\"<\/p>\n

1\u3001\u68c0\u67e5 device \u662f\u5426\u5b58\u5728\uff0ci2c bus number \u548c device address \u662f\u5426\u6b63\u786e\u3002\u793a\u4f8b\u5982\u4e0b\uff1ai2c number \u4e3a 6\uff0caddr \u4e3a 0x28\uff1a<\/p>\n

[31.092951][xxx]i2c i2c-6:addr:0x28,ACK error<\/pre>\n

2\u3001\u68c0\u67e5 device \u662f\u5426\u5df2\u7ecf\u4e0a\u7535\u4f7f\u80fd\uff0c\u4ee5\u53ca\u6b63\u786e\u7684 init\u3002<\/p>\n

3\u3001\u68c0\u67e5 i2c speed \u662f\u5426\u9002\u914d\uff0cspeed \u5927\u4e8e device \u652f\u6301\u7684 max speed \u4e5f\u4f1a\u9020\u6210 ACK Error\u3002\u964d\u4f4e\u901f\u5ea6\uff0c\u5982\u679c\u4ecd\u7136\u53ef\u4ee5\u5de5\u4f5c\u7684\u8bdd\u5c31\u8bf4\u660e\u662f clk \u76f8\u5173\u95ee\u9898\u3002<\/p>\n

4\u3001\u68c0\u67e5 i2c device \u4fe1\u53f7\u7535\u5e73\u662f\u5426\u4e0e AP \u5339\u914d\u3002<\/p>\n

5\u3001GPIO check \u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\uff1a<\/p>\n

  • GPIO \u7535\u6d41\u9a71\u52a8\u80fd\u529b\u3002<\/li>\n
  • GPIO \u5de5\u4f5c\u6a21\u5f0f\u662f\u5426\u662f I2C \u6a21\u5f0f\u3002<\/li>\n
  • GPIO \u662f\u5426\u6709\u5185\u90e8\u4e0a\u62c9\u7535\u963b\u3002<\/li>\n
  • GPIO \u9ed8\u8ba4\u7535\u5e73\u72b6\u6001\u3002<\/li>\n

    \u4ece i2c spec \u770b\uff0c\u5982\u4e0b\u60c5\u51b5 NACK \u662f\u6b63\u5e38\u7684\u3002<\/p>\n

    I2C \u5199<\/p>\n

    \"\"<\/p>\n

    \u4e3b\u673a\u5411\u4ece\u673a\u53d1\u9001\u6570\u636e\u65f6\uff0c\u6700\u540e\u4e00\u4e2a Byte \u6570\u636e\u65f6\uff0c\u4ece\u673a\u53ef\u80fd\u5e94\u7b54\u4e5f\u53ef\u80fd\u975e\u5e94\u7b54\uff0c\u4f46\u4e0d\u600e\u6837\u4e3b\u673a\u90fd\u53ef\u4ee5\u4ea7\u751f\u505c\u6b62\u6761\u4ef6\uff0c\u5982\u679c\u4e3b\u673a\u5728\u5411\u4ece\u673a\u53d1\u9001\u6570\u636e\uff08\u751a\u81f3\u5305\u62ec\u4ece\u673a\u5730\u5740\u5728\u5185\uff09\u65f6\u68c0\u6d4b\u5230\u4ece\u673a\u975e\u5e94\u7b54\uff0c\u5219\u4f1a\u53ca\u65f6\u505c\u6b62\u4f20\u8f93\u3002<\/p>\n

    I2C \u8bfb<\/p>\n

    \"\"<\/p>\n

    \u4e3b\u673a\u4ece\u4ece\u673a\u63a5\u6536\u6570\u636e\u65f6\uff0c\u6700\u540e\u4e00\u4e2a Byte \u6570\u636e\u65f6\uff0c\u4e3b\u673a\u4e0d\u56de\u5e94\u8be5\u4ece\u673a\uff0c\u5373 NACK\u3002<\/p>\n

    \u4e8c\u3001I2C timeout<\/strong><\/div>\n

    \u5f53 I2C \u4f20\u8f93\u53d1\u751f timeout \u65f6\uff0c\u4e00\u822c kernel log \u4f1a\u6709\u7c7b\u4f3c\u5982\u4e0b\u6253\u5370\uff1a<\/p>\n

    [48.197718][xxx]i2c i2c-1: addr:0xa,transfer timeout<\/pre>\n

    \"\"<\/p>\n

    1\u3001GPIO check \u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\u3002<\/p>\n

  • GPIO \u7535\u6d41\u9a71\u52a8\u80fd\u529b\u3002<\/li>\n
  • GPIO \u5de5\u4f5c\u6a21\u5f0f\u662f\u5426\u662f I2C \u6a21\u5f0f\u3002<\/li>\n
  • GPIO \u662f\u5426\u6709\u5185\u90e8\u4e0a\u62c9\u7535\u963b\u3002<\/li>\n
  • GPIO \u9ed8\u8ba4\u7535\u5e73\u72b6\u6001\u3002<\/li>\n

    2\u3001\u6392\u67e5 slave \u987a\u5e8f\u3002<\/p>\n

  • log \u4e2d\u7b2c\u4e00\u4e2a\u53d1\u751f timeout \u7684 slave\u3002<\/li>\n
  • \u6709 power \u63a7\u5236\u548c reset \u63a7\u5236\u7684 slave\u3002<\/li>\n
  • other slave\u3002<\/li>\n

    \u590d\u73b0\u95ee\u9898\u540e\uff0c\u53ef\u4ee5\u624b\u52a8\u5c06\u76f8\u5e94\u5916\u8bbe\u53bb\u6389\uff0c\u786e\u8ba4\u662f\u54ea\u4e2a\u5916\u8bbe\u5c06 i2c bus \u62c9\u4f4f\uff0c\u518d\u4e0e\u4f9b\u5e94\u5546\u6c9f\u901a\uff0cdebug \u4e00\u4e0b\u8be5 IC \u72b6\u6001\uff0c\u7406\u6e05\u62c9\u4f4f i2c bus \u539f\u56e0\u3002<\/p>\n

    \u4e09\u3001i2c-tools<\/strong><\/div>\n

    i2c-tools \u4e5f\u5f88\u597d\u7528\uff0c\u8fd9\u4e2a\u5de5\u5177\u5de5\u5177\u535a\u4e3b\u4e0a\u6b21\u5199\u8fc7\uff0c\u53c2\u8003\u4ee5\u4e0b\u6587\u7ae0\uff1a<\/p>\n

    \u200b\u200b\u624b\u628a\u624b\u6559\u4f60\u4f7f\u7528 i2c-tools\u3002\u200b<\/p>\n

    \u56db\u3001\u5e38\u89c1\u95ee\u9898<\/strong><\/div>\n
    1\u3001\u540c\u4e00\u6761 i2c bus \u4e0a\u6240\u6709\u7684\u5916\u8bbe\u7684 i2c addr \u8981\u4e0d\u540c<\/strong><\/span><\/div>\n

    \uff081\uff09\u76f8\u540c address \u6ce8\u518c\u65f6\u51b2\u7a81.<\/p>\n

    [2.059184][xxx]i2c i2c-1:Failed to register i2c client 24c02 at 0x51(-16) \r\n[2.059189][xxx]i2c i2c-1:Can't create device at 0x51<\/pre>\n

    \u5bf9\u5e94\u7684\u9519\u8bef\u7801\u662f -16\u3002<\/p>\n

    \/kernel-5.10\/include\/uapi\/asm-generic\/errno-base.h\r\n#define EPERM   1 \/* Operation not permitted *\/\r\n#define ENOENT   2 \/* No such file or directory *\/\r\n#define ESRCH   3 \/* No such process *\/\r\n#define EINTR   4 \/* Interrupted system call *\/\r\n#define EIO   5 \/* I\/O error *\/\r\n#define ENXIO   6 \/* No such device or address *\/\r\n#define E2BIG   7 \/* Argument list too long *\/\r\n#define ENOEXEC   8 \/* Exec format error *\/\r\n#define EBADF   9 \/* Bad file number *\/\r\n#define ECHILD  10 \/* No child processes *\/\r\n#define EAGAIN  11 \/* Try again *\/\r\n#define ENOMEM  12 \/* Out of memory *\/\r\n#define EACCES  13 \/* Permission denied *\/\r\n#define EFAULT  14 \/* Bad address *\/\r\n#define ENOTBLK  15 \/* Block device required *\/\r\n#define EBUSY  16 \/* Device or resource busy *\/\r\n......<\/pre>\n

    \u53ef\u4ee5\u6267\u884c ls \/sys\/bus\/i2c\/devices \u67e5\u770b\u5bf9\u5e94\u7684 i2c-1 \u4e0b\u662f\u5426\u5df2\u7ecf\u6709\u6ce8\u518c\u76f8\u540c\u7684 address \u7684\u5916\u8bbe\u3002<\/p>\n

    \u82e5\u8fd4\u56de -11\uff0c-EAGAIN\u3002\u4ee3\u8868\u603b\u7ebf\u6b63\u5fd9\uff0c\u6216\u65e0\u6cd5\u7533\u8bf7\u5230\u603b\u7ebf\u9501\u3002\u5982\u679c\u603b\u7ebf\u6b63\u5fd9\uff0c\u8bf7 retry \u7b49\u5f85\uff0c\u6216\u67e5\u770b\u662f\u54ea\u4e2a device \u4e00\u76f4\u5728\u53d1\u9001\u3002\u5982\u679c\u65e0\u6cd5\u7533\u8bf7\u5230\u603b\u7ebf\u9501\uff0c\u8bf7\u67e5\u770b\u662f\u5426\u5728\u4e2d\u65ad\u51fd\u6570\u6216\u539f\u5b50\u4e0a\u4e0b\u6587\u8c03\u7528\u4e86 i2c_transfer\u3002<\/p>\n

    \uff082\uff09\u9690\u85cf\u7684 i2c address\uff0c\u5373\u5916\u8bbe\u5b58\u5728\u591a\u4e2a i2c addr \u6216\u5916\u8bbe HW bug\uff0c\u5bfc\u81f4 i2c \u901a\u8baf\u5f02\u5e38\u3002<\/p>\n

    \u793a\u4f8b\uff1aeeprom \u5728 i2c-1 \u4e0a\u6ce8\u518c\u4e86 0x50 \u5730\u5740\uff0c\u800c type c \u867d\u7136\u6ce8\u518c\u5230 0x60 \u5730\u5740\uff0c\u4f46\u5bf9 0x50 \u4e5f\u80fd\u4ea7\u751f\u54cd\u5e94\uff0ctype c \u62c9\u4f4e\u4e86 SDA \uff0c\u4ece\u800c timeout \u3002<\/p>\n

    debug \u65b9\u6cd5\uff1a<\/p>\n

  • \u8f6f\u4ef6\u4e0a\u786e\u8ba4\u6570\u636e\u53ca\u5bf9\u5e94\u7684 driver \u662f\u5426\u6b63\u786e\u3002<\/li>\n
  • \u786c\u4ef6\u4e0a\u9010\u4e00\u53bb\u6389\u5916\u8bbe\uff0c\u786e\u8ba4\u662f\u54ea\u4e2a\u5916\u8bbe\u5bfc\u81f4\u3002<\/li>\n
    2\u3001\u793a\u6ce2\u5668 ACK \u7684\u5730\u65b9\u6709\u6bdb\u523a<\/strong><\/span><\/div>\n

    slave \u5728\u7b2c 9 \u4e2a clk \u4ea7\u751f ACK \u5e94\u7b54\u540e\uff0c\u6362\u6210 mater \u7aef\u6765\u63a7\u5236\u65f6\u4ea7\u751f\u7684\u6bdb\u523a\u3002\u6b64\u6bdb\u523a\u4e0d\u4f1a\u5f71\u54cd I2C \u603b\u7ebf\u8bfb\u5199\u65f6\u5e8f\uff0c\u65e0\u9700\u5904\u7406\u3002<\/p>\n

    \u5373 slave \u548c master \u63a7\u5236\u603b\u7ebf\u5207\u6362\u95f4\u9694\uff0c\u6ca1\u6709\u4eba\u63a7\u5236\u603b\u7ebf\uff0c\u5e26\u6765\u7684\u6bdb\u523a\u3002<\/p>\n

    3\u3001\u534a\u9ad8\u7535\u5e73<\/strong><\/span><\/div>\n

    \u5728\u5916\u63a5\u4e0a\u62c9\u7535\u963b\u7684\u60c5\u51b5\u4e0b\uff0c\u6709 enable \u5185\u90e8\u4e0b\u62c9\u7535\u963b\uff0c\u5bfc\u81f4\u603b\u7ebf\u4e0a\u6709\u534a\u9ad8\u7535\u5e73\u3002<\/p>\n

    4\u3001\u603b\u7ebf\u4e0a\u7535\u5e73\u62c9\u4e0d\u5230\u5730<\/strong><\/span><\/div>\n

    master \u7aef\u53d1\u9001\u6570\u636e\u65f6\u7535\u5e73\u62c9\u4e0d\u5230\u5730\uff0c\u53ef\u4ee5\u589e\u5927\u9a71\u52a8\u7535\u6d41\u6216\u8005\u4e0a\u62c9\u7535\u963b\u3002<\/p>\n

    slave \u7aef\u62c9\u4e0d\u5230\u5730\uff0c\u53ef\u4ee5\u54a8\u8be2\u4f9b\u5e94\u5546\u770b\u662f\u5426\u80fd\u589e\u5927 slave \u7aef\u9a71\u52a8\u7535\u6d41\u6216\u8005\u4e0a\u62c9\u7535\u963b\u3002<\/p>\n

    5\u3001RK \u5e73\u53f0 I2C debug<\/strong><\/span><\/div>\n

    \u6765\u6e90\u4e8e firefly\u3002<\/p>\n

    https:\/\/wiki.t-firefly.com\/zh_CN\/Firefly-RK3399\/driver_i2c.html<\/pre>\n

    I2C \u901a\u4fe1\u5931\u8d25\uff0c\u51fa\u73b0 log: \u201ctimeout, ipd: 0x00, state: 1\u201d<\/p>\n

    \u8bf7\u68c0\u67e5\u786c\u4ef6\u4e0a\u62c9\u662f\u5426\u7ed9\u7535\u3002<\/p>\n

    \u8c03\u7528 i2c_transfer \u8fd4\u56de\u503c\u4e3a -6\uff1f<\/p>\n

    \u8fd4\u56de\u503c\u4e3a -6 \u8868\u793a\u4e3a NACK \u9519\u8bef\uff0c\u5373\u5bf9\u65b9\u8bbe\u5907\u65e0\u5e94\u7b54\u54cd\u5e94\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e00\u822c\u4e3a\u5916\u8bbe\u7684\u95ee\u9898\uff0c\u5e38\u89c1\u7684\u6709\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a<\/p>\n

  • I2C \u5730\u5740\u9519\u8bef\uff0c\u89e3\u51b3\u65b9\u6cd5\u662f\u6d4b\u91cf I2C \u6ce2\u5f62\uff0c\u786e\u8ba4\u662f\u5426 I2C \u8bbe\u5907\u5730\u5740\u9519\u8bef\uff1b<\/li>\n
  • I2C slave \u8bbe\u5907\u4e0d\u5904\u4e8e\u6b63\u5e38\u5de5\u4f5c\u72b6\u6001\uff0c\u6bd4\u5982\u672a\u7ed9\u7535\uff0c\u9519\u8bef\u7684\u4e0a\u7535\u65f6\u5e8f\u7b49\uff1b<\/li>\n
  • \u65f6\u5e8f\u4e0d\u7b26\u5408 I2C slave \u8bbe\u5907\u6240\u8981\u6c42\u4e5f\u4f1a\u4ea7\u751f Nack \u4fe1\u53f7\u3002<\/li>\n

    \u5f53\u5916\u8bbe\u5bf9\u4e8e\u8bfb\u65f6\u5e8f\u8981\u6c42\u4e2d\u95f4\u662f stop \u4fe1\u53f7\u4e0d\u662f repeat start \u4fe1\u53f7\u7684\u65f6\u5019\uff0c\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/p>\n

    \u8fd9\u65f6\u9700\u8981\u8c03\u7528\u4e24\u6b21 i2c_transfer, I2C read \u62c6\u5206\u6210\u4e24\u6b21\uff0c\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n

    static int i2c_read_bytes(struct i2c_client *client, u8 cmd, u8 *data, u8 data_len) {struct i2c_msg msgs[2];\r\n     int ret;\r\n     u8 *buffer;\r\n     buffer = kzalloc(data_len, GFP_KERNEL);\r\n     if (!buffer)\r\n         return -ENOMEM;\r\n     msgs[0].addr = client->addr;\r\n     msgs[0].flags = client->flags;\r\n     msgs[0].len = 1;\r\n     msgs[0].buf = &cmd;\r\n     ret = i2c_transfer(client->adapter, msgs, 1);\r\n     if (ret < 0) {\r\n      dev_err(&client->adapter->dev, \"i2c read failed\\n\");\r\n            kfree(buffer);\r\n       return ret;\r\n      }\r\n      msgs[1].addr = client->addr;\r\n      msgs[1].flags = client->flags | I2C_M_RD;\r\n      msgs[1].len = data_len;\r\n      msgs[1].buf = buffer;\r\n      ret = i2c_transfer(client->adapter, &msgs[1], 1);\r\n      if (ret < 0)\r\n          dev_err(&client->adapter->dev, \"i2c read failed\\n\");\r\n      else\r\n        memcpy(data, buffer, data_len);\r\n      kfree(buffer);\r\n      return ret;\r\n}<\/pre>\n

    \u76f8\u4fe1\u4ee5\u4e0a\u7684 I2C debug \u65b9\u6cd5\u5df2\u7ecf\u80fd\u4e3a\u5927\u5bb6\u89e3\u51b3\u5927\u90e8\u5206\u95ee\u9898\uff0c\u5982\u679c\u8fd8\u662f\u6ca1\u89e3\u51b3\uff0c\u4e00\u822c\u662f\u82af\u7247\u95ee\u9898\u6216\u8005\u539f\u5382\u5e95\u5c42 code bug\uff0c\u53ef\u4ee5\u627e\u82af\u7247\u539f\u5382\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    I2C \u5e38\u89c1\u6709\u4e24\u79cd\u9519\u8bef\uff1aI2C ACK error\u3001I2C timeout \u5728\u5e94\u8be5\u6536\u5230 ACK \u4fe1\u53f7\u7684\u65f6\u5019\u6ca1\u6709 […]<\/p>\n","protected":false},"author":1920,"featured_media":235674,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-245466","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/245466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=245466"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/245466\/revisions"}],"predecessor-version":[{"id":245472,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/245466\/revisions\/245472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/235674"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=245466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=245466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=245466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}