cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Discovery FATFS SPI2 problem

dawidos145
Associate II
Posted on October 09, 2016 at 21:54

Hi everyone, i have problem with a something project, i dont have comunication with microsd card on my board,  my debug stay on function wait ready and next rcvr_spi(), and nothing to do ;/ i dont know where is problem,

i paste my configuration. 

my connect : CS - PE15, SCK - PB13, MISO PB14, MOSI PB15, i use coocox for comiple and debug

MAIN

[code]int main( void )

{

    SystemInit();

     SPI_SD_Init();

        fresult = f_mount( 0, &fatfs);

        fresult = f_open( &plik, ''demo.wav'', FA_OPEN_EXISTING | FA_READ );

        if( fresult == FR_OK )

        {

        //fresult = f_read(&plik, void * buffor, 17, &zapisanych_bajtow);

        fresult = f_read( &plik, ( void * ) buffor2, 112940, &odczytanych_bajtow );

        }

        fresult = f_close( &plik );

}

[/code]

SPI_SD

[code]void SPI_SD_Init( void )  //inicialize spi

{

GPIO_InitTypeDef  GPIO_InitStructure;

SPI_InitTypeDef   SPI_InitStructure;

volatile BYTE dummyread;

 // Konfiguracja wyprowadzen i kontrolera SPI:

config output spi

/* Enable GPIO clock for CS */

   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

   /* Enable SPI clock, SPI2 */

 // Wlaczenie sygnalow zegarowych dla peryferiow

clock for peripherials

 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE);

 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE );

 GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2 );

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2 );

 // PE15 CS

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

 GPIO_Init(GPIOE, &GPIO_InitStructure);

 DESELECT_SD();

 //SCK, MISO and MOSI SD

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

 GPIO_Init(GPIOB, &GPIO_InitStructure);

 // Konfiguracja SPI enable spi

 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

 SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // low ?

 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);

 // Wlacz SPI

 SPI_Cmd(SPI2, ENABLE);

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

   dummyread = SPI_ReceiveData(SPI2);

}[/code]

ODEBRANIE BAJTU I WYSLANIE

[code]

static

void xmit_spi (uint8_t outB)  // Wyslanie bajtu do SPI

{

u8 Data = 0;

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

SPI_I2S_SendData(SPI2, Data);

}

static

BYTE rcvr_spi (void) // Odebranie bajtu z SPI

{

u8 Data = 0;

// Wysłanie 0xFF

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

SPI_I2S_SendData(SPI2, 0xFF);

// odebranie bajtu

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

Data = SPI_I2S_ReceiveData(SPI2);

return Data;

}

static

BYTE wait_ready (void)

{

    BYTE res;

    Timer2 = 50;    /* Wait for ready in timeout of 500ms */

    rcvr_spi();

    do

        res = rcvr_spi();

    while ((res != 0xFF) && Timer2);

    return res;

}[/code]

[code]

static

void power_on (void)

{

u8 i, cmd_arg[6];

u32 Count = 0xFFF;

 

// Inicjalizacja karty i przelaczenie w tryb SPI:

   DESELECT_SD();        // CS = 1

    for (i = 0; i < 10; i++)

           xmit_spi(0xFF);    // Wyslij 0xFF 10 razy = 80 cykli zegarowych

               // (wymaganych co najmniej 74 cykli)

    SELECT_SD();        // CS = 0

     // Przygotowanie ramki inicjujacej do wyslania

     cmd_arg[0] = (CMD0 | 0x40);

     cmd_arg[1] = 0;    // Argument komendy

     cmd_arg[2] = 0;    // nawet, gdy komenda go nie ma

     cmd_arg[3] = 0;    // musi zostac wyslany w postaci zer

     cmd_arg[4] = 0;

     cmd_arg[5] = 0x95;    // CRC = 0x95

     for (i = 0; i < 6; i++)        // Wyslanie ramki

           xmit_spi(cmd_arg[i]);

     while ((rcvr_spi() != 0x01) && Count) // Czeka na 0x01

           Count--;

     DESELECT_SD();        // CS = 1

     xmit_spi( 0XFF );    // Wyslij 0xFF

     PowerFlag = 1;

}[/code]

[/code]
5 REPLIES 5
Posted on October 10, 2016 at 01:05

You configure PE12 not PE15

There doesn't appear to be any timeout counter/interrupt implemented, so it won't leave the loop.

You might want to instrument the code to see what comes back, and review that in the context of the MMC/SD Card protocol documentation.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dawidos145
Associate II
Posted on October 18, 2016 at 18:30

thank you for answer, im beginner user, but i must do this for project.. can you help me ? im dont know where is problem, i change to PE15 but still stay on :

if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET) , and bistatus have always RESET

Posted on October 18, 2016 at 21:04

can you help me ?

 

I've provided SDIO examples for the STM32F4-DISCO board, unfortunately I can't expend my resources on debugging your code.

If the bits are always zero one might double check the peripheral clocks, but don't look at the SPI2 peripheral through a Peripheral View in the debugger, it will break/clear bits. The debugger can be very invasive at a peripheral level.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dawidos145
Associate II
Posted on October 23, 2016 at 19:45

At this moment i dont know..i do this everythin... i have on board only pull up resisotr on CS PIN 10K to 3.3 V. 

When i call function fresult = f_mount  i back FR_OK but when i call fresult = f_open i have problem on function rcvr_spi() and next still stay on function wait_ready and line '' while ((res != 0xFF) && Timer2); ''

when i call function before f_mount and f_open i have

fresult = disk_initialize()  -> FR_DISK_ERR

fresult = disk_read() -> FR_NO_FILE

and when i call only function disk_initialize(0) he still stay always ''while ((res != 0xFF) && Timer2);''

Posted on October 23, 2016 at 21:41

I would recommend fully validating the SPI/SD layer reads and writes before layering FATFS on top.

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