VDDBAT 锂电池欠压保护

[English]

概述

锂电池过度放电会造成不可逆的容量损失。在 ESP32-H21 上,VDDBAT 欠压保护驱动会在 VDDBAT 电压低于固定阈值时检测到欠压,并通过回调函数通知应用程序。之后由应用程序处理相关事件并进入 Deep-sleep,直到电池重新充电并恢复至 VDDBAT 阈值以上。

备注

VDDBAT 欠压保护驱动只负责报告欠压,不会自行进入 Deep-sleep、关闭外设或复位芯片。

驱动介绍

在使用 VDDBAT 欠压保护驱动之前,需要先了解检测是如何确认的,以及两个状态接口各自表示什么。

工作原理

欠压保护将 VDDBAT 与约 2.96 V 的阈值进行比较。比较器输出先经过硬件滤波器去抖,驱动再报告稳定欠压:

两个数值的差值就是滤波器的迟滞。under_volt_filter_ticks 越大,能忽略的跌落越长(例如 RF 突发发射),响应也越慢。没有特殊滤波需求时使用 VDDBAT_PROTECT_DEFAULT_CONFIG。

欠压状态

以下两个函数都返回状态,但含义不同:

快速入门

本节将带你快速了解如何使用 VDDBAT 欠压保护驱动。一般的使用流程如下:先在 menuconfig 中选择欠压保护,再安装驱动、注册回调并开启检测。发生欠压后,由任务进入 Deep-sleep,等 VDDBAT 恢复后唤醒。

配置保护方式

选择 CONFIG_ESP_VDDBAT_UNDER_VOLT_PROTECT,不要使用默认的 CONFIG_ESP_BROWNOUT_DET。此后 VDDBAT 跌落时芯片不再复位,改由本驱动报告欠压。该选项同时关闭 bootloader 中的掉电检测配置。

安装驱动

使用 esp_vddbat_protect_install() 和 VDDBAT_PROTECT_DEFAULT_CONFIG 安装保护。vddbat_protect_config_t 中滤波字段的取值见 API 参考。

同一时间只能存在一个句柄。卸载前再次安装会返回 ESP_ERR_INVALID_STATE。

安装会配置滤波器、分配中断,并将 VDDBAT_VGOOD 从模拟复位通路中移除。此时检测尚未开启,见 开启和关闭检测。

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

注册事件回调

在开启保护之前,调用 esp_vddbat_protect_register_callbacks() 注册回调函数。回调函数集中在 vddbat_protect_event_callbacks_t 中:

回调函数在中断处理程序中运行,因此不能阻塞,也不能直接进入 Deep-sleep。通常回调函数只负责唤醒执行关机流程的任务,并返回是否唤醒了更高优先级的任务。注册时传入的 user_arg 会传回给回调函数。

回调函数返回后,检测仍保持开启。可以在回调函数中调用 esp_vddbat_protect_disable(),在应用程序处理完本次欠压之前停止后续上报。

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

    // 在任务处理完本次欠压之前停止后续上报
    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));

开启和关闭检测

esp_vddbat_protect_enable() 会清零滤波计数器和待处理事件,然后开启欠压检测。调用前必须已注册回调函数,否则返回 ESP_ERR_INVALID_STATE。

启动过程中不会自动开启保护。从芯片启动到调用 esp_vddbat_protect_enable() 之前,没有任何欠压保护,因此请在应用程序中尽早安装并开启保护。如果开启保护时 VDDBAT 已经低于阈值,滤波器确认欠压后会立即调用回调函数。

esp_vddbat_protect_disable() 停止检测。中断处理程序、句柄和回调函数仍然保留,因此之后可以再次调用 esp_vddbat_protect_enable() 开启检测,例如在 VDDBAT 恢复之后。

ESP_ERROR_CHECK(esp_vddbat_protect_enable(protect));

进入 Deep-sleep

发生欠压时,芯片需要进入 Deep-sleep 状态以节省功耗保护电池,等电池重新充电到阈值以上后再唤醒。滤波器的恢复事件可用作 Deep-sleep 唤醒源 ESP_SLEEP_WAKEUP_VDDBAT_UP_VOLT。

请在任务中完成以下步骤,不要在回调函数中执行:

  1. 调用 esp_vddbat_prepare_deep_sleep()。该函数会开启 VDDBAT 恢复唤醒并清除欠压事件。如果 VDDBAT 已经恢复,该函数会关闭该唤醒源并返回 ESP_ERR_INVALID_STATE。此时不要进入 Deep-sleep,因为已经不会再有恢复事件来唤醒芯片。

  2. 完成其他睡眠前的工作,例如保存数据或停止 flash 写入。

  3. 调用 esp_deep_sleep_try_to_start()。如果芯片在进入 Deep-sleep 的过程中收到唤醒事件(包括 VDDBAT 恢复),本次睡眠会被拒绝,函数返回。

  4. 如果函数返回,调用 esp_sleep_disable_wakeup_source() 关闭 VDDBAT 唤醒源,并再次检查 esp_vddbat_is_under_volt()。如果 VDDBAT 仍然偏低,则重试。

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 已恢复
        }
        esp_deep_sleep_try_to_start();
        // 仅当睡眠被拒绝时才会执行到这里
        esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_VDDBAT_UP_VOLT);
        if (!esp_vddbat_is_under_volt(protect)) {
            break;
        }
        vTaskDelay(pdMS_TO_TICKS(100));
    }
    // 回调中已关闭检测,VDDBAT 恢复后重新开启
    ESP_ERROR_CHECK(esp_vddbat_protect_enable(protect));
}

芯片从 Deep-sleep 唤醒后会重新启动。如果是 VDDBAT 恢复导致的唤醒,esp_sleep_get_wakeup_causes() 的返回值中包含 BIT(ESP_SLEEP_WAKEUP_VDDBAT_UP_VOLT)。

卸载驱动

esp_vddbat_protect_uninstall() 释放中断和句柄,并将 VDDBAT_VGOOD 重新接回模拟复位通路。调用前必须先关闭保护,否则返回 ESP_ERR_INVALID_STATE。

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

应用示例

  • lowpower/vddbat 安装 VDDBAT 欠压保护,在上报欠压后由任务进入 Deep-sleep,并在 VDDBAT 恢复后唤醒芯片。

高级功能

线程安全

esp_vddbat_protect_disable() 可以在欠压回调函数中调用,其他函数都必须在任务上下文中调用。

驱动未做多任务并发保护,请避免在不同任务中同时调用相关函数。

使用限制

  • 锂电池保护检测器与掉电检测器(brownout detector)共用同一比较器,因此两者不能同时开启。这意味着在生产时锂电池和其它类型的供电电源不可共享一套固件代码。

  • 掉电复位和 VDDBAT 欠压保护互斥。选择 VDDBAT 欠压保护后,VDDBAT 跌落时硬件不再复位芯片,需要由应用程序负责处理欠压。

  • 从芯片启动到调用 esp_vddbat_protect_enable() 之前没有欠压保护。

API 参考

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.

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

  • ret_handle -- [out] the created protection handle

返回:

  • 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.

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

  • cbs -- [in] group of callbacks

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

返回:

  • 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.

备注

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

参数:

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

返回:

  • 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.

备注

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.

参数:

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

返回:

  • 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.

参数:

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

返回:

  • 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.

参数:

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

返回:

  • 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.

参数:

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

返回:

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.

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

  • state -- [out] the last reported state

返回:

  • 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


此文档对您有帮助吗?