Advertisement
Jad85

SD card code

Sep 26th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. /* ========================================
  2.  *
  3.  * The following firmware was developed by Cypress Semiconductor
  4.  * This work is licensed under a Creative Commons Attribution 3.0 Unported License.
  5.  *
  6.  * http://creativecommons.org/licenses/by/3.0/deed.en_US
  7.  *
  8.  * You are free to:
  9.  * -To Share — to copy, distribute and transmit the work
  10.  * -To Remix — to adapt the work
  11.  * -To make commercial use of the work
  12.  *
  13.  * ========================================
  14.  */
  15.  
  16. #include <device.h>
  17.  
  18. uint32 i;
  19. char * fnam;
  20. uint32 siz;
  21. uint8 no,fcnt,fre,j,bufnn;
  22.  
  23. // The main
  24. void main()
  25. {
  26.     char   cardInfo;                            // Card information  
  27.     char   fp;                                  // File Pointer  
  28.     uint8  cnt;                                 // Card inserted state variables  
  29.     uint8  load = 2;                            // Card initialization variable
  30.     char   data_r;                              // Data read from Card
  31.    
  32.     CyGlobalIntEnable;                          // Enable all global interrupts
  33.    
  34.     // Setup and initialize the SD Card
  35.     do
  36.     {
  37.         SDCard_Start();                         // Initialize SPI Bus for communication
  38.         SDCard_Select(SDCard_DISABLE);          // Select card
  39.            
  40.         cardInfo = 0;  
  41.         cnt = 0;
  42.         while ( ! cardInfo )                    // Wait for card to communicate  
  43.         {  
  44.            cnt++;
  45.            if(cnt == 0xFF) {cnt = 0;}
  46.            cardInfo = SDCard_InitCard();
  47.         }  
  48.            
  49.         load = load - 1;
  50.     }while(load > 0);                           // Stay in the loop till the card is initialized.
  51.    
  52.     fcnt = SDCard_GetFileCount();               // Get number of files on Card
  53.    
  54.     // Loop through all the existing files
  55.     // in the SD Card, one by one
  56.     // or skip is count = 0
  57.     for (cnt = 0; cnt < fcnt; cnt++)
  58.     {
  59.         fnam = SDCard_GetFilename(cnt);         // Get the filename of the file with index value in "cnt"
  60.     }
  61.    
  62.     // *****************************************
  63.     // 1. Write File
  64.     // *****************************************
  65.     fp = SDCard_fopen("file1.txt", "w");        // Open a file with write mode access. If the file is not present, a new file is created.
  66.     SDCard_fputcs("I appended this text", fp);  // Write a constant string into the file
  67.     SDCard_fclose(fp);                          // Close file
  68.     fp = SDCard_fopen("file_tmp.txt", "w");     // Open a file with write mode access. If the file is not present, a new file is created.
  69.     SDCard_fputcs("This is a temp file.", fp);  // Write a constant string into the file
  70.     SDCard_fclose(fp);                          // Close file
  71.    
  72.     // *****************************************
  73.     // 2. Append File
  74.     // *****************************************
  75.     fp = SDCard_fopen("file2.txt", "w");        // Open a file with write mode access. If the file is not present, a new file is created.
  76.     SDCard_fputcs("This is my 2nd file.", fp);  // Write a constant string into the file
  77.     SDCard_fclose(fp);                          // Close file
  78.     fp = SDCard_fopen("file2.txt", "a");        // Open the file with append mode access.
  79.     SDCard_fputcs("I appended this text.", fp); // Write a constant string into the file
  80.     SDCard_fclose(fp);                          // Close file
  81.    
  82.     // *****************************************
  83.     // 3. Read Read
  84.     // *****************************************
  85.     fp = SDCard_fopen("file2.txt", "r");        // Open a file with readonly access.
  86.     siz  = SDCard_GetFileSize(fp);              // Get the file size.
  87.     SDCard_fseek(fp, 0);                        // Move to the offset-0, which is the beginning of file
  88.     while(!SDCard_feof(fp))                     // Loop till end of file
  89.     {
  90.         data_r = SDCard_fgetc(fp);              // Read the character pointed by fp
  91.     }
  92.     SDCard_fclose(fp);                          // Close file
  93.    
  94.     // *****************************************
  95.     // 4. Rename File
  96.     // *****************************************
  97.     SDCard_Rename("file1.txt", "file_ren.txt"); // Rename file1.txt to file_ren.txt
  98.    
  99.     // *****************************************
  100.     // 5. Copy File
  101.     // *****************************************
  102.     SDCard_Copy("file_tmp.txt", "file_cpy.txt");// Copy the contents of file_tmp.txt to file_cpy.txt
  103.    
  104.     // *****************************************
  105.     // 6. Delete File
  106.     // *****************************************
  107.     SDCard_Remove("file_tmp.txt");              // Delete file_tmp.txt
  108.  
  109.     SDCard_Select(SDCard_DISABLE);              // Deselect card  
  110.     SDCard_Stop();                              // Stop the communication interface
  111.    
  112.     while(1);                                   // The End
  113. }
  114.  
  115. /* [] END OF FILE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement