]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/portable/GCC/ARM_CM4F/port.c
Refine the default tickless idle implementation in the Cortex-M3 port layers.
[freertos] / FreeRTOS / Source / portable / GCC / ARM_CM4F / port.c
index 23e194bbe11f1b7488f7280a4f6abf1ffd0a0b8d..f3826b1df4a7056473e57f6edf9b2f544149e62c 100644 (file)
 #define portNVIC_PENDSVCLEAR_BIT                       ( 1UL << 27UL )\r
 #define portNVIC_PEND_SYSTICK_CLEAR_BIT                ( 1UL << 25UL )\r
 \r
-#define portNVIC_PENDSV_PRI                            ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )\r
-#define portNVIC_SYSTICK_PRI                   ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )\r
+#define portNVIC_PENDSV_PRI                                    ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )\r
+#define portNVIC_SYSTICK_PRI                           ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )\r
 \r
 /* Constants required to manipulate the VFP. */\r
 #define portFPCCR                                      ( ( volatile unsigned long * ) 0xe000ef34 ) /* Floating point context control register. */\r
 #define portINITIAL_XPSR                       ( 0x01000000 )\r
 #define portINITIAL_EXEC_RETURN                ( 0xfffffffd )\r
 \r
-/* The priority used by the kernel is assigned to a variable to make access\r
-from inline assembler easier. */\r
-const unsigned long ulKernelPriority = configKERNEL_INTERRUPT_PRIORITY;\r
+/* The systick is a 24-bit counter. */\r
+#define portMAX_24_BIT_NUMBER                          ( 0xffffffUL )\r
+\r
+/* A fiddle factor to estimate the number of SysTick counts that would have\r
+occurred while the SysTick counter is stopped during tickless idle\r
+calculations. */\r
+#define portMISSED_COUNTS_FACTOR                       ( 45UL )\r
 \r
 /* Each task maintains its own interrupt status in the critical nesting\r
 variable. */\r
@@ -150,8 +154,8 @@ static void prvPortStartFirstTask( void ) __attribute__ (( naked ));
  * The number of SysTick increments that make up one tick period.\r
  */\r
 #if configUSE_TICKLESS_IDLE == 1\r
-       static unsigned long ulTimerReloadValueForOneTick = 0;\r
-#endif\r
+       static unsigned long ulTimerCountsForOneTick = 0;\r
+#endif /* configUSE_TICKLESS_IDLE */\r
 \r
 /*\r
  * The maximum number of tick periods that can be suppressed is limited by the\r
@@ -245,7 +249,7 @@ portBASE_TYPE xPortStartScheduler( void )
        See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
        configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
 \r
-       /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */\r
+       /* Make PendSV and SysTick the lowest priority interrupts. */\r
        portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI;\r
        portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI;\r
 \r
@@ -387,20 +391,17 @@ void xPortPendSVHandler( void )
 \r
 void xPortSysTickHandler( void )\r
 {\r
-       /* Only reset the systick load register if configUSE_TICKLESS_IDLE is set to\r
-       1.  If it is set to 0 tickless idle is not being used.  If it is set to a\r
-       value other than 0 or 1 then a timer other than the SysTick is being used\r
-       to generate the tick interrupt. */\r
-       #if configUSE_TICKLESS_IDLE == 1\r
-               portNVIC_SYSTICK_LOAD_REG = ulTimerReloadValueForOneTick;\r
-       #endif\r
-\r
+       /* The SysTick runs at the lowest interrupt priority, so when this interrupt\r
+       executes all interrupts must be unmasked.  There is therefore no need to\r
+       save and then restore the interrupt mask value as its value is already\r
+       known. */\r
        ( void ) portSET_INTERRUPT_MASK_FROM_ISR();\r
        {\r
-               /* Increment the RTOS tick count. */\r
+               /* Increment the RTOS tick. */\r
                if( xTaskIncrementTick() != pdFALSE )\r
                {\r
-                       /* Pend a context switch. */\r
+                       /* A context switch is required.  Context switching is performed in\r
+                       the PendSV interrupt.  Pend the PendSV interrupt. */\r
                        portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
                }\r
        }\r
@@ -412,7 +413,7 @@ void xPortSysTickHandler( void )
 \r
        __attribute__((weak)) void vPortSuppressTicksAndSleep( portTickType xExpectedIdleTime )\r
        {\r
-       unsigned long ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickIncrements;\r
+       unsigned long ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;\r
        portTickType xModifiableIdleTime;\r
 \r
                /* Make sure the SysTick reload value does not overflow the counter. */\r
@@ -421,25 +422,20 @@ void xPortSysTickHandler( void )
                        xExpectedIdleTime = xMaximumPossibleSuppressedTicks;\r
                }\r
 \r
-               /* Calculate the reload value required to wait xExpectedIdleTime\r
-               tick periods.  -1 is used because this code will execute part way\r
-               through one of the tick periods, and the fraction of a tick period is\r
-               accounted for later. */\r
-               ulReloadValue = ( ulTimerReloadValueForOneTick * ( xExpectedIdleTime - 1UL ) );\r
-               if( ulReloadValue > ulStoppedTimerCompensation )\r
-               {\r
-                       ulReloadValue -= ulStoppedTimerCompensation;\r
-               }\r
-\r
                /* Stop the SysTick momentarily.  The time the SysTick is stopped for\r
                is accounted for as best it can be, but using the tickless mode will\r
                inevitably result in some tiny drift of the time maintained by the\r
                kernel with respect to calendar time. */\r
                portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT;\r
 \r
-               /* Adjust the reload value to take into account that the current time\r
-               slice is already partially complete. */\r
-               ulReloadValue += ( portNVIC_SYSTICK_LOAD_REG - ( portNVIC_SYSTICK_LOAD_REG - portNVIC_SYSTICK_CURRENT_VALUE_REG ) );\r
+               /* Calculate the reload value required to wait xExpectedIdleTime\r
+               tick periods.  -1 is used because this code will execute part way\r
+               through one of the tick periods. */\r
+               ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );\r
+               if( ulReloadValue > ulStoppedTimerCompensation )\r
+               {\r
+                       ulReloadValue -= ulStoppedTimerCompensation;\r
+               }\r
 \r
                /* Enter a critical section but don't use the taskENTER_CRITICAL()\r
                method as that will mask interrupts that should exit sleep mode. */\r
