USB Midi and Boost not working

Hi Guys,

The USB Midi seems to take a dive if using: hw.Init(false);

USBD_LL_Init@0x24009e0e (/Volumes/Crucial X8 2TB/Development/daisy/libDaisy/src/usbd/usbd_conf.c:46)
USBD_Init@0x24015e76 (/Volumes/Crucial X8 2TB/Development/daisy/libDaisy/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c:115)
InitFS@0x240167e2 (/Volumes/Crucial X8 2TB/Development/daisy/libDaisy/src/hid/usb.cpp:35)
daisy::UsbHandle::Init@0x240167e2 (/Volumes/Crucial X8 2TB/Development/daisy/libDaisy/src/hid/usb.cpp:97)
daisy::MidiUsbTransport::Impl::Init@0x24005826 (/Volumes/Crucial X8 2TB/Development/daisy/libDaisy/src/hid/usb_midi.cpp:88)
daisy::MidiUsbTransport::Init@0x24005826 (/Volumes/Crucial X8 2TB/Development/daisy/libDaisy/src/hid/usb_midi.cpp:183)
daisy::MidiHandler<daisy::MidiUsbTransport>::Init@0x240020f0 (/Volumes/Crucial X8 2TB/Development/daisy/libdaisy/src/hid/midi.h:99)
main@0x240020f0 (/Volumes/Crucial X8 2TB/Development/daisy/SimpleFMSynth/SimpleFMSynth.cpp:40)

Very rarely it get past this and works fine, maybe some kind of timing issue?

OK, I have some more info.

This is occurring when running code from sram with the isr vectors in flash, run everything in flash and all is ok.

Right I have tracked it down:

static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
{
  uint32_t count = 0U;

  /* Wait for AHB master IDLE state. */
  do
  {
    if (++count > 200000U)
    {
      return HAL_TIMEOUT;
    }
  }
  while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);

  /* Core Soft Reset */
  count = 0U;
  USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;

  do
  {
    if (++count > 200000U)
    {
      return HAL_TIMEOUT;
    }
  }
  while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);

  return HAL_OK;
}

As everything is build with -o3 those loops are optimised out, they do this sort of delay loop quite a bit in the HAL so this sort of issue may be in a few places.

A simple fix was:

#pragma GCC push_options
#pragma GCC optimize ("O0")

static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
{
  uint32_t count = 0U;

  /* Wait for AHB master IDLE state. */
  do
  {
    if (++count > 200000U)
    {
      return HAL_TIMEOUT;
    }
  }
  while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);

  /* Core Soft Reset */
  count = 0U;
  USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;

  do
  {
    if (++count > 200000U)
    {
      return HAL_TIMEOUT;
    }
  }
  while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);

  return HAL_OK;
}
#pragma GCC pop_options
1 Like

So this seems to be a known issue: ST Community

I have made similar changes here to what were mentioned in that link: Changes for issues with compiling with optimisation by AndrewCapon · Pull Request #484 · electro-smith/libDaisy · GitHub

1 Like

Thanks, @BobTheDog for reporting the issue, and making the PR! :slight_smile:

I’ll follow up a bit more on Github, but it looks like this is solid.

1 Like