How to read system clock value of RT117x

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to read system clock value of RT117x

Jump to solution
1,371 Views
yf2
Contributor III

Dear support,

I am learning RT117x programming with RT1170-EVK and I can run simple minimum program like below from debugger:

void main(){
int j;
for(int i=0;i<10;i++){
j=10-i;
}
}

This is obviously meaningless but it can run in debugger and we can add more features.

Now I am wondering what API is available for me to read the system wall clock value so that to know how time goes between different operations? 

Regards,

yf2

 

Tags (1)
0 Kudos
1 Solution
1,320 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @yf2 ,

 Some code snap like this:

uint32_t cnt_start_value;
uint32_t cnt_end_value;
uint32_t overhead;

void systick_init(void)
{
    SysTick->VAL   = 0UL;	//clear current timer value
    SysTick->LOAD = 0x00FFFFFF;
    SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;   
}

void systick_disable(void)
{
    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
}

void cal_systick_read_overhead(void)
{
  	uint32_t cnt_start_value;
    uint32_t cnt_end_value;
	
	cnt_start_value = SysTick->VAL;
  
    cnt_end_value = SysTick->VAL;
	
	overhead = cnt_start_value - cnt_end_value;
	
#ifdef DEBUG_PRINT
	printf("systick start value: 0x%x\n\r", cnt_start_value);
	printf("systick end value: 0x%x\n\r", cnt_end_value);
	printf("systick current value read overhead: 0x%x\n\r", overhead);
#endif

}

	systick_init();	
	cal_systick_read_overhead();
	systick_disable();

	systick_init();	
	test_app();
	systick_disable();

void test_app()
{
  	uint32_t cnt_start_value;
    uint32_t cnt_end_value;
	uint32_t execution_cycle;	//actual execution cycle
	cnt_start_value = SysTick->VAL;
//Your code line, eg
   int j;
   for(int i=0;i<10;i++){
      j=10-i;
   }	
	cnt_start_value = SysTick->VAL;
	
	execution_cycle = cnt_start_value - cnt_end_value - overhead;
	
#ifdef DEBUG_PRINT
	printf("systick start value: 0x%x\n\r", cnt_start_value);
	printf("systick end value: 0x%x\n\r", cnt_end_value);
	printf("actual execution cycle for code: 0x%x\n\r", execution_cycle);
#endif	
}

 

 

Wish it helps you!

Best Regards,

Kerry

View solution in original post

0 Kudos
6 Replies
1,353 Views
Masmiseim
Senior Contributor I

Hello yf2,

you can use the instruction counter:
DWT->CYCCNT

regards

1,363 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @yf2 ,

  Use this code to get the system clock:

SystemCoreClock = CLOCK_GetRootClockFreq(kCLOCK_Root_M7);

 

Wish it helps you!

Best Regards,

Kerry

0 Kudos
1,358 Views
yf2
Contributor III

@kerryzhou 

You might have misunderstood my question. I don't need system clock frequency, I need a simple way to know the current time, better to microsecond (us) precision.

Regards,

yf2

 

 

0 Kudos
1,356 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @yf2 ,

  So, you want to know the code execution time, right?

  If yes, you totally can use the systick,  before your API, reset the systick, then after API, read the systick counter, then you can cacluate the time.

 Even other timer also can do it.

Best Regards,

kerry

0 Kudos
1,340 Views
yf2
Contributor III

@kerryzhou 

 

Yes maybe this systick thing can meet my requirements, can you teach how to read it with a few lines of source code?

 

Regards,

yf2

0 Kudos
1,321 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @yf2 ,

 Some code snap like this:

uint32_t cnt_start_value;
uint32_t cnt_end_value;
uint32_t overhead;

void systick_init(void)
{
    SysTick->VAL   = 0UL;	//clear current timer value
    SysTick->LOAD = 0x00FFFFFF;
    SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;   
}

void systick_disable(void)
{
    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
}

void cal_systick_read_overhead(void)
{
  	uint32_t cnt_start_value;
    uint32_t cnt_end_value;
	
	cnt_start_value = SysTick->VAL;
  
    cnt_end_value = SysTick->VAL;
	
	overhead = cnt_start_value - cnt_end_value;
	
#ifdef DEBUG_PRINT
	printf("systick start value: 0x%x\n\r", cnt_start_value);
	printf("systick end value: 0x%x\n\r", cnt_end_value);
	printf("systick current value read overhead: 0x%x\n\r", overhead);
#endif

}

	systick_init();	
	cal_systick_read_overhead();
	systick_disable();

	systick_init();	
	test_app();
	systick_disable();

void test_app()
{
  	uint32_t cnt_start_value;
    uint32_t cnt_end_value;
	uint32_t execution_cycle;	//actual execution cycle
	cnt_start_value = SysTick->VAL;
//Your code line, eg
   int j;
   for(int i=0;i<10;i++){
      j=10-i;
   }	
	cnt_start_value = SysTick->VAL;
	
	execution_cycle = cnt_start_value - cnt_end_value - overhead;
	
#ifdef DEBUG_PRINT
	printf("systick start value: 0x%x\n\r", cnt_start_value);
	printf("systick end value: 0x%x\n\r", cnt_end_value);
	printf("actual execution cycle for code: 0x%x\n\r", execution_cycle);
#endif	
}

 

 

Wish it helps you!

Best Regards,

Kerry

0 Kudos