Skip to content

Instantly share code, notes, and snippets.

@sago35
Created October 5, 2021 21:16
Show Gist options
  • Save sago35/5edcdc6f38156048e1550f48dbb8405f to your computer and use it in GitHub Desktop.
Save sago35/5edcdc6f38156048e1550f48dbb8405f to your computer and use it in GitHub Desktop.
TinyGo + XIAO + USBCDC + UART (D6/D7)
package main
import (
"machine"
"time"
)
var (
usbcdc = machine.USB
uart = machine.UART1
tx = machine.UART_TX_PIN
rx = machine.UART_RX_PIN
)
func main() {
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 usbcdc.Buffered() > 0 {
data, _ := usbcdc.ReadByte()
switch data {
case 13:
// return key
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
uart.WriteByte(data)
input[i] = data
i++
}
}
if uart.Buffered() > 0 {
data, _ := uart.ReadByte()
usbcdc.Write([]byte{data})
}
time.Sleep(10 * time.Millisecond)
}
}
@sago35
Copy link
Author

sago35 commented Oct 5, 2021

By connecting D6 and D7, this code will work.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment