Skip to content

Commit 19d5e05

Browse files
aykevldeadprogram
authored andcommitted
esp32: configure the I/O matrix for GPIO pins
Only some pins (notably including GPIO2 aka machine.LED) have GPIO for the default function 1. Other pins (such as GPIO 15) had a different function by default. Function 3 means GPIO for all the pins, so always use that when configuring a pin to use as a GPIO pin. In the future, the mux configuration will need to be updated for other functions such as SPI, I2C, etc.
1 parent 9e599ba commit 19d5e05

File tree

1 file changed

+110
-2
lines changed

1 file changed

+110
-2
lines changed

src/machine/machine_esp32.go

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,43 @@ type PinMode uint8
2020
const (
2121
PinOutput PinMode = iota
2222
PinInput
23+
PinInputPullup
24+
PinInputPulldown
2325
)
2426

2527
// Configure this pin with the given configuration.
2628
func (p Pin) Configure(config PinConfig) {
27-
if config.Mode == PinOutput {
29+
var muxConfig uint32 // The mux configuration.
30+
31+
// Configure this pin as a GPIO pin.
32+
const function = 3 // function 3 is GPIO for every pin
33+
muxConfig |= (function - 1) << esp.IO_MUX_GPIO0_MCU_SEL_Pos
34+
35+
// Make this pin an input pin (always).
36+
muxConfig |= esp.IO_MUX_GPIO0_FUN_IE
37+
38+
// Set drive strength: 0 is lowest, 3 is highest.
39+
muxConfig |= 2 << esp.IO_MUX_GPIO0_FUN_DRV_Pos
40+
41+
// Select pull mode.
42+
if config.Mode == PinInputPullup {
43+
muxConfig |= esp.IO_MUX_GPIO0_FUN_WPU
44+
} else if config.Mode == PinInputPulldown {
45+
muxConfig |= esp.IO_MUX_GPIO0_FUN_WPD
46+
}
47+
48+
// Configure the pad with the given IO mux configuration.
49+
p.mux().Set(muxConfig)
50+
51+
switch config.Mode {
52+
case PinOutput:
2853
// Set the 'output enable' bit.
2954
if p < 32 {
3055
esp.GPIO.ENABLE_W1TS.Set(1 << p)
3156
} else {
3257
esp.GPIO.ENABLE1_W1TS.Set(1 << (p - 32))
3358
}
34-
} else {
59+
case PinInput, PinInputPullup, PinInputPulldown:
3560
// Clear the 'output enable' bit.
3661
if p < 32 {
3762
esp.GPIO.ENABLE_W1TC.Set(1 << p)
@@ -97,6 +122,89 @@ func (p Pin) Get() bool {
97122
}
98123
}
99124

125+
// mux returns the I/O mux configuration register corresponding to the given
126+
// GPIO pin.
127+
func (p Pin) mux() *volatile.Register32 {
128+
// I have no idea whether there is any pattern in the GPIO <-> pad mapping.
129+
// I couldn't find it.
130+
switch p {
131+
case 36:
132+
return &esp.IO_MUX.GPIO36
133+
case 37:
134+
return &esp.IO_MUX.GPIO37
135+
case 38:
136+
return &esp.IO_MUX.GPIO38
137+
case 39:
138+
return &esp.IO_MUX.GPIO39
139+
case 34:
140+
return &esp.IO_MUX.GPIO34
141+
case 35:
142+
return &esp.IO_MUX.GPIO35
143+
case 32:
144+
return &esp.IO_MUX.GPIO32
145+
case 33:
146+
return &esp.IO_MUX.GPIO33
147+
case 25:
148+
return &esp.IO_MUX.GPIO25
149+
case 26:
150+
return &esp.IO_MUX.GPIO26
151+
case 27:
152+
return &esp.IO_MUX.GPIO27
153+
case 14:
154+
return &esp.IO_MUX.MTMS
155+
case 12:
156+
return &esp.IO_MUX.MTDI
157+
case 13:
158+
return &esp.IO_MUX.MTCK
159+
case 15:
160+
return &esp.IO_MUX.MTDO
161+
case 2:
162+
return &esp.IO_MUX.GPIO2
163+
case 0:
164+
return &esp.IO_MUX.GPIO0
165+
case 4:
166+
return &esp.IO_MUX.GPIO4
167+
case 16:
168+
return &esp.IO_MUX.GPIO16
169+
case 17:
170+
return &esp.IO_MUX.GPIO17
171+
case 9:
172+
return &esp.IO_MUX.SD_DATA2
173+
case 10:
174+
return &esp.IO_MUX.SD_DATA3
175+
case 11:
176+
return &esp.IO_MUX.SD_CMD
177+
case 6:
178+
return &esp.IO_MUX.SD_CLK
179+
case 7:
180+
return &esp.IO_MUX.SD_DATA0
181+
case 8:
182+
return &esp.IO_MUX.SD_DATA1
183+
case 5:
184+
return &esp.IO_MUX.GPIO5
185+
case 18:
186+
return &esp.IO_MUX.GPIO18
187+
case 19:
188+
return &esp.IO_MUX.GPIO19
189+
case 20:
190+
return &esp.IO_MUX.GPIO20
191+
case 21:
192+
return &esp.IO_MUX.GPIO21
193+
case 22:
194+
return &esp.IO_MUX.GPIO22
195+
case 3:
196+
return &esp.IO_MUX.U0RXD
197+
case 1:
198+
return &esp.IO_MUX.U0TXD
199+
case 23:
200+
return &esp.IO_MUX.GPIO23
201+
case 24:
202+
return &esp.IO_MUX.GPIO24
203+
default:
204+
return nil
205+
}
206+
}
207+
100208
var (
101209
UART0 = UART{Bus: esp.UART0, Buffer: NewRingBuffer()}
102210
UART1 = UART{Bus: esp.UART1, Buffer: NewRingBuffer()}

0 commit comments

Comments
 (0)