Read data from 16-bit register addres (I2C)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
gepd
Posts: 12
Joined: Sat Oct 14, 2017 8:13 pm

Read data from 16-bit register addres (I2C)

Post by gepd » Fri Oct 27, 2017 11:34 pm

I'm trying to read the data from a sensor where I need to send it a 16-bit register addres

This is the diagram from the datasheet
16-bit.jpg
16-bit.jpg (49.64 KiB) Viewed 6980 times
When reading: Set the LSB of slave address to 1 so that is ABh (1010_1011b).

How should I do this in uPython?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Read data from 16-bit register addres (I2C)

Post by pythoncoder » Sat Oct 28, 2017 5:44 am

This seems to be an ad hoc protocol and I think you have no choice but to "bit bang" it - i.e. control the two I/O pins one bit at a time. The data line is presumably a bidirectional open-drain interface with a pullup resistor, and the SACK is a zero returned from the device. A bit banged solution could be concocted so long as there aren't any severe timing constraints.

If you're unfamiliar with the techniques your best bet would be to find and study existing examples.
Peter Hinch
Index to my micropython libraries.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Read data from 16-bit register addres (I2C)

Post by deshipu » Sat Oct 28, 2017 7:57 am

This is a standard I2C protocol, and you can do it using http://docs.micropython.org/en/latest/p ... adfrom_mem with the addrsize set to "16", for example:

Code: Select all

from machine import I2C, Pin

i2c =machine.I2C(sda=Pin(4), scl=Pin(5))
value = i2c.readfrom_mem(0x55, 0x2233, 1, addrsize=16)
If that doesn't work (addrsize is not implemented in all ports), you can take a look at my driver for the vl6180 sensor: https://bitbucket.org/thesheep/micropyt ... 0.py-26:30

gepd
Posts: 12
Joined: Sat Oct 14, 2017 8:13 pm

Re: Read data from 16-bit register addres (I2C)

Post by gepd » Sat Oct 28, 2017 4:29 pm

Thank you both for your answer.
deshipu wrote:
Sat Oct 28, 2017 7:57 am
If that doesn't work (addrsize is not implemented in all ports), you can take a look at my driver for the vl6180 sensor: https://bitbucket.org/thesheep/micropyt ... 0.py-26:30
addrsize is not implemented yet in the esp32 port, so I'm trying with your second suggestion.

I'm not familiar with the ustruct module, so I'm reading your _get_reg16 method but I still don't get how to implement it, my confusion is because I was doing it a little bit different in c++:

Code: Select all

uint8_t SSS::read(uint16_t addr) {

	uint8_t out;
	uint8_t input[ 2 ];

	input[ 0 ] = (addr & 0xFF00) >> 8; 
   	input[ 1 ] = addr & 0x00FF;

	Wire.beginTransmission(i2c_address); // start transmission
  	Wire.write(input, 2); // sends register
  	Wire.endTransmission(); // end transmission

  	delay(35);

  	Wire.requestFrom(i2c_address, 1);// send data n-bytes read
  	while(Wire.available() == 0);

  	out = Wire.read();
  	Wire.endTransmission(); // end transmission

  	return out;
}
can you help me a little bit with this?
I'm looking examples of the struct module to understand what I'm doing
Last edited by gepd on Sat Oct 28, 2017 9:31 pm, edited 1 time in total.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Read data from 16-bit register addres (I2C)

Post by deshipu » Sat Oct 28, 2017 9:03 pm

You can pretty much copy the _get_reg16 or _get_reg8 methods (depending on whether the value you are reading is 16 or 8 bit). There is no need to modify them. I don't understand what you are trying to do with your modifications. Just specify the address as a 16-bit number, and it should work.

gepd
Posts: 12
Joined: Sat Oct 14, 2017 8:13 pm

Re: Read data from 16-bit register addres (I2C)

Post by gepd » Sat Oct 28, 2017 9:34 pm

You're right. I did that the first time but for some reason it didn't work so I thought I had to change something, now seems like I'm getting the right data, I'll keep testing it.

Thanks!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Read data from 16-bit register addres (I2C)

Post by pythoncoder » Tue Oct 31, 2017 5:57 am

Apologies - brain fade on my part. It is indeed standard I2C. :oops:
Peter Hinch
Index to my micropython libraries.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Read data from 16-bit register addres (I2C)

Post by deshipu » Tue Oct 31, 2017 9:23 am

Happens to me all the time.

Post Reply