Preparation
MDK development environment: MDK-ARM 5.24 (official or evaluation version, version 5.14 and above) needs to be installed. This version is also a relatively new version, which can provide relatively complete debugging functions. How to install can be referred to the Keil MDK Installation.
First acquaintance with RT-Thread
As an operating system, what is the code size of RT-Thread? Before we can figure this out, the first thing we need to do is to get an example of RT-Thread that corresponds to this manual. This example can be obtained from the following link:
This example is a zip file, unzip it. Here, we decompressed it to D:/. The directory structure after decompression is as shown below:
Descriptions of the file types contained in each directory are shown in the following table:
In the directory, there is project.uvprojx file, which is an MDK5 project file in the sample referenced in this manual.
Double-click "project.uvprojx" icon to open the project file:
Under the "Project" column on the left side of the main window of the project, you can see the file list of the project. These files are stored in the following groups, respectively:
Now let's click the button from the toolbar on the top the window, compiling the project as shown:
The result of the compilation is displayed in the "Build Output" bar at the bottom of the window. If nothing else, it will say "0 Error(s), * Warning(s)." on the last line, that is, there are no errors or warnings.
After compiling RT-Thread/STM32, we can simulate running RT-Thread through the MDK-ARM simulator. Clickat the top right of the window or directly hit Ctrl+F5 to enter the simulation interface and hit F5 to start, then click the button in the toolbar shown in the screenshot or select “View→Serial Windows→UART#1” in the menu bar to open the serial port 1 window. You can see that the output of the serial port only shows the LOGO of RT-Thread.
This is because the user code is empty and the result of its simulation is as shown:
We can output all the commands supported by the current system by inputting the Tab key or help + enter, as shown in the following figure.
User Entry Code
The above startup code is basically related to the RT-Thread system, so how do users add initialization code for their own applications? RT-Thread uses main function as the user code entry, all you need to do is just add your own code to the main function.
int main(void)
{
/* user app entry */
return 0;
}
Note: In order to complete the initialization for the system functions before entering the main program, you can use the $sub$$ and $super$$ function identifiers to call another sample before entering the main program, this was, users can ignore the initialization operations before the main() function. See ARM® Compiler v5.06 for µVision® armlink User Guide for details.
Example of a Marquee
For technical engineers working on electronics, marquee is probably the simplest example, it is like the first program Hello World in every programming language that programmers learned. So we will start with a marquee in the following example, to make it periodically update (turn on or off) the LED.
Under UART#1, input msh command: led and then click Enter to run it, as shown:
Example of a Marquee
/*
* Manifest of programs: Marquee sample
*
* marquee is probably the simplest example, it is like the first program
* Hello World in every programming language that programmers learned. So we will start with a marquee in the following example, start a thread to make it periodically
* update (turn on or off) the LED.
*/
int led(void)
{
rt_uint8_t count;
rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
for(count = 0 ; count < 10 ;count++)
{
rt_pin_write(LED_PIN, PIN_HIGH);
rt_kprintf("led on, count : %d\r\n", count);
rt_thread_mdelay(500);
rt_pin_write(LED_PIN, PIN_LOW);
rt_kprintf("led off\r\n");
rt_thread_mdelay(500);
}
return 0;
}
MSH_CMD_EXPORT(led, RT-Thread first led sample);
Other Examples
Additional kernel examples can be found in the kernel-sample-0.1.0 directory.
Frequently Asked Question
Compilation error occurred as follows:
rt-thread\src\kservice.c(823): error: #929: incorrect use of vaarg fieldwidth = aarg(args, int);
rt-thread\src\kservice.c(842): error: #929: incorrect use of vaarg precision = aarg(args, int);
………
Cause: This type of problem is usually caused by installation of ADS, when ADS and keil coexist, the header file of va_start points to the ADS folder.
Solution:
RT-Thread Contact Info
Previously published at https://hashnode.com/post/how-to-use-mdk-arm-to-simulate-a-hardware-module-that-runs-the-rt-thread-rtos-ck9c7mhud03sgcxs1yxrbyjr9