Porting C lib for I2C device

I’m trying to wrap my head around using Nerves and ElixirAle to interface with an I2C device. I’m using a Rpi3 with an Adafruit BMP085 temperature + pressure sensor (the i2C device).

I’m new to this and not used to messing with bits at this level so bear with me…

I can connect to the device

iex(1)> alias ElixirALE.I2C                        
iex(2)> {:ok, tpid} = I2C.start_link("i2c-1", 0x77)
{:ok, #PID<0.503.0>}

And can read…

iex(3)> I2C.read(tpid, 22)                         
<<10, 0, 118, 98, 0, 181, 148, 60, 25, 18, 39, 37, 34, 115, 157, 32, 143, 251,
107, 199, 244, 130>>

But I have no idea what I’m getting here

Looking at the Adafruit library and the datasheet, there are 11 registers to read to get calibration data to be used in the measurement calculations. No idea how to get that i.e. how do I read 16 bits out of register 0xAA?

Is this possible with ElixirALEs I2C module?

Looking at this code, it sounds like the equivalent would be <<value :: 16>> = I2C.write_read(tpid, <<0xAA :: 8>>, 2) perhaps? The Elixir bitstring syntax is documented here if that helps.

Yes, that was suggested on the #nerves channel too and is probably it. I was thinking of direct access read from hardware and not thinking of the I2C protocol so didn’t think of the write_read call.

I’ll give it a shot this evening… after looking at the c implementation of the C read function, it’s doing a write_read so the elixir should be simple to translate :slight_smile:

I used the BMP280 for the I2C segment of my Nerves training at ElixirConf 2017. It’s a newer version of the module that you have, but I think that it’s similar enough that the material will still be relevant. If you want to page through the slides, you can find them here: https://www.dropbox.com/s/u5d1h4dowtie8ts/NervesTraining-03-I2C.pdf?dl=0. There’s a Github repo referenced in the slides with some code snippets. I realize that there’s a lot of context missing since I’m not including speaker notes, but I hope that they’re helpful nonetheless.

Also, one thing that was really neat about the BMP280 was that it was accurate enough to measure altitude differences in the hotel. Due to time limitations, I dropped it from the ElixirConf EU Nerves training, but it was a fun sensor to experiment with. I hope that enjoy getting it working too.


Thanks @dom, that was it. It’s making sense now, and thanks @fhunleth I’ll check that out!