Skip to content

Commit 6ea0603

Browse files
jfischer-nofabiobaltieri
authored andcommitted
usb: device_next: allow message callback to be executed from USBD thread
Allowing message callback execution from the USBD thread saves about 800 bytes. For small devices, the option can be useful to reduce flash/RAM usage, those with enough resources should not bother about it. Signed-off-by: Johann Fischer <[email protected]>
1 parent 187703c commit 6ea0603

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

include/zephyr/usb/usbd.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,14 @@ struct usbd_status {
269269
/**
270270
* @brief Callback type definition for USB device message delivery
271271
*
272-
* The implementation uses the system workqueue, and a callback provided and
273-
* registered by the application. The application callback is called in the
274-
* context of the system workqueue. Notification messages are stored in a queue
275-
* and delivered to the callback in sequence.
272+
* If the Kconfig option USBD_MSG_DEFERRED_MODE is enabled, then the callback
273+
* is executed in the context of the system workqueue. Notification messages are
274+
* stored in a queue and delivered to the callback in sequence.
275+
*
276+
* If the Kconfig option USBD_MSG_DEFERRED_MODE is disabled, the callback is
277+
* executed in the context of the USB device stack thread. The user should make
278+
* sure that the callback execution does not block or disrupt device stack
279+
* handling.
276280
*
277281
* @param[in] ctx Pointer to USB device support context
278282
* @param[in] msg Pointer to USB device message

subsys/usb/device_next/Kconfig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,15 @@ config USBD_MSG_SLAB_COUNT
8181
help
8282
Maximum number of USB device notification messages that can be queued.
8383

84+
config USBD_MSG_DEFERRED_MODE
85+
bool "Execute message callback from system workqueue"
86+
default y
87+
help
88+
Execute message callback from system workqueue. If disabled, message
89+
callback will be executed in the device stack context.
90+
8491
config USBD_MSG_WORK_DELAY
85-
int "USB device notification messages work delay"
92+
int "USB device notification messages work delay" if USBD_MSG_DEFERRED_MODE
8693
range 1 100
8794
default 1
8895
help

subsys/usb/device_next/usbd_msg.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ void usbd_msg_pub_simple(struct usbd_context *const ctx,
116116
};
117117

118118
if (ctx->msg_cb != NULL) {
119-
usbd_msg_pub(ctx, msg);
119+
if (IS_ENABLED(CONFIG_USBD_MSG_DEFERRED_MODE)) {
120+
usbd_msg_pub(ctx, msg);
121+
} else {
122+
ctx->msg_cb(ctx, &msg);
123+
}
120124
}
121125
}
122126

@@ -129,6 +133,10 @@ void usbd_msg_pub_device(struct usbd_context *const ctx,
129133
};
130134

131135
if (ctx->msg_cb != NULL) {
132-
usbd_msg_pub(ctx, msg);
136+
if (IS_ENABLED(CONFIG_USBD_MSG_DEFERRED_MODE)) {
137+
usbd_msg_pub(ctx, msg);
138+
} else {
139+
ctx->msg_cb(ctx, &msg);
140+
}
133141
}
134142
}

0 commit comments

Comments
 (0)