Last time I had an actually hard problem…, a mis-aligned stack which caused randomly misbehaving software. This time, I had a more prosaic problem. One that while not necessarily as interesting, was even more time consuming and frustrating.
The serial port wouldn’t work. I had copied the module I used for DMA based receive and transmit from the STM32F4 and it just wasn’t working. Nothing was written and nothing was received. I carefully inspected the software many times. I looked at the registers in the debugger and nothing seemed obviously amiss. I read the datasheet to look for subtle differences in the theory of operation between the STM32F4 and STM32G4 but came up empty.
Eventually, I went completely out of the box and compiled one of the STM provided UART examples from STM32CubeMX. That did work! But how? I once again carefully inspected its source and mine and saw no obvious differences.
Then, I took the ultimate slow path in debugging and binary searched, gradually transforming the STM provided example into my code half by half to figure out which transformation was the ultimate cause. This did do the trick. The final fix:
- huart_.Init.WordLength = 8; - huart_.Init.StopBits = 1; + huart_.Init.WordLength = UART_WORDLENGTH_8B; + huart_.Init.StopBits = UART_STOPBITS_1;
That’s it. In the old version, I passed in to the HAL the word size and number of stop bits as integers. Somehow this actually worked, despite being documented as requiring the necessary #defines even in that version.
Notch another win up to: “it sure would be nice if there was less undefined behavior in C/C++”.