Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8051

SDK • Re: Atomic register write

$
0
0

I guess my naivete didn't see the difference between doing a read-modify-write and doing a clear-set.
Advantage of clear-set is that while it is plainly not atomic with respect to the bits you are modifying, it avoids interaction with the other bits in the register (which in the general case, there could be some other activity modifying them - interrupt or other CPU - and the read/modify/write would cause updates to be lost).
I had considered the XOR, but worried since it wasn't deterministic. Calling it twice sets it back to the original value. If I'm only going to use it in one function then, yeah, I'll probably call the XOR and the beginning and end of the 16-bit transfer function. Still not crazy about non-deterministic behavior, though.
All depends on the reason for doing this. If you just care about atomic updates to the register, you could track the current value of the pull threshold in an ordinary variable and calculate the required xor - getting genuine atomic updates of the register but a cost in efficiency.

However, in your case where you already need the PIO to be idle before changing this you probably don't in fact care about atomicity of updates to the pull threshold. It's still worth using the masked updates to avoid disturbing the other bits in the register but you can choose any of the approaches: the clear-then-set is probably just as fast as a conventional read-modify-write; the xor-and-know-by-context will be faster; the xor-and-keep-track-in-a-variable slower but cleaner code.

Statistics: Posted by arg001 — Wed Apr 09, 2025 4:18 pm



Viewing all articles
Browse latest Browse all 8051

Trending Articles