This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Bit banging" – news · newspapers · books · scholar · JSTOR (July 2014) (Learn how and when to remove this message) |
In computer engineering and electrical engineering, bit banging or bit bashing is a term of art for any method of data transmission that employs software as a substitute for dedicated hardware to generate transmitted or process received signals. Such software directly sets and samples the states of GPIOs (e.g., pins on a microcontroller) to transmit and receive, respectively, and is responsible for meeting all timing requirements and protocol sequencing of the signals. In contrast to bit banging, dedicated hardware (e.g., UART, SPI, I²C) satisfies these requirements. If necessary, it also provides a data buffer to relax software timing. Bit banging can be implemented at very low cost, and is commonly used in embedded systems.
Bit banging allows a device to implement different protocols with minimal or no hardware changes. In some cases, bit banging is made feasible by newer, faster processors, as modern hardware operates much more quickly than the hardware used when standard communication protocols were created.
C code example
See also: SPI bus - simultaneously transmit and receive a byteThe following C language code example transmits a byte of data on an SPI bus.
// transmit byte serially, MSB first void send_8bit_serial_data(unsigned char data) { int i; // select device (active low) output_low(SD_CS); // send bits 7..0 for (i = 0; i < 8; i++) { // consider leftmost bit // set line high if bit is 1, low if bit is 0 if (data & 0x80) output_high(SD_DI); else output_low(SD_DI); // pulse the clock state to indicate that bit value should be read output_low(SD_CLK); delay(); output_high(SD_CLK); // shift byte left so next bit will be leftmost data <<= 1; } // deselect device output_high(SD_CS); }
Considerations
The question of whether to deploy bit banging or not is a trade-off between load, performance and reliability on one hand, and the availability of a hardware alternative on the other. The software emulation process consumes more processing power than does supporting dedicated hardware. The microcontroller spends much of its time sending or receiving samples to and from the pins, at the expense of other tasks. The signal produced typically has more jitter or glitches, especially if the processor is executing other tasks while communicating. However, if the bit-banging software is interrupt-driven by the signal itself, this may be less important, especially if control signals such as RTS, CTS, or DCD are available. The implementation in software can be a solution when specific hardware support is not available or requires a more expensive microcontroller.
See also
- Bit manipulation
- Bit stream
- Bit twiddler (disambiguation)
- Bit-serial architecture
- 1-bit architecture
- Fast loader
- Integrated Woz Machine (IWM)
- FTDI (a series of USB to serial converter chips also supporting a bit bang mode)
- Light pen (on MDA, HGC, CGA and EGA PC-compatible graphics cards the sensor of an optional light pen could be read in a fashion similar to bit banging)
- Virtual machine (VM) (implementing virtual device drivers emulating actual hardware controllers sometimes involves utilizing programming techniques similar to bit banging)
- Software-defined radio (SDR)
- Polling (computer science)
References
- "Analog Devices Glossary of Electrical Engineering (EE) Terms". Retrieved 2024-09-22.
- Predko, Michael (2000). Programming and customizing PICmicro microcontrollers (2nd ed.). McGraw-Hill Professional. pp. 10–12. ISBN 978-0-07-136172-9.
External links
- Asynchronous serial (RS-232)
- I²C bus
- SPI bus