-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathSTM32中NVIC(嵌套向量中断控制)的理解.html
149 lines (146 loc) · 9.49 KB
/
STM32中NVIC(嵌套向量中断控制)的理解.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
</style>
<link rel="stylesheet" href="../stylesheets/Github.css" type="text/css" />
<title>STM32中NVIC(嵌套向量中断控制)的理解</title>
</head>
<body>
<div id="header"><center>
<p class="header_titleline">
<a href="../index.html" target="_self" title="主页">主页 </a><a href="../Search.html" target="_self" title="站内搜索">站内搜索 </a><a href="../Projects.html" target="_self" title="项目研究">项目研究 </a><a href="../Archives.html" target="_self" title="文章存档">文章存档 </a><a href="../README.html" target="_self" title="分类目录">分类目录 </a><a href="../AboutMe.html" target="_self" title="关于我">关于我 </a>
</p>
</center></div>
<h1>STM32中NVIC(嵌套向量中断控制)的理解</h1>
<h4>2011-10-04,整理于2014-09-25 / xiahouzuoxin</h4>
<h4>Tags: ARM</h4>
转载请注明出处: <a href="http://xiahouzuoxin.github.io/notes/">http://xiahouzuoxin.github.io/notes/</a>
<!---title:STM32中NVIC(嵌套向量中断控制)的理解-->
<!---keywords:ARM-->
<!---date:2011-10-04,整理于2014-09-25-->
<p>这是一篇旧文,早在上大二的时候,在地下室(当时实验室在防空地下室)顺手在word上写的,当时STM32正刚刚开始蓬勃发展,如今时隔3年,整理到这里!</p>
<p>两日被些许琐事所牵,身心俱累,本无心记录,但回想前几天的点滴收获,无录甚是可惜,于是身倚椅,旁敲键盘记之,唯慰藉自己及共享同道仁友。废言不再多,就此入题。</p>
<p>NVIC,中文名嵌套中断向量控制器,是Cortex-M3系列控制器内部独有集成单元,与CPU结合紧密,降低中断延迟时间并且能更加高效处理后续中断。举个例子,比如火车站买票,那些火车站的规章制度就是NVIC,规定学生和军人有比一般人更高优先级,它们则给你单独安排个窗口,同学与同学之间也有区别,那就是你也得排队,也就是你的组别(抢断优先级)和你的排队序号(响应优先级)决定你何时能买到票。</p>
<p>抢断优先级,顾名思义,能再别人中断是抢占别人中断,实现中断嵌套。响应优先级则只能排队,不能抢在前面插别人的对,即不能嵌套。</p>
<p>STM32中指定优先级的寄存器为4位,其定义如下:</p>
<blockquote>
<p>第0组:所有4位用于指定响应优先级</p>
</blockquote>
<blockquote>
<p>第1组:最高1位用于指定抢占式优先级,最低3位用于指定响应优先级</p>
</blockquote>
<blockquote>
<p>第2组:最高2位用于指定抢占式优先级,最低2位用于指定响应优先级</p>
</blockquote>
<blockquote>
<p>第3组:最高3位用于指定抢占式优先级,最低1位用于指定响应优先级</p>
</blockquote>
<blockquote>
<p>第4组:所有4位用于指定抢占式优先级</p>
</blockquote>
<p>以上定义也称作中断优先级分组,相关内容在STM32固件库的misc.h文件中有详细定义。</p>
<p>基础了解了就可以对中断进行操作了:</p>
<ol style="list-style-type: decimal">
<li><p>使用<code>void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)</code>函数对优先级分组配置。<code>NVIC_PriorityGroup</code>可以配置为</p>
<p>NVIC_PriorityGroup_0 => 选择第0组</p>
<p>NVIC_PriorityGroup_1 => 选择第1组</p>
<p>NVIC_PriorityGroup_2 => 选择第2组</p>
<p>NVIC_PriorityGroup_3 => 选择第3组</p>
<p>NVIC_PriorityGroup_4 => 选择第4组</p>
<p>例如:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0)配置为0组。</p></li>
<li><p>中断初始化结构体配置,结构体类型定义如下:</p>
<pre class="sourceCode c"><code class="sourceCode c"><span class="kw">typedef</span> <span class="kw">struct</span>
{
<span class="dt">uint8_t</span> NVIC_IRQChannel;
<span class="dt">uint8_t</span> NVIC_IRQChannelPreemptionPriority; <span class="co">//抢断优先级</span>
<span class="dt">uint8_t</span> NVIC_IRQChannelSubPriority; <span class="co">//响应优先级 </span>
FunctionalState NVIC_IRQChannelCmd;
} NVIC_InitTypeDef;</code></pre>
<p>例如:STM32外部中断0配置如下:</p>
<pre><code>EXTI_NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
EXTI_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级别(0~1)
EXTI_NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7; //响应优先级别(0~7)
EXTI_NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;</code></pre></li>
<li><p>中断初始化结构体初始化操作如下</p>
<pre><code>NVIC_Init(&EXTI_NVIC_InitStructure);</code></pre></li>
<li><p>开关总中断操作。在STM32中是通过改变CPU优先级来允许和禁止中断的。</p>
<ul>
<li><p>下面两个函数等效关闭总中断</p>
<pre class="sourceCode c"><code class="sourceCode c"><span class="dt">void</span> NVIC_SETPRIMASK(<span class="dt">void</span>);
<span class="dt">void</span> NVIC_SETFAULTMASK(<span class="dt">void</span>);</code></pre></li>
<li><p>下面两个函数等效开放总中断</p>
<pre class="sourceCode c"><code class="sourceCode c"><span class="dt">void</span> NVIC_RESETPRIMASK(<span class="dt">void</span>);
<span class="dt">void</span> NVIC_RESETFAULTMASK(<span class="dt">void</span>);</code></pre></li>
<li><p>常用操作是先关后开中断</p>
<pre class="sourceCode c"><code class="sourceCode c">NVIC_SETPRIMASK(); <span class="co">// Disable Interrupts </span>
NVIC_RESETPRIMASK(); <span class="co">// Enable Interrupts</span></code></pre></li>
</ul>
<p>两种类型函数要成对使用。</p></li>
</ol>
<div class="ds-thread" data-thread-key="STM32中NVIC(嵌套向量中断控制)的理解" data-title="STM32中NVIC(嵌套向量中断控制)的理解" data-url="xiahouzuoxin.github.io/notes/html/STM32中NVIC(嵌套向量中断控制)的理解.html"></div>
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"slide":{"type":"slide","bdImg":"5","bdPos":"right","bdTop":"300"},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"xiahouzuoxin"};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';ds.async = true;
ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
</script>
<!-- 多说公共JS代码 end -->
<div id="footer">
<p class="footer_subline">联系邮箱: [email protected]</p>
<p class="footer_subline">声明: 本站所有文章如非特别说明均为原创,转载请注明出处!
<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1253219218'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1253219218%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script>
</p>
</div>
<!-- 回到顶部 -->
<script>
lastScrollY=0;
function heartBeat(){
var diffY;
if (document.documentElement && document.documentElement.scrollTop)
diffY = document.documentElement.scrollTop;
else if (document.body)
diffY = document.body.scrollTop
else
{/*Netscape stuff*/}
percent=.1*(diffY-lastScrollY);
if(percent>0)percent=Math.ceil(percent);
else percent=Math.floor(percent);
document.getElementById("full").style.top=parseInt(document.getElementById("full").style.top)+percent+"px";
lastScrollY=lastScrollY+percent;
}
suspendcode="<div id=\"full\" style='right:1px;POSITION:absolute;TOP:600px;z-index:100'><a onclick='window.scrollTo(0,0);'><img src='../images/top.png'></a><br></div>"
document.write(suspendcode);
window.setInterval("heartBeat()",1);
</script>
</body>
</html>