[Memo] ESP32-S3-BOX-3 開発環境

## 概要
- ESP32-S3 のIDF開発環境をWindowsのWSL環境で試してみよう。

## 環境
- ESP32-S3-BOX3  --- 随分前(2023-11?)に購入していた...

## 参考
- ESP-IDF Programming Guide
https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/index.html

- Schematics: https://github.com/espressif/esp-box/tree/master/hardware/SCH_ESP32-S3-BOX-3_V1.0
- ESP-BOX AIoT Development Framework: https://github.com/espressif/esp-box

https://espressif.github.io/esp-launchpad/?flashConfigURL=https://espressif.github.io/esp-box/launchpad.toml


## 手順
### インストール
- Linux 
https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/get-started/linux-macos-setup.html


1. 前準備 / Prerequisites

	- For Linux Users
		```bash
		sudo apt-get install git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
		```
	- Installing Python 3 --- WSLに元から入ってる(?)ので、スキップ

2. ESP-IDFの取得 / Get ESP-IDF
	```bash
	mkdir -p ~/esp
	cd ~/esp
	git clone --recursive https://github.com/espressif/esp-idf.git -j 6
	```

    - ちなみにサイズはこんな感じ
		```shell-session
		$ cd
		$ date
		Thu Jan 30 22:37:21 PST 2025
		$ du -sh ./esp/esp-idf
		1. 3G    ./esp/esp-idf
		$ du -sk ./esp/esp-idf
		2385272 ./esp/esp-idf
		```
		
3. ツールのダウンロードとセットアップ / Set up the Tools
	```bash
	# bashで.
	cd ~/esp/esp-idf
	./install.sh esp32s3
	```

	- ちなみにサイズはこんな感じ
		```bash
		$ cd
		$ du -sh ./.espressif/
		1. 8G    ./.espressif/
		$ du -sk ./.espressif/
		3940860 ./.espressif/
		```
		
4. 環境変数の設定 / Set up the Environment Variables
	```bash
	. $HOME/esp/esp-idf/export.sh
	```

	- こんな感じ
		```shell-session
		$ . $HOME/esp/esp-idf/export.sh
		Checking "python3" ...
		Python 3.12.3
		"python3" has been detected
		Activating ESP-IDF 5.5
		Setting IDF_PATH to '/home/mt08/esp/esp-idf'.
		* Checking python version ... 3.12.3
		* Checking python dependencies ... OK
		* Deactivating the current ESP-IDF environment (if any) ... OK
		* Establishing a new ESP-IDF environment ... OK
		* Identifying shell ... bash
		* Detecting outdated tools in system ... OK - no outdated tools found
		* Shell completion ... Autocompletion code generated
		
		Done! You can now compile ESP-IDF projects.
		Go to the project directory and run:
		
		  idf.py build
		$
		```

    ----
    ----
    ----

    - WindowsホストのUSBデバイスをWSLで使うため、`usbipd`をWindows側にインストール
    	1. PowerShellを管理者で起動
    	2. `winget install usbipd` 
    		にてインストール
    	3. 一度、PowerShellを閉じて、再度AdminでPowerShell起動
    	4.  `usbipd list`
    		デバイスリスト表示にて、ESP32S3のBUSIDを確認。↓こんなやつ
    		```ps
    		BUSID  VID:PID    DEVICE                                                        STATE
    		2-6    303a:1001  USB Serial Device (COM3), USB JTAG/serial debug unit          Not shared
    		```
    	5. `bind`して、`wsl`に`attach`
    		```ps
    		# 2-6はBUSID
    		usbipd bind --busid 2-6
    		usbipd list	
    		usbipd attach --wsl --busid 2-6
    		usbipd list
    		```
    
    - Linux側にて
    	- `sudo apt install usbutils -y`
    		(lsusbを使うため)
    	- `lsusb` で確認
    		```shell-session
    		$ lsusb
    		Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    		Bus 001 Device 003: ID 303a:1001 Espressif USB JTAG/serial debug unit
    		Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
    		$
    		```
    
    - ちなみに　取り外しは、Windows側で、`detach` して `unbind`する
    	```
    	usbipd detach --busid 2-6
    	usbipd unbind --busid 2-6
    	```
    ----
    ----
    ----

5. サンプルビルド、書き込み、シリアルモニター / First Steps on ESP-IDF
	
	```
	# サンプルプロジェクトのコピー
	cd ~/esp
	cp -r $IDF_PATH/examples/get-started/hello_world .
	
	# ESP32-S3をUSB接続
		
	# esp32s3用に設定
	cd ~/esp/hello_world
	idf.py set-target esp32s3
	# menuconfigでさらに細かく設定もできるらしい
	# idf.py menuconfig
	
	# ビルド / Build the Project
	idf.py build
	
	# 書き込み / Flash
	idf.py flash
	# ポートを指定してもできる
	## idf.py -p PORT flash  
	## idf.py -p /dev/ttyACM0 flash
	
	# シリアルモニター起動 CTRL + ] で抜ける// Monitor the Output  (CTRL+] to Exit)
	idf.py monitor
	# ポートを指定してもできる
	## idf.py -p PORT monitor
	```


# 実行例

- monitorは、CTRL+] で抜ける
    
    ```bash
    $ idf.py monitor
    Executing action: monitor
    Serial port /dev/ttyACM0
    Connecting...
    Detecting chip type... ESP32-S3
    Running idf_monitor in directory /home/mt08/esp/hello_world
    Executing "/home/mt08/.espressif/python_env/idf5.5_py3.12_env/bin/python /home/mt08/esp/esp-idf/tools/idf_monitor.py -p /dev/ttyACM0 -b 115200 --toolchain-prefix xtensa-esp32s3-elf- --target esp32s3 --revision 0 /home/mt08/esp/hello_world/build/hello_world.elf /home/mt08/esp/hello_world/build/bootloader/bootloader.elf -m '/home/mt08/.espressif/python_env/idf5.5_py3.12_env/bin/python' '/home/mt08/esp/esp-idf/tools/idf.py'"...
    --- esp-idf-monitor 1.5.0 on /dev/ttyACM0 115200
    --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
    ESP-ROM:esp32s3-20210327
    Build:Mar 27 2021
    rst:0x15 (USB_UART_CHIP_RESET),boot:0xa (SPI_FAST_FLASH_BOOT)
    Saved PC:0x40048ee0
    --- 0x40048ee0: recv_packet in ROM
    
    SPIWP:0xee
    mode:DIO, clock div:1
    load:0x3fce2820,len:0x15d0
    load:0x403c8700,len:0xd20
    --- 0x403c8700: _stext at ??:?
    
    load:0x403cb700,len:0x2f60
    entry 0x403c8924
    --- 0x403c8924: call_start_cpu0 at /home/mt08/esp/esp-idf/components/bootloader/subproject/main/bootloader_start.c:25
    
    I (18) boot: ESP-IDF v5.5-dev-1655-gc5865270b5 2nd stage bootloader
    I (18) boot: compile time Jan 30 2025 22:45:09
    I (18) boot: Multicore bootloader
    I (20) boot: chip revision: v0.2
    I (23) boot: efuse block revision: v1.3
    I (26) boot.esp32s3: Boot SPI Speed : 80MHz
    I (30) boot.esp32s3: SPI Mode       : DIO
    I (34) boot.esp32s3: SPI Flash Size : 2MB
    I (38) boot: Enabling RNG early entropy source...
    I (42) boot: Partition Table:
    I (45) boot: ## Label            Usage          Type ST Offset   Length
    I (51) boot:  0 nvs              WiFi data        01 02 00009000 00006000
    I (58) boot:  1 phy_init         RF data          01 01 0000f000 00001000
    I (64) boot:  2 factory          factory app      00 00 00010000 00100000
    I (71) boot: End of partition table
    I (74) esp_image: segment 0: paddr=00010020 vaddr=3c020020 size=07e64h ( 32356) map
    I (87) esp_image: segment 1: paddr=00017e8c vaddr=3fc91f00 size=02868h ( 10344) load
    I (91) esp_image: segment 2: paddr=0001a6fc vaddr=40374000 size=0591ch ( 22812) load
    I (101) esp_image: segment 3: paddr=00020020 vaddr=42000020 size=13814h ( 79892) map
    I (118) esp_image: segment 4: paddr=0003383c vaddr=4037991c size=085d4h ( 34260) load
    I (126) esp_image: segment 5: paddr=0003be18 vaddr=600fe000 size=0001ch (    28) load
    I (132) boot: Loaded app from partition at offset 0x10000
    I (132) boot: Disabling RNG early entropy source...
    I (143) cpu_start: Multicore app
    I (153) cpu_start: Pro cpu start user code
    I (153) cpu_start: cpu freq: 160000000 Hz
    I (153) app_init: Application information:
    I (153) app_init: Project name:     hello_world
    I (157) app_init: App version:      1
    I (160) app_init: Compile time:     Jan 30 2025 22:45:02
    I (165) app_init: ELF file SHA256:  2fcf9bc55...
    I (170) app_init: ESP-IDF:          v5.5-dev-1655-gc5865270b5
    I (175) efuse_init: Min chip rev:     v0.0
    I (179) efuse_init: Max chip rev:     v0.99
    I (183) efuse_init: Chip rev:         v0.2
    I (187) heap_init: Initializing. RAM available for dynamic allocation:
    I (193) heap_init: At 3FC94FB0 len 00054760 (337 KiB): RAM
    I (198) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
    I (203) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
    I (209) heap_init: At 600FE01C len 00001FCC (7 KiB): RTCRAM
    I (215) spi_flash: detected chip: gd
    I (217) spi_flash: flash io: dio
    W (220) spi_flash: Detected size(16384k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
    I (233) sleep_gpio: Configure to isolate all GPIO pins in sleep state
    I (239) sleep_gpio: Enable automatic switching of GPIO sleep configuration
    I (246) main_task: Started on CPU0
    I (256) main_task: Calling app_main()
    Hello world!
    This is esp32s3 chip with 2 CPU core(s), WiFi/BLE, silicon revision v0.2, 2MB external flash
    Minimum free heap size: 393108 bytes
    Restarting in 10 seconds...
    Restarting in 9 seconds...
    Restarting in 8 seconds...
    Restarting in 7 seconds...
    Restarting in 6 seconds...
    Restarting in 5 seconds...
    Restarting in 4 seconds...
    Restarting in 3 seconds...
    Restarting in 2 seconds...
    Restarting in 1 seconds...
    Restarting in 0 seconds...
    Restarting now.
    ESP-ROM:esp32s3-20210327
    Build:Mar 27 2021
    rst:0xc (RTC_SW_CPU_RST),boot:0xa (SPI_FAST_FLASH_BOOT)
    Saved PC:0x403758d0
    --- 0x403758d0: esp_restart_noos at /home/mt08/esp/esp-idf/components/esp_system/port/soc/esp32s3/system_internal.c:160
    
    SPIWP:0xee
    mode:DIO, clock div:1
    load:0x3fce2820,len:0x15d0
    load:0x403c8700,len:0xd20
    --- 0x403c8700: _stext at ??:?
    
    load:0x403cb700,len:0x2f60
    entry 0x403c8924
    --- 0x403c8924: call_start_cpu0 at /home/mt08/esp/esp-idf/components/bootloader/subproject/main/bootloader_start.c:25
    
    I (18) boot: ESP-IDF v5.5-dev-1655-gc5865270b5 2nd stage bootloader
    ...
    ```