diff --git a/src/machine/machine_nxpmk66f18_uart.go b/src/machine/machine_nxpmk66f18_uart.go index ccbb18edb..762cb1b2b 100644 --- a/src/machine/machine_nxpmk66f18_uart.go +++ b/src/machine/machine_nxpmk66f18_uart.go @@ -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) { diff --git a/src/machine/uart.go b/src/machine/uart.go index ccfc64ee7..2a4f641a5 100644 --- a/src/machine/uart.go +++ b/src/machine/uart.go @@ -3,7 +3,9 @@ package machine -import "errors" +import ( + "errors" +) var errUARTBufferEmpty = errors.New("UART buffer empty") @@ -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) @@ -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 diff --git a/src/machine/usb.go b/src/machine/usb.go index 1789e170a..1f1af5a20 100644 --- a/src/machine/usb.go +++ b/src/machine/usb.go @@ -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)