Add configSYSTICK_CLOCK_HZ to Cortex-M0 ports (#484)

This is needed to support the case when SysTick timer is not clocked
from the same source as CPU. This support already exists in other
Cortex-M ports.

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
diff --git a/portable/GCC/ARM_CM0/port.c b/portable/GCC/ARM_CM0/port.c
index 63dbc2a..18d073f 100644
--- a/portable/GCC/ARM_CM0/port.c
+++ b/portable/GCC/ARM_CM0/port.c
@@ -34,13 +34,23 @@
 #include "FreeRTOS.h"

 #include "task.h"

 

+#ifndef configSYSTICK_CLOCK_HZ

+    #define configSYSTICK_CLOCK_HZ      configCPU_CLOCK_HZ

+    /* Ensure the SysTick is clocked at the same frequency as the core. */

+    #define portNVIC_SYSTICK_CLK_BIT    ( 1UL << 2UL )

+#else

+

+/* The way the SysTick is clocked is not modified in case it is not the same

+ * as the core. */

+    #define portNVIC_SYSTICK_CLK_BIT    ( 0 )

+#endif

+

 /* Constants required to manipulate the NVIC. */

 #define portNVIC_SYSTICK_CTRL_REG             ( *( ( volatile uint32_t * ) 0xe000e010 ) )

 #define portNVIC_SYSTICK_LOAD_REG             ( *( ( volatile uint32_t * ) 0xe000e014 ) )

 #define portNVIC_SYSTICK_CURRENT_VALUE_REG    ( *( ( volatile uint32_t * ) 0xe000e018 ) )

 #define portNVIC_INT_CTRL_REG                 ( *( ( volatile uint32_t * ) 0xe000ed04 ) )

 #define portNVIC_SHPR3_REG                    ( *( ( volatile uint32_t * ) 0xe000ed20 ) )

-#define portNVIC_SYSTICK_CLK_BIT              ( 1UL << 2UL )

 #define portNVIC_SYSTICK_INT_BIT              ( 1UL << 1UL )

 #define portNVIC_SYSTICK_ENABLE_BIT           ( 1UL << 0UL )

 #define portNVIC_SYSTICK_COUNT_FLAG_BIT       ( 1UL << 16UL )

@@ -382,9 +392,9 @@
     /* Calculate the constants required to configure the tick interrupt. */

     #if ( configUSE_TICKLESS_IDLE == 1 )

         {

-            ulTimerCountsForOneTick = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );

+            ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );

             xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;

-            ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR;

+            ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );

         }

     #endif /* configUSE_TICKLESS_IDLE */

 

@@ -393,7 +403,7 @@
     portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;

 

     /* Configure SysTick to interrupt at the requested rate. */

-    portNVIC_SYSTICK_LOAD_REG = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;

+    portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;

     portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;

 }

 /*-----------------------------------------------------------*/

diff --git a/portable/IAR/ARM_CM0/port.c b/portable/IAR/ARM_CM0/port.c
index f1c401f..6fbc852 100644
--- a/portable/IAR/ARM_CM0/port.c
+++ b/portable/IAR/ARM_CM0/port.c
@@ -37,13 +37,23 @@
 #include "FreeRTOS.h"

 #include "task.h"

 

+#ifndef configSYSTICK_CLOCK_HZ

+    #define configSYSTICK_CLOCK_HZ      configCPU_CLOCK_HZ

+    /* Ensure the SysTick is clocked at the same frequency as the core. */

+    #define portNVIC_SYSTICK_CLK_BIT    ( 1UL << 2UL )

+#else

+

+/* The way the SysTick is clocked is not modified in case it is not the same

+ * as the core. */

+    #define portNVIC_SYSTICK_CLK_BIT    ( 0 )

+#endif

+

 /* Constants required to manipulate the NVIC. */

 #define portNVIC_SYSTICK_CTRL_REG             ( *( ( volatile uint32_t * ) 0xe000e010 ) )

 #define portNVIC_SYSTICK_LOAD_REG             ( *( ( volatile uint32_t * ) 0xe000e014 ) )

 #define portNVIC_SYSTICK_CURRENT_VALUE_REG    ( *( ( volatile uint32_t * ) 0xe000e018 ) )

 #define portNVIC_INT_CTRL_REG                 ( *( ( volatile uint32_t * ) 0xe000ed04 ) )

 #define portNVIC_SHPR3_REG                    ( *( ( volatile uint32_t * ) 0xe000ed20 ) )

-#define portNVIC_SYSTICK_CLK_BIT              ( 1UL << 2UL )

 #define portNVIC_SYSTICK_INT_BIT              ( 1UL << 1UL )

 #define portNVIC_SYSTICK_ENABLE_BIT           ( 1UL << 0UL )

 #define portNVIC_SYSTICK_COUNT_FLAG_BIT       ( 1UL << 16UL )

@@ -247,9 +257,9 @@
     /* Calculate the constants required to configure the tick interrupt. */

     #if ( configUSE_TICKLESS_IDLE == 1 )

         {

-            ulTimerCountsForOneTick = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );

+            ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );

             xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;

-            ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR;

+            ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );

         }

     #endif /* configUSE_TICKLESS_IDLE */

 

@@ -258,7 +268,7 @@
     portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;

 

     /* Configure SysTick to interrupt at the requested rate. */

-    portNVIC_SYSTICK_LOAD_REG = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;

+    portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;

     portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;

 }

 /*-----------------------------------------------------------*/