cancel
Showing results for 
Search instead for 
Did you mean: 

Problem of writing/reading microsd card using the code generated by stm32f4cubemx

rwmao
Senior
Posted on May 30, 2015 at 02:53

This is is very simple project, using stm32f4 discovery and wired microsd socket to read/write data to microsd card.

the code is generated by the stm32f4cubemx.

However error occurs at f_open, tracking down it shows FS_DISK_ERR in ff.c

Please help..

The code for reading/write is:

  /* Register the file system object to the FatFs module */

  if(f_mount(&SDDISKFatFs, (TCHAR const*)SD_Path, 0) != FR_OK)

  {

    /* FatFs Initialization Error */

    Error_Handler();

  }

  else

  {

LED_ON(ORANGE,1);

      /* Create and Open a new text file object with write access */

if(f_open(&MySdFile, ''STM32.TXT'', FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) 

      {

LED_ON(RED,1);

HAL_Delay(2000);

LED_ON(RED,0);HAL_Delay(1000);

        /* 'STM32.TXT' file Open for write Error */

        Error_Handler();

      }

      else

      {

LED_ON(BLUE,1);

        /* Write data to the text file */

        res = f_write(&MySdFile, wtext, sizeof(wtext), (void *)&byteswritten);

        

        if((byteswritten == 0) || (res != FR_OK))

        {

          /* 'STM32.TXT' file Write or EOF Error */

          Error_Handler();

        }

        else

        {

LED_ON(GREEN,1);

          /* Close the open text file */

          f_close(&MySdFile);

          

        /* Open the text file object with read access */

        if(f_open(&MySdFile, ''STM32.TXT'', FA_READ) != FR_OK)

        {

          /* 'STM32.TXT' file Open for read Error */

          Error_Handler();

        }

        else

        {

          /* Read data from the text file */

          res = f_read(&MySdFile, rtext, sizeof(rtext), (void *)&bytesread);

          

          if((bytesread == 0) || (res != FR_OK))

          {

            /* 'STM32.TXT' file Read or EOF Error */

            Error_Handler();

          }

          else

          {

            /* Close the open text file */

            f_close(&MySdFile);

            

            /* Compare read data with the expected data */

            if((bytesread != byteswritten))

            {                

              /* Read data is different from the expected data */

              Error_Handler();

            }

            else

            {

          /* Success of the demo: no error occurrence */

//              BSP_LED_On(LED4);

            }

          }

        }

      }

    }

  }

23 REPLIES 23
rwmao
Senior
Posted on June 02, 2015 at 05:46

The board I am using is purchased from 

http://www.ebay.com/itm/381265789938?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

0690X00000603MjQAI.jpg

And I am using this figure for pin assignment.

0690X00000603OzQAI.jpg

1. Resistor for D0-D3, and Cmd is 30k. I just pick up the close one I have.

2. The voltage is 3.0v. There is only 3v on the discovery board.

Thanks

rwmao
Senior
Posted on June 02, 2015 at 06:19

Just now, I retested it. The result is different.

It may be because of the connection problem of uSD card?

When I plugged in, it shows it can write. But after I push the reset button, it freezes.

Tomorrow I will try to use another socket board and test again.

Thanks

0690X00000605g7QAA.png

Posted on June 02, 2015 at 18:15

30K should be fine, I picked 33K as it's what I had to hand. My wire lengths were in the order of 1-1.5'' (25-40mm)

8 PC9 DAT1
7 PC8 DAT0/DO
6 GND VSS 
5 PC12 CLK/SCLK
4 3V VDD
3 PD2 CMD/DI
2 PC11 DAT3/CS
1 PC10 DAT2

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rwmao
Senior
Posted on June 02, 2015 at 22:10

Still getting the error after I resolder to another board using molex socket. I believe the connection is solid and perfect.

I usedmultimeter to checked the connection.

0690X00000605eQQAQ.png

rwmao
Senior
Posted on June 02, 2015 at 22:20

0690X00000603S2QAI.jpg

0690X00000603PEQAY.jpg

backside of the board. 5 resistors(43kohm).

0690X00000603S7QAI.jpg

rwmao
Senior
Posted on June 03, 2015 at 07:02

Hi Clivel,

can you guide me how to implement SWV (Serial Wire Viewer) using SWO (Serial Wire Output) output without loosing the function of fprintf?

I want to output debug info to SWV but I still want to access microsd reading/writing.

How can I do that?

For the microsd issue, I may design a board and figure it out later.

Deeply appreciate your advise.

AvaTar
Lead
Posted on June 03, 2015 at 11:30

I suspect the wiring (length and layout) does not support ''higher megabit'' data rates.

Try scaling down the SDIO clock by one order of magnitude

rwmao
Senior
Posted on June 03, 2015 at 19:44

Argentum,

I used pretty low clock frequency.

Not sure what is going on.

I am gonna to buy

STM32F4DIS-BB to test.

0690X00000605gaQAA.png

Posted on June 03, 2015 at 21:34

can you guide me how to implementSWV (Serial Wire Viewer) using SWO (Serial Wire Output) output without loosing the function of fprintf? I want to output debug info to SWV but I still want to access microsd reading/writing.

//****************************************************************************
// STM3221G-EVAL SWV Test - SOURCER32@GMAIL.COM
//****************************************************************************
#include <
stdio.h
>
#include <
stdlib.h
>
#include <
string.h
>
#include ''stm322xg_eval.h''
//****************************************************************************
static int Debug_ITMDebug = 0;
//****************************************************************************
void Debug_ITMDebugEnable(void)
{
volatile unsigned int *ITM_TER = (volatile unsigned int *)0xE0000E00;
volatile unsigned int *SCB_DHCSR = (volatile unsigned int *)0xE000EDF0;
volatile unsigned int *DBGMCU_CR = (volatile unsigned int *)0xE0042004;
*DBGMCU_CR |= 0x27; // DBGMCU_CR
if ((*SCB_DHCSR & 1) && (*ITM_TER & 1)) // Enabled?
Debug_ITMDebug = 1;
}
//****************************************************************************
void Debug_ITMDebugOutputChar(char ch)
{
static volatile unsigned int *ITM_STIM0 = (volatile unsigned int *)0xE0000000; // ITM Port 0
static volatile unsigned int *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC;
if (Debug_ITMDebug && (*SCB_DEMCR & 0x01000000))
{
while(*ITM_STIM0 == 0);
*((volatile char *)ITM_STIM0) = ch;
}
}
//****************************************************************************
void Debug_ITMDebugOutputString(char *Buffer)
{
if (Debug_ITMDebug)
while(*Buffer)
Debug_ITMDebugOutputChar(*Buffer++);
}
//******************************************************************************
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f2xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f2xx.c file
*/
Debug_ITMDebugEnable();
Debug_ITMDebugOutputString(''SWV Enabled
'');
printf(''%ld
'', SystemCoreClock);
printf(''Hello World!
'');
while(1); /* Infinite loop */
}
//******************************************************************************
/**
* @brief Retargets the C library printf function to the USART (GNU)
* @param None
* @retval None
*/
int __io_putchar(int ch)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
Debug_ITMDebugOutputChar(ch); // SWV
return(ch);
}
//******************************************************************************
/**
* @brief Retargets the C library scanf function to the USART (GNU)
* @param None
* @retval None
*/
int __io_getchar(void)
{
/* Place your implementation of fgetc here */
/* e.g. read a character from the USART */
return((int)-1);
}
//******************************************************************************
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
while(1); /* Infinite loop */
}
#endif
//******************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rwmao
Senior
Posted on June 04, 2015 at 07:34

Thanks.Clivel.