Joined: Fri Aug 30, 2002 1:09 am Posts: 8546 Location: Southern California
|
Quote: Err, I daresay I only know what they are in rough terms. Its basically, instead of sending the data at regular time intervals over a parallel bus or the sorts, you simply send it like serial communications would, except at higher speeds. That's my rough idea for it. I have no clue what SPI, I²C, Microwire, and the other things are. Even my concept of RS232 communication is pretty bad. I'll try my hand at a description that's short yet useful.
In interfaces, bits go through one at a time, not 4, 8, 16, or any other number like processor data buses use. Obviously the receiver needs a reference so it can tell what bit it is receiving at any given time.
In RS-232, that reference is strictly the timing, and the transmitter and receiver must agree that every bit takes a certain number of microseconds. Each frame begins with a "0" start bit, and its leading edge starts the receiver's timer. Then the receiver knows when to sample the data for each bit after that, usually 8 data bits, optionally (but not often) a parity bit for error detection, and one or more "1" stop bits which also serve for error detection as well as setting up the line for the next frame's start bit. You can see that if the transmitter's and receiver's bit clocks don't quite run the same speed, you'll be coming up with errors. After ten bits in a frame, a 5% error is half a bit time, which will put you in the transition area between bits. In many situations, you can get away with a 2% speed error. RC timing is not tight enough; but ceramic resonators, which have less than 1% error, will be good enough. Usually crystal control is used, which it more accurate.
RS-232 voltages are high to minimize interference, which is the main reason line drivers and receivers such as the popular MAX232 are used. (There are plenty of other line drivers and receivers though. I prefer the MC145406 which has three of each in a 16-pin IC.) The RS-232 specification says the receiver must interpret -3 to -25 volts as a "1", and +3 to +25 volts as a "0". (The line drivers and receivers invert the voltage, making a "1" the low voltage and a "0" the high voltage.) The transmitter must transmit -5 to -15 volts for a "1", and +5 to +15 for a "0", and must withstand shorts to ground indefinitely without damage. The area from -3V to +3V is not defined. The load offered by the receiver is to be 3,000 to 7,000 ohms. Additional lines are often used, most commonly for the receiver to tell the transmitter if it is ready for more data. These additional lines are simple logic states (driven to RS-232 voltages), and do not have the start, stop, parity, and data bits.
Intended uses for RS-232 are for communication between pieces of equipment across a room, even a big one like a factory or large office. RS-232 can typically go at least a few hundred feet, depending on bit rate and wire type. It reaches much, much farther than USB. I have an RS-232 primer on my website, at http://wilsonminesco.com/RS-232/RS-232primer.html .
If RS-232 doesn't go far enough for you, or you want something similar that goes a lot faster, you can go to RS-422 or RS-485 which are differential (balanced), and the output can come from 5V or 3V drivers since the inputs can tolerate up to 6V and you only need 200mV difference between the two wires to get a valid logic level. Here's the idea, showing transmitting $D3:
Attachment:
RS-485_waveform.gif [ 8.13 KiB | Viewed 1744 times ]
RS-422 can do 10Mbps at 40 feet, and RS-485 can do 35Mbps at 33 feet. Both can go at least 90kbps at 3/4 mile. A rule of thumb for RS-485 max speed versus cable length is that the speed in bit/s multiplied by the length in meters should not exceed 10^8. Thus a 50-meter cable (164 feet) should not signal faster than 2 Mbit/s.
There are several popular synchronous serial interfaces. They are more commonly used to communicate between ICs on a board, not across a room. ICs using these have fewer pins, take less room on the board, and dramatically reduce the number of connections you will need to make. Take for example a multi-megabyte EEPROM or flash memory in an 8-pin package, as opposed to a parallel equivalent having 48 pins. The synchronous serial interfaces, as far as I can think of right now, all transmit the most-significant bit first, unlike RS-232 which transmits the least-significant first (after the start bit).
SPI (serial peripheral interface) was devised by Motorola. There is a master and one or more slaves. Each slave has 4 unidirectional SPI lines: data in, data out, clock in, and select in. The master has a minimum of four: data in and out, plus clock out, and one select line for each slave, so it may have several select outputs. For brevity and clarity, the data-in and -out pins are referred to as MISO (master-in, slave-out), and MOSI (master-out, slave-in). MISO, MOSI, and clock are bussed to all devices. These typically operate at 3-5V and light CMOS loads, unlike RS-232.
To talk to a slave, the SPI master sets that slave's select line true, puts the first data bit on its data-out line (which goes to all the slaves' data-in lines), and puts the first active edge on its clock-out line. The slave will put its first data bit for the master on its data-out pin for the master to latch in on the clock's next active edge. Since a bit is sent by the master and another one is sent by the slave with every clock cycle, sometimes there's dummy data going one of the directions when there's no need for data transfer in that direction. There's more than one mode relating to clock polarities, but if you look at the data sheet for any IC of interest, it will have all the info you need in order to use it. You don't need to first get familiar with the SPI spec from another document.
I have a note here that you can daisy-chain SPI parts if desired, but from what I know about the SPI parts I've used, I have a hard time imagining they could be used that way. If you daisychain them, the same select line will go to the select pin of every IC in an entire daisychain.
There's a 65c02 assembly-language example of bit-banging SPI on 65SIB with a 6522 VIA at http://wilsonminesco.com/6502primer/SPI.ASM .
Microwire is National's invention and is very similar to SPI, but there is only the one mode instead of SPI's four modes of clock polarity.
Going down the line of intelligence, you can use a long daisychain of dumb shift registers like the 4094, 4021, 74HC165, and 74HC595 connected directly to the VIA's CB1 and CB2 to get hundreds of bits of I/O through that port. Instead of a select line, you'll need a VIA output bit to latch the shift registers' values into the outputs (in the case of serial-in to parallel-out) or to load the parallel inputs into the shift registers to send serially to the VIA (in the case of parallel-in to serial-out). The VIA's SR can be used for output or input, but not both at once. I have a diagram showing how to connect a lot of 74HC595's to the VIA's shift register pins here, one showing how to connect a lot of 74HC165's to the VIA here, and one showing both types connected at once here about 60% of the way down the front page of the math look-up tables section of my website.
The 4094 and 4021 can handle up to 15 volts. They're nice in systems where for example you have to have a lot of 12V inputs and outputs, and you don't want to have to put voltage translators on all those bits. You run the 4021's and/or 4094's on your 12V and only put the translators on the data line, the clock line, and the latch line, totaling only three instead of possibly hundreds. (This example is actually from my own work.)
Some synchronous-serial parts, for example National's LM1973 digital potentiometers, may never need to send data back to the master, and in that case, you can eliminate the master's data-in connection and just go with three wires for a whole daisy chain of them: MOSI (which goes to the first IC's data-in), clock (which gets connected to all ICs), and select (which may get called something else like "shift/load\"). The first IC's data-out pin gets connected to the next IC's data-in pin, its data out goes to the third one's data in, and so on. Eight of those LM1973's would get you 24 channels of digital pots on three VIA pins, and of course you could put other things on the same daisy chain.
I²C (inter integrated circuit) is a two-wire synchronous-serial interface from Philips. One wire is clock and the other is bi-directional data. At least the latter must have a passive pull-up resistor, and any of the ICs can pull the data line down to ground to produce a "0". There are a couple of situations where the clock line may need a passive pull-up. One is if there are two or more controllers that may control the interface at different times. Another is if you have one or more devices which may hold the clock line low to pause the master so the device doesn't fall behind. IOW, it's a way to ask more more time. Of course, if the controller does not pull the clock line up itself, a pull-up resistor will be needed. Often there's only one resistor—the one on the data line.
Instead of using select lines which make for more wires, a "start" condition is produced by lowering the data line while the clock is high. A "stop" condition is produced by letting the data line up while the clock is high. Otherwise the data line state is only changed while the clock line is low, and data is only sampled during positive (up-down) clock pulses. Each communication starts by shifting out an address (usually one byte) telling all the ICs on the interface which one is to be selected. After that, only the selected one pays attention, and the rest ignore everything until there's another "start" condition. After the address, there's an instruction, potentially followed by data. If the instruction tells a slave to send data, then the controller will listen on the data line instead of transmitting. As with SPI, Microwire, and the chain of dumb shift registers above, the controller controls the clock and therefore the data rate. There's no need for the bit speed to be constant like RS-232 needs.
There is an additional clock pulse after each byte, for the listening end to respond with an acknowledge. One function for that is that for example if the controller has started a sequential read from a memory device and that device is doing the transmitting at the time, the controller can, after it has gotten all the data it wants, refrain from acknowledging a byte so the device will stop sending data and release the data line so the master can produce a "stop" condition. The acknowledge (ACK) is made by the receiving end pulling the data line down for that 9th clock pulse. Not-acknowledge (NAK) is accomplished by letting it remain high for that 9th clock pulse.
I²C speed is limited by the time constant produced by the pull-up resistance multiplied by the capacitive load on the data line. 400kbps is pretty typical, which, although faster than virtually all RS-232, is nowhere near as fast as SPI's tens of Mbps. Some I²C devices can go 1Mbps, and a few can go faster.
There's a 65c02 assembly-language example bit-banging I²C with a 6522 VIA at http://wilsonminesco.com/6502primer/GENRLI2C.ASM . The circuit used is at http://wilsonminesco.com/6502primer/pot ... ITBANG_I2C .
I²C versus SPI pros and cons (since feedback showed confusion):
- SPI needs three lines (clock, data to the device, called MOSI, for "master-out, slave-in," and data from the device, called MISO, for "master-in, slave-out") plus one select line for each device; so you need four lines for one device, five lines for two, six lines for three, etc.. I²C only needs two, regardless of the number of devices on the bus: clock, and bidirectional data.
- SPI can go much faster. I've seen up to nearly 200MHz clock speeds for SPI. I²C's rates are severely limited by the fact that the outputs are open-drain (or open-collector), and pullups are passive, and they take time to charge the parasitic capacitance on the lines. Most I²C devices are made for 400kHz or 1MHz clock speed. None go over 5MHz.
- I²C only has one mode, which is simpler, whereas SPI has four (modes 0, 1, 2, and 3), and you have to pay attention to which mode(s) any given device can operate in.
- SPI's lines are always unidirectional, making voltage translation easier. However, most I²C devices (and certainly all the ones I've used) can operate at a range of voltages up to 5V, making voltage translation unnecessary. Many SPI devices cannot go above 3.3V. That's why the SPI-10 proposed connector standard has different versions for 5V versus 3.3V. You wouldn't want to damage a 3.3V module by plugging it into a 5V port; and a 5V device may not work properly at 3.3V. To keep them straight, one hole of the socket is plugged, and the corresponding pin of the plug on the motherboard is cut. For 5V, it's pin 3, and for 3.3V, it's pin 4.
- Many I²C devices can dictate the data rate by holding the clock line down if they need more time to process each bit. The controller watches for the clock line to be up before pulling it back down; so it won't get ahead of the device. With SPI, you just have to know ahead of time how fast it can go (although we're not in any danger of outrunning it with our 65xx projects and bit-banging).
- Because of the tradeoffs between speed, number of connections, and operating voltage range, the availability of certain IC functions will lean more toward one or the other. Large flash memories for example will be SPI, because it would take too long to transfer a large file by I²C, whereas things like keypad interfaces, RTCs, and digital thermometers may be more available in I²C.
- I²C allows passing control from one master to another, something SPI does not. Myself, I've never had the need for that.
I like, and use, both I²C and SPI, for different reasons and applications.
SMBus, defined by Intel and Duracell in 1994, is almost the same as I²C. One of the main differences is that there are time-outs built into the SMBus specification. I²C is much more common.
1-Wire is the invention of Dallas Semiconductor, now part of Maxim. It's almost carrying it too far if you ask me. The master produces a clock pulse on the line by pulling it down for a microsecond. If the sender (whether that's the master or a slave) wants to send a "1", it lets the line float up at the end of that clock pulse. If it wants to send a "0", it then holds the line down a little longer. The receiver will look anywhere from 15 to 60µs after the low clock pulse to see if the transmitter is still holding the line low to indicate a 0. After the 60µs plus adequate time to make sure the line has time to float up by the pull-up resistor, the transmitter can make the next low clock pulse. As you can see, it won't be very fast.
Every 1-Wire device has a serial number; so even if you have a bunch of three-pin digital-thermometer ICs of all the same type for example, the master can still address them separately. As you can see, it is important to have control of the timing on 1-Wire, even though it's supposedly a synchronous-serial interface, ie, one where the master controls the clock and all the devices adhere to it. You cannot allow interrupts during bits when bit-banging. Interrupts between bits is ok.
You can put I²C and certain modes of SPI on the same lines if you take a couple of precautions. First, when using the SPI, you must be careful not to accidentally toggle the data line while the clock line is high, since that could produce an unwanted "start" condition on the I²C. Second, make sure no SPI select line is ever true (low) when you carry out I²C communication. SPI speed might also have to be held low enough that an I²C device would not misinterpret something to be a start condition; but you probably would not be bit-banging it that fast through a home-made 6522 system. You can probably also see by now that you could combine more than just these two interfaces on the same clock and data lines, as long as you don't violate the rules for any of the interfaces.
some links: my RS-232 primer, with 6502 and 6551 relevance RS-422 article on Wikipedia RS-485 article on Wikipedia I²C overview from Philips Semiconductor SMBus article on Wikipedia I2CChip.com I²C, SPI, 1-Wire interfacing made easy Guide for reliable long-line 1-Wire networks B&B Electronics (Advantech): serial converters SD-card SPI Maxim ap note SD/MMC cards, using SPI mode SD/MMC/SDHC card library MAX3421E USB peripheral/host controller IC with SPI National Semi Microwire description (.pdf) SS22: 6522 synchronous-serial data link between computers 65SIB: a hobbyist-friendly serial interface bus, compatible with SPI but more flexible I2C-6: a hobbyist-friendly connector standard for small I²C modules, suitable for common perfboard SPI-10: a hobbyist-friendly connector standard for small SPI modules, suitable for common perfboard Daryl Rictor's 65SPI chip he sells, version 2, made using using an Atmel ATF1504 CPLD. (65SPI is a 65-family SPI I/O IC, not to be confused with 65SIB which is the super-flexible interface method)
Edited Dec 2, 2020 to fix links that had gone dead, and May 3, 2022 to add the pros & cons of I²C versus SPI.
_________________ http://WilsonMinesCo.com/ lots of 6502 resources The "second front page" is http://wilsonminesco.com/links.html . What's an additional VIA among friends, anyhow?
Last edited by GARTHWILSON on Tue Apr 10, 2012 5:27 am, edited 2 times in total.
|
|