当前位置:网站首页>Protection of RT thread critical section
Protection of RT thread critical section
2022-06-22 07:05:00 【1ShyJn25】
Protection of critical section
What is critical section
Critical section , In one sentence, it is a code segment that cannot be interrupted during execution . stay RT-Thread in , The most common scenario for critical segments is the operation of global variables , Global variables are like a target , Anyone can shoot him , But one shot , No one else can shoot , Otherwise, I don't know who hit the target .
So when will the critical section be interrupted ? One is system scheduling , There is also an external interrupt . stay RT-Thread in , Finally, system scheduling is also generated PendSV interrupt , stay PendSV Handler Implementation of thread switching in , So it can be reduced to interruption . In this case ,RT-Thread The protection of critical sections is simply handled , Turn off the interrupt directly , but NMI FAULT And hard FAULT With the exception of .
Cortex-M Kernel fast shutdown interrupt instruction
In order to switch off the interrupt quickly ,Cortex-M The kernel has a special CPS Instructions , Yes 4 Usage .
Code list CPS Instruction usage
CPSID I ;PRIMASK=1 ; Close the interrupt
CPSIE I ;PRIMASK=0 ; Open the interrupt
CPSID F ;FAULTMASK=1 ; Turn off the abnormality
CPSIE F ;FAULTMASK=0 ; Open abnormally
In the code listing PRIMASK and FAULTMAST yes Cortex-M The kernel 3 In interrupt mask registers 2 individual , The other one is BASEPRI, About this 3 The detailed usage of registers is shown in the table .

Close the interrupt
RT-Thread The function that closes the interrupt is contex_rvds.s In the definition of , stay rthw.h In a statement , See the code list for the specific implementation 4-2.
Close the interrupt
/* ; * rt_base_t rt_hw_interrupt_disable(); ; */
rt_hw_interrupt_disable PROC
EXPORT rt_hw_interrupt_disable
MRS r0, PRIMASK
CPSID I
BX LR
ENDP
1): keyword PROC Indicates that the assembly subroutine starts .
2): Use EXPORT Keyword export label rt_hw_interrupt_disable, Make it global , After the external header file declaration ( stay rthw.h In a statement ), You can go to C Call in file .
3): adopt MRS The instruction will special register PRIMASK The value of the register is stored in a general-purpose register r0. When in C When the subroutine called in the assembly returns , Will r0 As the return value of the function . So in C Call in rt_hw_interrupt_disable() when , You need to declare a variable in advance to store rt_hw_interrupt_disable() The return value of , namely r0 Register value , That is to say PRIMASK Value .
4): Turn off interrupt , That is to use CPS Instructions will be PRIMASK Set the value of the register 1.
5): Subprogram return .
6):ENDP Indicates the end of the assembly subroutine , And PROC Use in pairs .
Open the interrupt
RT-Thread The interrupt function is in the contex_rvds.s In the definition of , stay rthw.h In a statement .
Open the interrupt
/* * void rt_hw_interrupt_enable(rt_base_t level); */
rt_hw_interrupt_enable PROC
EXPORT rt_hw_interrupt_enable
MSR PRIMASK, r0
BX LR
ENDP
1): keyword PROC Indicates that the assembly subroutine starts .
2): Use EXPORT Keyword export label rt_hw_interrupt_enable, Make it global , After the external header file declaration ( stay rthw.h In a statement ), You can go to C Call in file .
3): adopt MSR The instruction will the general purpose register r0 The value of is stored in a special register PRIMASK.
4): Subprogram return .
5):ENDP Indicates the end of the assembly subroutine , And PROC Use in pairs .
Application of critical segment code
Before entering the critical section , We will turn off the interrupt first , When exiting the critical section, open the interrupt , and Cortex-M The kernel has set the fast shutdown interrupt CPS Instructions , The implementation of switch interrupt function and the protection of critical segment code .
The implementation of switch interrupt function and the protection of critical segment code
Implementation of switch interrupt function
/* * void rt_hw_interrupt_disable(); */
rt_hw_interrupt_disable PROC
EXPORT rt_hw_interrupt_disable
CPSID I
BX LR
ENDP
;/* ; * void rt_hw_interrupt_enable(void); ; */
rt_hw_interrupt_enable PROC
EXPORT rt_hw_interrupt_enable
CPSIE I
BX LR
ENDP
PRIMASK = 0; /* PRIMASK The initial value is 0, It means no interruption */
/* Critical segment code protection */
{
/* The critical section begins */
rt_hw_interrupt_disable(); /* Close the interrupt ,PRIMASK = 1 */
{
/* Execute critical code , Non interruptible */
}
/* The critical section ends */
rt_hw_interrupt_enable(); /* Open the interrupt ,PRIMASK = 0 */
}
边栏推荐
- 六月集训(第22天) —— 有序集合
- Introduction to 51 Single Chip Microcomputer -- minimum system of single chip microcomputer
- [write CPU by yourself] implementation of exception related instructions
- Xh_ CMS penetration test documentation
- Yolov1 (training process)
- Neuron+ekuiper realizes data collection, cleaning and anti control of industrial Internet of things
- vue连接mysql数据库失败
- 自然语言处理理论和应用
- Difference between grail layout and twin wing layout
- Data security practice guide - data collection security practice - data classification and classification
猜你喜欢

Introduction to 51 single chip microcomputer - 8x8 dot matrix LED
![[meta learning] classic work MAML and reply (Demo understands meta learning mechanism)](/img/e5/ea68e197834ddcfe10a14e631c68d6.jpg)
[meta learning] classic work MAML and reply (Demo understands meta learning mechanism)

如何才能有效缓解焦虑?看看猿辅导怎么说

Advanced usage of setting breakpoints during keil debugging

In depth analysis of 20million OP events stolen by optimization (including code)

The journey of an operator in the framework of deep learning

Yolov1 (training process)

golang調用sdl2,播放pcm音頻,報錯signal arrived during external code execution。

Error: unable to find vcvarsall Solutions to bat errors

【实习】跨域问题
随机推荐
Several methods of array de duplication in JS
六月集训(第22天) —— 有序集合
Yolov1 (prediction process)
Qt development simple Bluetooth debugging assistant (low power Bluetooth)
Languo technology helps the ecological prosperity of openharmony
sessionStorage 和 localStorage 的使用
Jupyter notebook file storage location
CNN model collection | RESNET variants -wideresnet interpretation
Five common SQL interview questions
Introduction to 51 Single Chip Microcomputer -- timer and external interrupt
CNN模型合集 | Resnet变种-WideResnet解读
Data security practice guide - data collection security management
Successfully solved raise keyerror (F "none of [{key}] are in the [{axis\u name}]") keyerror: "none of [index (['age.in.y
[internship] cross domain problems
Training penetration range 02 | 3-star VH LLL target | vulnhub range node1
实训渗透靶场02|3星vh-lll靶机|vulnhub靶场Node1
QT connect to Alibaba cloud using mqtt protocol
Error: unable to find vcvarsall Solutions to bat errors
Cesium加载3D Tiles模型
Self supervised learning for general out of distribution detection AAAI '20