Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/examples/usb/keyboard/keyboard.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package main

import (
"machine"
"machine/usb"
"time"
)

var keyboard = machine.HID0.Keyboard()
var keyboard *usb.Keyboard

func init() {
hid := usb.HID{Port: 0}
hid.Configure(usb.HIDConfig{})
keyboard = hid.Keyboard()
}

func main() {

Expand Down
2 changes: 1 addition & 1 deletion src/examples/usb/serial/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
uart := machine.UART0
uart := machine.Serial
uart.Write([]byte("Echo console enabled. Type something then press enter:\r\n"))

input := make([]byte, 4096)
Expand Down
11 changes: 1 addition & 10 deletions src/machine/board_teensy40.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//go:build teensy40
// +build teensy40

package machine

import (
"device/nxp"
"machine/usb"
"runtime/interrupt"
)

Expand Down Expand Up @@ -105,15 +105,6 @@ func init() {
_UART7.Interrupt = interrupt.New(nxp.IRQ_LPUART7, _UART7.handleInterrupt)
}

// #=====================================================#
// | USB |
// #=====================================================#
var (
UART0 = usb.UART{Port: 0}
// HID0 = usb.HID{Port: 0}
// UART0 = &UART1
)

// #=====================================================#
// | UART |
// #===========#===========#=============#===============#
Expand Down
20 changes: 20 additions & 0 deletions src/machine/machine_mimxrt1062_usb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build mimxrt1062 && serial.usb
// +build mimxrt1062,serial.usb

package machine

import (
"machine/usb"
)

// usbUART wraps the usb.UART so that Configure() can be overridden
// to accept machine.UARTConfig as its argument
type usbUART struct {
*usb.UART
}

func (u *usbUART) Configure(cfg UARTConfig) {
u.UART.Configure(usb.UARTConfig{})
}

var USB = &usbUART{&usb.UART{Port: 0}}
3 changes: 2 additions & 1 deletion src/machine/usb/desc_mimxrt1062.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build mimxrt1062
// +build mimxrt1062

package usb
Expand All @@ -16,7 +17,7 @@ const descCDCACMCount = 1

// descHIDCount defines the number of USB cores that may be configured as a
// composite (keyboard + mouse + joystick) human interface device (HID).
const descHIDCount = 0
const descHIDCount = 1
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only solutions I can think of to this:

  1. tinygo does an in-place file edit:
    • Would need a robust technique to find and edit this and other consts.
    • Would need to decide if/how to restore the edits afterwards.
      • Restoring the edits will confuse IDEs/debuggers that are comparing the source to the compiled executable.
      • NOT restoring the edits means compiling source code is a destructive operation on that source code!
    • Kinda hacky IMO
  2. Move these respective constants to their own standalone source files with appropriate build tag constraints:
    • Let tinygo adjust active build tags automatically if command-line flag given.
      • For example: GOFLAGS="-tags=...,usb.hid,... if command-line flag -usb=hid was given.

Of course, 2. would be my preference and also seems the easiest to implement.

Copy link
Copy Markdown
Owner

@ardnew ardnew Feb 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if 2. would be preferable, it will be necessary to add a separate command-line flag to tinygo — piggybacking off of the -serial flag is insufficient.

Something like the following, along with logic to verify the flags given are compatible (e.g., -serial=usb -usb=hid contradicts itself)

tinygo -serial={none,{uart,usb}[:PORT]} -usb={cdc,hid}[:PORT]

As mentioned in the first comment, this new -usb flag would be responsible for setting the respective build tags which causes the corresponding source file(s) containing USB configuration constants to get included.


// General USB device identification constants.
const (
Expand Down
15 changes: 4 additions & 11 deletions src/runtime/runtime_mimxrt1062.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build mimxrt1062
// +build mimxrt1062

package runtime
Expand All @@ -6,7 +7,6 @@ import (
"device/arm"
"device/nxp"
"machine"
"machine/usb"
"math/bits"
"unsafe"
)
Expand Down Expand Up @@ -107,8 +107,7 @@ func initPeripherals() {
initPins() // configure GPIO

enablePeripheralClocks() // activate peripheral clock gates
initUSB() // configure USB CDC-ACM (UART0)
initUART() // configure hardware UART (UART1)
initUART() // configure Serial interface
}

func initPins() {
Expand All @@ -120,17 +119,11 @@ func initPins() {
}

func initUART() {
machine.UART1.Configure(machine.UARTConfig{})
}

func initUSB() {
// machine.HID0.Configure(usb.HIDConfig{})
machine.UART0.Configure(usb.UARTConfig{})
machine.Serial.Configure(machine.UARTConfig{})
}

func putchar(c byte) {
machine.UART0.WriteByte(c) // print to USB UART
// machine.UART1.WriteByte(c) // print to hardware UART
machine.Serial.WriteByte(c)
}

func exit(code int) {
Expand Down
3 changes: 1 addition & 2 deletions targets/teensy40.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"inherits": ["cortex-m7"],
"build-tags": ["teensy40", "teensy", "mimxrt1062", "nxp"],
"serial": "uart",
"serial": "usb",
"automatic-stack-size": false,
"default-stack-size": 4096,
"linkerscript": "targets/mimxrt1062-teensy40.ld",
Expand All @@ -11,4 +11,3 @@
],
"flash-command": "teensy_loader_cli -mmcu=imxrt1062 -v -w {hex}"
}