diff options
author | Ayke van Laethem <[email protected]> | 2019-05-22 17:46:14 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2019-06-28 10:00:14 +0200 |
commit | 4ecd478d828498840080356a8684cce2d61494dc (patch) | |
tree | 16593991bb3a92113dc9bfd8828acd5141e2fce3 | |
parent | 40b193f1faa507e8d6407624c1f00937602b0b89 (diff) | |
download | tinygo-4ecd478d828498840080356a8684cce2d61494dc.tar.gz tinygo-4ecd478d828498840080356a8684cce2d61494dc.zip |
machine: add generic board support on non-baremetal hardware
Instead of trying to modify periperhals directly, external functions are
called. For example, __tinygo_gpio_set sets a GPIO pin to a specified
value (high or low). It is expected that binaries made this way will be
linked with some extra libraries that implement support for these
functions.
One particularly interesting case is this experimental board simulator:
https://github.com/aykevl/tinygo-play
Compiling code to WebAssembly with the correct build tag for a board
will enable this board to be simulated in the browser.
Atmel/Microchip based SAMD boards are not currently supported, because
their I2C/SPI support is somewhat uncommon and harder to support in the
machine API. They may require a modification to the machine API for
proper support.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/machine/board_arduino.go | 2 | ||||
-rw-r--r-- | src/machine/board_bluepill.go | 2 | ||||
-rw-r--r-- | src/machine/board_digispark.go | 2 | ||||
-rw-r--r-- | src/machine/board_microbit.go | 2 | ||||
-rw-r--r-- | src/machine/board_pca10031.go | 2 | ||||
-rw-r--r-- | src/machine/board_pca10040.go | 2 | ||||
-rw-r--r-- | src/machine/board_stm32.go | 16 | ||||
-rw-r--r-- | src/machine/board_stm32f4disco.go | 2 | ||||
-rw-r--r-- | src/machine/i2c.go | 2 | ||||
-rw-r--r-- | src/machine/machine_dummy.go | 40 | ||||
-rw-r--r-- | src/machine/machine_generic.go | 184 | ||||
-rw-r--r-- | src/machine/machine_stm32.go | 11 | ||||
-rw-r--r-- | src/machine/spi.go | 2 |
14 files changed, 210 insertions, 61 deletions
@@ -107,7 +107,7 @@ smoketest-no-avr: tinygo build -size short -o test.elf -target=pca10040 examples/serial tinygo build -size short -o test.elf -target=pca10040 examples/test # test all targets/boards - tinygo build -o test.elf examples/blinky2 # TODO: re-enable -size flag with MachO support + tinygo build -o test.wasm -tags=pca10040 examples/blinky2 tinygo build -size short -o test.elf -target=microbit examples/echo tinygo build -size short -o test.elf -target=nrf52840-mdk examples/blinky1 tinygo build -size short -o test.elf -target=pca10031 examples/blinky1 diff --git a/src/machine/board_arduino.go b/src/machine/board_arduino.go index d64a9de3d..b3582b5a9 100644 --- a/src/machine/board_arduino.go +++ b/src/machine/board_arduino.go @@ -1,4 +1,4 @@ -// +build avr,arduino +// +build arduino package machine diff --git a/src/machine/board_bluepill.go b/src/machine/board_bluepill.go index b950c9356..7ec731472 100644 --- a/src/machine/board_bluepill.go +++ b/src/machine/board_bluepill.go @@ -1,4 +1,4 @@ -// +build stm32,bluepill +// +build bluepill package machine diff --git a/src/machine/board_digispark.go b/src/machine/board_digispark.go index 1fbd13a56..9a483d50c 100644 --- a/src/machine/board_digispark.go +++ b/src/machine/board_digispark.go @@ -1,4 +1,4 @@ -// +build attiny85,digispark +// +build digispark package machine diff --git a/src/machine/board_microbit.go b/src/machine/board_microbit.go index ca9b00c8b..888c7c963 100644 --- a/src/machine/board_microbit.go +++ b/src/machine/board_microbit.go @@ -1,4 +1,4 @@ -// +build nrf51,microbit +// +build microbit package machine diff --git a/src/machine/board_pca10031.go b/src/machine/board_pca10031.go index 9627918c1..b2b37c69c 100644 --- a/src/machine/board_pca10031.go +++ b/src/machine/board_pca10031.go @@ -1,4 +1,4 @@ -// +build nrf51,pca10031 +// +build pca10031 // pca10031 is a nrf51 based dongle, intended for use in wireless applications. // diff --git a/src/machine/board_pca10040.go b/src/machine/board_pca10040.go index 7ac47582b..f00e357b6 100644 --- a/src/machine/board_pca10040.go +++ b/src/machine/board_pca10040.go @@ -1,4 +1,4 @@ -// +build nrf,pca10040 +// +build pca10040 package machine diff --git a/src/machine/board_stm32.go b/src/machine/board_stm32.go new file mode 100644 index 000000000..02ab51a65 --- /dev/null +++ b/src/machine/board_stm32.go @@ -0,0 +1,16 @@ +// +build bluepill stm32f4disco + +package machine + +// Peripheral abstraction layer for the stm32. + +const ( + portA Pin = iota * 16 + portB + portC + portD + portE + portF + portG + portH +) diff --git a/src/machine/board_stm32f4disco.go b/src/machine/board_stm32f4disco.go index 05150caba..36c173b29 100644 --- a/src/machine/board_stm32f4disco.go +++ b/src/machine/board_stm32f4disco.go @@ -1,4 +1,4 @@ -// +build stm32,stm32f4disco +// +build stm32f4disco package machine diff --git a/src/machine/i2c.go b/src/machine/i2c.go index c3dfe24c3..bd6c1dc2c 100644 --- a/src/machine/i2c.go +++ b/src/machine/i2c.go @@ -1,4 +1,4 @@ -// +build avr nrf sam stm32f103xx +// +build !stm32f4disco package machine diff --git a/src/machine/machine_dummy.go b/src/machine/machine_dummy.go deleted file mode 100644 index 11a4c9a8f..000000000 --- a/src/machine/machine_dummy.go +++ /dev/null @@ -1,40 +0,0 @@ -// +build !avr,!nrf,!sam,!stm32 - -package machine - -// Dummy machine package, filled with no-ops. - -type PinMode uint8 - -const ( - PinInput PinMode = iota - PinOutput -) - -// Fake LED numbers, for testing. -const ( - LED Pin = LED1 - LED1 Pin = 0 - LED2 Pin = 0 - LED3 Pin = 0 - LED4 Pin = 0 -) - -// Fake button numbers, for testing. -const ( - BUTTON Pin = BUTTON1 - BUTTON1 Pin = 0 - BUTTON2 Pin = 0 - BUTTON3 Pin = 0 - BUTTON4 Pin = 0 -) - -func (p Pin) Configure(config PinConfig) { -} - -func (p Pin) Set(value bool) { -} - -func (p Pin) Get() bool { - return false -} diff --git a/src/machine/machine_generic.go b/src/machine/machine_generic.go new file mode 100644 index 000000000..ccc3e2b71 --- /dev/null +++ b/src/machine/machine_generic.go @@ -0,0 +1,184 @@ +// +build !avr,!nrf,!sam,!stm32 + +package machine + +// Dummy machine package that calls out to external functions. + +var ( + SPI0 = SPI{0} + I2C0 = I2C{0} + UART0 = UART{0} +) + +type PinMode uint8 + +const ( + PinInput PinMode = iota + PinOutput + PinInputPullup + PinInputPulldown +) + +func (p Pin) Configure(config PinConfig) { + gpioConfigure(p, config) +} + +func (p Pin) Set(value bool) { + gpioSet(p, value) +} + +func (p Pin) Get() bool { + return gpioGet(p) +} + +//go:export __tinygo_gpio_configure +func gpioConfigure(pin Pin, config PinConfig) + +//go:export __tinygo_gpio_set +func gpioSet(pin Pin, value bool) + +//go:export __tinygo_gpio_get +func gpioGet(pin Pin) bool + +type SPI struct { + Bus uint8 +} + +type SPIConfig struct { + Frequency uint32 + SCK Pin + MOSI Pin + MISO Pin + Mode uint8 +} + +func (spi SPI) Configure(config SPIConfig) { + spiConfigure(spi.Bus, config.SCK, config.MOSI, config.MISO) +} + +// Transfer writes/reads a single byte using the SPI interface. +func (spi SPI) Transfer(w byte) (byte, error) { + return spiTransfer(spi.Bus, w), nil +} + +//go:export __tinygo_spi_configure +func spiConfigure(bus uint8, sck Pin, mosi Pin, miso Pin) + +//go:export __tinygo_spi_transfer +func spiTransfer(bus uint8, w uint8) uint8 + +// InitADC enables support for ADC peripherals. +func InitADC() { + // Nothing to do here. +} + +// Configure configures an ADC pin to be able to be used to read data. +func (adc ADC) Configure() { +} + +// Get reads the current analog value from this ADC peripheral. +func (adc ADC) Get() uint16 { + return adcRead(adc.Pin) +} + +//go:export __tinygo_adc_read +func adcRead(pin Pin) uint16 + +// InitPWM enables support for PWM peripherals. +func InitPWM() { + // Nothing to do here. +} + +// Configure configures a PWM pin for output. +func (pwm PWM) Configure() { +} + +// Set turns on the duty cycle for a PWM pin using the provided value. +func (pwm PWM) Set(value uint16) { + pwmSet(pwm.Pin, value) +} + +//go:export __tinygo_pwm_set +func pwmSet(pin Pin, value uint16) + +// I2C is a generic implementation of the Inter-IC communication protocol. +type I2C struct { + Bus uint8 +} + +// I2CConfig is used to store config info for I2C. +type I2CConfig struct { + Frequency uint32 + SCL Pin + SDA Pin +} + +// Configure is intended to setup the I2C interface. +func (i2c I2C) Configure(config I2CConfig) { + i2cConfigure(i2c.Bus, config.SCL, config.SDA) +} + +// Tx does a single I2C transaction at the specified address. +func (i2c I2C) Tx(addr uint16, w, r []byte) error { + i2cTransfer(i2c.Bus, &w[0], len(w), &r[0], len(r)) + // TODO: do something with the returned error code. + return nil +} + +//go:export __tinygo_i2c_configure +func i2cConfigure(bus uint8, scl Pin, sda Pin) + +//go:export __tinygo_i2c_transfer +func i2cTransfer(bus uint8, w *byte, wlen int, r *byte, rlen int) int + +type UART struct { + Bus uint8 +} + +type UARTConfig struct { + BaudRate uint32 + TX Pin + RX Pin +} + +// Configure the UART. +func (uart UART) Configure(config UARTConfig) { + uartConfigure(uart.Bus, config.TX, config.RX) +} + +// Read from the UART. +func (uart UART) Read(data []byte) (n int, err error) { + return uartRead(uart.Bus, &data[0], len(data)), nil +} + +// Write to the UART. +func (uart UART) Write(data []byte) (n int, err error) { + return uartWrite(uart.Bus, &data[0], len(data)), nil +} + +// Buffered returns the number of bytes currently stored in the RX buffer. +func (uart UART) Buffered() int { + return 0 +} + +// ReadByte reads a single byte from the UART. +func (uart UART) ReadByte() (byte, error) { + var b byte + uartRead(uart.Bus, &b, 1) + return b, nil +} + +// WriteByte writes a single byte to the UART. +func (uart UART) WriteByte(b byte) error { + uartWrite(uart.Bus, &b, 1) + return nil +} + +//go:export __tinygo_uart_configure +func uartConfigure(bus uint8, tx Pin, rx Pin) + +//go:export __tinygo_uart_read +func uartRead(bus uint8, buf *byte, bufLen int) int + +//go:export __tinygo_uart_write +func uartWrite(bus uint8, buf *byte, bufLen int) int diff --git a/src/machine/machine_stm32.go b/src/machine/machine_stm32.go index 4e28d03d8..89fed477f 100644 --- a/src/machine/machine_stm32.go +++ b/src/machine/machine_stm32.go @@ -5,14 +5,3 @@ package machine // Peripheral abstraction layer for the stm32. type PinMode uint8 - -const ( - portA Pin = iota * 16 - portB - portC - portD - portE - portF - portG - portH -) diff --git a/src/machine/spi.go b/src/machine/spi.go index 83f16d5ed..a3e6235f3 100644 --- a/src/machine/spi.go +++ b/src/machine/spi.go @@ -1,4 +1,4 @@ -// +build nrf stm32f103xx atsamd21g18a +// +build !stm32f407,!avr package machine |