Yahoo Groups archive

AVR-Chat

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

Thread

TWI not working when optimization is on

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

Re: [AVR-Chat] TWI not working when optimization is on

2013-08-07 by John Samperi

At 09:47 PM 7/08/2013, you wrote:
>All works fine until I turn on optimization (-O).

You are turning it OFF!! The code will be HUGE and will not keep
some necessary tight timings. Very bad boy, use -Os at least
which is good for most jobs.

Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

RE: [AVR-Chat] TWI not working when optimization is on

2013-08-07 by Chuck Hackett

> From: John Samperi
>
> At 09:47 PM 7/08/2013, you wrote:
> >All works fine until I turn on optimization (-O).
> 
> You are turning it OFF!! The code will be HUGE and will not keep some
necessary
> tight timings. Very bad boy, use -Os at least which is good for most jobs.

Hi John,

I could be wrong, but, on page 86 of the GCC manual it says that "-O" (or
"-O1"):

"With ‘-O’, the compiler tries to reduce code size and execution time,
without performing any optimizations that take a great deal of compilation
time. ‘-O’ turns on the following optimization flags: ..."

For "-Os" it says:

"Optimize for size. ‘-Os’ enables all ‘-O2’ optimizations that do not
typically increase code size. It also performs further optimizations
designed to reduce code size."

As far as I can tell, "-O" turns on basic optimization.  When I use it no
optimization my code size is 73942 bytes (56.4% of an ATMega1284p) and 48660
bytes (37.1%) with "-O" specified.

So the code does get smaller with "-O" specified 

... and, just for completeness ... 44668 bytes (34.1%) with "-Os" specified
... :-)

... or did I misread something?
 
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

RE: [AVR-Chat] TWI not working when optimization is on

2013-08-07 by John Samperi

At 09:42 AM 8/08/2013, you wrote:
>I could be wrong, but, on page 86 of the GCC manual it says that "-O" (or
>"-O1"):

I use C under duress...but.. -O needs to be followed by another number
AFAIK like -O1, -O2, -O3 and -Os.

Maybe -O without anything else is equal to -O0.

I always use the AS4 default of -Os, in fact I will use that
even with the Code Red compiler or AS6 whenever I must use them.

The project I'm working on at the moment has TWI and I'm using -Os.

Maybe we will need to wait for a GCC guru to enlighten both of us. :-)




Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

RE: [AVR-Chat] TWI not working when optimization is on

2013-08-08 by Chuck Hackett

I just realized that I left out an important bit of information:

When the TWI "Start Failure" happens using the optimized (-O) code the
program is accessing an EEPROM that does not exist.  I would have expected
the failure of the slave address send (repeated NACKs received).

The reason the EEPROM does not exist is that it is mounted on an external
board that may, or may not be present.  The (repeated) NACK to the slave
address is normally detected and interpreted as "EEPROM (and hence the
external board) not present" and is a normal occurrence. 

From what I recall (I'll have to retest it) everything works (access to the
TWI LED controller and the TWI EEPROM) when the EEPROM is present.

The failure occurs when "-O" is used AND the EEPROM is not present ...

Sorry, I should have included this before ...
 
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

RE: [AVR-Chat] TWI not working when optimization is on

2013-08-08 by John Samperi

At 11:29 AM 8/08/2013, you wrote:
>The failure occurs when "-O" is used AND the EEPROM is not present ...

As a matter of interest how did you manage to get just the -O option
to be generated? Are you using a manual makefile?

There is no way of selecting just -O in Studio's Project 
Configuration Options,
I have it in front of me now.

The current levels of optimization are:
    * -O0 No optimization. This is the same as not specifying any 
optimization.
    * -01 Optimize. Reduces code size and execution time without 
performing any optimizations that take a great deal of compilation time.
    * -O2 Optimize even more. avr-gcc performs almost all 
