📄 wearleveling.c
字号:
/*
**********************************************************************
* Micrium, Inc.
* 949 Crestview Circle
* Weston, FL 33327-1848
*
* uC/FS
*
* (c) Copyright 2001 - 2003, Micrium, Inc.
* All rights reserved.
*
***********************************************************************
----------------------------------------------------------------------
File : WearLeveling.c
Purpose : Sample program demonstrating wear leveling
---------------------------END-OF-HEADER------------------------------
*/
#include "fs_api.h"
#include <stdio.h>
void MainTask(void);
#if FS_USE_FLASH_DRIVER // This application can be used with flash driver only
/*********************************************************************
*
* Static functions
*
**********************************************************************
*/
/*********************************************************************
*
* _CreateBigFile()
*/
static void _CreateBigFile(void) {
int i;
FS_FILE * pFile;
FS_U8 acData[0x20000];
//
// Create static dummy file first
//
FS_X_Log("Creating static dummy file\n");
pFile = FS_FOpen("Dummy.txt", "w");
if (!pFile) {
FS_ErrorOut("Could not write file");
}
for (i = 0; i < 5; i++) {
FS_FWrite(acData, sizeof(acData), 1, pFile);
}
FS_FClose(pFile);
}
/*********************************************************************
*
* _Stress()
*/
static void _StressRandom(void) {
int i;
FS_FILE * pFile;
FS_FLASH_DISK_INFO DiskInfo;
FS_U8 acData[0x20000];
FS_FLASH_GetDiskInfo(0, &DiskInfo);
FS_X_Log("Writing into file\n");
pFile = FS_FOpen("Test.txt", "w");
if (!pFile) {
FS_ErrorOut("Could not write file");
}
for (i = 0; i < 5000; i++) {
FS_FWrite("Test file\r\n", 11, 1, pFile);
}
FS_FClose(pFile);
FS_X_Log("Writing sector");
for (i = 0; i < 5000; i++) {
FS_IoCtl("flash:",FS_CMD_WRITE_SECTOR, DiskInfo.NumLogSectors -1, acData);
}
}
/*********************************************************************
*
* _Init()
*/
static void _Init(void) {
if (FS_IoCtl("flash:", FS_CMD_REQUIRES_FORMAT, 0, 0)) {
FS_IoCtl("flash:", FS_CMD_FORMAT_LOW_LEVEL, 0, 0); /* Erase & Low-level format the flash */
FS_IoCtl("flash:", FS_CMD_FORMAT_AUTO, 0, 0); /* High-level format the flash */
}
}
/*********************************************************************
*
* _ShowInfo()
*/
static void _ShowInfo(void) {
char ac[200];
int i;
FS_FLASH_DISK_INFO DiskInfo;
FS_FLASH_GetDiskInfo(0, &DiskInfo);
FS_X_Log("******************************************************************\n");
sprintf(ac, "Number of phys. sectors: %d\n", DiskInfo.NumPhysSectors);
FS_X_Log(ac);
sprintf(ac, "Number of log. sectors: %d (%dkB raw)\n", DiskInfo.NumLogSectors, DiskInfo.NumLogSectors /2);
FS_X_Log(ac);
sprintf(ac, "Used log. sectors: %d (%dkB raw)\n", DiskInfo.NumUsedSectors, DiskInfo.NumUsedSectors /2);
FS_X_Log(ac);
for (i = 0; i < (int)DiskInfo.NumPhysSectors; i++) {
FS_FLASH_SECTOR_INFO SectorInfo;
FS_FLASH_GetSectorInfo(0, i, &SectorInfo);
sprintf(ac, "Sector %.2d: %dkB @ %.8X - ECnt: %d Log.sectors: (T=%d, U=%d, F=%d, E=%d)\n",
i, SectorInfo.Size / 1024, SectorInfo.Off, SectorInfo.EraseCnt
, SectorInfo.NumUsedSectors + SectorInfo.NumFreeSectors + SectorInfo.NumEraseableSectors
, SectorInfo.NumUsedSectors, SectorInfo.NumFreeSectors, SectorInfo.NumEraseableSectors
);
FS_X_Log(ac);
}
}
/*********************************************************************
*
* Global functions
*
**********************************************************************
*/
/*********************************************************************
*
* MainTask()
*/
void MainTask(void) {
int i;
/* buffers used to read and write to file */
char acWriteText[20] = "Hello World";
_Init();
_ShowInfo();
_CreateBigFile();
for (i = 0; i < 10; i++) {
_ShowInfo();
_StressRandom();
}
}
#else
void MainTask(void) {
FS_X_Log("This application requires the flash driver\n");
}
#endif
/*************************** End of file ****************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -