|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <stdatomic.h> |
| 10 | +#include "sdkconfig.h" |
| 11 | +#include "esp_log.h" |
| 12 | +#include "esp_check.h" |
| 13 | +#include "freertos/FreeRTOS.h" |
| 14 | +#include "driver/isp_core.h" |
| 15 | +#include "driver/isp_wbg.h" |
| 16 | +#include "esp_private/isp_private.h" |
| 17 | +#include "hal/efuse_hal.h" |
| 18 | +#include "soc/chip_revision.h" |
| 19 | + |
| 20 | +/*--------------------------------------------------------------- |
| 21 | + WBG |
| 22 | +---------------------------------------------------------------*/ |
| 23 | + |
| 24 | +static const char *TAG = "ISP_WBG"; |
| 25 | + |
| 26 | +esp_err_t esp_isp_wbg_configure(isp_proc_handle_t isp_proc, const esp_isp_wbg_config_t *config) |
| 27 | +{ |
| 28 | +#if CONFIG_IDF_TARGET_ESP32P4 |
| 29 | + unsigned chip_version = efuse_hal_chip_revision(); |
| 30 | + if (!ESP_CHIP_REV_ABOVE(chip_version, 300)) { |
| 31 | + ESP_RETURN_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, TAG, "WBG is not supported on ESP32P4 chips prior than v3.0"); |
| 32 | + } |
| 33 | +#endif |
| 34 | + |
| 35 | + ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); |
| 36 | + ESP_RETURN_ON_FALSE(isp_proc->wbg_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "wbg is enabled already"); |
| 37 | + |
| 38 | + // Configure clock control mode |
| 39 | + isp_ll_awb_set_wb_gain_clk_ctrl_mode(isp_proc->hal.hw, ISP_LL_PIPELINE_CLK_CTRL_AUTO); |
| 40 | + |
| 41 | + return ESP_OK; |
| 42 | +} |
| 43 | + |
| 44 | +esp_err_t esp_isp_wbg_enable(isp_proc_handle_t isp_proc) |
| 45 | +{ |
| 46 | + ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); |
| 47 | + ESP_RETURN_ON_FALSE(isp_proc->wbg_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "wbg is enabled already"); |
| 48 | + |
| 49 | + // Enable WBG module |
| 50 | + isp_ll_awb_enable_wb_gain(isp_proc->hal.hw, true); |
| 51 | + isp_proc->wbg_fsm = ISP_FSM_ENABLE; |
| 52 | + |
| 53 | + ESP_LOGD(TAG, "WBG enabled"); |
| 54 | + return ESP_OK; |
| 55 | +} |
| 56 | + |
| 57 | +esp_err_t esp_isp_wbg_set_wb_gain(isp_proc_handle_t isp_proc, isp_wbg_gain_t gain) |
| 58 | +{ |
| 59 | + ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); |
| 60 | + ESP_RETURN_ON_FALSE(isp_proc->wbg_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "wbg isn't enabled yet"); |
| 61 | + |
| 62 | + // Set WBG gain |
| 63 | + isp_ll_awb_set_wb_gain(isp_proc->hal.hw, gain); |
| 64 | + |
| 65 | + return ESP_OK; |
| 66 | +} |
| 67 | + |
| 68 | +esp_err_t esp_isp_wbg_disable(isp_proc_handle_t isp_proc) |
| 69 | +{ |
| 70 | + ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); |
| 71 | + ESP_RETURN_ON_FALSE(isp_proc->wbg_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "wbg isn't enabled yet"); |
| 72 | + |
| 73 | + // Disable WBG module |
| 74 | + isp_ll_awb_enable_wb_gain(isp_proc->hal.hw, false); |
| 75 | + isp_proc->wbg_fsm = ISP_FSM_INIT; |
| 76 | + |
| 77 | + ESP_LOGD(TAG, "WBG disabled"); |
| 78 | + return ESP_OK; |
| 79 | +} |
0 commit comments