Skip to content

Commit 4ca6d07

Browse files
committed
usb: host: class: support for usb host video class
add host video class driver; implement video capture example based on usb host video class to connect with usb camera for actual operation. Signed-off-by: Aiden Hu <[email protected]>
1 parent 9b5aa6b commit 4ca6d07

File tree

18 files changed

+4968
-53
lines changed

18 files changed

+4968
-53
lines changed

boards/nxp/rd_rw612_bga/rd_rw612_bga.dtsi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ zephyr_udc0: &usb_otg {
237237
status = "okay";
238238
};
239239

240+
zephyr_uhc0: &usb_otg {
241+
status = "okay";
242+
};
243+
240244
&dma0 {
241245
status = "okay";
242246
};
@@ -316,3 +320,13 @@ nxp_8080_touch_panel_i2c: &arduino_i2c {
316320
status = "okay";
317321
wakeup-source;
318322
};
323+
324+
zephyr_mipi_dbi_spi: &lcdic {
325+
status = "okay";
326+
pinctrl-0 = <&pinmux_lcdic>;
327+
pinctrl-names = "default";
328+
};
329+
330+
nxp_pmod_touch_panel_i2c: &arduino_i2c {
331+
status = "okay";
332+
};

boards/nxp/rd_rw612_bga/rd_rw612_bga.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ supported:
2828
- pwm
2929
- spi
3030
- usb_device
31+
- usb_host
32+
- usbh
3133
- watchdog
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright 2025 NXP
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
USB Video Class (UVC) host instance.
6+
7+
Each UVC instance added to the USB Host Controller (UHC) node will be visible
8+
as a new camera from the host point of view.
9+
10+
compatible: "zephyr,uvc-host"
11+
12+
include: base.yaml

include/zephyr/usb/usbh.h

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
extern "C" {
2727
#endif
2828

29+
/**
30+
* @brief Match flags for USB device identification
31+
*/
32+
#define USBH_MATCH_DEVICE (1U << 0) /* Match device class code */
33+
#define USBH_MATCH_INTFACE (1U << 1) /* Match interface code */
34+
35+
/* device signal value definitions */
36+
#define USBH_DEVICE_CONNECTED 1
37+
#define USBH_DEVICE_DISCONNECTED 2
38+
2939
/**
3040
* @brief USB HOST Core Layer API
3141
* @defgroup usb_host_core_api USB Host Core API
@@ -49,6 +59,8 @@ struct usbh_contex {
4959
struct usb_device *root;
5060
/** Allocated device addresses bit array */
5161
struct sys_bitarray *addr_ba;
62+
/** Registered classes */
63+
sys_slist_t registered_classes;
5264
};
5365

5466
#define USBH_CONTROLLER_DEFINE(device_name, uhc_dev) \
@@ -73,21 +85,60 @@ struct usbh_code_triple {
7385
};
7486

