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

usbus: allow to forward EP0 transfer events to other handlers #19493

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

dylad
Copy link
Member

@dylad dylad commented Apr 21, 2023

Contribution description

This PR introduces a new usbus handler flag to allows to forward transfer complete event from EP0 only (control endpoints) to other handlers.
The idea is to let other interfaces know the state of the transfer on these EPs. Each interface should could set this flag (at any moment) so it will receive these events.

For a more real use case on DFU:
Currently, DFU uses ztimer for a unique purpose, schedule the reboot. At the end of the firmware transfer, dfu-util will request the 'DFU state' of the device. Device must answer it, then reboot to start the newly flashed application. Thus, we cannot simply reboot as soon as we fill the buffer and ready the transfer with usbdev_ep_xmit(). We need to wait the end of the transfer and that the host ACK it. This is why ztimer was used. To wait a bit, to let the transfer completes properly, then reboot. Rebooting without this small delay, leads to an error in dfu-util message log (but the device is correctly flashed).
dfu-util: unable to read DFU status after completion (LIBUSB_ERROR_PIPE)

Nevertheless, using ztimer for this purpose is more a workaround than a proper solution. That's why, a new flag is introduced by this PR. Each handler can set this bit at any point in time (we don't necessarily need it all the time). When the EP0 transfer event handling is done by usbus, it will loop through all available handlers (except the first one which is already being call, to avoid a deathloop) and forward the event to interfaces that need it.

ztimer is now optional for riotboot_dfu. It will only init ztimer if the dependency is pull by another module (e.g periph_usbdev on STM32).

If ztimer dependency was only pull by DFU, this PR allows to reduce the bootloader size:

saml21-xpro master arm-none-eabi-gcc (Arm GNU Toolchain 12.2.MPACBTI-Bet1)
   text    data     bss     dec     hex filename
  11464      20    2936   14420    3854 /home/dylan/work/RIOT/bootloaders/riotboot_dfu/bin/saml21-xpro/riotboot_dfu.elf
make : on quitte le répertoire « /home/dylan/work/RIOT/bootloaders/riotboot_dfu »

saml21-xpro w/ PR same toolchain

   text    data     bss     dec     hex filename
   9776       0    2816   12592    3130 /home/dylan/work/RIOT/bootloaders/riotboot_dfu/bin/saml21-xpro/riotboot_dfu.elf
make : on quitte le répertoire « /home/dylan/work/RIOT/bootloaders/riotboot_dfu »

Testing procedure

Flash riotboot_dfu on any board that support periph_usbdev and riotboot
make -j8 BOARD=xxxx -C bootloaders/riotboot_dfu flash

Reboot into bootloader by resetting your board while pushing on BTN0.

Then flash any test app on your board through DFU:
PROGRAMMER=dfu-util USEMODULE=usbus_dfu make -j8 BOARD=xxxx -C tests/shell riotboot/flash-slot1

Board should reboot automatically and start the newly flashed test application.
No error message are reported by dfu-util

Issues/PRs references

None.

Signed-off-by: Dylan Laduranty <dylan.laduranty@mesotic.com>
Signed-off-by: Dylan Laduranty <dylan.laduranty@mesotic.com>
Signed-off-by: Dylan Laduranty <dylan.laduranty@mesotic.com>
Signed-off-by: Dylan Laduranty <dylan.laduranty@mesotic.com>
Signed-off-by: Dylan Laduranty <dylan.laduranty@mesotic.com>
@dylad dylad added the Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. label Apr 21, 2023
@dylad dylad requested a review from gschorcht April 21, 2023 19:49
@github-actions github-actions bot added Area: sys Area: System Area: USB Area: Universal Serial Bus labels Apr 21, 2023
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 28, 2023
bootloaders/riotboot_dfu/main.c Show resolved Hide resolved
/* Forward to every handler, EP0 transfer event, except the first
* handler as this is the current handler used for this event */
for (usbus_handler_t *hdl = handler->next; hdl; hdl = hdl->next) {
if(usbus_handler_isset_flag(hdl, USBUS_HANDLER_FLAG_TR_EP0_FWD)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if(usbus_handler_isset_flag(hdl, USBUS_HANDLER_FLAG_TR_EP0_FWD)) {
if (usbus_handler_isset_flag(hdl, USBUS_HANDLER_FLAG_TR_EP0_FWD)) {

@@ -141,6 +141,7 @@ extern "C" {
*/
#define USBUS_HANDLER_FLAG_TR_FAIL (0x0010)
#define USBUS_HANDLER_FLAG_TR_STALL (0x0020) /**< Report transfer stall complete */
#define USBUS_HANDLER_FLAG_TR_EP0_FWD (0x0100) /**< Forward transfer complete */
Copy link
Contributor

Choose a reason for hiding this comment

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

So EP0 means transfer complete?

@riot-ci
Copy link

riot-ci commented Apr 28, 2023

Murdock results

✔️ PASSED

4b57db1 bootloaders/riotboot_dfu: optionally init ztimer

Success Failures Total Runtime
6931 0 6931 10m:41s

Artifacts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System Area: USB Area: Universal Serial Bus CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants