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.
This commit is contained in:
Ayke van Laethem
2020-08-31 14:28:31 +02:00
committed by Ron Evans
parent 0e6d2af028
commit 753162f4e0
4 changed files with 57 additions and 2 deletions
+6
View File
@@ -0,0 +1,6 @@
// +build esp32_wroom_32
package machine
// Blue LED on the ESP32-WROOM-32 module.
const LED = Pin(2)
+46 -1
View File
@@ -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<<p) != 0
} else {
return esp.GPIO.IN1.Get()&(1<<(p-32)) != 0
}
}
var (
UART0 = UART{Bus: esp.UART0, Buffer: NewRingBuffer()}