dfs_elm.c 15.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * File      : dfs_elm.c
 * This file is part of Device File System in RT-Thread RTOS
 * COPYRIGHT (C) 2008-2011, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2008-02-22     QiuYi        The first version.
 * 2011-10-08     Bernard      fixed the block size in statfs.
B
bernard.xiong@gmail.com 已提交
14
 * 2011-11-23     Bernard      fixed the rename issue.
15
 * 2012-07-26     aozima       implement ff_memalloc and ff_memfree.
16
 * 2012-12-19     Bernard      fixed the O_APPEND and lseek issue.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 */
 
#include <rtthread.h>
#include "ffconf.h"
#include "ff.h"

/* ELM FatFs provide a DIR struct */
#define HAVE_DIR_STRUCTURE

#include <dfs_fs.h>
#include <dfs_def.h>

static rt_device_t disk[_VOLUMES] = {0};

static int elm_result_to_dfs(FRESULT result)
{
	int status = DFS_STATUS_OK;

	switch (result)
	{
	case FR_OK:
		break;

	case FR_NO_FILE:
	case FR_NO_PATH:
	case FR_NO_FILESYSTEM:
		status = -DFS_STATUS_ENOENT;
		break;

	case FR_INVALID_NAME:
		status = -DFS_STATUS_EINVAL;
		break;

	case FR_EXIST:
	case FR_INVALID_OBJECT:
		status = -DFS_STATUS_EEXIST;
		break;

	case FR_DISK_ERR:
	case FR_NOT_READY:
	case FR_INT_ERR:
		status = -DFS_STATUS_EIO;
		break;

	case FR_WRITE_PROTECTED:
	case FR_DENIED:
		status = -DFS_STATUS_EROFS;
		break;

	case FR_MKFS_ABORTED:
		status = -DFS_STATUS_EINVAL;
		break;

	default:
		status = -1;
		break;
	}

	return status;
}

78
int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
79 80 81
{
	FATFS *fat;
	FRESULT result;
82
	BYTE  index;
83 84 85 86 87 88 89 90 91

	/* handle RT-Thread device routine */
	for (index = 0; index < _VOLUMES; index ++)
	{
		if (disk[index] == RT_NULL)
		{
			break;
		}
	}
92 93
	if (index == _VOLUMES)
		return -DFS_STATUS_ENOSPC;
94 95 96 97

	/* get device */
	disk[index] = fs->dev_id;

98
	fat = (FATFS *)rt_malloc(sizeof(FATFS));
99 100 101 102 103 104 105 106
	if (fat == RT_NULL)
	{
		return -1;
	}

	/* mount fatfs, always 0 logic driver */
	result = f_mount(index, fat);
	if (result == FR_OK)
107 108 109 110
	{
		char drive[8];
		DIR * dir;

111
		rt_snprintf(drive, sizeof(drive), "%d:/", index);
112 113 114 115 116 117 118 119 120 121 122 123 124
		dir = (DIR *)rt_malloc(sizeof(DIR));
		if (dir == RT_NULL)
			return -DFS_STATUS_ENOMEM;

		/* open the root directory to test whether the fatfs is valid */
		result = f_opendir(dir, drive);
		if (result != FR_OK)
		{
			rt_free(dir);
			return elm_result_to_dfs(result);
		}
		rt_free(dir);

125
		fs->data = fat;
126
	}
127 128 129 130 131 132 133 134 135
	else
	{
		rt_free(fat);
		return elm_result_to_dfs(result);
	}

	return 0;
}

136
int dfs_elm_unmount(struct dfs_filesystem *fs)
137 138 139
{
	FATFS *fat;
	FRESULT result;
140
	BYTE index;
141

142
	fat = (FATFS *)fs->data;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

	RT_ASSERT(fat != RT_NULL);

	/* find the device index and then umount it */
	for (index = 0; index < _VOLUMES; index ++)
	{
		if (disk[index] == fs->dev_id)
		{
			result = f_mount(index, RT_NULL);

			if (result == FR_OK)
			{
				fs->data = RT_NULL;
				disk[index] = RT_NULL;
				rt_free(fat);
				return DFS_STATUS_OK;
			}
		}
	}

	return -DFS_STATUS_ENOENT;
}

166
int dfs_elm_mkfs(const char *device_name)
167 168 169 170 171 172 173 174 175
{
	BYTE drv;
	rt_device_t dev;
	FRESULT result;

	/* find device name */
	for (drv = 0; drv < _VOLUMES; drv ++)
	{
		dev = disk[drv];
G
goprife@gmail.com 已提交
176
		if (dev != RT_NULL && rt_strncmp(dev->parent.name, device_name, RT_NAME_MAX) == 0)
177 178 179 180
		{
			/* 1: no partition table */
			/* 0: auto selection of cluster size */
			result = f_mkfs(drv, 1, 0);
181
			if (result != FR_OK)
182 183 184 185 186 187 188 189 190 191 192 193 194 195
			{
				rt_kprintf("format error\n");
				return elm_result_to_dfs(result);
			}

			return DFS_STATUS_OK;
		}
	}

	/* can't find device driver */
	rt_kprintf("can not find device driver: %s\n", device_name);
	return -DFS_STATUS_EIO;
}

196
int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf)
197 198 199 200 201 202 203 204 205
{
	FATFS *f;
	FRESULT res;
	char driver[4];
	DWORD fre_clust, fre_sect, tot_sect;

	RT_ASSERT(fs != RT_NULL);
	RT_ASSERT(buf != RT_NULL);

206
	f = (FATFS *)fs->data;
207 208 209

	rt_snprintf(driver, sizeof(driver), "%d:", f->drv);
	res = f_getfree(driver, &fre_clust, &f);
210 211
	if (res) 
		return elm_result_to_dfs(res);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

	/* Get total sectors and free sectors */
	tot_sect = (f->n_fatent - 2) * f->csize;
	fre_sect = fre_clust * f->csize;

	buf->f_bfree = fre_sect;
	buf->f_blocks = tot_sect;
#if _MAX_SS != 512
	buf->f_bsize = f->ssize;
#else
    buf->f_bsize = 512;
#endif

	return 0;
}

228
int dfs_elm_open(struct dfs_fd *file)
229
{
230
	FIL *fd;
231 232 233 234 235 236 237 238 239 240
	BYTE mode;
	FRESULT result;
	char *drivers_fn;

#if (_VOLUMES > 1)
	int vol;
	extern int elm_get_vol(FATFS *fat);

	/* add path for ELM FatFS driver support */
	vol = elm_get_vol((FATFS *)file->fs->data);
241 242
	if (vol < 0)
		return -DFS_STATUS_ENOENT;
243
	drivers_fn = rt_malloc(256);
244 245
	if (drivers_fn == RT_NULL)
		return -DFS_STATUS_ENOMEM;
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

	rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path);
#else
	drivers_fn = file->path;
#endif

	if (file->flags & DFS_O_DIRECTORY)
	{
		DIR *dir;

		if (file->flags & DFS_O_CREAT)
		{
			result = f_mkdir(drivers_fn);
			if (result != FR_OK)
			{
#if _VOLUMES > 1
				rt_free(drivers_fn);
#endif
				return elm_result_to_dfs(result);
			}
		}

		/* open directory */
		dir = (DIR *)rt_malloc(sizeof(DIR));
		if (dir == RT_NULL)
		{
#if _VOLUMES > 1
			rt_free(drivers_fn);
#endif
			return -DFS_STATUS_ENOMEM;
		}

		result = f_opendir(dir, drivers_fn);
#if _VOLUMES > 1
		rt_free(drivers_fn);
#endif
		if (result != FR_OK)
		{
			rt_free(dir);
			return elm_result_to_dfs(result);
		}

		file->data = dir;
		return DFS_STATUS_OK;
	}
	else
	{
		mode = FA_READ;

295 296 297 298
		if (file->flags & DFS_O_WRONLY)
			mode |= FA_WRITE;
		if ((file->flags & DFS_O_ACCMODE) & DFS_O_RDWR)
			mode |= FA_WRITE;
299
		/* Opens the file, if it is existing. If not, a new file is created. */
300 301
		if (file->flags & DFS_O_CREAT)
			mode |= FA_OPEN_ALWAYS;
302
		/* Creates a new file. If the file is existing, it is truncated and overwritten. */
303 304
		if (file->flags & DFS_O_TRUNC)
			mode |= FA_CREATE_ALWAYS;
305
		/* Creates a new file. The function fails if the file is already existing. */
306 307
		if (file->flags & DFS_O_EXCL)
			mode |= FA_CREATE_NEW;
308 309

		/* allocate a fd */
310
		fd = (FIL *)rt_malloc(sizeof(FIL));
311 312
		if (fd == RT_NULL)
		{
313 314 315
#if _VOLUMES > 1
			rt_free(drivers_fn);
#endif
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
			return -DFS_STATUS_ENOMEM;
		}

		result = f_open(fd, drivers_fn, mode);
#if _VOLUMES > 1
		rt_free(drivers_fn);
#endif
		if (result == FR_OK)
		{
			file->pos  = fd->fptr;
			file->size = fd->fsize;
			file->data = fd;

			if (file->flags & DFS_O_APPEND)
			{
331 332 333
				/* seek to the end of file */
				f_lseek(fd, fd->fsize);
				file->pos = fd->fptr;
334 335 336 337 338 339 340 341 342 343 344 345 346
			}
		}
		else
		{
			/* open failed, return */
			rt_free(fd);
			return elm_result_to_dfs(result);
		}
	}

	return DFS_STATUS_OK;
}

347
int dfs_elm_close(struct dfs_fd *file)
348 349 350 351 352 353
{
	FRESULT result;

	result = FR_OK;
	if (file->type == FT_DIRECTORY)
	{
354
		DIR *dir;
355

356
		dir = (DIR *)(file->data);
357 358 359 360 361 362 363
		RT_ASSERT(dir != RT_NULL);

		/* release memory */
		rt_free(dir);
	}
	else if (file->type == FT_REGULAR)
	{
364 365
		FIL *fd;
		fd = (FIL *)(file->data);
366 367 368 369 370 371 372 373 374 375 376 377 378
		RT_ASSERT(fd != RT_NULL);

		result = f_close(fd);
		if (result == FR_OK)
		{
			/* release memory */
			rt_free(fd);
		}
	}

	return elm_result_to_dfs(result);
}

379
int dfs_elm_ioctl(struct dfs_fd *file, int cmd,	void *args)
380 381 382 383
{
	return -DFS_STATUS_ENOSYS;
}

384
int dfs_elm_read(struct dfs_fd *file, void *buf, rt_size_t len)
385
{
386
	FIL *fd;
387 388 389 390 391 392 393 394
	FRESULT result;
	UINT byte_read;

	if (file->type == FT_DIRECTORY)
	{
		return -DFS_STATUS_EISDIR;
	}

395
	fd = (FIL *)(file->data);
396 397 398 399 400
	RT_ASSERT(fd != RT_NULL);

	result = f_read(fd, buf, len, &byte_read);
	/* update position */
	file->pos  = fd->fptr;
401 402
	if (result == FR_OK)
		return byte_read;
403 404 405 406

	return elm_result_to_dfs(result);
}

407
int dfs_elm_write(struct dfs_fd *file, const void *buf, rt_size_t len)
408
{
409
	FIL *fd;
410 411 412 413 414 415 416 417
	FRESULT result;
	UINT byte_write;

	if (file->type == FT_DIRECTORY)
	{
		return -DFS_STATUS_EISDIR;
	}

418
	fd = (FIL *)(file->data);
419 420 421 422 423 424
	RT_ASSERT(fd != RT_NULL);

	result = f_write(fd, buf, len, &byte_write);
	/* update position and file size */
	file->pos  = fd->fptr;
	file->size = fd->fsize;
425 426
	if (result == FR_OK)
		return byte_write;
427 428 429 430

	return elm_result_to_dfs(result);
}

431
int dfs_elm_flush(struct dfs_fd *file)
432
{
433
	FIL *fd;
434 435
	FRESULT result;

436
	fd = (FIL *)(file->data);
437 438 439 440 441 442
	RT_ASSERT(fd != RT_NULL);

	result = f_sync(fd);
	return elm_result_to_dfs(result);
}

443
int dfs_elm_lseek(struct dfs_fd *file, rt_off_t offset)
444
{
wuyangyong's avatar
wuyangyong 已提交
445
	FRESULT result = FR_OK;
446 447
	if (file->type == FT_REGULAR)
	{
448
		FIL *fd;
449 450

		/* regular file type */
451
		fd = (FIL *)(file->data);
452 453 454 455 456 457
		RT_ASSERT(fd != RT_NULL);
		
		result = f_lseek(fd, offset);
		if (result == FR_OK)
		{
			/* return current position */
458
			file->pos = fd->fptr;
459 460 461 462 463 464
			return fd->fptr;
		}
	}
	else if (file->type == FT_DIRECTORY)
	{
		/* which is a directory */
465
		DIR *dir;
466

467
		dir = (DIR *)(file->data);
468 469 470 471 472 473 474 475 476 477 478 479 480 481
		RT_ASSERT(dir != RT_NULL);

		result = f_seekdir(dir, offset / sizeof(struct dirent));
		if (result == FR_OK)
		{
			/* update file position */
			file->pos = offset;
			return file->pos;
		}
	}

	return elm_result_to_dfs(result);
}

482
int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
483
{
484
	DIR *dir;
485 486 487
	FILINFO fno;
	FRESULT result;
	rt_uint32_t index;
488
	struct dirent *d;
489

490
	dir = (DIR *)(file->data);
491 492 493 494
	RT_ASSERT(dir != RT_NULL);

	/* make integer count */
	count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
495 496
	if (count == 0)
		return -DFS_STATUS_EINVAL;
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

#if _USE_LFN
	/* allocate long file name */
	fno.lfname = rt_malloc(256);
	fno.lfsize = 256;
#endif

	index = 0;
	while (1)
	{
		char *fn;

		d = dirp + index;

		result = f_readdir(dir, &fno);
512 513
		if (result != FR_OK || fno.fname[0] == 0)
			break;
514 515 516 517 518 519 520 521

#if _USE_LFN
		fn = *fno.lfname? fno.lfname : fno.fname;
#else
		fn = fno.fname;
#endif

		d->d_type = DFS_DT_UNKNOWN;
522 523 524 525
		if (fno.fattrib & AM_DIR)
			d->d_type = DFS_DT_DIR;
		else
			d->d_type = DFS_DT_REG;
526

527
		d->d_namlen = (rt_uint8_t)rt_strlen(fn);
528 529 530 531
		d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
		rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);

		index ++;
532
		if (index * sizeof(struct dirent) >= count)
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
			break;
	}

#if _USE_LFN
	rt_free(fno.lfname);
#endif

	if (index == 0)
		return elm_result_to_dfs(result);

	file->pos += index * sizeof(struct dirent);

	return index * sizeof(struct dirent);
}

548
int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path)
549 550 551 552 553 554 555 556 557 558
{
	FRESULT result;

#if _VOLUMES > 1
	int vol;
	char *drivers_fn;
	extern int elm_get_vol(FATFS *fat);

	/* add path for ELM FatFS driver support */
	vol = elm_get_vol((FATFS *)fs->data);
559 560
	if (vol < 0)
		return -DFS_STATUS_ENOENT;
561
	drivers_fn = rt_malloc(256);
562 563
	if (drivers_fn == RT_NULL)
		return -DFS_STATUS_ENOMEM;
564 565 566 567 568 569 570 571 572 573 574 575 576 577

	rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
#else
	const char *drivers_fn;
	drivers_fn = path;
#endif

	result = f_unlink(drivers_fn);
#if _VOLUMES > 1
	rt_free(drivers_fn);
#endif
	return elm_result_to_dfs(result);
}

