您的位置 首页 硬件

GNU ARM汇编–(五)中止汇编之嵌套中止处理

在上篇《GNUARM汇编–(四)中断汇编之非嵌套中断处理》中分析了最简单的中断处理的写法,再看TQ2440启动代码中的中断向量表的写法

在上篇《GNU ARM汇编–(四)中止汇编之非嵌套中止处理》中剖析了最简略的中止处理的写法,再看TQ2440发动代码中的中止向量表的写法就一望而知了.今日抽时间对嵌套中止处理的学习做下收拾.

嵌套中止处理的中心代码如下:

[cpp]view plaincopy

  1. ;/*
  2. ;*____________________________________________________________________
  3. ;*
  4. ;*Copyright(c)2004,AndrewN.Sloss,ChrisWrightandDominicSymes
  5. ;*Allrightsreserved.
  6. ;*____________________________________________________________________
  7. ;*
  8. ;*NON-COMMERCIALUSELicense
  9. ;*
  10. ;*Redistributionanduseinsourceandbinaryforms,withorwithout
  11. ;*modification,arepermittedprovidedthatthefollowingconditions
  12. ;*aremet:
  13. ;*
  14. ;*1.ForNON-COMMERCIALUSEonly.
  15. ;*
  16. ;*2.Redistributionsofsourcecodemustretaintheabovecopyright
  17. ;*notice,thislistofconditionsandthefollowingdisclaimer.
  18. ;*
  19. ;*3.Redistributionsinbinaryformmustreproducetheabove
  20. ;*copyrightnotice,thislistofconditionsandthefollowing
  21. ;*disclaimerinthedocumentationand/orothermaterialsprovided
  22. ;*withthedistribution.
  23. ;*
  24. ;*4.Alladvertisingmaterialsmentioningfeaturesoruseofthis
  25. ;*softwaremustdisplaythefollowingacknowledgement:
  26. ;*
  27. ;*ThisproductincludessoftwaredevelopedbyAndrewN.Sloss,
  28. ;*ChrisWrightandDominicSymes.
  29. ;*
  30. ;*THISSOFTWAREISPROVIDEDBYTHECONTRIBUTORS“ASISANDANY
  31. ;*EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THE
  32. ;*IMPLIEDWARRANTIESOFMERCHANTABILITYANDFITNESSFORAPARTICULAR
  33. ;*PURPOSEAREDISCLAIMED.INNOEVENTSHALLTHECONTRIBUTORSBE
  34. ;*LIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,SPECIAL,EXEMPLARY,
  35. ;*ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,
  36. ;*PROCUREMENTOFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,
  37. ;*ORPROFITS;ORBUSINESSINTERRUPTION)HOWEVERCAUSEDANDONANY
  38. ;*THEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,OR
  39. ;*TORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUT
  40. ;*OFTHEUSEOFTHISSOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITY
  41. ;*OFSUCHDAMAGE.
  42. ;*
  43. ;*Ifyouhavequestionsaboutthislicenseorwouldlikeadifferent
  44. ;*licensepleaseemail:
  45. ;*
  46. ;*andrew@sloss.net
  47. ;*
  48. ;*
  49. ;*/
  50. ;/***********************************************************************
  51. ;*
  52. ;*Module:nih9_9.s
  53. ;*Descriptions:NestedInterruptHandler
  54. ;*Example:9.9
  55. ;*OS:generic
  56. ;*Platform:generic
  57. ;*History:
  58. ;*
  59. ;*31thDecember2003
  60. ;*-addedheader
  61. ;*
  62. ;***********************************************************************/
  63. EXPORTnestedInterruptHandler
  64. MaskmdEQU0x1f;processormodemask
  65. SVC32mdEQU0x13;SVCmode
  66. I_BitEQU0x80;IRQbit
  67. FRAME_R0EQU0x00
  68. FRAME_R1EQUFRAME_R0+4
  69. FRAME_R2EQUFRAME_R1+4
  70. FRAME_R3EQUFRAME_R2+4
  71. FRAME_R4EQUFRAME_R3+4
  72. FRAME_R5EQUFRAME_R4+4
  73. FRAME_R6EQUFRAME_R5+4
  74. FRAME_R7EQUFRAME_R6+4
  75. FRAME_R8EQUFRAME_R7+4
  76. FRAME_R9EQUFRAME_R8+4
  77. FRAME_R10EQUFRAME_R9+4
  78. FRAME_R11EQUFRAME_R10+4
  79. FRAME_R12EQUFRAME_R11+4
  80. FRAME_PSREQUFRAME_R12+4
  81. FRAME_LREQUFRAME_PSR+4
  82. FRAME_PCEQUFRAME_LR+4
  83. FRAME_SIZEEQUFRAME_PC+4
  84. AREAnih9_9,CODE,readonly
  85. nestedInterruptHandler;instructionstate:comment
  86. SUBr14,r14,#4;2:
  87. STMDBr13!,{r0-r3,r12,r14};2:savecontext
  88. ;
  89. BLread_RescheduleFlag;3:moreprocessing
  90. CMPr0,#0;3:ifprocessing?
  91. LDMNEIAr13!,{r0-r3,r12,pc}^;4:thenreturn
  92. MRSr2,SPSR;5:copySPSR_irq
  93. MOVr0,r13;5:copyr13_irq
  94. ADDr13,r13,#6*4;5:resetstack
  95. MRSr1,CPSR;6:copyCPSR
  96. BICr1,r1,#Maskmd;6:
  97. ORRr1,r1,#SVC32md;6:
  98. MSRCPSR_c,r1;6:changeSVCmode
  99. SUBr13,r13,#FRAME_SIZE-FRAME_R4;7:makestackspace
  100. STMIAr13,{r4-r11};7:saver4-r11
  101. LDMIAr0,{r4-r9};7:r4-r9IRQstack
  102. BICr1,r1,#I_Bit;8:
  103. MSRCPSR_c,r1;8:enableint
  104. STMDBr13!,{r4-r7};9:saver4-r7SVC
  105. STRr2,[r13,#FRAME_PSR];9:savePSR
  106. STRr8,[r13,#FRAME_R12];9:saver12
  107. STRr9,[r13,#FRAME_PC];9:savepc
  108. STRr14,[r13,#FRAME_LR];9:savelr
  109. ;
  110. LDMIAr13!,{r0-r12,r14};11:restorecontext
  111. MSRSPSR_cxsf,r14;11:restoreSPSR
  112. LDMIAr13!,{r14,pc}^;11:return
  113. read_RescheduleFlag
  114. ;
  115. MOVr0,#0;moreprocessing
  116. MOVpc,r14;return
  117. END

代码的要害便是在中止后切换到SVC形式下,使用svc mode的stack来完成中止嵌套进程的备份以及康复操作.从代码中能够看到,从R0到PC都在栈中有备份,这儿咱们叫栈帧.记住《深化了解计算机体系》一书在讲x86汇编的函数调用时也是栈帧的概念.这点上中止嵌套和函数调用有相似之处.有了这个栈帧,使用压栈出栈操作就全部ok了.

刚看这个代码,对有个当地有疑问,便是觉得中止开早了:

BIC r1,r1,#I_Bit ; 8 :
MSR CPSR_c,r1 ; 8 : enable int
STMDB r13!,{r4-r7} ; 9 : save r4-r7 SVC
STR r2,[r13,#FRAME_PSR] ; 9 : save PSR
STR r8,[r13,#FRAME_R12] ; 9 : save r12
STR r9,[r13,#FRAME_PC] ; 9 : save pc
STR r14,[r13,#FRAME_LR] ; 9 : save lr

觉得开中止的代码应该放在后边,这样才干确保svc mode下的stack frame不会被损坏.但在草稿纸上画一下irq和svc下的stack图,就发现仓库操作并没有问题.能够假定刚开中止立马就有新的中止了,r4-r7 r8 r9都有在STMIA r13,{r4-r11} 中保存到svc的stack中,LDMIA r0,{r4-r9} 和STMDB r13!,{r4-r7} 确保了开始的r0-r3在栈中,而LDMIA r0,{r4-r9}和STR r8,[r13,#FRAME_R12] 以及STR r9,[r13,#FRAME_PC] 确保了R12和PC,确保正确回来.(这儿的r9装的是r14_irq,所以pc便是r14_irq,这样就确保了从中止服务例程中回来).至于STR r14,[r13,#FRAME_LR]中的r14是r14_svc,将其压入svc的stack中,中止例程用bl就不会呈现错误了,在最终LDMIA r13!,{r14,pc}^ 中r14得到康复.而r2保存的是spsr,也便是svc形式的状况,一向不变,不必忧虑会被掩盖.

最终,再看了一遍图,觉得r10和r11的帧能够省去,由于r4-r9是用来存atpcs的r0-r3,r12,r14,而r10和r11用不到.形似能够省点空间和时间,详细的待会试验一下.

下面给出实践的嵌套中止处理,使用r10来保存INTOFFSET的值,依据该值来断定是什么中止,然后做不同的处理.详细的作用是:代码会做流水灯的动作,Key1代表INT1,中止处理动作是4个灯全全亮然后全灭,Key4代表代表INT0,中止处理动作是第一个灯和第三个灯亮,然后第二个灯和第四个灯亮.

[cpp]view plaincopy

  1. /*
  2. simpleinterruption
  3. copyleft@dndxhej@gmail.com
  4. */
  5. .equMaskmd,0x1f@processormodemask
  6. .equSVC32md,0x13@SVCmode
  7. .equI_Bit,0x80@IRQbit
  8. .equFRAME_R0,0x00
  9. .equFRAME_R1,FRAME_R0+4
  10. .equFRAME_R2,FRAME_R1+4
  11. .equFRAME_R3,FRAME_R2+4
  12. .equFRAME_R4,FRAME_R3+4
  13. .equFRAME_R5,FRAME_R4+4
  14. .equFRAME_R6,FRAME_R5+4
  15. .equFRAME_R7,FRAME_R6+4
  16. .equFRAME_R8,FRAME_R7+4
  17. .equFRAME_R9,FRAME_R8+4
  18. .equFRAME_R10,FRAME_R9+4
  19. .equFRAME_R11,FRAME_R10+4
  20. .equFRAME_R12,FRAME_R11+4
  21. .equFRAME_PSR,FRAME_R12+4
  22. .equFRAME_LR,FRAME_PSR+4
  23. .equFRAME_PC,FRAME_LR+4
  24. .equFRAME_SIZE,FRAME_PC+4
  25. .equNOINT,0xc0
  26. .equWTCON,0x53000000
  27. .equGPBCON,0x56000010@led
  28. .equGPBDAT,0x56000014@led
  29. .equGPBUP,0x56000018@led
  30. .equGPFCON,0x56000050@interruptconfig
  31. .equEINTMASK,0x560000a4
  32. .equEXTINT0,0x56000088
  33. .equEXTINT1,0x5600008c
  34. .equEXTINT2,0x56000090
  35. .equINTMSK,0x4A000008
  36. .equEINTPEND,0x560000a8
  37. .equINTSUBMSK,0X4A00001C
  38. .equSRCPND,0X4A000000
  39. .equINTPND,0X4A000010
  40. .equINTOFFSET,0x4A000014
  41. .global_start
  42. _start:breset
  43. ldrpc,_undefined_instruction
  44. ldrpc,_software_interrupt
  45. ldrpc,_prefetch_abort
  46. ldrpc,_data_abort
  47. ldrpc,_not_used
  48. @birq
  49. ldrpc,_irq
  50. ldrpc,_fiq
  51. _undefined_instruction:.wordundefined_instruction
  52. _software_interrupt:.wordsoftware_interrupt
  53. _prefetch_abort:.wordprefetch_abort
  54. _data_abort:.worddata_abort
  55. _not_used:.wordnot_used
  56. _irq:.wordirq
  57. _fiq:.wordfiq
  58. .balignl16,0xdeadbeef
  59. reset:
  60. ldrr3,=WTCON
  61. movr4,#0x0
  62. strr4,[r3]@disablewatchdog
  63. ldrr0,=GPBCON
  64. ldrr1,=0x15400
  65. strr1,[r0]
  66. ldrr2,=GPBDAT
  67. ldrr1,=0x160
  68. strr1,[r2]
  69. bldelay
  70. msrcpsr_c,#0xd2@进入中止形式
  71. ldrsp,=0xc00@中止形式的栈指针界说
  72. msrcpsr_c,#0xd3@进入svc形式
  73. ldrsp,=0xfff@设置svc形式的栈指针
  74. @——————————————–
  75. ldrr0,=GPBUP
  76. ldrr1,=0x03f0
  77. strr1,[r0]

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/yingjian/258060.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部