4 Replies Latest reply on Jan 25, 2011 1:15 PM by ronan.lyons

    Problems with SD card

    mario.conde
              I have problems with the example "Hello Word" wich comes with the example projects. I have an ICE so I can debug the project.   
         
      If I build the original code, the SD card loos its format. So I create a new one from the beginning, but this new cant write (create) the archive. Appears that the fp = SDCard_fopen(helloFile, "a") and the SDCard_fputcs("HelloWorld", fp) are not working.   
         
      My C code is the next   
         
      //----------------------------------------------------------------------------   
      // C main line   
      //----------------------------------------------------------------------------   
         
      //----------------------------------------------------------------------------   
      // Hello World Sample Project   
      //   
      // Description:   
      // This is a simple example of what it take to write a string to a SD Card File.   
      //   
      // Program Flow:   
      // 1) Wait for card to be inserted.   
      // 2) Initializes card interface.   
      // 3) Opens file "hello.txt" for writing. If not there it creates the file.   
      // 4) Writes the string "Hello World" with a CR and LF.   
      // 5) Closes the file. Then goes back to step '1' and waits for card to   
      // be inserted.   
         
      #include <m8c.h> // part specific constants and macros   
      #include "PSoCAPI.h" // PSoC API definitions for all User Modules   
         
      #define CARD_PRESENT 1   
      #define CARD_NOT_PRESENT 0   
         
      char helloFile[] = "hello.txt";   
         
      void main()   
      {   
      char cardInfo; // Card information   
      char fp; // File Pointer   
      BYTE CardState, oldCardState; // Card inserted state variables   
         
      BusyLED_Off(); // Make sure LED is Off   
      oldCardState = CARD_NOT_PRESENT; // Initialize card insertion state   
      SDCard_Start( ); // initalize hardware and SDCard_lib buffers   
         
      while(1)   
      {   
      CardState = SDCard_Present(); // Get current card insertion state   
      if(CardState != oldCardState) // Check for a change   
      {   
      oldCardState = CardState; // Save last state   
      if(CardState == CARD_PRESENT) // Card inserted   
      {   
      BusyLED_On(); // About to talk to card, turn on busy LED   
      SDCard_Select(SDCard_ENABLE); // Select card   
      cardInfo = 0;   
      while ( ! cardInfo ) // Wait for card to communicate   
      {   
      // initialize card, determine card type and file system type   
      cardInfo = SDCard_InitCard();   
      }   
         
      fp = SDCard_fopen(helloFile, "a"); // Open file to append data   
      SDCard_fputcs("HelloWorld", fp); // Write string   
      SDCard_fclose(fp); // Close file   
      SDCard_Select(SDCard_DISABLE); // Deselect card   
      BusyLED_Off(); // Turn off busy LED   
      }   
      }   
      }   
      }