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

📄 main.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
    return -1;
  }
  if (freadFFS(Buf, 1, 800, stream) != 800)
  {
    perror("error reading from file!");
    return -1;
  }
  if (freadFFS(Buf, 1, 323, stream) != 323)
  {
    perror("error reading from file!");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Close the file.                                                   */
  /*-------------------------------------------------------------------*/
  if (fcloseFFS(stream))
  {
    perror("error closing file!");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Rename file.                                                      */
  /*-------------------------------------------------------------------*/
  if (renameFFS("write.txt", "sooner.txt"))
  {
    perror("error renaming file!");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Remove the file.                                                  */
  /*-------------------------------------------------------------------*/
  if (removeFFS("sooner.txt"))
  {
    perror("error removing file!");
    return -1;
  }

  return 0;
}

/***********************************************************************/
/*     hexDump: Display string in ASCII and in hexadecimal             */
/*                                                                     */
/***********************************************************************/
static void hexDump(char *data, int len)
{
  int i;
  ui8 ch;

  /*-------------------------------------------------------------------*/
  /* Print ASCII value.                                                */
  /*-------------------------------------------------------------------*/
  for (i = 0; i < len; ++i)
  {
    ch = data[i];
    if (!isprint(ch))
      ch = '.';
    putchar(ch);
  }
  printf("     ");

  /*-------------------------------------------------------------------*/
  /* Print hex value.                                                  */
  /*-------------------------------------------------------------------*/
  for (i = 0; i < len; ++i)
    printf(" %02X", data[i]);
  putchar('\n');
}

/***********************************************************************/
/*   overwrite: Test file overwriting and truncation                   */
/*                                                                     */
/***********************************************************************/
static int overwrite(char *vol_name)
{
  int i;
  void *fp;
  char string1[4] = {'a', 'b', 'c', 0};
  char string2[8] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 0};
  char string3[10];

  /*-------------------------------------------------------------------*/
  /* Initialize file if it already exists.                             */
  /*-------------------------------------------------------------------*/
  fp = fopenFFS("xyz", "rb");
  if (fp)
  {
    memset(string3, 0, sizeof(string3));
    i = freadFFS(string3, 1, 10, fp);
    printf("\n\rFS_Read count = %d\n", i);
    hexDump(string3, i);
    fcloseFFS(fp);
  }

  /*-------------------------------------------------------------------*/
  /* Write 8 bytes to file.                                            */
  /*-------------------------------------------------------------------*/
  fp = fopenFFS("xyz", "wb");
  if (fp == NULL)
  {
    perror("error creating file!");
    return -1;
  }
  i = fwriteFFS(string2, 1, 8, fp);
  printf("\n\rFS_Write count = %d\n", i);
  if (i != 8)
  {
    perror("error writing file!");
    return -1;
  }
  fcloseFFS(fp);
  hexDump(string2, i);

  /*-------------------------------------------------------------------*/
  /* Read everything from file.                                        */
  /*-------------------------------------------------------------------*/
  fp = fopenFFS("xyz", "rb");
  memset(string3, 0, sizeof(string3));
  i = freadFFS(string3, 1, 10, fp);
  printf("\n\rFS_Read2 count = %d\n", i);
  if (i != 8)
  {
    perror("error reading file!");
    return -1;
  }
  hexDump(string3, i);
  fcloseFFS(fp);

  /*-------------------------------------------------------------------*/
  /* Write 4 bytes to file.                                            */
  /*-------------------------------------------------------------------*/
  fp = fopenFFS("xyz", "wb");
  i = fwriteFFS(string1, 1, 4 , fp);
  printf("\n\rFS_Write2 count = %d\n", i);
  if (i != 4)
  {
    perror("error writing file!");
    return -1;
  }
  fcloseFFS(fp);
  hexDump(string1, i);

  /*-------------------------------------------------------------------*/
  /* Read everything from file.                                        */
  /*-------------------------------------------------------------------*/
  fp = fopenFFS("xyz", "rb");
  memset(string3, 0, sizeof(string3));
  i = freadFFS(string3, 1, 10, fp);
  printf("\n\rFS_Read3 count = %d\n", i);
  if (i != 4)
  {
    perror("error reading file!");
    return -1;
  }
  hexDump(string3, i);
  fcloseFFS(fp);
  return 0;
}

/***********************************************************************/
/* Global Function Definitions                                         */
/***********************************************************************/

/***********************************************************************/
/*        main: Application entry point                                */
/*                                                                     */
/***********************************************************************/
void main(ui32 unused)
{
  int i;
  char *vol_name;

  /*-------------------------------------------------------------------*/
  /* Lower interrupt mask and start scheduling.                        */
  /*-------------------------------------------------------------------*/
  OsStart();

  /*-------------------------------------------------------------------*/
  /* Lower our priority.                                               */
  /*-------------------------------------------------------------------*/
  taskSetPri(RunningTask, 20);

  /*-------------------------------------------------------------------*/
  /* Null CWD variables and assign this task's user and group ID.      */
  /*-------------------------------------------------------------------*/
  FsSetId(0, 0);
  FsSaveCWD(0, 0);

  /*-------------------------------------------------------------------*/
  /* Initialize the file system.                                       */
  /*-------------------------------------------------------------------*/
  if (InitFFS())
    SysFatalError(errno);

#if NUM_RFS_VOLS
  /*-------------------------------------------------------------------*/
  /* Add the TargetRFS volume.                                         */
  /*-------------------------------------------------------------------*/
  if (RfsAddVol(RFS_NAME))
    SysFatalError(errno);
#endif

  /*-------------------------------------------------------------------*/
  /* Run the test programs in an endless loop.                         */
  /*-------------------------------------------------------------------*/
  for (i = 0;; ++i)
  {
    printf("\n****************** Test Loop %u ******************\n", i);
    vol_name = Volume[i % (sizeof(Volume) / sizeof(char *))];
    printf("Beginning \"%s\" test\n", vol_name);

    /*-----------------------------------------------------------------*/
    /* Format the volume.                                              */
    /*-----------------------------------------------------------------*/
    if (format(vol_name))
      SysFatalError(errno);

    /*-----------------------------------------------------------------*/
    /* Mount the volume and change to its root directory.              */
    /*-----------------------------------------------------------------*/
    if (mount(vol_name))
      SysFatalError(errno);
    if (chdir(vol_name))
      SysFatalError(errno);

    /*-----------------------------------------------------------------*/
    /* Test lseek().                                                   */
    /*-----------------------------------------------------------------*/
    if (lseek_test(vol_name))
      SysFatalError(errno);

    /*-----------------------------------------------------------------*/
    /* Test file overwriting and truncation.                           */
    /*-----------------------------------------------------------------*/
    if (overwrite(vol_name))
      SysFatalError(errno);

    /*-----------------------------------------------------------------*/
    /* Test various file system stdio calls.                           */
    /*-----------------------------------------------------------------*/
    if (stdio_test(vol_name))
      SysFatalError(errno);

    /*-----------------------------------------------------------------*/
    /* Unmount volume before starting next test.                       */
    /*-----------------------------------------------------------------*/
    if (unmount(vol_name))
      SysFatalError(errno);

    /*-----------------------------------------------------------------*/
    /* Announce that the test was successful.                          */
    /*-----------------------------------------------------------------*/
    printf("\"%s\" test completed successfully\n", vol_name);
  }
}

/***********************************************************************/
/*  OsIdleTask: kernel idle task                                       */
/*                                                                     */
/***********************************************************************/
void OsIdleTask(ui32 unused)
{
  /*-------------------------------------------------------------------*/
  /* Loop forever. Feel free to add code here, but nothing that could  */
  /* block (Don't try a printf()).                                     */
  /*-------------------------------------------------------------------*/
  for (;;) OsAuditStacks();
}

/***********************************************************************/
/*   AppModule: Application interface to software module manager       */
/*                                                                     */
/*       Input: req = module request code                              */
/*              ... = additional parameters specific to request        */
/*                                                                     */
/***********************************************************************/
void *AppModule(int req, ...)
{
  switch (req)
  {
#if PCCARD_SUPPORT
    va_list ap;
    pccSocket *sock;

    case kCardInserted:
      printf("Known card inserted\n");

      /*---------------------------------------------------------------*/
      /* Use va_arg mechanism to fetch pointer to command string.      */
      /*---------------------------------------------------------------*/
      va_start(ap, req);
      sock = va_arg(ap, pccSocket *);
      va_end(ap);

      /*---------------------------------------------------------------*/
      /* Parse card type.                                              */
      /*---------------------------------------------------------------*/
      switch (sock->card.type)
      {
        case PCCC_ATA:
          printf("ATA Drive\n");
          if (pccAddATA(sock, "ata"))
          {
            perror("pccAddATA");
            break;
          }
          break;

        default:
          printf("unk type = %d\n", sock->card.type);
          break;
      }
      break;

    case kCardRemoved:
      printf("Card removed\n");
      break;
#endif
  }

  return NULL;
}

/***********************************************************************/
/*     FsGetId: Get process user and group ID                          */
/*                                                                     */
/*      Inputs: uid = place to store user ID                           */
/*              gid = place to store group ID                          */
/*                                                                     */
/***********************************************************************/
void FsGetId(uid_t *uid, gid_t *gid)
{
  ui32 id = taskGetReg(RunningTask, ID_REG);

  *uid = (uid_t)id;
  *gid = id >> 16;
}

/***********************************************************************/
/*     FsSetId: Set process user and group ID                          */
/*                                                                     */
/*      Inputs: uid = user ID                                          */
/*              gid = group ID                                         */
/*                                                                     */
/***********************************************************************/
void FsSetId(uid_t uid, gid_t gid)
{
  ui32 id = (gid << 16) | uid;

  taskSetReg(RunningTask, ID_REG, id);
}

/***********************************************************************/
/*   FsSaveCWD: Save per-task current working directory state          */
/*                                                                     */
/*      Inputs: word1 = 1 of 2 words to save                           */
/*              word2 = 2 of 2 words to save                           */
/*                                                                     */
/***********************************************************************/
void FsSaveCWD(ui32 word1, ui32 word2)
{
  taskSetReg(RunningTask, CWD_WD1, word1);
  taskSetReg(RunningTask, CWD_WD2, word2);
}

/***********************************************************************/
/*   FsReadCWD: Read per-task current working directory state          */
/*                                                                     */
/*     Outputs: word1 = 1 of 2 words to retrieve                       */
/*              word2 = 2 of 2 words to retrieve                       */
/*                                                                     */
/***********************************************************************/
void FsReadCWD(ui32 *word1, ui32 *word2)
{
  *word1 = taskGetReg(RunningTask, CWD_WD1);
  *word2 = taskGetReg(RunningTask, CWD_WD2);
}

⌨️ 快捷键说明

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