cancel
Showing results for 
Search instead for 
Did you mean: 

Reason of USBPD_MALLOCERROR In STM32Cube Expansion USBPD_F0_V2.1.0?

FaikN
Associate II

Hi,

I am using firmware package of USBPD_F0_V2.1.0 for utilising Power delivery by TCPM/TCPC configuration, using the example application. During initialisation- debugging of USBPD_DPM , I get an error code of USBPD_StatusTypeDef of 0x22 which is USPD_MALLOCERROR. This error comes from the function of USBPD_TCPM_HWInit which initialises the TCPC drivers in usbpd_dpm_core file. I would like to know what is the source of this error. I am using heap_4.c and the heap size is more than enough for memory allocation. What can be the reason of malloc error in USBPD_TCPM_HWInit function? I am using a heap size of 4200 bytes with dynamic allocation set to 1.

16 REPLIES 16

Thanks a lot by the way with the help so far!

Found a information here... Do you syscall.c in your environment? if yes, do you have function '_sbrk' inside?

Hey Yohann,

Thanks again. Yes I have the syscalls.c file in my environment within the project. And it has the _sbrk function where the heap management is done. I have checked the project settings and I have the -specs=nosys.specs settings and i couldn't delete this from the linker settings. Do you know how i can edit this in true studio? Besides when i comment out the _sbrk function in sys calls.c, the malloc works fine, so does the USBPD_TCPM_HWInit() function. Is this a good solution? or i may face problems regarding the stack collision management?

Hello mynja

We checked the TrueStudio User Manual and in page51, there is a recommendation to generate Syscall when RTOS is used.

I suggest to use the following code in your syscall.c:

#define FreeRTOS
#define MAX_STACK_SIZE 0x500
 
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
 
#ifndef FreeRTOS
  register char * stack_ptr asm("sp");
#endif
 
caddr_t _sbrk(int incr)
{
	extern char end asm("end");
	static char *heap_end;
	char *prev_heap_end,*min_stack_ptr;
 
	if (heap_end == 0)
		heap_end = &end;
 
	prev_heap_end = heap_end;
 
#ifdef FreeRTOS
	/* Use the NVIC offset register to locate the main stack pointer. */
	min_stack_ptr = (char*)(*(unsigned int *)*(unsigned int *)0xE000ED08);
	/* Locate the STACK bottom address */
	min_stack_ptr -= MAX_STACK_SIZE;
 
	if (heap_end + incr > min_stack_ptr)
#else
	if (heap_end + incr > stack_ptr)
#endif
	{
//		write(1, "Heap and stack collision\n", 25);
//		abort();
		errno = ENOMEM;
		return (caddr_t) -1;
	}
 
	heap_end += incr;
 
	return (caddr_t) prev_heap_end;
}

Regards,

Yohann

Hey Yohann,

WE are still working on the same project with TCPM/TCPC architecture. Indeed we are trying to establish firmware with PortNumbers=8. WE are getting USBPD_ERROR when the number of ports is 3. We realised that there is internal USBPD_MAXPORT_COUNT within the code and we could not find this definition in the library. What is this number and is there a way to change it? It is important for us since we need to use 8 Ports configuration.

Thanks in advance.

Best Regards,

Faik

​Hi Faik,

You are right, we have limited max of ports number to 2 (for memory footprint mainly). We will remove this dependancy in a future library delivery.

For your information, even I2C allows to use a big list of slaves, you could have a latency issue to answer in the good timeframe to a PD message (refer to study done in USB DevDays presentation done in 2015, https://manualzz.com/doc/12523600/usb-type-c%C2%99-port-controller--tcpc--for-usb).

Regards,

Yohann

Hey Yohann,

Thanks for your quick response! I would like to ask if there is a possibility for you to edit the library to be used for max ports number of 8 and we can do the testing and debugging in exchange for you? This may speed up the process for both sides i think. Otherwise, do you know when there will be the updated library delivery?

I will also have a look into latency issues may arise for i2c.

Best Regards,

Faik.