Skip to content

Commit

Permalink
machine: add callback for UART and USB CDC ACM
Browse files Browse the repository at this point in the history
  • Loading branch information
xudongzheng committed Nov 4, 2023
1 parent 731532c commit dbc215f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/machine/machine_rp2040_uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions src/machine/uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 13 additions & 3 deletions src/machine/usb/cdc/usbcdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit dbc215f

Please sign in to comment.