Skip to content

Commit 77491a6

Browse files
committed
drivers: video: sw_generator: convert to use the devicetree
Make the video software generator a devicetree node, which allows enabling several instances, and select it as chosen { zephyr,camera = &... }; node. It can be enabled via a `video-sw-generator` snippet. Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent 9b8e9eb commit 77491a6

File tree

15 files changed

+98
-57
lines changed

15 files changed

+98
-57
lines changed

drivers/video/Kconfig.sw_generator

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
# MT9m114
2-
31
# Copyright (c) 2016 Linaro Limited
42
# SPDX-License-Identifier: Apache-2.0
53

6-
DT_CHOSEN_ZEPHYR_CAMERA := zephyr,camera
7-
84
config VIDEO_SW_GENERATOR
95
bool "Video Software Generator"
10-
depends on !$(dt_chosen_enabled,$(DT_CHOSEN_ZEPHYR_CAMERA))
6+
depends on DT_HAS_ZEPHYR_VIDEO_SW_GENERATOR_ENABLED
7+
default y
118
help
129
Enable video pattern generator (for testing purposes).

drivers/video/video_sw_generator.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#define DT_DRV_COMPAT zephyr_sw_generator
7+
#define DT_DRV_COMPAT zephyr_video_sw_generator
88

99
#include <zephyr/kernel.h>
1010
#include <zephyr/drivers/video.h>
@@ -310,14 +310,6 @@ static DEVICE_API(video, video_sw_generator_driver_api) = {
310310
#endif
311311
};
312312

313-
static struct video_sw_generator_data video_sw_generator_data_0 = {
314-
.fmt.width = 320,
315-
.fmt.height = 160,
316-
.fmt.pitch = 320 * 2,
317-
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565,
318-
.frame_rate = DEFAULT_FRAME_RATE,
319-
};
320-
321313
static int video_sw_generator_init_controls(const struct device *dev)
322314
{
323315
struct video_sw_generator_data *data = dev->data;
@@ -338,8 +330,19 @@ static int video_sw_generator_init(const struct device *dev)
338330
return video_sw_generator_init_controls(dev);
339331
}
340332

341-
DEVICE_DEFINE(video_sw_generator, "VIDEO_SW_GENERATOR", &video_sw_generator_init, NULL,
342-
&video_sw_generator_data_0, NULL, POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY,
343-
&video_sw_generator_driver_api);
344-
345-
VIDEO_DEVICE_DEFINE(video_sw_generator, DEVICE_GET(video_sw_generator), NULL);
333+
#define VIDEO_SW_GENERATOR_DEFINE(n) \
334+
static struct video_sw_generator_data video_sw_generator_data_##n = { \
335+
.fmt.width = 320, \
336+
.fmt.height = 160, \
337+
.fmt.pitch = 320 * 2, \
338+
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565, \
339+
.frame_rate = DEFAULT_FRAME_RATE, \
340+
}; \
341+
\
342+
DEVICE_DT_INST_DEFINE(n, &video_sw_generator_init, NULL, &video_sw_generator_data_##n, \
343+
NULL, POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, \
344+
&video_sw_generator_driver_api); \
345+
\
346+
VIDEO_DEVICE_DEFINE(video_sw_generator_##n, DEVICE_DT_INST_GET(n), NULL);
347+
348+
DT_INST_FOREACH_STATUS_OKAY(VIDEO_SW_GENERATOR_DEFINE)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright 2025 tinyVision.ai Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Emulated Video test pattern generator
5+
6+
compatible: "zephyr,video-sw-generator"
7+
8+
include: base.yaml

samples/drivers/video/capture/README.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,16 @@ using the :ref:`dvp_20pin_ov7670` and :ref:`lcd_par_s035` connected to the board
8383
:goals: build
8484
:compact:
8585

86-
For testing purpose without the need of any real video capture and/or display hardwares,
86+
For testing purpose and without the need of any real video capture and/or display hardwares,
8787
a video software pattern generator is supported by the above build commands without
88-
specifying the shields.
88+
specifying the shields, and using :ref:`snippet-video-sw-generator`:
89+
90+
.. zephyr-app-commands::
91+
:zephyr-app: samples/drivers/video/capture
92+
:board: native_sim/native/64
93+
:snippets: video-sw-generator
94+
:goals: build
95+
:compact:
8996

9097
Sample Output
9198
=============

samples/drivers/video/capture/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
CONFIG_VIDEO=y
2-
CONFIG_VIDEO_SW_GENERATOR=y
32
CONFIG_SHELL=y
43
CONFIG_DEVICE_SHELL=y
54
CONFIG_PRINTK=y

samples/drivers/video/capture/src/main.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ LOG_MODULE_REGISTER(main);
2323
#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
2424
#endif
2525

26-
#define VIDEO_DEV_SW "VIDEO_SW_GENERATOR"
27-
2826
#if DT_HAS_CHOSEN(zephyr_display)
2927
static inline int display_setup(const struct device *const display_dev, const uint32_t pixfmt)
3028
{
@@ -90,6 +88,7 @@ static inline void video_display_frame(const struct device *const display_dev,
9088

9189
int main(void)
9290
{
91+
const struct device *video_dev;
9392
struct video_buffer *buffers[CONFIG_VIDEO_BUFFER_POOL_NUM_MAX], *vbuf;
9493
struct video_format fmt;
9594
struct video_caps caps;
@@ -101,21 +100,11 @@ int main(void)
101100
int i = 0;
102101
int err;
103102

104-
#if DT_HAS_CHOSEN(zephyr_camera)
105-
const struct device *const video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
106-
103+
video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
107104
if (!device_is_ready(video_dev)) {
108105
LOG_ERR("%s: video device is not ready", video_dev->name);
109106
return 0;
110107
}
111-
#else
112-
const struct device *const video_dev = device_get_binding(VIDEO_DEV_SW);
113-
114-
if (video_dev == NULL) {
115-
LOG_ERR("%s: video device not found or failed to initialized", VIDEO_DEV_SW);
116-
return 0;
117-
}
118-
#endif
119108

120109
LOG_INF("Video device: %s", video_dev->name);
121110

samples/drivers/video/capture_to_lvgl/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ For :zephyr:board:`mini_stm32h743`, build this sample application with the follo
3737
:gen-args: -DCONFIG_BOOT_DELAY=2000
3838
:compact:
3939

40+
For testing purpose and without the need of any real video capture hardware,
41+
a video software pattern generator is supported by using :ref:`snippet-video-sw-generator`:
42+
43+
.. zephyr-app-commands::
44+
:zephyr-app: samples/drivers/video/capture
45+
:board: native_sim/native/64
46+
:snippets: video-sw-generator
47+
:goals: build
48+
:compact:
49+
4050
Sample Output
4151
=============
4252

samples/drivers/video/capture_to_lvgl/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
CONFIG_VIDEO=y
2-
CONFIG_VIDEO_SW_GENERATOR=y
32

43
CONFIG_PRINTK=y
54
CONFIG_LOG=y

samples/drivers/video/capture_to_lvgl/src/main.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
#include <zephyr/logging/log.h>
1616
LOG_MODULE_REGISTER(main);
1717

18-
#define VIDEO_DEV_SW "VIDEO_SW_GENERATOR"
19-
2018
int main(void)
2119
{
2220
struct video_buffer *buffers[2], *vbuf;
2321
const struct device *display_dev;
22+
const struct device *video_dev;
2423
struct video_format fmt;
2524
struct video_caps caps;
2625
enum video_buf_type type = VIDEO_BUF_TYPE_OUTPUT;
@@ -34,19 +33,11 @@ int main(void)
3433
return 0;
3534
}
3635

37-
#if DT_HAS_CHOSEN(zephyr_camera)
3836
video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
3937
if (!device_is_ready(video_dev)) {
4038
LOG_ERR("%s device is not ready", video_dev->name);
4139
return 0;
4240
}
43-
#else
44-
video_dev = device_get_binding(VIDEO_DEV_SW);
45-
if (video_dev == NULL) {
46-
LOG_ERR("%s device not found", VIDEO_DEV_SW);
47-
return 0;
48-
}
49-
#endif
5041

5142
LOG_INF("- Device name: %s", video_dev->name);
5243

samples/drivers/video/tcpserversink/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ If a mt9m114 camera shield is missing, video software generator will be used ins
3939
:goals: build
4040
:compact:
4141

42+
For testing purpose and without the need of any real video capture hardware,
43+
a video software pattern generator is supported by using :ref:`snippet-video-sw-generator`:
44+
45+
.. zephyr-app-commands::
46+
:zephyr-app: samples/drivers/video/capture
47+
:board: native_sim/native/64
48+
:snippets: video-sw-generator
49+
:goals: build
50+
:compact:
51+
4252
Sample Output
4353
=============
4454

0 commit comments

Comments
 (0)