数码之家

 找回密码
 立即注册
搜索
查看: 5988|回复: 3

[Arduino] ESP32 - ULP 协处理器在deepsleep模式下读片内温度传感器 arduino 实现汇编方法

[复制链接]
发表于 2020-12-16 11:19:39 | 显示全部楼层 |阅读模式
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 (注:对于不同的芯片这个数值可能是不一样的)。
  1. #include "esp_sleep.h"
  2. #include "nvs.h"
  3. #include "nvs_flash.h"
  4. #include "soc/rtc_cntl_reg.h"
  5. #include "soc/rtc_io_reg.h"
  6. #include "soc/sens_reg.h"
  7. #include "soc/soc.h"
  8. #include "soc/soc_ulp.h"
  9. #include "driver/gpio.h"
  10. #include "driver/rtc_io.h"
  11. #include "esp32/ulp.h"
  12. #include "ulp_main.h"
  13. #include "ulptool.h"


  14. extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
  15. extern const uint8_t ulp_main_bin_end[]   asm("_binary_ulp_main_bin_end");

  16. static void init_ulp_program()
  17. {
  18.     esp_err_t err = ulptool_load_binary(0, ulp_main_bin_start,(ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));         
  19.     ESP_ERROR_CHECK(err);

  20.     /* Set ULP wake up period to 3s */
  21.     ulp_set_wakeup_period(0, 3*1000*1000);
  22. }

  23. static void print_tsens()
  24. {
  25.     Serial.printf("ulp_tsens_value:%d.\r\n",(uint16_t)ulp_tsens_value);
  26. }

  27. void setup() {
  28.   Serial.begin(115200);
  29.     esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  30.     if (cause != ESP_SLEEP_WAKEUP_ULP) {
  31.         Serial.printf("Not ULP wakeup, initializing ULP\n");
  32.         init_ulp_program();
  33.     } else {
  34.       Serial.printf("ULP wakeup, printing temperature sensor value\n");
  35.         print_tsens();
  36.     }

  37.     Serial.printf("Entering deep sleep\n\n");
  38.     /* Start the ULP program */
  39.     ESP_ERROR_CHECK( ulp_run((&ulp_entry - RTC_SLOW_MEM) / sizeof(uint32_t)));
  40.     ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup() );
  41.     esp_deep_sleep_start();
  42. }

  43. void loop() {
  44.   // not used
  45. }
复制代码







三、安装汇编和ULP环境
  • [tr]下载此存储库的最新版本并解压缩->[color=var(--color-text-link)]Https://github.com/duff2013/ulptool/releases/latest[tr]删除发布版本号,文件夹改为“ulptools”
  • [tr]下载并解压最新预编译的二进制文件-esp32ulp工具链,用于Mac/Linux/Windows:[color=var(--color-text-link)]Https://github.com/espressif/binutils-esp32ulp/releases/latest,Windows下[color=var(--color-text-link)]binutils-esp32ulp-win32-2.28.51-esp-20191205.zip
  • [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

打赏

参与人数 1家元 +20 收起 理由
jjbboox + 20 優秀文章

查看全部打赏

发表于 2020-12-25 09:32:38 | 显示全部楼层
虽然看不懂,但知道是高手。
ESP32的主CPU都还没搞明白的路过。
:sweat:
回复 支持 反对

使用道具 举报

发表于 2022-4-13 15:47:39 | 显示全部楼层
运行不起来,缺文件
回复 支持 反对

使用道具 举报

发表于 2022-4-16 09:19:19 | 显示全部楼层
这个是在ULP低功耗下运行,还是直接唤醒了系统运行啊,不太明白。如果是运行在ULP模式下,可以读取到I2C中数据吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2025-5-6 07:03 , Processed in 0.234001 second(s), 17 queries , Redis On.

Powered by Discuz!

© 2006-2025 MyDigit.Net

快速回复 返回顶部 返回列表