Bit-Bang
Bit-banging is a process where one emulates an unavailable peripheral using direct port manipulation commands on GPIO pins.
This article concerns digital protocol emulation. For the similar-sounding but generally unrelated topic of controlling a system, see Bang-bang.
A common example of this process would be the Arduino SoftwareSerial library, which emulates additional UARTs on a microcontroller with only one in hardware.
Uses
Some microcontrollers may have a limited number of key peripherals, or may map them to pins required for other purposes. If for any reason a peripheral of the correct type is unavailable, bit-banging is an alternative for many protocols. SPI is the easiest protocol to bit-bang, as it is synchronous and therefore resistant to slight timing changes caused by interrupt execution. However, using clever programming with hardware timers, USARTs and I2C can be emulated as well. Unusual or custom protocols must often also be bit-banged.
When emulating a peripheral, the timing requirements must carefully be examined to ensure that any interruptions caused by other events will not corrupt data. Interrupts may need to be disabled during certain critical sections. If an unused timer module is available, its output compare or input capture methods can perform precisely timed operations and allow the program to get to them when it has time.
Code example
As the VEX Cortex lacks an accessible SPI port, SPI can be easily implemented by bit-banging. A trivial example is shown below; better performance can be achieved with a kernel-mode driver.
Teams Contributed to this Article:
BLRS (Purdue SIGBots)
Last updated