From b3832b86c16a5373f0dc9a8ce1208f9d23407e82 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 30 Jun 2025 16:36:02 +0200 Subject: [PATCH] common/csi: Fix CSI detection & init logic. Simplify the detection logic and handle the case where all detected sensors (one or more) are auxiliary. Signed-off-by: iabdalkader --- common/omv_csi.c | 31 ++++++++++++++----------------- common/omv_csi.h | 1 + 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/common/omv_csi.c b/common/omv_csi.c index 915b77b1c..43c04ab65 100644 --- a/common/omv_csi.c +++ b/common/omv_csi.c @@ -447,6 +447,7 @@ int omv_csi_probe(omv_i2c_t *i2c) { // Initialize detected sensors. for (size_t i=0; idetected = true; csi->power_on = true; @@ -455,15 +456,12 @@ int omv_csi_probe(omv_i2c_t *i2c) { csi->chip_id = dev_list[i].chip_id; csi->slv_addr = dev_list[i].slv_addr; - uint32_t clk_hz = 0; - sensor_init_t init_fun = NULL; - // Find the sensors init function. for (size_t i=0; ichip_id == config->chip_id) { - clk_hz = config->clk_hz; init_fun = config->init_fun; + csi->clk_hz = config->clk_hz; break; } } @@ -477,35 +475,34 @@ int omv_csi_probe(omv_i2c_t *i2c) { // Special case for OV5640. #if (OMV_OV5640_REV_Y_CHECK == 1) if (csi->chip_id == OV5640_ID && HAL_GetREVID() < 0x2003) { - clk_hz = OMV_OV5640_REV_Y_FREQ; + csi->clk_hz = OMV_OV5640_REV_Y_FREQ; } #endif // Allow reconfiguring (or disabling) the external clock // if just one sensor is detected, or for main sensors. if (dev_count == 1 || !csi->auxiliary) { - omv_csi_set_clk_frequency(clk_hz); + omv_csi_set_clk_frequency(csi->clk_hz); } // Count aux devices. aux_count += csi->auxiliary; } - // Special case: A single aux sensor was detected, clear - // the auxiliary flag so it gets used as the main sensor. - if (dev_count == 1 && csi_all[0].auxiliary) { - csi_all[0].auxiliary = 0; - aux_count--; - } - - // Special case: Soft-CSI and another aux sensor detected, - // (Lepton for example). Use Soft-CSI for the main sensor. + // Special case: all detected sensors are aux (e.g., Soft-CSI or + // Soft-CSI + Lepton). If only one is found, use it as main. If + // multiple, pick the first non-Soft-CSI sensor as main. if (dev_count == aux_count) { for (size_t i=0; ichip_id == SOFTCSI_ID) { - csi->auxiliary = 0; + if (dev_count == 1 || csi->chip_id != SOFTCSI_ID) { aux_count--; + csi->auxiliary = 0; + // If more than one aux sensor was detected, the clock + // hasn't been changed, so reconfigure using this freq. + if (dev_count > 1) { + omv_csi_set_clk_frequency(csi->clk_hz); + } break; } } diff --git a/common/omv_csi.h b/common/omv_csi.h index c9c3a6b0c..4124e5200 100644 --- a/common/omv_csi.h +++ b/common/omv_csi.h @@ -339,6 +339,7 @@ typedef struct _omv_csi { bool auto_rotation; // Rotate Image Automatically bool detected; // Set to true when the sensor is initialized. bool power_on; // Set to true when the sensor is active. + uint32_t clk_hz; // Clock frequency requested by the driver. omv_i2c_t *i2c; // SCCB/I2C bus. framebuffer_t *fb; // Frame buffer pointer