From e690a6824ef6bb1c9217e0321df052bd3c9ee5b0 Mon Sep 17 00:00:00 2001 From: Felix Kopp Date: Sun, 29 Nov 2020 23:23:37 +0100 Subject: [PATCH] io: add I/O thread base --- include/ardix/serial.h | 5 ++++ kernel/Makefile | 2 ++ kernel/io/Makefile | 29 ++++++++++++++++++++ kernel/io/io.c | 60 ++++++++++++++++++++++++++++++++++++++++++ kernel/serial.c | 25 +++++++++++++----- 5 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 kernel/io/Makefile create mode 100644 kernel/io/io.c diff --git a/include/ardix/serial.h b/include/ardix/serial.h index 1ad184e..2e0a3bb 100644 --- a/include/ardix/serial.h +++ b/include/ardix/serial.h @@ -7,6 +7,11 @@ #include #include +#ifndef CONFIG_SERIAL_BAUD +/** serial baud rate */ +#define CONFIG_SERIAL_BAUD 115200 +#endif + #ifndef SERIAL_BUFSZ /** size of a serial I/O buffer in bytes */ #define SERIAL_BUFSZ 256 diff --git a/kernel/Makefile b/kernel/Makefile index 7e27cae..794651c 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -25,6 +25,8 @@ ARDIX_KERNEL_PWD = $(PWD)/kernel +include $(ARDIX_KERNEL_PWD)/io/Makefile + ARDIX_SOURCES += \ $(ARDIX_KERNEL_PWD)/printk.c \ $(ARDIX_KERNEL_PWD)/ringbuf.c \ diff --git a/kernel/io/Makefile b/kernel/io/Makefile new file mode 100644 index 0000000..9363643 --- /dev/null +++ b/kernel/io/Makefile @@ -0,0 +1,29 @@ +# +# Copyright (c) 2020 Felix Kopp +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# 3. Neither the name of the copyright holder nor the names of its contributors may be +# used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY +# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +ARDIX_KERNEL_IO_PWD = $(ARDIX_KERNEL_PWD)/io + +ARDIX_SOURCES += \ + $(ARDIX_KERNEL_IO_PWD)/io.c diff --git a/kernel/io/io.c b/kernel/io/io.c new file mode 100644 index 0000000..b85b388 --- /dev/null +++ b/kernel/io/io.c @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* See the end of this file for copyright, licensing, and warranty information. */ + +#include + +#include +#include +#include + +#include + +__naked void io_thread_entry(void) +{ + while (1) { + io_serial_buf_update(serial_default_interface); + + sched_switch_early(PROC_QUEUE); + } +} + +int io_init(void) +{ + int ret; + struct process *proc; + + ret = serial_init(serial_default_interface, CONFIG_SERIAL_BAUD); + if (ret) + return ret; + + proc = sched_process_create(&io_thread_entry); + if (proc == NULL) + ret = -1; + + return ret; +} + +/* + * Copyright (c) 2020 Felix Kopp + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ diff --git a/kernel/serial.c b/kernel/serial.c index d37a361..c180f67 100644 --- a/kernel/serial.c +++ b/kernel/serial.c @@ -3,9 +3,10 @@ #include #include +#include -#include #include +#include #include @@ -58,13 +59,25 @@ ssize_t serial_read(void *dest, struct serial_interface *interface, size_t len) ssize_t serial_write(struct serial_interface *interface, const void *data, size_t len) { - ssize_t ret; + size_t ret = 0; + size_t tmp; - sched_atomic_enter(); - ret = (ssize_t)ringbuf_write(interface->tx, data, len); - sched_atomic_leave(); + while (1) { + sched_atomic_enter(); + tmp = ringbuf_write(interface->tx, data, len); + sched_atomic_leave(); + ret += tmp; - return ret; + if (ret != len) { /* buffer full, suspend until I/O is ready */ + len -= tmp; + data += tmp; + sched_switch_early(PROC_IOWAIT); + } else { + break; + } + } + + return (ssize_t)ret; } /*