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

📄 start.c

📁 嵌入式文件系统 ucfs.包含全部源代码
💻 C
字号:
/*
**********************************************************************
*                          Micrium, Inc.
*                      949 Crestview Circle
*                     Weston,  FL 33327-1848
*
*                            uC/FS
*
*             (c) Copyright 2001 - 2003, Micrium, Inc.
*                      All rights reserved.
*
***********************************************************************

----------------------------------------------------------------------
File        : Start.c
Purpose     : Sample program for using the file system
---------------------------END-OF-HEADER------------------------------
*/

#include "fs_api.h"
#include "fs_fat.h"
#include <string.h>
#include <stdio.h>

/*********************************************************************
*
*             Global variables
*
**********************************************************************
*/

static FS_FILE * _pFile;
static char _acBuffer[0x100];

static const char _sDefaultText[] = "This text was written on your default device.\n";
#if FS_USE_RAMDISK_DRIVER
  static const char _sRAMText[] = "This text was written on your RAM disk.\n";
#endif

/*********************************************************************
*
*             Local functions
*
**********************************************************************
*/

/*********************************************************************
*
*             _WriteFile
*
*  This routine demonstrates, how to create and write to a file
*  using the file system.
*/

static void _WriteFile(const char *pName, const char *pTxt) {
  int x;
  /* create file */
  FS_X_Log("Writing file ");
  FS_X_Log(pName);
  FS_X_Log("\n");
  _pFile = FS_FOpen(pName, "w");
  if (_pFile) {
    /* write to file */
    x = FS_FWrite(pTxt,1, strlen(pTxt), _pFile);
    /* all data written ? */
    if (x != (int)strlen(pTxt)) {
      /* check, why not all data was written */
      x = FS_FError(_pFile);
      sprintf(_acBuffer, "Not all bytes written: %s.\n", FS_ErrorNo2Text(x));
      FS_X_ErrorOut(_acBuffer);
    }
    /* close file */
    FS_FClose(_pFile);
  }
  else {
    sprintf(_acBuffer, "Unable to create file %s\n", pName);
    FS_X_ErrorOut(_acBuffer);
  }
}


/*********************************************************************
*
*             _DumpFile
*
* This routine demonstrates, how to open and read from a file using
* the file system.
*/

static void _DumpFile(const char *pName) {
  int x;
  /* open file */
  _pFile = FS_FOpen(pName,"r");
  FS_X_Log("Contents of file ");
  FS_X_Log(pName);
  FS_X_Log("\n");
  if (_pFile) {
    /* read until EOF has been reached */
    do {
      x = FS_FRead(_acBuffer, 1, sizeof(_acBuffer) - 1, _pFile);
      _acBuffer[x]=0;
      if (x) {
        FS_X_Log(_acBuffer);
      }
    } while (x);
    /* check, if there is no more data, because of EOF */
    x = FS_FError(_pFile);
    if (x != FS_ERR_EOF) {
      /* there was a problem during read operation */
      sprintf(_acBuffer, "Error during read operation: %s\n", FS_ErrorNo2Text(x));
      FS_X_ErrorOut(_acBuffer);
    }
    /* close file */
    FS_FClose(_pFile);
  }
  else {
    sprintf(_acBuffer, "Unable to open file %s.\n", pName);
    FS_X_ErrorOut(_acBuffer);
  }
  FS_X_Log("\n");
}


/*********************************************************************
*
*             _ShowDirectory
*
* This routine demonstrates, how to read a directory.
*/

#if FS_POSIX_DIR_SUPPORT

