Skip to content

Commit 190faae

Browse files
committed
drivers: video: ov7670 driver changes in prep for adding OV7675
drivers: video: ov7670 driver changes in prep for adding OV7675 Updates ov7670.c in prep for incorporation of OV7675. See previous PR. Includes revisions requested by reviewers (9/25). Signed-off-by: Michael Smorto <[email protected]>
1 parent dc40055 commit 190faae

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

drivers/video/ov7670.c

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ LOG_MODULE_REGISTER(video_ov7670, CONFIG_VIDEO_LOG_LEVEL);
1818

1919
struct ov7670_config {
2020
struct i2c_dt_spec bus;
21-
const struct gpio_dt_spec reset;
22-
const struct gpio_dt_spec pwdn;
21+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(reset_gpios)
22+
struct gpio_dt_spec reset;
23+
#endif
24+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(pwdn_gpios)
25+
struct gpio_dt_spec pwdn;
26+
#endif
2327
};
2428

2529
struct ov7670_ctrls {
@@ -142,7 +146,7 @@ static const struct video_reg8 ov7670_init_regtbl[] = {
142146

143147
/* configure the output timing */
144148
/* PCLK does not toggle during horizontal blank, one PCLK, one pixel */
145-
{OV7670_COM10, 0x03}, /* COM10 */
149+
{OV7670_COM10, 0x20}, /* COM10 */
146150
{OV7670_COM12, 0x00}, /* COM12,No HREF when VSYNC is low */
147151
/* Brightness Control, with signal -128 to +128, 0x00 is middle value */
148152
{OV7670_BRIGHT, 0x2f},
@@ -291,7 +295,6 @@ static const struct video_reg8 ov7670_init_regtbl[] = {
291295
{0xb8, 0x0a},
292296
};
293297

294-
#if DT_HAS_COMPAT_STATUS_OKAY(ovti_ov7670)
295298
/* Resolution settings for camera, based on those present in MCUX SDK */
296299
static const struct video_reg8 ov7670_regs_qcif[] = {
297300
{OV7670_COM7, 0x2c},
@@ -346,8 +349,8 @@ static const struct video_format_cap fmts[] = {
346349
OV767X_VIDEO_FORMAT_CAP(320, 240, VIDEO_PIX_FMT_YUYV), /* QVGA */
347350
OV767X_VIDEO_FORMAT_CAP(352, 288, VIDEO_PIX_FMT_YUYV), /* CIF */
348351
OV767X_VIDEO_FORMAT_CAP(640, 480, VIDEO_PIX_FMT_YUYV), /* VGA */
349-
{0}};
350-
#endif
352+
{0}
353+
};
351354

352355
static int ov7670_get_caps(const struct device *dev, struct video_caps *caps)
353356
{
@@ -361,7 +364,7 @@ static int ov7670_set_fmt(const struct device *dev, struct video_format *fmt)
361364
const struct ov7670_config *config = dev->config;
362365
struct ov7670_data *data = dev->data;
363366
int ret;
364-
uint8_t i = 0U;
367+
uint8_t icount = 0U;
365368

366369
if (fmt->pixelformat != VIDEO_PIX_FMT_RGB565 && fmt->pixelformat != VIDEO_PIX_FMT_YUYV) {
367370
LOG_ERR("Only RGB565 and YUYV supported!");
@@ -375,40 +378,46 @@ static int ov7670_set_fmt(const struct device *dev, struct video_format *fmt)
375378

376379
memcpy(&data->fmt, fmt, sizeof(data->fmt));
377380

381+
uint32_t a, b, c;
382+
378383
/* Set output resolution */
379-
while (fmts[i].pixelformat) {
380-
if (fmts[i].width_min == fmt->width &&
381-
fmts[i].height_min == fmt->height &&
382-
fmts[i].pixelformat == fmt->pixelformat) {
384+
ret = -ENOTSUP;
385+
icount = 0;
386+
while (fmts[icount].pixelformat) {
387+
a = fmts[icount].width_min;
388+
b = fmts[icount].height_min;
389+
c = fmts[icount].pixelformat;
390+
if (a == fmt->width && b == fmt->height && c == fmt->pixelformat) {
383391
/* Set output format */
384-
switch (fmts[i].width_min) {
392+
switch (fmts[icount].width_min) {
385393
case 176: /* QCIF */
386394
ret = video_write_cci_multiregs8(&config->bus, ov7670_regs_qcif,
387395
ARRAY_SIZE(ov7670_regs_qcif));
388396
break;
389-
case 352: /* CIF */
397+
case 352: /* QCIF */
390398
ret = video_write_cci_multiregs8(&config->bus, ov7670_regs_cif,
391399
ARRAY_SIZE(ov7670_regs_cif));
392400
break;
393401
case 320: /* QVGA */
394402
ret = video_write_cci_multiregs8(&config->bus, ov7670_regs_qvga,
395403
ARRAY_SIZE(ov7670_regs_qvga));
396-
break;
397-
398-
default: /* VGA */
404+
case 640: /* VGA */
399405
ret = video_write_cci_multiregs8(&config->bus, ov7670_regs_vga,
400406
ARRAY_SIZE(ov7670_regs_vga));
407+
default: /* QVGA */
408+
ret = video_write_cci_multiregs8(&config->bus, ov7670_regs_qvga,
409+
ARRAY_SIZE(ov7670_regs_vga));
401410
break;
402411
}
403412
if (ret < 0) {
404-
LOG_ERR("Resolution not set or not supported!");
413+
LOG_ERR("Resolution not set!");
405414
return ret;
406415
}
407416
}
408-
i++;
417+
icount++;
409418
}
410419

411-
return 0;
420+
return ret;
412421
}
413422

414423
static int ov7670_get_fmt(const struct device *dev, struct video_format *fmt)
@@ -564,22 +573,31 @@ static DEVICE_API(video, ov7670_api) = {
564573
.set_ctrl = ov7670_set_ctrl,
565574
};
566575

567-
#define OV7670_INIT(n, id) \
568-
static const struct ov7670_config ov7670_config_##n = { \
569-
.bus = I2C_DT_SPEC_INST_GET(n), \
570-
.reset = \
571-
GPIO_DT_SPEC_INST_GET_OR(n, reset_gpios, {}), \
572-
.pwdn = \
573-
GPIO_DT_SPEC_INST_GET_OR(n, pwdn_gpios, {}), \
574-
}; \
576+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(reset_gpios)
577+
#define OV7670_RESET_GPIO(n) .reset = GPIO_DT_SPEC_INST_GET_OR(n, reset_gpios, {}),
578+
#else
579+
#define OV7670_RESET_GPIO(n)
580+
#endif
581+
582+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(pwdn_gpios)
583+
#define OV7670_PWDN_GPIO(n) .pwdn = GPIO_DT_SPEC_INST_GET_OR(n, pwdn_gpios, {}),
584+
#else
585+
#define OV7670_PWDN_GPIO(n)
586+
#endif
587+
588+
#define OV7670_INIT(n) \
589+
const struct ov7670_config ov7670_config_##n = { \
590+
.bus = I2C_DT_SPEC_INST_GET(n), \
591+
OV7670_RESET_GPIO(n) \
592+
OV7670_PWDN_GPIO(n)}; \
575593
\
576594
static struct ov7670_data ov7670_data_##n; \
577595
\
578596
DEVICE_DT_INST_DEFINE(n, \
579597
ov7670_init, \
580598
NULL, \
581-
&ov7670##_data_##n, \
582-
&ov7670##_config_##n, \
599+
&ov7670_data_##n, \
600+
&ov7670_config_##n, \
583601
POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, \
584602
&ov7670_api); \
585603
\
@@ -588,4 +606,4 @@ static DEVICE_API(video, ov7670_api) = {
588606

589607
#undef DT_DRV_COMPAT
590608
#define DT_DRV_COMPAT ovti_ov7670
591-
DT_INST_FOREACH_STATUS_OKAY_VARGS(OV7670_INIT, 7670)
609+
DT_INST_FOREACH_STATUS_OKAY(OV7670_INIT)

0 commit comments

Comments
 (0)