2019-08-29 00:53:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, Linaro Limited
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 15:58:46 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2019-08-29 00:53:00 +08:00
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/video.h>
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
#define VIDEO_PATTERN_COLOR_BAR 0
|
|
|
|
#define VIDEO_PATTERN_FPS 30
|
|
|
|
|
|
|
|
struct video_sw_generator_data {
|
2020-05-01 02:33:38 +08:00
|
|
|
const struct device *dev;
|
2019-08-29 00:53:00 +08:00
|
|
|
struct video_format fmt;
|
|
|
|
struct k_fifo fifo_in;
|
|
|
|
struct k_fifo fifo_out;
|
2021-04-08 17:48:23 +08:00
|
|
|
struct k_work_delayable buf_work;
|
2021-04-10 17:28:50 +08:00
|
|
|
struct k_work_sync work_sync;
|
2019-08-29 00:53:00 +08:00
|
|
|
int pattern;
|
|
|
|
bool ctrl_hflip;
|
|
|
|
bool ctrl_vflip;
|
2019-11-29 17:13:57 +08:00
|
|
|
struct k_poll_signal *signal;
|
2019-08-29 00:53:00 +08:00
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_set_fmt(const struct device *dev,
|
2019-08-29 00:53:00 +08:00
|
|
|
enum video_endpoint_id ep,
|
|
|
|
struct video_format *fmt)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
if (ep != VIDEO_EP_OUT) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->fmt = *fmt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_get_fmt(const struct device *dev,
|
2019-08-29 00:53:00 +08:00
|
|
|
enum video_endpoint_id ep,
|
|
|
|
struct video_format *fmt)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
if (ep != VIDEO_EP_OUT) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*fmt = data->fmt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_stream_start(const struct device *dev)
|
2019-08-29 00:53:00 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
|
2021-04-08 17:48:23 +08:00
|
|
|
return k_work_schedule(&data->buf_work, K_MSEC(33));
|
2019-08-29 00:53:00 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_stream_stop(const struct device *dev)
|
2019-08-29 00:53:00 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
|
2021-04-10 17:28:50 +08:00
|
|
|
k_work_cancel_delayable_sync(&data->buf_work, &data->work_sync);
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Black, Blue, Red, Purple, Green, Aqua, Yellow, White */
|
2020-05-28 00:26:57 +08:00
|
|
|
uint16_t rgb565_colorbar_value[] = { 0x0000, 0x001F, 0xF800, 0xF81F,
|
2019-08-29 00:53:00 +08:00
|
|
|
0x07E0, 0x07FF, 0xFFE0, 0xFFFF };
|
|
|
|
|
|
|
|
static void __fill_buffer_colorbar(struct video_sw_generator_data *data,
|
|
|
|
struct video_buffer *vbuf)
|
|
|
|
{
|
|
|
|
int bw = data->fmt.width / 8;
|
|
|
|
int h, w, i = 0;
|
|
|
|
|
|
|
|
for (h = 0; h < data->fmt.height; h++) {
|
|
|
|
for (w = 0; w < data->fmt.width; w++) {
|
|
|
|
int color_idx = data->ctrl_vflip ? 7 - w / bw : w / bw;
|
|
|
|
if (data->fmt.pixelformat == VIDEO_PIX_FMT_RGB565) {
|
2020-05-28 00:26:57 +08:00
|
|
|
uint16_t *pixel = (uint16_t *)&vbuf->buffer[i];
|
2019-08-29 00:53:00 +08:00
|
|
|
*pixel = rgb565_colorbar_value[color_idx];
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vbuf->timestamp = k_uptime_get_32();
|
|
|
|
vbuf->bytesused = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __buffer_work(struct k_work *work)
|
|
|
|
{
|
2021-04-08 17:48:23 +08:00
|
|
|
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
|
2019-08-29 00:53:00 +08:00
|
|
|
struct video_sw_generator_data *data;
|
|
|
|
struct video_buffer *vbuf;
|
|
|
|
|
2021-04-08 17:48:23 +08:00
|
|
|
data = CONTAINER_OF(dwork, struct video_sw_generator_data, buf_work);
|
2019-08-29 00:53:00 +08:00
|
|
|
|
2021-04-08 17:48:23 +08:00
|
|
|
k_work_reschedule(&data->buf_work, K_MSEC(1000 / VIDEO_PATTERN_FPS));
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
vbuf = k_fifo_get(&data->fifo_in, K_NO_WAIT);
|
|
|
|
if (vbuf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (data->pattern) {
|
|
|
|
case VIDEO_PATTERN_COLOR_BAR:
|
|
|
|
__fill_buffer_colorbar(data, vbuf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_fifo_put(&data->fifo_out, vbuf);
|
|
|
|
|
2019-11-29 17:13:57 +08:00
|
|
|
if (IS_ENABLED(CONFIG_POLL) && data->signal) {
|
2019-08-29 00:53:00 +08:00
|
|
|
k_poll_signal_raise(data->signal, VIDEO_BUF_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
k_yield();
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_enqueue(const struct device *dev,
|
2019-08-29 00:53:00 +08:00
|
|
|
enum video_endpoint_id ep,
|
|
|
|
struct video_buffer *vbuf)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
if (ep != VIDEO_EP_OUT) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_fifo_put(&data->fifo_in, vbuf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_dequeue(const struct device *dev,
|
2019-08-29 00:53:00 +08:00
|
|
|
enum video_endpoint_id ep,
|
2020-05-04 18:03:14 +08:00
|
|
|
struct video_buffer **vbuf,
|
|
|
|
k_timeout_t timeout)
|
2019-08-29 00:53:00 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
if (ep != VIDEO_EP_OUT) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vbuf = k_fifo_get(&data->fifo_out, timeout);
|
|
|
|
if (*vbuf == NULL) {
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_flush(const struct device *dev,
|
2019-08-29 00:53:00 +08:00
|
|
|
enum video_endpoint_id ep,
|
|
|
|
bool cancel)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
struct video_buffer *vbuf;
|
|
|
|
|
|
|
|
if (!cancel) {
|
|
|
|
/* wait for all buffer to be processed */
|
|
|
|
do {
|
2019-11-08 22:58:37 +08:00
|
|
|
k_sleep(K_MSEC(1));
|
2019-08-29 00:53:00 +08:00
|
|
|
} while (!k_fifo_is_empty(&data->fifo_in));
|
|
|
|
} else {
|
|
|
|
while ((vbuf = k_fifo_get(&data->fifo_in, K_NO_WAIT))) {
|
|
|
|
k_fifo_put(&data->fifo_out, vbuf);
|
2019-11-29 17:13:57 +08:00
|
|
|
if (IS_ENABLED(CONFIG_POLL) && data->signal) {
|
2019-08-29 00:53:00 +08:00
|
|
|
k_poll_signal_raise(data->signal,
|
|
|
|
VIDEO_BUF_ABORTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct video_format_cap fmts[] = {
|
|
|
|
{
|
|
|
|
.pixelformat = VIDEO_PIX_FMT_RGB565,
|
2019-11-29 17:10:38 +08:00
|
|
|
.width_min = 64,
|
|
|
|
.width_max = 1920,
|
|
|
|
.height_min = 64,
|
|
|
|
.height_max = 1080,
|
|
|
|
.width_step = 1,
|
|
|
|
.height_step = 1,
|
2019-08-29 00:53:00 +08:00
|
|
|
},
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_get_caps(const struct device *dev,
|
2019-08-29 00:53:00 +08:00
|
|
|
enum video_endpoint_id ep,
|
|
|
|
struct video_caps *caps)
|
|
|
|
{
|
|
|
|
caps->format_caps = fmts;
|
|
|
|
caps->min_vbuf_count = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-29 17:13:57 +08:00
|
|
|
#ifdef CONFIG_POLL
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_set_signal(const struct device *dev,
|
2019-08-29 00:53:00 +08:00
|
|
|
enum video_endpoint_id ep,
|
|
|
|
struct k_poll_signal *signal)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
if (data->signal && signal != NULL) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->signal = signal;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-29 17:13:57 +08:00
|
|
|
#endif
|
2019-08-29 00:53:00 +08:00
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static inline int video_sw_generator_set_ctrl(const struct device *dev,
|
2019-08-29 00:53:00 +08:00
|
|
|
unsigned int cid,
|
|
|
|
void *value)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
switch (cid) {
|
|
|
|
case VIDEO_CID_VFLIP:
|
|
|
|
data->ctrl_vflip = (bool)value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct video_driver_api video_sw_generator_driver_api = {
|
|
|
|
.set_format = video_sw_generator_set_fmt,
|
|
|
|
.get_format = video_sw_generator_get_fmt,
|
|
|
|
.stream_start = video_sw_generator_stream_start,
|
|
|
|
.stream_stop = video_sw_generator_stream_stop,
|
|
|
|
.flush = video_sw_generator_flush,
|
|
|
|
.enqueue = video_sw_generator_enqueue,
|
|
|
|
.dequeue = video_sw_generator_dequeue,
|
|
|
|
.get_caps = video_sw_generator_get_caps,
|
|
|
|
.set_ctrl = video_sw_generator_set_ctrl,
|
2019-11-29 17:13:57 +08:00
|
|
|
#ifdef CONFIG_POLL
|
2019-08-29 00:53:00 +08:00
|
|
|
.set_signal = video_sw_generator_set_signal,
|
2019-11-29 17:13:57 +08:00
|
|
|
#endif
|
2019-08-29 00:53:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct video_sw_generator_data video_sw_generator_data_0 = {
|
|
|
|
.fmt.width = 320,
|
2019-11-29 17:10:38 +08:00
|
|
|
.fmt.height = 160,
|
|
|
|
.fmt.pitch = 320*2,
|
2019-08-29 00:53:00 +08:00
|
|
|
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565,
|
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int video_sw_generator_init(const struct device *dev)
|
2019-08-29 00:53:00 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct video_sw_generator_data *data = dev->data;
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
data->dev = dev;
|
|
|
|
k_fifo_init(&data->fifo_in);
|
|
|
|
k_fifo_init(&data->fifo_out);
|
2021-04-08 17:48:23 +08:00
|
|
|
k_work_init_delayable(&data->buf_work, __buffer_work);
|
2019-08-29 00:53:00 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-18 22:52:46 +08:00
|
|
|
DEVICE_DEFINE(video_sw_generator, "VIDEO_SW_GENERATOR",
|
2021-04-28 18:05:24 +08:00
|
|
|
&video_sw_generator_init, NULL,
|
2020-12-18 22:52:46 +08:00
|
|
|
&video_sw_generator_data_0, NULL,
|
2020-04-06 21:58:02 +08:00
|
|
|
POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY,
|
2019-08-29 00:53:00 +08:00
|
|
|
&video_sw_generator_driver_api);
|