mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
machine: changed to block if no read data exists on UART and USB
This commit is contained in:
@@ -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) {
|
||||
|
||||
+13
-4
@@ -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
|
||||
|
||||
+7
-3
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user