diff --git a/src/machine/machine_rp2040_uart.go b/src/machine/machine_rp2040_uart.go index 1d927df128..ea73175d4b 100644 --- a/src/machine/machine_rp2040_uart.go +++ b/src/machine/machine_rp2040_uart.go @@ -9,7 +9,9 @@ import ( // UART on the RP2040. type UART struct { - Buffer *RingBuffer + Buffer *RingBuffer + rxCallback func(byte) bool + Bus *rp.UART0_Type Interrupt interrupt.Interrupt } diff --git a/src/machine/uart.go b/src/machine/uart.go index eeeb7d6a0b..c452926066 100644 --- a/src/machine/uart.go +++ b/src/machine/uart.go @@ -99,8 +99,17 @@ func (uart *UART) Buffered() int { return int(uart.Buffer.Used()) } +func (uart *UART) SetRXCallback(cb func(byte) bool) { + uart.rxCallback = cb +} + // Receive handles adding data to the UART's data buffer. // Usually called by the IRQ handler for a machine. func (uart *UART) Receive(data byte) { + if uart.rxCallback != nil { + if uart.rxCallback(data) { + return + } + } uart.Buffer.Put(data) } diff --git a/src/machine/usb/cdc/usbcdc.go b/src/machine/usb/cdc/usbcdc.go index 5b5ffbf7c4..8459f0ee66 100644 --- a/src/machine/usb/cdc/usbcdc.go +++ b/src/machine/usb/cdc/usbcdc.go @@ -59,17 +59,27 @@ func (usbcdc *USBCDC) Buffered() int { return int(usbcdc.rxBuffer.Used()) } +func (usbcdc *USBCDC) SetRXCallback(cb func(byte) bool) { + usbcdc.rxCallback = cb +} + // Receive handles adding data to the UART's data buffer. // Usually called by the IRQ handler for a machine. func (usbcdc *USBCDC) Receive(data byte) { + if usbcdc.rxCallback != nil { + if usbcdc.rxCallback(data) { + return + } + } usbcdc.rxBuffer.Put(data) } // USBCDC is the USB CDC aka serial over USB interface. type USBCDC struct { - rxBuffer *rxRingBuffer - txBuffer *txRingBuffer - waitTxc bool + rxBuffer *rxRingBuffer + txBuffer *txRingBuffer + rxCallback func(byte) bool + waitTxc bool } var (