Page 2 of 3

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 04, 2016 5:23 pm
by joan
You need to start the daemon once per session.

But there is no point in trying the Python if it doesn't work from the command line.

I have assumed you are using I2C bus 1 on GPIO 2/3 (pins 3/5).

If you keep the baud rate to about 50kbps http://abyz.co.uk/rpi/pigpio/piscope.html should show the signals.

Re: RPi Zero and I2C Clockstretching

Posted: Sat Nov 05, 2016 9:01 am
by milito
Hi i tired it out so may be i understood it but i have two questions regarding the bit banging:

1. when i write the python programm with the pigpio should i start at first sudo pigpiod ? or i can run directly the script?

2: the second question is, there are the two parameters scl and sda. these are the pins right? cam i use other pins as parameters for this or i should use the original i2c pins from the board?

Re: RPi Zero and I2C Clockstretching

Posted: Sat Nov 05, 2016 9:20 am
by joan
1. The Python script needs a running daemon. The daemon may be started when the Pi is booted and left running. Or you can start the daemon before the script and leave it running or stop it. That is up to you.

2. You can bit bang on any spare GPIO. They need to have pull-ups to 3V3 for the correct bus operation. For testing you could probably get away with using the internal pull-ups (http://abyz.co.uk/rpi/pigpio/python.htm ... ll_up_down) but for reliability you should fit external pulls.

http://abyz.co.uk/rpi/pigpio/python.html#bb_i2c_open

Re: RPi Zero and I2C Clockstretching

Posted: Sat Nov 05, 2016 11:24 am
by milito
Hi ok thank you.

To answer 1: ok now i understood. To start this deamon automatically when i boot the pi is there a script or settings to set this up, because i want that this happens automatically? Taht means everytime when I boot the RPi the deamon is running.


To anser 2: ok regarding the pullups this is not a problem, the sensor has 10KOhm resistor on the SDA and SCL Pin.

Re: RPi Zero and I2C Clockstretching

Posted: Sat Nov 05, 2016 11:29 am
by joan
Use the following command (once) and the daemon should start at boot,

sudo systemctl enable pigpiod

Re: RPi Zero and I2C Clockstretching

Posted: Thu Nov 10, 2016 2:43 pm
by milito
Hi so I am working again on this I hadnt time the last days.
Thanks it works with the activation of the daemon. I gave a look in your webpage with your desription of all the commands. Iam using the bb_i2c_open and bb_i2c_close to initialize the Bus and close it right?
So because iam doing bitbanging Iam using these commands. But which commands I should use to write and read for bitbanging?-Could I use i 2c_write_byte & i2c_read_byte or I should use this one:bb_i2c_zip?

Re: RPi Zero and I2C Clockstretching

Posted: Thu Nov 10, 2016 3:45 pm
by milito
No I tried to run this code:

Code: Select all

import pigpio
import time
pi1 = pigpio()

while True:
          pi1.bb_i2c_open(23,24,50000)
          time.sleep(1)      
I ve got an error message: Traceback (most recent call last):
File h= pi1.bb_i2c_open(23,24,50000)
File.....
raise error(error_text(v))
pigpio.error= GPIO already in use

What I did wrong?

Re: RPi Zero and I2C Clockstretching

Posted: Thu Nov 10, 2016 4:11 pm
by joan
The bit bang I2C operations are used together.

bb_i2c_open Opens GPIO for bit banging I2C
bb_i2c_close Closes GPIO for bit banging I2C
bb_i2c_zip Performs multiple bit banged I2C transactions

Use the first and the second at the start and end of your script.

Use bb_i2c_zip to actually transfer data.

You are probably getting that error because you already have opened 23/24 for bit bang on I2C and didn't close it before the script ended.

Try the following command and re-run the script

pigs bi2cc 23

Re: RPi Zero and I2C Clockstretching

Posted: Thu Nov 10, 2016 4:26 pm
by milito
Hi thanks fpr the answer, i added now pi1.bb_i2c_close(23)
normally it should be closed or Iam wrong?

Re: RPi Zero and I2C Clockstretching

Posted: Thu Nov 10, 2016 4:42 pm
by joan
milito wrote:Hi thanks fpr the answer, i added now pi1.bb_i2c_close(23)
normally it should be closed or Iam wrong?
The state is maintained by the pigpio daemon and it will be closed when the pigpio daemon is started. If you open the GPIO they will stay open until they are closed or the pigpio daemon is restarted.

Re: RPi Zero and I2C Clockstretching

Posted: Thu Nov 10, 2016 4:46 pm
by milito
ok that means that after every open i2c_bb_open i should write bb_i2c_close right?

Re: RPi Zero and I2C Clockstretching

Posted: Thu Nov 10, 2016 4:56 pm
by milito
and regarding the pullups iam using the pulls ups i am using the 10K from the sensor. That means i connected the sensor to pin 23 and 25 without adding pullups in the RPi because the Sensor board has these pullup is this a problem?

When i want to attach multiple sensor can I use the same pins fpr bitbanging ?

Re: RPi Zero and I2C Clockstretching

Posted: Thu Nov 10, 2016 5:18 pm
by joan
milito wrote:and regarding the pullups iam using the pulls ups i am using the 10K from the sensor. That means i connected the sensor to pin 23 and 25 without adding pullups in the RPi because the Sensor board has these pullup is this a problem?

When i want to attach multiple sensor can I use the same pins fpr bitbanging ?
The pull-ups from the sensor should be fine.

It's best if all pull-ups are to 3V3 rather than 5V. 5V pull-ups may damage the Pi.

Yes, you can attach multiple devices to the same GPIO.

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 2:08 pm
by milito
Hi ok thanks . So the question is every sensor board has his own pullups for i2c. So that means I should connect every sensor board with the pullups to the bit banged i2c pins? Or its enough when only one sensor device has the pullups and the other I connect without the pullups?

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 2:27 pm
by joan
It is best if there is only one set of pull-ups to 3V3.

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 2:39 pm
by milito
ok; I tried to write 2 bytes that means the register where i want to write and then the value:

address = 0x28
reg = 0x3D
value = 0x3C

i did it like this:

pi1.bb_i2c_open(23,25,50000) #init bus i2c
pi1.bb_i2c_zip(23,[4,address, 2,7,reg, value,0])
pi1.bb_i2c_close(23)

but i cannot see anything

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 2:43 pm
by joan
The 7 cmd argument is the number of bytes to write so use

pi1.bb_i2c_zip(23, [4, address, 2, 7, 2, reg, value, 3, 0])

The 3 cmd at the end sends the stop condition.

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 2:49 pm
by milito
Sorry it doesnt work i have the same output. maybe i should check the connection.
pin 23 (SDA) and pin 25(SCL) are the BCM numers so GPIO 23 and GPIO 25 the logival number of these would be : 16, 18. So normally it should work?

I am not understanding the syntax, that you send me know -> why 2,7,2 , 2 is for start and 7 fro write or not? why the second ?

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 3:43 pm
by joan
GPIO 25 is connected to pin 22.

Code: Select all

#!/usr/bin/env python

import pigpio

address = 0x28

reg = 0x3D
value = 0x3C

pi = pigpio.pi()

if not pi.connected:
   exit()

pi.set_pull_up_down(23, pigpio.PUD_UP)
piset_pull_up_down(25, pigpio.PUD_UP)

pi.bb_i2c_open(23, 25, 5000) #init bus i2c

pi.bb_i2c_zip(23, [4, address, 2, 7, 2, reg, value, 3, 0])

pi.bb_i2c_close(23)

pi.stop()
4, address - set address
2 - start sequence
7, 2, reg, value - write 2 bytes
3 - stop sequence
0 - exit
bb-i2c.png
bb-i2c.png (34.88 KiB) Viewed 1613 times

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 3:48 pm
by milito
Hi know it works thank you :). I was a liitle bit confused by the description table from your Webpage with 2 = sequence and 7 P?
Name Cmd & Data Meaning
End 0 No more commands
Escape 1 Next P is two bytes
Start 2 Start condition
Stop 3 Stop condition
Address 4 P Set I2C address to P
Flags 5 lsb msb Set I2C flags to lsb + (msb << 8)
Read 6 P Read P bytes of data
Write 7 P ... Write P bytes of data

