Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

TWI not working when optimization is on

2013-08-07 by Chuck Hackett

I have a large program that uses TWI to interface to an LED controller and a
24xx64 EEPROM.

All works fine until I turn on optimization (-O).  With optimization on I
get "Start Failures".  I am not using interrupts with the TWI interface
(wait loops).  I have code that waits for the interrupt bit to come on, etc.
as shown in the data sheet.

Here is one of the functions (c++):

uint8_t TWI_cl::TWI__SendStart( void )
{
    uint8_t		twst;

    TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);	//	send start condition

    while ((TWCR & _BV(TWINT)) == 0)
        ;							//	wait
for transmission
    twst = TW_STATUS;

    //
    //	Determine if we have entered the started state
    //
    switch (twst)
    {
    	case TW_START:
    	case TW_REP_START:
			State = TWIState_Started;
			break;
		default:
			break;
    }

    return twst;
}

I can post more code if needed but I thought that I would just give a high
level description to see if someone else has had a similar problem.

I have looked at the code emitted and it appears that the TWINT bit is being
reloaded correctly so it does not appear to be a "volatile" issue with the
TWCR register.

My guess is that there is some kind of timing issue going on ...

Processor:	Atmega1284p
WinAVR:	WinAVR-20100110

Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.