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

📄 main.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:

  /*-------------------------------------------------------------------*/
  /* Determine validity of both files. Ensure at least one is valid.   */
  /*-------------------------------------------------------------------*/
  valid1 = valid(fid1);
  valid2 = valid(fid2);
  if ((valid1 == FALSE) && (valid2 == FALSE))
    error("Neither file is valid!");

  /*-------------------------------------------------------------------*/
  /* If both files are valid, the most recent is the boot file.        */
  /*-------------------------------------------------------------------*/
  if (valid1 && valid2)
  {
    struct stat buf1, buf2;

    /*-----------------------------------------------------------------*/
    /* Read the file state info for both files.                        */
    /*-----------------------------------------------------------------*/
    if (fstat(fid1, &buf1))
      error("Unable to fstat() exe1");
    if (fstat(fid2, &buf2))
      error("Unable to fstat() exe2");

    /*-----------------------------------------------------------------*/
    /* Use the most recently modified file as the boot file.           */
    /*-----------------------------------------------------------------*/
    if (buf1.st_mtime > buf2.st_mtime)
    {
      boot = "exe1";
      upgrade = "exe2";
    }
    else
    {
      boot = "exe2";
      upgrade = "exe1";
    }
  }

  /*-------------------------------------------------------------------*/
  /* Else, the valid one is the boot and the other is the download.    */
  /*-------------------------------------------------------------------*/
  else
  {
    if (valid1)
    {
      boot = "exe1";
      upgrade = "exe2";
    }
    else
    {
      boot = "exe2";
      upgrade = "exe1";
    }
  }

  /*-------------------------------------------------------------------*/
  /* Close both files.                                                 */
  /*-------------------------------------------------------------------*/
  close(fid1);
  close(fid2);

  /*-------------------------------------------------------------------*/
  /* Download a new upgrade file.                                      */
  /*-------------------------------------------------------------------*/
  path = getcwd(NULL, 0);
  printf("Using \"%s%s\" as the boot file ", path, boot);
  printf("and \"%s%s\" as the download file\n", path, upgrade);
  free(path);
  download(upgrade);

  /*-------------------------------------------------------------------*/
  /* Delete the old boot file.                                         */
  /*-------------------------------------------------------------------*/
  if (unlink(boot))
    error("Unable to delete the old boot file!");

  /*-------------------------------------------------------------------*/
  /* Unmount the volume.                                               */
  /*-------------------------------------------------------------------*/
  if (unmount(vol_name))
    error("Unable to unmount flash volume!");
}

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

/***********************************************************************/
/*        main: Application Entry Point                                */
/*                                                                     */
/***********************************************************************/
void main(ui32 unused)
{
  int i, prep_req = FALSE;

  /*-------------------------------------------------------------------*/
  /* 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())
    error("Unable to initialize flash file system!");

  /*-------------------------------------------------------------------*/
  /* Prompt for command to initialize flash rather than start test.    */
  /*-------------------------------------------------------------------*/
  printf("\nStarting powerloss recovery test\n"
         "Press 'I' in 2 seconds to initialize flash:");
  for (i = 0; i < 40; ++i)
  {
    if (TtyIoctl(stdin, TTY_KB_HIT) && (getCmdKey() == 'I'))
    {
      printf(" I");
      prep_req = TRUE;
      break;
    }
    SysWait50ms();
  }
  putchar('\n');

  /*-------------------------------------------------------------------*/
  /* If requested, prepare the initial image.                          */
  /*-------------------------------------------------------------------*/
  if (prep_req)
  {
#if INC_NOR_FS
    prepare("nor");
#endif
#if INC_NAND_FS
    prepare("nand");
#endif
    printf("Finished formatting and installing \"exe1\"\n");
  }

  /*-------------------------------------------------------------------*/
  /* Simulate network downloads in an infinite loop. The object is to  */
  /* interrupt this loop by removing power at random points and ensure */
  /* that recovery is successful. A fatal error occurs if there is not */
  /* at least one valid file found during recovery.                    */
  /*-------------------------------------------------------------------*/
  for (;;)
  {
#if INC_NOR_FS
    boot("nor");
#endif
#if INC_NAND_FS
    boot("nand");
#endif
  }
}

/***********************************************************************/
/*  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();
}

/***********************************************************************/
/*     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 + -