In life, you need two examples: good one, and bad one. Here’s one of my bad design of interrupt handling.
The purpose of the firmware was to read a line (CR/LF terminated characters) from serial port, then parse it. Actually, I tried to port my simple AT handler from Java into C (AVR-GCC). It works, and as you can see, this program runs okay under x86 PC, but not under AVR with 8Mhz xtal.
Here’s the actual problem:
SerialLineReader_add(*data++);
if (SerialLineReader_isReady()) {
printf(“Line: %sn”, SerialLineReader_readline());
SerialLineReader_setReady(0×00);
}
The source code for x86 PC is trying to emulate AVR’s UART receive interrupt handler.
ISR(USART0_RX_vect)
{
uint8_t data = UDR0;
SerialLineReader_add(data);
if (SerialLineReader_isReady()) {
SerialHelper_puts(SerialLineReader_readline());
SerialLineReader_setReady(0×00);
}
}
Testing it under PC, no problem has found. Under AVR, the problem will show up if you pump a lot of newline eventhough the data is below the READ_BUFFER_SIZE. The problem even worse if the newline is very short (2-10 characters). You will lose a lot of newline.
Well, I guess I have to redesign my work. Btw, here’s the link of my bad example.