1# Getting Started of QEMU (Ubuntu)
2
3The development of embedded software is inseparable from the development board. Without physical development boards, similar virtual machines like QEMU can be used to simulate the development board. QEMU is a virtual machine that supports cross-platform virtualization. It can virtualize many development boards. To facilitate the experience of RT-Thread without a development board, RT-Thread provides a board-level support package (BSP) for QEMU-simulated **ARM vexpress A9** development board.
4
5## 1 Install dependency libraries
6
7We need to type commands as following in the terminal:
8
9```shell
10sudo apt install gcc
11sudo apt install python3
12sudo apt install python3-pip
13sudo apt install gcc-arm-none-eabi
14sudo apt install gdb-arm-none-eabi
15sudo apt install binutils-arm-none-eabi
16sudo apt install scons
17sudo apt install libncurses5-dev
18sudo apt install qemu
19sudo apt install qemu-system-arm
20sudo apt install git
21```
22
23## 2 Get RT-Thread source code
24
25Download RT-Thread Source Code : `git clone https://github.com/RT-Thread/rt-thread.git`
26
27You can directly ignore the following steps, this is used for setting GCC compiler manually. Usually, you don't need to set this.
28
29> - Install the compiler. If the compiler version installed with `apt-get` command is too old, it will cause compilation errors. You can download and install the new version using the following command in turn. The download link and the decompression folder name will vary according to the download version. The following Compression Packet will unzip to the `/opt` folder.
30>
31>   - `wget https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-rm/6-2016q4/gcc-arm-none-eabi-6_2-2016q4-20161216-linux.tar.bz2`
32>   - `cd /opt`
33>   - `sudo tar xf ~/gcc-arm-none-eabi-6_2-2016q4-20161216-linux.tar.bz2`
34>
35>
36> - After the compiler is installed, it is necessary to modify the `rtconfig.py` file under `rt-thread/bsp/qemu-vexpress-a9` BSP, modify the corresponding path to the bin directory corresponding to the compiler decompressed into the opt directory. Referring to the following figure, the directory name varies according to the downloaded version of the compiler:
37>
38> ![edit EXEC_PATH in rtconfig.py](figures/ubuntu-rtconfig-py.png)
39>
40
41
42
43## 3 Build QEMU Project
44
45### 3.1 Move into QEMU folder
46
47```
48cd rt-thread/bsp/qemu-vexpress-a9/
49```
50
51### 3.2 Configure the environment of Env tool
52
53#### 3.2.1 Remap python command
54
55We need to remap `python` command as python3 by default.
56
57Using `whereis` command to identify your python3's version:
58
59![python3-version](figures/python3-version.png)
60
61For instance, as you can see, in my computer, the python3's version is python 3.9. You need to identify python3's version in your computer. Then, we remap the `python` command as python3 by default:
62
63```shell
64sudo rm -rf /usr/bin/python3
65sudo rm -rf /usr/bin/python
66sudo ln -s /usr/bin/python3.9 /usr/bin/python3
67sudo ln -s /usr/bin/python3.9 /usr/bin/python
68```
69
70### 3.3 Install Env and Configure BSP
71
72Type following the command under  `bsp/qemu-vexpress-a9` folder:
73
74```
75scons --menuconfig
76```
77
78The Env tool will be installed and initialized after using the `scons --menuconfig` command. Then it will enter the configuration interface, and you could configure the BSP:
79
80![install env tool](figures/ubuntu-menuconfig.png)
81
82![enter the configuration interface](figures/ubuntu-env-menu.png)
83
84You can use the keyboard `↑` key and `↓` key to look up and down menu items, use the `Enter` key to enter the selected directory, use the `Space` key to select or cancel bool variables, and press `Esc Esc` to exit the current directory.
85
86> Notice: Please make sure that the terminal size is larger than 80x24 character size.
87
88### 3.4 Configure the QEMU BSP and acquisition of software packages
89
90```
91source ~/.env/env.sh
92scons --menuconfig
93pkgs --update
94```
95
96The `env.sh` file is a file that needs to be executed. It configures the environment variables so that we can update the package with the pkgs command and execute it with the `source ~/.env/env.sh` command.
97
98Then use `scons --menuconfig` command to enter menuconfig, and you could select the online packages by this time.
99
100![commands of acquisition of pkgs](figures/ubuntu-pkg-menuconfig.png)
101
102![add pkg menu](figures/ubuntu-pkgs-add-to-menu.png)
103
104For example, select the kernel sample package: semphore sample.
105
106![select a package](figures/ubuntu-select-pkg.png)
107
108Exit and save the configuration.
109
110![save the configuration](figures/ubuntu-save.png)
111
112If you have selected an online package, you can download the package to the packages folder in the BSP directory using the `pkgs --update` command (Git needs to be installed):
113
114![download the package](figures/ubuntu-update-pkg.png)
115
116#### 4.1 Tips
117
118Before you use the `pkgs` command, you need to type command `source ~/.env/env.sh`. This is a annoying work. We can attach this command as a new line at the end of `~/.bashrc`, which can let you to to use `pkgs` command directly.
119
120### 3.5 Compile the QEMU project
121
122```
123scons
124```
125
126Using the `scons` command to compile the BSP.
127
128![compile the BSP](figures/ubuntu-scons.png)
129
130## 4 Introduction of QEMU BSP Catalogue
131
132The board-level support package (BSP) provided by RT-Thread simulates ARM vexpress A9 development board is located in the `qemu-vexpress-a9` folder under the `bsp` directory of RT-Thread source code. This BSP implements LCD, keyboard, mouse, SD card, Ethernet card, serial port and other related drivers. The contents of the folder are shown in the following figure.
133
134![qemu-vexpress-a9 folder](figures/ubuntu-qemu-bsp.png)
135
136The main files and directories of `qemu-vexpress-a9` BSP are described as follows:
137
138| Fles/Directories | Description                                 |
139| ---------------- | ------------------------------------------- |
140| applications     | User application code directory             |
141| drivers          | The underlying driver provided by RT-Thread |
142| qemu.bat         | Script files running on Windows platform    |
143| qemu.sh          | Script files running on Linux platform      |
144| qemu-dbg.bat     | Debugging script files on Windows platform  |
145| qemu-dbg.sh      | Debugging script files on Linux platform    |
146| README.md        | Description document of BSP                 |
147| rtconfig.h       | A header file of BSP                        |
148
149## 5 Compile and Run
150
151### 5.1 Use the *scons* Command to Compile the Project
152
153Switch to the QEMU BSP directory and enter the `scons` command to compile the project. If the compilation is correct, the `rtthread.elf`  file will be generated in the BSP directory, which is a target file required for QEMU to run.
154
155![compile the project](figures/ubuntu-scons.png)
156
157### 5.2 Use the *./qemu.sh* Command to Run the Project
158
159After compiling, type `./qemu.sh` to start the virtual machine and BSP project. `qemu.sh` is a Linux batch file. This file is located in the BSP folder, mainly including the execution instructions of QEMU. The first run of the project will create a blank `sd.bin` file under the BSP folder, which is a virtual SD card with a size of 64M. The Env command interface displays the initialization information and version number information printed during the start-up of RT-Thread system, and the QEMU virtual machine is also running. As shown in the following picture:
160
161![run the project](figures/ubuntu-qume-sh.png)
162
163### 5.3 Run the Finsh Console
164
165RT-Thread supports Finsh, and users can use command operations in command line mode.
166
167Type `help` or press `Tab` to view all supported commands. As shown in the figure below, commands are on the left and command descriptions are on the right.
168
169![view all supported commands](figures/ubuntu-msh-help.png)
170
171For example, by entering the `list_thread` command, you can see the currently running threads, thread status and stack size; by entering the `list_timer`, you can see the status of the timers.
172
173![threads and timers](figures/ubuntu-thread-timer.png)
174
175### 5.4 Run the File System
176
177Type `list_device` to view all devices registered in the system. You can see the virtual SD card "sd0" device as shown in the following picture. Next, we can format the SD card using the `mkfs sd0` command, which will format the SD card into a FatFS file system. FatFs is a Microsoft fat-compatible file system developed for small embedded devices. It is written in ANSI C, uses abstract hardware I/O layer and provides continuous maintenance, so it has good portability.
178
179> For more information on FatFS, click on the link: [http://elm-chan.org/fsw/ff/00index_e.html](http://elm-chan.org/fsw/ff/00index_e.html)
180
181![format the SD card](figures/ubuntu-mkfs-sd0.png)
182
183The file system will not be loaded immediately after the first formatting of the SD card, and the file system will be loaded correctly only after the second boot. So exit the virtual machine, and then restart the virtual machine and project by entering `./qemu.sh` on the command line interface. Entering `ls` command, you can see that the `Directory` directory has been added, the file system has been loaded, and then you can experience the file system with other commands provided by RT-Thread:
184
185![commands of file system](figures/ubuntu-filesys.png)
186
187- ls: Display the file and directory information
188- cd: Switch to the specified directory
189- rm: Delete files or directories
190- echo: Writes the specified content to the target file
191- cat: Displays the details of a file
192- mkdir: Create folders
193
194Please enter `help` to see more commands.
195
196## 6 More Functions
197
198You can configure more functions in the menuconfig's configuration interface. use `scons --menuconfig` to config the BSP. After the configuration is completed, save the configuration first, and then exit the configuration interface, then:
199
2001. If you choose a package, you need to use the command `pkgs --update` to download the package.
2012. Compile with `scons`.
2023. Then enter `./qemu.sh` to run QEMU.
2034. Use `help` to view all commands of the BSP. And then use the commands.
204