freertos tick factor 2 too fast on stm32f4xx

被刻印的时光 ゝ 提交于 2019-12-11 07:25:47

问题


my current freertos setup must have a mistake because when i use the task delay function or the timer period, both timings are twice as fast as desired.

what have i checked already:

  1. DEBUG VARIABLES: all three parts included in incrementing the tick (TIMER IRQ HANDLER, FREERTOS TICK HANDLER, FREERTOS TICK HOOK) are called 10'000 timer per 10 seconds. So far OK.
  2. TASK and TIMER periods: when multiplying the period by 2, the timing is OK, so far NOT ok.
  3. Freertos Config: SystemClockCore is 168MHz. Ticks are set to 1000 ticks per second. is there somewhere a mistake in the config?

I hope someone could give me a tipp what is causing this issue.. P.S.: The system tick is currently running too slow as well, but this doesn't concern me currently since not used.. could have an influence though.

// DEBUG VARIABLES
DEBUG_CT DEBUG_CLOCK_TICK_T {...}   
    DEBUG_CT_ISR_SYSTEM_TICK        uint32_t    65  // IS NOT USED
    DEBUG_CT_ISR_TIM_TICK           uint32_t    10036   
    DEBUG_CT_FREERTOS_HOOK_TICK     uint32_t    10036   
    DEBUG_CT_FREERTOS_TICK_HANDLER  uint32_t    10036   

// FREERTOS TICK HANDLER
#ifdef DEBUG_CLOCK_TICK
DEBUG_CT.DEBUG_CT_FREERTOS_TICK_HANDLER++;
#endif
xPortSysTickHandler();

// THREAD DELAY
void delay(uint32_t ms)
{
    M_ASSERT_BOOL(_created);
    ms *= 2; // !!! CORRECTION, WHY ???
    vTaskDelay(ms/portTICK_PERIOD_MS);
}

// TIMER CREATION
_handle = xTimerCreateStatic(
    _name,
    pdMS_TO_TICKS(_msDelay*2), // !!! CORRECTION, WHY ???
    autoreload,
    (void*)_name,
    _expired,
    &_timerBuffer);

// PORTMACRO.H
#define portSTACK_GROWTH            ( -1 )
#define portTICK_PERIOD_MS          ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
#define portBYTE_ALIGNMENT          8

// FREERTOS CONFIG
#define configUSE_PREEMPTION                     1
#define configSUPPORT_STATIC_ALLOCATION          1
#define configSUPPORT_DYNAMIC_ALLOCATION         0
#define configUSE_IDLE_HOOK                      1
#define configUSE_TICK_HOOK                      1
#define configCPU_CLOCK_HZ                       ( SystemCoreClock )
#define configTICK_RATE_HZ                       ((TickType_t)1000)
#define configMAX_PRIORITIES                     ( 7 )
#define configMINIMAL_STACK_SIZE                 ((uint16_t)32)
#define configMAX_TASK_NAME_LEN                  ( 32 )
#define configUSE_16_BIT_TICKS                   0
#define configUSE_MUTEXES                        1
#define configQUEUE_REGISTRY_SIZE                8
#define configCHECK_FOR_STACK_OVERFLOW           1
#define configUSE_MALLOC_FAILED_HOOK             1
#define configUSE_DAEMON_TASK_STARTUP_HOOK       1
#define configENABLE_BACKWARD_COMPATIBILITY      0
#define configUSE_PORT_OPTIMISED_TASK_SELECTION  1
#define configUSE_TICKLESS_IDLE                  1

/* Co-routine definitions. */
#define configUSE_CO_ROUTINES                    0
#define configMAX_CO_ROUTINE_PRIORITIES          ( 2 )

