|
ESP32 在deepsleep下也可以工作,还可以控制GPIO,这个对电池供电设备尤为重要。但ULP只能用汇编,非常麻烦,
这里介绍了Arduino IDE ESP32 下 ULP 协处理器如何在低功耗模式下读片内温度传感器 TSENS例子,和如何建立Arduino 的汇编编程环境。
一、 片内温度传感器
ESP32 芯片内内置了温度传感器,其温度测量范围在 -40℃ 到 125℃ 之间,通过测量周围环境(例如 WiFi 电路)产生的热量,可以大致评估系统的负荷散热以及环境温度情况。
需要指出的是,由于制程工艺偏差的原因,每片芯片的温度电压特性可能有差异,因此,温度传感器的主要应用场景为测量温度的变化量,而非温度的绝对值。需要说明,TSENS 值是一个字节,范围是 0 - 255,其数值变化和环境温度变化近似成线性关系,用户需要自己定义和测量其对应的外界温度值。
二、 温度传感器读取示例
本例子 ULP 协处理器每隔 3 S 唤醒一次,唤醒后在低功耗模式下通过 TSENS 指令读取片内温度传感器数值,然后唤醒主 CPU 打印出获取的数值,并再次进入 DeepSleep 状态。
下图演示的是,用拇指和食指捏住 Core Board ,通过 PCB 板将手指热量传递到芯片,最终终端打印数值从 144 变化到 149 (注:对于不同的芯片这个数值可能是不一样的)。- #include "esp_sleep.h"
- #include "nvs.h"
- #include "nvs_flash.h"
- #include "soc/rtc_cntl_reg.h"
- #include "soc/rtc_io_reg.h"
- #include "soc/sens_reg.h"
- #include "soc/soc.h"
- #include "soc/soc_ulp.h"
- #include "driver/gpio.h"
- #include "driver/rtc_io.h"
- #include "esp32/ulp.h"
- #include "ulp_main.h"
- #include "ulptool.h"
- extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
- extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end");
- static void init_ulp_program()
- {
- esp_err_t err = ulptool_load_binary(0, ulp_main_bin_start,(ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
- ESP_ERROR_CHECK(err);
- /* Set ULP wake up period to 3s */
- ulp_set_wakeup_period(0, 3*1000*1000);
- }
- static void print_tsens()
- {
- Serial.printf("ulp_tsens_value:%d.\r\n",(uint16_t)ulp_tsens_value);
- }
- void setup() {
- Serial.begin(115200);
- esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
- if (cause != ESP_SLEEP_WAKEUP_ULP) {
- Serial.printf("Not ULP wakeup, initializing ULP\n");
- init_ulp_program();
- } else {
- Serial.printf("ULP wakeup, printing temperature sensor value\n");
- print_tsens();
- }
- Serial.printf("Entering deep sleep\n\n");
- /* Start the ULP program */
- ESP_ERROR_CHECK( ulp_run((&ulp_entry - RTC_SLOW_MEM) / sizeof(uint32_t)));
- ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup() );
- esp_deep_sleep_start();
- }
- void loop() {
- // not used
- }
复制代码
三、安装汇编和ULP环境
[tr]找到Arduino IDE使用的Arduino-esp 32核心目录: Typically (Mac OS) -> ~/Library/Arduino15/packages/esp32 Typically (Windows) -> C:\Users\<USERNAME>\AppData\Local\Arduino15\packages\esp32 Typically (Linux) -> ~/.arduino15/packages/esp32[tr]移动[tr][tr]ulptool下载并解压缩到此处的Tools文件夹->./esp 32/tools/ulptools/. [tr]将‘platform.local.txt’文件复制到./ESP 32/[tr]hardware/esp 32/1.0.0/[tr]。记住1.0.0[tr]必须匹配您的esp 32核心版本。 [tr]在[tr=rgb(247, 238, 255)]ulptool文件夹中,移动或复制./ulptools/src/ulp_[tr][tr]examples文件夹到Arduino保存程序的地方。 [tr]移动Esp32ulp-[tr]elf-binutils[tr]下载并解压缩到->的文件夹./esp32/tools/ulptool/src/esp32ulp-elf-binutils/.
[tr]现在就可以让编译ULP汇编代码了!
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
打赏
-
查看全部打赏
|