Skip to main content
Visitor II
December 1, 2021
Solved

STM32F7 microsecond delay problem

  • December 1, 2021
  • 5 replies
  • 1506 views

Hi,

I am using STM32F746NG discovery board. I try to setup 60 micro second delay. With ST-Link debugger, the 60

micro second delay is fine. But when I cycle the power, the firmware does not run.

When I comment out the delay function DWT_Delay_us(), firmware runs. Anything wrong?

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;

void DWT_Delay_Init(void)

{

 *DWT_LAR = 0xC5ACCE55; // unlock (CM7)

 *SCB_DEMCR |= 0x01000000;

 *DWT_CYCCNT = 0; // reset the counter

 *DWT_CONTROL |= 1 ; // enable the counter

}

void DWT_Delay_us( uint32_t us)

{

 uint32_t clk_cycle_start = *DWT_CYCCNT;

 /* Go to number of cycles for system */

 us *= (SystemCoreClock / 1000000);

 /* Delay till end */

 while ((*DWT_CYCCNT - clk_cycle_start) < us);

}

    This topic has been closed for replies.
    Best answer by JChen.24

    Thanks for the help, everybody!

    It works.

    5 replies

    Graduate II
    December 1, 2021

    Check you're actually initializing the DWT properly, and that it is counting.

    The debugger likely enables this either way.

    JChen.24Author
    Visitor II
    December 1, 2021

    I find the problem. If use micro USB as power source, it detects as usb device, so firmware no run.

    I use external power source of 5V. Everytime firmware runs.

    Graduate II
    December 2, 2021

    Check boot pin wiring, check reset wiring.

    Graduate II
    December 2, 2021

    This is how I activate CYCCNT on an STM32F767, not for delay, but anyway, it works.

    But I must admit that I forgot what the PCSAMPLENA bit does...

       CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;

       DWT->LAR = 0xC5ACCE55;

       DWT->CYCCNT = 0;

       DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

       DWT->CTRL |= DWT_CTRL_PCSAMPLENA_Msk;

    JChen.24AuthorAnswer
    Visitor II
    December 2, 2021

    Thanks for the help, everybody!

    It works.