📄 sd_card.c
字号:
// Get the index to the last character in the current path.
//
uIdx = strlen(g_cTmpBuf) - 1;
//
// Back up from the end of the path name until a separator (/)
// is found, or until we bump up to the start of the path.
//
while((g_cTmpBuf[uIdx] != '/') && (uIdx > 1))
{
//
// Back up one character.
//
uIdx--;
}
//
// Now we are either at the lowest level separator in the
// current path, or at the beginning of the string (root).
// So set the new end of string here, effectively removing
// that last part of the path.
//
g_cTmpBuf[uIdx] = 0;
}
//
// Otherwise this is just a normal path name from the current
// directory, and it needs to be appended to the current path.
//
else
{
//
// Test to make sure that when the new additional path is
// added on to the current path, there is room in the buffer
// for the full new path. It needs to include a new separator,
// and a trailing null character.
//
if(strlen(g_cTmpBuf) + strlen(argv[1]) + 1 + 1 > sizeof(g_cCwdBuf))
{
UARTprintf("Resulting path name is too long\n");
return(0);
}
//
// The new path is okay, so add the separator and then append
// the new directory to the path.
//
else
{
//
// If not already at the root level, then append a /
//
if(strcmp(g_cTmpBuf, "/"))
{
strcat(g_cTmpBuf, "/");
}
//
// Append the new directory to the path.
//
strcat(g_cTmpBuf, argv[1]);
}
}
//
// At this point, a candidate new directory path is in chTmpBuf.
// Try to open it to make sure it is valid.
//
fresult = f_opendir(&g_sDirObject, g_cTmpBuf);
//
// If it cant be opened, then it is a bad path. Inform
// user and return.
//
if(fresult != FR_OK)
{
UARTprintf("cd: %s\n", g_cTmpBuf);
return(fresult);
}
//
// Otherwise, it is a valid new path, so copy it into the CWD.
//
else
{
strncpy(g_cCwdBuf, g_cTmpBuf, sizeof(g_cCwdBuf));
}
//
// Return success.
//
return(0);
}
//*****************************************************************************
//
// This function implements the "pwd" command. It simply prints the
// current working directory.
//
//*****************************************************************************
int
Cmd_pwd(int argc, char *argv[])
{
//
// Print the CWD to the console.
//
UARTprintf("%s\n", g_cCwdBuf);
//
// Return success.
//
return(0);
}
//*****************************************************************************
//
// This function implements the "cat" command. It reads the contents of
// a file and prints it to the console. This should only be used on
// text files. If it is used on a binary file, then a bunch of garbage
// is likely to printed on the console.
//
//*****************************************************************************
int
Cmd_cat(int argc, char *argv[])
{
FRESULT fresult;
unsigned short usBytesRead;
//
// First, check to make sure that the current path (CWD), plus
// the file name, plus a separator and trailing null, will all
// fit in the temporary buffer that will be used to hold the
// file name. The file name must be fully specified, with path,
// to FatFs.
//
if(strlen(g_cCwdBuf) + strlen(argv[1]) + 1 + 1 > sizeof(g_cTmpBuf))
{
UARTprintf("Resulting path name is too long\n");
return(0);
}
//
// Copy the current path to the temporary buffer so it can be manipulated.
//
strcpy(g_cTmpBuf, g_cCwdBuf);
//
// If not already at the root level, then append a separator.
//
if(strcmp("/", g_cCwdBuf))
{
strcat(g_cTmpBuf, "/");
}
//
// Now finally, append the file name to result in a fully specified file.
//
strcat(g_cTmpBuf, argv[1]);
//
// Open the file for reading.
//
fresult = f_open(&g_sFileObject, g_cTmpBuf, FA_READ);
//
// If there was some problem opening the file, then return
// an error.
//
if(fresult != FR_OK)
{
return(fresult);
}
//
// Enter a loop to repeatedly read data from the file and display it,
// until the end of the file is reached.
//
do
{
//
// Read a block of data from the file. Read as much as can fit
// in the temporary buffer, including a space for the trailing null.
//
fresult = f_read(&g_sFileObject, g_cTmpBuf, sizeof(g_cTmpBuf) - 1,
&usBytesRead);
//
// If there was an error reading, then print a newline and
// return the error to the user.
//
if(fresult != FR_OK)
{
UARTprintf("\n");
return(fresult);
}
//
// Null terminate the last block that was read to make it a
// null terminated string that can be used with printf.
//
g_cTmpBuf[usBytesRead] = 0;
//
// Print the last chunk of the file that was received.
//
UARTprintf("%s", g_cTmpBuf);
//
// Continue reading until less than the full number of bytes are
// read. That means the end of the buffer was reached.
//
}
while(usBytesRead == sizeof(g_cTmpBuf) - 1);
//
// Return success.
//
return(0);
}
//*****************************************************************************
//
// This function implements the "help" command. It prints a simple list
// of the available commands with a brief description.
//
//*****************************************************************************
int
Cmd_help(int argc, char *argv[])
{
tCmdLineEntry *pEntry;
//
// Print some header text.
//
UARTprintf("\nAvailable commands\n");
UARTprintf("------------------\n");
//
// Point at the beginning of the command table.
//
pEntry = &g_sCmdTable[0];
//
// Enter a loop to read each entry from the command table. The
// end of the table has been reached when the command name is NULL.
//
while(pEntry->pcCmd)
{
//
// Print the command name and the brief description.
//
UARTprintf("%s%s\n", pEntry->pcCmd, pEntry->pcHelp);
//
// Advance to the next entry in the table.
//
pEntry++;
}
//
// Return success.
//
return(0);
}
//*****************************************************************************
//
// This is the table that holds the command names, implementing functions,
// and brief description.
//
//*****************************************************************************
tCmdLineEntry g_sCmdTable[] =
{
{ "help", Cmd_help, " : Display list of commands" },
{ "h", Cmd_help, " : alias for help" },
{ "?", Cmd_help, " : alias for help" },
{ "ls", Cmd_ls, " : Display list of files" },
{ "chdir", Cmd_cd, ": Change directory" },
{ "cd", Cmd_cd, " : alias for chdir" },
{ "pwd", Cmd_pwd, " : Show current working directory" },
{ "cat", Cmd_cat, " : Show contents of a text file" },
{ 0, 0, 0 }
};
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
//
// The program main function. It performs initialization, then runs
// a command processing loop to read commands from the console.
//
//*****************************************************************************
int
main(void)
{
int nStatus;
FRESULT fresult;
//
// Set the system clock to run at 8 MHz from the main oscillator.
//
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC |
SYSCTL_XTAL_8MHZ | SYSCTL_OSC_MAIN);
//
// Enable the peripherals used by this example.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Configure SysTick for a 100Hz interrupt. The FatFs driver
// wants a 10 ms tick.
//
SysTickPeriodSet(SysCtlClockGet() / 100);
SysTickEnable();
SysTickIntEnable();
//
// Enable Interrupts
//
IntMasterEnable();
//
// Set GPIO A0 and A1 as UART.
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Initialize the UART as a console for text I/O.
//
UARTStdioInit(0);
//
// Print hello message to user.
//
UARTprintf("\n\nSD Card Example Program\n");
UARTprintf("Type \'help\' for help.\n");
//
// Mount the file system, using logical disk 0.
//
fresult = f_mount(0, &g_sFatFs);
if(fresult != FR_OK)
{
UARTprintf("f_mount error: %s\n", StringFromFresult(fresult));
return(1);
}
//
// Enter an infinite loop for reading and processing commands from the
// user.
//
while(1)
{
//
// Print a prompt to the console. Show the CWD.
//
UARTprintf("\n%s> ", g_cCwdBuf);
//
// Get a line of text from the user.
//
UARTgets(g_cCmdBuf, sizeof(g_cCmdBuf));
//
// Pass the line from the user to the command processor.
// It will be parsed and valid commands executed.
//
nStatus = CmdLineProcess(g_cCmdBuf);
//
// Handle the case of bad command.
//
if(nStatus == CMDLINE_BAD_CMD)
{
UARTprintf("Bad command!\n");
}
//
// Handle the case of too many arguments.
//
else if(nStatus == CMDLINE_TOO_MANY_ARGS)
{
UARTprintf("Too many arguments for command processor!\n");
}
//
// Otherwise the command was executed. Print the error
// code if one was returned.
//
else if(nStatus != 0)
{
UARTprintf("Command returned error code %s\n",
StringFromFresult((FRESULT)nStatus));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -