ardix/kernel/serial.c
Felix Kopp 2df4efdf2a
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.
2020-11-14 04:04:59 +01:00

82 lines
2.8 KiB
C

/* 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.
*/