This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RM48L952: I want to read the stack pointer, but I'm getting an error.

Part Number: RM48L952

Tool/software:

/** @file sys_main.c 
*   @brief Application main file
*   @date 11-Dec-2018
*   @version 04.07.01
*
*   This file contains an empty main function,
*   which can be used for the application.
*/

/* 
* Copyright (C) 2009-2018 Texas Instruments Incorporated - www.ti.com 
* 
* 
*  Redistribution and use in source and binary forms, with or without 
*  modification, are permitted provided that the following conditions 
*  are met:
*
*    Redistributions of source code must retain the above copyright 
*    notice, this list of conditions and the following disclaimer.
*
*    Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the 
*    documentation and/or other materials provided with the   
*    distribution.
*
*    Neither the name of Texas Instruments Incorporated nor the names of
*    its contributors may be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/


/* USER CODE BEGIN (0) */
//#include <stdio.h>
/* USER CODE END */

/* Include Files */

#include "sys_common.h"

/* USER CODE BEGIN (1) */
/* USER CODE END */

/** @fn void main(void)
*   @brief Application main function
*   @note This function is empty by default.
*
*   This function is called after startup.
*   The user can use this function to implement the application.
*/

/* USER CODE BEGIN (2) */
#define STACK_GUARD_VALUE   0xBEEFBEEF
#define STACK_GUARD_ADDRESS 0x08000000
#define USER_STACK_BASE_ADDRESS 0x08000000
#define NUMBER_OF_VARIABLES 500

void test01(void);
void test02(void);
void test03(void);
void test04(void);
void test05(void);
void test06(void);
void test07(void);

void InitStackGuard(void);
void CheckStackGuard(void);
void CheckStackPointer(void);
unsigned int get_sp(void);

uint8_t gucUserStackOverflowFlag  = 0U;
uint32_t gucSP;

/* USER CODE END */

int main(void)
{
/* USER CODE BEGIN (3) */
    InitStackGuard();

    while(1U)
    {
        test01();
    }
/* USER CODE END */
}


/* USER CODE BEGIN (4) */
void test01(void)
{
    int32_t i;
    uint32_t uiTest01[NUMBER_OF_VARIABLES];
    //CheckStackGuard();
    CheckStackPointer();

    for (i = 0; i < NUMBER_OF_VARIABLES; i++)
    {
        uiTest01[i] = i;
    }
    test02();
}
void test02(void)
{
    //CheckStackGuard();
    CheckStackPointer();

    uint32_t uiTest02[4] = {0,};
}

// Stack Guard
void InitStackGuard(void)
{
    *(volatile uint32_t *)STACK_GUARD_ADDRESS = STACK_GUARD_VALUE;
}
void CheckStackGuard(void)
{
    if (*(volatile uint32_t *)STACK_GUARD_ADDRESS != STACK_GUARD_VALUE)
    {
        gucUserStackOverflowFlag = 1U;
        //printf("Stack overflow detected! Guard value corrupted.\n");
        while (1U)
            {
                ;// 무한 루프 (오류 처리)
            }
    }
}

//CheckStackPointer
void CheckStackPointer(void)
{
    if (gucSP < USER_STACK_BASE_ADDRESS)
    {
        gucUserStackOverflowFlag = 1U;
        while (1U)
        {
            ;// 무한 루프 (오류 처리)
        }
    }
}

unsigned int get_sp(void)
{
    unsigned int sp;
    __asm("MOV %0, SP" : "=r"(sp));  // SP 값을 sp 변수에 저장
    return sp;
}

/* USER CODE END */

Hello,

I’m encountering an issue with a parentheses error while trying to read the stack pointer in my code.

Would anyone be able to explain why this error might be occurring?

I would appreciate any insights or suggestions to help resolve it.

Thank you for your time and assistance!

Best regards,

  • Hi Kim,

    Apologies for the delayed response.

    I am already discussing this with our compiler team and will try to provide our updates ASAP.

    --
    Thanks & regards,
    Jagadish.

  • I presume you use the TI Arm compiler (short name armcl), and not the TI Arm Clang compiler (short name tiarmclang).  The function get_sp in the first post has an __asm statement that uses what is known as GCC-syntax.  Unfortunately, this syntax is not supported by the TI Arm compiler.  

    As a workaround, consider ...

    #include <stdint.h>
    
    uintptr_t get_sp()
    {
        int64_t local_variable;
    
        return (uintptr_t)&local_variable + sizeof(local_variable);
    }

    It's an ugly hack.  The code generated is a bit bigger and slower.  But it computes the same result.

    Thanks and regards,

    -George