Saturday, May 7, 2016

Naze32 Custom Code: Binking Lights with GCC ARM Embedded

Modern flight controllers for drones and RC aircraft make great prototyping boards. At the time of writing, there are STM32F103, STM32F30x and also STM32F4xx based controllers. They are inexpensive, small, include voltage regulators, nice 0.1" header pins, as well as s series of inertial and barometric sensors.

I wanted to build an altimeter for amateur aviation use. At first I wanted to build it on a custom PCB, but then I realized that a flight controller fits the bill nicely, and will come much smaller and cheaper. Because they are widely available, the project can be replicated easily by more amateurs, compared to a custom PCB project

So I took a Flip32 board I had laying around (an exact clone of Naze32 which is quite popular), and decided to port some custom code on it from another similar STM32F103 project for a different board. Schematics are easily found on the Internet. So I found the pin which the LED is connected: Port PB4. Compiled the code, flashed and verified, but... It did not run. What could be the problem?

I looked around, and considered many possibilities, mostly focusing around clock initialization problems. The STM32 series have quite complex clock trees with many different sources of clock and fallback clocks in case one fails. However, after many trials there was no improvement. What could be the problem? I also looked in the source of Cleanflight. It was written for several processors and is divided into many source files, so it is quite difficult to trace the exact startup sequence for STM32F103.

Eventually, looking over the reference manuals (well over 1100 pages), I found the likely suspect. The STM32 has a powerful JTAG interface for debugging which is enabled by default at power-on.  It uses several pins, one of which, PB4 is NJTRST. It is possible to disable the JTAG functionality during the processor initialization, so that PB4 becomes a useable pin again. If you want to be able to use JTAG later on, it is possible to do that anyway, because it is always active after power on. So diabling it here is not a big deal.

You can download the blink source code here. It sets up the processor and blinks the two on-board LEDs in unison. The code itself is simple enough, just another LED blink program. The interesting part is the first few lines of  initialize_PROC() function, where JTAG pins are returned to their GPIO configuration. Programming an ARM STM32F10x processor is described here.

The result is (well, OK, you cannot see it blink in the still shot but...):

Flip32 with blinking lights program. Wire below is from a LiPo cell stuck to the bottom.





I scratched my head a lot over this, so I hope this will help others.









1 comment:

  1. I am just doing probably the same thing as you did and kept searching for any possible reason causing the two LEDs on Naze32 not working until reading your post. By free up the JTAG occupied GPIO pins(PB3/4), both LEDs now working in expected way. Thanks. Joe

    ReplyDelete