dannyoneill
Posts: 3
Joined: Sun Dec 15, 2013 3:10 pm

i2c addressing - set device to unaddressable by RPi address!

Sun Dec 15, 2013 3:37 pm

Hi all, thanks in advance to anyone who can help me recover my stupid mistake!

I have connected up an ultrasound SRF02 board to the Pi via a little 3v3->5v board I made from mosfets. That was working well. The ultrasound device comes set with an i2c address of E0, which translated to 70. I didn't quite understand this - mostly because I was too lazy to take the time to read up on i2c properly - I was able to talk to it with the smbus library in python and was having a lot of fun writing code to mess with the range-finding. The device by the way is 9 pounds and very accurate. So then I thought I'd get clever...

I will need an array of them so thought I'd try changing the address of the device so that I could start using more than one. This is easy to do (code below) and I changed it from E0 to E4, which translated to 0x77 (found with i2cdetect) if I remember correctly. I checked I could talk to it and I could (I could read and write to registers and it was still happy range-finding), all was well. Then I tried changing it to FE, the highest of the 16 possible addresses the device will allow according to it's specs. And now I cant talk to it!!

- i2cdetect now shows no connected devices.
- The device itself flashes an led on start-up with a number corresponding to its configured address and it still does this correctly (it flashed once when it was on factory default of E0, and now since setting the address to FE it flashes 14 times) so it, the SRF02, presumably thinks all is well. But the Pi can't talk to it anymore.

Is there a range that the raspberry pi can address and have I set my device outside that range? Seems odd that the device's firmware might be configured to allow addresses outside of a defined range for the i2c standard, or is it that the Pi can only address a subset of the standard address range? I'm hoping the answer to both of those is "No, the device and the pi are fine, you are being an idiot and need to address it as follows:..."

I can't find anything on the net about doing a hardware reset on the ultrasound device to default it's address so that I can start talking to it again. The specs are well-detailed here (although as I say, no hard reset mentioned):
http://www.robot-electronics.co.uk/htm/srf02techI2C.htm

The following is the code used to change the address (which as I say, worked, except I now cant address it!!):

import smbus

bus = smbus.SMBus(1)

currentDeviceAddress = 0x77
commandRegister = 0x00
changeCommand1 = 0xA0
changeCommand2 = 0xAA
changeCommand3 = 0xA5
changeAddressTo = 0xFE

bus.write_byte_data(currentDeviceAddress, commandRegister, changeCommand1)
bus.write_byte_data(currentDeviceAddress, commandRegister, changeCommand2)
bus.write_byte_data(currentDeviceAddress, commandRegister, changeCommand3)
bus.write_byte_data(currentDeviceAddress, commandRegister, changeAddressTo)


If anyone can suggest anything I'd really appreciate it!

User avatar
joan
Posts: 15791
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: i2c addressing - set device to unaddressable by RPi addr

Sun Dec 15, 2013 4:02 pm

I don't know about the Pi's I2C addressing range.

I suggest you change the address back to one which works by using the serial interface.

You can do that by grounding the mode contact and connecting the Pi's TX pin to the devices RX contact. I wouldn't connect the Pi's RX to the devices TX (in case it feeds 5V to the Pi).

You'll then need to send the command to change the address.

dannyoneill
Posts: 3
Joined: Sun Dec 15, 2013 3:10 pm

Re: i2c addressing - set device to unaddressable by RPi addr

Sun Dec 15, 2013 4:06 pm

Sort of solved!

So I read some more (http://www.byteparadigm.com/application ... protocols/ is excellent and very well written).

I also read the man page on i2cdetect to see if it was possible to make is scan in a fashion that would try to find things outside the usual addresses and of course there is (-a). I have done this and found that setting an address of:
FE, which the device thinks of as 14, can be found at 7e.

I think I notionally understand why this is, but certainly wont attempt to explain it and doubtless embarase myself! Anyway, am talking to the device again and am going to solder and connect up some more of them and see if I cant talk to them all at once! (not literally at the actual same time of course :) )

Anyway, apologies for posting a little prematurely!

dannyoneill
Posts: 3
Joined: Sun Dec 15, 2013 3:10 pm

Re: i2c addressing - set device to unaddressable by RPi addr

Sun Dec 15, 2013 4:11 pm

Hi there Joan, thanks for the suggestion - I was thinking about trying to talk to it with serial to get it's address back in line, but hadn't quite the stomach for it, but if you read my other post, you'll see I've worked round / sorted out the issue. Mostly guess work and stumbling about, no real understanding of exactly what is going on - partly due to obfusation by the smbus library, which IS explained - i.e. what it actually sends compared to what you tell it to send - but I work 14 hour days in a technical job and smoked too much of the green stuff while at uni, so at age 36 and on a Sunday, my mind is no longer capable of interpreting between integers, hex, bytes being shifted about and bits shoved on the end and what might result from all that. All I need is a translation between what I tell the device it's address is, and what I need to use to address it, and now I have successfully set them to addresses at the full range I'm confident that with the i2cdetect (with the -a switch) I can form such a table and all will be well!!

Thanks anyway :)

User avatar
joan
Posts: 15791
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: i2c addressing - set device to unaddressable by RPi addr

Sun Dec 15, 2013 4:37 pm

Good to hear you are sorted.

I have the same device and have just confirmed that you can change the address using the serial link.

Connections as follows:

Pi 5V to device 5V
Pi ground to device ground
Pi ground to device mode
Pi TX to device RX

Then configure the Pi serial link to 9600.

stty -F /dev/ttyAMA0 9600

Then use echo to send commands to the device.

The first byte is the device address (0 - 15). The second byte is a command.

To change the device from address 0 to address 5 use

echo -ne "\x00\xA0\x00\xAA\x00\xA5\x00\x05" >/dev/ttyAMA0

To change the device from address 5 back to address 0 use

echo -ne "\x05\xA0\x05\xAA\x05\xA5\x05\x00" >/dev/ttyAMA0

All the details are in the docs you linked.

danjperron
Posts: 3891
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: i2c addressing - set device to unaddressable by RPi addr

Tue Dec 17, 2013 6:16 pm

Hi DannyOneill,

Physically when you send the i2c address, it is one byte header which contains I2C address with the read/write flag.


bit7..1 i2c address
bit0 Read or Write mode

This is why your range finder has address with even number only.

You just need to divide by 2. I don't think that the range finder do the "bit shifting" for the address.

P.S. I2C could be 10 bit address but a second header byte is needed. But normally it is only 7 bits (0..127).

Maybe this is why you have some obscur address translation to do.

http://i2c.info/i2c-bus-specification

Daniel

Return to “Interfacing (DSI, CSI, I2C, etc.)”