Hard-fault coming during svc to start first task

I am using LPC controller. I create 3 tasks. but when I tried to run the program Hard fault is coming in the bellow section.

__asm void prvStartFirstTask( void )

/* Use the NVIC offset register to locate the stack. */
ldr r0, =0xE000ED08
ldr r0, [r0]
ldr r0, [r0]
/* Set the msp back to the start of the stack. */
msr msp, r0
/* Clear the bit that indicates the FPU is in use in case the FPU was used
before the scheduler was started - which would otherwise result in the
unnecessary leaving of space in the SVC stack for lazy saving of FPU
registers. */
mov r0, #0
msr control, r0
/* Globally enable interrupts. */
cpsie i
cpsie f
/* Call SVC to start the first task. */
svc 0


Can anyone please help how to remove this fault. inside the task I am trying to simply toggling the LED. I took the RTOS file from the KEIL library.

Does your FreeRTOSConfig.h have the three #define statements that map the FreeRTOS ISRs to their CMSIS names? Usually something like this:

#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler

When users have posted similar problems in the past it has turned out that the svc instruction started the scheduler correctly, and the crash actually occurred in a task. If you step through the debugger though, the last thing you see in the svc call, and not the scv handler.

I suggest creating an application with a single task that is running at a high priority (it has to be higher than the idle and timer task priorities so you know which task will run first - the latter set by configTIMER_TASK_PRIORITY) - then put a breakpoint on the opening bracket “{” of the function that implements the task and see if the breakpoint is hit.

These three line is inside of port.c file. should I declare again in FreeRTOSConfig.h file?

Those three lines do belong in FreeRTOSConfig.h, not in port.c, but that’s probably not going to fix your issue. I would make sure the scheduler is actually starting successfully as Richard describes above. Also look at the “common” issues addressed in this post.

I created a task and and set higher priority than timer task and idle task but it still goes to hard fault.

Can you create the smallest project you can that demonstrates the issue then attach it to a post here so we can see - thanks.

eth_test2.zip (2.7 MB)

Here I attached a smallest project. When I tried to run this project inside this function vTaskStartScheduler(); when svc call happens that time the program goes to hard fault.

I didn’t see any definition for configASSERT() in FreeRTOSConfig.h. You should define that macro so that FreeRTOS can detect configuration and other issues at runtime.

The definition of configMAX_SYSCALL_INTERRUPT_PRIORITY doesn’t look right. See FreeRTOS - The Free RTOS configuration constants and configuration options - FREE Open Source RTOS for small real time embedded systems for help setting that properly.

The FreeRTOS source isn’t in your zip file. Are you using a the M4F port provided by FreeRTOS? What version of FreeRTOS are you using?

I am taken it from keil cimsis library and its version is 10.3.1.

Did you define configASSERT() yet? The help page recommends this implementation during debugging:

/* Define configASSERT() to disable interrupts and sit in a loop. */
#define configASSERT ( x )     if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }