fat_example.c File Reference


Detailed Description

FAT access example application.

This file gives an example of using the FAT access, through a shell.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file fat_example.c.

#include <string.h>
#include "compiler.h"
#include "preprocessor.h"
#include "board.h"
#include "print_funcs.h"
#include "ctrl_access.h"
#include "power_clocks_lib.h"
#include "gpio.h"
#include "usart.h"
#include "spi.h"
#include "conf_at45dbx.h"
#include "fat.h"
#include "file.h"
#include "navigation.h"

Go to the source code of this file.

Defines

#define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused)   {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION},
Special Char Values
#define ABORT_CHAR   CTRL_C
#define BKSPACE_CHAR   '\b'
#define CR   '\r'
#define CTRL_C   0x03
#define CTRL_Q   0x11
#define HISTORY_CHAR   '!'
#define LF   '\n'
#define QUIT_APPEND   CTRL_Q
Shell Commands
#define CMD_APPEND   0x09
#define CMD_CAT   0x04
#define CMD_CD   0x03
#define CMD_CP   0x0D
#define CMD_DF   0x0E
#define CMD_FAT   0x11
#define CMD_FORMAT   0x10
#define CMD_FORMAT32   0x12
#define CMD_GOTO_ID   0x0C
#define CMD_HELP   0x05
#define CMD_LS   0x02
#define CMD_MKDIR   0x06
#define CMD_MOUNT   0x01
#define CMD_MV   0x0F
#define CMD_NB_DRIVE   0x0A
#define CMD_NONE   0x00
#define CMD_RM   0x08
#define CMD_SET_ID   0x0B
#define CMD_TOUCH   0x07
String Messages
#define MSG_APPEND_WELCOME   "\nSimple text editor, enter char to append, ^q to exit and save\n"
#define MSG_ER_CMD_NOT_FOUND   "Command not found\n"
#define MSG_ER_DRIVE   "Drive does not exist\n"
#define MSG_ER_FORMAT   "Format fails\n"
#define MSG_ER_MOUNT   "Unable to mount drive\n"
#define MSG_ER_MV   "Error during move\n"
#define MSG_ER_RM   "Can not erase; if the name is a directory, check it is empty\n"
#define MSG_ER_UNKNOWN_FILE   "Unknown file\n"
#define MSG_HELP
#define MSG_PROMPT   "$>"
#define MSG_WELCOME
Shell USART Configuration
#define SHL_USART   (&AVR32_USART1)
#define SHL_USART_BAUDRATE   57600
#define SHL_USART_RX_FUNCTION   AVR32_USART1_RXD_0_0_FUNCTION
#define SHL_USART_RX_PIN   AVR32_USART1_RXD_0_0_PIN
#define SHL_USART_TX_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION
#define SHL_USART_TX_PIN   AVR32_USART1_TXD_0_0_PIN
String Values for Commands
#define STR_A   "a:"
#define STR_APPEND   "append"
#define STR_B   "b:"
#define STR_C   "c:"
#define STR_CAT   "cat"
#define STR_CD   "cd"
#define STR_CP   "cp"
#define STR_D   "d:"
#define STR_DF   "df"
#define STR_DISK   "disk"
#define STR_FAT   "fat"
#define STR_FORMAT   "format"
#define STR_FORMAT32   "format32"
#define STR_GOTO   "goto"
#define STR_HELP   "help"
#define STR_LS   "ls"
#define STR_MARK   "mark"
#define STR_MKDIR   "mkdir"
#define STR_MOUNT   "mount"
#define STR_MV   "mv"
#define STR_RM   "rm"
#define STR_TOUCH   "touch"

Functions

static void at45dbx_resources_init (void)
 Initializes the dataflash memory AT45DBX resources: GPIO, SPI and AT45DBX.
static void fat_example_append_file (void)
 Minimalist file editor to append char to a file.
static void fat_example_build_cmd (void)
 Gets the full command line on RS232 input to be interpreted. The cmd_str variable is built with the user inputs.
static void fat_example_parse_cmd (void)
 Decodes full command line into command type and arguments.
static void init_shl_rs232 (long pba_hz)
 Sets up USART for shell.
int main (void)
 Main function. Execution starts here.

Variables

static Bool cmd
 flag for a command presence
static char cmd_str [10+2 *MAX_FILE_PATH_LENGTH]
 string for command
static U8 cmd_type
 command number
static Bool first_ls
 flag for first ls : mount if set
static U8 i_str = 0
 string length
static char par_str1 [MAX_FILE_PATH_LENGTH]
 string for first arg
static char par_str2 [MAX_FILE_PATH_LENGTH]
 string for second arg
static char str_buff [MAX_FILE_PATH_LENGTH]
 buffer for command line


Define Documentation

#define ABORT_CHAR   CTRL_C

Definition at line 227 of file fat_example.c.

Referenced by fat_example_build_cmd().

#define AT45DBX_ENABLE_NPCS_PIN ( NPCS,
unused   )     {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION},

Referenced by at45dbx_resources_init().

#define BKSPACE_CHAR   '\b'

Definition at line 226 of file fat_example.c.

Referenced by fat_example_build_cmd().

#define CMD_APPEND   0x09

Definition at line 207 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_CAT   0x04

Definition at line 202 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_CD   0x03

Definition at line 201 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_CP   0x0D

Definition at line 211 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_DF   0x0E

Definition at line 212 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_FAT   0x11

Definition at line 215 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_FORMAT   0x10

Definition at line 214 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_FORMAT32   0x12

Definition at line 216 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_GOTO_ID   0x0C

Definition at line 210 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_HELP   0x05

Definition at line 203 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_LS   0x02

Definition at line 200 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_MKDIR   0x06

Definition at line 204 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_MOUNT   0x01

Definition at line 199 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_MV   0x0F

Definition at line 213 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_NB_DRIVE   0x0A

Definition at line 208 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_NONE   0x00

Definition at line 198 of file fat_example.c.

Referenced by main().

#define CMD_RM   0x08

Definition at line 206 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_SET_ID   0x0B

Definition at line 209 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CMD_TOUCH   0x07

Definition at line 205 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define CR   '\r'

Definition at line 222 of file fat_example.c.

Referenced by fat_example_append_file(), and fat_example_build_cmd().

#define CTRL_C   0x03

Definition at line 224 of file fat_example.c.

#define CTRL_Q   0x11

Definition at line 225 of file fat_example.c.

#define HISTORY_CHAR   '!'

Definition at line 229 of file fat_example.c.

#define LF   '\n'

Definition at line 223 of file fat_example.c.

Referenced by fat_example_append_file(), fat_example_build_cmd(), and main().

#define MSG_APPEND_WELCOME   "\nSimple text editor, enter char to append, ^q to exit and save\n"

Definition at line 274 of file fat_example.c.

Referenced by fat_example_append_file().

#define MSG_ER_CMD_NOT_FOUND   "Command not found\n"

Definition at line 267 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

#define MSG_ER_DRIVE   "Drive does not exist\n"

Definition at line 269 of file fat_example.c.

Referenced by main().

#define MSG_ER_FORMAT   "Format fails\n"

Definition at line 273 of file fat_example.c.

Referenced by main().

#define MSG_ER_MOUNT   "Unable to mount drive\n"

Definition at line 268 of file fat_example.c.

Referenced by main().

#define MSG_ER_MV   "Error during move\n"

Definition at line 272 of file fat_example.c.

Referenced by main().

#define MSG_ER_RM   "Can not erase; if the name is a directory, check it is empty\n"

Definition at line 270 of file fat_example.c.

Referenced by main().

#define MSG_ER_UNKNOWN_FILE   "Unknown file\n"

Definition at line 271 of file fat_example.c.

Referenced by main().

#define MSG_HELP

Value:

"Commands summary\n" \
                              " a:, b:... goto selected drive               mount disk(a, b...)\n" \
                              " cd dirname                                  ls\n" \
                              " mkdir dirname                               cat filename\n" \
                              " touch filename                              disk: get number of drives\n" \
                              " append filename                             goto: goto bookmark\n" \
                              " mark: bookmark current directory            df: get free space information\n" \
                              " cp filename: copy filename to bookmark      fat: get FAT type for current drive\n" \
                              " rm filename: erase file or EMPTY directory  format drivename, with drivename: a, b...\n" \
                              " mv src dst: move file or directory          format32 drivename, with drivename: a, b...\n" \
                              " help\n"

Definition at line 275 of file fat_example.c.

Referenced by main().

#define MSG_PROMPT   "$>"

Definition at line 262 of file fat_example.c.

Referenced by fat_example_build_cmd(), fat_example_parse_cmd(), and main().

#define MSG_WELCOME

Value:

"\n" \
                              "-------------------------\n" \
                              "    ATMEL AVR32 Shell\n" \
                              "-------------------------\n"

Definition at line 263 of file fat_example.c.

Referenced by main().

#define QUIT_APPEND   CTRL_Q

Definition at line 228 of file fat_example.c.

Referenced by fat_example_append_file().

#define SHL_USART   (&AVR32_USART1)

#define SHL_USART_BAUDRATE   57600

Definition at line 154 of file fat_example.c.

Referenced by init_shl_rs232().

#define SHL_USART_RX_FUNCTION   AVR32_USART1_RXD_0_0_FUNCTION

Definition at line 151 of file fat_example.c.

Referenced by init_shl_rs232().

#define SHL_USART_RX_PIN   AVR32_USART1_RXD_0_0_PIN

Definition at line 150 of file fat_example.c.

Referenced by init_shl_rs232().

#define SHL_USART_TX_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION

Definition at line 153 of file fat_example.c.

Referenced by init_shl_rs232().

#define SHL_USART_TX_PIN   AVR32_USART1_TXD_0_0_PIN

Definition at line 152 of file fat_example.c.

Referenced by init_shl_rs232().

#define STR_A   "a:"

Definition at line 249 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_APPEND   "append"

Definition at line 243 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_B   "b:"

Definition at line 250 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_C   "c:"

Definition at line 251 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_CAT   "cat"

Definition at line 244 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_CD   "cd"

Definition at line 235 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_CP   "cp"

Definition at line 237 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_D   "d:"

Definition at line 252 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_DF   "df"

Definition at line 240 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_DISK   "disk"

Definition at line 245 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_FAT   "fat"

Definition at line 256 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_FORMAT   "format"

Definition at line 254 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_FORMAT32   "format32"

Definition at line 255 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_GOTO   "goto"

Definition at line 247 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_HELP   "help"

Definition at line 253 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_LS   "ls"

Definition at line 238 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_MARK   "mark"

Definition at line 246 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_MKDIR   "mkdir"

Definition at line 241 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_MOUNT   "mount"

Definition at line 236 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_MV   "mv"

Definition at line 248 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_RM   "rm"

Definition at line 239 of file fat_example.c.

Referenced by fat_example_parse_cmd().

#define STR_TOUCH   "touch"

Definition at line 242 of file fat_example.c.

Referenced by fat_example_parse_cmd().


Function Documentation

static void at45dbx_resources_init ( void   )  [static]

Initializes the dataflash memory AT45DBX resources: GPIO, SPI and AT45DBX.

Definition at line 555 of file fat_example.c.

References AT45DBX_ENABLE_NPCS_PIN, AT45DBX_MEM_CNT, AT45DBX_SPI_BITS, AT45DBX_SPI_FIRST_NPCS, and AT45DBX_SPI_MASTER_SPEED.

Referenced by main().

00556 {
00557   // GPIO map for SPI.
00558   static const gpio_map_t AT45DBX_SPI_GPIO_MAP =
00559   {
00560     {AT45DBX_SPI_SCK_PIN,          AT45DBX_SPI_SCK_FUNCTION         },  // SPI Clock.
00561     {AT45DBX_SPI_MISO_PIN,         AT45DBX_SPI_MISO_FUNCTION        },  // MISO.
00562     {AT45DBX_SPI_MOSI_PIN,         AT45DBX_SPI_MOSI_FUNCTION        },  // MOSI.
00563 #define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused) \
00564     {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION},  // Chip Select NPCS.
00565     MREPEAT(AT45DBX_MEM_CNT, AT45DBX_ENABLE_NPCS_PIN, ~)
00566 #undef AT45DBX_ENABLE_NPCS_PIN
00567   };
00568 
00569   // Options for SPI.
00570   spi_options_t spiOptions =
00571   {
00572     .reg          = AT45DBX_SPI_FIRST_NPCS,   // Defined in conf_at45dbx.h.
00573     .baudrate     = AT45DBX_SPI_MASTER_SPEED, // Defined in conf_at45dbx.h.
00574     .bits         = AT45DBX_SPI_BITS,         // Defined in conf_at45dbx.h.
00575     .spck_delay   = 0,
00576     .trans_delay  = 0,
00577     .stay_act     = 1,
00578     .spi_mode     = 0,
00579     .modfdis      = 1
00580   };
00581 
00582   // Assign I/Os to SPI.
00583   gpio_enable_module(AT45DBX_SPI_GPIO_MAP,
00584                      sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0]));
00585 
00586   // Initialize as master.
00587   spi_initMaster(AT45DBX_SPI, &spiOptions);
00588 
00589   // Set selection mode: variable_ps, pcs_decode, delay.
00590   spi_selectionMode(AT45DBX_SPI, 0, 0, 0);
00591 
00592   // Enable SPI.
00593   spi_enable(AT45DBX_SPI);
00594 
00595   // Initialize data flash with SPI clock Osc0.
00596   at45dbx_init(spiOptions, FOSC0);
00597 }

static void fat_example_append_file ( void   )  [static]

Minimalist file editor to append char to a file.

Note:
Hit ^q to exit and save file.

Definition at line 481 of file fat_example.c.

References CR, file_putc(), LF, MSG_APPEND_WELCOME, QUIT_APPEND, and SHL_USART.

Referenced by main().

00482 {
00483   int c;
00484 
00485   print(SHL_USART, MSG_APPEND_WELCOME);
00486 
00487   // Wait for ^q to quit.
00488   while (TRUE)
00489   {
00490     // If something new in the USART
00491     usart_reset_status(SHL_USART);
00492     if (usart_read_char(SHL_USART, &c) == USART_SUCCESS)
00493     {
00494       // if this is not the quit char
00495       if (c != QUIT_APPEND)
00496       {
00497         // Echo the char.
00498         print_char(SHL_USART, c);
00499         // Add it to the file.
00500         file_putc(c);
00501         // if it is a carriage return.
00502         if (c == CR)
00503         {
00504           // Echo line feed.
00505           print_char(SHL_USART, LF);
00506           // Add line feed to the file.
00507           file_putc(LF);
00508         }
00509       }
00510       // Quit char received.
00511       else
00512       {
00513         // Exit the append function.
00514         break;
00515       }
00516     }
00517   }
00518 }

static void fat_example_build_cmd ( void   )  [static]

Gets the full command line on RS232 input to be interpreted. The cmd_str variable is built with the user inputs.

Definition at line 430 of file fat_example.c.

References ABORT_CHAR, BKSPACE_CHAR, cmd_str, CR, fat_example_parse_cmd(), i_str, LF, MSG_PROMPT, and SHL_USART.

Referenced by main().

00431 {
00432   int c;
00433 
00434   // If something new in the USART
00435   usart_reset_status(SHL_USART);
00436   if (usart_read_char(SHL_USART, &c) == USART_SUCCESS)
00437   {
00438     switch (c)
00439     {
00440     case CR:
00441       // Add LF.
00442       print_char(SHL_USART, LF);
00443       // Add NUL char.
00444       cmd_str[i_str] = '\0';
00445       // Decode the command.
00446       fat_example_parse_cmd();
00447       i_str = 0;
00448       break;
00449     // ^c abort cmd.
00450     case ABORT_CHAR:
00451       // Reset command length.
00452       i_str = 0;
00453       // Display prompt.
00454       print(SHL_USART, "\n" MSG_PROMPT);
00455       break;
00456     // Backspace.
00457     case BKSPACE_CHAR:
00458       if (i_str > 0)
00459       {
00460         // Replace last char.
00461         print(SHL_USART, "\b \b");
00462         // Decraese command length.
00463         i_str--;
00464       }
00465       break;
00466     default:
00467       // Echo.
00468       print_char(SHL_USART, c);
00469       // Append to cmd line.
00470       cmd_str[i_str++] = c;
00471       break;
00472     }
00473   }
00474 }

static void fat_example_parse_cmd ( void   )  [static]

