【嵌入式硬件Esp32】(1)例程Hello World Example 注释

/* 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_system.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");

for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
}
void esp_chip_info(esp_chip_info_t * out_info )
使用有关芯片的信息填充esp_chip_info_t结构。

参数
out_info:要填充的结构
Structures
结构:esp_chip_info_t
该结构代表有关芯片的信息。

公众成员

esp_chip_model_t model
芯片模型,esp_chip_model_t之一

uint32_t的features
CHIP_FEATURE_x功能标志的位掩码

uint8_t cores
CPU核心数量

uint8_t revision
芯片修订号
size_t spi_flash_get_chip_size()
获取闪存芯片大小,如二进制映像头中所设置。
注意
此值不一定与实际闪存大小匹配。
返回
闪存芯片的大小,以字节为单位

  

void vTaskDelay(const TickType_t xTicksToDelay)
Delay a task for a given number of ticks.

The actual time that the task remains blocked depends on the tick rate. The constant portTICK_PERIOD_MS can be used to calculate real time from the tick rate - with the resolution of one tick period.

INCLUDE_vTaskDelay must be defined as 1 for this function to be available. See the configuration section for more information.

vTaskDelay() specifies a time at which the task wishes to unblock relative to the time at which vTaskDelay() is called. For example, specifying a block period of 100 ticks will cause the task to unblock 100 ticks after vTaskDelay() is called. vTaskDelay() does not therefore provide a good method of controlling the frequency of a periodic task as the path taken through the code, as well as other task and interrupt activity, will effect the frequency at which vTaskDelay() gets called and therefore the time at which the task next executes. See vTaskDelayUntil() for an alternative API function designed to facilitate fixed frequency execution. It does this by specifying an absolute time (rather than a relative time) at which the calling task should unblock.

Example usage:

void vTaskFunction( void * pvParameters )
{
// Block for 500ms.
const TickType_t xDelay = 500 / portTICK_PERIOD_MS;

    for( ;; )
    {
        // Simply toggle the LED every 500ms, blocking between each toggle.
        vToggleLED();
        vTaskDelay( xDelay );
    }
}
Parameters
xTicksToDelay: The amount of time, in tick periods, that the calling task should block.

看完hello_world也总要学点东西吧:

1.#include <stdio.h>可以看出来该SDK是依赖标准C库

2.#include "freertos/FreeRTOS.h"可以看出来该SDK定义为FreeRTOS系统,即嵌入式实时操作系统RTOS
   #include "freertos/task.h"

3.void app_main(){}可以看出app_main是程序入口函数,也是我们所说的主函数main

4.vTaskDelay(1000 / portTICK_PERIOD_MS)是FreeRTOS里面的常用延迟函数,这函数有一定的研究价值,后面讨论

5.fflush(stdout)的使用很细心,乐鑫代码水平还是不错的

分析fflush(stdout)的使用:

printf是一个行缓冲函数,先写到缓冲区,满足条件后,才将缓冲区刷到对应文件中,刷缓冲区的条件如下:    

1 )缓冲区填满    

2 )写入的字符中有'\n'     

3 )调用fflush手动刷新缓冲区    

4 )调用scanf要从缓冲区中读取数据时,也会将缓冲区内的数据刷新

有人问为何我以前程序没有添加'\n'也能顺利打印出来,因为你printf不是在一个循环里面,便也不会牵扯到问题,因为就算执行printf后只是将内容送到缓冲区,但是你到程序结束里,程序结束便会导致缓冲区刷新,你便看到你到屏幕上有你期望到东西出现了,当然,经过测试,这也是跟编译器有关,但在while里面使用printf是跟上一句fflush(stdout)是保险的做法,手动刷新缓冲区,避免造成不必要的bug

最后,这代码分析到这里,可以尝试运行你的第一个hello_world demo
 

猜你喜欢

转载自www.cnblogs.com/xiaoyehack/p/9900190.html