1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#include <string.h>
14#include <stdlib.h>
15#include <platsupport/serial.h>
16#include "chardev.h"
17
18ssize_t uart_write(
19    ps_chardevice_t *d,
20    const void *vdata,
21    size_t count,
22    chardev_callback_t rcb UNUSED,
23    void *token UNUSED)
24{
25    const unsigned char *data = (const unsigned char *)vdata;
26    for (int i = 0; i < count; i++) {
27        if (uart_putchar(d, data[i]) < 0) {
28            return i;
29        }
30    }
31    return count;
32}
33
34ssize_t uart_read(
35    ps_chardevice_t *d,
36    void *vdata,
37    size_t count,
38    chardev_callback_t rcb UNUSED,
39    void *token UNUSED)
40{
41    char *data = (char *)vdata;
42    for (int i = 0; i < count; i++) {
43        int ret = uart_getchar(d);
44        if (EOF == ret) {
45            return i;
46        }
47        data[i] = ret;
48    }
49    return count;
50}
51