STM32 PWM only works correctly when I leave in two unused variables: StartUpCounter and HSEStatus


I'm using the stm32f3discovery board to try and generate the signal for a ws2812 light strip.

I've got src/system_stm32f30x.c, which I copied from the StdPeriph examples. I'm currently using the internal HSI, and the clock speed is 48MHz.

I'm using gcc-arm-none-eabi-4_8-2014q3 to compile.

If I leave src/system_stm32f30x.c as is, I get compiler warnings about unused variables, HSEStatus, and StartUpCounter, but my program works correctly. If I rename the variables to test1 and test2, it still works as expected, so it appears that it doesn't matter what the variables are named, it just matters that they are taking up memory (I guess).

However, if I comment these variables in order to get rid of the warning, my program no longer works correctly. The first pulse of the PWM signal is either grossly too short, or way to long. It causes the first LED on my strip to flash erratically (though the rest of the LEDs are fine).

My code is here:

What could it be?

Best Answer

Is PLL_SOURCE_HSI #defined in your code?

In system_stm32f30x.c, it looks like HSEStatus, and StartUpCounter should never be defined IFF your code is using the HSI oscillator.

Their definition is in the code:

#if defined (PLL_SOURCE_HSI)
#if defined (PLL_SOURCE_HSE)
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;

So, it appears that something is undefine-ing PLL_SOURCE_HSI, and hence allowing StartUpCounter and HSEStatus to be defined.

I can't see anything in the source file src/system_stm32f30x.c to cause that.

So either:

  1. the src/system_stm32f30x.c file at github is not the one being used in your build, or
  2. your build is somehow causing PLL_SOURCE_HSI to become undefined and PLL_SOURCE_HSE to be defined (which seems unlikely to get both), or
  3. the include file stm32f30x.h contains a syntax error which is causing some of this.

1 and 3 seem more likely.

Those two variables are local to SetSysClock. So their names can be safely changed.

Commenting out their definition should cause the compile of that source file to fail.

If the build is producing a program, then src/system_stm32f30x.c is not part of the program, or an old object is being used, or their is an inconsistency in the question.

If the HSI oscillator is being used in one case, and not in another, it is plausible that the PWM period is inconsistent.