Close

Recap #5: Abusing the debug port

A project log for BradWii on the Hubsan Q4

In which the author runs open source firmware on a palm sized quadcopter

ajlittajlitt 03/16/2015 at 03:500 Comments

So it turns out BradWii can do autotuning of the PID coefficients. It pitches and rolls +/- 15 degrees, systematically varying each coefficient until it converges. Once the tuning is done, the new coefficients can be saved to EEPROM. Autotune is started, stopped, and parameters saved using an extra TX channel so that it can be started when the quad is hovering in an open space. The consensus is that the BradWii autotuning is very effective as long as the initial PIDs are good enough to get into a hover.

I planned to use autotuning to find workable PID coefficents and pull them out of the EEPROM with the debugger. I bought a Hubsan X4 for its remote (now I'm in deep...) which is compatible with the Q4 but has a couple of pushbutton channels, one of which I'd use to control the autotuning. But not long after I started playing with autotune I realized that the battery voltage ADC wasn't tripping the low battery detection at all. I needed to know what the ADC was reading, and while it could be done with memory inspection it would be easier if I had a stdout to printf to...

Fortunately ARM specifies a standard for "semihosting", or sending arbitrary data between the debug host and target applications via the debug port. The standard specifies fairly straightforward stdio and file operations that the target application can ask the debug host to perform. One of these prints a null-terminated string to the debug console. This was exactly what I needed.

I added some inline assembly functions to the serial driver so I could later pipe the UART configuration protocol over SWD. But for now printfs to the screen would be enough. I also had to make the breakpoint ISR check that the breakpoint was for semihosting so it could continue execution if the debugger wasn't attached.

Semihosting works ok in practice if you know the limitations. Or limitation. Semihosting is incredibly slow. It's transferring a 1k file over a 300 baud modem slow. But it's enough to print the autotuned PID parameters from EEPROM on power-up and to print the ADC raw value for debug.

This was the last piece in making BradWii autotuning work and find the right mapping of battery voltage to ADC counts.

Discussions