简体   繁体   English

STM32上Systick计时器的功耗

[英]Power consumption of a Systick timer on STM32

In the code I'm working on ( STM32L4 project ) the Systick is enabled to tick every 1ms and its interrupt is enabled. 在我正在处理的代码(STM32L4项目)中,Systick被启用以每1ms计时一次,并且其中断被启用。 This means every 1ms the CPU exits from WFI (currently the CPU spends around 2/3 of the time in Sleep mode). 这意味着CPU每隔1毫秒从WFI退出一次(当前,CPU在休眠模式下花费的时间约为时间的2/3)。 I am wondering if it does not consume too much power to use the Systick, what do you think ? 我想知道使用Systick是否不会消耗太多能量,您认为呢?

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 eg 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. 您可以使用DWT-> CYCCNT和一些代码计数周期 (但是这将是非常不准确的,不计算例如处理程序的进入和退出周期),或者启动计时器( TIM2TIM5是32位,否则请注意溢出) ),通过RCC->APBxSMENR寄存器告诉控制器在睡眠模式下将其停止,那么您将获得一个精确的周期数,即控制器不处于睡眠状态的时间。

When you know how long is the controller not sleeping, you can use STM32CubeMX to calculate power consumption. 当您知道控制器不休眠多长时间时,可以使用STM32CubeMX来计算功耗。

在此处输入图片说明

Not using SysTick at all 完全不使用SysTick

If the only purpose of SysTick in your program is to maintain the milliseconds counter, you can use a 32 bit timer instead. 如果您的程序中SysTick的唯一目的是维护毫秒计数器,则可以改用32位计时器。 Instead of starting Systick, start the timer, and replace references to the milliseconds counter with (TIM2->CNT) . 而不是启动Systick,而是启动计时器,并用(TIM2->CNT)替换对毫秒计数器的引用。 When using HAL, it would be just 使用HAL时,

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM