VDDBAT Under-Voltage Protection

[中文]

Overview

Over-discharging a lithium battery causes irreversible capacity loss. On ESP32-H21, the VDDBAT under-voltage protection driver detects when the VDDBAT voltage drops below a fixed threshold and notifies the application through a callback. The application then handles the event and enters Deep-sleep until the battery is recharged and recovers above the VDDBAT threshold.

Note

The VDDBAT under-voltage protection driver only reports the under-voltage. It does not enter Deep-sleep, stop peripherals, or reset the chip by itself.

Driver Introduction

Before using the VDDBAT under-voltage protection driver, you need to understand how the detection is confirmed and what each of the two state functions represents.

Working Principle

Under-voltage protection compares VDDBAT with a threshold of about 2.96 V. A hardware filter debounces the comparator before the driver reports a stable under-voltage:

The gap between the two values is the filter hysteresis. A larger under_volt_filter_ticks ignores longer dips, such as those caused by a burst of RF transmission, and reacts more slowly. Use VDDBAT_PROTECT_DEFAULT_CONFIG unless the application needs a different filter.

Under-Voltage State

Two functions return the state, with different meanings:

Quick Start

This section shows how to use the VDDBAT under-voltage protection driver. Typical usage flow: select the under-voltage protection in menuconfig, then install the driver, register the callback, and enable detection. When an under-voltage occurs, enter Deep-sleep from a task and wake up once VDDBAT recovers.

Configure Protection Mode

Select CONFIG_ESP_VDDBAT_UNDER_VOLT_PROTECT instead of the default CONFIG_ESP_BROWNOUT_DET. The chip is then not reset when VDDBAT drops, and this driver reports the under-voltage. This also disables the brownout configuration done by the bootloader.

Install Driver

Install the protection with esp_vddbat_protect_install() and VDDBAT_PROTECT_DEFAULT_CONFIG. The filter fields of vddbat_protect_config_t are described in the API reference.

Only one handle can exist at a time. Installing again before uninstalling returns ESP_ERR_INVALID_STATE.

Installing configures the filter, allocates the interrupt, and removes VDDBAT_VGOOD from the analog reset path. Detection is not armed yet, see Enable and Disable Detection.

vddbat_protect_config_t config = VDDBAT_PROTECT_DEFAULT_CONFIG();
vddbat_protect_handle_t protect = NULL;
ESP_ERROR_CHECK(esp_vddbat_protect_install(&config, &protect));

Register Event Callbacks

Before enabling the protection, call esp_vddbat_protect_register_callbacks() to register the callback. The callbacks are grouped in vddbat_protect_event_callbacks_t:

The callback runs in the interrupt handler, so it must not block, and it must not enter Deep-sleep directly. A typical callback only wakes up a task that performs the shutdown flow, and returns whether a higher priority task was woken. The user_arg passed at registration is passed back to the callback.

Detection stays enabled after the callback returns. Call esp_vddbat_protect_disable() from the callback to stop further reports until the application has handled this one.

static IRAM_ATTR bool under_volt_isr_cb(vddbat_protect_handle_t handle, void *user_arg)
{
    BaseType_t task_woken = pdFALSE;

    // Stop further reports until the task has handled this one
    esp_vddbat_protect_disable(handle);
    xSemaphoreGiveFromISR((SemaphoreHandle_t)user_arg, &task_woken);
    return task_woken == pdTRUE;
}

vddbat_protect_event_callbacks_t cbs = {
    .on_under_volt = under_volt_isr_cb,
};
ESP_ERROR_CHECK(esp_vddbat_protect_register_callbacks(protect, &cbs, under_volt_sem));

Enable and Disable Detection

esp_vddbat_protect_enable() clears the filter counter and pending events, and then arms the under-voltage detection. A callback must be registered first, otherwise ESP_ERR_INVALID_STATE is returned.

The protection is not enabled automatically during startup. Because there is no under-voltage protection from chip boot until esp_vddbat_protect_enable() is called, install and enable the protection early in the application. If VDDBAT is already below the threshold when the protection is enabled, the callback is called as soon as the filter confirms the under-voltage.

esp_vddbat_protect_disable() stops the detection. The interrupt handler, the handle, and the callback stay in place, so esp_vddbat_protect_enable() can be called again later to re-enable detection, for example after VDDBAT has recovered.

ESP_ERROR_CHECK(esp_vddbat_protect_enable(protect));

Enter Deep-sleep

When an under-voltage occurs, the chip should enter Deep-sleep to save power and protect the battery, and wake up once the battery is recharged above the threshold. The recovery event of the filter is available as the Deep-sleep wakeup source ESP_SLEEP_WAKEUP_VDDBAT_UP_VOLT.

