ESP32 32kHz晶振无法找到的问题

pengjs
Posts: 18
Joined: Fri Apr 09, 2021 6:32 am

ESP32 32kHz晶振无法找到的问题

Postby pengjs » Tue Apr 27, 2021 7:38 am

问题如下:最近外挂了一个外部32kHz晶振,sdkconfig中宏定义:#define CONFIG_BTDM_LPCLK_SEL_EXT_32K_XTAL 1

bt.c 部分代码如下:
#if CONFIG_BTDM_LPCLK_SEL_EXT_32K_XTAL
// check whether or not EXT_CRYS is working
if (rtc_clk_slow_freq_get() == RTC_SLOW_FREQ_32K_XTAL) {
btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL32K; // set default value
#ifdef CONFIG_PM_ENABLE
s_btdm_allow_light_sleep = true;
#endif
} else {
ESP_LOGW(BTDM_LOG_TAG, "32.768kHz XTAL not detected, fall back to main XTAL as Bluetooth sleep clock\n"
"light sleep mode will not be able to apply when bluetooth is enabled");
btdm_lpclk_sel = BTDM_LPCLK_SEL_XTAL; // set default value
}

开机logo如下:
[14:42:52.219]收←◆[0;33mW (1826) clk: 32 kHz XTAL not found, switching to internal 150 kHz oscillator[0m

[14:42:52.361]收←◆[0;31mE (2090) DBY?[0;33mW (2095) BTDM_INIT: 32.768kHz XTAL not detected, fall back to main XTAL as Bluetooth sleep clock
light sleep mode will not be able to apply when bluetooth is enabled[0m

为什么找不到32kHz的晶振,我还需要做什么另外的操作吗?外挂晶振电路图是按照硬件指导做的。
如有了解的,恳请尽快解答,谢谢~

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: ESP32 32kHz晶振无法找到的问题

Postby ESP_Gargamel » Tue Apr 27, 2021 12:34 pm

你使用的是 ECO3 的 ESP32 吗?可以在启动时的 log 上看到,如:

Code: Select all

I (30) boot: chip revision: 3
如果是 ECO3 的,那晶振部分电路请参考文档 https://www.espressif.com/sites/default ... nes_cn.pdf 的 2.4.2 点章节,和之前版本芯片有些许差异。

pengjs
Posts: 18
Joined: Fri Apr 09, 2021 6:32 am

Re: ESP32 32kHz晶振无法找到的问题

Postby pengjs » Wed Apr 28, 2021 4:24 am

@ESP_Gargamel 参考您发的资料后,外部晶振可以测量到波形了。但是我软件读出来的还是 160khz。这是什么原因呢?
代码如下:
do {
if (rtc_slow_freq == RTC_SLOW_FREQ_32K_XTAL) {
/* 32k XTAL oscillator needs to be enabled and running before it can
* be used. Hardware doesn't have a direct way of checking if the
* oscillator is running. Here we use rtc_clk_cal function to count
* the number of main XTAL cycles in the given number of 32k XTAL
* oscillator cycles. If the 32k XTAL has not started up, calibration
* will time out, returning 0.
*/
ESP_EARLY_LOGW(TAG, "waiting for 32k oscillator to start up");
if (slow_clk == SLOW_CLK_32K_XTAL) {
rtc_clk_32k_bootstrap(512);
rtc_clk_32k_bootstrap(512);
rtc_clk_32k_enable(true);
} else if (slow_clk == SLOW_CLK_32K_EXT_OSC) {
rtc_clk_32k_enable_external();
}
// When SLOW_CLK_CAL_CYCLES is set to 0, clock calibration will not be performed at startup.
if (SLOW_CLK_CAL_CYCLES > 0) {
cal_val = CALIBRATE_ONE(RTC_CAL_32K_XTAL);
if (cal_val == 0 || cal_val < MIN_32K_XTAL_CAL_VAL) {
if (retry_32k_xtal-- > 0) {
continue;
}
ESP_EARLY_LOGW(TAG, "32 kHz XTAL not found, switching to internal 150 kHz oscillator");
rtc_slow_freq = RTC_SLOW_FREQ_RTC;
}
}
} else if (rtc_slow_freq == RTC_SLOW_FREQ_8MD256) {
rtc_clk_8m_enable(true, true);
}
rtc_clk_slow_freq_set(SLOW_CLK_32K_XTAL);
if (cal_val == 0)
{
ESP_EARLY_LOGW(TAG,"32K XTAL OSC has not started up");
}
else
{
ESP_EARLY_LOGW(TAG,"done\n");
}

if (rtc_clk_32k_enabled())
{
ESP_EARLY_LOGW(TAG, "OSC Enabled");
}

if (SLOW_CLK_CAL_CYCLES > 0) {
/* TODO: 32k XTAL oscillator has some frequency drift at startup.
* Improve calibration routine to wait until the frequency is stable.
*/
cal_val = rtc_clk_cal(RTC_CAL_RTC_MUX, SLOW_CLK_CAL_CYCLES);
} else {
const uint64_t cal_dividend = (1ULL << RTC_CLK_CAL_FRACT) * 1000000ULL;
cal_val = (uint32_t) (cal_dividend / rtc_clk_slow_freq_get_hz());
}
} while (cal_val == 0);
ESP_EARLY_LOGW(TAG, "RTC_SLOW_CLK calibration value: %d", cal_val);
esp_clk_slowclk_cal_set(cal_val);

#define CALIBRATE_ONE(cali_clk) calibrate_one(cali_clk, #cali_clk)

static uint32_t calibrate_one(rtc_cal_sel_t cal_clk, const char *name)
{

const uint32_t cal_count = 1024;
const float factor = (1 << 19) * 1000.0f;
uint32_t cali_val;
ESP_EARLY_LOGW(TAG,"%s:\n", name);
for (int i = 0; i < 9; ++i)
{
ESP_EARLY_LOGW(TAG,"calibrate (%d): ", i);
cali_val = rtc_clk_cal(cal_clk, cal_count);
ESP_EARLY_LOGW(TAG,"%.3f kHz\n", factor / (float)cali_val);
}
return cali_val;
}

打印logo如下:
[12:12:51.134]收←◆[0;33mW (1675) clk: CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS[0m
[0;33mW (1676) clk: waiting for 32k oscillator to start up[0m
[0;33mW (1708) clk: RTC_CAL_32K_XTAL:
[0m
[0;33mW (1708) clk: calibrate (0): [0m
[0;33mW (1740) clk: %f0 kHz
[0m
[0;33mW (1740) clk: calibrate (1): [0m

[12:12:51.230]收←◆[0;33mW (1771) clk: %f0 kHz
[0m
[0;33mW (1771) clk: calibrate (2): [0m

[12:12:51.261]收←◆[0;33mW (1803) clk: %f0 kHz
[0m
[0;33mW (1803) clk: calibrate (3): [0m

[12:12:51.293]收←◆[0;33mW (1834) clk: %f0 kHz
[0m
[0;33mW (1834) clk: calibrate (4): [0m

[12:12:51.324]收←◆[0;33mW (1866) clk: %f0 kHz
[0m
[0;33mW (1866) clk: calibrate (5): [0m

[12:12:51.356]收←◆[0;33mW (1897) clk: %f0 kHz
[0m
[0;33mW (1897) clk: calibrate (6): [0m

[12:12:51.387]收←◆[0;33mW (1929) clk: %f0 kHz
[0m
[0;33mW (1929) clk: calibrate (7): [0m

[12:12:51.419]收←◆[0;33mW (1960) clk: %f0 kHz
[0m
[0;33mW (1960) clk: calibrate (8): [0m

[12:12:51.450]收←◆[0;33mW (1992) clk: %f0 kHz
[0m
[0;33mW (1992) clk: done
[0m
[0;33mW (1992) clk: OSC Enabled[0m
[0;33mW (2023) clk: RTC_SLOW_CLK calibration value: 15999923[0m

为什么我设置是的:RTC_CAL_32K_XTAL, 中间设置读出来都是0,最后读出来是15999923,而不是32k呢?

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: ESP32 32kHz晶振无法找到的问题

Postby ESP_Gargamel » Wed Apr 28, 2021 6:12 am

这个已经对了。

Code: Select all

/**
 * @brief Measure RTC slow clock's period, based on main XTAL frequency
 *
 * This function will time out and return 0 if the time for the given number
 * of cycles to be counted exceeds the expected time twice. This may happen if
 * 32k XTAL is being calibrated, but the oscillator has not started up (due to
 * incorrect loading capacitance, board design issue, or lack of 32 XTAL on board).
 *
 * @param cal_clk  clock to be measured
 * @param slow_clk_cycles  number of slow clock cycles to average
 * @return average slow clock period in microseconds, Q13.19 fixed point format,
 *         or 0 if calibration has timed out
 */
uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slow_clk_cycles);
rtc_clk_cal 返回的值为 32 位的 slow clk 周期,有个固定的格式,其中高 13 位为整数,低 19 位为小数。
15999923 = 1111 0100 0010 0011 1011 0011,那周期就是 11110.1000010001110110011,你再换算一下,频率就是 32.768K 附近了。

pengjs
Posts: 18
Joined: Fri Apr 09, 2021 6:32 am

Re: ESP32 32kHz晶振无法找到的问题

Postby pengjs » Wed Apr 28, 2021 6:46 am

我目前是在验证自动的light_sleep低功耗模式。
BTDM_INIT: 32.768kHz XTAL not detected, fall back to main XTAL as Bluetooth sleep clock light sleep mode will not be able to apply when bluetooth is enabled(这个logo已经没有了)
并且:btdm_sleep_exit_phase3_wrapper()在可以函数计数打印,可以成功打印logo。 我可以认为此时已经成功进入了休眠?但是我们现在板子的平均电流是80mA(此时蓝牙未连接)我蓝牙连接广播设置的是5s。

配置如下:
ESP-IDF menuconfig options:
Enable Power Management:
menuconfig ---> Component config ---> Power management --->
[*] Support for power management

Enable Tickless Idle:
menuconfig ---> Component config ---> FreeRTOS --->
[*] Tickless idle support
(3) Minimum number of ticks to enter sleep mode for (NEW)

Note: Tickless idle needs to be enabled to allow automatic light sleep. FreeRTOS will enter light sleep if no tasks need to run
for 3(by default) ticks, that is 30ms if tick rate is 100Hz. Configure the FreeRTOS tick rate to be higher if you want to allow
shorter duration light sleep, for example:
menuconfig ---> Component config ---> FreeRTOS ->
(1000) Tick rate (Hz)

Configure external 32.768Hz crystal as RTC clock source:
menuconfig ---> Component config ---> ESP32-specific --->
RTC clock source (External 32kHz crystal)
[*] Additional current for external 32kHz crystal
Note that the "additional current" option is a workaround for a hardware issue on ESP32 that the crystal can fail in oscillating.
Please enable this option when you use external 32kHz crystal. This hardware issue will be resolved in the next ECO chip.

Enable Bluetooth modem sleep with external 32.768kHz crystal as low power clock:
menuconfig ---> Component config ---> Bluetooth ---> Bluetooth controller ---> MODEM SLEEP Options --->
[*] Bluetooth modem sleep
Bluetooth Modem sleep mode (ORIG mode(sleep with low power clock))
Bluetooth low power clock (External 32kHz crystal)
Enable light sleep by calling power management API in application:
In your application source code, to enable automatic light sleep, use power management API esp_pm_configure like this:
#include "esp_err.h"
#include "esp_pm.h"

esp_pm_config_esp32_t pm_config = {
.max_freq_mhz = EXAMPLE_MAX_CPU_FREQ_MHZ, // e.g. 80, 160, 240
.min_freq_mhz = EXAMPLE_MIN_CPU_FREQ_MHZ, // e.g. 40
.light_sleep_enable = true, // enable light sleep
};
ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: ESP32 32kHz晶振无法找到的问题

Postby ESP_Gargamel » Thu Apr 29, 2021 1:18 am

这个数据不太对,你的测试环境是如何搭建的?如果只有 modem sleep,电流情况是多少?需要只测模组本身的电流。
我这里有一些之前的数据:
Attachments
企业微信截图_161965875322.png
企业微信截图_161965875322.png (54.29 KiB) Viewed 10323 times

pengjs
Posts: 18
Joined: Fri Apr 09, 2021 6:32 am

Re: ESP32 32kHz晶振无法找到的问题

Postby pengjs » Thu Apr 29, 2021 6:55 am

modem sleep 和 light sleep的区别是不是 esp_pm_config_esp32_t pm_config = {
.max_freq_mhz = 160, // e.g. 80, 160, 240
.min_freq_mhz = 40, // e.g. 40
.light_sleep_enable = 1, // enable light sleep
};
.light_sleep_enable = 1为light sleep,.light_sleep_enable = 0为modem sleep??
我目前设置0或者1,功耗都是无区别的。目前的测试方法是:用直流源供电(3.3V),测模阻的电流。目前平均电流约为80mA。在未配置pm_config 时,平均电流为100mA。

ESP_XieWX
Posts: 27
Joined: Fri Jun 22, 2018 7:39 am

Re: ESP32 32kHz晶振无法找到的问题

Postby ESP_XieWX » Thu Apr 29, 2021 9:00 am

请问您现在用的 IDF 版本是多少?
另外,可以用 IDF4.0 也同步测试一下吗

pengjs
Posts: 18
Joined: Fri Apr 09, 2021 6:32 am

Re: ESP32 32kHz晶振无法找到的问题

Postby pengjs » Thu Apr 29, 2021 9:56 am

我目前使用的版本是:
commit dddcc2ede8601915ad134ecf46f8682e5e6de2a4 (HEAD)
Merge: 21b02a930 a7ddc9c12
Author: Jiang Jiang Jian <jack@espressif.com>
Date: Wed Sep 16 14:06:28 2020 +0800

Merge branch 'bugfix/add_protection_for_spp_api_4.2' into 'release/v4.2'

Bugfix/add protection for spp api 4.2

See merge request espressif/esp-idf!10281

pengjs
Posts: 18
Joined: Fri Apr 09, 2021 6:32 am

Re: ESP32 32kHz晶振无法找到的问题

Postby pengjs » Fri Apr 30, 2021 2:24 am

开启light mode,打开蓝牙,未连接设备,未连接设备,平均电流为:8mA左右
连接蓝牙:15mA (进入休眠蓝牙会自动断开,然后找不到蓝牙了???)

开启modem sleep,打开蓝牙,未连接设备,未连接设备,平均电流为:20mA左右
连接蓝牙:50mA-20mA跳变

未开休眠,打开蓝牙,未连接设备,未连接设备,平均电流为:35mA左右
连接蓝牙:42mA
测试代码:esp-idf/examples/bluetooth/bluedroid/ble/ble_ancs
esp-idf commit id dddcc2ede8601915ad134ecf46f8682e5e6de2a4

问题:为什么light mode模式会断开蓝牙,然后搜索不到蓝牙了。自动的light mode不是会保持蓝牙连接吗?

Who is online

Users browsing this forum: No registered users and 80 guests