Skip to main content
Tu Nguyen Ngoc
Associate III
March 25, 2021
Solved

How to allocating dynamic memory in External SDRAM?

  • March 25, 2021
  • 7 replies
  • 3065 views

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,

This topic has been closed for replies.
Best answer by PMath.4

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;
}

7 replies

Ozone
Principal
March 25, 2021

> 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.

Tu Nguyen Ngoc
Associate III
March 25, 2021

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?

MM..1
Super User
March 25, 2021

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
PMath.4Best answer
Senior III
March 25, 2021

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;
}

Tu Nguyen Ngoc
Associate III
March 26, 2021

Could you please share your linker file. Thank you.