]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/portable/RVDS/ARM_CM7/r0p1/port.c
Updates to the Cortex-M tickless idle code to reduce clock slippage.
[freertos] / FreeRTOS / Source / portable / RVDS / ARM_CM7 / r0p1 / port.c
index c851e695bcfb92019a43b13ad625c0e73ca18232..e3ca6f4d302b1d566f2b2a32ac1e87bbd6a8ffd7 100644 (file)
@@ -139,20 +139,16 @@ is defined. */
 #define portINITIAL_EXEC_RETURN                ( 0xfffffffd )\r
 \r
 /* The systick is a 24-bit counter. */\r
-#define portMAX_24_BIT_NUMBER                          ( 0xffffffUL )\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
+#define portMISSED_COUNTS_FACTOR       ( 45UL )\r
 \r
 /* For strict compliance with the Cortex-M spec the task start address should\r
 have bit-0 clear, as it is loaded into the PC on exit from an ISR. */\r
-#define portSTART_ADDRESS_MASK                         ( ( StackType_t ) 0xfffffffeUL )\r
-\r
-/* Each task maintains its own interrupt status in the critical nesting\r
-variable. */\r
-static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
+#define portSTART_ADDRESS_MASK         ( ( StackType_t ) 0xfffffffeUL )\r
 \r
 /*\r
  * Setup the timer to generate the tick interrupts.  The implementation in this\r
@@ -185,10 +181,14 @@ static void prvTaskExitError( void );
 \r
 /*-----------------------------------------------------------*/\r
 \r
+/* Each task maintains its own interrupt status in the critical nesting\r
+variable. */\r
+static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
+\r
 /*\r
  * The number of SysTick increments that make up one tick period.\r
  */\r
-#if configUSE_TICKLESS_IDLE == 1\r
+#if( configUSE_TICKLESS_IDLE == 1 )\r
        static uint32_t ulTimerCountsForOneTick = 0;\r
 #endif /* configUSE_TICKLESS_IDLE */\r
 \r
@@ -196,7 +196,7 @@ static void prvTaskExitError( void );
  * The maximum number of tick periods that can be suppressed is limited by the\r
  * 24 bit resolution of the SysTick timer.\r
  */\r
-#if configUSE_TICKLESS_IDLE == 1\r
+#if( configUSE_TICKLESS_IDLE == 1 )\r
        static uint32_t xMaximumPossibleSuppressedTicks = 0;\r
 #endif /* configUSE_TICKLESS_IDLE */\r
 \r
@@ -204,7 +204,7 @@ static void prvTaskExitError( void );
  * Compensate for the CPU cycles that pass while the SysTick is stopped (low\r
  * power functionality only.\r
  */\r
-#if configUSE_TICKLESS_IDLE == 1\r
+#if( configUSE_TICKLESS_IDLE == 1 )\r
        static uint32_t ulStoppedTimerCompensation = 0;\r
 #endif /* configUSE_TICKLESS_IDLE */\r
 \r
@@ -547,11 +547,11 @@ void xPortSysTickHandler( void )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-#if configUSE_TICKLESS_IDLE == 1\r
+#if( configUSE_TICKLESS_IDLE == 1 )\r
 \r
        __weak void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
        {\r
-       uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements, ulSysTickCTRL;\r
+       uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;\r
        TickType_t xModifiableIdleTime;\r
 \r
                /* Make sure the SysTick reload value does not overflow the counter. */\r
@@ -627,23 +627,41 @@ void xPortSysTickHandler( void )
                        }\r
                        configPOST_SLEEP_PROCESSING( xExpectedIdleTime );\r
 \r
-                       /* Stop SysTick.  Again, the time the SysTick is stopped for is\r
-                       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
-                       ulSysTickCTRL = portNVIC_SYSTICK_CTRL_REG;\r
-                       portNVIC_SYSTICK_CTRL_REG = ( ulSysTickCTRL & ~portNVIC_SYSTICK_ENABLE_BIT );\r
-\r
-                       /* Re-enable interrupts - see comments above __disable_irq() call\r
-                       above. */\r
+                       /* Re-enable interrupts to allow the interrupt that brought the MCU\r
+                       out of sleep mode to execute immediately.  see comments above\r
+                       __disable_interrupt() call above. */\r
                        __enable_irq();\r
-\r
-                       if( ( ulSysTickCTRL & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )\r
+                       __dsb( portSY_FULL_READ_WRITE );\r
+                       __isb( portSY_FULL_READ_WRITE );\r
+\r
+                       /* Disable interrupts again because the clock is about to be stopped\r
+                       and interrupts that execute while the clock is stopped will increase\r
+                       any slippage between the time maintained by the RTOS and calendar\r
+                       time. */\r
+                       __disable_irq();\r
+                       __dsb( portSY_FULL_READ_WRITE );\r
+                       __isb( portSY_FULL_READ_WRITE );\r
+                       \r
+                       /* Disable the SysTick clock without reading the \r
+                       portNVIC_SYSTICK_CTRL_REG register to ensure the\r
+                       portNVIC_SYSTICK_COUNT_FLAG_BIT is not cleared if it is set.  Again, \r
+                       the time the SysTick is stopped for is accounted for as best it can \r
+                       be, but using the tickless mode will inevitably result in some tiny \r
+                       drift of the time maintained by the kernel with respect to calendar \r
+                       time*/\r
+                       portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT );\r
+\r
+                       /* Determine if the SysTick clock has already counted to zero and\r
+                       been set back to the current reload value (the reload back being\r
+                       correct for the entire expected idle time) or if the SysTick is yet\r
+                       to count to zero (in which case an interrupt other than the SysTick\r
+                       must have brought the system out of sleep mode). */\r
+                       if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )\r
                        {\r
                                uint32_t ulCalculatedLoadValue;\r
 \r
-                               /* The tick interrupt has already executed, and the SysTick\r
-                               count reloaded with ulReloadValue.  Reset the\r
+                               /* The tick interrupt is already pending, and the SysTick count\r
+                               reloaded with ulReloadValue.  Reset the\r
                                portNVIC_SYSTICK_LOAD_REG with whatever remains of this tick\r
                                period. */\r
                                ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );\r
@@ -658,11 +676,9 @@ void xPortSysTickHandler( void )
 \r
                                portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;\r
 \r
-                               /* The tick interrupt handler will already have pended the tick\r
-                               processing in the kernel.  As the pending tick will be\r
-                               processed as soon as this function exits, the tick value\r
-                               maintained by the tick is stepped forward by one less than the\r
-                               time spent waiting. */\r
+                               /* As the pending tick will be processed as soon as this\r
+                               function exits, the tick value maintained by the tick is stepped\r
+                               forward by one less than the time spent waiting. */\r
                                ulCompleteTickPeriods = xExpectedIdleTime - 1UL;\r
                        }\r
                        else\r
@@ -684,17 +700,14 @@ void xPortSysTickHandler( void )
 \r
                        /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG\r
                        again, then set portNVIC_SYSTICK_LOAD_REG back to its standard\r
-                       value.  The critical section is used to ensure the tick interrupt\r
-                       can only execute once in the case that the reload register is near\r
-                       zero. */\r
+                       value. */\r
                        portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
-                       portENTER_CRITICAL();\r
-                       {\r
-                               portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
-                               vTaskStepTick( ulCompleteTickPeriods );\r
-                               portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;\r
-                       }\r
-                       portEXIT_CRITICAL();\r
+                       portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
+                       vTaskStepTick( ulCompleteTickPeriods );\r
+                       portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;\r
+\r
+                       /* Exit with interrpts enabled. */\r
+                       __enable_irq();\r
                }\r
        }\r
 \r
@@ -706,12 +719,12 @@ void xPortSysTickHandler( void )
  * Setup the SysTick timer to generate the tick interrupts at the required\r
  * frequency.\r
  */\r
-#if configOVERRIDE_DEFAULT_TICK_CONFIGURATION == 0\r
+#if( configOVERRIDE_DEFAULT_TICK_CONFIGURATION == 0 )\r
 \r
        void vPortSetupTimerInterrupt( void )\r
        {\r
                /* Calculate the constants required to configure the tick interrupt. */\r
-               #if configUSE_TICKLESS_IDLE == 1\r
+               #if( configUSE_TICKLESS_IDLE == 1 )\r
                {\r
                        ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );\r
                        xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;\r
@@ -794,7 +807,7 @@ __asm uint32_t vPortGetIPSR( void )
                devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the\r
                scheduler.  Note however that some vendor specific peripheral libraries\r
                assume a non-zero priority group setting, in which cases using a value\r
-               of zero will result in unpredicable behaviour. */\r
+               of zero will result in unpredictable behaviour. */\r
                configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );\r
        }\r
 \r