cancel
Showing results for 
Search instead for 
Did you mean: 

getMCULoadPct() function

qqAnton
Senior

Hello

I want to know the MCU Load, so I found this function

McuLoad = touchgfx::HAL::getInstance()->getMCULoadPct();

I call it in

void Model::tick()

But I always get 0, whatever I do it alwsys returns zero, so I don't think it's working well,

so, how could I resolve the problem? maybe I should call it from another place?

Thank you

Anton

24 REPLIES 24

@Martin KJELDSEN​  I think the problem is in TIM1, i checked TIM1->CNT value and it's always = 0, looks like it's not working,

but I don't know why

@Martin KJELDSEN​ 

void STM32F4Instrumentation::init()
{
   RCC_ClkInitTypeDef clkconfig;
    uint32_t uwTimclock, uwAPB1Prescaler = 0U;
    uint32_t pFLatency;
 
    __TIM1_CLK_ENABLE();
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 0;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  {
    Error_Handler( );
  }
 
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler( );
  }
 
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    Error_Handler( );
  }
 
    /* Get clock configuration */
    HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
 
    /* TIM2 is on APB1 bus */
    uwAPB1Prescaler = clkconfig.APB1CLKDivider;
 
    if (uwAPB1Prescaler == RCC_HCLK_DIV1)
        uwTimclock = HAL_RCC_GetPCLK1Freq();
    else
        uwTimclock = 2 * HAL_RCC_GetPCLK1Freq();
 
    m_sysclkRatio = HAL_RCC_GetHCLKFreq() / uwTimclock;
 
    HAL_TIM_Base_Start(&htim1);
}

This is my timer init code, I change

htim1.Init.Prescaler = 0;

and

htim1.Init.Period = 0;

so, finally getMCULoadPct() returns numbers except 0.

Could you advise how to configure the timer better?

Here's the configuration we use for TIM2 for STM32F7. 

void STM32F7Instrumentation::init()
{
    __TIM2_CLK_ENABLE();
    tim.Instance = TIM2;
    tim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    tim.Init.CounterMode = TIM_COUNTERMODE_UP;
    tim.Init.Period = 0xFFFFFFFF;
    tim.Init.Prescaler = 0;
    tim.Init.RepetitionCounter = 1;
    HAL_TIM_Base_Init(&tim);
    HAL_TIM_Base_Start(&tim);
}

Thank you for your time, now it's working

but CPU Load value only has two states 1% and 100%, maybe it's ok

ALomb
Senior

Hi @qqAnton​ and @Martin KJELDSEN​ ,

I think the problem is in the initialization of the timer.

I selected the TIM1 in Cube, but the initialization (generated) is incorrect (it seems to refer to TIM2).

This is my initialization

static TIM_HandleTypeDef htim1;
 
void STM32F7Instrumentation::init()
{
#if 1
   RCC_ClkInitTypeDef clkconfig;
    uint32_t uwTimclock, uwAPB1Prescaler = 0U;
    uint32_t pFLatency;
 
    __TIM1_CLK_ENABLE();  //__TIM2_CLK_ENABLE();
 
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 0xFFFFFFFF;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  {
    Error_Handler( );
  }
 
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler( );
  }
 
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 0xFFFFFFFF;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
  {
    Error_Handler( );
  }
 
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    Error_Handler( );
  }
 
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler( );
  }
 
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  sBreakDeadTimeConfig.DeadTime = 0;
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  sBreakDeadTimeConfig.BreakFilter = 0;
  sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
  sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
  sBreakDeadTimeConfig.Break2Filter = 0;
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  {
    Error_Handler( );
  }
 
    /* Get clock configuration */
    HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
 
    /* TIM1 is on APB1 bus */
    uwAPB1Prescaler = clkconfig.APB1CLKDivider;
 
    if (uwAPB1Prescaler == RCC_HCLK_DIV1)
        uwTimclock = HAL_RCC_GetPCLK1Freq();
    else
        uwTimclock = 2 * HAL_RCC_GetPCLK1Freq();
 
    m_sysclkRatio = HAL_RCC_GetHCLKFreq() / uwTimclock;
 
    HAL_TIM_Base_Start(&htim1);
#else
    __TIM1_CLK_ENABLE();
    htim1.Instance = TIM1;
    htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim1.Init.Period = 0xFFFFFFFF;
    htim1.Init.Prescaler = 0;
    htim1.Init.RepetitionCounter = 1;
    HAL_TIM_Base_Init(&htim1);
    HAL_TIM_Base_Start(&htim1);
#endif
}

Hi, i'm back from vacation.

Hmm, that's strange - At which points in time is it going to 100% ? This should only happen if you're e.g. busy-looping somewhere or do not have DMA2D enabled (Using CPU to render everything)

/Martin

@qqAnton​,

Any updates on your end?

@Martin KJELDSEN​ ,

I think the problem is that I use handleTickEvent() on every screen, where I poll hardware buttons state,

or refresh text on the screen, so CPU is always busy.

HCon
Associate II

The Tim1 is 16-bit, The Tim2 is 32-bit.

So Timer selection Tim2.

In file STM32F4Instrumentation.cpp:

#include "stm32f4xx_hal.h"

#include "stm32f4xx_hal_tim.h"

#include <touchgfx/hal/HAL.hpp>

#include <STM32F4Instrumentation.hpp>

/* USER CODE BEGIN user includes */

/* USER CODE END user includes */

namespace touchgfx

{

static TIM_HandleTypeDef htim2;

void STM32F4Instrumentation::init()

{

  RCC_ClkInitTypeDef clkconfig;

  uint32_t uwTimclock, uwAPB1Prescaler = 0U;

  uint32_t pFLatency;

  __TIM2_CLK_ENABLE();

 TIM_ClockConfigTypeDef sClockSourceConfig = {0};

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 htim2.Instance = TIM2;

 htim2.Init.Prescaler = 0;

 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim2.Init.Period = 0xffffffff;

 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

 if (HAL_TIM_Base_Init(&htim2) != HAL_OK)

 {

  Error_Handler( );

 }

 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

 if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)

 {

  Error_Handler( );

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

 {

  Error_Handler( );

 }

  /* Get clock configuration */

  HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);

  /* TIM2 is on APB1 bus */

  uwAPB1Prescaler = clkconfig.APB1CLKDivider;

  if (uwAPB1Prescaler == RCC_HCLK_DIV1)

    uwTimclock = HAL_RCC_GetPCLK1Freq();

  else

    uwTimclock = 2 * HAL_RCC_GetPCLK1Freq();

  m_sysclkRatio = HAL_RCC_GetHCLKFreq() / uwTimclock;

  HAL_TIM_Base_Start(&htim2);

}

//Board specific clockfrequency

unsigned int STM32F4Instrumentation::getElapsedUS(unsigned int start, unsigned int now, unsigned int clockfrequency)

{

  return ((now - start) + (clockfrequency / 2)) / clockfrequency;

}

unsigned int STM32F4Instrumentation::getCPUCycles()

{

  return __HAL_TIM_GET_COUNTER(&htim2) * m_sysclkRatio;

}

void STM32F4Instrumentation::setMCUActive(bool active)

{

  if (active) //idle task sched out

  {

    cc_consumed += getCPUCycles() - cc_in;

  }

  else //idle task sched in

  {

    cc_in = getCPUCycles();

  }

}

}

HCon
Associate II

STM32CubeMX :

0690X00000ARPHrQAP.jpg