freeRTOS LwIP UNALIGNED hardfault access. STM32H7

The code stopes in the uxListRemove function of the list.c file at this line of code: List_t * const pxList = pxItemToRemove->pxContainer;
Also the task that was working during the error was IDLE task.

that does not help at all. We are looking for the root cause which very likely took place long before you see the issue

I don’t know if you checked it above, in my previous reply because I edit my post. This article https://www.beningo.com/how-to-debug-a-hard-fault-on-an-arm-cortex-m/ speaks about preciserr error and it says that it was due to a linkerscript error because the author of the code added a new section into it, without doing a resize to the other flash sections. Since I added the lwip_sec in my linkerscript, could that be a similar problem?

It may, but then again, it may be something completly different. Again, if possible, step back to a code base that does not show the problem, then rebuild your code piece by piece until the problem shows.

Hello @RAc,
I did what you suggested and I went step by step to my code and I realized that:

  1. My default task, was executing another function
 /* init code for USB_HOST */
  MX_USB_HOST_Init();

simultaneously with the LwIP. So I comment that function and my default task had only the LwIP. After that I disabled the MQTT implementation and set the configUSE_IDLE_HOOK to 0 from the freeRTOSConfig.h file, while my configTOTAL_HEAP_SIZE is equla to 131072. Also I set DEFAULT_THREAD_STACKSIZE = SLIPIF_THREAD_STACKSIZE = TCPIP_THREAD_STACKSIZE = 1024, from the lwipotps.h file, while my default task size is 6000*4. When I did that the error disappeared, however if I set one of the DEFAULT_THREAD_STACKSIZE = SLIPIF_THREAD_STACKSIZE = TCPIP_THREAD_STACKSIZE to another higher value (I need that because I have large mqtt messages to handle and the function that handles the incoming uses the TCPIP_THREAD_STACKSIZE) it gives me a hardfault error. Moreover, the mqtt implementation works correctly accept from the call back that handles the incoming messages:

static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags){

	char new_data[len];
	strncpy(new_data, (char *)data, len);
	new_data[len] = '\0';

	//u8_t *new_data = (u8_t *)malloc(len+1);
	if (new_data != NULL) {
		//memcpy(new_data, data, len);
		//new_data[len] = '\0';

		if (flags & MQTT_DATA_FLAG_LAST){
			/*f1-f3 handles the right socket requests*/
			if (flag0.fl.f1){	// Right state
				//strncpy(right.state, payload, len);
				strncpy(right.state, new_data, len);
				right_status = 1;
				flag0.fl.f1 = 0;
			}
			if (flag0.fl.f2){	// Right charging seconds.
				//setTime(payload, &right);
				setTime(new_data, &right);
				right_status = 1;
				flag0.fl.f2=0;
			}
			if (flag0.fl.f3){	// Right charging seconds.
				//parse_json(payload, &right);
				parse_json(new_data, &right);
				right_status = 1;
				flag0.fl.f3=0;
			}
			/*f4-f6 handles left socket requests*/
			if (flag0.fl.f4){	// Left state
				//strncpy(left.state, payload, len);
				strncpy(left.state, new_data, len);
				flag0.fl.f4 = 0;
				left_status=1;
			}
			if (flag0.fl.f5){	// Left charging seconds.
				//setTime(payload, &left);
				setTime(new_data, &left);
				left_status=1;
				flag0.fl.f5 = 0;
			}
			if (flag0.fl.f6){	// Left charging seconds.
				//parse_json(payload, &left);
				parse_json(new_data, &left);
				left_status=1;
				flag0.fl.f6 = 0;
			}
		}
	}
}

It is being called every second and I have six topics that are using this function. That’s why the flags are there. I noticed that the memcpy are causing error, and when I changed that to strncpy the error occured after a while than the previous with the memcpy.

That was my investigation until now.

The last line must read

	new_data[len-1] = '\0';

or allocate the array 1 bit larger.

Hello folks,
@RAc @aggarg

I think that I finally found the problem. Every time it was showing the same error, whatever changes I had been making in the software. I noticed that those functions that was appearing to the debug view has a stack in the ethernetif.c file which is the INTERFACE_THREAD_STACK_SIZE. I increase this value and also I increased the configTOTAL_HEAP_SIZE ((size_t)331072). Also, I made a change in the main.c file of my program and I set the MPU_Config() function to be called before the enabling of SCB_EnableDCache and SCB_EnableICache. Furthermore, I increased the TCPIP_THREAD_STACKSIZE 4096 parameter and finally my program works correctly without any error and hardfaults.

Thank you for sharing your solution!