Skip to content

Commit 7e12308

Browse files
jfischer-nonashif
authored andcommitted
doc: usb: document USB message notifications
Add documentation about USB message notifications. Use literalinclude to pull code snippets from the samples. Signed-off-by: Johann Fischer <[email protected]>
1 parent afa6411 commit 7e12308

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

doc/connectivity/usb/device_next/usb_device.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,31 @@ enumerating the device. The application can disable the USB device using
193193
:dedent:
194194
:start-after: doc device enable start
195195
:end-before: doc device enable end
196+
197+
USB Message notifications
198+
=========================
199+
200+
The application can register a callback using :c:func:`usbd_msg_register_cb` to
201+
receive message notification from the USB device support subsystem. The
202+
messages are mostly about the common device state changes, and a few specific
203+
types from the USB CDC ACM implementation.
204+
205+
.. literalinclude:: ../../../../samples/subsys/usb/common/sample_usbd_init.c
206+
:language: c
207+
:dedent:
208+
:start-after: doc device init-and-msg start
209+
:end-before: doc device init-and-msg end
210+
211+
The helper function :c:func:`usbd_msg_type_string()` can be used to convert
212+
:c:enumerator:`usbd_msg_type` to a human readable form for logging.
213+
214+
If the controller supports VBUS state change detection, the battery-powered
215+
application may want to enable the USB device only when it is connected to a
216+
host. A generic application should use :c:func:`usbd_can_detect_vbus` to check
217+
for this capability.
218+
219+
.. literalinclude:: ../../../../samples/subsys/usb/hid-keyboard/src/main.c
220+
:language: c
221+
:dedent:
222+
:start-after: doc device msg-cb start
223+
:end-before: doc device msg-cb end

samples/subsys/usb/common/sample_usbd_init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ struct usbd_context *sample_usbd_init_device(usbd_msg_cb_t msg_cb)
148148

149149
sample_fix_code_triple(&sample_usbd, USBD_SPEED_FS);
150150

151-
/* doc message callback register start */
152151
if (msg_cb != NULL) {
152+
/* doc device init-and-msg start */
153153
err = usbd_msg_register_cb(&sample_usbd, msg_cb);
154154
if (err) {
155155
LOG_ERR("Failed to register message callback");
156156
return NULL;
157157
}
158+
/* doc device init-and-msg end */
158159
}
159-
/* doc message callback register end */
160160

161161
if (IS_ENABLED(CONFIG_SAMPLE_USBD_20_EXTENSION_DESC)) {
162162
(void)usbd_device_set_bcd(&sample_usbd, USBD_SPEED_FS, 0x0201);

samples/subsys/usb/hid-keyboard/src/main.c

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,28 @@ struct hid_device_ops kb_ops = {
141141
.output_report = kb_output_report,
142142
};
143143

144+
/* doc device msg-cb start */
145+
static void msg_cb(struct usbd_context *const usbd_ctx,
146+
const struct usbd_msg *const msg)
147+
{
148+
LOG_INF("USBD message: %s", usbd_msg_type_string(msg->type));
149+
150+
if (usbd_can_detect_vbus(usbd_ctx)) {
151+
if (msg->type == USBD_MSG_VBUS_READY) {
152+
if (usbd_enable(usbd_ctx)) {
153+
LOG_ERR("Failed to enable device support");
154+
}
155+
}
156+
157+
if (msg->type == USBD_MSG_VBUS_REMOVED) {
158+
if (usbd_disable(usbd_ctx)) {
159+
LOG_ERR("Failed to disable device support");
160+
}
161+
}
162+
}
163+
}
164+
/* doc device msg-cb end */
165+
144166
int main(void)
145167
{
146168
struct usbd_context *sample_usbd;
@@ -178,19 +200,21 @@ int main(void)
178200
return ret;
179201
}
180202

181-
sample_usbd = sample_usbd_init_device(NULL);
203+
sample_usbd = sample_usbd_init_device(msg_cb);
182204
if (sample_usbd == NULL) {
183205
LOG_ERR("Failed to initialize USB device");
184206
return -ENODEV;
185207
}
186208

187-
/* doc device enable start */
188-
ret = usbd_enable(sample_usbd);
189-
if (ret) {
190-
LOG_ERR("Failed to enable device support");
191-
return ret;
209+
if (!usbd_can_detect_vbus(sample_usbd)) {
210+
/* doc device enable start */
211+
ret = usbd_enable(sample_usbd);
212+
if (ret) {
213+
LOG_ERR("Failed to enable device support");
214+
return ret;
215+
}
216+
/* doc device enable end */
192217
}
193-
/* doc device enable end */
194218

195219
LOG_INF("HID keyboard sample is initialized");
196220

0 commit comments

Comments
 (0)