[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] [bugs #12040] sbi in FAQ

From: Erik Walthinsen
Subject: Re: [avr-libc-dev] [bugs #12040] sbi in FAQ
Date: Mon, 28 Feb 2005 23:06:47 -0800
User-agent: Debian Thunderbird 1.0 (X11/20050116)

John Altstadt wrote:
Out of curiosity on my part, why do you think that
   PORTB |= 0x03;
   PORTB |= 0x01;
   PORTB |= 0x02;
are equivalent statements?

They either are or aren't depending on what register you're manipulating. You are totally correct, in certain registers or in hardware where PORTB timings are critical, the effects would different.

However, in the |= 0x03 case, the compiler read the register first, modifies it, then writes it out. That can also cause improper operation and side effects depending on the register being accessed.

So the question is can the compiler optimize the case where two bits are set or cleared by using sbi/cbi, without stomping on certain usages? I can see that either method can cause problems, the question is which one is more potentially harmful vs. the instruction savings?

The read/modify/write method takes longer and thus could cause problems especially in cases like the WDTCR's 4-cycle window. This can be avoided by explicitely using the two-operation method.

The dual-sbi/cbi method is a cycle shorter and the effect is essentially immediate. However, there would be no obvious way to revert to the r/m/w method, which could be bad. OTOH, the dual-sbi/cbi method only functions for the lower 32 registers, which in the few AVR devices I've checked are pretty much UART, TWI, and PORT registers. Not using r/m/w for two-bit operations on such registers may or may not be an issue, would need to think through more scenarios.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]