0

In the code I'm working on ( STM32L4 project ) the Systick is enabled to tick every 1ms and its interrupt is enabled. This means every 1ms the CPU exits from WFI (currently the CPU spends around 2/3 of the time in Sleep mode). I am wondering if it does not consume too much power to use the Systick, what do you think ?

Guillaume Petitjean
  • 2,408
  • 1
  • 21
  • 47
  • 1
    What is too much power? Are you using a coin cell and need it to last for years? Are you mains powered? You need to add specifics. – Colin Apr 30 '18 at 08:06
  • Do you need figures in mA ? I don't really see the relevance of your comment. For most battery powered devices, power consumption is a major (and difficult) issue. – Guillaume Petitjean May 03 '18 at 07:03
  • So power consumption of a powered peripheral that is simply not counting vs counting? why not just measure it? start the timer put the chip in wfi with no interrupts, then same timer settings but just dont assert the enable, measure the power consumption. Would be shocked if you could accurately measure that and that is by far the least of your power consumption worries...on the other hand enabling a timer peripheral you might be able to measure that even if you enable the clock gate but dont start the timer. systick should be cheaper, but just measure and see... – old_timer May 30 '18 at 17:20

1 Answers1

2

First, measure how long does the interrupt handler take. You can count the cycles with DWT->CYCCNT and some code (but it'll be quite inaccurate, not counting e.g. handler entry and exit cycles), or start a timer (TIM2 or TIM5 as they are 32 bits, otherwise mind the overflow), tell the controller to stop it in sleep mode with the RCC->APBxSMENR register, then you'll have an exact count of cycles how long the controller was not sleeping.

When you know how long is the controller not sleeping, you can use STM32CubeMX to calculate power consumption.

enter image description here

Not using SysTick at all

If the only purpose of SysTick in your program is to maintain the milliseconds counter, you can use a 32 bit timer instead. Instead of starting Systick, start the timer, and replace references to the milliseconds counter with (TIM2->CNT). When using HAL, it would be just

HAL_StatusTypeDef HAL_InitTick(uint32_t prio __attribute__((unused))) {
  RCC->APB1ENR = RCC_APB1ENR_TIM2EN;
  asm volatile("dsb sy");
  TIM2->CR1 = TIM_CR1_CEN;
  return HAL_OK;
}

uint32_t HAL_GetTick(void) {
  return TIM2->CNT;
}
  • 1
    I do no use the HAL but I kept the same Systick API in my code but modified the implementation to use LPTIMx instead. The benefit that I see is that it is independant from the CPU frequency, so I can decrease this frequency (for power consumption) without changing the systick time base. – Guillaume Petitjean May 03 '18 at 06:59