当前位置:网站首页>What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
2022-07-06 08:01:00 【Infinite MCU programming】
Hello everyone , I am boundless .
Once I was broadcasting live to internal students , A few friends asked me , Why enter the critical , What's the use of criticality ?
At that time, I didn't expect a good explanation , Today, let me give you a few examples to answer .
If any boss 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 yet .
Enter critical and exit critical , 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 quite low.
Entering the critical point is to shut down the MCU and interrupt , Exit critical It is to resume the interruption of single chip microcomputer , 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 ? What's the role ?
Let's suppose a scenario :
Let's look at this piece of code .
Suppose our program runs to 12 That's ok , That is to say a = 0 The location of ,*p The value of must be equal to 2 Right? ?
Suddenly a timer interrupts , Then in the timer interrupt handler , We did p++.
After execution, return to the main program to continue execution , That is to say, I will implement 13 Line code , This is the time *p Guess the value of ?
you 're right , Definitely not 2, It is 3.
Obviously, the condition judgment is 2, Finally, the value becomes 3, such bug It's estimated that it can make you cry .
At this time, entering the critical point is very stable , It can be changed to the following code .
STM32 Unlike 51 SCM is so direct EA=0 You can turn off the total interrupt , But through __get_PRIMASK and __set_PRIMASK To do it , You can also use it __disable_irq and __enable_irq.
Specifically, you can search in the project , Read notes .
Every single chip microcomputer turns off the total interrupt in a different way , So there is also a difference between entering the critical code and exiting the critical code .
You just need to remember The essence is to shut down and interrupt , 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 our above program , A lot of people would say , I avoid this problem when writing programs , Not in the timer right p Just operate 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 , Put the data in the queue , Then analyze 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 , It is almost impossible to achieve , Even the flexibility and portability are not high .
Just imagine , If this kind of pointer is frequently operated in the main function and interrupt , Without criticality, the program is easy to crash due to abnormal pointer pointing or program logic problems caused by data disorder .
Of course , In addition to protecting global variables 、 Array 、 Structure and other data are not bad, except , It can also be hardware data , such as IO、SCI、SPI、flash.
Of course ,RTOS Maybe it's more complicated , I won't explain it here , Just understand the essence .
边栏推荐
- 使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据
- TiDB备份与恢复简介
- Analysis of Top1 accuracy and top5 accuracy examples
- Uibehavior, a comprehensive exploration of ugui source code
- "Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
- Golang DNS write casually
- P3047 [USACO12FEB]Nearby Cows G(树形dp)
- Wireshark grabs packets to understand its word TCP segment
- Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
- Mex related learning
猜你喜欢
Database basic commands
Qualitative risk analysis of Oracle project management system
[untitled]
【Redis】NoSQL数据库和redis简介
Step by step guide to setting NFT as an ens profile Avatar
让学指针变得更简单(三)
Interview Reply of Zhuhai Jinshan
Wireshark grabs packets to understand its word TCP segment
Opencv learning notes 9 -- background modeling + optical flow estimation
解决方案:智慧工地智能巡检方案视频监控系统
随机推荐
CAD ARX gets the current viewport settings
The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
shu mei pai
Mex related learning
数据治理:误区梳理篇
在 uniapp 中使用阿里图标
Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University
数据治理:元数据管理篇
Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
Data governance: Data Governance under microservice architecture
esRally国内安装使用避坑指南-全网最新
Wireshark grabs packets to understand its word TCP segment
WebRTC系列-H.264预估码率计算
Webrtc series-h.264 estimated bit rate calculation
远程存储访问授权
"Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
Document 2 Feb 12 16:54
Make learning pointer easier (3)
P3047 [usaco12feb]nearby cows g (tree DP)