cancel
Showing results for 
Search instead for 
Did you mean: 

SPI configuration problem on STM32F4 Discovery

cagibomber
Associate II
Posted on August 07, 2012 at 18:55

Hi everyone,

I am trying to send some bytes of data on a sd-card (8GB) using SPI.

I started with wiring the sd slot and configuring the spi but I have nothing, not even the clock...

I have followed what's explained in the file spi.h and seen other post about spi but I still don't see where I am wrong.

I'd really appreciate if you can help me with it ! Thank you.

#spi-sd-card-stm32-discovery
33 REPLIES 33
cagibomber
Associate II
cagibomber
Associate II
Posted on August 07, 2012 at 18:57

#include ''stm32f4_discovery.h''

void SD_GPIO_Config(void);

void SPI_Config(void);

uint16_t data = 0xFF;

int main(void)

SD_GPIO_Config();

SPI_Config();

GPIO_ResetBits(GPIOD, GPIO_Pin_0);

//SPI2->DR = data; // = sent...

while(1){

while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI2, data);

}

}

void SD_GPIO_Config(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

  

RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  

  // Connect SPI pins to AF5

GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_SPI2); // clk

GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_SPI2); // mosi

GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_SPI2); // miso

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

  // Configure CS pin in output pushpull mode

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // CS

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

}

void SPI_Config(void)

{

SPI_InitTypeDef  SPI_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

/* Deselect the FLASH: Chip Select high */

GPIO_SetBits(GPIOD, GPIO_Pin_0);

  /* SPI configuration */

  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // Or 16b

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI2, &SPI_InitStructure);

  SPI_Cmd(SPI2, ENABLE);

}

Posted on August 07, 2012 at 19:04

RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

Pick the correct APB

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cagibomber
Associate II
Posted on August 08, 2012 at 13:55

Thanks Clive1 !

I just changed it but still nothing happens.

Posted on August 08, 2012 at 15:53

I just changed it but still nothing happens.

Are you sure the SPI2 exits via the PD1, PD2 and PD3 pins? Doesn't look to be a valid routing.

PI1, PI2 and PI3 look better candidates, or some PBx or PCx pins.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cagibomber
Associate II
Posted on August 08, 2012 at 17:32

Thanks for your help,

I don't have GPIO port I on stm32f4 discovery...

But I manage to make my SD-Card in SDIO mode.

I can send data to it and read it back. 

Now I would like to write my data in a way I could read it from my computer to put it in a excel file.

I think I just can create a .txt file, right ?

I downloaded your code about FATFs. From what I've understand so far, I need it to convert my data in Fat format in a file and then load it on the sd-card.

Is that the idea ?

Thank you.

Posted on August 08, 2012 at 18:37

Or perhaps a .CSV file (comma separated entries in a text file)

You'd write the data into a file in much the same way as you'd do it with STDIO file functions.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cagibomber
Associate II
Posted on August 08, 2012 at 19:09

Ok, CSV file sound good !

So far I didn't use a DMA since I just store the data every time I need to.

Do I need to use a DMA if I want to store my data in a csv file ?

Do I need to create the .csv file in my STM32 memory and then load it on the card or can I create it in the card and then write the data in the .csv fie ?

Thanks ! :)

I'm a bit lost in all the readings...

Thank you very much for your help !

Posted on August 08, 2012 at 19:15

Not sure how DMA plays into your particular circumstances.

I'd write the CSV file a line at a time, in much the same way I did with the DIR.TXT file I was creating in the FATFs example.

It would be more efficient to buffer up larger blocks of data, but that might just over complicate things for you during initial development.

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