Skip to content

Commit

Permalink
machine: changed to block if no read data exists on UART and USB
Browse files Browse the repository at this point in the history
  • Loading branch information
sago35 committed Mar 28, 2022
1 parent cc3009b commit 3591e19
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
3 changes: 0 additions & 3 deletions src/machine/machine_nxpmk66f18_uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ var (
ErrNotConfigured = errors.New("device has not been configured")
)

//go:linkname gosched runtime.Gosched
func gosched()

// PutcharUART writes a byte to the UART synchronously, without using interrupts
// or calling the scheduler
func PutcharUART(u *UART, c byte) {
Expand Down
17 changes: 13 additions & 4 deletions src/machine/uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

package machine

import "errors"
import (
"errors"
)

var errUARTBufferEmpty = errors.New("UART buffer empty")

Expand Down Expand Up @@ -40,12 +42,16 @@ const (

// Read from the RX buffer.
func (uart *UART) Read(data []byte) (n int, err error) {
// check if RX buffer is empty
size := uart.Buffered()
if size == 0 {
if len(data) == 0 {
return 0, nil
}

size := uart.Buffered()
for size == 0 {
gosched()
size = uart.Buffered()
}

// Make sure we do not read more from buffer than the data slice can hold.
if len(data) < size {
size = len(data)
Expand Down Expand Up @@ -89,3 +95,6 @@ func (uart *UART) Buffered() int {
func (uart *UART) Receive(data byte) {
uart.Buffer.Put(data)
}

//go:linkname gosched runtime.Gosched
func gosched() int
10 changes: 7 additions & 3 deletions src/machine/usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,16 @@ func newUSBSetup(data []byte) usbSetup {

// Read from the RX buffer.
func (usbcdc *USBCDC) Read(data []byte) (n int, err error) {
// check if RX buffer is empty
size := usbcdc.Buffered()
if size == 0 {
if len(data) == 0 {
return 0, nil
}

size := usbcdc.Buffered()
for size == 0 {
gosched()
size = usbcdc.Buffered()
}

// Make sure we do not read more from buffer than the data slice can hold.
if len(data) < size {
size = len(data)
Expand Down

0 comments on commit 3591e19

Please sign in to comment.