cancel
Showing results for 
Search instead for 
Did you mean: 

DWT_Library not working for STM32F767

VPras
Associate II

Hi,

I'm using DWT library to generate us_delay.

#include "stm32f7xx_hal.h"          // change to whatever MCU you use
#include "dwt_delay.h"
 
/**
 * Initialization routine.
 * You might need to enable access to DWT registers on Cortex-M7
 *   DWT->LAR = 0xC5ACCE55
 */
void DWT_Init(void)
{
    if (!(CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) {
    	DWT->LAR = 0xC5ACCE55;
        CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
        DWT->CYCCNT = 0;
        DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
    }
}
 
#if DWT_DELAY_NEWBIE
/**
 * If you are a newbie and see magic in DWT_Delay, consider this more
 * illustrative function, where you explicitly determine a counter
 * value when delay should stop while keeping things in bounds of uint32.
*/
void DWT_Delay(uint32_t us) // microseconds
{
    uint32_t startTick  = DWT->CYCCNT,
             targetTick = DWT->CYCCNT + us * (SystemCoreClock/1000000);
 
    // Must check if target tick is out of bounds and overflowed
    if (targetTick > startTick) {
        // Not overflowed
        while (DWT->CYCCNT < targetTick);
    } else {
        // Overflowed
        while (DWT->CYCCNT > startTick || DWT->CYCCNT < targetTick);
    }
}
#else
/**
 * Delay routine itself.
 * Time is in microseconds (1/1000000th of a second), not to be
 * confused with millisecond (1/1000th).
 *
 * No need to check an overflow. Let it just tick :)
 *
 * @param uint32_t us  Number of microseconds to delay for
 */
void DWT_Delay(uint32_t us) // microseconds
{
    uint32_t startTick = DWT->CYCCNT,
             delayTicks = us * (SystemCoreClock/1000000);
 
    while (DWT->CYCCNT - startTick < delayTicks);
}
 
#endif

I'm trying to integrate the DS18B20 Temperature sensor,where i need microsecond delay .

I feel initialization of DWT is not happening?

any idea to solve the issue?

7 REPLIES 7
//******************************************************************************
 
volatile unsigned int *DWT_CYCCNT   = (volatile unsigned int *)0xE0001004; //address of the register
volatile unsigned int *DWT_CONTROL  = (volatile unsigned int *)0xE0001000; //address of the register
volatile unsigned int *DWT_LAR      = (volatile unsigned int *)0xE0001FB0; //address of the register
volatile unsigned int *SCB_DEMCR    = (volatile unsigned int *)0xE000EDFC; //address of the register
 
//******************************************************************************
 
void EnableTiming(void)
{
  *DWT_LAR = 0xC5ACCE55; // unlock
  *SCB_DEMCR |= 0x01000000;
  *DWT_LAR = 0xC5ACCE55; // unlock
  *DWT_CYCCNT = 0; // reset the counter
  *DWT_CONTROL |= 1 ; // enable the counter
}
 
//******************************************************************************
 
void HAL_Delay_us(uint32_t tick) // SystemCoreClock / 1000000 = 1us
{
  unsigned int start, current;
 
  start = *DWT_CYCCNT;
 
  do
  {
    current = *DWT_CYCCNT;
  } while((current - start) < tick);
}
 
//******************************************************************************

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

Hi Clive ,

I'm toggling the GPIO pin for 50us using above function given by you.but i'm not getting the exact delay .0690X00000AAVRRQA5.jpg

Please let me know how to fix this???

Don't You see that Clive's code actually waits clock ticks not microseconds?

That, and one might have to compensate for cycles eaten entering/exiting.

I would tend to use precomputed constants so as not to repeat the math.

Also your wave-form doesn't look 50/50, so clearly doing something else there that you need to adjust/compensate for.

Hard to understand what exactly you're doing when you fail to provide code.

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

got it working thanks

JMJV
Associate II

Could you please share

JMJV
Associate II

Hi VPras, Could you kindly share your code or share which was the error?