static void _ShowDirectory(const char * pName) {
  FS_DIR *pDir;
  FS_DIRENT *pDirEnt;
  pDir = FS_OpenDir(pName); /* Open root directory of default device */
  if (pDir) {
    do {
      char acName[20];
      FS_U8 Attr;
      pDirEnt = FS_ReadDir(pDir);
      FS_DirEnt2Name(pDirEnt, acName);
      FS_DirEnt2Attr(pDirEnt, &Attr);
      if ((void*)pDirEnt == NULL) {
        break; /* No more files */
      }
      sprintf(_acBuffer," %s %s Attributes: %s%s%s%s Size: %lu\n", acName,
                          (Attr & FS_ATTR_DIRECTORY) ? "(Dir)" : " ",
                          (Attr & FS_ATTR_ARCHIVE)   ? "A" : "-",
                          (Attr & FS_ATTR_READ_ONLY) ? "R" : "-",
                          (Attr & FS_ATTR_HIDDEN)    ? "H" : "-",
                          (Attr & FS_ATTR_SYSTEM)    ? "S" : "-",
                          FS_DirEnt2Size(pDirEnt));
      FS_X_Log(_acBuffer);
    } while (1);
    FS_CloseDir(pDir);
  } else {
    FS_X_ErrorOut("Unable to open directory\n");
  }
  FS_X_Log("\n");
}
#endif /* FS_POSIX_DIR_SUPPORT */


/*********************************************************************
*
*             _ShowFree
*
*  This routine demonstrates, how to read disk space information.
*/
#if (FS_FAT_DISKINFO)
static void _ShowFree(const char *pDevice) {
  FS_U32 FreeSpace;
  FS_U32 TotalSize;
  TotalSize = FS_GetTotalSpace(pDevice, 0);
  if (TotalSize == 0) {
    FS_X_ErrorOut("Invalid drive specified\n");
    return;
  }
  FreeSpace = FS_GetFreeSpace(pDevice, 0);

  FS_X_Log("Disk information of ");
  FS_X_Log((*pDevice == 0) ? "default device" : pDevice);
  FS_X_Log("\n");
  if (TotalSize <= 0x7fff) {
    sprintf(_acBuffer, "  Total space on disk: %d bytes\nFree space: %d bytes\n", (int)TotalSize, (int)FreeSpace);
  } else if (TotalSize <= 0x2000000) {
    TotalSize >>= 10;
    FreeSpace >>= 10;
    sprintf(_acBuffer, "  Total space on disk: %d kb\nFree space: %d Kb\n", (int)TotalSize, (int)FreeSpace);
  } else {
    TotalSize >>= 20;
    FreeSpace >>= 20;
    sprintf(_acBuffer, "  Total space on disk: %d Mb\nFree space: %d Mb\n", (int)TotalSize, (int)FreeSpace);
  }
  FS_X_Log(_acBuffer);
  FS_X_Log("\n");
}
#endif

/*********************************************************************
*
*             Global functions
*
**********************************************************************
*/



/*********************************************************************
*
*             MainTask
*/

void MainTask(void);
void MainTask(void) {
#if FS_USE_RAMDISK_DRIVER     /* Use the RAM driver if it has been configured */
  _WriteFile("ram:\\ram.txt", _sRAMText);    /* create file 'ram.txt' on your default RAM disk */
  #if FS_FAT_DISKINFO
    _ShowFree("ram:");                         /* display disk space information of the RAM disk */
  #endif
  _DumpFile("ram:\\ram.txt");                /* dump file 'ram.txt' on you default RAM disk */
  #if FS_POSIX_DIR_SUPPORT
    _ShowDirectory("ram:");                  /* show root directory of RAM disk */
  #endif
#endif
  _WriteFile("default.txt", _sDefaultText);  /* create file 'default.txt' on your default device */
#if FS_FAT_DISKINFO
  _ShowFree("");                             /* display disk space information of the default device */
#endif
  _DumpFile("default.txt");                  /* dump file 'default.txt' on you default device */
#if FS_POSIX_DIR_SUPPORT
  _ShowDirectory("");                        /* show root directory of default device */
#endif /* FS_POSIX_DIR_SUPPORT */
  FS_X_Log("Finished...\n");
#ifndef WIN32
  while (1) {
  }
#endif
}


/*************************** End of file ****************************/

⌨️ 快捷键说明

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