element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
STM32F4DISCOVERY Expansion Boards
  • Products
  • Dev Tools
  • STM32F4DISCOVERY Expansion Boards
  • More
  • Cancel
STM32F4DISCOVERY Expansion Boards
Forum SD CARD READ ERROR
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join STM32F4DISCOVERY Expansion Boards to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 12 replies
  • Subscribers 7 subscribers
  • Views 1800 views
  • Users 0 members are here
Related

SD CARD READ ERROR

philip-1992
philip-1992 over 12 years ago

So I tweeked a bit with an example regarding SD card (2GB). Mainly I wish to load a message from a text file which is stored inside an SD card to be displayed on the touch screen in which later on hoepfully I would be able to use the same logic of this program to allow me to laod a bitmap image from SD CARD. However when I am running the program, the file is not read and it is not creating a new file either! is there something wrong with the logic of this program? As you can see I made a function that whenever there is a read or write error it goes to the fault_err() function in which the program terminates.

 

Any help regarding this issue would be greatly appreciated.

 

Thanks in advance below is my source code

 

/*Private Defines ------------------------------------------------------------*/

#define  LCD_BASE_Data               ((u32)(0x60000000|0x00100000))

#define  LCD_Data                    (*(vu16 *)LCD_BASE_Data)

 

 

/* Private variables ---------------------------------------------------------*/

SD_Error Status = SD_OK;

 

 

FATFS filesystem;              /* volume lable */

 

 

FRESULT ret;                        /* Result code */

 

 

FIL file;                        /* File object */

 

 

DIR dir;                        /* Directory object */

 

 

FILINFO fno;                        /* File information object */

 

 

UINT bw, br;

 

 

uint8_t buff[128];

 

 

/* Private function prototypes -----------------------------------------------*/

static void Delay(__IO uint32_t nCount);

static void fault_err (FRESULT rc);

 

 

void delay(__IO uint32_t nCount);

 

 

/*Main Function --------------------------------------------------------------*/

int main(void)

{

  uint8_t text [100];

 

  volatile uint32_t dlycnt;

  /* wait the power stable */

  for (dlycnt = 0; dlycnt < 10000000; dlycnt++);

 

  /* Initializes LCD */

  STM32f4_Discovery_LCD_Init();

         

  /* Clear the LCD */

  LCD_Clear(LCD_COLOR_WHITE);

 

  /* Set the LCD Text size */

  LCD_SetFont(&Font16x24);

 

   /* Clear the LCD */

  LCD_Clear(LCD_COLOR_BLACK);

 

  /* Set the LCD Back Color */

  LCD_SetBackColor(LCD_COLOR_BLACK);

 

  /* Set The LCD Text colour */

  LCD_SetTextColor(LCD_COLOR_WHITE);

 

  STM32f4_Discovery_Debug_Init();

 

  LCD_DisplayStringLine(LINE(2), "  Welcome");

  Delay(200);

  LCD_Clear(LCD_COLOR_BLACK);

  LCD_SetFont(&Font8x12);

 

 

  /* Initialize Debug uart available on Devkit407 board *******************/

  STM32f4_Discovery_Debug_Init();

  

  /* mount the filesystem */

  if (f_mount(0, &filesystem) != FR_OK) {

      LCD_DisplayStringLine (Line2, "  Could not open filesystem");

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

  }

  Delay(10);

 

 

    LCD_DisplayStringLine (Line2, "  Open a test file");

    Delay(200);

    LCD_Clear(LCD_COLOR_BLACK);

  ret = f_open(&file, "message", FA_READ);

  if (ret) {

      LCD_DisplayStringLine (Line2, "  Not exist the test file");

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

  } else {

      LCD_DisplayStringLine (Line2, "  Type the file content");

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

    for (;;) {

      ret = f_read(&file, buff, sizeof(buff), &br);          /* Read a chunk of file */

      if (ret || !br) {

        break;                              /* Error or end of file */

      }

      buff[br] = 0;

     

      sprintf((char*)text,"  %s",buff);//Display the Message

      LCD_DisplayStringLine(LINE(3), text);

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

    }

    if (ret) {

      LCD_DisplayStringLine (Line2, "  Read the file error");

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

      fault_err(ret);

    }

 

 

    LCD_DisplayStringLine (Line2,  "  Close the File");

    ret = f_close(&file);

    if (ret) {

      LCD_DisplayStringLine (Line2, "  Close the file error");

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

    }

  }

 

 

  /*  hello.txt write test*/

  Delay(50);

  LCD_DisplayStringLine (Line2, "  Create a new file");

  Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

  ret = f_open(&file, "HELLO.TXT", FA_WRITE | FA_CREATE_ALWAYS);

  if (ret)

  {

    LCD_DisplayStringLine (Line2, "  Create a new file error");

    fault_err(ret);

  }

  else

  {  

    LCD_DisplayStringLine (Line2, "  Write a text data. (hello.txt)");

    Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

    ret = f_write(&file, "Hello world!", 14, &bw);

    if (ret)

    {

    LCD_DisplayStringLine (Line2, "  Write a text data to file error");

    Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

    }

    else

    {

      sprintf((char*)text,"  %u bytes written", bw);

      LCD_DisplayStringLine (Line2, text);

    }

    Delay(50);

    LCD_DisplayStringLine (Line2, "  Close the file");

    Delay(200);

    LCD_Clear(LCD_COLOR_BLACK);

    ret = f_close(&file);

    if (ret)

    {

      LCD_DisplayStringLine (Line2, "  Create a new file error");

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

    }

  }

 

 

  /*  hello.txt read test*/

  Delay(50);

  LCD_DisplayStringLine (Line2, "  Read the file");

  Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

  ret = f_open(&file, "HELLO.TXT", FA_READ);

  if (ret)

  {

    LCD_DisplayStringLine (Line2, "  Open file error");

    Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

  }

  else

  {

    LCD_DisplayStringLine (Line2, "  Type the file content");

    Delay(200);

    LCD_Clear(LCD_COLOR_BLACK);

    for (;;)

    {

      ret = f_read(&file, buff, sizeof(buff), &br);          /* Read a chunk of file */

      if (ret || !br)

      {

        break;                              /* Error or end of file */

      }

      buff[br] = 0;

     

      sprintf((char*)text,"%s",buff);

      LCD_DisplayStringLine(LINE(3), text);

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

    }

    if (ret)

    {

      LCD_DisplayStringLine (Line2, "  Read File Error");

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

      fault_err(ret);

    }

 

 

    LCD_DisplayStringLine (Line2, "  Close the File");

    Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

    ret = f_close(&file);

    if (ret)

    {

      LCD_DisplayStringLine (Line2, "  Close file error");

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

    }

  }

 

 

  /* directory display test*/

  Delay(50);

  LCD_DisplayStringLine (Line2, "Open Root Directory");

  ret = f_opendir(&dir, "");

  if (ret)

  {

    LCD_DisplayStringLine (Line2, "Open root directory error");

  } else

  {

    LCD_DisplayStringLine (Line2, "Directory listing...");

    for (;;)

    {

      ret = f_readdir(&dir, &fno);                    /* Read a directory item */

      if (ret || !fno.fname[0])

      {

        break;          /* Error or end of dir */

      }

      if (fno.fattrib & AM_DIR)

      {

        sprintf((char*)text,"  <dir>  %s\n\r", fno.fname);

        LCD_DisplayStringLine(LINE(3), text);

      }

      else

      {

        sprintf((char*)text,"%8lu  %s\n\r", fno.fsize, fno.fname);

        LCD_DisplayStringLine(LINE(3), text);

      }

    }

    if (ret)

    {

      LCD_DisplayStringLine (Line2, "Read a directory error");

      fault_err(ret);

    }

  }

  Delay(50);

  LCD_DisplayStringLine (Line2, "Test completed\n\r");

 

 

  /* Infinite loop */

  while (1)

  {

   // STM_EVAL_LEDToggle(LED3);

    Delay(100);

  }

}

 

 

/**

* @brief   FatFs err dispose

* @param  None

* @retval None

*/

static void fault_err (FRESULT rc)

