Skip to content

usbd: Add basic HID keypad (with status LED detection), basic mass storage #1

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

Draft
wants to merge 25 commits into
base: feature/usbd_python
Choose a base branch
from

Conversation

turmoni
Copy link

@turmoni turmoni commented Jun 28, 2023

Creating as a draft pull request because I don't think this is really ready to be merged as-is, since I was mostly working on just getting it working for me and I imagine the structure could do with being improved, especially when it comes to where to put constants and the like. Points of note:

  • I think there has been some confusion between IN and OUT - see my modifications to hid.py where I've swapped them around.
  • The mass storage is very limited, and only implements what I wanted, which is basically read-only, a single LUN, and with the entire filesystem as a variable in RAM. (Update: now also supports objects using the AbstractBlockDev protocol.)
  • Some of this code can eventually be removed since it's there to work around issues I failed to figure out (see _retry_xfer_cb in device.py and try_to_prepare_cbw in msc.py for examples)

I also know that I'm not following micropython-lib's conventions for submitting a PR, but since I'm submitting this to you and I'm not necessarily expecting it to be directly merged I hope that's less important. At least, I made my commit messages and pushed them up before reading that.

projectgus and others added 7 commits March 2, 2023 11:00
Rely on support implemented in the machine.USBD() object on the MicroPython
side, providing a thin wrapper around TinyUSB "application" device class
driver.
- Add micropython-lib 'usbd' package (provisional).
- Update midi implementation a bit.
- Rearrange code to work with package structure
- Convert docstrings to regular comments to save flash.
@andrewleech
Copy link

Impressive effort getting this going, looks great! With the mass storage, have you had it running against a "real" flash filesystem on the device? Does it need / work with a Block device eg one that's formatted as FAT?

@turmoni
Copy link
Author

turmoni commented Jun 29, 2023

Impressive effort getting this going, looks great! With the mass storage, have you had it running against a "real" flash filesystem on the device? Does it need / work with a Block device eg one that's formatted as FAT?

Thanks! I've realised that "filesystem" is really a bit of a misnomer, since it doesn't know or care about the filesystem, it just needs something it can operate on. Currently it needs something that works like a bytestring, but I imagine it wouldn't be too hard to extend it to do a type check on whatever it is you give it and access it appropriately. I would say it would probably be possible to create an object to pass in to it that would work transparently, but micropython doesn't support the __bytes__ method on classes so I'm not sure how you'd do that. All the changes that would be needed to be made would be in the StorageDevice class though, since the upper layers don't actually care about anything but translating between the host and the (virtual) SCSI device.

@turmoni
Copy link
Author

turmoni commented Jun 29, 2023

You've successfully got me into "just quickly adding" the ability to use rp2.Flash, which exposed a bug, and my fix has exposed other bugs, and so I guess I'll be figuring out how to fix those. I have got the main functionality working though, I can cat /dev/sde and (I think?) get the littlefs filesystem on my Pico.

(This chain of bugs brought to you by Linux asking for 30 4096-byte blocks in one command, which I hadn't seen before due to my filesystem entirely fitting in RAM.)

@andrewleech
Copy link

Nice one! Yeah my biggest issue with MSC is that it just exposes the underlying block device and filesystem - which for me is always littlefs so not easily supported on computer.

I'm actually keen to use this new USB support to add MTP as used on cameras and Android phones - this operates at the file level so should be good on any filesystem.

@turmoni turmoni changed the title Add basic HID keypad (with status LED detection), basic mass storage usbd: Add basic HID keypad (with status LED detection), basic mass storage Jun 30, 2023
@turmoni
Copy link
Author

turmoni commented Jun 30, 2023

I've now added support for AbstractBlockDev-based devices, plus the required bug fixes. I'm not sure it's done in the neatest way, but it does seem to reliably work

@projectgus
Copy link
Owner

Hi @turmoni,

This looks really great, thanks!

I think there has been some confusion between IN and OUT - see my modifications to hid.py where I've swapped them around.

Good catch on this, I'm not sure how I got this far without noticing I had them backwards!

I'm keen to bring quite a bit of this into the main micropython-lib PR. When you get a minute, do you mind going back to pop some Copyright & License notices on the new files? Feel free to add yourself to the ones you fixed/modified, as well.

@projectgus projectgus force-pushed the feature/usbd_python branch from c8ad6ca to 53f9558 Compare July 10, 2023 08:03
projectgus and others added 2 commits July 10, 2023 18:05
Rely on support implemented in the machine.USBD() object on the MicroPython
side, providing a thin wrapper around TinyUSB "application" device class
driver.
@projectgus projectgus force-pushed the feature/usbd_python branch from 53f9558 to 581a662 Compare July 10, 2023 08:06
@turmoni
Copy link
Author

turmoni commented Jul 10, 2023

Thanks! I've added those to the files I created now (and got rid of my own EP constants and some other bits to adjust to the changes you've made), I'm happy to make any changes here (or in a new branch) to make it easier if needed.

@projectgus
Copy link
Owner

@turmoni Thanks dave, that should be heaps for now. I still need to figure out exactly how best to structure the PRs into micropython-lib (to not add too much in one PR, and to correctly group things like examples or modules for particular functionality). So I'm happy to pick and choose from your branch as needed at the moment.

If you did have time, I'd be very curious if your MSC class still needs the extra workaround of _always_cb or if it now works OK on Windows without it. If you don't have time then I'll get around to testing this myself soon as well, though.

@turmoni
Copy link
Author

turmoni commented Jul 10, 2023

I cleaned that bit up on seeing the changes you'd made and it seems like it works fine without it now - my fork doesn't have it any more. At least, I currently have it connected up with the rp2.Flash object and I can see many debug logs continuing to flood away in the background, even if Windows tells me the filesystem needs to be formatted before it can be used, and on Linux I can happily cat the device and get everything off it.

Implement by overriding USBInterface.handle_open or handle_reset.
Necessary for MSC device class, possibly other purposes.
projectgus and others added 8 commits July 26, 2023 11:16
As contributed in projectgus#1 commit 5c51a9e

This version of the hidkeypad module depends on some other code changes from the
linked PR that aren't included here, so it won't work here yet.
- Use SET_REPORT via control transfer.
- Merge the keycodes module into hid_keypad.
- Remove LEDs other than NumLock to make the report handler even simpler.
@turmoni
Copy link
Author

turmoni commented Sep 2, 2023

Right, I think I have added decent usage of the new stall options - unfortunately I think my code ended up getting robust enough and my memory of how to do this stuff has degraded enough that I'm not entirely sure how to test it properly, so I'm by no means guaranteeing that this will appropriately behave in response to a reset or an invalid/non-meaningful CBW.

projectgus added a commit that referenced this pull request Oct 5, 2023
As contributed in #1 commit 5c51a9e

This version of the hidkeypad module depends on some other code changes from the
linked PR that aren't included here, so it won't work here yet.
@projectgus projectgus force-pushed the feature/usbd_python branch 2 times, most recently from 40d1674 to d6da573 Compare October 5, 2023 01:55
projectgus added a commit that referenced this pull request Nov 22, 2023
As contributed in #1 commit 5c51a9e

This version of the hidkeypad module depends on some other code changes from the
linked PR that aren't included here, so it won't work here yet.
projectgus added a commit that referenced this pull request Jan 11, 2024
As contributed in #1 commit 5c51a9e

This version of the hidkeypad module depends on some other code changes from the
linked PR that aren't included here, so it won't work here yet.
projectgus added a commit that referenced this pull request Jan 11, 2024
As contributed in #1
commit 5c51a9e

This version of the hidkeypad module depends on some other code changes
from the linked PR that aren't included here, so it won't work here yet.

Signed-off-by: Angus Gratton <[email protected]>
@projectgus projectgus force-pushed the feature/usbd_python branch 2 times, most recently from 9cf4a60 to 4f2220d Compare February 28, 2024 06:42
projectgus added a commit that referenced this pull request Feb 28, 2024
As contributed in #1
commit 5c51a9e

This version of the hidkeypad module depends on some other code changes
from the linked PR that aren't included here, so it won't work here yet.

Signed-off-by: Angus Gratton <[email protected]>
@projectgus projectgus force-pushed the feature/usbd_python branch 2 times, most recently from 40a430d to 744f8c4 Compare March 27, 2024 03:12
@projectgus projectgus force-pushed the feature/usbd_python branch from 90ce5f2 to 81962f1 Compare April 16, 2024 05:14
@projectgus projectgus force-pushed the feature/usbd_python branch 2 times, most recently from 97aac4a to 583bc0d Compare April 30, 2024 05:58
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.

4 participants