?? 改進uc-os ii,減少內存使用量--armstrong.htm
字號:
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=orange-bar>博客文章</TD></TR></TBODY></TABLE>
<TABLE class=space-4 cellSpacing=0 cellPadding=0 width="100%"
border=0>
<TBODY>
<TR>
<TD vAlign=top align=middle>
<TABLE cellSpacing=0 cellPadding=3 width="100%" border=0>
<TBODY>
<TR>
<TD class="S word-break lh15" align=left>
<P align=left>
<TABLE
style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all"
cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=oblog_t_4 borderColor=#999999
bgColor=#cccccc><SPAN class=style1><FONT
size=3><STRONG>改進uC/OS II,減少內存使用量</STRONG></FONT></SPAN></TD></TR>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width="100%"
border=0>
<TBODY>
<TR>
<TD>
<DIV align=right>流星 發表于 2005-8-16
14:06:10</DIV></TD></TR></TBODY></TABLE><BR><SPAN
id=ob_logd2191></SPAN>
<P
class=p4>在以uC/OS為操作系統的項目中,系統可能要處理各種不同的中斷請求,如果某個中斷處理程序需要調用uC/OS的各種Post函數向任務發出消息,那么uC/OS建議中斷服務程序的寫法是:<BR>1、保存全部CPU寄存器<BR>2、調用OSIntEnter或OSIntNesting直接加1<BR>3、執行用戶代碼做中斷服務<BR>4、調用OSIntExit<BR>5、恢復所有CPU寄存器<BR>6、執行中斷返回指令<BR>暫且稱為“標準中斷”方式,這種方式實際上是將這個中斷處理加入了任務調度系統,也就是說這個中斷可以引起任務的切換。</P>
<P
class=p4>如果在中斷處理中沒有調用各種Post函數的話,則可以用一般的、象原來沒有操作系統時的寫法:<BR>1、保存中斷處理程序需要用到的CPU寄存器<BR>2、執行中斷處理<BR>3、恢復保存了的CPU寄存器<BR>4、執行中斷返回指令<BR>暫且稱為“快中斷”方式,按照這種方法定義的中斷永遠不會引起任務切換。</P>
<P
class=p4>在uC/OS系統中,每個任務都要定義獨立的棧空間,一個棧空間的使用包括5個部分:<BR>1、任務包括的各個函數的調用返回地址<BR>2、任務包括的各個函數中可能在棧上分配的局部變量<BR>3、發生了“標準中斷”方式定義的中斷或任務被掛起時,所要保存的任務上下文<BR>4、發生了“快中斷”方式定義的中斷時,中斷處理程序所需要的棧空間<BR>5、中斷嵌套時,所要保存的中斷嵌套上下文</P>
<P
class=p4>在這些使用的部分中,1,2,3,4的內存占用量是比較容易估算的,最精確和保險的確定方法是:查看由C生成的asm文件,并計算各個函數的棧使用量。但是第5部分的棧空間使用量是隨中斷嵌套的深度而不斷增加的,是不確定的,一般的方法只能定義一個充分大的棧空間,使之不會溢出。</P>
<P
class=p4>為每個任務都定義一個充分大的棧空間,這在某些內存稀缺的小項目中是非常痛苦的,有時不得不增擴內存,這就會使成本增加。</P>
<P
class=p4>我深入研究了uC/OS后,認為,可以將所有任務棧空間使用的第5部分合并,這樣將會大大的降低整個系統對內存的需求。</P>
<P
class=p4>uC/OS的任務調度是靠OS_Sched和OSIntExit來完成的,這兩個函數中都要先判斷一個叫
OSIntNesting的系統變量,如果OSIntNesting不為0,則不進行任務切換。也就是說:在OSIntNesting為1(當前只有一個中斷在處理中,并且沒有嵌套的中斷)時起,如果發生了嵌套的中斷(不管嵌套的層數有深),那么在所有嵌套的中斷一層一層地都返回直到
OSIntNesting再次為1時止,任務棧是不會切換的(棧指針都在一個任務的棧空間中變化)。</P>
<P
class=p4>據此,我們可以這樣改動:設置一個緩沖區OSInterruptStk,作為嵌套中斷的棧空間,由所有任務共享,中斷服務程序改為:<BR>1、保存全部CPU寄存器<BR>2、調用OSIntEnter或OSIntNesting直接加1<BR>增加:2.1、判斷OSIntNesting是否等于1,如果不是則轉到3<BR>增加:2.2、將棧指針SP保存到OSTCBCur->OSTCBStkPtr<BR>增加:2.3、將SP指向OSInterruptStk的棧頂(注意棧增長的方向)。<BR>3、執行用戶代碼做中斷服務<BR>4、調用OSIntExit<BR>增加:4.1、判斷OSIntNesting是否等于0,如果不是則轉到5<BR>增加:4.2、從OSTCBCur->OSTCBStkPtr中恢復棧指針SP<BR>5、恢復所有CPU寄存器<BR>6、執行中斷返回指令</P>
<P
class=p4>并且要修改OSIntCtxSw函數,原始的OSIntCtxSw函數的寫法是:<BR>1、調整棧指針來去掉在調用:OSIntExit,OSIntCtxSw過程中入棧的多余內容<BR>2、將當前任務棧指針保存到OSTCBCur中(OSTCBCur->OSTCBStkPtr
= __SP__)<BR>3、如果需要則調用OSTaskSwHook<BR>4、OSTCBCur =
OSTCBHighRdy<BR>5、OSPrio =
OSPrioHighRdy<BR>6、從OSTCBCur中恢復棧指針(__SP__ =
OSTCBCur->OSTCBStkPtr)<BR>7、恢復保存了的CPU寄存器<BR>8、執行中斷返回指令</P>
<P
class=p4>新的寫法只需將原寫法中的1,2去掉即可,因為1,2步只是保存舊任務的棧指針,而新的寫法中,這些步被移到了“中斷服務程序”中的2.2。</P><BR></TD></TR>
<TR>
<TD class=oblog_t_4></TD></TR>
<TR>
<TD class=oblog_t_4> </TD></TR>
<TR>
<TD class=oblog_t_4><A
href="http://www.mcublog.com/blog/user1/2268/archives/2005/2191.html#">閱讀全文<SPAN
id=ob_logreaded></SPAN></A> | <A
href="http://www.mcublog.com/blog/user1/2268/archives/2005/2191.html#cmt">回復(0)</A>
<SPAN id=ob_logm2191></SPAN>| <A
href="http://www.mcublog.com/blog/showtb.asp?id=2191"
target=_blank>引用通告<SPAN id=ob_tbnum></SPAN></A> |
<A
href="http://www.mcublog.com/blog/user_post.asp?logid=2191"
target=_blank>編輯</A></TD></TR>
<TR>
<TD class=oblog_t_4> </TD></TR>
<SCRIPT language=javascript>
function click() {
}
function click1() {
if (event.button==2) {alert('想要資料嗎?聯系我吧') }}
function CtrlKeyDown(){
if (event.ctrlKey) {alert('不許動!舉起手來,繳槍不殺') }}
document.onkeydown=CtrlKeyDown;
document.onselectstart=click;
document.onmousedown=click1;
</SCRIPT>
</TBODY></TABLE>
<DIV id=morelog>
<UL>
<LI>上一篇:<A
href="http://www.mcublog.com/blog/user1/2268/archives/2005/2190.html">Linux命令使用技巧集錦</A>
<LI>下一篇:<A
href="http://www.mcublog.com/blog/user1/2268/archives/2005/2197.html">關于VxWorks下的中斷和異常</A>
</LI></UL></DIV>
<DIV id=form_comment>
<DIV id=gg_usercomment></DIV><A name=cmt></A>
<DIV class=title>發表評論:</DIV>
<FORM id=commentform name=commentform
onsubmit="return Verifycomment()"
action=/blog/savecomment.asp?logid=2191 method=post>
<DIV class=d1><LABEL>昵稱:<INPUT id=UserName maxLength=20
name=UserName></LABEL></DIV>
<DIV class=d2><LABEL>密碼:<INPUT id=Password type=password
maxLength=20 name=Password> (游客無須輸入密碼)</LABEL></DIV>
<DIV class=d3><LABEL>主頁:<INPUT id=homepage maxLength=50
size=42 value=http:// name=homepage></LABEL></DIV>
<DIV class=d4><LABEL>標題:<INPUT id=commenttopic
maxLength=50 size=42 value=Re:改進uC/OS II,減少內存使用量
name=commenttopic></LABEL></DIV>
<DIV class=d5><INPUT id=edit type=hidden name=edit>
<DIV id=oblog_edit>MCU博客數據載入中, 請稍候...</DIV></DIV>
<DIV class=d6><SPAN id=ob_code></SPAN><INPUT onclick='oblog_edittext.createTextRange().execCommand("Copy");' type=submit value= 提 交 ></DIV></FORM></DIV>
<P></P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></CENTER>
<CENTER>
<TABLE height=60 cellSpacing=0 cellPadding=0 width=950 align=center border=0>
<TBODY>
<TR>
<TD align=middle height=60><A
href="http://www.chanet.com.cn/click.cgi?a=1320&d=249&u="
target=_blank><IMG height=60
src="改進uC-OS II,減少內存使用量--ARMStrong.files/s_image.htm" width=468
border=0></A></TD></TR></TBODY></TABLE></CENTER><!-- tangtang -->
<CENTER class=space-10>
<DIV class=space-10>
<DIV class=space-10></DIV>
<TABLE class=gray-bar-border cellSpacing=0 cellPadding=0 width=950 border=0>
<TBODY>
<TR>
<TD class=M>圖片鏈接</TD></TR></TBODY></TABLE>
<TABLE class=gray-border cellSpacing=0 cellPadding=3 width=950 border=0>
<TBODY>
<TR>
<TD colSpan=2>
<TABLE borderColor=#efefef cellSpacing=1 cellPadding=1 width="100%"
border=0>
<TBODY>
<TR vAlign=center align=middle>
<TD height=31> </TD>
<TD width=88 height=31><A href="http://www.datasheet.cn/"
target=_blank><IMG height=31 alt=中國手冊網 src="" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.ispdown.com/"
target=_blank><IMG height=31 alt=電子在線編程www.ispdown.com src=""
width=88 border=0></A></TD>
<TD width=88 height=31><A href="http://cardmcu.vicp.net/"
target=_blank><IMG height=31 alt=單片機與智能卡 src="" width=88 border=0
heigh="31"></A></TD>
<TD width=88 height=31><A href="http://www.61ic.com/"
target=_blank><IMG height=31 alt=中國電子在線www.61ic.com src="" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.xiao-qi.com/"
target=_blank><IMG height=31 alt=曉奇工作室 src="" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.51eda.com/"
target=_blank><IMG height=31 alt=中國EDA技術網 src="" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.fjmcu.com/"
target=_blank><IMG height=31 alt=精誠電子設計 src="" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.1bei9.com/"
target=_blank><IMG height=31 alt=一杯酒軟件站 src="" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.mcu99.com/"
target=_blank><IMG height=31 alt=單片機啟點網 src="" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.iamrobot.net/"
target=_blank><IMG height=31 alt=機器人天地 src="" width=88
border=0></A></TD>
<TD width=88 height=31> </TD></TR>
<TBODY>
<TR vAlign=center align=middle>
<TD height=31> </TD>
<TD height=31><A href="http://www.pic16.com/" target=_blank><IMG
height=31 src="" width=88 border=0></A></TD>
<TD height=31><A href="http://77169.com/" target=_blank><IMG
height=31 src="" width=88 border=0></A></TD>
<TD height=31><A href="http://www.dz013.com/" target=_blank><IMG
height=31 src="" width=88 border=0></A></TD>
<TD height=31><A href="http://www.chinamcu.net/" target=_blank><IMG
height=31 src="" width=88 border=0></A></TD>
<TD height=31><A href="http://www.ic00.com/" target=_blank><IMG
height=31 src="" width=88 border=0></A></TD>
<TD height=31><A href="http://www.51dzrc.com/" target=_blank><IMG
height=31 src="" width=88 border=0></A></TD>
<TD height=31><A href="http://www.embedworld.com/"
target=_blank><IMG height=31 src="" width=88 border=0></A></TD>
<TD height=31><A href="http://www.86et.com/" target=_blank><IMG
height=31 src="" width=88 border=0></A></TD>
<TD><A href="http://www.zychina.com.cn/" target=_blank><IMG
height=31 src="" width=88 border=0></A></TD>
<TD><A href="http://www.allbiz.cn/" target=_blank><IMG height=31
alt=上海注冊信息網≡注冊上海公司/上海公司注冊 src="" width=88 border=0></A></TD>
<TD height=31> </TD></TR>
<TR vAlign=center align=middle>
<TD width=88 height=31> </TD>
<TD width=88 height=31><A href="http://www.51kaifa.com/"
target=_blank><IMG height=31 alt=無憂電子開發網
src="改進uC-OS II,減少內存使用量--ARMStrong.files/logo2.jpg" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.genphoal.net/"
target=_blank><IMG height=31 alt=LED專業論壇 src="" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.mculab.com/"
target=_blank><IMG height=31 alt=芯片實驗室
src="改進uC-OS II,減少內存使用量--ARMStrong.files/nologo.gif" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.arm9.net/"
target=_blank><IMG height=31 alt=友善之臂
src="改進uC-OS II,減少內存使用量--ARMStrong.files/arm9logo.gif" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.funtek.cn/"
target=_blank><IMG height=31 alt=方技電子
src="改進uC-OS II,減少內存使用量--ARMStrong.files/invalid.jpg" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.dykf.com/"
target=_blank><IMG height=31 alt=電源開發網
src="改進uC-OS II,減少內存使用量--ARMStrong.files/logo.gif" width=88
border=0></A></TD>
<TD width=88 height=31><A href="http://www.study-kit.com/index.asp"
target=_blank><IMG height=31 alt=開發板之家www.study-kit.com src=""
width=88 border=0></A></TD>
<TD width=88 height=31><A href="http://www.ednchina.com/"
target=_blank><IMG height=31 alt=EDN
src="改進uC-OS II,減少內存使用量--ARMStrong.files/edn88X31.gif" width=88
border=0 China 電子設計技術></A></TD>
<TD align=middle width=88 height=31><SPAN class=style1><A
href="http://guestbook.activepower.net/script/user/list.asp?userid=32665"
target=_blank>您的位置</A></SPAN></TD>
<TD width=88 height=31><A
href="http://www.mcublog.com/blog/user1/2268/archives/2005/"
target=_blank><IMG height=31 alt=MCU博客-中國電子工程師博客網
src="改進uC-OS II,減少內存使用量--ARMStrong.files/mcublogo.gif" width=88
border=0></A></TD>
<TD width=88
height=31> </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV>
<DIV class=space-10></DIV>
<TABLE class=gray-bar-border cellSpacing=0 cellPadding=0 width=950 border=0>
<TBODY>
<TR>
<TD class=M>文字鏈接-<A
href="http://www.mcublog.com/blog.asp?name=zhlyp">嵌入式之家</A></TD></TR></TBODY></TABLE>
<TABLE class=gray-border cellSpacing=0 cellPadding=3 width=950 border=0>
<TBODY>
<TR>
<TD colSpan=2>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=S style="PADDING-LEFT: 15px; PADDING-TOP: 5px" vAlign=bottom
align=middle><A href="http://www.cpubbs.com/"
target=_blank>CPUBBS</A> | <A href="http://www.embyte.com/"
target=_blank>微碼科技</A> | <A href="http://www.c51bbs.com/"
target=_blank>C51BBS</A> | <A href="http://www.21ic.com/"
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -