machine/rp2040: add support for GPIO input

This commit is contained in:
deadprogram
2021-05-29 10:41:19 +02:00
committed by Ron Evans
parent e8c4c4a865
commit 36dffb5554
+31 -1
View File
@@ -63,6 +63,9 @@ const (
const ( const (
PinOutput PinMode = iota PinOutput PinMode = iota
PinInput
PinInputPulldown
PinInputPullup
) )
// set drives the pin high // set drives the pin high
@@ -83,6 +86,11 @@ func (p Pin) xor() {
rp.SIO.GPIO_OUT_XOR.Set(mask) 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 { func (p Pin) ioCtrl() *volatile.Register32 {
return &ioBank0.io[p].ctrl return &ioBank0.io[p].ctrl
} }
@@ -91,6 +99,16 @@ func (p Pin) padCtrl() *volatile.Register32 {
return &padsBank0.io[p] 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. // setFunc will set pin function to fn.
func (p Pin) setFunc(fn pinFunc) { func (p Pin) setFunc(fn pinFunc) {
// Set input enable, Clear output disable // Set input enable, Clear output disable
@@ -108,7 +126,6 @@ func (p Pin) init() {
rp.SIO.GPIO_OE_CLR.Set(mask) rp.SIO.GPIO_OE_CLR.Set(mask)
p.clr() p.clr()
p.setFunc(fnSIO) p.setFunc(fnSIO)
} }
// Configure configures the gpio pin as per mode. // Configure configures the gpio pin as per mode.
@@ -118,6 +135,14 @@ func (p Pin) Configure(config PinConfig) {
switch config.Mode { switch config.Mode {
case PinOutput: case PinOutput:
rp.SIO.GPIO_OE_SET.Set(mask) 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() p.clr()
} }
} }
// Get reads the pin value.
func (p Pin) Get() bool {
return p.get()
}