Access to hardware timer

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
User8570
Level 3
Level 3
I am using XMC4500 platform and using a 5 us timer I am not getting good/precise timing. Wondering if somehow I can have access to the CCU8 using native commands? Does anybody try to use that timer to check delta times on interrupt routines?

thanks
0 Likes
15 Replies
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
Hi,

Are you using the system timer tick? This should be quite precise to check delta time base on the number of clock cycles.

Best Regards
Travis
0 Likes
User8570
Level 3
Level 3
Travis

On my current setup a 5 us is not working for me. Measuring a 20 Ms pwm is not always giving me accurate readings.

Changing to 1 us. Make my software unstable.

Can u tell me how to use the cc4u slice?
0 Likes
User8570
Level 3
Level 3
I would like to have 500 ns resolution. Any suggestions how to achieve that?
0 Likes
User8570
Level 3
Level 3
Travis

Can you provide a way to read the system timer tick?
0 Likes
User8570
Level 3
Level 3
Travis

Based on other threads. I used the following to setup a CC4 slice:

CCU40->GCTRL = (uint32_t)0;

// Set the prescaler value: clock is divided by 2^n, with n=0..15
CCU40_CC40->PSC = 0x0004; // divide 32MHz by 2^15 = 32768 --> 976.6Hz
// Set the period of the timer: 2x per second
CCU40_CC40->PRS = (977 >> 1)-1;
// Enable the synchronized transfer of the period value into the active register
CCU40->GCSS |= CCU4_GCSS_S0SE_Msk;
// Set which interrupt line the timer interrupt is directed toward
WR_REG(CCU40_CC40->SRS, CCU4_CC4_SRS_POSR_Msk, CCU4_CC4_SRS_POSR_Pos, (uint32_t)0x00); // Service Request 0
// Enable interrupt generation on period match
SET_BIT(CCU40_CC40->INTE, CCU4_CC4_INTE_PME_Pos);
/* Enables the timer */
SET_BIT(CCU40->GIDLC, CCU4_GIDLC_CS0I_Pos);
/* Start the timer */
SET_BIT(CCU40_CC40->TCSET, CCU4_CC4_TCSET_TRBS_Pos);

CCU40_CC40->TCSET =0x01;
//CC40TCSET |= 0x00000001; //Start Timer CC40 directly by setting the TRBS bit = 1

// configure and enable interrupt for CCU40 SR0
NVIC_SetPriority(CCU40_0_IRQn, 0xC0); //This CMSIS function configures node 1 to priority level 0 (highes Priority)
NVIC_ClearPendingIRQ(CCU40_0_IRQn); //This function clears node 1 pending status
NVIC_EnableIRQ(CCU40_0_IRQn); //This function enables node 1 for interrupt request generation


and also the IRQ by:

void CCU40_0_IRQHandler() {
IO004_TogglePin(IO004_Handle6);

}

Can you provide some advice on how to debug this issue or setup the timer correctly? thanks
0 Likes
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
Hi,

you can do something like below...


uint32_t Systick_Value[2],Delta;

Systick_Value[0] = SYST_CVR; //Start of measurement, log the first systick counter



Systick_Value[1] = SYST_CVR; //End of measurement, log the second systick counter

Delta = Systick_Value[1] - Systick_Value[0];
0 Likes
User8570
Level 3
Level 3
Travis

What do I need to include to get access to SYST_CVR. Currently I am getting an error about undeclared variable.

I am planning to read the syst_tick in an ISR.
0 Likes
Not applicable
Hi Travis, jptalledo

If you want use systick, you must use this command - this configures timer

SysTick_Config(SystemCoreClock/1000); // tick as 1ms 

Next, you must create handler:

void SysTick_Handler(void) {
if you don`t create handle you can have problem with dummy_handler
}


Now, you can read value from systick -> systick[0] = SysTick->VAL <- actual value systick

Best Regards,
Blackom
0 Likes
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
jptalledo wrote:
Travis

What do I need to include to get access to SYST_CVR. Currently I am getting an error about undeclared variable.

I am planning to read the syst_tick in an ISR.


Hi,

The code mentioned is not a direct plug n play but to give you an ideal how you can go about doing it. To some extend you had to adapt the code to your software with knowledge of C programming.
0 Likes
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
Hi jptalledo,

Pls take the advise from Blackom.
0 Likes
User8570
Level 3
Level 3
Thanks Travis. My concern is that my Dave's Systm002 block is running at 5 us. I would like to use 1 us or better resolution without changing the system tick timer.

any ideas? Can u provide an example using the CCU4 unit instead?
0 Likes
User8570
Level 3
Level 3
Travis/Blackhom

I have the systimer configure to 5 us to control other modules. My concern at this point is that timer can't be used for my other needs. I would like to setup another timer with a 0.1 or 0.5 us resolution. CCU4 modules fits the needs but the code I tried seems to do not work. Any advise or help how to setup that timer?
0 Likes
Not applicable
Hi jptalledo,
This is my code to run CCU4:


SCU_RESET->PRSET0 = SCU_RESET_PRSET0_CCU40RS_Msk; // reset CCU4
SCU_RESET->PRCLR0 = SCU_RESET_PRCLR0_CCU40RS_Msk; // end reset
SCU_CLK->CLKSET = SCU_CLK_CLKSET_CCUCEN_Msk; // connect 120Mhz clock to fccu
CCU40->GIDLS = CCU4_GIDLS_SS2I_Msk; // disconnect slice timer CC42 from CCU40
CCU40_CC42->PSC |= 0x0B< CCU40_CC42->TC = CCU4_CC4_TC_CLST_Msk; // set write shadow register to timer whan timer overflow
SCU_GENERAL->CCUCON = SCU_GENERAL_CCUCON_GSC40_Msk; // connect fccu clock to CCU40
CCU40_CC42->INTE = CCU4_CC4_INTE_PME_Msk; //set period match
CCU40->GIDLC = CCU4_GIDLC_SPRB_Msk; //turn on prescaler
CCU40->GIDLC = CCU4_GIDLC_CS2I_Msk; //set idle state timer4_2
CCU40_CC42->TCSET = CCU4_CC4_TCSET_TRBS_Msk; //turn on timer
CCU40_CC42->PRS=293;// set shadow register PR
CCU40->GCSS = CCU4_GCSS_S2SE_Msk; //this command copy value from shadow PR to timer
NVIC_SetPriority(CCU40_0_IRQn, 54); // set priority of IRQ timer
NVIC_EnableIRQ(CCU40_0_IRQn); //turn on IRQ timer

If you need 0.1 or 0.5 us resolution you must set prescaler on 0x03. you take 0.06us resolution(120Mhz/8=15Mhz).

Best regards,
Blackom
0 Likes
User8570
Level 3
Level 3
Blackhom

Thanks for the source code. I was able to get some interrupts and verifying by toggling a PIN (P1.1) on the Relax Kit. Unfortunately the lowest period I can get is 866 ns.

I changed the PSC to 3 and the PRS to 1,2,3 with the same results. Wondering if the prescaler is doing the correct thing.
0 Likes
Not applicable
Hi,
Yes it maybe true,because you can`t fast toggle pin in software mode. You can generate PWM and it will be more faster.

Best regards,
Blackom
0 Likes