## 參考文獻
[FreeRTOS on ESP32](https://hackmd.io/@RainbowEye0486/S1zxtaQ9_)
[ESP32使用双核FreeRTOS和Arduino环境](https://www.mekesim.com/doc/ESP32/ESP32%E4%BD%BF%E7%94%A8%E5%8F%8C%E6%A0%B8FreeRTOS%E5%92%8CArduino%E7%8E%AF%E5%A2%83/)
[ESP32從入門到進階](https://www.nmking.io/index.php/blog/esp32-started/)
[ESP32居家應用](https://esp32io.com/)
[160+ ESP32 Projects, Tutorials and Guides with Arduino IDE](https://randomnerdtutorials.com/projects-esp32/)
[ESP32應用](https://www.nmking.io/index.php/blog/esp32/?query-0-page=2)
~~[“platformio.ini” (Project Configuration File)](https://docs.platformio.org/en/latest/projectconf/index.html)~~
[VSCode ESP-IDF Extension](https://github.com/espressif/vscode-esp-idf-extension/blob/master/docs/tutorial/install.md)
[vsdcode esp-idf插件安装](https://www.cnblogs.com/xiaohuzaixue/p/17558731.html)
[ESP-IDF 编程指南](https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/index.html)
[idf-eclipse-plugin install](https://github.com/espressif/idf-eclipse-plugin/tree/master#FlashApplication)
[基於jtag對esp32-C3的調適及下載](https://docs.wireless-tech.cn/doc/7/)
[ESP32 WIFI CONNECTIVITY TUTORIAL: Menuconfig and Code Implementation Explained](https://www.youtube.com/watch?v=m2z6iwXdItc&ab_channel=InnovateYourself)
[ESP32教學](https://www.chosemaker.com/board/esp32/)
[认识 ESP-IDF-v4.3+ 工程结构(ESP32-C3 应用调整示例)](https://xie.infoq.cn/article/ddb67ebf28bfe7fecce6a2368)
[ESP-C3啟動流程詳解](https://docs.wireless-tech.cn/doc/9/)
[ESP32-C3 应用程序的启动流程](https://xie.infoq.cn/article/c7c59030b3e22ef1dcaa51368)
[ESP32 學習筆記](https://leung-manwah.blog.csdn.net/category_10970773.html)
[ESP32教學系列(八):序列周邊介面(SPI)](https://www.circuspi.com/index.php/2023/12/07/esp32-spi-introduction/)
[ESP32 教学专栏 (基于ESP-IDF)](https://blog.csdn.net/m0_50064262/category_10298046.html)
[漢亞科技 樂鑫 ESP32 相關](https://www.youtube.com/playlist?list=PLKzif2izCgMivkAPqoJHgtwQBwy0fqFg8)
[Core Dump:调试 ESP32 开发板上 Zephyr 程序错误的强大工具](https://zhuanlan.zhihu.com/p/650195547)
[ESP-IDF + Vscode ESP32 开发环境搭建以及开发入门](https://www.guyuehome.com/44770)
## 程式安裝
~~[ESP-IDF Tools Installer](https://docs.espressif.com/projects/esp-idf/en/v5.0/esp32c3/get-started/windows-setup.html#esp-idf-tools-installer)~~
[Visual Studio Code](https://code.visualstudio.com/)
## 工具安裝
於extension搜尋以下工具
* Espressif IDF
* Teleplot
~~* PlatformIO~~
### VS Code 環境設定
[Configuring Visual Studio Code](https://uchicago-cs.github.io/student-resource-guide/vscode/config.html)
### Espressif IDF
Espressif IDF 安裝完後,如VS Code無自動跳出ESP-IDF Extension Congifure,請於命令欄(Ctrl + Shift + P)輸入以下指令。
```
ESP_IDF: Configure ESP-IDF Extension
```
選擇 ESP-IDF version 後按下 Install

#### Espressif IDF安裝異常
如發生
Espressif\tools\idf-python\3.11.2\python.exe -m pip" is not valid. (ERROR_INVALID_PIP)
於資料夾Espressif\tools\idf-python\3.11.2開啟命令視窗或開啟ESP-IDF 5.1 CMD。
* 使用命令視窗時,須先輸入以下指令 (需安裝 ESP-IDF Tools)
```
C:\WINDOWS\system32\cmd.exe /k ""C:\Espressif\idf_cmd_init.bat" esp-idf-0e2582fd252b057fc2c285b273065d53"
```
* 使用ESP-IDF 5.1 CMD時,需移動到Espressif\tools\idf-python\3.11.2資料夾
```
cd \
cd Espressif\tools\idf-python\3.11.2
```
再輸入以下指令。
```
python -m ensurepip
python.exe -m pip install --upgrade pip
python -m ensurepip
```
### teleplot
build project upload後,點選右下角teleplot並開啟comport。畫面即開始繪製。
**注意:需在terminal畫面開始列印前,開啟teleplot comport。**

## VS Code PlatformIO Arduino Framwaork 專案
### 環境設定
[Platformio.ini設定](https://docs.platformio.org/en/latest/platforms/espressif32.html#cpu-frequency)
開啟platformio.ini
```
[platformio]
src_dir = ./
[env:esp32-s3-devkitc-1]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
build_flags =
-O0
-I device_driver
-I func
-I low_driver
-I app
;-I ./.pio/libdeps/esp32-s3-devkitc-1/ESP32Console
;-DCORE_DEBUG_LEVEL=0 ;None
;-DCORE_DEBUG_LEVEL=1 ;Error
;-DCORE_DEBUG_LEVEL=2 ;Warning
-DCORE_DEBUG_LEVEL=3 ;Info
;-DCORE_DEBUG_LEVEL=4 ;Debug
;-DCORE_DEBUG_LEVEL=5 ;Verbose
lib_deps = jbtronics/ESP32Console@^1.2.2
monitor_speed = 115200 ;監控COM PORT Baudrate
monitor_port = COM11 ;監控COM PORT
upload_speed = 2000000 ;ESP32S3 USB-Serial Converter maximum 2000000bps
upload_port = COM10 ;USB JTAG PORT PORT程式上傳
;upload_protocol = esp-builtin
debug_tool = esp-builtin
debug_init_break = break setup
build_type = debug ;build in debug mode instead of release mode
board_build.mcu = esp32s3 ; change microcontroller
board_build.f_cpu = 240000000L ; set frequency to 240MHz
```
## VS Code PlatformIO ESP32 Framwaork 專案
### 環境設定
開啟platformio.ini
```
[env:esp32-s3-devkitc-1]
platform = espressif32
board = esp32-s3-devkitc-1
framework = espidf
monitor_speed = 12800 ;監控COM PORT Baudrate
monitor_port = COM10 ;監控COM PORT
upload_speed = 12000000 ;USB JTAG PORT程式上傳baudrate
upload_port = COM11 ;USB JTAG PORT PORT程式上傳
debug_tool = esp-builtin
debug_init_break = break setup
build_type = debug ;build in debug mode instead of release mode
```
~~debug_tool須設定相對應的工具~~
~~```
C:\Users\hugo_peng\.platformio\packages\tool-openocd-Users\hugo_peng\.platformio\packages\tool-openocd-esp32\share\openocd\scripts\board
~~```~~

參考teleplot測試程式
```
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
void app_main()
{
float i = 0;
static const char *TAG = "MAIN";
ESP_LOGI(TAG, "Hello World!!!");
while (1)
{
ESP_LOGI(TAG, "Hello World!!!");
// Plot a sinus
printf(">sin:%f\n", sin(i));
// Plot a cosinus
printf(">cos:%f\n", cos(i));
// usleep(10000);
vTaskDelay(500 / portTICK_PERIOD_MS);
i += 0.1;
}
}
```
#### terminal baud
開啟 Terminal 輸入以下指令。
```
C:\WINDOWS\system32\cmd.exe /k ""C:\Espressif\idf_cmd_init.bat" esp-idf-0e2582fd252b057fc2c285b273065d53"
```
再輸入 idf.py menuconfig,可開啟系統設置。
terminal baud 須設定與 UART0 Baud一致。

注:於VS code terminal內可使用 J/K 上下移動
#### terminal print
build project upload後,點選monitor。列印的字串會顯示在terminal畫面。

#### ESP Log Setting
將LOG Output 設定置為 VerBose,即可顯示ESP_LOG所有level訊息。

[ESP-IDF components](https://components.espressif.com/)
## VS Code ESP-IDF 專案
### 新增專案
開啟command palette (ctrl + shift + P),尋找 ESP-IDF: New Project,即可新增專案。

### 載入專案
於File -> Open Folder,即可載入專案。

### 新增 ESP-IDF components
開啟command palette (ctrl + shift + P),尋找ESP-IDF: Show Component Registry。尋找led_strip,選擇版本後,按下Install。

確認於目錄下有無新增 idf_component.yml,並且檔案有include led_strip及版本以上限定。

### console baud
點下側齒輪開啟SDK Configuration Editor (menuconfig)。
於 Component config -> ESP System Settings -> Memory protection 下將 Channel for console output 設定成 Custom UART,即可設定 baud。

### terminal baud
於Extensiion下,對Espressif IDF按右鍵或點選齒輪選擇 Extension Settings,即可設定monitor baud。須注意此baud需與ESP32 console baud設定一致。

### Log Setting
點下側齒輪開啟SDK Configuration Editor (menuconfig)。
於 Component config -> ESP System Settings -> Log output,即可設定 esp_log.h 輸出層級。

### MCU & COM Port Setting
1. 選擇COM Port
2. 選擇MCU
3. 選擇燒入介面
4. 選擇燒入方式
5. 燒入(flash)

### 測試程式
```
#include <stdio.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "led_strip.h"
#include "sdkconfig.h"
#define BLINK_GPIO 48
static uint8_t s_led_state = 0;
static led_strip_handle_t led_strip;
static const char *TAG = "MAIN";
static void blink_led(void)
{
/* If the addressable LED is enabled */
ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
if (s_led_state)
{
/* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
led_strip_set_pixel(led_strip, 0, 16, 16, 16);
/* Refresh the strip to send data */
led_strip_refresh(led_strip);
}
else
{
/* Set all LED off to clear all pixels */
led_strip_clear(led_strip);
}
s_led_state = !s_led_state;
}
static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink addressable LED!");
/* LED strip initialization with the GPIO and pixels number*/
led_strip_config_t strip_config = {
.strip_gpio_num = BLINK_GPIO,
.max_leds = 1, // at least one LED on board
};
led_strip_rmt_config_t rmt_config = {
.resolution_hz = 10 * 1000 * 1000, // 10MHz
};
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
/* Set all LED off to clear all pixels */
led_strip_clear(led_strip);
}
void app_main()
{
float i = 0;
ESP_LOGI(TAG, "Hello World!!!");
configure_led();
while (1)
{
ESP_LOGI(TAG, "Hello World!!!");
// Plot a sinus
printf(">sin:%f\n", sin(i));
// Plot a cosinus
printf(">cos:%f\n", cos(i));
blink_led();
// usleep(10000);
vTaskDelay(500 / portTICK_PERIOD_MS);
i += 0.1;
}
}
```
### Debug mode
進行Debug mode前,將燒入模式選擇USB-JTAG後,透過JTAG燒入方式燒入ESP32。

進入後,上方可進行單步執行等指令。

如發生以下異常,在點選一次Start Debugger。~~當確認無法進入Debug mode時,請做環境確認及launch.json 修改。~~

#### 環境確認
透過Zadig確認WCID狀態。打勾的更新驅動程式為WinUSB,打X的設定為USB Serial (CDC)。


~~#### launch.json 修改~~
```
{
"version": "0.2.0",
"configurations": [
{
"type": "espidf",
"name": "Launch",
"request": "launch",
"mode": "auto",
"skipVerifyAppBinBeforeDebug": true
}
]
}
```