How to use __initial_sp and Stack_Mem inside C file - Page 1
Forum section: electronics / general

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: January 07, 2024, 01:16:14 pm
content: HiI want to use Stack_Mem and __initial_sp inside the c file for a stm32 project using keil Ide,I have this simple main fileCode:#include <stdint.h>extern uint32_t __initial_sp; // Located in the startup codeextern uint32_t Stack_Mem; // Located in the startup codeint main(void){uint32_t mystk = (uint32_t)&Stack_Mem;uint32_t stack_start_addr = (uint32_t)&__initial_sp;return 0;}the documentation says that I should EXPORT __initial_sp and Stack_Mem in startup.s asm file too,something like thisCode:; EXPORT the Stack_Mem and __initial_sp symbols for use in C filesEXPORT Stack_MemEXPORT __initial_spThe actual startup.s codeCode:;******************** (C) COPYRIGHT 2011 STMicroelectronics ********************;* File Name : startup_stm32f10x_md.s;* Author : MCD Application Team;* Version : V3.5.1;* Date : 08-September-2021;* Description : STM32F10x Medium Density Devices vector table for MDK-ARM;* toolchain.;* This module performs:;* - Set the initial SP;* - Set the initial PC == Reset_Handler;* - Set the vector table entries with the exceptions ISR address;* - Configure the clock system;* - Branches to __main in the C library (which eventually;* calls main()).;* After Reset the CortexM3 processor is in Thread mode,;* priority is Privileged, and the Stack is set to Main.;* <<< Use Configuration Wizard in Context Menu >>>;*******************************************************************************;*;* Copyright (c) 2011 STMicroelectronics.;* All rights reserved.;*;* This software is licensed under terms that can be found in the LICENSE file;* in the root directory of this software component.;* If no LICENSE file comes with this software, it is provided AS-IS.;;*******************************************************************************; Amount of memory (in bytes) allocated for Stack; Tailor this value to your application needs; <h> Stack Configuration; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>; </h>Stack_Size EQU 0x00000400AREA STACK, NOINIT, READWRITE, ALIGN=3Stack_Mem SPACE Stack_Size__initial_sp; <h> Heap Configuration; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>; </h>Heap_Size EQU 0x00000200AREA HEAP, NOINIT, READWRITE, ALIGN=3__heap_baseHeap_Mem SPACE Heap_Size__heap_limitPRESERVE8THUMB; Vector Table Mapped to Address 0 at ResetAREA RESET, DATA, READONLYEXPORT __VectorsEXPORT __Vectors_EndEXPORT __Vectors_Size__Vectors DCD __initial_sp ; Top of StackDCD Reset_Handler ; Reset HandlerDCD NMI_Handler ; NMI HandlerDCD HardFault_Handler ; Hard Fault HandlerDCD MemManage_Handler ; MPU Fault HandlerDCD BusFault_Handler ; Bus Fault HandlerDCD UsageFault_Handler ; Usage Fault HandlerDCD 0 ; ReservedDCD 0 ; ReservedDCD 0 ; ReservedDCD 0 ; ReservedDCD SVC_Handler ; SVCall HandlerDCD DebugMon_Handler ; Debug Monitor HandlerDCD 0 ; ReservedDCD PendSV_Handler ; PendSV HandlerDCD SysTick_Handler ; SysTick Handler; External InterruptsDCD WWDG_IRQHandler ; Window WatchdogDCD PVD_IRQHandler ; PVD through EXTI Line detectDCD TAMPER_IRQHandler ; TamperDCD RTC_IRQHandler ; RTCDCD FLASH_IRQHandler ; FlashDCD RCC_IRQHandler ; RCCDCD EXTI0_IRQHandler ; EXTI Line 0DCD EXTI1_IRQHandler ; EXTI Line 1DCD EXTI2_IRQHandler ; EXTI Line 2DCD EXTI3_IRQHandler ; EXTI Line 3DCD EXTI4_IRQHandler ; EXTI Line 4DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7DCD ADC1_2_IRQHandler ; ADC1_2DCD USB_HP_CAN1_TX_IRQHandler ; USB High Priority or CAN1 TXDCD USB_LP_CAN1_RX0_IRQHandler ; USB Low Priority or CAN1 RX0DCD CAN1_RX1_IRQHandler ; CAN1 RX1DCD CAN1_SCE_IRQHandler ; CAN1 SCEDCD EXTI9_5_IRQHandler ; EXTI Line 9..5DCD TIM1_BRK_IRQHandler ; TIM1 BreakDCD TIM1_UP_IRQHandler ; TIM1 UpdateDCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and CommutationDCD TIM1_CC_IRQHandler ; TIM1 Capture CompareDCD TIM2_IRQHandler ; TIM2DCD TIM3_IRQHandler ; TIM3DCD TIM4_IRQHandler ; TIM4DCD I2C1_EV_IRQHandler ; I2C1 EventDCD I2C1_ER_IRQHandler ; I2C1 ErrorDCD I2C2_EV_IRQHandler ; I2C2 EventDCD I2C2_ER_IRQHandler ; I2C2 ErrorDCD SPI1_IRQHandler ; SPI1DCD SPI2_IRQHandler ; SPI2DCD USART1_IRQHandler ; USART1DCD USART2_IRQHandler ; USART2DCD USART3_IRQHandler ; USART3DCD EXTI15_10_IRQHandler ; EXTI Line 15..10DCD RTCAlarm_IRQHandler ; RTC Alarm through EXTI LineDCD USBWakeUp_IRQHandler ; USB Wakeup from suspend__Vectors_End__Vectors_Size EQU __Vectors_End - __VectorsAREA |.text|, CODE, READONLY; Reset handlerReset_Handler PROCEXPORT Reset_Handler IMPORT __mainIMPORT SystemInitLDR R0, =SystemInitBLX R0LDR R0, =__mainBX R0ENDP; Dummy Exception Handlers (infinite loops which can be modified)NMI_Handler PROCEXPORT NMI_Handler B .ENDPHardFault_Handler\PROCEXPORT HardFault_Handler B .ENDPMemManage_Handler\PROCEXPORT MemManage_Handler B .ENDPBusFault_Handler\PROCEXPORT BusFault_Handler B .ENDPUsageFault_Handler\PROCEXPORT UsageFault_Handler B .ENDPSVC_Handler PROCEXPORT SVC_Handler B .ENDPDebugMon_Handler\PROCEXPORT DebugMon_Handler B .ENDPPendSV_Handler PROCEXPORT PendSV_Handler B .ENDPSysTick_Handler PROCEXPORT SysTick_Handler B .ENDPDefault_Handler PROCEXPORT WWDG_IRQHandler EXPORT PVD_IRQHandler EXPORT TAMPER_IRQHandler EXPORT RTC_IRQHandler EXPORT FLASH_IRQHandler EXPORT RCC_IRQHandler EXPORT EXTI0_IRQHandler EXPORT EXTI1_IRQHandler EXPORT EXTI2_IRQHandler EXPORT EXTI3_IRQHandler EXPORT EXTI4_IRQHandler EXPORT DMA1_Channel1_IRQHandler EXPORT DMA1_Channel2_IRQHandler EXPORT DMA1_Channel3_IRQHandler EXPORT DMA1_Channel4_IRQHandler EXPORT DMA1_Channel5_IRQHandler EXPORT DMA1_Channel6_IRQHandler EXPORT DMA1_Channel7_IRQHandler EXPORT ADC1_2_IRQHandler EXPORT USB_HP_CAN1_TX_IRQHandler EXPORT USB_LP_CAN1_RX0_IRQHandler EXPORT CAN1_RX1_IRQHandler EXPORT CAN1_SCE_IRQHandler EXPORT EXTI9_5_IRQHandler EXPORT TIM1_BRK_IRQHandler EXPORT TIM1_UP_IRQHandler EXPORT TIM1_TRG_COM_IRQHandler EXPORT TIM1_CC_IRQHandler EXPORT TIM2_IRQHandler EXPORT TIM3_IRQHandler EXPORT TIM4_IRQHandler EXPORT I2C1_EV_IRQHandler EXPORT I2C1_ER_IRQHandler EXPORT I2C2_EV_IRQHandler EXPORT I2C2_ER_IRQHandler EXPORT SPI1_IRQHandler EXPORT SPI2_IRQHandler EXPORT USART1_IRQHandler EXPORT USART2_IRQHandler EXPORT USART3_IRQHandler EXPORT EXTI15_10_IRQHandler EXPORT RTCAlarm_IRQHandler EXPORT USBWakeUp_IRQHandler WWDG_IRQHandlerPVD_IRQHandlerTAMPER_IRQHandlerRTC_IRQHandlerFLASH_IRQHandlerRCC_IRQHandlerEXTI0_IRQHandlerEXTI1_IRQHandlerEXTI2_IRQHandlerEXTI3_IRQHandlerEXTI4_IRQHandlerDMA1_Channel1_IRQHandlerDMA1_Channel2_IRQHandlerDMA1_Channel3_IRQHandlerDMA1_Channel4_IRQHandlerDMA1_Channel5_IRQHandlerDMA1_Channel6_IRQHandlerDMA1_Channel7_IRQHandlerADC1_2_IRQHandlerUSB_HP_CAN1_TX_IRQHandlerUSB_LP_CAN1_RX0_IRQHandlerCAN1_RX1_IRQHandlerCAN1_SCE_IRQHandlerEXTI9_5_IRQHandlerTIM1_BRK_IRQHandlerTIM1_UP_IRQHandlerTIM1_TRG_COM_IRQHandlerTIM1_CC_IRQHandlerTIM2_IRQHandlerTIM3_IRQHandlerTIM4_IRQHandlerI2C1_EV_IRQHandlerI2C1_ER_IRQHandlerI2C2_EV_IRQHandlerI2C2_ER_IRQHandlerSPI1_IRQHandlerSPI2_IRQHandlerUSART1_IRQHandlerUSART2_IRQHandlerUSART3_IRQHandlerEXTI15_10_IRQHandlerRTCAlarm_IRQHandlerUSBWakeUp_IRQHandlerB .ENDPALIGN;*******************************************************************************; User Stack and Heap initialization;*******************************************************************************IF :DEF:__MICROLIBEXPORT __initial_spEXPORT __heap_baseEXPORT __heap_limitELSEIMPORT __use_two_region_memoryEXPORT __user_initial_stackheap__user_initial_stackheapLDR R0, = Heap_MemLDR R1, =(Stack_Mem + Stack_Size)LDR R2, = (Heap_Mem + Heap_Size)LDR R3, = Stack_MemBX LRALIGNENDIFENDBut no matter where I put it, it would generate errors, so do you have any Ideas?

