ESP32-c3下试跑Zephyr--基于nanoESP32-C3开发板

Creative Commons
本作品采用知识共享署名

nanoESP32-C3开发板上跑Zephyr。

上周MuseLab的吴同学寄来一片nanoESP32-C3–一块带有ESPLink(base DAPlink)的ESP32-C3开发板。下图看个正脸:

正好最近支持esp32-c3的pr已经merge进入zephyr main,就拿来试跑一下。
想要在esp32-c3上尝鲜的同学请注意,目前esp32-c3刚刚把soc移植进去,对驱动支援有限,要实际使用可能还有比较长的一段路要走。

Zephyr esp32c3编译和下载

编译环境的搭建和esp32差别不大,参考文章Zephyr ESP32环境搭建即可,主要差别是toolchain的位置,修改环境变量即可

1
export ESPRESSIF_TOOLCHAIN_PATH="${HOME}/.espressif/tools/riscv32-esp-elf/1.24.0.123_64eb9ff-8.4.0/riscv32-esp-elf"

使用esp32c3_devkitm编译出来的sample可以直接在nanoESP32-C3上跑起来:

1
west build -b esp32c3_devkitm samples/hello_world

烧写方式和esp32一致

1
west flash --esp-device /dev/ttyS5

跑起来的log如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x1 (POWERON),boot:0xc (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fcd6100,len:0x17f4
load:0x403ce000,len:0x8c8
load:0x403d0000,len:0x2c04
SHA-256 comparison failed:
Calculated: 0f38ba975edd6cdf3fa47a8186ed194cb3bf731b54225a98c8467c9b601ce1a8
Expected: 6b0078e7157b7406a271378332f5b65ceec18db5ee861bd4c76427a90993a786
Attempting to boot anyway...
entry 0x403ce000
I (49) boot: ESP-IDF c21d252a2 2nd stage bootloader
I (49) boot: compile time 14:31:11
I (49) boot: chip revision: 3
I (51) boot_comm: chip revision: 3, min. bootloader chip revision: 2
I (59) boot.esp32c3: SPI Speed : 40MHz
I (63) boot.esp32c3: SPI Mode : DIO
I (68) boot.esp32c3: SPI Flash Size : 4MB
I (73) boot: Enabling RNG early entropy source...
I (78) boot: Partition Table:
I (82) boot: ## Label Usage Type ST Offset Length
I (89) boot: 0 nvs WiFi data 01 02 00002000 00006000
I (96) boot: 1 phy_init RF data 01 01 00008000 00001000
I (104) boot: 2 factory factory app 00 00 00010000 00100000
I (111) boot: End of partition table
I (116) boot_comm: chip revision: 3, min. application chip revision: 0
I (123) esp_image: segment 0: paddr=00010020 vaddr=3c010020 size=00254h ( 596) map
I (132) esp_image: segment 1: paddr=0001027c vaddr=3fc82488 size=00068h ( 104) load
I (140) esp_image: segment 2: paddr=000102ec vaddr=40380000 size=02488h ( 9352) load
I (151) esp_image: segment 3: paddr=0001277c vaddr=00000000 size=0d89ch ( 55452)
I (168) esp_image: segment 4: paddr=00020020 vaddr=42000020 size=003ech ( 1004) map
I (170) boot: Loaded app from partition at offset 0x10000
I (172) boot: Disabling RNG early entropy source...
*** Booting Zephyr OS build zephyr-v2.6.0-1514-g718c77a4cdc4 ***
Hello World! esp32c3

和esp32一样zephyr是作为app被ESP32C3的loader引导起来的,今年开发者大会乐鑫的ppt上提到今年4季会引入MCUBoot,也许到时候stage1不再是黑盒子

调试

编译openocd

nanoESP32-C3上面带有daplink可以搭配openocd对esp32-c3进行jtag调试。由于我主力使用windows,需要windwos版本的,但官方提供编译好的windows openocd不支持daplink,因此需要自己编译。按官方的步骤编译有一些依赖问题,这里记录一下我的编译步骤:

1. 安装MSYS2

注意一定要安装MSYS2带有32位支持,执行的时候用mingw32.exe

2. 安装依赖软件

1
pacman -S --noconfirm --needed autoconf automake git make mingw-w64-i686-gcc mingw-w64-i686-toolchain mingw-w64-i686-libtool mingw-w64-i686-pkg-config mingw-w64-cross-winpthreads-git p7zip

安装libusb和libhidapi, 官方文档的libusb是手动指定的,但在configure的时候会提示找不到。libhidapi是cmsis-dap的依赖

1
2
pacman -S mingw-w64-i686-libusb
pacman -S mingw-w64-i686-hidapi

3. 编译

1
2
3
4
5
6
7
8
9
10
11
12
export LDFLAGS="$LDFLAGS -L/mingw32/bin/"
export CPPFLAGS="$CPPFLAGS -D__USE_MINGW_ANSI_STDIO=1 -Wno-error"; export CFLAGS="$CFLAGS -Wno-error"
git clone --recursive https://github.com/espressif/openocd-esp32.git
cd openocd-esp32/
./bootstrap
./configure --disable-doxygen-pdf --enable-ftdi --enable-jlink --enable-ulink --enable-cmsis-dap --build=i686-w64-mingw32 --host=i686-w64-mingw32
make
mkdir out
export DESTDIR="$PWD/out"
make install
cp /mingw32/bin/libusb-1.0.dll $DESTDIR/mingw32/bin
cp /mingw32/bin/libhidapi-0.dll $DESTDIR/mingw32/bin

指定-L/mingw32/bin/是因为在configure检查的时候遇到找不到libz的情况。
以上步骤做完后 openocd就编译并安装到out下了

问题处理

./bootstrap提示

1
2
configure.ac:205: error: possibly undefined macro: AC_DEFINE
configure.ac:614: error: possibly undefined macro: AC_MSG_NOTICE

安装pkg-config

1
acman -S pkg-config

提示

1
./bootstrap: Error: libtool is required

安装libtool

1
pacman -S libtool

启用esp32-c3 jtag

esp32-c3默认没有开启jtag支持,需要使用esp-idf下的工具重写fuse, 脚本在components/esptool_py/esptool/下,执行

1
./espefuse.py -p /dev/ttyS5 burn_efuse JTAG_SEL_ENABLE

按照提示输入BURN,看到下面的信息说明已经烧成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Connecting....
Detecting chip type... ESP32-C3
espefuse.py v3.1-dev
The efuses to burn:
from BLOCK0
- JTAG_SEL_ENABLE

Burning efuses:

- 'JTAG_SEL_ENABLE' (Set this bit to enable selection between usb_to_jtag and pad_to_jtag through strapping gpio10 when both reg_dis_usb_jtag and reg_dis_pad_jtag are equal to 0.) 0b0 -> 0b1

Check all blocks for burn...
idx, BLOCK_NAME, Conclusion
[00] BLOCK0 is empty, will burn the new value
.
This is an irreversible operation!
Type 'BURN' (all capitals) to continue.
BURN
BURN BLOCK0 - OK (write block == read block)
Reading updated efuses...
Checking efuses...
Successful
`

使用

在share/openocd/scripts/interface/cmsis-dap.cfg中添加

1
adapter_khz 5000

否则会提示Error: CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.而退出openocd
执行

1
2
cd out/mingw32/bin/
./openocd.exe -f ../share/openocd/scripts/interface/cmsis-dap.cfg -f ../share/openocd/scripts/target/esp32c3.cfg

将会看到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Open On-Chip Debugger v0.10.0-esp32-20210721 (2021-07-28-17:08)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
adapter speed: 5000 kHz

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : CMSIS-DAP: SWD Supported
Info : CMSIS-DAP: JTAG Supported
Info : CMSIS-DAP: FW Version = 0255
Info : CMSIS-DAP: Serial# = 0800000100540055430000094e504332a5a5a5a597969908
Info : CMSIS-DAP: Interface Initialised (JTAG)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 5000 kHz
Info : cmsis-dap JTAG TLR_RESET
Info : cmsis-dap JTAG TLR_RESET
Info : JTAG tap: esp32c3.cpu tap/device found: 0x00005c25 (mfg: 0x612 (Espressif Systems), part: 0x0005, ver: 0x0)
Info : datacount=2 progbufsize=16
Info : Examined RISC-V core; found 1 harts
Info : hart 0: XLEN=32, misa=0x40101104
Info : Listening on port 3333 for gdb connections

在WSL中执行

1
~/.espressif/tools/riscv32-esp-elf/1.24.0.123_64eb9ff-8.4.0/riscv32-esp-elf/bin/riscv32-esp-elf-gdb build/zephyr/zephyr.elf

开启gdb后执行

1
2
3
4
5
6
target remote 127.0.0.1:3333
set remote hardware-watchpoint-limit 2
mon reset halt
flushregs
thb main
c

就可以跳到main执行

1
2
Temporary breakpoint 1, main () at /mnt/d/code/zephyrproject/zephyr/samples/hello_world/src/main.c:12
12 printk("Hello World! %s\n", CONFIG_BOARD);

之后用gdb进行debug即可,当然也可以通过vscode调用gdb进行图像化的调试操作

参考

https://docs.zephyrproject.org/latest/boards/riscv/esp32c3_devkitm/doc/index.html
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/jtag-debugging/building-openocd-windows.html