mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-29 08:08:42 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3591e194f7 | |||
| cc3009b5c1 |
@@ -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
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
+28
-2
@@ -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) {
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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-- {
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -45,6 +45,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()
|
||||
}
|
||||
|
||||
// machineInit is provided by package machine.
|
||||
func machineInit()
|
||||
|
||||
|
||||
@@ -20,6 +20,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()
|
||||
}
|
||||
|
||||
// initCLK sets clock to 72MHz using HSE 8MHz crystal w/ PLL X 9 (8MHz x 9 = 72MHz).
|
||||
func initCLK() {
|
||||
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)
|
||||
}
|
||||
|
||||
func getchar() byte {
|
||||
for machine.Serial.Buffered() == 0 {
|
||||
Gosched()
|
||||
}
|
||||
v, _ := machine.Serial.ReadByte()
|
||||
return v
|
||||
}
|
||||
|
||||
func buffered() int {
|
||||
return machine.Serial.Buffered()
|
||||
}
|
||||
|
||||
func initCLK() {
|
||||
// Reset clock registers
|
||||
// Set HSION
|
||||
|
||||
@@ -163,3 +163,15 @@ func initCOM() {
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -38,6 +38,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 initCLK() {
|
||||
// PWR_CLK_ENABLE
|
||||
stm32.RCC.APB1ENR.SetBits(stm32.RCC_APB1ENR_PWREN)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
func initCLK() {
|
||||
// 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)
|
||||
|
||||
@@ -47,6 +47,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 initCLK() {
|
||||
// PWR_CLK_ENABLE
|
||||
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_PWREN)
|
||||
|
||||
@@ -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 initCLK() {
|
||||
|
||||
// PWR_CLK_ENABLE
|
||||
|
||||
@@ -102,3 +102,15 @@ func initCLK() {
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -55,6 +55,16 @@ func putchar(c byte) {
|
||||
stdoutWrite.Set(uint8(c))
|
||||
}
|
||||
|
||||
func getchar() byte {
|
||||
// dummy, TODO
|
||||
return 0
|
||||
}
|
||||
|
||||
func buffered() int {
|
||||
// dummy, TODO
|
||||
return 0
|
||||
}
|
||||
|
||||
func abort() {
|
||||
exit(1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user