Просмотр исходного кода

Use ringbuffer for UART receiving

Johannes Zellner 5 месяцев назад
Родитель
Сommit
95b9cc216c
9 измененных файлов с 108 добавлено и 22 удалено
  1. 6 3
      Makefile
  2. 3 8
      bin_recv.c
  3. 1 0
      kernel.c
  4. 30 6
      serial.c
  5. 5 2
      serial.h
  6. 3 3
      serial_loop.c
  7. 41 0
      serial_ring_buf.c
  8. 12 0
      serial_ring_buf.h
  9. 7 0
      switch.S

+ 6 - 3
Makefile

@@ -62,7 +62,10 @@ run.o: run.c fat.h io.h fbtext.h run_api.h timer.h vga.h
 rtc.o: rtc.c rtc.h
 rtc.o: rtc.c rtc.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-serial.o: serial.c serial.h pci.h
+serial.o: serial.c serial.h serial_ring_buf.h pci.h idle.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+serial_ring_buf.o: serial_ring_buf.c serial_ring_buf.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 serial_loop.o: serial_loop.c serial.h io.h timer.h
 serial_loop.o: serial_loop.c serial.h io.h timer.h
@@ -125,7 +128,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -249,4 +252,4 @@ send_serial: send_serial.c
 	gcc -O2 -o $@ $<
 	gcc -O2 -o $@ $<
 
 
 clean:
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin send_serial
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin send_serial

+ 3 - 8
bin_recv.c

@@ -6,19 +6,14 @@
 #include "serial.h"
 #include "serial.h"
 #include "io.h"
 #include "io.h"
 #include "timer.h"
 #include "timer.h"
+#include "idle.h"
 
 
 #define TIMEOUT_MS 5000
 #define TIMEOUT_MS 5000
 
 
 static int wait_byte(unsigned int port, unsigned char *out, unsigned int timeout_ms)
 static int wait_byte(unsigned int port, unsigned char *out, unsigned int timeout_ms)
 {
 {
-	unsigned int start = get_ticks_ms();
-	while (get_ticks_ms() - start < timeout_ms) {
-		if (serial_has_data(port)) {
-			*out = (unsigned char)serial_getc_nb(port);
-			return 1;
-		}
-	}
-	return 0;
+	(void)port;
+	return serial_ring_wait(timeout_ms, out);
 }
 }
 
 
 int bin_recv(unsigned int port, void *buf, unsigned int max_size)
 int bin_recv(unsigned int port, void *buf, unsigned int max_size)

+ 1 - 0
kernel.c

@@ -129,6 +129,7 @@ void kernel_main(void)
 		*p++ = 0;
 		*p++ = 0;
 
 
 	serial_init(SERIAL_COM1);
 	serial_init(SERIAL_COM1);
+	serial_ring_init();
 	io_init();
 	io_init();
 	idle_init();
 	idle_init();
 	timer_init();
 	timer_init();

+ 30 - 6
serial.c

@@ -1,5 +1,7 @@
 #include "serial.h"
 #include "serial.h"
+#include "serial_ring_buf.h"
 #include "pci.h"
 #include "pci.h"
