⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sd_file.c

📁 STM32F103 SD卡控制程序
💻 C
📖 第 1 页 / 共 2 页
字号:
   fnew = get_entry (next, &next);
   if (fnew == NULL) {
      printf ("\nNew Filename missing.\n");
      return;
   }
   if (strcmp (fname,fnew) == 0) {
      printf ("\nNew name is the same.\n");
      return;
   }
   if (frename (fname, fnew) == 0) {
      printf ("\nFile %s renamed to %s\n",fname,fnew);
      }
      else {
         printf ("\nFile rename error.\n");
      }
   }

/*----------------------------------------------------------------------------
 *        Copy a File
 *---------------------------------------------------------------------------*/
static void cmd_copy (char *par) {
   char *fname,*fnew,*fmer,*next;
   FILE *fin,*fout;
   U32 cnt,total;
   char buf[512];
   BOOL merge;

   fname = get_entry (par, &next);
   if (fname == NULL) {
      printf ("\nFilename missing.\n");
      return;
   }
   fmer = get_entry (next, &next);
   if (fmer == NULL) {
      printf ("\nNew Filename missing.\n");
      return;
   }
   fnew = get_entry (next, &next);
   if (fnew != NULL) {
      merge = __TRUE;
   }
   else {
      merge = __FALSE;
      fnew = fmer;
   }
   if ((strcmp (fname,fnew) == 0)        ||
       (merge && strcmp (fmer,fnew) == 0)) {
      printf ("\nNew name is the same.\n");
      return;
   }

   fin = fopen (fname,"r");           /* open the file for reading           */
   if (fin == NULL) {
      printf ("\nFile %s not found!\n",fname);
      return;
   }

   if (merge == __FALSE) {
      printf ("\nCopy file %s to %s\n",fname,fnew);
   }
   else {
      printf ("\nCopy file %s, %s to %s\n",fname,fmer,fnew);
   }
   fout = fopen (fnew,"w");           /* open the file for writing           */
   if (fout == NULL) {
      printf ("\nFailed to open %s for writing!\n",fnew);
      fclose (fin);
      return;
   }

   total = 0;
   while ((cnt = fread (&buf, 1, 512, fin)) != 0) {
      fwrite (&buf, 1, cnt, fout);
      total += cnt;
   }
   fclose (fin);                      /* close input file when done          */

   if (merge == __TRUE) {
      fin = fopen (fmer,"r");         /* open the file for reading           */
      if (fin == NULL) {
         printf ("\nFile %s not found!\n",fmer);
      }
      else {
         while ((cnt = fread (&buf, 1, 512, fin)) != 0) {
            fwrite (&buf, 1, cnt, fout);
            total += cnt;
         }
         fclose (fin);
      }
   }
   fclose (fout);
   dot_format (total, &buf[0]);
   printf ("\n%s bytes copied.\n", &buf[0]);
}

/*----------------------------------------------------------------------------
 *        Delete a File
 *---------------------------------------------------------------------------*/
static void cmd_delete (char *par) {
   char *fname,*next;

   fname = get_entry (par, &next);
   if (fname == NULL) {
      printf ("\nFilename missing.\n");
      return;
   }

   if (fdelete (fname) == 0) {
      printf ("\nFile %s deleted.\n",fname);
   }
   else {
      printf ("\nFile %s not found.\n",fname);
   }
}

/*----------------------------------------------------------------------------
 *        Print a Flash Memory Card Directory
 *---------------------------------------------------------------------------*/
static void cmd_dir (char *par) {
   U32 fsize,files;
   char temp[32],*mask,*next;
   FINFO info;

   mask = get_entry (par, &next);
   if (mask == NULL) {
      mask = "*.*";
   }

   printf ("\nFile System Directory...");
   files = 0;
   fsize = 0;
   info.fileID = 0;
   while (ffind (mask,&info) == 0) {
      dot_format (info.size, &temp[0]);
      printf ("\n%-20s %14s ", info.name, temp);
      printf ("  %02d.%02d.%04d  %02d:%02d  ID[%04d]",
               info.time.day, info.time.mon, info.time.year,
               info.time.hr, info.time.min, info.fileID);
      fsize += info.size;
      files++;
   }
   if (info.fileID == 0) {
      printf ("\nNo files...");
   }
   else {
      dot_format (fsize, &temp[0]);
      printf ("\n%9d File(s)    %14s bytes", files, temp);
   }
   dot_format (ffree(""), &temp[0]);
   printf ("\n%35s bytes free.\n",temp);
}

/*----------------------------------------------------------------------------
 *        Format a Flash Memory Card
 *---------------------------------------------------------------------------*/
static void cmd_format (char *par) {
   char *label,*next;
   U32 retv;

   label = get_entry (par, &next);
   if (label == NULL) {
      label = "KEIL";
   }
   printf ("\nFormat Flash Memory Card? [Y/N]\n");
   retv = getkey();
   if (retv == 'y' || retv == 'Y') {
      /* Format the Card with Label "KEIL". "*/
      if (fformat (label) == 0) {
         printf ("Memory Card Formatted.\n");
         printf ("Card Label is %s\n",label);
      }
      else {
         printf ("Formatting failed.\n");
      }
   }
}

/*----------------------------------------------------------------------------
 *        Display Command Syntax help
 *---------------------------------------------------------------------------*/
static void cmd_help (char *par) {
   printf (help);
}

/*----------------------------------------------------------------------------
 *        Initialize a Flash Memory Card
 *---------------------------------------------------------------------------*/
static void init_card (void) {
   U32 retv;

   while ((retv = finit ()) != 0) {           /* Wait until the Card is ready*/
      if (retv == 1) {
         printf ("\nSD/MMC Init Failed");
         printf ("\nInsert Memory card and press key...\n");
         getkey ();
      }
      else {
         printf ("\nSD/MMC Card is Unformatted");
         strcpy (&in_line[0], "KEIL\r\n");
         cmd_format (&in_line[0]);
      }
   }
}

/*----------------------------------------------------------------------------
 *        Main: 
 *---------------------------------------------------------------------------*/
int main (void) {
   char *sp,*next;
   U32 i;

   stm32_Init();                              /* initialize the STM32        */

   init_display ();
   printf (intro);                            /* display example info        */
   printf (help);

   init_card ();
   while (1) {
      printf ("\nCmd> ");                     /* display prompt              */
      fflush (stdout);
                                              /* get command line input      */
      if (getline (in_line, sizeof (in_line)) == __FALSE) {
         continue;
      }
      sp = get_entry (&in_line[0], &next);
      if (*sp == 0) {
         continue;
      }

      for (i = 0; i < CMD_COUNT; i++) {
         if (strcmp (sp, (const S8 *)&cmd[i].val)) {
            continue;
         }
         init_card();                         /* check if card is removed    */
         cmd[i].func (next);                  /* execute command function    */
         break;
      }
      if (i == CMD_COUNT) {
        printf ("\nCommand error\n");
      }
   }
}


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -