From: batt Date: Thu, 18 Sep 2008 16:42:55 +0000 (+0000) Subject: Switch to new disk write interface:the disk should now supply the write buffer. X-Git-Tag: 2.0.0~121 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=802cec6861e5f1988f23085b2a6849d86368628c;p=bertos.git Switch to new disk write interface:the disk should now supply the write buffer. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1813 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/fs/battfs.c b/bertos/fs/battfs.c index 6253af88..59b03ae4 100644 --- a/bertos/fs/battfs.c +++ b/bertos/fs/battfs.c @@ -156,8 +156,8 @@ static bool battfs_writeHeader(struct BattFsSuper *disk, pgcnt_t page, struct Ba * Header is actually a footer, and so * resides at page end. */ - if (disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN) - != BATTFS_HEADER_LEN) + if (!(disk->bufferWrite(disk, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN) + == BATTFS_HEADER_LEN && disk->save(disk, page))) { LOG_ERR("Error: page[%d]\n", page); return false; @@ -363,7 +363,9 @@ bool battfs_init(struct BattFsSuper *disk) /* Disk open must set all of these */ ASSERT(disk->read); - ASSERT(disk->write); + ASSERT(disk->load); + ASSERT(disk->bufferWrite); + ASSERT(disk->save); ASSERT(disk->erase); ASSERT(disk->close); ASSERT(disk->page_size); diff --git a/bertos/fs/battfs.h b/bertos/fs/battfs.h index ee104db8..d5afb523 100644 --- a/bertos/fs/battfs.h +++ b/bertos/fs/battfs.h @@ -124,13 +124,33 @@ typedef bool (*disk_open_t) (struct BattFsSuper *d); */ typedef size_t (*disk_page_read_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t); + /** - * Type interface for disk page write function. - * \a page is the page address, \a addr the address inside the page, + * Type interface for disk page load function. + * The disk should supply a buffer used for loading/saving pages. + * This has to be done by the disk driver because it knows memory details + * (e.g. some memories can have the buffer inside the memory itself). + * \a page is the page to be loaded from the disk in the buffer. + * \return true if ok, false on errors. + */ +typedef bool (*disk_page_load_t) (struct BattFsSuper *d, pgcnt_t page); + +/** + * Type interface for disk pagebuffer write function. + * \a addr is the address inside the current loaded page, * \a size the lenght to be written. * \return the number of bytes written. */ -typedef size_t (*disk_page_write_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t); +typedef size_t (*disk_buffer_write_t) (struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t); + +/** + * Type interface for disk page save function. + * The disk should supply a buffer used for loading/saving pages. + * For details \see disk_page_load_t. + * \a page is the page where the buffer will be written. + * \return true if ok, false on errors. + */ +typedef bool (*disk_page_save_t) (struct BattFsSuper *d, pgcnt_t page); /** * Type interface for disk page erase function. @@ -157,7 +177,9 @@ typedef struct BattFsSuper { disk_open_t open; ///< Disk init. disk_page_read_t read; ///< Page read. - disk_page_write_t write; ///< Page write. + disk_page_load_t load; ///< Page load. + disk_buffer_write_t bufferWrite; ///< Buffer write. + disk_page_save_t save; ///< Page save. disk_page_erase_t erase; ///< Page erase. disk_close_t close; ///< Disk deinit. diff --git a/bertos/fs/battfs_test.c b/bertos/fs/battfs_test.c index dad64712..0580495f 100644 --- a/bertos/fs/battfs_test.c +++ b/bertos/fs/battfs_test.c @@ -53,6 +53,7 @@ FILE *fp; const char test_filename[]="battfs_disk.bin"; +static uint8_t page_buffer[PAGE_SIZE]; static bool disk_open(struct BattFsSuper *d) { @@ -73,11 +74,25 @@ static size_t disk_page_read(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, return fread(buf, 1, size, fp); } -static size_t disk_page_write(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t size) +static size_t disk_buffer_write(struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t size) { //TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size); - fseek(fp, page * d->page_size + addr, SEEK_SET); - return fwrite(buf, 1, size, fp); + ASSERT(addr + size <= d->page_size); + memcpy(&page_buffer[addr], buf, size); + + return size; +} + +static bool disk_page_load(struct BattFsSuper *d, pgcnt_t page) +{ + fseek(fp, page * d->page_size, SEEK_SET); + return fread(page_buffer, 1, d->page_size, fp) == d->page_size; +} + +static bool disk_page_save(struct BattFsSuper *d, pgcnt_t page) +{ + fseek(fp, page * d->page_size, SEEK_SET); + return fwrite(page_buffer, 1, d->page_size, fp) == d->page_size; } static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page) @@ -526,7 +541,9 @@ int battfs_testRun(void) disk.open = disk_open; disk.read = disk_page_read; - disk.write = disk_page_write; + disk.load = disk_page_load; + disk.bufferWrite = disk_buffer_write; + disk.save = disk_page_save; disk.erase = disk_page_erase; disk.close = disk_close; test1(&disk);