MarlinFirmware / Marlin

Marlin is an optimized firmware for RepRap 3D printers based on the Arduino platform. Many commercial 3D printers come with Marlin installed. Check with your vendor if you need source code for your specific machine.
https://marlinfw.org
GNU General Public License v3.0
16.19k stars 19.22k forks source link

[BUG] USB_FLASH_DRIVE_SUPPORT causes compilation error for STM32F401CC/STM32F411CE MCUs #26079

Open jmz52 opened 1 year ago

jmz52 commented 1 year ago

Did you test the latest bugfix-2.1.x code?

Yes, and the problem still exists.

Bug Description

Unlike their more expensive STM32F407 cousins, the STM32F4x1Cx MCUs do not have a luxury of secondary USB controller.

USB_FLASH_DRIVE_SUPPORT on STM32 requires custom framework-arduinoststm32 https://github.com/rhapsodyv/Arduino_Core_STM32/archive/usb-host-msc-cdc-msc-3.zip USB Host initialization function expects both USB_OTG_FS (Full Speed) and USB_OTG_HS (High Speed) to be defined at compilation time. STM32F401CC MCU only has USB_OTG_FS which leads to compilation errors due to undefined USB_OTG_HS macro.

USBH_LL_Init() function should be updated to exclude HOST_HS code when USB_OTG_HS is not defined. https://github.com/rhapsodyv/Arduino_Core_STM32/blob/usb-host-msc-cdc-msc-3/cores/arduino/stm32/usb_host/usbh_conf.c#L237

Here is the corrected code where the #ifdef USB_OTG_HS check is used to block unused section of the code. @rhapsodyv, please apply this fix to usb-host-msc-cdc-msc-3 branch.

USBH_StatusTypeDef USBH_LL_Init(USBH_HandleTypeDef *phost)
{
  /* Init USB_IP */
  if (phost->id == HOST_FS) {
    /* Link the driver to the stack. */
    g_hhcd.pData = phost;
    phost->pData = &g_hhcd;

    g_hhcd.Instance = USB_OTG_FS;
    g_hhcd.Init.Host_channels = 8;
    g_hhcd.Init.speed = HCD_SPEED_FULL;
    g_hhcd.Init.dma_enable = DISABLE;
    g_hhcd.Init.phy_itface = HCD_PHY_EMBEDDED;
    g_hhcd.Init.Sof_enable = DISABLE;
    if (HAL_HCD_Init(&g_hhcd) != HAL_OK) {
      // Error_Handler( );
      return USBH_FAIL;
    }

    USBH_LL_SetTimer(phost, HAL_HCD_GetCurrentFrame(&g_hhcd));
  }
#ifdef USB_OTG_HS
  else if (phost->id == HOST_HS) {
    /* Link the driver to the stack. */
    g_hhcd.pData = phost;
    phost->pData = &g_hhcd;

    g_hhcd.Instance = USB_OTG_HS;
    g_hhcd.Init.Host_channels = 12;
    g_hhcd.Init.speed = HCD_SPEED_FULL;
    g_hhcd.Init.dma_enable = DISABLE;
#ifdef USE_USB_HS_IN_FS
    g_hhcd.Init.phy_itface = USB_OTG_EMBEDDED_PHY;
#else
    g_hhcd.Init.phy_itface = USB_OTG_ULPI_PHY;
#endif
    g_hhcd.Init.Sof_enable = DISABLE;
    g_hhcd.Init.low_power_enable = DISABLE;
    g_hhcd.Init.vbus_sensing_enable = DISABLE;
    g_hhcd.Init.use_external_vbus = DISABLE;

    if (HAL_HCD_Init(&g_hhcd) != HAL_OK) {
      Error_Handler();
    }

    USBH_LL_SetTimer(phost, HAL_HCD_GetCurrentFrame(&g_hhcd));
  }
#endif // USB_OTG_HS
  return USBH_OK;
}

Expected behavior

Marlin can be compiled for STM32F401CC with USB_FLASH_DRIVE_SUPPORT and USE_OTG_USB_HOST options enabled.

Actual behavior

Enabling USB_FLASH_DRIVE_SUPPORT and USE_OTG_USB_HOST causes compilation errors.

Version of Marlin Firmware

bugfix-2.1.x

Electronics

STM32F401CCU6 and STM32F411CEU6 boards a.k.a black pill

thisiskeithb commented 1 year ago

I've pinged @rhapsodyv on Discord and they were willing to update their fork, but we may want to bringArduino_Core_STM32 under Marlin's list of managed code / bring in my Arduino_Core_STM32 fork with SoftwareSerial fix for the Biqu BX as well while we're at it.