cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L031K6 Nucleo and DWT?

Sany
Associate III

I have a STM32L031K6 Nucleo Board...

I want to use the DWT.. but I become this errors:

../Core/Src/../Inc/stm32_delay.h:35:30: error: 'DWT' undeclared (first use in this function)
   uint32_t clk_cycle_start = DWT->CYCCNT;

How can I Use DWT or Enable it? I Use STM32CubeIDE...

My Function:

#ifndef INC_STM32_DELAY_H_
#define INC_STM32_DELAY_H_
 
#ifdef __cplusplus
extern "C" {
#endif
 
#ifndef __STM32L0xx_HAL_H
#include "stm32l0xx_hal.h"
#endif
 
/**
 * @brief  Initializes DWT_Cycle_Count for DWT_Delay_us function
 * @return Error DWT counter
 *         1: DWT counter Error
 *         0: DWT counter works
 */
uint32_t DWT_Delay_Init(void);
 
 
 
/**
 * @brief  This function provides a delay (in microseconds)
 * @param  microseconds: delay in microseconds
 */
__STATIC_INLINE void DWT_Delay_us(volatile uint32_t microseconds)
{
  uint32_t clk_cycle_start = DWT->CYCCNT;
 
  /* Go to number of cycles for system */
  microseconds *= (HAL_RCC_GetHCLKFreq() / 1000000);
 
  /* Delay till end */
  while ((DWT->CYCCNT - clk_cycle_start) < microseconds);
}
 
 
#ifdef __cplusplus
}
#endif
 
#endif /* INC_STM32_DELAY_H_ */

Thanks guys..

1 ACCEPTED SOLUTION

Accepted Solutions

Pretty sure the L031 lacks any 32-bit timer, ST not really understanding expectations for 32-bit processors in this regard.

Sharper engineers would have put in at least a 32-bit counter, dealing with the critical-path/propagation issues, and built a less complex assortment of timing units..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5

No CM0(+) doesn't have this, adds too many transistors.

You'll need to do timing via a free-running 16-bit (or 32-bit if available) TIM

You can set the TIM prescaler such that it counts micro-seconds, or processor cycles.

Period = 0xFFFF, or 0xFFFFFFFF

Use TIMx->CNT to observe passage of time

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Sany
Associate III

Hello,

Thank you, okay, I am really new to stm32 and come from Atmel/Microchip development.

I there a example to use a TIM (32 bit available) as a micro-seconds counter? 😉

__STATIC_INLINE void TIM_Delay_us(uint16_t microseconds)

{

 uint16_t clk_cycle_start = TIM2->CNT;

 /* Go to number of cycles for system */

 microseconds *= (HAL_RCC_GetHCLKFreq() / 1000000);

 /* Delay till end */

 while ((TIM2->CNT - clk_cycle_start) < microseconds);

}

Doing this blind, something like this to set up

TIM_HandleTypeDef  TimHandle;

 /* TIMx Peripheral clock enable */

 TIM2_CLK_ENABLE();

 /* Set TIMx instance */

 TimHandle.Instance = TIM2;

 TimHandle.Init.Period      = 0xFFFF; // MAXIMAL 16-bit

 TimHandle.Init.Prescaler     = 0; // DIV1

 TimHandle.Init.ClockDivision   = 0;

 TimHandle.Init.CounterMode    = TIM_COUNTERMODE_UP;

 if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK)

 {

  /* Initialization Error */

  Error_Handler();

 }

 /* Start Timebase */

 if (HAL_TIM_Base_Start(&TimHandle) != HAL_OK)

 {

  /* Starting Error */

  Error_Handler();

 }

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Pretty sure the L031 lacks any 32-bit timer, ST not really understanding expectations for 32-bit processors in this regard.

Sharper engineers would have put in at least a 32-bit counter, dealing with the critical-path/propagation issues, and built a less complex assortment of timing units..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Sany
Associate III

Hello!

Thanks, I have 1 TIM available, and I need a usDelay Timer for a SPI strobe...

then I must use a 16-bit Timer...