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
Uwe Bonnes
Principal III

Probably malloc failed because of running out of RAM. Check your RAM allocation carefully.

Yohann M.
ST Employee

hello

this error is related to malloc issue done to allocate TCPC handle used by the USB-PD stack.

I suggest you to increase your heap size in your workspace which seems to be lower regarding this malloc.

(allocation is not linked to FreeRTOS allocation)

(ex on IAR: __ICFEDIT_size_heap__=0x800 used for .STM32CubeExpansion_USBPD_F0_V2.1.0\Projects\STM32F072RB-Nucleo\Applications\USB_PD\EVAL_FUSB307_DRP\EWARM\Project.ewp)

Yohann

FaikN
Associate II

Hi Yohann,

Thanks for the information. On a side note, I am using Atomic TrueStudio and GCC Compiler/linker. On the true studio project settings, I have changed the C Linker optimisation; which is page size allocation for malloc to 4096 bytes; but didn't do the work. is it something that i need to change regarding the linker settings- minimum heap size?

​I think you have to change the ld file available in your environment and to change the following line (refer to AC6 project):

_Min_Heap_Size = 0x1000;     /* required amount of heap */

FaikN
Associate II

I have changed the flash.ld file and increased the min heap size to 0x1000 as said. However i still get the same error code from us bpd_sttatus 0x22=mallocerror. RAM has already 57 kB free space also so i don't think that is the problem neither. Can there be any reasons or solutions?

Uwe Bonnes
Principal III

Are you talking about some STM32F0 as USBPD_F0_V2.1.0 implies? None of them has 56 kB RAM!

FaikN
Associate II

Im using the TCPM/TCPC architecture as example project and using STM32F105RC MCU as TCPM and PTN5110 as TCPC. So actually im not using STM32F0 as my TCPM. However, this was not issue before. now i wanted to create a clean project to test interrupts and alerts on power delivery and i face this issue.

Code of the function is very simple:

USBPD_StatusTypeDef USBPD_TCPM_HWInit(uint8_t PortNum, uint8_t TCPC_ToggleRole, USBPD_ParamsTypeDef *Params, USBPD_CAD_Callbacks *CallbackFunctions, TCPC_DrvTypeDef *TCPC_Driver)
{
  TCPM_DEBUG_TRACE(PortNum, TCPM_DEBUG_LEVEL_0, "USBPD_TCPM_HWInit");
  /* Initialize USB PD Handle structure to zero */
  if (PortNum < USBPD_MAXPORT_COUNT)
  {
    TCPM_Handles[PortNum] = malloc(sizeof(TCPM_HandleTypeDef));
    if (NULL == TCPM_Handles[PortNum])
    {
      return USBPD_MALLOCERROR;
    }
    memset(TCPM_Handles[PortNum], 0, sizeof(TCPM_HandleTypeDef));
    NbPortConfigured++;
  }
  else
  {
    return USBPD_ERROR;
  }
  TCPM_Handles[PortNum]->IsSwapOngoing        = TCPM_PortIsDoingSwap;
  TCPM_Handles[PortNum]->TCPC_ToggleRole      = TCPC_ToggleRole;
  TCPM_Handles[PortNum]->CAD_callback         = CallbackFunctions;
  TCPM_Handles[PortNum]->Params               = Params;
  TCPM_Handles[PortNum]->TCPC_Driver          = TCPC_Driver;
 
  return USBPD_OK;
}
 

With

typedef struct
{
  uint32_t   TCPM_SupportedSOP;         /*!< Supported SOP to be enabled by PRL */
  USBPD_TCPM_HandleTypeDef  e;
  uint8_t    TCPM_Initialized;
  uint8_t    TCPM_Connected;
  uint8_t    TCPM_TransmissionOnGoing;
  uint8_t (*IsSwapOngoing)(uint8_t);    /*!< function used by PHY to avoid Idle BUS check when a swap is ongoing */
  TCPC_DrvTypeDef *TCPC_Driver;
  USBPD_CAD_Callbacks *CAD_callback;    /*!< USBPD CAD callback                 */
  USBPD_ParamsTypeDef *Params;          /*!< USBPD Parameters                   */
  uint8_t    TCPC_ToggleRole;
  uint8_t    TCPC_BistOnGoing;
} TCPM_HandleTypeDef;
 

IT Seems that the problem i am facing is with malloc(). with malloc(0 i always get NULL result. on the other hand, if i use pvPortMalloc rather than malloc, i get results on memory allocation. The weird thing is I have successfully initialised the DPM before in a different project, where i also used TCPM/TCPC architecture same way i created from CubeMX. So I don't know what can be the problem with malloc in the USBPD_TCPM_HWInit() function. Is there any other recommendation you can give?