Firmware Tips & Techniques

From diychristmas.org wiki
Jump to navigation Jump to search

Overview

Light controllers are essentially small computers that have a dedicated purpose. They don't have features for a keyboard or mouse, they don't have an electronic subsystem for hi-res video displays, hard disk drives or networking, but they have the ability to turn things "on" and "off," sometimes very quickly -- hundreds and thousands of times per second. To do this, the "chip" or "chips" that are inside a light controller use "firmware." Firmware for one of these chips is the equivalent of "software" for your PC. Except while you can generally control what software does on your computer because you're sitting in front of it, firmware on a chip generally runs unattended and only reacts to whatever data is sent to it.

Chips

From the factory, these chips are blank and do not contain the firmware control to make lights blink or fade up/down. It is up to the user to put the necessary firmware ONTO the chip. The process for putting firmware on a chip is called "flashing the chip." Once the chip has been flashed with firmware, it retains that firmware whether the chip is powered on or not. If you remove the chip from the controller, the firmware remains stored on the chip, and it remains there unless you either erase it or replace it with different firmware. Most chips can be reflashed with different firmware many tens or hundreds of thousands of times, so you don't have to worry about wearing it out. But it's important to know that the firmware for one type of chip may not work on a different kind of chip. This is because of the internal electrical design of the chips themselves -- it's not a one-size-fits-all kind of thing. Some chips have only 8 pins or "legs" while others may have 14, 16, 18, 24, or 40 pins, and each chip's internal structure is different, so you must use the firmware designed for the chip you're using in your controller or the firmware likely won't work. For example, if you drive a Corvette, you use gasoline fuel. You can't put diesel fuel in the Corvette and expect it to run. Firmware is like that. You need the right firmware for each kind of chip.

Firmware

Firmware comes in two basic formats: source code and compiled code. The source code is the human-readable format that is written/modified using common text editor tools. The source code is usually modified by the user to tweak it to his/her needs. For example, you might want the controller to use a specific communication speed so your computer can talk to it. The communication speed (i.e. baud rate) is a number that you would change in the source code. Then you would use a piece of software on your computer to load the source code and "compile" it into compiled format. While the source code is the stuff humans understand, the compiled code is the machine code that the chip understands; the "compiler" transforms the source code into machine code. Compiled, machine code is usually a "hex" file because it typically has "hex" at the end of the file name, such as renard_dmx.hex. Source code is often referred to as "asm" because the file name may have "asm" at the end of the file name, such as renard_dmx.asm. If you're curious, here's what source code and compiled code looks like:

Code examples.png

In the case of the source code (above, left) notice the text formatting. Source code lines must be kept intact when editing -- they cannot be combined together like the following example. This sometimes happens if you edit and save the code with a text editor that is set to use "word wrapping." The compiler can't understand jumbled code like this and subsequently won't compile it.

Wordwrap code.png

The Compiler

The compiler is not generic for all chips. It is provided by the chip manufacturer for that manufacturer's chips and is usually free. This is the case with Microchip's products although some others may require purchase. Microchip's compiler will not work with chips manufactured by Atmel -- you need Atmel's compiler for them. The Propeller chip is manufactured by Parallax, so for their chips, you need whatever compiler Parallax recommends. See how this works? If you use all different kinds of controllers, you may need many different kinds of software compilers installed on your computer. Sounds complicated but it's really pretty logical.

Because of the many differences in compiler display screens and options they are not shown here. There are ample video tutorials available on the Internet and usually from the manufacturer's web site as well and users are encouraged to seek them out. Suffice to say that the following outline is the general sequence used by most compiler software:

  • Start the compiler software.
  • Set the compiler's options to use a specific programming device that's approved for use by the compiler software. Your device may or may not be on the list. If it is a compatible device, you may have to try different options.
  • Set the compiler's options for the specific chip to be used.
  • Load the desired source code file into the compiler's editor.
  • Make the desired changes to the source code.
  • Save the revised source code.
  • Select to "compile" the source code. The output format is normally a ".hex" file.
  • Select the option to "flash" or "write" the compiled firmware to the chip using the programming device.
Note that sometimes one or more of the above steps may be combined together into a single step.