Skip to main content
Pablo Leibovich
Associate III
May 20, 2017
Solved

I can't connect a SD Card to my STM32F072 Discovery Board

  • May 20, 2017
  • 11 replies
  • 2204 views
Posted on May 21, 2017 at 00:24

Hi all!

I'm trying to read/write data to an SD card from my STM32F072 Discovery board.

I configure SPI1 on CubeMx and the FatFs middleware. I've written the user_dsikio.c and the Input-Output functions based on the STM32F072 Eval board, but I can't make it work.

When I try to open a file, or format the SD, the microcontroller stops there and finishes at the Default_Handler, as I can see with the debugger.

Where can I find an example of a STM32F072 Discovery board with an SD?

Can anyone help me?

Thanks!!!!!!!!!!

#sd-card #stm32f072 #stm32f072-discovery
This topic has been closed for replies.
Best answer by Zt Liu
Posted on May 26, 2017 at 05:05

Hi, there. I just download your project.

In your io_sd.c

static void SPIx_Init(void)

{

//SPI baudrate is set to 12 MHz (PCLK1/SPI_BaudRatePrescaler = 48/4 = 12 MHz)

//PCLK1 frequency is set to 48 MHz

//SD card SPI interface max baudrate is 25MHz for write/read

hal_Spi.Instance = SPI1;

hal_Spi.Init.Mode = SPI_MODE_MASTER;

hal_Spi.Init.Direction = SPI_DIRECTION_2LINES;

hal_Spi.Init.DataSize = SPI_DATASIZE_8BIT;

hal_Spi.Init.CLKPolarity = SPI_POLARITY_LOW;

hal_Spi.Init.CLKPhase = SPI_PHASE_2EDGE;

hal_Spi.Init.NSS = SPI_NSS_SOFT;

hal_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;

hal_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;

hal_Spi.Init.TIMode = SPI_TIMODE_DISABLE;

hal_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hal_Spi.Init.CRCPolynomial = 7;

hal_Spi.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

hal_Spi.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

...

Should it be SPI_POLARITY_HIGH?

You may use debug step by step to discover SPIx is really initialzed.

Hope it helps.

zt

11 replies

Tesla DeLorean
Guru
May 21, 2017
Posted on May 21, 2017 at 03:25

Do you have an adequate stack allocation? Is there an interrupt you are not handling?

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Pablo Leibovich
Associate III
May 22, 2017
Posted on May 22, 2017 at 20:55

Hi!

The stack allocation is configured to 0x230. May be it not enough?

What interrupts I have to define? I set FatFs with the default configurations.

Sorry for my questions, I'm new on this middleware.

Thanks!

Tesla DeLorean
Guru
May 22, 2017
Posted on May 22, 2017 at 21:39

That's rather small, try starting with 0x1000 (4KB). Review how deep you're into it when you hit the Default Handler.

The ones from SPI1 if it is using them. Just suggesting the most probable causes for the failure you describe.

>>

I'm new on this middleware

That's Ok, I'm not supporting it

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Zt Liu
Senior III
May 26, 2017
Posted on May 26, 2017 at 03:21

Hey Pablo,

in 

http://www.st.com/en/embedded-software/stm32cubef0.html

, there is an  example of uSD/Fatfs for STM32F072 Eval

(download the source, unzip it , located in STM32Cube_FW_F0_V1.8.0\Projects\STM32072B_EVAL\Applications\FatFs\FatFs_uSD)

----------------------------------------------------------------------------------------------------------------------

I did use spi and F0 device with Fatfs  and uSD few months ago, I basically follow the structure in the cube example.

And it works fine. However, there is a malloc instruction called in  BSP_SD_WriteBlocks and  BSP_SD_ReadBlocks

subroutines. You may change it if you don't like.

Hope that helps you too! 

Zt.