Skip to content

Commit 9767b7b

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 55371d8 commit 9767b7b

File tree

15 files changed

+99
-55
lines changed

15 files changed

+99
-55
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: 15 additions & 12 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>
@@ -349,14 +349,6 @@ static DEVICE_API(video, video_sw_generator_driver_api) = {
349349
#endif
350350
};
351351

352-
static struct video_sw_generator_data video_sw_generator_data_0 = {
353-
.fmt.width = 320,
354-
.fmt.height = 160,
355-
.fmt.pitch = 320 * 2,
356-
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565,
357-
.frame_rate = DEFAULT_FRAME_RATE,
358-
};
359-
360352
static int video_sw_generator_init(const struct device *dev)
361353
{
362354
struct video_sw_generator_data *data = dev->data;
@@ -369,6 +361,17 @@ static int video_sw_generator_init(const struct device *dev)
369361
return 0;
370362
}
371363

372-
DEVICE_DEFINE(video_sw_generator, "VIDEO_SW_GENERATOR", &video_sw_generator_init, NULL,
373-
&video_sw_generator_data_0, NULL, POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY,
374-
&video_sw_generator_driver_api);
364+
#define VIDEO_SW_GENERATOR_DEFINE(n) \
365+
static struct video_sw_generator_data video_sw_generator_data_##n = { \
366+
.fmt.width = 320, \
367+
.fmt.height = 160, \
368+
.fmt.pitch = 320 * 2, \
369+
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565, \
370+
.frame_rate = DEFAULT_FRAME_RATE, \
371+
}; \
372+
\
373+
DEVICE_DT_INST_DEFINE(n, &video_sw_generator_init, NULL, &video_sw_generator_data_##n, \
374+
NULL, POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, \
375+
&video_sw_generator_driver_api);
376+
377+
DT_INST_FOREACH_STATUS_OKAY(VIDEO_SW_GENERATOR_DEFINE)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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
9+
10+
child-binding:
11+
child-binding:
12+
include: video-interfaces.yaml

samples/drivers/video/capture/README.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ commands:
7272

7373
For testing purpose without the need of any real video capture and/or display hardwares,
7474
a video software pattern generator is supported by the above build commands without
75-
specifying the shields.
75+
specifying the shields, and using :ref:`snippet-video-sw-generator`:
76+
77+
.. zephyr-app-commands::
78+
:zephyr-app: samples/drivers/video/capture
79+
:board: native_sim
80+
:snippets: video-sw-generator
81+
:goals: build
82+
:compact:
7683

7784
Sample Output
7885
=============

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
@@ -22,8 +22,6 @@ LOG_MODULE_REGISTER(main);
2222
#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
2323
#endif
2424

25-
#define VIDEO_DEV_SW "VIDEO_SW_GENERATOR"
26-
2725
#if DT_HAS_CHOSEN(zephyr_display)
2826
static inline int display_setup(const struct device *const display_dev, const uint32_t pixfmt)
2927
{
@@ -82,6 +80,7 @@ static inline void video_display_frame(const struct device *const display_dev,
8280

8381
int main(void)
8482
{
83+
const struct device *video_dev;
8584
struct video_buffer *buffers[CONFIG_VIDEO_BUFFER_POOL_NUM_MAX], *vbuf;
8685
struct video_format fmt;
8786
struct video_caps caps;
@@ -92,21 +91,11 @@ int main(void)
9291
int i = 0;
9392
int err;
9493

95-
#if DT_HAS_CHOSEN(zephyr_camera)
96-
const struct device *const video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
97-
94+
video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
9895
if (!device_is_ready(video_dev)) {
9996
LOG_ERR("%s: video device is not ready", video_dev->name);
10097
return 0;
10198
}
102-
#else
103-
const struct device *const video_dev = device_get_binding(VIDEO_DEV_SW);
104-
105-
if (video_dev == NULL) {
106-
LOG_ERR("%s: video device not found or failed to initialized", VIDEO_DEV_SW);
107-
return 0;
108-
}
109-
#endif
11099

111100
LOG_INF("Video device: %s", video_dev->name);
112101

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 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
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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
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;
26-
const struct device *video_dev;
2725
size_t bsize;
2826
int i = 0;
2927

@@ -33,19 +31,11 @@ int main(void)
3331
return 0;
3432
}
3533

36-
#if DT_HAS_CHOSEN(zephyr_camera)
3734
video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
3835
if (!device_is_ready(video_dev)) {
3936
LOG_ERR("%s device is not ready", video_dev->name);
4037
return 0;
4138
}
42-
#else
43-
video_dev = device_get_binding(VIDEO_DEV_SW);
44-
if (video_dev == NULL) {
45-
LOG_ERR("%s device not found", VIDEO_DEV_SW);
46-
return 0;
47-
}
48-
#endif
4939

5040
LOG_INF("- Device name: %s", video_dev->name);
5141

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 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
48+
:snippets: video-sw-generator
49+
:goals: build
50+
:compact:
51+
4252
Sample Output
4353
=============
4454

0 commit comments

Comments
 (0)