cancel
Showing results for 
Search instead for 
Did you mean: 

Question about DWT on STM32L431

RrW1
Associate III

I have a straightforward question regarding the DWT counter. I am using the DWT->CYCCNT counter to count clock cycle differences in the STM32L431CC, and I noticed that CYCCNT is 32-bits wide. If I were to use HSI16 as my internal clock source, the CYCCNT would still go up to the maximum value of a uint32_t which is 4294967295, and not 16000000 based on my HSI16 clock source right?

Thanks!

timestamp_1 = DWT->CYCCNT;
DoSomeTask();
timestamp_2 = DWT->CYCCNT;
 
if(timestamp_2 < timestamp_1)
{
   /* I originally filled 16000000 instead of MAX_INT here */
   timedifference = timestamp_2 + MAX_INT - timestamp_1;
}
else
{
   timedifference = timestamp_2 - timestamp_1;
}

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Correct.

Also, because it's 32-bits, you don't need to do any fancy logic based on if the new value is less than the old one. The overflow will get this correct.

uint32_t timestamp_1 = DWT->CYCCNT;
DoSomeTask();
uint32_t timestamp_2 = DWT->CYCCNT;
 
uint32_t duration_ticks = timestamp_2 - timestamp_1;

 Your code as written will be off by 1 in the case that timestamp_2 < timestamp_1.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

5 REPLIES 5
TDK
Guru

Correct.

Also, because it's 32-bits, you don't need to do any fancy logic based on if the new value is less than the old one. The overflow will get this correct.

uint32_t timestamp_1 = DWT->CYCCNT;
DoSomeTask();
uint32_t timestamp_2 = DWT->CYCCNT;
 
uint32_t duration_ticks = timestamp_2 - timestamp_1;

 Your code as written will be off by 1 in the case that timestamp_2 < timestamp_1.

If you feel a post has answered your question, please click "Accept as Solution".
RrW1
Associate III

Thanks for the answer TDK! I was not aware of that last part though, thanks for adding that to your answer too.

RrW1
Associate III

I have been thinking about this, and I am still curious if this would work for clock cycle counts at the higher extreme end. If, for example:

timestamp_1 = 0xFFFFFFFF;  
timestamp_2 = 0x00000001;
 
// duration_ticks = timestamp_2 - timestamp_1 = 0xFFFFFFFE;

yet, in the example above, it would only take 2 clock cycles to go from timestamp_1 to timestamp_2. If timestamp_1 + 2 clockcycles, it would be:

// timestamp_2 + 2 = 0xFFFFFFFF + 0x2 = 0x1 0000 0001
/* Since only the last 32-bits are included, that would mean DWT->CYCCNT would still hold
     the value 0x00000001 wouldn't it?
  */

I would still need the logic wouldn't I (which would include adding timestamp_2 by the MAX_UINT32 and then subtract that summation with timestamp_1? Or is my understanding about the 32-bit DWT->CYCCNT counter incorrect?

Thanks!

> timestamp_1 = 0xFFFFFFFF;

> timestamp_2 = 0x00000001;

> // duration_ticks = timestamp_2 - timestamp_1 = 0xFFFFFFFE;

This is not what duration_ticks would equal. It would equal 2.

> I would still need the logic wouldn't I

No, it will still give you the correct answer. Try it:

https://onlinegdb.com/ry_rlCPoP

In mathematical terms, results are calculated modulo 2^32.

If you feel a post has answered your question, please click "Accept as Solution".
RrW1
Associate III

Hi TDK, thanks for the example and explanation! I think I understand what you are talking about now.