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.

GPTM timer interval load value when up counting

I'm using the timer module 0 to write my own delay routine that takes a value in miliseconds and poll the TATORIS flag busy waiting for the time out event.
All is fine when down counting (the prescaler acts as a true prescaler containing the least significant bits of the count according to the data sheet), and I use this code to calculate the start value to load into TAILR:

GPTM0->TAILR = millis*16000/256 - 1;          // down-counting (true prescaler)
GPTM0->TAPR = 255;

the formula I use is the following:

Ftimer = Fbus/((PR+1)(ILR+1))

But when up-counting, the prescaler acts as a timer extension and contains the most significant bits of the count, so the timer starts from 0x00 and counts up to the combination of TAPR and TAILR (the timer counts up to 0xFFFF and then increments the prescaler until the prescaler equals TAPR and then the timer counts up to TAILR,right?).

how can I calculate the value to load into TAILR?

Ftimer = Fbus/(n+1)

n is 0xPS.ILR (bit fields) 

  • Hello Luca

    Any specific reason why you need the up count when the down count is working as expected for the delay count?

    Regards
    Amit
  • Experience teaches that this "up count" (appears) easier.   (simply increment until the "match value" is reached.)

    Timer's implementation adds complications.

    So - perhaps a "wide timer" "loaded w/a value higher than ever expected to see use" (max_val) - and configured to "count down" UNTIL:

    (max_val - "computed" interval_load_val = desired duration) enables the (simpler) down-count while (likely) avoiding the compication wrought by the prescaler.

    Trick here is to pre-load the Timer w/a value which will never over-flow - and then subtract from that (high value) the number of timer ticks which equals the desired timer duration.   That calculated value - when reached - signals "timer match."   (different ARM MCUs are better suited to accommodate this method - I'm unsure about those here...)

  • Thinking about this bit more - the switch to "Wide Timer" and then the simple load of the Timer Start Register with a value equal to timer duration (system clock * timer ticks) should generate the Timer Interrupt when that timer's "Down Count" is exhausted.   (reaches zero)

    No pre-scalers (and their complications/management) are required via this method.

    Whether counting up - or counting down - use of the wide timer enables the SAME "timer tick" value to be employed. (thus removes the (seeming) advantage (directness) of the count-up method...)

  • Hello cb1

    Poster never clarified which device it is: 123 or 129?

    Regards
    Amit
  • Hi Amit,

    Indeed - nor the "need" for (more complex up-count) and rejection of the API. (seeks to write his own - rather than employ, "Tried/True/Tested.") Usually - such proves unwise...
  • Hello cb1

    Good point. The use of the DRM missed my attention

    Regards
    Amit