Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera branch #17394

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
/drivers/*/*xec* @franciscomunoz @albertofloyd @scottwcpg
/drivers/wifi/ @jukkar @tbursztyka @pfalcon
/drivers/wifi/eswifi/ @loicpoulain
/drivers/camera/ @JunYangNXP
/dts/arm/atmel/samr21.dtsi @benpicco
/dts/arm/st/ @erwango
/dts/arm/ti/cc13?2* @bwitherspoon
Expand Down Expand Up @@ -302,6 +303,7 @@
/samples/subsys/shell/ @jakub-uC @nordic-krch
/samples/subsys/usb/ @jfischer-phytec-iot @finikorg
/samples/subsys/power/ @wentongwu @pizi-nordic
/samples/camera/ @JunYangNXP
/scripts/coccicheck @himanshujha199640 @JuliaLawall
/scripts/coccinelle/ @himanshujha199640 @JuliaLawall
/scripts/kconfig/ @ulfalizer
Expand Down
7 changes: 7 additions & 0 deletions boards/arm/mimxrt1050_evk/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,11 @@ endchoice

endif # LVGL

if CAMERA

config CAMERA_DISPLAY_DEV_NAME
default "ELCDIF_1"

endif

endif # BOARD_MIMXRT1050_EVK || BOARD_MIMXRT1050_EVK_QSPI
14 changes: 14 additions & 0 deletions boards/arm/mimxrt1050_evk/mimxrt1050_evk.dts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ arduino_serial: &uart3 {};
int1-gpios = <&gpio1 10 0>;
int2-gpios = <&gpio1 11 0>;
};

/* I2C address of image sensor is not fixed.
* Camera driver scans the image sensor support list to
* identify the image sensor present.
*/
image-senosr@3ff {
compatible = "zephyr,image-sensor";
reg = <0x3ff>;
pwr-gpios = <&gpio1 4 1>;
};
};

&uart1 {
Expand Down Expand Up @@ -128,3 +138,7 @@ arduino_serial: &uart3 {};
pwr-gpios = <&gpio1 5 0>;
cd-gpios = <&gpio2 28 0>;
};

&csi1 {
status = "okay";
};
42 changes: 42 additions & 0 deletions boards/arm/mimxrt1050_evk/pinmux.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,43 @@ static void mimxrt1050_evk_usdhc_pinmux(
}
#endif

#ifdef CONFIG_CAMERA
static void mimxrt1050_evk_csi_mclk_enable(bool enable)
{
if (enable) {
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_05_CSI_MCLK, 0U);
} else {
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_05_GPIO1_IO21, 0U);
}
}

static void mimxrt1050_evk_csi_input_cfg(void)
{
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_04_CSI_PIXCLK, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_06_CSI_VSYNC, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_07_CSI_HSYNC, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_08_CSI_DATA09, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_09_CSI_DATA08, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_10_CSI_DATA07, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_11_CSI_DATA06, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_12_CSI_DATA05, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_13_CSI_DATA04, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_14_CSI_DATA03, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_15_CSI_DATA02, 0U);
}

static void mimxrt1050_evk_camera_init(void)
{
mimxrt1050_evk_csi_mclk_enable(false);
mimxrt1050_evk_csi_input_cfg();
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_04_GPIO1_IO04, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_00_LPI2C1_SCL, 1U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_01_LPI2C1_SDA, 1U);
IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_00_LPI2C1_SCL, 0xD8B0u);
IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_01_LPI2C1_SDA, 0xD8B0u);
}
#endif

