Пример #1
0
void LOS_init(void)
{	
	if (media_init() == 0)
	{
        mSD_status = SD_ERR_INIT;
		printf("ERROR: Media init failed\n");
		return;
	}
	
	// Attach media access functions to library
    if (fl_attach_media(media_read, media_write) != FAT_INIT_OK)
    {
        mSD_status = SD_ERR_ATTACH;
		printf("ERROR: Media attach failed\n");
		return; 
    }

#ifndef __LOCAL_STORE_ACCEL_MODE__
	BQ_init(&LOS_queue, LOS_write_buffer, sizeof(LOS_write_buffer));
#endif
    
	LOS_tid = registerTask(LOS_proc, 0, 0, 0xFF);

#ifdef FRAM_ENABLED	
    FRAM_WR_init();
#endif
	
	// Delete File
	int i = 0;
    while ((i = fl_remove("/file.txt")) == -1);
	if (!i)
	{
		//printf("ERROR: Delete file failed\n");
    }
}
Пример #2
0
int32_t file_mount(mountpoint_t* mountpoint)
{
    // TODO: how can we pass the mountpoint to media_read and write?
    if(fl_attach_media(media_read, media_write) != FAT_INIT_OK)
    {
        return -1;
    }
    return 0;
}
Пример #3
0
int fat_init(void) {
  // Initialise File IO Library
  print_dbg("\r\n beginning FAT library init.");
  fl_init();
  print_dbg("\r\n finished FAT library init.");
  // Attach media access functions to library
  if ( fl_attach_media((fn_diskio_read)media_read, (fn_diskio_write)media_write) != FAT_INIT_OK ) {
    print_dbg("\r\n failed to attach media access functions to fat_io_lib \r\n");
    return 1;
  } else {
    print_dbg("\r\n attached media access functions to fat_io_lib");
    return 0;
  }
}
Пример #4
0
int media_access_init(int drive)
{
	int ret;
	io_floppy_timeout = 0;

	ret = init_fdc(drive);
	if( ret != ERR_NO_ERROR )
		return ret;

	#ifdef DEBUG
	dbg_printf("init_fdc Done\n");
	#endif

	ret = media_init();
	if( ret == ERR_NO_ERROR )
	{
		#ifdef DEBUG
		dbg_printf("media_init done\n");
		#endif

		// Initialise File IO Library
		fl_init();

		#ifdef DEBUG
		dbg_printf("fl_init done\n");
		#endif

		/* Attach media access functions to library*/
		if (fl_attach_media(media_read, media_write) != FAT_INIT_OK)
		{
			return -ERR_MEDIA_ATTACH;
		}

		#ifdef DEBUG
		dbg_printf("fl_attach_media done\n");
		#endif

		return ERR_NO_ERROR;
	}

	return ret;
}
//-----------------------------------------------------------------------------
// 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;
}
Пример #6
0
void fs_init() {
  mmchs_init();
  fl_init();
  fl_attach_media(fs_read, fs_write);
}
Пример #7
0
//-----------------------------------------------------------------
// Main: Test bench file to create 5 files with psuedo random
// sequences in of varying lengths - read them back and complete
// then remove them.
//-----------------------------------------------------------------
void main()
{
	int i,j,x;

	FL_FILE * files[5];
	FL_FILE *readFile;
	char filenames[5][260];
	BYTE fileData[5][10000];
	BYTE readBuffer[10000];
	int fileLengths[5];
	BYTE *massiveData;
	time_t timeStart, timeEnd;

#define TESTFILES 6
	char *testfile[] = { "X:\\1", "X:\\1.bin", "X:\\12345678.321",
						 "X:\\mylongfilename", "X:\\mylongfilename.bin",
						 "X:\\the Quick Brown Fox jumped over the lazy dog.elf.binfile.jar"	};

	srand(time(NULL));

	// Initialise
	FAT32_InitDrive();

	fl_init();

	if (fl_attach_media(FAT_ReadSector, FAT_WriteSector) != FAT_INIT_OK)
		return;

	// List directory
	fl_listdirectory("C:\\");
//	return ;

test_start:

	// Generate 5 random files
	memset(filenames, 0x00, 260*5);
	for (j=0;j<5;j++)
	{
		// Length
		fileLengths[j] = GetRandom(9999);

		// Data
		for (x=0;x<fileLengths[j];x++)
			fileData[j][x] = (BYTE)GetRandom(255);

		// Names
		sprintf(filenames[j], "X:\\Auto Generated Filename Number %d", j+1);
	}

	// Create some files
	for (j=0;j<5;j++)
	{
		printf("Creating File: %s [%d bytes]\n", filenames[j], fileLengths[j]);

		// Create File
		files[j] = fl_fopen(filenames[j], "w");
		if (files[j]!=NULL)
		{
			if (fl_fwrite(fileData[j], 1, fileLengths[j], files[j])!=fileLengths[j])
			{
				printf("ERROR: File Write Block Failed File %s Length %d\n", filenames[j], fileLengths[j]);
				fl_assert(0);
			}
		}
		else
		{
			printf("ERROR: Error Creating File %s\n", filenames[j]);
			fl_assert(0);
		}

		fl_fclose(files[j]);

		// Clear buffer
		for (i=0;i<sizeof(readBuffer);i++)
			readBuffer[i] = 0;

		// Verify File
		readFile = fl_fopen(filenames[j], "r");
		if (readFile!=NULL)
		{
			int failed = FALSE;

			printf("File %s Read Check (fread whole file) [%d bytes]\n", filenames[j], fileLengths[j]);

			if (fl_fread(readBuffer, 1, fileLengths[j], readFile)!=fileLengths[j])
			{
				printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]);
				fl_assert(0);
			}

			for (i=0;i<fileLengths[j];i++)
				if ( fileData[j][i] != (BYTE)readBuffer[i] )
					failed = TRUE;

			if (failed)
			{
				printf("ERROR: File %s Data Verify Failed\n", filenames[j]);
				fl_assert(0);
			}
		}
		fl_fclose(readFile);

		// Clear buffer
		for (i=0;i<sizeof(readBuffer);i++)
			readBuffer[i] = 0;

		// Verify File using fgetc
		readFile = fl_fopen(filenames[j], "r");
		if (readFile!=NULL)
		{
			int failed = FALSE;

			printf("File %s Read Check (fgetc) [%d bytes]\n", filenames[j], fileLengths[j]);

			i = 0;
			while (i < fileLengths[j])
			{
				int res = fl_fgetc(readFile);
				if (res == -1)
					break;

				readBuffer[i++] = (BYTE)res;
			}

			if (i != fileLengths[j])
			{
				printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]);
				fl_assert(0);
			}

			for (i=0;i<fileLengths[j];i++)
				if ( fileData[j][i] != (BYTE)readBuffer[i] )
					failed = TRUE;

			if (failed)
			{
				printf("ERROR: File %s Data Verify Failed\n", filenames[j]);
				fl_assert(0);
			}
		}
		fl_fclose(readFile);

		// Clear buffer
		for (i=0;i<sizeof(readBuffer);i++)
			readBuffer[i] = 0;

		// Verify File chunks
		readFile = fl_fopen(filenames[j], "r");
		if (readFile!=NULL)
		{
			int failed = FALSE;

			printf("File %s Read Check (fread chunks) [%d bytes]\n", filenames[j], fileLengths[j]);

			i = 0;
			while (i < fileLengths[j])
			{
				int read_length = GetRandom(1025);

				if (read_length > (fileLengths[j] - i))
					read_length = fileLengths[j] - i;

				if (fl_fread(readBuffer + i, 1, read_length, readFile) != read_length)
				{
					printf("ERROR: File %s fread error\n", filenames[j]);
					fl_assert(0);
					break;
				}

				i += read_length;
			}

			if (i != fileLengths[j])
			{
				printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]);
				fl_assert(0);
			}

			for (i=0;i<fileLengths[j];i++)
				if ( fileData[j][i] != (BYTE)readBuffer[i] )
				{
					failed = TRUE;
					break;
				}

			if (failed)
			{
				printf("ERROR: File %s Data Verify Failed at %d\n", filenames[j], i);
				fl_assert(0);
			}
		}
		fl_fclose(readFile);

		// Delete File
		if (fl_remove(filenames[j])<0)
			printf("ERROR: Delete File %s Failed\n", filenames[j]);

		// Verify file is no longer present!
		readFile = fl_fopen(filenames[j], "r");
		if (readFile != NULL)
		{
			printf("ERROR: File %s still present after delete!\n", filenames[j]);
			fl_assert(0);
			fl_fclose(readFile);
		}
	}

	// Create folder
	fl_createdirectory("C:\\folder1");

#if 0

	// Create massive file
#define MASSIVE_FILE_LEN (1024 * 1024)
	printf("Creating Massive File [%d bytes]\n", MASSIVE_FILE_LEN);

	massiveData = malloc(MASSIVE_FILE_LEN);
	if (!massiveData)
	{
		printf("ERROR: Could not allocate memory for massive array!\n");
		fl_assert(0);
		fl_shutdown();
		return ;
	}

	// Create random data for file
	for (x=0;x<MASSIVE_FILE_LEN;x++)
		massiveData[x] = (BYTE)GetRandom(255);

	// Remove if it already exists!
	fl_remove("X:\\folder1\\massive file.bin");

	timeStart = time(NULL);

	// Create Large File
	readFile = fl_fopen("X:\\folder1\\massive file.bin", "w");
	if (readFile != NULL)
	{
		if (fl_fwrite(massiveData, 1, MASSIVE_FILE_LEN, readFile) != MASSIVE_FILE_LEN)
		{
			printf("ERROR: File Write Block Failed for massive file (Length %d)\n", MASSIVE_FILE_LEN);
			fl_assert(0);
		}
	}
	else
	{
		printf("ERROR: Error Creating massive file\n");
		fl_assert(0);
	}

	fl_fclose(readFile);

	// Verify Massive File
	readFile = fl_fopen("X:\\folder1\\massive file.bin", "r");
	if (readFile!=NULL)
	{
		int failed = FALSE;

		printf("File Massive File Read Check (fread whole file) [%d bytes]\n", MASSIVE_FILE_LEN);

		i = 0;
		while (i < MASSIVE_FILE_LEN)
		{
			int read_length = GetRandom(2048) + 128;

			if (read_length > (MASSIVE_FILE_LEN - i))
				read_length = MASSIVE_FILE_LEN - i;

			if (fl_fread(readBuffer, 1, read_length, readFile) != read_length)
			{
				printf("ERROR: File massive file fread error\n");
				fl_assert(0);
				break;
			}

			for (j=0;j<read_length;j++)
				if ( massiveData[i+j] != (BYTE)readBuffer[j] )
				{
					printf("ERROR: File Massive File Data Verify Failed at %d\n", i+j);
					fl_assert(0);
					break;
				}

			i += read_length;
		}

		if (i != MASSIVE_FILE_LEN)
		{
			printf("ERROR: File massive file Read Length Error %d\n", MASSIVE_FILE_LEN);
			fl_assert(0);
		}
	}
	fl_fclose(readFile);

	timeEnd = time(NULL);
	printf("Time taken %d seconds\n", (int)(timeEnd-timeStart));

	free(massiveData);
#endif

	// Filename test
	for (i=0;i<TESTFILES;i++)
	{
		// Create File
		readFile = fl_fopen(testfile[i], "w");
		if (readFile != NULL)
			;
		else
		{
			printf("ERROR: Error Creating File %s\n", testfile[i]);
			fl_assert(0);
		}

		fl_fputc(0, readFile);
		fl_fclose(readFile);

		readFile = fl_fopen(testfile[i], "r");
		assert(readFile);
		fl_fclose(readFile);
	}

	// List directory
	fl_listdirectory("C:\\");

	for (i=0;i<TESTFILES;i++)
	{
		// Delete File
		if (fl_remove(testfile[i])<0)
		{
			printf("ERROR: Delete File %s Failed\n", testfile[i]);
			fl_assert(0);
		}
	}

	fl_shutdown();

	printf("\r\nCompleted\r\n");

	// List directory
	fl_listdirectory("C:\\");
}