Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block reading of uart and usb #2739

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ smoketest:
@$(MD5SUM) test.hex
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/echo
@$(MD5SUM) test.hex
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/echo2
@$(MD5SUM) test.hex
$(TINYGO) build -size short -o test.hex -target=circuitplay-express examples/i2s
@$(MD5SUM) test.hex
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/mcp3008
Expand Down
31 changes: 31 additions & 0 deletions src/examples/echo2/echo2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This is a echo console running on the os.Stdin and os.Stdout.
// Stdin and os.Stdout are connected to machine.Serial in the baremetal target.
//
// Serial can be switched with the -serial option as follows
// 1. tinygo flash -target wioterminal -serial usb examples/echo2
// 2. tinygo flash -target wioterminal -serial uart examples/echo2
//
// This example will also work with standard Go.
package main

import (
"bufio"
"fmt"
"os"
)

func main() {
fmt.Printf("Echo console enabled. Type something then press enter:\r\n")

scanner := bufio.NewScanner(os.Stdin)

for {
msg := ""
fmt.Scanf("%s\n", &msg)
fmt.Printf("You typed (scanf) : %s\r\n", msg)

if scanner.Scan() {
fmt.Printf("You typed (scanner) : %s\r\n", scanner.Text())
}
}
}
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
30 changes: 28 additions & 2 deletions src/os/file_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,26 @@ func NewFile(fd uintptr, name string) *File {
return &File{&file{stdioFileHandle(fd), name}}
}

// Read is unsupported on this system.
// Read reads up to len(b) bytes from machine.Serial.
// It returns the number of bytes read and any error encountered.
func (f stdioFileHandle) Read(b []byte) (n int, err error) {
return 0, ErrUnsupported
if len(b) == 0 {
return 0, nil
}

size := buffered()
for size == 0 {
gosched()
size = buffered()
}

if size > len(b) {
size = len(b)
}
for i := 0; i < size; i++ {
b[i] = getchar()
}
return size, nil
}

func (f stdioFileHandle) ReadAt(b []byte, off int64) (n int, err error) {
Expand Down Expand Up @@ -78,6 +95,15 @@ func (f stdioFileHandle) Fd() uintptr {
//go:linkname putchar runtime.putchar
func putchar(c byte)

//go:linkname getchar runtime.getchar
func getchar() byte

//go:linkname buffered runtime.buffered
func buffered() int

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

func Pipe() (r *File, w *File, err error) {
return nil, nil, ErrNotImplemented
}
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/runtime_arm7tdmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ func putchar(c byte) {
// dummy, TODO
}

func getchar() byte {
// dummy, TODO
return 0
}

func buffered() int {
// dummy, TODO
return 0
}

//go:extern _sbss
var _sbss [0]byte

Expand Down
12 changes: 12 additions & 0 deletions src/runtime/runtime_atmega.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c)
}

func getchar() byte {
for machine.Serial.Buffered() == 0 {
Gosched()
}
v, _ := machine.Serial.ReadByte()
return v
}

func buffered() int {
return machine.Serial.Buffered()
}

// Sleep for a given period. The period is defined by the WDT peripheral, and is
// on most chips (at least) 3 bits wide, in powers of two from 16ms to 2s
// (0=16ms, 1=32ms, 2=64ms...). Note that the WDT is not very accurate: it can
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/runtime_atsamd21.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c)
}

func getchar() byte {
for machine.Serial.Buffered() == 0 {
Gosched()
}
v, _ := machine.Serial.ReadByte()
return v
}

func buffered() int {
return machine.Serial.Buffered()
}

func initClocks() {
// Set 1 Flash Wait State for 48MHz, required for 3.3V operation according to SAMD21 Datasheet
sam.NVMCTRL.CTRLB.SetBits(sam.NVMCTRL_CTRLB_RWS_HALF << sam.NVMCTRL_CTRLB_RWS_Pos)
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/runtime_atsamd51.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c)
}

func getchar() byte {
for machine.Serial.Buffered() == 0 {
Gosched()
}
v, _ := machine.Serial.ReadByte()
return v
}

func buffered() int {
return machine.Serial.Buffered()
}

func initClocks() {
// set flash wait state
sam.NVMCTRL.CTRLA.SetBits(0 << sam.NVMCTRL_CTRLA_RWS_Pos)
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/runtime_attiny.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ func putchar(c byte) {
// UART is not supported.
}

func getchar() byte {
// UART is not supported.
return 0
}

func buffered() int {
// UART is not supported.
return 0
}

func sleepWDT(period uint8) {
// TODO: use the watchdog timer instead of a busy loop.
for i := 0x45; i != 0; i-- {
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/runtime_cortexm_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ func putchar(c byte) {
stdoutWrite.Set(uint8(c))
}

func getchar() byte {
// dummy, TODO
return 0
}

func buffered() int {
// dummy, TODO
return 0
}

func waitForEvents() {
arm.Asm("wfe")
}
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/runtime_esp32xx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c)
}

func getchar() byte {
for machine.Serial.Buffered() == 0 {
Gosched()
}
v, _ := machine.Serial.ReadByte()
return v
}

func buffered() int {
return machine.Serial.Buffered()
}

// Initialize .bss: zero-initialized global variables.
// The .data section has already been loaded by the ROM bootloader.
func clearbss() {
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/runtime_esp8266.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c)
}

func getchar() byte {
for machine.Serial.Buffered() == 0 {
Gosched()
}
v, _ := machine.Serial.ReadByte()
return v
}

func buffered() int {
return machine.Serial.Buffered()
}

// Write to the internal control bus (using I2C?).
// Signature found here:
// https://github.com/espressif/ESP8266_RTOS_SDK/blob/14171de0/components/esp8266/include/esp8266/rom_functions.h#L54
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/runtime_fe310.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c)
}

func getchar() byte {
for machine.Serial.Buffered() == 0 {
Gosched()
}
v, _ := machine.Serial.ReadByte()
return v
}

func buffered() int {
return machine.Serial.Buffered()
}

var timerWakeup volatile.Register8

func ticks() timeUnit {
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/runtime_k210.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c)
}

func getchar() byte {
for machine.Serial.Buffered() == 0 {
Gosched()
}
v, _ := machine.Serial.ReadByte()
return v
}

func buffered() int {
return machine.Serial.Buffered()
}

var timerWakeup volatile.Register8

func ticks() timeUnit {
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/runtime_mimxrt1062.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c)
}

func getchar() byte {
for machine.UART1.Buffered() == 0 {
Gosched()
}
v, _ := machine.UART1.ReadByte()
return v
}

func buffered() int {
return machine.UART1.Buffered()
}

func exit(code int) {
abort()
}
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/runtime_nrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c)
}

func getchar() byte {
for machine.Serial.Buffered() == 0 {
Gosched()
}
v, _ := machine.Serial.ReadByte()
return v
}

func buffered() int {
return machine.Serial.Buffered()
}

func sleepTicks(d timeUnit) {
for d != 0 {
ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side)
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/runtime_nxpmk66f18.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ func putchar(c byte) {
machine.PutcharUART(machine.UART0, c)
}

func getchar() byte {
// dummy, TODO
return 0
}

func buffered() int {
// dummy, TODO
return 0
}

func exit(code int) {
abort()
}
Expand Down
Loading