nanoframework / Home

:house: The landing page for .NET nanoFramework repositories.
https://www.nanoframework.net
MIT License
844 stars 75 forks source link

nanoFramework.System.Device.I2c and .Slave not working as expected when sending bytes from master to slave #1494

Open TheMo opened 1 month ago

TheMo commented 1 month ago

Library/API/IoT binding

nanoFramework.System.Device.I2c

Visual Studio version

VS2022

.NET nanoFramework extension version

1.1.16

Target name(s)

ESP32

Firmware version

No response

Device capabilities

System Information HAL build info: nanoCLR running @ ESP32 built with ESP-IDF e7771c7 Target: ESP32_REV3 Platform: ESP32

Firmware build Info: Date: Jun 5 2024 Type: MinSizeRel build, chip rev. >= 3, without support for PSRAM CLR Version: 1.9.1.187 Compiler: GNU ARM GCC v12.2.0

OEM Product codes (vendor, model, SKU): 0, 0, 0

Serial Numbers (module, system): 00000000000000000000000000000000 0000000000000000

Target capabilities: Has nanoBooter: NO IFU capable: NO Has proprietary bootloader: YES

AppDomains:

Assemblies: NFAppI2cCommunication, 1.0.0.0 mscorlib, 1.15.6.0 nanoFramework.Hardware.Esp32, 1.6.15.0 System.Device.I2c.Slave, 1.0.6.0 System.Device.Gpio, 1.1.41.0 System.Device.I2c, 1.1.16.0 nanoFramework.Runtime.Events, 1.11.18.0

Native Assemblies: mscorlib v100.5.0.19, checksum 0x445C7AF9 nanoFramework.Runtime.Native v100.0.9.0, checksum 0x109F6F22 nanoFramework.Hardware.Esp32 v100.0.10.0, checksum 0x6A20A689 nanoFramework.Hardware.Esp32.Rmt v100.0.4.0, checksum 0x608C5658 nanoFramework.Device.OneWire v100.0.4.0, checksum 0xB95C43B4 nanoFramework.Networking.Sntp v100.0.4.4, checksum 0xE2D9BDED nanoFramework.ResourceManager v100.0.0.1, checksum 0xDCD7DF4D nanoFramework.System.Collections v100.0.1.0, checksum 0x2DC2B090 nanoFramework.System.Text v100.0.0.1, checksum 0x8E6EB73D nanoFramework.System.IO.Hashing v100.0.0.1, checksum 0xEBD8ED20 nanoFramework.System.Security.Cryptography v100.0.0.2, checksum 0xF4AEFE6C nanoFramework.Runtime.Events v100.0.8.0, checksum 0x0EAB00C9 EventSink v1.0.0.0, checksum 0xF32F4C3E System.IO.FileSystem v1.1.0.0, checksum 0xCC556D24 System.Math v100.0.5.5, checksum 0x9F9E2A7E System.Net v100.2.0.1, checksum 0xD82C1452 System.Device.Adc v100.0.0.0, checksum 0xE5B80F0B System.Device.Dac v100.0.0.6, checksum 0x02B3E860 System.Device.Gpio v100.1.0.6, checksum 0x097E7BC5 System.Device.I2c v100.0.0.2, checksum 0xFA806D33 System.Device.I2c.Slave v1.0.0.0, checksum 0x4238164B System.Device.I2s v100.0.0.1, checksum 0x478490FE System.Device.Pwm v100.1.0.4, checksum 0xABF532C3 System.IO.Ports v100.1.6.1, checksum 0xB798CE30 System.Device.Spi v100.1.2.0, checksum 0x3F6E2A7E System.Runtime.Serialization v100.0.0.0, checksum 0x0A066871 System.Device.Wifi v100.0.6.4, checksum 0x00A058C6 Windows.Storage v100.0.3.0, checksum 0xF0C37E1B

