Skip to content

Commit 40c37bd

Browse files
authored
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 <[email protected]>
1 parent cf4ff12 commit 40c37bd

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

portable/GCC/ARM_CM0/port.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,23 @@
3434
#include "FreeRTOS.h"
3535
#include "task.h"
3636

37+
#ifndef configSYSTICK_CLOCK_HZ
38+
#define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
39+
/* Ensure the SysTick is clocked at the same frequency as the core. */
40+
#define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
41+
#else
42+
43+
/* The way the SysTick is clocked is not modified in case it is not the same
44+
* as the core. */
45+
#define portNVIC_SYSTICK_CLK_BIT ( 0 )
46+
#endif
47+
3748
/* Constants required to manipulate the NVIC. */
3849
#define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
3950
#define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
4051
#define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
4152
#define portNVIC_INT_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
4253
#define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
43-
#define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
4454
#define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
4555
#define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
4656
#define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
@@ -382,9 +392,9 @@ __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
382392
/* Calculate the constants required to configure the tick interrupt. */
383393
#if ( configUSE_TICKLESS_IDLE == 1 )
384394
{
385-
ulTimerCountsForOneTick = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
395+
ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
386396
xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
387-
ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR;
397+
ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
388398
}
389399
#endif /* configUSE_TICKLESS_IDLE */
390400

@@ -393,7 +403,7 @@ __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
393403
portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
394404

395405
/* Configure SysTick to interrupt at the requested rate. */
396-
portNVIC_SYSTICK_LOAD_REG = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
406+
portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
397407
portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
398408
}
399409
/*-----------------------------------------------------------*/

portable/IAR/ARM_CM0/port.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,23 @@
3737
#include "FreeRTOS.h"
3838
#include "task.h"
3939

40+
#ifndef configSYSTICK_CLOCK_HZ
41+
#define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
42+
/* Ensure the SysTick is clocked at the same frequency as the core. */
43+
#define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
44+
#else
45+
46+
/* The way the SysTick is clocked is not modified in case it is not the same
47+
* as the core. */
48+
#define portNVIC_SYSTICK_CLK_BIT ( 0 )
49+
#endif
50+
4051
/* Constants required to manipulate the NVIC. */
4152
#define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
4253
#define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
4354
#define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
4455
#define portNVIC_INT_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
4556
#define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
46-
#define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
4757
#define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
4858
#define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
4959
#define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
@@ -247,9 +257,9 @@ __weak void vPortSetupTimerInterrupt( void )
247257
/* Calculate the constants required to configure the tick interrupt. */
248258
#if ( configUSE_TICKLESS_IDLE == 1 )
249259
{
250-
ulTimerCountsForOneTick = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
260+
ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
251261
xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
252-
ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR;
262+
ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
253263
}
254264
#endif /* configUSE_TICKLESS_IDLE */
255265

@@ -258,7 +268,7 @@ __weak void vPortSetupTimerInterrupt( void )
258268
portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
259269

260270
/* Configure SysTick to interrupt at the requested rate. */
261-
portNVIC_SYSTICK_LOAD_REG = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
271+
portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
262272
portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
263273
}
264274
/*-----------------------------------------------------------*/

0 commit comments

Comments
 (0)