AnsweredAssumed Answered

Very short delays(us) with TPM Module

Question asked by Diego Colombo on Feb 11, 2014
Latest reply on Mar 10, 2014 by jeremyzhou

Hi to everybody here

Please, hints needed.

I want to implement precise delays from few us to 100us.This is for squeezing  consumption optimization in some peripheral ,following directives of a co-worker.

Of course it is not a disaster if i would wait 5us instead of 3us in some sequence.But it is mostly for me ,to learn to use the timer correctly.

Now i'm using a Freedom Board KL05Z,with core,bus and TPM clock=20.97 MHz,global optimization level=0

NB:the TPM0 prescaler is set to 0


I did a simple C function acting on TPM0 registers:

void Mydelay_usec(unsigned int del) {   TPM0_MOD=del;   TPM0_CNT =0x00;//clean counter   TPM0_SC   |=TPM_SC_CMOD(0x01)|TPM_SC_TOF_MASK ;//inc. on every edge,clean overflow flag,up   while(!(TPM0_SC & TPM_SC_TOF_MASK) );//wait for overflow   TPM0_SC   &=~(TPM_SC_CMOD_MASK);//stop the timer }      


I measure the delay by reading a  pulse width on a pin on the oscilloscope :

GPIOB_PDOR &=~0x200; Mydelay_usec((test_var);//<<<<<  GPIOB_PDOR |=0x200;      

I leave the function empty ,just to measure the time needed to call it ,and to switch the test pin.The pulse width is 1.5us

void Mydelay_usec(unsigned int del){}      

...i expected something less


With the function complete,if i call it with input parameter=1, the resulting time is about 4.1us that lowers to 3us with global optimization level=3

I don't have experience of ARM assembly.Looking the content of my function not far form assembly level, i expected less assembly instructions(see the alleged file).



Another issue is that incrementing the MOD value  by 1,does not correspond to a progressive increment of the pulse width-

It looks,that the overflow,and the related pulse width are varying only when the MOD increases by 8 (?) units.

I guess this is because of the time between two checks of the TOF flag in the while(..) loop,that is too long to resolve a count of few units.

So a loop shorter than a TPM count is needed, but this is impossible unless i slow the count with a prescaler.

What is the right approach in this case?Interrupt?I should ever check some flag changed by the ISR..


Thank you all.


Original Attachment has been moved to: