Skip to content

Commit 94fec09

Browse files
aykevldeadprogram
authored andcommitted
machine/fe310: implement UART receive interrupts
This makes the echo example work on the HiFive1 rev B board.
1 parent 415c605 commit 94fec09

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/machine/machine_fe310.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package machine
44

55
import (
66
"device/sifive"
7+
"runtime/interrupt"
78
)
89

910
func CPUFrequency() uint32 {
@@ -65,6 +66,23 @@ func (uart UART) Configure(config UARTConfig) {
6566
// 115200 baud rate is 138.
6667
sifive.UART0.DIV.Set(138)
6768
sifive.UART0.TXCTRL.Set(sifive.UART_TXCTRL_ENABLE)
69+
sifive.UART0.RXCTRL.Set(sifive.UART_RXCTRL_ENABLE)
70+
sifive.UART0.IE.Set(sifive.UART_IE_RXWM) // enable the receive interrupt (only)
71+
intr := interrupt.New(sifive.IRQ_UART0, UART0.handleInterrupt)
72+
intr.SetPriority(5)
73+
intr.Enable()
74+
}
75+
76+
func (uart *UART) handleInterrupt(interrupt.Interrupt) {
77+
rxdata := uart.Bus.RXDATA.Get()
78+
c := byte(rxdata)
79+
if uint32(c) != rxdata {
80+
// The rxdata has other bits set than just the low 8 bits. This probably
81+
// means that the 'empty' flag is set, which indicates there is no data
82+
// to be read and the byte is garbage. Ignore this byte.
83+
return
84+
}
85+
uart.Receive(c)
6886
}
6987

7088
func (uart UART) WriteByte(c byte) {

0 commit comments

Comments
 (0)