+#include "timer.h"
 
 
 void serial_init(unsigned int port)
 void serial_init(unsigned int port)
 {
 {
@@ -30,7 +32,7 @@ void serial_puts(unsigned int port, const char *s)
 
 
 void serial_puthex(unsigned int port, unsigned int n)
 void serial_puthex(unsigned int port, unsigned int n)
 {
 {
-	static const char hex[] = "0123456789ABCDEF";
+	static const char hex[] = "01234589ABCDEF";
 	int i;
 	int i;
 	serial_puts(port, "0x");
 	serial_puts(port, "0x");
 	for (i = 28; i >= 0; i -= 4)
 	for (i = 28; i >= 0; i -= 4)
@@ -55,12 +57,34 @@ void serial_putdec(unsigned int port, unsigned int n)
 
 
 int serial_has_data(unsigned int port)
 int serial_has_data(unsigned int port)
 {
 {
-	return (port_inb(port + 5) & 0x01) != 0;
+	(void)port;
+	return serial_ring_count() > 0;
 }
 }
 
 
-char serial_getc_nb(unsigned int port)
+void serial_poll(void)
 {
 {
-	if (!serial_has_data(port))
-		return 0;
-	return (char)port_inb(port);
+	unsigned char c;
+	while ((port_inb(SERIAL_COM1 + 5) & 0x01) != 0) {
+		c = (unsigned char)port_inb(SERIAL_COM1);
+		serial_ring_push(c);
+	}
+}
+
+int serial_ring_getc(void)
+{
+	unsigned char c;
+	if (serial_ring_pop(&c))
+		return (int)c;
+	return 0;
+}
+
+int serial_ring_wait(unsigned int timeout_ms, unsigned char *out)
+{
+	unsigned int start = get_ticks_ms();
+	while (get_ticks_ms() - start < timeout_ms) {
+		if (serial_ring_pop(out))
+			return 1;
+		__asm__ volatile ("hlt");
+	}
+	return 0;
 }
 }

+ 5 - 2
serial.h

@@ -10,8 +10,11 @@ void serial_putc(unsigned int port, char c);
 void serial_puts(unsigned int port, const char *s);
 void serial_puts(unsigned int port, const char *s);
 void serial_puthex(unsigned int port, unsigned int n);
 void serial_puthex(unsigned int port, unsigned int n);
 void serial_putdec(unsigned int port, unsigned int n);
 void serial_putdec(unsigned int port, unsigned int n);
-int serial_has_data(unsigned int port);
-char serial_getc_nb(unsigned int port);
+int  serial_has_data(unsigned int port);
+void serial_poll(void);
+void serial_ring_init(void);
+int  serial_ring_getc(void);
+int  serial_ring_wait(unsigned int timeout_ms, unsigned char *out);
 
 
 extern unsigned int serial_recv_size;
 extern unsigned int serial_recv_size;
 
 

+ 3 - 3
serial_loop.c

@@ -22,10 +22,10 @@ void program_serial_loop_main(const char *arg)
 			serial_puts(port, msg);
 			serial_puts(port, msg);
 		}
 		}
 
 
-		while (serial_has_data(port)) {
-			char c = serial_getc_nb(port);
+		{
+			int c = serial_ring_getc();
 			if (c)
 			if (c)
-				io_putchar(c);
+				io_putchar((char)c);
 		}
 		}
 
 
 		cpu_idle();
 		cpu_idle();

+ 41 - 0
serial_ring_buf.c

@@ -0,0 +1,41 @@
+/* Serial ring buffer: 4096-byte circular buffer for COM1 receive.
+   Single writer (IRQ), single reader (process) — no locks needed. */
+
+#include "serial_ring_buf.h"
+
+static volatile unsigned char ring_buf[SERIAL_RING_SIZE];
+static volatile unsigned int ring_head; /* write index, IRQ only */
+static volatile unsigned int ring_tail; /* read index, process only */
+
+void serial_ring_init(void)
+{
+	ring_head = 0;
+	ring_tail = 0;
+}
+
+void serial_ring_push(unsigned char byte)
+{
+	unsigned int next = (ring_head + 1) % SERIAL_RING_SIZE;
+	if (next != ring_tail) {
+		ring_buf[ring_head] = byte;
+		ring_head = next;
+	}
+}
+
+int serial_ring_pop(unsigned char *byte)
+{
+	if (ring_tail == ring_head)
+		return 0;
+	*byte = ring_buf[ring_tail];
+	ring_tail = (ring_tail + 1) % SERIAL_RING_SIZE;
+	return 1;
+}
+
+unsigned int serial_ring_count(void)
+{
+	unsigned int h = ring_head;
+	unsigned int t = ring_tail;
+	if (h >= t)
+		return h - t;
+	return SERIAL_RING_SIZE - t + h;
+}

+ 12 - 0
serial_ring_buf.h

@@ -0,0 +1,12 @@
+/* Serial ring buffer: IRQ-driven receive for COM1. */
+#ifndef SERIAL_RING_BUF_H
+#define SERIAL_RING_BUF_H
+
+#define SERIAL_RING_SIZE 4096
+
+void serial_ring_init(void);
+void serial_ring_push(unsigned char byte);
+int  serial_ring_pop(unsigned char *byte);
+unsigned int serial_ring_count(void);
+
+#endif

+ 7 - 0
switch.S

@@ -8,6 +8,9 @@
  *
  *
  * Shadow buffer flush: every ~33 ticks (30Hz), set pending flag.
  * Shadow buffer flush: every ~33 ticks (30Hz), set pending flag.
  * Main loop checks dirty rows and copies only what changed.
  * Main loop checks dirty rows and copies only what changed.
+ *
+ * Serial poll: every tick, check UART for received data and push
+ * to ring buffer so no bytes are lost during preemption.
  */
  */
 .code32
 .code32
 .text
 .text
@@ -17,6 +20,7 @@
 .extern procs
 .extern procs
 .extern schedule
 .extern schedule
 .extern vga_shadow_flush_pending
 .extern vga_shadow_flush_pending
+.extern serial_poll
 
 
 /* struct process offsets (must match process.h / process.c) */
 /* struct process offsets (must match process.h / process.c) */
 .equ PCB_ESP,   0
 .equ PCB_ESP,   0
@@ -34,6 +38,9 @@ pit_irq_stub:
 	movb  $0x20, %al
 	movb  $0x20, %al
 	outb  %al, $0x20
 	outb  %al, $0x20
 
 
+	/* Poll UART for received serial data (prevents FIFO overflow). */
+	call  serial_poll
+
 	/* Shadow buffer flush at 30Hz: set pending flag, main loop does the copy. */
 	/* Shadow buffer flush at 30Hz: set pending flag, main loop does the copy. */
 	decl  flush_counter
 	decl  flush_counter
 	jnz   .no_flush
 	jnz   .no_flush