Skip to main content
Topic: Binary BitBang in Python very slow  (Read 2050 times) previous topic - next topic

Binary BitBang in Python very slow

Hi,

I'm playing with my new BP and wrote this small python test. The script just toggles the CS pin on and off.
The max speed I can get is about 100ms for one period.

I wonder why?


Code: [Select]
#!/usr/bin/env python
# encoding: utf-8

import os, sys
import serial

from time import sleep

from pyBusPirateLite.UART import *
from pyBusPirateLite.BitBang import *

#change this path
BUS_PIRATE_DEV = "/dev/ttyUSB-buspirate"

bp = UART(BUS_PIRATE_DEV,115200)

print "Entering binmode: ",
if bp.BBmode():
    print "OK."
else:
    print "failed."
    sys.exit()

bp.raw_set_pins(BBIOPins.POWER|BBIOPins.PULLUP)


def on():
    bp.raw_cfg_pins(PinCfg.CS)
def off():
    bp.raw_cfg_pins(0)

tWait = 0.1
while 1:
    on()
    #sleep(tWait)
    off()
    #sleep(tWait)

Re: Binary BitBang in Python very slow

Reply #1
[s:]in the while() loop:[/s:]

Code: [Select]
#sleep(tWait)

[s:]You wait .1s and then toggle the CS pin. If you comment this statement, it will run at full speed.[/s:]

double commenting won't help..

Re: Binary BitBang in Python very slow

Reply #2
Hi,

this statement is already commented!
If I use
Code: [Select]
sleep(tWait)
the delay is actually 200ms.

Re: Binary BitBang in Python very slow

Reply #3
Hmm you're right.. been playing too much with c lately.

Now I'm trying a more helpfull:

from the bitbang.py:
Code: [Select]
def raw_cfg_pins(self, config):
self.port.write(chr(0x40 | config))
self.timeout(0.1)
return self.response(1)

with timeout function:
Code: [Select]
def timeout(self, timeout=0.1):
select.select([], [], [], timeout)

Could it be that this will wait 100ms?

Re: Binary BitBang in Python very slow

Reply #4
Bingo.

Max speed is now 4ms. Thank You!

Unfortunately, the time is not constant. Some periods are 6ms, some are 8ms.

Re: Binary BitBang in Python very slow

Reply #5
You could change it to:

Code: [Select]
self.timeout(0.010)

In order to get more predictable result. However the buspirate isn't know for his timing accurancy :)