7587
/**
76-
* @brief USB host class data and class instance API
88+
* @brief USB device code table for device matching
89+
*/
90+
struct usbh_device_code_table {
91+
/** Match type for device identification */
92+
uint32_t match_type;
93+
/** Vendor ID */
94+
uint16_t vid;
95+
/** Product ID */
96+
uint16_t pid;
97+
/** device's class code, subclass code, protocol code. */
98+
struct usbh_code_triple device_code;
99+
/** USB interface class code */
100+
uint8_t interface_class_code;
101+
/** USB interface subclass code */
102+
uint8_t interface_subclass_code;
103+
/** USB interface protocol code */
104+
uint8_t interface_protocol_code;
105+
};
106+
107+
/**
108+
* @brief USB host speed
109+
*/
110+
enum usbh_speed {
111+
/** Host supports or is connected to a full speed bus */
112+
USBH_SPEED_FS,
113+
/** Host supports or is connected to a high speed bus */
114+
USBH_SPEED_HS,
115+
/** Host supports or is connected to a super speed bus */
116+
USBH_SPEED_SS,
117+
};
118+
119+
/**
120+
* @brief USB HOST Core Layer API
121+
* @defgroup usb_host_core_api USB Host Core API
122+
* @ingroup usb
123+
* @{
77124
*/
78-
struct usbh_class_data {
79-
/** Class code supported by this instance */
80-
struct usbh_code_triple code;
81125

126+
/**
127+
* @brief USB host class data and class instance API
128+
*/
129+
struct usbh_class_api {
82130
/** Initialization of the class implementation */
83-
/* int (*init)(struct usbh_contex *const uhs_ctx); */
131+
int (*init)(struct usbh_contex *const uhs_ctx,
132+
struct usbh_class_data *cdata);
84133
/** Request completion event handler */
85134
int (*request)(struct usbh_contex *const uhs_ctx,
86135
struct uhc_transfer *const xfer, int err);
87136
/** Device connected handler */
88-
int (*connected)(struct usbh_contex *const uhs_ctx);
137+
int (*connected)(struct usb_device *udev,
138+
void *desc_start_addr, void *desc_end_addr, struct usbh_class_data *cdata);
89139
/** Device removed handler */
90-
int (*removed)(struct usbh_contex *const uhs_ctx);
140+
int (*removed)(struct usbh_contex *const uhs_ctx,
141+
struct usbh_class_data *cdata);
91142
/** Bus remote wakeup handler */
92143
int (*rwup)(struct usbh_contex *const uhs_ctx);
93144
/** Bus suspended handler */
@@ -97,11 +148,46 @@ struct usbh_class_data {
97148
};
98149

99150
/**
151+
* @brief USB host class data and class instance API
100152
*/
101-
#define USBH_DEFINE_CLASS(name) \
102-
static STRUCT_SECTION_ITERABLE(usbh_class_data, name)
153+
struct usbh_class_data {
154+
/** System linked list node for registered classes */
155+
sys_snode_t node;
156+
/** Name of the USB host class instance */
157+
const char *name;
158+
/** Pointer to host support class API */
159+
const struct usbh_class_api *api;
160+
/** Pointer to private data */
161+
void *priv;
162+
/** Pointer to device code table for class matching */
163+
const struct usbh_device_code_table *device_code_table;
164+
/** Number of items in device code table */
165+
uint8_t table_items_count;
166+
/** Flag indicating if class has been matched to a device */
167+
uint8_t class_matched;
168+
};
103169

104170

171+
/**
172+
* @brief Define USB host support class data
173+
*
174+
* Macro defines class (function) data, as well as corresponding node
175+
* structures used internally by the stack.
176+
*
177+
* @param class_name Class name
178+
* @param class_api Pointer to struct usbd_class_api
179+
* @param class_priv Class private data
180+
*/
181+
#define USBH_DEFINE_CLASS(class_name, class_api, class_priv, code_table, items_count) \
182+
static STRUCT_SECTION_ITERABLE(usbh_class_data, class_name) = { \
183+
.name = STRINGIFY(class_name), \
184+
.api = class_api, \
185+
.priv = class_priv, \
186+
.device_code_table = code_table, \
187+
.table_items_count = items_count, \
188+
.class_matched = 0, \
189+
};
190+
105191
/**
106192
* @brief Initialize the USB host support;
107193
*

samples/drivers/display/sample.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ tests:
111111
- platform:frdm_mcxn236/mcxn236:SHIELD=lcd_par_s035_8080
112112
- platform:frdm_mcxa156/mcxa156:SHIELD=lcd_par_s035_8080
113113
- platform:frdm_rw612:SHIELD=lcd_par_s035_spi
114+
- platform:rd_rw612_bga:SHIELD=lcd_par_s035_spi
114115
- platform:ek_ra8d1:SHIELD=rtkmipilcdb00000be
115116
- platform:ek_ra8d1:SHIELD=rtk7eka6m3b00001bu
116117
- platform:nucleo_g071rb/stm32g071xx:SHIELD=x_nucleo_gfx01m2
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CONFIG_UHC_DRIVER=y
2+
CONFIG_USB_HOST_STACK=y
3+
CONFIG_USBH_VIDEO_CLASS=y
4+
5+
6+
# Video buffer configuration
7+
CONFIG_VIDEO_BUFFER_POOL_SZ_MAX=307200
8+
CONFIG_VIDEO_BUFFER_POOL_NUM_MAX=4
9+
CONFIG_VIDEO_BUFFER_POOL_ALIGN=32
10+
11+
12+
# Memory configuration for USB transfers
13+
CONFIG_HEAP_MEM_POOL_SIZE=32768
14+
CONFIG_MAIN_STACK_SIZE=4096
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
uvc_host: uvc_host {
9+
compatible = "zephyr,uvc-host";
10+
};
11+
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CONFIG_VIDEO=y
2-
CONFIG_SHELL=y
3-
CONFIG_DEVICE_SHELL=y
2+
CONFIG_USB_HOST_STACK=y
43
CONFIG_PRINTK=y
54
CONFIG_LOG=y
65
CONFIG_DISPLAY=y
76
CONFIG_REQUIRES_FLOAT_PRINTF=y
87
CONFIG_LOG_MODE_DEFERRED=y
8+
CONFIG_USBH_VIDEO_LOG_LEVEL_WRN=y

samples/drivers/video/capture/sample.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ tests:
44
sample.video.capture:
55
tags:
66
- video
7+
- usb
78
- shield
89
- samples
910
extra_args:
@@ -13,9 +14,11 @@ tests:
1314
- platform:frdm_mcxn947/mcxn947/cpu0:SHIELD="dvp_20pin_ov7670;lcd_par_s035_8080"
1415
- platform:frdm_mcxn236/mcxn236:SHIELD="dvp_20pin_ov7670;lcd_par_s035_8080"
1516
- platform:stm32h7b3i_dk:SHIELD="st_b_cams_omv_mb1683"
17+
- platform:rd_rw612_bga/rw612:SHIELD="usb_camera;lcd_par_s035_8080"
1618
extra_configs:
1719
- CONFIG_TEST=y
1820
- CONFIG_FPU=y
21+
- CONFIG_USB_HOST=y
1922
harness: console
2023
harness_config:
2124
fixture: fixture_camera
@@ -31,15 +34,19 @@ tests:
3134
- mimxrt1064_evk/mimxrt1064
3235
- mimxrt1170_evk/mimxrt1176/cm7
3336
- mimxrt1170_evk@B/mimxrt1176/cm7
37+
- rd_rw612_bga/rw612
3438
- frdm_mcxn947/mcxn947/cpu0
3539
- frdm_mcxn236/mcxn236
3640
- mm_swiftio
3741
- esp32s3_eye/esp32s3/procpu
3842
- stm32h7b3i_dk
39-
depends_on: video
43+
depends_on:
44+
- video
45+
- usbh
4046
integration_platforms:
4147
- mimxrt1064_evk/mimxrt1064
4248
- mimxrt1170_evk/mimxrt1176/cm7
49+
- rd_rw612_bga/rw612
4350
sample.video.capture.shell:
4451
tags:
4552
- video

0 commit comments

Comments
 (0)