summaryrefslogtreecommitdiff
path: root/src/uart.c
blob: 4023b675290718bc2a1b883e15eaa6b49389b318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (c) 2017 Richard Braun.
 * Copyright (c) 2017 Jerko Lenstra.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#include <lib/cbuf.h>
#include <lib/macros.h>

#include "cpu.h"
#include "io.h"
#include "uart.h"
#include "thread.h"

#define UART_BAUD_RATE          115200

#define UART_CLOCK              115200
#define UART_DIVISOR            (UART_CLOCK / UART_BAUD_RATE)

#define UART_IRQ                4

#define UART_IER_DATA           0x1

#define UART_LCR_8BITS          0x3
#define UART_LCR_STOP1          0
#define UART_LCR_PARITY_NONE    0
#define UART_LCR_DLAB           0x80

#define UART_LSR_DATA_READY     0x01
#define UART_LSR_TX_EMPTY       0x20

#define UART_COM1_PORT          0x3F8
#define UART_REG_DAT            0
#define UART_REG_DIVL           0
#define UART_REG_IER            1
#define UART_REG_DIVH           1
#define UART_REG_LCR            3
#define UART_REG_LSR            5

#define UART_BUFFER_SIZE        16

#if !ISP2(UART_BUFFER_SIZE)
#error "invalid buffer size"
#endif

/*
 * Data shared between threads and the interrupt handler.
 *
 * Interrupts and preemption must be disabled when accessing these data.
 */
static uint8_t uart_buffer[UART_BUFFER_SIZE];
static struct cbuf uart_cbuf;
static struct thread *uart_waiter;

static void
uart_irq_handler(void *arg)
{
    uint8_t byte;
    int error;
    bool spurious;

    (void)arg;

    spurious = true;

    for (;;) {
        byte = io_read(UART_COM1_PORT + UART_REG_LSR);

        if (!(byte & UART_LSR_DATA_READY)) {
            break;
        }

        spurious = false;
        byte = io_read(UART_COM1_PORT + UART_REG_DAT);
        error = cbuf_pushb(&uart_cbuf, byte, false);

        if (error) {
            printf("uart: error: buffer full\n");
            break;
        }
    }

    if (!spurious) {
        thread_wakeup(uart_waiter);
    }
}

void
uart_setup(void)
{
    cbuf_init(&uart_cbuf, uart_buffer, sizeof(uart_buffer));

    io_write(UART_COM1_PORT + UART_REG_LCR, UART_LCR_DLAB);
    io_write(UART_COM1_PORT + UART_REG_DIVL, UART_DIVISOR);
    io_write(UART_COM1_PORT + UART_REG_DIVH, UART_DIVISOR >> 8);
    io_write(UART_COM1_PORT + UART_REG_LCR, UART_LCR_8BITS | UART_LCR_STOP1
                                            | UART_LCR_PARITY_NONE);
    io_write(UART_COM1_PORT + UART_REG_IER, UART_IER_DATA);

    cpu_irq_register(UART_IRQ, uart_irq_handler, NULL);
}

static void
uart_tx_wait(void)
{
    uint8_t byte;

    for (;;) {
        byte = io_read(UART_COM1_PORT + UART_REG_LSR);

        if (byte & UART_LSR_TX_EMPTY) {
            break;
        }
    }
}

static void
uart_write_byte(uint8_t byte)
{
    uart_tx_wait();
    io_write(UART_COM1_PORT + UART_REG_DAT, byte);
}

void
uart_write(uint8_t byte)
{
    if (byte == '\n') {
        uart_write_byte('\r');
    }

    uart_write_byte(byte);
}

int
uart_read(uint8_t *byte)
{
    uint32_t eflags;
    int error;

    thread_preempt_disable();
    eflags = cpu_intr_save();

    if (uart_waiter) {
        error = EBUSY;
        goto out;
    }

    for (;;) {
        error = cbuf_popb(&uart_cbuf, byte);

        if (!error) {
            break;
        }

        uart_waiter = thread_self();
        thread_sleep();
        uart_waiter = NULL;
    }

    error = 0;

out:
    cpu_intr_restore(eflags);
    thread_preempt_enable();

    return error;
}