Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated teensy40 board and machine/usb to utilize new machine.Serial interface #1

Open
wants to merge 1 commit into
base: feature/usb-common
Choose a base branch
from

Conversation

bgould
Copy link

@bgould bgould commented Jan 17, 2022

This PR refactored TinyGo to have a compiler flag for choosing the type of output - one of "usb", "uart", or "none". This PR updates the Teensy 4.0 implementation and HID example to utilize this mechanism, making it much easier to switch between HID and CDC.

Please note that I set the descriptor count for HID to be 1 by default, which uses a few kb more RAM than necessary... but IMO that is an ok tradeoff for now, especially since Teensy 4.0/4.1 have a whole 1MB of RAM to work with. I think this can be optimized/fixed in the future.

@@ -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
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
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.

ardnew pushed a commit that referenced this pull request Jun 21, 2022
Instead of doing everything in the interrupt lowering pass, generate
some more code in gen-device to declare interrupt handler functions and
do some work in the compiler so that interrupt lowering becomes a lot
simpler.

This has several benefits:

  - Overall code is smaller, in particular the interrupt lowering pass.
  - The code should be a bit less "magical" and instead a bit easier to
    read. In particular, instead of having a magic
    runtime.callInterruptHandler (that is fully written by the interrupt
    lowering pass), the runtime calls a generated function like
    device/sifive.InterruptHandler where this switch already exists in
    code.
  - Debug information is improved. This can be helpful during actual
    debugging but is also useful for other uses of DWARF debug
    information.

For an example on debug information improvement, this is what a
backtrace might look like before this commit:

    Breakpoint 1, 0x00000b46 in UART0_IRQHandler ()
    (gdb) bt
    #0  0x00000b46 in UART0_IRQHandler ()
    #1  <signal handler called>
    [..etc]

Notice that the debugger doesn't see the source code location where it
has stopped.

After this commit, breaking at the same line might look like this:

    Breakpoint 1, (*machine.UART).handleInterrupt (arg1=..., uart=<optimized out>) at /home/ayke/src/github.com/tinygo-org/tinygo/src/machine/machine_nrf.go:200
    200			uart.Receive(byte(nrf.UART0.RXD.Get()))
    (gdb) bt
    #0  (*machine.UART).handleInterrupt (arg1=..., uart=<optimized out>) at /home/ayke/src/github.com/tinygo-org/tinygo/src/machine/machine_nrf.go:200
    #1  UART0_IRQHandler () at /home/ayke/src/github.com/tinygo-org/tinygo/src/device/nrf/nrf51.go:176
    #2  <signal handler called>
    [..etc]

By now, the debugger sees an actual source location for UART0_IRQHandler
(in the generated file) and an inlined function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants