How to call xTaskGetTickCount(); from isr?

fojtik wrote on Thursday, May 15, 2008:


I have aattempted to call xTaskGetTickCount() inside ISR. It causes RTOS to crash after several successfull calls. I hope that reading a single value should not do this.


rtel wrote on Thursday, May 15, 2008:

It will crash because of the critical section used.  You can:

1) Provide an new function xTaskGetTickCountFromISR() that does not use a critical section.
2) Remove the critical section if the tick count variable has a size that equals the word size of the architecture you are using.
3) Just make uxTickCount global.


incrediball wrote on Sunday, May 18, 2008:

I find your second suggestion interesting. This obviously works because incrementing the tick count variable is an atomic operation which cannot be interrupted. However, if this is a reasonable thing to do, why not optimize all the ports to do that?

ravaz wrote on Sunday, May 18, 2008:

Incremnting a vairables is not an atomic operation because it’s done in at least three instruction (load, increment and write back). Ovever the instruction xTaskGetTickCount is only reading the variable, and as said by richard if the processor as the same architecture size as the variable (ie both 32bits) this operation is atomic.

incrediball wrote on Wednesday, May 21, 2008:

Quite right. The early morning coffee hadn’t fully worked when I wrote that. (Obviously the increment is not going to be interrupted either because it is done by the interrupt.) Anyway, my question is still much the same: if it is valid to read the tick counter (because the read operation is atomic), why not optimize all the relevant ports to do that?

rtel wrote on Wednesday, May 21, 2008:

The read is only atomic when the variable is the natural word size for the architecture.  There is an option in to use either 16bit or 32bit variables for the tick count (32bit giving you the ability to specify much longer block periods).  An 8 or 16 bit device reading a 32bit tick count would not be atomic, hence the critical section.


incrediball wrote on Thursday, May 22, 2008:

I agree about the 8 and 16 bit devices, which is why I said "all the relevant ports". For all the 32 bit devices such as AT91SAM7, etc the critical section could be excluded (and in any other relevant places).

What actually happens if the variable is smaller than the natural word size? For example if the option to use 16 bit tick counts is used on the AT91SAM7, is that still atomic? It seems that 16 bit variables don’t seem to be handled as well by the ARM and the compiler usually produces larger code.

fojtik wrote on Friday, May 23, 2008:

Thank you very much for your reply. I suspected somethink like this. As far as 32 bit CPU has been used, critical section is not needed. I will prefer solution 1.