diff --git a/components/esp_hw_support/include/esp_private/esp_ldo.h b/components/esp_hw_support/include/esp_private/esp_ldo.h index efde4d84aa..9848665911 100644 --- a/components/esp_hw_support/include/esp_private/esp_ldo.h +++ b/components/esp_hw_support/include/esp_private/esp_ldo.h @@ -31,7 +31,7 @@ typedef struct { * @brief LDO driver initial configurations */ typedef struct { - int unit_id; ///< LDO unit + int unit_id; ///< LDO ID, this is aligned with datasheet, e.g. you should set this to 1, if using LDO ID 1 esp_ldo_unit_cfg_t cfg; ///< LDO unit configuration struct { uint32_t enable_unit: 1; ///< Enable the LDO unit after it's initialised diff --git a/components/esp_hw_support/ldo/esp_ldo.c b/components/esp_hw_support/ldo/esp_ldo.c index 6003fec4f8..dfc139fe82 100644 --- a/components/esp_hw_support/ldo/esp_ldo.c +++ b/components/esp_hw_support/ldo/esp_ldo.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -47,22 +47,23 @@ static bool s_ldo_unit_needs_claim(const esp_ldo_unit_init_cfg_t *init_config); esp_ldo_unit_handle_t esp_ldo_init_unit_early(const esp_ldo_unit_init_cfg_t *init_config) { assert(init_config); - assert(init_config->unit_id < LDO_LL_UNIT_NUM); + assert(ldo_ll_is_valid_ldo_id(init_config->unit_id)); - ldo_unit_ctx_t *unit = &s_ctx.units[init_config->unit_id]; + int unit_id = LDO_ID2UNIT(init_config->unit_id); + ldo_unit_ctx_t *unit = &s_ctx.units[unit_id]; bool needs_claim = s_ldo_unit_needs_claim(init_config); bool success_claim = false; if (needs_claim) { - success_claim = s_ldo_unit_claim(init_config->unit_id); + success_claim = s_ldo_unit_claim(unit_id); assert(success_claim); - unit->unit_id = init_config->unit_id; + unit->unit_id = unit_id; unit->voltage_mv = init_config->cfg.voltage_mv; unit->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED; - ldo_ll_set_output_voltage_mv(init_config->unit_id, init_config->cfg.voltage_mv); + ldo_ll_set_output_voltage_mv(unit_id, init_config->cfg.voltage_mv); if (init_config->flags.enable_unit) { - ldo_ll_enable(init_config->unit_id, true); + ldo_ll_enable(unit_id, true); unit->enabled = true; } } else { @@ -78,28 +79,29 @@ esp_ldo_unit_handle_t esp_ldo_init_unit_early(const esp_ldo_unit_init_cfg_t *ini esp_err_t esp_ldo_init_unit(const esp_ldo_unit_init_cfg_t *init_config, esp_ldo_unit_handle_t *ret_unit) { ESP_RETURN_ON_FALSE(init_config && ret_unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); - ESP_RETURN_ON_FALSE(init_config->unit_id < LDO_LL_UNIT_NUM, ESP_ERR_INVALID_ARG, TAG, "invalid unit"); + ESP_RETURN_ON_FALSE(ldo_ll_is_valid_ldo_id(init_config->unit_id), ESP_ERR_INVALID_ARG, TAG, "invalid ldo id"); - ldo_unit_ctx_t *unit = &s_ctx.units[init_config->unit_id]; + int unit_id = LDO_ID2UNIT(init_config->unit_id); + ldo_unit_ctx_t *unit = &s_ctx.units[unit_id]; bool needs_claim = s_ldo_unit_needs_claim(init_config); bool success_claim = false; if (needs_claim) { - success_claim = s_ldo_unit_claim(init_config->unit_id); - ESP_RETURN_ON_FALSE(success_claim, ESP_ERR_NOT_FOUND, TAG, "ldo%d is already in use", init_config->unit_id + 1); - unit->unit_id = init_config->unit_id; + success_claim = s_ldo_unit_claim(unit_id); + ESP_RETURN_ON_FALSE(success_claim, ESP_ERR_NOT_FOUND, TAG, "ldo%d is already in use", init_config->unit_id); + unit->unit_id = unit_id; unit->voltage_mv = init_config->cfg.voltage_mv; unit->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED; - ldo_ll_set_output_voltage_mv(init_config->unit_id, init_config->cfg.voltage_mv); + ldo_ll_set_output_voltage_mv(unit_id, init_config->cfg.voltage_mv); if (init_config->flags.enable_unit) { - ldo_ll_enable(init_config->unit_id, true); + ldo_ll_enable(unit_id, true); unit->enabled = true; } - ESP_LOGD(TAG, "new ldo unit%d is created", unit->unit_id); + ESP_LOGD(TAG, "new ldo %d is created", unit->unit_id + 1); } else { bool same_voltage = init_config->cfg.voltage_mv == unit->voltage_mv; - ESP_RETURN_ON_FALSE(same_voltage, ESP_ERR_INVALID_ARG, TAG, "not same voltage, cannot share ldo%d", init_config->unit_id + 1); - ESP_LOGD(TAG, "new ldo unit%d is shared", unit->unit_id); + ESP_RETURN_ON_FALSE(same_voltage, ESP_ERR_INVALID_ARG, TAG, "not same voltage, cannot share ldo%d", init_config->unit_id); + ESP_LOGD(TAG, "new ldo %d is shared", unit->unit_id + 1); } portENTER_CRITICAL(&unit->spinlock); @@ -195,8 +197,9 @@ static bool s_ldo_unit_free(uint32_t unit) static bool s_ldo_unit_needs_claim(const esp_ldo_unit_init_cfg_t *init_config) { bool needs_claim = false; + int unit_id = LDO_ID2UNIT(init_config->unit_id); - if (s_ctx.units[init_config->unit_id].ref_cnt == 0) { + if (s_ctx.units[unit_id].ref_cnt == 0) { needs_claim = true; } else { if (!init_config->flags.shared_ldo) { diff --git a/components/esp_hw_support/ldo/esp_ldo_psram.c b/components/esp_hw_support/ldo/esp_ldo_psram.c index f65a0ce0af..b329fd2171 100644 --- a/components/esp_hw_support/ldo/esp_ldo_psram.c +++ b/components/esp_hw_support/ldo/esp_ldo_psram.c @@ -19,7 +19,7 @@ void esp_ldo_vdd_psram_early_init(void) { if (CONFIG_ESP_VDD_PSRAM_LDO_ID != -1) { esp_ldo_unit_init_cfg_t unit_cfg = { - .unit_id = LDO_ID2UNIT(CONFIG_ESP_VDD_PSRAM_LDO_ID), + .unit_id = CONFIG_ESP_VDD_PSRAM_LDO_ID, .cfg = { .voltage_mv = CONFIG_ESP_VDD_PSRAM_LDO_VOLTAGE_MV, }, diff --git a/components/esp_hw_support/test_apps/esp_hw_support_unity_tests/main/test_ldo.c b/components/esp_hw_support/test_apps/esp_hw_support_unity_tests/main/test_ldo.c index fe0e693c7d..546f9baed4 100644 --- a/components/esp_hw_support/test_apps/esp_hw_support_unity_tests/main/test_ldo.c +++ b/components/esp_hw_support/test_apps/esp_hw_support_unity_tests/main/test_ldo.c @@ -50,8 +50,9 @@ TEST_CASE("LDO unit output", "[LDO][mannual][ignore]") esp_ldo_unit_init_cfg_t early_unit_cfg = { .unit_id = LDO_UNIT_2, .cfg = { - .voltage_mv = 2500, + .voltage_mv = 1800, }, + .flags.shared_ldo = true, .flags.enable_unit = true, }; esp_ldo_unit_handle_t early_unit2 = esp_ldo_init_unit_early(&early_unit_cfg); diff --git a/components/hal/esp32p4/include/hal/ldo_ll.h b/components/hal/esp32p4/include/hal/ldo_ll.h index e3d2ce4bd0..5e58532039 100644 --- a/components/hal/esp32p4/include/hal/ldo_ll.h +++ b/components/hal/esp32p4/include/hal/ldo_ll.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -54,6 +54,17 @@ extern "C" { */ #define LDO_ID2UNIT(ldo_id) ((ldo_id) - 1) +/** + * @brief Check if a LDO ID is valid + * + * @return True for valid + */ +__attribute__((always_inline)) +static inline bool ldo_ll_is_valid_ldo_id(int ldo_id) +{ + return ((ldo_id > 0) && (ldo_id <= LDO_LL_UNIT_NUM)); +} + /** * @brief Enable a LDO * diff --git a/components/hal/include/hal/ldo_types.h b/components/hal/include/hal/ldo_types.h index f4b8c47d69..600e2c4366 100644 --- a/components/hal/include/hal/ldo_types.h +++ b/components/hal/include/hal/ldo_types.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -17,10 +17,10 @@ extern "C" { * @note See datasheet to know LDO, alias includes but not least to `VFB/VOn` */ typedef enum { - LDO_UNIT_1, ///< LDO 1 - LDO_UNIT_2, ///< LDO 2 - LDO_UNIT_3, ///< LDO 3 - LDO_UNIT_4, ///< LDO 4 + LDO_UNIT_1 = 1, ///< LDO 1 + LDO_UNIT_2, ///< LDO 2 + LDO_UNIT_3, ///< LDO 3 + LDO_UNIT_4, ///< LDO 4 } ldo_unit_t; #ifdef __cplusplus