hello everyone , I am boundless .

Once when I was broadcasting live to internal students , Some friends asked me , Why enter the critical stage , What's the use of criticality ?

I didn't think of a good explanation for a moment and a half , Let me give you a few examples today .

If a big man thinks I'm wrong, please talk privately and I'll correct it , Don't secretly spray me in the comment area , Can't I change it .

Entry and exit criticality , My first contact was in ucos system , At that time, I also felt that this professional term was awesome and complicated .

When I finish talking , You'll find it's actually pretty good low.

Entering the threshold is to turn off the total interrupt of MCU , Exit critical is to restore MCU interrupt , Remember, it's best to recover , Not open , Because before entering the critical point, the total interruption of MCU may not be on .

Then why enter the critical point ? What does it do ?

Let's assume a scenario :

Let's look at such a piece of code .

Suppose we run the program to the third 12 that 's ok , that is a = 0 Location of ,*p The value of must be equal to 2 Right ?

Suddenly a timer interrupt came , Then in the timer interrupt processing function , We did p++.

After execution, return to the main program to continue execution , That is, it will be executed 13 Line code , At this time *p Guess what the value is ?

you 're right , Definitely not 2, But 3.

Clearly conditional judgment is 2, Finally, the value becomes 3, such bug I guess I can make you cry .

At this time, when we enter the critical point, we are very stable , Can be changed to the following code .

STM32 Not like 51 Single chip microcomputer is so direct EA=0 You can turn off the total interrupt , But through __get_PRIMASK and __set_PRIMASK To do , Of course, it can also be used __disable_irq and __enable_irq.

Specific information can be searched in the project , Look at the notes .

Every single chip microcomputer turns off the total interrupt in a different way , Therefore, there are also differences between entry critical and exit critical codes .

You just need to remember that the essence is to shut down the total interruption , After executing the procedure , Just restore the total interruption .

Don't worry too much about how to switch when the single chip microcomputer is always interrupted , You can find it on the Internet .

Then continue to talk about the program above , Many people may say , I avoid this problem when I write programs , Not in the timer, right p Just operate on the pointer ?

If you are doing some big projects , Almost impossible to avoid , For example, our queue algorithm .

We will receive the data in the serial port interrupt , Throw data into the queue , Then it is analyzed in the main function .

After using the queue , The stability of data transmission can be greatly improved , Even if the data transmission is fast , There is a large amount of data and there will be no packet loss .

Let's take a look at the listing function of the queue algorithm .

such , If you don't use a pointer , Almost impossible , Even the flexibility and portability are not high .

Imagine , If this happens, the pointer will be operated frequently in the main function and interrupt , Without criticality, the program is easy to crash due to pointer pointing exception or program logic problems caused by data disorder .

of course , Enter critical in addition to protecting global variables , array , Structure and other data , It can also be hardware data , such as IO,SCI,SPI,flash.

of course ,RTOS It may be more complicated , I won't explain them one by one here , Just understand the essence .

Technology
Daily Recommendation