9 years, 6 months ago.

PWM with MbedRTOS on K64F

Im having some trouble with PWM on the K64F. The problem seems specific to using MBED RTOS in combination to PWM. Here is my program flow (cant post actual code because of corporate rules):

my code

Create PwmOut object
Loop over:
    pwm.write(dutyCycle)
    dutyCycle += 0.1
    if dutyCycle > 0.9
        dutyCycle = 0.1
repeat loop

When i single step through this loop it successfully will increase the duty cycle up to 0.9 and repeat. When I let this run without single stepping the program will restart over and over and over. This was proven by putting printf's everywhere. Without debugging it would set the duty cycle to 0.1 then restart shortly after (usually in the next instruction). I copied this exact program to the online compiler and compiled/ran and it worked fine. Granted this was without mbedrtos compiled in but that shouldn't matter since I'm not calling any rtos API's. I'm using the GCC ARM toolchain. The only interaction i can think of is the interrupt contention between PWM and MbedRTOS. Has anyone else had problems with PWM and MbedRTOS or PWM alone on the K64F.

EDIT: When I compile (locally) the example program (except change the pin) for PwmOut it will crash in wait(). Single stepping works fine but running normally will start the pwm pin initially but after trying to change the duty cycle (line 8) and then calling wait() it crashes. Note: The behavior is the same calling wait() vs Thread::wait().

#include "mbed.h"
 
PwmOut led(PTA2);
 
int main() {
    while(1) {
        for(float p = 0.0f; p < 1.0f; p += 0.1f) {
            led = p;
            wait(0.1);
        }
    }
}

If you can't copy paste here, can you create a test program ? We can look at it then.

Restart over? Like MCU restarts or PWM restarts?

posted by Martin Kojtal 14 Oct 2014

Can you first of all verify if it is or is not dependent on RTOS being there? And in the case of RTOS it can matter if it is compiled in, even if you don't call any of its APIs.

posted by Erik - 14 Oct 2014

When I simply run just the sample using the online compiler (without the RTOS) it works fine. When the RTOS is compiled in it doesn't work. So it is most likely dependent on the RTOS being compiled into the final image.

posted by James Prestwood 16 Oct 2014
Be the first to answer this question.