@@ -496,10 +492,10 @@ void xPortSysTickHandler( void )
                        if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )\r
                        {\r
                                /* The tick interrupt has already executed, and the SysTick\r
-                               count reloaded with the portNVIC_SYSTICK_LOAD_REG value.\r
-                               Reset the portNVIC_SYSTICK_LOAD_REG with whatever remains of\r
-                               this tick period. */\r
-                               portNVIC_SYSTICK_LOAD_REG = ulTimerReloadValueForOneTick - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );\r
+                               count reloaded with ulReloadValue.  Reset the\r
+                               portNVIC_SYSTICK_LOAD_REG with whatever remains of this tick\r
+                               period. */\r
+                               portNVIC_SYSTICK_LOAD_REG = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );\r
 \r
                                /* The tick interrupt handler will already have pended the tick\r
                                processing in the kernel.  As the pending tick will be\r
@@ -511,16 +507,18 @@ void xPortSysTickHandler( void )
                        else\r
                        {\r
                                /* Something other than the tick interrupt ended the sleep.\r
-                               Work out how long the sleep lasted. */\r
-                               ulCompletedSysTickIncrements = ( xExpectedIdleTime * ulTimerReloadValueForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;\r
+                               Work out how long the sleep lasted rounded to complete tick\r
+                               periods (not the ulReload value which accounted for part\r
+                               ticks). */\r
+                               ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;\r
 \r
                                /* How many complete tick periods passed while the processor\r
                                was waiting? */\r
-                               ulCompleteTickPeriods = ulCompletedSysTickIncrements / ulTimerReloadValueForOneTick;\r
+                               ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;\r
 \r
                                /* The reload value is set to whatever fraction of a single tick\r
                                period remains. */\r
-                               portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1 ) * ulTimerReloadValueForOneTick ) - ulCompletedSysTickIncrements;\r
+                               portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1 ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;\r
                        }\r
 \r
                        /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG\r
@@ -530,6 +528,10 @@ void xPortSysTickHandler( void )
                        portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;\r
 \r
                        vTaskStepTick( ulCompleteTickPeriods );\r
+\r
+                       /* The counter must start by the time the reload value is reset. */\r
+                       configASSERT( portNVIC_SYSTICK_CURRENT_VALUE_REG );\r
+                       portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;\r
                }\r
        }\r
 \r
@@ -545,9 +547,9 @@ __attribute__(( weak )) void vPortSetupTimerInterrupt( void )
        /* Calculate the constants required to configure the tick interrupt. */\r
        #if configUSE_TICKLESS_IDLE == 1\r
        {\r
-               ulTimerReloadValueForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
-               xMaximumPossibleSuppressedTicks = 0xffffffUL / ( ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL );\r
-               ulStoppedTimerCompensation = 45UL / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );\r
+               ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );\r
+               xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;\r
+               ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );\r
        }\r
        #endif /* configUSE_TICKLESS_IDLE */\r
 \r