当前位置:网站首页>02 - bare metal and RTOS development modes: five development modes of bare metal and the introduction of RTOS
02 - bare metal and RTOS development modes: five development modes of bare metal and the introduction of RTOS
2022-06-30 07:17:00 【Oil splashed sliced noodles】
02 - Bare metal and RTOS Development mode
One 、 Bare metal development mode
1. Polling mode
int main()
{
while(1)
{
do_something1();
do_something2();
}
}
- For the most part ,mian() There are no long blocking tasks in the
- shortcoming : Functions affect each other , Doing it do_something1() Other functions are completely blocked , You can only wait for the function to complete
2. Event driven way ( Taiwan before and after )
int main()
{
while(1)
{
}
}
do_sth1() // Interrupt function 1
{
do_something1;
}
do_sth2()// Interrupt function 2
{
do_something2;
}
- Only applicable to functions with short processing time in interrupts , If the time is long and the interrupt priority is high , Will affect other functions
- At the same time ( Macroscopic ) Only one function can be processed , Other interrupts may be lost
- Relative to polling , For functions with high priority, the real-time performance is improved
3. Improved event driven approach
int main()
{
while(1)
{
if(1 == flag)
do_sth1();
if(2 == flag)
do_sth2();
}
}
do_sth1() // Interrupt function 1
{
flag = 1;
}
do_sth2()// Interrupt function 2
{
flag = 2;
}
- Interrupt processing is fast , Ensure that interrupts are not lost
- For the task corresponding to the interrupt , Put it all together main Function to poll for execution , It's like a trade-off between polling and front and back
4. Common time driven methods : Timer ***( Focus on )***
- Maximum operation time : The maximum time from the start of a function to its end
- Real time response index : The longest period of time that a function is required to respond
Timer mode at maximum running time 、 When the real-time response indicators are determined , Artificially put them on the timeline , It is more accurate than the front and background . In the absence of RTOS front , In this way .
typedef struct soft_timer {
int remain;
int period;
void (*function)(void);
}soft_timer, *p_soft_timer;
static soft_timer timers[] = {
{
1, 1, A},
{
2, 2, B},
{
3, 3, C},
};
void main()
{
while (1)
{
}
}
void timer_isr()
{
int i;
/* timers Of each member in the array expire All minus one */
for (i = 0; i < 3; i++)
timers[i].remain--;
/* If timers Of a member in an array expire be equal to 0: * 1. Call its function * 2. recovery expire by period */
for (i = 0; i < 3; i++)
{
if (timers[i].remain == 0)
{
timer[i].function();
timers[i].remain = timers[i].period;
}
}
}
It can be seen that , This way function() The execution of will affect the count of the counter , And then it will affect all the following functions .
Refer to the front and background methods , We can put the task function into main Function , The advantages and disadvantages are similar to those of the front and background .
typedef struct soft_timer {
int remain;
int period;
void (*function)(void);
}soft_timer, *p_soft_timer;
static soft_timer timers[] = {
{
1, 1, A},
{
2, 2, B},
{
3, 3, C},
};
void main()
{
int i;
while (1)
{
/* If timers Of a member in an array expire be equal to 0: * 1. Call its function * 2. recovery expire by period */
for (i = 0; i < 3; i++)
{
if (timers[i].remain == 0)
{
timer[i].function();
timers[i].remain = timers[i].period;
}
}
}
}
void timer_isr()
{
int i;
/* timers Of each member in the array expire All minus one */
for (i = 0; i < 3; i++)
if (timers[i].remain)
timers[i].remain--;
}
5. State machine thinking (RTOS matting )
The above four methods will cause varying degrees of impact when the function processing time is very long , In order to mitigate this effect , We can artificially split a function into several segments , Execute one paragraph at a time , Jump out immediately after execution , Check if there are other higher priority tasks to do , If there is a priority , without , Continue executing the segmented function from where it left off
The code is as follows :
/***** Before change *****/
void A()
{
// function A Implementation takes a particularly long time
}
int main()
{
while(1)
{
A();
}
}
/***** After the change *****/
void A()
{
static int state = 0;
switch(state)
{
case 0:
A1();
state++;
return;
case 1:
A2();
state++;
return;
...
}
}
int main()
{
while(1)
{
A();
B();
...
}
}
Two 、RTOS The introduction of and programming considerations
1.RTOS introduce
According to the bare metal development mode , In the programming mode of state machine thought , Single chip microcomputer system can obtain a more excellent performance .
But in the actual programming process , The splitting of each function requires actual hardware and software debugging , Requires experience and is less efficient .
RTOS One of its functions is to rotate tasks in time slices , To achieve the same effect as the state machine , And the programming method is simple .
The sample code is as follows :
// RTOS Program
Task1()
{
while (1)
{
do_sth1();
}
}
Task2()
{
while (1)
{
do_sth2();
}
}
void main()
{
create_task(Task1);// Create tasks
create_task(Task2);
start_scheduler();// Task scheduling
while (1)
{
sleep();
}
}
2. Programming considerations
2.1 Critical zone protection
stay RTOS In development , Switch between different tasks , Two tasks modify the same variable at the same time .
Such as TaskA Yes a The value of is passed through the register R0 Conduct a++ modify :
(A1)R0 = a;
(A2)R0 = R0 +1;
(A3)a = R0;
When it comes to (A2) Suddenly TaskB adopt R1 Yes a The value of is also a++ modify :
(A1)R0 = a++;
(A2)R0 = R0 +1;
(B1)R1 = a;
(B2)R1 = R1 + 1;
(B3)a = R1;
(A3)a = R0;
After the whole process, it is only for a There was one a++ operation , The function needs to be executed twice , Lost once a++.
So in RTOS in , Modifying the same variable in different tasks requires critical de protection , That is, the process of variable modification is protected by the critical area , It is not allowed to modify other tasks at the same time .
2.2 Sleep — Wake up mechanism : Improve CPU utilization
int main()
{
while(1)
{
A();//flag = 0; flag++; A Function will flag from 0 Start accumulating
B();//if(flag == 1000){do_sth;} B Function wait until flag1000 perform
}
}
stay while During each execution , It's all going on flag The steps of accumulation and query , Yes 1000 Time ×(flag++) and 1000 Time × if(flag == 1000), But most of them if All judgments are invalid .
RTOS Can be accepted for the first time if When judging, put the function B recorded , wait until flag = 1000 Take the initiative to notify the function B.
Thus, the number of 999 Time if Judge ,CPU You can use the time for this operation to perform other tasks , And then improve CPU utilization .
3、 ... and 、 Other questions
1. Bare metal programming and RTOS Who is more real-time ?
Whether bare metal or RTOS, Can be divided into Interrupt handling and User program processing There are two ways to process tasks , And the priority of interrupt is higher than that of user program ( That is, as long as there is an interruption , Just execute the interrupt , Unless other interrupts have a higher priority ).
- Bare metal function execution time :
- interrupt : According to the interrupt priority , Give priority to higher priority functions .
- User programs : according to main Sequence of task functions placed in function , From the front to the back , After processing the interrupt function , Continue to execute downward at the last left position .
- summary : Bare metal , The best real-time response is placed in the interrupt function with the highest priority . Except for interrupt function , The real-time response of other user programs depends on main Function with different execution times , More mechanical .
- RTOS Function execution time :
- interrupt : According to the interrupt priority , Give priority to higher priority functions . But in some critical zone protection functions , Some interrupts will be masked , This leads to poor real-time interrupt response in this part ( The actual time is just a few instructions , The impact is not big ).
- User programs : According to the priority in the ready list , Give priority to high priority tasks .
- summary : If you want absolute real-time response, write to the interrupt function with the highest priority .
- contrast : For different interrupts or user programs , Bare metal development and RTOS The real-time response of the development has its own characteristics . Generally speaking ,RTOS The sequence control of response is more flexible and effective .
2. Does bare metal programming require critical zone protection ?
- answer : need
- If in main() For a variable in a function a To operate , A sudden interruption is also right a To operate , At this time, mistakes may occur
- There were no errors in bare metal programming before , The guess is that the probability of such a problem is too low , Even if , The problem disappears after a reboot , It's hard to find out why
边栏推荐
- [resolved] MySQL exception: error 1045 (28000): unknown error 1045, forgetting the initial password
- app闪退
- Skillfully use 5 keys to improve office efficiency
- [daily question] 535 Encryption and decryption of tinyurl
- First line of code (Third Edition) learning notes
- 大学刚毕业不知道做什么工作怎么办?
- Record common problems: spaces in encodeuricomponent decoding and the use of Schema in third-party apps to invoke apps
- 神经网络计算量及参数量
- QT msvc2015 compiler reports an error: error: lnk1158: unable to run "rc.exe"
- Installation du serveur linux redis
猜你喜欢

Install go language development tools

Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)

app quits unexpectedly

编写并运行第一个Go语言程序

2021-07-02

视频播放器(一):流程

Calculation and parameter quantity of neural network

Class template case - encapsulation of array classes

系统软件开发基础知识

Deploying web projects using idea
随机推荐
Go项目目录结构介绍
Raspberry pie trivial configuration
Servlet principle
nRF52832 GPIO LED
B站首个UP主付费观看视频还是来了!价格“劝退”网友
QT elementary notes
June 29, 2022 -- take the first step with C # -- add decision logic to the code using the "if", "else" and "else if" statements in C #
Resolution: div failed to get keyboard event
如果我在珠海,到哪里开户比较好?另外,手机开户安全么?
Keil serial port redirection
Skillfully use 5 keys to improve office efficiency
How to use string branches for switch case
Utilisation de la commande grep
[solved] failed! Error: Unknown error 1130
[Hot100]10. Regular Expression Matching
oracle数据库报列表中最大表达式为1000错误
安装Go语言开发工具
IDEA import导入的类明明存在,却飘红?
Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)
What if I don't know what to do after graduating from university?