/* Software timer definitions. */
#define configUSE_TIMERS                         1
#define configTIMER_TASK_PRIORITY                ( 2 )
#define configTIMER_QUEUE_LENGTH                 10
#define configTIMER_TASK_STACK_DEPTH             256

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet            1
#define INCLUDE_uxTaskPriorityGet           1
#define INCLUDE_vTaskDelete                 1
#define INCLUDE_vTaskCleanUpResources       0
#define INCLUDE_vTaskSuspend                1
#define INCLUDE_vTaskDelayUntil             1
#define INCLUDE_vTaskDelay                  1
#define INCLUDE_xTaskGetSchedulerState      1
#define INCLUDE_pcTaskGetTaskName           1
#define INCLUDE_xTaskGetCurrentTaskHandle   1
#define INCLUDE_eTaskGetState               1
#define INCLUDE_xTaskGetHandle              1

/* Cortex-M specific definitions. */
#ifdef __NVIC_PRIO_BITS
 /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
 #define configPRIO_BITS         __NVIC_PRIO_BITS
#else
 #define configPRIO_BITS         4
#endif

/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY   15

/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5

/* Interrupt priorities used by the kernel port layer itself.  These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY         ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY    ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )

/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
/* USER CODE BEGIN 1 */   
#define configASSERT( x ) if ((x) == 0) {taskDISABLE_INTERRUPTS(); for( ;; );} 
/* USER CODE END 1 */

/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler    SVC_Handler
#define xPortPendSVHandler PendSV_Handler

/* IMPORTANT: This define MUST be commented when used with STM32Cube firmware, 
              to prevent overwriting SysTick_Handler defined within STM32Cube HAL */
/* #define xPortSysTickHandler SysTick_Handler */

/* USER CODE BEGIN Defines */             
/* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */
/* USER CODE END Defines */ 

#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
void PreSleepProcessing(uint32_t *ulExpectedIdleTime);
void PostSleepProcessing(uint32_t *ulExpectedIdleTime);
#endif /* defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__) */

/* The configPRE_SLEEP_PROCESSING() and configPOST_SLEEP_PROCESSING() macros
allow the application writer to add additional code before and after the MCU is
placed into the low power state respectively. */
#if configUSE_TICKLESS_IDLE == 1 
#define configPRE_SLEEP_PROCESSING                        PreSleepProcessing
#define configPOST_SLEEP_PROCESSING                       PostSleepProcessing
#endif /* configUSE_TICKLESS_IDLE == 1 */

#endif /* FREERTOS_CONFIG_H */

回答1:


FreeRTOS on an STM32F4 is normally configured to use the SysTick as the timer for generating the OS tick timer. However you can use any timer you want if you set it up correctly.

If the SysTick is running half the speed it should be then that probably explains your problem as it runs of the main clock (ie 168MHz).

I would think that if this is the case then you have the PLL configured incorrectly and you main clock frequency is 168MHz / 2 rather than 168MHz (or you are running off a different clock).

I would suggest not using tickless idle whilst debbugging this as it will probably make things easier.




回答2:


Like mentioned before, FreeRTOS is configured to use PLL as clock source at 168MHz on STM32F4. In function SetSysClock in system_stm32f4xx.c, three prescaler are set (check the clock diagram in datasheet):

  • AHB prescaler is set to 1,
  • APB2 prescaler is set to 2,
  • APB1 prescaler is set to 4.

If we take for example Timer 2 - it is connected to the bus APB1, which means it uses prescaler with division 4 (168MHz/4 = 42MHZ). Then there is also a TIMPRE bit in register RCC_DCKCFGR. By default this bit is set to 0, which means:

when set to 0: "If the APB prescaler (PPRE1, PPRE2 in the RCC_CFGR register) is configured to a division factor of 1, TIMxCLK = PCLKx. Otherwise, the timer clock frequencies are set to twice to the frequency of the APB domain to which the timers are connected: TIMxCLK = 2xPCLKx."

This means that the timer clock is twice as fast as APB clock that comes from prescaler, making it 84MHz.



来源:https://stackoverflow.com/questions/46968844/freertos-tick-factor-2-too-fast-on-stm32f4xx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!