?? sd_file.c
字號:
/*----------------------------------------------------------------------------
* 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.14
*----------------------------------------------------------------------------
* This code is part of the RealView Run-Time Library.
* Copyright (c) 2004-2007 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 "STM32_Init.h" /* stm32 initialisation */
#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 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 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[80];
/* Local Function Prototypes */
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 (); /* initialise LCD */
lcd_clear ();
lcd_print ("MCBSTM32 SD_File");
set_cursor (0, 1);
lcd_print (" www.keil.com ");
}
/*----------------------------------------------------------------------------
* Process input string for an entry
*---------------------------------------------------------------------------*/
static char *get_entry (char *cp, char **pNext) {
char *sp;
if (cp == NULL) { /* skip NULL pointers */
*pNext = cp;
return (cp);
}
for ( ; *cp == ' '; cp++) { /* skip blanks */
*cp = 0;
}
for (sp = cp; *sp != ' ' && *sp != CR && *sp != LF; sp++) {
*sp = (char)toupper (*sp); /* convert entry to uppercase */
}
for ( ; *sp == ' ' || *sp == CR || *sp == LF; sp++) {
*sp = 0;
}
*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;
fname = get_entry (par, &next);
if (fname == NULL) {
printf ("\nFilename missing.\n");
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -