RT-Thread recording (1. RT-Thread version, RT-Thread Studio development environment and quick start with CubeMX development)

距离完成 FreeRTOS 的记录也有一段时间了,除了最近在测试的ESP32-C3 ,也该记录一下 RT-Thread。
期间几个月时间关于 RT-Thread 的项目有好几个,感觉最近 RT-Thread 越来越火,
在几年前用过 RT-Thread,那个时候都还没有RT-Thread Studio IDE,现在啥都有了。
RT-Thread官方资料也多,理论,示例,说明应有尽有,自己重新学习一边,做个记录贴。

foreword

As a domestic operating system, compared to other operating systems, the official website is extremely friendly to most engineers = =!

What information can be directly viewed on the official website. So if you have any questions, go to the official website first! Below are some commonly used links, which are actually available on the official website for easy reference.

RT-Thread Documentation Center

RT-Thread Official Forum

RT-Thread Reference Design Center

RT-Thread Official Video Center

RT-Thread Source Code Download Center

RT-Thread package

1. RT-Thread version (which version to start learning with)

At present, RT-Thread is mainly divided into 3 major versions, which are available in the document center. Here is a brief description according to your own situation:

1.1 RT-Thread Nano

RT-Thread Nano and FreeRTOS are similar things. One kernel, Nano is suitable for some MCUs with small memory space, such as the STM32L051C8 that I commonly use now, which only has 8KB of RAM space. When explaining FreeRTOS, I mentioned the use of STM32L051C8 more than once. to memory problems.

If you want to learn according to your own needs, it is recommended to start learning from the standard version, because the standard version includes everything of Nano, as well as many more components and easy-to-use software packages.

In practical applications, Nano is estimated to be used more in ordinary application projects, and memory is the cost!

The information of RT-Thread Nano can be viewed in the official documentation center, just remember one thing, it is a minimalist version of the hard real-time kernel.
insert image description here

1.2 RT-Thread Standard Edition

The standard version of RT-Thread includes Nano, and the standard version comes with rich component software packages, which is one of the main differences between it and FreeRTOS. In some applications, such as MQTT, cloud access, etc., RT-Thread has ready-made The software package is simple and convenient to use.
If you are learning, it is recommended to start with the standard version. Although in some cases, the rich components and software packages may make it difficult for beginners to understand how components and software packages are related to the hardware, but only after you really learn and understand. You will find its convenience, and at the same time understand the principle, you can also tailor and increase the components according to your own application, which can better meet the needs of various projects.

The introduction is still on the official website. The introduction is taken from the official website:
One of the main differences between RT-Thread and many other RTOSs such as FreeRTOS and uC/OS is that it is not only a real-time kernel, but also has a wealth of middle-layer components:
insert image description here

Even if the RT-Thread standard version is selected as the learning version, there are many versions as far as the standard version is concerned:
insert image description here
the specific version and how to choose can refer to the official instructions, the link is as follows:

How to choose a suitable RT-Thread version for development?

1.3 RT-Thread Smart

RT-Thread Smart didn't exist a few years ago. The most recent one is for chips with MMU, which used to be able to run Linux systems.
For example, in the past two years, there have been two series of chips, STM32MP1 and IMX6ULL, which have been popular in development boards. Of course, the developed development boards were mainly based on Linux systems at first, and now RT-Thread can also run on these chips.
This version is not what the blog post in this column needs to record. If you have the opportunity to make some records after writing embedded Linux later.

The introduction is taken from the official website:
RT-Thread Smart (referred to as rt-smart) is a new branch based on the RT-Thread operating system, targeting chips with MMU and mid-to-high-end applications, such as ARM Cortex-A series chips, MIPS chips, with MMU RISC-V chip, etc. rt-smart enables independent, complete process mode based on RT-Thread operating system, while executing in hybrid microkernel mode.

2. RT-Thread development environment

Through the above introduction, we will use the standard version to record the RT-Thread. The board uses the STM32F103VGT6 that I drew before. At that time, the price of the chip has not increased = =! The picture can refer to my previous blog post:

Starting from RTOS (preparation and ideas for RTOS application learning)

2.1 Development tools

I still remember that when I learned RT-Thread before, RT-Thread Studio IDE has not yet been released. The overall project construction needs to use tools such as ENV, SCons, etc. Whether it is MDK, IRA, GCC tool chain development, you must first use ENV and Scons tool, configure and operate once, for beginners, it is very unfriendly, the detailed introduction can be inquired on the official website:

Official development tools introduction page
insert image description here

The initial study was based on the official documentation, manuals, videos, etc., and I used it myself, but after a period of no use, the commands were almost forgotten.
Well now, RT-Thread official team has launched RT-Thread Studio. RT-Thread Studio has integrated these previous development tools, so there is no need to memorize the previous commands, and the project construction of interface graphics is more simple and intuitive than before.
As a domestic IDE, the interface operation is quite friendly. Our tutorials in this series are for the purpose of projects. In order to be able to use RT-Thread to complete the development of a complete project, we will not spend too much energy tossing the engineering structure. things like structuring, so using RT-Thread Studio is perfect.

2.2 Environment Construction

According to the above introduction, we decided to use RT-Thread Studio as the development IDE for this series of blog posts. The installation and download can be found directly in the official website. Please refer to the following documents:

Official RT-Thread Studio Instructions

Install it according to the manual:
insert image description here

After the installation is complete, you need to log in to the RT-Thread account to run the software:
insert image description here

According to the official documentation, the first step is to install and update the SDK package:
insert image description here

On my computer, I have built an environment before. I feel that RT-Thread Studio will automatically recognize the environment variables in the computer to find the environment that already exists on the computer, such as the above version, and the GCC toolchain version I use. :
insert image description here
After installing the SDK package, you can basically start building the project test.

Once again, RT-Thread Studio user manual, interface configuration and other operations, official documents, videos, all have detailed instructions. Recorded here are the necessary steps for your own use.

2.3 RT-Thread Studio For Vscode

(To be updated... First, use the IDE to become proficient, and then play with the plug-in in Vscode. After all, the IDE configuration is relatively simple)

3. The first project

3.1 Create a new project according to your own environment

After the above operation is over, we will directly create a new project. Before creating a new project:

Once again, explain the environment used in this article:

  1. Using RT-Thread Standard Edition
  2. The development board drawn by myself, based on STM32F103VGT6

So we make the following choices in the interface of the new project in RT-Thread Studio:
insert image description here

Click Finish and wait for the project to be automatically generated, because serial port 1 is already configured by default when creating a new project, then compile it directly, and burn it:
insert image description here

Connect to the development board through the serial port assistant, the default baud rate is 115200:
insert image description here

You can see that RT-Thread is running normally on the board:

insert image description here

3.2 Use with CubeMX configuration

The above test results seen through the terminal only use the serial port function. As for the start marquee that has remained unchanged for thousands of years, we have not seen it, so how can it be reflected in the hardware? There are also corresponding in the official documents. The introduction of:
insert image description here

However, there are not too many official instructions in this part. We still need to test and explain here. The test results are smooth in the front, and there are some small problems in the subsequent operations, which need to be paid attention to! ! !

Click on CubeMx Setting under the project directory:
insert image description here

After opening it, pay attention to how to set the clock, serial port, IO port, etc. when using CubeMx before. Although we can already pass the clock and serial port when the project is established, but after opening this setting, we have to reset all settings. again!

If the STM32CubMX project will not be set, you can refer to the blog post:
STM32L051 test (1. Use CubeMX to generate project files - common to ST series chips)

According to the steps, we configure the basic clock , debugging mode, serial port, LED, and buttons for a preliminary simple test.
insert image description here
Check the Code Generator column before producing the code:
insert image description here
then click Generate Project.

After the project is generated, it does not need to be opened, but CubeMX must be closed, so that the following operations can be performed normally on RT-Thread Studio!