static int mimxrt1050_evk_init(struct device *dev)
{
ARG_UNUSED(dev);
Expand Down Expand Up @@ -304,6 +341,11 @@ static int mimxrt1050_evk_init(struct device *dev)
imxrt_usdhc_pinmux_cb_register(mimxrt1050_evk_usdhc_pinmux);
#endif

#ifdef CONFIG_CAMERA
mimxrt1050_evk_camera_init();
imxrt_csi_mclk_cb_register(mimxrt1050_evk_csi_mclk_enable);
#endif

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_subdirectory_if_kconfig(wifi)
add_subdirectory_if_kconfig(can)
add_subdirectory_if_kconfig(audio)
add_subdirectory_if_kconfig(hwinfo)
add_subdirectory_if_kconfig(camera)

add_subdirectory_ifdef(CONFIG_FLASH_HAS_DRIVER_ENABLED flash)
add_subdirectory_ifdef(CONFIG_SERIAL_HAS_DRIVER serial)
Expand Down
2 changes: 2 additions & 0 deletions drivers/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ source "drivers/neural_net/Kconfig"

source "drivers/hwinfo/Kconfig"

source "drivers/camera/Kconfig"

endmenu
10 changes: 10 additions & 0 deletions drivers/camera/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: Apache-2.0

add_subdirectory_ifdef(CONFIG_MT9M114 mt9m114)
add_subdirectory_ifdef(CONFIG_NXP_MCUX_CSI nxp_csi)

zephyr_sources(camera_dev.c)
zephyr_sources(image_sensor_dev.c)

zephyr_sources_ifdef(CONFIG_USERSPACE camera_handlers.c)
zephyr_sources_ifdef(CONFIG_USERSPACE image_sensor_handlers.c)
64 changes: 64 additions & 0 deletions drivers/camera/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Kconfig - Camera drivers

#
# Copyright (c) 2019 NXP Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

menuconfig CAMERA
bool "Camera Drivers"
help
Enable camera drivers

if CAMERA

module = CAMERA
module-str = camera

config CAMERA_DISPLAY_DEV_NAME
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be put under a boolean option, now it looks like a camera requires a display.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes sense and I am trying add.

string "Camera display device name"
default "CAMERA DISPLAY"
help
Name of the display device to display camera.

config IMAGE_SENSOR_INIT_PRIO
int "Image sensor initalization priority level"
default 70
range 70 80
help
Image sensor initialization priority level.
This number tells how early in the boot
the image sensor driver and image sensor vendor
driver are initialized.

config CAMERA_INIT_PRIO
int "Camera initalization priority level"
default 81
range 81 90
help
Camera initialization priority level.
This number tells how early in the boot
the camera vendor drivers are initialized.
Camera initalization depneds on image sensors,
so CAMERA_INIT_PRIO must be greater than
IMAGE_SENSOR_INIT_PRIO.

menuconfig NXP_MCUX_CSI
bool "NXP i.MXRT cmos sensor interface"
depends on HAS_MCUX_CSI
help
Enable NXP i.MXRT cmos sensor driver.

source "drivers/camera/nxp_csi/Kconfig"

menuconfig MT9M114
bool "MT9M114 image sensor"
depends on I2C
default y
help
Enable driver for the MT9M114 image sensor.

source "drivers/camera/mt9m114/Kconfig"

endif
139 changes: 139 additions & 0 deletions drivers/camera/camera_dev.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2019 NXP Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <drivers/camera_drv.h>
#define LOG_LEVEL LOG_LEVEL_ERR
#include <logging/log.h>
#define CAMERA_DEV_DBG

#undef LOG_ERR
#define LOG_ERR printk

#ifdef CAMERA_DEV_DBG
#undef LOG_INF
#define LOG_INF printk
#endif

static int z_camera_num;
static struct device *z_camera_dev[CAMERA_MAX_NUMBER];

#define CAMERA_DRV_DATA_MAX_SIZE 1024
static u8_t z_camera_drv_data[CAMERA_MAX_NUMBER *
CAMERA_DRV_DATA_MAX_SIZE] __aligned(64);

static K_MUTEX_DEFINE(z_camera_lock);

int camera_dev_get_cap(struct device *cam_dev,
struct camera_capability *cap)
{
struct camera_driver_data *data = cam_dev->driver_data;

memcpy((char *)cap, (char *)&data->cap,
sizeof(struct camera_capability));

return 0;
}

int camera_dev_configure(struct device *cam_dev,
struct camera_fb_cfg *fb_cfg)
{
struct camera_driver_data *data = cam_dev->driver_data;

if (fb_cfg->cfg_mode == CAMERA_DEFAULT_CFG) {
memcpy((char *)&fb_cfg->fb_attr,
(char *)&data->fb_attr,
sizeof(struct camera_fb_attr));
} else {
memcpy((char *)&data->fb_attr,
(char *)&fb_cfg->fb_attr,
sizeof(struct camera_fb_attr));
}

return 0;
}

int camera_dev_register(struct device *dev)
{
(void)k_mutex_lock(&z_camera_lock, K_FOREVER);

if (z_camera_num < CAMERA_MAX_NUMBER) {
z_camera_dev[z_camera_num] = dev;
z_camera_num++;

k_mutex_unlock(&z_camera_lock);

return 0;
}

k_mutex_unlock(&z_camera_lock);

return -ENOSPC;
}

struct camera_driver_data *camera_drv_data_alloc(
u32_t priv_size, enum camera_id id, bool clear)
{
struct camera_driver_data *data;

if ((priv_size + sizeof(struct camera_driver_data)) >
CAMERA_DRV_DATA_MAX_SIZE) {

LOG_ERR("Camera data alloc size %d exceeds max size\r\n",
(int)(CAMERA_DRV_DATA_MAX_SIZE -
sizeof(struct camera_driver_data)));
return 0;
}

if (id != CAMERA_PRIMARY_ID &&
id != CAMERA_SECONDARY_ID) {

LOG_ERR("Camera data alloc id %d is illegal\r\n", id);
return 0;
}

data = (struct camera_driver_data *)
&z_camera_drv_data[(id - CAMERA_PRIMARY_ID) *
CAMERA_DRV_DATA_MAX_SIZE];
if (clear) {
memset((char *)data, 0, sizeof(struct camera_driver_data));
}
data->id = id;

return data;
}

static struct device *camera_get_by_id(enum camera_id id)
{
int i;
struct device *dev;
struct camera_driver_data *camera_data;

(void)k_mutex_lock(&z_camera_lock, K_FOREVER);

for (i = 0; i < z_camera_num; i++) {
dev = z_camera_dev[i];
camera_data = dev->driver_data;
if (camera_data->id == id) {

k_mutex_unlock(&z_camera_lock);

return dev;
}
}

k_mutex_unlock(&z_camera_lock);

return 0;
}

struct device *camera_get_primary(void)
{
return camera_get_by_id(CAMERA_PRIMARY_ID);
}

struct device *camera_get_secondary(void)
{
return camera_get_by_id(CAMERA_SECONDARY_ID);
}
55 changes: 55 additions & 0 deletions drivers/camera/camera_handlers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2019 NXP Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <drivers/camera.h>
#include <drivers/display.h>
#include <syscall_handler.h>

Z_SYSCALL_HANDLER(camera_power, cam_dev, power)
{
return z_impl_camera_power((struct device *)cam_dev,
(bool)power);
}

Z_SYSCALL_HANDLER(camera_get_cap, cam_dev, cap)
{
return z_impl_camera_get_cap((struct device *)cam_dev,
(struct camera_capability *)cap);
}

Z_SYSCALL_HANDLER(camera_reset, cam_dev)
{
return z_impl_camera_reset((struct device *)cam_dev);
}

Z_SYSCALL_HANDLER(camera_configure, cam_dev,
width, height, pixformat)
{
return z_impl_camera_configure((struct device *)cam_dev,
(u16_t)width, (u16_t)height,
(enum display_pixel_format)pixformat);
}

Z_SYSCALL_HANDLER(camera_start, cam_dev,
mode, bufs, buf_num, cb)
{
return z_impl_camera_start((struct device *)cam_dev,
(enum camera_mode)mode, (void **)bufs, (u8_t)buf_num,
(camera_capture_cb)cb);
}

Z_SYSCALL_HANDLER(camera_acquire_fb, cam_dev,
fb, timeout)
{
return z_impl_camera_acquire_fb((struct device *)cam_dev,
(void **)fb, (s32_t)timeout);
}

Z_SYSCALL_HANDLER(camera_release_fb, cam_dev,
fb)
{
return z_impl_camera_release_fb((struct device *)cam_dev,
(void *)fb);
}
Loading