]> git.sur5r.net Git - freertos/commitdiff
Introduce configKERNEL_INTERRUPT_PRIORITY and make some performance optimisations.
authorrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Mon, 24 Mar 2008 10:17:30 +0000 (10:17 +0000)
committerrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Mon, 24 Mar 2008 10:17:30 +0000 (10:17 +0000)
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@265 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

Source/portable/RVDS/ARM_CM3/port.c
Source/portable/RVDS/ARM_CM3/portmacro.h

index e5b7f2893f5f37c43d6355193ce0dacfc4e7480a..c8d2e945a462abc311c3d04bef008674022297d4 100644 (file)
        ***************************************************************************\r
 */\r
 \r
-/*\r
-       Changes between V4.0.0 and V4.0.1\r
-\r
-       + Reduced the code used to setup the initial stack frame.\r
-       + The kernel no longer has to install or handle the fault interrupt.\r
-*/\r
-\r
 /*-----------------------------------------------------------\r
  * Implementation of functions defined in portable.h for the ARM CM3 port.\r
  *----------------------------------------------------------*/\r
 #include "FreeRTOS.h"\r
 #include "task.h"\r
 \r
+#ifndef configKERNEL_INTERRUPT_PRIORITY\r
+       #define configKERNEL_INTERRUPT_PRIORITY 255\r
+#endif\r
+\r
 /* Constants required to manipulate the NVIC. */\r
 #define portNVIC_SYSTICK_CTRL          ( ( volatile unsigned portLONG *) 0xe000e010 )\r
 #define portNVIC_SYSTICK_LOAD          ( ( volatile unsigned portLONG *) 0xe000e014 )\r
 #define portNVIC_INT_CTRL                      ( ( volatile unsigned portLONG *) 0xe000ed04 )\r
 #define portNVIC_SYSPRI2                       ( ( volatile unsigned portLONG *) 0xe000ed20 )\r
-#define portNVIC_SYSPRI1                       ( ( volatile unsigned portLONG *) 0xe000ed1c )\r
-#define portNVIC_HARD_FAULT_STATUS     0xe000ed2c\r
-#define portNVIC_FORCED_FAULT_BIT      0x40000000\r
 #define portNVIC_SYSTICK_CLK           0x00000004\r
 #define portNVIC_SYSTICK_INT           0x00000002\r
 #define portNVIC_SYSTICK_ENABLE                0x00000001\r
 #define portNVIC_PENDSVSET                     0x10000000\r
-#define portNVIC_PENDSV_PRI                    0x00ff0000\r
-#define portNVIC_SVCALL_PRI                    0xff000000\r
-#define portNVIC_SYSTICK_PRI           0xff000000\r
+#define portNVIC_PENDSV_PRI                    ( ( ( unsigned portLONG ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
+#define portNVIC_SYSTICK_PRI           ( ( ( unsigned portLONG ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
 \r
 /* Constants required to set up the initial stack. */\r
 #define portINITIAL_XPSR                       ( 0x01000000 )\r
 variable. */\r
 unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;\r
 \r
-/* Constant hardware definitions to assist asm code. */\r
-const unsigned long ulHardFaultStatus = portNVIC_HARD_FAULT_STATUS;\r
-const unsigned long ulNVICIntCtrl = ( unsigned long ) 0xe000ed04;\r
-const unsigned long ulForceFaultBit = portNVIC_FORCED_FAULT_BIT;\r
-const unsigned long ulPendSVBit = portNVIC_PENDSVSET;\r
-\r
 /* \r
  * Setup the timer to generate the tick interrupts.\r
  */\r
 static void prvSetupTimerInterrupt( void );\r
 \r
 /*\r
- * Set the MSP/PSP to a known value.\r
+ * Exception handlers.\r
  */\r
-void prvSetMSP( unsigned long ulValue );\r
-void prvSetPSP( unsigned long ulValue );\r
+void xPortPendSVHandler( void );\r
+void xPortSysTickHandler( void );\r
+void vPortSVCHandler( void );\r
+\r
+/*\r
+ * Start first task is a separate function so it can be tested in isolation.\r
+ */\r
+void vPortStartFirstTask( unsigned long ulValue );\r
 \r
 /*-----------------------------------------------------------*/\r
 \r
@@ -108,7 +101,7 @@ portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE
        pxTopOfStack--;\r
        *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
        pxTopOfStack--;\r
-       *pxTopOfStack = 0xfffffffd;     /* LR */\r
+       *pxTopOfStack = 0     /* LR */\r
        pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
        *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
        pxTopOfStack -= 9;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
@@ -118,19 +111,28 @@ portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-__asm void prvSetPSP( unsigned long ulValue )\r
+__asm void vPortSVCHandler( void )\r
 {\r
        PRESERVE8\r
-       msr psp, r0\r
-       bx lr;\r
+\r
+       ldr     r3, =pxCurrentTCB               /* Restore the context. */\r
+       ldr r1, [r3]                            /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */\r
+       ldr r0, [r1]                            /* The first item in pxCurrentTCB is the task top of stack. */\r
+       ldmia r0!, {r1, r4-r11}         /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */\r
+       ldr r2, =uxCriticalNesting      /* Restore the critical nesting count used by the task. */\r
+       str r1, [r2]                            \r
+       msr psp, r0                                     /* Restore the task stack pointer. */\r
+       orr r14, #0xd                           \r
+       bx r14                                          \r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-__asm void prvSetMSP( unsigned long ulValue )\r
+__asm void vPortStartFirstTask( unsigned long ulValue )\r
 {\r
        PRESERVE8\r
-       msr msp, r0\r
-       bx lr;\r
+\r
+       msr msp, r0                                     /* Set the msp back to the start of the stack. */\r
+       svc 0                                           /* System call to start first task. */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -139,22 +141,16 @@ __asm void prvSetMSP( unsigned long ulValue )
  */\r
 portBASE_TYPE xPortStartScheduler( void )\r
 {\r
-       /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
-       here already. */\r
-       prvSetupTimerInterrupt();\r
-\r
-       /* Make PendSV, CallSV and SysTick the lowest priority interrupts. */\r
+       /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */\r
        *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
        *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
-       *(portNVIC_SYSPRI1) |= portNVIC_SVCALL_PRI;\r
 \r
+       /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
+       here already. */\r
+       prvSetupTimerInterrupt();\r
+       \r
        /* Start the first task. */\r
-       prvSetPSP( 0 );\r
-       prvSetMSP( *((unsigned portLONG *) 0 ) );\r
-       *(portNVIC_INT_CTRL) |= portNVIC_PENDSVSET;\r
-\r
-       /* Enable interrupts */\r
-       portENABLE_INTERRUPTS();\r
+       vPortStartFirstTask( *((unsigned portLONG *) 0 ) );\r
 \r
        /* Should not get here! */\r
        return 0;\r
@@ -171,30 +167,17 @@ void vPortEndScheduler( void )
 void vPortYieldFromISR( void )\r
 {\r
        /* Set a PendSV to request a context switch. */\r
-       *(portNVIC_INT_CTRL) |= portNVIC_PENDSVSET;     \r
-       portENABLE_INTERRUPTS();\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-__asm void vPortDisableInterrupts( void )\r
-{\r
-       PRESERVE8\r
-       cpsid i;\r
-       bx lr;\r
-}\r
-/*-----------------------------------------------------------*/\r
+       *(portNVIC_INT_CTRL) |= portNVIC_PENDSVSET;\r
 \r
-__asm void vPortEnableInterrupts( void )\r
-{\r
-       PRESERVE8\r
-       cpsie i;\r
-       bx lr;\r
+       /* This function is also called in response to a Yield(), so we want\r
+       the yield to occur immediately. */\r
+       portENABLE_INTERRUPTS();\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 void vPortEnterCritical( void )\r
 {\r
-       vPortDisableInterrupts();\r
+       portDISABLE_INTERRUPTS();\r
        uxCriticalNesting++;\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -204,7 +187,7 @@ void vPortExitCritical( void )
        uxCriticalNesting--;\r
        if( uxCriticalNesting == 0 )\r
        {\r
-               vPortEnableInterrupts();\r
+               portENABLE_INTERRUPTS();\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -217,81 +200,46 @@ __asm void xPortPendSVHandler( void )
 \r
        PRESERVE8\r
 \r
-       /* Start first task if the stack has not yet been setup. */\r
-       mrs r0, psp                                             \r
-       cbz r0, no_save                                 \r
+       mrs r0, psp                                              \r
 \r
-       /* Save the context into the TCB. */\r
-       stmdb r0!, {r4-r11}                             \r
-       sub r0, r0, #0x04                                       \r
-       ldr r1, =uxCriticalNesting\r
-       ldr r2, =pxCurrentTCB           \r
+       ldr     r3, =pxCurrentTCB                       /* Get the location of the current TCB. */\r
+       ldr     r2, [r3]                                                \r
+\r
+       ldr r1, =uxCriticalNesting              /* Save the remaining registers and the critical nesting count onto the task stack. */\r
        ldr r1, [r1]                                    \r
-       ldr r2, [r2]                                    \r
-       str r1, [r0]                                    \r
-       str r0, [r2]                                    \r
+       stmdb r0!, {r1,r4-r11}                  \r
+       str r0, [r2]                                     /* Save the new top of stack into the first member of the TCB. */\r
 \r
-no_save;\r
-       \r
-       /* Find the task to execute. */\r
-       push {r14}\r
-       cpsid i\r
-       bl vTaskSwitchContext\r
-       cpsie i\r
-       pop {r14}       \r
-\r
-       /* Restore the context. */\r
-       ldr r1, =pxCurrentTCB\r
-       ldr r1, [r1];\r
-       ldr r0, [r1];\r
-       ldmia r0!, {r1, r4-r11}\r
+       stmdb sp!, {r3, r14}                     \r
+       bl vTaskSwitchContext                   \r
+       ldmia sp!, {r3, r14}                    \r
+\r
+       ldr r1, [r3]                                     \r
        ldr r2, =uxCriticalNesting\r
-       str r1, [r2]\r
-       ldr r2, [r2]\r
-       msr psp, r0\r
-       orr r14, #0xd\r
+       ldr r0, [r1]                                     /* The first item in pxCurrentTCB is the task top of stack. */\r
+       ldmia r0!, {r1, r4-r11}                  /* Pop the registers and the critical nesting count. */\r
+       str r1, [r2]                                     /* Save the new critical nesting value into ulCriticalNesting. */ \r
+       msr psp, r0                                              \r
+       orr r14, #0xd                                   \r
 \r
-       /* Exit with interrupts in the state required by the task. */\r
-       cbnz r2, sv_disable_interrupts\r
-       \r
+       cbnz r1, sv_disable_interrupts   /* If the nesting count is greater than 0 we need to exit with interrupts masked. */\r
        bx r14\r
 \r
-sv_disable_interrupts;\r
-       cpsid i\r
-       bx r14\r
+sv_disable_interrupts\r
+       mov r1, #configKERNEL_INTERRUPT_PRIORITY\r
+       msr     basepri, r1                                     \r
+       bx r14                                                  \r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-__asm void xPortSysTickHandler( void )\r
+void xPortSysTickHandler( void )\r
 {\r
-       extern vTaskIncrementTick\r
-       PRESERVE8\r
-\r
-       /* Call the scheduler tick function. */\r
-       push {r14}\r
-       cpsid i\r
-       bl vTaskIncrementTick\r
-       cpsie i\r
-       pop {r14}       \r
-\r
+       vTaskIncrementTick();\r
+       \r
        /* If using preemption, also force a context switch. */\r
        #if configUSE_PREEMPTION == 1\r
-       extern vPortYieldFromISR\r
-               push {r14}\r
-               bl vPortYieldFromISR\r
-               pop {r14}\r
+               *(portNVIC_INT_CTRL) |= portNVIC_PENDSVSET;     \r
        #endif\r
-\r
-       /* Exit with interrupts in the correct state. */\r
-       ldr r2, =uxCriticalNesting\r
-       ldr r2, [r2]\r
-       cbnz r2, tick_disable_interrupts\r
-\r
-       bx r14\r
-\r
-tick_disable_interrupts;\r
-       cpsid i\r
-       bx r14\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -305,5 +253,26 @@ void prvSetupTimerInterrupt( void )
        *(portNVIC_SYSTICK_LOAD) = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
        *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
 }\r
+/*-----------------------------------------------------------*/\r
 \r
+__asm void vPortSetInterruptMask( void )\r
+{\r
+       PRESERVE8\r
 \r
+       push { r0 }\r
+       mov r0, #configKERNEL_INTERRUPT_PRIORITY\r
+       msr basepri, r0\r
+       pop { r0 }\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+__asm void vPortClearInterruptMask( void )\r
+{\r
+       PRESERVE8\r
+\r
+       push { r0 }\r
+       mov r0, #0\r
+       msr basepri, r0\r
+       pop { r0 }\r
+}\r
index 2a1a9d07133022a80620a1af9f54f8d6714dd22a..fa3b747a64fb32014e5785561590bb91aa0cd66b 100644 (file)
@@ -94,13 +94,13 @@ extern void vPortYieldFromISR( void );
 \r
 /* Critical section management. */\r
 \r
-extern void vPortDisableInterrupts( void );\r
-extern void vPortEnableInterrupts( void );\r
+extern void vPortSetInterruptMask( void );\r
+extern void vPortClearInterruptMask( void );\r
 extern void vPortEnterCritical( void );\r
 extern void vPortExitCritical( void );\r
 \r
-#define portDISABLE_INTERRUPTS()       vPortDisableInterrupts()\r
-#define portENABLE_INTERRUPTS()                vPortEnableInterrupts()\r
+#define portDISABLE_INTERRUPTS()       vPortSetInterruptMask()\r
+#define portENABLE_INTERRUPTS()                vPortClearInterruptMask()\r
 #define portENTER_CRITICAL()           vPortEnterCritical()\r
 #define portEXIT_CRITICAL()                    vPortExitCritical()\r
 /*-----------------------------------------------------------*/\r