++++++++++++++++++++++++++++++++ ++ Memory Map ++ ++++++++++++++++++++++++++++++++ Type Start Size ++++++++++++++++++++++++++++++++ RAM 0x3ffe46bc 0x0001b000 FLASH 0x00000000 0x00400000

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ Flash Sector Map ++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Region Start Blocks Bytes/Block Usage +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 0 0x00010000 1 0x1B0000 nanoCLR 1 0x001C0000 1 0x1E0000 Deployment 2 0x003C0000 1 0x040000 Configuration

+++++++++++++++++++++++++++++++++++++++++++++++++++ ++ Storage Usage Map ++ +++++++++++++++++++++++++++++++++++++++++++++++++++ Start Size (kB) Usage +++++++++++++++++++++++++++++++++++++++++++++++++++ 0x003C0000 0x040000 (256kB) Configuration 0x00010000 0x1B0000 (1728kB) nanoCLR 0x001C0000 0x1E0000 (1920kB) Deployment

Deployment Map Empty

Description

I am trying to use a master esp32 to communicate with a esp32 slave using the library mentioned above. nanoFramework.System.Device.I2c and nanoFramework.System.Device.I2c .Slave.

If i use the example code the master sends bytes to slave in a loop and the slave does not receive anything instead when it hits the device.ReadByte(out registerAddress) function it stays there.

I have also tried to see if this works on arduino ide using an example i found here: https://microcontrollerslab.com/esp32-i2c-communication-tutorial-arduino-ide/ At the bottom is a example with code written c for the esp32. And when I deploy this to my 2 esp32 then they do communicate with eachother. I can see sending mytes from master to slave.

How to reproduce

// ESP32 Master code public static void Main() { // Configure I2C Configuration.SetPinFunction(Gpio.IO21, DeviceFunction.I2C1_DATA); Configuration.SetPinFunction(Gpio.IO22, DeviceFunction.I2C1_CLOCK); s_GpioController = new GpioController(); _led_Internal = s_GpioController.OpenPin(2, PinMode.Output); GpioPin led = s_GpioController.OpenPin(Gpio.IO23, PinMode.Output);

// create I2C device
var myI2cDevice = I2cDevice.Create(new I2cConnectionSettings(1, 0x10, I2cBusSpeed.FastMode));
while (true)
{
    _led_Internal.Toggle();
    // setup read buffer
    var buffer = new byte[2];

    // set address to read from
    myI2cDevice.Write(new byte[] { 0x22 });
    myI2cDevice.Read(buffer);

    // expected buffer content is: 0xBE, 0xEF
    Debug.WriteLine($"Register content: {buffer[0]:X2} {buffer[1]:X2}");
}

}

// ESP32 Slave code public static void Main() { s_GpioController = new GpioController(); _led_Internal = s_GpioController.OpenPin(2, PinMode.Output); Configuration.SetPinFunction(Gpio.IO21, DeviceFunction.I2C1_DATA); Configuration.SetPinFunction(Gpio.IO22, DeviceFunction.I2C1_CLOCK);

// create an I2C slave device on bus 1 with address 0x10
var device = new I2cSlaveDevice(1, 0x10);

  byte registerAddress;
  if (device.ReadByte(out registerAddress))
  {
      switch (registerAddress)
      {
          case 0x22:
              device.Write(new byte[] { 0xBE, 0xEF });
              _led_Internal.Toggle();
              Debug.WriteLine($"Received message: {GetString(registerAddress)}");
              break;
      }
  }

}

Expected behaviour

I should receive bytes when sending from master to slave.

Screenshots

No response

Sample project or code

https://microcontrollerslab.com/esp32-i2c-communication-tutorial-arduino-ide/ https://github.com/nanoframework/System.Device.I2c.Slave

Aditional information

No response

nfbot commented 1 month ago

Hey @TheMo! 👋 If you are a commercial user, time to market maybe be important to you. Know that Professional Support options are available. If you are stuck on something, need your bug fixed in a hurry or would like to sponsor the feature that you're currently missing, feel free to reach out to us here or on the project's Discord server. If this it not of interest to you, that's fine too. This issue will get into the queue and will be eventually addressed.