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

Teensy 3.6 USB CDC #1223

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

firelizzard18
Copy link
Contributor

USB CDC support for Teensy 3.6 (NXP MK66F18)

@firelizzard18
Copy link
Contributor Author

At one point, it looked like this version was working. Or at least I was seeing far more log messages from the USB ISR. But now it doesn't get past the device descriptor:

usb istat 0x01
usb istat 0x08
usb stat 0x00
usb bd tok 0xD
usb setup raw 8006000100004000
usb setup request=6 type=80 value=100 index=0
usb setup get descriptor
usb ep0 transmit 18 byte(s): 12010002ef020140c0168304000101020301
usb istat 0x08
usb stat 0x08
usb bd tok 0x9
usb istat 0x01

Except now, I'm not even getting that... All I get is "usb done" (indicating that machine.USB0.Configure() completed).

@deadprogram @aykevl Any ideas? As far as I can tell, things are failing early enough that Wireshark doesn't see anything.

Copy link
Member

@aykevl aykevl left a comment

Choose a reason for hiding this comment

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

I never worked directly with USB-CDC so I don't think I can help you much debugging it, unfortunately.

(one quick comment below, I haven't really looked through the code)

Comment on lines +125 to +132
// for background about this startup delay, please see these conversations
// https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
// https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273

sleepTicks(25 * 1000) // 50 for TeensyDuino < 142
// usb_STRING_SERIAL = readSerialNumber()
USB0.Configure()
sleepTicks(275 * 1000) // 350 for TeensyDuino < 142
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason not to include this directly in postinit? It's not like you're supposed to call InitPlatform (except for the runtime).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought I would leave room for non-Teensy boards that are based on the NXP MK66F18. I believe these delays are more related to how the Teensy programmer detects boards than to the chip itself.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. So the function is only intended to be called from the runtime but put in the machine package because it is board specific? In that case, it would be useful to add a comment explaining that, so that it's clear this function isn't supposed to be called elsewhere.

Copy link
Member

@aykevl aykevl left a comment

Choose a reason for hiding this comment

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

I'm not sure I understand the purpose of this PR. It doesn't seem to implement USB-CDC support, so what does it add in functionality?

Comment on lines +125 to +132
// for background about this startup delay, please see these conversations
// https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
// https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273

sleepTicks(25 * 1000) // 50 for TeensyDuino < 142
// usb_STRING_SERIAL = readSerialNumber()
USB0.Configure()
sleepTicks(275 * 1000) // 350 for TeensyDuino < 142
Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. So the function is only intended to be called from the runtime but put in the machine package because it is board specific? In that case, it would be useful to add a comment explaining that, so that it's clear this function isn't supposed to be called elsewhere.

"unsafe"
)

const debugUSB = true
Copy link
Member

Choose a reason for hiding this comment

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

Is this intentional? It looks like it should be false.


import (
"device/nxp"
"fmt"
Copy link
Member

Choose a reason for hiding this comment

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

Don't import fmt in the machine package. This is a huge dependency with a large dependency graph and importing it in the machine package may lead to circular dependencies. It also bloats the binary and increases compile time.

Instead, I would suggest to use println for debugging (and disabled by default).

Comment on lines +599 to +603
slice := (*reflect.SliceHeader)(unsafe.Pointer(&data))

*bde.retain = data
bde.record.address.Set(uint32(slice.Data))
bde.Describe(uint32(slice.Len), data1)
Copy link
Member

Choose a reason for hiding this comment

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

The reflect package is a relatively large dependency and shouldn't be used in the machine package. It is also possible to do without in this case:

Suggested change
slice := (*reflect.SliceHeader)(unsafe.Pointer(&data))
*bde.retain = data
bde.record.address.Set(uint32(slice.Data))
bde.Describe(uint32(slice.Len), data1)
*bde.retain = data
bde.record.address.Set(uint32(uintptr(unsafe.Pointer(&data[0]))))
bde.Describe(len(data), data1)

Comment on lines +610 to +613
slice := (*reflect.SliceHeader)(unsafe.Pointer(&data))
slice.Len = uintptr(length)
slice.Cap = uintptr(length)
slice.Data = uintptr(bde.record.address.Get())
Copy link
Member

Choose a reason for hiding this comment

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

Where does bde.record.address come from? I'm guessing it is some sort of DMA buffer, did you put it in a global somewhere (for example) to make sure it is not garbage collected?
I think it would be much better to somehow keep the original slice around and use that, to avoid some unsafe code.

Comment on lines +39 to +63
Log []string
}

func (u *USB) logf(format string, args ...interface{}) {
u.Log = append(u.Log, fmt.Sprintf(format, args...))
}

func (u *USB) debugf(format string, args ...interface{}) {
if debugUSB {
u.logf(format, args...)
}
}

func (u *USB) DumpLog() []string {
var log []string

state := interrupt.Disable()
log, u.Log = u.Log, nil
interrupt.Restore(state)

if log == nil {
return []string{}
}
return log
}
Copy link
Member

Choose a reason for hiding this comment

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

This looks like debug code. While functions that aren't called won't take up extra space (they get optimized away), the Log buffer cannot be optimized away. Maybe you can make it a separate global (separate from the USB type) if you want to keep it?

Handshake: true,
})

// clear all ending interrupts
Copy link
Member

Choose a reason for hiding this comment

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

typo?

Suggested change
// clear all ending interrupts
// clear all pending interrupts

}

func (u USBCDC) WriteByte(byte) {
panic("not implemented")
Copy link
Member

Choose a reason for hiding this comment

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

This seems strange, you implement a USBCDC type that doesn't actually work?

usb_TOK_PID_SETUP = 0xD
)

type register8Aligned32 struct {
Copy link
Member

Choose a reason for hiding this comment

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

Just to be clear, this struct is not aligned on a 32-bit (4 byte) boundary. It is still 8 bit aligned but simply takes up four bytes. Was this intentional?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants