Please login or register.

Login with username, password and session length
 

News:

Latest updates at DangerousPrototypes.com.


Author Topic: Scripting the Bus Pirate from Perl, Python, etc  (Read 1697 times)

ian

  • Crew
  • Hero Member
  • *****
  • Posts: 2916
  • Karma: +61/-0
    • View Profile
Scripting the Bus Pirate from Perl, Python, etc
« on: October 13, 2009, 03:01:41 AM »
Beginning with firmware v2.5, the Bus Pirate has a new binary IO mode (binmode) that can be scripted from a PC with Perl, Python, etc.

Here's some resources to get you started:
Binary bitbang mode.
Binary SPI mode.

Examples:
Perl bitbang test script.
Perl SPI EEPROM programmer.

More scripts are saved in the project SVN scripts folder. I'll like to add your scripts, no matter how simple.

Extensive AVR and EEPROM programming features will soon be available through AVRDude, and I hope to get Bus Pirate binmode support into full-featured JTAG programming software soon. There's also a possible ARM Cortex-M3 programmer in the works.

Are there any scripts you'd find useful?

MioTheGreat

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Scripting the Bus Pirate from Perl, Python, etc
« Reply #1 on: October 22, 2009, 07:02:28 AM »
Once v3 preorder 1 ships, I'll have to look into some Powershell scripting...

ian

  • Crew
  • Hero Member
  • *****
  • Posts: 2916
  • Karma: +61/-0
    • View Profile
Re: Scripting the Bus Pirate from Perl, Python, etc
« Reply #2 on: October 22, 2009, 07:10:35 AM »
Eric just emailed to let me know that Bus Pirate preorder 1 is at assembly now and "will be ready within this week". I guess that means it will ship next week?

