STM32 F4 Discovery: Problems with PWM

Posted on
  • Hello,
    I have some problems with the following Code from this example on the STM32 F4 Discovery board:

    function setServo(pin,pos) {
     if (pos<0) pos=0;
     if (pos>1) pos=1;
     analogWrite(pin, (1+pos) / 50.0, {freq:20});
    }
    
    setServo(<PIN>, 0.5);
    

    On some pins (I've tested C9, A8, B13, E13, E14) I get half the frequency I have selected (measured 10 hz with DSO). On these pins (C7, C6, C8, B14) I get the expected results.
    Wrong initialized timer?

  • Interesting... :)

    I guess some timers might work from a different clock domain, as I think APB2 runs at half the speed of APB1. Maybe you could file a bug at: http://www.github.com/espruino/Espruino/­issues

    I don't have time to fix this now I'm afraid, but the problem code will be around here: https://github.com/espruino/Espruino/blo­b/master/targets/stm32/jshardware.c#L140­0

  • Hello Gordon,

    I don't have time to fix this now I'm afraid, but the problem code will be around here: github.com/espruino/Espruino/blob/master­/targets/stm32/jshardware.c#L1400

    OK. It's time to dive deeper into the code :-)
    When I have time this week I will try to find out whats going wrong here.
    If I get stucked I will file a bug :-)

    Thanks.

  • Thanks - that'd be awesome!

  • Hi,

    Just to say I noticed another problem (caused by modifying the APB clocks to fix USB), and in the process of fixing that I believe I've just fixed this. It turns out that different timers run off of different clock sources (APB1 or APB2) and each is a different speed.

    Maybe you could try the next nightly (I only just committed the change) and see if that fixes the problem?

    thanks!

  • Hi Gordon,
    ok, tomorrow I will try the nightly.
    Today I've collected all the datasheets and manuals for the STM32F4 that are needed to understand the structure of the peripheral bus, clock sources, prescalers and timers. The information is spread about several documents.
    The clock system seems to be very powerful - and dangerous :-)

  • Yes, it's amazingly complicated for something so small!

    You will probably find that the stm32f4 'reference' is the best place for information. That's what I end up using.

    Annoyingly what you need is spread all over it - and then you have to guess what ST's API is doing so you can call that in the right way to do what you want!

  • Maybe you could try the next nightly (I only just committed the change) and see if that fixes the problem?

    Yes, the problem is fixed. I've tested C9, A8, B13, E13, E14 and they all provide the selected frequency of 20 Hz.

    Many thanks!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

STM32 F4 Discovery: Problems with PWM

Posted by Avatar for tickTock @tickTock

Actions