cancel
Showing results for 
Search instead for 
Did you mean: 

How can I "remount" an SD-card?

David Martins
Senior

Hello.

Short Story:

I'm doing a little project that has to write some sensor data to an SD-Card.

After setting up with CubeMX, I found this (well-known) example of "kiwih" (https://github.com/kiwih/cubemx-mmc-sd-card?fbclid=IwAR1JE_SkRHDFSikRvwptlszMZUA-ZmuBhtI3UJPCThuC68mt_NsgG7M5qTE).

Everything is working perfect and I can do f_mount, f_open, etc ...

So far so good.

Problem:

To facilitate the connection of the SD-Card I used a uSD extension so I can not use the "card dettect" pin.

0690X000009XeBGQA0.jpg

Sometimes the system boots and no card is present. So when I do f_mount I check if the answer is "FR_OK".

After that I wait to introduce the uSD and try "f_mount" again.

The problem is that as long as I do not reset the MCU, the response from f_mount (already with the card inserted) is always "FR_DISK_ERR".

I have already tried restart the SPI, the FATFS, clean the FATFS object, etc ...

But all my attempts were unsuccessful.

In this case, having to reset the MCU is not a solution for me.

Does anyone have any ideas that can help me?

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
David Martins
Senior

Hello.

In my case, this pin has a pull-up (I put pull-up resistors on all pins of the SD card). I did not want to remove it from the PCBs.

It would also have the drawback that after saving and removing the card it would never recover.

After analyse the problem, I discovered the following solution.

Edit the fatfs.c file with the following

0690X000009XfsLQAS.jpg

And, of course, declare the function in fatfs.h

0690X000009XfsVQAS.jpg

My code to mount the SD card looks something like this:

0690X000009Xft9QAC.jpg

Problem solved.

I hope this can help someone.

Thank you!

View solution in original post

5 REPLIES 5

You should be able to read the pin state via GPIOC->IDR to see if a card is present, seem to recall at least one of the pins of the card should be "CD" in the SPI sense.

If I were debugging this I'd probably dig down and understand specifically which commands or functionality to the card were actually failing. At the top level FATFS the failure is far too non-specific.

The SDIO layers should have a DeInit, static structures Cube/HAL use are not cleaned post startup.

The cards themselves should have an 80 clock cycle trick to pull them out of odd states.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
David Martins
Senior

Hello Clive.

Thanks for the answer.

I do not have access to the card detect pin. The extension is always placed in the socket.

I'll analyze it better and I'll put here the place where I get the error.

>>I do not have access to the card detect pin. The extension is always placed in the socket.

You'll need to reparse what I said. The CARD has a pin typically usable for this purpose, not just the SOCKET one you're focusing on.

CD/CMD3

0690X000009Xfd1QAC.png

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
David Martins
Senior

Hello.

In my case, this pin has a pull-up (I put pull-up resistors on all pins of the SD card). I did not want to remove it from the PCBs.

It would also have the drawback that after saving and removing the card it would never recover.

After analyse the problem, I discovered the following solution.

Edit the fatfs.c file with the following

0690X000009XfsLQAS.jpg

And, of course, declare the function in fatfs.h

0690X000009XfsVQAS.jpg

My code to mount the SD card looks something like this:

0690X000009Xft9QAC.jpg

Problem solved.

I hope this can help someone.

Thank you!

Clive do you have sample source code to share on how to do the 80 clock cycle trick