feat(ldo): add ldo regulator driver for public use

This commit is contained in:
morris
2024-03-16 23:58:01 +08:00
parent 950740dc62
commit 061e5dc695
11 changed files with 342 additions and 72 deletions

View File

@@ -9,7 +9,7 @@ set(srcs "test_app_main.c"
"test_key_mgr.c"
)
if(CONFIG_SOC_MULTI_USAGE_LDO_SUPPORTED)
if(CONFIG_SOC_GP_LDO_SUPPORTED)
list(APPEND srcs "test_ldo.c")
endif()

View File

@@ -1,74 +1,89 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "unity.h"
#include "esp_private/esp_ldo.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "unity.h"
#include "esp_ldo_regulator.h"
TEST_CASE("LDO unit early / normal allocation", "[LDO]")
TEST_CASE("LDO channel acquire and release (no adjustable)", "[LDO]")
{
esp_ldo_unit_init_cfg_t init_early_unit_cfg = {
.unit_id = LDO_UNIT_3,
.cfg = {
.voltage_mv = 1800,
},
.flags.enable_unit = true,
esp_ldo_channel_handle_t success_ldo_chans[3] = {};
esp_ldo_channel_handle_t fail_ldo_chan = NULL;
esp_ldo_channel_config_t ldo_chan_config = {
.chan_id = 4,
.voltage_mv = 1800,
};
esp_ldo_unit_handle_t early_unit = esp_ldo_init_unit_early(&init_early_unit_cfg);
TEST_ASSERT(esp_ldo_enable_unit(early_unit) == ESP_ERR_INVALID_STATE);
TEST_ESP_OK(esp_ldo_disable_unit(early_unit));
esp_ldo_unit_handle_t unit = NULL;
esp_ldo_unit_init_cfg_t init_unit_cfg = {
.unit_id = LDO_UNIT_4,
.cfg = {
.voltage_mv = 2500,
},
for (int i = 0; i < 3; i++) {
TEST_ESP_OK(esp_ldo_acquire_channel(&ldo_chan_config, &success_ldo_chans[i]));
}
TEST_ASSERT_EQUAL(success_ldo_chans[0], success_ldo_chans[1]);
TEST_ASSERT_EQUAL(success_ldo_chans[0], success_ldo_chans[2]);
// can't acquire with a different voltage
ldo_chan_config.voltage_mv = 3300;
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, esp_ldo_acquire_channel(&ldo_chan_config, &fail_ldo_chan));
// the channel has been acquired as "not adjustable" before, so we can't acquire it as "adjustable" again
ldo_chan_config = (esp_ldo_channel_config_t) {
.chan_id = 4,
.voltage_mv = 1800,
.flags.adjustable = true,
};
TEST_ESP_OK(esp_ldo_init_unit(&init_unit_cfg, &unit));
TEST_ESP_OK(esp_ldo_enable_unit(unit));
TEST_ESP_OK(esp_ldo_disable_unit(unit));
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, esp_ldo_acquire_channel(&ldo_chan_config, &fail_ldo_chan));
init_unit_cfg.flags.shared_ldo = true;
esp_ldo_unit_handle_t shared_unit = NULL;
TEST_ESP_OK(esp_ldo_init_unit(&init_unit_cfg, &shared_unit));
// can't change the voltage for a non-adjustable channel
TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, esp_ldo_channel_adjust_voltage(success_ldo_chans[0], 3300));
TEST_ESP_OK(esp_ldo_deinit_unit(early_unit));
TEST_ESP_OK(esp_ldo_deinit_unit(shared_unit));
TEST_ESP_OK(esp_ldo_deinit_unit(unit));
for (int i = 0; i < 3; i++) {
TEST_ESP_OK(esp_ldo_release_channel(success_ldo_chans[i]));
}
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_ldo_release_channel(success_ldo_chans[0]));
}
TEST_CASE("LDO unit output", "[LDO][mannual][ignore]")
TEST_CASE("LDO channel acquire and release (adjustable)", "[LDO]")
{
esp_ldo_unit_init_cfg_t early_unit_cfg = {
.unit_id = LDO_UNIT_2,
.cfg = {
.voltage_mv = 1800,
},
.flags.shared_ldo = true,
.flags.enable_unit = true,
esp_ldo_channel_handle_t success_ldo_chan = NULL;
esp_ldo_channel_handle_t fail_ldo_chan = NULL;
esp_ldo_channel_config_t ldo_chan_config = {
.chan_id = 4,
.voltage_mv = 1800,
.flags.adjustable = true,
};
esp_ldo_unit_handle_t early_unit2 = esp_ldo_init_unit_early(&early_unit_cfg);
assert(early_unit2);
TEST_ESP_OK(esp_ldo_acquire_channel(&ldo_chan_config, &success_ldo_chan));
early_unit_cfg.unit_id = LDO_UNIT_3;
early_unit_cfg.cfg.voltage_mv = 3300;
esp_ldo_unit_handle_t early_unit3 = esp_ldo_init_unit_early(&early_unit_cfg);
assert(early_unit3);
// can't acquire multiple handles for the same adjustable channel
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, esp_ldo_acquire_channel(&ldo_chan_config, &fail_ldo_chan));
// even we acquire another copy as non-adjustable, it's still not allowed
ldo_chan_config.flags.adjustable = false;
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, esp_ldo_acquire_channel(&ldo_chan_config, &fail_ldo_chan));
early_unit_cfg.unit_id = LDO_UNIT_4;
early_unit_cfg.cfg.voltage_mv = 1100;
esp_ldo_unit_handle_t early_unit4 = esp_ldo_init_unit_early(&early_unit_cfg);
assert(early_unit4);
// can change voltage for an adjustable channel
TEST_ESP_OK(esp_ldo_channel_adjust_voltage(success_ldo_chan, 3300));
TEST_ESP_OK(esp_ldo_release_channel(success_ldo_chan));
}
esp_ldo_usage_dump(stdout);
TEST_CASE("LDO channel state dump", "[LDO][manual][ignore]")
{
esp_ldo_channel_handle_t success_ldo_chans[3] = {};
esp_ldo_channel_config_t ldo_chan_config = {
.chan_id = 2,
.voltage_mv = 1800,
};
TEST_ESP_OK(esp_ldo_acquire_channel(&ldo_chan_config, &success_ldo_chans[0]));
ldo_chan_config.chan_id = 3;
ldo_chan_config.voltage_mv = 2500;
TEST_ESP_OK(esp_ldo_acquire_channel(&ldo_chan_config, &success_ldo_chans[1]));
ldo_chan_config.chan_id = 4;
ldo_chan_config.voltage_mv = 1100;
TEST_ESP_OK(esp_ldo_acquire_channel(&ldo_chan_config, &success_ldo_chans[2]));
esp_ldo_dump(stdout);
while (1) {
vTaskDelay(1);