ÈÎÎñÇл»
Ê×ÏÈÈÎÎñÇл»ÒªÊµÏÖÁ½¸ö×î»ù±¾µÄÄ¿µÄ£º
1.±£´æµ±Ç°ÈÎÎñµÄ¶Ïµã£¨ÈÎÎñ·¢ÉúÇл»µÄʱºòµÄÏÂÒ»Ìõ´úÂëµØÖ·£©¡¢±£´æ·¢ÉúÈÎÎñÇл»µÄCPU״̬£¨Ò²¾ÍÊÇCPUµÄ¼Ä´æÆ÷£©¡£
2.»Ö¸´ÒªÇл»µ½µÄÈÎÎñÔÚÉÏÒ»´Î±»Çл»³öÈ¥µÄʱºòµÄCPU״̬£¬ÒÔ¼°¶ÏµãµÄPCÖµ¡£
ÈçÏÂͼËùʾ
ͼ1
Õâ¸ö¹ý³ÌÊÇÓÉ»ã±àʵÏֵĴúÂëÈçÏÂ×¢ÒâºìÉ«×ÖÌå
IMPORT OSRunning ; External references IMPORT OSPrioCur IMPORT OSPrioHighRdy IMPORT OSTCBCur IMPORT OSTCBHighRdy IMPORT OSIntNesting IMPORT OSIntExit IMPORT OSTaskSwHook EXPORT OSStartHighRdy EXPORT OSCtxSw EXPORT OSIntCtxSw ;ÕâЩº¯ÊýÃû»òÕßÊDZäÁ¿Ãû¶¼ÊÇÔÚCÎļþÖж¨ÒåµÄ EXPORT OS_CPU_SR_Save ; Functions declared in this file EXPORT OS_CPU_SR_Restore EXPORT PendSV_Handler NVIC_INT_CTRL EQU 0xE000ED04 ; ÖжϿØÖƼĴæÆ÷ NVIC_SYSPRI2 EQU 0xE000ED22 ; ϵͳÓÅÏȼ¶¼Ä´æÆ÷(2) NVIC_PENDSV_PRI EQU 0xFFFF ; PendSVÖжϺÍϵͳ½ÚÅÄÖÐ¶Ï ; (¶¼Îª×îµÍ£¬0xff). NVIC_PENDSVSET EQU 0x10000000 ; ´¥·¢Èí¼þÖжϵÄÖµ. //ÕâÀïÊÇOSÆô¶¯µÄʱºòÖ´ÐеĵÚÒ»¸öÈÎÎñ OSStartHighRdy LDR R4, =NVIC_SYSPRI2 ; set the PendSV exception priority LDR R5, =NVIC_PENDSV_PRI STR R5, [R4] MOV R4, #0 ; set the PSP to 0 for initial context switch call MSR PSP, R4 LDR R4, =OSRunning ; OSRunning = TRUE MOV R5, #1 STRB R5, [R4] ;Çл»µ½×î¸ßÓÅÏȼ¶µÄÈÎÎñ LDR R4, =NVIC_INT_CTRL ;¹ÒÆðÒ»¸ö PendSV Öжϣ¬ÔÚËüµÄÖжϷþÎñº¯ÊýÀïÃæÖ´ÐÐÈÎÎñÇл» LDR R5, =NVIC_PENDSVSET STR R5, [R4] CPSIE I ;enable interrupts at processor level OSStartHang B OSStartHang ;should never get here //PendSV_Handler µÄÖжϷþÎñº¯ÊýÈÎÎñÇл»ÔÚÕâÀï·¢Éú PendSV_Handler CPSID I ; ¹Ø±ÕÖжϣ¬ÒòΪÔÚÈÎÎñÇл»¹ý³ÌÖв»ÔÊÐí±»´ò¶Ï MRS R0, PSP ; ÅжÏÊÇ·ñÐèÒª±£´æµ±Ç°CPUµÄ״̬£¬Èç¹û²»ÊÇ0´ú±í²»ÊǵÚÎñÔËÐÐÒ²¾ÍÊÇ˵Ҫ±£´æ´ËʱµÄCPU״̬ CBZ R0, PendSV_Handler_Nosave ; ÕâÀï´ú±í×Ų»ÐèÒª±£´æµ±Ç°CPU״̬£¬Ö±½ÓÖ´ÐÐÏÂÒ»¸ö²½Öè TST R14, #0x10 ;ÅжÏÊÇ·ñʹÓÃÁ˸¡µãµ¥ÔªÈç¹ûʹÓÃÁËÒª±£´æ¸¡µãµ¥ÔªµÄ¼Ä´æÆ÷ IT EQ VSTMDBEQ R0!, {S16-S31} ;ÕâÀïÊDZ£´æ¸¡µãµ¥ÔªµÄÏà¹Ø¼Ä´æÆ÷ SUBS R0, R0, #0x20 ; Save remaining regs r4-11 on process stack STM R0, {R4-R11} ;ÕâÀï±£´æCPUµÄ¼Ä´æÆ÷£¬ÓÐÒ»²¿·ÖÒѾÔÚ·¢ÉúÖжϵÄʱºòÓÉÓ²¼þ×Ô¶¯±£´æ£¬ÕâÀï±£´æµÄÊÇÊ£ÓàµÄ²¿·Ö //ÕâÀ↑ʼ±£´æ×¼±¸ÒªÇгöÈ¥µÄÈÎÎñÒ²¾ÍÊǵ±Ç°ÈÎÎñµÄÕ»Ö¸Õ룬ºÃÈÃÏ´ÎÇлØÀ´µÄʱºò¿ÉÒÔÀûÓÃÕ»Ö¸Õë»Ö¸´CPU״̬¡£ÈçÉÏͼÖТٵĹý³Ì LDR R1, =OSTCBCur ; OSTCBCur->OSTCBStkPtr = SP£¬ÈÎÎñ¿ØÖÆ¿éµÄÊ×µØÖ·£¬Ò²ÊÇÈÎÎñÕ»Ö¸ÕëµØÖ· LDR R1, [R1] STR R0, [R1] ; ÕâÀï¾ÍÊDZ£´æSPµ½OSTCBCur->OSTCBStkPtr ; At this point, entire context of process has been saved PendSV_Handler_Nosave PUSH {R14} ; Save LR exc_return value LDR R0, =OSTaskSwHook ; OSTaskSwHook(); BLX R0 POP {R14} ;ÕâÀïµÄ4¾äÊÇÖ´ÐÐÈÎÎñ¹³×Óº¯Êý¿ÉÒÔÏȲ»ÓÃÀí LDR R0, =OSPrioCur ; OSPrioCur = OSPrioHighRdy; LDR R1, =OSPrioHighRdy LDRB R2, [R1] STRB R2, [R0] ;ÕâÀïÊǽ«×î¸ßÓÅÏȼ¶¸³Öµ¸øµ±¡°Ç°ÓÅÏȼ¶¡±×¢ÒâÒýºÅÏÂÃæ»á½âÊÍ LDR R0, =OSTCBCur ; OSTCBCur = OSTCBHighRdy; LDR R1, =OSTCBHighRdy LDR R2, [R1] STR R2, [R0] ;ÕâÀïÊǽ«×î¸ßÓÅÏÈÈÎÎñ¿ØÖƿ鼶¸³Öµ¸øµ±¡°Ç°ÓÅÏȼ¶ÈÎÎñ¿ØÖƿ顱עÒâÕâÀïµÄÒýºÅ£¬¾¹ýÕâ8ÌõÓï¾ä£¬¼´½«ÒªÖ´ÐеÄÈÎÎñ±ä³ÉÁ˵±Ç°ÈÎÎñ£¬¶ø±»Çл»³öÈ¥µÄÈÎÎñ´ÓÏÖÔÚ¿ªÊ¼²»ÔÙÊǵ±Ç°ÈÎÎñ£¬ÄÄÅÂËüÏÖÔÚ»¹Ã»ÓÐÕæÕýµÄÇл»³öÈ¥¡£ LDR R0, [R2] ; R0 is new process SP; SP = OSTCBHighRdy->OSTCBStkPtr; LDM R0, {R4-R11} ; Restore r4-11 from new process stack ADDS R0, R0, #0x20 ;ÕâÀ↑ʼ»Ö¸´ÈÎÎñ2Éϴα»ÇгöÈ¥µÄʱºò±£´æµÄCPU״̬£¬Ò²¾ÍÊÇÉÏͼÖеĹý³Ì¢Ú ;Is the task using the FPU context? If so, push high vfp registers. TST R14, #0x10 IT EQ VLDMIAEQ R0!, {S16-S31} ;Õâ3¾äºÍÇ°ÃæµÄÒ»ÑùÅжÏÓÐûÓÐʹÓø¡µãµ¥Ôª£¬ÓоÍÒª»Ö¸´Ïà¹Ø¼Ä´æÆ÷µÄÖµ MSR PSP, R0 ; Õâ¾ä·Ç³£¹Ø¼ü,ÕâÀïÊǽ«ÈÎÎñ2Éϴα»ÇгöÈ¥µÄʱºòÓ²¼þ×Ô¶¯±£´æµÄCPU״̬£¬ÔÚÈÎÎñ2µÄÕ»ÀïÃæµÄ×îºó±£´æCPU¼Ä´æÆ÷µÄµØÖ·¸³Öµ¸øÄں˵ÄÕ»Ö¸ÕëPSP,¶ø½ÓÏÂÁ˵ÄÖжϷµ»ØÖ¸Áî»áÓÉÓ²¼þ×Ô¶¯µÄ½«ÕâЩ¼Ä´æÆ÷µÄÖµ¡°»¹Ôµ½CPUÖС±£¬Ò²¾ÍÊÇÉÏͼµÄ¹ý³Ì¢Û¡£×¢ÒâÕâÀïµÄ¡°×Ô¶¯»¹Ô¡±¸ÕºÃºÍÈÎÎñ2Éϴα»ÇгöÈ¥µÄ¡°×Ô¶¯±£´æÏà¶ÔÓ¦¡±¡£¶øÕⲿ·Ö¡°»¹Ô¡±µÄ¼Ä´æÆ÷ÖоͰüº¬ÁËÈÎÎñ2Éϴα»ÇгöȥʱºòµÄPCÖµ£¬Ò²¾ÍÊǶϵ㡣ÕâÒ²ÊÇΪʲôÖжϷµ»Øºó¿ÉÒԻص½ÈÎÎñ2֮ǰµÄ¶Ïµã¼ÌÐøÖ´ÐеÄÔÒò¡£ ORR LR, LR, #0x04 ; Ensure exception return uses process stack CPSIE I BX LR ; Õâ3¾ä±íʾÍ˳öÖжϵÄʱºòʹÓõÄÊǽø³ÌÕ»Ò²¾ÍÊÇPSP£¬¿ªÆôÖжϣ¬²¢ÇÒÖ´ÐÐÖжϷµ»Ø¡£Ò²¾ÍÊÇÔÚÕâÀïÕæÕýµÄÇлص½ÈÎÎñ2È¥Ö´ÐÐÁË¡£ NOP end |
ºÃÁ˵½ÕâÀïÈÎÎñÇл»¾ÍÍê³ÉÁË£¬¾ÍÊÇÕâô¼òµ¥£¬ÄãûÓп´´í¾ÍÊÇÕâô¶þÈýÊ®ÐдúÂë¾Í¸ã¶¨ÁË¡£
ÈÎÎñÇл»¿ÉÒÔ˵ÊDzÙ×÷ϵͳµÄºËÐÄÖеĺËÐÄ£¬±»Ö´ÐÐƵÂÊ×î¸ßµÄ´úÂ룬ÒòΪÿһ¸öϵͳ½ÚÅIJÙ×÷ϵͳ¶¼ÒªÖ´ÐÐÒ»´ÎÈÎÎñµ÷¶È¡£
¶øÔÚÈÎÎñµ÷¶ÈÀïÃæ¾ÍÊÇÕÒ³ö¾ÍÐ÷ÈÎÎñÖÐÓÅÏȼ¶×î¸ßµÄ£¬È»ºóÖ´ÐÐÈÎÎñÇл»»òÕß˵ÉÏÏÂÎÄÇл»¡£