# An unknown function is invoked during the ISR's execution

**URL:** <https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699>\
**Category:** Kernel\
**Created:** [May 28, 2021, 1:18am UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699 "2021-05-28T01:18:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [May 28, 2021, 1:18am UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/1 "2021-05-28T01:18:06Z")

</div>

So…I have an idle task and then a UART interrupt fires upon a character being sent over the serial, and somewhere during the execution of the ISR, it attempts to execute an “unknown” function as shown in the callstack [here](https://postimg.cc/k212MrQm). I don’t see this error when I comment out the **vTaskStartScheduler()** in main.

And here’s the respective [ASM](https://postimg.cc/SJVsV0tM) code but I can’t seem to trace down the C/C++ function being executed

IDE: Segger  
Platform: nRF52

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [May 28, 2021, 8:50am UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/2 "2021-05-28T08:50:32Z")

</div>

well, don’t rely on the call stack. First of all, you should make it a habit to at least reveal to us the target platform and the IDE. Tons of what you see and don’t see depend on these.

Your IDE may or may not know that FreeRTOS switches stacks upon ISR invocation and likewise not attempt to follow the entire chain of events. Also, the call stack is not reliable, for example on Cortex M platforms where the compiler may make lots of optimizationd such as tail recursion collating or function inlining that will make the call stack look very different from what you think it should look. In short, don’t waste your time barking up a possibly wrong tree.

Very likely your application suffers from one of the top three errors:

1. Stack overflow
2. Misconfigured interrupt priorities
3. Synchronization problems.

That’s where you should look first, they account for \> 90% of all problems related to FreeRTOS (as the discussions in this forum prove over and over again).

And of course, if you don’t start the OS in the first place, none of the top three problems will show.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [May 28, 2021, 4:10pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/3 "2021-05-28T16:10:20Z")

</div>

Thanks. I have mentioned the platform aloing with the IDE.

1. How would you track the stack possibly getting overflowed?
2. What do you think may happen in case of misconfigured IRQ priorities? There’s currently only one IRQ and one task (which is idling)
3. Why synchronization when the IRQ is supposed to fire? There’s a stream buffer waiting on the data in a task but it should be idling

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [May 28, 2021, 4:25pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/4 "2021-05-28T16:25:43Z")

</div>

you can start here:

> **[FreeRTOS - stacks and stack overflow checking](https://freertos.org/Stacks-and-stack-overflow-checking.html)**
>
> FreeRTOS is a portable, open source, mini Real Time kernel. A free RTOS for small embedded systems

or type “stack overflow” into the search box in the forum. Almost no topic has been discussed here as extensively.

Misconfigured task priorites refer to IRQs configured to run above MAX\_SYSCALL priority but use FreeRTOS API functions or systems configured to run the sys tick or service isr on too high priorities.

About synchronization: what I refer to is code that fails to use appropriate synchronization mechanisms to prevent shared data structures from being corrupted. That’s also been discussed here many many times.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [May 28, 2021, 9:06pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/5 "2021-05-28T21:06:32Z")

</div>

Thanks for posting the link.

1. So one way to test is to have `void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName )` in the application which would get invoked as soon as stack overflow is detected, yes? Thing is the hook function is never called

2. I don’t have any IRQs running freeRTOS APIs

3. I doubt if that’s causing this as currently I only have one stream buffer which is being written to and read from through a single write() function which isn’t even being used at this point.

Also now, after enabling the `configCHECK_FOR_STACK_OVERFLOW` flag, I don’t see the hook function being called but I do see the IRQ is being invoked and the actual registers aren’t being modified inside it.  
For instance the following line is what’s used to disable `NRF_UART_EVENT_RXDRDY` but now it’s not being disabled and the interrupt keeps firing. Why would having a scheduler mess with this?

```auto
pInstance->setNrfEvent(NRF_UART_EVENT_RXDRDY, StatusDisable);

```

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [May 29, 2021, 6:14am UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/6 "2021-05-29T06:14:27Z")

</div>

> [@MasterSil](#):
>
> nRF52

If I read it correctly, the Nordic chip is based on a Cortex M4. If the interrupt keeps firing though disabled on the UART level, next thing to look at is whether you clear the ISR on the NVIC level.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [May 29, 2021, 6:17am UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/7 "2021-05-29T06:17:49Z")

</div>

Thing though is, the same operation works as expected when the scheduler isn’t running (the same register gets disabled and IRQ doesn’t get fired after until the event happens). What’s concerning to me is the register not being disabled when scheduler is running

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [May 29, 2021, 8:53am UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/8 "2021-05-29T08:53:56Z")

</div>

well, as long as we don’t see any code, pretty much everything is guess work…

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [May 29, 2021, 4:33pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/9 "2021-05-29T16:33:59Z")

</div>

```auto
// logger.cpp
TaskHandle_t Logger::loggerThread;
TaskHandle_t Logger::taskMsgBuffer;
StreamBufferHandle_t Logger::streamBuffer;

typedef enum
{
    totalStreamBufferBytes = 100,
    triggerLevel = 5
} streamBufferParams;

Logger::Logger()
{
   Logger::streamBuffer = xStreamBufferCreate(totalStreamBufferBytes, triggerLevel);

    // create a task
    if (pdPASS != xTaskCreate(Logger::Process, "PROCESS_LOG", 256, this, 5, &Logger::loggerThread))
    {
        // error handle
    }
}

// uart.cpp
static Uart* pInstance;

extern "C" {
    void UARTE0_UART0_IRQHandler(void)
    {
       // ...
       if (rxIrqEnabled && dataRxd)
       {
           pInstance->setEvent(NRF_UART_EVENT_RXDRDY, 0); // disable event -- DOESN'T SEEM TO WORK AS EXPECTED WHEN SCHEDULER RUNS!
       }
	 
    }
} 

Uart::Uart()
{
   pInstance = this;
   // ...
}

// main.c
int main(void)
{
   Logger logger;
   Uart uart;
   clockInit();
   vTaskStartScheduler();
}

```

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [May 29, 2021, 5:08pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/10 "2021-05-29T17:08:35Z")

</div>

You know the main stack is reset and REUSED as ISR stack for Cortex-M ports.  
This might cause corruptions of your logger and UART instances allocated on main stack.  
The MSP is reset when the scheduler is started.  
Either allocate the instances from a (main) task stack right before entering it’s forever task loop or take from heap or make them static/global.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [May 29, 2021, 5:13pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/11 "2021-05-29T17:13:12Z")

</div>

**Update:** now I see the IRQ being invoked but the `NRF_UART_EVENT_RXDRDY` isn’t being set. With no scheduler, I had no issues setting it to 0.

> You know the main stack is reset and REUSED as ISR stack for Cortex-M ports.

how do we ensure that though? I have a hook function that doesn’t get called atll.

> Either allocate the instances from a (main) task stack right before entering it’s forever task loop or take from heap or make the static/global.

Sorry, still trying to understand this. The UART and Logger instances are already defined inside main and before the task scheduler. Are you implying to make these instances global or static?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [May 29, 2021, 5:24pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/12 "2021-05-29T17:24:47Z")

</div>

The reuse/reset of the main stack is done internally by FreeRTOS when starting the scheduler and can’t be _hooked_.  
Cortex-M MCUs have a main stack (see MSP register) and a process stack (see PSP register). The latter is used for tasks stack managed by FreeRTOS. The main stack is used by ISRs.  
You create the logger and UART instances on the main stack and start FreeRTOS.  
Hence both objects living in the main stack might and likely will be corrupted as soon as an ISR runs.  
In short DON‘T use the main stack for anything you need after starting FreeRTOS.  
So make the instances e.g. global or static as mentioned before to avoid their corruption.

Edit. This is also found multiple times in the forum and for instance here:

> [@Main stack pointer reset when starting the scheduler](https://forums.freertos.org/t/main-stack-pointer-reset-when-starting-the-scheduler/5227/4):
>
> [rtel](https://sourceforge.net/u/rtel/) wrote on [Friday, January 02, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/e5a776c1/#92b3): It has always been done this way, and is especially important on devices which only have a few K of RAM. I think the original Cortex-M ports were created on devices with 2K or RAM, in which case it was extremely important to re-use all the RAM that was not intended to be used after the scheduler has started. Once the scheduler has started main() no longer exists. There is no way back to it and no way of [directly] referencing memory on its stack [which…

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [May 29, 2021, 6:13pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/13 "2021-05-29T18:13:38Z")

</div>

Thanks. I tried making these two instances global and it worked. Follow ups:

- I understood that the main task is being used by the ISR but is the overflow really caused by ISR taking up too much memory hence corrupting the instances allocated in main i.e uart and. logger?

- I tried monitoring MSP register but having trouble figuring out how it could be used to see how overflow is caused. From my understanding, overflow happens when the MSP exceeds the stack bound which means it starts writing to ‘external’ addresses. If that’s the case, why would the stuff allocated within the stack get corrupted?

- Where would the following instance get allocated? `uart` is on a stack which no longer exists after the function is done executing. Would this still mess up with memory taken up by ISR?

```auto
void Init()
{
  Uart uart;
}
int main(void)
{
   Init();
}
```

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [May 29, 2021, 6:27pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/14 "2021-05-29T18:27:36Z")

</div>

It’s not a stack overflow ! Main stack and process stack are different stacks.  
Again the memory where the objects were created (in main) on main stack before FreeRTOS is started is let’s say _freed_ and used for something else (as stack for ISRs) as soon as FreeRTOS scheduler is started.  
I don‘t understand point 3.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [May 29, 2021, 6:36pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/15 "2021-05-29T18:36:44Z")

</div>

I see. So it’s more of a memory reusage where the same main stack which was initially used by the logger, uart instances is now used by the ISR since the scheduler reset the MSP (?) hence the corruption, yes?

So instead of defining `Uart uart` inside the main, you do it inside another function `Init()`. Is that also a part of “main stack”?

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [May 29, 2021, 6:53pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/16 "2021-05-29T18:53:42Z")

</div>

The basic rule is that you can’t create object ‘inside’ main or any function that it calls that aren’t marked as static, globals or put on the heap. All variables created as locals in main are possibly wiped out when the scheduler starts.

For small machines with limited memory, this makes a lot of sense. It does cause this sort of confusion to others on bigger processors. Sometimes I wish that it just set the ISR stack to the current main stack instead of its begining, losing a bit of stack space that is likly just ‘dead’ now, but avoids this sort of problem.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [May 29, 2021, 8:02pm UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/17 "2021-05-29T20:02:25Z")

</div>

Just an additional hint: Even though the code of a FreeRTOS application in main() until invoking `vTaskStartScheduler` looks like normal C-code with some function calls it‘s not exactly true. In fact it should be considered as bootstrap or bootloader code finally booting the real application by `vTaskStartScheduler` starting the first task.  
Starting the scheduler might switch e.g. the processor mode and change some parts of the C-runtime environment (stack). These things are obviously not covered by the C-standard and might cause surprising side effects if relying on environment (e.g. variables resp. objects on main stack) _before_ the scheduler was started.  
I think it‘s a good and simple model to create just a main or the first, initial task without arguments where all initializations (maybe including creating all other tasks) are done in it‘s preamble part before entering it‘s forever loop.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [May 30, 2021, 5:11am UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/18 "2021-05-30T05:11:02Z")

</div>

So basically everything defined on the stack in main get wiped out prior to a scheduler call, hence accessing any of it elsewhere in the code would result in garbage.

- Is this how most RTOS’s work or is it specific to FreeRTOS? as in why wipe out whatever was initially defined?

- how can you monitor this using MSP?

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [May 30, 2021, 7:38am UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/19 "2021-05-30T07:38:30Z")

</div>

> [@MasterSil](#):
>
> So basically everything defined on the stack in main get wiped out prior to a scheduler call, hence accessing any of it elsewhere in the code would result in garbage.

That pretty much sums it up, although it’s possible that some ports handle this differently. You could even rewrite your port to maintain the startup stack if you wanted to.

> [@MasterSil](#):
>
> - Is this how most RTOS’s work or is it specific to FreeRTOS? as in why wipe out **whatever was initially defined**?

Please remember your own expression before - only what is ON THE STARTUP stack is “wiped out.” Everything residing in other memory areas remains untouched - for example global semaphores, tasks etc.

Impossible to say if “most” RTOSs handle it the same way. The RTOS market is still fairly heterogeneous including a number of home brews.

> [@MasterSil](#):
>
> - how can you monitor this using MSP?

I don’t understand the question, sorry. What you could do if you wanted to is to memset() your startup stack in your vPortStartFirstTask() to a known signature, but since I don’t know what you intend to do by “monitoring”, this may or may not accomplish it.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [May 30, 2021, 8:02am UTC](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699/20 "2021-05-30T08:02:17Z")

</div>

> [@MasterSil](#):
>
> how can you monitor this using MSP?

Using a debugger capable of displaying the _registers_ of your MCU.

[Next page](https://forums.freertos.org/t/an-unknown-function-is-invoked-during-the-isrs-execution/12699.md?page=2)
