From 753162f4e06e4bcb664f18de9c884812c3be0ffc Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 31 Aug 2020 14:28:31 +0200 Subject: [PATCH] esp32: add support for basic GPIO GPIO is much more advanced on the ESP32, but this is a starting point. It gets examples/blinky1 to work. --- Makefile | 2 +- src/machine/board_esp32-wroom-32.go | 6 ++++ src/machine/machine_esp32.go | 47 ++++++++++++++++++++++++++++- targets/esp32-wroom-32.json | 4 +++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/machine/board_esp32-wroom-32.go create mode 100644 targets/esp32-wroom-32.json diff --git a/Makefile b/Makefile index abfa058e..efe25b27 100644 --- a/Makefile +++ b/Makefile @@ -347,7 +347,7 @@ ifneq ($(AVR), 0) @$(MD5SUM) test.hex endif ifneq ($(XTENSA), 0) - $(TINYGO) build -size short -o test.bin -target=esp32 examples/serial + $(TINYGO) build -size short -o test.bin -target=esp32-wroom-32 examples/blinky1 endif $(TINYGO) build -size short -o test.hex -target=hifive1b examples/blinky1 @$(MD5SUM) test.hex diff --git a/src/machine/board_esp32-wroom-32.go b/src/machine/board_esp32-wroom-32.go new file mode 100644 index 00000000..deb329a4 --- /dev/null +++ b/src/machine/board_esp32-wroom-32.go @@ -0,0 +1,6 @@ +// +build esp32_wroom_32 + +package machine + +// Blue LED on the ESP32-WROOM-32 module. +const LED = Pin(2) diff --git a/src/machine/machine_esp32.go b/src/machine/machine_esp32.go index 9501f472..4bdbed6a 100644 --- a/src/machine/machine_esp32.go +++ b/src/machine/machine_esp32.go @@ -13,7 +13,52 @@ const ( PinInput ) -func (p Pin) Set(value bool) +// Configure this pin with the given configuration. +func (p Pin) Configure(config PinConfig) { + if config.Mode == PinOutput { + // Set the 'output enable' bit. + if p < 32 { + esp.GPIO.ENABLE_W1TS.Set(1 << p) + } else { + esp.GPIO.ENABLE1_W1TS.Set(1 << (p - 32)) + } + } else { + // Clear the 'output enable' bit. + if p < 32 { + esp.GPIO.ENABLE_W1TC.Set(1 << p) + } else { + esp.GPIO.ENABLE1_W1TC.Set(1 << (p - 32)) + } + } +} + +// Set the pin to high or low. +// Warning: only use this on an output pin! +func (p Pin) Set(value bool) { + if value { + if p < 32 { + esp.GPIO.OUT_W1TS.Set(1 << p) + } else { + esp.GPIO.OUT1_W1TS.Set(1 << (p - 32)) + } + } else { + if p < 32 { + esp.GPIO.OUT_W1TC.Set(1 << p) + } else { + esp.GPIO.OUT1_W1TC.Set(1 << (p - 32)) + } + } +} + +// Get returns the current value of a GPIO pin when the pin is configured as an +// input. +func (p Pin) Get() bool { + if p < 32 { + return esp.GPIO.IN.Get()&(1<