2 years, 9 months ago.

conflicts between serial ports & Interrupt (or Thread)

  1. include "mbed.h"
  2. include "rtos.h"

the code is designed to initial a thread which blink with delay. And the button will change the delay time. It works well without the serial ports.But once I add the serial Ports and print "helloworld" even in the main function, the interrupt just doesnt work, once i press the button the light just stop blinking and remain the state it was,either on or off.

So my speculate is that the Interrupt just break the thread?

the code is listed below

example for serial ports conflicts

#include "mbed.h"
#include "rtos.h"

InterruptIn mybutton(USER_BUTTON);
DigitalOut led1(LED1);
Serial pc(USBTX,USBRX);    
float delay = 1.0; // 1 sec
void pressed()
    if (delay == 1.0)
        delay = 0.2; // 200 ms
        delay = 1.0; // 1 sec

void blink_thread(void const *args){
    led1 =! led1;
int main()
    Thread thread(blink_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
    while (1){}

Question relating to:

Affordable and flexible platform to ease prototyping using a STM32F401RET6 microcontroller.

I have a similar issue on the NUCLEO-F429ZI, I have a ticker interrupt controlling a DAC which worked great until I added a serial port to the code and then the DAC output is just a square wave rather than the audio waveform.

Any fix would be greatly appreciated as my project deadline is Sunday :((((

posted by Harry Willis 09 May 2017

Can you please post the code with proper formatting with <<code>> tags. You can see how to do this with "editing tips" when editing your post.

posted by Sarah Marsh 09 May 2017

You could try putting a wait call inside the main loop, say Thread::wait(1); In os 5, main is a thread, it's probably better to specifically release it to the scheduler.

posted by Graham S. 13 May 2017

2 Answers

2 years, 8 months ago.

I just tested on NUCLEO_F446RE and I cannot reproduce the problem - the application works fine - printf is OK and pressing the button changes the rate of LED blinking. Will test as well on NUCLEO_F401RE soon.

Note: I had to modify the test because DEFAULT_STACK_SIZE was not defined, so I put it to 768.

Tested as well with NUCLEO_F401RE and NUCLEO_F411RE - the test just works fine. So I'd guess that something has been fixed recently in mbed RTX. Would be great to test again and confirm

posted by Laurent Meunier 23 Jun 2017
2 years, 8 months ago.

Don't use wait(x) but use a ticker instead.


When you call wait your board’s CPU will be busy in a loop waiting for the required time to pass. Using the mbed RTOS you can make a call to Thread::wait instead. The OS scheduler will put the current thread in waiting state, allowing another thread to execute.

  1. include "mbed.h"

DigitalOut led1(LED1); DigitalOut led2(LED2); Thread thread;

void led2_thread() { while (true) { led2 = !led2; wait(1); } }

int main() { thread.start(led2_thread);

while (true) { led1 = !led1; wait(0.5); } }