1# QEMU(Tutorials of ESP32) 2 3## 1.Introduction 4 5QEMU can simulate the kernel to run on different boards ,eliminate dependence on physical development boards. The esp32 subdirectory contains part of the OpenHarmony kernel\_liteos\_m code verified by the Qemu esp32 virtualization platform, it can be used to simulate a single-core esp32 single board. 6 7## 2.Setup Environment 8 9 1. Install esp-idf 10 11 Please refer to the installation instructions: (https://docs.espressif.com/projects/esp-idf/en/release-v4.1/get-started/index.html) 12 13 Annotation: The installation of esp-idf can be skipped. The bootloader.bin and partition-table.bin generated have been placed in the vendor\ohemu\qemu_xtensa_mini_system_demo\image folder. 14 15 Annotation: If you skip the installation of esp-idf, you can install the toolchain as follows: 16 17 Tip: Users can also skip this step by using the following instructions directly to use the compiler configured in the default environment. 18 19 To use the default environment, execute '3.Get source code' and then install the default compiler in the root directory by executing the following instructions. 20 21 ```shell 22 sh build/prebuilts_download.sh 23 ``` 24 25 Optional compiler installation process: 26 27 a) Download the esp official release the SDK package: https://www.espressif.com/zh-hans/support/download/sdks-demos?keys=&field_type_tid%5B%5D=13 28 29 b) Put the downloaded SDK package into the Linux system, go to the directory, and run the following commands: 30 31 ```shell 32 unzip esp-idf-v4.3.1.zip 33 cd esp-idf-v4.3.1/ 34 ./install.sh 35 . ./export.sh 36 ``` 37 38 c) Delete the default compiler path: 39 40 change esp32\liteos_m\config.gni: 41 42 ```c 43 board_toolchain_path = "$ohos_root_path/prebuilts/gcc/linux-x86/esp/esp-2019r2-8.2.0/xtensa-esp32-elf/bin/" 44 ``` 45 46 to 47 48 ```c 49 board_toolchain_path = "" 50 ``` 51 52 Annotation: The version of the toolchain used in the test is GCC Version 8.2.0 (Crosstool-ng ESP-2019R2) or GCC Version 8.4.0 (Crosstool-ng ESP-2021R1). 53 54 2. Install esptool 55 56 a) In step 1, the export.sh script will set the esptool in the environment path, you need to ensure that the esptool version is 3.1 and above. 57 ```shell 58 esptool.py version 59 ``` 60 b) If the version of the esptool that comes with esp-idf is too low, delete the environment variable corresponding to the current esptool path and run the following commands. (The recommended python version is 3.8 and above) 61 ```shell 62 python -m pip install esptool 63 ``` 64 65 3. Compile qemu-system-xtensa 66 67 a) Install and compile 68 69 ```shell 70 git clone https://github.com/espressif/qemu.git 71 cd qemu 72 ./configure --target-list=xtensa-softmmu \ 73 --enable-gcrypt \ 74 --enable-debug --enable-sanitizers \ 75 --disable-strip --disable-user \ 76 --disable-capstone --disable-vnc \ 77 --disable-sdl --disable-gtk 78 ``` 79 80 b) Waitting for the completion of the compilation and executing the installation command (If the compilation fail, please refer to https://github.com/espressif/qemu/issues/21): 81 82 ```shell 83 ninja -C build 84 ``` 85 86 c) Add qemu to the environment variable (modify user_qemu_xxx_path to your own installation path): 87 88 ```shell 89 vim ~/.bashrc 90 export QEMU=user_qemu_xxx_path/qemu/build 91 source ~/.bashrc 92 ``` 93 94 d) Installation dependencies 95 96 ```shell 97 ldd $QEMU/qemu-system-xtensa 98 ``` 99 100 According to the execution result of ldd, install the missing dependent libraries 101 102 (Annotation: For more installation instructions, please refer to the following link: [Home · espressif/qemu Wiki · GitHub](https://github.com/espressif/qemu/wiki#configure)) 103 104## 3.Get source code 105 106[code acquisition ](https://gitee.com/openharmony/docs/blob/master/en/device-dev/get-code/sourcecode-acquire.md) 107 108Hint : You can use the `repo` command to get the source code. 109 110## 4.Source buildding 111 112 1. Execute the hb set command and select the project `qemu_xtensa_mini_system_demo`. 113 114 2. Execute the hb clean && hb build command to build the executable file that produces `OHOS_Image`. 115 116 ```shell 117 hb set 118 hb clean && hb build 119 ``` 120 121 3. After the buildding is complete, the corresponding executable file is in the home directory: 122 123 ``` 124 out/esp32/qemu_xtensa_mini_system_demo/ 125 ``` 126 127## 5.Run the image in Qemu 128 129 1. Run qemu(Don't cooperate with GDB ) 130 131 ```shell 132 ./qemu-run 133 ``` 134 135 2. Run qemu(Cooperate with GDB) 136 137 a) Start the GDB server and wait for the connection 138 139 ```shell 140 ./qemu-run -g 141 ``` 142 143 b) Create a new terminal and use GDB to connect to qemu 144 145 ```shell 146 xtensa-esp32-elf-gdb out/esp32/qemu_xtensa_mini_system_demo/OHOS_Image -ex "target remote :1234" 147 ``` 148 149 Annotation:Since the qemu-system-xtensa tool of qemu has the same name as the qemu-system-xtensa tool of esp32, the absolute path is used to execute the qemu-system-xtensa tool of esp32. 150 Annotation:The way to exit qemu : press ctrl and a, then release and press x. 151 152(Annotation:For more operating instructions, please refer to:[Home · espressif/qemu Wiki · GitHub](https://github.com/espressif/qemu/wiki#configure)) 153