From b6879dd52657a5cfb5b4f54a0b5c6280b3904a94 Mon Sep 17 00:00:00 2001 From: batt Date: Fri, 19 Sep 2008 20:12:32 +0000 Subject: [PATCH] Add cache read API. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1823 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/fs/battfs.c | 13 +++++++++---- bertos/fs/battfs.h | 9 +++++++++ bertos/fs/battfs_test.c | 10 ++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/bertos/fs/battfs.c b/bertos/fs/battfs.c index 9c72f749..b766f625 100644 --- a/bertos/fs/battfs.c +++ b/bertos/fs/battfs.c @@ -408,6 +408,7 @@ bool battfs_init(struct BattFsSuper *disk) ASSERT(disk->read); ASSERT(disk->load); ASSERT(disk->bufferWrite); + ASSERT(disk->bufferRead); ASSERT(disk->save); ASSERT(disk->erase); ASSERT(disk->close); @@ -564,12 +565,16 @@ static size_t battfs_read(struct KFile *fd, void *_buf, size_t size) addr_offset = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN); read_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset)); - /* Flush current page if needed */ + /* Try to read from cache */ if (fdb->start[pg_offset] == fdb->disk->curr_page) - battfs_flushBuffer(fdb->disk); - + { + if (fdb->disk->bufferRead(fdb->disk, addr_offset, buf, read_len) != read_len) + { + #warning TODO set error? + } + } /* Read from disk */ - if (fdb->disk->read(fdb->disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len) + else if (fdb->disk->read(fdb->disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len) { #warning TODO set error? } diff --git a/bertos/fs/battfs.h b/bertos/fs/battfs.h index e0829917..f53bb916 100644 --- a/bertos/fs/battfs.h +++ b/bertos/fs/battfs.h @@ -143,6 +143,14 @@ typedef bool (*disk_page_load_t) (struct BattFsSuper *d, pgcnt_t page); */ typedef size_t (*disk_buffer_write_t) (struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t); +/** + * Type interface for disk pagebuffer read function. + * \a addr is the address inside the current loaded page, + * \a size the lenght to be read. + * \return the number of bytes read. + */ +typedef size_t (*disk_buffer_read_t) (struct BattFsSuper *d, pgaddr_t addr, void *buf, size_t); + /** * Type interface for disk page save function. * The disk should supply a buffer used for loading/saving pages. @@ -179,6 +187,7 @@ typedef struct BattFsSuper disk_page_read_t read; ///< Page read. disk_page_load_t load; ///< Page load. disk_buffer_write_t bufferWrite; ///< Buffer write. + disk_buffer_read_t bufferRead; ///< Buffer read. 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 e5f50f02..7c983aed 100644 --- a/bertos/fs/battfs_test.c +++ b/bertos/fs/battfs_test.c @@ -83,6 +83,15 @@ static size_t disk_buffer_write(struct BattFsSuper *d, pgaddr_t addr, const void return size; } +static size_t disk_buffer_read(struct BattFsSuper *d, pgaddr_t addr, void *buf, size_t size) +{ + TRACEMSG("addr:%d, size:%d", addr, size); + ASSERT(addr + size <= d->page_size); + memcpy(buf, &page_buffer[addr], size); + + return size; +} + static bool disk_page_load(struct BattFsSuper *d, pgcnt_t page) { TRACEMSG("page:%d", page); @@ -661,6 +670,7 @@ int battfs_testRun(void) disk.read = disk_page_read; disk.load = disk_page_load; disk.bufferWrite = disk_buffer_write; + disk.bufferRead = disk_buffer_read; disk.save = disk_page_save; disk.erase = disk_page_erase; disk.close = disk_close; -- 2.25.1