2019-08-28 11:35 PM
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?
2019-08-28 11:57 PM
//******************************************************************************
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);
}
//******************************************************************************
2019-08-29 02:17 AM
Hi Clive ,
I'm toggling the GPIO pin for 50us using above function given by you.but i'm not getting the exact delay .
Please let me know how to fix this???
2019-08-31 05:25 AM
Don't You see that Clive's code actually waits clock ticks not microseconds?
2019-08-31 06:49 AM
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.
2019-09-02 10:33 PM
got it working thanks
2020-02-27 03:36 PM
Could you please share
2020-03-03 06:17 PM
Hi VPras, Could you kindly share your code or share which was the error?