USART woes on the STM32F4DISCOVERY board

Now that I have the ability to use a FAT filesystem on an SD card (post to come shortly), my next task was to write some code to allow me to send debug spew to a serial port. My plan is to use an Adafruit Bluefruit EZ-Link bluetooth link to send debug data to my laptop computer while debugging my AVC code. So yesterday I sat down to bang out the code. I mean, how hard can it be, right? Configure a USART with the proper baud rate, parity, data and stop bits, and pump data into the TX register. Piece of cake, right?

In about a half hour I had some code built and running. But I was getting garbage on my laptop over the FT232 cable I was testing with. I figured maybe I'd miscalculated the baud rate, or the clock speed, or something, so I tried doubling the bitrate on the microcontroller. Same problem, so I tried halving the original bitrate. No good.

I whipped out my Saleae Logic logic analyzer and took a gander at what was happening on the wire. The data simply made no sense. The width of each bit was not only not what I calculated it should be, but successive bit times weren't even an integer multiple of each other! The bit time was all over the map.. some short, some long, and very, very few were what they were supposed to be.

I banged my head against this problem for a few hours before, in a fit of desperation, I switched from USART1 to USART2 as a sanity check.

Whammo. I had a glorious set of "This is a test" messages appearing in my terminal window on the laptop, over and over and over again while the microcontroller spun in a loop sending that message across the wire.

I happened to get to this point just before I had to pack up and go out for the night, but I was in a great frame of mind during my date with Supermodel Wife knowing that my code was not the issue here.

This morning I dug into the problem a little deeper and found my answer in an online forum post and verified it in the STM32F4DISCOVERY schematic. Pin PA9, which is where USART1_TX appears, has a 4.7uF capacitor to ground as part of the USB circuitry on this board. There's no way I could have sustained a fast bitrate with that capacitor there, and even 4800 baud was too fast to overcome the buffering from this capacitor.

So, kids, remember to check the schematic and ensure that the pins you want to use aren't used by something else!

I currently have the ability to read and write to files on an SD card, and send data to a debug serial port. I also have the ability to send servo control signals to the steering servo and the Electronic Speed Control (ESC) for throttle control. I have also verified that my laptop can receive data from the GPS over the FT232 link. My next steps are to 1) pair the Bluefruit EZ-Link with my laptop, which doesn't seem to want anything to do with any other bluetooth device, and 2) start on code to parse NEMA sentences from the GPS. I also need to finish up a blog post on accessing FAT filesystems on the SD card. As soon as I get replacement parts for my R/C car from the other side of the world, I'll fix the car and start grafting the car to my controller board. Stay tuned!