cancel
Showing results for 
Search instead for 
Did you mean: 

How to allocating dynamic memory in External SDRAM?

Tu Nguyen Ngoc
Associate II

Hi all,

I'm currently working on a project using the STM32H723 interface with SDRAM IS42S16400J. After booting up, I have to read a large JSON file [2MB] from flash into SDRAM then parse it to get the information. I wonder if I could allocating dynamic memory on SDRAM. Someone mentioned about modified linker and _sbrk function. But I don't know where to start.

Could anyone give me an example or advice? Thank you so much

Sincerely,

1 ACCEPTED SOLUTION

Accepted Solutions
PMath.4
Senior III

Yes just modify syscalls.c

here is my version SDRAMBASE is set to 0xD0000000. Ignore the error test that is application specific. You will need to test for the end of the area in SDRAM that you allocate

caddr_t _sbrk(int incr)
{
//	extern char end asm("end");
	static char *heap_end;
	char *prev_heap_end;
 
	if (heap_end == 0)
		heap_end = (char *)SDRAMBASE;//&end;
 
	prev_heap_end = heap_end;
//	if (heap_end + incr > stack_ptr)
	if ((uint32_t)heap_end + incr > (uint32_t)minMMheap)	{
		error("Heap and stack collision");
//		abort();
		errno = ENOMEM;
		return (caddr_t) -1;
	}
 
	heap_end += incr;
//	PIntH((uint64_t)(uint32_t)heap_end);PIntComma((uint64_t)(uint32_t)incr);PRet();
	return (caddr_t) prev_heap_end;
}

View solution in original post

7 REPLIES 7
Ozone
Lead

> Someone mentioned about modified linker and _sbrk function.

The _sbrk function is used by the malloc/free type functions, you would need to check and adapt it for your application accordingly.

This functions use memory from the heap, thus you would need to use SDRAM for the heap section.

And the _sbrk function needs "knowledge" about the size limits.

I don't know the H7 series, though.

But assuming the external bus is still 16 bit, I am not sure you are gaining anything.

Thanks for your reply,

Yes, I'm trying to move the heap section from RAM into SDRAM, so I can use malloc/calloc/free on SDRAM, btw what do you mean 16 bit, is this different with internal bus will affect my program?

You writes that your data is in flash, then you dont need copy it to ram when is used as readonly const source for parsing.

PMath.4
Senior III

Yes just modify syscalls.c

here is my version SDRAMBASE is set to 0xD0000000. Ignore the error test that is application specific. You will need to test for the end of the area in SDRAM that you allocate

caddr_t _sbrk(int incr)
{
//	extern char end asm("end");
	static char *heap_end;
	char *prev_heap_end;
 
	if (heap_end == 0)
		heap_end = (char *)SDRAMBASE;//&end;
 
	prev_heap_end = heap_end;
//	if (heap_end + incr > stack_ptr)
	if ((uint32_t)heap_end + incr > (uint32_t)minMMheap)	{
		error("Heap and stack collision");
//		abort();
		errno = ENOMEM;
		return (caddr_t) -1;
	}
 
	heap_end += incr;
//	PIntH((uint64_t)(uint32_t)heap_end);PIntComma((uint64_t)(uint32_t)incr);PRet();
	return (caddr_t) prev_heap_end;
}

> ... btw what do you mean 16 bit, is this different with internal bus will affect my program?

For all the STM32's I know, the external memory bus (FSMC), is 16-bit wide. This is a GPIO allocation issue.

And external interfaces tend to be slower than internal one's.

Could you please share your linker file. Thank you.

Thank you. After struggling with the linker file and _sbrk function, I'm able to move the heap section into sdram 🙂