-1

I am trying to read out an i2c ambient light sensor BH1750 from C in userspace. I am using a BeagleBone pocket with Linux.

The Datasheet says, that after a measurement operation the result needs to be read with the following i2c sequence: S Adr Rd (A) (Data) A (Data) NA P.

The issue I ran into: All examples/commands I have found either are only reading 1 byte or are requiring a register address to read more.

The closest I found was the following command: i2c_master_recv(). However my interpretation of the documentation is that this command is used in kernel-device-drivers.

How can I issue such a I2C-Read-Sequence from Userspace?

Anderle
  • 43
  • 5
  • There is in-kernel driver for it https://elixir.bootlin.com/linux/latest/source/drivers/iio/light/bh1750.c, so use it! – 0andriy Oct 03 '21 at 11:53
  • 1
    Assuming the I2C controller has been opened, and the `I2C_SLAVE` ioctl command has been used to set `Adr`, The `read` syscall with a length of 2 will perform the above I2C read transaction. – Ian Abbott Oct 04 '21 at 13:45

1 Answers1

0

I was able to solve it using this comment from Ian Abbott:

Assuming the I2C controller has been opened, and the I2C_SLAVE ioctl command has been used to set Adr, The read syscall with a length of 2 will perform the above I2C read transaction. – Ian Abbott Oct 4 at 13:45

Here is a Logicanalyzer-Trace of the resulting I2C communication:

trace

Anderle
  • 43
  • 5
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 16 '21 at 11:42
  • I edited your answer to include the mentioned comment, since that seemed to be the useful information, and comments can get lost in the future. However, I think this answer could still be improved, since it's basically a link-only answer (although that might be good enough in this case - I cannot really judge that). You might want to add some additional details to help future readers. – wovano Oct 17 '21 at 10:54
  • Thx for making it nice! Sorry - my fault, I referenced the wrong comment. Copy/pasted the full comment, sonst is an complete answer, should the comments get lost. – Anderle Oct 17 '21 at 11:00