簡介
本節主要是對於Esp.h文件的理解,在該頭文件中主要是實現了對內核中的RAM資源的管理、片外SPI RAM管理、獲取芯片的基本信息、Flash管理。
RAM資源的管理
其中分為片內與片外RAM的管理。
//Internal RAM uint32_t getHeapSize(); //total heap size 全部的片內內存大小 uint32_t getFreeHeap(); //available heap 可以內存大小 uint32_t getMinFreeHeap(); //lowest level of free heap since boot uint32_t getMaxAllocHeap(); //largest block of heap that can be allocated at once //SPI RAM 外設需要自己設計 uint32_t getPsramSize(); uint32_t getFreePsram(); uint32_t getMinFreePsram(); uint32_t getMaxAllocPsram();
芯片的基本信息
獲取芯片基本信息,包括了芯片的id物理地址、版本、運行頻率等
uint8_t getChipRevision(); uint32_t getCpuFreqMHz(){ return getCpuFrequencyMhz(); } inline uint32_t getCycleCount() __attribute__((always_inline)); const char * getSdkVersion(); uint64_t getEfuseMac();
例子:
1 #include <Arduino.h> 2 3 void setup() 4 { 5 Serial.begin(9600); 6 Serial.println(""); 7 } 8 9 uint64_t chipid; 10 11 void loop() 12 { 13 chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes). 14 Serial.printf("ESP32 Chip ID = %04X",(uint16_t)(chipid>>32));//print High 2 bytes 15 Serial.printf("%08X\n",(uint32_t)chipid);//print Low 4bytes. 16 17 Serial.printf("total heap size = %u\n",ESP.getHeapSize()); 18 Serial.printf("available heap = %u\n",ESP.getFreeHeap()); 19 Serial.printf("lowest level of free heap since boot = %u\n",ESP.getMinFreeHeap()); 20 Serial.printf("largest block of heap that can be allocated at once = %u\n",ESP.getMaxAllocHeap()); 21 22 Serial.printf("total Psram size = %u\n",ESP.getPsramSize()); 23 Serial.printf("available Psram = %u\n",ESP.getFreePsram()); 24 Serial.printf("lowest level of free Psram since boot = %u\n",ESP.getMinFreePsram()); 25 Serial.printf("largest block of Psram that can be allocated at once = %u\n",ESP.getMinFreePsram()); 26 27 Serial.printf("get Chip Revision = %u\n",ESP.getChipRevision()); 28 Serial.printf("getCpuFreqMHz = %u\n",ESP.getCpuFreqMHz()); 29 Serial.printf("get Cycle Count = %u\n",ESP.getCycleCount()); 30 Serial.printf("get SdkVersion = %s\n",ESP.getSdkVersion()); 31 32 delay(5000); 33 }
睡眠模式
void deepSleep(uint32_t time_us);
調用后進入深度睡眠模式,深度睡眠時間過后,設備會自動醒來,在醒來時,設備調用深睡眠喚醒存根,然后繼續加載應用程序。
調用這個函數相當於調用esp_deep_sleep_enable_timer_wakeup,然后調用esp_deep_sleep_start。
esp_deep_sleep不會優雅地關閉WiFi、BT和更高級別的協議連接。確保調用了相關的WiFi和BT棧函數來關閉任何連接,並對外設進行初始化。這些包括:esp_bluedroid_disable、esp_bt_controller_disable、esp_wifi_stop
此函數不返回。time_us:深度睡眠時間,單位:微秒
這是極為簡單的睡眠調用,且只能是定時喚醒。后面將仔細研究ESP32的各種睡眠與喚醒:鏈接
1 #include <Arduino.h> 2 3 void setup(){ 4 Serial.begin(9600); 5 delay(1000); 6 Serial.println("Going to sleep now\n"); 7 ESP.deepSleep(5000000); // 注時間意單位為:us 8 Serial.println("This will never be printed\n"); 9 } 10 11 void loop(){ 12 // 這里不會調用 13 }