Hello Team,
This is regarding a Hardfault issue in LPC55S69.
I have created a c++ project and implemented changes for my project. As part of it, I am making use of C++ STL library features.
Below is the code snippet,
I have an enum defined with few CMD codes,
typedef enum
{
CMD_INVALID = 0x9999,
CMD_0 = 0x0000,
CMD_1 = 0x0001,
CMD_2 = 0x0002,
CMD_3 = 0x0003,
CMD_4 = 0x0004,
CMD_5 = 0x0005,
CMD_6 = 0x0006,
CMD_7 = 0x0007,
CMD_8 = 0x0008,
CMD_9 = 0x0009,
}CMD_CODES;
I have initialized the map container
std::map<std::string, CMD_CODES> Protocol::commands_map;
In an initialization routine, inserting the elements
commands_map.emplace( std::string( "CMD1" ), CMD_1);
commands_map.emplace( std::string( "CMD2" ), CMD_2 );
commands_map.emplace( std::string( "CMD3" ), CMD_3 );
commands_map.emplace( std::string( "CMD4" ), CMD_4 );
commands_map.emplace( std::string( "CMD5" ), CMD_5 );
commands_map.emplace( std::string( "CMD6" ), CMD_6 );
commands_map.emplace( std::string( "CMD7" ), CMD_7 );
commands_map.emplace( std::string( "CMD8" ), CMD_8 );
commands_map.emplace( std::string( "CMD9" ), CMD_9 );
After this initialization, I will performing other tasks.
But I am getting a hardfault error, in the initialization routine while inserting the elements. I am wondering, what is going on here.
Can someone please let me know, what the issue is?
Regards,
San
Solved! Go to Solution.
Hi @sushmasan ,
both heap and stack (MSP) size you can easily change in the linker settings in MCUXpresso IDE (no linker file modification needed). Here are the settings with the MPS stack marked:
As for PSP (process stack pointer): this is used for the stacks of threads. Settings is depending on the RTOS used, for example for FreeRTOS it is a configuration define:
#define configTOTAL_HEAP_SIZE (32*1024)
I hope this helps,
Erich
Thanks for explanation @ErichStyger , @Harry_Zhang, it is very helpful.
While debugging the code, what I noticed was the heap size increasing which is very close to 100%, that's when hardfault is triggered. When I checked the allocated stack and heap size it was 4KB, that was the reason for the failure. After configuring the heap size to 8KB, the code worked just fine. The heap size used was around 6.2KB.
As you mentioned try more space for MSP, PSP stack. How can I do that?
Hi @sushmasan ,
both heap and stack (MSP) size you can easily change in the linker settings in MCUXpresso IDE (no linker file modification needed). Here are the settings with the MPS stack marked:
As for PSP (process stack pointer): this is used for the stacks of threads. Settings is depending on the RTOS used, for example for FreeRTOS it is a configuration define:
#define configTOTAL_HEAP_SIZE (32*1024)
I hope this helps,
Erich
to add to that: the stack size for each task in FreeRTOS is assigned as parameter in the xTaskCreate() API call.
Hi @sushmasan
To increase the stack sizes for the Main Stack Pointer (MSP) and the Process Stack Pointer (PSP) in an MCUXpresso project, you typically need to modify the linker script or relevant configuration settings in your project. Here’s how you can do that:
1. Check the Heap Size Definition
If you need to modify the heap size as well, look for similar definitions for the heap:
__HeapBase = __end__;
__HeapLimit = __StackLimit;
The heap size is usually defined by the difference between __HeapBase and __HeapLimit. Adjust these definitions accordingly if needed.
2. Adjusting MSP and PSP Stack Sizes
The MSP is used by default and typically allocated from the top of the RAM. If you are using a Real-Time Operating System (RTOS) like FreeRTOS, the PSP is used for tasks, and its size is managed by the RTOS configuration. For bare-metal applications, MSP adjustments in the linker script are usually sufficient.
For RTOS-based applications:
• MSP Stack Size: Controlled by the linker script (as shown above).
• PSP Stack Size: Adjusted via the RTOS configuration, often defined in FreeRTOSConfig.h or a similar configuration file.
BR
Hang
Looks like a possible stack overflow to me. Try to reserve more space for your MSP and PSP stack.
Other than that: this is the hardfault handler I'm using:
https://github.com/ErichStyger/McuOnEclipseLibrary/blob/master/lib/src/McuHardFault.c
I hope this helps,
Erich