文件系统是操作系统用于明确磁盘或分区上的文件的方法和数据结构;即在磁盘上组织文件的方法。MiCO 为开发者提供在 flash 上进行文件操作的功能,包括:文件创建,读写,关闭,查找,存储等。MiCO 文件系统功能 API 接口包括:
更详细的 API 参数及返回值说明,可参考: doxygen 文档,具体示例代码查看 MiCO SDK 中 demo。
示例实现:
fatfs.c
#include "mico.h"
#include "ff_gen_drv.h"
#include "sflash_diskio.h"
#define os_fatfs_log(M, ...) custom_log("FATFS", M, ##__VA_ARGS__)
FATFS SFLASHFatFs; /* File system object for flash disk logical drive */
FIL SFLASHFile; /* File object */
char SFLASHPath[4]; /* SFLASH logical drive path */
void test_fatfs( )
{
FRESULT err;
filesystem_info fatfs_info;
uint32_t byteswritten, bytesread;
char filename[] = "MiCO.txt";
uint8_t wtext[] = "hello MiCO!";
uint8_t rtext[100];
FATFS_LinkDriver( &SFLASHDISK_Driver, SFLASHPath );
err = f_mount( &SFLASHFatFs, (TCHAR const*) SFLASHPath, 0 );
require_noerr( err, exit );
do
{
fatfs_info = fatfs_get_info( (uint8_t*) SFLASHPath );
if ( fatfs_info.total_space == 0 )
{
os_fatfs_log("filesystem free space is %d, need format", fatfs_info.total_space);
os_fatfs_log("start format filesystem");
err = f_mkfs( (TCHAR const*) SFLASHPath, 0, _MAX_SS );
}
} while ( fatfs_info.total_space == 0 );
os_fatfs_log("filesystem total space is %dKB, free space is %dKB", fatfs_info.total_space, fatfs_info.free_space);
err = f_open( &SFLASHFile, filename, FA_CREATE_ALWAYS | FA_WRITE );
require_noerr( err, exit );
err = f_write( &SFLASHFile, wtext, sizeof(wtext), (void *) &byteswritten );
if ( (byteswritten == 0) || (err != FR_OK) )
{
os_fatfs_log("fatfs write error %d", err);
goto exit;
}
os_fatfs_log("fatfs write file, name:%s, data:%s", filename, wtext);
err = f_close( &SFLASHFile );
require_noerr( err, exit );
err = f_open( &SFLASHFile, filename, FA_READ );
require_noerr( err, exit );
err = f_read( &SFLASHFile, rtext, sizeof(rtext), (void *) &bytesread );
if ( (bytesread == 0) || (err != FR_OK) )
{
os_fatfs_log("fatfs read error %d", err);
goto exit;
}
os_fatfs_log("fatfs read file, name:%s, data:%s", filename, rtext);
err = f_close( &SFLASHFile );
require_noerr( err, exit );
exit:
FATFS_UnLinkDriver( SFLASHPath );
}
int application_start( void )
{
test_fatfs( );
return 0;
}
上一篇:4.4 Json 格式解析
下一篇:4.2 Http 通信
版权所有 © 2017 - 2018 MXCHIP授权代理商 - 深圳市博易特智能科技有限公司 粤ICP备17063559号
服务热线:0755-23733662 Email:info@mxchip.cc