1. 程式人生 > >【嵌入式硬體Esp32】(1)例程Hello World Example 註釋

【嵌入式硬體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