Decodes full command line into command type and arguments.

This function allows to set the cmd_type variable to the command type decoded with its respective arguments par_str1 and par_str2.

Definition at line 317 of file fat_example.c.

References cmd, CMD_APPEND, CMD_CAT, CMD_CD, CMD_CP, CMD_DF, CMD_FAT, CMD_FORMAT, CMD_FORMAT32, CMD_GOTO_ID, CMD_HELP, CMD_LS, CMD_MKDIR, CMD_MOUNT, CMD_MV, CMD_NB_DRIVE, CMD_RM, CMD_SET_ID, cmd_str, CMD_TOUCH, cmd_type, i_str, MSG_ER_CMD_NOT_FOUND, MSG_PROMPT, par_str1, par_str2, SHL_USART, STR_A, STR_APPEND, STR_B, STR_C, STR_CAT, STR_CD, STR_CP, STR_D, STR_DF, STR_DISK, STR_FAT, STR_FORMAT, STR_FORMAT32, STR_GOTO, STR_HELP, STR_LS, STR_MARK, STR_MKDIR, STR_MOUNT, STR_MV, STR_RM, and STR_TOUCH.

Referenced by fat_example_build_cmd().

00318 {
00319   U8 i, j;
00320 
00321   // Get command type.
00322   for (i = 0; cmd_str[i] != ' ' && i < i_str; i++);
00323 
00324   if (i)
00325   {
00326     cmd = TRUE;
00327     // Save last byte
00328     j = cmd_str[i];
00329     // Reset vars
00330     cmd_str[i] = '\0';
00331     par_str1[0] = '\0';
00332     par_str2[0] = '\0';
00333 
00334     // Decode command type.
00335     if      (!strcmp(cmd_str, STR_CD      )) cmd_type = CMD_CD;
00336     else if (!strcmp(cmd_str, STR_MOUNT   )) cmd_type = CMD_MOUNT;
00337     else if (!strcmp(cmd_str, STR_FAT     )) cmd_type = CMD_FAT;
00338     else if (!strcmp(cmd_str, STR_CP      )) cmd_type = CMD_CP;
00339     else if (!strcmp(cmd_str, STR_LS      )) cmd_type = CMD_LS;
00340     else if (!strcmp(cmd_str, STR_RM      )) cmd_type = CMD_RM;
00341     else if (!strcmp(cmd_str, STR_DF      )) cmd_type = CMD_DF;
00342     else if (!strcmp(cmd_str, STR_MKDIR   )) cmd_type = CMD_MKDIR;
00343     else if (!strcmp(cmd_str, STR_TOUCH   )) cmd_type = CMD_TOUCH;
00344     else if (!strcmp(cmd_str, STR_APPEND  )) cmd_type = CMD_APPEND;
00345     else if (!strcmp(cmd_str, STR_CAT     )) cmd_type = CMD_CAT;
00346     else if (!strcmp(cmd_str, STR_DISK    )) cmd_type = CMD_NB_DRIVE;
00347     else if (!strcmp(cmd_str, STR_MARK    )) cmd_type = CMD_SET_ID;
00348     else if (!strcmp(cmd_str, STR_GOTO    )) cmd_type = CMD_GOTO_ID;
00349     else if (!strcmp(cmd_str, STR_MV      )) cmd_type = CMD_MV;
00350     else if (!strcmp(cmd_str, STR_A       )) cmd_type = CMD_MOUNT, par_str1[0] = 'a', par_str1[1] = '\0';
00351     else if (!strcmp(cmd_str, STR_B       )) cmd_type = CMD_MOUNT, par_str1[0] = 'b', par_str1[1] = '\0';
00352     else if (!strcmp(cmd_str, STR_C       )) cmd_type = CMD_MOUNT, par_str1[0] = 'c', par_str1[1] = '\0';
00353     else if (!strcmp(cmd_str, STR_D       )) cmd_type = CMD_MOUNT, par_str1[0] = 'd', par_str1[1] = '\0';
00354     else if (!strcmp(cmd_str, STR_HELP    )) cmd_type = CMD_HELP;
00355     else if (!strcmp(cmd_str, STR_FORMAT  )) cmd_type = CMD_FORMAT;
00356     else if (!strcmp(cmd_str, STR_FORMAT32)) cmd_type = CMD_FORMAT32;
00357     else
00358     {
00359       // error : command not found
00360       print(SHL_USART, MSG_ER_CMD_NOT_FOUND);
00361       cmd = FALSE;
00362     }
00363     // restore last byte
00364     cmd_str[i] = j;
00365   }
00366   // if command isn't found, display prompt
00367   if (!cmd)
00368   {
00369     print(SHL_USART, MSG_PROMPT);
00370     return;
00371   }
00372 
00373   // Get first arg (if any).
00374   if (++i < i_str)
00375   {
00376     j = 0;
00377     // remove " if used
00378     if (cmd_str[i] == '"')
00379     {
00380       i++;
00381       for (; cmd_str[i] != '"' && i < i_str; i++, j++)
00382       {
00383         par_str1[j] = cmd_str[i];
00384       }
00385       i++;
00386     }
00387     // get the arg directly
00388     else
00389     {
00390       for(; cmd_str[i] != ' ' && i < i_str; i++, j++)
00391       {
00392         par_str1[j] = cmd_str[i];
00393       }
00394     }
00395     // null terminated arg
00396     par_str1[j] = '\0';
00397   }
00398 
00399   // Get second arg (if any).
00400   if (++i < i_str)
00401   {
00402     j = 0;
00403     // remove " if used
00404     if (cmd_str[i] == '"')
00405     {
00406       i++;
00407       for (; cmd_str[i] != '"' && i < i_str; i++, j++)
00408       {
00409         par_str2[j] = cmd_str[i];
00410       }
00411       i++;
00412     }
00413     // get the arg directly
00414     else
00415     {
00416       for (; cmd_str[i] != ' ' && i < i_str; i++, j++)
00417       {
00418         par_str2[j] = cmd_str[i];
00419       }
00420     }
00421     // null terminated arg
00422     par_str2[j] = '\0';
00423   }
00424 }

static void init_shl_rs232 ( long  pba_hz  )  [static]

Sets up USART for shell.

Parameters:
pba_hz The current module frequency.

Definition at line 525 of file fat_example.c.

References SHL_USART, SHL_USART_BAUDRATE, SHL_USART_RX_FUNCTION, SHL_USART_RX_PIN, SHL_USART_TX_FUNCTION, and SHL_USART_TX_PIN.

Referenced by main().

00526 {
00527   // GPIO map for USART.
00528   static const gpio_map_t SHL_USART_GPIO_MAP =
00529   {
00530     {SHL_USART_RX_PIN, SHL_USART_RX_FUNCTION},
00531     {SHL_USART_TX_PIN, SHL_USART_TX_FUNCTION}
00532   };
00533 
00534   // Options for USART.
00535   static const usart_options_t SHL_USART_OPTIONS =
00536   {
00537     .baudrate = SHL_USART_BAUDRATE,
00538     .charlength = 8,
00539     .paritytype = USART_NO_PARITY,
00540     .stopbits = USART_1_STOPBIT,
00541     .channelmode = USART_NORMAL_CHMODE
00542   };
00543 
00544   // Set up GPIO for SHL_USART, size of the GPIO map is 2 here.
00545   gpio_enable_module(SHL_USART_GPIO_MAP,
00546                      sizeof(SHL_USART_GPIO_MAP) / sizeof(SHL_USART_GPIO_MAP[0]));
00547 
00548   // Initialize it in RS232 mode.
00549   usart_init_rs232(SHL_USART, &SHL_USART_OPTIONS, pba_hz);
00550 }

int main ( void   ) 

Main function. Execution starts here.

Definition at line 602 of file fat_example.c.

References at45dbx_resources_init(), cmd, CMD_APPEND, CMD_CAT, CMD_CD, CMD_CP, CMD_DF, CMD_FAT, CMD_FORMAT, CMD_FORMAT32, CMD_GOTO_ID, CMD_HELP, CMD_LS, CMD_MKDIR, CMD_MOUNT, CMD_MV, CMD_NB_DRIVE, CMD_NONE, CMD_RM, CMD_SET_ID, CMD_TOUCH, cmd_type, COPY_BUSY, CTRL_GOOD, fat_example_append_file(), fat_example_build_cmd(), file_close(), file_eof(), file_getc(), file_open(), first_ls, FOPEN_MODE_APPEND, FOPEN_MODE_R, FS_DIR, FS_FILE, FS_FIND_NEXT, FS_FORMAT_DEFAULT, FS_FORMAT_FAT32, FS_NAME_GET, FS_SHIFT_B_TO_SECTOR, FS_TYPE_FAT_12, FS_TYPE_FAT_16, FS_TYPE_FAT_32, init_shl_rs232(), LF, LUN_ID_AT45DBX_MEM, MAX_FILE_PATH_LENGTH, mem_name(), mem_read_capacity(), mem_test_unit_ready(), MSG_ER_CMD_NOT_FOUND, MSG_ER_DRIVE, MSG_ER_FORMAT, MSG_ER_MOUNT, MSG_ER_MV, MSG_ER_RM, MSG_ER_UNKNOWN_FILE, MSG_HELP, MSG_PROMPT, MSG_WELCOME, nav_dir_make(), nav_dir_name(), nav_drive_format(), nav_drive_get(), nav_drive_nb(), nav_drive_set(), nav_file_copy(), nav_file_create(), nav_file_del(), nav_file_isdir(), nav_file_lgt(), nav_file_lgtsector(), nav_file_name(), nav_file_paste_start(), nav_file_paste_state(), nav_file_rename(), nav_filelist_first(), nav_filelist_nb(), nav_filelist_reset(), nav_filelist_set(), nav_getindex(), nav_gotoindex(), nav_partition_freespace(), nav_partition_mount(), nav_partition_space(), nav_partition_type(), nav_reset(), nav_setcwd(), par_str1, par_str2, SHL_USART, and str_buff.

