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.

HALCoGen 4.05.00 FreeRTOS programs built with GCC toolset work when flashed but crash on power-cycle (missing naked attribute on getResetSource)

Other Parts Discussed in Thread: HALCOGEN

I noticed that the FreeRTOS based applications worked fine when flashed from CCS, but the programs crashed when the Hercules development boards were power-cycled.  I found the issue to be the following:

Here are the first few lines of _c_int00 in HL_sys_startup.c

void _c_int00(void)
{

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

    /* Reset handler: the following instructions read from the system exception status register
     * to identify the cause of the CPU reset.
     */
    switch(getResetSource())
    {
        case POWERON_RESET:
        case DEBUG_RESET:
        case EXT_RESET:

Note that it makes a function call to getResetSource(). The problem here is that the stack isn't setup yet. _coreInitRegisters_ has not been called and _coreInitStackPointer_ has not been called either. So the stack pointer is currently just set to whatever garbage was in the registers at power on.

By default, the first thing that the code for getResetSource is going to do is to push something to the stack. But the stack hasn't been setup, and often points to an invalid address and the whole thing crashes.

I found a temporary fix is to add the naked attribute to the declaration of getResetSource() in HL_sys_startup.c. The code looks like the following:

__attribute__ ((naked))
resetSource_t getResetSource(void)
{
    register resetSource_t rst_source;

see www.freertos.org/implementation/a00013.html for information on the GCC naked attribute. 

Is this something we can get into the next version of HALCoGen? I think it is coming out next week.