AnsweredAssumed Answered

Return Code in Cube MX's initialization functions

Question asked by hartung.robert on Sep 9, 2015
Latest reply on Oct 12, 2015 by 68516
When using CubeMX to generate project (e.g. for SW4STM32), the code for SD Card looks like this:

void MX_SDIO_SD_Init(void)
{
  hsd.Instance = SDIO;
  hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
  hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
  hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
  hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
  hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd.Init.ClockDiv = 4;
  HAL_SD_Init(&hsd, &SDCardInfo);

  HAL_SD_WideBusOperation_Config(&hsd, SDIO_BUS_WIDE_4B);
}

The user has no possibility to add own error detection code, as there is no use section. My suggestion is to return the status type like this:

HAL_SD_ErrorTypedef MX_SDIO_SD_Init(void)
{
 HAL_SD_ErrorTypedef err = SD_OK;
  hsd.Instance = SDIO;
  hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
  hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
  hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
  hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
  hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd.Init.ClockDiv = 4;


  err = HAL_SD_Init(&hsd, &SDCardInfo);
  if(err != SD_OK) {
       return err;
  }


  err = HAL_SD_WideBusOperation_Config(&hsd, SDIO_BUS_WIDE_4B);
  return err;
}

- Robert

Outcomes