An Embedded Real-Time Control System with Independent Control of Hardware and Software

Click the blue text to follow us~~~

——/// Abstract \\——

☟☟☟

This article constructs an embedded real-time control system with independent control of hardware and software based on the domestic GD32 microcontroller and RT-Thread real-time operating system. It studies the transplantation method of the RT-Thread operating system on the GD32 chip development board and rewrites the driver program, as well as writes multitasking applications to test the system’s stable and reliable operation. This provides a technical implementation path for the development of domestic embedded control systems.

An Embedded Real-Time Control System with Independent Control of Hardware and Software

Introduction

Embedded real-time control systems composed of microcontrollers and real-time operating systems are widely used in many fields such as industrial production, transportation, energy supply, and national defense equipment, serving as the core hub for realizing automation technology. Currently, most of the embedded real-time control systems used domestically are based on foreign microcontroller chips and software products. After the “Prism Gate” [1] and “Stuxnet Virus” [2] incidents, the security issues of embedded systems used in key areas have been emphasized by domestic users, making the development of independently controllable and safe embedded real-time control systems an inevitable trend [3].

With the continuous development and increasing maturity of domestic microcontroller chips and real-time operating system software, it has become feasible to build an independently controllable embedded real-time control system based on domestic software and hardware products. This article uses the domestic microcontroller GD32F207 chip with an ARM Cortex-M3 core and the RT-Thread real-time operating system to establish a fully domestically produced embedded real-time control system, providing a technical experience summary and beneficial exploration for the establishment and application of domestically controllable embedded real-time control systems.

01

Composition of Embedded Real-Time Control Systems

Embedded systems and general-purpose computer systems have architectural similarities, both consisting of a main controller chip and peripheral function chips as the hardware circuit foundation, with software providing low-level hardware operation functions as the operating system. Users ultimately access and control the hardware through the operating system [4, 5]; embedded systems are usually designed for a specific control task, thus requiring real-time processing capability and reliability, while general-purpose computers have a wider range of processing applicability, focusing more on compatibility with weaker real-time processing capabilities.

1.1 GD32F207 Microcontroller and Development Board

The GD32F2 series of domestic microcontroller chips are designed based on the ARM Cortex-M3 core architecture. The GD32F207ZET6 chip used in this article has a maximum clock frequency of 120MHz, with 128KB of SRAM and 512KB of Flash memory capacity, and provides numerous peripheral function interfaces such as UART, ADC, Ethernet, and LCD controllers, greatly reducing the required peripheral functional circuit configuration. The Colibri-F207ZE development board is a “pocket laboratory” type product designed based on this chip, as shown in Figure 1. In addition to having IO interfaces compatible with Arduino platform pin layouts, it also includes TFT display interfaces, 2 user buttons, 3 LED indicators, 1 USB serial port, and an onboard GDLINK debugger function. This development board will be used as the hardware platform to construct the embedded control system.

An Embedded Real-Time Control System with Independent Control of Hardware and Software

Figure 1 Colibri-F207ZE Development Board

An Embedded Real-Time Control System with Independent Control of Hardware and Software

Figure 2 RT-Thread Operating System Hierarchical Structure

1.2 RT-Thread Real-Time Operating System

The development of the RT-Thread embedded real-time operating system began in 2006 and follows the GPLv2+ copyright license agreement. After years of extensive use in industries such as energy, instrumentation, and vehicles, RT-Thread has been verified as a stable and reliable embedded real-time operating system. From literature [6], a comparison of RT-Thread with internationally mainstream embedded real-time operating systems shows its superior performance. The structural hierarchy diagram of the RT-Thread system is shown in Figure 2, where the kernel layer (Kernel) is the key part of RT-Thread involving core operations such as clock management, real-time scheduling, and task switching; the component layer (Components) mainly provides additional operational functions such as Shell interaction, file systems, and protocol stacks; between the kernel layer and hardware is the software interface part (Porting) used for system transplantation, which implements various specific operational functions of the operating system on the hardware. In this article, the latest released RT-Thread version 2.1.0 is used as the transplantation object for analysis, code modification, and ultimately achieving the embedded real-time control system together with the Colibri-F207ZE development board.

02

Transplantation of RT-Thread on GD32 Microcontroller

The RT-Thread operating system has been hosted on GitHub, so the source code compressed file of RT-Thread version 2.1.0 can be directly downloaded from GitHub [7]. This article uses the MDK 5.16a version of the ARM development tool for transplantation work, and the DFP support package for GD32F2 series chips needs to be installed in the MDK Pack Installer. In the transplantation work, the serial communication and LED driving functions of the RT-Thread operating system have been completed, enabling Shell interaction between RT-Thread and the host and direct observation of running tasks in the test program.

2.1 Conversion of RT-Thread Directory Structure

Opening the downloaded RT-Thread source code file package reveals a directory structure categorized by code functionality, with each folder containing the following contents and purposes:

bsp folder: board support package, includes drivers for specific microcontroller models and external devices.

components: corresponds to the component layer of the RT-Thread operating system, containing most additional operational functions and hardware device driver models beyond the core functions of RT-Thread.

documentation: RT-Thread programming style description, providing guidance for developers and users to master RT-Thread.

examples: application and test program examples for the RT-Thread operating system.

include: header files used by the RT-Thread operating system, callable by external applications.

libcpu: CPU support package, containing low-level drivers for various common CPUs, mostly developed in assembly language.

src: corresponds to the kernel layer of the RT-Thread operating system, containing all core functions such as RT-Thread real-time scheduling, clock management, memory allocation, and thread management.

tools: Scons build script files, containing various script execution files for different development tools.

Based on the purposes of each folder in the RT-Thread source code file package, combined with the peripheral firmware library, CMSIS configuration files provided by the GD32 chip, and the board support package for the Colibri-F207ZE development board, a directory structure based on the GD32F207 chip and RT-Thread operating system is constructed, as shown in Figure 3. The App folder includes the startup file startup.c of RT-Thread and user-developed applications; the GD32F207ZET6 folder includes two subfolders related to hardware, where Colibri_BSP contains the peripheral driver programs for the development board, and GD32F20x_StdPeriph_Driver contains the peripheral firmware library provided by the GD32 chip, including chip drivers, configuration, and interrupt handling files; the Obj folder contains the project files created by MDK and stores the compiled axf files; the RT-Thread210 folder includes all software modules of the RT-Thread operating system, which are directly copied from the relevant content of the source code file package.

An Embedded Real-Time Control System with Independent Control of Hardware and Software

Figure 3 RT-Thread System Directory Structure Based on GD32 Chip

2.2 Establishment of Project Engineering and Driver Program Rewrite

After completing the construction of the above directory structure, the MDK development tool needs to be used to organically combine the RT-Thread source code and relevant files of the GD32 chip to achieve the transplantation of the RT-Thread operating system on the Colibri-F207ZE development board. Within the MDK tool, various files are grouped and managed according to logical relationships. Here, the groups starting with GD32 are related to embedded system hardware, and those starting with RT are related to operating system software, as shown in Figure 4. This grouping structure effectively achieves software-hardware isolation, facilitating future upgrades or replacements of hardware or software code.

2.2.1 Software and Hardware Configuration and Code Modification

In the configuration of RT-Thread and GD32, macro definitions are used to achieve conditional compilation of the required software functional modules and set chip performance parameters to meet the purpose of trimming and configuring the embedded system’s software and hardware. Therefore, selective settings of these macro definitions are required during the transplantation process.

Modify the system_GD32f20x.c file, which mainly implements the clock tree configuration of the system clock and the components connected to the AHB and APB buses. In this article, the system clock is set to 72MHz.

Modify the colibri_board.h file, which is mainly used to set the capacity of the internal and external RAM of the GD32 chip. According to the GD32F207ZET6 chip used in this article, the internal RAM capacity is set to 128KB; the development board does not expand external RAM, so the corresponding macro definition GD32_EXT_SRAM is set to 0.

Modify the rtconfig.h file, which is mainly used for parameter configuration of the RT-Thread operating system and trimming software functional modules. Users can set according to their needs; the more software functional modules retained, the larger the compiled HEX file size and the more RAM required for operation. In this article, the maximum priority of RT-Thread RT_THREAD_PRIORITY_MAX is set to 16; the number of ticks per second RT_TICK_PER_SECOND is set to 100; the console and Shell interaction functions RT_USING_CONSOLE and RT_USING_FINSH are enabled to facilitate application program debugging.

An Embedded Real-Time Control System with Independent Control of Hardware and Software

Figure 4 Group Management of GD32 Chip and RT-Thread System Files

2.2.2 Device Driver Program Rewrite

Writing driver programs is an important step in the transplantation process of the operating system, as it is used to realize the final operation of software on hardware. In RT-Thread, driver programs belong to the Porting layer and adopt a unified device driver model framework, which can be accessed and called by application programs through standard interface functions. Literature [6, 8, 9] describes the composition of the RT-Thread device driver framework. Since the GD32F2 series microcontrollers have architectural similarities with STM32 microcontrollers, this article implements the serial communication and LED driver program on the Colibri-F207ZE development board by rewriting the driver programs in the /bsp/stm32f10x/drivers subfolder of the RT-Thread source code file package. For other functional interface driver programs on the GD32 chip, this method can be referenced and rewritten as needed.

Taking the driver program rewrite for serial communication as an example, the application program controls the underlying I/O devices through the mapping relationship between standard interface functions in the I/O device module and functions in the device driver program, as shown in Figure 5. The standard interface functions do not require modification and are implemented by the serial.c file in the RT_DeviceDrivers group, while the serial driver program is implemented by the colibri_board_usart.c file in the GD32_BSP group, requiring modifications to the corresponding functional functions and configurations. The rewritten content mainly includes replacing the header files for accessing on-chip peripherals, implementing the chip pin definitions for serial communication functions, setting serial parameters in the rt_hw_usart_init() function, and registering the serial device in the RT-Thread device driver framework through the rt_hw_serial_register() function, thus establishing the association between the standard interface functions and the serial device. It should be noted that the structure definitions and function functionalities provided in the peripheral firmware libraries of GD32 and STM32 chips are similar but have different names, which need to be modified during the transplantation process. Table 1 lists the structure and function names that need to be modified in the serial driver program colibri_board_usart.c file. Thanks to the CMSIS interface standard followed by the Cortex core and the device driver framework mechanism adopted by RT-Thread, only a few modifications are needed to complete the transplantation work between different chips.

An Embedded Real-Time Control System with Independent Control of Hardware and Software

Figure 5 Mapping of Interface Functions for Application Programs Operating Underlying Devices

Table 1 Structure and Function Names That Need to Be Modified in the Serial Driver Program

Chip

STM32

GD32

Definition Location

Structure

USART_TypeDef

USART_InitPara

gd32f20x_usart.h

GPIO_InitTypeDef

GPIO_InitPara

gd32f20x_gpio.h

NVIC_InitTypeDef

NVIC_InitPara

gd32f20x_misc.h

Function

USART_Cmd

USART_Enable

gd32f20x_usart.h

USART_ITConfig

USART_INT_Set

USART_GetITStatus

USART_GetIntBitState

USART_ClearITPendingBit

USART_ClearIntBitState

USART_GetFlagStatus

USART_GetBitState

RCC_APB2PeriphClockCmd

RCC_APB2PeriphClock_Enable

gd32f20x_rcc.h

2.3 Writing Test Applications for RT-Thread

After powering on the development board, the GD32 chip resets, and the loading instruction at the reset address in the file startup_gd32f20x_cl.s calls the RT-Thread system main() function (located in the startup.c file). After completing the initialization of the development board, RT-Thread system kernel, timer, user tasks, and idle tasks, the RT-Thread scheduler is started through the rt_system_scheduler_start() function to achieve real-time scheduling of various user tasks. To verify whether the transplantation of RT-Thread on the Colibri-F207ZE development board is successful, two user tasks are established using the rt_thread_init() function to control two LED lights to flash at cycles of 0.5 seconds and 1 second respectively.

03

Testing of Embedded Real-Time Control System

After completing the transplantation of RT-Thread on the Colibri-F207ZE development board and writing the test program, the axf file obtained from MDK compilation is downloaded to the development board for execution. Through the serial communication tool of the upper computer, interaction with the Shell provided by RT-Thread can be achieved, and corresponding commands can be used to observe the operation of the RT-Thread system and the usage of RAM, as shown in Figure 6. It can be seen that a total of 4 tasks are running, where LED_GREE and LED_RED are user tasks controlling the LED lights to flash, while tshell and tidle are the system’s shell interaction task and idle task respectively; in RT-Thread, there is one registered device, which is the uart1 serial port providing Shell functionality. This test program indicates that RT-Thread has been stably running on the Colibri-F207ZE development board, achieving the expected transplantation effect.

An Embedded Real-Time Control System with Independent Control of Hardware and Software

Figure 6 RT-Thread System Shell Interaction Interface

An Embedded Real-Time Control System with Independent Control of Hardware and Software

Conclusion

This article constructs an embedded real-time control system based on the domestic GD32 microcontroller and RT-Thread real-time operating system, characterized by independent control of hardware and software, and safety and reliability, providing effective solutions for key areas of military and civilian applications such as industrial production and national defense equipment. Through the research and exploration in this article, technical verification has been accumulated in the construction of independently controllable embedded real-time control systems, laying a solid foundation for subsequent application development.

References

[1] Hou Yumei, Zhu Xiangdong. The Warning of the Prism Gate Incident to China’s Cybersecurity [J]. Computer Security, 2014, 05:33-35.

[2] Xiao Xinguang. A Glimpse into the Leopard – Analysis Fragments and Reflections on Stuxnet, Duqu, and Flame [J]. Information Security and Communication Confidentiality, 2012, 07:18-19.

[3] Wan Junwei, Zhao Hui, Bao Zhonggui, et al. Current Situation and Application Analysis of Independent and Controllable Information Technology Development [J]. Journal of Flight Control, 2015, 04:318-324.

[4] Wang Tingting. The Relationship Between General Computer Systems, Embedded Computer Systems, and Microcontrollers [J]. Journal of Tongren Normal University (Comprehensive Edition), 2005, 06:39-41.

[5] He Limin. From the Perspective of Modern Computers on Embedded Systems (3) – The Two Branches of Next-Generation Computers [J]. Microcontrollers and Embedded Systems Applications, 2016, 03:78-79.

[6] Tu Zhuan, Zhao Biao. Porting and Application of RT-Thread on LPC2378 [J]. Journal of Shanghai Ship Transportation Science Research Institute, 2013, 01:44-49.

[7] RT-Thread v2.1.0 released [EB/OL]. [2016-04-24] https://github.com/RT-Thread/rt-thread/releases/tag/v2.1.0.

[8] Zhu Chuanhong, Zhang Liquan. Porting of RT-Thread Embedded Real-Time Operating System on SEP4020 [J]. Computer and Digital Engineering, 2010, 11:93-96.

[9] Ye Sichao. Design of a Handheld High-Performance RFID Reader Based on RT-Thread [D]. Chengdu: University of Electronic Science and Technology of China, 2015.

Author Biography

Lü Huayi (Engineer), mainly engaged in the design of mechanical sensors, intelligent instruments, and measurement and control systems; Xie Zheng (Master’s Student), research direction is the design of measurement and control systems and embedded system development.

An Embedded Real-Time Control System with Independent Control of Hardware and Software

If you want to join the RT-Thread Nano communication group and communicate directly with the RT-Thread official team, please add WeChat 13924608367 and specify rt-thread to be added directly to the group.

An Embedded Real-Time Control System with Independent Control of Hardware and Software

END

There is a kind of love called likingAn Embedded Real-Time Control System with Independent Control of Hardware and SoftwareAn Embedded Real-Time Control System with Independent Control of Hardware and Software

Leave a Comment