From 36dffb55545b9e64ca53b408a9343a7a56bbbbc1 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 29 May 2021 10:41:19 +0200 Subject: [PATCH] machine/rp2040: add support for GPIO input --- src/machine/machine_rp2040_gpio.go | 32 +++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/machine/machine_rp2040_gpio.go b/src/machine/machine_rp2040_gpio.go index 1d35254c..b82e0435 100644 --- a/src/machine/machine_rp2040_gpio.go +++ b/src/machine/machine_rp2040_gpio.go @@ -63,6 +63,9 @@ const ( const ( PinOutput PinMode = iota + PinInput + PinInputPulldown + PinInputPullup ) // set drives the pin high @@ -83,6 +86,11 @@ func (p Pin) xor() { rp.SIO.GPIO_OUT_XOR.Set(mask) } +// get returns the pin value +func (p Pin) get() bool { + return rp.SIO.GPIO_IN.HasBits(uint32(1) << p) +} + func (p Pin) ioCtrl() *volatile.Register32 { return &ioBank0.io[p].ctrl } @@ -91,6 +99,16 @@ func (p Pin) padCtrl() *volatile.Register32 { return &padsBank0.io[p] } +func (p Pin) pullup() { + p.padCtrl().SetBits(rp.PADS_BANK0_GPIO0_PUE) + p.padCtrl().ClearBits(rp.PADS_BANK0_GPIO0_PDE) +} + +func (p Pin) pulldown() { + p.padCtrl().SetBits(rp.PADS_BANK0_GPIO0_PDE) + p.padCtrl().ClearBits(rp.PADS_BANK0_GPIO0_PUE) +} + // setFunc will set pin function to fn. func (p Pin) setFunc(fn pinFunc) { // Set input enable, Clear output disable @@ -108,7 +126,6 @@ func (p Pin) init() { rp.SIO.GPIO_OE_CLR.Set(mask) p.clr() p.setFunc(fnSIO) - } // Configure configures the gpio pin as per mode. @@ -118,6 +135,14 @@ func (p Pin) Configure(config PinConfig) { switch config.Mode { case PinOutput: rp.SIO.GPIO_OE_SET.Set(mask) + case PinInput: + rp.SIO.GPIO_OE_CLR.Set(mask) + case PinInputPulldown: + rp.SIO.GPIO_OE_CLR.Set(mask) + p.pulldown() + case PinInputPullup: + rp.SIO.GPIO_OE_CLR.Set(mask) + p.pullup() } } @@ -129,3 +154,8 @@ func (p Pin) Set(value bool) { p.clr() } } + +// Get reads the pin value. +func (p Pin) Get() bool { + return p.get() +}