Skip to content

Commit 3875f8d

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 0117988 commit 3875f8d

File tree

15 files changed

+97
-57
lines changed

15 files changed

+97
-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>
@@ -338,14 +338,6 @@ static DEVICE_API(video, video_sw_generator_driver_api) = {
338338
#endif
339339
};
340340

341-
static struct video_sw_generator_data video_sw_generator_data_0 = {
342-
.fmt.width = 320,
343-
.fmt.height = 160,
344-
.fmt.pitch = 320 * 2,
345-
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565,
346-
.frame_rate = DEFAULT_FRAME_RATE,
347-
};
348-
349341
static int video_sw_generator_init_controls(const struct device *dev)
350342
{
351343
struct video_sw_generator_data *data = dev->data;
@@ -366,8 +358,19 @@ static int video_sw_generator_init(const struct device *dev)
366358
return video_sw_generator_init_controls(dev);
367359
}
368360

369-
DEVICE_DEFINE(video_sw_generator, "VIDEO_SW_GENERATOR", &video_sw_generator_init, NULL,
370-
&video_sw_generator_data_0, NULL, POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY,
371-
&video_sw_generator_driver_api);
372-
373-
VIDEO_DEVICE_DEFINE(video_sw_generator, DEVICE_GET(video_sw_generator), NULL);
361+
#define VIDEO_SW_GENERATOR_DEFINE(n) \
362+
static struct video_sw_generator_data video_sw_generator_data_##n = { \
363+
.fmt.width = 320, \
364+
.fmt.height = 160, \
365+
.fmt.pitch = 320 * 2, \
366+
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565, \
367+
.frame_rate = DEFAULT_FRAME_RATE, \
368+
}; \
369+
\
370+
DEVICE_DT_INST_DEFINE(n, &video_sw_generator_init, NULL, &video_sw_generator_data_##n, \
371+
NULL, POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, \
372+
&video_sw_generator_driver_api); \
373+
\
374+
VIDEO_DEVICE_DEFINE(video_sw_generator_##n, DEVICE_DT_INST_GET(n), NULL);
375+
376+
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: 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
@@ -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
{
@@ -83,6 +81,7 @@ static inline void video_display_frame(const struct device *const display_dev,
8381

8482
int main(void)
8583
{
84+
const struct device *video_dev;
8685
struct video_buffer *buffers[CONFIG_VIDEO_BUFFER_POOL_NUM_MAX], *vbuf;
8786
struct video_format fmt;
8887
struct video_caps caps;
@@ -93,21 +92,11 @@ int main(void)
9392
int i = 0;
9493
int err;
9594

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

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

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)