Is P the parameter? for the size of the package?
And for the Address you have it to: 4P is this fpr 10 bit addresses?

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 3:52 pm
by joan
milito wrote:Hi know it works thank you :). I was a liitle bit confused by the description table from your Webpage with 2 = sequence and 7 P?
Name Cmd & Data Meaning
End 0 No more commands
Escape 1 Next P is two bytes
Start 2 Start condition
Stop 3 Stop condition
Address 4 P Set I2C address to P
Flags 5 lsb msb Set I2C flags to lsb + (msb << 8)
Read 6 P Read P bytes of data
Write 7 P ... Write P bytes of data

Is P the parameter? for the size of the package?
And for the Address you have it to: 4P is this fpr 10 bit addresses?
You have to also read the sentence after the table.

The address, read, and write commands take a parameter P. Normally P is one byte (0-255). If the command is preceded by the Escape command then P is two bytes (0-65535, least significant byte first).

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 3:58 pm
by milito
Hahah you are right, my fault.

With escape you mean no input of Parameter or is it a space as Parameter?
For the Adresses why you need the parameter? Is it for addresses bigger than 7Bit?

Thanks that you are patient with me. I learnt a lot because of you

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 4:24 pm
by joan
The escape command modifies the next read/write/address command.

So 4 3 x means address 3 - command x

whereas 1 4 3 1 x means address 259 - command x

It doesn't actually matter as 10 bit addressing isn't supported anyhow.

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 4:50 pm
by milito
Ok 1 4 3 1 x is 259 how you come to 259 , is 43 the address?

Re: RPi Zero and I2C Clockstretching

Posted: Fri Nov 11, 2016 5:30 pm
by joan
[quote="milito"]Ok 1 4 3 1 x is 259 how you come to 259 , is 43 the address?[/quoteess1 is the escape command (so the next P is two bytes)
4 is the address command which now requires two parameter bytes because of the escape.
3 is the low byte of the address
1 is the high byte of the address
x is the next command

The address is 3 + (1*256) = 259