From e952f1734b72f858252e699d86ebb572ada656a3 Mon Sep 17 00:00:00 2001 From: gaurav Date: Wed, 6 Jul 2022 23:49:46 +0200 Subject: [PATCH] IMX219 Driver changed to adapt quick camera sensor change. --- .../Source/sensor_imx219.c | 300 ++++++------------ .../Source/sensor_imx219.h | 28 +- Cypress_FX3_USB_Firmware/Source/uvc.c | 5 +- 3 files changed, 108 insertions(+), 225 deletions(-) diff --git a/Cypress_FX3_USB_Firmware/Source/sensor_imx219.c b/Cypress_FX3_USB_Firmware/Source/sensor_imx219.c index c09f7d1..e1f4333 100644 --- a/Cypress_FX3_USB_Firmware/Source/sensor_imx219.c +++ b/Cypress_FX3_USB_Firmware/Source/sensor_imx219.c @@ -95,18 +95,16 @@ static const imx219_reg_t mode_default[]={ //default register settings, Resoluti }; -image_sensor_config_t *sensor_config; +imgsensor_mode_t *sensor_config; -static image_sensor_config_t sensor_config_2LANE = { - .sensor_mode = 0x01, - - .mode_640x480_30 = { //200Mhz 2Lane 311040 312000 +static imgsensor_mode_t sensor_config_2LANE[] = { + { //200Mhz 2Lane .pix_clk_mul = 0x2E, .pix_clk_div = 0x4, //only 4 or 5 or 8 or 10 - .integration = 1725 - 4, //must be < (linelength- 4) to maintain frame rate by framelength or integration time will slow frame rate + .integration = 258 - 4, //must be < (linelength- 4) to maintain frame rate by framelength or integration time will slow frame rate .gain = 0x70, .linelength = 3448, //Warning! This value need to be either 0xD78 or 0xDE7 regardless of frame size and FPS, other values will result undefined and ununderstanable issues in image - .framelength = 1725, //decided how long is frame, basically frame rate with pix clock, it has second priority to integration time. absolute minimum is 255 for imx219 + .framelength = 258, //decided how long is frame, basically frame rate with pix clock, it has second priority to integration time. absolute minimum is 255 for imx219 .startx = 1000, .starty = 750, .endx = 2280, @@ -114,44 +112,13 @@ static image_sensor_config_t sensor_config_2LANE = { .width = 640, .height = 480, //each frame will have two extra line to compensate for debayer crop .binning = 2, + .fps = 200, .test_pattern = 0 }, - .mode_640x480_200 = { //200fps with 2 lane and 200MHz mipi - .pix_clk_mul = 0x2E, - .pix_clk_div = 0x4, - .integration = 258 - 4, - .gain = 0x70, - .linelength = 3448, - .framelength = 258, - .startx = 1000, - .starty = 750, - .endx = 2280, - .endy = 1715, - .width = 640, - .height = 480, - .binning = 2, - .test_pattern = 0 - }, - .mode_1280x720_30 = { //200Mhz 2 lane - .pix_clk_mul = 0x2E, - .pix_clk_div = 0x4, - .integration = 1720 - 4, - .gain = 0x80, - .linelength = 3448, - .framelength = 1720, - .startx = 4, - .starty = 4, - .endx = 0xA27, - .endy = 0x6EB, - .width = 1280, - .height = 720, - .binning = 0, - .test_pattern = 0 - }, - .mode_1280x720_60 = { //200Mhz mipi 2 lane 927360 + { //200Mhz mipi 2 lane .pix_clk_mul = 0x2E, .pix_clk_div = 0x4, .integration = 862 - 4, @@ -165,10 +132,11 @@ static image_sensor_config_t sensor_config_2LANE = { .width = 1280, .height = 720, .binning = 0, + .fps = 60, .test_pattern = 0 }, - .mode_1920x1080_30 = { //200Mhz 2Lane 2082240 + { //200Mhz 2Lane .pix_clk_mul = 0x20, .pix_clk_div = 0x4, .integration = 1200 - 4, @@ -182,27 +150,12 @@ static image_sensor_config_t sensor_config_2LANE = { .width = 1920, .height = 1080, .binning = 0, + .fps = 30, .test_pattern = 0 }, - .mode_640x128_600 = { //200Mhz 2LANE 82944 - .pix_clk_mul = 0x2D, - .pix_clk_div = 0x4, - .integration = 84 - 4, - .gain = 200, - .linelength = 0xD78, - .framelength = 84, - .startx = 1320, - .starty = 990, - .endx = 2600, - .endy = 1609, - .width = 640, - .height = 128, - .binning = 2, - .test_pattern = 0 - }, - .mode_640x80_900 = { //200Mhz 2Lane 51840 + { //200Mhz 2Lane .pix_clk_mul = 0x2D, .pix_clk_div = 0x4, .integration = 56 - 4, @@ -216,10 +169,11 @@ static image_sensor_config_t sensor_config_2LANE = { .width = 640, .height = 80, .binning = 2, + .fps = 900, .test_pattern = 0 }, - .mode_3280x2464_7 = { //200Mhz 2 Lane 3055360 + { //200Mhz 2 Lane .pix_clk_mul = 0x12, .pix_clk_div = 0x4, .integration = 2670 - 4, @@ -233,22 +187,39 @@ static image_sensor_config_t sensor_config_2LANE = { .width = 3280, .height = 2464, .binning = 0, + .fps = 5, + .test_pattern = 0 + }, + + { //200Mhz 2 Lane + .pix_clk_mul = 0x12, + .pix_clk_div = 0x4, + .integration = 2670 - 4, + .gain = 200, + .linelength = 0xD78, //3448 + .framelength = 2670, + .startx = 0, + .starty = 0, + .endx = 3279, + .endy = 2463, + .width = 3280, + .height = 2464, + .binning = 0, + .fps = 7, .test_pattern = 0 }, }; -static image_sensor_config_t sensor_config_4LANE = { - .sensor_mode = 0x01, - - .mode_640x480_30 = { +static imgsensor_mode_t sensor_config_4LANE[] = { + { .pix_clk_mul = 0x53, .pix_clk_div = 0x4, //only 4 or 5 or 8 or 10 - .integration = 3209 - 4, //must be < (linelength- 4) to maintain frame rate by framelength or integration time will slow frame rate + .integration = 481 - 4, //must be < (linelength- 4) to maintain frame rate by framelength or integration time will slow frame rate .gain = 0x70, .linelength = 3448, //Warning! This value need to be either 0xD78 or 0xDE7 regardless of frame size and FPS, other values will result undefined and ununderstanable issues in image - .framelength = 3209, + .framelength = 481, .startx = 1000, .starty = 750, .endx = 2280, @@ -256,58 +227,11 @@ static image_sensor_config_t sensor_config_4LANE = { .width = 640, .height = 482, //each frame will have two extra line to compensate for debayer crop .binning = 2, - .test_pattern = 0 - }, - .mode_640x480_200 = { - .pix_clk_mul = 0x53, - .pix_clk_div = 0x4, - .integration = 481 - 4, - .gain = 0x70, - .linelength = 3448, - .framelength = 481, - .startx = 1000, - .starty = 750, - .endx = 2280, - .endy = 1715, - .width = 640, - .height = 482, - .binning = 2, + .fps = 200, .test_pattern = 0 }, - .mode_1280x720_30 = { - .pix_clk_mul = 0x58, - .pix_clk_div = 0x4, - .integration = 3402 - 10, - .gain = 0x80, - .linelength = 3448, - .framelength = 3402, - .startx = 4, - .starty = 4, - .endx = 0xA27, - .endy = 0x6EB, - .width = 1280, - .height = 722, - .binning = 0, - .test_pattern = 0 - }, - .mode_1280x720_60 = { - .pix_clk_mul = 0x58, - .pix_clk_div = 0x4, - .integration = 1701 - 4, - .gain = 0x80, - .linelength = 0xD78, - .framelength = 1701, - .startx = 0x2A8, - .starty = 0x2B4, - .endx = 0xA27, - .endy = 0x6EB, - .width = 1280, - .height = 722, - .binning = 0, - .test_pattern = 0 - }, - .mode_1280x720_120 = { //Camera output @120 + { //Camera output @120 .pix_clk_mul = 0x58, .pix_clk_div = 0x4, .integration = 850 - 4, @@ -321,25 +245,11 @@ static image_sensor_config_t sensor_config_4LANE = { .width = 1280, .height = 722, .binning = 0, + .fps = 120, .test_pattern = 0 }, - .mode_1920x1080_30 = { //camera output 1920x1080 @30FPS - .pix_clk_mul = 0x50, - .pix_clk_div = 0x5, - .integration = 2474 - 4, - .gain = 0x80, - .linelength = 0xD78, - .framelength = 2474, - .startx = 0x2A8, - .starty = 0x2B4, - .endx = 0xA27, - .endy = 0x6EB, - .width = 1920, - .height = 1082, - .binning = 0, - .test_pattern = 0 - }, - .mode_1920x1080_60 = { //camera output 1920x1080 @60FPS + + { //camera output 1920x1080 @60FPS .pix_clk_mul = 0x50, .pix_clk_div = 0x5, .integration = 1237 - 4, @@ -353,26 +263,10 @@ static image_sensor_config_t sensor_config_4LANE = { .width = 1920, .height = 1082, .binning = 0, + .fps = 60, .test_pattern = 0 }, - .mode_640x128_682 = { //camera output 640x128 pixel @682 FPS - .pix_clk_mul = 0x53, - .pix_clk_div = 0x4, - .integration = 141 - 4, - .gain = 200, - .linelength = 0xD78, - .framelength = 141, - .startx = 1320, - .starty = 990, - .endx = 2600, - .endy = 1609, - .width = 640, - .height = 128, - .binning = 2, - .test_pattern = 0 - }, - - .mode_640x80_1000 = { //camera output 640x80 pixel @1000 FPS + { //camera output 640x80 pixel @1000 FPS .pix_clk_mul = 0x53, .pix_clk_div = 0x4, .integration = 96 - 4, @@ -386,9 +280,11 @@ static image_sensor_config_t sensor_config_4LANE = { .width = 640, .height = 80, .binning = 2, + .fps = 1000, .test_pattern = 0 }, - .mode_3280x2464_15 = { //full frame 3280x2464 @15FPS + + { //full frame 3280x2464 @15FPS .pix_clk_mul = 0x31, .pix_clk_div = 0x5, .integration = 3031 - 4, @@ -402,8 +298,29 @@ static image_sensor_config_t sensor_config_4LANE = { .width = 3280, .height = 2464, .binning = 0, + .fps = 5, .test_pattern = 0 }, + + + { //full frame 3280x2464 @15FPS + .pix_clk_mul = 0x31, + .pix_clk_div = 0x5, + .integration = 3031 - 4, + .gain = 200, + .linelength = 0xD78, + .framelength = 3031, + .startx = 0, + .starty = 0, + .endx = 3279, + .endy = 2463, + .width = 3280, + .height = 2464, + .binning = 0, + .fps = 15, + .test_pattern = 0 + }, + }; @@ -513,72 +430,49 @@ void sensor_handle_uvc_control(uint8_t frame_index, uint32_t interval) { switch(frame_index) { - case FRAME_640x480: + case FRAME_MODE0: { - if (interval == INTERVAL_30) + if (interval == INTERVAL_MODE0) { - selected_img_mode = &sensor_config->mode_640x480_30; + selected_img_mode = &sensor_config[0]; } - else if(interval == INTERVAL_200) + } + break; + case FRAME_MODE1: + { + if (interval == INTERVAL_MODE1) { - selected_img_mode = &sensor_config->mode_640x480_200; + selected_img_mode = &sensor_config[1]; + } + } + break; + case FRAME_MODE2: + { + if (interval == INTERVAL_MODE2) + { + selected_img_mode = &sensor_config[2]; } } break; - case FRAME_1280x720: + case FRAME_MODE3: { - if (interval == INTERVAL_30) - { - selected_img_mode = &sensor_config->mode_1280x720_30; - } - else if(interval == INTERVAL_60) - { - selected_img_mode = &sensor_config->mode_1280x720_60; - } - else if(interval == INTERVAL_120) - { - //selected_img_mode = &sensor_config->mode_1280x720_120; - selected_img_mode = &sensor_config->mode_1280x720_60; - } - } - break; - case FRAME_1920x1080: - { - if (interval == INTERVAL_30) - { - selected_img_mode = &sensor_config->mode_1920x1080_30; - } - else if(interval == INTERVAL_60) - { - //selected_img_mode = &sensor_config->mode_1920x1080_60; - selected_img_mode = &sensor_config->mode_1920x1080_30; - - } - } - break; - case FRAME_3280x2464: - { - if (interval == INTERVAL_7) + if (interval == INTERVAL_MODE3) { //selected_img_mode = &sensor_config->mode_3280x2464_15; - selected_img_mode = &sensor_config->mode_3280x2464_7; + selected_img_mode = &sensor_config[3]; } } break; - case FRMAE_640x128: + case FRAME_MODE4: { - if (interval == INTERVAL_682) + if (interval == INTERVAL_MODE4_MIN) { - selected_img_mode = &sensor_config->mode_640x128_682; + selected_img_mode = &sensor_config[4]; } - } - break; - case FRMAE_640x80: - { - if (interval == INTERVAL_1000) + else if (interval == INTERVAL_MODE4) { - selected_img_mode = &sensor_config->mode_640x80_1000; + selected_img_mode = &sensor_config[5]; } } break; @@ -644,7 +538,7 @@ void sensor_configure_mode(imgsensor_mode_t * mode) sensor_i2c_write(REG_BINNING_H, 0x00); sensor_i2c_write(REG_BINNING_V, 0x00); } - camera_stream_on(sensor_config->sensor_mode); + camera_stream_on(1); } uint8_t SensorI2cBusTest (void) @@ -677,8 +571,12 @@ void SensorInit (void) //CyU3PDebugPrint (4, "Reg 0x%x val 0x%x\n", (mode_default + i)->address, (mode_default + i)->val); sensor_i2c_write((mode_default + i)->address, (mode_default + i)->val); } - sensor_config = &sensor_config_2LANE; - sensor_configure_mode(&sensor_config->mode_1280x720_30); + #if LANES == 2 + sensor_config = sensor_config_2LANE; + #else + sensor_config = sensor_config_4LANE; + #endif + sensor_configure_mode(&sensor_config_2LANE[5]); } uint8_t SensorGetBrightness (void) diff --git a/Cypress_FX3_USB_Firmware/Source/sensor_imx219.h b/Cypress_FX3_USB_Firmware/Source/sensor_imx219.h index 2687700..30b9f13 100644 --- a/Cypress_FX3_USB_Firmware/Source/sensor_imx219.h +++ b/Cypress_FX3_USB_Firmware/Source/sensor_imx219.h @@ -6,6 +6,9 @@ #include #include "sensor_imx219.h" +#define LANES 2 + + #define SENSOR_ADDR_WR 0x20 /* Slave address used to write sensor registers. */ #define SENSOR_ADDR_RD 0x21 /* Slave address used to read from sensor registers. */ #define I2C_SLAVEADDR_MASK 0xFE /* Mask to get actual I2C slave address value without direction bit. */ @@ -133,30 +136,11 @@ typedef struct imgsensor_mode_struct_s { uint16_t height; uint16_t framerate; uint8_t binning; + uint8_t fps; uint8_t test_pattern; } imgsensor_mode_t; -/* SENSOR PRIVATE STRUCT FOR CONSTANT*/ -typedef struct image_sensor_config_s { - uint8_t sensor_mode; - - imgsensor_mode_t mode_640x480_30; //640x480 30fps - imgsensor_mode_t mode_1280x720_30; //1280x720 30fps - imgsensor_mode_t mode_1280x720_60; //1280x720 60fps - imgsensor_mode_t mode_1280x720_120; //1280x720 120fps - imgsensor_mode_t mode_1920x1080_30; //1920x1080 30fps - imgsensor_mode_t mode_1920x1080_60; //1920x1080 60fps - imgsensor_mode_t mode_640x480_200; - imgsensor_mode_t mode_640x128_682; - imgsensor_mode_t mode_640x128_600; - imgsensor_mode_t mode_640x80_900; - imgsensor_mode_t mode_640x80_1000; - imgsensor_mode_t mode_3280x2464_15; - imgsensor_mode_t mode_3280x2464_7; -} image_sensor_config_t; - -#define LANES 2 #if LANES == 2 #define SENSOR_MODE0_WIDTH (unsigned int)640 @@ -177,7 +161,7 @@ typedef struct image_sensor_config_s { #define SENSOR_MODE4_WIDTH (unsigned int)3280 #define SENSOR_MODE4_HEIGHT (unsigned int)2464 -#define SENSOR_MODE4_MIN (unsigned int)5 +#define SENSOR_MODE4_FPS_MIN (unsigned int)5 #define SENSOR_MODE4_FPS (unsigned int)15 #else @@ -199,7 +183,7 @@ typedef struct image_sensor_config_s { #define SENSOR_MODE4_WIDTH (unsigned int)3280 #define SENSOR_MODE4_HEIGHT (unsigned int)2464 -#define SENSOR_MODE4_MIN (unsigned int)10 +#define SENSOR_MODE4_FPS_MIN (unsigned int)10 #define SENSOR_MODE4_FPS (unsigned int)30 #endif diff --git a/Cypress_FX3_USB_Firmware/Source/uvc.c b/Cypress_FX3_USB_Firmware/Source/uvc.c index b89cfbc..2dee930 100644 --- a/Cypress_FX3_USB_Firmware/Source/uvc.c +++ b/Cypress_FX3_USB_Firmware/Source/uvc.c @@ -13,7 +13,8 @@ #include #include "uvc.h" -#include "sensor_imx219.h" +//#include "sensor_imx219.h" +#include "sensor_imx477.h" #include "camera_ptzcontrol.h" #include "cyfxgpif2config.h" #include "uvc_settings.h" @@ -1430,7 +1431,7 @@ UVCHandleProcessingUnitRqts ( { exposure_val = CY_U3P_MAKEWORD(glEp0Buffer[1], glEp0Buffer[0]); - sensor_set_exposure (exposure_val); + //sensor_set_exposure (exposure_val); } break; default: