From b818a94202f992aa0404cb9d4a63ae2c3d8c69c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Sun, 4 Jan 2026 10:55:43 +0100 Subject: [PATCH] SERIAL: Wrong use of TI-flag causes IRQ-storm. TI-interrupt flag is used in the code, this is only fine when SERIAL-interrupt is not enabled/used but when is it used, it generates an interrupt which is not handled, so it keeps generating interrupts and trashing the performance and caused starvation on lower priority interrupts like TIMER 2. Use an extra flag to signal to the code that the TX-buffer is empty. --- rtlplayground.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/rtlplayground.c b/rtlplayground.c index 91bd7f8..53c9cd7 100644 --- a/rtlplayground.c +++ b/rtlplayground.c @@ -121,6 +121,7 @@ __xdata char sfp_module_vendor[2][17]; __xdata char sfp_module_model[2][17]; __xdata char sfp_module_serial[2][17]; __xdata uint8_t sfp_options[2]; +__sbit tx_buf_empty = 1; #define ETHERTYPE_OFFSET (12 + VLAN_TAG_SIZE + RTL_TAG_SIZE) @@ -145,9 +146,13 @@ void isr_timer2(void) __interrupt(5) void isr_serial(void) __interrupt(4) { if (RI == 1) { + RI = 0; sbuf[sbuf_ptr] = SBUF; sbuf_ptr = (sbuf_ptr + 1) & (SBUF_SIZE - 1); - RI = 0; + } + if (TI == 1) { + TI = 0; + tx_buf_empty = 1; } } @@ -155,14 +160,14 @@ void isr_serial(void) __interrupt(4) void write_char(char c) { do { - } while (TI == 0); - TI = 0; + } while (tx_buf_empty == 0); if (c =='\n') { + tx_buf_empty = 0; SBUF = '\r'; do { - } while (TI == 0); - TI = 0; + } while (tx_buf_empty == 0); } + tx_buf_empty = 0; SBUF = c; } @@ -1705,8 +1710,8 @@ void setup_serial_timer1(void) TCON |= 0x40; // Start timer 1 ET1 = 0; // Timer1 Interrupt is NOT wanted! - TI = 1; - RI = 0; + TI = 0; // Clear TI-interrupt flag + RI = 0; // Clear RI-interrupt flag ES = 1; // Enable serial IRQ }