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

📄 sd_file.c

📁 ARM实例程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------------
 *      R T L   F l a s h   F i l e   S y s t e m   E x a m p l e
 *----------------------------------------------------------------------------
 *      Name:    SD_FILE.C
 *      Purpose: File manipulation example program
 *      Rev.:    V3.21
 *----------------------------------------------------------------------------
 *      This code is part of the RealView Run-Time Library.
 *      Copyright (c) 2004-2008 KEIL - An ARM Company. All rights reserved.
 *---------------------------------------------------------------------------*/

#include <RTL.h>                      /* RTL kernel functions & defines      */
#include <stdio.h>                    /* standard I/O .h-file                */
#include <ctype.h>                    /* character functions                 */
#include <string.h>                   /* string and memory functions         */
#include "File_Config.h"
#include "SD_File.h"
#include "LCD.h"

/* Command Functions */
static void cmd_capture (char *par);
static void cmd_type (char *par);
static void cmd_rename (char *par);
static void cmd_copy (char *par);
static void cmd_delete (char *par);
static void cmd_dir (char *par);
static void cmd_format (char *par);
static void cmd_help (char *par);
static void cmd_fill (char *par);

/* Local constants */
static const char intro[] =
   "\n\n\n\n\n\n\n\n"
   "+-----------------------------------------------------------------------+\n"
   "|                SD/MMC Card File Manipulation example                  |\n";
static const char help[] = 
   "+ command ------------------+ function ---------------------------------+\n"
   "| CAP \"fname\" [/A]          | captures serial data to a file            |\n"
   "|                           |  [/A option appends data to a file]       |\n"
   "| FILL \"fname\" [nnnn]       | create a file filled with text            |\n"
   "|                           |  [nnnn - number of lines, default=1000]   |\n"
   "| TYPE \"fname\"              | displays the content of a text file       |\n"
   "| REN \"fname1\" \"fname2\"     | renames a file 'fname1' to 'fname2'       |\n"
   "| COPY \"fin\" [\"fin2\"] \"fout\"| copies a file 'fin' to 'fout' file        |\n"
   "|                           |  ['fin2' option merges 'fin' and 'fin2']  |\n"
   "| DEL \"fname\"               | deletes a file                            |\n"
   "| DIR \"[mask]\"              | displays a list of files in the directory |\n"
   "| FORMAT [label]            | formats Flash Memory Card                 |\n"
   "| HELP  or  ?               | displays this help                        |\n"
   "+---------------------------+-------------------------------------------+\n";
static const char reading_free[] =
   "\nReading free space for the first time ...\n";

static const SCMD cmd[] = {
   "CAP",    cmd_capture,
   "TYPE",   cmd_type,
   "REN",    cmd_rename,
   "COPY",   cmd_copy,
   "DEL",    cmd_delete,
   "DIR",    cmd_dir,
   "FORMAT", cmd_format,
   "HELP",   cmd_help,
   "FILL",   cmd_fill,
   "?",      cmd_help };

#define CMD_COUNT   (sizeof (cmd) / sizeof (cmd[0]))

/* Local variables */
static char in_line[160];

/* Local Function Prototypes */
static void init_display (void);
static void dot_format (U32 val, char *sp);
static char *get_entry (char *cp, char **pNext);
static void init_card (void);

/*----------------------------------------------------------------------------
 *        Initialize On Board LCD Module
 *---------------------------------------------------------------------------*/
static void init_display (void) {
   /* LCD Module.2x16 init*/

   LCD_init ();
   LCD_cur_off ();
   LCD_puts ("    RL-ARM      "
             "SD_File example ");
}

/*----------------------------------------------------------------------------
 *        Process input string for long or short name entry
 *---------------------------------------------------------------------------*/
static char *get_entry (char *cp, char **pNext) {
   char *sp, lfn = 0, sep_ch = ' ';

   if (cp == NULL) {                          /* skip NULL pointers          */
      *pNext = cp;
      return (cp);
   }

   for ( ; *cp == ' ' || *cp == '\"'; cp++) { /* skip blanks and starting  " */
      if (*cp == '\"') { sep_ch = '\"'; lfn = 1; }
      *cp = 0;
   }
 
   for (sp = cp; *sp != CR && *sp != LF; sp++) {
      if ( lfn && *sp == '\"') break;
      if (!lfn && *sp == ' ' ) break;
   }

   for ( ; *sp == sep_ch || *sp == CR || *sp == LF; sp++) {
      *sp = 0;
      if ( lfn && *sp == sep_ch) { sp ++; break; }
   }

   *pNext = (*sp) ? sp : NULL;                /* next entry                  */
   return (cp);
}

/*----------------------------------------------------------------------------
 *        Print size in dotted fomat
 *---------------------------------------------------------------------------*/
static void dot_format (U32 val, char *sp) {

   if (val >= (U32)1e9) {
      sp += sprintf (sp,"%d.",val/(U32)1e9);
      val %= (U32)1e9;
      sp += sprintf (sp,"%03d.",val/(U32)1e6);
      val %= (U32)1e6;
      sprintf (sp,"%03d.%03d",val/1000,val%1000);
      return;
   }
   if (val >= (U32)1e6) {
      sp += sprintf (sp,"%d.",val/(U32)1e6);
      val %= (U32)1e6;
      sprintf (sp,"%03d.%03d",val/1000,val%1000);
      return;
   }
   if (val >= 1000) {
      sprintf (sp,"%d.%03d",val/1000,val%1000);
      return;
   }
   sprintf (sp,"%d",val);
}

/*----------------------------------------------------------------------------
 *        Capture serial data to file
 *---------------------------------------------------------------------------*/
static void cmd_capture (char *par) {
   char *fname,*next;
   BOOL append,retv;
   FILE *f;

   fname = get_entry (par, &next);
   if (fname == NULL) {
      printf ("\nFilename missing.\n");
      return;
   }
   append = __FALSE;
   if (next) {
      par = get_entry (next, &next);
      if (strcmp (par, "/A") == 0) {
         append = __TRUE;
      }
      else {
         printf ("\nCommand error.\n");
         return;
      }
   }
   printf ((append) ? "\nAppend data to file %s" :
                      "\nCapture data to file %s", fname);
   printf("\nPress ESC to stop.\n");
   f = fopen (fname,append ? "a" : "w");/* open a file for writing           */
   if (f == NULL) {
      printf ("\nCan not open file!\n");/* error when trying to open file    */
      return;
   } 
   do {
      retv = getline (in_line, sizeof (in_line));
      fputs (in_line, f);
   } while (retv == __TRUE);
   fclose (f);                        /* close the output file               */
   printf ("\nFile closed.\n");
}

/*----------------------------------------------------------------------------
 *        Create a file and fill it with some text
 *---------------------------------------------------------------------------*/
static void cmd_fill (char *par) {
   char *fname, *next;
   FILE *f;
   int i,cnt = 1000;

   fname = get_entry (par, &next);
   if (fname == NULL) {
      printf ("\nFilename missing.\n");
      return;
   }
   if (next) {
      par = get_entry (next, &next);
      if (sscanf (par,"%d", &cnt) == 0) {
         printf ("\nCommand error.\n");
         return;
      }
   }

   f = fopen (fname, "w");              /* open a file for writing           */
   if (f == NULL) {
      printf ("\nCan not open file!\n");/* error when trying to open file    */
      return;
   } 
   for (i = 0; i < cnt; i++)  {
      fprintf (f, "This is line # %d in file %s\n", i, fname);
   }
   fclose (f);                        /* close the output file               */
   printf ("\nFile closed.\n");
}


/*----------------------------------------------------------------------------
 *        Read file and dump it to serial window
 *---------------------------------------------------------------------------*/
static void cmd_type (char *par) {
   char *fname,*next;
   FILE *f;
   int ch;

   fname = get_entry (par, &next);
   if (fname == NULL) {
      printf ("\nFilename missing.\n");
      return;
   }
   printf("\nRead data from file %s\n",fname);
   f = fopen (fname,"r");             /* open the file for reading           */
   if (f == NULL) {
      printf ("\nFile not found!\n");
      return;
   }
  
   while ((ch = fgetc (f)) != EOF) {  /* read the characters from the file   */
      putchar (ch);                   /* and write them on the screen        */
   }
   fclose (f);                        /* close the input file when done      */
   printf ("\nFile closed.\n");
}

/*----------------------------------------------------------------------------
 *        Rename a File
 *---------------------------------------------------------------------------*/
static void cmd_rename (char *par) {
   char *fname,*fnew,*next,dir;

   fname = get_entry (par, &next);
   if (fname == NULL) {
      printf ("\nFilename missing.\n");
      return;
   }
   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;
   }

   dir = 0;
   if (*(fname + strlen(fname) - 1) == '\\') {
      dir = 1;
   }

   if (frename (fname, fnew) == 0) {
      if (dir) {
         printf ("\nDirectory %s renamed to %s\n",fname,fnew);
      }
      else {
         printf ("\nFile %s renamed to %s\n",fname,fnew);
      }
   }
   else {
      if (dir) {
         printf ("\nDirectory rename error.\n");
      }
      else {
         printf ("\nFile rename error.\n");

⌨️ 快捷键说明

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