UPDATE: new firmware with JTAG and more
We’re always ecstatic to get a new chip or SIM card to interface, but our enthusiasm is typically dampened by the prototyping process. Interfacing any chip normally indicates breadboarding a circuit, writing code, and hauling out the programmer; maybe even a prototyping PCB.
A few years ago we built the first ‘Bus Pirate’, a universal bus interface that talks to a lot of chips from a PC serial terminal. several conventional serial protocols are supported at 3.3-5volts, including I2C, SPI, and asynchronous serial. additional ‘raw’ 2- and 3- wire libraries can interface nearly any proprietary serial protocols. considering that this has been such a beneficial tool for us, we cleaned up the code, documented the design, and released it here with specs, schematic, and source code.
Concept Overview
The Bus Pirate is a serial terminal bridge to multiple IC interface protocols. We type commands into a serial terminal on the computer. The commands go to the Bus Pirate through the PC serial port. The Bus Pirate talks to a microchip in the proper protocol, and returns the results to the PC.
All pins output 3.3volts, but are 5volt tolerant. On-board 3.3volt and 5volt power supplies are available to power the connected chip. software configurable I2C pull-up resistors complete the package.
The serial terminal interface works with any system: PC, Mac, Linux, palm Pilots, WinCE devices, etc; no crapware required. We considered a USB device, but USB isn’t compatible with the substantial number of hand-held devices that have a serial port. We also wanted a 3.3volt device with 5volt tolerant inputs, but a lot of popular through-hole USB microcontollers were 5volt parts (e.g. the PIC18Fx550).
The Bus Pirate currently ‘speaks’ three hardware protocols for high-speed interfacing, and has two software protocol libraries for easy bus manipulation. The theory and specification of each protocol is beyond what we can cover here, but check out some of these tutorials:
I2C
A slow 2 wire bus. Wikipedia is a terrific place to start for I2C background. I2C-Bus.org, Robot Electronics, Embedded Systems Academy, and Embedded.com have respectable I2C tutorials.
SPI
A basic 3 wire bus. Wikipedia has background; Embedded.com has a terrific tutorial and comparison to I2C.
Universal Asynchronous Receiver Transmitter (UART or serial)
A clock and timing dependent serial protocol best known for its appearance as the PC serial port protocol. Wikipedia has background on asynchronous serial protocols.
Raw 2 wire
This is a generic 2 wire protocol library, similar to I2C but without an ACK bit. I2C and lots of proprietary 2 wire protocols can be formed using the bus manipulations available in this mode. use this library to work with non-I2C 2 wire devices, like smartcards or Sensirion SHT11 temperature/humidity sensors.
Raw 3 wire
This is a generic 3 wire protocol library, similar to SPI but without the constraints of a hardware module. use this library to work with devices that use non-8bit compatible 3-wire protocols, like the Sparkfun Nokia 6100 LCD knock-off. lots of 3 wire protocols can be formed using the bus manipulations available in this mode.
Σκεύη, εξαρτήματα
Click for a full size PCB placement image (PNG). Screw terminals connect to the power supplies. A row of seven pin headers connect to the IO pins. despite the label, only 7volts DC is required.
ΚΑΡΦΙΤΣΑ
SPI
I2C
RS232
B9
MOSI
Sda
–
B8
CLK
Δίδω
–
B7
Μου
–
RX
B6
Cs
–
TX
B5
Ατόμων
Ατόμων
Ατόμων
Εδαφος
ΓΕ
ΓΕ
ΓΕ
This table shows the pin connections for each bus mode. Raw 2 wire mode uses the same pin configuration as I2C. Raw 3 wire mode uses the same pin configuration as SPI.
Click for a full size circuit image (PNG). The circuit and PCB are created using the freeware version of Cadsoft Eagle. download the project archive (ZIP).
PIC 24FJ64GA002
We used a PIC24FJ64GA002 microcontroller in the Bus Pirate; this is the same chip we used in our mini-server project. It’s fast enough to do everything we want (16MIPS), and the peripheral pin select feature allows the hardware SPI, UART, and I2C modules to share output pins. Each power pin needs a decoupling capacitor(C12,13), and the MCLR function requires a resistor (R7) between pin 1 and 3.3volts. The photo has an internal voltage regulator that requires a 10uF tantalum capacitor (C3), though we used a plain electrolytic capacitor without issue. read about programming and working with this chip in our PIC24F tutorial. If you don’t have a photo debugger, several readers recommend the under-$40 ICD2 clones on eBay.
The photo runs at 3.3volts, but the digital-only pins are 5volt tolerant for interfacing 5volt logic. Pins 14,15,16,17,18,21, and 22, are digital only, which we figured out by looking through the datasheet and eliminating any pins with an analog connection type (table 1-2, pages 11-16). According to the datasheet,I2C pins are also 5volt tolerant. There’s a bunch of conflicting information on the web, but datasheet page 230, parameter DI28, clearly states that the max input for a 24FJ64GA002 I2C pin without analog circuitry is 5.5volts.
Pins 21 and 22 (RB10/11) can pull-up SDA/SCL through resistors R4 and R5.
MAX3223CPP
This chip converts 3.3volt serial output to +/-10volt RS232 signals compatible with a PC serial port. The MAX3223CPP is a 3-5volt version of the MAX202, with extra power saving features. MAX RS232 transceivers require four 0.1uF capacitors for a charge pump (C4,5,7,8), and one decoupling capacitor (C17). We used the same capacitors for everything.
We used a MAX3223CPP, which doesn’t seem to be available anymore. MAX3223EEPP+ is a pin-compatible newer version, available at Digikey for $7. Ωχ! None of the 3223’s power saving features are used, so a cheaper, simpler 3.3volt RS232 transceiver must be substituted if in any way possible.
Τροφοδοτικά
Most chips can be powered from the Bus Pirate’s on-board 3.3volt and 5volt supplies. 5volts is supplied by a common 7805 regulator (VR2) and two decoupling capacitors (C9,10). An LM317 adjustable regulator (VR1) is set to 3.3volts using two resistors (R2,3), and requires two decoupling capacitors (C6,7). The circuit requires a 7-10volt DC supply (J1).
Λίστα εξαρτημάτων
Μέρος
αξία
Ic1
PIC24FJ64GA002-DIP
Ic2
MAX3223CPP (try MAX3223EEPP+)
C3
10uF capacitor (preferably tantalum)
C4-13,17
0.1uF capacitors
R1
330 ohm resistor
R2
240 ohm resistor
R3
390 ohm resistor
R4,5,7
2K2 ohm resistor
Vr1
LM317
Vr2
LM7805
X1
Screw clamp (3 terminals) *untested
X2
DB9 female connector (serial port) *untested
ICSP,SV3
.1″ pin header, ideal angle
J1
Power jack, 2.1mm pin
LED1
3mm LED (optional)
Υλικολογισμικό
Το υλικολογισμικό γράφεται στο C χρησιμοποιώντας την δωρεάν έκδοση επίδειξης της φωτογραφίας C30 Compiler. Μάθετε όλα σχετικά με την εργασία με αυτή τη φωτογραφία στην εισαγωγή μας στη σειρά φωτογραφιών 24F. download the project archive (ZIP).
main.c – Handles the user terminal interface.
busPirate.c – Abstraction routines that convert syntax to actions on the proper bus.
uartIO.c – IO routines for both hardware UARTs.
m_i2c_1.c – software I2C routines by [Michael Pearce]. We couldn’t get the photo hardware I2C to work, so we used this valuable library. The software doesn’t take into account the I2C speed setting, and seems to work at about 5KHz.
SPI.c – routines that drive the hardware SPI module.
raw2wire.c – software 2-wire interface library.
raw3wire.c – software 3-wire (SPI) interface library.
User input is held in a 4000 byte buffer until a newline character (enter) is detected. If the first character of the input is a menu option (see below), the menu dialog is shown, otherwise the string is parsed for data to send over the bus (see syntax). The code consists of an embarrassing number of switch statements and spaghetti code.
Terminal interface
Rather than write a junk piece of software to control the device, we gave it a serial command line interface that will work with any ASCII terminal. The bus pirate responds to commands with three digit result codes and a short message. The codes are created with PC automation in mind. We’ve included a table of result codes in the project archive (zip).
Menu options
Menu options are single character commands that don’t involve data transfers. enter the character, followed by , to access the menu.
? – show a help menu with commands and syntax.
M – set the bus mode (SPI, I2C, UART, raw 2 wire, raw 3 wire). followed right away by a prompt for speed, polarity, and output state (mode dependent).
Bus speeds: SPI:30, 125, 250, 1000KHz. I2C:100, 400, 1000KHz. UART: 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200bps. Raw modes: 1, 10, 50KHz.
Inverse clock setting sets the idle state opposite of normal (normal SPI:idle low; normal UART:idle high): SPI:idle high; UART:idle low.
Some modes have optional high-z output modes for use with pull-up resistors (Low=ground, High=input).
L – Toggle bit transmit/receive order: most/least significant bit first.
P – SDA/SCL pin pull-up resistor toggle (3.3volts). only valid in I2C and raw 2 wire modes.
O – set number output display format. The terminal can display numbers as decimal, hexadecimal, and binary ASCII values. A fourth format sends the raw, unprocessed byte for reading ASCII formatted text.
Σύνταξη
A basic syntax is used to communicate with chips over a bus. Syntax commands have generic functions that normally apply to all bus types.
A/a/@ – Toggle auxiliary pin. capital “A” sets AUX high, small “a” sets to ground. @ sets aux to input (high impedance mode) and reads the pin value.
[ – start data write. SPI/raw 3 wire: chip select enabled. I2C/raw 2 wire: start condition. RS232: open UART, discard received bytes.
{ – start data write with reads. same as [, except: SPI/raw 3 wire: show the read byte for each write. RS232: display data as it arrives asynchrπνεύμα.
] ή} – Γράψτε τα στοιχεία λήξης. SPI / RAW 3 σύρμα: Chip Επιλέξτε απενεργοποιημένο. I2C / RAW 2 καλώδιο: κατάσταση διακοπής. RS232: Κλείστε UART.
R / R – Διαβάστε Byte. SPI / RAW 3 σύρμα: Στείλτε το Dummy Byte, Return Reve. I2C: Διαβάστε byte με ACK. RAW 2 WIRE: Διαβάστε 8 bits. RS232: Ελέγξτε το UART για byte και επιστροφή ή αποτύχει εάν είναι κενή. Χρησιμοποιήστε το 0R1 … 255 για το χύμα διαβάζει έως και 255 byte.
0b – Γράψτε αυτή τη δυαδική τιμή. Η μορφή είναι 0b00000000 για ένα byte, αλλά τα μερικά bytes είναι επίσης καλά: 0b1001.
0h ή 0x – Γράψτε αυτή την τιμή hex. Η μορφή είναι 0h01 ή 0x01. Τα μερικά bytes είναι καλά: 0xa. Το A-F μπορεί να είναι μικρά γράμματα ή κεφαλαία γράμματα.
0-255 – Γράψτε αυτή τη δεκαδική τιμή. Οποιοσδήποτε αριθμός που δεν προηγείται των 0x, 0h, ή 0b ερμηνεύεται ως δεκαδική τιμή.
, ή οριοθετητής αξίας χώρου. Χρησιμοποιήστε ένα κώμα ή χώρο για να διαχωρίσετε τους αριθμούς. Οποιοσδήποτε συνδυασμός είναι ωραία, δεν απαιτείται οριοθέτης μεταξύ των μη αριθμητικών τιμών: {0xA6,0, 0 16 5 0b111 0HAF}.
Εντολές χειρισμού άμεσης διαύλου για λειτουργία RAW 2 WIRE και λειτουργία RAW 3 WIRE.
^ – Στείλτε ένα ρολόι. Χρησιμοποιήστε το 0 ^ 1 … 255 για πολλά κρότωνες ρολογιών.
/ και \ – Εναλλαγή επίπεδου ρολογιού υψηλό (/) και χαμηλό (\). Περιλαμβάνει καθυστέρηση ρολογιού (100us).
– / – – Εναλλαγή δεδομένων Υψηλή (-) και χαμηλή (_). Περιλαμβάνει καθυστέρηση εγκατάστασης δεδομένων (20us).
! – Διαβάστε ένα κομμάτι με το ρολόι.
. – Διαβάστε το κράτος PIN δεδομένων (χωρίς ρολόι).
& – καθυστέρηση 1us. Χρησιμοποιήστε 0 & 1 … 255 για πολλές καθυστερήσεις.
Χρησιμοποιώντας το
Ακολουθούν δύο παραδείγματα που δείχνουν τον πειρατή του λεωφορείου σε δράση. Οι τερματικοί σταθμοί πρέπει να ρυθμιστούν σε λειτουργία ASCII με τοπική ECHO, χρησιμοποιήσαμε το σειριακό τερματικό των Windows. Η σειριακή σύνδεση PC-πλευρά είναι 115200bp, 8N1. Ο πειρατής του λεωφορείου πρέπει να ανταποκρίνεται σε οποιοδήποτε τύπο τροφοδοσίας μιας γραμμής (0x0a, 0x0d), ή και τα δύο (Windows Style).
.I2C / SPI – Flash 24LC1025 EEPROM
Το EEPROMS του μικροτσίπ είναι δημοφιλείς μάρκες μνήμης μόνιμης αποθήκευσης, το 24LC1025 έχει 128kbytes αποθήκευσης με διεπαφή I2C. Μπορούμε να δοκιμάσουμε αυτό το τσιπ χωρίς την επιβίβαση από ψωμί ένα μεγάλο κύκλωμα ή έναν κώδικα γραφής.
Η εικόνα δείχνει ένα 24LC1025 συνδεδεμένο με τον πειρατή του λεωφορείου. Το EEPROM λειτουργεί από 2,7 έως 5βουλες, έτσι χρησιμοποιήσαμε την τροφοδοσία 3.3volt από τον πειρατή του λεωφορείου για να τροφοδοτήσει το κύκλωμα. Οι αντιστάτες SDA / SCL RUD-UP HOLD HOW το δίαυλο I2C υψηλό και εξαλείφουν την ανάγκη για εξωτερικές αντιστάσεις. Ένας ενιαίος πυκνωτής 0,1UF αποκλείει το EEPROM από την παροχή ρεύματος.
Ρυθμίστε τη λειτουργία i2c
Πρώτον, ρυθμίζουμε τον πειρατή του λεωφορείου για τη λειτουργία I2C και ενεργοποιούμε τις αντιστάσεις pull-up. Λαμβάνοντας υπόψη ότι ο πειρατής λεωφορείων χρησιμοποιεί σήμερα μια βιβλιοθήκη λογισμικού I2C, η ρύθμιση ταχύτητας δεν έχει πραγματικά αποτέλεσμα.
SPI> M <-ENTER M για λειτουργία Επιλέξτε 1. SPI 2. I2C 3. UART 4. RAW 2 καλώδιο 5. RAW 3 καλώδιο Λειτουργία> 2 <-ENTER 2 για το I2C 900 Λειτουργία Ρύθμιση ταχύτητας: 1. 100kHz (Standard) 2. 400KHz (γρήγορη λειτουργία) 3. 1MHz (υψηλή ταχύτητα) Ταχύτητα> 1 <-speed δεν κάνει τίποτα ... 901 SPEED SET 202 I2C έτοιμο, p / p για pullups I2c> p <-Ενεργοποίηση των αντιστάσεων I2C Pull-up 205 i2c pullup on I2c>
Γράψτε στο EEPROM (I2C)
Όλες οι λειτουργίες I2C ξεκινούν με μια κατάσταση έναρξης {ή [και το τέλος με μια κατάσταση διακοπής} ή]. Μια εγγραφή αρχίζει απευθύνοντας τη συσκευή (1 byte) και προσπαθώντας να βρούμε ένα bit επιβεβαίωσης (ACK). Εάν το EEPROM ανταποκρίνεται, μπορούμε να στείλουμε τη θέση δεδομένων για να γράψουμε (2 bytes) και το ωφέλιμο φορτίο δεδομένων (n bytes). Ο πειρατής του λεωφορείου ελέγχει αυτόματα για ένα ACK στο τέλος κάθε εγγραφής και Acks κάθε ανάγνωση.
Η διεύθυνση βάσης 24LC1025 είναι 1010xxy, όπου το XX καταλαμβάνεται από την κατάσταση των πείρων 2 και 3 και το y διαβάζεται (1) ή εγγραφή (0). Έχουμε δεσμευτεί οι καρφίτσες 2 και 3 ψηλά, κάνοντας την πλήρη διεύθυνση εγγραφής 1010110. Θα αρχίσουμε να γράφουμε στη συσκευή στην πρώτη θέση δεδομένων (0 0) και να γράψουμε ένα έως δεκατρείς χρησιμοποιώντας ένα μείγμα μορφών εισόδου δεδομένων (1 … 13) .
I2C> {0b10100110 0 0 1 2 3 4 5 6 7 8 9 10 0xB 0xc 13} <-i2c εντολή 210 Ι2C Κατάσταση εκκίνησης <-Bus Έναρξη 220 I2C Γράψτε: 0xA6 πήρε ACK: ναι <-Address που στάλθηκε και η ACK έλαβε 220 I2C Γράψτε: 0x00 πήρε ACK: ναι <-WRITE Διεύθυνση 220 I2C Γράψτε: 0x00 πήρε ACK: ναι <-WRITE Διεύθυνση 220 I2C Γράψτε: 0x01 πήρε ACK: ναι <-data ... 220 I2C Γράψτε: 0x0d πήρε ACK: Ναι 240 I2C Stop Condition I2c>
Διαβάστε από την EEPROM (I2C)
Η ανάγνωση του 24LC1025 λαμβάνει δύο βήματα. Πρώτον, μια εντολή εγγραφής χωρίς δεδομένα ορίζει τον δείκτη διεύθυνσης. Second, a read comm