bugAVR C Runtime Library - Bugs: bug #25778, Writing to SP loads the stack...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #25778: Writing to SP loads the stack pointer improperly

Submitter:  Fred Hansen <bldrcowboy>
Submitted:  Fri 06 Mar 2009 04:50:36 PM UTC
   
 
Category:  None Severity:  5 - Blocker
Priority:  9 - Immediate Item Group:  None
Status:  Fixed Assigned to:  arcanum
Percent Complete:  100% Open/Closed:  Closed
Release:  1.6.4 Fixed Release:  None

Sat 07 Mar 2009 05:32:28 AM UTC, comment #2: 

Fixed in the xmega patch to GCC.

Eric Weddington <arcanum>
Group administrator
Fri 06 Mar 2009 08:53:01 PM UTC, comment #1: 

This is a GCC problem, not with avr-libc.

I'm rebuilding now with a fixed patch, and will test soon.

Eric Weddington <arcanum>
Group administrator
Fri 06 Mar 2009 04:50:36 PM UTC, original submission:  

The attached list file shows an quick example of the bug. I'm loading the stack pointer at the beginning of a naked routine (used in the boot loader) by writing to the global SP with the statement:
    SP = RAMEND;

If you look at the assembly code it loads the bytes into the stack pointer in the wrong order:
  35 0006 8FEF      ldi r24,lo8(16383)
  36 0008 9FE3      ldi r25,hi8(16383)
  37 000a 9DBF      out 61-0,r25
  38 000c 8EBF      out (61)+1-0,r24
r424 contains the low byte of RAMEND (which is 0x3FFF) but it's loaded into the IO register 62 which is the upper byte of the stack pointer. r25 has the high byte and is loaded into the load byte.

The quick workaround that I've been using is to load the bytes explicitly:
    SPL = RAMEND & 0xFF;
    SPH = (RAMEND >> 8) & 0xFF;

I'm compiling for an XMega128A1. I'm using WinAVR 20081205.

The command line that performed this compile is:
avr-gcc -c -mmcu=atxmega128a1 -I. -gdwarf-2 -DF_CPU=29491200UL -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wundef -Wa,-adhlns=obj/.\Boot\SPBugExample.lst -I. -I.\FreeRTOS -I.\Boot -I.\Cmd -I.\Util -I.\Ctrl -std=gnu99 -fgnu89-inline -Os -MMD -MP -MF .dep/SPBugExample.o.d ./.\Boot\SPBugExample.c -o obj/.\Boot\SPBugExample.o

Fred Hansen <bldrcowboy>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #17600:  SPBugExample.c added by bldrcowboy (1KiB - application/octet-stream)
file #17601:  SPBugExample.lst added by bldrcowboy (2KiB - text/plain)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by arcanum (Posted a comment)
  • -email is unavailable- added by bldrcowboy (Submitted the item)
  •  

    Follow 10 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2009-03-07 arcanum StatusNone Fixed
        Percent Complete30% 100%
        Open/ClosedOpen Closed
    2009-03-06 arcanum CategoryLibrary None
        Severity3 - Normal 5 - Blocker
        Priority5 - Normal 9 - Immediate
        Percent Complete0% 30%
        Assigned toNone arcanum
    2009-03-06 bldrcowboy Attached File- Added SPBugExample.c, #17600
        Attached File- Added SPBugExample.lst, #17601

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code