electro-smith / libDaisy

Hardware Library for the Daisy Audio Platform
https://www.electro-smith.com/daisy
MIT License
312 stars 131 forks source link

USB Host callbacks #511

Closed stephenhensley closed 2 years ago

stephenhensley commented 2 years ago

Adds callback mechanism to the USB Host class.

This allows for running the USBHostHandle::Process() function in the main while loop as a background task, and writing specific callbacks for dealing with connection, disconnection, etc.

At the moment we're only handling one class (MSC), so the callbacks are pretty straight forward, but once we have add a multiple classes (audio, HID, etc.) we'd have to add some breaking change to make those easy to parse, handle separately.

So it might be worth just setting up some of the class checking data now, and updating the USBClassActive callback to be class-specific now. Alternatively, we could pass in the USBHost data itself and let the user parse it.

As an aside, using the USBHostHandle class, and FatFS results in binaries too large to debug without using the custom bootloader.

github-actions[bot] commented 2 years ago

Unit Test Results

    1 files  ±0    16 suites  ±0   0s :stopwatch: ±0s 147 tests ±0  147 :heavy_check_mark: ±0  0 :zzz: ±0  0 :x: ±0 

Results for commit 3e9289b0. ± Comparison against base commit 0013f723.

:recycle: This comment has been updated with latest results.

stephenhensley commented 2 years ago

I want to do a separate guide in the docs for working with USB host, and this may warrant a few upcoming changes as well.

In the projects I'm working on so far I'm mimicking the internal, unused AppliState_TypeDef for my own application in the user callbacks, and then using those to drive a state machine to mount the filesystem, etc.

So probably a bit of stuff to rework.

Also, the Result codes aren't entirely helpful because the blocking functions always return USBD_OK if stuff is functioning, and I haven't found a great way to detect when all activity (especially diskio) is actually complete as it seems there is a bit of lag between when something like f_close returns and the subsequent USBHost::Process() finish negotiating the completion of activity.

But for now it works, and allows a user application to support replugging USB while running which wasn't possible before with the existing API.