cancel
Showing results for 
Search instead for 
Did you mean: 

SDIO, USB Mass Storage, and Mac

sb_st
Associate III

Hi! I am trying to learn about interfacing with an stm32f405 as a USB Mass Storage Device. My (homemade) development board has an onboard microSD card slot, and so I would like to present the card as my mass storage. 

I'm running into some problems with this, however, and here are my symptoms:

  • If I set aside the SDIO component of things temporarily, and just try to present the Stm32's 1kb of internal memory as my mass storage, I see a nice "New accessory detected" popup on my mac, and see the stm32 enumerated in Disk Utility. My mac complains the disk is unformatted, and so it is not mounted. But overall this seems to suggest that the USB Mass Storage bit of things is working. 
  • When I try to use 1-wire SDIO (and modify usbd_storage_if.c accordingly) I can see the Mass Storage device enumerated in Disk Utility, and it is listed as a fat filesystem, but I cannot mount the drive. I am unsure why. 
  • When I try to use 4-wire SDIO, the system isn't recognized at all by my mac. Stepping through the debugger, it seems that I am entering the Error_Handler() part of main.c by way of SD_FindSCR().

I have tried swapping out my development board for an Adafruit Feather that uses the same exact microcontroller, and this exhibits the same behavior. I have tried swapping my memory card, and my USB cable.

This is my first foray into this, so I am rather inexperienced in knowing where/how to troubleshoot this. I have a vague sense that SD cards can be finicky, though I am not sure how true that is. I'm curious if anyone might be able to suggest some troubleshooting steps, or learning resources that might help me teach myself what might be going wrong here?

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
sb_st
Associate III

Hm, digging through the forums, this "1-bit works, 4-bit doesn't" problem seems pretty commonly reported, and commonly traced to a HAL issue, rather than a hardware issue. 

This solution:

https://community.st.com/t5/stm32cubemx-mcus/sdio-interface-not-working-in-4bits-with-stm32f4-firmware/m-p/591803/highlight/true#M26033

seems to work in my case. Namely, HAL creates this code natively for my f4 MCU:

 

  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_4B;
  hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd.Init.ClockDiv = 5;
  if (HAL_SD_Init(&hsd) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK)
  {
    Error_Handler();
  }

 

and, if I change line 5 above to be:

 

hsd.Init.BusWide = SDIO_BUS_WIDE_1B;

 

such that we init at 1b mode, then shift to 4b mode, my device seems to present itself (and mount itself) fine on my macbook, and I can successfully copy a file to/from it. 

View solution in original post

1 REPLY 1
sb_st
Associate III

Hm, digging through the forums, this "1-bit works, 4-bit doesn't" problem seems pretty commonly reported, and commonly traced to a HAL issue, rather than a hardware issue. 

This solution:

https://community.st.com/t5/stm32cubemx-mcus/sdio-interface-not-working-in-4bits-with-stm32f4-firmware/m-p/591803/highlight/true#M26033

seems to work in my case. Namely, HAL creates this code natively for my f4 MCU:

 

  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_4B;
  hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd.Init.ClockDiv = 5;
  if (HAL_SD_Init(&hsd) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK)
  {
    Error_Handler();
  }

 

and, if I change line 5 above to be:

 

hsd.Init.BusWide = SDIO_BUS_WIDE_1B;

 

such that we init at 1b mode, then shift to 4b mode, my device seems to present itself (and mount itself) fine on my macbook, and I can successfully copy a file to/from it.