This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Stack Location (Cortex-M)

Hi guys,

I'm wishing to move the location of the stack but how is this possible? In the startup.s I have

                EXPORT  __initial_sp
Stack_Size      EQU     0x00000200

                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp

However there's no option for the location and nothing specific for the stack appears to show up in the generate scatter file.

I'm a little lost and would appreciate any help.

Many thanks

Parents
  • Thank you for the response.

    In my situation I'll be setting up both a main and process stack where resident bootloader code and interrupts will run in the main stack and application code in the process stack.

    I am using an NXP Cortex-M0 chip and so am relocating my vector table to RAM and plan on using the remaining space in the relocated 512B to store the main stack, leaving all remaining memory to the application and its process stack.

    My issue is in placing the start of the stack at the end of this 512B block. I have attempted to reserve space for the vector table (amongst other items) using code like this:

    AREA    VECTORS, NOINIT, DATA, READWRITE  ; Reserve space for vector table in RAM
    
                                    SPACE   0xBC
    

    However the stack always seems to locate its start at 0x0 + stacksize, as opposed to at the far end of the 512B block. I could just change the size of the stack to 0x200 perhaps, however if I ever used a heap etc this would mess-up any heap-stack collision detection.

    Any ideas?

Reply
  • Thank you for the response.

    In my situation I'll be setting up both a main and process stack where resident bootloader code and interrupts will run in the main stack and application code in the process stack.

    I am using an NXP Cortex-M0 chip and so am relocating my vector table to RAM and plan on using the remaining space in the relocated 512B to store the main stack, leaving all remaining memory to the application and its process stack.

    My issue is in placing the start of the stack at the end of this 512B block. I have attempted to reserve space for the vector table (amongst other items) using code like this:

    AREA    VECTORS, NOINIT, DATA, READWRITE  ; Reserve space for vector table in RAM
    
                                    SPACE   0xBC
    

    However the stack always seems to locate its start at 0x0 + stacksize, as opposed to at the far end of the 512B block. I could just change the size of the stack to 0x200 perhaps, however if I ever used a heap etc this would mess-up any heap-stack collision detection.

    Any ideas?

Children
  • Deconvoluting a little of what I said, here's my ASM code:

    ; Vector Table Mapped to Address 0 at Reset
    
                    AREA    RESET, DATA, READONLY
    
                    EXPORT  __Vectors
    __Vectors       DCD     __initial_sp              ; Top of Stack
                    DCD     Reset_Handler             ; Reset Handler
                    DCD     NMI_Handler               ; NMI Handler
                    DCD     HardFault_Handler         ; Hard Fault Handler
                    DCD     0                         ; Reserved
                    DCD     0                         ; Reserved
                    DCD     0                         ; Reserved
                    DCD     0                         ; Reserved
                    DCD     0                         ; Reserved
                    DCD     0                         ; Reserved
                    DCD     0                         ; Reserved
                    DCD     SVC_Handler               ; SVCall Handler
                    DCD     0                         ; Reserved
                    DCD     0                         ; Reserved
                    DCD     PendSV_Handler            ; PendSV Handler
                    DCD     SysTick_Handler           ; SysTick Handler
    
                    ; External Interrupts
                    DCD     WAKEUP_IRQHandler         ; 12 wakeup sources for all the
                    DCD     WAKEUP_IRQHandler         ; I/O pins starting from PIO0 (0:11)
                    DCD     WAKEUP_IRQHandler         ; all 40 are routed to the same ISR
                    DCD     WAKEUP_IRQHandler
                    DCD     WAKEUP_IRQHandler
                    DCD     WAKEUP_IRQHandler
                    DCD     WAKEUP_IRQHandler
                    DCD     WAKEUP_IRQHandler
                    DCD     WAKEUP_IRQHandler
                    DCD     WAKEUP_IRQHandler
                    DCD     WAKEUP_IRQHandler
                    DCD     WAKEUP_IRQHandler
                    DCD     I2C_IRQHandler            ; I2C
                    DCD     TIMER16_0_IRQHandler      ; 16-bit Timer0
                    DCD     TIMER16_1_IRQHandler      ; 16-bit Timer1
                    DCD     TIMER32_0_IRQHandler      ; 32-bit Timer0
                    DCD     TIMER32_1_IRQHandler      ; 32-bit Timer1
                    DCD     SSP_IRQHandler            ; SSP
                    DCD     UART0_IRQHandler          ; UART0
                    DCD     UART1_IRQHandler          ; UART1
                    DCD     COMP_IRQHandler           ; Comparator
                    DCD     ADC_IRQHandler            ; A/D Converter
                    DCD     WDT_IRQHandler            ; Watchdog timer
                    DCD     BOD_IRQHandler            ; Brown Out Detect
                    DCD     FMC_IRQHandler            ; IP2111 Flash Memory Controller
                    DCD     PIOINT0_IRQHandler        ; PIO INT0
                    DCD     PIOINT1_IRQHandler        ; PIO INT1
                    DCD     PIOINT2_IRQHandler        ; PIO INT2
                    DCD     PMU_IRQHandler            ; PMU/Wakeup
                    DCD     DMA_IRQHandler            ; DMA
                    DCD     RTC_IRQHandler            ; RTC
    
                    AREA    VECTORS, NOINIT, DATA, READWRITE  ; Reserve space for vector table in RAM
                    SPACE   0xBC
    
                    AREA    FIRMWARE, NOINIT, DATA, READWRITE ; Reserve space for Firmware Flashing in RAM
    
                    EXPORT  __UpdateBuffer
    __UpdateBuffer  SPACE   0x80            ; Reserve 128B of space as a firmware update buffer
    
    ; <h> Stack Configuration
    ;   <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
    ; </h>
                    EXPORT  __initial_sp
    Stack_Size      EQU     0x00000147
    
                    AREA    STACK, NOINIT, READWRITE, ALIGN=3
    Stack_Mem       SPACE   Stack_Size
    __initial_sp
    
                                    END
    

    As it stands, the start of the stack is always the same as the Stack_Size, in this case 0x147. I instead want it at 0x1FF with a size of 0x147. As you can see I'm attempting to reserve space, however it's not working so well.

  • Have you considered a scatter file for specifying where you want to locate different AREAs?

  • Yes, I will ultimately do this if I have no choice but I was hoping to keep within the default uVision layout - this is so that those who come after me will have less of a learning curve and the software is easier to maintain.

    It feels as though I should be able to reserve a block of space within an area without having to resort to scatter files.

    One thing I can say form all of this is that the scatter-file/memory allocation system in uVision could do with a rethink and overhaul. Ideally you want scatter file editing to be natively possible, as it seems, for me at least, I almost always end up having to implement my own.