Why do spacecraft and missiles prefer to use microcontrollers instead of embedded systems?


Brother Tao's 029th original

I. Introduction

A few days ago, I chatted with a post in a certain research institute. He said: In the current weapons and equipment such as aviation, aerospace and missiles, almost all control systems use single-chip microcomputers instead of embedded systems .

At first glance, it contradicts our intuition: The control logic of such a tall device must be very complicated. How can such a complicated function control be accomplished without an embedded system? Then I took a closer look and realized that the answer is: safe + controllable .

In this article, let's talk about those things between MCU and embedded, operating system and RTOS ! Through this article, let you have a systematic and comprehensive understanding of the real-time nature of your operating system!

2. About the definition between single-chip microcomputer and embedded system

To be honest, no one can give a standard and correct answer regarding the distinction between the two . Everyone understands microcontrollers and embedded systems slightly differently.

Regardless of the hardware, from the perspective of application development, I understand it like this:

Single-chip microcomputer : You can directly use the state machine to implement the program framework, or you can use some RTOS (ucOS, FreeRTOS, vxWorks, RT-Thread), etc. to complete some scheduling functions.

Embedded system : Use embedded Linux operating system and some variants to write application programs.

I know that my understanding may be wrong, at least not rigorous and narrow in scope. Since there is no standard answer, let’s just quote the definition in Wikipedia. After all, the concept is dead, and more importantly, how we do it according to actual needs. Choose .

1. Microcontroller

  1. Single-chip microcomputer, full name single-chip microcomputer (single-chip microcomputer), also known as microcontroller unit MCU (microcontroller unit).
  2. A microcomputer that integrates the central processing unit, memory, timer/counter, various input and output interfaces, etc. on an integrated circuit chip.
  3. Because of its rapid development, the definition of the old single-chip microcomputer can no longer be satisfied, so it is called a wider range of microcontrollers in many applications;

2. Embedded System

  1. Embedded System (Embedded System) is a computer system embedded in a mechanical or electrical system with specific functions and real-time computing performance.
  2. Embedded systems are often used to efficiently control many common devices. The embedded system is usually a complete device containing digital hardware and mechanical components, such as the anti-lock braking system of a car.
  3. Modern embedded systems are usually based on microcontrollers (such as central processing units with integrated memory and/or peripheral interfaces), but in more complex systems, ordinary microprocessors (using external memory chips and peripheral interface circuits) It is also very common.

3. Embedded Linux

  1. Embedded Linux (English: Embedded Linux) is a general term for a type of embedded operating system. This type of operating system is based on the Linux kernel and is designed to be used in embedded devices.
  2. It is essentially the same as the Linux system running on the computer. Although it has undergone some functional tailoring, it is essentially the same. It mainly uses the task scheduling, memory management, hardware abstraction and other functions in the Linux kernel.

4. RTOS

  1. Real-time operating system (RTOS), also known as real-time operating system, runs and manages system resources in a sequence, and provides a consistent basis for developing applications.
  2. Compared with general operating systems, the biggest feature of real-time operating systems is "real-time". If there is a task to be executed, the real-time operating system will execute the task immediately (in a short time) without a long delay. Time. This feature ensures the timely execution of various tasks.

Three, non-real-time, soft real-time, hard real-time

First of all, we must understand what is real-time ? Real-time consideration is not speed, performance, throughput, but determinism , that is to say: when an event occurs, how long it can be guaranteed to be processed deterministically, as long as it can meet this requirement, it can become Hard real time. such as:

Operating system 1: When an interrupt occurs, it can be guaranteed to get here within 1 second, then it is a hard real-time system. Although the response time is long, it is definite;
operating system 2: When an interrupt occurs, it can almost always be 1 If it is completed within milliseconds, then it cannot become a hard real system. Although the response time is short, it is uncertain.

I have also seen an article saying: The ambiguous statement of soft real-time should be cancelled , either real-time or non-real-time!

The operating system contains many functions: task scheduling, memory management, file management, etc. The core of which is task scheduling , which is also the biggest difference between non-real-time, soft real-time, and hard real-time.

That measure real-time performance indicators is:

1. Interrupt delay : When an interrupt caused by an external event occurs, the time elapsed until the first instruction of the corresponding interrupt handler is executed;
2. Task preemption delay : When a high-priority task is ready Time, the elapsed time to grab CPU resources from the low-priority tasks being executed;

Different operating systems have different task scheduling mechanisms , and the strategy of this scheduling mechanism is related to actual usage scenarios . Therefore, there is no such thing as which is good and which is not good. The right one is the best !

For example, in our desktop system, we need to consider multitasking and concurrency , and we need to execute multiple programs at the same time. The user does not care which program is slower, or even notices it; but for a missile control system , when an external sensor inputs a signal, When an event is triggered, the corresponding processing must be executed immediately , otherwise there will be a delay of 1 millisecond, and the result may be a thousand miles away!

Fourth, the scheduling strategy of x86 Linux system

The main goal of the PC we use every day is to execute multiple tasks in parallel, and the emphasis is on throughput (execute as much application code as possible). Therefore, it uses a time-sharing operating system , that is, each task There is a time slice . When the time slice allocated by a task is used up, it is automatically swapped out (scheduling), and then the next task is executed.

When we usually write ordinary client programs on the x86 platform, we rarely need to specify the scheduling strategy and priority of the application , and we use the default scheduling mechanism of the system. Conversely speaking, that is, in certain situations where it is needed, it is possible to set the scheduling strategy and priority of the process.

For example, in a Linux system, three scheduling strategies can be set through system functions : sched_setscheduler()

  1. SCHED_OTHER: The default scheduling strategy of the system, which calculates the dynamic priority (counter+20-nice), and puts it at the end of the ready queue when the time slice is used up;
  2. SCHED_FIFO: Real-time scheduling strategy, scheduling according to priority, once the CPU is occupied, it will be executed until it gives up execution or has a higher priority task to be executed;
  3. SCHED_RR: It is also a real-time scheduling strategy, adding a time slice on the basis of SCHED_FIFO. During execution, it can be interrupted by higher-priority tasks. If there is no higher-priority task, then when the execution time slice of the task is exhausted, it will look for tasks of the same priority to execute.

1. Why is the Linux system soft real-time?

Some friends may have questions: Since the SCHED_FIFO priority-based scheduling strategy is provided in the Linux system , why can't it be called a real hard real-time operating system? This starts with the development history of Linux.

At the beginning of the design, the Linux operating system was developed for desktop applications. In that era, multiple terminals (teletypewriters and screens) were connected to the same computer host, which needed to deal with multitasking and parallel operations . Consider real-time performance. Therefore, some genes in the Linux kernel seriously affect its real-time performance. For example, there are several factors as follows:

(1) The kernel cannot be preempted

We know that when an application is executed, it can be executed in user mode and kernel mode (when a system function is called, for example: write, it will enter the kernel mode for execution). At this time, the task is not preemptible .

Even if a higher priority task is ready, the current task can not be stopped immediately. Instead, you must wait until the current task returns to the user mode , or when you need to wait for a resource in the kernel mode to sleep , the high-priority task can be executed.

Therefore, it is obvious that the real-time performance of high-priority tasks cannot be guaranteed.

(2) Spin lock

A spin lock is a lock used for multi-thread synchronization . It is a synchronization mechanism for shared resources. The thread repeatedly checks whether the lock variable is available. Because the thread keeps executing in this process, it is a busy wait. Once the spin lock is acquired, the thread will keep the lock until the spin lock is explicitly released.

The spin lock avoids the scheduling overhead of the process context, so it is effective for occasions where the thread will only block for a short time , that is, it is only suitable to use the spin lock when the thread is blocked for a short time.

However, during the spin lock period, task preemption will fail . That is to say, even if the blocking time of the spin lock is very short, this will still increase the task preemption delay and make scheduling uncertain .

(3) The priority of the interrupt is the highest

At any time, as long as an interrupt occurs, the interrupt service routine will be executed immediately, that is, the interrupt priority is the highest . Only when all external interrupts and soft terminals are processed can normal tasks be executed.

This seems like a good thing, but think about it, what if there are tasks with higher priority than interrupts? If the system is running and the network port continues to receive data, the interrupt will always be executed , and then other tasks may not have the opportunity to execute . This is a huge challenge that affects the real-time performance of the Linux system.

(4) Turn off the interrupt during synchronous operation

If you look at the code of the Linux kernel, you can see that the interrupt instruction is executed in many places . If an interrupt occurs during this period, the interrupt response time cannot be guaranteed.

2. How to change the Linux system to hard real-time?

The several factors described above pose great obstacles to the realization of real real -time performance of Linux , but there are indeed many occasions in the real world that require Linux to have hard real-time, so solutions must be proposed for each of the above factors .

There are currently 2 mainstream solutions:

  1. Single-kernel solution: Patch the Linux kernel to solve the problems mentioned above, for example: RT-Preempt;
  2. Dual-core solution: above the hardware abstraction layer, run two cores: real-time kernel + Linux kernel, which provide API functions to the upper layer, for example: Xenomai;

These two solutions have different implementations. According to the research situation, RT-Preempt and Xenomai are used more. Let's take a look at their advantages and disadvantages.

(1)RT-Preempt

This method is mainly to patch the Linux kernel , which solves the problems mentioned above: the kernel cannot be preempted, spin locks, shutdown interrupts, and terminal priority issues.

As for how each problem is solved, I won’t introduce it here due to space. Interested friends can learn more about it if necessary.

Because it is directly patched on the Linux kernel (it will definitely be merged into the main branch in the future), for application development, the API interface functions provided by the operating system to the upper layer can remain unchanged , which is important for application development Is a good thing.

(2) Xenomai

Xenomai is a real-time development framework of the Linux kernel. It hopes to provide user-space applications with comprehensive, interface-independent hard real-time performance by seamlessly integrating into the Linux environment. The following is the architecture diagram of Xenomai:

Above the hardware abstraction layer, there are two parallel domains (kernels) , which respectively provide their own API interface functions to the upper layer .

In the figure, glibc is a library function provided by the Linux system, and applications write programs by calling library functions and system calls.

Xenomai also provides the corresponding library function libcobalt . This library function needs to be compiled and installed at the user level, just like installing a third-party library.

In addition, Xenomai also refers to different operating system styles and provides several sets of API functions (previously: skins). The API interface functions are here :

As you can see from the figure, the Alchemy API interface provides more complete functions, providing: timers, memory management, condition variables, events, mutex locks, message queues, tasks (can be understood as threads) and other API functions.
The specific functions in this set of API functions are roughly the same as the POSIX standard, and there are some differences in some details.

Since Xenomai provides an independent set of API functions to the application layer , if we need to create real-time tasks, we must call this set of interface functions to create tasks, including using some of the resources (for example: memory allocation). And the document also puts forward some points of attention, for example: some resources cannot be mixed between Xenomai and Linux systems.

5. Advantages of RTOS

As mentioned above, the main goal of the Linux desktop system is throughput , executing more code per unit of time.

But for the microcontroller, the primary goal is not throughput, but certainty . Therefore, an important indicator of the robustness of a real-time operating system is the time required for the system to complete the task from receiving a task. In other words, task scheduling is the first consideration .

In the development of single-chip microcomputers, there are generally two programming models: based on state machine (naked running) and based on RTOS .

If it is based on a state machine, there is no task scheduling problem, because there is only one execution sequence, all operations are executed serially , and the only control flow that needs attention is interrupt processing .

If it is based on RTOS, the main use is task scheduling to achieve true hard real-time . Best of this area is VxWorks, of course, the price is very impressive, some companies after the purchase, in addition to even put the task scheduling module modules other than rewrite it all, which is enough to prove that indeed the VxWorks task scheduling process Very powerful, this is also its housekeeping skills!

Of course, for key programs that are simple and require strict control of the execution sequence, using the programming framework of the finite state machine , everything is in your own control. As long as there are no bugs in the code, then in theory, all behaviors are under control, which is why many military equipment use single-chip microcomputers!

Six, summary

The issue of task scheduling is the top priority of an operating system, and there are still many things to learn. Recently, I just bought a new book by Mr. Chen Haibo, who is the soul behind Huawei's Hongmeng system.

If you have a new learning experience, share it with everyone.


references:

https://linuxfoundation.org/blog/intro-to-real-time-linux-for-embedded-developers/
https://wiki.archlinux.org/index.php/Realtime_kernel_patchset
http://www.faqs.org/faqs/realtime-computing/faq/
https://xenomai.org/documentation/xenomai-3/html/README.INSTALL/


Good articles should be forwarded; the more you share, the luckier you are!


Recommended reading

[C language]

C language pointer-from the underlying principles to fancy skills, use graphics and code to help you explain
the underlying debugging principles of the original gdb so simple
step-by-step analysis-how to use C to implement object-oriented programming to
improve the code's powerful tool: macro definition- From entry to giving up,
use setjmp and longjmp in the C language to implement exception capture and coroutines

[Application Programming] It is
said that the software architecture should be layered and divided into modules, and what should be done specifically (1) It is
said that the software architecture should be layered and divided into modules, and what should be done specifically (2)
IoT gateway development: based on MQTT message bus Design process (on)
IoT gateway development: design process based on MQTT message bus (below)
my favorite way of communication between processes-message bus

【Internet of Things】

The things about encryption and certificates
go deep into the LUA scripting language, allowing you to fully understand the principle of debugging

[Nonsense] Based
on my failed career experience: a few tips for technicians who are new to the workplace

Guess you like

Origin blog.csdn.net/u012296253/article/details/114881275