Skip to content

Commit 8a59475

Browse files
committed
The c example now reads and mirrors UART input.
1 parent a3d1b9d commit 8a59475

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

c-riscv-blink/src/main.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
#include <generated/csr.h>
22
#include <time.h>
33

4+
#define UART_EV_TX 0x1
5+
#define UART_EV_RX 0x2
6+
47
void isr(void) {
58
asm("nop");
69
}
710

811

912
int main(void) {
1013
uint32_t x = 0;
14+
uint32_t buf = 0;
1115
while (1) {
12-
uart_rxtx_write('a');
13-
leds_out_write(x);
14-
if (x == 0x02) {
16+
17+
// Wait for an incoming byte
18+
while (uart_rxempty_read()) {
19+
// Prevent the while loop from being optimized out
20+
asm("nop");
21+
}
22+
23+
// Read an incoming byte
24+
buf = uart_rxtx_read();
25+
// Tell the UART that we read a byte out of the FIFO
26+
// and that it can give us another.
27+
uart_ev_pending_write(UART_EV_RX);
28+
// Mirror the bytes back
29+
uart_rxtx_write(buf);
30+
31+
// Write new LED output and toggle
32+
leds_out_write(x | (buf << 2));
33+
if (x == 2) {
1534
x = 0x00000001;
1635
} else {
1736
x = 0x00000002;
1837
}
19-
msleep(80);
2038
}
2139
}

0 commit comments

Comments
 (0)