author: mikerj
author_rank: Super Contributor
timestamp: January 09, 2024, 03:09:04 pm
content: You must ensure that the EXPORT keyword and symbol are correctly indented, if you put it at the start of the line it will be treated as a label and opcode and will produce an error:This will not work:Code:IF :DEF:__MICROLIBEXPORT Stack_MemEXPORT __initial_spEXPORT __heap_baseEXPORT __heap_limitELSEThis will work:Code:IF :DEF:__MICROLIBEXPORT Stack_MemEXPORT __initial_spEXPORT __heap_baseEXPORT __heap_limitELSE

author: Tation
author_rank: Regular Contributor
timestamp: January 09, 2024, 04:43:25 pm
content: Knowing the errors reported by the IDE will help...We need to know if you are using Microlib or not, as the .s file you posted has an IF depending on this (IF :DEF:__MICROLIB).Maybe you can post the minimum μVision project as a whole...In any case, have you read:https://developer.arm.com/documentation/dui0475/g/CJAGBBEG?

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: January 10, 2024, 09:12:03 pm
content: Thanks mikerj for the updateThis is the keil default stm32f103rb starup.s file contentCode:;******************** (C) COPYRIGHT 2011 STMicroelectronics ********************;* File Name : startup_stm32f10x_md.s;* Author : MCD Application Team;* Version : V3.5.1;* Date : 08-September-2021;* Description : STM32F10x Medium Density Devices vector table for MDK-ARM;* toolchain.;* This module performs:;* - Set the initial SP;* - Set the initial PC == Reset_Handler;* - Set the vector table entries with the exceptions ISR address;* - Configure the clock system;* - Branches to __main in the C library (which eventually;* calls main()).;* After Reset the CortexM3 processor is in Thread mode,;* priority is Privileged, and the Stack is set to Main.;* <<< Use Configuration Wizard in Context Menu >>>;*******************************************************************************;*;* Copyright (c) 2011 STMicroelectronics.;* All rights reserved.;*;* This software is licensed under terms that can be found in the LICENSE file;* in the root directory of this software component.;* If no LICENSE file comes with this software, it is provided AS-IS.;;*******************************************************************************; Amount of memory (in bytes) allocated for Stack; Tailor this value to your application needs; <h> Stack Configuration; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>; </h>Stack_Size EQU 0x00000400AREA STACK, NOINIT, READWRITE, ALIGN=3Stack_Mem SPACE Stack_Size__initial_sp; <h> Heap Configuration; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>; </h>Heap_Size EQU 0x00000200AREA HEAP, NOINIT, READWRITE, ALIGN=3__heap_baseHeap_Mem SPACE Heap_Size__heap_limitPRESERVE8THUMB; Vector Table Mapped to Address 0 at ResetAREA RESET, DATA, READONLYEXPORT __VectorsEXPORT __Vectors_EndEXPORT __Vectors_Size__Vectors DCD __initial_sp ; Top of StackDCD Reset_Handler ; Reset HandlerDCD NMI_Handler ; NMI HandlerDCD HardFault_Handler ; Hard Fault HandlerDCD MemManage_Handler ; MPU Fault HandlerDCD BusFault_Handler ; Bus Fault HandlerDCD UsageFault_Handler ; Usage Fault HandlerDCD 0 ; ReservedDCD 0 ; ReservedDCD 0 ; ReservedDCD 0 ; ReservedDCD SVC_Handler ; SVCall HandlerDCD DebugMon_Handler ; Debug Monitor HandlerDCD 0 ; ReservedDCD PendSV_Handler ; PendSV HandlerDCD SysTick_Handler ; SysTick Handler; External InterruptsDCD WWDG_IRQHandler ; Window WatchdogDCD PVD_IRQHandler ; PVD through EXTI Line detectDCD TAMPER_IRQHandler ; TamperDCD RTC_IRQHandler ; RTCDCD FLASH_IRQHandler ; FlashDCD RCC_IRQHandler ; RCCDCD EXTI0_IRQHandler ; EXTI Line 0DCD EXTI1_IRQHandler ; EXTI Line 1DCD EXTI2_IRQHandler ; EXTI Line 2DCD EXTI3_IRQHandler ; EXTI Line 3DCD EXTI4_IRQHandler ; EXTI Line 4DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7DCD ADC1_2_IRQHandler ; ADC1_2DCD USB_HP_CAN1_TX_IRQHandler ; USB High Priority or CAN1 TXDCD USB_LP_CAN1_RX0_IRQHandler ; USB Low Priority or CAN1 RX0DCD CAN1_RX1_IRQHandler ; CAN1 RX1DCD CAN1_SCE_IRQHandler ; CAN1 SCEDCD EXTI9_5_IRQHandler ; EXTI Line 9..5DCD TIM1_BRK_IRQHandler ; TIM1 BreakDCD TIM1_UP_IRQHandler ; TIM1 UpdateDCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and CommutationDCD TIM1_CC_IRQHandler ; TIM1 Capture CompareDCD TIM2_IRQHandler ; TIM2DCD TIM3_IRQHandler ; TIM3DCD TIM4_IRQHandler ; TIM4DCD I2C1_EV_IRQHandler ; I2C1 EventDCD I2C1_ER_IRQHandler ; I2C1 ErrorDCD I2C2_EV_IRQHandler ; I2C2 EventDCD I2C2_ER_IRQHandler ; I2C2 ErrorDCD SPI1_IRQHandler ; SPI1DCD SPI2_IRQHandler ; SPI2DCD USART1_IRQHandler ; USART1DCD USART2_IRQHandler ; USART2DCD USART3_IRQHandler ; USART3DCD EXTI15_10_IRQHandler ; EXTI Line 15..10DCD RTCAlarm_IRQHandler ; RTC Alarm through EXTI LineDCD USBWakeUp_IRQHandler ; USB Wakeup from suspend__Vectors_End__Vectors_Size EQU __Vectors_End - __VectorsAREA |.text|, CODE, READONLY; Reset handlerReset_Handler PROCEXPORT Reset_Handler IMPORT __mainIMPORT SystemInitLDR R0, =SystemInitBLX R0LDR R0, =__mainBX R0ENDP; Dummy Exception Handlers (infinite loops which can be modified)NMI_Handler PROCEXPORT NMI_Handler B .ENDPHardFault_Handler\PROCEXPORT HardFault_Handler B .ENDPMemManage_Handler\PROCEXPORT MemManage_Handler B .ENDPBusFault_Handler\PROCEXPORT BusFault_Handler B .ENDPUsageFault_Handler\PROCEXPORT UsageFault_Handler B .ENDPSVC_Handler PROCEXPORT SVC_Handler B .ENDPDebugMon_Handler\PROCEXPORT DebugMon_Handler B .ENDPPendSV_Handler PROCEXPORT PendSV_Handler B .ENDPSysTick_Handler PROCEXPORT SysTick_Handler B .ENDPDefault_Handler PROCEXPORT WWDG_IRQHandler EXPORT PVD_IRQHandler EXPORT TAMPER_IRQHandler EXPORT RTC_IRQHandler EXPORT FLASH_IRQHandler EXPORT RCC_IRQHandler EXPORT EXTI0_IRQHandler EXPORT EXTI1_IRQHandler EXPORT EXTI2_IRQHandler EXPORT EXTI3_IRQHandler EXPORT EXTI4_IRQHandler EXPORT DMA1_Channel1_IRQHandler EXPORT DMA1_Channel2_IRQHandler EXPORT DMA1_Channel3_IRQHandler EXPORT DMA1_Channel4_IRQHandler EXPORT DMA1_Channel5_IRQHandler EXPORT DMA1_Channel6_IRQHandler EXPORT DMA1_Channel7_IRQHandler EXPORT ADC1_2_IRQHandler EXPORT USB_HP_CAN1_TX_IRQHandler EXPORT USB_LP_CAN1_RX0_IRQHandler EXPORT CAN1_RX1_IRQHandler EXPORT CAN1_SCE_IRQHandler EXPORT EXTI9_5_IRQHandler EXPORT TIM1_BRK_IRQHandler EXPORT TIM1_UP_IRQHandler EXPORT TIM1_TRG_COM_IRQHandler EXPORT TIM1_CC_IRQHandler EXPORT TIM2_IRQHandler EXPORT TIM3_IRQHandler EXPORT TIM4_IRQHandler EXPORT I2C1_EV_IRQHandler EXPORT I2C1_ER_IRQHandler EXPORT I2C2_EV_IRQHandler EXPORT I2C2_ER_IRQHandler EXPORT SPI1_IRQHandler EXPORT SPI2_IRQHandler EXPORT USART1_IRQHandler EXPORT USART2_IRQHandler EXPORT USART3_IRQHandler EXPORT EXTI15_10_IRQHandler EXPORT RTCAlarm_IRQHandler EXPORT USBWakeUp_IRQHandler WWDG_IRQHandlerPVD_IRQHandlerTAMPER_IRQHandlerRTC_IRQHandlerFLASH_IRQHandlerRCC_IRQHandlerEXTI0_IRQHandlerEXTI1_IRQHandlerEXTI2_IRQHandlerEXTI3_IRQHandlerEXTI4_IRQHandlerDMA1_Channel1_IRQHandlerDMA1_Channel2_IRQHandlerDMA1_Channel3_IRQHandlerDMA1_Channel4_IRQHandlerDMA1_Channel5_IRQHandlerDMA1_Channel6_IRQHandlerDMA1_Channel7_IRQHandlerADC1_2_IRQHandlerUSB_HP_CAN1_TX_IRQHandlerUSB_LP_CAN1_RX0_IRQHandlerCAN1_RX1_IRQHandlerCAN1_SCE_IRQHandlerEXTI9_5_IRQHandlerTIM1_BRK_IRQHandlerTIM1_UP_IRQHandlerTIM1_TRG_COM_IRQHandlerTIM1_CC_IRQHandlerTIM2_IRQHandlerTIM3_IRQHandlerTIM4_IRQHandlerI2C1_EV_IRQHandlerI2C1_ER_IRQHandlerI2C2_EV_IRQHandlerI2C2_ER_IRQHandlerSPI1_IRQHandlerSPI2_IRQHandlerUSART1_IRQHandlerUSART2_IRQHandlerUSART3_IRQHandlerEXTI15_10_IRQHandlerRTCAlarm_IRQHandlerUSBWakeUp_IRQHandlerB .ENDPALIGN;*******************************************************************************; User Stack and Heap initialization;*******************************************************************************IF :DEF:__MICROLIBEXPORT __initial_spEXPORT __heap_baseEXPORT __heap_limitELSEIMPORT __use_two_region_memoryEXPORT __user_initial_stackheap__user_initial_stackheapLDR R0, = Heap_MemLDR R1, =(Stack_Mem + Stack_Size)LDR R2, = (Heap_Mem + Heap_Size)LDR R3, = Stack_MemBX LRALIGNENDIFENDThe problem is that when I add indented EXPORT in the beginning of the file, it would generate another error that I do not know how to solve itQuote

author: Tation
author_rank: Regular Contributor
timestamp: January 12, 2024, 05:59:28 pm
content: Quote from: ali_asadzadeh on January 07, 2024, 01:16:14 pmWhat documentation? Usage of __initial_sp is documented (points to the value of the initial SP and shall be exported if using MicroLib), but usage of Stack_Mem is not. Stack_Mem is used by function __user_initial_stackheap() to set stack and heap, but this heap+stack conf mechanism is incompatible with MicroLib. Thus, if you EXPORT both __initial_sp & Stack_Mem, the library detects the problem and you get the reported error.So, why do you need both __initial_sp and Stack_Mem exported?

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: January 13, 2024, 10:35:50 am
content: QuoteThanks for the feedback, I need them both for using embos, do you know how to solve it?