• Join
  • Sign In with my.TI Login
Texas Instruments
  • Products
  • Applications
  • Tools & Software
  • Support & Community
  • Sample & Buy
  • About TI
Sample & Purchase Cart Sample & Purchase Cart
  • Search
  • Advanced
TI E2E™ Community
  • Support Forums
  • Blogs
  • Groups
  • Videos
  • 简体中文
  • More ...
TI Home » TI E2E Community » Support Forums » Microcontrollers » Stellaris® ARM® Microcontrollers » Stellaris® ARM® LM3S Microcontrollers Forum » moving fatfs operations outside of main.c
Share
Stellaris® ARM® Microcontrollers
  • Forum
Options
  • Subscribe via RSS
Helpful Stellaris® LM4F Series Links
  • LM4F Series
  • Stellaris PinMux Utility
  • Stellaris® LM4F120 LaunchPad
  • LM4F MCU Applications
  • LM4F MCU Video
  • ARM Cortex-M4F Whitepaper
  • Stellaris MCU Brochure
  • LM4F232 Eval Kit
  • Forums

    moving fatfs operations outside of main.c

    This question is answered
    jim schwendeman
    Posted by jim schwendeman
    on Aug 11 2012 22:35 PM
    Prodigy120 points

    Howdy forum,

    I currently have a working test program that will mount a drive, create a folder, create a numbered file and write in it.  Everything is included in the main.c file, and my goal is to move the file creation operations to my FATFS.c file.  When I do this, however, I'm still able to successfully mount the drive, create an enumerated file, but reading and syncing fail.  Any thoughts on what might be causing this?

    So.  This one works dandy:

    //Stuff for FATFS
    #include <string.h>
    #include "driverlib/interrupt.h"
    #include "driverlib/systick.h"
    #include "fatfs/src/ff.h"
    #include "fatfs/src/diskio.h"
    #include "utils/ustdlib.h"
    #include "BR_FATFS.h"
    // The following are data structures used by FatFs.
    FRESULT fresult;
    FATFS g_sFatFs;
    FIL fsrc;
    WORD written;
    //*****************************************************************************
    // The error routine that is called if the driver library encounters an error.
    // If Driverlib has been built with the symbol 'DEBUG' defined then this is 
    // needed.
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, unsigned long ulLine)
    {
    }
    #endif
    void main(void)
     {      
      //16MHz external XTAL and PLL @ 200MHz to get to 50-80 MHz. External XTAL problem, use internal
    //  SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
      SysCtlClockSet(SYSCTL_SYSDIV_25 | SYSCTL_USE_PLL | SYSCTL_OSC_INT | SYSCTL_MAIN_OSC_DIS);
     
      /*** Added Section to test writing to SD card ***/ 
      debug_printf("\nSD EXAMPLE STARTING!\n");
      // Initialize FATFS
      BR_FATFS_INIT();
      fresult = f_mount(0, &g_sFatFs);
      debug_printf("f_mount error: %s\n", StringFromFresult(fresult));
      
      //Check for/create test folder
      fresult = f_mkdir ("test");
      debug_printf("f_mkdir error: %s\n", StringFromFresult(fresult));
      //Attempt to create initial filename.  If exists, iterate through until new name found
      fresult = f_open(&fsrc, "test/test0.txt", FA_CREATE_NEW | FA_WRITE);
      debug_printf("fresult: %s\n", StringFromFresult(fresult));
      
      unsigned int i = 1;
      static char s[20];
      
      while(fresult != FR_OK)
      {   
    //      debug_printf("START----------------------\n");
          usprintf(&s,"test/test%d.txt",i);
          debug_printf("s= '%s'\n", s);
          
          unsigned int uIdx = 0;  
          uIdx = strlen(s);
    //      debug_printf("strnlen(s)= %d\n", uIdx);
          
          fresult = f_open(&fsrc, s, FA_CREATE_NEW | FA_WRITE);
          debug_printf("f_open status: %s\n", StringFromFresult(fresult));
          i++;
      }
      unsigned char buffer[64] = "\0";
    //  usprintf(&buffer,"Data1 %d,Data2 %d,Data3 %d,Data4 %d\r",123,234,345,567);
      usprintf(&buffer,"Sample, Altitude (cm), ");
      fresult = f_write(&fsrc, buffer, sizeof(buffer), &written); 
      debug_printf("f_write: %s\n", StringFromFresult(fresult));
      fresult = f_sync(&fsrc);
      debug_printf("f_sync: %s\n", StringFromFresult(fresult));
      while(1){}
    }

    But when I try this:

    //Stuff for FATFS
    #include <string.h>
    #include "driverlib/interrupt.h"
    #include "driverlib/systick.h"
    #include "fatfs/src/ff.h"
    #include "fatfs/src/diskio.h"
    #include "utils/ustdlib.h"
    #include "BR_FATFS.h"
    // The following are data structures used by FatFs.
    FRESULT fresult;
    FATFS g_sFatFs;
    FIL fsrc;
    WORD written;
    //*****************************************************************************
    // The error routine that is called if the driver library encounters an error.
    // If Driverlib has been built with the symbol 'DEBUG' defined then this is 
    // needed.
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, unsigned long ulLine)
    {
    }
    #endif
    void main(void)
     {      
      //16MHz external XTAL and PLL @ 200MHz to get to 50-80 MHz. External XTAL problem, use internal
    //  SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
      SysCtlClockSet(SYSCTL_SYSDIV_25 | SYSCTL_USE_PLL | SYSCTL_OSC_INT | SYSCTL_MAIN_OSC_DIS);
     
      /*** Added Section to test writing to SD card ***/ 
      debug_printf("\nSD EXAMPLE STARTING!\n");
      // Initialize FATFS
      BR_FATFS_INIT();
    BR_Initialize_File(fresult, fsrc, g_sFatFs);
      unsigned char buffer[64] = "\0";
    //  usprintf(&buffer,"Data1 %d,Data2 %d,Data3 %d,Data4 %d\r",123,234,345,567);
      usprintf(&buffer,"Sample, Altitude (cm), ");
      fresult = f_write(&fsrc, buffer, sizeof(buffer), &written); 
      debug_printf("f_write: %s\n", StringFromFresult(fresult));
      fresult = f_sync(&fsrc);
      debug_printf("f_sync: %s\n", StringFromFresult(fresult));
      while(1){}
    }
    With this sub in my FATFS.c file:

    void BR_Initialize_File(FRESULT fresult, FIL fsrc, FATFS g_sFatFs)
    {
    fresult = f_mount(0, &g_sFatFs);
    debug_printf("f_mount error: %s\n", StringFromFresult(fresult));

    //Check for/create test folder
    fresult = f_mkdir ("test");
    debug_printf("f_mkdir error: %s\n", StringFromFresult(fresult));

    //Attempt to create initial filename. If exists, iterate through until new name found
    fresult = f_open(&fsrc, "test/test0.txt", FA_CREATE_NEW | FA_WRITE);
    debug_printf("f_open: %s\n", StringFromFresult(fresult));

    unsigned int i = 1;
    static char s[20];

    while(fresult != FR_OK)
    {
    // debug_printf("START----------------------\n");
    usprintf(&s,"test/test%d.txt",i);
    debug_printf("s= '%s'\n", s);

    unsigned int uIdx = 0;
    uIdx = strlen(s);
    // debug_printf("strnlen(s)= %d\n", uIdx);

    fresult = f_open(&fsrc, s, FA_CREATE_NEW | FA_WRITE);
    debug_printf("f_open: %s\n", StringFromFresult(fresult));
    i++;
    }
    }

    It appears to mount correctly, indicate that the folder exists (FR_EXIST), enumerates to the current file name and creates it, but then throws FR_INVALID_OBJECT for the f_write and f_sync operations.  Any thoughts?

    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    All Replies
    • Norman Wong
      Posted by Norman Wong
      on Aug 11 2012 23:36 PM
      Verified Answer
      Verified by jim schwendeman
      Guru14880 points

      I believe you are passing, by value, the parameteres fresult, fsrc, g_sFatFs into BR_Initialize_File() BR_Initialize_File() will make changes to it's own copies. main() will not get the changed values back. It looks like you are using C++. Switch to passing by reference by adding the '&' character in function and the prototype in the .h file.

      void BR_Initialize_File(FRESULT &fresult, FIL &fsrc, FATFS &g_sFatFs)
      {
        ...
      }

      In C, you would have to pass in a pointers. Suggest returning the result:

      FRESULT BR_Initialize_File(FIL *pfsrc, FATFS *psFatFs)
      {
        FRESULT fresult;

        fresult = f_mount(0, psFatFs);
        debug_printf("f_mount error: %s\n", StringFromFresult(fresult));

        //Check for/create test folder
        fresult = f_mkdir ("test");
        debug_printf("f_mkdir error: %s\n", StringFromFresult(fresult));

        //Attempt to create initial filename. If exists, iterate through until new name found
        fresult = f_open(pfsrc, "test/test0.txt", FA_CREATE_NEW | FA_WRITE);
        debug_printf("f_open: %s\n", StringFromFresult(fresult));

        unsigned int i = 1;
        static char s[20];

        while(fresult != FR_OK)
        {
          // debug_printf("START----------------------\n");
          usprintf(&s,"test/test%d.txt",i);
          debug_printf("s= '%s'\n", s);

          unsigned int uIdx = 0;
          uIdx = strlen(s);
          // debug_printf("strnlen(s)= %d\n", uIdx);

          fresult = f_open(pfsrc, s, FA_CREATE_NEW | FA_WRITE);
          debug_printf("f_open: %s\n", StringFromFresult(fresult));
          i++;
        }

        return(fresult);
      }

      and called in main() like so:

      fresult = BR_Initialize_File(&fsrc, &g_sFatFs);

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    TI E2E™ Community
    • Support Forums
    • Blogs
    • Videos
    • Groups
    • Site Support & Feedback
    • Settings
    TI E2E™ Community Groups
    • TI University Program
    • Make the Switch
    • Microcontroller Projects
    • Motor Drive & Control
    Other Communities
    • Deyisupport
    • Designsomething.org
    • beagleboard.org
    • TI on Element 14
    • TI on TechXchangeSM
    Other Technical & Support Resources
    • WEBENCH® Design Center
    • Product Information Centers
    • Technical Documents
    • TI Design Network
    • TI Technical Articles
    • TI Training

    All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.

    Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms of Use of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms of Use of this site. TI, its suppliers and providers of content reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

    Follow Us Texas Instruments on Facebook Texas Instruments on Twitter Texas Instruments on LinkedIn Texas Instruments on Google+
    TI Worldwide | Contact Us | my.TI Login | Site Map | Corporate Citizenship | mobile m.ti.com (Mobile Version)

    TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and
    embedded processors, along with software, tools and the industry’s largest sales/support staff.

    © Copyright 1995-2013 Texas Instruments Incorporated. All rights reserved.
    Trademarks | Privacy Policy | Terms of Use