当前位置:网站首页>RTOS RT thread bare metal system and multi thread system
RTOS RT thread bare metal system and multi thread system
2022-06-12 16:12:00 【1ShyJn25】
Bare metal systems and multithreaded systems
Bare metal system
Bare metal systems are usually divided into polling systems and front and back systems
polling systems
The polling system is in the process of bare metal programming , First initialize the relevant hardware , Then let the main program circulate in an endless loop , Do all kinds of things in sequence , See the code list for the pseudo code 2-1. Polling system is a very simple software structure , Generally, it is only applicable to those operations that only need to execute code in sequence and do not need external events to drive . In the code list 2-1 in , If you just realize LED Flip 、 Serial output 、 LCD and other operations , Then the polling system will be perfect . however , If the key operation and other events that need to detect external signals are added , Or to simulate an emergency alarm , Then the real-time response ability of the whole system will not be so good . hypothesis DoSomething3 It is a key scanning operation , When the external key is pressed , It is equivalent to generating an alarm , This is the time , Need immediate response , And make emergency treatment , At this time, the program just runs to DoSomething1, also DoSomething1 The execution time will be long , It's too long to finish the execution after the key is released , Then when the DoSomething3 An event will be lost . It can be seen that the polling system is only suitable for the function code of sequential execution , When driven by external events , Real time will be reduced .
Polling system pseudo code
int main(void)
{
/* Hardware related initialization */
HardWareInit();
/* Infinite loop */
for (;;) {
/* Handling events 1 */
DoSomething1();
/* Handling events 2 */
DoSomething2();
/* Handling events 3 */
DoSomething3();
}
}
Front and rear system
Compared to the polling system , The front and rear systems add interrupt on the basis of polling system . The response to external events is completed in the interrupt , The event processing is still completed by returning to the polling system . Here we call interrupts “ The front desk ”,main() The infinite loop in a function is called “ backstage ”.
Front and rear system pseudo code
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int main(void)
{
/* Hardware related initialization */
HardWareInit();
/* Infinite loop */
for (;;) {
if (flag1) {
/* Handling events 1 */
DoSomething1();
}
if (flag2) {
/* Handling events 2 */
DoSomething2();
}
if (flag3) {
/* Handling events 3 */
DoSomething3();
}
}
}
void ISR1(void)
{
/* Set the flag bit */
flag1 = 1;
/* If the event processing time is very short , It is processed in the interrupt ; If the event processing time is long , Return to background processing */
DoSomething1();
}
void ISR2(void)
{
/* Set the flag bit */
flag2 = 1;
/* If the event processing time is very short , It is processed in the interrupt ; If the event processing time is long , Return to background processing */
DoSomething2();
}
void ISR3(void)
{
/* Set the flag bit */
flag3 = 1;
/* If the event processing time is very short , It is processed in the interrupt ; If the event processing time is long , Return to background processing */
DoSomething3();
}
When executing daemons sequentially , If an interrupt occurs , Then the interrupt will interrupt the normal execution flow of the background program , Instead, execute the interrupt service program , Mark the event in the interrupt service program . If the event to be handled is brief , It can be handled in the interrupt service program , If the events to be handled are complicated , Return to the background program for processing . Although the response and handling of events are separated , However, the processing of events is executed in the background in sequence , Compared with polling system , The front and rear systems ensure that events are not lost , In addition, interrupts have nested functions , This can greatly improve the real-time response ability of the program . In most small and medium-sized projects , The front and rear systems are well used , Comparable to the operating system .
Multithreading system
Compared to the front and back systems , The event response of multithreaded system is also completed in interrupt , But the event processing is done in the thread . In a multithreaded system , Threads are the same as interrupts , Also has priority , Threads with higher priority will be executed first . When an emergency is flagged in an interrupt , If the priority of the thread corresponding to the event is high enough , Will get an immediate response . Compared to the front and back systems , The real-time performance of multithreaded system has been improved .
Multithreaded system pseudo code
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int main(void)
{
/* Hardware related initialization */
HardWareInit();
/* OS initialization */
RTOSInit();
/* OS start-up , Start multithreading scheduling , Don't go back */
RTOSStart();
}
void ISR1(void)
{
/* Set the flag bit */
flag1 = 1;
}
void ISR2(void)
{
/* Set the flag bit */
flag2 = 2;
}
void ISR3(void)
{
/* Set the flag bit */
flag3 = 1;
}
void DoSomething1(void)
{
/* Infinite loop , Can't return */
for (;;) {
/* Thread entity */
if (flag1) {
}
}
}
void DoSomething2(void)
{
/* Infinite loop , Can't return */
for (;;) {
/* Thread entity */
if (flag2) {
}
}
}
void DoSomething3(void)
{
/* Infinite loop , Can't return */
for (;;) {
/* Thread entity */
if (flag3) {
}
}
}
Compared with the program body executed in the background in the front and back system , In a multithreaded system , According to the function of the program , We divide the program body into independent 、 An applet that loops indefinitely and cannot return , This little program we call “ Threads ”. Each thread is independent 、 non-interfering , And have their own priorities , It's managed by the operating system . After joining the operating system , We do not need to carefully design the execution flow of the program when programming , Don't worry about interference between each functional module . Added the operating system , Our programming has become simpler . The extra cost of the whole system is the small amount of the operating system FLASH and RAM. Today, , Single chip microcomputer FLASH and RAM More and more capacity , Completely enough to support RTOS The cost of .
Whether it's a polling system 、 Front and back systems are also multi-threaded systems , You can't simply judge which is better or worse , They are the products of different times , It has considerable application value in their respective fields , Only the right is the best .
边栏推荐
- Scanpy (VI) analysis and visualization of spatial transcriptome data
- Glibc memory management model frees C library memory cache
- 面试:什么是浅拷贝、深拷贝?
- Defer learning in golang
- Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code
- 5-5配置Mysql复制 基于日志点的复制
- 盒马,最能代表未来的零售
- Global and Chinese market for material injection 2022-2028: Research Report on technology, participants, trends, market size and share
- acwing796 子矩阵的和
- go net库(待续)
猜你喜欢
C packing and unpacking
Project training of Software College of Shandong University rendering engine system radiation pre calculation (IX)
Redis General Command
redis String类型常见命令
Multimix:从医学图像中进行的少量监督,可解释的多任务学习
Saga体系结构模式:微服务架构下跨服务事务的实现
Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code
一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤
Use of packet capturing tool Fiddler: simulating speed limit test process in weak network environment
连续八年包装饮用水市占率第一,这个品牌DTC是如何持续增长的?
随机推荐
Data analysis | kmeans data analysis
Five models of software testing
Global and Chinese market for commercial ceiling fans 2022-2028: Research Report on technology, participants, trends, market size and share
面试:了解装箱和拆箱操作吗?
Example of bit operation (to be continued)
(四)GoogleNet复现
Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]
Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code
< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(八)
acwing 790. 数的三次方根(浮点数二分)
FPGA (III) trigger and latch
puppeteer入门之 BrowserContext 类
从斐波那契数列求和想到的俗手、本手和妙手
Let's talk about events. Listen to those things. - Part one
Scanpy (VI) analysis and visualization of spatial transcriptome data
Introduction and download website of common data of GIS, remote sensing, hydrology and Geography (2), supplementary~
Task output: dense snow ice city theme song 0612
联通网管协议框图
面试:为什么整数包装类尽量用equals()来比较大小
Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)