nihui / ncnn_on_esp32

66 stars 10 forks source link

ncnn on esp32

1. setup esp-idf

see https://github.com/espressif/esp-idf

2. build ncnn library for esp32

git clone https://github.com/Tencent/ncnn.git

mkdir build-esp32
cd build-esp32
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/esp32c6.toolchain.cmake ..
make -j4
make install

3. build ncnn esp32 mnist sample

modify ncnn_DIR in CMakeLists.txt to your ncnn installation

set(ncnn_DIR "/home/nihui/dev/ncnn/build-esp32/install/lib/cmake/ncnn")

enable sgemm convolution for esp32c6 / esp32s3 with larger ram, edit main/main.cpp

// but esp32c3 has lower memory
net.opt.use_sgemm_convolution = true;

build project with idf.py

idf.py set-target esp32c6
idf.py menuconfig

idf.py build

4. flash to device and monitor serial port

idf.py flash
tio /dev/ttyACM0

or

tio /dev/ttyUSB0

esp32c3

ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0xc (RTC_SW_CPU_RST),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x40380700
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0x1738
load:0x403cc710,len:0xb9c
load:0x403ce710,len:0x2e40
entry 0x403cc71a
I (35) boot: ESP-IDF v5.3-dev-2815-gbe06a6f5ff 2nd stage bootloader
I (35) boot: compile time Apr 14 2024 13:28:16
I (36) boot: chip revision: v0.3
I (40) boot.esp32c3: SPI Speed      : 80MHz
I (45) boot.esp32c3: SPI Mode       : DIO
I (49) boot.esp32c3: SPI Flash Size : 2MB
I (54) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (63) boot: ## Label            Usage          Type ST Offset   Length
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (78) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (85) boot:  2 factory          factory app      00 00 00010000 00100000
I (93) boot: End of partition table
I (97) esp_image: segment 0: paddr=00010020 vaddr=3c0c0020 size=0d884h ( 55428) map
I (114) esp_image: segment 1: paddr=0001d8ac vaddr=3fc89400 size=00f74h (  3956) load
I (115) esp_image: segment 2: paddr=0001e828 vaddr=40380000 size=017f0h (  6128) load
I (123) esp_image: segment 3: paddr=00020020 vaddr=42000020 size=b7cb4h (752820) map
I (248) esp_image: segment 4: paddr=000d7cdc vaddr=403817f0 size=07a04h ( 31236) load
I (257) boot: Loaded app from partition at offset 0x10000
I (257) boot: Disabling RNG early entropy source...
I (269) cpu_start: Unicore app
I (278) cpu_start: Pro cpu start user code
I (278) cpu_start: cpu freq: 160000000 Hz
I (278) app_init: Application information:
I (281) app_init: Project name:     main
I (285) app_init: App version:      a36153d-dirty
I (291) app_init: Compile time:     Apr 14 2024 13:28:13
I (297) app_init: ELF file SHA256:  99b26c75b...
I (302) app_init: ESP-IDF:          v5.3-dev-2815-gbe06a6f5ff
I (308) efuse_init: Min chip rev:     v0.3
I (313) efuse_init: Max chip rev:     v1.99
I (318) efuse_init: Chip rev:         v0.3
I (323) heap_init: Initializing. RAM available for dynamic allocation:
I (330) heap_init: At 3FC8B320 len 00034CE0 (211 KiB): RAM
I (336) heap_init: At 3FCC0000 len 0001C710 (113 KiB): Retention RAM
I (343) heap_init: At 3FCDC710 len 00002950 (10 KiB): Retention RAM
I (350) heap_init: At 50000200 len 00001DE8 (7 KiB): RTCRAM
I (357) spi_flash: detected chip: generic
I (361) spi_flash: flash io: dio
W (365) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (378) sleep: Configure to isolate all GPIO pins in sleep state
I (385) sleep: Enable automatic switching of GPIO sleep configuration
I (392) main_task: Started on CPU0
I (402) main_task: Calling app_main()
Loading ncnn mnist model...Done.
Preparing input...Start Mesuring!
Done!
0: -1.63
1: 3.05
2: 12.36
3: 7.95
4: -17.89
5: -9.99
6: -15.13
7: 16.36
8: 0.45
9: -3.34
I think it is number 7!
Latency, avg: 105.66ms, max: 105.71, min: 105.64. Avg Flops: 7.38MFlops
Restarting now.

esp32c6

ESP-ROM:esp32c6-20220919
Build:Sep 19 2022
rst:0xc (SW_CPU),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x4001975a
SPIWP:0xee
mode:DIO, clock div:2
load:0x40875720,len:0x1804
load:0x4086c110,len:0xe2c
load:0x4086e610,len:0x2e30
entry 0x4086c11a
I (22) boot: ESP-IDF v5.3-dev-2815-gbe06a6f5ff 2nd stage bootloader
I (23) boot: compile time Apr 14 2024 11:52:36
I (24) boot: chip revision: v0.0
I (26) boot.esp32c6: SPI Speed      : 80MHz
I (31) boot.esp32c6: SPI Mode       : DIO
I (36) boot.esp32c6: SPI Flash Size : 2MB
I (41) boot: Enabling RNG early entropy source...
I (46) boot: Partition Table:
I (50) boot: ## Label            Usage          Type ST Offset   Length
I (57) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (64) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (72) boot:  2 factory          factory app      00 00 00010000 00100000
I (79) boot: End of partition table
I (83) esp_image: segment 0: paddr=00010020 vaddr=420b0020 size=0a9e0h ( 43488) map
I (110) esp_image: segment 1: paddr=0001aa08 vaddr=40800000 size=05610h ( 22032) load
I (122) esp_image: segment 2: paddr=00020020 vaddr=42000020 size=aa960h (698720) map
I (407) esp_image: segment 3: paddr=000ca988 vaddr=40805610 size=03e74h ( 15988) load
I (416) esp_image: segment 4: paddr=000ce804 vaddr=40809490 size=00f64h (  3940) load
I (424) boot: Loaded app from partition at offset 0x10000
I (425) boot: Disabling RNG early entropy source...
I (436) cpu_start: Unicore app
I (446) cpu_start: Pro cpu start user code
I (446) cpu_start: cpu freq: 160000000 Hz
I (447) app_init: Application information:
I (449) app_init: Project name:     main
I (454) app_init: App version:      a36153d-dirty
I (459) app_init: Compile time:     Apr 14 2024 11:52:33
I (465) app_init: ELF file SHA256:  0a0e007ce...
I (470) app_init: ESP-IDF:          v5.3-dev-2815-gbe06a6f5ff
I (477) efuse_init: Min chip rev:     v0.0
I (482) efuse_init: Max chip rev:     v0.99
I (487) efuse_init: Chip rev:         v0.0
I (491) heap_init: Initializing. RAM available for dynamic allocation:
I (499) heap_init: At 4080B3B0 len 00071260 (452 KiB): RAM
I (505) heap_init: At 4087C610 len 00002F54 (11 KiB): RAM
I (511) heap_init: At 50000000 len 00003FE8 (15 KiB): RTCRAM
I (518) spi_flash: detected chip: generic
I (522) spi_flash: flash io: dio
W (526) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (539) sleep: Configure to isolate all GPIO pins in sleep state
I (546) sleep: Enable automatic switching of GPIO sleep configuration
I (553) coexist: coex firmware version: d96c1e51f
I (558) coexist: coexist rom version 5b8dcfa
I (563) main_task: Started on CPU0
I (563) main_task: Calling app_main()
Loading ncnn mnist model...Done.
Preparing input...Start Mesuring!
Done!
0: -1.63
1: 3.05
2: 12.36
3: 7.95
4: -17.89
5: -9.99
6: -15.13
7: 16.36
8: 0.45
9: -3.34
I think it is number 7!
Latency, avg: 78.77ms, max: 79.48, min: 78.68. Avg Flops: 9.90MFlops
Restarting now.