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; }
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 Moderatorhttps://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
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;}
void SysTick_Handler (void)
{
tick ++;
}
uint32_t millis (void)
return tick;
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.
No, I am not using RTOS.
Unrecoverable error occurs significantly later, somewhere in main loop. I cannot reach this point running step by step, but it happens right away as I let program run automatically. I believe, crash happens on first tick...
I have created another regular GPT timer in FSP, set interval as 1 mS and increment counter there, seems like it works, although it may not optimal resources usage... Thank you.
You are welcome. This is really weird issue. I couldn't find what is wrong there - everything seems correct. Maybe need to use the lower interrupt priority.
Returning to the DWT, you can find some information about it in this post community.renesas.com/.../64801. Maybe you will succeed in using it.
Not a problem, I will leave as it is now, with extra GPT timer.