{

  uint8_t text [100];

  const char *str =

                    "OK\0" "DISK_ERR\0" "INT_ERR\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0"

                    "INVALID_NAME\0" "DENIED\0" "EXIST\0" "INVALID_OBJECT\0" "WRITE_PROTECTED\0"

                    "INVALID_DRIVE\0" "NOT_ENABLED\0" "NO_FILE_SYSTEM\0" "MKFS_ABORTED\0" "TIMEOUT\0"

                    "LOCKED\0" "NOT_ENOUGH_CORE\0" "TOO_MANY_OPEN_FILES\0";

  FRESULT i;

 

 

  for (i = (FRESULT)0; i != rc && *str; i++) {

    while (*str++) ;

  }

  sprintf((char*)text,"  rc=%u FR_%s", (UINT)rc, str);

  LCD_DisplayStringLine (Line2, text);

  Delay(200);

  LCD_Clear(LCD_COLOR_BLACK);

  //STM_EVAL_LEDOn(LED6);

  while(1);

}

 

 

/**

  * @brief  Delay

  * @param  None

  * @retval None

  */

static void Delay(__IO uint32_t nCount)

{

  __IO uint32_t index = 0;

  for (index = (100000 * nCount); index != 0; index--);

}

 

 

#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\r\n", file, line) */

 

 

  /* Infinite loop */

  while (1)

  {}

}

#endif

  • Sign in to reply
  • Cancel
  • Kilohercas
    0 Kilohercas over 12 years ago

    Did you already tryed SD card with FATFS and it worked for you, since i know that is bug inside program, so you need to replace DISCOVERY_SDIO.c and .h files

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • philip-1992
    0 philip-1992 over 12 years ago in reply to Kilohercas

    Yes but the printf commands do not work. Nothing is displayed either on the monitor nor on the LCD. However when I entered an empty SD CARD, a new file with the text HELLO WORLD is created, therefore the example is working

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Kilohercas
    0 Kilohercas over 12 years ago in reply to philip-1992

    It should be fprintf

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • philip-1992
    0 philip-1992 over 12 years ago in reply to Kilohercas

    tried that before but it gives the following error: Error[Pe167]: argument of type "char *" is incompatible with parameter of type "struct __FILE *"

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • philip-1992
    0 philip-1992 over 12 years ago in reply to philip-1992

    If it helps the last thing that is shown on the screen when the program goes to the fault_err() function is : "rc = 1  FR_DISK_ERRe error"

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • philip-1992
    0 philip-1992 over 12 years ago

    I have been doing some more debugging on this issues, and after the following commands;

     

      LCD_DisplayStringLine (Line2, "  Open a test file");

      Delay(200);

      LCD_Clear(LCD_COLOR_BLACK);

      ret = f_open(&file, "message.txt", FA_READ);

    I added a watch on the variable ret which is resulting FR_DISK_ERR. is this error because incompatiblity issues with SD card? or because the SD is not initializing correctly? The sd card I am using is 2GB pq1

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Kilohercas
    0 Kilohercas over 12 years ago in reply to philip-1992

    is it HC ? (SDHC)
    Better try below 1GB, like 512MB or 128MB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • philip-1992
    0 philip-1992 over 12 years ago in reply to Kilohercas

    Well it doesn't state that its SDHC. I will try to find a smaller SD card. Does the dev board has a limit to the size of SD card? Do you think that this is part of the problem? Because when I tried the example from ST, a text file was being created on the SD

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • zavovi
    0 zavovi over 11 years ago in reply to philip-1992

    Hi, I have the same problem. I'm using SDIO library with STM32F4 discovery kit and I programming webserver with SD card. If I use a demo project from element14 for read and write to SD card, it's ok. But if I use the same code in my project, it's problem.

     

    In line :   ret = f_open(&file, "message.txt", FA_READ); is error. Debugger is stopped here and nothing do. If I debog lower function, error is in checking SD card.

     

    Why in demo project is all ok but in my (the same functions) is error?

     

    Can you help me please?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • philip-1992
    0 philip-1992 over 11 years ago in reply to zavovi

    Hi there,

    Your problem is very similar to mine. i did spend about 3 weeks to understand what was happening and apparantly there is some instances at which certain libraries work. Thus in order for your program to work, you need to save your project exactly at the same file where the examle is so that your program uses the same libraries just as the example. I gave you the solution that has worked for me. hope I was helpful... goodluck

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube