Skip to content

Commit ea637b7

Browse files
committed
targets: add Pin interface implementation for GPIO on TKey
Signed-off-by: deadprogram <[email protected]>
1 parent 4be20f7 commit ea637b7

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/device/tkey/tkey.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,9 @@ const (
131131
TK1_MMIO_TK1_LED_R_BIT = 2
132132
TK1_MMIO_TK1_LED_G_BIT = 1
133133
TK1_MMIO_TK1_LED_B_BIT = 0
134+
135+
TK1_MMIO_TK1_GPIO1_BIT = 0
136+
TK1_MMIO_TK1_GPIO2_BIT = 1
137+
TK1_MMIO_TK1_GPIO3_BIT = 2
138+
TK1_MMIO_TK1_GPIO4_BIT = 3
134139
)

src/machine/machine_tkey.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,31 @@ const (
2727

2828
TKEY_TOUCH = Pin(3) // 3 is unused, but we need a value here to match the Pin interface.
2929
BUTTON = TKEY_TOUCH
30+
31+
GPIO1 = Pin(tkey.TK1_MMIO_TK1_GPIO1_BIT + 8)
32+
GPIO2 = Pin(tkey.TK1_MMIO_TK1_GPIO2_BIT + 8)
33+
GPIO3 = Pin(tkey.TK1_MMIO_TK1_GPIO3_BIT + 8)
34+
GPIO4 = Pin(tkey.TK1_MMIO_TK1_GPIO4_BIT + 8)
3035
)
3136

32-
var buttonConfig PinConfig
37+
var touchConfig, gpio1Config, gpio2Config PinConfig
3338

3439
// No config needed for TKey, just to match the Pin interface.
3540
func (p Pin) Configure(config PinConfig) {
3641
switch p {
3742
case BUTTON:
38-
buttonConfig = config
43+
touchConfig = config
3944

4045
// Clear any pending touch events.
4146
tkey.TOUCH.STATUS.Set(0)
47+
case GPIO1:
48+
gpio1Config = config
49+
case GPIO2:
50+
gpio2Config = config
4251
}
4352
}
4453

45-
// Set GPIO pin to high or low.
54+
// Set pin to high or low.
4655
func (p Pin) Set(high bool) {
4756
switch p {
4857
case LED_BLUE, LED_GREEN, LED_RED:
@@ -51,26 +60,46 @@ func (p Pin) Set(high bool) {
5160
} else {
5261
tkey.TK1.LED.ClearBits(1 << uint(p))
5362
}
63+
case GPIO3, GPIO4:
64+
if high {
65+
tkey.TK1.GPIO.SetBits(1 << uint(p-8))
66+
} else {
67+
tkey.TK1.GPIO.ClearBits(1 << uint(p-8))
68+
}
5469
}
5570
}
5671

72+
// Get returns the current value of a pin.
5773
func (p Pin) Get() bool {
74+
pushed := false
75+
mode := PinInput
76+
5877
switch p {
5978
case BUTTON:
60-
pushed := false
79+
mode = touchConfig.Mode
6180
if tkey.TOUCH.STATUS.HasBits(1) {
6281
tkey.TOUCH.STATUS.Set(0)
6382
pushed = true
6483
}
65-
66-
switch buttonConfig.Mode {
67-
case PinInputPullup:
68-
return !pushed
69-
case PinInput, PinInputPulldown:
70-
return pushed
84+
case GPIO1:
85+
mode = gpio1Config.Mode
86+
if tkey.TK1.GPIO.HasBits(1 << uint(p-8)) {
87+
pushed = true
88+
}
89+
case GPIO2:
90+
mode = gpio2Config.Mode
91+
if tkey.TK1.GPIO.HasBits(1 << uint(p-8)) {
92+
pushed = true
7193
}
7294
}
7395

96+
switch mode {
97+
case PinInputPullup:
98+
return !pushed
99+
case PinInput, PinInputPulldown:
100+
return pushed
101+
}
102+
74103
return false
75104
}
76105

0 commit comments

Comments
 (0)