Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 6 months ago.
conflicts between serial ports & Interrupt (or Thread)
- include "mbed.h"
- 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 else delay = 1.0; // 1 sec } void blink_thread(void const *args){ while(1){ led1 =! led1; wait(delay); } } int main() { pc.baud(115200); printf("helloworldhelloworldhelloworld\n"); mybutton.fall(&pressed); Thread thread(blink_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); while (1){} }
Question relating to:
2 Answers
7 years, 4 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.
7 years, 4 months ago.
Don't use wait(x) but use a ticker instead.
or:
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.
- 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); } }
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 2017Can you please post the code with proper formatting with
posted by Sarah Marsh 09 May 2017<<code>>
tags. You can see how to do this with "editing tips" when editing your post.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