os, runtime: enable os.Stdin for baremetal target

Этот коммит содержится в:
sago35 2022-03-29 08:25:04 +09:00 коммит произвёл Ron Evans
родитель 97842b367c
коммит 39805bca45
26 изменённых файлов: 327 добавлений и 2 удалений

Просмотреть файл

@ -396,6 +396,8 @@ smoketest:
@$(MD5SUM) test.hex @$(MD5SUM) test.hex
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/echo $(TINYGO) build -size short -o test.hex -target=pca10040 examples/echo
@$(MD5SUM) test.hex @$(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 $(TINYGO) build -size short -o test.hex -target=circuitplay-express examples/i2s
@$(MD5SUM) test.hex @$(MD5SUM) test.hex
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/mcp3008 $(TINYGO) build -size short -o test.hex -target=pca10040 examples/mcp3008

31
src/examples/echo2/echo2.go Обычный файл
Просмотреть файл

@ -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())
}
}
}

Просмотреть файл

@ -38,9 +38,26 @@ func NewFile(fd uintptr, name string) *File {
return &File{&file{stdioFileHandle(fd), name}} 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) { 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) { func (f stdioFileHandle) ReadAt(b []byte, off int64) (n int, err error) {
@ -78,6 +95,15 @@ func (f stdioFileHandle) Fd() uintptr {
//go:linkname putchar runtime.putchar //go:linkname putchar runtime.putchar
func putchar(c byte) 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) { func Pipe() (r *File, w *File, err error) {
return nil, nil, ErrNotImplemented return nil, nil, ErrNotImplemented
} }

Просмотреть файл

@ -14,6 +14,16 @@ func putchar(c byte) {
// dummy, TODO // dummy, TODO
} }
func getchar() byte {
// dummy, TODO
return 0
}
func buffered() int {
// dummy, TODO
return 0
}
//go:extern _sbss //go:extern _sbss
var _sbss [0]byte var _sbss [0]byte

Просмотреть файл

@ -16,6 +16,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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 // 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 // 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 // (0=16ms, 1=32ms, 2=64ms...). Note that the WDT is not very accurate: it can

Просмотреть файл

@ -39,6 +39,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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() { func initClocks() {
// Set 1 Flash Wait State for 48MHz, required for 3.3V operation according to SAMD21 Datasheet // 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) sam.NVMCTRL.CTRLB.SetBits(sam.NVMCTRL_CTRLB_RWS_HALF << sam.NVMCTRL_CTRLB_RWS_Pos)

Просмотреть файл

@ -39,6 +39,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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() { func initClocks() {
// set flash wait state // set flash wait state
sam.NVMCTRL.CTRLA.SetBits(0 << sam.NVMCTRL_CTRLA_RWS_Pos) sam.NVMCTRL.CTRLA.SetBits(0 << sam.NVMCTRL_CTRLA_RWS_Pos)

Просмотреть файл

@ -14,6 +14,16 @@ func putchar(c byte) {
// UART is not supported. // 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) { func sleepWDT(period uint8) {
// TODO: use the watchdog timer instead of a busy loop. // TODO: use the watchdog timer instead of a busy loop.
for i := 0x45; i != 0; i-- { for i := 0x45; i != 0; i-- {

Просмотреть файл

@ -49,6 +49,16 @@ func putchar(c byte) {
stdoutWrite.Set(uint8(c)) stdoutWrite.Set(uint8(c))
} }
func getchar() byte {
// dummy, TODO
return 0
}
func buffered() int {
// dummy, TODO
return 0
}
func waitForEvents() { func waitForEvents() {
arm.Asm("wfe") arm.Asm("wfe")
} }

Просмотреть файл

@ -15,6 +15,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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. // Initialize .bss: zero-initialized global variables.
// The .data section has already been loaded by the ROM bootloader. // The .data section has already been loaded by the ROM bootloader.
func clearbss() { func clearbss() {

Просмотреть файл

@ -18,6 +18,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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?). // Write to the internal control bus (using I2C?).
// Signature found here: // Signature found here:
// https://github.com/espressif/ESP8266_RTOS_SDK/blob/14171de0/components/esp8266/include/esp8266/rom_functions.h#L54 // https://github.com/espressif/ESP8266_RTOS_SDK/blob/14171de0/components/esp8266/include/esp8266/rom_functions.h#L54

Просмотреть файл

@ -99,6 +99,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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 var timerWakeup volatile.Register8
func ticks() timeUnit { func ticks() timeUnit {

Просмотреть файл

@ -111,6 +111,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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 var timerWakeup volatile.Register8
func ticks() timeUnit { func ticks() timeUnit {

Просмотреть файл

@ -126,6 +126,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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) { func exit(code int) {
abort() abort()
} }

Просмотреть файл

@ -66,6 +66,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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) { func sleepTicks(d timeUnit) {
for d != 0 { for d != 0 {
ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side) ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side)

Просмотреть файл

@ -231,6 +231,16 @@ func putchar(c byte) {
machine.PutcharUART(machine.UART0, c) machine.PutcharUART(machine.UART0, c)
} }
func getchar() byte {
// dummy, TODO
return 0
}
func buffered() int {
// dummy, TODO
return 0
}
func exit(code int) { func exit(code int) {
abort() abort()
} }

Просмотреть файл

@ -58,6 +58,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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()
}
// machineInit is provided by package machine. // machineInit is provided by package machine.
func machineInit() func machineInit()

Просмотреть файл

@ -20,6 +20,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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()
}
// initCLK sets clock to 72MHz using HSE 8MHz crystal w/ PLL X 9 (8MHz x 9 = 72MHz). // initCLK sets clock to 72MHz using HSE 8MHz crystal w/ PLL X 9 (8MHz x 9 = 72MHz).
func initCLK() { func initCLK() {
stm32.FLASH.ACR.SetBits(stm32.FLASH_ACR_LATENCY_WS2) // Two wait states, per datasheet stm32.FLASH.ACR.SetBits(stm32.FLASH_ACR_LATENCY_WS2) // Two wait states, per datasheet

Просмотреть файл

@ -21,6 +21,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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 initCLK() { func initCLK() {
// Reset clock registers // Reset clock registers
// Set HSION // Set HSION

Просмотреть файл

@ -163,3 +163,15 @@ func initCOM() {
func putchar(c byte) { func putchar(c byte) {
machine.Serial.WriteByte(c) 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()
}

Просмотреть файл

@ -38,6 +38,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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 initCLK() { func initCLK() {
// PWR_CLK_ENABLE // PWR_CLK_ENABLE
stm32.RCC.APB1ENR.SetBits(stm32.RCC_APB1ENR_PWREN) stm32.RCC.APB1ENR.SetBits(stm32.RCC_APB1ENR_PWREN)

Просмотреть файл

@ -16,6 +16,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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 initCLK() { func initCLK() {
// Set Power Regulator to enable max performance (1.8V) // Set Power Regulator to enable max performance (1.8V)
stm32.PWR.CR.ReplaceBits(1<<stm32.PWR_CR_VOS_Pos, stm32.PWR_CR_VOS_Msk, 0) stm32.PWR.CR.ReplaceBits(1<<stm32.PWR_CR_VOS_Pos, stm32.PWR_CR_VOS_Msk, 0)

Просмотреть файл

@ -47,6 +47,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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 initCLK() { func initCLK() {
// PWR_CLK_ENABLE // PWR_CLK_ENABLE
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_PWREN) stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_PWREN)

Просмотреть файл

@ -39,6 +39,18 @@ func putchar(c byte) {
machine.Serial.WriteByte(c) 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 initCLK() { func initCLK() {
// PWR_CLK_ENABLE // PWR_CLK_ENABLE

Просмотреть файл

@ -102,3 +102,15 @@ func initCLK() {
func putchar(c byte) { func putchar(c byte) {
machine.Serial.WriteByte(c) 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()
}

Просмотреть файл

@ -55,6 +55,16 @@ func putchar(c byte) {
stdoutWrite.Set(uint8(c)) stdoutWrite.Set(uint8(c))
} }
func getchar() byte {
// dummy, TODO
return 0
}
func buffered() int {
// dummy, TODO
return 0
}
func abort() { func abort() {
exit(1) exit(1)
} }