Skip to content

WriteRead using I2C and MAX7311 #111

Closed

Description

@dominikjeske

I’m trying to communicate with MAX7311 using UnixI2cDevice on Raspians. Generally I2C with other devices works but in this case I need to write some data and then immediately read response from MAX7311 device.
When I try to do something like this:

var buffer = new Span<byte>();
var device = new System.Device.I2c.Drivers.UnixI2cDevice(new I2cConnectionSettings(BusId, address));
device.Write(new byte[] { 0x00 });
device.Read(buffer);

I get exception “Error when attempting to perform the I2c data transfer”
When I use some custom implementation of I2C from this repo

https://github.com/chkr1011/Wirehome.Core/blob/master/Wirehome.Core/Hardware/I2C/I2CBusService.cs

and use WriteRead method I can communicate with success.

var device = new LinuxI2CBusAdapter(Logger);
device.Enable();
var readBuffer = new byte[2];
device.WriteRead(address, new byte[] { 0x00 }, readBuffer);

It would be nice if this WriteRead could be supported – or I’m doing something wrong?

Activity

  1. shaggygi commented on Dec 30, 2018

    @shaggygi
    Contributor

    I didn't look into this much, but you might need to set the buffer size to 1. Not completely sure. Also, make sure the BusId is correct. I believe it should be 1.

    var device = new System.Device.I2c.Drivers.UnixI2cDevice(new I2cConnectionSettings(BusId, address));
    device.Write(new byte[] { 0x00 });
    byte[] readBuffer = new byte[1];
    device.Read(buffer);
  2. joperezr commented on Jan 9, 2019

    @joperezr
    Member

    Because I2C protocol only uses 1 data pin, there is not a method like the TransferFullDuplex that we have in SPI. Internally what we are doing is to just do ioctl on the I2c device and send the information, so I would expect that as long as you set the right size of the read and write buffers that it should work correctly. Did you try what @shaggygi suggested?

  3. added
    question
    area-device-bindings
    on Jan 10, 2019
  4. shaggygi commented on Jan 13, 2019

    @shaggygi
    Contributor

    I tested this (only on Unix) and can repro this when sending Write or Read commands with an empty array or Span similar in @bigdnf example.

    @joperezr I don't understand why the ioctl wouldn't take/ignore 0 buffer length, but I guess it is expecting something when doing a write/read function. To fix this, we could put a check in ReadWriteInterfaceTransfer and FileInterfaceTransfer to just return if writeBufferLength and readBufferLength are both 0.

    I don't like this as it is a little more overhead for checking on every Write/Read call and if someone used an empty array/Span... no error would be shown, the buffer would still be empty and you'd probably see another GitHub issue created stating the I2C method wasn't working.

    In addition, when cleaning/updating the I2C and SPI summaries in PRs #143/#144, I added comments for buffer size when reading. This would probably help a little.

    Close issue as is?

  5. dominikjeske commented on Jan 17, 2019

    @dominikjeske
    Author

    Thanks for info - I'm currently on business trip so I can't test anything. I will do it after return and close issue if I can fix it after your suggestions.

  6. joperezr commented on Feb 12, 2019

    @joperezr
    Member

    @bigdnf have you had the chance if @shaggygi suggestions work in your scenario?

    I tested this (only on Unix) and can repro this when sending Write or Read commands with an empty array or Span similar in @bigdnf example.

    I don't see that in the example @bigdnf is using an empty array or Span, so that might be a different issue.

    To fix this, we could put a check in ReadWriteInterfaceTransfer and FileInterfaceTransfer to just return if writeBufferLength and readBufferLength are both 0.

    I believe that this is an actual bug if we aren't checking the size, and if so, I would probably even throw an exception maybe if the read buffer or writebuffer is 0 as this shouldn't be allowed.

  7. added
    bug
    and removed
    question
    on Feb 12, 2019
  8. dominikjeske commented on Feb 13, 2019

    @dominikjeske
    Author

    @joperezr sorry for my delay with testing this - I will try to give feedback as soon as possible.

  9. joperezr commented on Feb 13, 2019

    @joperezr
    Member

    no worries at all, just wanted to check if that was something you still had in your plans 😄

  10. krwq commented on Feb 22, 2019

    @krwq
    Member

    I think the correct fix should be to throw a descriptive exception when someone passes 0-length buffer to Read or am I missing something?

  11. added this to the milestone on Feb 22, 2019
  12. shaggygi commented on Feb 23, 2019

    @shaggygi
    Contributor

    @krwq The original issue was because there were no bytes allocated to perform read. Your suggestion with having a better exception description would help in this case. Other than that, I would label issue as stale and close.

  13. self-assigned this
    on Feb 24, 2019
  14. ghost locked as resolved and limited conversation to collaborators on Oct 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-device-bindingsbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions