From 666098804ac6d74dc10671ee425d528c56bc2e67 Mon Sep 17 00:00:00 2001 From: armando Date: Mon, 27 Oct 2025 11:01:35 +0800 Subject: [PATCH 1/2] 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 ccb8d58c87..75f7ac6453 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 2a64a7ea71..5de9bdb149 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 From 06b35ab86be8fbc150517f30c8ba2f5dc1b6f0a4 Mon Sep 17 00:00:00 2001 From: armando Date: Mon, 27 Oct 2025 15:12:30 +0800 Subject: [PATCH 2/2] fix(isp): use atomic fsm check --- .../include/esp_private/isp_private.h | 31 +++++++++++++------ components/esp_driver_isp/src/isp_bf.c | 10 +++--- components/esp_driver_isp/src/isp_blc.c | 10 +++--- components/esp_driver_isp/src/isp_ccm.c | 4 +++ components/esp_driver_isp/src/isp_color.c | 10 +++--- components/esp_driver_isp/src/isp_core.c | 23 +++++++++----- components/esp_driver_isp/src/isp_demosaic.c | 10 +++--- components/esp_driver_isp/src/isp_gamma.c | 4 +++ components/esp_driver_isp/src/isp_lsc.c | 10 +++--- components/esp_driver_isp/src/isp_sharpen.c | 10 +++--- components/esp_driver_isp/src/isp_wbg.c | 11 +++---- 11 files changed, 80 insertions(+), 53 deletions(-) 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 98ccafe5a2..e94fe16e64 100644 --- a/components/esp_driver_isp/include/esp_private/isp_private.h +++ b/components/esp_driver_isp/include/esp_private/isp_private.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,6 +9,7 @@ #include #include #include +#include #include "sdkconfig.h" #include "esp_attr.h" #include "esp_log.h" @@ -27,8 +28,12 @@ #include "soc/isp_periph.h" #endif +// Helper macros for atomic operations to ensure Clang compatibility #ifdef __cplusplus -extern "C" { +#include +#define ISP_ATOMIC_TYPE(T) std::atomic +#else +#define ISP_ATOMIC_TYPE(T) _Atomic T #endif #if CONFIG_ISP_ISR_IRAM_SAFE || CONFIG_ISP_CTRL_FUNC_IN_IRAM @@ -39,6 +44,10 @@ extern "C" { #define ISP_MEM_ALLOC_CAPS MALLOC_CAP_DEFAULT #endif +#ifdef __cplusplus +extern "C" { +#endif + typedef enum { ISP_FSM_INIT, // Controller is initialized, but not enabled ISP_FSM_ENABLE, // Controller is enabled, but is not running @@ -59,7 +68,7 @@ typedef struct isp_processor_t { int csi_brg_id; void *csi_brg_hw; #endif - isp_fsm_t isp_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) isp_fsm; portMUX_TYPE spinlock; color_space_pixel_format_t in_color_format; color_space_pixel_format_t out_color_format; @@ -72,13 +81,15 @@ typedef struct isp_processor_t { isp_awb_ctlr_t awb_ctlr; isp_ae_ctlr_t ae_ctlr; isp_hist_ctlr_t hist_ctlr; - isp_fsm_t bf_fsm; - isp_fsm_t demosaic_fsm; - isp_fsm_t sharpen_fsm; - isp_fsm_t color_fsm; - isp_fsm_t lsc_fsm; - isp_fsm_t blc_fsm; - isp_fsm_t wbg_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) bf_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) blc_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) ccm_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) color_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) demosaic_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) gamma_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) lsc_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) sharpen_fsm; + ISP_ATOMIC_TYPE(isp_fsm_t) wbg_fsm; esp_isp_evt_cbs_t cbs; void *user_data; diff --git a/components/esp_driver_isp/src/isp_bf.c b/components/esp_driver_isp/src/isp_bf.c index f6f0befc2f..066076624f 100644 --- a/components/esp_driver_isp/src/isp_bf.c +++ b/components/esp_driver_isp/src/isp_bf.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -49,10 +49,10 @@ esp_err_t esp_isp_bf_configure(isp_proc_handle_t proc, const esp_isp_bf_config_t esp_err_t esp_isp_bf_enable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->bf_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "bf is enabled already"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->bf_fsm, &expected_fsm, ISP_FSM_ENABLE), ESP_ERR_INVALID_STATE, TAG, "bf is enabled already"); isp_ll_bf_enable(proc->hal.hw, true); - proc->bf_fsm = ISP_FSM_ENABLE; return ESP_OK; } @@ -60,10 +60,10 @@ esp_err_t esp_isp_bf_enable(isp_proc_handle_t proc) esp_err_t esp_isp_bf_disable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->bf_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "bf isn't enabled yet"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->bf_fsm, &expected_fsm, ISP_FSM_INIT), ESP_ERR_INVALID_STATE, TAG, "bf isn't enabled yet"); isp_ll_bf_enable(proc->hal.hw, false); - proc->bf_fsm = ISP_FSM_INIT; return ESP_OK; } diff --git a/components/esp_driver_isp/src/isp_blc.c b/components/esp_driver_isp/src/isp_blc.c index bad515da5e..c808e66d04 100644 --- a/components/esp_driver_isp/src/isp_blc.c +++ b/components/esp_driver_isp/src/isp_blc.c @@ -64,11 +64,11 @@ esp_err_t esp_isp_blc_configure(isp_proc_handle_t isp_proc, const esp_isp_blc_co esp_err_t esp_isp_blc_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->blc_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "blc is enabled already"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&isp_proc->blc_fsm, &expected_fsm, ISP_FSM_ENABLE), ESP_ERR_INVALID_STATE, TAG, "blc is enabled already"); // Enable BLC module isp_ll_blc_enable(isp_proc->hal.hw, true); - isp_proc->blc_fsm = ISP_FSM_ENABLE; ESP_LOGD(TAG, "BLC enabled"); return ESP_OK; @@ -77,7 +77,7 @@ esp_err_t esp_isp_blc_enable(isp_proc_handle_t isp_proc) esp_err_t esp_isp_blc_set_correction_offset(isp_proc_handle_t isp_proc, esp_isp_blc_offset_t *offset) { ESP_RETURN_ON_FALSE(isp_proc && offset, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(isp_proc->blc_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "blc isn't enabled yet"); + ESP_RETURN_ON_FALSE(atomic_load(&isp_proc->blc_fsm) == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "blc isn't enabled yet"); // Set correction offset for each channel isp_ll_blc_set_correction_offset(isp_proc->hal.hw, offset->top_left_chan_offset, offset->top_right_chan_offset, offset->bottom_left_chan_offset, offset->bottom_right_chan_offset); @@ -92,11 +92,11 @@ esp_err_t esp_isp_blc_set_correction_offset(isp_proc_handle_t isp_proc, esp_isp_ esp_err_t esp_isp_blc_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->blc_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "blc isn't enabled yet"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&isp_proc->blc_fsm, &expected_fsm, ISP_FSM_INIT), ESP_ERR_INVALID_STATE, TAG, "blc isn't enabled yet"); // Disable BLC module isp_ll_blc_enable(isp_proc->hal.hw, false); - isp_proc->blc_fsm = ISP_FSM_INIT; ESP_LOGD(TAG, "BLC disabled"); return ESP_OK; diff --git a/components/esp_driver_isp/src/isp_ccm.c b/components/esp_driver_isp/src/isp_ccm.c index e47af37025..b8b28bad44 100644 --- a/components/esp_driver_isp/src/isp_ccm.c +++ b/components/esp_driver_isp/src/isp_ccm.c @@ -45,6 +45,8 @@ esp_err_t esp_isp_ccm_configure(isp_proc_handle_t proc, const esp_isp_ccm_config esp_err_t esp_isp_ccm_enable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->ccm_fsm, &expected_fsm, ISP_FSM_ENABLE), ESP_ERR_INVALID_STATE, TAG, "ccm is enabled already"); portENTER_CRITICAL(&proc->spinlock); isp_ll_ccm_enable(proc->hal.hw, true); @@ -56,6 +58,8 @@ esp_err_t esp_isp_ccm_enable(isp_proc_handle_t proc) esp_err_t esp_isp_ccm_disable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->ccm_fsm, &expected_fsm, ISP_FSM_INIT), ESP_ERR_INVALID_STATE, TAG, "ccm isn't enabled yet"); portENTER_CRITICAL(&proc->spinlock); isp_ll_ccm_enable(proc->hal.hw, false); diff --git a/components/esp_driver_isp/src/isp_color.c b/components/esp_driver_isp/src/isp_color.c index cfd8227c89..d10504e3f2 100644 --- a/components/esp_driver_isp/src/isp_color.c +++ b/components/esp_driver_isp/src/isp_color.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -49,10 +49,10 @@ esp_err_t esp_isp_color_configure(isp_proc_handle_t proc, const esp_isp_color_co esp_err_t esp_isp_color_enable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->color_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "color is enabled already"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->color_fsm, &expected_fsm, ISP_FSM_ENABLE), ESP_ERR_INVALID_STATE, TAG, "color is enabled already"); isp_ll_color_enable(proc->hal.hw, true); - proc->color_fsm = ISP_FSM_ENABLE; return ESP_OK; } @@ -60,10 +60,10 @@ esp_err_t esp_isp_color_enable(isp_proc_handle_t proc) esp_err_t esp_isp_color_disable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->color_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "color isn't enabled yet"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->color_fsm, &expected_fsm, ISP_FSM_INIT), ESP_ERR_INVALID_STATE, TAG, "color isn't enabled yet"); isp_ll_color_enable(proc->hal.hw, false); - proc->color_fsm = ISP_FSM_INIT; return ESP_OK; } diff --git a/components/esp_driver_isp/src/isp_core.c b/components/esp_driver_isp/src/isp_core.c index d6dd2bbeae..b1d1b45e14 100644 --- a/components/esp_driver_isp/src/isp_core.c +++ b/components/esp_driver_isp/src/isp_core.c @@ -119,7 +119,16 @@ esp_err_t esp_isp_new_processor(const esp_isp_processor_cfg_t *proc_config, isp_ isp_ll_set_clock_div(proc->hal.hw, &clk_div); } proc->clk_src = clk_src; - proc->isp_fsm = ISP_FSM_INIT; + atomic_init(&proc->isp_fsm, ISP_FSM_INIT); + atomic_init(&proc->bf_fsm, ISP_FSM_INIT); + atomic_init(&proc->blc_fsm, ISP_FSM_INIT); + atomic_init(&proc->ccm_fsm, ISP_FSM_INIT); + atomic_init(&proc->color_fsm, ISP_FSM_INIT); + atomic_init(&proc->demosaic_fsm, ISP_FSM_INIT); + atomic_init(&proc->gamma_fsm, ISP_FSM_INIT); + atomic_init(&proc->lsc_fsm, ISP_FSM_INIT); + atomic_init(&proc->sharpen_fsm, ISP_FSM_INIT); + atomic_init(&proc->wbg_fsm, ISP_FSM_INIT); proc->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED; //Input & Output color format @@ -189,7 +198,7 @@ err: esp_err_t esp_isp_del_processor(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->isp_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "processor isn't in init state"); + ESP_RETURN_ON_FALSE(atomic_load(&proc->isp_fsm) == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "processor isn't in init state"); //declaim first, then do free ESP_RETURN_ON_ERROR(s_isp_declaim_processor(proc), TAG, "declaim processor fail"); @@ -205,7 +214,7 @@ esp_err_t esp_isp_del_processor(isp_proc_handle_t proc) esp_err_t esp_isp_register_event_callbacks(isp_proc_handle_t proc, const esp_isp_evt_cbs_t *cbs, void *user_data) { ESP_RETURN_ON_FALSE(proc && cbs, ESP_ERR_INVALID_ARG, TAG, "invalid argument"); - ESP_RETURN_ON_FALSE(proc->isp_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "processor isn't in the init state"); + ESP_RETURN_ON_FALSE(atomic_load(&proc->isp_fsm) == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "processor isn't in the init state"); #if CONFIG_ISP_ISR_IRAM_SAFE if (cbs->on_sharpen_frame_done) { @@ -230,11 +239,11 @@ esp_err_t esp_isp_register_event_callbacks(isp_proc_handle_t proc, const esp_isp esp_err_t esp_isp_enable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->isp_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "processor isn't in init state"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->isp_fsm, &expected_fsm, ISP_FSM_ENABLE), ESP_ERR_INVALID_STATE, TAG, "processor isn't in init state"); ESP_RETURN_ON_FALSE(proc->bypass_isp == false, ESP_ERR_INVALID_STATE, TAG, "processor is configured to be bypassed"); isp_ll_enable(proc->hal.hw, true); - proc->isp_fsm = ISP_FSM_ENABLE; return ESP_OK; } @@ -242,10 +251,10 @@ esp_err_t esp_isp_enable(isp_proc_handle_t proc) esp_err_t esp_isp_disable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->isp_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "processor isn't in enable state"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->isp_fsm, &expected_fsm, ISP_FSM_INIT), ESP_ERR_INVALID_STATE, TAG, "processor isn't in enable state"); isp_ll_enable(proc->hal.hw, false); - proc->isp_fsm = ISP_FSM_INIT; return ESP_OK; } diff --git a/components/esp_driver_isp/src/isp_demosaic.c b/components/esp_driver_isp/src/isp_demosaic.c index 92a224caae..37f299bd71 100644 --- a/components/esp_driver_isp/src/isp_demosaic.c +++ b/components/esp_driver_isp/src/isp_demosaic.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -46,10 +46,10 @@ esp_err_t esp_isp_demosaic_configure(isp_proc_handle_t proc, const esp_isp_demos esp_err_t esp_isp_demosaic_enable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->demosaic_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "demosaic is enabled already"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->demosaic_fsm, &expected_fsm, ISP_FSM_ENABLE), ESP_ERR_INVALID_STATE, TAG, "demosaic is enabled already"); isp_ll_demosaic_enable(proc->hal.hw, true); - proc->demosaic_fsm = ISP_FSM_ENABLE; return ESP_OK; } @@ -57,13 +57,13 @@ esp_err_t esp_isp_demosaic_enable(isp_proc_handle_t proc) esp_err_t esp_isp_demosaic_disable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->demosaic_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "demosaic isn't enabled yet"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->demosaic_fsm, &expected_fsm, ISP_FSM_INIT), ESP_ERR_INVALID_STATE, TAG, "demosaic isn't enabled yet"); if (proc->out_color_format.color_space == (uint32_t)COLOR_SPACE_RAW) { // for other out_color_format, demosaic module is needed for rgb interpolation algorithm isp_ll_demosaic_enable(proc->hal.hw, false); } - proc->demosaic_fsm = ISP_FSM_INIT; return ESP_OK; } diff --git a/components/esp_driver_isp/src/isp_gamma.c b/components/esp_driver_isp/src/isp_gamma.c index c8d323f416..52b77ba9d6 100644 --- a/components/esp_driver_isp/src/isp_gamma.c +++ b/components/esp_driver_isp/src/isp_gamma.c @@ -58,6 +58,8 @@ esp_err_t esp_isp_gamma_configure(isp_proc_handle_t proc, color_component_t comp esp_err_t esp_isp_gamma_enable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->gamma_fsm, &expected_fsm, ISP_FSM_ENABLE), ESP_ERR_INVALID_STATE, TAG, "gamma is enabled already"); portENTER_CRITICAL(&proc->spinlock); isp_ll_gamma_enable(proc->hal.hw, true); @@ -69,6 +71,8 @@ esp_err_t esp_isp_gamma_enable(isp_proc_handle_t proc) esp_err_t esp_isp_gamma_disable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->gamma_fsm, &expected_fsm, ISP_FSM_INIT), ESP_ERR_INVALID_STATE, TAG, "gamma is disabled already"); portENTER_CRITICAL(&proc->spinlock); isp_ll_gamma_enable(proc->hal.hw, false); diff --git a/components/esp_driver_isp/src/isp_lsc.c b/components/esp_driver_isp/src/isp_lsc.c index 758d780758..8ddf94126d 100644 --- a/components/esp_driver_isp/src/isp_lsc.c +++ b/components/esp_driver_isp/src/isp_lsc.c @@ -28,7 +28,7 @@ static const char *TAG = "ISP_LSC"; esp_err_t esp_isp_lsc_allocate_gain_array(isp_proc_handle_t isp_proc, esp_isp_lsc_gain_array_t *gain_array, size_t *out_array_size_per_channel) { ESP_RETURN_ON_FALSE(isp_proc && gain_array && out_array_size_per_channel, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(isp_proc->lsc_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "lsc is enabled already"); + ESP_RETURN_ON_FALSE(atomic_load(&isp_proc->lsc_fsm) == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "lsc is enabled already"); int num_grids_x_max = ISP_LSC_GET_GRIDS(ISP_LL_HSIZE_MAX); int num_grids_y_max = ISP_LSC_GET_GRIDS(ISP_LL_VSIZE_MAX); @@ -89,10 +89,10 @@ esp_err_t esp_isp_lsc_configure(isp_proc_handle_t isp_proc, const esp_isp_lsc_co esp_err_t esp_isp_lsc_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->lsc_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "lsc is enabled already"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&isp_proc->lsc_fsm, &expected_fsm, ISP_FSM_ENABLE), ESP_ERR_INVALID_STATE, TAG, "lsc is enabled already"); isp_ll_lsc_enable(isp_proc->hal.hw, true); - isp_proc->lsc_fsm = ISP_FSM_ENABLE; return ESP_OK; } @@ -100,10 +100,10 @@ esp_err_t esp_isp_lsc_enable(isp_proc_handle_t isp_proc) esp_err_t esp_isp_lsc_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->lsc_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "lsc isn't enabled yet"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&isp_proc->lsc_fsm, &expected_fsm, ISP_FSM_INIT), ESP_ERR_INVALID_STATE, TAG, "lsc isn't enabled yet"); isp_ll_lsc_enable(isp_proc->hal.hw, false); - isp_proc->lsc_fsm = ISP_FSM_INIT; return ESP_OK; } diff --git a/components/esp_driver_isp/src/isp_sharpen.c b/components/esp_driver_isp/src/isp_sharpen.c index 750a5adb13..b8ac88abeb 100644 --- a/components/esp_driver_isp/src/isp_sharpen.c +++ b/components/esp_driver_isp/src/isp_sharpen.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -51,11 +51,11 @@ esp_err_t esp_isp_sharpen_configure(isp_proc_handle_t proc, const esp_isp_sharpe esp_err_t esp_isp_sharpen_enable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->sharpen_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "sharpen is enabled already"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->sharpen_fsm, &expected_fsm, ISP_FSM_ENABLE), ESP_ERR_INVALID_STATE, TAG, "sharpen is enabled already"); isp_ll_enable_intr(proc->hal.hw, ISP_LL_EVENT_SHARP_FRAME, true); isp_ll_sharp_enable(proc->hal.hw, true); - proc->sharpen_fsm = ISP_FSM_ENABLE; return ESP_OK; } @@ -63,11 +63,11 @@ esp_err_t esp_isp_sharpen_enable(isp_proc_handle_t proc) esp_err_t esp_isp_sharpen_disable(isp_proc_handle_t proc) { ESP_RETURN_ON_FALSE(proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(proc->sharpen_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "sharpen isn't enabled yet"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&proc->sharpen_fsm, &expected_fsm, ISP_FSM_INIT), ESP_ERR_INVALID_STATE, TAG, "sharpen isn't enabled yet"); isp_ll_sharp_enable(proc->hal.hw, false); isp_ll_enable_intr(proc->hal.hw, ISP_LL_EVENT_SHARP_FRAME, false); - proc->sharpen_fsm = ISP_FSM_INIT; return ESP_OK; } diff --git a/components/esp_driver_isp/src/isp_wbg.c b/components/esp_driver_isp/src/isp_wbg.c index a80a59be4a..e03d0eda52 100644 --- a/components/esp_driver_isp/src/isp_wbg.c +++ b/components/esp_driver_isp/src/isp_wbg.c @@ -33,7 +33,6 @@ esp_err_t esp_isp_wbg_configure(isp_proc_handle_t isp_proc, const esp_isp_wbg_co #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); @@ -44,11 +43,11 @@ esp_err_t esp_isp_wbg_configure(isp_proc_handle_t isp_proc, const esp_isp_wbg_co 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"); + isp_fsm_t expected_fsm = ISP_FSM_INIT; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&isp_proc->wbg_fsm, &expected_fsm, ISP_FSM_ENABLE), 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; @@ -57,7 +56,7 @@ esp_err_t esp_isp_wbg_enable(isp_proc_handle_t isp_proc) 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"); + ESP_RETURN_ON_FALSE(atomic_load(&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); @@ -68,11 +67,11 @@ esp_err_t esp_isp_wbg_set_wb_gain(isp_proc_handle_t isp_proc, isp_wbg_gain_t gai 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"); + isp_fsm_t expected_fsm = ISP_FSM_ENABLE; + ESP_RETURN_ON_FALSE(atomic_compare_exchange_strong(&isp_proc->wbg_fsm, &expected_fsm, ISP_FSM_INIT), 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;