anton.todorov

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Scripting the Bus Pirate from Perl, Python, etc
« Reply #3 on: March 10, 2010, 01:29:38 AM »
Hi,
Thi is my first post to the forum. I wuld like to share perl scripts(thy are inspired by the Ian's one) used to read and write 25LC640(SPI EEPROM) using BP SPI bitbang mode. I use 3M test clip to attach to soldered SOIC8 chip on powered and working board(it reads the eeprom only at boot stage). Vcc from the chip is returned to BP vpullup, so pullups resistor in use. My read time is ~2 min, write time ~4min. Maybe there is more space for improvement, but it works for my needs.

Some other comments:
The BP SPI bitbang page has description of functions that are not implemented in the firmware ( for example read config, read peripherals, read SPI speed...). Well I do not need them but  is it possible to check and mark somehow what is implemented and what not or remove not implemented/obsolete/?.

One strange thing wit the eeprom chip - I can not read the status register of the eeprom chip(RDSR 0x05)?! In the code there are my tries to read it using nibble and bulk method, both returning same strange results.

BR,
Anton Todorov

ian

  • Crew
  • Hero Member
  • *****
  • Posts: 2916
  • Karma: +61/-0
    • View Profile
Re: Scripting the Bus Pirate from Perl, Python, etc
« Reply #4 on: March 10, 2010, 02:15:07 AM »
Thanks Anton. I'll add this script to the SVN if that's ok with you.

You're right about the bitbang read commands, they're not implemented yet. The other protocols I think note that they're not available but it might not be in all the guides.

What do you get when you read the status register?

anton.todorov

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Scripting the Bus Pirate from Perl, Python, etc
« Reply #5 on: March 10, 2010, 11:40:14 AM »
I am OK to add it to the SVN. here is attached newer version (is it possible to change/improve the script after it is added to SVN?).
 I added two log files. With CLI the reading of status register is working(CLI.log): I first read 4 bytes( { 3 0 0 0 0 0 0 }), then Enable WriteLatch({ 6 }), read status register ({ 5 0 }), disable WriteLatch ({ 4 }) and reread Status register. It works! The second one is my script with bitbang mode. I added both Nibble and bulk functions to read status register to SPIread.pl. Then I run  `SPIread.pl -b 4` to read first 4 bytes. There are some debug info. here is in brief:
Reading with the nibble method:
Quote
< send high nibble of Byte %
! RS232 write incomplete! 0x01!=0x02
< send low nibble of Byte and EEPROM read status
< RAWrsTxRx(0011)(0x25=0b00100101=37) rawRx:0xff [ ]

RDSR 1: ff
Here is with Bulk read:
Quote
< SPIbulkTxRx(1)...
& decodeBPCmdH:0b0001 Bulk SPI transfer: 0+1 bulk bytes follow
< RAWrsTxRx(0111)(0x10=0b00010000=16) rawRx:0x01 [ ]
< RAWrsTxRx(0011)(0x05=0b00000101=5) rawRx:0xff [ ]
RDSR 2: ff

and at the end I red same 4 bytes as in CLI(bulk sending EEPROM READ cmd and the address then with nibble reading...):
Quote
& Read bytes:0x0004(4) addr:0000 eesize:0x2000(8192) file:dump_r.bin
< EE cmd:0x03[0b0011] addr:0000 arr[0x03 00 00 ]
< SPIbulkTxRx(3)...
& decodeBPCmdH:0b0001 Bulk SPI transfer: 2+1 bulk bytes follow
<    rsTxRx(0101)(0x12=0b00010010=18) [ACK 0x01]
<    rsTxRx(0001)(0x03=0b00000011=3) [NAK 0xff]
<    rsTxRx(0001)(0x00=0b00000000=0) [NAK 0xff]
<    rsTxRx(0001)(0x00=0b00000000=0) [NAK 0xff]
# NIBBLE METHOD
& decodeBPCmdH:0b0011 High 4 bits:0x0_
<    rsTxRx(0101)(0x30=0b00110000=48) [ACK 0x01]
< RAWrsTxRx(0011)(0x20=0b00100000=32) rawRx:0x1e [^]
< RAWrsTxRx(0011)(0x20=0b00100000=32) rawRx:0x9e [ ]
< RAWrsTxRx(0011)(0x20=0b00100000=32) rawRx:0x02 [ ]
< RAWrsTxRx(0011)(0x20=0b00100000=32) rawRx:0x0b [ ]
There are "! RS232 write incomplete! 0x01!=0x02" lines, but I am not sure are they wrong or not, because reading and writing to flash is working...
I do not have oscilloscope and can not check what is happening at the wires :(

BR,
Anton Todorov

cliff52

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Scripting the Bus Pirate from Perl, Python, etc
« Reply #6 on: April 15, 2010, 06:12:35 AM »
First, thanks for the cool scripts - here's some notes I learned in getting them working with Windows 7:
1.  Google 'ActiveState Perl', find it, and install it.  I used "C:\Perl" as the location on my machine. 
2.  The actual Perl.exe is in the "c:\perl\bin" subdirectory.  If you want to run perl from anywhere, you have to add this to the 'environment variable'.
3.  Modify the environment variable by clicking on the round windows globe at the bottom left hand corner of the screen.  "Right-click" on "Computer" and select "Properties" in that menu.  Find 'Advanced Settings' on the window that opens.  That gives yet another window.  Click on the 'Advanced' tab.  At the bottom of this screen (still with me?) there's a button that says 'Environment Variables'.  Click it and another window opens.  In the bottom window, scroll down to 'Path' - click once to highlight it and click on 'Edit'.  Now, at the end of the string, add a semicolon, then "c:\perl\bin".  Click 'Okay' to save your work.
4.  Click the windows globe thing and type 'cmd' in the search window.  Mine comes up in the 'c:\windows\system32' directory.  I always move to another directory, so run 'cd c:\perl', for instance.
5.  You will occasionally run into permissions issues with Windows 7.  Sometimes, the fix for this is to 'Right-Click' on the 'cmd' icon and select 'Run as administrator'.  You can google this first and read about the benefits and possible problems with doing this. 
6.  The BusPirate perl scripts need Win32::SerialPort to work.  I couldn't find a pre-compiled module with ppm for this.  Google 'Win32::SerialPort' and 'CPAN' and download it as an archive.  Extract it to 'c:\perl\lib'.  It will make a directory  'Win32-SerialPort' there.
7.  If, like me, you don't have a compiler and make program installed, google 'MinGW' and find its' home.  There's an install program.  Mine ended up in 'c:\MinGW'.  I put 'c:\mingw' and 'c:\mingw\bin' in my environment variable.
8.  Google 'dmake' and find its' home.  I downloaded the archive and extracted it to 'c:\dmake'.  You can add this to the environment variable, or copy the executable and 'startup' subdirectory to some directory already in your path.  I copied to 'c:\perl\bin'.
9.  Cd to some harmless directory, say 'c:\perl', and run 'mingw' from the command line to see that it works.  Also try 'dmake'.  They won't do anything, but should show that your system knows where they are...
10.  Now, cd to 'c:\perl\lib\win32-serialport' .  You should be able to run 'perl makefile.pl' from there.  I then ran 'dmake' from there and it appeared to install the Win32::SerialPort module.  After that, I was able to run the example perl scripts here, though I had to change them from 'COM2' to 'COM4'.

rct

  • Jr. Member
  • **
  • Posts: 62
  • Karma: +2/-0
    • View Profile
Re: Scripting the Bus Pirate from Perl, Python, etc
« Reply #7 on: April 29, 2010, 09:59:28 AM »
Regarding Perl (ActivePerl) on Windows and Win32::SerialPort support.   First thanks very much for providing those instructions.   Second a little background on why Win32::SerialPort isn't available as an easily installable PPM.

6.  The BusPirate perl scripts need Win32::SerialPort to work.  I couldn't find a pre-compiled module with ppm for this.  Google 'Win32::SerialPort' and 'CPAN' and download it as an archive.  Extract it to 'c:\perl\lib'.  It will make a directory  'Win32-SerialPort' there.

I did a little googling on this.  It looks like ActiveState tries to automatically build this and make it available, however building Win32::SerialPort v.20 fails for some reason for Perl 5.10. 

Here's the log: http://ppm4.activestate.com/MSWin32-x86/5.10/1000/B/BB/BBIRTH/Win32-SerialPort-0.20.d/log-20100403T033835.txt.
The status of the builds can be found here: http://ppm4.activestate.com/idx/W3...WU.html.

It does however succeed for Perl versions 5.8 and 5.12.   5.12 look like it's still a little early (less than half of the packages are available for it).  Though if you only need Win32::SerialPort and a few others, it might be worth an early upgrade to 5.12.    Alternatively ActivePerl 5.8 while old might be a better choice.

I sent a note off to the author of Win32::SerialPort. I'll let you know if I hear anything back.

--Rob