RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1606802
Accepted
Maxim Timakov
Maxim Timakov
Asked:2025-02-13 02:18:30 +0000 UTC2025-02-13 02:18:30 +0000 UTC 2025-02-13 02:18:30 +0000 UTC

在 ESP32-WROOM-32 上扫描 I2C 时出错

  • 772

ESP32-WROOM-32 上有一个 DevBoard(30 针版本、USB-Type-C、CH340C)

ESP32-Wroom-32 在此处输入图片描述

Connecting.....
Chip is ESP32-D0WD-V3 (revision v3.1)

我画出了扫描仪的代码:

// ESP-IDF v5.4
#include <stdio.h>

#include <esp_err.h>
#include <esp_system.h>
#include <driver/gpio.h>
#include <driver/i2c_master.h>
#include <driver/i2c_types.h>

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>


esp_err_t init_i2c(i2c_master_bus_handle_t * handle)
{
    i2c_master_bus_config_t bus = {
            .i2c_port = I2C_NUM_0,
            .sda_io_num = GPIO_NUM_21,
            .scl_io_num = GPIO_NUM_22,
            .clk_source = I2C_CLK_SRC_DEFAULT,
            .glitch_ignore_cnt = 7,
            .intr_priority = 0, // use default
            .trans_queue_depth = 0, // no async ops
            .flags.enable_internal_pullup = true,
            .flags.allow_pd = 0, // disallow power down
    };

    return i2c_new_master_bus(&bus, handle);
}

void app_main(void)
{
    i2c_master_bus_handle_t bus = NULL;

    gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT); // onboard blue led

    gpio_set_level(GPIO_NUM_2, true);

    ESP_ERROR_CHECK(init_i2c(&bus));

    for (uint16_t addr = 1; addr < 127; ++addr)
    {
        esp_err_t result = i2c_master_probe(bus, addr, -1); // -1 == infinity
        const char * reaction = "Unknown error";

        switch (result)
        {
            case ESP_OK:
                reaction = "OK";
                break;
            case ESP_ERR_NOT_FOUND:
                reaction = "NOT FOUND";
                break;
            case ESP_ERR_TIMEOUT:
                reaction = "TIMEOUT (bus failure)";
                break;
            default:
                break;
        }

        printf("%02X : %s (%d) \n", addr, reaction, result);
    }
    gpio_set_level(GPIO_NUM_2, false);

    ESP_ERROR_CHECK(i2c_del_master_bus(bus));

    printf("Restart:");
    for (int i = 10; i > 0; --i)
    {
        printf(" %d", i);
        fflush(stdout);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
    printf(" NOW!\n");
    esp_restart();
}

i2c_master_probe调用调试控制台时,它会抛出总线错误:

E (279) i2c.master: I2C hardware timeout detected
E (279) i2c.master: probe device timeout. Please check if xfer_timeout_ms and pull-ups are correctly set up

告诉我我可能哪里搞砸了?

附加信息:

  • 有 2 个设备连接到总线(Arduino 模块,每个模块都有自己的上拉电阻)
  • 设备正在运行 - 在 Arduino Uno 和 Raspberry Pi 上检测到
  • 当交换GPIO_NUM_21<-> GPIO_NUM_22(物理上 + 在总线设置中)时,错误消失,但未检测到设备。
c
  • 1 1 个回答
  • 28 Views

1 个回答

  • Voted
  1. Best Answer
    Maxim Timakov
    2025-02-14T05:34:42Z2025-02-14T05:34:42Z

    事实证明,我的 DevBoard 副本在使用 GPIO21 时出现了硬件问题 - 输出端的电压持续约为 2 伏。

    与其他人相连:

    • GPIO16-SDA(数据)
    • GPIO17-SCL(时钟)
    
    esp_err_t init_i2c(i2c_master_bus_handle_t * handle)
    {
        i2c_master_bus_config_t bus = {
                .i2c_port = I2C_NUM_0,
                .sda_io_num = GPIO_NUM_16,
                .scl_io_num = GPIO_NUM_17,
                .clk_source = I2C_CLK_SRC_DEFAULT,
                .glitch_ignore_cnt = 7,
                .intr_priority = 0, // use default
                .trans_queue_depth = 0, // no async ops
                .flags.enable_internal_pullup = true,
                //.flags.allow_pd = 0, // disallow power down
        };
    
        return i2c_new_master_bus(&bus, handle);
    }
    

    此后,问题中的代码就起作用了。

    还发现了旧版本 I2C 驱动程序的代码:

    #include <driver/i2c.h>
    void app_main(void)
    {
        {
            i2c_config_t cfg = {
                .mode = I2C_MODE_MASTER,
                .sda_io_num = GPIO_NUM_16,
                .scl_io_num = GPIO_NUM_17,
                .sda_pullup_en = GPIO_PULLUP_ENABLE,
                .scl_pullup_en = GPIO_PULLUP_ENABLE,
                .master.clk_speed = 100000,
                .clk_flags = 0
            };
            
            ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &cfg));
            
            ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER
                , 0
                , 0
                , 0
            ));
        }
        while (true) {
            printf("Start scan\n");
            esp_err_t res;
            printf("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n");
            printf("00:         ");
            for (uint8_t i = 3; i < 0x78; i++)
            {
                i2c_cmd_handle_t cmd = i2c_cmd_link_create();
                i2c_master_start(cmd);
                i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1 /* expect ack */);
                i2c_master_stop(cmd);
        
                res = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
                if (i % 16 == 0)
                    printf("\n%.2x:", i);
                if (res == 0)
                    printf(" %.2x", i);
                else
                    printf(" --");
                i2c_cmd_link_delete(cmd);
            }
            printf("\n\n");
            sleep(1);
        }
    }
    
    

    使用 Arduino 框架中的 Wire 时也存在类似的问题。
    为了解决这个问题,您需要明确指定所使用的引脚:

    void setup() {
      Serial.begin(115200);
      Wire.begin(GPIO_NUM_16, GPIO_NUM_17); // sda, scl
    }
    
    • 0

相关问题

  • free 出于某种原因不会从内存中删除数组

  • 请帮助代码

  • 为什么 masm 对字符串或文本文字太长发誓,为什么在结构中设置 db 或 dw?

  • 如何将数字拆分为位并将其写入 C 中的数组?

  • 如何以给定的角度移动物体?

  • 解决“子集和问题”的时效算法

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5