DelayUntil causes hardware fault on arm cortex m4 when task function is moved into a separate c file

neicdech wrote on Monday, February 11, 2019:

Hello All,
I am working on a FreeRTOS project on mcuxpresso , ksdk 2.5 and cortex m4 and at the moment, I simply want to get a task to blink an LED. The project runned without problems when the task function was defined in the main.c file. For clarity, I moved the LED task into its its own “User_task_function.c” and included the “User_task_function.h” in the main. Afterwards, the program will raise a hardware fault in the LED task when it calls DelayUntil(). I have tried for several days. I will appreciate help in resolving this issue. I have attached the project.

                `
                #ifdef __cplusplus

extern “C” {
#endif

#include "MK22F51212.h"
#include "fsl_clock.h"
#include "clock_config.h"
#include "Defs.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "timers.h"
#include "queue.h"
#include "fsl_port.h"
#include "fsl_gpio.h"
#include "global_variable.h"
#include "user_task_functions.h"
#include "pins.h"
#include "pin_mux.h"

#ifdef cplusplus
}
#endif

int main ()
{
BOARD_BootClockHSRUN();
BOARD_InitBootPins();

if (xTaskCreate(air_blinky, "air_blinky_A", 200, &Board_A_blinky, blinkyPriority,
	&BlinkyAhandle) == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY)
	exit(-1);


vTaskStartScheduler();

while(true);

return 0;

}
`

// Below is the Header for the task function.

#ifndef USER_TASK_FUNCTIONS_H_ #define USER_TASK_FUNCTIONS_H_ #include “Defs.h” #include “FreeRTOS.h” #include “task.h” #include “global_variable.h” #include “fsl_gpio.h”`

void air_blinky(void* blinky_parameters);
void vApplicationIdleHook( void );

`#endif /* USER_TASK_FUNCTIONS_H_ */``

// Below is the task functions definition

`#include “user_task_functions.h”

void air_blinky(void* blinky_parameters)
{

volatile BLINKY_param_t* 	LED 						= (BLINKY_param_t*) blinky_parameters;
char* 						Mytaskname 			 		= pcTaskGetName(NULL);
TickType_t 				 	xBlinkyLastwakeuptime 		= xTaskGetTickCount();
const TickType_t			Flash_Time 					= 100;
const TickType_t			Flash_Return_Time 			= 2000;


GPIO_PortClear(LED->GPIO, mask(LED->Pin_name)); // on

for (;;)
{

	if( isTasksuspended & (!(strcmp(Mytaskname,"air_blinky_A")) ? 0b10000000 : 0b00010000) )     // area to publish a paper on
	{
		portTICK_TYPE_ENTER_CRITICAL();

		xBlinkyLastwakeuptime = xTaskGetTickCount();
		isTasksuspended &= ( (!(strcmp(Mytaskname,"air_blinky_A"))) ? 0b01111111 : 0b11101111 ) ;

		portTICK_TYPE_EXIT_CRITICAL();
	}

	{
		GPIO_PortClear	(LED->GPIO, mask(LED->Pin_name));

		GPIO_PortToggle	(LED->GPIO, mask(LED->Pin_name));

		vTaskDelayUntil	(&xBlinkyLastwakeuptime,Flash_Time);

		GPIO_PortToggle	(LED->GPIO, mask(LED->Pin_name));

		vTaskDelayUntil	(&xBlinkyLastwakeuptime,Flash_Time);

		GPIO_PortToggle	(LED->GPIO, mask(LED->Pin_name));

		vTaskDelayUntil	(&xBlinkyLastwakeuptime,Flash_Time);

		GPIO_PortToggle	(LED->GPIO, mask(LED->Pin_name));

		vTaskDelayUntil	(&xBlinkyLastwakeuptime, Flash_Return_Time);
	}



}

`

`
// Below is the header for the global variables

#ifndef GLOBAL_VARIABLE_H_
#define GLOBAL_VARIABLE_H_

#include "stdint.h"
#include "stddef.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "Defs.h"
#include "pins.h"
#include "arm_math.h"

extern uint8_t isTasksuspended ;
extern BLINKY_param_t Board_A_blinky ;
extern BLINKY_param_t Board_B_blinky ;
extern TaskHandle_t BlinkyAhandle ;
extern TaskHandle_t BlinkyBhandle ;
extern TaskHandle_t MasterTaskhandle ;
extern SemaphoreHandle_t INTERRUPT_semphr ;
extern bool ActuatorOneDetectState ;
extern bool ActuatorTwoDetectState ;
extern arm_biquad_casd_df1_inst_f32 NoiseFilter ;
extern float32_t corrupt_dataA[Blocksize];
extern float32_t corrupt_dataB[Blocksize];
extern float32_t outputA[Blocksize] ;
extern float32_t outputB[Blocksize] ;

#endif /* GLOBAL_VARIABLE_H_ */
`

// Below is my global variables

`#include “global_variable.h”

	uint8_t 							isTasksuspended 					= 	0;
	BLINKY_param_t 						Board_A_blinky 						= 	{ GPIOE, PTE1 } ;
	BLINKY_param_t 						Board_B_blinky 						= 	{ GPIOB, PTB22} ;
	TaskHandle_t 						BlinkyAhandle    					= 	NULL			;
	TaskHandle_t 						BlinkyBhandle     					= 	NULL			;
	TaskHandle_t 						MasterTaskhandle 					= 	NULL			;
	SemaphoreHandle_t 					INTERRUPT_semphr 					= 	NULL			;
	bool 								ActuatorOneDetectState 				= 	false			;
	bool 								ActuatorTwoDetectState 				= 	false			;
	arm_biquad_casd_df1_inst_f32 		NoiseFilter												;
	float32_t 							corrupt_dataA[Blocksize] 			= 	{0}				;
	float32_t 							corrupt_dataB[Blocksize] 			= 	{0}				;
	float32_t 							outputA[Blocksize] 					= 	{0}				;
	float32_t 							outputB[Blocksize] 					= 	{0}				;

`

rtel wrote on Monday, February 11, 2019:

There does not seem to be anything wrong with the call to
vTaskDelayUntil(). Can you try taking out the calls to
GPIO_PortToggle() to see if there is anything wrong with the way the
parameter is being passed into the task. If you take out the
GPIO_PortToggle() calls then obviously the LED won’t blink, but you will
at least be able to see that you don’t end up in the hard fault handler.

Additionally, did you try stepping into the code to see where the hard
fault occurs? It might be that the vTaskDelay() is causing a context
switch, and the fault actually occurs somewhere else. Inspecting the
pxCurrentTCB variable (which you may have to cast to a (tskTCB*) in the
debugger) will show you the name of the running task. You can also
follow the instructions here:
https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html
to see what that tells you. If it doesn’t tell you much at first then
also note the “Handling Imprecise Faults” section at the bottom of that
page.

neicdech wrote on Wednesday, February 13, 2019:

Hi Richard, thanks for the help, I have been able to find what was causing the error. I had configured the idlehooktask to call a function for which had not properly set up and that was causing the hardware fault immediately after the context switch. thanks once again