STM32 + GCC + Bootloader + FreeRTOS

Hello everyone.
I am using STM32F103VB uP with CodeSourcery GCC compiler. I have following problem. I’ve developed bootloader, which is loaded at the beginning of flash (0x08000000) . This bootloader uses a SD card with FAT file system, and check if some file with new firmware exists on SD card, and then loads it to flash at address 0x08008000 and call new application using such asm code:   .thumb_func
CallApplication:
  // Set the vector table address to the beginning of the application.
  ldr     r0, =APP_VEC_TAB_OFFSET
  ldr     r1, =(SCB_BASE + 8)
  str     r0,   // Load the initial LR as it should be after the reset.
  movs    lr, #0xffffffff   // Load the stack pointer from the application’s vector table.
  ldr     r0, =DEF_APP_ADDRESS
  ldr     r1,
  msr     msp, r1   // Load the initial PC from the application’s vector table and branch to
  // the application’s entry point.
  ldr     r0,
  bx      r0 Where DEF_APP_ADDRESS is 0x08000000.
When I load in such way application that is not using FreeRTOSeverything is fine. Application is loading and than running correctly, but doing the same with application based on FreeRTOS i get the HardFault Exception before calling the main in section LoopFillZerobss of startup file:
Reset_Handler: /* Copy the data segment initializers from flash to SRAM */ 
  movs r1, #0
  b LoopCopyDataInit CopyDataInit:
ldr r3, =_sidata
ldr r3,
str r3,
adds r1, r1, #4
   
LoopCopyDataInit:
ldr r0, =_sdata
ldr r3, =_edata
adds r2, r0, r1
cmp r2, r3
bcc CopyDataInit
ldr r2, =_sbss
b LoopFillZerobss
/* Zero fill the bss segment. */ 
FillZerobss:
movs r3, #0
str r3, , #4
   
LoopFillZerobss:
ldr r3, = _ebss
cmp r2, r3
bcc FillZerobss
/* Call the clock system intitialization function.*/
  bl  SystemInit
/* Call the application’s entry point.*/
bl main
bx lr   
.size Reset_Handler, .-Reset_Handler Of course i change in the linker script line:
FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 128K
to:
FLASH (rx)      : ORIGIN = 0x08008000, LENGTH = (128K – 0x8000)
and in function void vSetupHardware(void) I changed line:
NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
to:
NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x8000 );
I don’t know where to find problem.

STM32 + GCC + Bootloader + FreeRTOS

I forgot to add that application, which i tried to load an then execute using bootloader works perfectly in normal situation:
- in linker script is FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K
- in function vSetupHardware: NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );

STM32 + GCC + Bootloader + FreeRTOS

Without having fully inspected your code, is it possible that your problem is something to do with the code that starts the scheduler loading the stack location from the interrupt table (in port.c)
void vPortStartFirstTask( void )
{
    __asm volatile(
                    " ldr r0, =0xE000ED08   n" /* Use the NVIC offset register to locate the stack. */
                    " ldr r0, [r0]          n"
                    " ldr r0, [r0]          n"
                    " msr msp, r0           n" /* Set the msp back to the start of the stack. */
                    " svc 0                 n" /* System call to start first task. */
                );
}

STM32 + GCC + Bootloader + FreeRTOS

I checked this, but this function is called in vTaskStartScheduler(), but I dont even reach the main() function. Application with FreeRTOS crash with HardFault exception in startup file in this section:
/* Zero fill the bss segment. */
FillZerobss:
movs r3, #0 str r3, , #4
LoopFillZerobss:
ldr r3, = _ebss cmp r2, r3
bcc FillZerobss
This section is also present in application without FreeRTOS (what is obvious), but in application without FreeRTOS everything works fine.

STM32 + GCC + Bootloader + FreeRTOS

I was wrong and you were like always. The problem is in vPortStartFirstTask function. So i have to change something in this function. I will try and let you know when i solved the problem.

STM32 + GCC + Bootloader + FreeRTOS

So the problem is with line:
" msr msp, r0                   n"
When i debug code and put breakpoint on line:
" ldr r0, [r0]          n"
The value of R0 register is 0x20005000 so its correct for my processor. Maybe the the core of stm32 processor is in wrong state and cannot execute msr command. How can I check current state and how can I change it? To execute command MSR i have to be in Thread mode with privileged state?

STM32 + GCC + Bootloader + FreeRTOS

Can you just inspect the status register to see which mode you are in?

STM32 + GCC + Bootloader + FreeRTOS

The CPSR register is uequal to 0x61000020

STM32 + GCC + Bootloader + FreeRTOS

…have you interpreted what that means with respect to modes and stacks. You are going to have to look it up in the manual if your debugger is not giving you the information.

STM32 + GCC + Bootloader + FreeRTOS

I discover that in function vPortStartFirstTask processor is in Thread Priviliged mode and I dont know what is causing HardFault on
" msr msp, r0           n"
Its unbelieveable.

STM32 + GCC + Bootloader + FreeRTOS

Ok, again my fault. Initialization of MSP is done correctly. Problem is with line
"svc 0              n"
. Its quite strange because this code works properlyy when program starts from 0x08000000. In startup file i have something like this:
.extern xPortPendSVHandler
.extern xPortSysTickHandler
.extern vPortSVCHandler
.
.
.
.
.
.
g_pfnVectors:
    .word   _estack
    .word   Reset_Handler
    .word   NMI_Handler
    .word   HardFault_Handler
    .word   MemManage_Handler
    .word   BusFault_Handler
    .word   UsageFault_Handler
    .word   0
    .word   0
    .word   0
    .word   0
/*  .word   SVC_Handler */
  .word vPortSVCHandler
    .word   DebugMon_Handler
    .word   0
/*  .word   PendSV_Handler */
  .word xPortPendSVHandler
/*  .word   SysTick_Handler */
... rest of handlers ...
where

STM32 + GCC + Bootloader + FreeRTOS

Ok i solved problem. The problem was, that in bootloader i used Systick Timer and before calling application i didnt disable this timer. Now I disable SysTick timer in bootloader, and loaded and calles FreeRTOS application runs properly.