After the project is generated, closing CubeMX will prompt that cubemx.ioc has been modified. If you want to save the configuration, please click Yes.
insert image description here

  • The original hal_conf.h is replaced by the newly generated cubeMX

  • RT-Thread Studio will and only use the contents of the Inc and Src folders generated by CubeMX

  • The main function RT-Thread Studio will be declared with _WEAK before the main function generated by CubeMX

  • Clock initialization function, will use the settings in CubeMx
    insert image description here

  • Some basic builds are automatically set up by the current version of RT-Thread Studio and passed SConscript.insert image description here

The project can be compiled directly.

Our main function is in the main.c file under applications, so to use the configuration generated by CubeMX, we need to include the corresponding header file in the main.c file under applications, and call initialization in the function main.

But I didn't expect to encounter a problem here:
insert image description here
I looked carefully and found that it was impossible to find the implementation of these two initializations. Looking at the engineering architecture, there were indeed no two .c files. After researching for a long time, I still found a way. Modify the SConscriptfile, similar to Makefile:
insert image description here

After rebuilding cubemx -> Src -> there are more corresponding .c files below, and the above project can be compiled normally!

Additional Notes:

Through the Makefile, you can directly see how many .c files our CubeMX has generated:
insert image description here
In addition to what RT-Thread Studio added to us by default, we also added GPIO and serial port driver files, and one it is related to interrupts. We didn't add it! !

So is it impossible to respond to the interrupt?

Also, if the GPIO and serial port driver files generated by CubeMX are not added, and the initialization in CubeMX is not used, will GPIO be unavailable?

These problems will not be explained for the time being. The RT-Thread kernel itself has board-level drivers, because this is some knowledge of the RT-Thread kernel driver that needs to be introduced in the following articles.

3.3 Create a marquee task

After completing the above configuration, we can start to walk with the marquee. Since RTOS is used, of course, we can complete our quick start by creating an LED marquee task.

We statically create a thread in the program, led1_blinkstart scheduling and start running when power on; to
dynamically create a thread led2_blink, we need to start scheduling through commands. The program source code is as follows:

/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-02-16     QZH    	first lesson for test
 */

#include <rtthread.h>
#include "main.h"
#include "usart.h"
#include "gpio.h"

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

static struct rt_thread led1_thread;    //led1线程
static char led1_thread_stack[256];

static rt_thread_t led2_thread = RT_NULL; //led2线程

static void led1_thread_entry(void *par){
    
    
    while(1){
    
    
        LED1_ON;
        rt_thread_mdelay(1000);
        LED1_OFF;
        rt_thread_mdelay(1000);
    }
}

static void led2_thread_entry(void *par){
    
    
    while(1){
    
    
        LED2_ON;
        rt_thread_mdelay(500);
        LED2_OFF;
        rt_thread_mdelay(500);
    }
}

int main(void)
{
    
    
    MX_GPIO_Init();
    MX_USART1_UART_Init();
    int count = 1;

    rt_err_t rst2;
    rst2 = rt_thread_init(&led1_thread,
                        "led1_blink ",
                        led1_thread_entry,
                        RT_NULL,
                        &led1_thread_stack[0],
                        sizeof(led1_thread_stack),
                        RT_THREAD_PRIORITY_MAX -1,
                        50);

    if(rst2 == RT_EOK){
    
    
        rt_thread_startup(&led1_thread);
    }

    while (count++)
    {
    
    
        if(count < 11){
    
    
            LOG_D("Hello RT-Thread!%d",count);
        }
        if(count >= 0XFFFFFF00) count = 0;
        rt_thread_mdelay(1000);
    }

    return RT_EOK;
}

void led2_Blink(){
    
    
    led2_thread = rt_thread_create("led2_blink",
                            led2_thread_entry,
                            RT_NULL,
                            256,
                            RT_THREAD_PRIORITY_MAX -1,
                            50);

    /* 如果获得线程控制块,启动这个线程 */
    if (led2_thread != RT_NULL)
        rt_thread_startup(led2_thread);
}

MSH_CMD_EXPORT(led2_Blink, Led2 sample);

The results of the above code test are as follows:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42328389/article/details/122881902