optimizations that don't involve a space-time tradeoff.
    * -O3 Optimize yet more. This level performs all optimizations at 
-O2 along with -finline-functions and -frename-registers.
    * -Os Optimize for size. Enables all -O2 optimizations that don't 
increase code size. It also performs further optimizations designed 
to reduce code size.
For more information on optimization, see the 'man' pages for avr-gcc.



Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

RE: [AVR-Chat] TWI not working when optimization is on

2013-08-08 by Chuck Hackett

> From: John Samperi
> 
> At 11:29 AM 8/08/2013, you wrote:
> >The failure occurs when "-O" is used AND the EEPROM is not present ...
> 
> As a matter of interest how did you manage to get just the -O option to be
> generated? Are you using a manual makefile?

I use the "Code::Blocks" development environment and it can (as an option)
pass the "-O" to gcc.  It can also pass the "-O1" which, as I read the
manual, is the same as "-O".

BTW: I just tried "-Os" and I get the same failure ...

I have not tried forcing the WTI LED controller to not answer (it's an SMT
part on the same board as the ATMega1284p) but I assume I would get the same
results.

Have you ever tried your "-Os" optimized code to communicate with a TWI
device that was not present and recover?  (as opposed to "any error => give
up")
 
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

RE: [AVR-Chat] TWI not working when optimization is on

2013-08-08 by John Samperi

At 11:50 AM 8/08/2013, you wrote:
>Have you ever tried your "-Os" optimized code to communicate with a TWI
>device that was not present and recover?

If your TWI code doesn't have any time out and locks waiting for a
response then it won't matter if the code is optimised or not I guess.

If the device doesn't respond then the code should give up and return
an error code of sort.

So are you saying that if you turn optimization OFF (the code gets bigger)
the TWI still recovers without a device being present and if the optimization
is on (any of the 4 levels) then the TWI doesn't recover?

Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

RE: [AVR-Chat] TWI not working when optimization is on

2013-08-09 by Chuck Hackett

> From: John Samperi
> 
> At 11:50 AM 8/08/2013, you wrote:
> >Have you ever tried your "-Os" optimized code to communicate with a TWI
> >device that was not present and recover?
> 
> If your TWI code doesn't have any time out and locks waiting for a
response
> then it won't matter if the code is optimised or not I guess.
> 
> If the device doesn't respond then the code should give up and return an
error
> code of sort.

Hi John,

When the problem occurs (optimization on, no EEPROM present) what is
(apparently) happening is that the setting of the "Start Condition" fails
... it never even gets to the code that sends the slave address (immediately
after the setting of the Start Condition).

The "Send Start Condition" and "Send Slave Address" are contained in a loop
that counts "NACK" failures.  

If the EEPROM is busy with a write it will NACK (not respond) to the SLA
address.  If the EEPROM is present but busy with a write the loop will
eventually exit the loop successfully when the EEPROM finishes its write
sequence and ACKs the SLA.  

If the EEPROM is not present the loop will exit (after 200 attempts I think
at the moment) with a NACK indication which is taken to mean that the EEPROM
is not there.  I record the EEPROM as being absent and do not attempt to
communicate with it again until the next restart.

> So are you saying that if you turn optimization OFF (the code gets bigger)
the
> TWI still recovers without a device being present and if the optimization
is on
> (any of the 4 levels) then the TWI doesn't recover?

Yes ... when optimization is off and the EEPROM is not present the code
recovers normally (after 200 NACKS) and records the EEPROM as not present
without difficulty.
 
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

RE: [AVR-Chat] TWI not working when optimization is on

2013-08-09 by John Samperi

At 02:47 AM 10/08/2013, you wrote:
>Yes ... when optimization is off and the EEPROM is not present the code
>recovers normally (after 200 NACKS) and records the EEPROM as not present
>without difficulty.

Interesting but out of my expertise unfortunately. It seems strange
that this should happen as code would normally be better when optimised.

Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

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.