cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H743VI USB mass storage device and uSD card

RPuccio
Associate II

Hello everyone, and thank you in advance for chiming in on this topic !

 

I'm having trouble getting an STM32H743VI pass a 32GB uSD-Card to my computer through USB_OTG_FS. Please consider me a noob at embedded system.

I'm following Phil's Lab video (https://www.youtube.com/watch?v=aEwwQMdKd-c) which uses the same family of microcontrollers and manages to get his SD card recognized by his computer, with write and read working. I'm following the same steps, but when I connect the board to my computer, no available space is shown and double clicking on the listed USB device prompt me to format it (even though trying to format it doesn't work that way).

The project I'm working need wave file transfer between a computer and the STM32 so that the STM can read it later on and use them for real-time audio processing. In order to achieve that, I have enabled FatFS, SDMMC1, USB_OTG_FS and USB_DEVICE. I already have some code running the real-time audio processing part, but I disabled it just for testing.

I've used some default FatFS examples to try to write to the SD card and can confirm that I've been able to format it and write some text files that can properly be read on my computer. It leads me to believe that the STM32 and the uSD connection is working, so hardware should be alright.

IDE version : 1.18.1
STM32H7 firmware version : 1.12.1

As per the video, I've added a couple lines to the usbd_storage_if.c file :

#define SD_CARD_TIMEOUT_MS 5000
extern SD_HandleTypeDef hsd1;

And modified the STORAGE_GetCapacity_FS function :

int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
  /* USER CODE BEGIN 3 */
  HAL_SD_CardInfoTypeDef sdInfo;
  if(HAL_SD_GetCardInfo(&hsd1, &sdInfo) != HAL_OK){
    return (USBD_FAIL);
  }
  *block_num = sdInfo.LogBlockNbr - 1;
  *block_size = sdInfo.LogBlockSize;
  return (USBD_OK);
  /* USER CODE END 3 */
}

 

Read and Write functions have also been modified to match what's written in the video, but just going through debug and adding a breakpoint on the line "return (USBD_OK);" is enough to see that the HAL_SD_GetCardInfo gets properly triggered, but *block_num = -1 and *block_size = 0.

Clock configuration seems ok to me, I've tried the same settings as in the video and the ones attached in the files below and the issue remains. Please note that I am using HSI instead of an external oscillator. Here are the other important info not shown in the screenshots :

SDMMC1 :
- Tried either Very High and Low output speed with no luck.
- Global interrupt are enabled.

SDMMC1.png

USB_OTG_FS :
- All interrupts enabled

USB_OTG_FS.png

USB_DEVICE :
- Device descriptor has not been changed

USB_DEVICE.png

FatFS :
- SD card is the only option checked
- "Set Defines" is left at default values
- "Use dma template" is Enabled
- "Platform settings" Detect_SDIO pin is properly set, with voltage measuring 3.3V (pull-up) without SD card and 0V with the SD card inserted.

I'm not sure where to look, as it seems the STM32 can talk both to the computer (otherwise it wouldn't been displayed as a USB mass storage device) and to the SD card (or I wouldn't be able to write on it at all with the FatFS basic example). I feel like something is wrong and that the STM32 can't get the proper values for the block_num and block_size parameters.

I'm sure I'm missing some point here, and I've never went so deep in embedded systems before so bare with my *** questions. At this point, any help is appreciated !

Thanks in advance,
Best regards,
Rodolphe

 

1 ACCEPTED SOLUTION

Accepted Solutions
AScha.3
Super User

Hi,

try first. sdmmc with 1 bit mode , no DMA; pin setting : all pins pullups ON, speed medium ;

clock for sdmmc 50MHz (i use 100M , internal divider 1 ( so is /2  = 50M );

+

And board ? custom ? (Line length to sdmmc is critical, less than 50mm is fine )

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

6 REPLIES 6
RPuccio
Associate II

Quick update : I tried again with an empty project, following the same instruction as in the video, but no luck !

AScha.3
Super User

Hi,

try first. sdmmc with 1 bit mode , no DMA; pin setting : all pins pullups ON, speed medium ;

clock for sdmmc 50MHz (i use 100M , internal divider 1 ( so is /2  = 50M );

+

And board ? custom ? (Line length to sdmmc is critical, less than 50mm is fine )

If you feel a post has answered your question, please click "Accept as Solution".
RPuccio
Associate II

Hi AScha.3 and thank you very much for your reply !

Now trying SDMMC1 in "SD 1 bit" mode, all pins pullups on, speed medium, clock 50 MHz by setting the clock divider factor to 1. DMA is already disabled in my test project now, because I enabled the bare minimum which are USB_DEVICE in mass storage class, SDMMC1 and USB_OTG_FS in device-only mode with no internal IP DMA.

Board is custom indeed. Max line length to SDMMC is about 43mm, and I don't expect to run the SD card at high speed as it's just for transferring wave files between the project and a computer.

Now, I do get the SD card to show after a short time (maybe 10 seconds ?).

Trying a couple things, in order :
- Switching to SDMMC 4 bit mode, and a divider of 4 (clock frequency 12.5 MHz) -> it works.
- Switching to SDMMC 4 bit mode, and a divider of 2 (clock frquency 25MHz) -> it does not work.
- Switching off the pull-up/pull-down on the STM32 configuration, as I already have them in hardware, and going back to a divider of 4 -> it works.
At that point, oscilloscope is now scanning the clock and sure enough, the signal has a lot of overshoot (25% !)
- Switching to a very high output speed : still works, no change to overshoot.
I use 22 ohm resistors on every pin, placed as close as possible to the STM32. TVS are placed as close as possible to the SD card connector, so I guess my issue is hardware based.
- Switching to a low output speed : overshoot looks way better on the scope (3%), but I still can't increase the clock frequency above 12.5MHz.

Well, at least, I know how picky the STM32 and the SD card can be when it comes to signal quality. I'll try a couple other hardware modifications (no TVS, higher series resistors) and see if it lets me increase the clock frequency, but as long as I can transfer data easily, I'm good !

Still, I don't understand how it is possible that FastFS can read and write data at higher frequencies, but the USB mass storage needs lower speed. I may open topics related to that project later, but I'm not stuck anymore for now on. Thank you very much AScha.3 for helping me making that thing work ! 

Ok,

so with real pullups , no need to switch on the internal (but also no bad.).

+

> Please note that I am using HSI instead

No good idea for USB ! (maybe also SD card) Use HSE crystal or oscillator as "master" clock.

USB only working on other than a crystal reference, if explicitly stated in rm.

+

pin speed for USB : try all....low to high; (depens on your added damping resitors an trace impedance, what best).

If you feel a post has answered your question, please click "Accept as Solution".

Hi @RPuccio 

It seems the bottleneck in your case the USB FS. MSC implementation often use smaller buffer sizes due to USB packet size limitations ( 64 bytes for FS bulk), constrained by the USB protocol's limitations, including its lower maximum throughput. Also the hardware is sensitive to signal integrity issues and requires careful design.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


RPuccio
Associate II

AScha.3 : unfortunately, I didn't design the board with an external oscillator input. I could still hack one on top of it though but soldering gets harder ! The USB in the clock configuration runs out of the internal RC 48MHz oscillator, which is surely not as precise as a dedicated crystal run through a PLL. I have tried a couple configuration and settled on a medium speed with 100R series resistors for now, but couldn't get the clock faster than 12.5MHz.

 

FBL : thanks for the reply ! That may explain why I can increase the speed within FatFS, but not when using the USB mass storage class. I'll be more careful when I build the final product, as I'm working on a prototype board right now.

 

All the best,

Rodolphe