Skip to content

dcd_dwc2.c - reset_core() halts in infinite loop #2074

Open
@MarekRyn

Description

@MarekRyn

Operating System

Windows 10

Board

Custom STM32H747BGT6

Firmware

Custom firmware with use of TinyUSB lib. Affected file: src/portable/synopsys/dwc2/dcd_dwc2.c

What happened ?

dcd_dwc2.c - function reset_core() halts in infinite loop after restart or power up of MCU. It happens at random times.

Present code:

static void reset_core(dwc2_regs_t * dwc2)
{
  // reset core
  dwc2->grstctl |= GRSTCTL_CSRST;

  // wait for reset bit is cleared
  // TODO version 4.20a should wait for RESET DONE mask
  while (dwc2->grstctl & GRSTCTL_CSRST) { }     // <<---- Software halts here

  // wait for AHB master IDLE
  while ( !(dwc2->grstctl & GRSTCTL_AHBIDL) ) { }

  // wait for device mode ?
}

Solution:
As per ST reference manual RM0399 page 2011: The software must also check that bit 31 in this register is set to 1 (AHB Master is Idle) before starting any operation.

Also in HAL original library mentioned check is carried out before reset operation (see: function USB_CoreReset in stm32h7xx_ll_usb.c)

Proposal for corrected code:

static void reset_core(dwc2_regs_t * dwc2)
{
  // wait for AHB master IDLE
  while ( !(dwc2->grstctl & GRSTCTL_AHBIDL) ) { }

  // reset core
  dwc2->grstctl |= GRSTCTL_CSRST;

  // wait for reset bit is cleared
  // TODO version 4.20a should wait for RESET DONE mask
  while (dwc2->grstctl & GRSTCTL_CSRST) { }

  // wait for device mode ?
}

How to reproduce ?

Error happens randomly during restart or powering up of uC.

Debug Log as txt file (LOG/CFG_TUSB_DEBUG=2)

N/A

Screenshots

No response

I have checked existing issues, dicussion and documentation

  • I confirm I have checked existing issues, dicussion and documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions