mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 19:43:44 +00:00
Add core support for multiple UARTs (#152)
* machine/uart: add core support for multiple UARTs by allowing for multiple RingBuffers * machine/uart: complete core support for multiple UARTs * machine/uart: no need to store pointer to UART, better to treat like I2C and SPI * machine/uart: increase ring buffer size to 128 bytes * machine/uart: improve godocs comments and use comma-ok idiom for buffer Put/Get methods
This commit is contained in:
@@ -7,27 +7,34 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// change these to test a different UART or pins if available
|
||||
var (
|
||||
uart = machine.UART0
|
||||
tx uint8 = machine.UART_TX_PIN
|
||||
rx uint8 = machine.UART_RX_PIN
|
||||
)
|
||||
|
||||
func main() {
|
||||
machine.UART0.Configure(machine.UARTConfig{})
|
||||
machine.UART0.Write([]byte("Echo console enabled. Type something then press enter:\r\n"))
|
||||
uart.Configure(machine.UARTConfig{TX: tx, RX: rx})
|
||||
uart.Write([]byte("Echo console enabled. Type something then press enter:\r\n"))
|
||||
|
||||
input := make([]byte, 64)
|
||||
i := 0
|
||||
for {
|
||||
if machine.UART0.Buffered() > 0 {
|
||||
data, _ := machine.UART0.ReadByte()
|
||||
if uart.Buffered() > 0 {
|
||||
data, _ := uart.ReadByte()
|
||||
|
||||
switch data {
|
||||
case 13:
|
||||
// return key
|
||||
machine.UART0.Write([]byte("\r\n"))
|
||||
machine.UART0.Write([]byte("You typed: "))
|
||||
machine.UART0.Write(input[:i])
|
||||
machine.UART0.Write([]byte("\r\n"))
|
||||
uart.Write([]byte("\r\n"))
|
||||
uart.Write([]byte("You typed: "))
|
||||
uart.Write(input[:i])
|
||||
uart.Write([]byte("\r\n"))
|
||||
i = 0
|
||||
default:
|
||||
// just echo the character
|
||||
machine.UART0.WriteByte(data)
|
||||
uart.WriteByte(data)
|
||||
input[i] = data
|
||||
i++
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user