cancel
Showing results for 
Search instead for 
Did you mean: 

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

Pablo Leibovich
Associate II
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
1 ACCEPTED SOLUTION

Accepted Solutions
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

View solution in original post

11 REPLIES 11
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
Up vote any posts that you find helpful, it shows what's working..
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!

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
Up vote any posts that you find helpful, it shows what's working..
Posted on May 22, 2017 at 22:13

I will try and let you know!

Thanks!

Posted on May 23, 2017 at 01:07

Hi,

I changed the stack, but it still doesn't work. I post in the link my project with the source code.

https://drive.google.com/open?id=0B3Bi8jcrgm3qNmptbTFRcS1vTGs

 

SD is connected to SPI1.

What I'm doing wrong? : (

Thanks!

Posted on May 23, 2017 at 10:48

What I'm doing wrong? : (

I'm no Cube user either.

You can evaluate the SCB registers for causes once you are in the fault handler, or trying to step through the code until you find the 'offensive' code.

Both requires usage of the debugger.

Zt Liu
Senior III
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.  

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

Posted on June 09, 2017 at 00:26

Thank you very much!! I was out of the office, that's why I didn't reply.

It's Working!

Thanks a lot