How to use mbed OS2 on platformio

Hi all, I have been playing with my new stm32 Nano, but if you look down that page on the right you will see it is only supported for mbed OS2. I’ve been having some problems getting things to run on it, particularly timers, pwm, etc. timeout particularly seems to give some odd results. I’m wondering if this is caused by PIO using mbed 5 instead of mbed 2 for this board? Is there some way I can force it to use mbed OS2?
Thanks,
Ian

mbed-os 5 is backwards compatible with mbed-os 2 in the sense that every mbed-os 2 program also compiles under mbed-os 5. (see Release mbed-os-5.1.0 · ARMmbed/mbed-os · GitHub, Does mbed-os-5 and mbed-os-2 use the same mbed.h? - Question | Mbed).

With regards to the problem: What exact code and platformio.ini are you using, what is the expected result, what is the actual result?

Hi again Max, and thanks for the reply.

I’m trying to use the mbed timeout function to make my own pwm sort of function, but I want every pulse width to be different. I’m still playing with generating a PPM output stream (the same project I’ve been working on for several lifetimes it seems…) for my home brew R/C transmitter. I’ve got it working pretty well on an Arduino Nano, but I wanted to see if I could do it on an stm32 based board as well, and the stm32 Nano seemed like the perfect choice as it is size and pin compatible with the Arduino Nano. And I was hoping I wouldn’t have to dig into the registers as I had to with the Arduino.

My problem was that, regardless of what time I put in the timeout command, it always generated a pulse with roughly the same pulse width, about 35ms. I had a few printf commands through the code (not in the ISR) to try to see what was happening, and they all showed that what I was expecting to happen was happening, and that the value I was passing to the timeout command was correct. (I was using a variable to pass the time value).

In my searching I found a post that mentioned the the stm32 Nano only supports mbed OS2, not OS5, hence this post, thinking that, if it was trying to us OS5 that might cause some memory problems or timing problems or something. Then I basically gave up and deleted all the printf commands with the idea of posting the code on the mbed forum to see if anybody could tell me what I was doing wrong. Reran the program out of idle curiosity and the thing worked! It seems that, somehow, the printf statements were interfering with the timeout function. Or I had somehow changed something without realising. Now, of course, I can’t reproduce the problem. Tried putting printf commands back in the program, but everything continues to work perfectly. Go figure.

So it seems PIO is doing the right thing. The program size is about 33k, and in my reading I learned that even a simple blinky with mbed OS5 takes at least 35k to accommodate the rtos and threading.

Here’s the program

#include "mbed.h"
volatile timestamp_t pulseWidth = 1000;

long int channel[9] = {1000, 1100, 1200, 1300, 1400, 1500, 1600, 2000, 11400};
int ch = 0;
volatile int swPulse = 286; // approximates to 300us pulse width
volatile bool swFlip = true;
const float fudgeFactor = 0.95; // correction factor to "fix" output as 
// actual value gives pulsewidth about 5% too big

Timeout flipper;
DigitalOut PPMpin(D2);

void flip() {
    PPMpin = !PPMpin;
    if (PPMpin == 1) {
        ch = ++ch % 9;
        pulseWidth = (fudgeFactor * channel[ch]) - swPulse;
    } else {
        pulseWidth = swPulse;
    }
    flipper.attach_us(&flip, pulseWidth);
}
int main() {
    flipper.attach_us(&flip, 1500); // initial attach to kick off the process
    while(true) {
    }
}

and here’s the platformio.ini

[env:nucleo_f303k8]
platform = ststm32
board = nucleo_f303k8
framework = mbed
upload_protocol = stlink
debug_tool = stlink
build_flags = -Wl,-u,_printf_float,-u,_scanf_float