PIC32 CP0 operations

crc1021 wrote on Monday, February 15, 2010:

The code for the PIC32 in portmacro.h does a read-modify-write on CP0 to control stuff like interrupts, which does not seem safe.  For example this code enables interrupts.
    ulStatus = _CP0_GET_STATUS()
    ulStatus &= ~portALL_IPL_BITS;
    _CP0_SET_STATUS( ulStatus );
why do that rather than this

rtel wrote on Tuesday, February 16, 2010:

The code generated by your suggestion is:

9D000020  40026000   mfc0        v0,Status << read
9D000024  00401821   addu        v1,v0,zero
9D000028  3C02FFFF   lui         v0,0xffff
9D00002C  344203FF   ori         v0,v0,0x3ff
9D000030  00621024   and         v0,v1,v0
9D000034  40826000   mtc0        v0,Status << write

so it is still performing a read/modify/write sequence so I’m not sure what is to be gained.

That said, the original code is probably less than ideal, however I don’t think it is problematic because each task has its own Status value, and its own stack onto which ulStatus is stored.


crc1021 wrote on Tuesday, February 16, 2010:

Thanks for the reply, I should have looked at the assembly first.
I’m still left wondering how atomic the operation is, but the is independent of FreeRTOS.