//-----------------------------------------------------------------------------
// fl_attach_media: 
//-----------------------------------------------------------------------------
int fl_attach_media(fn_diskio_read rd, fn_diskio_write wr)
{
	int res;

	// If first call to library, initialise
	CHECK_FL_INIT();

	_fs.disk_io.read_sector = rd;
	_fs.disk_io.write_sector = wr;

	// Initialise FAT parameters
	if ((res = fatfs_init(&_fs)) != FAT_INIT_OK)
	{
		#ifdef FAT_PRINT
		   FAT_PRINT("FAT_FS: Error could not load FAT details!  Formatting...\r\n");
        #endif

		if(fl_format() != 1)
		{
			#ifdef FAT_PRINT
			   FAT_PRINT("FAT_FS:  Could not format");
			#endif
			res = FAT_INIT_COULD_NOT_FORMAT;
			return res;
		}
	}

	_filelib_valid = 1;
	return FAT_INIT_OK;
}
Example #2
0
static void test_root_entries_print()
{
    FAT_PRINT("Root Entries info :\n");
	int ret = 0;
    const char * file_name = "a.img.flp";

    //1. alloc mem
	size_t re_size = 224 * 32;
	uchar * pre = (uchar *) malloc(sizeof(uchar) * re_size);
	assert(pre);

    //2. read dev with root directory sectors 
    DECLARE_DEVICE_R(fdev, file_name);
    ret = fat_dev_read(&fdev, 19*512, (uchar*)pre, re_size);
	assert(ret == re_size);
    fat_dev_destroy(&fdev);

    //3. print the entries
	fat_root_entries_print(pre, re_size);

    const char * fn = "A.TXT";
    FAT_DEBUG("find the file name is [%s]\n", fn);
    fat_dentry_t  * ptr =fat_find_entry_in_sector(pre, re_size, fn);
    if(NULL != ptr){
        FAT_DEBUG("find the dentry with file_name 0x%X\n", ptr);
    }
    else{
        FAT_DEBUG("can not find the dentry with file_name [%s]\nn",fn);
    }

    //4. free the mem 
	free(pre); pre = NULL;
    FAT_PRINT("Finished......\n\n");
}
void fl_listdirectory(const char *path)
{
	struct fs_dir_list_status dirstat;
//	int filenumber = 0;

	// If first call to library, initialise
	CHECK_FL_INIT();

	FL_LOCK(&_fs);

	FAT_PRINT(("\r\nNo.             Filename\r\n"));

	if (fl_list_opendir(path, &dirstat))
	{
		struct fs_dir_ent dirent;

		while (fl_list_readdir(&dirstat, &dirent))
		{
			FAT_PRINT(dirent.filename);
			if (dirent.is_dir)
			{
				FAT_PRINT(" <DIR>");
			}
			FAT_PRINT("\r\n");
		}
	}

	FL_UNLOCK(&_fs);
}
Example #4
0
void test_device()
{
    //1. lll......
    FAT_PRINT("test read...\n");
    test_r();

    FAT_PRINT("test write...\n");
    test_w();

    FAT_PRINT("test read&write...\n");
    test_rw();
}
Example #5
0
void test_w(){
   const char * file_name = "w_test_file";

   uchar buf[6] = {0x64, 0x12, 0x24, };
   fat_dev_t fdv_f;
   fat_dev_create_file(&fdv_f, file_name);
   fat_dev_write(&fdv_f, 0, buf, 3);

   FAT_PRINT("%02x %02x %02x\n", buf[3], buf[4], buf[5]);
   fat_dev_read(&fdv_f, 0, buf + 3, 3);
   FAT_PRINT("%02x %02x %02x\n", buf[3], buf[4], buf[5]);
   fat_dev_destroy(&fdv_f);

}
Example #6
0
void test_r(){
   const char * file_name = "r_test_file";
   fat_dev_t fdv_f;
   fat_dev_init_r(&fdv_f, file_name);

   uchar buf[128] = {0,};
   int ret =fat_dev_read(&fdv_f, 0, buf, sizeof(buf));
   assert(ret <= 128 && ret >= 0  );

   FAT_PRINT("%s\n", buf);
   FAT_PRINT("%02x %02x %02x\n", buf[79], buf[80], buf[81]);

   fat_dev_destroy(&fdv_f);
}
//-----------------------------------------------------------------------------
// fatfs_show_details: Show the details about the filesystem
//-----------------------------------------------------------------------------
void fatfs_show_details(struct fatfs *fs)
{
	FAT_PRINT("\r\nCurrent Disc FAT details\r\n------------------------\r\nRoot Dir First Cluster = ");
	FAT_PRINT("0x");
	FAT_PRINT_INT32(fs->rootdir_first_cluster);
	FAT_PRINT("\r\nFAT Begin LBA = ");
	FAT_PRINT("0x");
	FAT_PRINT_INT32(fs->fat_begin_lba);
	FAT_PRINT("\r\nCluster Begin LBA = ");
	FAT_PRINT("0x");
	FAT_PRINT_INT32(fs->cluster_begin_lba);
	FAT_PRINT("\r\nSectors Per Cluster = ");
	FAT_PRINT_CHAR(fs->sectors_per_cluster);
	FAT_PRINT("\r\n\r\nFormula for conversion from Cluster num to LBA is;");
	FAT_PRINT("\r\nLBA = (cluster_begin_lba + ((Cluster_Number-2)*sectors_per_cluster)))\r\n");
}
//-----------------------------------------------------------------------------
// fl_init: Initialise library
//-----------------------------------------------------------------------------
int fl_init(void)
{	
	int i;

	// Add all file objects to free list
	for (i=0;i<FATFS_MAX_OPEN_FILES;i++)
	{
		_files[i].next = _free_file_list;
		_free_file_list = &_files[i];
	}

	// Initialize media
	//if(!_filelib_init)
	_filelib_init = media_init();

    int ret = FAT_INIT_MEDIA_ACCESS_ERROR;
	
    if(_filelib_init)
	{
		// Attach media access functions to library
		if ((ret = fl_attach_media(media_read, media_write)) != FAT_INIT_OK)
		{
			#ifdef FAT_PRINT
			FAT_PRINT("Failed to attach media. ERROR = ");
			FAT_PRINT_INT8(ret);
			FAT_PRINT("\n");
			#endif

			return ret; 
		}
	
		#ifdef FAT_PRINT
		else
			FAT_PRINT("Filesystem OK.\n");
		#endif
	}

    return ret;
}
Example #9
0
void test_rw()
{
    // set_up();
    const char * f_file = "a.img.flp";

    const char * t_file = "b.img.flp";
    fat_dev_t fdv_f;
    fat_dev_init_rw(&fdv_f, f_file);

    //read a file with content
    uchar  buf[2048] = {0,};
    fat_dev_read(&fdv_f, 0, buf, sizeof(buf));
    FAT_PRINT("jmp: %02x %02x %02x\n", buf[0], buf[1], buf[2]);
    fat_dev_destroy(&fdv_f);

    //write to another file
    fat_dev_t fdv_t;
    fat_dev_create_file(&fdv_t, t_file);
    fat_dev_write(&fdv_t, 0, buf, sizeof(buf));
    FAT_PRINT("jmp: %02x %02x %02x\n", buf[0], buf[1], buf[2]);

    fat_dev_destroy(&fdv_t);
//    tear_down();
}