578
int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath)
579 580 581 582
{
	FRESULT result;

#if _VOLUMES > 1
B
bernard.xiong@gmail.com 已提交
583 584
	char *drivers_oldfn;
	const char *drivers_newfn;
585 586 587 588 589
	int vol;
	extern int elm_get_vol(FATFS *fat);

	/* add path for ELM FatFS driver support */
	vol = elm_get_vol((FATFS *)fs->data);
590 591
	if (vol < 0)
		return -DFS_STATUS_ENOENT;
592 593

	drivers_oldfn = rt_malloc(256);
594 595
	if (drivers_oldfn == RT_NULL)
		return -DFS_STATUS_ENOMEM;
B
bernard.xiong@gmail.com 已提交
596
	drivers_newfn = newpath;
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

	rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, oldpath);
#else
	const char *drivers_oldfn, *drivers_newfn;

	drivers_oldfn = oldpath;
	drivers_newfn = newpath;
#endif

	result = f_rename(drivers_oldfn, drivers_newfn);
#if _VOLUMES > 1
	rt_free(drivers_oldfn);
#endif
	return elm_result_to_dfs(result);
}

613
int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
614 615 616 617 618 619 620 621 622 623 624
{
	FILINFO file_info;
	FRESULT result;

#if _VOLUMES > 1
	int vol;
	char *drivers_fn;
	extern int elm_get_vol(FATFS *fat);

	/* add path for ELM FatFS driver support */
	vol = elm_get_vol((FATFS *)fs->data);
625 626
	if (vol < 0)
		return -DFS_STATUS_ENOENT;
627
	drivers_fn = rt_malloc(256);
628 629
	if (drivers_fn == RT_NULL)
		return -DFS_STATUS_ENOMEM;
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

	rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
#else
	const char *drivers_fn;
	drivers_fn = path;
#endif

#if _USE_LFN
	/* allocate long file name */
	file_info.lfname = rt_malloc(256);
	file_info.lfsize = 256;
#endif

	result = f_stat(drivers_fn, &file_info);
#if _VOLUMES > 1
	rt_free(drivers_fn);
#endif
	if (result == FR_OK)
	{
		/* convert to dfs stat structure */
650
		st->st_dev = 0;
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

		st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
		DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
		if (file_info.fattrib & AM_DIR)
		{
			st->st_mode &= ~DFS_S_IFREG;
			st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
		}
		if (file_info.fattrib & AM_RDO)
			st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH);

		st->st_size  = file_info.fsize;
		st->st_mtime = file_info.ftime;
		st->st_blksize = 512;
	}

#if _USE_LFN
	rt_free(file_info.lfname);
#endif

	return elm_result_to_dfs(result);
}

static const struct dfs_filesystem_operation dfs_elm =
{
	"elm",
677
	DFS_FS_FLAG_DEFAULT,
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
	dfs_elm_mount,
	dfs_elm_unmount,
	dfs_elm_mkfs,
	dfs_elm_statfs,

	dfs_elm_open,
	dfs_elm_close,
	dfs_elm_ioctl,
	dfs_elm_read,
	dfs_elm_write,
	dfs_elm_flush,
	dfs_elm_lseek,
	dfs_elm_getdents,
	dfs_elm_unlink,
	dfs_elm_stat,
	dfs_elm_rename,
};

int elm_init(void)
{
    /* register fatfs file system */
    dfs_register(&dfs_elm);

	return 0;
}

/*
 * RT-Thread Device Interface for ELM FatFs
 */
