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.
5 years, 1 month ago.
Serial.Attach - leads to hang of the board and other strange effects
Hello EveryOne!
I'm trying to learn how to read from Serial for further parsing but it seems that there is something work. I use Nucleo STM32F103RB board and Mbed Studio (mbed 5):
#include "mbed.h" int a=0; Serial pc(USBTX, USBRX); // tx, rx void communicationHappen(){ a++; } int main() { pc.baud(115200); pc.attach(&communicationHappen,Serial::RxIrq); pc.printf("Hello World\r\n"); while (true) { HAL_Delay(1000); pc.printf("...%u\r\n",a); } }
After pressing any key in terminal I receive not responded board. No errors, nothing.
If I use this code in a little bit more complex environment then I receive stranger errors like ISR que overflow and similar. So that is why I tried to use some simpler code. And it seems that something is missing?
1 Answer
5 years, 1 month ago.
Hi there,
From my point of view, your code what you wrote, stay locked because interrupt flag was set and not cleaned because you not call a read function (getc, scanf...) of the buffer where the flag is normally cleaned and instead of that you use "a++".
However when you use correct function (getc, scanf...) for reading the buffer, that will probably cause fatal error about mutex because that functions contains mutexes and it can not be used in the interrupt context.
So you need take that out of interrupt context, but I not found a correct way because that always caused the overflow error.
Your choices are:
- When you not need MbedOs5 with RTOS, you can use bare metal profile = no RTOS = no Mutex
- If you want use RTOS and interrupts then use RawSerial, there are not mutexes.
- Else, use standart polling method.
Import programSerial_ex_2
Additional Serial Example
Maybe I misunderstood something but that is my experiences.
Best regards, Jan
I believe Jan is right about the missing call to getc() causing program to get stuck in a loop. Last time I checked there is a class called RawSerial that can be used the same way as Serial, except there are no mutexes so you may call getc() inside an ISR. I have used RawSerial successfully with ISR.
posted by 08 Oct 2019