From bc2a3372a66c3ec94fc5f8726784642346370c65 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 25 Jun 2025 19:44:45 +0200 Subject: [PATCH] common/csi: Refactor I2C scanning code. This should be much faster as it starts with the most common config: power-down active low, reset active low. Signed-off-by: iabdalkader --- common/omv_csi.c | 71 +++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 43 deletions(-) diff --git a/common/omv_csi.c b/common/omv_csi.c index e42074a92..1dab42293 100644 --- a/common/omv_csi.c +++ b/common/omv_csi.c @@ -399,60 +399,45 @@ static size_t omv_csi_detect(omv_i2c_t *i2c, i2c_dev_t *dev_list) { int omv_csi_probe(omv_i2c_t *i2c) { int init_ret = 0; - bool reset_pol = OMV_CSI_ACTIVE_HIGH; - bool power_pol = OMV_CSI_ACTIVE_HIGH; - size_t dev_count = 0; size_t aux_count = 0; i2c_dev_t dev_list[OMV_CSI_MAX_DEVICES] = { 0 }; - #if defined(OMV_CSI_POWER_PIN) - // Do a power cycle - omv_gpio_write(OMV_CSI_POWER_PIN, 1); - mp_hal_delay_ms(10); + bool reset_pol = OMV_CSI_ACTIVE_HIGH; + bool power_pol = OMV_CSI_ACTIVE_HIGH; - omv_gpio_write(OMV_CSI_POWER_PIN, 0); - mp_hal_delay_ms(OMV_CSI_POWER_DELAY); - #endif + // Active power-down state, active reset state + const omv_csi_polarity_t polarity_configs[][2] = { + { OMV_CSI_ACTIVE_LOW, OMV_CSI_ACTIVE_LOW }, + { OMV_CSI_ACTIVE_LOW, OMV_CSI_ACTIVE_HIGH }, + { OMV_CSI_ACTIVE_HIGH, OMV_CSI_ACTIVE_LOW }, + { OMV_CSI_ACTIVE_HIGH, OMV_CSI_ACTIVE_HIGH }, + }; + + // Scan the bus multiple times using different reset and power polarities, + // until a supported sensor is detected. + for (size_t i=0; dev_count == 0 && i