Arduino ESP32 I2C可以为从属设置自定义引脚(客户端)

发布于 2025-01-17 21:27:39 字数 1536 浏览 6 评论 0原文

是否可以在Arduino环境中为I2C 从属(客户端,外围)设置自定义引脚?

上下文:

在ESP32(来自Freenove的ESP32-Wrover)上,我正在尝试与

此特定的ESP32-ESP32-ESP32-ESP32-ESP32-ESP32-ESP板上的I2C Masters(Adafruit Monster M4SK)进行通信。相机使用默认的SDA和SCL引脚。 因此,我必须在不同的引脚上设置I2C。我将引脚2用作SDA,并将引脚15用作SDL。

我可以轻松地在这些引脚上设置I2C作为主人,使用Wire.Begin(2,15)可以正常工作。我发现有关设置自定义销,使用电线或二线的多个公共汽车的大量文档。

我真正想做的就是这样的事情:

#include <Wire.h>

#define SDA1 2
#define SCL1 15

#define SDA2 21
#define SCL2 22

#define SLAVE_ADDRESS_ON_BUS_1 0x52
#define SLAVE_ADDRESS_ON_BUS_2 0x33

setup()
{
  Wire.begin(SDA1,SCL1,SLAVE_ADDRESS_ON_BUS_1); // Join I2C bus 1 using pins 2 and 15
  Wire1.begin(SDA2,SCL2,SLAVE_ADDRESS_ON_BUS_2);// Join I2C bus 2 using pins 21 and 22


  Wire.onReceive(receiveI2CBus1Event); // register event for when master on i2c bus 1  writes
  Wire.onRequest(WriteToI2CBus1Event); // register event for when master1 wants on i2c bus 2 wants to read

  Wire1.onReceive(receiveI2CBus2Event); // register event for when master on i2c bus 2 writes
  Wire1.onRequest(WriteToI2CBus2Event); // register event for when master on i2c bus 2  wants to read
}

据我所知,没有办法使用电线或两次互二线来在自定义的引脚上创建外围...

Wire.begin(MY_ADDRESS);

我尝试重新定义SDA和SCL,但是这似乎不起作用 请注意,我正在运行Expressif的ESP32库v2.0.2(ESP32库1.0.6,PIROR不支持ESP32作为从 我尝试使用Arduino IDE 1.8.19和Arduino IDE 2.0.0.RC5

i cose Cose 是第一个人,试图将ESP32作为一个作为一种行为I2C从使用默认引脚以外的其他东西...

我是否必须求助于某种I2C桥/开关/MUX?如果是这样,有什么建议吗? (最好是使用Arduino示例代码的建议,显示主如何将地址分配给另一个主人)

Is it possible to set custom pins for an I2C slave (client, peripheral) within the Arduino environment?

Context:

On an ESP32 (ESP32-WROVER from Freenove) I am trying to communicate with 2 devices that are I2C masters (Adafruit Monster M4sk)

On this particular ESP32-WROVER board the default SDA and SCL pins are used by a camera.
So I have to set up I2c on different pins. I'm using pin 2 as SDA and pin 15 as SDL.

I can easily set up I2C as a MASTER on those pins, works just fine using Wire.begin(2,15). I find lots of documentation about setting custom pins, multiple busses using Wire or TwoWire.

What I really want to do is something like this:

#include <Wire.h>

#define SDA1 2
#define SCL1 15

#define SDA2 21
#define SCL2 22

#define SLAVE_ADDRESS_ON_BUS_1 0x52
#define SLAVE_ADDRESS_ON_BUS_2 0x33

setup()
{
  Wire.begin(SDA1,SCL1,SLAVE_ADDRESS_ON_BUS_1); // Join I2C bus 1 using pins 2 and 15
  Wire1.begin(SDA2,SCL2,SLAVE_ADDRESS_ON_BUS_2);// Join I2C bus 2 using pins 21 and 22


  Wire.onReceive(receiveI2CBus1Event); // register event for when master on i2c bus 1  writes
  Wire.onRequest(WriteToI2CBus1Event); // register event for when master1 wants on i2c bus 2 wants to read

  Wire1.onReceive(receiveI2CBus2Event); // register event for when master on i2c bus 2 writes
  Wire1.onRequest(WriteToI2CBus2Event); // register event for when master on i2c bus 2  wants to read
}

As far as I can tell there is no way to use either Wire or TwoWire to create a peripheral on a custom set of pins...

Wire.begin(MY_ADDRESS);

I have tried re-defining SDA and SCL but that does not seem to work
Note I am running Expressif's ESP32 libraries v2.0.2 (ESP32 Libraries 1.0.6 and prior did not support ESP32 as a slave)
I have tried this (this being redefining SDA and SCL) using both Arduino IDE 1.8.19 and Arduino IDE 2.0.0.rc5

I can't be the first person trying to have an ESP32 act as an i2c slave using something other than the default pins...

Am I gonna have to resort to some sort of I2C bridge/switch/mux? if so, any recommendations? (preferably recommendations with arduino sample code showing how a master can assign an address to another master)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2025-01-24 21:27:40

你已经非常接近了,只是参数的顺序颠倒了。地址首先出现,然后是引脚号。

是可选参数 Wire.begin() 允许您指定在从机模式下用于 I2C 控制器的引脚:

    bool begin(uint8_t slaveAddr, int sda=-1, int scl=-1, uint32_t frequency=0);

因此您可以调用

Wire.begin(MY_ADDRESS, SDA1, SCL1);

并选择将频率指定为最终参数(如果需要)。

虽然这种形式的 begin() 看起来像是针对大师的,但它的代码 显式设置 is_slave 标志

    is_slave = true;

并调用i2cSlaveInit()

我实际上没有使用过它,所以我不能保证它有效,但这就是代码的组织方式。

You're very close, you just have the order of the parameters flipped. The address comes first, followed by the pin numbers.

There are optional parameters to Wire.begin() that allow you to specify the pins used for the I2C controller in slave mode:

    bool begin(uint8_t slaveAddr, int sda=-1, int scl=-1, uint32_t frequency=0);

So you could call

Wire.begin(MY_ADDRESS, SDA1, SCL1);

and optionally specify the frequency as a final argument if you need to.

While this form of begin() looks like it's for a master, its code explicitly sets the is_slave flag:

    is_slave = true;

and calls i2cSlaveInit().

I haven't actually used this so I can't promise it works, but this is the way the code is organized.

迷迭香的记忆 2025-01-24 21:27:40

另一种方法是在调用 Wire.begin() 之前调用 Wire.setPins()。它相当于将引脚号传递给构造函数,因此没有理由优先选择其中之一。随你选吧。

Another way is to invoke Wire.setPins() before invoking Wire.begin(). It's equivalent to passing the pin numbers to the constructor, so there's no reason to prefer one over the other. Just take your pick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文