AnsweredAssumed Answered

KV58: DWT Delay Problem (DWT延时卡死)

Question asked by shenjiaqihi@gmail.com on Dec 6, 2018
Latest reply on Dec 7, 2018 by shenjiaqihi@gmail.com

IC:MKV58F1M0VLQ24

IAR 8.22

I use DWT to some accurate delays. It works well in debug mode, but it stuck when I restart its power supply.

How to solve?

我用DWT实现一些精准的延时,在IAR的调试模式中是能正常工作的,但是当给单片机重新上电后,程序卡死在执行DWT延时时。请问如何解决?

 

Here are the codes:


#define DEMCR ( *(unsigned int *)0xE000EDFC )
#define TRCENA ( 0x01 << 24) 
#define DWT_CTRL ( *(unsigned int *)0xE0001000 )
#define CYCCNTENA ( 0x01 << 0 ) 
#define DWT_CYCCNT ( *(unsigned int *)0xE0001004) 


void dwt_init(int sys_clk)
{
   DEMCR |= TRCENA;
   DWT_CTRL |= CYCCNTENA;
}

void dwt_delay_us(uint32 uSec)
{
   int ticks_start, ticks_end, ticks_delay;
   dwt_init(MCU_SYSCLK);
   ticks_start = DWT_CYCCNT;
   ticks_delay = (uSec * (MCU_SYSCLK / (1000000)));
   ticks_end = ticks_start + ticks_delay;
   if(ticks_end < ticks_start)
   {
      while( DWT_CYCCNT > ticks_end ); 
   }
   while( DWT_CYCCNT < ticks_end );

}

void dwt_delay_ms(uint32 mSec)
{
   while(mSec--)
   {
      dwt_delay_us(1000);
   }
}

Outcomes