00603 {
00604   U8 i, j;
00605   U16 file_size;
00606   Fs_index sav_index;
00607   static Fs_index mark_index;
00608   const char *part_type;
00609   U32 VarTemp;
00610 
00611   // Switch to external oscillator 0.
00612   pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00613 
00614   // Initialize RS232 shell text output.
00615   init_shl_rs232(FOSC0);
00616 
00617   // Initialize AT45DBX resources: GPIO, SPI and AT45DBX.
00618   at45dbx_resources_init();
00619 
00620   // Display memory status
00621   print(SHL_USART, MSG_WELCOME "\nMemory ");
00622 
00623   // Test if the memory is ready - using the control access memory abstraction layer (/SERVICES/MEMORY/CTRL_ACCESS/)
00624   if (mem_test_unit_ready(LUN_ID_AT45DBX_MEM) == CTRL_GOOD)
00625   {
00626     // Get and display the capacity
00627     mem_read_capacity(LUN_ID_AT45DBX_MEM, &VarTemp);
00628     print(SHL_USART, "OK:\t");
00629     print_ulong(SHL_USART, (VarTemp + 1) >> (20 - FS_SHIFT_B_TO_SECTOR));
00630     print(SHL_USART, " MB\n");
00631   }
00632   else
00633   {
00634     // Display an error message
00635     print(SHL_USART, "Not initialized: Check if memory is ready...\n");
00636   }
00637 
00638   // Display the prompt
00639   print(SHL_USART, MSG_PROMPT);
00640 
00641   // reset vars
00642   cmd = FALSE;
00643   cmd_type = CMD_NONE;
00644   first_ls = TRUE;
00645   // reset all navigators
00646   nav_reset();
00647 
00648   // always loop
00649   while (TRUE)
00650   {
00651     // While a usable user command on RS232 isn't received, build it
00652     if (!cmd)
00653     {
00654       fat_example_build_cmd();
00655     }
00656     // perform the command
00657     else
00658     {
00659       switch (cmd_type)
00660       {
00661       // this is a "mount" command
00662       case CMD_MOUNT:
00663         // Get drive number
00664         i = par_str1[0] - 'a';
00665         // If drive doesn't exist
00666         if (i >= nav_drive_nb())
00667         {
00668           // Display error message.
00669           print(SHL_USART, MSG_ER_DRIVE);
00670         }
00671         else
00672         {
00673           // Reset all navigators.
00674           nav_reset();
00675           // Select the desired drive.
00676           nav_drive_set(i);
00677           // Try to mount it.
00678           if (!nav_partition_mount())
00679           {
00680             // Display error message.
00681             print(SHL_USART, MSG_ER_MOUNT);
00682             // Retry to mount at next "ls".
00683             first_ls = TRUE;
00684           }
00685           else
00686           {
00687             // Clear flag, no need to remount at next "ls".
00688             first_ls = FALSE;
00689           }
00690         }
00691         break;
00692       // this is a "fat" information command
00693       case CMD_FAT:
00694         // Regarding the partition type :
00695         switch (nav_partition_type())
00696         {
00697         case FS_TYPE_FAT_12:
00698           // Display FAT12.
00699           part_type = "Drive uses FAT12\n";
00700           break;
00701         case FS_TYPE_FAT_16:
00702           // Display FAT16.
00703           part_type = "Drive uses FAT16\n";
00704           break;
00705         case FS_TYPE_FAT_32:
00706           // Display FAT32.
00707           part_type = "Drive uses FAT32\n";
00708           break;
00709         default:
00710           // Display error message.
00711           part_type = "Drive uses an unknown partition type\n";
00712           break;
00713         }
00714         print(SHL_USART, part_type);
00715         break;
00716       // this is a "ls" command
00717       case CMD_LS:
00718         // Check if params are correct or mount needed.
00719         if (nav_drive_get() >= nav_drive_nb() || first_ls)
00720         {
00721           first_ls = FALSE;
00722           // Reset navigators .
00723           nav_reset();
00724           // Use the last drive available as default.
00725           nav_drive_set(nav_drive_nb() - 1);
00726           // Mount it.
00727           nav_partition_mount();
00728         }
00729         // Get the volume name
00730         nav_dir_name((FS_STRING)str_buff, MAX_FILE_PATH_LENGTH);
00731         // Display general informations (drive letter and current path)
00732         print(SHL_USART, "\nVolume is ");
00733         print_char(SHL_USART, 'A' + nav_drive_get());
00734         print(SHL_USART, ":\nDir name is ");
00735         print(SHL_USART, str_buff);
00736         print_char(SHL_USART, LF);
00737         // Try to sort items by folders
00738         if (!nav_filelist_first(FS_DIR))
00739         {
00740           // Sort items by files
00741           nav_filelist_first(FS_FILE);
00742         }
00743         // Display items informations
00744         print(SHL_USART, "\tSize (Bytes)\tName\n");
00745         // reset filelist before to start the listing
00746         nav_filelist_reset();
00747         // While an item can be found
00748         while (nav_filelist_set(0, FS_FIND_NEXT))
00749         {
00750           // Get and display current item informations
00751           print(SHL_USART, (nav_file_isdir()) ? "Dir\t" : "   \t");
00752           print_ulong(SHL_USART, nav_file_lgt());
00753           print(SHL_USART, "\t\t");
00754           nav_file_name((FS_STRING)str_buff, MAX_FILE_PATH_LENGTH, FS_NAME_GET, TRUE);
00755           print(SHL_USART, str_buff);
00756           print_char(SHL_USART, LF);
00757         }
00758         // Display the files number
00759         print_ulong(SHL_USART, nav_filelist_nb(FS_FILE));
00760         print(SHL_USART, "  Files\n");
00761         // Display the folders number
00762         print_ulong(SHL_USART, nav_filelist_nb(FS_DIR));
00763         print(SHL_USART, "  Dir\n");
00764         break;
00765       // this is a "cd" command
00766       case CMD_CD:
00767         // get arg1 length
00768         i = strlen(par_str1);
00769         // Append the '/' char for the nav_setcwd to enter the chosen directory.
00770         if (par_str1[i - 1] != '/')
00771         {
00772           par_str1[i] = '/';
00773           par_str1[i + 1] = '\0';
00774         }
00775         // Try to to set navigator on arg1 folder.
00776         if (!nav_setcwd((FS_STRING)par_str1, TRUE, FALSE))
00777         {
00778           // Display error message.
00779           print(SHL_USART, MSG_ER_UNKNOWN_FILE);
00780         }
00781         break;
00782       // this is a "cat" command
00783       case CMD_CAT:
00784         // Try to to set navigator on arg1 file.
00785         if (!nav_setcwd((FS_STRING)par_str1, TRUE, FALSE))
00786         {
00787           // Display error message.
00788           print(SHL_USART, MSG_ER_UNKNOWN_FILE);
00789         }
00790         else
00791         {
00792           // Open the file.
00793           file_open(FOPEN_MODE_R);
00794           // While the end isn't reached
00795           while (!file_eof())
00796           {
00797             // Display next char from file.
00798             print_char(SHL_USART, file_getc());
00799           }
00800           // Close the file.
00801           file_close();
00802           print_char(SHL_USART, LF);
00803         }
00804         break;
00805       // this is a "help" command
00806       case CMD_HELP:
00807         // Display help on USART
00808         print(SHL_USART, MSG_HELP);
00809         break;
00810       // this is a "mkdir" command
00811       case CMD_MKDIR:
00812         // Create the folder;
00813         nav_dir_make((FS_STRING)par_str1);
00814         break;
00815       // this is a "touch" command
00816       case CMD_TOUCH:
00817         // Create the file.
00818         nav_file_create((FS_STRING)par_str1);
00819         break;
00820       // this is a "rm" command
00821       case CMD_RM:
00822         // Save current nav position.
00823         sav_index = nav_getindex();
00824         // Try to to set navigator on arg1 folder or file.
00825         if (!nav_setcwd((FS_STRING)par_str1, TRUE, FALSE))
00826         {
00827           // Display error message.
00828           print(SHL_USART, MSG_ER_UNKNOWN_FILE);
00829         }
00830         // Try to delete the file
00831         else if (!nav_file_del(FALSE))
00832         {
00833           // Display error message.
00834           print(SHL_USART, MSG_ER_RM);
00835         }
00836         // Restore nav position.
00837         nav_gotoindex(&sav_index);
00838         break;
00839       // this is a "append"  command: Append a char to selected file.
00840       case CMD_APPEND:
00841         // Try to to set navigator on arg1 file.
00842         if (!nav_setcwd((FS_STRING)par_str1, TRUE, TRUE))
00843         {
00844           // Display error message.
00845           print(SHL_USART, MSG_ER_UNKNOWN_FILE);
00846         }
00847         else
00848         {
00849           // File exists, open it in append mode
00850           file_open(FOPEN_MODE_APPEND);
00851           // Append from USART
00852           fat_example_append_file();
00853           // Close the file
00854           file_close();
00855           // Display a line feed to user
00856           print_char(SHL_USART, LF);
00857         }
00858         break;
00859       // this is a "disk" command
00860       case CMD_NB_DRIVE:
00861         // Display number of drives.
00862         print(SHL_USART, "Nb Drive(s): ");
00863         print_char(SHL_USART, '0' + nav_drive_nb());
00864         print_char(SHL_USART, LF);
00865         break;
00866       // this is a "mark" command
00867       case CMD_SET_ID:
00868         // get marked index from current navigator location
00869         mark_index = nav_getindex();
00870         break;
00871       // this is a "goto" command
00872       case CMD_GOTO_ID:
00873         // set navigator to the marked index
00874         nav_gotoindex(&mark_index);
00875         break;
00876       // this is a "cp" command: Copy file to other location.
00877       case CMD_CP:
00878         // Try to set navigator on arg1 file.
00879         if (!nav_setcwd((FS_STRING)par_str1, TRUE, FALSE))
00880         {
00881           // Display error message.
00882           print(SHL_USART, MSG_ER_UNKNOWN_FILE);
00883         }
00884         else
00885         {
00886           // Get name of source to be used as same destination name.
00887           nav_file_name((FS_STRING)par_str1, MAX_FILE_PATH_LENGTH, FS_NAME_GET, TRUE);
00888           // Get file size.
00889           file_size = nav_file_lgtsector();
00890           // Mark source.
00891           nav_file_copy();
00892           // Save current source position.
00893           sav_index = nav_getindex();
00894           // Goto destination.
00895           nav_gotoindex(&mark_index);
00896           // Free space check.
00897           if (nav_partition_space() > file_size)
00898           {
00899             // Paste.
00900             nav_file_paste_start((FS_STRING)par_str1);
00901             // Restore previous nav position.
00902             nav_gotoindex(&sav_index);
00903             // Performs copy.
00904             while (nav_file_paste_state(FALSE) == COPY_BUSY);
00905           }
00906           // Restore previous nav position.
00907           nav_gotoindex(&sav_index);
00908         }
00909         break;
00910       // this is a "mv" command: Rename file.
00911       case CMD_MV:
00912         // Save current nav position.
00913         sav_index = nav_getindex();
00914         // Try to to set navigator on arg1 folder or file.
00915         if (!nav_setcwd((FS_STRING)par_str1, TRUE, FALSE))
00916         {
00917           // Display error message.
00918           print(SHL_USART, MSG_ER_UNKNOWN_FILE);
00919         }
00920         // Try to rename the file
00921         else if (!nav_file_rename((FS_STRING)par_str2))
00922         {
00923           // Display error message.
00924           print(SHL_USART, MSG_ER_MV);
00925         }
00926         // Restore nav position.
00927         nav_gotoindex(&sav_index);
00928         break;
00929       // this is a "df" command: Display free space information for all connected drives.
00930       case CMD_DF:
00931         // Save current nav position.
00932         sav_index = nav_getindex();
00933         // For all available drives :
00934         for (i = 0; i < nav_drive_nb(); i++)
00935         {
00936           // Select drive.
00937           nav_drive_set(i);
00938           // Try to mount.
00939           if (nav_partition_mount())
00940           {
00941             // Display memory name.
00942             print(SHL_USART, mem_name(i));
00943             // Display drive letter name.
00944             print(SHL_USART, " (");
00945             print_char(SHL_USART, 'A' + i);
00946             print(SHL_USART, ":) Free Space: ");
00947             // Display free space.
00948             print_ulong(SHL_USART, nav_partition_freespace() << FS_SHIFT_B_TO_SECTOR);
00949             print(SHL_USART, " Bytes / ");
00950             // Display available space.
00951             print_ulong(SHL_USART, nav_partition_space() << FS_SHIFT_B_TO_SECTOR);
00952             print(SHL_USART, " Bytes\n");
00953           }
00954         }
00955         // Restore nav position.
00956         nav_gotoindex(&sav_index);
00957         break;
00958       // this is a "format" command : Format disk.
00959       case CMD_FORMAT:
00960         // Get disk number.
00961         i = par_str1[0] - 'a';
00962         // if drive number isn't valid
00963         if (i >= nav_drive_nb())
00964         {
00965           // Display error message.
00966           print(SHL_USART, MSG_ER_DRIVE);
00967         }
00968         else
00969         {
00970           // Get the current drive in the navigator.
00971           j = nav_drive_get();
00972           // Select drive to format.
00973           nav_drive_set(i);
00974           // If format fails.
00975           if (!nav_drive_format(FS_FORMAT_DEFAULT))
00976           {
00977             // Display error message.
00978             print(SHL_USART, MSG_ER_FORMAT);
00979             // Return to the previous.
00980             nav_drive_set(j);
00981           }
00982           // Format succeds, if drives is the one we were navigating on
00983           else if (i == j)
00984           {
00985             // Reset the navigators.
00986             nav_reset();
00987             // Set current drive.
00988             nav_drive_set(j);
00989             // If partition mounting fails.
00990             if (!nav_partition_mount())
00991             {
00992               // Display error message.
00993               print(SHL_USART, MSG_ER_MOUNT);
00994               // this will be the first "ls"
00995               first_ls = TRUE;
00996             }
00997             else
00998             {
00999               // this won't be the first "ls", system is already mounted
01000               first_ls = FALSE;
01001             }
01002           }
01003           // Format succeds, restore previous navigator drive.
01004           else nav_drive_set(j);
01005         }
01006         break;
01007       // this is a "format32" command: Format disk as FAT 32 if possible.
01008       case CMD_FORMAT32:
01009         // Get disk number.
01010         i = par_str1[0] - 'a';
01011         // if drive number isn't valid
01012         if (i >= nav_drive_nb())
01013         {
01014           // Display error message.
01015           print(SHL_USART, MSG_ER_DRIVE);
01016         }
01017         else
01018         {
01019           // Get the current drive in the navigator.
01020           j = nav_drive_get();
01021           // Select drive to format.
01022           nav_drive_set(i);
01023           // If format fails.
01024           if (!nav_drive_format(FS_FORMAT_FAT32))
01025           {
01026             // Display error message.
01027             print(SHL_USART, MSG_ER_FORMAT);
01028             // Return to the previous.
01029             nav_drive_set(j);
01030           }
01031           // Format succeds, if drives is the one we were navigating on
01032           else if (i == j)
01033           {
01034             // Reset the navigators.
01035             nav_reset();
01036             // Set current drive.
01037             nav_drive_set(j);
01038             // If partition mounting fails.
01039             if (!nav_partition_mount())
01040             {
01041               // Display error message.
01042               print(SHL_USART, MSG_ER_MOUNT);
01043               // this will be the first "ls"
01044               first_ls = TRUE;
01045             }
01046             else
01047             {
01048               // this won't be the first "ls", system is already mounted
01049               first_ls = FALSE;
01050             }
01051           }
01052           // Format succeds, restore previous navigator drive.
01053           else nav_drive_set(j);
01054         }
01055         break;
01056       // Unknown command.
01057       default:
01058         // Display error message.
01059         print(SHL_USART, MSG_ER_CMD_NOT_FOUND);
01060         break;
01061       }
01062       // Reset vars.
01063       cmd_type = CMD_NONE;
01064       cmd = FALSE;
01065       // Display prompt.
01066       print(SHL_USART, MSG_PROMPT);
01067     }
01068   }
01069 }


Variable Documentation

Bool cmd [static]

flag for a command presence

Definition at line 291 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

char cmd_str[10+2 *MAX_FILE_PATH_LENGTH] [static]

string for command

Definition at line 300 of file fat_example.c.

Referenced by fat_example_build_cmd(), and fat_example_parse_cmd().

U8 cmd_type [static]

command number

Definition at line 293 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

Bool first_ls [static]

flag for first ls : mount if set

Definition at line 295 of file fat_example.c.

Referenced by main().

U8 i_str = 0 [static]

string length

Definition at line 297 of file fat_example.c.

Referenced by fat_example_build_cmd(), and fat_example_parse_cmd().

char par_str1[MAX_FILE_PATH_LENGTH] [static]

string for first arg

Definition at line 302 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

char par_str2[MAX_FILE_PATH_LENGTH] [static]

string for second arg

Definition at line 304 of file fat_example.c.

Referenced by fat_example_parse_cmd(), and main().

char str_buff[MAX_FILE_PATH_LENGTH] [static]

buffer for command line

Definition at line 307 of file fat_example.c.

Referenced by main().


Generated on Fri Feb 19 02:28:56 2010 for AVR32 - FAT Services by  doxygen 1.5.5