machine: changed to block if no read data exists on UART and USB

This commit is contained in:
sago35
2022-03-29 08:25:37 +09:00
parent cc3009b5c1
commit 3591e194f7
3 changed files with 20 additions and 10 deletions
-3
View File
@@ -60,9 +60,6 @@ var (
ErrNotConfigured = errors.New("device has not been configured") 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 // PutcharUART writes a byte to the UART synchronously, without using interrupts
// or calling the scheduler // or calling the scheduler
func PutcharUART(u *UART, c byte) { func PutcharUART(u *UART, c byte) {
+13 -4
View File
@@ -3,7 +3,9 @@
package machine package machine
import "errors" import (
"errors"
)
var errUARTBufferEmpty = errors.New("UART buffer empty") var errUARTBufferEmpty = errors.New("UART buffer empty")
@@ -40,12 +42,16 @@ const (
// Read from the RX buffer. // Read from the RX buffer.
func (uart *UART) Read(data []byte) (n int, err error) { func (uart *UART) Read(data []byte) (n int, err error) {
// check if RX buffer is empty if len(data) == 0 {
size := uart.Buffered()
if size == 0 {
return 0, nil 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. // Make sure we do not read more from buffer than the data slice can hold.
if len(data) < size { if len(data) < size {
size = len(data) size = len(data)
@@ -89,3 +95,6 @@ func (uart *UART) Buffered() int {
func (uart *UART) Receive(data byte) { func (uart *UART) Receive(data byte) {
uart.Buffer.Put(data) uart.Buffer.Put(data)
} }
//go:linkname gosched runtime.Gosched
func gosched() int
+7 -3
View File
@@ -606,12 +606,16 @@ func newUSBSetup(data []byte) usbSetup {
// Read from the RX buffer. // Read from the RX buffer.
func (usbcdc *USBCDC) Read(data []byte) (n int, err error) { func (usbcdc *USBCDC) Read(data []byte) (n int, err error) {
// check if RX buffer is empty if len(data) == 0 {
size := usbcdc.Buffered()
if size == 0 {
return 0, nil 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. // Make sure we do not read more from buffer than the data slice can hold.
if len(data) < size { if len(data) < size {
size = len(data) size = len(data)