This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Looking function returns number of milliseconds since restart. Similar to millis() in Arduino world.

Hello all,

I am looking the way to make function returns number of milliseconds since restart, same functionality as millis() in Arduino world.

Using e2 Studio, micro is R7FA6T1AD3CFM.

I have tried such function, see below, but it works only under debugger, otherwise (DWT->CYCCNT) returns constant so whole function also returns constant.

Are there any solution?

uint32_t millis()
{
  uint32_t res = R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKD);  //get system clock in Nz
  res = res / BSP_DELAY_UNITS_MILLISECONDS;  //convert to kHz
  res = (DWT->CYCCNT) / res; //get #of cycles and convert to mS
  return res;
}




[locked by: Sai (SWF) at 11:41 (GMT 0) on 5 Jan 2023]
Parents
  • Hello Serge,

    If your project doesn't use an RTOS, you can use the SysTick timer to calculate the time intervals or any of the available timers otherwise.

    If you still want to use the DWT module, please provide its initialization code to check if everything is correct in it.

    Kind regards,

    Sergey

    If this response, or one provided by another user, answers your question, please verify the answer. Thank you!

    Renesas Engineering Community Moderator
    https://community.renesas.com/
    https://academy.renesas.com/
    https://en-support.renesas.com/knowledgeBase/

  • SysTick timer sounds ok for me, I do not have any special initialization for WDT.

    I am new to e2Studio, how to add SysTick timer to my project? I did not see it in FSP -> Stack Configuration...

  • It's not presented in the FSP Stacks as it's the Cortex-M core feature.

    The initialization should be the following

    SysTick_Config(SystemCoreClock / 1000);//1000 is the number of ticks per second
        NVIC_SetPriority(SysTick_IRQn, 0); // Set User configured Priority for Systick Interrupt, 0 is the interrupt priority

    And the usage:

    void SysTick_Handler (void)
    {
        tick ++;
    }

    uint32_t millis (void)
    {
        return tick;
    }

    Kind regards,

    Sergey

    If this response, or one provided by another user, answers your question, please verify the answer. Thank you!

    Renesas Engineering Community Moderator
    https://community.renesas.com/
    https://academy.renesas.com/
    https://en-support.renesas.com/knowledgeBase/

  • Getting unrecoverable error...

    I have:

    volatile uint32_t msTicks = 0; // Variable to store millisecond ticks
    
    void SysTick_Handler(void)  // SysTick interrupt Handler.
      {
      msTicks++;
      }
    
    void hal_entry(void)
    {
    // .........
    uint32_t returnCode;
    returnCode = SysTick_Config(SystemCoreClock / 1000);      // Configure SysTick to generate an interrupt every millisecond
    if (returnCode != 0)
      {                                   /* Check return code for errors */
      // Error Handling
      }
    
    NVIC_SetPriority(SysTick_IRQn, 0); // Set User configured Priority for SysTick Interrupt, 0 is the interrupt priority
    }

    SysTick_Config() returns zero, error is delayed.

    Does SysTick_Handler() require declaration? E2Studio gives me warning

  • I have just double checked the code on my board, everything works fine. I have RA2A1 MCU though.

    Do you use any RTOS in your program?

    Did you try to run the program step-by-step? At which line the unrecoverable error occurs?

    As for the warning, you can create the declaration to suspend it, it doesn't affect anything.

    Kind regards,

    Sergey

    If this response, or one provided by another user, answers your question, please verify the answer. Thank you!

    Renesas Engineering Community Moderator
    https://community.renesas.com/
    https://academy.renesas.com/
    https://en-support.renesas.com/knowledgeBase/

Reply Children