Firmware - The Decoding State Machine: How Software Brings the Barcode Scanner to Life |
Subtitle: A Deep Dive into the Embedded Code, the State Machine Architecture, and the Algorithms That Turn Pulse Widths into Data - with Real-World Examples from Symbol, Zebra, Honeywell, Datalogic, Microchip, and NXP |

|
Opening Summary |
The analogue front-end has done its job. The comparator has produced a clean digital square wave. The timer capture module has measured the pulse widths. Now, the software takes over. The firmware is the intelligence of the barcode scanner. It orchestrates the entire decoding process, from the initial detection of the quiet zone to the final output of the decoded data. At the heart of the firmware is the decoding state machine - a software architecture that guides the decoder through the sequential steps of reading a barcode. |
This article is dedicated to the firmware - the embedded code that runs on the microcontroller. We will explore the state machine architecture, the key states (idle, scanning, decoding, and done), and the transitions between them. We will examine the algorithms for module width estimation, run-length decoding, and character decoding. We will look at how major companies have implemented their firmware. We will examine the firmware of Symbol's (now Zebra's) LS2208, which is a classic example of a well-structured embedded system. We will explore Honeywell's Adaptus firmware, which includes advanced image processing and decoding algorithms. We will examine Datalogic's Auto-Adaptive Decoding firmware, which is optimized for industrial applications. We will also look at reference designs from Microchip, NXP, and STMicroelectronics. |
By the end of this journey, you will understand that the firmware is not just a collection of code but a carefully engineered system that must balance speed, accuracy, and resource constraints. You will see how the state machine architecture provides a clear and robust framework for the decoding process. |

|
Full Article |
Section 1: The Firmware's Mission - To Decode the Barcode |
The firmware's mission is to take the raw pulse widths from the timer capture module and turn them into meaningful data. This is a multi-step process: |
1. Detection: Detect the presence of a barcode (the quiet zone). |
2. Capture: Capture the pulse widths. |
3. Estimate: Estimate the module width. |
4. Normalize: Normalize the pulse widths to module units. |
5. Decode: Decode the characters using the symbology's rules. |
6. Verify: Verify the checksum. |
7. Output: Output the decoded data. |
The firmware must perform these steps quickly and accurately. It must also be robust to variations in print quality, scanning speed, and ambient conditions. |

|
Section 2: The State Machine - The Architectural Framework |
The firmware is typically structured as a state machine. A state machine is a software architecture that has a finite number of states. The machine transitions from one state to another in response to events. The state machine provides a clear and robust framework for the decoding process. |
The states of a barcode decoder are typically: |
Idle: The scanner is waiting for a trigger or a barcode. |
Scanning: The scanner is capturing pulse widths. |
Decoding: The scanner is processing the captured data. |
Done: The scanner has decoded the data or has encountered an error. |

|
Section 3: The Idle State - Waiting for a Trigger |
In the idle state, the scanner is waiting for a trigger. The trigger can be a button press (for a handheld scanner) or the detection of an object (for a presentation scanner). When the trigger is activated, the scanner transitions to the scanning state. |
In the idle state, the scanner is in a low-power mode to conserve battery power. |
Section 4: The Scanning State - Capturing the Pulse Widths |
In the scanning state, the scanner captures the pulse widths. The timer capture module is enabled. The microcontroller's interrupt service routine (ISR) is triggered on each edge of the digitised waveform. The ISR reads the capture value, calculates the pulse width, and stores it in a buffer. |
The scanning state continues until the quiet zone is detected at the end of the barcode. The quiet zone indicates that the barcode has ended. |
Section 5: The Decoding State - Processing the Data |
In the decoding state, the scanner processes the captured data. The pulse widths are normalized to module units. The characters are decoded using the symbology's rules. The checksum is verified. |
If the decoding is successful, the scanner transitions to the done state. If the decoding fails, the scanner may return to the scanning state or the idle state. |

|
Section 6: The Done State - Outputting the Data |
In the done state, the scanner outputs the decoded data. The data is sent to the host via the communication interface (USB, UART, Bluetooth, etc.). The scanner also provides feedback to the user (a good read beep or a green LED). |
After outputting the data, the scanner transitions back to the idle state. |
Section 7: Symbol's LS2208 - The Classic Firmware |
Symbol's LS2208 uses a classic state machine architecture. The firmware is written in C and assembly language. The firmware is optimized for the Motorola HC08 microcontroller's limited resources. The LS2208's firmware is a testament to the effectiveness of the state machine architecture. |
Section 8: Honeywell's Adaptus - The Advanced Firmware |
Honeywell's Adaptus firmware is a more advanced decoding engine. It includes image processing and decoding algorithms for 1D and 2D barcodes. The Adaptus firmware is written in C and is optimized for ARM Cortex-M microcontrollers. |
The Adaptus firmware includes a sophisticated state machine that can handle a wide variety of barcode types and conditions. |

|
Section 9: Datalogic's Auto-Adaptive Decoding - The Industrial Firmware |
Datalogic's Auto-Adaptive Decoding firmware is optimized for industrial applications. It includes algorithms for handling noisy, damaged, and distorted barcodes. The firmware is written in C and is optimized for the custom ASIC used in Datalogic's scanners. |
The Auto-Adaptive Decoding firmware includes a state machine that adapts to the scanning conditions. |
Section 10: The Module Width Estimation - The First Step in Decoding |
The module width estimation is the first step in decoding. The module width is the width of the narrowest bar or space. The decoder estimates the module width from the captured pulse widths. |
The estimation can be done using the shortest pulse method, the histogram method, or the running-average method. |
Section 11: The Shortest Pulse Method - A Simple Approach |
The shortest pulse method finds the shortest pulse in the captured sequence. The shortest pulse is assumed to be one module wide. The module width is set to the duration of the shortest pulse. |
The shortest pulse method is simple and effective. It is used in Symbol's LS2208. |

|
Section 12: The Histogram Method - A Robust Approach |
The histogram method constructs a histogram of the pulse widths. The histogram has peaks at the module width, twice the module width, and three times the module width. The decoder finds the first peak in the histogram, which corresponds to the module width. |
The histogram method is more robust than the shortest pulse method. It is used in Honeywell's Adaptus firmware. |
Section 13: The Running-Average Method - An Adaptive Approach |
The running-average method maintains a running average of the pulse widths. The running average is updated with each new pulse. The module width is estimated from the running average. |
The running-average method is adaptive. It can handle changes in scanning speed. It is used in Datalogic's Auto-Adaptive Decoding firmware. |
Section 14: The Run-Length Decoding - Normalizing the Pulse Widths |
Run-length decoding normalizes the pulse widths to module units. The decoder divides each pulse width by the module width and rounds to the nearest integer. The result is a sequence of element widths in module units. |
The sequence is the input to the symbology decoder. |

|
Section 15: The Symbology Decoder - Interpreting the Pattern |
The symbology decoder interprets the sequence of element widths. The decoder uses a lookup table to map the element patterns to characters. The lookup table is specific to the symbology (Code 39, UPC, Code 128, etc.). |
The symbology decoder is the core of the decoding algorithm. |
Section 16: The Code 39 Decoder - A Simple Example |
The Code 39 decoder groups the elements into 9-element patterns (5 bars and 4 spaces). Each pattern has 3 wide elements and 6 narrow elements. The decoder compares the pattern to the Code 39 lookup table. |
The Code 39 decoder also verifies the modulo 43 checksum, if present. |
Section 17: The UPC Decoder - A More Complex Example |
The UPC decoder groups the elements into 7-element patterns (2 bars and 2 spaces). The decoder uses the L-code and R-code lookup tables. The decoder also verifies the modulo 10 checksum. |

|
The UPC decoder must also handle the left and right guard patterns and the center guard pattern. |
Section 18: The Code 128 Decoder - A High-Density Example |
The Code 128 decoder groups the elements into 6-element patterns (3 bars and 3 spaces). The decoder uses the Code 128 lookup table. The decoder also verifies the modulo 103 checksum. |
The Code 128 decoder is more complex than the Code 39 or UPC decoders. |
Section 19: The Checksum Verification - The Final Check |
The checksum verification is the final check. The decoder calculates the checksum from the decoded data and compares it to the checksum that was encoded. If the checksums match, the data is valid. |
The checksum verification is an essential error-checking mechanism. |
Section 20: The Error Handling - What to Do When Things Go Wrong |
The decoder must handle errors. If the decoding fails, the scanner must report an error. The scanner may also attempt to re-read the barcode. |
The error handling is an important part of the firmware. |

|
Section 21: The Communication - Sending the Data |
The decoded data is sent to the host via the communication interface. The communication interface is typically USB, UART, or Bluetooth. The data is formatted according to the communication protocol. |
The communication is an important part of the firmware. |
Section 22: The User Interface - Providing Feedback |
The firmware controls the user interface. The user interface includes the beeper, the LED indicator, and the trigger. The firmware provides feedback to the user (good read beep, bad read beep, etc.). |
The user interface is an important part of the scanner's operation. |
Section 23: The Power Management - Conserving Battery Power |
The firmware manages the power consumption. The scanner enters a low-power sleep mode when it is not in use. The scanner wakes up when the trigger is pressed. |
The power management is an important part of the firmware for battery-powered scanners. |

|
Section 24: The Firmware Development Process - From Design to Deployment |
The firmware development process includes design, coding, testing, and deployment. The firmware is written in C and assembly language. The firmware is tested on the target hardware. |
The firmware development is an important part of the scanner's development. |
Section 25: The Firmware Debugging - Finding the Bugs |
Firmware debugging is the process of finding and fixing errors in the code. The debugging can be done with a debugger, a logic analyzer, or an oscilloscope. |
The firmware debugging is an important part of the development process. |
Section 26: The Firmware Update - Upgrading the Scanner |
The firmware can be updated in the field. The update is done through the bootloader. The bootloader receives the new firmware and writes it to the flash memory. |
The firmware update is an important feature for maintaining the scanner's performance. |

|
Section 27: The Firmware in Microchip's Reference Design |
Microchip's reference design includes a complete firmware example. The firmware includes a state machine, a module width estimator, a run-length decoder, and decoders for Code 39, UPC, and Code 128. |
Section 28: The Firmware in NXP's Reference Design |
NXP's reference design also includes a complete firmware example. The firmware is optimized for the LPC microcontroller's DMA engine. |
Section 29: The Firmware in STMicroelectronics' Reference Design |
STMicroelectronics' reference design includes a complete firmware example. The firmware is optimized for the STM32 microcontroller's timer capture module. |

|
Section 30: The Firmware and the Real-Time Constraints |
The firmware must meet real-time constraints. The edge capture ISR must execute quickly. The decoding must be completed within a reasonable time. |
The real-time constraints are an important part of the firmware design. |
Section 31: The Firmware and the Memory Constraints |
The firmware must fit within the microcontroller's program and data memory. The firmware must be efficient and compact. |
The memory constraints are an important part of the firmware design. |
Section 32: The Firmware and the Code Quality |
The firmware must be of high quality. The code must be well-structured, well-documented, and well-tested. |
The code quality is an important part of the firmware development. |

|
Section 33: The Firmware and the Security |
The firmware may need to be secure. The firmware should prevent unauthorized access and modification. |
The security is an important consideration for some applications. |
Section 34: The Firmware and the Certification |
The firmware may need to be certified. The certification is required for some applications (e.g., medical devices). |
The certification is an important part of the firmware development. |
Section 35: The Firmware and the Future - Machine Learning |
The future of firmware is machine learning. Machine learning algorithms can improve the decoding performance. The algorithms can be trained to recognize and decode barcodes more accurately. |
Machine learning is an emerging trend in barcode decoding. |

|
Section 36: The Firmware - A Summary of Best Practices |
Based on our exploration, let us summarize the best practices for firmware development for a barcode scanner: |
1. Use a State Machine: A state machine provides a clear and robust framework. |
2. Use Efficient Algorithms: The algorithms must be efficient and robust. |
3. Handle Errors Gracefully: The firmware must handle errors without crashing. |
4. Provide Feedback: The firmware must provide feedback to the user. |
5. Conserve Power: The firmware must manage power consumption. |
6. Test Thoroughly: The firmware must be tested thoroughly. |
7. Document the Code: The code must be well-documented. |

|
Final Summary |
The firmware is the intelligence of the barcode scanner. It orchestrates the entire decoding process, from the initial detection of the quiet zone to the final output of the decoded data. The firmware is typically structured as a state machine, with states for idle, scanning, decoding, and done. |
We have seen how major companies have implemented their firmware. Symbol's LS2208 uses a classic state machine architecture. Honeywell's Adaptus firmware includes advanced image processing and decoding algorithms. Datalogic's Auto-Adaptive Decoding firmware is optimized for industrial applications. Microchip, NXP, and STMicroelectronics provide reference designs with complete firmware examples. |
The firmware is a critical part of the barcode scanner. It must be efficient, robust, and reliable. By following the best practices, you can create firmware that ensures the scanner's performance and reliability. |