Skip to content

Commit f5d0ec9

Browse files
kenbelldeadprogram
authored andcommitted
esp32: configure uarts to specified pins and yield waiting for buffer
1 parent 3a0d724 commit f5d0ec9

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

src/machine/machine_esp32.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,30 +295,64 @@ var DefaultUART = UART0
295295

296296
var (
297297
UART0 = &_UART0
298-
_UART0 = UART{Bus: esp.UART0, Buffer: NewRingBuffer()}
298+
_UART0 = UART{
299+
Bus: esp.UART0,
300+
Buffer: NewRingBuffer(),
301+
TXRXSignal: 14,
302+
RTSCTSSignal: 15,
303+
}
299304
UART1 = &_UART1
300-
_UART1 = UART{Bus: esp.UART1, Buffer: NewRingBuffer()}
305+
_UART1 = UART{
306+
Bus: esp.UART1,
307+
Buffer: NewRingBuffer(),
308+
TXRXSignal: 17,
309+
RTSCTSSignal: 18,
310+
}
301311
UART2 = &_UART2
302-
_UART2 = UART{Bus: esp.UART2, Buffer: NewRingBuffer()}
312+
_UART2 = UART{
313+
Bus: esp.UART2,
314+
Buffer: NewRingBuffer(),
315+
TXRXSignal: 198,
316+
RTSCTSSignal: 199,
317+
}
303318
)
304319

305320
type UART struct {
306-
Bus *esp.UART_Type
307-
Buffer *RingBuffer
321+
Bus *esp.UART_Type
322+
Buffer *RingBuffer
323+
TXRXSignal uint32
324+
RTSCTSSignal uint32
308325
}
309326

310327
func (uart *UART) Configure(config UARTConfig) {
311328
if config.BaudRate == 0 {
312329
config.BaudRate = 115200
313330
}
314331
uart.Bus.CLKDIV.Set(peripheralClock / config.BaudRate)
332+
333+
if config.RX != NoPin {
334+
config.RX.configure(PinConfig{Mode: PinInputPullup}, uart.TXRXSignal)
335+
}
336+
337+
if config.TX != NoPin {
338+
config.TX.configure(PinConfig{Mode: PinOutput}, uart.TXRXSignal)
339+
}
340+
341+
if config.RTS != NoPin {
342+
config.RTS.configure(PinConfig{Mode: PinOutput}, uart.RTSCTSSignal)
343+
}
344+
345+
if config.CTS != NoPin {
346+
config.CTS.configure(PinConfig{Mode: PinInputPullup}, uart.RTSCTSSignal)
347+
}
315348
}
316349

317350
func (uart *UART) writeByte(b byte) error {
318351
for (uart.Bus.STATUS.Get()>>16)&0xff >= 128 {
319352
// Read UART_TXFIFO_CNT from the status register, which indicates how
320353
// many bytes there are in the transmit buffer. Wait until there are
321354
// less than 128 bytes in this buffer (the default buffer size).
355+
gosched()
322356
}
323357
// Write to the TX_FIFO register.
324358
(*volatile.Register8)(unsafe.Add(unsafe.Pointer(uart.Bus), 0x200C0000)).Set(b)

0 commit comments

Comments
 (0)