Introduction to Bit Banging

Build an Interrupt-driven Software Serial for STM32F3 MCU

Looi Kian Seong
5 min readAug 28, 2020

In embedded system, microcontroller might need to interface with other devices such as sensors, Bluetooth module and even PC. Most of the time, those devices cannot be directly interfaced via GPIO, but need specific communication protocol. However, not all microcontrollers have the dedicated communication interface and even if they do, there might only be a limited number of it. So, one of the solution to this problem is by emulating the communication interface in software by using bit banging technique. According to Wikipedia, bit banging is a method of data transmission by using software instead of the dedicated hardware to generate the transmitted signals and process the received signals through GPIO pins. The software is responsible for timing, synchronisation, setting the state of output pin (transmit) and sampling the input pin (receive).

In this post, software serial communication will be demonstrated and implemented on STM32F3 microcontroller. The most common serial communication interface which is the Universal Asynchronous Receiver/Transmitter (UART) communication will be emulated in software. Before diving into the implementation of software UART communication, let’s check on the fundamental of UART communication.

Fundamental of UART

Figure 1: Data frame in UART communication.
  • Start bit is the synchronisation bit that marks the beginning of data frame. During idle state, the data line is held high (1). The UART transmitter pulls the data line from high (1) to low (0) to start the transmission. When the UART receiver detects the falling edge (start bit), it will start to sample the data from the data line according to the agreed baud rate.
  • Data bits (5 - 9 bits) are the serialised data transmitted in UART communication. The data are transmitted from LSB to MSB. Note that 9-bit data can only be used if there is no parity bit.
  • Parity bit is an optional bit which is used for error detecting. There are two types of parity bit namely even parity and odd parity. For even parity, if the occurrence of 1's in data bits is odd number, the parity bit is set to 1 to make the total count of 1's in data bits and parity bit even. In contrast, if the occurrence of 1’s in data bits is even, the parity bit is set to 0 so that the total count of 1’s in data bits and parity bit is even. The same logic works for odd parity where the parity bit will be set to 1 when occurrence of 1’s in data bit is even and set to 0 when it is odd.
  • Stop bit marks the end of the transmission of data. The data line is held high (1) at the end of transmission.

Since UART is asynchronous, thus there must be an agreed data transmission rate between the communicating devices such as 9600, 19200 and 115200. The data bits transmitted per second is known as baud rate. For baud rate of 9600, 1/9600 second is used to transmit one bit of data.

Software UART

Let’s get our hand dirty in implementing software UART after understanding the basics of UART communication.

The target microcontroller is STM32F303VC. The peripherals used are general-purpose input/output (GPIO) and general-purpose timer. Two GPIO pins are used as transmitter (tx) and receiver (rx) respectively. The timer is used for maintaining the dedicated baud rate for communication. It is also configured to enable input capture on the rx pin and output compare on the tx pin.

Timer configuration:

Timer 3 which is an 16-bit general-purpose timer is used. There are 4 independent channels for input capture, output compare, PWM generation and one-pulse mode output. In the implementation of software UART, 3 channels will be used:

  • Transmitter — one output compare

The output compare for transmitter will alter the corresponding GPIO pin periodically according to the agreed baud rate. It will also trigger an interrupt to configure the next output state to be set on the pin.

  • Receiver — one input capture (on falling edge) and one output compare

The input capture will trigger an interrupt on detecting a falling edge (start bit) and the timer count will be captured at the same time. The receiver output compare timer interrupt (for data reception) will be enabled in software once the falling edge is detected. The captured timer count will be used as the starting time for the data reception interrupt with the frequency corresponded to the baud rate.

GPIO configuration:

Pin 6 and pin 7 of GPIO port C will be configured as output and input respectively. Pin 6 and pin 7 will also be configured with alternate function for output compare and input capture respectively.

Algorithm of Software UART Transmitter:

Figure 2: Flow chart for data transmission.

Algorithm of Software UART Receiver:

Figure 3: Flow chart for data reception.

The complete implementation can be found in my GitHub.

Conclusion

In conclusion, serial communication can be emulated in software when the dedicated peripheral is not available in the microcontroller. However, extra care must be taken during coding for the software serial to minimise software overhead. In this implementation, timer input capture is utilised for falling edge (start bit) detection so that superfluous checking for start bit can be avoided. In addition, the software UART can be improved by using direct memory access (DMA) transfer (not implemented yet) which will further minimise the software overhead and CPU load.

Thank you for reading and hope that you learnt the concept of bit-banging and able to create another software serial beside software UART after reading this post.

Feel free to comment if you find any part is confusing and I will try my best to help.

References

  1. “ Bit-banging ” retrieved from https://en.wikipedia.org/wiki/Bit_banging
  2. “ UART Communication : Block Diagram and Its Applications ” retrieved from https://www.elprocus.com/basics-of-uart-communication-block-diagram-applications/
  3. Application note, AN4655 — “ Virtually increasing the number of serial communication peripherals in STM32 applications ”

--

--