cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U575 - USBPD_CAD_Init error wit Thread-Safe setting

Pepils
Associate II

Hello,

I am building an application using ThreadX, FileX, USBX and USBPD. Everyting runs perfectly until I bring USBPD in the game. Whatever I do, I always get an error when the library calls USBPD_CAD_Init().

More precisely, the function fails at a call to malloc when calling _mutex_aquire(mutex_t *lock). The error comes from the fact that the mutex is not initialized. This gives me a hardfault.

After some digging, I found out the mutex is well initialized at startup, but then uninitialized right away. The two related functions are called in this exact order:

  1. _init_alloc() => _mutex_initialize(mutex_t *lock): initializes the mutex and returns 1.
  2. __ccp_initialize_aeabi_() => __armlib_thread_safety_init() : mark all mutexes as uninitialized

It this expected?

I am using the "multi-threaded support" option and tried with different strategies (default, #2 and #3), always get the same result.

Environment
STM32CubeMX: v6.8.1
FW Package: STM32Cube FW_U5 V1.2.0
MCU: STM32U575VGTx
IDE: MDK-ARM v5.37
Board: custom

Many thanks in advance for anyone that can help

5 REPLIES 5
HFISTM
ST Employee

Hello @Pepils ,

The USBPD_CAD_Init functions calls some part of the USBPD lib which performs a dynamic allocation (malloc).

What is the return code of USBPD_CAD_Init ? Could it be USBPD_CAD_MALLOCERROR ?
You may want to try to increase the Heap in your linker settings. 

Regards

Hello @HFISTM , thank you for your answer.

The USBPD_CAD_Init does not return anything as it is blocked in this code sample from armlib_lock_glue.c

/**
  * @brief Acquire lock mutex
  * @param lock The lock
  */
__attribute__((used)) void _mutex_acquire(mutex_t *lock)
{
  STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);

  stm32_lock_acquire(&static_list_lock);
  if (STM32_LOCK_INITIALIZED(STM32_GET_LOCK_PTR(lock)) == 1)
  {
    stm32_lock_acquire(STM32_LOCK_PARAMETER(STM32_GET_LOCK_PTR(lock)));
  }
  else
  {
    STM32_LOCK_BLOCK(); ==> Blocking here
  }
  stm32_lock_release(&static_list_lock);
}

 STM32_LOCK_BLOCK() doing the following:

#define STM32_LOCK_BLOCK()                      \
  do                                            \
  {                                             \
    __disable_irq();                            \
    Error_Handler();                            \
    while (1);                                  \
  } while (0)

 The heap is currently set to 0x8000 which should be way more than needed.

Any idea?

KR

Piranha
Chief II
Pepils
Associate II

Hello again,

I still haven't got it to work. Tried lots of configurations, nothing works. Can it be a problem with Keil? Because I see that everything happends in precompiled libraries dedicated to keil. And all example codes that are published in GitHub have an IOC file setup for EWARM.

I bought the STM32U575I-EV eval board so I can try flashing the example code without changing anything. Will see if this works.

PS: I moved to CubeMX V6.9.1 by the time, no differences.

Piranha
Chief II

Put a breakpoint in _mutex_initialize() function. What happens there? Is it called at all?