Pic32MX port pragmatics

randers wrote on Sunday, December 21, 2008:

By analyzing the Pic32 demo I found lines as follows in the demo source partest.c:
This means direct access to the SFR’s.
Each access results in three machine instuctions. So it is not atomic.
So, why direct SFR-access is commented with
“Use of the … register removes the need to use a critical section”?

In the official documentation "Pic32 Family Reference Manual 61132B" is to find:
"A write to a PORT register writes to the corresponding LAT register (PORT data latch)." …
"A write to a PORT register is the effectively the same as a write to a LAT register."

So I think “PORTA = value;” is effectively the same as “LATA = value”.
(Although the resulting machine instructions are not identical!)
Confirming that, isn’t it more pragmatic, to use the library functions, documented in
“32-bit-Peripheral-Library-Guide” and implemted in “…\pic32mx\include\peripheral\ports.h”
with defines like
#define mPORTAWrite(_lat)    (LATA = (unsigned int)(_lat))

Conclusio: I would prefer to use "mPORTAWrite(value);" instead of "PORTA = value;".
And I need the confirmation: "It is effectively the same!"


davedoors wrote on Sunday, December 21, 2008:

Using the set and clear registers means you write just the bits you want to set or clear. There is no read modify write operation so there is no mutual exclusion issue. It will work even if the operation is interrupted by another task or an interrupt.

randers wrote on Monday, December 22, 2008:

I originally wanted to critisize, that the comment "…removes the need to use a critical section…" could lead to the opinion,
that (only) the special statement "PORTA = value;" is the right way.
For someone, who is new to a CPU/MCU/OS, it is important to understand the "little differences".
But I can not see effective differences between "PORTA = value;", "LATA = value;" and finally "mPORTAWrite(value);".
So I think, in all these ways there is no need for a critical section. (This is by MCU-design…)
And finally, "thinking abstract, thinking in C" I find "mPORTAWrite(value);" is the best pragmatic way for "putting
some LEDs on/off". And of course mPORTASetBits(), mPORTAClearBits(), mPORTAToggleBits(), …
That is, what I want to have confirmed.