From 235d607671ffc069e4dcb49290fd66cbc82fd74e Mon Sep 17 00:00:00 2001 From: armando Date: Mon, 27 Oct 2025 11:01:35 +0800 Subject: [PATCH] change(isp): make wbg standalone --- components/esp_driver_isp/CMakeLists.txt | 4 + .../esp_driver_isp/include/driver/isp.h | 1 + .../esp_driver_isp/include/driver/isp_awb.h | 12 --- .../esp_driver_isp/include/driver/isp_wbg.h | 81 +++++++++++++++++++ .../include/esp_private/isp_private.h | 1 + components/esp_driver_isp/src/isp_awb.c | 18 ----- components/esp_driver_isp/src/isp_wbg.c | 79 ++++++++++++++++++ components/hal/esp32p4/include/hal/isp_ll.h | 19 ++++- components/hal/include/hal/isp_types.h | 12 +++ .../esp32p4/include/soc/Kconfig.soc_caps.in | 4 + components/soc/esp32p4/include/soc/soc_caps.h | 1 + 11 files changed, 200 insertions(+), 32 deletions(-) create mode 100644 components/esp_driver_isp/include/driver/isp_wbg.h create mode 100644 components/esp_driver_isp/src/isp_wbg.c diff --git a/components/esp_driver_isp/CMakeLists.txt b/components/esp_driver_isp/CMakeLists.txt index 57728bf667..f20d5847b7 100644 --- a/components/esp_driver_isp/CMakeLists.txt +++ b/components/esp_driver_isp/CMakeLists.txt @@ -42,6 +42,10 @@ if(CONFIG_SOC_ISP_LSC_SUPPORTED) list(APPEND srcs "src/isp_lsc.c") endif() +if(CONFIG_SOC_ISP_WBG_SUPPORTED) + list(APPEND srcs "src/isp_wbg.c") +endif() + if(NOT ${target} STREQUAL "linux") list(APPEND requires esp_mm) endif() diff --git a/components/esp_driver_isp/include/driver/isp.h b/components/esp_driver_isp/include/driver/isp.h index 73f6fbd465..0bdd11d8db 100644 --- a/components/esp_driver_isp/include/driver/isp.h +++ b/components/esp_driver_isp/include/driver/isp.h @@ -24,3 +24,4 @@ #include "driver/isp_hist.h" #include "driver/isp_lsc.h" #include "driver/isp_sharpen.h" +#include "driver/isp_wbg.h" diff --git a/components/esp_driver_isp/include/driver/isp_awb.h b/components/esp_driver_isp/include/driver/isp_awb.h index 10883e2c66..4cd508cd10 100644 --- a/components/esp_driver_isp/include/driver/isp_awb.h +++ b/components/esp_driver_isp/include/driver/isp_awb.h @@ -164,18 +164,6 @@ esp_err_t esp_isp_awb_controller_start_continuous_statistics(isp_awb_ctlr_t awb_ */ esp_err_t esp_isp_awb_controller_stop_continuous_statistics(isp_awb_ctlr_t awb_ctlr); -/** - * @brief Set AWB white balance gain - * - * @param[in] awb_ctlr AWB controller handle - * @param[in] gain AWB white balance gain - * @return - * - ESP_OK On success - * - ESP_ERR_INVALID_ARG Null pointer - * - ESP_ERR_INVALID_STATE Driver state is invalid. - */ -esp_err_t esp_isp_awb_controller_set_wb_gain(isp_awb_ctlr_t awb_ctlr, isp_awb_gain_t gain); - /** * @brief Event data of callbacks */ diff --git a/components/esp_driver_isp/include/driver/isp_wbg.h b/components/esp_driver_isp/include/driver/isp_wbg.h new file mode 100644 index 0000000000..b8add5cd50 --- /dev/null +++ b/components/esp_driver_isp/include/driver/isp_wbg.h @@ -0,0 +1,81 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include "esp_err.h" +#include "driver/isp_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/*--------------------------------------------------------------- + WBG (White Balance Gain) +---------------------------------------------------------------*/ +/** + * @brief ISP BLC configurations + */ +typedef struct { + //for future proof +} esp_isp_wbg_config_t; + +/** + * @brief ISP WBG configuration + * + * @note After calling this API, WBG doesn't take into effect until `esp_isp_wbg_enable` is called + * + * @param[in] isp_proc Processor handle + * @param[in] config WBG configurations + * + * @return + * - ESP_OK On success + * - ESP_ERR_INVALID_STATE Not allowed to be called under current state + * - ESP_ERR_INVALID_ARG If the combination of arguments is invalid + * - ESP_ERR_NOT_SUPPORTED Not supported + */ +esp_err_t esp_isp_wbg_configure(isp_proc_handle_t isp_proc, const esp_isp_wbg_config_t *config); + +/** + * @brief Enable ISP WBG function + * + * @param[in] isp_proc Processor handle + * + * @return + * - ESP_OK On success + * - ESP_ERR_INVALID_ARG If the combination of arguments is invalid. + * - ESP_ERR_INVALID_STATE Driver state is invalid. + */ +esp_err_t esp_isp_wbg_enable(isp_proc_handle_t isp_proc); + +/** + * @brief Set AWB white balance gain + * + * @param[in] isp_proc Processor handle + * @param[in] gain WBG white balance gain + * @return + * - ESP_OK On success + * - ESP_ERR_INVALID_ARG Null pointer + * - ESP_ERR_INVALID_STATE Driver state is invalid. + */ +esp_err_t esp_isp_wbg_set_wb_gain(isp_proc_handle_t isp_proc, isp_wbg_gain_t gain); + +/** + * @brief Disable ISP WBG function + * + * @param[in] isp_proc Processor handle + * + * @return + * - ESP_OK On success + * - ESP_ERR_INVALID_ARG If the combination of arguments is invalid. + * - ESP_ERR_INVALID_STATE Driver state is invalid. + */ +esp_err_t esp_isp_wbg_disable(isp_proc_handle_t isp_proc); + +#ifdef __cplusplus +} +#endif diff --git a/components/esp_driver_isp/include/esp_private/isp_private.h b/components/esp_driver_isp/include/esp_private/isp_private.h index 452d3c2323..98ccafe5a2 100644 --- a/components/esp_driver_isp/include/esp_private/isp_private.h +++ b/components/esp_driver_isp/include/esp_private/isp_private.h @@ -78,6 +78,7 @@ typedef struct isp_processor_t { isp_fsm_t color_fsm; isp_fsm_t lsc_fsm; isp_fsm_t blc_fsm; + isp_fsm_t wbg_fsm; esp_isp_evt_cbs_t cbs; void *user_data; diff --git a/components/esp_driver_isp/src/isp_awb.c b/components/esp_driver_isp/src/isp_awb.c index e734985bcd..28ab0393bb 100644 --- a/components/esp_driver_isp/src/isp_awb.c +++ b/components/esp_driver_isp/src/isp_awb.c @@ -111,10 +111,6 @@ esp_err_t esp_isp_new_awb_controller(isp_proc_handle_t isp_proc, const esp_isp_a isp_ll_awb_enable(isp_proc->hal.hw, false); isp_ll_awb_set_clk_ctrl_mode(isp_proc->hal.hw, ISP_LL_PIPELINE_CLK_CTRL_AUTO); isp_ll_awb_enable_algorithm_mode(isp_proc->hal.hw, true); -#if !CONFIG_ESP32P4_SELECTS_REV_LESS_V3 - isp_ll_awb_set_wb_gain_clk_ctrl_mode(isp_proc->hal.hw, ISP_LL_PIPELINE_CLK_CTRL_AUTO); - isp_ll_awb_enable_wb_gain(isp_proc->hal.hw, true); -#endif ESP_GOTO_ON_ERROR(s_esp_isp_awb_config_hardware(isp_proc, awb_cfg), err2, TAG, "configure awb hardware failed"); *ret_hdl = awb_ctlr; @@ -139,9 +135,6 @@ esp_err_t esp_isp_del_awb_controller(isp_awb_ctlr_t awb_ctlr) s_isp_declaim_awb_controller(awb_ctlr); isp_ll_awb_enable_algorithm_mode(awb_ctlr->isp_proc->hal.hw, false); -#if !CONFIG_ESP32P4_SELECTS_REV_LESS_V3 - isp_ll_awb_enable_wb_gain(awb_ctlr->isp_proc->hal.hw, false); -#endif s_isp_awb_free_controller(awb_ctlr); return ESP_OK; @@ -228,17 +221,6 @@ esp_err_t esp_isp_awb_controller_stop_continuous_statistics(isp_awb_ctlr_t awb_c return ESP_OK; } -#if !CONFIG_ESP32P4_SELECTS_REV_LESS_V3 -esp_err_t esp_isp_awb_controller_set_wb_gain(isp_awb_ctlr_t awb_ctlr, isp_awb_gain_t gain) -{ - ESP_RETURN_ON_FALSE(awb_ctlr, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(atomic_load(&awb_ctlr->fsm) != ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "controller not in init state"); - isp_ll_awb_set_wb_gain(awb_ctlr->isp_proc->hal.hw, gain); - - return ESP_OK; -} -#endif //#if !CONFIG_ESP32P4_SELECTS_REV_LESS_V3 - /*--------------------------------------------------------------- INTR ---------------------------------------------------------------*/ diff --git a/components/esp_driver_isp/src/isp_wbg.c b/components/esp_driver_isp/src/isp_wbg.c new file mode 100644 index 0000000000..a80a59be4a --- /dev/null +++ b/components/esp_driver_isp/src/isp_wbg.c @@ -0,0 +1,79 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include +#include +#include +#include "sdkconfig.h" +#include "esp_log.h" +#include "esp_check.h" +#include "freertos/FreeRTOS.h" +#include "driver/isp_core.h" +#include "driver/isp_wbg.h" +#include "esp_private/isp_private.h" +#include "hal/efuse_hal.h" +#include "soc/chip_revision.h" + +/*--------------------------------------------------------------- + WBG +---------------------------------------------------------------*/ + +static const char *TAG = "ISP_WBG"; + +esp_err_t esp_isp_wbg_configure(isp_proc_handle_t isp_proc, const esp_isp_wbg_config_t *config) +{ +#if CONFIG_IDF_TARGET_ESP32P4 + unsigned chip_version = efuse_hal_chip_revision(); + if (!ESP_CHIP_REV_ABOVE(chip_version, 300)) { + ESP_RETURN_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, TAG, "WBG is not supported on ESP32P4 chips prior than v3.0"); + } +#endif + + ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); + ESP_RETURN_ON_FALSE(isp_proc->wbg_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "wbg is enabled already"); + + // Configure clock control mode + isp_ll_awb_set_wb_gain_clk_ctrl_mode(isp_proc->hal.hw, ISP_LL_PIPELINE_CLK_CTRL_AUTO); + + return ESP_OK; +} + +esp_err_t esp_isp_wbg_enable(isp_proc_handle_t isp_proc) +{ + ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); + ESP_RETURN_ON_FALSE(isp_proc->wbg_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "wbg is enabled already"); + + // Enable WBG module + isp_ll_awb_enable_wb_gain(isp_proc->hal.hw, true); + isp_proc->wbg_fsm = ISP_FSM_ENABLE; + + ESP_LOGD(TAG, "WBG enabled"); + return ESP_OK; +} + +esp_err_t esp_isp_wbg_set_wb_gain(isp_proc_handle_t isp_proc, isp_wbg_gain_t gain) +{ + ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); + ESP_RETURN_ON_FALSE(isp_proc->wbg_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "wbg isn't enabled yet"); + + // Set WBG gain + isp_ll_awb_set_wb_gain(isp_proc->hal.hw, gain); + + return ESP_OK; +} + +esp_err_t esp_isp_wbg_disable(isp_proc_handle_t isp_proc) +{ + ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); + ESP_RETURN_ON_FALSE(isp_proc->wbg_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "wbg isn't enabled yet"); + + // Disable WBG module + isp_ll_awb_enable_wb_gain(isp_proc->hal.hw, false); + isp_proc->wbg_fsm = ISP_FSM_INIT; + + ESP_LOGD(TAG, "WBG disabled"); + return ESP_OK; +} diff --git a/components/hal/esp32p4/include/hal/isp_ll.h b/components/hal/esp32p4/include/hal/isp_ll.h index 288cb3ab90..2fec895ab4 100644 --- a/components/hal/esp32p4/include/hal/isp_ll.h +++ b/components/hal/esp32p4/include/hal/isp_ll.h @@ -1742,14 +1742,29 @@ static inline void isp_ll_awb_set_wb_gain_clk_ctrl_mode(isp_dev_t *hw, isp_ll_pi * @brief Set AWB white balance gain * * @param[in] hw Hardware instance address - * @param[in] gain AWB white balance gain + * @param[in] gain WBG white balance gain */ -static inline void isp_ll_awb_set_wb_gain(isp_dev_t *hw, isp_awb_gain_t gain) +static inline void isp_ll_awb_set_wb_gain(isp_dev_t *hw, isp_wbg_gain_t gain) { hw->wbg_coef_r.wbg_r = gain.gain_r; hw->wbg_coef_g.wbg_g = gain.gain_g; hw->wbg_coef_b.wbg_b = gain.gain_b; } +#else +static inline void isp_ll_awb_enable_wb_gain(isp_dev_t *hw, bool enable) +{ + //for compatibility +} + +static inline void isp_ll_awb_set_wb_gain_clk_ctrl_mode(isp_dev_t *hw, isp_ll_pipeline_clk_ctrl_t mode) +{ + //for compatibility +} + +static inline void isp_ll_awb_set_wb_gain(isp_dev_t *hw, isp_wbg_gain_t gain) +{ + //for compatibility +} #endif //#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300 /*--------------------------------------------------------------- diff --git a/components/hal/include/hal/isp_types.h b/components/hal/include/hal/isp_types.h index 29ecffe1b0..dc72142c19 100644 --- a/components/hal/include/hal/isp_types.h +++ b/components/hal/include/hal/isp_types.h @@ -435,6 +435,18 @@ typedef union { uint32_t val; ///< 32-bit gradient ratio value } isp_lsc_gain_t; +/*--------------------------------------------------------------- + WBG (White Balance Gain) +---------------------------------------------------------------*/ +/** + * @brief ISP White Balance Gain + */ +typedef struct { + uint32_t gain_r; ///< White balance gain for R channel + uint32_t gain_g; ///< White balance gain for G channel + uint32_t gain_b; ///< White balance gain for B channel +} isp_wbg_gain_t; + #ifdef __cplusplus } #endif diff --git a/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in b/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in index 9469d37803..3771d1ed65 100644 --- a/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in @@ -919,6 +919,10 @@ config SOC_ISP_SHARPEN_SUPPORTED bool default y +config SOC_ISP_WBG_SUPPORTED + bool + default y + config SOC_ISP_SHARE_CSI_BRG bool default y diff --git a/components/soc/esp32p4/include/soc/soc_caps.h b/components/soc/esp32p4/include/soc/soc_caps.h index e65af10cd4..ea7abb9426 100644 --- a/components/soc/esp32p4/include/soc/soc_caps.h +++ b/components/soc/esp32p4/include/soc/soc_caps.h @@ -353,6 +353,7 @@ #define SOC_ISP_DVP_SUPPORTED 1 #define SOC_ISP_LSC_SUPPORTED 1 #define SOC_ISP_SHARPEN_SUPPORTED 1 +#define SOC_ISP_WBG_SUPPORTED 1 #define SOC_ISP_SHARE_CSI_BRG 1 #define SOC_ISP_NUMS 1U