# FreeRTOS crashes when called from Bootloader ARM Cortex M0

**URL:** <https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074>\
**Category:** ST\
**Tags:** debug\
**Created:** [January 30, 2024, 2:10pm UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074 "2024-01-30T14:10:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![BitHacker](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@BitHacker](https://forums.freertos.org/u/BitHacker)\
**Post date:** [January 30, 2024, 2:10pm UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/1 "2024-01-30T14:10:00Z")

</div>

Hello,  
i have a typical bare metal bootloader and freeRTOS Application szenario. The Target is a STM32F030RC ( Arm Cortex M0)  
When the bootloader jumps to a bare metal application everything is fine, but when the bootloader jumps to the freeRTOS application the application crashes.

I know that i should manipulate as less registers as possible from within the bootloader.  
Therefore i have this minimal version that only inits the clock tree and than jumps to the application (this code snippet works with bare metal application but not with freeRTOS application) . I also have defined NMI\_Handler, HardFAult\_Handler and Systick\_Handler in the bootloader code but they are only defined but never called when i reduce the bootloader to this snippet.

**The Bootloader code:**

```auto
void main()
{
    clock_init(); // Configures only RCC Registers
    __disable_irq();

	for(unsigned int i=0; i< RAM_VECTORTABLE_LEN;i++){
		ram_vectortable[i] = *(__IO uint32_t*)(FIRMWARE_START_ADR+(i<<2));
	}
	RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // Sysconfig Clock must be enabled to work
	SYSCFG->CFGR1 |= (SYSCFG_CFGR1_MEM_MODE_0 | SYSCFG_CFGR1_MEM_MODE_1);	// MEMMODE = 3

	void (*mainprog) (void) = (void (*)) *(__IO uint32_t*)(FIRMWARE_START_ADR + 4); // +4 to call the Reset_handler of the vector table
	
	mainprog();
	
	// code should never run here
	while(1){
		assert(0);
	}
}

```

When i try to debug the application i see this backtrace:  
 ![backtraceFreeRTOSApplication](https://d2v6vdsk2p900z.cloudfront.net/original/2X/5/5c3ea9ab84e223a76d507c398be2f6e8e6b645ef.png)  
I can’t really Debug the Application. I think the reason is that the application is already started from the bootloader or maybe a incomplete startup setup.

My Debug Configuration for the Application looks like this:

 ![debugConfig](https://d2v6vdsk2p900z.cloudfront.net/original/2X/4/4100a309b40bcb52236fb148d24785cc3f8f5b86.png)

My Flash-Layout looks like this:

 ![FlashLayout](https://d2v6vdsk2p900z.cloudfront.net/original/2X/6/6fdb38a0e5161369a6258c5fc1ee02a3dc857486.png)

Thanks for all your advices.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [January 30, 2024, 3:04pm UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/2 "2024-01-30T15:04:54Z")

</div>

You probably need to program VTOR to correctly install the application vector table. This is a useful article from ARM - [Documentation – Arm Developer](https://developer.arm.com/documentation/ka002218/latest/).

---

<div class="post-metadata">

**Author:** ![BitHacker](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@BitHacker](https://forums.freertos.org/u/BitHacker)\
**Post date:** [January 30, 2024, 3:10pm UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/3 "2024-01-30T15:10:40Z")

</div>

No the cortex M0 has no VTOR.  
For this reason i copy the vector table to the SRAM and than change the memory-mode to MEMMODE\_3.  
There are a lot of tutorials how to do this for a cortex M0.  
I like this one: [https://www.linkedin.com/pulse/bootloader-application-image-arm-cortexm0-petr-dvo%C5%99%C3%A1k/](https://www.linkedin.com/pulse/bootloader-application-image-arm-cortexm0-petr-dvo%C5%99%C3%A1k/)

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [January 30, 2024, 3:13pm UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/4 "2024-01-30T15:13:54Z")

</div>

Alright - my bad. Have you installed FreeRTOS handlers for SVC, SysTick and PendSV - [FreeRTOS-Kernel/portable/GCC/ARM\_CM0/port.c at main · FreeRTOS/FreeRTOS-Kernel · GitHub](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM_CM0/port.c#L105-L107)?

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [January 31, 2024, 12:41am UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/5 "2024-01-31T00:41:16Z")

</div>

Right - that’s M0+ with VTOR. Basic M0 does not have VTOR but silicon vendors like ST can provide a way to shadow RAM at address 0x00000000 allowing you a RAM-based vector table.

Regarding the question from @aggarg – to install the FreeRTOS vectors you would typically have something like this in your FreeRTOSConfig.h:

```auto
/* Map the FreeRTOS port interrupt handlers to their CMSIS standard names. */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler

```

---

<div class="post-metadata">

**Author:** ![BitHacker](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@BitHacker](https://forums.freertos.org/u/BitHacker)\
**Post date:** [January 31, 2024, 8:12am UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/6 "2024-01-31T08:12:07Z")

</div>

Yes is have mapped the freeRTOS Port Interrupt Handlers to their CMSIS Standard names.

```auto
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler

```

and a custom redefinition of the SysTick\_Handler.  
This was necessary to avoid startup problems.

```auto
void SysTick_Handler(void) {
	if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
		xPortSysTickHandler();
	}
}

```

but for PendSV\_Handler this means that freeRTOS functions call the Default\_handler, which is a endless while loop.  
Al this is defined in the startup\_stm32f030rctx.S which has no customization by me

**startup\_stm32f030rctx.S**

```auto
  .weak SVC_Handler
  .thumb_set SVC_Handler,Default_Handler

  .weak PendSV_Handler
  .thumb_set PendSV_Handler,Default_Handler

```

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [January 31, 2024, 9:46am UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/7 "2024-01-31T09:46:11Z")

</div>

> [@BitHacker](#):
>
> but for PendSV\_Handler this means that freeRTOS functions call the Default\_handler, which is a endless while loop.

This is not correct - `xPortPendSVHandler` needs to be called for PendSV interrupt and `vPortSVCHandler` for SVC.

---

<div class="post-metadata">

**Author:** ![BitHacker](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@BitHacker](https://forums.freertos.org/u/BitHacker)\
**Post date:** [January 31, 2024, 11:19am UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/8 "2024-01-31T11:19:55Z")

</div>

okay i missunderstood this.  
So what this defines do is that an SVC or PendSV Interrupt call the freeRTOS handler  
xPortPendSVHandler and vPortSVCHandler ?

```auto
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler

```

That means for me that i have done this right so far, am i correct ?  
So how can i debug what kind of Problem the freeRTOS has ?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [January 31, 2024, 11:40am UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/9 "2024-01-31T11:40:01Z")

</div>

> [@BitHacker](#):
>
> So what this defines do is that an SVC or PendSV Interrupt call the freeRTOS handler  
> xPortPendSVHandler and vPortSVCHandler ?

This defines changes the name of `xPortPendSVHandler` to `PendSV_Handler` and `vPortSVCHandler` to `SVC_Handler` in the preprocessing step so that definitions provided by FreeRTOS are used instead of the weak definitions in the startup code.

> [@BitHacker](#):
>
> So how can i debug what kind of Problem the freeRTOS has ?

Can you examine the vector table entries and see if handlers for SVC and PendSV are correctly installed? Is it possible for you to share your complete code?

---

<div class="post-metadata">

**Author:** ![BitHacker](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@BitHacker](https://forums.freertos.org/u/BitHacker)\
**Post date:** [February 1, 2024, 10:45am UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/10 "2024-02-01T10:45:50Z")

</div>

i have found my bug.  
i forgot the RAM offset in the application

**STM32F030RCTX\_FLASH\_APPLICATION.ld (with BUG)**

```auto
MEMORY
{
  RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
  FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 256K - 32K
} 

```

**STM32F030RCTX\_FLASH\_APPLICATION.ld (with bugfix)**

```auto
MEMORY
{
  RAM (xrw) : ORIGIN = 0x200000BC, LENGTH = 32K - 0xBC /* The Vector Table in RAM (size = 4x47Byte) must not be used by the application*/
  FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 256K - 32K
} 

```

My bare-metal application with the buggy linker-script only worked, because it never called any entry of the vector table.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [February 1, 2024, 10:50am UTC](https://forums.freertos.org/t/freertos-crashes-when-called-from-bootloader-arm-cortex-m0/19074/11 "2024-02-01T10:50:58Z")

</div>

Thank you for reporting back!
