Stack underflow with function pointers

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

I'm having an unusual problem with a project I'm working on. Basically I need is the ability to be able to register callback functions, store them in an array, and execute them in order. After writing my program I found that the function pointers are getting assigned improperly. A similar but much simplified version gives the same, stack underflow, error. This is an exerpt of the .lss file, minus the initialization code.

0000008e :

void TestPortB( void )
{
	static uint8_t toggle = 0b01010101;
	toggle = ~toggle;
  8e:	80 91 60 00 	lds	r24, 0x0060
  92:	80 95       	com	r24
  94:	80 93 60 00 	sts	0x0060, r24
	PORTB = toggle;
  98:	88 bb       	out	0x18, r24	; 24
  9a:	08 95       	ret

0000009c :
	return;
}

void TestPortD( void )
{
	static uint8_t toggle = 0b11000011;
	toggle = ~toggle;
  9c:	80 91 61 00 	lds	r24, 0x0061
  a0:	80 95       	com	r24
  a2:	80 93 61 00 	sts	0x0061, r24
	PORTD = toggle;
  a6:	82 bb       	out	0x12, r24	; 18
  a8:	08 95       	ret

000000aa 
: return; } int main( void ) { aa: cf e5 ldi r28, 0x5F ; 95 ac: d4 e0 ldi r29, 0x04 ; 4 ae: de bf out 0x3e, r29 ; 62 b0: cd bf out 0x3d, r28 ; 61 DDRB = 0xFF; b2: 8f ef ldi r24, 0xFF ; 255 b4: 87 bb out 0x17, r24 ; 23 DDRD = 0xFF; b6: 81 bb out 0x11, r24 ; 17 functions[ 0 ] = &TestPortB; b8: 87 e4 ldi r24, 0x47 ; 71 ba: 90 e0 ldi r25, 0x00 ; 0 bc: 90 93 63 00 sts 0x0063, r25 c0: 80 93 62 00 sts 0x0062, r24 functions[ 1 ] = &TestPortD; c4: 8e e4 ldi r24, 0x4E ; 78 c6: 90 e0 ldi r25, 0x00 ; 0 c8: 90 93 65 00 sts 0x0065, r25 cc: 80 93 64 00 sts 0x0064, r24 d0: c1 e0 ldi r28, 0x01 ; 1 d2: 02 e6 ldi r16, 0x62 ; 98 d4: 10 e0 ldi r17, 0x00 ; 0 while( 1 ) { uint8_t index = 0; for( index = 0; index < 2; index++ ) { testFunction execute = functions[ index ]; d6: d8 01 movw r26, r16 d8: ed 91 ld r30, X+ da: fd 91 ld r31, X+ dc: 8d 01 movw r16, r26 ( *execute )(); de: 09 95 icall e0: c1 50 subi r28, 0x01 ; 1 e2: c7 ff sbrs r28, 7 e4: f8 cf rjmp .-16 ; 0xd6 e6: f4 cf rjmp .-24 ; 0xd0

The thing to note is that the array of function pointers, functions has the wrong values for the beginnings of the functions; indeed, the program is jumping back into the vector table.

The question is this...what am I doing that is causing this, why are the function pointers incorrect. As far as I can tell everything else is working properly. Nevertheless, in AVR Studio, the program is arriving at the functions just as it should, albeit after giving the stack underflow error. Quite honestly at the moment I'm out of ideas.

Martin Jay McKee

As with most things in engineering, the answer is an unabashed, "It depends."

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

The function pointers are correct, the flash memory is accessed with word addresses while the listing shows byte addresses. So e.g., TestPortB is at 0x47 in the array and 2*0x47 = 0x8e which is the byte address of that function.

The underflow report is a problem with AVR Studio:
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=41875

https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=43557
/Lars

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

Word vs. Byte, bites me again...
Oh well, good to know that my code wasn't completely off.

I had looked for possible AVR Studio related sources for the underflow, as it shouldn't be happening, but I hadn't found that... Thank you.

Martin Jay McKee

As with most things in engineering, the answer is an unabashed, "It depends."