cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G071 hardfault in USBPD middleware when configured with 1 source and 1 sink port

NathanWolcott
Associate III

I'm trying to get the USBPD middleware running with 2 ports: 1 source and 1 sink. I already followed the tutorial to get 1 port working as a sink. When I add the second port as a source, the chip immediately hits a hardfault after starting. I traced it back and the problem is on line 1013 of usbpd_cad_hw_if.c, where _handle->CAD_PtrStateMachine(PortNum, pEvent, pCCXX) is run. The problem is that for some reason, _handle->CAD_PtrStateMachine is never initialized and is actually a NULL pointer. Naturally, this hardfaults immediately.

The problem with the init seems to be around line 296 to 358. There are a number of #if defined (***) macros that control which functions are assigned to _handle->CAD_PtrStateMachine depending on whether _SNK, _SRC, or _DRP are defined in the project. It looks like CubeMx is defining _DRP when one of the ports is a sink and the other is a source even though neither port is actually configured for dual role. This activates the code path at line 298 but then, since neither port is actually dual role, the it fails the check on 299 and so CAD_PtrStateMachine is never set.

After the init finishes, a cable detech event is fired for both ports, which eventually causes _handle->CAD_PtrStateMachine() to be executed unconditionally.

I've pasted the snippet from usbpd_cad_hw_if.c below (note that the line numbers won't match).

Yes, it works just fine with both ports configured as sinks.

I'm using firmware V1.4.1 and CubeIDE 1.6 (although the same problem exists in CubeIDE 1.5).

 /* Set the state machine according the SW configuration */
#if !defined(USBPDCORE_LIB_NO_PD)
#if defined(_DRP)
  if (Ports[PortNum].settings->CAD_RoleToggle == USBPD_TRUE)
  {
    _handle->CAD_PtrStateMachine = CAD_StateMachine_DRP;
    _handle->CAD_Accessory_SRC = Ports[PortNum].settings->CAD_AccesorySupport;
#if defined(USBPDCORE_VPD)
    _handle->CAD_VPD_SRC = Ports[PortNum].settings->CAD_VPDSupport;
#endif /* USBPDCORE_VPD */
  }
  else
#endif /* !USBPDCORE_LIB_NO_PD */
  {
#if defined(_SRC)
    if (USBPD_PORTPOWERROLE_SRC == Ports[PortNum].settings->PE_DefaultRole)
    {
      _handle->CAD_PtrStateMachine = CAD_StateMachine_SRC;
      _handle->CAD_Accessory_SRC = Ports[PortNum].settings->CAD_AccesorySupport;
#if defined(USBPDCORE_VPD)
      _handle->CAD_VPD_SRC = Ports[PortNum].settings->CAD_VPDSupport;
#endif /* USBPDCORE_VPD */
    }
    else
#endif /* _SRC */
    {
#if defined(_SNK)
      _handle->CAD_PtrStateMachine = CAD_StateMachine_SNK;
      _handle->CAD_Accessory_SNK = Ports[PortNum].settings->CAD_AccesorySupport;
#if defined(USBPDCORE_VPD)
      _handle->CAD_VPD_SNK = Ports[PortNum].settings->VPDSupport;
#endif /* USBPDCORE_VPD */
#endif /* _SNK */
    }
  }
#else  /* USBPDCORE_LIB_NO_PD */
#if defined(USBPD_TYPE_STATE_MACHINE)
#if defined(_SRC)
    if (USBPD_PORTPOWERROLE_SRC == Ports[PortNum].settings->PE_DefaultRole)
    {
      _handle->CAD_PtrStateMachine = CAD_StateMachine_SRC;
#if !defined(USBPDCORE_LIB_NO_PD)
      _handle->CAD_Accessory_SRC = Ports[PortNum].settings->CAD_AccesorySupport;
#endif /* !USBPDCORE_LIB_NO_PD */
#if defined(USBPDCORE_VPD)
      _handle->CAD_VPD_SRC = Ports[PortNum].settings->CAD_VPDSupport;
#endif /* USBPDCORE_VPD */
    }
    else
#endif /* _SRC */
    {
#if defined(_SNK)
      _handle->CAD_PtrStateMachine = CAD_StateMachine_SNK;
#if !defined(USBPDCORE_LIB_NO_PD)
      _handle->CAD_Accessory_SNK = Ports[PortNum].settings->CAD_AccesorySupport;
#endif /* !USBPDCORE_LIB_NO_PD */
#if defined(USBPDCORE_VPD)
      _handle->CAD_VPD_SNK = Ports[PortNum].settings->CAD_VPDSupport;
#endif /* USBPDCORE_VPD */
#endif /* _SNK */
    }
#endif  /* USBPD_TYPE_STATE_MACHINE */
#endif  /* USBPDCORE_LIB_NO_PD */

11 REPLIES 11

Dear @NathanWolcott​ 

I took your IOC and unfortunately, I did not able to reproduce your issue. Did you try to generate the project into an empty folder? I mean just with your IOC file?

I would suggest to open a new thread, it will allow my CubeMX colleagues to participate to this thread.

In this thread, could you please attach 'STM32CubeMX.log' available in your folder 'C:\Users\<login>\.stm32cubemx'?

Thanks

Yohann

Hi @Yohann M.​ 

I tried copying the IOC file into a different directory, opening it in CubeMX, and regenerating the project. The library file was correctly configured in the new project. Very strange.

I will go ahead a create a new thread for this problem.

As for the log file, is this the correct file when I'm using the build-in CubeMX inside CubeIDE 1.6 to generate the project? As I mentioned earlier, since I switched over from TrueStudio to CubeIDE, I don't usually use the standalone version of CubeMX to generate my projects. I wasn't sure if both versions use the same log file.

Thanks,

Nathan