Skip to content

Ensure SPI/I2C always reads requested number of bytes #288

Closed

Description

@krwq

Issue created to track comment #278 (comment)

We should double check if we should not return int from the Read which will tell you how many bytes were read.

Usually when reading from Stream the API is allowed to read anything from 0 to requested number of bytes. With I2C/SPI we rely on the fact that we know what the expected number of bytes is.

We need to investigate if that is always the case and if the way we handle it is correct.

cc: @joperezr

Activity

  1. added this to the milestone on Feb 27, 2019
  2. added
    bug
    area-System.Device.Gpio
    on Feb 27, 2019
  3. added
    question
    and removed
    bug
    on Feb 27, 2019
  4. krwq commented on Mar 3, 2019

    @krwq
    MemberAuthor

    In cases of SPI master controls the clock and when is slave supposed to be writing so the Read should return void since the protocol doesn't give a chance to slave to say that it's finished.

    Will look deeper into I2C later.

  5. krwq commented on Mar 23, 2019

    @krwq
    MemberAuthor

    just looked at the I2C - the only time you will receive less bytes if slave stretches the clock forever but in terms of I2C this is technically allowed (same for SPI) and is not considered error (although would be rather unexpected). Since master controls the clock and slave is supposed write as long as SDA is up that means that slave must write as many bytes as user expected and if it doesn't then you will get bogus data but there is no way you can know about it (at least on the first abstraction layer).

  6. raffaeler commented on May 9, 2019

    @raffaeler
    Contributor

    Question: what if the device can send a variable number of bytes?
    The Linux I2C driver returns the number of read bytes for this reason and IMO it makes sense because it's an application protocol decision, not a hw peripheral specification.

  7. joperezr commented on May 9, 2019

    @joperezr
    Member

    @raffaeler can you provide an example of a case where the number of bytes is variable? AFAIK all of the device bindings we've seen and all the libraries similar to GPIO will know how many bytes to read for every I2c call. There are some devices who might send a variable number of bytes of data, but usually for these you first would make an I2C call which will return you 1 byte telling you the size, and then you can make your second I2C call querying for that number of bytes of data.

  8. raffaeler commented on May 9, 2019

    @raffaeler
    Contributor

    Hi @joperezr, not yet.
    I am translating the Flick C library using wiringPi and the Linux i2c library, but the source code does not mention how many bytes are expected nor there is any comment about the length of the packets.
    The C library just read data in the buffer and use the length as a discriminator.

    I guess the current C# I2C library will throw for a timeout if the buffer is not entirely filled. Am I right?

    I am just afraid that any device relying on an extrernal line (such as the one I am using) or simply a timeout, would be more difficult to use.
    Changing the API now is simple, but should any device work as I am probably seeing, could introduce an annoying breaking change.

  9. 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-System.Device.Gpioquestion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions