-
Notifications
You must be signed in to change notification settings - Fork 573
/
uart.c
163 lines (130 loc) · 3.02 KB
/
uart.c
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
#include "uart.h"
#include <irq.h>
#include <generated/csr.h>
#ifdef CSR_UART_BASE
/*
* Buffer sizes must be a power of 2 so that modulos can be computed
* with logical AND.
*/
//#define UART_POLLING
#ifndef UART_POLLING
#define UART_RINGBUFFER_SIZE_RX 128
#define UART_RINGBUFFER_MASK_RX (UART_RINGBUFFER_SIZE_RX-1)
static char rx_buf[UART_RINGBUFFER_SIZE_RX];
static volatile unsigned int rx_produce;
static unsigned int rx_consume;
#define UART_RINGBUFFER_SIZE_TX 128
#define UART_RINGBUFFER_MASK_TX (UART_RINGBUFFER_SIZE_TX-1)
static char tx_buf[UART_RINGBUFFER_SIZE_TX];
static unsigned int tx_produce;
static volatile unsigned int tx_consume;
void uart_isr(void)
{
unsigned int stat, rx_produce_next;
stat = uart_ev_pending_read();
if(stat & UART_EV_RX) {
while(!uart_rxempty_read()) {
rx_produce_next = (rx_produce + 1) & UART_RINGBUFFER_MASK_RX;
if(rx_produce_next != rx_consume) {
rx_buf[rx_produce] = uart_rxtx_read();
rx_produce = rx_produce_next;
}
uart_ev_pending_write(UART_EV_RX);
#if defined(__cva6__)
asm volatile("fence\n");
#endif
}
}
if(stat & UART_EV_TX) {
uart_ev_pending_write(UART_EV_TX);
while((tx_consume != tx_produce) && !uart_txfull_read()) {
uart_rxtx_write(tx_buf[tx_consume]);
tx_consume = (tx_consume + 1) & UART_RINGBUFFER_MASK_TX;
}
}
}
/* Do not use in interrupt handlers! */
char uart_read(void)
{
char c;
if(irq_getie()) {
while(rx_consume == rx_produce);
} else if (rx_consume == rx_produce) {
return 0;
}
c = rx_buf[rx_consume];
rx_consume = (rx_consume + 1) & UART_RINGBUFFER_MASK_RX;
return c;
}
int uart_read_nonblock(void)
{
return (rx_consume != rx_produce);
}
void uart_write(char c)
{
unsigned int oldmask;
unsigned int tx_produce_next = (tx_produce + 1) & UART_RINGBUFFER_MASK_TX;
if(irq_getie()) {
while(tx_produce_next == tx_consume);
} else if(tx_produce_next == tx_consume) {
return;
}
oldmask = irq_getmask();
irq_setmask(oldmask & ~(1 << UART_INTERRUPT));
if((tx_consume != tx_produce) || uart_txfull_read()) {
tx_buf[tx_produce] = c;
tx_produce = tx_produce_next;
} else {
uart_rxtx_write(c);
}
irq_setmask(oldmask);
}
void uart_init(void)
{
rx_produce = 0;
rx_consume = 0;
tx_produce = 0;
tx_consume = 0;
uart_ev_pending_write(uart_ev_pending_read());
uart_ev_enable_write(UART_EV_TX | UART_EV_RX);
if (irq_attach)
irq_attach(UART_INTERRUPT, uart_isr);
irq_setmask(irq_getmask() | (1 << UART_INTERRUPT));
}
void uart_sync(void)
{
while(tx_consume != tx_produce);
}
#else
void uart_isr(void)
{
}
char uart_read(void)
{
char c;
while (uart_rxempty_read());
c = uart_rxtx_read();
uart_ev_pending_write(UART_EV_RX);
return c;
}
int uart_read_nonblock(void)
{
return (uart_rxempty_read() == 0);
}
void uart_write(char c)
{
while (uart_txfull_read());
uart_rxtx_write(c);
uart_ev_pending_write(UART_EV_TX);
}
void uart_init(void)
{
uart_ev_pending_write(uart_ev_pending_read());
uart_ev_enable_write(UART_EV_TX | UART_EV_RX);
}
void uart_sync(void)
{
while (uart_txfull_read());
}
#endif
#endif