edward0429 / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

Way to check if Serial.write() will block or way to have it fail instead of block. #672

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Use Arduino RC1.
2. Download the code attached, and upload it to an Arduino board. (Uno or 
older, it doesn't matter)
3. Trigger the interrupt by grounding pin 2, sometimes this takes a few tries.

What is the expected output? What do you see instead?

The interrupt should trigger, but the serial shouldn't print out until the ISR 
is done under the new scheme of non-blocking serial writes. This happens most 
of the time.

However, sometimes, when the ISR is triggered, the serial still starts printing 
a few characters, doesn't finish, and hangs the processor. Even when the pin 2 
grounding is removed, and the ISR should finish, it stays stuck there, and I 
have to use the hardware reset button to get it to work again. 

The ISR does not hang the processor when the serial print is removed, so I'm 
pretty sure the problem is in there somewhere. This all seems to happen more 
often when the interrupt is rapidly triggered back and forth, though I'm not 
absolutely sure of this. 

What version of the Arduino software are you using? On what operating
system?  Which Arduino board are you using?
I'm using Arduino RC1, downloaded on 10/8/11, on MAC OSX 10.6.8. This problem 
occurs on both the Arduino Uno and the Bare Bones Board clone running the 
non-optiboot Duemilanove firmware.

Original issue reported on code.google.com by hjohnson...@ric.edu on 8 Oct 2011 at 4:59

Attachments:

GoogleCodeExporter commented 8 years ago
Yes, of course.  If the buffer is full when you write, the function will block 
waiting for room.  Since (uart) interrupts are required for characters to be 
removed from the buffer, and all interrupts are disabled during any ISR, you 
are stuck.

This is another example of what I keep saying about buffered output not solving 
the problems that people think it solves.  ("Nyah, nyah... Told you so!")

Original comment by wes...@gmail.com on 8 Oct 2011 at 11:04

GoogleCodeExporter commented 8 years ago
Well, I submitted a pull request for a potential fix on GitHub, namely a 
function called Serial.waitForBufferSpace(0); that the user can call that 
disables that waiting inside of Serial.write, and simply makes it returns 0 
when this condition occurs. Problem solved, at least for me.

Original comment by hjohnson...@ric.edu on 9 Oct 2011 at 12:37

GoogleCodeExporter commented 8 years ago

Original comment by dmel...@gmail.com on 10 Oct 2011 at 4:45

GoogleCodeExporter commented 8 years ago
Follow up here:

https://github.com/arduino/Arduino/issues/672

Original comment by c.mag...@arduino.cc on 28 Jan 2014 at 5:02