From 94fec09b31adaa16ad99c039926f3f1725cb3746 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 10 Jan 2020 16:58:35 +0100 Subject: [PATCH] machine/fe310: implement UART receive interrupts This makes the echo example work on the HiFive1 rev B board. --- src/machine/machine_fe310.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/machine/machine_fe310.go b/src/machine/machine_fe310.go index 5c2df0ee..c85fc898 100644 --- a/src/machine/machine_fe310.go +++ b/src/machine/machine_fe310.go @@ -4,6 +4,7 @@ package machine import ( "device/sifive" + "runtime/interrupt" ) func CPUFrequency() uint32 { @@ -65,6 +66,23 @@ func (uart UART) Configure(config UARTConfig) { // 115200 baud rate is 138. sifive.UART0.DIV.Set(138) sifive.UART0.TXCTRL.Set(sifive.UART_TXCTRL_ENABLE) + sifive.UART0.RXCTRL.Set(sifive.UART_RXCTRL_ENABLE) + sifive.UART0.IE.Set(sifive.UART_IE_RXWM) // enable the receive interrupt (only) + intr := interrupt.New(sifive.IRQ_UART0, UART0.handleInterrupt) + intr.SetPriority(5) + intr.Enable() +} + +func (uart *UART) handleInterrupt(interrupt.Interrupt) { + rxdata := uart.Bus.RXDATA.Get() + c := byte(rxdata) + if uint32(c) != rxdata { + // The rxdata has other bits set than just the low 8 bits. This probably + // means that the 'empty' flag is set, which indicates there is no data + // to be read and the byte is garbage. Ignore this byte. + return + } + uart.Receive(c) } func (uart UART) WriteByte(c byte) {