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, 7 months ago.
What happens when I deattach serial function?
To clear the serial buffer I have the choice of
include the mbed library with this snippet
void flushSerialBuffer() { COM.attach(NULL); while (COM.readable()) { COM.getc(); } COM.attach(&newCommand); //reattach ISR routine }
or
include the mbed library with this snippet
void flushSerialBuffer() { while (COM.readable()) { COM.getc(); } }
where COM is my RawSerial.
Which one works correctly? I want to clear current characters waiting in the buffer, but if I don't disable ISR, wouldn't more characters be cleared then there should be? On the other hand, will disabling ISR make the characters arriving in the serial buffer gone if there is no ISR attached when they arrive, or will they wait? Will all characters arriving in serial wait indefinitely for com.getc() to collect them, or is there a limit?
Working on LPC1768.
1 Answer
5 years, 7 months ago.
Hello L B,
- Once started, the execution of an ISR is not interrupted. It means that no other ISR (of whatever priority) is triggered, or the same ISR re-triggered, until the ISR is finished and the program execution returned from the ISR. Hence, detaching and then re-attaching an ISR within the same ISR doesn't have any effect and both snippets above are effectively the same. Apart the first one takes a little bit more time to execute so the second one shall be preferred. Because the CPU cannot do anything else (for example handle other interrupts occurring while this ISR is being executed) we should keep the ISR as short as possible. One solution is to let the ISR only set a flag and defer handling into another thread as outlined here. However, in that case the ISR has to be detached in order to prevent re-triggering on new serial data arrival because the thread is executed after the ISR had been already finished.
- Serial bytes are received and loaded into the serial register or serial buffer by hardware called universal synchronous asynchronous receiver transmitter (USART). If your program doesn't read them out quickly until new bytes arrive the USART won't wait. They will be overwritten (hence lost) by new bytes. So a receive buffer is advantageous over a single byte receive register because it provides more time to read the data received.
"Hence, detaching and then re-attaching an ISR within the same ISR doesn't have any effect and both snippets above are effectively the same." The flushSerialBuffer() function is called from main(). main() first runs the ISR routine which executes the function newCommand(). I'm not deattaching the ISR within an ISR, im doing it within a function.
posted by 31 May 2019Not fully understand. Could you please show us the related main()
and ISR
code?
"main() first runs the ISR routine which executes the function newCommand()."
Is the ISR attached to handle serial interrupts but then also periodically called in the main()
loop?
Im trying to say that I don't deattach and reattach an ISR within an ISR. flush is just a function which runs once being called from main(). So in main, there is com.attach(&newCommand), and then further down the main thread will call flush function depending on some sort of trigger. Flush itself isn't an ISR.
posted by 06 Jun 2019