This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LAUNCHXL-F28379D: Time counter

Part Number: LAUNCHXL-F28379D
Other Parts Discussed in Thread: C2000WARE

Tool/software:

Hello there,

I would to use a timer (CpuTimer0 or any other timer) for a situation where I can start the timer/counter and let it go to a specific value and hold it there until my other condition met and reset it back to zero.

Could you assist me how to do that? I have experimented this by configuring CpuTimer0 for interrupt but what I really want is simple counter not the interrupt. 

Thank you,

  • Hi Sumit,

    I can start the timer/counter and let it go to a specific value

    Are you wanting to use the timer/counter to indicate a specific amount of elapsed time? Or are you really wanting to use the counter value for something?

    If you are trying to have the timer "go off" after x amount of time and not restart until a certain event, I believe you could do the following:

    1. Set up the CPU timer to interrupt every x seconds and start the timer. The timer_ex1_cputimers example in C2000ware can be used to guide with the configurations.
    2. In the Timer ISR perform any desired functionality, then call CPUTimer_stopTimer();
    3. When the condition is met, call CPUTimer_startTimer();

    Let me know if I am misunderstanding the application. If you are wanting the timer to increment somewhere in your code and not to be time based, you could simply increment a global variable instead until it reaches a certain value and clear it when your condition is met.

    Best Regards,

    Delaney

  • Hello Delaney,

    Are you wanting to use the timer/counter to indicate a specific amount of elapsed time? Or are you really wanting to use the counter value for something?

     - Yes, want to use the timer/counter to indicate a specific amount of elapsed time. 

    application scenario: I want to start the timer when a specific event happened (not receiving anything on Rx line) and keep it going till lets say some xxxx microseconds elapsed but I want it to stay at xxxx microseconds until some other event happens (Rx Interrupt) and when it does reset the timer back to zero. 

    Thanks

  • Hi Sumit,

    Alright, I would suggest going with the approach I mentioned previously then. You can set up the timer to have a period of xxxx microseconds, and in the CPUTimer ISR, call CPUTimer_stopTimer(); to stop it from counting. Then in your RX ISR, call CPUTimer_startTimer(); to start it over and begin counting again. 

    The only difference between this approach and what you are describing is that the CPUTimer module counts down rather than up. So initially it loads the period value into the TIM (counter) register and begins decrementing. When it reaches zero, the CPUTimer ISR will be called, in which case you can stop the timer from decrementing. Then when you start the timer again, it will reload the period value into the TIM (counter) register and begin decrementing again. Let me know if this would be an issue.

    Best Regards,

    Delaney

  • Delaney,

    Yes, this is what I am doing but I don't have the ISR (interrupt function) although I have enabled the TCR->TIE (CPU-Timer Interrupt Enabled).

    and using CpuTimer0Regs.TCR.bit.TSS to stop/start the timer and using CpuTimer0Regs.TCR.bit.TIF flag to check if counter has reached to zero.

    now my questions are:

    1. Do I really need a CPU Timer ISR?

    2. Lets say when counter is running (decrementing) and we do a stop (either using APIs you mentioned or the bit fields) and start it again, does the counter start from the beginning (meaning PRD (period) value will be reloaded automatically) or it will start from where it was left off?

    3. When counter reaches to 0, does it automatically reloaded with PRD and start decrementing again?

    4. Are there APIs for each (start from the beginning or resume from where it was stopped previously) ?

    Thanks,

  • Hi Sumit,

    Here are the answers to your questions:

    1. Do I really need a CPU Timer ISR?

    The only reason I could see for needing the ISR is to stop the timer from counting. If you want it to hold its value (of zero) until the other event starts it over, you will need to know when to stop the timer. If you want to do this in a polling loop in the background, that will also work. Now if you remove the interrupt and just want to do one polling loop that sets a global flag saying that the timer period has elapsed, then you could check this flag in your RX ISR, in which case you could restart the counter.

    2. Lets say when counter is running (decrementing) and we do a stop (either using APIs you mentioned or the bit fields) and start it again, does the counter start from the beginning (meaning PRD (period) value will be reloaded automatically) or it will start from where it was left off?

    If using the API, the CPUTimer_startTimer() would reload the period value and restart. The CPUTimer_resumeTimer() would be the case where it starts decrementing again from where it left off. If using bitfield code, you would just need to do the equivalent register reads/writes as is done in the CPUTimer_startTimer() function.

    3. When counter reaches to 0, does it automatically reloaded with PRD and start decrementing again?

    Yes, this happens at the start of the next timer input clock cycle.

    4. Are there APIs for each (start from the beginning or resume from where it was stopped previously) ?

    Yes, these would be CPUTimer_startTimer() and CPUTimer_resumeTimer() respectively.

    Best Regards,

    Delaney

  • Hello Delaney,

    Thank you for answering my questions.

    I wanted to know little bit more about the CPU Timer. Lets consider the below function:

    ConfigCpuTimer(&CpuTimer0, 200, 1500); //200 is the freq in MHz and 1500 is the period in microseconds

    Does the CpuTimer run in ticks? if yes how long is one tick in (microseconds)?

    Thanks, 

  • Hi Sumit,

    The CPU Timer module will be synchronized to the system clock (SYSCLK) of the device. If the SYSCLK is set to 200MHz, then the clock signal will have a rising edge every 5 nanoseconds. Every x clock cycles, the 32-bit value in the TIM register is decremented by 1. The value of x is determined by the 8-bit values written in:

    • The TDDRH field of the TPRH register (which is used as the most significant 8 bits of x)
    • The TDDR field of the TPR register (which is used as the least significant 8 bits of x, and +1 is added)

    The PSCH and PSC fields in the same registers keep track of how many SYSCLK cycles have elapsed by decrementing on each SYSCLK rising edge. When the PSCH and PSC fields are 0, the value in the TIM register is decremented and the PSCH and PSC fields are reloaded with the TDDRH and TDDR values. So yes, the CPU timer ticks according to the SYSCLK frequency * x.

    It may help to look through the register descriptions for the CPU timer registers in the F2837xD TRM linked here. See section System Control and Interrupt >> System Control Registers >> CPUTIMER_REGS Registers.

    Best Regards,

    Delaney

  • okay, so if I got it right it what you mean is:


    The CPU Timer module will be synchronized to the system clock (SYSCLK) of the device. If the SYSCLK is set to 200MHz, then the clock signal will have a rising edge every 5 nanoseconds. Every x clock cycles, the 32-bit value in the TIM register. 

    - did you mean to say "Every x clock cycles, the 32-bit value in the TIM register decrements by 1"

    lets consider ConfigCpuTimer(&CpuTimer0, 200, 2000); // PRD = 2000 microseconds

    lets say if I don't have any prescaler and divide-down values (let them be 0) then with 200 MHz clock (each cycle is 5ns long) it will take 2000 clock cycle to decrement the period value of 2000 microseconds to 0 (total of 2000 * 5 = 10,000 ns)

    But with TDDRH:TDDR being set to 4, it will take even longer to decrement the period to 0, 4 times the amount ( 2000 * (5*4) = 40,000 ns).

    and once PSCH:PSC decrements to 0 it will be reloaded with content of TDDRH:TDDR.

    Let me know if I got it wrong.

    Thanks

  • Hi Sumit,

    did you mean to say "Every x clock cycles, the 32-bit value in the TIM register decrements by 1"

    Yes, that is what I meant to say.

    And yes, what you are saying is correct. If no values for "x" are set, then the TIM register values will decrement once every clock cycle, which would be the quickest way to reach 0 when starting at the value programmed in the PRD register. Generally, it is easier to first look at the desired period (in seconds) and clock speed, then calculate the necessary values to write to the PRD and TPR registers.  

    Best Regards,

    Delaney

  • Thank you Delaney, I have closed this issue.