#include "diskio.h"

709
/* Initialize a Drive */
710
DSTATUS disk_initialize(BYTE drv)
711 712 713 714 715
{
	return 0;
}

/* Return Disk Status */
716
DSTATUS disk_status(BYTE drv)
717 718 719 720 721
{
	return 0;
}

/* Read Sector(s) */
722
DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
723 724 725 726 727 728 729 730 731 732 733 734 735 736
{
	rt_size_t result;
	rt_device_t device = disk[drv];

	result = rt_device_read(device, sector, buff, count);
	if (result == count)
	{
		return RES_OK;
	}

	return RES_ERROR;
}

/* Write Sector(s) */
737
DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
738 739 740 741 742 743 744 745 746 747 748 749 750 751
{
	rt_size_t result;
	rt_device_t device = disk[drv];

	result = rt_device_write(device, sector, buff, count);
	if (result == count)
	{
		return RES_OK;
	}

	return RES_ERROR;
}

/* Miscellaneous Functions */
752
DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
753 754 755
{
	rt_device_t device = disk[drv];

756 757
	if (device == RT_NULL)
		return RES_ERROR;
758 759 760 761 762 763 764 765

	if (ctrl == GET_SECTOR_COUNT)
	{
		struct rt_device_blk_geometry geometry;

		rt_memset(&geometry, 0, sizeof(geometry));
		rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);

766 767 768
		*(DWORD *)buff = geometry.sector_count;
		if (geometry.sector_count == 0)
			return RES_ERROR;
769 770 771 772 773 774 775 776
	}
	else if (ctrl == GET_SECTOR_SIZE)
	{
		struct rt_device_blk_geometry geometry;

		rt_memset(&geometry, 0, sizeof(geometry));
		rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);

777
		*(WORD *)buff = (WORD)(geometry.bytes_per_sector);
778 779 780 781 782 783 784 785
	}
	else if (ctrl == GET_BLOCK_SIZE) /* Get erase block size in unit of sectors (DWORD) */
	{
		struct rt_device_blk_geometry geometry;

		rt_memset(&geometry, 0, sizeof(geometry));
		rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);

786
		*(DWORD *)buff = geometry.block_size/geometry.bytes_per_sector;
787
	}
788
	else if (ctrl == CTRL_SYNC)
G
goprife@gmail.com 已提交
789
	{
790
		rt_device_control(device, RT_DEVICE_CTRL_BLK_SYNC, RT_NULL);
G
goprife@gmail.com 已提交
791 792 793 794 795 796
	}
	else if (ctrl == CTRL_ERASE_SECTOR)
	{
		rt_device_control(device, RT_DEVICE_CTRL_BLK_ERASE, buff);
	}
	
797 798 799
	return RES_OK;
}

800
rt_time_t get_fattime(void)
801 802 803 804 805
{
	return 0;
}

#if _FS_REENTRANT
806
int ff_cre_syncobj(BYTE drv, _SYNC_t *m)
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
{
    char name[8];
    rt_mutex_t mutex;

    rt_snprintf(name, sizeof(name), "fat%d", drv);
    mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
    if (mutex != RT_NULL)
    {
        *m = mutex;
        return RT_TRUE;
    }

    return RT_FALSE;
}

int ff_del_syncobj(_SYNC_t m)
{
    rt_mutex_delete(m);

    return RT_TRUE;
}

int ff_req_grant(_SYNC_t m)
{
831 832
    if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK)
		return RT_TRUE;
833 834 835 836 837 838 839 840 841 842

    return RT_FALSE;
}

void ff_rel_grant(_SYNC_t m)
{
	rt_mutex_release(m);
}

#endif
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

/* Memory functions */
#if _USE_LFN == 3
/* Allocate memory block */
void* ff_memalloc (UINT size)
{
    return rt_malloc(size);
}

/* Free memory block */
void ff_memfree (void* mem)
{
    rt_free(mem);
}
#endif /* _USE_LFN == 3 */