Avr studio compile problem?

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello!

I just installed Avrstudio 6 and have ran into a weird problem (I am used to Avr GCC on Mac and don't have much experience with avr studio). The following does not work (when I program the MCU = Attiny25).

void testSet(void){
	SETBIT(I2C_PORT,I2C_SDA);
}

void testClr(void){
	CLEARBIT(I2C_PORT,I2C_SDA);
}

int main(void)
{
	
	//init();
	DDRB = 0x18;
	
	for(;;)
	{
		if (bit_is_clear(PINB, PB0)){
			_delay_ms(SM_DELAY); //prell delay
			testClr();
			//CLEARBIT(I2C_PORT,I2C_SDA);
			while(bit_is_clear(PINB, PB0)); //until button is released
			_delay_ms(SM_DELAY); //prell delay
			testSet();
			//SETBIT(I2C_PORT,I2C_SDA);
		}
		
	}
	
	return 0;
}

But if I do like this. Then it works:

void testSet(void){
	SETBIT(I2C_PORT,I2C_SDA);
}

void testClr(void){
	CLEARBIT(I2C_PORT,I2C_SDA);
}

int main(void)
{
	
	//init();
	DDRB = 0x18;
	
	for(;;)
	{
		if (bit_is_clear(PINB, PB0)){
			_delay_ms(SM_DELAY); //prell delay
			//testClr();
			CLEARBIT(I2C_PORT,I2C_SDA);
			while(bit_is_clear(PINB, PB0)); //until button is released
			_delay_ms(SM_DELAY); //prell delay
			//testSet();
			SETBIT(I2C_PORT,I2C_SDA);
		}
		
	}
	
	return 0;
}

same deal if I move the

DDRB = 0x18; 

inside the init function.

Any suggestions to what the cause can be?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is this one of those spot the difference competitions? I'm having a hard time seeing what's different between those two (apart from the fact that both are incomplete and don't show header inclusion or how the macros used are defined).

EDIT: OK I gave in and copied the text to two files then diff'd them:

E:\>diff diff1.txt diff2.txt
19,20c19,20
<          testClr();
<          //CLEARBIT(I2C_PORT,I2C_SDA);
---
>          //testClr();
>          CLEARBIT(I2C_PORT,I2C_SDA);
23,24c23,24
<          testSet();
<          //SETBIT(I2C_PORT,I2C_SDA);
---
>          //testSet();
>          SETBIT(I2C_PORT,I2C_SDA);

So it would appear to be whether you call functions or not. That sounds like code being built for the wrong micro so the stack is not working.

I suppose it's just vaguely possible that the CRT for t25 (crtt25.o) is wrong and gives SP the wrong initial value. You are using AS6 SP2 and not just AS6 aren't you?

EDIT2: OK so I had to test that too. I build an "empty" program for t25 in AS6 SP2 and looked at the .lss. The CRT starts with:
0000001e <__ctors_end>:

  1e:	11 24       	eor	r1, r1
  20:	1f be       	out	0x3f, r1	; 63
  22:	cf ed       	ldi	r28, 0xDF	; 223
  24:	cd bf       	out	0x3d, r28	; 61

so SP being initialised to 0x00DF. The t25 datasheet shows 128 bytes for th2 5 model spanning 0x0060 to 0x00DF so that sure looks right (and the other init loops in the CRT are aimed at 0x0060 as expected).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
EDIT: OK I gave in and copied the text to two files then diff'd them

There are also online diff tools you can use, for example
http://www.diffchecker.com/E01UbZ42
They seem more convenient in cases like this.

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much clawson. Sorry for the incomplete info.

Quote:
You are using AS6 SP2 and not just AS6 aren't you?

Yes, should it make a difference?

If I repeat the EDIT2 test you made I get exactly the same code compiled as you posted.

(I have hooked up a switch and LED to the MCU that's how I can verify that it is not working as expected).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Post the .lss for the version that does not work.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Post the .lss for the version that does not work.
Clawson. Sorry for the late reply! I have been forced to work that last week. I really appreciate your help! Here is the.lss file. (I'm not sure if it got attached so I paste the code just in case)
GccApplication1.elf:     file format elf32-avr

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         0000019a  00000000  00000000  00000094  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .data         00000000  00800060  0000019a  0000022e  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000003  00800060  00800060  0000022e  2**0
                  ALLOC
  3 .stab         000006cc  00000000  00000000  00000230  2**2
                  CONTENTS, READONLY, DEBUGGING
  4 .stabstr      00000085  00000000  00000000  000008fc  2**0
                  CONTENTS, READONLY, DEBUGGING
  5 .debug_aranges 00000080  00000000  00000000  00000988  2**3
                  CONTENTS, READONLY, DEBUGGING
  6 .debug_info   0000059d  00000000  00000000  00000a08  2**0
                  CONTENTS, READONLY, DEBUGGING
  7 .debug_abbrev 000001d3  00000000  00000000  00000fa5  2**0
                  CONTENTS, READONLY, DEBUGGING
  8 .debug_line   0000038f  00000000  00000000  00001178  2**0
                  CONTENTS, READONLY, DEBUGGING
  9 .debug_frame  00000104  00000000  00000000  00001508  2**2
                  CONTENTS, READONLY, DEBUGGING
 10 .debug_str    0000019b  00000000  00000000  0000160c  2**0
                  CONTENTS, READONLY, DEBUGGING
 11 .debug_loc    00000142  00000000  00000000  000017a7  2**0
                  CONTENTS, READONLY, DEBUGGING

Disassembly of section .text:

00000000 <__vectors>:
   0:	0c 94 2a 00 	jmp	0x54	; 0x54 <__ctors_end>
   4:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
   8:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
   c:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  10:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  14:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  18:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  1c:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  20:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  24:	0c 94 6d 00 	jmp	0xda	; 0xda <__vector_9>
  28:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  2c:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  30:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  34:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  38:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  3c:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  40:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  44:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  48:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  4c:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>
  50:	0c 94 47 00 	jmp	0x8e	; 0x8e <__bad_interrupt>

00000054 <__ctors_end>:
  54:	11 24       	eor	r1, r1
  56:	1f be       	out	0x3f, r1	; 63
  58:	cf e5       	ldi	r28, 0x5F	; 95
  5a:	d4 e0       	ldi	r29, 0x04	; 4
  5c:	de bf       	out	0x3e, r29	; 62
  5e:	cd bf       	out	0x3d, r28	; 61

00000060 <__do_copy_data>:
  60:	10 e0       	ldi	r17, 0x00	; 0
  62:	a0 e6       	ldi	r26, 0x60	; 96
  64:	b0 e0       	ldi	r27, 0x00	; 0
  66:	ea e9       	ldi	r30, 0x9A	; 154
  68:	f1 e0       	ldi	r31, 0x01	; 1
  6a:	02 c0       	rjmp	.+4      	; 0x70 <__do_copy_data+0x10>
  6c:	05 90       	lpm	r0, Z+
  6e:	0d 92       	st	X+, r0
  70:	a0 36       	cpi	r26, 0x60	; 96
  72:	b1 07       	cpc	r27, r17
  74:	d9 f7       	brne	.-10     	; 0x6c <__do_copy_data+0xc>

00000076 <__do_clear_bss>:
  76:	10 e0       	ldi	r17, 0x00	; 0
  78:	a0 e6       	ldi	r26, 0x60	; 96
  7a:	b0 e0       	ldi	r27, 0x00	; 0
  7c:	01 c0       	rjmp	.+2      	; 0x80 <.do_clear_bss_start>

0000007e <.do_clear_bss_loop>:
  7e:	1d 92       	st	X+, r1

00000080 <.do_clear_bss_start>:
  80:	a3 36       	cpi	r26, 0x63	; 99
  82:	b1 07       	cpc	r27, r17
  84:	e1 f7       	brne	.-8      	; 0x7e <.do_clear_bss_loop>
  86:	0e 94 51 00 	call	0xa2	; 0xa2 
8a: 0c 94 cb 00 jmp 0x196 ; 0x196 <_exit> 0000008e <__bad_interrupt>: 8e: 0c 94 00 00 jmp 0 ; 0x0 <__vectors> 00000092 : void ADC_init(void); void init(void); void init(){ DDRB = 0x18; 92: 88 e1 ldi r24, 0x18 ; 24 94: 87 bb out 0x17, r24 ; 23 SETBIT(PORTB,PB4); //Test!! 96: c4 9a sbi 0x18, 4 ; 24 //i2c_init(); //ADC_init(); } 98: 08 95 ret 0000009a : void testSet(void){ SETBIT(I2C_PORT,I2C_SDA); 9a: c4 9a sbi 0x18, 4 ; 24 } 9c: 08 95 ret 0000009e : void testClr(void){ CLEARBIT(I2C_PORT,I2C_SDA); 9e: c4 98 cbi 0x18, 4 ; 24 } a0: 08 95 ret 000000a2
: int main(void) { //init(); DDRB = 0x18; a2: 88 e1 ldi r24, 0x18 ; 24 a4: 87 bb out 0x17, r24 ; 23 for(;;) { if (bit_is_clear(PINB, PB0)){ a6: b0 99 sbic 0x16, 0 ; 22 a8: fe cf rjmp .-4 ; 0xa6 #else //round up by default __ticks_dc = (uint32_t)(ceil(fabs(__tmp))); #endif __builtin_avr_delay_cycles(__ticks_dc); aa: 83 ec ldi r24, 0xC3 ; 195 ac: 99 e0 ldi r25, 0x09 ; 9 ae: 01 97 sbiw r24, 0x01 ; 1 b0: f1 f7 brne .-4 ; 0xae b2: 00 c0 rjmp .+0 ; 0xb4 b4: 00 00 nop _delay_ms(SM_DELAY); //prell delay testClr(); b6: 0e 94 4f 00 call 0x9e ; 0x9e //CLEARBIT(I2C_PORT,I2C_SDA); while(bit_is_clear(PINB, PB0)); //until button is released ba: b0 9b sbis 0x16, 0 ; 22 bc: fe cf rjmp .-4 ; 0xba be: 83 ec ldi r24, 0xC3 ; 195 c0: 99 e0 ldi r25, 0x09 ; 9 c2: 01 97 sbiw r24, 0x01 ; 1 c4: f1 f7 brne .-4 ; 0xc2 c6: 00 c0 rjmp .+0 ; 0xc8 c8: 00 00 nop _delay_ms(SM_DELAY); //prell delay testSet(); ca: 0e 94 4d 00 call 0x9a ; 0x9a ce: eb cf rjmp .-42 ; 0xa6 000000d0 : void i2c_init(void){ I2C_PORT_DIR = 0xC0;//I2C_SDA_MASK | I2C_SCL_MASK; d0: 80 ec ldi r24, 0xC0 ; 192 d2: 87 bb out 0x17, r24 ; 23 SETBIT(I2C_PORT, I2C_SDA); //SCL & SDA high d4: c4 9a sbi 0x18, 4 ; 24 SETBIT(I2C_PORT, I2C_SCL); d6: c3 9a sbi 0x18, 3 ; 24 } d8: 08 95 ret 000000da <__vector_9>: ISR(TIMER0_OVF_vect){ da: 1f 92 push r1 dc: 0f 92 push r0 de: 0f b6 in r0, 0x3f ; 63 e0: 0f 92 push r0 e2: 11 24 eor r1, r1 e4: 8f 93 push r24 e6: 9f 93 push r25 TCNT0 = 250; e8: 8a ef ldi r24, 0xFA ; 250 ea: 82 bf out 0x32, r24 ; 50 timeCounter++; ec: 80 91 60 00 lds r24, 0x0060 f0: 90 91 61 00 lds r25, 0x0061 f4: 01 96 adiw r24, 0x01 ; 1 f6: 90 93 61 00 sts 0x0061, r25 fa: 80 93 60 00 sts 0x0060, r24 } fe: 9f 91 pop r25 100: 8f 91 pop r24 102: 0f 90 pop r0 104: 0f be out 0x3f, r0 ; 63 106: 0f 90 pop r0 108: 1f 90 pop r1 10a: 18 95 reti 0000010c : 10c: 87 ea ldi r24, 0xA7 ; 167 10e: 91 e6 ldi r25, 0x61 ; 97 110: 01 97 sbiw r24, 0x01 ; 1 112: f1 f7 brne .-4 ; 0x110 114: 00 c0 rjmp .+0 ; 0x116 116: 00 00 nop void dacPause(void){ _delay_ms(DAC_DELAY); } 118: 08 95 ret 0000011a : void dacClkShft(void){ SETBIT(DAC_PORT, DAC_CLK); //high 11a: c0 9a sbi 0x18, 0 ; 24 CLEARBIT(DAC_PORT, DAC_CLK); //low 11c: c0 98 cbi 0x18, 0 ; 24 } 11e: 08 95 ret 00000120 : sei(); //enable interrupts } unsigned char eepromRead(unsigned int uiAddress){ cli(); //disable interrupts 120: f8 94 cli while (EECR & (1<<EEWE)); //wait for completion of prev. write 122: e1 99 sbic 0x1c, 1 ; 28 124: fe cf rjmp .-4 ; 0x122 EEAR = uiAddress; 126: 9f bb out 0x1f, r25 ; 31 128: 8e bb out 0x1e, r24 ; 30 EECR |=(1<<EERE); 12a: e0 9a sbi 0x1c, 0 ; 28 return EEDR; 12c: 8d b3 in r24, 0x1d ; 29 sei(); //enable interrupts } 12e: 08 95 ret 00000130 : { eepromWrite(i,0); } } void eepromWrite(unsigned int uiAddress, unsigned char ucData){ 130: 1f 93 push r17 132: cf 93 push r28 134: df 93 push r29 136: ec 01 movw r28, r24 138: 16 2f mov r17, r22 cli(); //disable interrupts 13a: f8 94 cli while(EECR & (1<<EEWE));//wait for completion of previous write 13c: e1 99 sbic 0x1c, 1 ; 28 13e: fe cf rjmp .-4 ; 0x13c if (eepromRead(uiAddress) != ucData){ // if data is not the same as we are trying to write 140: ce 01 movw r24, r28 142: 0e 94 90 00 call 0x120 ; 0x120 146: 81 17 cp r24, r17 148: 29 f0 breq .+10 ; 0x154 EEAR = uiAddress; 14a: df bb out 0x1f, r29 ; 31 14c: ce bb out 0x1e, r28 ; 30 EEDR = ucData; 14e: 1d bb out 0x1d, r17 ; 29 EECR |= (1<<EEMWE); //master write enable 150: e2 9a sbi 0x1c, 2 ; 28 EECR |= (1<<EEWE); //write enable 152: e1 9a sbi 0x1c, 1 ; 28 } sei(); //enable interrupts 154: 78 94 sei } 156: df 91 pop r29 158: cf 91 pop r28 15a: 1f 91 pop r17 15c: 08 95 ret 0000015e : SETBIT(DAC_PORT, DAC_CLK); //high CLEARBIT(DAC_PORT, DAC_CLK); //low } void resetToFactoryDefault(){ //!! this will delete ALL user presets!! 15e: cf 93 push r28 160: df 93 push r29 unsigned int i; for(i = 0; i < EEPROM_SIZE; ++i) 162: c0 e0 ldi r28, 0x00 ; 0 164: d0 e0 ldi r29, 0x00 ; 0 { eepromWrite(i,0); 166: ce 01 movw r24, r28 168: 60 e0 ldi r22, 0x00 ; 0 16a: 0e 94 98 00 call 0x130 ; 0x130 } void resetToFactoryDefault(){ //!! this will delete ALL user presets!! unsigned int i; for(i = 0; i < EEPROM_SIZE; ++i) 16e: 21 96 adiw r28, 0x01 ; 1 170: c0 38 cpi r28, 0x80 ; 128 172: d1 05 cpc r29, r1 174: c1 f7 brne .-16 ; 0x166 { eepromWrite(i,0); } } 176: df 91 pop r29 178: cf 91 pop r28 17a: 08 95 ret 0000017c : sei(); //enable interrupts } void ADC_init(void) { ADMUX = (1<<MUX0); //ADC1 PB2 17c: 81 e0 ldi r24, 0x01 ; 1 17e: 87 b9 out 0x07, r24 ; 7 ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);//|(1<<ADIE); 180: 97 e8 ldi r25, 0x87 ; 135 182: 96 b9 out 0x06, r25 ; 6 //timer TCCR0 = (1<<CS02) | (1<<CS00); //prescaler 1024 = 15625cps 184: 95 e0 ldi r25, 0x05 ; 5 186: 93 bf out 0x33, r25 ; 51 TIFR = (1<<TOV0); //clear pending interrupts 188: 88 bf out 0x38, r24 ; 56 TIMSK = (1<<TOIE0); //Enable timer 0 overflow 18a: 89 bf out 0x39, r24 ; 57 //ADC ADC_init(); 18c: 0e 94 be 00 call 0x17c ; 0x17c CLEARBIT(PINA,PA0); 190: c8 98 cbi 0x19, 0 ; 25 sei(); //global interrupt enable 192: 78 94 sei 194: 08 95 ret 00000196 <_exit>: 196: f8 94 cli 00000198 <__stop_program>: 198: ff cf rjmp .-2 ; 0x198 <__stop_program>
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK I think we need to know what "does not work" actually means now. There is little doubt that the main() loop is going to do:

         testClr();
  b6:   0e 94 4f 00    call   0x9e   ; 0x9e 
         //CLEARBIT(I2C_PORT,I2C_SDA);
         while(bit_is_clear(PINB, PB0)); //until button is released 
         testSet();
  ca:   0e 94 4d 00    call   0x9a   ; 0x9a 
  ce:   eb cf          rjmp   .-42        ; 0xa6 

which call to:

0000009e :

void testClr(void){
   CLEARBIT(I2C_PORT,I2C_SDA);
  9e:   c4 98          cbi   0x18, 4   ; 24
}
  a0:   08 95          ret 

and

0000009a :

void testSet(void){
   SETBIT(I2C_PORT,I2C_SDA);
  9a:   c4 9a          sbi   0x18, 4   ; 24
}
  9c:   08 95          ret

So bit 4 of PORTB will be affected by this code. I can only guess that "does not work" means that the timing is perhaps affected by the additional time it takes to CALL/RET in each case but that's just a difference of microseconds. Could it be that the system is sensitive to microsecond differences in operation?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Could it be that the system is sensitive to microsecond differences in operation?

Well right now the system consists of a switch and a LED.. I'm going to try to compile the same code on my Mac with AVR GCC one of the following days to see if the problem is still the same/determine if it could be hardware related.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, have tried now to compile the same file on my Mac (using Avr gcc). And when I download that .hex file to the Attiny it works as expected so I guess it must be something with the compiler?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oleoleo2 wrote:
And when I download that .hex file to the Attiny it works as expected so I guess it must be something with the compiler?
Tell us what you mean by "it works". What does the code do? What happens when it doesn't work?

EDIT: if you are having problems only when calling functions, perhaps you could try e.g. blink the LED a few times then call a function to manipulate the LED, then return and blink a few times more.

/Jakob Selbing