Skip to main content
Topic: Trouble using bit bang mode with 1-wire protocol (Read 4099 times) previous topic - next topic

Trouble using bit bang mode with 1-wire protocol

Hi Folks,

I'm playing around with python scripting for the BP through the bit bang mode. I've got a Dallas 1-wire temperature sensor (18B20) that I have no problem communicating with through the serial terminal.  When I try to do the same through bit bang mode I'm unable to read data. Here's what I'm attempting:

1. Open the proper port
2. Send as string of twenty 0x00 to put it in bit bang mode (sends back BBIO1 as expected)
3. Send 0x04 to get into 1-wire mode (sends back 1W01 as expected)
4. Turn on voltage regulator by sending 0x48 (LED lights up as expected)
5. Scan for 1-wire address by sending 0x08 (Expected address comes back)
Here's where the problems start?
6. I need to send a matchrom signal (0x55) followed by the 8-byte address, then the start conversion command (0x44). I send the bulkwrite command in front of these like so: 0x19 0x55 0x28 0xa8 0xcd 0x5e 0x02 0x00 0x00 0xaf 0x44. I get back a six 0x01.
7 I then need to read the temp so I send 0xbe instead of 0xhh at the end: 0x19 0x55 0x28 0xa8 0xcd 0x5e 0x02 0x00 0x00 0xaf 0xbe.

No dice. I get a string of 0x01 back again.

Any idea why I'm not getting the expected results for steps 6 and 7?

Thanks!

Re: Trouble using bit bang mode with 1-wire protocol

Reply #1
Hi szczys - Can you post your code, I'd be happy to take a look and try it myself. You can attached it as a text file under 'additional options'. It very well may be a binary mode issue, some of the modes are pretty much untested.

Are you sending the reset signal manually before each ROM command? The macros in terminal mode send them, but you need to send it manually in the binary mode.

How many bytes do you read after sending the commands?
Got a question? Please ask in the forum for the fastest answers.

Re: Trouble using bit bang mode with 1-wire protocol

Reply #2
[quote author="szczys"]
6. I need to send a matchrom signal (0x55) followed by the 8-byte address, then the start conversion command (0x44). I send the bulkwrite command in front of these like so: 0x19 0x55 0x28 0xa8 0xcd 0x5e 0x02 0x00 0x00 0xaf 0x44. I get back a six 0x01.
7 I then need to read the temp so I send 0xbe instead of 0xhh at the end: 0x19 0x55 0x28 0xa8 0xcd 0x5e 0x02 0x00 0x00 0xaf 0xbe.

No dice. I get a string of 0x01 back again.
[/quote]
I have not tried this but according to page 18 of the 18B20 data sheet you need a reset command between your start conversion and read scratchpad command. You have to follow that with nine read byte commands to read the scratchpad area of the 18B20.

Klaus Leiss

Re: Trouble using bit bang mode with 1-wire protocol

Reply #3
Thanks Ian and Leiss,

You're right, I need to be sending the reset command. I figured that out after I posted yesterday. But that's not really the problem.  It seem's I'm overflowing the buffer by sending too many bytes too quickly.  If I reduce the number of bytes sent to no more than 4 at a time I get an ok response for each byte (0x01). After issuing the commands I can then read out the data one byte at a time by sending (0x04).

I was confused because there address search macro handles multiple bits (incoming) without a problem. But anyway, working now. Thanks!

-Mike

Re: Trouble using bit bang mode with 1-wire protocol

Reply #4
Glad you got it working. You ran up against the PIC's 4 byte UART buffer. Best practice is to wait for the reply before sending the next command.
Got a question? Please ask in the forum for the fastest answers.

Re: Trouble using bit bang mode with 1-wire protocol

Reply #5
The problem I'm having with that method is knowing how many bytes to expect back. For instance, when the 1-Wire reset is sent, instead of getting 0x01 back the BP sends 0x42 0x55 0x53 0x20 0x52 0x45 0x53 0x45 0x54 0x20 0x4F 0x4B 0x0D 0x0A 0x01 which is "BUS RESET OK" with some extra hex at the end. 

Is this bug in the bit bang protocol?

Re: Trouble using bit bang mode with 1-wire protocol

Reply #6
That would be a big bug! I'll try to fix it now.
Got a question? Please ask in the forum for the fastest answers.

Re: Trouble using bit bang mode with 1-wire protocol

Reply #7
Thanks for finding that bug. It used the wrong function. Here's a new nightly compile (v4.2 nightly) that should fix that issue. It's for the v4 bootloader, so don't use it unless you've done the upgrade. If you need help with that please let me know.

http://code.google.com/p/the-bus-pirate ... /BPv3&v2go
Got a question? Please ask in the forum for the fastest answers.

Re: Trouble using bit bang mode with 1-wire protocol

Reply #8
Great, thanks for the fix! That should make things easier for me moving forward. For now I thought I'd share some test code I've thrown together. This is basic proof of concept that I can get python to talk to the Bus Pirate.  I'm using a Dallas DS1820 1-wire temp sensor.

Code: [Select]
import serial

ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=0.05)
print ser.read(100)

#Funtion sends one byte at a time to the BP
def writeBytes(myData):
    for c in myData:
        ser.write(c)
        s = ser.read(100)
        for c in s:
            print '%02X'%ord(c),
    print
    return s



#Put BP into Bit Bang mode (Mode LED should light up)
ser.write("x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00")
s = ser.read(100)
print s

#Put BP into 1-wire mode
writeBytes("x04")

#Turn on voltage regulator
writeBytes("x48")


#bus reset
writeBytes("x02")

#Get address
s = writeBytes("x08")
#Slice out 8 bytes excluding the first byte (it was a confirmation byte)
tempSensorAddr = s[1:9]

print "nUsing 1-Wire Address:"
for c in tempSensorAddr:
print '%02X'%ord(c),
print "n"

#bus reset
writeBytes("x02")

#Start conversion by sending 0x44
writeBytes("x19x55" + tempSensorAddr + "x44")

#bus reset
writeBytes("x02")

#Read from sketchpad by sending 0xBE
writeBytes("x19x55" + tempSensorAddr + "xBE")

#Read first two bytes
lsByte = writeBytes("x04")
msByte = writeBytes("x04")

#Convert lsByte and msByte to a usable decimal sum
lsByte = int(ord(lsByte))
msByte = int(ord(msByte))*256
rawTemp = lsByte + msByte
print rawTemp

#Convert rawTemp to precision temp, then to Fahrenheit
tempCelsius = rawTemp * 0.0625
print "Temperature Celsius: ", tempCelsius
tempFahrenheit = ((tempCelsius*9)/5)+32
print "Temperature Fahrenheit: ", tempFahrenheit

Here's the output of the script:
Code: [Select]
BBIO1

Bus Pirate v3

Firmware v4.2 (rxxx) Bootloader v4.1

DEVID:0x0447 REVID:0x3003 (A3)

http://
dangerousprototypes.com

HiZ>BBIO1
31 57 30 31
01
01
01 28 A8 CD 5E 02 00 00 AF FF FF FF FF FF FF FF FF

Using 1-Wire Address:
28 A8 CD 5E 02 00 00 AF

01
01 01 01 01 01 01 01 01 01 01 01
01
01 01 01 01 01 01 01 01 01 01 01
75
01
373
Temperature Celsius:  23.3125
Temperature Fahrenheit:  73.9625