Skip to content

Commit

Permalink
machine: implement callback for UART and USB CDC ACM
Browse files Browse the repository at this point in the history
  • Loading branch information
xudongzheng committed Nov 6, 2023
1 parent 731532c commit a56237c
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/machine/machine_atmega.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func init() {

// UART on the AVR.
type UART struct {
Buffer *RingBuffer
Buffer *RingBuffer
rxCallback func(byte) bool

dataReg *volatile.Register8
baudRegH *volatile.Register8
Expand Down
9 changes: 5 additions & 4 deletions src/machine/machine_atsamd21.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,11 @@ func waitADCSync() {

// UART on the SAMD21.
type UART struct {
Buffer *RingBuffer
Bus *sam.SERCOM_USART_Type
SERCOM uint8
Interrupt interrupt.Interrupt
Buffer *RingBuffer
rxCallback func(byte) bool
Bus *sam.SERCOM_USART_Type
SERCOM uint8
Interrupt interrupt.Interrupt
}

const (
Expand Down
9 changes: 5 additions & 4 deletions src/machine/machine_atsamd51.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,10 +964,11 @@ func (a ADC) getADCChannel() uint8 {

// UART on the SAMD51.
type UART struct {
Buffer *RingBuffer
Bus *sam.SERCOM_USART_INT_Type
SERCOM uint8
Interrupt interrupt.Interrupt // RXC interrupt
Buffer *RingBuffer
rxCallback func(byte) bool
Bus *sam.SERCOM_USART_INT_Type
SERCOM uint8
Interrupt interrupt.Interrupt // RXC interrupt
}

var (
Expand Down
5 changes: 3 additions & 2 deletions src/machine/machine_esp32.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,9 @@ var (
)

type UART struct {
Bus *esp.UART_Type
Buffer *RingBuffer
Bus *esp.UART_Type
Buffer *RingBuffer
rxCallback func(byte) bool
}

func (uart *UART) Configure(config UARTConfig) {
Expand Down
1 change: 1 addition & 0 deletions src/machine/machine_esp32c3.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ var (
type UART struct {
Bus *esp.UART_Type
Buffer *RingBuffer
rxCallback func(byte) bool
ParityErrorDetected bool // set when parity error detected
DataErrorDetected bool // set when data corruption detected
DataOverflowDetected bool // set when data overflow detected in UART FIFO buffer or RingBuffer
Expand Down
3 changes: 2 additions & 1 deletion src/machine/machine_esp8266.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ var UART0 = &_UART0
var _UART0 = UART{Buffer: NewRingBuffer()}

type UART struct {
Buffer *RingBuffer
Buffer *RingBuffer
rxCallback func(byte) bool
}

// Configure the UART baud rate. TX and RX pins are fixed by the hardware so
Expand Down
5 changes: 3 additions & 2 deletions src/machine/machine_fe310.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ func (p Pin) PortMaskClear() (*uint32, uint32) {
}

type UART struct {
Bus *sifive.UART_Type
Buffer *RingBuffer
Bus *sifive.UART_Type
Buffer *RingBuffer
rxCallback func(byte) bool
}

var (
Expand Down
5 changes: 3 additions & 2 deletions src/machine/machine_k210.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,9 @@ func (p Pin) SetInterrupt(change PinChange, callback func(Pin)) error {
}

type UART struct {
Bus *kendryte.UARTHS_Type
Buffer *RingBuffer
Bus *kendryte.UARTHS_Type
Buffer *RingBuffer
rxCallback func(byte) bool
}

var (
Expand Down
7 changes: 4 additions & 3 deletions src/machine/machine_mimxrt1062_uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
// UART peripheral abstraction layer for the MIMXRT1062

type UART struct {
Bus *nxp.LPUART_Type
Buffer *RingBuffer
Interrupt interrupt.Interrupt
Bus *nxp.LPUART_Type
Buffer *RingBuffer
rxCallback func(byte) bool
Interrupt interrupt.Interrupt

// txBuffer should be allocated globally (such as when UART is created) to
// prevent it being reclaimed or cleaned up prematurely.
Expand Down
3 changes: 2 additions & 1 deletion src/machine/machine_nrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func (p Pin) SetInterrupt(change PinChange, callback func(Pin)) error {

// UART on the NRF.
type UART struct {
Buffer *RingBuffer
Buffer *RingBuffer
rxCallback func(byte) bool
}

// UART
Expand Down
1 change: 1 addition & 0 deletions src/machine/machine_nxpmk66f18_uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type UART struct {

// state
Buffer RingBuffer // RX Buffer
rxCallback func(byte) bool
TXBuffer RingBuffer
Configured bool
Transmitting volatile.Register8
Expand Down
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
1 change: 1 addition & 0 deletions src/machine/machine_stm32_uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// UART representation
type UART struct {
Buffer *RingBuffer
rxCallback func(byte) bool
Bus *stm32.USART_Type
Interrupt interrupt.Interrupt
TxAltFuncSelector uint8
Expand Down
14 changes: 12 additions & 2 deletions src/machine/uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ const (
// To implement the UART interface for a board, you must declare a concrete type as follows:
//
// type UART struct {
// Buffer *RingBuffer
// Buffer *RingBuffer
// rxCallback func(byte) bool
// }
//
// You can also add additional members to this struct depending on your implementation,
// but the *RingBuffer is required.
// Buffer and rxCallback are required.
// When you are declaring your UARTs for your board, make sure that you also declare the
// RingBuffer using the NewRingBuffer() function when you declare your UART:
//
Expand Down Expand Up @@ -99,8 +100,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 a56237c

Please sign in to comment.