Hello @__AdhipShukla ,
To modify the linker script so that a specific memory region (like SRAM1) is used exclusively for dynamic allocation by an RTOS like ThreadX, As you mentioned you need to ensure that the region is defined correctly in the linker script . You also need to inform ThreadX to use this region for its memory pool.
First, you need to define the memory region in your linker script:
MEMORY
{
/* Define other memory regions */
SRAM1 (rw) : ORIGIN = 0x20000000, LENGTH = 64K /* Adjust the origin and length as per your MCU's specifications */
/* Define other memory regions */
}
Next, you'll want to create a section within this memory region that will be used for dynamic allocation:
SECTIONS
{
.dynamic_alloc :
{
. = ALIGN(4);
_sbrk_heap_start = .; /* Define start of the heap */
*(.dynamic_heap)
. = ALIGN(4);
_sbrk_heap_end = .; /* Define end of the heap */
} > SRAM1
/* Define other sections */
}
PS: Make sure that no other data or code is placed into this section by the linker. This is typically done by not assigning any other sections or symbols to .dynamic_heap.
In your application code, you need to inform ThreadX to use the defined memory region for its memory pool. This is typically done by passing the start address and size to the ThreadX memory pool initialization function:
extern CHAR* _sbrk_heap_start;
extern CHAR* _sbrk_heap_end;
void tx_application_define(void* first_unused_memory)
{
/* Calculate the size of the heap */
ULONG heap_size = (ULONG)(_sbrk_heap_end - _sbrk_heap_start);
/* Create a memory pool from the heap */
tx_byte_pool_create(&my_byte_pool, "byte pool", _sbrk_heap_start, heap_size);
}
Finally, modify your application's dynamic memory allocation calls to use the ThreadX memory pool API instead of the standard library allocation functions such as malloc() and free():
void* ptr;
UINT status;
/* Allocate memory from the pool */
status = tx_byte_allocate(&my_byte_pool, &ptr, size_needed, TX_NO_WAIT);
if (status != TX_SUCCESS)
{
/* Handle allocation error */
}
I hope my answer has helped you. When your question is answered please close this topic by marking as Best the reply that answered you, it will help others find that answer faster. Thanks for your contribution.