Perform the following steps in a task, not in the callback:

  1. Call esp_vddbat_prepare_deep_sleep(). This function enables the VDDBAT recovery wakeup and clears the under-voltage event. If VDDBAT has already recovered, it disables the wakeup source and returns ESP_ERR_INVALID_STATE. In that case, do not enter Deep-sleep, because there will be no recovery event left to wake the chip.

  2. Finish any other pre-sleep work, such as saving data or stopping flash writes.

  3. Call esp_deep_sleep_try_to_start(). If a wakeup event (including VDDBAT recovery) arrives while the chip is entering Deep-sleep, the sleep is rejected and the function returns.

  4. If the function returns, disable the VDDBAT wakeup source with esp_sleep_disable_wakeup_source() and check esp_vddbat_is_under_volt() again. Retry if VDDBAT is still low.

static void under_volt_handler(vddbat_protect_handle_t protect)
{
    while (esp_vddbat_is_under_volt(protect)) {
        if (esp_vddbat_prepare_deep_sleep(protect) != ESP_OK) {
            break;  // VDDBAT already recovered
        }
        esp_deep_sleep_try_to_start();
        // Only reached if the sleep was rejected
        esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_VDDBAT_UP_VOLT);
        if (!esp_vddbat_is_under_volt(protect)) {
            break;
        }
        vTaskDelay(pdMS_TO_TICKS(100));
    }
    // Disabled in the callback, arm it again now that VDDBAT recovered
    ESP_ERROR_CHECK(esp_vddbat_protect_enable(protect));
}

When the chip wakes up from Deep-sleep, it reboots. If the wakeup was caused by VDDBAT recovery, the return value of esp_sleep_get_wakeup_causes() contains BIT(ESP_SLEEP_WAKEUP_VDDBAT_UP_VOLT).

Uninstall Driver

esp_vddbat_protect_uninstall() frees the interrupt and the handle, and reconnects VDDBAT_VGOOD to the analog reset path. The protection must be disabled first, otherwise ESP_ERR_INVALID_STATE is returned.

ESP_ERROR_CHECK(esp_vddbat_protect_disable(protect));
ESP_ERROR_CHECK(esp_vddbat_protect_uninstall(protect));

Application Example

  • lowpower/vddbat installs the VDDBAT under-voltage protection, enters Deep-sleep from a task once an under-voltage is reported, and wakes up the chip when VDDBAT recovers.

Advanced

Thread Safety

esp_vddbat_protect_disable() can be called from the under-voltage callback. All other functions must be called from task context.

The driver does not provide concurrency protection across tasks; avoid calling its functions concurrently from different tasks.

Limitations

  • The lithium battery protection detector shares the same comparator with the brownout detector, so the two cannot be enabled at the same time. This means lithium battery and other power supply types cannot share a single firmware binary in production.

  • The brownout reset and the VDDBAT under-voltage protection are mutually exclusive. With the VDDBAT protection selected, the chip is no longer reset by hardware when VDDBAT drops, so the application is responsible for handling the under-voltage.

  • There is no under-voltage protection between chip boot and esp_vddbat_protect_enable().

API Reference

Header File

Functions

esp_err_t esp_vddbat_protect_install(const vddbat_protect_config_t *config, vddbat_protect_handle_t *ret_handle)

Install the VDDBAT under voltage protection.

Parameters:
  • config -- [in] protection configuration, see VDDBAT_PROTECT_DEFAULT_CONFIG()

  • ret_handle -- [out] the created protection handle

Returns:

  • ESP_OK: driver installed

  • ESP_ERR_INVALID_ARG: NULL pointer, or the filter ticks are out of range

  • ESP_ERR_INVALID_STATE: the driver is already installed

  • ESP_ERR_NO_MEM: out of memory

  • Others: interrupt allocation failed

esp_err_t esp_vddbat_protect_register_callbacks(vddbat_protect_handle_t handle, const vddbat_protect_event_callbacks_t *cbs, void *user_arg)

Register the VDDBAT protection callbacks.

Parameters:
  • handle -- [in] the handle returned by esp_vddbat_protect_install()

  • cbs -- [in] group of callbacks

  • user_arg -- [in] argument passed back to the callbacks

Returns:

  • ESP_OK: callbacks registered

  • ESP_ERR_INVALID_ARG: invalid handle or NULL cbs, or the callback is not in IRAM and user_arg is not in internal RAM while CONFIG_ESP_VDDBAT_UNDER_VOLT_PROTECT_ISR_CACHE_SAFE is enabled

  • ESP_ERR_INVALID_STATE: protection is enabled

esp_err_t esp_vddbat_protect_enable(vddbat_protect_handle_t handle)

Enable the VDDBAT under voltage protection.

Handles the under voltage detection.

Note

This is not called during startup, the application decides when the protection becomes active. There is no under voltage protection before this call.

Parameters:

handle -- [in] the handle returned by esp_vddbat_protect_install()

Returns:

  • ESP_OK: detection armed

  • ESP_ERR_INVALID_ARG: invalid handle

  • ESP_ERR_INVALID_STATE: no callback has been registered

esp_err_t esp_vddbat_protect_disable(vddbat_protect_handle_t handle)

Disable the VDDBAT under voltage protection.

Disables the detection. The interrupt handler, the handle and the callbacks stay in place, so esp_vddbat_protect_enable() can arm it again. esp_vddbat_protect_uninstall() releases the rest.

Note

Can be called from the under voltage callback. It is placed in IRAM when CONFIG_ESP_VDDBAT_UNDER_VOLT_PROTECT_ISR_CACHE_SAFE is enabled.

Parameters:

handle -- [in] the handle returned by esp_vddbat_protect_install()

Returns:

  • ESP_OK: protection disabled

  • ESP_ERR_INVALID_ARG: invalid handle

  • ESP_ERR_INVALID_STATE: protection is not enabled

esp_err_t esp_vddbat_protect_uninstall(vddbat_protect_handle_t handle)

Uninstall the VDDBAT under voltage protection.

Releases the interrupt handler and the handle, and puts VDDBAT_VGOOD back on the analog reset path. The protection has to be disabled first.

Parameters:

handle -- [in] the handle returned by esp_vddbat_protect_install()

Returns:

  • ESP_OK: driver uninstalled

  • ESP_ERR_INVALID_ARG: invalid handle

  • ESP_ERR_INVALID_STATE: protection is still enabled

esp_err_t esp_vddbat_prepare_deep_sleep(vddbat_protect_handle_t handle)

Prepare deep sleep that wakes when VDDBAT recovers.

Handles the VDDBAT recovery wakeup and clears the under-voltage interrupt. Does not enter deep sleep. Call esp_deep_sleep_start() afterwards, after any extra shutdown work.

Parameters:

handle -- [in] the handle returned by esp_vddbat_protect_install()

Returns:

  • ESP_ERR_INVALID_ARG: invalid handle

  • ESP_ERR_INVALID_STATE: VDDBAT already recovered

bool esp_vddbat_is_under_volt(vddbat_protect_handle_t handle)

Get the live under voltage flag of the hardware filter.

Parameters:

handle -- [in] the handle returned by esp_vddbat_protect_install()

Returns:

true while VDDBAT is below the under voltage threshold

esp_err_t esp_vddbat_get_state(vddbat_protect_handle_t handle, esp_vddbat_state_t *state)

Get the last reported VDDBAT state.

Reflects what the interrupt handler last observed, use esp_vddbat_is_under_volt(handle) for the current hardware state.

Parameters:
  • handle -- [in] the handle returned by esp_vddbat_protect_install()

  • state -- [out] the last reported state

Returns:

  • ESP_OK: state read

  • ESP_ERR_INVALID_ARG: handle or state is NULL

Structures

struct vddbat_protect_config_t

Configuration of the VDDBAT under voltage protection.

Public Members

uint32_t under_volt_filter_ticks

Net low ticks of the RC_FAST clocked filter counter needed to confirm an under voltage, from 1 to 1023

uint32_t up_volt_filter_ticks

Filter counter value at which a recovery is confirmed, must be lower than under_volt_filter_ticks. The distance between the two is the hysteresis of the filter

struct vddbat_protect_event_callbacks_t

Group of VDDBAT protection callbacks, all of them run in ISR context.

Public Members

esp_vddbat_under_volt_cb_t on_under_volt

Called on a confirmed under voltage

Macros

VDDBAT_PROTECT_DEFAULT_CONFIG()

Default protection configuration.

512 net low ticks is about 64 us at the nominal 8 MHz RC_FAST.

Type Definitions

typedef struct vddbat_protect_t *vddbat_protect_handle_t

VDDBAT under voltage protection.

The driver detects the under voltage and calls a user callback from the interrupt handler. Everything else is up to the application: deep sleep cannot be requested from an ISR, so the callback typically only unblocks a task that does the actual shutdown. See the lowpower/vddbat example for a complete flow.

Type of the VDDBAT under voltage protection handle

typedef bool (*esp_vddbat_under_volt_cb_t)(vddbat_protect_handle_t handle, void *user_arg)

VDDBAT under voltage callback.

Called from the interrupt handler once an under voltage is confirmed. It runs in ISR context, so it must not block and must not request deep sleep directly. Detection stays enabled after the callback. Call esp_vddbat_protect_disable() here to stop further reports, and esp_vddbat_protect_enable() after VDDBAT recovers.

Param handle:

[in] the handle returned by esp_vddbat_protect_install()

Param user_arg:

[in] the argument passed to esp_vddbat_protect_register_callbacks()

Return:

whether a high priority task has been woken up by this function

Enumerations

enum esp_vddbat_state_t

Values:

enumerator ESP_VDDBAT_STATE_NORMAL

VDDBAT is above the under voltage threshold

enumerator ESP_VDDBAT_STATE_UNDER_VOLTAGE

VDDBAT dropped below the under voltage threshold


Was this page helpful?