前面一篇文章已构建了开发环境,接下来基于已有的环境,构建第一个应用程序。首先创建一个存放工程的目录,接下来直接将sdk
目录下的hello world
例子内容拷贝到该目录。内容如下:
$ tree
.
├── main
│ ├── component.mk
│ └── hello_world_main.c
├── Makefile
├── sdkconfig
└── sdkconfig.old
1 directory, 5 files
因为原先的例子是要重启系统的,稍微更改一下程序,使其每隔一秒钟计数即可,修改后内容如下:
/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_spi_flash.h"
void app_main()
{
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ",
chip_info.cores,
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
printf("silicon revision %d, ", chip_info.revision);
printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
int i = 0;
while (1) {
printf("In %d seconds...\n", i++);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
return;
}
为何我们的主程序入口是app_main
呢?具体查阅components/esp32/cpu_start.c
这个文件的源码就明白了。vTaskDelay
是FreeRTOS
系统定义的接口,表示系统延迟若干个时间片,执行到这里,系统会挂起,去执行其它的任务。
编译安装:
$ make
$ make flash monitor
CC build/main/hello_world_main.o
AR build/main/libmain.a
LD build/hello-world.elf
esptool.py v2.1
Flashing binaries to serial port /dev/ttyUSB0 (app at offset 0x10000)...
esptool.py v2.1
Connecting.....
Chip is ESP32D0WDQ5 (revision 0)
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 19600 bytes to 11521...
Wrote 19600 bytes (11521 compressed) at 0x00001000 in 1.0 seconds (effective 153.5 kbit/s)...
Hash of data verified.
Compressed 130304 bytes to 69678...
Wrote 130304 bytes (69678 compressed) at 0x00010000 in 6.2 seconds (effective 169.2 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 82...
Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 2117.9 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting...
MONITOR
--- idf_monitor on /dev/ttyUSB0 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_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:0x3fff0018,len:4
load:0x3fff001c,len:5692
load:0x40078000,len:0
load:0x40078000,len:13804
entry 0x40079030
I (69) boot: Detected ESP32
I (32) boot: ESP-IDF v3.0-dev-1295-g08be5213 2nd stage bootloader
I (32) boot: compile time 23:00:41
I (32) boot: Enabling RNG early entropy source...
I (38) boot: SPI Speed : 40MHz
I (42) boot: SPI Mode : DIO
I (46) boot: SPI Flash Size : 4MB
I (50) boot: Partition Table:
I (54) boot: ## Label Usage Type ST Offset Length
I (61) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (69) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (76) boot: 2 factory factory app 00 00 00010000 00100000
I (84) boot: End of partition table
I (88) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x04ab0 ( 19120) map
I (103) esp_image: segment 1: paddr=0x00014ad8 vaddr=0x3ffb0000 size=0x02118 ( 8472) load
I (109) esp_image: segment 2: paddr=0x00016bf8 vaddr=0x40080000 size=0x00400 ( 1024) load
0x40080000: _iram_start at /srv/esp/esp-idf/components/freertos/./xtensa_vectors.S:1685
I (115) esp_image: segment 3: paddr=0x00017000 vaddr=0x40080400 size=0x081f4 ( 33268) load
I (137) esp_image: segment 4: paddr=0x0001f1fc vaddr=0x400c0000 size=0x00000 ( 0) load
I (138) esp_image: segment 5: paddr=0x0001f204 vaddr=0x00000000 size=0x00e0c ( 3596)
I (145) esp_image: segment 6: paddr=0x00020018 vaddr=0x400d0018 size=0x0fcbc ( 64700) map
0x400d0018: _stext at ??:?
I (180) boot: Loaded app from partition at offset 0x10000
I (180) boot: Disabling RNG early entropy source...
I (181) cpu_start: Pro cpu up.
I (184) cpu_start: Starting app cpu, entry point is 0x40080fe0
0x40080fe0: call_start_cpu1 at /srv/esp/esp-idf/components/esp32/./cpu_start.c:222
I (0) cpu_start: App cpu up.
I (195) heap_init: Initializing. RAM available for dynamic allocation:
I (201) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (208) heap_init: At 3FFB2920 len 0002D6E0 (181 KiB): DRAM
I (214) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (220) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (227) heap_init: At 400885F4 len 00017A0C (94 KiB): IRAM
I (233) cpu_start: Pro cpu start user code
I (250) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Hello world!
This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 0, 4MB external flash
In 0 seconds...
In 1 seconds...
In 2 seconds...
In 3 seconds...
In 4 seconds...
In 5 seconds...
In 6 seconds...
In 7 seconds...
In 8 seconds...
In 9 seconds...
In 10 seconds...
portTICK_PERIOD_MS
定义在components/freertos/include/freertos/portmacro.h
文件中:
#define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
configTICK_RATE_HZ
定义在components/freertos/include/freertos/FreeRTOSConfig.h
文件中:
#define configTICK_RATE_HZ ( CONFIG_FREERTOS_HZ )
而CONFIG_FREERTOS_HZ
则定义在sdkconfig
文件(执行make menuconfig
后生成的配置文件)中,说明该值是可以根据实际需求配置的。
$ cat sdkconfig | grep CONFIG_FREERTOS_HZ
CONFIG_FREERTOS_HZ=100
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。