freeRTOS + SAME70 = HEAP Problem

rtimenoob wrote on Tuesday, March 28, 2017:

Hello,
I am running freeRTOS together with LWIP on a SAME70Q19.
I implemented freeRTOS out of the ASF in my AtmelStudio 7.0.

My Problem:
When I create a new task, my application stucks in the Function
vTaskStartScheduler(): Subfunction: xPortStartScheduler() Subfunction
prvPortStartFirstTask() From here the application jumps with hardfault
error to the dummy interrupt handler.
I noticed that this must be a problem with the heap memory.
When I increase configTOTAL_HEAP_SIZE to 40000 I cannot receive
CAN messages any longer. Although the application jumps into the
RX Interrupt, the Mailboxes stay empty (0x00) When I decrease
the Heap size, I receive proper data. Very strange to me!!
I am using heap4.c and I configured freeRTOS as following:

#define configUSE_PREEMPTION			1
#define configUSE_IDLE_HOOK				0
#define configUSE_TICK_HOOK				0
#define configCPU_CLOCK_HZ				( sysclk_get_cpu_hz() )
#define configTICK_RATE_HZ				( ( portTickType ) 1000 )
#define configMAX_PRIORITIES			( ( unsigned portBASE_TYPE ) 7 )
#define configMINIMAL_STACK_SIZE		( ( unsigned short ) 130 )
#define configTOTAL_HEAP_SIZE			( ( size_t ) ( 28000 ) )
#define configMAX_TASK_NAME_LEN			( 10 )
#define configUSE_TRACE_FACILITY		1
#define configUSE_16_BIT_TICKS			0
#define configIDLE_SHOULD_YIELD			1
#define configUSE_MUTEXES				1
#define configQUEUE_REGISTRY_SIZE		8
#define configCHECK_FOR_STACK_OVERFLOW	0
#define configUSE_RECURSIVE_MUTEXES		1
#define configUSE_MALLOC_FAILED_HOOK	0		//1
#define configUSE_APPLICATION_TASK_TAG	0
#define configUSE_COUNTING_SEMAPHORES	1

/* Co-routine definitions. */
#define configUSE_CO_ROUTINES 			0
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )

/* Software timer definitions. */
#define configUSE_TIMERS				1
#define configTIMER_TASK_PRIORITY		( configMAX_PRIORITIES - 1 )
#define configTIMER_QUEUE_LENGTH		5
#define configTIMER_TASK_STACK_DEPTH	( configMINIMAL_STACK_SIZE * 2 )

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet		1
#define INCLUDE_uxTaskPriorityGet		1
#define INCLUDE_vTaskDelete				1
#define INCLUDE_vTaskCleanUpResources	1
#define INCLUDE_vTaskSuspend			1
#define INCLUDE_vTaskDelayUntil			1
#define INCLUDE_vTaskDelay				1

/* FreeRTOS+CLI definitions. */

/* Dimensions a buffer into which command outputs can be written.  The buffer
can be declared in the CLI code itself, to allow multiple command consoles to
share the same buffer.  For example, an application may allow access to the
command interpreter by UART and by Ethernet.  Sharing a buffer is done purely
to save RAM.  Note, however, that the command console itself is not re-entrant,
so only one command interpreter interface can be used at any one time.  For
that reason, no attempt at providing mutual exclusion to the buffer is
attempted. */
#define configCOMMAND_INT_MAX_OUTPUT_SIZE 400


/* Cortex-M specific definitions. */

#ifdef __NVIC_PRIO_BITS
	/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
	#define configPRIO_BITS       		__NVIC_PRIO_BITS
#else
	#define configPRIO_BITS       		4        /* 15 priority levels */
#endif

/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY			0x0f

/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY	10

/* Interrupt priorities used by the kernel port layer itself.  These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY 		( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 	( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )

/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ) __asm volatile( "NOP" ); }
#define INCLUDE_MODULE_TEST 0

#define FREERTOS_USED

My Tasks are:

xTaskCreate(task_led, "test", 
					TASK_LED_STACK_SIZE, NULL,
					TASK_LED_STACK_PRIORITY, NULL); 
		
		xTaskCreate(CAN_to_ETH_TASK, "CANTOETH", 
					TASK_CAN_TO_ETH_STACK_SIZE, NULL,
					TASK_CAN_TO_ETH_STACK_PRIORITY, NULL);
					
		xTaskCreate(CAN0_Task, "CAN0",
					CAN_TASK_STACK_SIZE, NULL,
					CAN_TASK_STACK_PRIORITY, NULL);
		
		xTaskCreate(vStartEthernetTask, "ETHLAUNCH", 
					configMINIMAL_STACK_SIZE, NULL,
					tskIDLE_PRIORITY + 4, NULL);
                    

Here the defines:

#define TASK_MONITOR_STACK_SIZE            50
#define TASK_MONITOR_STACK_PRIORITY        (tskIDLE_PRIORITY)
#define TASK_LED_STACK_SIZE                50
#define TASK_LED_STACK_PRIORITY            (tskIDLE_PRIORITY)
#define TASK_CAN_TO_ETH_STACK_SIZE		   500
#define TASK_CAN_TO_ETH_STACK_PRIORITY	   (tskIDLE_PRIORITY)+6
#define CAN_TASK_STACK_SIZE				   400
#define CAN_TASK_STACK_PRIORITY			   (tskIDLE_PRIORITY)+6

/*! define stack size for WEB server task */
#define lwipBASIC_WEB_SERVER_STACK_SIZE   512

/*! define stack size for TFTP server task */
#define lwipBASIC_TFTP_SERVER_STACK_SIZE  2048

/*! define stack size for SMTP Client task */
#define lwipBASIC_SMTP_CLIENT_STACK_SIZE  256

/*! define stack size for lwIP task */
#define lwipINTERFACE_STACK_SIZE         2048

/*! define stack size for netif task */
#define netifINTERFACE_TASK_STACK_SIZE    2048

/*! define WEB server priority */
#define lwipBASIC_WEB_SERVER_PRIORITY     (tskIDLE_PRIORITY + 2)

/*! define TFTP server priority */
#define lwipBASIC_TFTP_SERVER_PRIORITY    (tskIDLE_PRIORITY + 3)

/*! define SMTP Client priority */
#define lwipBASIC_SMTP_CLIENT_PRIORITY    (tskIDLE_PRIORITY + 5)

/*! define lwIP task priority */
#define lwipINTERFACE_TASK_PRIORITY       (configMAX_PRIORITIES - 1)

/*! define netif task priority */
#define netifINTERFACE_TASK_PRIORITY      (configMAX_PRIORITIES - 1)

Can somebody help me with this issue? How much Heap should I use? How is it possible that
the CAN-Bus doesn’t receive messages any longer with greater heap size?
Why can’t I add new Tasks without jumping into hard fault error?
I am looking forward to your answers.

rtel wrote on Tuesday, March 28, 2017:

First question. You say the first task doesn’t start unless you set the
heap to 40000. Does that mean that with this change to the heap size
the first task does start, and the system runs ok other than the CAN
interrupts?

Have you ever seen the CAN interrupts working, and if so, is it only
when you use FreeRTOS that they don’t work?

If the CAN interrupts only don’t work when you are using FreeRTOS, are
you talking about them not working before the scheduler starts or after
the scheduler starts?