Skip to content

Commit 39805bc

Browse files
sago35deadprogram
authored andcommitted
os, runtime: enable os.Stdin for baremetal target
1 parent 97842b3 commit 39805bc

26 files changed

+327
-2
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ smoketest:
396396
@$(MD5SUM) test.hex
397397
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/echo
398398
@$(MD5SUM) test.hex
399+
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/echo2
400+
@$(MD5SUM) test.hex
399401
$(TINYGO) build -size short -o test.hex -target=circuitplay-express examples/i2s
400402
@$(MD5SUM) test.hex
401403
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/mcp3008

src/examples/echo2/echo2.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This is a echo console running on the os.Stdin and os.Stdout.
2+
// Stdin and os.Stdout are connected to machine.Serial in the baremetal target.
3+
//
4+
// Serial can be switched with the -serial option as follows
5+
// 1. tinygo flash -target wioterminal -serial usb examples/echo2
6+
// 2. tinygo flash -target wioterminal -serial uart examples/echo2
7+
//
8+
// This example will also work with standard Go.
9+
package main
10+
11+
import (
12+
"bufio"
13+
"fmt"
14+
"os"
15+
)
16+
17+
func main() {
18+
fmt.Printf("Echo console enabled. Type something then press enter:\r\n")
19+
20+
scanner := bufio.NewScanner(os.Stdin)
21+
22+
for {
23+
msg := ""
24+
fmt.Scanf("%s\n", &msg)
25+
fmt.Printf("You typed (scanf) : %s\r\n", msg)
26+
27+
if scanner.Scan() {
28+
fmt.Printf("You typed (scanner) : %s\r\n", scanner.Text())
29+
}
30+
}
31+
}

src/os/file_other.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,26 @@ func NewFile(fd uintptr, name string) *File {
3838
return &File{&file{stdioFileHandle(fd), name}}
3939
}
4040

41-
// Read is unsupported on this system.
41+
// Read reads up to len(b) bytes from machine.Serial.
42+
// It returns the number of bytes read and any error encountered.
4243
func (f stdioFileHandle) Read(b []byte) (n int, err error) {
43-
return 0, ErrUnsupported
44+
if len(b) == 0 {
45+
return 0, nil
46+
}
47+
48+
size := buffered()
49+
for size == 0 {
50+
gosched()
51+
size = buffered()
52+
}
53+
54+
if size > len(b) {
55+
size = len(b)
56+
}
57+
for i := 0; i < size; i++ {
58+
b[i] = getchar()
59+
}
60+
return size, nil
4461
}
4562

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

98+
//go:linkname getchar runtime.getchar
99+
func getchar() byte
100+
101+
//go:linkname buffered runtime.buffered
102+
func buffered() int
103+
104+
//go:linkname gosched runtime.Gosched
105+
func gosched() int
106+
81107
func Pipe() (r *File, w *File, err error) {
82108
return nil, nil, ErrNotImplemented
83109
}

src/runtime/runtime_arm7tdmi.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ func putchar(c byte) {
1414
// dummy, TODO
1515
}
1616

17+
func getchar() byte {
18+
// dummy, TODO
19+
return 0
20+
}
21+
22+
func buffered() int {
23+
// dummy, TODO
24+
return 0
25+
}
26+
1727
//go:extern _sbss
1828
var _sbss [0]byte
1929

src/runtime/runtime_atmega.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ func putchar(c byte) {
1616
machine.Serial.WriteByte(c)
1717
}
1818

19+
func getchar() byte {
20+
for machine.Serial.Buffered() == 0 {
21+
Gosched()
22+
}
23+
v, _ := machine.Serial.ReadByte()
24+
return v
25+
}
26+
27+
func buffered() int {
28+
return machine.Serial.Buffered()
29+
}
30+
1931
// Sleep for a given period. The period is defined by the WDT peripheral, and is
2032
// on most chips (at least) 3 bits wide, in powers of two from 16ms to 2s
2133
// (0=16ms, 1=32ms, 2=64ms...). Note that the WDT is not very accurate: it can

src/runtime/runtime_atsamd21.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ func putchar(c byte) {
3939
machine.Serial.WriteByte(c)
4040
}
4141

42+
func getchar() byte {
43+
for machine.Serial.Buffered() == 0 {
44+
Gosched()
45+
}
46+
v, _ := machine.Serial.ReadByte()
47+
return v
48+
}
49+
50+
func buffered() int {
51+
return machine.Serial.Buffered()
52+
}
53+
4254
func initClocks() {
4355
// Set 1 Flash Wait State for 48MHz, required for 3.3V operation according to SAMD21 Datasheet
4456
sam.NVMCTRL.CTRLB.SetBits(sam.NVMCTRL_CTRLB_RWS_HALF << sam.NVMCTRL_CTRLB_RWS_Pos)

src/runtime/runtime_atsamd51.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ func putchar(c byte) {
3939
machine.Serial.WriteByte(c)
4040
}
4141

42+
func getchar() byte {
43+
for machine.Serial.Buffered() == 0 {
44+
Gosched()
45+
}
46+
v, _ := machine.Serial.ReadByte()
47+
return v
48+
}
49+
50+
func buffered() int {
51+
return machine.Serial.Buffered()
52+
}
53+
4254
func initClocks() {
4355
// set flash wait state
4456
sam.NVMCTRL.CTRLA.SetBits(0 << sam.NVMCTRL_CTRLA_RWS_Pos)

src/runtime/runtime_attiny.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ func putchar(c byte) {
1414
// UART is not supported.
1515
}
1616

17+
func getchar() byte {
18+
// UART is not supported.
19+
return 0
20+
}
21+
22+
func buffered() int {
23+
// UART is not supported.
24+
return 0
25+
}
26+
1727
func sleepWDT(period uint8) {
1828
// TODO: use the watchdog timer instead of a busy loop.
1929
for i := 0x45; i != 0; i-- {

src/runtime/runtime_cortexm_qemu.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ func putchar(c byte) {
4949
stdoutWrite.Set(uint8(c))
5050
}
5151

52+
func getchar() byte {
53+
// dummy, TODO
54+
return 0
55+
}
56+
57+
func buffered() int {
58+
// dummy, TODO
59+
return 0
60+
}
61+
5262
func waitForEvents() {
5363
arm.Asm("wfe")
5464
}

src/runtime/runtime_esp32xx.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ func putchar(c byte) {
1515
machine.Serial.WriteByte(c)
1616
}
1717

18+
func getchar() byte {
19+
for machine.Serial.Buffered() == 0 {
20+
Gosched()
21+
}
22+
v, _ := machine.Serial.ReadByte()
23+
return v
24+
}
25+
26+
func buffered() int {
27+
return machine.Serial.Buffered()
28+
}
29+
1830
// Initialize .bss: zero-initialized global variables.
1931
// The .data section has already been loaded by the ROM bootloader.
2032
func clearbss() {

0 commit comments

Comments
 (0)