Implement core serial routines

This is just a rough baseline for the higher-level
serial handling code and will probably change
within the next couple of commits.
This commit is contained in:
Felix Kopp 2020-11-14 04:04:59 +01:00
parent 0c24e3fde8
commit 2df4efdf2a
No known key found for this signature in database
GPG key ID: C478BA0A85F75728
7 changed files with 240 additions and 7 deletions

View file

@ -27,6 +27,7 @@ ARDIX_ARCH_PWD = $(PWD)/arch/at91sam3x8e
ARDIX_SOURCES += \ ARDIX_SOURCES += \
$(ARDIX_ARCH_PWD)/sched.c \ $(ARDIX_ARCH_PWD)/sched.c \
$(ARDIX_ARCH_PWD)/serial.c \
$(ARDIX_ARCH_PWD)/startup.c \ $(ARDIX_ARCH_PWD)/startup.c \
$(ARDIX_ARCH_PWD)/sys.c $(ARDIX_ARCH_PWD)/sys.c

69
arch/at91sam3x8e/serial.c Normal file
View file

@ -0,0 +1,69 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
#include <ardix/serial.h>
#include <ardix/types.h>
#include <ardix/ringbuf.h>
#include <arch/hardware.h>
#include <arch/serial.h>
int arch_serial_init(struct serial_interface *interface)
{
/* TODO */
return -1;
}
void arch_serial_exit(struct serial_interface *interface)
{
/* TODO */
}
int arch_serial_op_begin(struct serial_interface *interface)
{
/* TODO */
return -1;
}
ssize_t arch_serial_op_read(struct serial_interface *interface)
{
/* TODO */
return 0;
}
ssize_t arch_serial_op_write(struct serial_interface *interface)
{
/* TODO */
return 0;
}
struct serial_operations arch_serial_operations = {
.begin = &arch_serial_op_begin,
.read = &arch_serial_op_read,
.write = &arch_serial_op_write,
};
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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.
*/

View file

@ -0,0 +1,31 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
#pragma once
#include <ardix/serial.h>
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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.
*/

42
include/arch/serial.h Normal file
View file

@ -0,0 +1,42 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
#pragma once
#include <ardix/serial.h>
extern struct serial_operations arch_serial_operations;
int arch_serial_init(struct serial_interface *interface);
void arch_serial_exit(struct serial_interface *interface);
#ifdef ARCH_AT91SAM3X8E
#include <arch/at91sam3x8e/serial.h>
#else
#error "Unsupported architecture"
#endif
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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.
*/

View file

@ -7,20 +7,28 @@
#include <ardix/ringbuf.h> #include <ardix/ringbuf.h>
#include <toolchain.h> #include <toolchain.h>
#ifndef SERIAL_BUFSZ
/** size of a serial I/O buffer in bytes */
#define SERIAL_BUFSZ 64
#endif
struct serial_interface;
/** Low-level hardware operations implemented by the serial driver. */ /** Low-level hardware operations implemented by the serial driver. */
struct serial_operations { struct serial_operations {
/** Begin transmission at the specified baud rate. */ /** Do any hardware setup required to transmit data. */
int (*begin)(struct serial_interface *serial, long int baud); int (*begin)(struct serial_interface *serial);
/** Read from the hardware input buffer and store it into the rx ring buffer */ /** Read from the hardware input buffer and store it into the rx ring buffer */
ssize_t (*read)(uint8_t *dest, struct serial_interface *serial, size_t len); ssize_t (*read)(struct serial_interface *serial);
/** Read from the tx ring buffer and store it into the hardware output buffer */ /** Read from the tx ring buffer and store it into the hardware output buffer */
ssize_t (*write)(struct serial_interface *serial, const uint8_t *src, size_t len); ssize_t (*write)(struct serial_interface *serial);
}; };
struct serial_interface { struct serial_interface {
struct ringbuf *rx; struct ringbuf *rx;
struct ringbuf *tx; struct ringbuf *tx;
long int baud; long int baud;
int id;
struct serial_operations operations; struct serial_operations operations;
}; };
@ -32,10 +40,9 @@ extern struct serial_interface *serial_default_interface;
* *
* @param interface: The serial interface. * @param interface: The serial interface.
* @param baud: The baud rate (bits/second). * @param baud: The baud rate (bits/second).
* @param buflen: The respective length of the read and write buffer.
* @returns 0 on success, a negative number otherwise. * @returns 0 on success, a negative number otherwise.
*/ */
int serial_init(struct serial_interface *interface, long int baud, size_t buflen); int serial_init(struct serial_interface *interface, long int baud);
/** /**
* Flush all buffers (if possible) and close the serial interface. * Flush all buffers (if possible) and close the serial interface.

View file

@ -27,4 +27,5 @@ ARDIX_KERNEL_PWD = $(PWD)/kernel
ARDIX_SOURCES += \ ARDIX_SOURCES += \
$(ARDIX_KERNEL_PWD)/ringbuf.c \ $(ARDIX_KERNEL_PWD)/ringbuf.c \
$(ARDIX_KERNEL_PWD)/sched.c $(ARDIX_KERNEL_PWD)/sched.c \
$(ARDIX_KERNEL_PWD)/serial.c

82
kernel/serial.c Normal file
View file

@ -0,0 +1,82 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
#include <ardix/ringbuf.h>
#include <ardix/serial.h>
#include <arch/serial.h>
#include <stddef.h>
int serial_init(struct serial_interface *interface, long int baud)
{
int err = -1;
if (interface->id < 0)
return -1; /* invalid interface */
interface->baud = baud;
interface->rx = ringbuf_create(SERIAL_BUFSZ);
if (interface->rx == NULL)
return -1;
interface->tx = ringbuf_create(SERIAL_BUFSZ);
if (interface->tx == NULL) {
ringbuf_destroy(interface->rx);
return -1;
}
err = arch_serial_init(interface);
if (err) {
ringbuf_destroy(interface->rx);
ringbuf_destroy(interface->tx);
}
return err;
}
void serial_exit(struct serial_interface *interface)
{
arch_serial_exit(interface);
ringbuf_destroy(interface->rx);
ringbuf_destroy(interface->tx);
interface->id = -1;
}
/* TODO: tell the scheduler to run the I/O thread next after these calls */
ssize_t serial_read(uint8_t *dest, struct serial_interface *interface, size_t len)
{
return (ssize_t)ringbuf_read(dest, interface->rx, len);
}
ssize_t serial_write(struct serial_interface *interface, const uint8_t *data, size_t len)
{
return (ssize_t)ringbuf_write(interface->tx, data, len);
}
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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.
*/