From ecbdbdfde166a72e3b92d213165d76f09a1e787c Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 2 Jul 2025 18:12:34 +0200 Subject: [PATCH] common/csi: Refactor omv_csi_init. Signed-off-by: iabdalkader --- common/omv_csi.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++-- common/omv_csi.h | 3 ++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/common/omv_csi.c b/common/omv_csi.c index 43c04ab65..3476d5ed0 100644 --- a/common/omv_csi.c +++ b/common/omv_csi.c @@ -119,6 +119,7 @@ uint16_t resolution[][2] = { {2592, 1944}, /* WQXGA2 */ }; +static omv_i2c_t csi_i2c; omv_csi_t csi_all[OMV_CSI_MAX_DEVICES] = {0}; __weak void omv_csi_init0() { @@ -149,9 +150,73 @@ __weak void omv_csi_init0() { } __weak int omv_csi_init() { - // Reset the csi state - memset(csi_all, 0, sizeof(csi_all)); - return OMV_CSI_ERROR_CTL_UNSUPPORTED; + int ret = 0; + + // List of I2C buses to scan. + uint32_t buses[][2] = { + { OMV_CSI_I2C_ID, OMV_CSI_I2C_SPEED }, + #if defined(OMV_CSI_I2C_ALT_ID) + { OMV_CSI_I2C_ALT_ID, OMV_CSI_I2C_ALT_SPEED }, + #endif + }; + + // Configure CSI GPIOs + #if defined(OMV_CSI_RESET_PIN) + omv_gpio_config(OMV_CSI_RESET_PIN, OMV_GPIO_MODE_OUTPUT, OMV_GPIO_PULL_NONE, OMV_GPIO_SPEED_LOW, -1); + #endif + #if defined(OMV_CSI_FSYNC_PIN) + omv_gpio_config(OMV_CSI_FSYNC_PIN, OMV_GPIO_MODE_OUTPUT, OMV_GPIO_PULL_NONE, OMV_GPIO_SPEED_LOW, -1); + #endif + #if defined(OMV_CSI_POWER_PIN) + omv_gpio_config(OMV_CSI_POWER_PIN, OMV_GPIO_MODE_OUTPUT, OMV_GPIO_PULL_NONE, OMV_GPIO_SPEED_LOW, -1); + #endif + + // Initialize the CSIs using the port's ops as defaults, + // which can be overridden by sensor drivers during probe. + for (size_t i=0; ii2c = &csi_i2c; + csi->fb = framebuffer_get(-1); + csi->color_palette = rainbow_table; + omv_csi_ops_init(csi); + } + + // Configure the csi external clock (XCLK). + if (omv_csi_set_clk_frequency(OMV_CSI_CLK_FREQUENCY) != 0) { + return OMV_CSI_ERROR_TIM_INIT_FAILED; + } + + // Detect and initialize sensor(s). + for (uint32_t i=0, n_buses=OMV_ARRAY_SIZE(buses); ienabled = 0; + return 0; } omv_csi_t *omv_csi_get(int id) { diff --git a/common/omv_csi.h b/common/omv_csi.h index 4124e5200..e52414ece 100644 --- a/common/omv_csi.h +++ b/common/omv_csi.h @@ -400,6 +400,9 @@ void omv_csi_init0(); // Initializes CSI struct with default ops. int omv_csi_init(); +// Implemented by ports to set the default CSI ops. +int omv_csi_ops_init(omv_csi_t *csi); + // Return CSI instance. // If id == -1, return the main csi, otherwise look up csi by chip-id. omv_csi_t *omv_csi_get(int id);