ESP32例程为C语言开发,并非Python/Arduino/AT指令开发,基于ESP-IDF_V4.2框架库,采用纯C语言开发,开发工具为Visual Studio Code
Android(安卓)例程全部为原生Android开发,例程全部支持到SDK版本29 Android 10.0(Q) 开发工具为Andirod Studio 4.0
编程第一课必写helloworld,因为 The C Programming Language 中使用它做为第一个演示程序,非常著名,所以后来的程序员在学习编程或进行设备调试时延续了这一习惯,入行久了才会明白,一入编程深似海,从此头发是路人,先不要和世界问好了,以后陪伴你更多的是Bug。
第一个示例先向Bug问好
printf:一个ISR(中断服务处理,Interrupt Service Routines)应该避免调用不可重入函数。一些标准库函数是不可重入的,如经常实现的malloc和printf。因为中断可能发生在执行任务中,因为任务可能是在“malloc”的函数调用中,如果ISR调用此相同的不可重入函数,由此产生的行为可能是灾难性的。
不可重入函数是指这样的一类函数,不可以在它还没有返回就再次被调用。例如printf,malloc,free等都是不可重入函数。因为信号可能在任何时候发生,例如在printf执行过程中,因此不能在信号处理函数里调用printf,否则printf将会被重入。 函数不可重入大多数是因为在函数中引用了全局变量。
例如,printf会引用全局变量stdout,malloc,free会引用全局的内存分配表。
不建议在 ISR 中使用 printf 和其余输出函数。出于调试的目的,可以在ISR 中使用 ESP_EARLY_LOGx 来输出日志,不过要确保将 TAG 和格式字符串都放在了DRAM 中。
ESP_LOGX日志
日志库有两种管理日志详细程度的方法:编译阶段,通过菜单设置;运行阶段,使用esp_log_level_set()函数设置。
日志等级有:错误,警告,信息,调试和详细(详细度从最低到最高)
ESP_LOGE - 错误(最低)
ESP_LOGW - 警告
ESP_LOGI - 信息
ESP_LOGD - 调试
ESP_LOGV - 详细(最高)
考虑到线程安全,在FREERTOS中尽量使用ESP_LOGX来输出调试信息和打印消息
代码:
// Hello Bug Example
#include 《stdio.h》
#include “freertos/FreeRTOS.h”
#include “freertos/task.h”
#include “esp_system.h”
#include “esp_spi_flash.h”
#include 《esp_log.h》
const static char *TAG = “HelloBug Demo”;
void app_main()
{
uint8_t MAC[6];
ESP_LOGI(TAG, “Hello Bug”);
// 打印芯片信息
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
ESP_LOGI(TAG, “ESP32 Chip Cores Count: %d”,chip_info.cores);
if(chip_info.model == 1){
ESP_LOGI(TAG, “ESP32 Chip Model is: ESP32”);
}else if(chip_info.model == 2){
ESP_LOGI(TAG, “ESP32 Chip Model is: ESP32S2”);
}else{
ESP_LOGI(TAG, “ESP32 Chip Model is: Unknown Model”);
}
ESP_LOGI(TAG, “ESP32 Chip Features is: %d”,chip_info.features);
ESP_LOGI(TAG, “ESP32 Chip Revision is: %d”,chip_info.revision);
ESP_LOGI(TAG, “ESP32 Chip, WiFi%s%s, ”,
(chip_info.features & CHIP_FEATURE_BT) ? “/BT” : “”,
(chip_info.features & CHIP_FEATURE_BLE) ? “/BLE” : “”);
ESP_LOGI(TAG, “SPI Flash Chip Size: %dMByte %s flash”, spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? “Embedded” : “External”);
ESP_LOGI(TAG, “Free Heap Size is: %d Byte”,esp_get_free_heap_size());
ESP_LOGI(TAG, “Free Internal Heap Size is: %d Byte”,esp_get_free_internal_heap_size());
ESP_LOGI(TAG, “Free minimum Heap Size is: %d Byte”,esp_get_minimum_free_heap_size());
esp_base_mac_addr_get(MAC);
ESP_LOGI(TAG, “Base MAC Addr : %02X.%02X.%02X.%02X.%02X.%02X”,MAC[0],MAC[1],MAC[2],MAC[3],MAC[4],MAC[5]);
for (int i = 10; i 》= 0; i--) {
ESP_LOGI(TAG, “Hello Bug! ^_^ %d”, i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
while(1){
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
//ESP_LOGI(TAG, “Restarting now.”);
//fflush(stdout);
//esp_restart();
}
示例完整串口输出:
ets Jul 29 2019 12:21:46
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4
load:0x3fff0034,len:7144
load:0x40078000,len:13696
ho 0 tail 12 room 4
load:0x40080400,len:4000
entry 0x40080688
I (31) boot: ESP-IDF v4.2-dev-1995-g636b964c8-dirty 2nd stage bootloader
I (31) boot: compile time 22:38:56
I (32) boot: chip revision: 3
I (36) boot_comm: chip revision: 3, min. bootloader chip revision: 0
I (43) boot.esp32: SPI Speed : 40MHz
I (48) boot.esp32: SPI Mode : DIO
I (52) boot.esp32: SPI Flash Size : 4MB
I (57) boot: Enabling RNG early entropy source.。.
I (62) boot: Partition Table:
I (66) boot: ## Label Usage Type ST Offset Length
I (73) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (81) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (88) boot: 2 factory factory app 00 00 00010000 00100000
I (96) boot: End of partition table
I (100) boot_comm: chip revision: 3, min. application chip revision: 0
I (107) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x05d40 ( 23872) map
I (125) esp_image: segment 1: paddr=0x00015d68 vaddr=0x3ffb0000 size=0x021c4 ( 8644) load
I (129) esp_image: segment 2: paddr=0x00017f34 vaddr=0x40080000 size=0x00404 ( 1028) load
I (134) esp_image: segment 3: paddr=0x00018340 vaddr=0x40080404 size=0x07cd8 ( 31960) load
I (156) esp_image: segment 4: paddr=0x00020020 vaddr=0x400d0020 size=0x1304c ( 77900) map
I (186) esp_image: segment 5: paddr=0x00033074 vaddr=0x400880dc size=0x01e50 ( 7760) load
I (196) boot: Loaded app from partition at offset 0x10000
I (196) boot: Disabling RNG early entropy source.。.
I (198) cpu_start: Pro cpu up.
I (202) cpu_start: Application information:
I (207) cpu_start: Project name: hello-bug
I (212) cpu_start: App version: 1
I (216) cpu_start: Compile time: Sep 2 2020 22:38:37
I (222) cpu_start: ELF file SHA256: 5eb98a9eca0e63a6.。.
I (228) cpu_start: ESP-IDF: v4.2-dev-1995-g636b964c8-dirty
I (235) cpu_start: Starting app cpu, entry point is 0x400815c4
I (0) cpu_start: App cpu up.
I (246) heap_init: Initializing. RAM available for dynamic allocation:
I (252) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (258) heap_init: At 3FFB2A00 len 0002D600 (181 KiB): DRAM
I (265) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (271) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (277) heap_init: At 40089F2C len 000160D4 (88 KiB): IRAM
I (284) cpu_start: Pro cpu start user code
flash id ==== 0x204016
I (302) spi_flash: detected chip: generic
I (303) spi_flash: flash io: dio
I (303) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (313) HelloBug Demo: Hello Bug
I (313) HelloBug Demo: ESP32 Chip Cores Count: 2
I (323) HelloBug Demo: ESP32 Chip Model is: ESP32
I (323) HelloBug Demo: ESP32 Chip Features is: 50
I (333) HelloBug Demo: ESP32 Chip Revision is: 3
I (343) HelloBug Demo: ESP32 Chip, WiFi/BT/BLE,
I (343) HelloBug Demo: SPI Flash Chip Size: 4MByte External flash
I (353) HelloBug Demo: Free Heap Size is: 299588 Byte
I (353) HelloBug Demo: Free Internal Heap Size is: 299588 Byte
I (363) HelloBug Demo: Free minimum Heap Size is: 298768 Byte
I (373) system_api: Base MAC address is not set
I (373) HelloBug Demo: Base MAC Addr : DC.45.08.80.70.48
I (383) HelloBug Demo: Hello Bug! ^_^ 10
I (1383) HelloBug Demo: Hello Bug! ^_^ 9
I (2383) HelloBug Demo: Hello Bug! ^_^ 8
I (3383) HelloBug Demo: Hello Bug! ^_^ 7
I (4383) HelloBug Demo: Hello Bug! ^_^ 6
I (5383) HelloBug Demo: Hello Bug! ^_^ 5
I (6383) HelloBug Demo: Hello Bug! ^_^ 4
I (7383) HelloBug Demo: Hello Bug! ^_^ 3
I (8383) HelloBug Demo: Hello Bug! ^_^ 2
I (9383) HelloBug Demo: Hello Bug! ^_^ 1
I (10383) HelloBug Demo: Hello Bug! ^_^ 0
ESP32例程为C语言开发,并非Python/Arduino/AT指令开发,基于ESP-IDF_V4.2框架库,采用纯C语言开发,开发工具为Visual Studio Code
Android(安卓)例程全部为原生Android开发,例程全部支持到SDK版本29 Android 10.0(Q) 开发工具为Andirod Studio 4.0
编程第一课必写helloworld,因为 The C Programming Language 中使用它做为第一个演示程序,非常著名,所以后来的程序员在学习编程或进行设备调试时延续了这一习惯,入行久了才会明白,一入编程深似海,从此头发是路人,先不要和世界问好了,以后陪伴你更多的是Bug。
第一个示例先向Bug问好
printf:一个ISR(中断服务处理,Interrupt Service Routines)应该避免调用不可重入函数。一些标准库函数是不可重入的,如经常实现的malloc和printf。因为中断可能发生在执行任务中,因为任务可能是在“malloc”的函数调用中,如果ISR调用此相同的不可重入函数,由此产生的行为可能是灾难性的。
不可重入函数是指这样的一类函数,不可以在它还没有返回就再次被调用。例如printf,malloc,free等都是不可重入函数。因为信号可能在任何时候发生,例如在printf执行过程中,因此不能在信号处理函数里调用printf,否则printf将会被重入。 函数不可重入大多数是因为在函数中引用了全局变量。
例如,printf会引用全局变量stdout,malloc,free会引用全局的内存分配表。
不建议在 ISR 中使用 printf 和其余输出函数。出于调试的目的,可以在ISR 中使用 ESP_EARLY_LOGx 来输出日志,不过要确保将 TAG 和格式字符串都放在了DRAM 中。
ESP_LOGX日志
日志库有两种管理日志详细程度的方法:编译阶段,通过菜单设置;运行阶段,使用esp_log_level_set()函数设置。
日志等级有:错误,警告,信息,调试和详细(详细度从最低到最高)
ESP_LOGE - 错误(最低)
ESP_LOGW - 警告
ESP_LOGI - 信息
ESP_LOGD - 调试
ESP_LOGV - 详细(最高)
考虑到线程安全,在FREERTOS中尽量使用ESP_LOGX来输出调试信息和打印消息
代码:
// Hello Bug Example
#include 《stdio.h》
#include “freertos/FreeRTOS.h”
#include “freertos/task.h”
#include “esp_system.h”
#include “esp_spi_flash.h”
#include 《esp_log.h》
const static char *TAG = “HelloBug Demo”;
void app_main()
{
uint8_t MAC[6];
ESP_LOGI(TAG, “Hello Bug”);
// 打印芯片信息
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
ESP_LOGI(TAG, “ESP32 Chip Cores Count: %d”,chip_info.cores);
if(chip_info.model == 1){
ESP_LOGI(TAG, “ESP32 Chip Model is: ESP32”);
}else if(chip_info.model == 2){
ESP_LOGI(TAG, “ESP32 Chip Model is: ESP32S2”);
}else{
ESP_LOGI(TAG, “ESP32 Chip Model is: Unknown Model”);
}
ESP_LOGI(TAG, “ESP32 Chip Features is: %d”,chip_info.features);
ESP_LOGI(TAG, “ESP32 Chip Revision is: %d”,chip_info.revision);
ESP_LOGI(TAG, “ESP32 Chip, WiFi%s%s, ”,
(chip_info.features & CHIP_FEATURE_BT) ? “/BT” : “”,
(chip_info.features & CHIP_FEATURE_BLE) ? “/BLE” : “”);
ESP_LOGI(TAG, “SPI Flash Chip Size: %dMByte %s flash”, spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? “Embedded” : “External”);
ESP_LOGI(TAG, “Free Heap Size is: %d Byte”,esp_get_free_heap_size());
ESP_LOGI(TAG, “Free Internal Heap Size is: %d Byte”,esp_get_free_internal_heap_size());
ESP_LOGI(TAG, “Free minimum Heap Size is: %d Byte”,esp_get_minimum_free_heap_size());
esp_base_mac_addr_get(MAC);
ESP_LOGI(TAG, “Base MAC Addr : %02X.%02X.%02X.%02X.%02X.%02X”,MAC[0],MAC[1],MAC[2],MAC[3],MAC[4],MAC[5]);
for (int i = 10; i 》= 0; i--) {
ESP_LOGI(TAG, “Hello Bug! ^_^ %d”, i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
while(1){
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
//ESP_LOGI(TAG, “Restarting now.”);
//fflush(stdout);
//esp_restart();
}
示例完整串口输出:
ets Jul 29 2019 12:21:46
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4
load:0x3fff0034,len:7144
load:0x40078000,len:13696
ho 0 tail 12 room 4
load:0x40080400,len:4000
entry 0x40080688
I (31) boot: ESP-IDF v4.2-dev-1995-g636b964c8-dirty 2nd stage bootloader
I (31) boot: compile time 22:38:56
I (32) boot: chip revision: 3
I (36) boot_comm: chip revision: 3, min. bootloader chip revision: 0
I (43) boot.esp32: SPI Speed : 40MHz
I (48) boot.esp32: SPI Mode : DIO
I (52) boot.esp32: SPI Flash Size : 4MB
I (57) boot: Enabling RNG early entropy source.。.
I (62) boot: Partition Table:
I (66) boot: ## Label Usage Type ST Offset Length
I (73) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (81) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (88) boot: 2 factory factory app 00 00 00010000 00100000
I (96) boot: End of partition table
I (100) boot_comm: chip revision: 3, min. application chip revision: 0
I (107) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x05d40 ( 23872) map
I (125) esp_image: segment 1: paddr=0x00015d68 vaddr=0x3ffb0000 size=0x021c4 ( 8644) load
I (129) esp_image: segment 2: paddr=0x00017f34 vaddr=0x40080000 size=0x00404 ( 1028) load
I (134) esp_image: segment 3: paddr=0x00018340 vaddr=0x40080404 size=0x07cd8 ( 31960) load
I (156) esp_image: segment 4: paddr=0x00020020 vaddr=0x400d0020 size=0x1304c ( 77900) map
I (186) esp_image: segment 5: paddr=0x00033074 vaddr=0x400880dc size=0x01e50 ( 7760) load
I (196) boot: Loaded app from partition at offset 0x10000
I (196) boot: Disabling RNG early entropy source.。.
I (198) cpu_start: Pro cpu up.
I (202) cpu_start: Application information:
I (207) cpu_start: Project name: hello-bug
I (212) cpu_start: App version: 1
I (216) cpu_start: Compile time: Sep 2 2020 22:38:37
I (222) cpu_start: ELF file SHA256: 5eb98a9eca0e63a6.。.
I (228) cpu_start: ESP-IDF: v4.2-dev-1995-g636b964c8-dirty
I (235) cpu_start: Starting app cpu, entry point is 0x400815c4
I (0) cpu_start: App cpu up.
I (246) heap_init: Initializing. RAM available for dynamic allocation:
I (252) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (258) heap_init: At 3FFB2A00 len 0002D600 (181 KiB): DRAM
I (265) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (271) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (277) heap_init: At 40089F2C len 000160D4 (88 KiB): IRAM
I (284) cpu_start: Pro cpu start user code
flash id ==== 0x204016
I (302) spi_flash: detected chip: generic
I (303) spi_flash: flash io: dio
I (303) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (313) HelloBug Demo: Hello Bug
I (313) HelloBug Demo: ESP32 Chip Cores Count: 2
I (323) HelloBug Demo: ESP32 Chip Model is: ESP32
I (323) HelloBug Demo: ESP32 Chip Features is: 50
I (333) HelloBug Demo: ESP32 Chip Revision is: 3
I (343) HelloBug Demo: ESP32 Chip, WiFi/BT/BLE,
I (343) HelloBug Demo: SPI Flash Chip Size: 4MByte External flash
I (353) HelloBug Demo: Free Heap Size is: 299588 Byte
I (353) HelloBug Demo: Free Internal Heap Size is: 299588 Byte
I (363) HelloBug Demo: Free minimum Heap Size is: 298768 Byte
I (373) system_api: Base MAC address is not set
I (373) HelloBug Demo: Base MAC Addr : DC.45.08.80.70.48
I (383) HelloBug Demo: Hello Bug! ^_^ 10
I (1383) HelloBug Demo: Hello Bug! ^_^ 9
I (2383) HelloBug Demo: Hello Bug! ^_^ 8
I (3383) HelloBug Demo: Hello Bug! ^_^ 7
I (4383) HelloBug Demo: Hello Bug! ^_^ 6
I (5383) HelloBug Demo: Hello Bug! ^_^ 5
I (6383) HelloBug Demo: Hello Bug! ^_^ 4
I (7383) HelloBug Demo: Hello Bug! ^_^ 3
I (8383) HelloBug Demo: Hello Bug! ^_^ 2
I (9383) HelloBug Demo: Hello Bug! ^_^ 1
I (10383) HelloBug Demo: Hello Bug! ^_^ 0
举报